1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © 2009 Canonical Ltd.
4 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
16 #include "time-util.h"
20 static usec_t arg_timeout
= 120 * USEC_PER_SEC
;
21 static const char *arg_exists
= NULL
;
23 static int 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=SEC Maximum time to wait for events\n"
29 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
30 , program_invocation_short_name
);
35 static int parse_argv(int argc
, char *argv
[]) {
36 static const struct option options
[] = {
37 { "timeout", required_argument
, NULL
, 't' },
38 { "exit-if-exists", required_argument
, NULL
, 'E' },
39 { "version", no_argument
, NULL
, 'V' },
40 { "help", no_argument
, NULL
, 'h' },
41 { "seq-start", required_argument
, NULL
, 's' }, /* removed */
42 { "seq-end", required_argument
, NULL
, 'e' }, /* removed */
43 { "quiet", no_argument
, NULL
, 'q' }, /* removed */
49 while ((c
= getopt_long(argc
, argv
, "t:E:Vhs:e:q", options
, NULL
)) >= 0) {
52 r
= parse_sec(optarg
, &arg_timeout
);
54 return log_error_errno(r
, "Failed to parse timeout value '%s': %m", optarg
);
66 log_info("Option -%c no longer supported.", c
);
71 assert_not_reached("Unknown option.");
78 int settle_main(int argc
, char *argv
[], void *userdata
) {
79 _cleanup_(udev_queue_unrefp
) struct udev_queue
*queue
= NULL
;
84 r
= parse_argv(argc
, argv
);
88 deadline
= now(CLOCK_MONOTONIC
) + arg_timeout
;
90 /* guarantee that the udev daemon isn't pre-processing */
92 _cleanup_(udev_ctrl_unrefp
) struct udev_ctrl
*uctrl
= NULL
;
94 uctrl
= udev_ctrl_new();
96 r
= udev_ctrl_send_ping(uctrl
, MAX(5U, arg_timeout
/ USEC_PER_SEC
));
98 log_debug_errno(r
, "Failed to connect to udev daemon.");
104 queue
= udev_queue_new(NULL
);
106 return log_error_errno(errno
, "Failed to get udev queue: %m");
108 r
= udev_queue_get_fd(queue
);
110 log_debug_errno(r
, "Queue is empty, nothing to watch.");
114 pfd
= (struct pollfd
) {
120 if (arg_exists
&& access(arg_exists
, F_OK
) >= 0)
123 /* exit if queue is empty */
124 if (udev_queue_get_queue_is_empty(queue
))
127 if (now(CLOCK_MONOTONIC
) >= deadline
)
130 /* wake up when queue becomes empty */
131 if (poll(&pfd
, 1, MSEC_PER_SEC
) > 0 && pfd
.revents
& POLLIN
) {
132 r
= udev_queue_flush(queue
);
134 return log_error_errno(r
, "Failed to flush queue: %m");