1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright © 2009 Canonical Ltd.
4 * Copyright © 2009 Scott James Remnant <scott@netsplit.com>
18 #include "libudev-util.h"
19 #include "string-util.h"
21 #include "time-util.h"
22 #include "udev-ctrl.h"
28 static usec_t arg_timeout
= 120 * USEC_PER_SEC
;
29 static const char *arg_exists
= NULL
;
31 static int help(void) {
32 printf("%s settle [OPTIONS]\n\n"
33 "Wait for pending udev events.\n\n"
34 " -h --help Show this help\n"
35 " -V --version Show package version\n"
36 " -t --timeout=SEC Maximum time to wait for events\n"
37 " -E --exit-if-exists=FILE Stop waiting if file exists\n"
38 , program_invocation_short_name
);
43 static int parse_argv(int argc
, char *argv
[]) {
44 static const struct option options
[] = {
45 { "timeout", required_argument
, NULL
, 't' },
46 { "exit-if-exists", required_argument
, NULL
, 'E' },
47 { "version", no_argument
, NULL
, 'V' },
48 { "help", no_argument
, NULL
, 'h' },
49 { "seq-start", required_argument
, NULL
, 's' }, /* removed */
50 { "seq-end", required_argument
, NULL
, 'e' }, /* removed */
51 { "quiet", no_argument
, NULL
, 'q' }, /* removed */
57 while ((c
= getopt_long(argc
, argv
, "t:E:Vhs:e:q", options
, NULL
)) >= 0) {
60 r
= parse_sec(optarg
, &arg_timeout
);
62 return log_error_errno(r
, "Failed to parse timeout value '%s': %m", optarg
);
68 return print_version();
74 return log_info_errno(SYNTHETIC_ERRNO(EINVAL
),
75 "Option -%c no longer supported.",
80 assert_not_reached("Unknown option.");
87 static int emit_deprecation_warning(void) {
88 _cleanup_(sd_bus_flush_close_unrefp
) sd_bus
*bus
= NULL
;
89 _cleanup_free_
char *unit
= NULL
, *unit_path
= NULL
;
90 _cleanup_strv_free_
char **a
= NULL
, **b
= NULL
;
93 r
= sd_pid_get_unit(0, &unit
);
94 if (r
< 0 || !streq(unit
, "systemd-udev-settle.service"))
97 log_notice("systemd-udev-settle.service is deprecated.");
99 r
= sd_bus_open_system(&bus
);
101 return log_debug_errno(r
, "Failed to open system bus, skipping dependency queries: %m");
103 unit_path
= unit_dbus_path_from_name("systemd-udev-settle.service");
107 (void) sd_bus_get_property_strv(
109 "org.freedesktop.systemd1",
111 "org.freedesktop.systemd1.Unit",
116 (void) sd_bus_get_property_strv(
118 "org.freedesktop.systemd1",
120 "org.freedesktop.systemd1.Unit",
125 r
= strv_extend_strv(&a
, b
, true);
129 if (!strv_isempty(a
)) {
130 _cleanup_free_
char *t
= NULL
;
132 t
= strv_join(a
, ", ");
136 log_notice("Hint: please fix %s not to pull it in.", t
);
142 int settle_main(int argc
, char *argv
[], void *userdata
) {
143 _cleanup_(udev_queue_unrefp
) struct udev_queue
*queue
= NULL
;
148 r
= parse_argv(argc
, argv
);
152 if (running_in_chroot() > 0) {
153 log_info("Running in chroot, ignoring request.");
157 deadline
= now(CLOCK_MONOTONIC
) + arg_timeout
;
159 /* guarantee that the udev daemon isn't pre-processing */
161 _cleanup_(udev_ctrl_unrefp
) struct udev_ctrl
*uctrl
= NULL
;
163 if (udev_ctrl_new(&uctrl
) >= 0) {
164 r
= udev_ctrl_send_ping(uctrl
);
166 log_debug_errno(r
, "Failed to connect to udev daemon: %m");
170 r
= udev_ctrl_wait(uctrl
, MAX(5 * USEC_PER_SEC
, arg_timeout
));
172 return log_error_errno(r
, "Failed to wait for daemon to reply: %m");
176 queue
= udev_queue_new(NULL
);
178 return log_error_errno(errno
, "Failed to get udev queue: %m");
180 r
= udev_queue_get_fd(queue
);
182 log_debug_errno(r
, "Queue is empty, nothing to watch.");
186 pfd
= (struct pollfd
) {
191 (void) emit_deprecation_warning();
194 if (arg_exists
&& access(arg_exists
, F_OK
) >= 0)
197 /* exit if queue is empty */
198 if (udev_queue_get_queue_is_empty(queue
))
201 if (now(CLOCK_MONOTONIC
) >= deadline
)
204 /* wake up when queue becomes empty */
205 if (poll(&pfd
, 1, MSEC_PER_SEC
) > 0 && pfd
.revents
& POLLIN
) {
206 r
= udev_queue_flush(queue
);
208 return log_error_errno(r
, "Failed to flush queue: %m");