chiark / gitweb /
unify/cleanup event handling
[elogind.git] / udev / udevadm-settle.c
index 9059adc341a59482834a681cb76e059b3e98209d..4f422f90927d410b3287721245fed26444435840 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2008 Kay Sievers <kay@vrfy.org>
+ * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
  * Copyright (C) 2009 Canonical Ltd.
  * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
  *
@@ -28,6 +28,7 @@
 #include <syslog.h>
 #include <getopt.h>
 #include <signal.h>
+#include <time.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
@@ -54,6 +55,7 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
                { "seq-start", required_argument, NULL, 's' },
                { "seq-end", required_argument, NULL, 'e' },
                { "timeout", required_argument, NULL, 't' },
+               { "exit-if-exists", required_argument, NULL, 'E' },
                { "quiet", no_argument, NULL, 'q' },
                { "help", no_argument, NULL, 'h' },
                {}
@@ -61,8 +63,10 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
        unsigned long long start = 0;
        unsigned long long end = 0;
        int quiet = 0;
+       const char *exists = NULL;
        int timeout = DEFAULT_TIMEOUT;
        struct sigaction act;
+       sigset_t mask;
        struct udev_queue *udev_queue = NULL;
        int rc = 1;
 
@@ -75,12 +79,16 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
        act.sa_flags = 0;
        sigaction(SIGALRM, &act, NULL);
        sigaction(SIGUSR1, &act, NULL);
+       sigemptyset(&mask);
+       sigaddset(&mask, SIGUSR1);
+       sigaddset(&mask, SIGALRM);
+       sigprocmask(SIG_UNBLOCK, &mask, NULL);
 
        while (1) {
                int option;
                int seconds;
 
-               option = getopt_long(argc, argv, "s:e:t:qh", options, NULL);
+               option = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL);
                if (option == -1)
                        break;
 
@@ -102,12 +110,16 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
                case 'q':
                        quiet = 1;
                        break;
+               case 'E':
+                       exists = optarg;
+                       break;
                case 'h':
                        printf("Usage: udevadm settle OPTIONS\n"
-                              "  --timeout=<seconds>   maximum time to wait for events\n"
-                              "  --seq-start=<seqnum>  first seqnum to wait for\n"
-                              "  --seq-end=<seqnum>    last seqnum to wait for\n"
-                              "  --quiet               do not print list after timeout\n"
+                              "  --timeout=<seconds>     maximum time to wait for events\n"
+                              "  --seq-start=<seqnum>    first seqnum to wait for\n"
+                              "  --seq-end=<seqnum>      last seqnum to wait for\n"
+                              "  --exit-if-exists=<file> stop waiting if file exists\n"
+                              "  --quiet                 do not print list after timeout\n"
                               "  --help\n\n");
                        exit(0);
                }
@@ -133,14 +145,12 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
 
                if (start > end) {
                        err(udev, "seq-start larger than seq-end, ignoring\n");
-                       fprintf(stderr, "seq-start larger than seq-end, ignoring\n");
                        start = 0;
                        end = 0;
                }
 
                if (start > kernel_seq || end > kernel_seq) {
                        err(udev, "seq-start or seq-end larger than current kernel value, ignoring\n");
-                       fprintf(stderr, "seq-start or seq-end larger than current kernel value, ignoring\n");
                        start = 0;
                        end = 0;
                }
@@ -148,7 +158,6 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
        } else {
                if (end > 0) {
                        err(udev, "seq-end needs seq-start parameter, ignoring\n");
-                       fprintf(stderr, "seq-end needs seq-start parameter, ignoring\n");
                        end = 0;
                }
        }
@@ -159,7 +168,7 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
 
                uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
                if (uctrl != NULL) {
-                       sigset_t mask, oldmask;
+                       sigset_t oldmask;
 
                        sigemptyset(&mask);
                        sigaddset(&mask, SIGUSR1);
@@ -173,6 +182,14 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
        }
 
        while (1) {
+               struct stat statbuf;
+               const struct timespec duration = { 0 , 1000 * 1000 * 1000 / LOOP_PER_SECOND };
+
+               if (exists != NULL && stat(exists, &statbuf) == 0) {
+                       rc = 0;
+                       break;
+               }
+
                if (start > 0) {
                        /* if asked for, wait for a specific sequence of events */
                        if (udev_queue_get_seqnum_sequence_is_finished(udev_queue, start, end) == 1) {
@@ -190,7 +207,7 @@ int udevadm_settle(struct udev *udev, int argc, char *argv[])
                if (is_timeout)
                        break;
 
-               usleep(1000 * 1000 / LOOP_PER_SECOND);
+               nanosleep(&duration, NULL);
        }
 
        /* if we reached the timeout, print the list of remaining events */