1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © 2009 Canonical Ltd.
4 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
16 #include "libudev-util.h"
17 #include "time-util.h"
19 #include "udev-ctrl.h"
23 static usec_t arg_timeout
= 120 * USEC_PER_SEC
;
24 static const char *arg_exists
= NULL
;
26 static int help(void) {
27 printf("%s settle [OPTIONS]\n\n"
28 "Wait for pending udev events.\n\n"
29 " -h --help Show this help\n"
30 " -V --version Show package version\n"
31 " -t --timeout=SEC Maximum time to wait for events\n"
32 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
33 , program_invocation_short_name
);
38 static int parse_argv(int argc
, char *argv
[]) {
39 static const struct option options
[] = {
40 { "timeout", required_argument
, NULL
, 't' },
41 { "exit-if-exists", required_argument
, NULL
, 'E' },
42 { "version", no_argument
, NULL
, 'V' },
43 { "help", no_argument
, NULL
, 'h' },
44 { "seq-start", required_argument
, NULL
, 's' }, /* removed */
45 { "seq-end", required_argument
, NULL
, 'e' }, /* removed */
46 { "quiet", no_argument
, NULL
, 'q' }, /* removed */
52 while ((c
= getopt_long(argc
, argv
, "t:E:Vhs:e:q", options
, NULL
)) >= 0) {
55 r
= parse_sec(optarg
, &arg_timeout
);
57 return log_error_errno(r
, "Failed to parse timeout value '%s': %m", optarg
);
63 return print_version();
69 return log_info_errno(SYNTHETIC_ERRNO(EINVAL
),
70 "Option -%c no longer supported.",
75 assert_not_reached("Unknown option.");
82 int settle_main(int argc
, char *argv
[], void *userdata
) {
83 _cleanup_(udev_queue_unrefp
) struct udev_queue
*queue
= NULL
;
88 r
= parse_argv(argc
, argv
);
92 if (running_in_chroot() > 0) {
93 log_info("Running in chroot, ignoring request.");
97 deadline
= now(CLOCK_MONOTONIC
) + arg_timeout
;
99 /* guarantee that the udev daemon isn't pre-processing */
101 _cleanup_(udev_ctrl_unrefp
) struct udev_ctrl
*uctrl
= NULL
;
103 if (udev_ctrl_new(&uctrl
) >= 0) {
104 r
= udev_ctrl_send_ping(uctrl
);
106 log_debug_errno(r
, "Failed to connect to udev daemon: %m");
110 r
= udev_ctrl_wait(uctrl
, MAX(5 * USEC_PER_SEC
, arg_timeout
));
112 return log_error_errno(r
, "Failed to wait for daemon to reply: %m");
116 queue
= udev_queue_new(NULL
);
118 return log_error_errno(errno
, "Failed to get udev queue: %m");
120 r
= udev_queue_get_fd(queue
);
122 log_debug_errno(r
, "Queue is empty, nothing to watch.");
126 pfd
= (struct pollfd
) {
132 if (arg_exists
&& access(arg_exists
, F_OK
) >= 0)
135 /* exit if queue is empty */
136 if (udev_queue_get_queue_is_empty(queue
))
139 if (now(CLOCK_MONOTONIC
) >= deadline
)
142 /* wake up when queue becomes empty */
143 if (poll(&pfd
, 1, MSEC_PER_SEC
) > 0 && pfd
.revents
& POLLIN
) {
144 r
= udev_queue_flush(queue
);
146 return log_error_errno(r
, "Failed to flush queue: %m");