chiark / gitweb /
udevd: add sd-daemon.c
[elogind.git] / udev / udevd.c
index e38c6016499048f0cabda9f10100efad47b702e3..2efab5b39d882d65b9413b29afc3f09a818e9174 100644 (file)
@@ -45,6 +45,7 @@
 #include <sys/utsname.h>
 
 #include "udev.h"
+#include "sd-daemon.h"
 
 #define UDEVD_PRIORITY                 -4
 #define UDEV_PRIORITY                  -2
@@ -227,8 +228,10 @@ static void worker_new(struct event *event)
        udev_monitor_enable_receiving(worker_monitor);
 
        worker = calloc(1, sizeof(struct worker));
-       if (worker == NULL)
+       if (worker == NULL) {
+               udev_monitor_unref(worker_monitor);
                return;
+       }
        /* worker + event reference */
        worker->refcount = 2;
        worker->udev = event->udev;
@@ -400,13 +403,13 @@ static void event_run(struct event *event, bool force)
        worker_new(event);
 }
 
-static void event_queue_insert(struct udev_device *dev)
+static int event_queue_insert(struct udev_device *dev)
 {
        struct event *event;
 
        event = calloc(1, sizeof(struct event));
        if (event == NULL)
-               return;
+               return -1;
 
        event->udev = udev_device_get_udev(dev);
        event->dev = dev;
@@ -427,8 +430,10 @@ static void event_queue_insert(struct udev_device *dev)
        /* run all events with a timeout set immediately */
        if (udev_device_get_timeout(dev) > 0) {
                event_run(event, true);
-               return;
+               return 0;
        }
+
+       return 0;
 }
 
 static void worker_kill(struct udev *udev, int retain)
@@ -896,10 +901,14 @@ static void static_dev_create_links(struct udev *udev, DIR *dir)
        unsigned int i;
 
        for (i = 0; i < ARRAY_SIZE(stdlinks); i++) {
-               udev_selinux_setfscreateconat(udev, dirfd(dir), stdlinks[i].link, S_IFLNK);
-               if (symlinkat(stdlinks[i].target, dirfd(dir), stdlinks[i].link) < 0 && errno == EEXIST)
-                       utimensat(dirfd(dir), stdlinks[i].link, NULL, AT_SYMLINK_NOFOLLOW);
-               udev_selinux_resetfscreatecon(udev);
+               struct stat sb;
+
+               if (stat(stdlinks[i].target, &sb) == 0) {
+                       udev_selinux_setfscreateconat(udev, dirfd(dir), stdlinks[i].link, S_IFLNK);
+                       if (symlinkat(stdlinks[i].target, dirfd(dir), stdlinks[i].link) < 0 && errno == EEXIST)
+                               utimensat(dirfd(dir), stdlinks[i].link, NULL, AT_SYMLINK_NOFOLLOW);
+                       udev_selinux_resetfscreatecon(udev);
+               }
        }
 }
 
@@ -951,81 +960,6 @@ static int mem_size_mb(void)
        return memsize;
 }
 
-static int init_notify(const char *state)
-{
-       int fd = -1, r;
-       struct msghdr msghdr;
-       struct iovec iovec;
-       struct ucred *ucred;
-       union {
-               struct sockaddr sa;
-               struct sockaddr_un un;
-       } sockaddr;
-       union {
-               struct cmsghdr cmsghdr;
-               uint8_t buf[CMSG_SPACE(sizeof(struct ucred))];
-       } control;
-       const char *e;
-
-       if (!(e = getenv("NOTIFY_SOCKET"))) {
-               r = 0;
-               goto finish;
-       }
-
-       /* Must be an abstract socket, or an absolute path */
-       if ((e[0] != '@' && e[0] != '/') || e[1] == 0) {
-               r = -EINVAL;
-               goto finish;
-       }
-
-       if ((fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0)) < 0) {
-               r = -errno;
-               goto finish;
-       }
-
-       memset(&sockaddr, 0, sizeof(sockaddr));
-       sockaddr.sa.sa_family = AF_UNIX;
-       strncpy(sockaddr.un.sun_path, e, sizeof(sockaddr.un.sun_path));
-
-       if (sockaddr.un.sun_path[0] == '@')
-               sockaddr.un.sun_path[0] = 0;
-
-       memset(&iovec, 0, sizeof(iovec));
-       iovec.iov_base = (char*) state;
-       iovec.iov_len = strlen(state);
-
-       memset(&control, 0, sizeof(control));
-       control.cmsghdr.cmsg_level = SOL_SOCKET;
-       control.cmsghdr.cmsg_type = SCM_CREDENTIALS;
-       control.cmsghdr.cmsg_len = CMSG_LEN(sizeof(struct ucred));
-
-       ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr);
-       ucred->pid = getpid();
-       ucred->uid = getuid();
-       ucred->gid = getgid();
-
-       memset(&msghdr, 0, sizeof(msghdr));
-       msghdr.msg_name = &sockaddr;
-       msghdr.msg_namelen = sizeof(struct sockaddr_un);
-       msghdr.msg_iov = &iovec;
-       msghdr.msg_iovlen = 1;
-       msghdr.msg_control = &control;
-       msghdr.msg_controllen = control.cmsghdr.cmsg_len;
-
-       if (sendmsg(fd, &msghdr, MSG_NOSIGNAL) < 0) {
-               r = -errno;
-               goto finish;
-       }
-
-       r = 0;
-
-finish:
-       if (fd >= 0)
-               close(fd);
-
-       return r;
-}
-
 int main(int argc, char *argv[])
 {
        struct udev *udev;
@@ -1058,7 +992,7 @@ int main(int argc, char *argv[])
        for (;;) {
                int option;
 
-               option = getopt_long(argc, argv, "cdeDthV", options, NULL);
+               option = getopt_long(argc, argv, "c:deDthV", options, NULL);
                if (option == -1)
                        break;
 
@@ -1278,7 +1212,7 @@ int main(int argc, char *argv[])
                        goto exit;
                }
        } else {
-               init_notify("READY=1");
+               sd_notify(1, "READY=1");
        }
 
        /* set scheduling priority for the main daemon process */
@@ -1288,16 +1222,23 @@ int main(int argc, char *argv[])
 
        f = fopen("/dev/kmsg", "w");
        if (f != NULL) {
-               fprintf(f, "<6>udev: starting version " VERSION "\n");
+               fprintf(f, "<6>udev[%u]: starting version " VERSION "\n", getpid());
                fclose(f);
        }
 
-       /* OOM_DISABLE == -17 */
-       fd = open("/proc/self/oom_adj", O_RDWR);
+       fd = open("/proc/self/oom_score_adj", O_RDWR);
        if (fd < 0) {
-               err(udev, "error disabling OOM: %m\n");
+               /* Fallback to old interface */
+               fd = open("/proc/self/oom_adj", O_RDWR);
+               if (fd < 0) {
+                       err(udev, "error disabling OOM: %m\n");
+               } else {
+                       /* OOM_DISABLE == -17 */
+                       write(fd, "-17", 3);
+                       close(fd);
+               }
        } else {
-               write(fd, "-17", 3);
+               write(fd, "-1000", 5);
                close(fd);
        }
 
@@ -1347,9 +1288,8 @@ int main(int argc, char *argv[])
 
                        dev = udev_monitor_receive_device(monitor);
                        if (dev != NULL)
-                               event_queue_insert(dev);
-                       else
-                               udev_device_unref(dev);
+                               if (event_queue_insert(dev) < 0)
+                                       udev_device_unref(dev);
                }
 
                /* start new events */