chiark / gitweb /
static-nodes: move creation of static nodes from udevd to tmpfiles
[elogind.git] / src / udev / udevd.c
index 589a8fe75333509cab9e7dd277e501f6f7b2d634..c4127cd03b45017309229b554b0d575e4b21ee8a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004-2012 Kay Sievers <kay.sievers@vrfy.org>
+ * Copyright (C) 2004-2012 Kay Sievers <kay@vrfy.org>
  * Copyright (C) 2004 Chris Friesen <chris_friesen@sympatico.ca>
  * Copyright (C) 2009 Canonical Ltd.
  * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
@@ -46,6 +46,9 @@
 
 #include "udev.h"
 #include "sd-daemon.h"
+#include "cgroup-util.h"
+#include "dev-setup.h"
+#include "fileio.h"
 
 static bool debug;
 
@@ -72,6 +75,7 @@ static int exec_delay;
 static sigset_t sigmask_orig;
 static UDEV_LIST(event_list);
 static UDEV_LIST(worker_list);
+char *udev_cgroup;
 static bool udev_exit;
 
 enum event_state {
@@ -92,17 +96,16 @@ struct event {
         size_t devpath_len;
         const char *devpath_old;
         dev_t devnum;
-        bool is_block;
         int ifindex;
+        bool is_block;
+#ifdef HAVE_FIRMWARE
+        bool nodelay;
+#endif
 };
 
-static struct event *node_to_event(struct udev_list_node *node)
+static inline struct event *node_to_event(struct udev_list_node *node)
 {
-        char *event;
-
-        event = (char *)node;
-        event -= offsetof(struct event, node);
-        return (struct event *)event;
+        return container_of(node, struct event, node);
 }
 
 static void event_queue_cleanup(struct udev *udev, enum event_state type);
@@ -122,7 +125,7 @@ struct worker {
         struct udev_monitor *monitor;
         enum worker_state state;
         struct event *event;
-        unsigned long long event_start_usec;
+        usec_t event_start_usec;
 };
 
 /* passed from worker to main process */
@@ -131,13 +134,9 @@ struct worker_message {
         int exitcode;
 };
 
-static struct worker *node_to_worker(struct udev_list_node *node)
+static inline struct worker *node_to_worker(struct udev_list_node *node)
 {
-        char *worker;
-
-        worker = (char *)node;
-        worker -= offsetof(struct worker, node);
-        return (struct worker *)worker;
+        return container_of(node, struct worker, node);
 }
 
 static void event_queue_delete(struct event *event, bool export)
@@ -267,6 +266,9 @@ static void worker_new(struct event *event)
                 /* request TERM signal if parent exits */
                 prctl(PR_SET_PDEATHSIG, SIGTERM);
 
+                /* reset OOM score, we only protect the main daemon */
+                write_string_file("/proc/self/oom_score_adj", "0");
+
                 for (;;) {
                         struct udev_event *udev_event;
                         struct worker_message msg;
@@ -325,11 +327,10 @@ static void worker_new(struct event *event)
                                 int fdcount;
                                 int i;
 
-                                fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), -1);
+                                fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), -1);
                                 if (fdcount < 0) {
                                         if (errno == EINTR)
                                                 continue;
-                                        err = -errno;
                                         log_error("failed to poll: %m\n");
                                         goto out;
                                 }
@@ -380,7 +381,7 @@ out:
                 worker->monitor = worker_monitor;
                 worker->pid = pid;
                 worker->state = WORKER_RUNNING;
-                worker->event_start_usec = now_usec();
+                worker->event_start_usec = now(CLOCK_MONOTONIC);
                 worker->event = event;
                 event->state = EVENT_RUNNING;
                 udev_list_node_append(&worker->node, &worker_list);
@@ -411,7 +412,7 @@ static void event_run(struct event *event)
                 worker_ref(worker);
                 worker->event = event;
                 worker->state = WORKER_RUNNING;
-                worker->event_start_usec = now_usec();
+                worker->event_start_usec = now(CLOCK_MONOTONIC);
                 event->state = EVENT_RUNNING;
                 return;
         }
@@ -441,8 +442,12 @@ static int event_queue_insert(struct udev_device *dev)
         event->devpath_len = strlen(event->devpath);
         event->devpath_old = udev_device_get_devpath_old(dev);
         event->devnum = udev_device_get_devnum(dev);
-        event->is_block = (strcmp("block", udev_device_get_subsystem(dev)) == 0);
+        event->is_block = streq("block", udev_device_get_subsystem(dev));
         event->ifindex = udev_device_get_ifindex(dev);
+#ifdef HAVE_FIRMWARE
+        if (streq(udev_device_get_subsystem(dev), "firmware"))
+                event->nodelay = true;
+#endif
 
         udev_queue_export_device_queued(udev_queue_export, dev);
         log_debug("seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
@@ -453,22 +458,13 @@ static int event_queue_insert(struct udev_device *dev)
         return 0;
 }
 
-static void worker_kill(struct udev *udev, int retain)
+static void worker_kill(struct udev *udev)
 {
         struct udev_list_node *loop;
-        int max;
-
-        if (children <= retain)
-                return;
-
-        max = children - retain;
 
         udev_list_node_foreach(loop, &worker_list) {
                 struct worker *worker = node_to_worker(loop);
 
-                if (max-- <= 0)
-                        break;
-
                 if (worker->state == WORKER_KILLED)
                         continue;
 
@@ -508,7 +504,7 @@ static bool is_devpath_busy(struct event *event)
                         return true;
 
                 /* check our old name */
-                if (event->devpath_old != NULL && strcmp(loop_event->devpath, event->devpath_old) == 0) {
+                if (event->devpath_old != NULL && streq(loop_event->devpath, event->devpath_old)) {
                         event->delaying_seqnum = loop_event->seqnum;
                         return true;
                 }
@@ -531,6 +527,12 @@ static bool is_devpath_busy(struct event *event)
                         return true;
                 }
 
+#ifdef HAVE_FIRMWARE
+                /* allow to bypass the dependency tracking */
+                if (event->nodelay)
+                        continue;
+#endif
+
                 /* parent device event found */
                 if (event->devpath[common] == '/') {
                         event->delaying_seqnum = loop_event->seqnum;
@@ -636,7 +638,7 @@ static struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl)
                 log_debug("udevd message (SET_LOG_PRIORITY) received, log_priority=%i\n", i);
                 log_set_max_level(i);
                 udev_set_log_priority(udev, i);
-                worker_kill(udev, 0);
+                worker_kill(udev);
         }
 
         if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
@@ -678,7 +680,7 @@ static struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl)
                         }
                         free(key);
                 }
-                worker_kill(udev, 0);
+                worker_kill(udev);
         }
 
         i = udev_ctrl_get_set_children_max(ctrl_msg);
@@ -731,7 +733,7 @@ static int handle_inotify(struct udev *udev)
                                 int fd;
 
                                 log_debug("device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
-                                util_strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
+                                strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
                                 fd = open(filename, O_WRONLY);
                                 if (fd >= 0) {
                                         if (write(fd, "change", 6) < 0)
@@ -810,302 +812,6 @@ static void handle_signal(struct udev *udev, int signo)
         }
 }
 
-static void static_dev_create_from_modules(struct udev *udev)
-{
-        struct utsname kernel;
-        char modules[UTIL_PATH_SIZE];
-        char buf[4096];
-        FILE *f;
-
-        uname(&kernel);
-        util_strscpyl(modules, sizeof(modules), "/lib/modules/", kernel.release, "/modules.devname", NULL);
-        f = fopen(modules, "r");
-        if (f == NULL)
-                return;
-
-        while (fgets(buf, sizeof(buf), f) != NULL) {
-                char *s;
-                const char *modname;
-                const char *devname;
-                const char *devno;
-                int maj, min;
-                char type;
-                mode_t mode;
-                char filename[UTIL_PATH_SIZE];
-
-                if (buf[0] == '#')
-                        continue;
-
-                modname = buf;
-                s = strchr(modname, ' ');
-                if (s == NULL)
-                        continue;
-                s[0] = '\0';
-
-                devname = &s[1];
-                s = strchr(devname, ' ');
-                if (s == NULL)
-                        continue;
-                s[0] = '\0';
-
-                devno = &s[1];
-                s = strchr(devno, ' ');
-                if (s == NULL)
-                        s = strchr(devno, '\n');
-                if (s != NULL)
-                        s[0] = '\0';
-                if (sscanf(devno, "%c%u:%u", &type, &maj, &min) != 3)
-                        continue;
-
-                if (type == 'c')
-                        mode = S_IFCHR;
-                else if (type == 'b')
-                        mode = S_IFBLK;
-                else
-                        continue;
-
-                util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/", devname, NULL);
-                util_create_path_selinux(udev, filename);
-                udev_selinux_setfscreatecon(udev, filename, mode);
-                log_debug("mknod '%s' %c%u:%u\n", filename, type, maj, min);
-                if (mknod(filename, mode, makedev(maj, min)) < 0 && errno == EEXIST)
-                        utimensat(AT_FDCWD, filename, NULL, 0);
-                udev_selinux_resetfscreatecon(udev);
-        }
-
-        fclose(f);
-}
-
-static int copy_dev_dir(struct udev *udev, DIR *dir_from, DIR *dir_to, int maxdepth)
-{
-        struct dirent *dent;
-
-        for (dent = readdir(dir_from); dent != NULL; dent = readdir(dir_from)) {
-                struct stat stats;
-
-                if (dent->d_name[0] == '.')
-                        continue;
-                if (fstatat(dirfd(dir_from), dent->d_name, &stats, AT_SYMLINK_NOFOLLOW) != 0)
-                        continue;
-
-                if (S_ISBLK(stats.st_mode) || S_ISCHR(stats.st_mode)) {
-                        udev_selinux_setfscreateconat(udev, dirfd(dir_to), dent->d_name, stats.st_mode & 0777);
-                        if (mknodat(dirfd(dir_to), dent->d_name, stats.st_mode, stats.st_rdev) == 0) {
-                                fchmodat(dirfd(dir_to), dent->d_name, stats.st_mode & 0777, 0);
-                                fchownat(dirfd(dir_to), dent->d_name, stats.st_uid, stats.st_gid, 0);
-                        } else {
-                                utimensat(dirfd(dir_to), dent->d_name, NULL, 0);
-                        }
-                        udev_selinux_resetfscreatecon(udev);
-                } else if (S_ISLNK(stats.st_mode)) {
-                        char target[UTIL_PATH_SIZE];
-                        ssize_t len;
-
-                        len = readlinkat(dirfd(dir_from), dent->d_name, target, sizeof(target));
-                        if (len <= 0 || len == (ssize_t)sizeof(target))
-                                continue;
-                        target[len] = '\0';
-                        udev_selinux_setfscreateconat(udev, dirfd(dir_to), dent->d_name, S_IFLNK);
-                        if (symlinkat(target, dirfd(dir_to), dent->d_name) < 0 && errno == EEXIST)
-                                utimensat(dirfd(dir_to), dent->d_name, NULL, AT_SYMLINK_NOFOLLOW);
-                        udev_selinux_resetfscreatecon(udev);
-                } else if (S_ISDIR(stats.st_mode)) {
-                        DIR *dir2_from, *dir2_to;
-
-                        if (maxdepth == 0)
-                                continue;
-
-                        udev_selinux_setfscreateconat(udev, dirfd(dir_to), dent->d_name, S_IFDIR|0755);
-                        mkdirat(dirfd(dir_to), dent->d_name, 0755);
-                        udev_selinux_resetfscreatecon(udev);
-
-                        dir2_to = fdopendir(openat(dirfd(dir_to), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
-                        if (dir2_to == NULL)
-                                continue;
-
-                        dir2_from = fdopendir(openat(dirfd(dir_from), dent->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC));
-                        if (dir2_from == NULL) {
-                                closedir(dir2_to);
-                                continue;
-                        }
-
-                        copy_dev_dir(udev, dir2_from, dir2_to, maxdepth-1);
-
-                        closedir(dir2_to);
-                        closedir(dir2_from);
-                }
-        }
-
-        return 0;
-}
-
-static void static_dev_create_links(struct udev *udev, DIR *dir)
-{
-        struct stdlinks {
-                const char *link;
-                const char *target;
-        };
-        static const struct stdlinks stdlinks[] = {
-                { "core", "/proc/kcore" },
-                { "fd", "/proc/self/fd" },
-                { "stdin", "/proc/self/fd/0" },
-                { "stdout", "/proc/self/fd/1" },
-                { "stderr", "/proc/self/fd/2" },
-        };
-        unsigned int i;
-
-        for (i = 0; i < ARRAY_SIZE(stdlinks); i++) {
-                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);
-                }
-        }
-}
-
-static void static_dev_create_from_devices(struct udev *udev, DIR *dir)
-{
-        DIR *dir_from;
-
-        dir_from = opendir(UDEVLIBEXECDIR "/devices");
-        if (dir_from == NULL)
-                return;
-        copy_dev_dir(udev, dir_from, dir, 8);
-        closedir(dir_from);
-}
-
-static void static_dev_create(struct udev *udev)
-{
-        DIR *dir;
-
-        dir = opendir(udev_get_dev_path(udev));
-        if (dir == NULL)
-                return;
-
-        static_dev_create_links(udev, dir);
-        static_dev_create_from_devices(udev, dir);
-
-        closedir(dir);
-}
-
-static int mem_size_mb(void)
-{
-        FILE *f;
-        char buf[4096];
-        long int memsize = -1;
-
-        f = fopen("/proc/meminfo", "r");
-        if (f == NULL)
-                return -1;
-
-        while (fgets(buf, sizeof(buf), f) != NULL) {
-                long int value;
-
-                if (sscanf(buf, "MemTotal: %ld kB", &value) == 1) {
-                        memsize = value / 1024;
-                        break;
-                }
-        }
-
-        fclose(f);
-        return memsize;
-}
-
-static int convert_db(struct udev *udev)
-{
-        char filename[UTIL_PATH_SIZE];
-        FILE *f;
-        struct udev_enumerate *udev_enumerate;
-        struct udev_list_entry *list_entry;
-
-        /* current database */
-        util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev), "/data", NULL);
-        if (access(filename, F_OK) >= 0)
-                return 0;
-
-        /* make sure we do not get here again */
-        util_create_path(udev, filename);
-        mkdir(filename, 0755);
-
-        /* old database */
-        util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev), "/.udev/db", NULL);
-        if (access(filename, F_OK) < 0)
-                return 0;
-
-        f = fopen("/dev/kmsg", "w");
-        if (f != NULL) {
-                fprintf(f, "<30>udevd[%u]: converting old udev database\n", getpid());
-                fclose(f);
-        }
-
-        udev_enumerate = udev_enumerate_new(udev);
-        if (udev_enumerate == NULL)
-                return -1;
-        udev_enumerate_scan_devices(udev_enumerate);
-        udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
-                struct udev_device *device;
-
-                device = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
-                if (device == NULL)
-                        continue;
-
-                /* try to find the old database for devices without a current one */
-                if (udev_device_read_db(device, NULL) < 0) {
-                        bool have_db;
-                        const char *id;
-                        struct stat stats;
-                        char devpath[UTIL_PATH_SIZE];
-                        char from[UTIL_PATH_SIZE];
-
-                        have_db = false;
-
-                        /* find database in old location */
-                        id = udev_device_get_id_filename(device);
-                        util_strscpyl(from, sizeof(from), udev_get_dev_path(udev), "/.udev/db/", id, NULL);
-                        if (lstat(from, &stats) == 0) {
-                                if (!have_db) {
-                                        udev_device_read_db(device, from);
-                                        have_db = true;
-                                }
-                                unlink(from);
-                        }
-
-                        /* find old database with $subsys:$sysname name */
-                        util_strscpyl(from, sizeof(from), udev_get_dev_path(udev),
-                                     "/.udev/db/", udev_device_get_subsystem(device), ":",
-                                     udev_device_get_sysname(device), NULL);
-                        if (lstat(from, &stats) == 0) {
-                                if (!have_db) {
-                                        udev_device_read_db(device, from);
-                                        have_db = true;
-                                }
-                                unlink(from);
-                        }
-
-                        /* find old database with the encoded devpath name */
-                        util_path_encode(udev_device_get_devpath(device), devpath, sizeof(devpath));
-                        util_strscpyl(from, sizeof(from), udev_get_dev_path(udev), "/.udev/db/", devpath, NULL);
-                        if (lstat(from, &stats) == 0) {
-                                if (!have_db) {
-                                        udev_device_read_db(device, from);
-                                        have_db = true;
-                                }
-                                unlink(from);
-                        }
-
-                        /* write out new database */
-                        if (have_db)
-                                udev_device_update_db(device);
-                }
-                udev_device_unref(device);
-        }
-        udev_enumerate_unref(udev_enumerate);
-        return 0;
-}
-
 static int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink)
 {
         int ctrl = -1, netlink = -1;
@@ -1142,40 +848,54 @@ static int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink)
         return 0;
 }
 
-static bool check_rules_timestamp(struct udev *udev)
+/*
+ * read the kernel commandline, in case we need to get into debug mode
+ *   udev.log-priority=<level>              syslog priority
+ *   udev.children-max=<number of workers>  events are fully serialized if set to 1
+ *   udev.exec-delay=<number of seconds>    delay execution of every executed program
+ */
+static void kernel_cmdline_options(struct udev *udev)
 {
-        char **p;
-        unsigned long long *stamp_usec;
-        int i, n;
-        bool changed = false;
+        char *line, *w, *state;
+        size_t l;
 
-        n = udev_get_rules_path(udev, &p, &stamp_usec);
-        for (i = 0; i < n; i++) {
-                struct stat stats;
+        if (read_one_line_file("/proc/cmdline", &line) < 0)
+                return;
 
-                if (stat(p[i], &stats) < 0)
-                        continue;
+        FOREACH_WORD_QUOTED(w, l, line, state) {
+                char *s, *opt;
 
-                if (stamp_usec[i] == ts_usec(&stats.st_mtim))
-                        continue;
+                s = strndup(w, l);
+                if (!s)
+                        break;
 
-                /* first check */
-                if (stamp_usec[i] != 0) {
-                        log_debug("reload - timestamp of '%s' changed\n", p[i]);
-                        changed = true;
+                /* accept the same options for the initrd, prefixed with "rd." */
+                if (in_initrd() && startswith(s, "rd."))
+                        opt = s + 3;
+                else
+                        opt = s;
+
+                if (startswith(opt, "udev.log-priority=")) {
+                        int prio;
+
+                        prio = util_log_priority(opt + 18);
+                        log_set_max_level(prio);
+                        udev_set_log_priority(udev, prio);
+                } else if (startswith(opt, "udev.children-max=")) {
+                        children_max = strtoul(opt + 18, NULL, 0);
+                } else if (startswith(opt, "udev.exec-delay=")) {
+                        exec_delay = strtoul(opt + 16, NULL, 0);
                 }
 
-                /* update timestamp */
-                stamp_usec[i] = ts_usec(&stats.st_mtim);
+                free(s);
         }
 
-        return changed;
+        free(line);
 }
 
 int main(int argc, char *argv[])
 {
         struct udev *udev;
-        FILE *f;
         sigset_t mask;
         int daemonize = false;
         int resolve_names = 1;
@@ -1194,23 +914,23 @@ int main(int argc, char *argv[])
         int fd_worker = -1;
         struct epoll_event ep_ctrl, ep_inotify, ep_signal, ep_netlink, ep_worker;
         struct udev_ctrl_connection *ctrl_conn = NULL;
-        char **s;
         int rc = 1;
 
         udev = udev_new();
         if (udev == NULL)
                 goto exit;
 
-        log_open();
+        log_set_target(LOG_TARGET_AUTO);
         log_parse_environment();
+        log_open();
         udev_set_log_fn(udev, udev_main_log);
         log_debug("version %s\n", VERSION);
-        udev_selinux_init(udev);
+        label_init("/dev");
 
         for (;;) {
                 int option;
 
-                option = getopt_long(argc, argv, "c:deDtN:hV", options, NULL);
+                option = getopt_long(argc, argv, "c:de:DtN:hV", options, NULL);
                 if (option == -1)
                         break;
 
@@ -1226,15 +946,15 @@ int main(int argc, char *argv[])
                         break;
                 case 'D':
                         debug = true;
-                        if (udev_get_log_priority(udev) < LOG_INFO)
-                                udev_set_log_priority(udev, LOG_INFO);
+                        log_set_max_level(LOG_DEBUG);
+                        udev_set_log_priority(udev, LOG_DEBUG);
                         break;
                 case 'N':
-                        if (strcmp (optarg, "early") == 0) {
+                        if (streq(optarg, "early")) {
                                 resolve_names = 1;
-                        } else if (strcmp (optarg, "late") == 0) {
+                        } else if (streq(optarg, "late")) {
                                 resolve_names = 0;
-                        } else if (strcmp (optarg, "never") == 0) {
+                        } else if (streq(optarg, "never")) {
                                 resolve_names = -1;
                         } else {
                                 fprintf(stderr, "resolve-names must be early, late or never\n");
@@ -1261,39 +981,7 @@ int main(int argc, char *argv[])
                 }
         }
 
-        /*
-         * read the kernel commandline, in case we need to get into debug mode
-         *   udev.log-priority=<level>              syslog priority
-         *   udev.children-max=<number of workers>  events are fully serialized if set to 1
-         *
-         */
-        f = fopen("/proc/cmdline", "r");
-        if (f != NULL) {
-                char cmdline[4096];
-
-                if (fgets(cmdline, sizeof(cmdline), f) != NULL) {
-                        char *pos;
-
-                        pos = strstr(cmdline, "udev.log-priority=");
-                        if (pos != NULL) {
-                                pos += strlen("udev.log-priority=");
-                                udev_set_log_priority(udev, util_log_priority(pos));
-                        }
-
-                        pos = strstr(cmdline, "udev.children-max=");
-                        if (pos != NULL) {
-                                pos += strlen("udev.children-max=");
-                                children_max = strtoul(pos, NULL, 0);
-                        }
-
-                        pos = strstr(cmdline, "udev.exec-delay=");
-                        if (pos != NULL) {
-                                pos += strlen("udev.exec-delay=");
-                                exec_delay = strtoul(pos, NULL, 0);
-                        }
-                }
-                fclose(f);
-        }
+        kernel_cmdline_options(udev);
 
         if (getuid() != 0) {
                 fprintf(stderr, "root privileges required\n");
@@ -1305,12 +993,9 @@ int main(int argc, char *argv[])
         chdir("/");
         umask(022);
 
-        /* /run/udev */
-        mkdir(udev_get_run_path(udev), 0755);
+        mkdir("/run/udev", 0755);
 
-        /* create standard links, copy static nodes, create nodes from modules */
-        static_dev_create(udev);
-        static_dev_create_from_modules(udev);
+        dev_setup(NULL);
 
         /* before opening new files, make sure std{in,out,err} fds are in a sane state */
         if (daemonize) {
@@ -1331,7 +1016,7 @@ int main(int argc, char *argv[])
         }
 
         if (systemd_fds(udev, &fd_ctrl, &fd_netlink) >= 0) {
-                /* get control and netlink socket from from systemd */
+                /* get control and netlink socket from systemd */
                 udev_ctrl = udev_ctrl_new_from_fd(udev, fd_ctrl);
                 if (udev_ctrl == NULL) {
                         log_error("error taking over udev control socket");
@@ -1345,6 +1030,10 @@ int main(int argc, char *argv[])
                         rc = 3;
                         goto exit;
                 }
+
+                /* get our own cgroup, we regularly kill everything udev has left behind */
+                if (cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &udev_cgroup) < 0)
+                        udev_cgroup = NULL;
         } else {
                 /* open control and netlink socket */
                 udev_ctrl = udev_ctrl_new(udev);
@@ -1391,7 +1080,6 @@ int main(int argc, char *argv[])
 
         if (daemonize) {
                 pid_t pid;
-                int fd;
 
                 pid = fork();
                 switch (pid) {
@@ -1408,30 +1096,12 @@ int main(int argc, char *argv[])
 
                 setsid();
 
-                fd = open("/proc/self/oom_score_adj", O_RDWR);
-                if (fd < 0) {
-                        /* Fallback to old interface */
-                        fd = open("/proc/self/oom_adj", O_RDWR);
-                        if (fd < 0) {
-                                log_error("error disabling OOM: %m\n");
-                        } else {
-                                /* OOM_DISABLE == -17 */
-                                write(fd, "-17", 3);
-                                close(fd);
-                        }
-                } else {
-                        write(fd, "-1000", 5);
-                        close(fd);
-                }
+                write_string_file("/proc/self/oom_score_adj", "-1000");
         } else {
                 sd_notify(1, "READY=1");
         }
 
-        f = fopen("/dev/kmsg", "w");
-        if (f != NULL) {
-                fprintf(f, "<30>udevd[%u]: starting version " VERSION "\n", getpid());
-                fclose(f);
-        }
+        print_kmsg("starting version " VERSION "\n");
 
         if (!debug) {
                 int fd;
@@ -1516,17 +1186,14 @@ int main(int argc, char *argv[])
                 goto exit;
         }
 
-        /* if needed, convert old database from earlier udev version */
-        convert_db(udev);
-
         if (children_max <= 0) {
-                int memsize = mem_size_mb();
+                cpu_set_t cpu_set;
 
-                /* set value depending on the amount of RAM */
-                if (memsize > 0)
-                        children_max = 128 + (memsize / 8);
-                else
-                        children_max = 128;
+                children_max = 8;
+
+                if (sched_getaffinity(0, sizeof (cpu_set), &cpu_set) == 0) {
+                        children_max +=  CPU_COUNT(&cpu_set) * 2;
+                }
         }
         log_debug("set children_max to %u\n", children_max);
 
@@ -1536,7 +1203,7 @@ int main(int argc, char *argv[])
         udev_list_node_init(&worker_list);
 
         for (;;) {
-                static unsigned long long last_usec;
+                static usec_t last_usec;
                 struct epoll_event ev[8];
                 int fdcount;
                 int timeout;
@@ -1562,7 +1229,7 @@ int main(int argc, char *argv[])
 
                         /* discard queued events and kill workers */
                         event_queue_cleanup(udev, EVENT_QUEUED);
-                        worker_kill(udev, 0);
+                        worker_kill(udev);
 
                         /* exit after all has cleaned up */
                         if (udev_list_node_is_empty(&event_list) && udev_list_node_is_empty(&worker_list))
@@ -1570,14 +1237,18 @@ int main(int argc, char *argv[])
 
                         /* timeout at exit for workers to finish */
                         timeout = 30 * 1000;
-                } else if (udev_list_node_is_empty(&event_list) && children <= 2) {
+                } else if (udev_list_node_is_empty(&event_list) && !children) {
                         /* we are idle */
                         timeout = -1;
+
+                        /* cleanup possible left-over processes in our cgroup */
+                        if (udev_cgroup)
+                                cg_kill(SYSTEMD_CGROUP_CONTROLLER, udev_cgroup, SIGKILL, false, true, NULL);
                 } else {
                         /* kill idle or hanging workers */
                         timeout = 3 * 1000;
                 }
-                fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), timeout);
+                fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
                 if (fdcount < 0)
                         continue;
 
@@ -1593,7 +1264,7 @@ int main(int argc, char *argv[])
                         /* kill idle workers */
                         if (udev_list_node_is_empty(&event_list)) {
                                 log_debug("cleanup idle workers\n");
-                                worker_kill(udev, 2);
+                                worker_kill(udev);
                         }
 
                         /* check for hanging events */
@@ -1603,8 +1274,8 @@ int main(int argc, char *argv[])
                                 if (worker->state != WORKER_RUNNING)
                                         continue;
 
-                                if ((now_usec() - worker->event_start_usec) > 30 * 1000 * 1000) {
-                                        log_error("worker [%u] timeout, kill it\n", worker->pid,
+                                if ((now(CLOCK_MONOTONIC) - worker->event_start_usec) > 30 * 1000 * 1000) {
+                                        log_error("worker [%u] %s timeout; kill it\n", worker->pid,
                                             worker->event ? worker->event->devpath : "<idle>");
                                         kill(worker->pid, SIGKILL);
                                         worker->state = WORKER_KILLED;
@@ -1637,21 +1308,21 @@ int main(int argc, char *argv[])
                 }
 
                 /* check for changed config, every 3 seconds at most */
-                if ((now_usec() - last_usec) > 3 * 1000 * 1000) {
-                        if (check_rules_timestamp(udev))
+                if ((now(CLOCK_MONOTONIC) - last_usec) > 3 * 1000 * 1000) {
+                        if (udev_rules_check_timestamp(rules))
                                 reload = true;
                         if (udev_builtin_validate(udev))
                                 reload = true;
 
-                        last_usec = now_usec();
+                        last_usec = now(CLOCK_MONOTONIC);
                 }
 
                 /* reload requested, HUP signal received, rules changed, builtin changed */
                 if (reload) {
-                        worker_kill(udev, 0);
+                        worker_kill(udev);
                         rules = udev_rules_unref(rules);
                         udev_builtin_exit(udev);
-                        reload = 0;
+                        reload = false;
                 }
 
                 /* event has finished */
@@ -1663,7 +1334,7 @@ int main(int argc, char *argv[])
 
                         dev = udev_monitor_receive_device(monitor);
                         if (dev != NULL) {
-                                udev_device_set_usec_initialized(dev, now_usec());
+                                udev_device_set_usec_initialized(dev, now(CLOCK_MONOTONIC));
                                 if (event_queue_insert(dev) < 0)
                                         udev_device_unref(dev);
                         }
@@ -1671,6 +1342,7 @@ int main(int argc, char *argv[])
 
                 /* start new events */
                 if (!udev_list_node_is_empty(&event_list) && !udev_exit && !stop_exec_queue) {
+                        udev_builtin_init(udev);
                         if (rules == NULL)
                                 rules = udev_rules_new(udev, resolve_names);
                         if (rules != NULL)
@@ -1728,7 +1400,7 @@ exit_daemonize:
         udev_queue_export_unref(udev_queue_export);
         udev_ctrl_connection_unref(ctrl_conn);
         udev_ctrl_unref(udev_ctrl);
-        udev_selinux_exit(udev);
+        label_finish();
         udev_unref(udev);
         log_close();
         return rc;