]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/udev/udev-ctrl.c
tree-wide: use mfree more
[thirdparty/systemd.git] / src / udev / udev-ctrl.c
index 98fd3a9acfcfdae6c8960409c6d48483a710b25c..7717ac7924aa2b36fe8c67442c60fcbbd0fbe903 100644 (file)
  */
 
 #include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
+#include <poll.h>
 #include <stddef.h>
+#include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/poll.h>
 #include <sys/socket.h>
 #include <sys/un.h>
+#include <unistd.h>
 
+#include "alloc-util.h"
+#include "fd-util.h"
+#include "formats-util.h"
+#include "socket-util.h"
 #include "udev.h"
 
 /* wire protocol magic must match */
@@ -57,7 +59,7 @@ struct udev_ctrl {
         int refcount;
         struct udev *udev;
         int sock;
-        struct sockaddr_un saddr;
+        union sockaddr_union saddr;
         socklen_t addrlen;
         bool bound;
         bool cleanup_socket;
@@ -84,7 +86,7 @@ struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd) {
         if (fd < 0) {
                 uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
                 if (uctrl->sock < 0) {
-                        log_error("error getting socket: %m");
+                        log_error_errno(errno, "error getting socket: %m");
                         udev_ctrl_unref(uctrl);
                         return NULL;
                 }
@@ -92,13 +94,18 @@ struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd) {
                 uctrl->bound = true;
                 uctrl->sock = fd;
         }
+
+        /*
+         * FIXME: remove it as soon as we can depend on this:
+         *   http://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/commit/?id=90c6bd34f884cd9cee21f1d152baf6c18bcac949
+         */
         r = setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
         if (r < 0)
-                log_warning("could not set SO_PASSCRED: %m");
+                log_warning_errno(errno, "could not set SO_PASSCRED: %m");
 
-        uctrl->saddr.sun_family = AF_LOCAL;
-        strscpy(uctrl->saddr.sun_path, sizeof(uctrl->saddr.sun_path), "/run/udev/control");
-        uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
+        uctrl->saddr.un.sun_family = AF_LOCAL;
+        strscpy(uctrl->saddr.un.sun_path, sizeof(uctrl->saddr.un.sun_path), "/run/udev/control");
+        uctrl->addrlen = SOCKADDR_UN_LEN(uctrl->saddr.un);
         return uctrl;
 }
 
@@ -110,24 +117,18 @@ int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl) {
         int err;
 
         if (!uctrl->bound) {
-                err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
+                err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
                 if (err < 0 && errno == EADDRINUSE) {
-                        unlink(uctrl->saddr.sun_path);
-                        err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
+                        unlink(uctrl->saddr.un.sun_path);
+                        err = bind(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen);
                 }
 
-                if (err < 0) {
-                        err = -errno;
-                        log_error("bind failed: %m");
-                        return err;
-                }
+                if (err < 0)
+                        return log_error_errno(errno, "bind failed: %m");
 
                 err = listen(uctrl->sock, 0);
-                if (err < 0) {
-                        err = -errno;
-                        log_error("listen failed: %m");
-                        return err;
-                }
+                if (err < 0)
+                        return log_error_errno(errno, "listen failed: %m");
 
                 uctrl->bound = true;
                 uctrl->cleanup_socket = true;
@@ -140,21 +141,19 @@ struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl) {
 }
 
 static struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl) {
-        if (uctrl == NULL)
-                return NULL;
-        uctrl->refcount++;
+        if (uctrl)
+                uctrl->refcount++;
+
         return uctrl;
 }
 
 struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl) {
-        if (uctrl == NULL)
-                return NULL;
-        uctrl->refcount--;
-        if (uctrl->refcount > 0)
-                return uctrl;
-        if (uctrl->sock >= 0)
-                close(uctrl->sock);
-        free(uctrl);
+        if (uctrl && -- uctrl->refcount == 0) {
+                if (uctrl->sock >= 0)
+                        close(uctrl->sock);
+                free(uctrl);
+        }
+
         return NULL;
 }
 
@@ -162,7 +161,7 @@ int udev_ctrl_cleanup(struct udev_ctrl *uctrl) {
         if (uctrl == NULL)
                 return 0;
         if (uctrl->cleanup_socket)
-                unlink(uctrl->saddr.sun_path);
+                unlink(uctrl->saddr.un.sun_path);
         return 0;
 }
 
@@ -187,33 +186,32 @@ struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl) {
         conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
         if (conn->sock < 0) {
                 if (errno != EINTR)
-                        log_error("unable to receive ctrl connection: %m");
+                        log_error_errno(errno, "unable to receive ctrl connection: %m");
                 goto err;
         }
 
         /* check peer credential of connection */
         r = getpeercred(conn->sock, &ucred);
         if (r < 0) {
-                log_error("unable to receive credentials of ctrl connection: %s", strerror(-r));
+                log_error_errno(r, "unable to receive credentials of ctrl connection: %m");
                 goto err;
         }
         if (ucred.uid > 0) {
-                log_error("sender uid=%i, message ignored", ucred.uid);
+                log_error("sender uid="UID_FMT", message ignored", ucred.uid);
                 goto err;
         }
 
         /* enable receiving of the sender credentials in the messages */
         r = setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
         if (r < 0)
-                log_warning("could not set SO_PASSCRED: %m");
+                log_warning_errno(errno, "could not set SO_PASSCRED: %m");
 
         udev_ctrl_ref(uctrl);
         return conn;
 err:
         if (conn->sock >= 0)
                 close(conn->sock);
-        free(conn);
-        return NULL;
+        return mfree(conn);
 }
 
 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn) {
@@ -224,15 +222,15 @@ struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connectio
 }
 
 struct udev_ctrl_connection *udev_ctrl_connection_unref(struct udev_ctrl_connection *conn) {
-        if (conn == NULL)
-                return NULL;
-        conn->refcount--;
-        if (conn->refcount > 0)
-                return conn;
-        if (conn->sock >= 0)
-                close(conn->sock);
-        udev_ctrl_unref(conn->uctrl);
-        free(conn);
+        if (conn && -- conn->refcount == 0) {
+                if (conn->sock >= 0)
+                        close(conn->sock);
+
+                udev_ctrl_unref(conn->uctrl);
+
+                free(conn);
+        }
+
         return NULL;
 }
 
@@ -251,7 +249,7 @@ static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int
                 ctrl_msg_wire.intval = intval;
 
         if (!uctrl->connected) {
-                if (connect(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen) < 0) {
+                if (connect(uctrl->sock, &uctrl->saddr.sa, uctrl->addrlen) < 0) {
                         err = -errno;
                         goto out;
                 }
@@ -361,7 +359,7 @@ struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) {
                         goto err;
                 } else {
                         if (!(pfd[0].revents & POLLIN)) {
-                                log_error("ctrl connection error: %m");
+                                log_error_errno(errno, "ctrl connection error: %m");
                                 goto err;
                         }
                 }
@@ -374,19 +372,23 @@ struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn) {
 
         size = recvmsg(conn->sock, &smsg, 0);
         if (size <  0) {
-                log_error("unable to receive ctrl message: %m");
+                log_error_errno(errno, "unable to receive ctrl message: %m");
                 goto err;
         }
+
+        cmsg_close_all(&smsg);
+
         cmsg = CMSG_FIRSTHDR(&smsg);
-        cred = (struct ucred *) CMSG_DATA(cmsg);
 
         if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
                 log_error("no sender credentials received, message ignored");
                 goto err;
         }
 
+        cred = (struct ucred *) CMSG_DATA(cmsg);
+
         if (cred->uid != 0) {
-                log_error("sender uid=%i, message ignored", cred->uid);
+                log_error("sender uid="UID_FMT", message ignored", cred->uid);
                 goto err;
         }
 
@@ -402,13 +404,11 @@ err:
 }
 
 struct udev_ctrl_msg *udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg) {
-        if (ctrl_msg == NULL)
-                return NULL;
-        ctrl_msg->refcount--;
-        if (ctrl_msg->refcount > 0)
-                return ctrl_msg;
-        udev_ctrl_connection_unref(ctrl_msg->conn);
-        free(ctrl_msg);
+        if (ctrl_msg && -- ctrl_msg->refcount == 0) {
+                udev_ctrl_connection_unref(ctrl_msg->conn);
+                free(ctrl_msg);
+        }
+
         return NULL;
 }