chiark / gitweb /
move udev_ctrl to libudev-private
authorKay Sievers <kay.sievers@vrfy.org>
Mon, 8 Sep 2008 15:59:00 +0000 (17:59 +0200)
committerKay Sievers <kay.sievers@vrfy.org>
Mon, 8 Sep 2008 15:59:00 +0000 (17:59 +0200)
16 files changed:
udev/Makefile.am
udev/lib/Makefile.am
udev/lib/exported_symbols
udev/lib/libudev-ctrl.c [new file with mode: 0644]
udev/lib/libudev-monitor.c
udev/lib/libudev-private.h
udev/lib/libudev.h
udev/lib/test-libudev.c
udev/udev-control.c [deleted file]
udev/udev.h
udev/udevadm-control.c
udev/udevadm-monitor.c
udev/udevadm-settle.c
udev/udevadm-trigger.c
udev/udevd.c
udev/udevd.h [deleted file]

index 4a30bf9e319477b33b4f65950aeeb3a0983ba2e2..6ee72d32062c6c51dd459eb24d6899c217e044f3 100644 (file)
@@ -36,7 +36,8 @@ common_files = \
        lib/libudev.h \
        lib/libudev-private.h \
        lib/libudev.c \
        lib/libudev.h \
        lib/libudev-private.h \
        lib/libudev.c \
-       lib/libudev-utils.c
+       lib/libudev-utils.c \
+       lib/libudev-ctrl.c
 
 
 if USE_SELINUX
 
 
 if USE_SELINUX
@@ -49,7 +50,6 @@ endif
 
 udevd_SOURCES = \
        $(common_files) \
 
 udevd_SOURCES = \
        $(common_files) \
-       udevd.h \
        udevd.c
 
 udevd_LDADD = \
        udevd.c
 
 udevd_LDADD = \
@@ -58,7 +58,6 @@ udevd_LDADD = \
 
 udevadm_SOURCES = \
        $(common_files) \
 
 udevadm_SOURCES = \
        $(common_files) \
-       udev-control.c \
        udevadm.c \
        udevadm-info.c \
        udevadm-control.c \
        udevadm.c \
        udevadm-info.c \
        udevadm-control.c \
index b7dd373d5efd972d0567a06f087426561b7bf341..630888d3d9d9fcdb6bb4749905aa435a8f979116 100644 (file)
@@ -20,11 +20,13 @@ include_HEADERS =\
        libudev.h
 
 libudev_la_SOURCES =\
        libudev.h
 
 libudev_la_SOURCES =\
+       exported_symbools \
        libudev-private.h \
        libudev.c \
        libudev-utils.c \
        libudev-device.c \
        libudev-enumerate.c \
        libudev-private.h \
        libudev.c \
        libudev-utils.c \
        libudev-device.c \
        libudev-enumerate.c \
+       libudev-ctrl.c \
        libudev-monitor.c \
        ../list.h \
        ../udev.h \
        libudev-monitor.c \
        ../list.h \
        ../udev.h \
index 66275f4f9caf7e3e959ca29e1daf413390177ef0..fa12633377f7a8cbb5dbbd67859c0009f370644f 100644 (file)
@@ -19,8 +19,9 @@ udev_device_get_devlinks
 udev_device_get_properties
 udev_devices_enumerate
 udev_monitor_new_from_socket
 udev_device_get_properties
 udev_devices_enumerate
 udev_monitor_new_from_socket
+udev_monitor_enable_receiving
 udev_monitor_ref
 udev_monitor_unref
 udev_monitor_get_udev
 udev_monitor_get_fd
 udev_monitor_ref
 udev_monitor_unref
 udev_monitor_get_udev
 udev_monitor_get_fd
-udev_monitor_get_device
+udev_monitor_receive_device
diff --git a/udev/lib/libudev-ctrl.c b/udev/lib/libudev-ctrl.c
new file mode 100644 (file)
index 0000000..b2af0c7
--- /dev/null
@@ -0,0 +1,332 @@
+/*
+ * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
+ *
+ *     This program is free software; you can redistribute it and/or modify it
+ *     under the terms of the GNU General Public License as published by the
+ *     Free Software Foundation version 2 of the License.
+ * 
+ *     This program is distributed in the hope that it will be useful, but
+ *     WITHOUT ANY WARRANTY; without even the implied warranty of
+ *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *     General Public License for more details.
+ * 
+ *     You should have received a copy of the GNU General Public License along
+ *     with this program; if not, write to the Free Software Foundation, Inc.,
+ *     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include "../udev.h"
+#include "libudev.h"
+#include "libudev-private.h"
+
+#define UDEV_CTRL_MAGIC                                "udevd-128"
+
+enum udev_ctrl_msg_type {
+       UDEV_CTRL_UNKNOWN,
+       UDEV_CTRL_SET_LOG_LEVEL,
+       UDEV_CTRL_STOP_EXEC_QUEUE,
+       UDEV_CTRL_START_EXEC_QUEUE,
+       UDEV_CTRL_RELOAD_RULES,
+       UDEV_CTRL_SET_ENV,
+       UDEV_CTRL_SET_MAX_CHILDS,
+       UDEV_CTRL_SET_MAX_CHILDS_RUNNING,
+};
+
+struct ctrl_msg {
+       char magic[32];
+       enum udev_ctrl_msg_type type;
+       union {
+               int intval;
+               char buf[256];
+       };
+};
+
+struct udev_ctrl_msg {
+       int refcount;
+       struct udev_ctrl *uctrl;
+       struct ctrl_msg ctrl_msg;
+};
+
+struct udev_ctrl {
+       int refcount;
+       struct udev *udev;
+       int sock;
+       struct sockaddr_un saddr;
+       socklen_t addrlen;
+};
+
+struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
+{
+       struct udev_ctrl *uctrl;
+
+       uctrl = malloc(sizeof(struct udev_ctrl));
+       if (uctrl == NULL)
+               return NULL;
+       memset(uctrl, 0x00, sizeof(struct udev_ctrl));
+       uctrl->refcount = 1;
+       uctrl->udev = udev;
+
+       uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
+       if (uctrl->sock < 0) {
+               err(udev, "error getting socket: %s\n", strerror(errno));
+               udev_ctrl_unref(uctrl);
+               return NULL;
+       }
+
+       uctrl->saddr.sun_family = AF_LOCAL;
+       strcpy(uctrl->saddr.sun_path, socket_path);
+       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;
+}
+
+int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl)
+{
+       int err;
+       const int feature_on = 1;
+
+       err= bind(uctrl->sock, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
+       if (err < 0) {
+               err(uctrl->udev, "bind failed: %s\n", strerror(errno));
+               return err;
+       }
+
+       /* enable receiving of the sender credentials */
+       setsockopt(uctrl->sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
+       return 0;
+}
+
+struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl)
+{
+       return uctrl->udev;
+}
+
+struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl)
+{
+       if (uctrl == NULL)
+               return NULL;
+       uctrl->refcount++;
+       return uctrl;
+}
+
+void udev_ctrl_unref(struct udev_ctrl *uctrl)
+{
+       if (uctrl == NULL)
+               return;
+       uctrl->refcount--;
+       if (uctrl->refcount > 0)
+               return;
+       if (uctrl->sock >= 0)
+               close(uctrl->sock);
+       free(uctrl);
+}
+
+int udev_ctrl_get_fd(struct udev_ctrl *uctrl)
+{
+       if (uctrl == NULL)
+               return -1;
+       return uctrl->sock;
+}
+
+static int ctrl_send(struct udev_ctrl *uctrl, enum udev_ctrl_msg_type type, int intval, const char *buf)
+{
+       struct ctrl_msg ctrl_msg;
+       int err;
+
+       memset(&ctrl_msg, 0x00, sizeof(struct ctrl_msg));
+       strcpy(ctrl_msg.magic, UDEV_CTRL_MAGIC);
+       ctrl_msg.type = type;
+
+       if (buf != NULL)
+               strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
+       else
+               ctrl_msg.intval = intval;
+
+       err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
+       if (err == -1) {
+               err(uctrl->udev, "error sending message: %s\n", strerror(errno));
+       }
+       return err;
+}
+
+int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority)
+{
+       ctrl_send(uctrl, UDEV_CTRL_SET_LOG_LEVEL, priority, NULL);
+       return 0;
+}
+
+int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl)
+{
+       ctrl_send(uctrl, UDEV_CTRL_STOP_EXEC_QUEUE, 0, NULL);
+       return 0;
+}
+
+int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl)
+{
+       ctrl_send(uctrl, UDEV_CTRL_START_EXEC_QUEUE, 0, NULL);
+       return 0;
+}
+
+int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl)
+{
+       ctrl_send(uctrl, UDEV_CTRL_RELOAD_RULES, 0, NULL);
+       return 0;
+}
+
+int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key)
+{
+       ctrl_send(uctrl, UDEV_CTRL_SET_ENV, 0, optarg);
+       return 0;
+}
+
+int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count)
+{
+       ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS, count, NULL);
+       return 0;
+}
+
+int udev_ctrl_send_set_max_childs_running(struct udev_ctrl *uctrl, int count)
+{
+       ctrl_send(uctrl, UDEV_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);
+       return 0;
+}
+
+struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl)
+{
+       struct udev_ctrl_msg *uctrl_msg;
+       ssize_t size;
+       struct msghdr smsg;
+       struct cmsghdr *cmsg;
+       struct iovec iov;
+       struct ucred *cred;
+       char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
+
+       uctrl_msg = malloc(sizeof(struct udev_ctrl_msg));
+       if (uctrl_msg == NULL)
+               return NULL;
+       memset(uctrl_msg, 0x00, sizeof(struct udev_ctrl_msg));
+       uctrl_msg->refcount = 1;
+       uctrl_msg->uctrl = uctrl;
+
+       iov.iov_base = &uctrl_msg->ctrl_msg;
+       iov.iov_len = sizeof(struct udev_ctrl_msg);
+
+       memset(&smsg, 0x00, sizeof(struct msghdr));
+       smsg.msg_iov = &iov;
+       smsg.msg_iovlen = 1;
+       smsg.msg_control = cred_msg;
+       smsg.msg_controllen = sizeof(cred_msg);
+
+       size = recvmsg(uctrl->sock, &smsg, 0);
+       if (size <  0) {
+               err(uctrl->udev, "unable to receive user udevd message: %s\n", strerror(errno));
+               goto err;
+       }
+       cmsg = CMSG_FIRSTHDR(&smsg);
+       cred = (struct ucred *) CMSG_DATA(cmsg);
+
+       if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
+               err(uctrl->udev, "no sender credentials received, message ignored\n");
+               goto err;
+       }
+
+       if (cred->uid != 0) {
+               err(uctrl->udev, "sender uid=%i, message ignored\n", cred->uid);
+               goto err;
+       }
+
+       if (strncmp(uctrl_msg->ctrl_msg.magic, UDEV_CTRL_MAGIC, sizeof(UDEV_CTRL_MAGIC)) != 0 ) {
+               err(uctrl->udev, "message magic '%s' doesn't match, ignore it\n", uctrl_msg->ctrl_msg.magic);
+               goto err;
+       }
+
+       info(uctrl->udev, "created ctrl_msg %p (%i)\n", uctrl_msg, uctrl_msg->ctrl_msg.type);
+       return uctrl_msg;
+err:
+       udev_ctrl_msg_unref(uctrl_msg);
+       return NULL;
+}
+
+struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg)
+{
+       if (ctrl_msg == NULL)
+               return NULL;
+       ctrl_msg->refcount++;
+       return ctrl_msg;
+}
+
+void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg)
+{
+       if (ctrl_msg == NULL)
+               return;
+       ctrl_msg->refcount--;
+       if (ctrl_msg->refcount > 0)
+               return;
+       info(ctrl_msg->uctrl->udev, "release ctrl_msg %p\n", ctrl_msg);
+       free(ctrl_msg);
+}
+
+int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg)
+{
+       if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_LOG_LEVEL)
+               return ctrl_msg->ctrl_msg.intval;
+       return -1;
+}
+
+int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg)
+{
+       if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_STOP_EXEC_QUEUE)
+               return 1;
+       return -1;
+}
+
+int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg)
+{
+       if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_START_EXEC_QUEUE)
+               return 1;
+       return -1;
+}
+
+int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg)
+{
+       if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_RELOAD_RULES)
+               return 1;
+       return -1;
+}
+
+const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg)
+{
+       if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_ENV)
+               return ctrl_msg->ctrl_msg.buf;
+       return NULL;
+}
+
+int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg)
+{
+       if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_MAX_CHILDS)
+               return ctrl_msg->ctrl_msg.intval;
+       return -1;
+}
+
+int udev_ctrl_get_set_max_childs_running(struct udev_ctrl_msg *ctrl_msg)
+{
+       if (ctrl_msg->ctrl_msg.type == UDEV_CTRL_SET_MAX_CHILDS_RUNNING)
+               return ctrl_msg->ctrl_msg.intval;
+       return -1;
+}
index 09845576d9951f003be7530e401827e6273a21de..2cba7caba00a3ff9d94b61c318702943146dfa2f 100644 (file)
@@ -37,7 +37,9 @@
 struct udev_monitor {
        struct udev *udev;
        int refcount;
 struct udev_monitor {
        struct udev *udev;
        int refcount;
-       int socket;
+       int sock;
+       struct sockaddr_un saddr;
+       socklen_t addrlen;
 };
 
 /**
 };
 
 /**
@@ -60,9 +62,6 @@ struct udev_monitor {
 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
 {
        struct udev_monitor *udev_monitor;
 struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path)
 {
        struct udev_monitor *udev_monitor;
-       struct sockaddr_un saddr;
-       socklen_t addrlen;
-       const int on = 1;
 
        if (udev == NULL)
                return NULL;
 
        if (udev == NULL)
                return NULL;
@@ -75,34 +74,38 @@ struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char
        udev_monitor->refcount = 1;
        udev_monitor->udev = udev;
 
        udev_monitor->refcount = 1;
        udev_monitor->udev = udev;
 
-       memset(&saddr, 0x00, sizeof(saddr));
-       saddr.sun_family = AF_LOCAL;
-       strcpy(saddr.sun_path, socket_path);
-       addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
+       udev_monitor->saddr.sun_family = AF_LOCAL;
+       strcpy(udev_monitor->saddr.sun_path, socket_path);
+       udev_monitor->addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(udev_monitor->saddr.sun_path);
 
        /* translate leading '@' to abstract namespace */
 
        /* translate leading '@' to abstract namespace */
-       if (saddr.sun_path[0] == '@')
-               saddr.sun_path[0] = '\0';
+       if (udev_monitor->saddr.sun_path[0] == '@')
+               udev_monitor->saddr.sun_path[0] = '\0';
 
 
-       udev_monitor->socket = socket(AF_LOCAL, SOCK_DGRAM, 0);
-       if (udev_monitor->socket == -1) {
+       udev_monitor->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
+       if (udev_monitor->sock == -1) {
                err(udev, "error getting socket: %s\n", strerror(errno));
                free(udev_monitor);
                return NULL;
        }
                err(udev, "error getting socket: %s\n", strerror(errno));
                free(udev_monitor);
                return NULL;
        }
+       return udev_monitor;
+}
 
 
-       if (bind(udev_monitor->socket, (struct sockaddr *) &saddr, addrlen) < 0) {
-               err(udev, "bind failed: %s\n", strerror(errno));
-               close(udev_monitor->socket);
-               free(udev_monitor);
-               return NULL;
+int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor)
+{
+       int err;
+       const int on = 1;
+
+       err = bind(udev_monitor->sock, (struct sockaddr *) &udev_monitor->saddr, udev_monitor->addrlen);
+       if (err < 0) {
+               err(udev_monitor->udev, "bind failed: %s\n", strerror(errno));
+               return err;
        }
 
        /* enable receiving of the sender credentials */
        }
 
        /* enable receiving of the sender credentials */
-       setsockopt(udev_monitor->socket, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
+       setsockopt(udev_monitor->sock, SOL_SOCKET, SO_PASSCRED, &on, sizeof(on));
        info(udev_monitor->udev, "udev_monitor: %p created\n", udev_monitor);
        info(udev_monitor->udev, "udev_monitor: %p created\n", udev_monitor);
-
-       return udev_monitor;
+       return 0;
 }
 
 /**
 }
 
 /**
@@ -125,7 +128,7 @@ struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor)
  * udev_monitor_unref:
  * @udev_monitor: udev monitor
  *
  * udev_monitor_unref:
  * @udev_monitor: udev monitor
  *
- * Drop a reference of a udev monitor. If the refcount reaches zero,
+ * Drop a reference ofa udev monitor. If the refcount reaches zero,
  * the bound socket will be closed, and the ressources of the monitor
  * will be released.
  *
  * the bound socket will be closed, and the ressources of the monitor
  * will be released.
  *
@@ -137,7 +140,8 @@ void udev_monitor_unref(struct udev_monitor *udev_monitor)
        udev_monitor->refcount--;
        if (udev_monitor->refcount > 0)
                return;
        udev_monitor->refcount--;
        if (udev_monitor->refcount > 0)
                return;
-       close(udev_monitor->socket);
+       if (udev_monitor->sock >= 0)
+               close(udev_monitor->sock);
        info(udev_monitor->udev, "udev_monitor: %p released\n", udev_monitor);
        free(udev_monitor);
 }
        info(udev_monitor->udev, "udev_monitor: %p released\n", udev_monitor);
        free(udev_monitor);
 }
@@ -169,14 +173,14 @@ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
 {
        if (udev_monitor == NULL)
                return -1;
 {
        if (udev_monitor == NULL)
                return -1;
-       return udev_monitor->socket;
+       return udev_monitor->sock;
 }
 
 /**
 }
 
 /**
- * udev_monitor_get_device:
+ * udev_monitor_receive_device:
  * @udev_monitor: udev monitor
  *
  * @udev_monitor: udev monitor
  *
- * Retrieve data from the udev monitor socket, allocate a new udev
+ * Receive data from the udev monitor socket, allocate a new udev
  * device, fill in the received data, and return the device.
  *
  * Only socket connections with uid=0 are accepted. The caller
  * device, fill in the received data, and return the device.
  *
  * Only socket connections with uid=0 are accepted. The caller
@@ -188,7 +192,7 @@ int udev_monitor_get_fd(struct udev_monitor *udev_monitor)
  *
  * Returns: a new udev device, or #NULL, in case of an error
  **/
  *
  * Returns: a new udev device, or #NULL, in case of an error
  **/
-struct udev_device *udev_monitor_get_device(struct udev_monitor *udev_monitor)
+struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor)
 {
        struct udev_device *udev_device;
        struct msghdr smsg;
 {
        struct udev_device *udev_device;
        struct msghdr smsg;
@@ -210,7 +214,7 @@ struct udev_device *udev_monitor_get_device(struct udev_monitor *udev_monitor)
        smsg.msg_control = cred_msg;
        smsg.msg_controllen = sizeof(cred_msg);
 
        smsg.msg_control = cred_msg;
        smsg.msg_controllen = sizeof(cred_msg);
 
-       if (recvmsg(udev_monitor->socket, &smsg, 0) < 0) {
+       if (recvmsg(udev_monitor->sock, &smsg, 0) < 0) {
                if (errno != EINTR)
                        info(udev_monitor->udev, "unable to receive message");
                return NULL;
                if (errno != EINTR)
                        info(udev_monitor->udev, "unable to receive message");
                return NULL;
index ef26ec7e111b31783b92c5c2aa3b0d4eb9d9ef90..b03c813c2f68b7720cc8a99d314b774cc368d2a8 100644 (file)
@@ -61,6 +61,34 @@ extern int device_set_devname(struct udev_device *udev_device, const char *devna
 extern int device_add_devlink(struct udev_device *udev_device, const char *devlink);
 extern int device_add_property(struct udev_device *udev_device, const char *property);
 
 extern int device_add_devlink(struct udev_device *udev_device, const char *devlink);
 extern int device_add_property(struct udev_device *udev_device, const char *property);
 
+/* udev_ctrl - daemon runtime setup */
+struct udev_ctrl;
+extern struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path);
+extern int udev_ctrl_enable_receiving(struct udev_ctrl *uctrl);
+extern struct udev_ctrl *udev_ctrl_ref(struct udev_ctrl *uctrl);
+extern void udev_ctrl_unref(struct udev_ctrl *uctrl);
+extern struct udev *udev_ctrl_get_udev(struct udev_ctrl *uctrl);
+extern int udev_ctrl_get_fd(struct udev_ctrl *uctrl);
+extern int udev_ctrl_send_set_log_level(struct udev_ctrl *uctrl, int priority);
+extern int udev_ctrl_send_stop_exec_queue(struct udev_ctrl *uctrl);
+extern int udev_ctrl_send_start_exec_queue(struct udev_ctrl *uctrl);
+extern int udev_ctrl_send_reload_rules(struct udev_ctrl *uctrl);
+extern int udev_ctrl_send_set_env(struct udev_ctrl *uctrl, const char *key);
+extern int udev_ctrl_send_set_max_childs(struct udev_ctrl *uctrl, int count);
+extern int udev_ctrl_send_set_max_childs_running(struct udev_ctrl *uctrl, int count);
+struct udev_ctrl_msg;
+extern struct udev_ctrl_msg *udev_ctrl_msg(struct udev_ctrl *uctrl);
+extern struct udev_ctrl_msg *udev_ctrl_receive_msg(struct udev_ctrl *uctrl);
+extern struct udev_ctrl_msg *udev_ctrl_msg_ref(struct udev_ctrl_msg *ctrl_msg);
+extern void udev_ctrl_msg_unref(struct udev_ctrl_msg *ctrl_msg);
+extern int udev_ctrl_get_set_log_level(struct udev_ctrl_msg *ctrl_msg);
+extern int udev_ctrl_get_stop_exec_queue(struct udev_ctrl_msg *ctrl_msg);
+extern int udev_ctrl_get_start_exec_queue(struct udev_ctrl_msg *ctrl_msg);
+extern int udev_ctrl_get_reload_rules(struct udev_ctrl_msg *ctrl_msg);
+extern const char *udev_ctrl_get_set_env(struct udev_ctrl_msg *ctrl_msg);
+extern int udev_ctrl_get_set_max_childs(struct udev_ctrl_msg *ctrl_msg);
+extern int udev_ctrl_get_set_max_childs_running(struct udev_ctrl_msg *ctrl_msg);
+
 /* libudev-utils */
 extern ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size);
 #endif
 /* libudev-utils */
 extern ssize_t util_get_sys_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size);
 #endif
index 98652c590490be125aac63828f15a4b917fce188..a2a06b58e9210abf458129ff073b51b4168d7f6b 100644 (file)
@@ -28,9 +28,6 @@
 #endif
 
 struct udev;
 #endif
 
 struct udev;
-struct udev_device;
-struct udev_monitor;
-
 extern struct udev *udev_new(void);
 extern struct udev *udev_ref(struct udev *udev);
 extern void udev_unref(struct udev *udev);
 extern struct udev *udev_new(void);
 extern struct udev *udev_ref(struct udev *udev);
 extern void udev_unref(struct udev *udev);
@@ -43,6 +40,7 @@ extern void udev_set_log_priority(struct udev *udev, int priority);
 extern const char *udev_get_sys_path(struct udev *udev);
 extern const char *udev_get_dev_path(struct udev *udev);
 
 extern const char *udev_get_sys_path(struct udev *udev);
 extern const char *udev_get_dev_path(struct udev *udev);
 
+struct udev_device;
 extern struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath);
 extern struct udev_device *udev_device_ref(struct udev_device *udev_device);
 extern void udev_device_unref(struct udev_device *udev_device);
 extern struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath);
 extern struct udev_device *udev_device_ref(struct udev_device *udev_device);
 extern void udev_device_unref(struct udev_device *udev_device);
@@ -65,11 +63,13 @@ extern int udev_devices_enumerate(struct udev *udev, const char *subsystem,
                                            const char *devpath, const char *subsystem, const char *name, void *data),
                                  void *data);
 
                                            const char *devpath, const char *subsystem, const char *name, void *data),
                                  void *data);
 
+struct udev_monitor;
 extern struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path);
 extern struct udev_monitor *udev_monitor_new_from_socket(struct udev *udev, const char *socket_path);
+extern int udev_monitor_enable_receiving(struct udev_monitor *udev_monitor);
 extern struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor);
 extern void udev_monitor_unref(struct udev_monitor *udev_monitor);
 extern struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor);
 extern int udev_monitor_get_fd(struct udev_monitor *udev_monitor);
 extern struct udev_monitor *udev_monitor_ref(struct udev_monitor *udev_monitor);
 extern void udev_monitor_unref(struct udev_monitor *udev_monitor);
 extern struct udev *udev_monitor_get_udev(struct udev_monitor *udev_monitor);
 extern int udev_monitor_get_fd(struct udev_monitor *udev_monitor);
-extern struct udev_device *udev_monitor_get_device(struct udev_monitor *udev_monitor);
+extern struct udev_device *udev_monitor_receive_device(struct udev_monitor *udev_monitor);
 
 #endif
 
 #endif
index d0095f15fc5828cc9cab1a1971b671be93eb678f..835536af84645d75b3d312d85f6cc7517d10793b 100644 (file)
@@ -112,6 +112,10 @@ static int test_monitor(struct udev *udev, const char *socket_path)
                printf("no socket\n");
                return -1;
        }
                printf("no socket\n");
                return -1;
        }
+       if (udev_monitor_enable_receiving(udev_monitor) < 0) {
+               printf("bind failed\n");
+               return -1;
+       }
 
        fd = udev_monitor_get_fd(udev_monitor);
        FD_ZERO(&readfds);
 
        fd = udev_monitor_get_fd(udev_monitor);
        FD_ZERO(&readfds);
@@ -128,7 +132,7 @@ static int test_monitor(struct udev *udev, const char *socket_path)
                printf("select fd count: %i\n", fdcount);
 
                if (FD_ISSET(fd, &readfds)) {
                printf("select fd count: %i\n", fdcount);
 
                if (FD_ISSET(fd, &readfds)) {
-                       device = udev_monitor_get_device(udev_monitor);
+                       device = udev_monitor_receive_device(udev_monitor);
                        if (device == NULL) {
                                printf("no device from socket\n");
                                continue;
                        if (device == NULL) {
                                printf("no device from socket\n");
                                continue;
diff --git a/udev/udev-control.c b/udev/udev-control.c
deleted file mode 100644 (file)
index 96e4b29..0000000
+++ /dev/null
@@ -1,135 +0,0 @@
-/*
- * Copyright (C) 2005-2008 Kay Sievers <kay.sievers@vrfy.org>
- *
- *     This program is free software; you can redistribute it and/or modify it
- *     under the terms of the GNU General Public License as published by the
- *     Free Software Foundation version 2 of the License.
- * 
- *     This program is distributed in the hope that it will be useful, but
- *     WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *     General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License along
- *     with this program; if not, write to the Free Software Foundation, Inc.,
- *     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- */
-
-#include "config.h"
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stddef.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <sys/un.h>
-
-#include "udev.h"
-#include "udevd.h"
-
-struct udev_ctrl {
-       struct udev *udev;
-       int sock;
-       struct sockaddr_un saddr;
-       socklen_t addrlen;
-};
-
-struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path)
-{
-       struct udev_ctrl *uctrl;
-
-       uctrl = malloc(sizeof(struct udev_ctrl));
-       if (uctrl == NULL)
-               return NULL;
-       memset(uctrl, 0x00, sizeof(struct udev_ctrl));
-       uctrl->udev = udev;
-
-       uctrl->sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
-       if (uctrl->sock < 0) {
-               err(udev, "error getting socket: %s\n", strerror(errno));
-               free(uctrl);
-               return NULL;
-       }
-
-       uctrl->saddr.sun_family = AF_LOCAL;
-       strcpy(uctrl->saddr.sun_path, socket_path);
-       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;
-}
-
-void udev_ctrl_unref(struct udev_ctrl *uctrl)
-{
-       if (uctrl == NULL)
-               return;
-       close(uctrl->sock);
-}
-
-static int ctrl_send(struct udev_ctrl *uctrl, enum udevd_ctrl_msg_type type, int intval, const char *buf)
-{
-       struct udevd_ctrl_msg ctrl_msg;
-       int err;
-
-       memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
-       strcpy(ctrl_msg.magic, UDEVD_CTRL_MAGIC);
-       ctrl_msg.type = type;
-
-       if (buf != NULL)
-               strlcpy(ctrl_msg.buf, buf, sizeof(ctrl_msg.buf));
-       else
-               ctrl_msg.intval = intval;
-
-       err = sendto(uctrl->sock, &ctrl_msg, sizeof(ctrl_msg), 0, (struct sockaddr *)&uctrl->saddr, uctrl->addrlen);
-       if (err == -1) {
-               err(uctrl->udev, "error sending message: %s\n", strerror(errno));
-       }
-       return err;
-}
-
-int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority)
-{
-       ctrl_send(uctrl, UDEVD_CTRL_SET_LOG_LEVEL, priority, NULL);
-       return 0;
-}
-
-int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl)
-{
-       ctrl_send(uctrl, UDEVD_CTRL_STOP_EXEC_QUEUE, 0, NULL);
-       return 0;
-}
-
-int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl)
-{
-       ctrl_send(uctrl, UDEVD_CTRL_START_EXEC_QUEUE, 0, NULL);
-       return 0;
-}
-
-int udev_ctrl_reload_rules(struct udev_ctrl *uctrl)
-{
-       ctrl_send(uctrl, UDEVD_CTRL_RELOAD_RULES, 0, NULL);
-       return 0;
-}
-
-int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key)
-{
-       ctrl_send(uctrl, UDEVD_CTRL_ENV, 0, optarg);
-       return 0;
-}
-
-int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count)
-{
-       ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS, count, NULL);
-       return 0;
-}
-
-int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count)
-{
-       ctrl_send(uctrl, UDEVD_CTRL_SET_MAX_CHILDS_RUNNING, count, NULL);
-       return 0;
-}
index 1dd55f6a23e23e7b5914b511b02d246925561e17..122e83d876c71a88c8d863a604e19a7dabdb1287 100644 (file)
 #define DEFAULT_PARTITIONS_COUNT               15
 #define UDEV_EVENT_TIMEOUT                     180
 
 #define DEFAULT_PARTITIONS_COUNT               15
 #define UDEV_EVENT_TIMEOUT                     180
 
+/* linux/include/linux/kobject.h */
+#define UEVENT_BUFFER_SIZE                     2048
+#define UEVENT_NUM_ENVP                                32
+
+#define UDEV_CTRL_SOCK_PATH                    "@" UDEV_PREFIX "/org/kernel/udev/udevd"
+
 #define UDEV_MAX(a,b) ((a) > (b) ? (a) : (b))
 
 /* pipes */
 #define UDEV_MAX(a,b) ((a) > (b) ? (a) : (b))
 
 /* pipes */
@@ -190,16 +196,4 @@ extern int udevadm_trigger(struct udev *udev, int argc, char *argv[]);
 extern int udevadm_settle(struct udev *udev, int argc, char *argv[]);
 extern int udevadm_test(struct udev *udev, int argc, char *argv[]);
 
 extern int udevadm_settle(struct udev *udev, int argc, char *argv[]);
 extern int udevadm_test(struct udev *udev, int argc, char *argv[]);
 
-/* udev_ctrl - daemon runtime setup */
-struct udev_ctrl;
-extern struct udev_ctrl *udev_ctrl_new_from_socket(struct udev *udev, const char *socket_path);
-extern void udev_ctrl_unref(struct udev_ctrl *uctrl);
-extern int udev_ctrl_set_log_level(struct udev_ctrl *uctrl, int priority);
-extern int udev_ctrl_stop_exec_queue(struct udev_ctrl *uctrl);
-extern int udev_ctrl_start_exec_queue(struct udev_ctrl *uctrl);
-extern int udev_ctrl_reload_rules(struct udev_ctrl *uctrl);
-extern int udev_ctrl_set_env(struct udev_ctrl *uctrl, const char *key);
-extern int udev_ctrl_set_max_childs(struct udev_ctrl *uctrl, int count);
-extern int udev_ctrl_set_max_childs_running(struct udev_ctrl *uctrl, int count);
-
 #endif
 #endif
index 6e10316fbe7cdae841b89a7f7b6998b6d94c8ff5..8e319be9460855fe0c28f92086b97ea04dbd7e6b 100644 (file)
 #include <sys/un.h>
 
 #include "udev.h"
 #include <sys/un.h>
 
 #include "udev.h"
-#include "udevd.h"
+
+static void print_help(void)
+{
+       printf("Usage: udevadm control COMMAND\n"
+               "  --log-priority=<level>   set the udev log level for the daemon\n"
+               "  --stop-exec-queue        keep udevd from executing events, queue only\n"
+               "  --start-exec-queue       execute events, flush queue\n"
+               "  --reload-rules           reloads the rules files\n"
+               "  --env=<KEY>=<value>      set a global environment variable\n"
+               "  --max-childs=<N>         maximum number of childs\n"
+               "  --max-childs-running=<N> maximum number of childs running at the same time\n"
+               "  --help                   print this help text\n\n");
+}
 
 int udevadm_control(struct udev *udev, int argc, char *argv[])
 {
 
 int udevadm_control(struct udev *udev, int argc, char *argv[])
 {
-       struct udev_ctrl *uctrl;
+       struct udev_ctrl *uctrl = NULL;
        int rc = 1;
 
        /* compat values with '_' will be removed in a future release */
        int rc = 1;
 
        /* compat values with '_' will be removed in a future release */
@@ -63,7 +75,7 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
                goto exit;
        }
 
                goto exit;
        }
 
-       uctrl = udev_ctrl_new_from_socket(udev, UDEVD_CTRL_SOCK_PATH);
+       uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
        if (uctrl == NULL)
                goto exit;
 
        if (uctrl == NULL)
                goto exit;
 
@@ -91,26 +103,31 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
                                fprintf(stderr, "invalid number '%s'\n", optarg);
                                goto exit;
                        }
                                fprintf(stderr, "invalid number '%s'\n", optarg);
                                goto exit;
                        }
-                       udev_ctrl_set_log_level(uctrl, log_priority(optarg));
+                       udev_ctrl_send_set_log_level(uctrl, log_priority(optarg));
+                       rc = 0;
                        break;
                case 's':
                case 's' + 256:
                        break;
                case 's':
                case 's' + 256:
-                       udev_ctrl_stop_exec_queue(uctrl);
+                       udev_ctrl_send_stop_exec_queue(uctrl);
+                       rc = 0;
                        break;
                case 'S':
                case 'S' + 256:
                        break;
                case 'S':
                case 'S' + 256:
-                       udev_ctrl_start_exec_queue(uctrl);
+                       udev_ctrl_send_start_exec_queue(uctrl);
+                       rc = 0;
                        break;
                case 'R':
                case 'R' + 256:
                        break;
                case 'R':
                case 'R' + 256:
-                       udev_ctrl_reload_rules(uctrl);
+                       udev_ctrl_send_reload_rules(uctrl);
+                       rc = 0;
                        break;
                case 'e':
                        if (strchr(optarg, '=') == NULL) {
                                fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
                                goto exit;
                        }
                        break;
                case 'e':
                        if (strchr(optarg, '=') == NULL) {
                                fprintf(stderr, "expect <KEY>=<valaue> instead of '%s'\n", optarg);
                                goto exit;
                        }
-                       udev_ctrl_set_env(uctrl, optarg);
+                       udev_ctrl_send_set_env(uctrl, optarg);
+                       rc = 0;
                        break;
                case 'm':
                case 'm' + 256:
                        break;
                case 'm':
                case 'm' + 256:
@@ -119,7 +136,8 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
                                fprintf(stderr, "invalid number '%s'\n", optarg);
                                goto exit;
                        }
                                fprintf(stderr, "invalid number '%s'\n", optarg);
                                goto exit;
                        }
-                       udev_ctrl_set_max_childs(uctrl, i);
+                       udev_ctrl_send_set_max_childs(uctrl, i);
+                       rc = 0;
                        break;
                case 'M':
                case 'M' + 256:
                        break;
                case 'M':
                case 'M' + 256:
@@ -128,18 +146,12 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
                                fprintf(stderr, "invalid number '%s'\n", optarg);
                                goto exit;
                        }
                                fprintf(stderr, "invalid number '%s'\n", optarg);
                                goto exit;
                        }
-                       udev_ctrl_set_max_childs_running(uctrl, i);
+                       udev_ctrl_send_set_max_childs_running(uctrl, i);
+                       rc = 0;
                        break;
                case 'h':
                        break;
                case 'h':
-                       printf("Usage: udevadm control COMMAND\n"
-                               "  --log-priority=<level>   set the udev log level for the daemon\n"
-                               "  --stop-exec-queue        keep udevd from executing events, queue only\n"
-                               "  --start-exec-queue       execute events, flush queue\n"
-                               "  --reload-rules           reloads the rules files\n"
-                               "  --env=<KEY>=<value>      set a global environment variable\n"
-                               "  --max-childs=<N>         maximum number of childs\n"
-                               "  --max-childs-running=<N> maximum number of childs running at the same time\n"
-                               "  --help                   print this help text\n\n");
+                       print_help();
+                       rc = 0;
                        goto exit;
                default:
                        goto exit;
                        goto exit;
                default:
                        goto exit;
@@ -156,24 +168,40 @@ int udevadm_control(struct udev *udev, int argc, char *argv[])
                    "this will stop working in a future release\n");
 
                if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
                    "this will stop working in a future release\n");
 
                if (!strncmp(arg, "log_priority=", strlen("log_priority="))) {
-                       udev_ctrl_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
+                       udev_ctrl_send_set_log_level(uctrl, log_priority(&arg[strlen("log_priority=")]));
+                       rc = 0;
+                       goto exit;
                } else if (!strcmp(arg, "stop_exec_queue")) {
                } else if (!strcmp(arg, "stop_exec_queue")) {
-                       udev_ctrl_stop_exec_queue(uctrl);
+                       udev_ctrl_send_stop_exec_queue(uctrl);
+                       rc = 0;
+                       goto exit;
                } else if (!strcmp(arg, "start_exec_queue")) {
                } else if (!strcmp(arg, "start_exec_queue")) {
-                       udev_ctrl_start_exec_queue(uctrl);
+                       udev_ctrl_send_start_exec_queue(uctrl);
+                       rc = 0;
+                       goto exit;
                } else if (!strcmp(arg, "reload_rules")) {
                } else if (!strcmp(arg, "reload_rules")) {
-                       udev_ctrl_reload_rules(uctrl);
+                       udev_ctrl_send_reload_rules(uctrl);
+                       rc = 0;
+                       goto exit;
                } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
                } else if (!strncmp(arg, "max_childs=", strlen("max_childs="))) {
-                       udev_ctrl_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
+                       udev_ctrl_send_set_max_childs(uctrl, strtoul(&arg[strlen("max_childs=")], NULL, 0));
+                       rc = 0;
+                       goto exit;
                } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
                } else if (!strncmp(arg, "max_childs_running=", strlen("max_childs_running="))) {
-                       udev_ctrl_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
+                       udev_ctrl_send_set_max_childs_running(uctrl, strtoul(&arg[strlen("max_childs_running=")], NULL, 0));
+                       rc = 0;
+                       goto exit;
                } else if (!strncmp(arg, "env", strlen("env"))) {
                } else if (!strncmp(arg, "env", strlen("env"))) {
-                       udev_ctrl_set_env(uctrl, &arg[strlen("env=")]);
-               } else {
-                       fprintf(stderr, "unrecognized command '%s'\n", arg);
-                       err(udev, "unrecognized command '%s'\n", arg);
+                       udev_ctrl_send_set_env(uctrl, &arg[strlen("env=")]);
+                       rc = 0;
+                       goto exit;
                }
        }
                }
        }
+
+       if (rc != 0) {
+               fprintf(stderr, "unrecognized command\n");
+               err(udev, "unrecognized command\n");
+       }
 exit:
        udev_ctrl_unref(uctrl);
        return rc;
 exit:
        udev_ctrl_unref(uctrl);
        return rc;
index 0b9b8118097c17c546fa26df19b50f2fb4e8dde8..caaf84ffa7b82136006f93b565ca88516fdd89fd 100644 (file)
@@ -33,7 +33,6 @@
 #include <linux/netlink.h>
 
 #include "udev.h"
 #include <linux/netlink.h>
 
 #include "udev.h"
-#include "udevd.h"
 
 static int uevent_netlink_sock = -1;
 static int udev_monitor_sock = -1;
 
 static int uevent_netlink_sock = -1;
 static int udev_monitor_sock = -1;
index c519f91cd583198eb7b92278212f7d401ce7ec8b..c6093a4311d72f5f2692c0a380753cb08cd15f52 100644 (file)
@@ -30,7 +30,6 @@
 #include <sys/types.h>
 
 #include "udev.h"
 #include <sys/types.h>
 
 #include "udev.h"
-#include "udevd.h"
 
 #define DEFAULT_TIMEOUT                        180
 #define LOOP_PER_SECOND                        20
 
 #define DEFAULT_TIMEOUT                        180
 #define LOOP_PER_SECOND                        20
index 1c57e0a217ff5c6a880970254789355c07c4afe7..fc871ccdcb8290a71b258d732829bfc8caadb8e0 100644 (file)
@@ -34,7 +34,6 @@
 #include <sys/un.h>
 
 #include "udev.h"
 #include <sys/un.h>
 
 #include "udev.h"
-#include "udevd.h"
 #include "udev_rules.h"
 
 static int verbose;
 #include "udev_rules.h"
 
 static int verbose;
index c9ee21a29cee0e81c0ab351468a7d9dfea752009..a711df53d4785c566da08568a5d04ceffc17ee97 100644 (file)
 
 #include "udev.h"
 #include "udev_rules.h"
 
 #include "udev.h"
 #include "udev_rules.h"
-#include "udevd.h"
 #include "udev_selinux.h"
 
 #include "udev_selinux.h"
 
+#define UDEVD_PRIORITY                 -4
+#define UDEV_PRIORITY                  -2
+
+/* maximum limit of forked childs */
+#define UDEVD_MAX_CHILDS               256
+/* start to throttle forking if maximum number of running childs in our session is reached */
+#define UDEVD_MAX_CHILDS_RUNNING       16
+
 static int debug;
 
 static void log_fn(struct udev *udev, int priority,
 static int debug;
 
 static void log_fn(struct udev *udev, int priority,
@@ -85,7 +92,7 @@ struct udevd_uevent_msg {
 
 static int debug_trace;
 static struct udev_rules rules;
 
 static int debug_trace;
 static struct udev_rules rules;
-static int udevd_sock = -1;
+static struct udev_ctrl *udev_ctrl;
 static int uevent_netlink_sock = -1;
 static int inotify_fd = -1;
 static pid_t sid;
 static int uevent_netlink_sock = -1;
 static int inotify_fd = -1;
 static pid_t sid;
@@ -248,7 +255,7 @@ static void udev_event_run(struct udevd_uevent_msg *msg)
        case 0:
                /* child */
                close(uevent_netlink_sock);
        case 0:
                /* child */
                close(uevent_netlink_sock);
-               close(udevd_sock);
+               udev_ctrl_unref(udev_ctrl);
                if (inotify_fd >= 0)
                        close(inotify_fd);
                close(signal_pipe[READ_END]);
                if (inotify_fd >= 0)
                        close(inotify_fd);
                close(signal_pipe[READ_END]);
@@ -665,97 +672,76 @@ static struct udevd_uevent_msg *get_msg_from_envbuf(struct udev *udev, const cha
 }
 
 /* receive the udevd message from userspace */
 }
 
 /* receive the udevd message from userspace */
-static void get_ctrl_msg(struct udev *udev)
+static void handle_ctrl_msg(struct udev_ctrl *uctrl)
 {
 {
-       struct udevd_ctrl_msg ctrl_msg;
-       ssize_t size;
-       struct msghdr smsg;
-       struct cmsghdr *cmsg;
-       struct iovec iov;
-       struct ucred *cred;
-       char cred_msg[CMSG_SPACE(sizeof(struct ucred))];
-       char *pos;
-
-       memset(&ctrl_msg, 0x00, sizeof(struct udevd_ctrl_msg));
-       iov.iov_base = &ctrl_msg;
-       iov.iov_len = sizeof(struct udevd_ctrl_msg);
-
-       memset(&smsg, 0x00, sizeof(struct msghdr));
-       smsg.msg_iov = &iov;
-       smsg.msg_iovlen = 1;
-       smsg.msg_control = cred_msg;
-       smsg.msg_controllen = sizeof(cred_msg);
-
-       size = recvmsg(udevd_sock, &smsg, 0);
-       if (size <  0) {
-               if (errno != EINTR)
-                       err(udev, "unable to receive user udevd message: %s\n", strerror(errno));
-               return;
-       }
-       cmsg = CMSG_FIRSTHDR(&smsg);
-       cred = (struct ucred *) CMSG_DATA(cmsg);
-
-       if (cmsg == NULL || cmsg->cmsg_type != SCM_CREDENTIALS) {
-               err(udev, "no sender credentials received, message ignored\n");
-               return;
-       }
+       struct udev *udev = udev_ctrl_get_udev(uctrl);
+       struct udev_ctrl_msg *ctrl_msg;
+       const char *str;
+       int i;
 
 
-       if (cred->uid != 0) {
-               err(udev, "sender uid=%i, message ignored\n", cred->uid);
+       ctrl_msg = udev_ctrl_receive_msg(uctrl);
+       if (ctrl_msg == NULL)
                return;
                return;
-       }
 
 
-       if (strncmp(ctrl_msg.magic, UDEVD_CTRL_MAGIC, sizeof(UDEVD_CTRL_MAGIC)) != 0 ) {
-               err(udev, "message magic '%s' doesn't match, ignore it\n", ctrl_msg.magic);
-               return;
+       i = udev_ctrl_get_set_log_level(ctrl_msg);
+       if (i >= 0) {
+               info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
+               udev_set_log_priority(udev, i);
+               sprintf(udev_log_env, "UDEV_LOG=%i", i);
+               putenv(udev_log_env);
        }
 
        }
 
-       switch (ctrl_msg.type) {
-       case UDEVD_CTRL_ENV:
-               pos = strchr(ctrl_msg.buf, '=');
-               if (pos == NULL) {
-                       err(udev, "wrong key format '%s'\n", ctrl_msg.buf);
-                       break;
-               }
-               pos[0] = '\0';
-               if (pos[1] == '\0') {
-                       info(udev, "udevd message (ENV) received, unset '%s'\n", ctrl_msg.buf);
-                       unsetenv(ctrl_msg.buf);
-               } else {
-                       info(udev, "udevd message (ENV) received, set '%s=%s'\n", ctrl_msg.buf, &pos[1]);
-                       setenv(ctrl_msg.buf, &pos[1], 1);
-               }
-               break;
-       case UDEVD_CTRL_STOP_EXEC_QUEUE:
+       if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
                info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
                stop_exec_q = 1;
                info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
                stop_exec_q = 1;
-               break;
-       case UDEVD_CTRL_START_EXEC_QUEUE:
+       }
+
+       if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
                info(udev, "udevd message (START_EXEC_QUEUE) received\n");
                stop_exec_q = 0;
                msg_queue_manager(udev);
                info(udev, "udevd message (START_EXEC_QUEUE) received\n");
                stop_exec_q = 0;
                msg_queue_manager(udev);
-               break;
-       case UDEVD_CTRL_SET_LOG_LEVEL:
-               info(udev, "udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", ctrl_msg.intval);
-               udev_set_log_priority(udev, ctrl_msg.intval);
-               sprintf(udev_log_env, "UDEV_LOG=%i", udev_get_log_priority(udev));
-               putenv(udev_log_env);
-               break;
-       case UDEVD_CTRL_SET_MAX_CHILDS:
-               info(udev, "udevd message (UDEVD_SET_MAX_CHILDS) received, max_childs=%i\n", ctrl_msg.intval);
-               max_childs = ctrl_msg.intval;
-               break;
-       case UDEVD_CTRL_SET_MAX_CHILDS_RUNNING:
-               info(udev, "udevd message (UDEVD_SET_MAX_CHILDS_RUNNING) received, max_childs_running=%i\n", ctrl_msg.intval);
-               max_childs_running = ctrl_msg.intval;
-               break;
-       case UDEVD_CTRL_RELOAD_RULES:
+       }
+
+       if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
                info(udev, "udevd message (RELOAD_RULES) received\n");
                reload_config = 1;
                info(udev, "udevd message (RELOAD_RULES) received\n");
                reload_config = 1;
-               break;
-       default:
-               err(udev, "unknown control message type\n");
        }
        }
+
+       str = udev_ctrl_get_set_env(ctrl_msg);
+       if (str != NULL) {
+               char *key = strdup(str);
+               char *val;
+
+               val = strchr(str, '=');
+               if (val != NULL) {
+                       val[0] = '\0';
+                       val = &val[1];
+                       if (val[0] == '\0') {
+                               info(udev, "udevd message (ENV) received, unset '%s'\n", key);
+                               unsetenv(str);
+                       } else {
+                               info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
+                               setenv(key, val, 1);
+                       }
+               } else {
+                       err(udev, "wrong key format '%s'\n", key);
+               }
+               free(key);
+       }
+
+       i = udev_ctrl_get_set_max_childs(ctrl_msg);
+       if (i >= 0) {
+               info(udev, "udevd message (SET_MAX_CHILDS) received, max_childs=%i\n", i);
+               max_childs = i;
+       }
+
+       i = udev_ctrl_get_set_max_childs_running(ctrl_msg);
+       if (i > 0) {
+               info(udev, "udevd message (SET_MAX_CHILDS_RUNNING) received, max_childs_running=%i\n", i);
+               max_childs_running = i;
+       }
+
+       udev_ctrl_msg_unref(ctrl_msg);
 }
 
 /* receive the kernel user event message and do some sanity checks */
 }
 
 /* receive the kernel user event message and do some sanity checks */
@@ -867,42 +853,6 @@ static void reap_sigchilds(void)
        }
 }
 
        }
 }
 
-static int init_udevd_socket(struct udev *udev)
-{
-       struct sockaddr_un saddr;
-       socklen_t addrlen;
-       const int feature_on = 1;
-       int retval;
-
-       memset(&saddr, 0x00, sizeof(saddr));
-       saddr.sun_family = AF_LOCAL;
-       strcpy(saddr.sun_path, UDEVD_CTRL_SOCK_PATH);
-       addrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
-       /* translate leading '@' to abstract namespace */
-       if (saddr.sun_path[0] == '@')
-               saddr.sun_path[0] = '\0';
-
-       udevd_sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
-       if (udevd_sock == -1) {
-               err(udev, "error getting socket: %s\n", strerror(errno));
-               return -1;
-       }
-
-       /* the bind takes care of ensuring only one copy running */
-       retval = bind(udevd_sock, (struct sockaddr *) &saddr, addrlen);
-       if (retval < 0) {
-               err(udev, "bind failed: %s\n", strerror(errno));
-               close(udevd_sock);
-               udevd_sock = -1;
-               return -1;
-       }
-
-       /* enable receiving of the sender credentials */
-       setsockopt(udevd_sock, SOL_SOCKET, SO_PASSCRED, &feature_on, sizeof(feature_on));
-
-       return 0;
-}
-
 static int init_uevent_netlink_sock(struct udev *udev)
 {
        struct sockaddr_nl snl;
 static int init_uevent_netlink_sock(struct udev *udev)
 {
        struct sockaddr_nl snl;
@@ -1040,17 +990,19 @@ int main(int argc, char *argv[])
        if (write(STDERR_FILENO, 0, 0) < 0)
                dup2(fd, STDERR_FILENO);
 
        if (write(STDERR_FILENO, 0, 0) < 0)
                dup2(fd, STDERR_FILENO);
 
-       /* init sockets to receive events */
-       if (init_udevd_socket(udev) < 0) {
-               if (errno == EADDRINUSE) {
-                       fprintf(stderr, "another udev daemon already running\n");
-                       err(udev, "another udev daemon already running\n");
-                       rc = 1;
-               } else {
-                       fprintf(stderr, "error initializing udevd socket\n");
-                       err(udev, "error initializing udevd socket\n");
-                       rc = 2;
-               }
+       /* init control socket, bind() ensures, that only one udevd instance is running */
+       udev_ctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
+       if (udev_ctrl == NULL) {
+               fprintf(stderr, "error initializing control socket");
+               err(udev, "error initializing udevd socket");
+               rc = 1;
+               goto exit;
+       }
+
+       if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
+               fprintf(stderr, "error binding control socket, seems udevd is already running\n");
+               err(udev, "error binding control socket, seems udevd is already running\n");
+               rc = 1;
                goto exit;
        }
 
                goto exit;
        }
 
@@ -1061,7 +1013,6 @@ int main(int argc, char *argv[])
                goto exit;
        }
 
                goto exit;
        }
 
-       /* setup signal handler pipe */
        retval = pipe(signal_pipe);
        if (retval < 0) {
                err(udev, "error getting pipes: %s\n", strerror(errno));
        retval = pipe(signal_pipe);
        if (retval < 0) {
                err(udev, "error getting pipes: %s\n", strerror(errno));
@@ -1115,10 +1066,11 @@ int main(int argc, char *argv[])
                }
        }
 
                }
        }
 
-       /* redirect std{out,err} fd's */
-       if (!debug)
+       /* redirect std{out,err} */
+       if (!debug) {
                dup2(fd, STDOUT_FILENO);
                dup2(fd, STDOUT_FILENO);
-       dup2(fd, STDERR_FILENO);
+               dup2(fd, STDERR_FILENO);
+       }
        if (fd > STDERR_FILENO)
                close(fd);
 
        if (fd > STDERR_FILENO)
                close(fd);
 
@@ -1219,7 +1171,7 @@ int main(int argc, char *argv[])
        if (debug_trace)
                putenv("DEBUG=1");
 
        if (debug_trace)
                putenv("DEBUG=1");
 
-       maxfd = udevd_sock;
+       maxfd = udev_ctrl_get_fd(udev_ctrl);
        maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
        maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
        maxfd = UDEV_MAX(maxfd, inotify_fd);
        maxfd = UDEV_MAX(maxfd, uevent_netlink_sock);
        maxfd = UDEV_MAX(maxfd, signal_pipe[READ_END]);
        maxfd = UDEV_MAX(maxfd, inotify_fd);
@@ -1230,7 +1182,7 @@ int main(int argc, char *argv[])
 
                FD_ZERO(&readfds);
                FD_SET(signal_pipe[READ_END], &readfds);
 
                FD_ZERO(&readfds);
                FD_SET(signal_pipe[READ_END], &readfds);
-               FD_SET(udevd_sock, &readfds);
+               FD_SET(udev_ctrl_get_fd(udev_ctrl), &readfds);
                FD_SET(uevent_netlink_sock, &readfds);
                if (inotify_fd >= 0)
                        FD_SET(inotify_fd, &readfds);
                FD_SET(uevent_netlink_sock, &readfds);
                if (inotify_fd >= 0)
                        FD_SET(inotify_fd, &readfds);
@@ -1243,8 +1195,8 @@ int main(int argc, char *argv[])
                }
 
                /* get control message */
                }
 
                /* get control message */
-               if (FD_ISSET(udevd_sock, &readfds))
-                       get_ctrl_msg(udev);
+               if (FD_ISSET(udev_ctrl_get_fd(udev_ctrl), &readfds))
+                       handle_ctrl_msg(udev_ctrl);
 
                /* get netlink message */
                if (FD_ISSET(uevent_netlink_sock, &readfds)) {
 
                /* get netlink message */
                if (FD_ISSET(uevent_netlink_sock, &readfds)) {
@@ -1311,8 +1263,7 @@ exit:
        if (signal_pipe[WRITE_END] >= 0)
                close(signal_pipe[WRITE_END]);
 
        if (signal_pipe[WRITE_END] >= 0)
                close(signal_pipe[WRITE_END]);
 
-       if (udevd_sock >= 0)
-               close(udevd_sock);
+       udev_ctrl_unref(udev_ctrl);
        if (inotify_fd >= 0)
                close(inotify_fd);
        if (uevent_netlink_sock >= 0)
        if (inotify_fd >= 0)
                close(inotify_fd);
        if (uevent_netlink_sock >= 0)
diff --git a/udev/udevd.h b/udev/udevd.h
deleted file mode 100644 (file)
index 63c9951..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Copyright (C) 2004 Ling, Xiaofeng <xiaofeng.ling@intel.com>
- * Copyright (C) 2004-2006 Kay Sievers <kay.sievers@vrfy.org>
- *
- *     This program is free software; you can redistribute it and/or modify it
- *     under the terms of the GNU General Public License as published by the
- *     Free Software Foundation version 2 of the License.
- * 
- *     This program is distributed in the hope that it will be useful, but
- *     WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- *     General Public License for more details.
- * 
- *     You should have received a copy of the GNU General Public License along
- *     with this program; if not, write to the Free Software Foundation, Inc.,
- *     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- *
- */
-
-#include "list.h"
-
-#define UDEVD_PRIORITY                 -4
-#define UDEV_PRIORITY                  -2
-
-/* maximum limit of forked childs */
-#define UDEVD_MAX_CHILDS               256
-/* start to throttle forking if maximum number of running childs in our session is reached */
-#define UDEVD_MAX_CHILDS_RUNNING       16
-
-/* linux/include/linux/kobject.h */
-#define UEVENT_BUFFER_SIZE             2048
-#define UEVENT_NUM_ENVP                        32
-
-#define UDEVD_CTRL_SOCK_PATH           "@" UDEV_PREFIX "/org/kernel/udev/udevd"
-#define UDEVD_CTRL_MAGIC               "udevd-128"
-
-enum udevd_ctrl_msg_type {
-       UDEVD_CTRL_UNKNOWN,
-       UDEVD_CTRL_SET_LOG_LEVEL,
-       UDEVD_CTRL_STOP_EXEC_QUEUE,
-       UDEVD_CTRL_START_EXEC_QUEUE,
-       UDEVD_CTRL_RELOAD_RULES,
-       UDEVD_CTRL_ENV,
-       UDEVD_CTRL_SET_MAX_CHILDS,
-       UDEVD_CTRL_SET_MAX_CHILDS_RUNNING,
-};
-
-struct udevd_ctrl_msg {
-       char magic[32];
-       enum udevd_ctrl_msg_type type;
-       union {
-               int intval;
-               char buf[256];
-       };
-};
-