chiark / gitweb /
udevadm: control - use /run/udev/control socket instead of abstract namespace one
authorKay Sievers <kay.sievers@vrfy.org>
Sun, 9 Oct 2011 20:52:03 +0000 (22:52 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Sun, 9 Oct 2011 20:52:03 +0000 (22:52 +0200)
TODO
init/udev-control.socket
udev/udev-ctrl.c
udev/udev.h
udev/udevadm-control.c
udev/udevadm-settle.c
udev/udevd.c

diff --git a/TODO b/TODO
index 3dcaf4c..1d26f45 100644 (file)
--- a/TODO
+++ b/TODO
@@ -1,8 +1,5 @@
  - test (now fixed) /dev/tape/ links
 
- - /run/udev/control socket
-   (add ConditionVirtualization=!pidns)
-
  - move udevd -> --libexecdir
 
  - have a $attrs{} ?
index 8f31061..ea21803 100644 (file)
@@ -5,4 +5,4 @@ ConditionVirtualization=!container
 
 [Socket]
 Service=udev.service
-ListenSequentialPacket=@/org/kernel/udev/udevd
+ListenSequentialPacket=/run/udev/control
index 85e6562..43484cc 100644 (file)
@@ -60,6 +60,7 @@ struct udev_ctrl {
        struct sockaddr_un saddr;
        socklen_t addrlen;
        bool bound;
+       bool cleanup_socket;
        bool connected;
 };
 
@@ -69,7 +70,7 @@ struct udev_ctrl_connection {
        int sock;
 };
 
-static struct udev_ctrl *udev_ctrl_new(struct udev *udev)
+struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd)
 {
        struct udev_ctrl *uctrl;
 
@@ -78,16 +79,6 @@ static struct udev_ctrl *udev_ctrl_new(struct udev *udev)
                return NULL;
        uctrl->refcount = 1;
        uctrl->udev = udev;
-       return uctrl;
-}
-
-struct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *socket_path, int fd)
-{
-       struct udev_ctrl *uctrl;
-
-       uctrl = udev_ctrl_new(udev);
-       if (uctrl == NULL)
-               return NULL;
 
        if (fd < 0) {
                uctrl->sock = socket(AF_LOCAL, SOCK_SEQPACKET|SOCK_NONBLOCK|SOCK_CLOEXEC, 0);
@@ -102,17 +93,15 @@ struct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *so
        }
 
        uctrl->saddr.sun_family = AF_LOCAL;
-       strcpy(uctrl->saddr.sun_path, socket_path);
+       util_strscpyl(uctrl->saddr.sun_path, sizeof(uctrl->saddr.sun_path),
+                      udev_get_run_path(udev), "/control", NULL);
        uctrl->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(uctrl->saddr.sun_path);
-       /* translate leading '@' to abstract namespace */
-       if (uctrl->saddr.sun_path[0] == '@')
-               uctrl->saddr.sun_path[0] = '\0';
        return uctrl;
 }
 
-struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
+struct udev_ctrl *udev_ctrl_new(struct udev *udev)
 {
-       return udev_ctrl_new_from_socket_fd(udev, socket_path, -1);
+       return udev_ctrl_new_from_fd(udev, -1);
 }
 
 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
@@ -121,6 +110,11 @@ int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
 
        if (!uctrl->bound) {
                err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
+               if (err < 0 && errno == EADDRINUSE) {
+                       unlink(uctrl->saddr.sun_path);
+                       err = bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
+               }
+
                if (err < 0) {
                        err = -errno;
                        err(uctrl->udev, "bind failed: %m\n");
@@ -135,6 +129,7 @@ int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
                }
 
                uctrl->bound = true;
+               uctrl->cleanup_socket = true;
        }
        return 0;
 }
@@ -161,6 +156,8 @@ struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl)
                return uctrl;
        if (uctrl->sock >= 0)
                close(uctrl->sock);
+       if (uctrl->cleanup_socket)
+               unlink(uctrl->saddr.sun_path);
        free(uctrl);
        return NULL;
 }
index 5aaf117..402c738 100644 (file)
@@ -26,8 +26,6 @@
 #include "libudev.h"
 #include "libudev-private.h"
 
-#define UDEV_CTRL_SOCK_PATH                    "@/org/kernel/udev/udevd"
-
 struct udev_event {
        struct udev *udev;
        struct udev_device *dev;
@@ -99,8 +97,8 @@ void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev
 
 /* udev-ctrl.c */
 struct udev_ctrl;
-struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path);
-struct udev_ctrl *udev_ctrl_new_from_socket_fd(struct udev *udev, const char *socket_path, int fd);
+struct udev_ctrl *udev_ctrl_new(struct udev *udev);
+struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd);
 int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl);
 struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl);
 struct udev_ctrl *udev_ctrl_unref(struct udev_ctrl *uctrl);
index b500d75..130a71b 100644 (file)
@@ -66,7 +66,7 @@ static int adm_control(struct udev *udev, int argc, char *argv[])
                return 1;
        }
 
-       uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
+       uctrl = udev_ctrl_new(udev);
        if (uctrl == NULL)
                return 2;
 
@@ -157,6 +157,11 @@ static int adm_control(struct udev *udev, int argc, char *argv[])
                        break;
                }
        }
+
+       if (argv[optind] != NULL)
+               fprintf(stderr, "unknown option\n");
+       else if (optind == 1)
+               fprintf(stderr, "missing option\n");
 out:
        udev_ctrl_unref(uctrl);
        return rc;
index 9f409b6..a59d7c3 100644 (file)
@@ -138,7 +138,7 @@ static int adm_settle(struct udev *udev, int argc, char *argv[])
        if (getuid() == 0) {
                struct udev_ctrl *uctrl;
 
-               uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
+               uctrl = udev_ctrl_new(udev);
                if (uctrl != NULL) {
                        if (udev_ctrl_send_ping(uctrl, timeout) < 0) {
                                info(udev, "no connection to daemon\n");
index b65b53f..fdb3e42 100644 (file)
@@ -1349,7 +1349,7 @@ int main(int argc, char *argv[])
 
        if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
                /* get control and netlink socket from from systemd */
-               udev_ctrl = udev_ctrl_new_from_socket_fd(udev, UDEV_CTRL_SOCK_PATH, fd_ctrl);
+               udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
                if (udev_ctrl == NULL) {
                        err(udev, "error taking over udev control socket");
                        rc = 1;
@@ -1364,7 +1364,7 @@ int main(int argc, char *argv[])
                }
        } else {
                /* open control and netlink socket */
-               udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
+               udev_ctrl = udev_ctrl_new(udev);
                if (udev_ctrl == NULL) {
                        fprintf(stderr, "error initializing udev control socket");
                        err(udev, "error initializing udev control socket");