chiark / gitweb /
udevadm: settle - fix timeout
[elogind.git] / udev / udevadm-settle.c
index f9e6415a00abbb413ee66d5881359e768f9aebe7..52d9c0b2446f03eaba816904179341d653be8116 100644 (file)
@@ -1,5 +1,7 @@
 /*
  * Copyright (C) 2006-2008 Kay Sievers <kay@vrfy.org>
+ * Copyright (C) 2009 Canonical Ltd.
+ * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -25,6 +27,7 @@
 #include <fcntl.h>
 #include <syslog.h>
 #include <getopt.h>
+#include <signal.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
 #define DEFAULT_TIMEOUT                        180
 #define LOOP_PER_SECOND                        20
 
+static volatile sig_atomic_t is_timeout;
+
+static void sig_handler(int signum)
+{
+       switch (signum) {
+               case SIGALRM:
+                       is_timeout = 1;
+               case SIGUSR1:
+                       ;
+       }
+}
+
 int udevadm_settle(struct udev *udev, int argc, char *argv[])
 {
        static const struct option options[] = {
@@ -45,14 +60,22 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
        };
        unsigned long long start = 0;
        unsigned long long end = 0;
-       int timeout = DEFAULT_TIMEOUT;
        int quiet = 0;
+       int timeout = 0;
+       struct sigaction act;
        struct udev_queue *udev_queue = NULL;
-       int loop;
        int rc = 0;
 
        dbg(udev, "version %s\n", VERSION);
 
+       /* set signal handlers */
+       memset(&act, 0x00, sizeof(act));
+       act.sa_handler = sig_handler;
+       sigemptyset (&act.sa_mask);
+       act.sa_flags = 0;
+       sigaction(SIGALRM, &act, NULL);
+       sigaction(SIGUSR1, &act, NULL);
+
        while (1) {
                int option;
                int seconds;
@@ -90,10 +113,14 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
                }
        }
 
+       if (timeout > 0)
+               alarm(timeout);
+       else
+               alarm(DEFAULT_TIMEOUT);
+
        udev_queue = udev_queue_new(udev);
        if (udev_queue == NULL)
                goto exit;
-       loop = timeout * LOOP_PER_SECOND;
 
        if (start > 0) {
                unsigned long long kernel_seq;
@@ -126,7 +153,26 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
                }
        }
 
-       while (loop--) {
+       /* guarantee that the udev daemon isn't pre-processing */
+       if (getuid() == 0) {
+               struct udev_ctrl *uctrl;
+
+               uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
+               if (uctrl != NULL) {
+                       sigset_t mask, oldmask;
+
+                       sigemptyset(&mask);
+                       sigaddset(&mask, SIGUSR1);
+                       sigaddset(&mask, SIGALRM);
+                       sigprocmask(SIG_BLOCK, &mask, &oldmask);
+                       if (udev_ctrl_send_settle(uctrl) > 0)
+                               sigsuspend(&oldmask);
+                       sigprocmask(SIG_SETMASK, &oldmask, NULL);
+                       udev_ctrl_unref(uctrl);
+               }
+       }
+
+       while (!is_timeout) {
                /* exit if queue is empty */
                if (udev_queue_get_queue_is_empty(udev_queue))
                        break;
@@ -149,7 +195,7 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
        }
 
        /* if we reached the timeout, print the list of remaining events */
-       if (loop <= 0) {
+       if (is_timeout) {
                struct udev_list_entry *list_entry;
 
                if (!quiet) {