chiark / gitweb /
udev: exclude device-mapper from block device ownership event locking
[elogind.git] / src / udev / udevd.c
index 694e758777aba49c1f6b320dfe3fae63c096e2fe..6c051049741b4c03b1f55690b887d44e76891bfa 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2004-2011 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>
@@ -31,6 +31,7 @@
 #include <time.h>
 #include <getopt.h>
 #include <dirent.h>
+#include <sys/file.h>
 #include <sys/time.h>
 #include <sys/prctl.h>
 #include <sys/socket.h>
@@ -46,6 +47,9 @@
 
 #include "udev.h"
 #include "sd-daemon.h"
+#include "cgroup-util.h"
+#include "dev-setup.h"
+#include "fileio.h"
 
 static bool debug;
 
@@ -53,22 +57,10 @@ void udev_main_log(struct udev *udev, int priority,
                    const char *file, int line, const char *fn,
                    const char *format, va_list args)
 {
-        if (debug) {
-                char buf[1024];
-                struct timespec ts;
-
-                vsnprintf(buf, sizeof(buf), format, args);
-                clock_gettime(CLOCK_MONOTONIC, &ts);
-                fprintf(stderr, "[%llu.%06u] [%u] %s: %s",
-                        (unsigned long long) ts.tv_sec, (unsigned int) ts.tv_nsec/1000,
-                        (int) getpid(), fn, buf);
-        } else {
-                vsyslog(priority, format, args);
-        }
+        log_metav(priority, file, line, fn, format, args);
 }
 
 static struct udev_rules *rules;
-static struct udev_queue_export *udev_queue_export;
 static struct udev_ctrl *udev_ctrl;
 static struct udev_monitor *monitor;
 static int worker_watch[2] = { -1, -1 };
@@ -83,6 +75,7 @@ static int exec_delay;
 static sigset_t sigmask_orig;
 static UDEV_LIST(event_list);
 static UDEV_LIST(worker_list);
+static char *udev_cgroup;
 static bool udev_exit;
 
 enum event_state {
@@ -103,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);
@@ -133,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 */
@@ -142,23 +134,14 @@ 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)
+static void event_queue_delete(struct event *event)
 {
         udev_list_node_remove(&event->node);
-
-        if (export) {
-                udev_queue_export_device_finished(udev_queue_export, event->dev);
-                info(event->udev, "seq %llu done with %i\n", udev_device_get_seqnum(event->dev), event->exitcode);
-        }
         udev_device_unref(event->dev);
         free(event);
 }
@@ -182,7 +165,7 @@ static void worker_unref(struct worker *worker)
         worker->refcount--;
         if (worker->refcount > 0)
                 return;
-        info(worker->udev, "worker [%u] cleaned up\n", worker->pid);
+        log_debug("worker [%u] cleaned up", worker->pid);
         worker_cleanup(worker);
 }
 
@@ -212,7 +195,7 @@ static void worker_new(struct event *event)
         udev_monitor_allow_unicast_sender(worker_monitor, monitor);
         udev_monitor_enable_receiving(worker_monitor);
 
-        worker = calloc(1, sizeof(struct worker));
+        worker = new0(struct worker, 1);
         if (worker == NULL) {
                 udev_monitor_unref(worker_monitor);
                 return;
@@ -237,7 +220,6 @@ static void worker_new(struct event *event)
                 free(worker);
                 worker_list_cleanup(udev);
                 event_queue_cleanup(udev, EVENT_UNDEF);
-                udev_queue_export_unref(udev_queue_export);
                 udev_monitor_unref(monitor);
                 udev_ctrl_unref(udev_ctrl);
                 close(fd_signal);
@@ -247,30 +229,30 @@ static void worker_new(struct event *event)
                 sigfillset(&mask);
                 fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
                 if (fd_signal < 0) {
-                        err(udev, "error creating signalfd %m\n");
+                        log_error("error creating signalfd %m");
                         rc = 2;
                         goto out;
                 }
 
                 fd_ep = epoll_create1(EPOLL_CLOEXEC);
                 if (fd_ep < 0) {
-                        err(udev, "error creating epoll fd: %m\n");
+                        log_error("error creating epoll fd: %m");
                         rc = 3;
                         goto out;
                 }
 
-                memset(&ep_signal, 0, sizeof(struct epoll_event));
+                memzero(&ep_signal, sizeof(struct epoll_event));
                 ep_signal.events = EPOLLIN;
                 ep_signal.data.fd = fd_signal;
 
                 fd_monitor = udev_monitor_get_fd(worker_monitor);
-                memset(&ep_monitor, 0, sizeof(struct epoll_event));
+                memzero(&ep_monitor, sizeof(struct epoll_event));
                 ep_monitor.events = EPOLLIN;
                 ep_monitor.data.fd = fd_monitor;
 
                 if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
                     epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_monitor, &ep_monitor) < 0) {
-                        err(udev, "fail to add fds to epoll: %m\n");
+                        log_error("fail to add fds to epoll: %m");
                         rc = 4;
                         goto out;
                 }
@@ -278,12 +260,16 @@ 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;
-                        int err;
+                        int fd_lock = -1;
+                        int err = 0;
 
-                        info(udev, "seq %llu running\n", udev_device_get_seqnum(dev));
+                        log_debug("seq %llu running", udev_device_get_seqnum(dev));
                         udev_event = udev_event_new(dev);
                         if (udev_event == NULL) {
                                 rc = 5;
@@ -296,29 +282,67 @@ static void worker_new(struct event *event)
                         if (exec_delay > 0)
                                 udev_event->exec_delay = exec_delay;
 
+                        /*
+                         * Take a "read lock" on the device node; this establishes
+                         * a concept of device "ownership" to serialize device
+                         * access. External processes holding a "write lock" will
+                         * cause udev to skip the event handling; in the case udev
+                         * acquired the lock, the external process will block until
+                         * udev has finished its event handling.
+                         */
+
+                        /*
+                         * <kabi_> since we make check - device seems unused - we try
+                         *         ioctl to deactivate - and device is found to be opened
+                         * <kay> sure, you try to take a write lock
+                         * <kay> if you get it udev is out
+                         * <kay> if you can't get it, udev is busy
+                         * <kabi_> we cannot deactivate openned device  (as it is in-use)
+                         * <kay> maybe we should just exclude dm from that thing entirely
+                         * <kabi_> IMHO this sounds like a good plan for this moment
+                         */
+                        if (streq_ptr("block", udev_device_get_subsystem(dev)) &&
+                            !startswith("dm-", udev_device_get_sysname(dev))) {
+                                struct udev_device *d = dev;
+
+                                if (streq_ptr("partition", udev_device_get_devtype(d)))
+                                        d = udev_device_get_parent(d);
+
+                                if (d) {
+                                        fd_lock = open(udev_device_get_devnode(d), O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);
+                                        if (fd_lock >= 0 && flock(fd_lock, LOCK_SH|LOCK_NB) < 0) {
+                                                log_debug("Unable to flock(%s), skipping event handling: %m", udev_device_get_devnode(d));
+                                                err = -EWOULDBLOCK;
+                                                fd_lock = safe_close(fd_lock);
+                                                goto skip;
+                                        }
+                                }
+                        }
+
                         /* apply rules, create node, symlinks */
-                        err = udev_event_execute_rules(udev_event, rules, &sigmask_orig);
+                        udev_event_execute_rules(udev_event, rules, &sigmask_orig);
 
-                        if (err == 0)
-                                udev_event_execute_run(udev_event, &sigmask_orig);
+                        udev_event_execute_run(udev_event, &sigmask_orig);
 
                         /* apply/restore inotify watch */
-                        if (err == 0 && udev_event->inotify_watch) {
+                        if (udev_event->inotify_watch) {
                                 udev_watch_begin(udev, dev);
                                 udev_device_update_db(dev);
                         }
 
+                        safe_close(fd_lock);
+
                         /* send processed event back to libudev listeners */
                         udev_monitor_send_device(worker_monitor, NULL, dev);
 
+skip:
                         /* send udevd the result of the event execution */
-                        memset(&msg, 0, sizeof(struct worker_message));
-                        if (err != 0)
-                                msg.exitcode = err;
+                        memzero(&msg, sizeof(struct worker_message));
+                        msg.exitcode = err;
                         msg.pid = getpid();
                         send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
 
-                        info(udev, "seq %llu processed with %i\n", udev_device_get_seqnum(dev), err);
+                        log_debug("seq %llu processed with %i", udev_device_get_seqnum(dev), err);
 
                         udev_device_unref(dev);
                         dev = NULL;
@@ -336,12 +360,11 @@ 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;
-                                        err(udev, "failed to poll: %m\n");
+                                        log_error("failed to poll: %m");
                                         goto out;
                                 }
 
@@ -366,24 +389,22 @@ static void worker_new(struct event *event)
                 }
 out:
                 udev_device_unref(dev);
-                if (fd_signal >= 0)
-                        close(fd_signal);
-                if (fd_ep >= 0)
-                        close(fd_ep);
+                safe_close(fd_signal);
+                safe_close(fd_ep);
                 close(fd_inotify);
                 close(worker_watch[WRITE_END]);
                 udev_rules_unref(rules);
                 udev_builtin_exit(udev);
                 udev_monitor_unref(worker_monitor);
                 udev_unref(udev);
-                udev_log_close();
+                log_close();
                 exit(rc);
         }
         case -1:
                 udev_monitor_unref(worker_monitor);
                 event->state = EVENT_QUEUED;
                 free(worker);
-                err(udev, "fork of child failed: %m\n");
+                log_error("fork of child failed: %m");
                 break;
         default:
                 /* close monitor, but keep address around */
@@ -391,12 +412,12 @@ 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);
                 children++;
-                info(udev, "seq %llu forked new worker [%u]\n", udev_device_get_seqnum(event->dev), pid);
+                log_debug("seq %llu forked new worker [%u]", udev_device_get_seqnum(event->dev), pid);
                 break;
         }
 }
@@ -414,7 +435,7 @@ static void event_run(struct event *event)
 
                 count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
                 if (count < 0) {
-                        err(event->udev, "worker [%u] did not accept message %zi (%m), kill it\n", worker->pid, count);
+                        log_error("worker [%u] did not accept message %zi (%m), kill it", worker->pid, count);
                         kill(worker->pid, SIGKILL);
                         worker->state = WORKER_KILLED;
                         continue;
@@ -422,14 +443,14 @@ 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;
         }
 
         if (children >= children_max) {
                 if (children_max > 1)
-                        info(event->udev, "maximum number (%i) of children reached\n", children);
+                        log_debug("maximum number (%i) of children reached", children);
                 return;
         }
 
@@ -441,7 +462,7 @@ static int event_queue_insert(struct udev_device *dev)
 {
         struct event *event;
 
-        event = calloc(1, sizeof(struct event));
+        event = new0(struct event, 1);
         if (event == NULL)
                 return -1;
 
@@ -452,11 +473,14 @@ 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);
-        info(event->udev, "seq %llu queued, '%s' '%s'\n", udev_device_get_seqnum(dev),
+        log_debug("seq %llu queued, '%s' '%s'", udev_device_get_seqnum(dev),
              udev_device_get_action(dev), udev_device_get_subsystem(dev));
 
         event->state = EVENT_QUEUED;
@@ -464,22 +488,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;
 
@@ -519,7 +534,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;
                 }
@@ -542,6 +557,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;
@@ -572,10 +593,8 @@ static void event_queue_start(struct udev *udev)
                         continue;
 
                 /* do not start event if parent or child event is still running */
-                if (is_devpath_busy(event)) {
-                        dbg(udev, "delay seq %llu (%s)\n", event->seqnum, event->devpath);
+                if (is_devpath_busy(event))
                         continue;
-                }
 
                 event_run(event);
         }
@@ -591,7 +610,7 @@ static void event_queue_cleanup(struct udev *udev, enum event_state match_type)
                 if (match_type != EVENT_UNDEF && match_type != event->state)
                         continue;
 
-                event_queue_delete(event, false);
+                event_queue_delete(event);
         }
 }
 
@@ -616,7 +635,7 @@ static void worker_returned(int fd_worker)
                         /* worker returned */
                         if (worker->event) {
                                 worker->event->exitcode = msg.exitcode;
-                                event_queue_delete(worker->event, true);
+                                event_queue_delete(worker->event);
                                 worker->event = NULL;
                         }
                         if (worker->state != WORKER_KILLED)
@@ -646,23 +665,24 @@ static struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl)
 
         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);
+                log_debug("udevd message (SET_LOG_LEVEL) received, log_priority=%i", 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) {
-                info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
+                log_debug("udevd message (STOP_EXEC_QUEUE) received");
                 stop_exec_queue = true;
         }
 
         if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
-                info(udev, "udevd message (START_EXEC_QUEUE) received\n");
+                log_debug("udevd message (START_EXEC_QUEUE) received");
                 stop_exec_queue = false;
         }
 
         if (udev_ctrl_get_reload(ctrl_msg) > 0) {
-                info(udev, "udevd message (RELOAD) received\n");
+                log_debug("udevd message (RELOAD) received");
                 reload = true;
         }
 
@@ -679,31 +699,31 @@ static struct udev_ctrl_connection *handle_ctrl_msg(struct udev_ctrl *uctrl)
                                 val[0] = '\0';
                                 val = &val[1];
                                 if (val[0] == '\0') {
-                                        info(udev, "udevd message (ENV) received, unset '%s'\n", key);
+                                        log_debug("udevd message (ENV) received, unset '%s'", key);
                                         udev_add_property(udev, key, NULL);
                                 } else {
-                                        info(udev, "udevd message (ENV) received, set '%s=%s'\n", key, val);
+                                        log_debug("udevd message (ENV) received, set '%s=%s'", key, val);
                                         udev_add_property(udev, key, val);
                                 }
                         } else {
-                                err(udev, "wrong key format '%s'\n", key);
+                                log_error("wrong key format '%s'", key);
                         }
                         free(key);
                 }
-                worker_kill(udev, 0);
+                worker_kill(udev);
         }
 
         i = udev_ctrl_get_set_children_max(ctrl_msg);
         if (i >= 0) {
-                info(udev, "udevd message (SET_MAX_CHILDREN) received, children_max=%i\n", i);
+                log_debug("udevd message (SET_MAX_CHILDREN) received, children_max=%i", i);
                 children_max = i;
         }
 
         if (udev_ctrl_get_ping(ctrl_msg) > 0)
-                info(udev, "udevd message (SYNC) received\n");
+                log_debug("udevd message (SYNC) received");
 
         if (udev_ctrl_get_exit(ctrl_msg) > 0) {
-                info(udev, "udevd message (EXIT) received\n");
+                log_debug("udevd message (EXIT) received");
                 udev_exit = true;
                 /* keep reference to block the client until we exit */
                 udev_ctrl_connection_ref(ctrl_conn);
@@ -725,7 +745,7 @@ static int handle_inotify(struct udev *udev)
 
         buf = malloc(nbytes);
         if (buf == NULL) {
-                err(udev, "error getting buffer for inotify\n");
+                log_error("error getting buffer for inotify");
                 return -1;
         }
 
@@ -737,17 +757,17 @@ static int handle_inotify(struct udev *udev)
                 ev = (struct inotify_event *)(buf + pos);
                 dev = udev_watch_lookup(udev, ev->wd);
                 if (dev != NULL) {
-                        info(udev, "inotify event: %x for %s\n", ev->mask, udev_device_get_devnode(dev));
+                        log_debug("inotify event: %x for %s", ev->mask, udev_device_get_devnode(dev));
                         if (ev->mask & IN_CLOSE_WRITE) {
                                 char filename[UTIL_PATH_SIZE];
                                 int fd;
 
-                                info(udev, "device %s closed, synthesising 'change'\n", udev_device_get_devnode(dev));
-                                util_strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
-                                fd = open(filename, O_WRONLY);
+                                log_debug("device %s closed, synthesising 'change'", udev_device_get_devnode(dev));
+                                strscpyl(filename, sizeof(filename), udev_device_get_syspath(dev), "/uevent", NULL);
+                                fd = open(filename, O_WRONLY|O_CLOEXEC);
                                 if (fd >= 0) {
                                         if (write(fd, "change", 6) < 0)
-                                                info(udev, "error writing uevent: %m\n");
+                                                log_debug("error writing uevent: %m");
                                         close(fd);
                                 }
                         }
@@ -785,28 +805,30 @@ static void handle_signal(struct udev *udev, int signo)
 
                                 if (worker->pid != pid)
                                         continue;
-                                info(udev, "worker [%u] exit\n", pid);
+                                log_debug("worker [%u] exit", pid);
 
                                 if (WIFEXITED(status)) {
                                         if (WEXITSTATUS(status) != 0)
-                                                err(udev, "worker [%u] exit with return code %i\n", pid, WEXITSTATUS(status));
+                                                log_error("worker [%u] exit with return code %i",
+                                                          pid, WEXITSTATUS(status));
                                 } else if (WIFSIGNALED(status)) {
-                                        err(udev, "worker [%u] terminated by signal %i (%s)\n",
-                                            pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
+                                        log_error("worker [%u] terminated by signal %i (%s)",
+                                                  pid, WTERMSIG(status), strsignal(WTERMSIG(status)));
                                 } else if (WIFSTOPPED(status)) {
-                                        err(udev, "worker [%u] stopped\n", pid);
+                                        log_error("worker [%u] stopped", pid);
                                 } else if (WIFCONTINUED(status)) {
-                                        err(udev, "worker [%u] continued\n", pid);
+                                        log_error("worker [%u] continued", pid);
                                 } else {
-                                        err(udev, "worker [%u] exit with status 0x%04x\n", pid, status);
+                                        log_error("worker [%u] exit with status 0x%04x", pid, status);
                                 }
 
                                 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
                                         if (worker->event) {
-                                                err(udev, "worker [%u] failed while handling '%s'\n",
-                                                    pid, worker->event->devpath);
+                                                log_error("worker [%u] failed while handling '%s'",
+                                                          pid, worker->event->devpath);
                                                 worker->event->exitcode = -32;
-                                                event_queue_delete(worker->event, true);
+                                                event_queue_delete(worker->event);
+
                                                 /* drop reference taken for state 'running' */
                                                 worker_unref(worker);
                                         }
@@ -822,302 +844,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);
-                info(udev, "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;
@@ -1148,46 +874,63 @@ static int systemd_fds(struct udev *udev, int *rctrl, int *rnetlink)
         if (ctrl < 0 || netlink < 0)
                 return -1;
 
-        info(udev, "ctrl=%i netlink=%i\n", ctrl, netlink);
+        log_debug("ctrl=%i netlink=%i", ctrl, netlink);
         *rctrl = ctrl;
         *rnetlink = netlink;
         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;
-
-        n = udev_get_rules_path(udev, &p, &stamp_usec);
-        for (i = 0; i < n; i++) {
-                struct stat stats;
+        _cleanup_free_ char *line = NULL;
+        char *w, *state;
+        size_t l;
+        int r;
+
+        r = proc_cmdline(&line);
+        if (r < 0)
+                log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
+        if (r <= 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) {
-                        info(udev, "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;
 }
 
 int main(int argc, char *argv[])
 {
         struct udev *udev;
-        FILE *f;
         sigset_t mask;
         int daemonize = false;
         int resolve_names = 1;
@@ -1206,22 +949,26 @@ 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;
 
-        udev_log_init("udevd");
+        log_set_target(LOG_TARGET_AUTO);
+        log_parse_environment();
+        log_open();
+
         udev_set_log_fn(udev, udev_main_log);
-        info(udev, "version %s\n", VERSION);
-        udev_selinux_init(udev);
+        log_set_max_level(udev_get_log_priority(udev));
+
+        log_debug("version %s", VERSION);
+        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;
 
@@ -1237,19 +984,19 @@ 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");
-                                err(udev, "resolve-names must be early, late or never\n");
+                                log_error("resolve-names must be early, late or never");
                                 goto exit;
                         }
                         break;
@@ -1272,43 +1019,11 @@ 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");
-                err(udev, "root privileges required\n");
+                log_error("root privileges required");
                 goto exit;
         }
 
@@ -1316,12 +1031,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) {
@@ -1337,31 +1049,35 @@ int main(int argc, char *argv[])
                                 close(fd);
                 } else {
                         fprintf(stderr, "cannot open /dev/null\n");
-                        err(udev, "cannot open /dev/null\n");
+                        log_error("cannot open /dev/null");
                 }
         }
 
         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) {
-                        err(udev, "error taking over udev control socket");
+                        log_error("error taking over udev control socket");
                         rc = 1;
                         goto exit;
                 }
 
                 monitor = udev_monitor_new_from_netlink_fd(udev, "kernel", fd_netlink);
                 if (monitor == NULL) {
-                        err(udev, "error taking over netlink socket\n");
+                        log_error("error taking over netlink socket");
                         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);
                 if (udev_ctrl == NULL) {
                         fprintf(stderr, "error initializing udev control socket");
-                        err(udev, "error initializing udev control socket");
+                        log_error("error initializing udev control socket");
                         rc = 1;
                         goto exit;
                 }
@@ -1370,7 +1086,7 @@ int main(int argc, char *argv[])
                 monitor = udev_monitor_new_from_netlink(udev, "kernel");
                 if (monitor == NULL) {
                         fprintf(stderr, "error initializing netlink socket\n");
-                        err(udev, "error initializing netlink socket\n");
+                        log_error("error initializing netlink socket");
                         rc = 3;
                         goto exit;
                 }
@@ -1379,37 +1095,29 @@ int main(int argc, char *argv[])
 
         if (udev_monitor_enable_receiving(monitor) < 0) {
                 fprintf(stderr, "error binding netlink socket\n");
-                err(udev, "error binding netlink socket\n");
+                log_error("error binding netlink socket");
                 rc = 3;
                 goto exit;
         }
 
         if (udev_ctrl_enable_receiving(udev_ctrl) < 0) {
                 fprintf(stderr, "error binding udev control socket\n");
-                err(udev, "error binding udev control socket\n");
+                log_error("error binding udev control socket");
                 rc = 1;
                 goto exit;
         }
 
-        udev_monitor_set_receive_buffer_size(monitor, 128*1024*1024);
-
-        /* create queue file before signalling 'ready', to make sure we block 'settle' */
-        udev_queue_export = udev_queue_export_new(udev);
-        if (udev_queue_export == NULL) {
-                err(udev, "error creating queue file\n");
-                goto exit;
-        }
+        udev_monitor_set_receive_buffer_size(monitor, 128 * 1024 * 1024);
 
         if (daemonize) {
                 pid_t pid;
-                int fd;
 
                 pid = fork();
                 switch (pid) {
                 case 0:
                         break;
                 case -1:
-                        err(udev, "fork of daemon failed: %m\n");
+                        log_error("fork of daemon failed: %m");
                         rc = 4;
                         goto exit;
                 default:
@@ -1419,30 +1127,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) {
-                                err(udev, "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;
@@ -1459,7 +1149,7 @@ int main(int argc, char *argv[])
         fd_inotify = udev_watch_init(udev);
         if (fd_inotify < 0) {
                 fprintf(stderr, "error initializing inotify\n");
-                err(udev, "error initializing inotify\n");
+                log_error("error initializing inotify");
                 rc = 4;
                 goto exit;
         }
@@ -1471,7 +1161,7 @@ int main(int argc, char *argv[])
         fd_signal = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC);
         if (fd_signal < 0) {
                 fprintf(stderr, "error creating signalfd\n");
-                err(udev, "error creating signalfd\n");
+                log_error("error creating signalfd");
                 rc = 5;
                 goto exit;
         }
@@ -1479,7 +1169,7 @@ int main(int argc, char *argv[])
         /* unnamed socket from workers to the main daemon */
         if (socketpair(AF_LOCAL, SOCK_DGRAM|SOCK_CLOEXEC, 0, worker_watch) < 0) {
                 fprintf(stderr, "error creating socketpair\n");
-                err(udev, "error creating socketpair\n");
+                log_error("error creating socketpair");
                 rc = 6;
                 goto exit;
         }
@@ -1489,33 +1179,33 @@ int main(int argc, char *argv[])
 
         rules = udev_rules_new(udev, resolve_names);
         if (rules == NULL) {
-                err(udev, "error reading rules\n");
+                log_error("error reading rules");
                 goto exit;
         }
 
-        memset(&ep_ctrl, 0, sizeof(struct epoll_event));
+        memzero(&ep_ctrl, sizeof(struct epoll_event));
         ep_ctrl.events = EPOLLIN;
         ep_ctrl.data.fd = fd_ctrl;
 
-        memset(&ep_inotify, 0, sizeof(struct epoll_event));
+        memzero(&ep_inotify, sizeof(struct epoll_event));
         ep_inotify.events = EPOLLIN;
         ep_inotify.data.fd = fd_inotify;
 
-        memset(&ep_signal, 0, sizeof(struct epoll_event));
+        memzero(&ep_signal, sizeof(struct epoll_event));
         ep_signal.events = EPOLLIN;
         ep_signal.data.fd = fd_signal;
 
-        memset(&ep_netlink, 0, sizeof(struct epoll_event));
+        memzero(&ep_netlink, sizeof(struct epoll_event));
         ep_netlink.events = EPOLLIN;
         ep_netlink.data.fd = fd_netlink;
 
-        memset(&ep_worker, 0, sizeof(struct epoll_event));
+        memzero(&ep_worker, sizeof(struct epoll_event));
         ep_worker.events = EPOLLIN;
         ep_worker.data.fd = fd_worker;
 
         fd_ep = epoll_create1(EPOLL_CLOEXEC);
         if (fd_ep < 0) {
-                err(udev, "error creating epoll fd: %m\n");
+                log_error("error creating epoll fd: %m");
                 goto exit;
         }
         if (epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_ctrl, &ep_ctrl) < 0 ||
@@ -1523,31 +1213,30 @@ int main(int argc, char *argv[])
             epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_signal, &ep_signal) < 0 ||
             epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_netlink, &ep_netlink) < 0 ||
             epoll_ctl(fd_ep, EPOLL_CTL_ADD, fd_worker, &ep_worker) < 0) {
-                err(udev, "fail to add fds to epoll: %m\n");
+                log_error("fail to add fds to epoll: %m");
                 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;
+                }
         }
-        info(udev, "set children_max to %u\n", children_max);
+        log_debug("set children_max to %u", children_max);
 
-        udev_rules_apply_static_dev_perms(rules);
+        rc = udev_rules_apply_static_dev_perms(rules);
+        if (rc < 0)
+                log_error("failed to apply permissions on static device nodes - %s", strerror(-rc));
 
         udev_list_node_init(&event_list);
         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;
@@ -1573,22 +1262,38 @@ 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))
+                        if (udev_list_node_is_empty(&event_list) && children == 0)
                                 break;
 
                         /* timeout at exit for workers to finish */
-                        timeout = 30 * 1000;
-                } else if (udev_list_node_is_empty(&event_list) && children <= 2) {
+                        timeout = 30 * MSEC_PER_SEC;
+                } else if (udev_list_node_is_empty(&event_list) && children == 0) {
                         /* 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;
+                        timeout = 3 * MSEC_PER_SEC;
+                }
+
+                /* tell settle that we are busy or idle */
+                if (!udev_list_node_is_empty(&event_list)) {
+                        int fd;
+
+                        fd = open("/run/udev/queue", O_WRONLY|O_CREAT|O_CLOEXEC|O_TRUNC|O_NOFOLLOW, 0444);
+                        if (fd >= 0)
+                                close(fd);
+                } else {
+                        unlink("/run/udev/queue");
                 }
-                fdcount = epoll_wait(fd_ep, ev, ARRAY_SIZE(ev), timeout);
+
+                fdcount = epoll_wait(fd_ep, ev, ELEMENTSOF(ev), timeout);
                 if (fdcount < 0)
                         continue;
 
@@ -1597,14 +1302,14 @@ int main(int argc, char *argv[])
 
                         /* timeout */
                         if (udev_exit) {
-                                err(udev, "timeout, giving up waiting for workers to finish\n");
+                                log_error("timeout, giving up waiting for workers to finish");
                                 break;
                         }
 
                         /* kill idle workers */
                         if (udev_list_node_is_empty(&event_list)) {
-                                info(udev, "cleanup idle workers\n");
-                                worker_kill(udev, 2);
+                                log_debug("cleanup idle workers");
+                                worker_kill(udev);
                         }
 
                         /* check for hanging events */
@@ -1614,18 +1319,18 @@ int main(int argc, char *argv[])
                                 if (worker->state != WORKER_RUNNING)
                                         continue;
 
-                                if ((now_usec() - worker->event_start_usec) > 30 * 1000 * 1000) {
-                                        err(udev, "worker [%u] timeout, kill it\n", worker->pid,
+                                if ((now(CLOCK_MONOTONIC) - worker->event_start_usec) > 30 * USEC_PER_SEC) {
+                                        log_error("worker [%u] %s timeout; kill it", worker->pid,
                                             worker->event ? worker->event->devpath : "<idle>");
                                         kill(worker->pid, SIGKILL);
                                         worker->state = WORKER_KILLED;
+
                                         /* drop reference taken for state 'running' */
                                         worker_unref(worker);
                                         if (worker->event) {
-                                                err(udev, "seq %llu '%s' killed\n",
-                                                    udev_device_get_seqnum(worker->event->dev), worker->event->devpath);
+                                                log_error("seq %llu '%s' killed", udev_device_get_seqnum(worker->event->dev), worker->event->devpath);
                                                 worker->event->exitcode = -64;
-                                                event_queue_delete(worker->event, true);
+                                                event_queue_delete(worker->event);
                                                 worker->event = NULL;
                                         }
                                 }
@@ -1648,21 +1353,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 * USEC_PER_SEC) {
+                        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 */
@@ -1674,7 +1379,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);
                         }
@@ -1682,6 +1387,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)
@@ -1720,8 +1426,8 @@ int main(int argc, char *argv[])
 
         rc = EXIT_SUCCESS;
 exit:
-        udev_queue_export_cleanup(udev_queue_export);
         udev_ctrl_cleanup(udev_ctrl);
+        unlink("/run/udev/queue");
 exit_daemonize:
         if (fd_ep >= 0)
                 close(fd_ep);
@@ -1736,11 +1442,10 @@ exit_daemonize:
         if (worker_watch[WRITE_END] >= 0)
                 close(worker_watch[WRITE_END]);
         udev_monitor_unref(monitor);
-        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);
-        udev_log_close();
+        log_close();
         return rc;
 }