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/>.
32 #include <sys/inotify.h>
35 #include <sys/types.h>
40 static int adm_settle(struct udev *udev, int argc, char *argv[])
42 static const struct option options[] = {
43 { "seq-start", required_argument, NULL, 's' },
44 { "seq-end", required_argument, NULL, 'e' },
45 { "timeout", required_argument, NULL, 't' },
46 { "exit-if-exists", required_argument, NULL, 'E' },
47 { "quiet", no_argument, NULL, 'q' },
48 { "help", no_argument, NULL, 'h' },
51 usec_t start_usec = now(CLOCK_MONOTONIC);
55 const char *exists = NULL;
56 unsigned int timeout = 120;
57 struct pollfd pfd[1] = { {.fd = -1}, };
58 struct udev_queue *udev_queue = NULL;
59 int rc = EXIT_FAILURE;
64 option = getopt_long(argc, argv, "s:e:t:E:qh", options, NULL);
67 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
75 start = strtoull(optarg, NULL, 0);
78 end = strtoull(optarg, NULL, 0);
83 r = safe_atou(optarg, &timeout);
85 fprintf(stderr, "Invalid timeout value '%s': %s\n",
86 optarg, strerror(-r));
98 printf("Usage: udevadm settle OPTIONS\n"
99 " --timeout=<seconds> maximum time to wait for events\n"
100 " --seq-start=<seqnum> first seqnum to wait for\n"
101 " --seq-end=<seqnum> last seqnum to wait for\n"
102 " --exit-if-exists=<file> stop waiting if file exists\n"
103 " --quiet do not print list after timeout\n"
111 udev_queue = udev_queue_new(udev);
112 if (udev_queue == NULL)
116 unsigned long long kernel_seq;
118 kernel_seq = udev_queue_get_kernel_seqnum(udev_queue);
120 /* unless specified, the last event is the current kernel seqnum */
122 end = udev_queue_get_kernel_seqnum(udev_queue);
125 log_error("seq-start larger than seq-end, ignoring\n");
130 if (start > kernel_seq || end > kernel_seq) {
131 log_error("seq-start or seq-end larger than current kernel value, ignoring\n");
135 log_debug("start=%llu end=%llu current=%llu\n", (unsigned long long)start, (unsigned long long)end, kernel_seq);
138 log_error("seq-end needs seq-start parameter, ignoring\n");
143 /* guarantee that the udev daemon isn't pre-processing */
145 struct udev_ctrl *uctrl;
147 uctrl = udev_ctrl_new(udev);
149 if (udev_ctrl_send_ping(uctrl, timeout) < 0) {
150 log_debug("no connection to daemon\n");
151 udev_ctrl_unref(uctrl);
155 udev_ctrl_unref(uctrl);
159 pfd[0].events = POLLIN;
160 pfd[0].fd = inotify_init1(IN_CLOEXEC);
162 log_error("inotify_init failed: %m\n");
164 if (inotify_add_watch(pfd[0].fd, "/run/udev" , IN_MOVED_TO) < 0) {
165 log_error("watching /run/udev failed\n");
174 if (exists != NULL && stat(exists, &statbuf) == 0) {
180 /* if asked for, wait for a specific sequence of events */
181 if (udev_queue_get_seqnum_sequence_is_finished(udev_queue, start, end) == 1) {
186 /* exit if queue is empty */
187 if (udev_queue_get_queue_is_empty(udev_queue)) {
193 if (pfd[0].fd >= 0) {
196 if (exists != NULL || start > 0)
200 /* wake up after delay, or immediately after the queue is rebuilt */
201 if (poll(pfd, 1, delay) > 0 && pfd[0].revents & POLLIN) {
202 char buf[sizeof(struct inotify_event) + PATH_MAX];
204 read(pfd[0].fd, buf, sizeof(buf));
213 age_usec = now(CLOCK_MONOTONIC) - start_usec;
214 if (age_usec / (1000 * 1000) >= timeout) {
215 struct udev_list_entry *list_entry;
217 if (!quiet && udev_queue_get_queued_list_entry(udev_queue) != NULL) {
218 log_debug("timeout waiting for udev queue\n");
219 printf("\nudevadm settle - timeout of %i seconds reached, the event queue contains:\n", timeout);
220 udev_list_entry_foreach(list_entry, udev_queue_get_queued_list_entry(udev_queue))
222 udev_list_entry_get_name(list_entry),
223 udev_list_entry_get_value(list_entry));
233 udev_queue_unref(udev_queue);
237 const struct udevadm_cmd udevadm_settle = {
240 .help = "wait for the event queue to finish",