libubus.h: add ubus_auto_shutdown()
[project/ubus.git] / ubusd.c
diff --git a/ubusd.c b/ubusd.c
index 8903105..f1f8ac7 100644 (file)
--- a/ubusd.c
+++ b/ubusd.c
@@ -17,6 +17,7 @@
 #ifdef FreeBSD
 #include <sys/param.h>
 #endif
+#include <syslog.h>
 #include <signal.h>
 #include <stdio.h>
 #include <unistd.h>
@@ -135,6 +136,9 @@ void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
 {
        int written;
 
+       if (ub->hdr.type != UBUS_MSG_MONITOR)
+               ubusd_monitor_message(cl, ub, true);
+
        if (!cl->tx_queue[cl->txq_cur]) {
                written = ubus_msg_writev(cl->sock.fd, ub, 0);
                if (written >= ub->len + sizeof(ub->hdr))
@@ -178,6 +182,7 @@ static void handle_client_disconnect(struct ubus_client *cl)
        while (ubus_msg_head(cl))
                ubus_msg_dequeue(cl);
 
+       ubusd_monitor_disconnect(cl);
        ubusd_proto_free_client(cl);
        if (cl->pending_msg_fd >= 0)
                close(cl->pending_msg_fd);
@@ -241,7 +246,7 @@ retry:
 
                fd_buf.fd = -1;
 
-               iov.iov_base = &cl->hdrbuf + offset;
+               iov.iov_base = ((char *) &cl->hdrbuf) + offset;
                iov.iov_len = sizeof(cl->hdrbuf) - offset;
 
                if (cl->pending_msg_fd < 0) {
@@ -296,6 +301,7 @@ retry:
                cl->pending_msg_fd = -1;
                cl->pending_msg_offset = 0;
                cl->pending_msg = NULL;
+               ubusd_monitor_message(cl, ub, false);
                ubusd_proto_receive_message(cl, ub);
                goto retry;
        }
@@ -350,11 +356,17 @@ static int usage(const char *progname)
 {
        fprintf(stderr, "Usage: %s [<options>]\n"
                "Options: \n"
+               "  -A <path>:           Set the path to ACL files\n"
                "  -s <socket>:         Set the unix domain socket to listen on\n"
                "\n", progname);
        return 1;
 }
 
+static void sighup_handler(int sig)
+{
+       ubusd_acl_load();
+}
+
 int main(int argc, char **argv)
 {
        const char *ubus_socket = UBUS_UNIX_SOCKET;
@@ -362,21 +374,26 @@ int main(int argc, char **argv)
        int ch;
 
        signal(SIGPIPE, SIG_IGN);
+       signal(SIGHUP, sighup_handler);
 
+       openlog("ubusd", LOG_PID, LOG_DAEMON);
        uloop_init();
 
-       while ((ch = getopt(argc, argv, "s:")) != -1) {
+       while ((ch = getopt(argc, argv, "A:s:")) != -1) {
                switch (ch) {
                case 's':
                        ubus_socket = optarg;
                        break;
+               case 'A':
+                       ubusd_acl_dir = optarg;
+                       break;
                default:
                        return usage(argv[0]);
                }
        }
 
        unlink(ubus_socket);
-       umask(0177);
+       umask(0111);
        server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL);
        if (server_fd.fd < 0) {
                perror("usock");
@@ -384,6 +401,7 @@ int main(int argc, char **argv)
                goto out;
        }
        uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER);
+       ubusd_acl_load();
 
        uloop_run();
        unlink(ubus_socket);