ubus: Remove unnecessary memset calls.
[project/ubus.git] / ubusd.c
diff --git a/ubusd.c b/ubusd.c
index 5b1d52c..f060b38 100644 (file)
--- a/ubusd.c
+++ b/ubusd.c
@@ -22,9 +22,6 @@
 #include <stdio.h>
 #include <unistd.h>
 #include <fcntl.h>
-#ifdef ENABLE_SYSTEMD
-#include <systemd/sd-daemon.h>
-#endif
 
 #include <libubox/blob.h>
 #include <libubox/uloop.h>
 
 static struct ubus_msg_buf *ubus_msg_ref(struct ubus_msg_buf *ub)
 {
-       if (ub->refcount == ~0)
-               return ubus_msg_new(ub->data, ub->len, false);
+       struct ubus_msg_buf *new_ub;
+       if (ub->refcount == ~0) {
+               new_ub = ubus_msg_new(ub->data, ub->len, false);
+               if (!new_ub)
+                       return NULL;
+               memcpy(&new_ub->hdr, &ub->hdr, sizeof(struct ubus_msghdr));
+               new_ub->fd = ub->fd;
+               return new_ub;
+       }
 
        ub->refcount++;
        return ub;
@@ -151,12 +155,13 @@ void ubus_msg_send(struct ubus_client *cl, struct ubus_msg_buf *ub, bool free)
 
        if (!cl->tx_queue[cl->txq_cur]) {
                written = ubus_msg_writev(cl->sock.fd, ub, 0);
-               if (written >= ub->len + sizeof(ub->hdr))
-                       goto out;
 
                if (written < 0)
                        written = 0;
 
+               if (written >= ub->len + sizeof(ub->hdr))
+                       goto out;
+
                cl->txq_ofs = written;
 
                /* get an event once we can write to the socket again */
@@ -383,12 +388,8 @@ static void sighup_handler(int sig)
 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);
@@ -409,37 +410,19 @@ int main(int argc, char **argv)
                }
        }
 
-#ifdef ENABLE_SYSTEMD
-       n_fds = sd_listen_fds(1);
-       if (n_fds > 1) {
-               fprintf(stderr, "Too many file descriptors received.\n");
-               ret = -1;
+       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;
-       } 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();
-
-       if (remove_socket)
-               unlink(ubus_socket);
+       unlink(ubus_socket);
 
 out:
        uloop_done();