chiark / gitweb /
update TODO
[elogind.git] / libudev / libudev-ctrl.c
index 7fa2d1d535cd7ad38c49b75d34f77ba37518e129..e0ec2fa3d7bcc69e1093b4595fad15844cba8144 100644 (file)
@@ -91,7 +91,7 @@ struct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *so
                return NULL;
 
        if (fd < 0) {
-               uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_CLOEXEC, 0);
+               uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
                if (uctrl->sock < 0) {
                        err(udev, "error getting socket: %m\n");
                        udev_ctrl_unref(uctrl);
@@ -176,6 +176,8 @@ int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
 struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
 {
        struct udev_ctrl_connection *conn;
+       struct ucred ucred;
+       socklen_t slen;
        const int on = 1;
 
        conn = calloc(1, sizeof(struct udev_ctrl_connection));
@@ -184,16 +186,33 @@ struct udev_ctrl_connection *udev_ctrl_get_connection(struct udev_ctrl *uctrl)
        conn->refcount = 1;
        conn->uctrl = uctrl;
 
-       conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC);
+       conn->sock = accept4(uctrl->sock, NULL, NULL, SOCK_CLOEXEC|SOCK_NONBLOCK);
        if (conn->sock < 0) {
-               free(conn);
-               return NULL;
+               if (errno != EINTR)
+                       err(uctrl->udev, "unable to receive ctrl connection: %m\n");
+               goto err;
+       }
+
+       /* check peer credential of connection */
+       slen = sizeof(ucred);
+       if (getsockopt(conn->sock, SOL_SOCKET, SO_PEERCRED, &ucred, &slen) < 0) {
+               err(uctrl->udev, "unable to receive credentials of ctrl connection: %m\n");
+               goto err;
+       }
+       if (ucred.uid > 0) {
+               err(uctrl->udev, "sender uid=%i, message ignored\n", ucred.uid);
+               goto err;
        }
 
-       /* enable receiving of the sender credentials */
+       /* enable receiving of the sender credentials in the messages */
        setsockopt(conn->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
        udev_ctrl_ref(uctrl);
        return conn;
+err:
+       if (conn->sock >= 0)
+               close(conn->sock);
+       free(conn);
+       return NULL;
 }
 
 struct udev_ctrl_connection *udev_ctrl_connection_ref(struct udev_ctrl_connection *conn)
@@ -331,6 +350,32 @@ struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
        uctrl_msg->conn = conn;
        udev_ctrl_connection_ref(conn);
 
+       /* wait for the incoming message */
+       for(;;) {
+               struct pollfd pfd[1];
+               int r;
+
+               pfd[0].fd = conn->sock;
+               pfd[0].events = POLLIN;
+
+               r = poll(pfd, 1, 10000);
+               if (r  < 0) {
+                       if (errno == EINTR)
+                               continue;
+                       goto err;
+               } else if (r == 0) {
+                       err(udev, "timeout waiting for ctrl message\n");
+                       goto err;
+               } else {
+                       if (!(pfd[0].revents & POLLIN)) {
+                               err(udev, "ctrl connection error: %m\n");
+                               goto err;
+                       }
+               }
+
+               break;
+       }
+
        iov.iov_base = &uctrl_msg->ctrl_msg_wire;
        iov.iov_len = sizeof(struct udev_ctrl_msg_wire);
        memset(&smsg, 0x00, sizeof(struct msghdr));
@@ -340,7 +385,7 @@ struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl_connection *conn)
        smsg.msg_controllen = sizeof(cred_msg);
        size = recvmsg(conn->sock, &smsg, 0);
        if (size <  0) {
-               err(udev, "unable to receive user udevd message: %m\n");
+               err(udev, "unable to receive ctrl message: %m\n");
                goto err;
        }
        cmsg = CMSG_FIRSTHDR(&smsg);