chiark / gitweb /
Merge keymap building in the top-level Makefile.am.
[elogind.git] / udev / udevd.c
index 876d2ec1df0133f0f78c1cf847a2ceb9094ea673..4024460d4b1de82faa486dbd0da8e0d9a85dcae7 100644 (file)
 #include <errno.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <ctype.h>
 #include <fcntl.h>
 #include <time.h>
 #include <getopt.h>
 #include <dirent.h>
+#include <sys/time.h>
 #include <sys/prctl.h>
 #include <sys/socket.h>
 #include <sys/signalfd.h>
 #define UDEVD_PRIORITY                 -4
 #define UDEV_PRIORITY                  -2
 
-static int debug;
+static bool debug;
 
 static void log_fn(struct udev *udev, int priority,
                   const char *file, int line, const char *fn,
                   const char *format, va_list args)
 {
        if (debug) {
-               fprintf(stderr, "[%d] %s: ", (int) getpid(), fn);
-               vfprintf(stderr, format, args);
+               char buf[1024];
+               struct timeval tv;
+               struct timezone tz;
+
+               vsnprintf(buf, sizeof(buf), format, args);
+               gettimeofday(&tv, &tz);
+               fprintf(stderr, "%llu.%06u [%u] %s: %s",
+                       (unsigned long long) tv.tv_sec, (unsigned int) tv.tv_usec,
+                       (int) getpid(), fn, buf);
        } else {
                vsyslog(priority, format, args);
        }
 }
 
-static int debug_trace;
+static bool debug_trace;
 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];
 static pid_t settle_pid;
-static int stop_exec_queue;
-static int reload_config;
+static bool stop_exec_queue;
+static bool reload_config;
 static int max_childs;
 static int childs;
 static struct udev_list_node event_list;
 static struct udev_list_node worker_list;
-static int udev_exit;
+static bool udev_exit;
 static volatile sig_atomic_t worker_exit;
 
 enum poll_fd {
@@ -128,6 +137,8 @@ enum worker_state {
 
 struct worker {
        struct udev_list_node node;
+       struct udev *udev;
+       int refcount;
        pid_t pid;
        struct udev_monitor *monitor;
        enum worker_state state;
@@ -154,7 +165,7 @@ static void event_queue_delete(struct event *event)
        udev_list_node_remove(&event->node);
 
        /* mark as failed, if "add" event returns non-zero */
-       if (event->exitcode && strcmp(udev_device_get_action(event->dev), "add") == 0)
+       if (event->exitcode != 0 && strcmp(udev_device_get_action(event->dev), "add") == 0)
                udev_queue_export_device_failed(udev_queue_export, event->dev);
        else
                udev_queue_export_device_finished(udev_queue_export, event->dev);
@@ -171,14 +182,27 @@ static void event_sig_handler(int signum)
                _exit(1);
                break;
        case SIGTERM:
-               worker_exit = 1;
+               worker_exit = true;
                break;
        }
 }
 
+static struct worker *worker_ref(struct worker *worker)
+{
+       worker->refcount++;
+       return worker;
+}
+
 static void worker_unref(struct worker *worker)
 {
+       worker->refcount--;
+       if (worker->refcount > 0)
+               return;
+
+       udev_list_node_remove(&worker->node);
        udev_monitor_unref(worker->monitor);
+       childs--;
+       info(worker->udev, "worker [%u] cleaned up\n", worker->pid);
        free(worker);
 }
 
@@ -201,6 +225,9 @@ static void worker_new(struct event *event)
        worker = calloc(1, sizeof(struct worker));
        if (worker == NULL)
                return;
+       /* worker + event reference */
+       worker->refcount = 2;
+       worker->udev = event->udev;
 
        pid = fork();
        switch (pid) {
@@ -244,9 +271,11 @@ static void worker_new(struct event *event)
 
                do {
                        struct udev_event *udev_event;
-                       struct worker_message msg;
+                       struct worker_message msg = {};
                        int err;
+                       int failed = 0;
 
+                       info(event->udev, "seq %llu running\n", udev_device_get_seqnum(dev));
                        udev_event = udev_event_new(dev);
                        if (udev_event == NULL)
                                _exit(3);
@@ -263,7 +292,7 @@ static void worker_new(struct event *event)
 
                        /* execute RUN= */
                        if (err == 0 && !udev_event->ignore_device && udev_get_run(udev_event->udev))
-                               udev_event_execute_run(udev_event);
+                               failed = udev_event_execute_run(udev_event);
 
                        /* reset alarm */
                        alarm(0);
@@ -278,7 +307,10 @@ static void worker_new(struct event *event)
                        udev_monitor_send_device(worker_monitor, NULL, dev);
 
                        /* send back the result of the event execution */
-                       msg.exitcode = err;
+                       if (err != 0)
+                               msg.exitcode = err;
+                       else if (failed != 0)
+                               msg.exitcode = failed;
                        msg.pid = getpid();
                        send(worker_watch[WRITE_END], &msg, sizeof(struct worker_message), 0);
 
@@ -339,18 +371,17 @@ static void event_run(struct event *event)
                if (worker->state != WORKER_IDLE)
                        continue;
 
-               worker->event = event;
-               worker->state = WORKER_RUNNING;
-               event->state = EVENT_RUNNING;
                count = udev_monitor_send_device(monitor, worker->monitor, event->dev);
                if (count < 0) {
-                       event->state = EVENT_QUEUED;
-                       worker->event = NULL;
                        err(event->udev, "worker [%u] did not accept message %zi (%m), kill it\n", worker->pid, count);
-                       worker->state = WORKER_KILLED;
                        kill(worker->pid, SIGKILL);
+                       worker->state = WORKER_KILLED;
                        continue;
                }
+               worker_ref(worker);
+               worker->event = event;
+               worker->state = WORKER_RUNNING;
+               event->state = EVENT_RUNNING;
                return;
        }
 
@@ -544,6 +575,7 @@ static void worker_returned(void)
                        worker->event = NULL;
                        if (worker->state != WORKER_KILLED)
                                worker->state = WORKER_IDLE;
+                       worker_unref(worker);
                        break;
                }
        }
@@ -570,17 +602,17 @@ static void handle_ctrl_msg(struct udev_ctrl *uctrl)
 
        if (udev_ctrl_get_stop_exec_queue(ctrl_msg) > 0) {
                info(udev, "udevd message (STOP_EXEC_QUEUE) received\n");
-               stop_exec_queue = 1;
+               stop_exec_queue = true;
        }
 
        if (udev_ctrl_get_start_exec_queue(ctrl_msg) > 0) {
                info(udev, "udevd message (START_EXEC_QUEUE) received\n");
-               stop_exec_queue = 0;
+               stop_exec_queue = false;
        }
 
        if (udev_ctrl_get_reload_rules(ctrl_msg) > 0) {
                info(udev, "udevd message (RELOAD_RULES) received\n");
-               reload_config = 1;
+               reload_config = true;
        }
 
        str = udev_ctrl_get_set_env(ctrl_msg);
@@ -649,7 +681,7 @@ static int handle_inotify(struct udev *udev)
                ev = (struct inotify_event *)(buf + pos);
                if (ev->len) {
                        dbg(udev, "inotify event: %x for %s\n", ev->mask, ev->name);
-                       reload_config = 1;
+                       reload_config = true;
                        continue;
                }
 
@@ -684,7 +716,7 @@ static void handle_signal(struct udev *udev, int signo)
        switch (signo) {
        case SIGINT:
        case SIGTERM:
-               udev_exit = 1;
+               udev_exit = true;
                break;
        case SIGCHLD:
                while (1) {
@@ -702,23 +734,24 @@ static void handle_signal(struct udev *udev, int signo)
                                if (worker->pid != pid)
                                        continue;
 
-                               /* fail event, if worker died unexpectedly */
-                               if (worker->event != NULL) {
-                                       worker->event->exitcode = status;
+                               info(udev, "worker [%u] exit\n", pid);
+                               if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
                                        err(udev, "worker [%u] unexpectedly returned with status 0x%04x\n", pid, status);
-                                       event_queue_delete(worker->event);
+                                       if (worker->event != NULL) {
+                                               err(udev, "worker [%u] failed while handling '%s'\n", pid, worker->event->devpath);
+                                               worker->event->exitcode = -32;
+                                               event_queue_delete(worker->event);
+                                               /* drop reference from running event */
+                                               worker_unref(worker);
+                                       }
                                }
-
-                               udev_list_node_remove(&worker->node);
                                worker_unref(worker);
-                               childs--;
-                               info(udev, "worker [%u] exit\n", pid);
                                break;
                        }
                }
                break;
        case SIGHUP:
-               reload_config = 1;
+               reload_config = true;
                break;
        }
 }
@@ -756,7 +789,7 @@ int main(int argc, char *argv[])
        int fd;
        sigset_t mask;
        const char *value;
-       int daemonize = 0;
+       int daemonize = false;
        int resolve_names = 1;
        static const struct option options[] = {
                { "daemon", no_argument, NULL, 'd' },
@@ -787,13 +820,13 @@ int main(int argc, char *argv[])
 
                switch (option) {
                case 'd':
-                       daemonize = 1;
+                       daemonize = true;
                        break;
                case 't':
-                       debug_trace = 1;
+                       debug_trace = true;
                        break;
                case 'D':
-                       debug = 1;
+                       debug = true;
                        if (udev_get_log_priority(udev) < LOG_INFO)
                                udev_set_log_priority(udev, LOG_INFO);
                        break;