1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © 2006-2009 Kay Sievers <kay@vrfy.org>
4 * Copyright © 2009 Canonical Ltd.
5 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
18 #include "parse-util.h"
20 #include "udevadm-util.h"
23 static void help(void) {
24 printf("%s settle [OPTIONS]\n\n"
25 "Wait for pending udev events.\n\n"
26 " -h --help Show this help\n"
27 " -V --version Show package version\n"
28 " -t --timeout=SECONDS Maximum time to wait for events\n"
29 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
30 , program_invocation_short_name
);
33 static int adm_settle(struct udev
*udev
, int argc
, char *argv
[]) {
34 static const struct option options
[] = {
35 { "timeout", required_argument
, NULL
, 't' },
36 { "exit-if-exists", required_argument
, NULL
, 'E' },
37 { "version", no_argument
, NULL
, 'V' },
38 { "help", no_argument
, NULL
, 'h' },
39 { "seq-start", required_argument
, NULL
, 's' }, /* removed */
40 { "seq-end", required_argument
, NULL
, 'e' }, /* removed */
41 { "quiet", no_argument
, NULL
, 'q' }, /* removed */
45 const char *exists
= NULL
;
46 unsigned int timeout
= 120;
47 struct pollfd pfd
[1] = { {.fd
= -1}, };
49 struct udev_queue
*queue
;
50 int rc
= EXIT_FAILURE
;
52 while ((c
= getopt_long(argc
, argv
, "t:E:Vhs:e:q", options
, NULL
)) >= 0) {
58 r
= safe_atou(optarg
, &timeout
);
60 log_error_errno(r
, "Invalid timeout value '%s': %m", optarg
);
81 log_info("Option -%c no longer supported.", c
);
88 assert_not_reached("Unknown argument");
93 fprintf(stderr
, "Extraneous argument: '%s'\n", argv
[optind
]);
97 deadline
= now(CLOCK_MONOTONIC
) + timeout
* USEC_PER_SEC
;
99 /* guarantee that the udev daemon isn't pre-processing */
101 struct udev_ctrl
*uctrl
;
103 uctrl
= udev_ctrl_new(udev
);
105 if (udev_ctrl_send_ping(uctrl
, MAX(5U, timeout
)) < 0) {
106 log_debug("no connection to daemon");
107 udev_ctrl_unref(uctrl
);
110 udev_ctrl_unref(uctrl
);
114 queue
= udev_queue_new(udev
);
116 log_error("unable to get udev queue");
120 pfd
[0].events
= POLLIN
;
121 pfd
[0].fd
= udev_queue_get_fd(queue
);
123 log_debug("queue is empty, nothing to watch");
129 if (exists
&& access(exists
, F_OK
) >= 0) {
134 /* exit if queue is empty */
135 if (udev_queue_get_queue_is_empty(queue
)) {
140 if (now(CLOCK_MONOTONIC
) >= deadline
)
143 /* wake up when queue is empty */
144 if (poll(pfd
, 1, MSEC_PER_SEC
) > 0 && pfd
[0].revents
& POLLIN
)
145 udev_queue_flush(queue
);
149 udev_queue_unref(queue
);
153 const struct udevadm_cmd udevadm_settle
= {
156 .help
= "Wait for pending udev events",