X-Git-Url: http://git.archive.openwrt.org/?p=project%2Fubus.git;a=blobdiff_plain;f=ubusd.c;h=5b1d52c808924c1e40eb3d699225db981ad9e1e3;hp=03215b33525f9011d0a681dfb185ae085292ccd4;hb=964adfdd3923ef1b54eb5c19487dd8e9e3f586b7;hpb=42bc27ae38d92f4fe11872b0f9d57f8d3578dcfe;ds=sidebyside diff --git a/ubusd.c b/ubusd.c index 03215b3..5b1d52c 100644 --- a/ubusd.c +++ b/ubusd.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2011 Felix Fietkau + * Copyright (C) 2011-2014 Felix Fietkau * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 2.1 @@ -12,11 +12,19 @@ */ #include +#include #include +#ifdef FreeBSD +#include +#endif +#include #include #include #include #include +#ifdef ENABLE_SYSTEMD +#include +#endif #include #include @@ -25,22 +33,10 @@ #include "ubusd.h" -static struct ubus_msg_buf *ubus_msg_unshare(struct ubus_msg_buf *ub) -{ - ub = realloc(ub, sizeof(*ub) + ub->len); - if (!ub) - return NULL; - - ub->refcount = 1; - memcpy(ub + 1, ub->data, ub->len); - ub->data = (void *) (ub + 1); - return ub; -} - static struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub) { if (ub->refcount == ~0) - return ubus_msg_unshare(ub); + return ubus_msg_new(ub->data, ub->len, false); ub->refcount++; return ub; @@ -58,6 +54,8 @@ struct ubus_msg_buf *ubus_msg_new(void *data, int len, bool shared) if (!ub) return NULL; + ub->fd = -1; + if (shared) { ub->refcount = ~0; ub->data = data; @@ -77,6 +75,9 @@ void ubus_msg_free(struct ubus_msg_buf *ub) switch (ub->refcount) { case 1: case ~0: + if (ub->fd >= 0) + close(ub->fd); + free(ub); break; default: @@ -87,14 +88,44 @@ void ubus_msg_free(struct ubus_msg_buf *ub) static int ubus_msg_writev(int fd, struct ubus_msg_buf *ub, int offset) { - struct iovec iov[2]; + static struct iovec iov[2]; + static struct { + struct cmsghdr h; + int fd; + } fd_buf = { + .h = { + .cmsg_len = sizeof(fd_buf), + .cmsg_level = SOL_SOCKET, + .cmsg_type = SCM_RIGHTS, + }, + }; + struct msghdr msghdr = { + .msg_iov = iov, + .msg_iovlen = ARRAY_SIZE(iov), + .msg_control = &fd_buf, + .msg_controllen = sizeof(fd_buf), + }; + + fd_buf.fd = ub->fd; + if (ub->fd < 0) { + msghdr.msg_control = NULL; + msghdr.msg_controllen = 0; + } if (offset < sizeof(ub->hdr)) { - iov[0].iov_base = ((char *) &ub->hdr) + offset; - iov[0].iov_len = sizeof(ub->hdr) - offset; + struct ubus_msghdr hdr; + + hdr.version = ub->hdr.version; + hdr.type = ub->hdr.type; + hdr.seq = cpu_to_be16(ub->hdr.seq); + hdr.peer = cpu_to_be32(ub->hdr.peer); + + iov[0].iov_base = ((char *) &hdr) + offset; + iov[0].iov_len = sizeof(hdr) - offset; iov[1].iov_base = (char *) ub->data; iov[1].iov_len = ub->len; - return writev(fd, iov, 2); + + return sendmsg(fd, &msghdr, 0); } else { offset -= sizeof(ub->hdr); return write(fd, ((char *) ub->data) + offset, ub->len - offset); @@ -115,6 +146,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)) @@ -158,7 +192,10 @@ 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); uloop_fd_delete(&cl->sock); close(cl->sock.fd); free(cl); @@ -168,6 +205,21 @@ static void client_cb(struct uloop_fd *sock, unsigned int events) { struct ubus_client *cl = container_of(sock, struct ubus_client, sock); struct ubus_msg_buf *ub; + static struct iovec iov; + static struct { + struct cmsghdr h; + int fd; + } fd_buf = { + .h = { + .cmsg_type = SCM_RIGHTS, + .cmsg_level = SOL_SOCKET, + .cmsg_len = sizeof(fd_buf), + } + }; + struct msghdr msghdr = { + .msg_iov = &iov, + .msg_iovlen = 1, + }; /* first try to tx more pending data */ while ((ub = ubus_msg_head(cl))) { @@ -202,10 +254,26 @@ retry: int offset = cl->pending_msg_offset; int bytes; - bytes = read(sock->fd, (char *)&cl->hdrbuf + offset, sizeof(cl->hdrbuf) - offset); + fd_buf.fd = -1; + + iov.iov_base = ((char *) &cl->hdrbuf) + offset; + iov.iov_len = sizeof(cl->hdrbuf) - offset; + + if (cl->pending_msg_fd < 0) { + msghdr.msg_control = &fd_buf; + msghdr.msg_controllen = sizeof(fd_buf); + } else { + msghdr.msg_control = NULL; + msghdr.msg_controllen = 0; + } + + bytes = recvmsg(sock->fd, &msghdr, 0); if (bytes < 0) goto out; + if (fd_buf.fd >= 0) + cl->pending_msg_fd = fd_buf.fd; + cl->pending_msg_offset += bytes; if (cl->pending_msg_offset < sizeof(cl->hdrbuf)) goto out; @@ -217,6 +285,9 @@ retry: if (!cl->pending_msg) goto disconnect; + cl->hdrbuf.hdr.seq = be16_to_cpu(cl->hdrbuf.hdr.seq); + cl->hdrbuf.hdr.peer = be32_to_cpu(cl->hdrbuf.hdr.peer); + memcpy(&cl->pending_msg->hdr, &cl->hdrbuf.hdr, sizeof(cl->hdrbuf.hdr)); memcpy(cl->pending_msg->data, &cl->hdrbuf.data, sizeof(cl->hdrbuf.data)); } @@ -239,8 +310,11 @@ retry: } /* accept message */ + ub->fd = cl->pending_msg_fd; + 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; } @@ -295,42 +369,77 @@ static int usage(const char *progname) { fprintf(stderr, "Usage: %s []\n" "Options: \n" + " -A : Set the path to ACL files\n" " -s : 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; + bool remove_socket = true; int ret = 0; int ch; +#ifdef ENABLE_SYSTEMD + int n_fds; +#endif 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); - server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL); - if (server_fd.fd < 0) { - perror("usock"); - ret = -1; +#ifdef ENABLE_SYSTEMD + n_fds = sd_listen_fds(1); + if (n_fds > 1) { + fprintf(stderr, "Too many file descriptors received.\n"); + ret = -1; goto out; + } else if (n_fds == 1) { + server_fd.fd = SD_LISTEN_FDS_START + 0; + fcntl(server_fd.fd, F_SETFD, fcntl(server_fd.fd, F_GETFD) | FD_CLOEXEC); + fcntl(server_fd.fd, F_SETFL, fcntl(server_fd.fd, F_GETFL) | O_NONBLOCK); + + remove_socket = false; + } else +#endif + { + unlink(ubus_socket); + umask(0111); + server_fd.fd = usock(USOCK_UNIX | USOCK_SERVER | USOCK_NONBLOCK, ubus_socket, NULL); + if (server_fd.fd < 0) { + perror("usock"); + ret = -1; + goto out; + } } uloop_fd_add(&server_fd, ULOOP_READ | ULOOP_EDGE_TRIGGER); + ubusd_acl_load(); uloop_run(); - unlink(ubus_socket); + + if (remove_socket) + unlink(ubus_socket); out: uloop_done();