2 * Copyright (C) 2006-2009 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2009 Canonical Ltd.
4 * Copyright (C) 2009 Scott James Remnant <scott@netsplit.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include <sys/types.h>
37 #define DEFAULT_TIMEOUT 180
38 #define LOOP_PER_SECOND 20
40 static volatile sig_atomic_t is_timeout;
42 static void sig_handler(int signum)
52 int udevadm_settle(struct udev *udev, int argc, char *argv[])
54 static const struct option options[] = {
55 { "seq-start", required_argument, NULL, 's' },
56 { "seq-end", required_argument, NULL, 'e' },
57 { "timeout", required_argument, NULL, 't' },
58 { "exit-if-exists", required_argument, NULL, 'E' },
59 { "quiet", no_argument, NULL, 'q' },
60 { "help", no_argument, NULL, 'h' },
63 unsigned long long start = 0;
64 unsigned long long end = 0;
66 const char *exists = NULL;
67 int timeout = DEFAULT_TIMEOUT;
69 struct udev_queue *udev_queue = NULL;
72 dbg(udev, "version %s\n", VERSION);
74 /* set signal handlers */
75 memset(&act, 0x00, sizeof(act));
76 act.sa_handler = sig_handler;
77 sigemptyset (&act.sa_mask);
79 sigaction(SIGALRM, &act, NULL);
80 sigaction(SIGUSR1, &act, NULL);
86 option = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL);
92 start = strtoull(optarg, NULL, 0);
95 end = strtoull(optarg, NULL, 0);
98 seconds = atoi(optarg);
102 fprintf(stderr, "invalid timeout value\n");
103 dbg(udev, "timeout=%i\n", timeout);
112 printf("Usage: udevadm settle OPTIONS\n"
113 " --timeout=<seconds> maximum time to wait for events\n"
114 " --seq-start=<seqnum> first seqnum to wait for\n"
115 " --seq-end=<seqnum> last seqnum to wait for\n"
116 " --exit-if-exists=<file> stop waiting if file exists\n"
117 " --quiet do not print list after timeout\n"
128 udev_queue = udev_queue_new(udev);
129 if (udev_queue == NULL)
133 unsigned long long kernel_seq;
135 kernel_seq = udev_queue_get_kernel_seqnum(udev_queue);
137 /* unless specified, the last event is the current kernel seqnum */
139 end = udev_queue_get_kernel_seqnum(udev_queue);
142 err(udev, "seq-start larger than seq-end, ignoring\n");
143 fprintf(stderr, "seq-start larger than seq-end, ignoring\n");
148 if (start > kernel_seq || end > kernel_seq) {
149 err(udev, "seq-start or seq-end larger than current kernel value, ignoring\n");
150 fprintf(stderr, "seq-start or seq-end larger than current kernel value, ignoring\n");
154 info(udev, "start=%llu end=%llu current=%llu\n", start, end, kernel_seq);
157 err(udev, "seq-end needs seq-start parameter, ignoring\n");
158 fprintf(stderr, "seq-end needs seq-start parameter, ignoring\n");
163 /* guarantee that the udev daemon isn't pre-processing */
165 struct udev_ctrl *uctrl;
167 uctrl = udev_ctrl_new_from_socket(udev, UDEV_CTRL_SOCK_PATH);
169 sigset_t mask, oldmask;
172 sigaddset(&mask, SIGUSR1);
173 sigaddset(&mask, SIGALRM);
174 sigprocmask(SIG_BLOCK, &mask, &oldmask);
175 if (udev_ctrl_send_settle(uctrl) > 0)
176 sigsuspend(&oldmask);
177 sigprocmask(SIG_SETMASK, &oldmask, NULL);
178 udev_ctrl_unref(uctrl);
184 const struct timespec duration = { 0 , 1000 * 1000 * 1000 / LOOP_PER_SECOND };
186 if (exists != NULL && stat(exists, &statbuf) == 0) {
192 /* if asked for, wait for a specific sequence of events */
193 if (udev_queue_get_seqnum_sequence_is_finished(udev_queue, start, end) == 1) {
198 /* exit if queue is empty */
199 if (udev_queue_get_queue_is_empty(udev_queue)) {
208 nanosleep(&duration, NULL);
211 /* if we reached the timeout, print the list of remaining events */
213 struct udev_list_entry *list_entry;
215 if (!quiet && udev_queue_get_queued_list_entry(udev_queue) != NULL) {
216 info(udev, "timeout waiting for udev queue\n");
217 printf("\nudevadm settle - timeout of %i seconds reached, the event queue contains:\n", timeout);
218 udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
220 udev_list_entry_get_name(list_entry),
221 udev_list_entry_get_value(list_entry));
225 udev_queue_unref(udev_queue);