Debian Codegraph @

src/udev/udevadm-settle.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright © 2009 Canonical Ltd.
4 * Copyright © 2009 Scott James Remnant <[email protected]>
5 */
6
7 #include <unistd.h>
8
9 #include "sd-bus.h"
10 #include "sd-event.h"
11 #include "sd-login.h"
12 #include "sd-messages.h"
13
14 #include "alloc-util.h"
15 #include "bus-util.h"
16 #include "format-table.h"
17 #include "help-util.h"
18 #include "options.h"
19 #include "path-util.h"
20 #include "string-util.h"
21 #include "strv.h"
22 #include "time-util.h"
23 #include "udev-util.h"
24 #include "udevadm.h"
25 #include "udevadm-util.h"
26 #include "unit-def.h"
27 #include "virt.h"
28
29 static usec_t arg_timeout_usec = 120 * USEC_PER_SEC;
30 static const char *arg_exists = NULL;
31
32 static int help(void) {
33 _cleanup_(table_unrefp) Table *options = NULL;
34 int r;
35
36 r = option_parser_get_help_table_ns("udevadm-settle", &options);
37 if (r < 0)
38 return r;
39
40 help_cmdline("settle [OPTIONS]");
41 help_abstract("Wait for pending udev events.");
42 help_section("Options");
43 r = table_print_or_warn(options);
44 if (r < 0)
45 return r;
46
47 help_man_page_reference("udevadm", "8");
48 return 0;
49 }
50
51 static int parse_argv(int argc, char *argv[]) {
52 int r;
53
54 assert(argc >= 0);
55 assert(argv);
56
57 OptionParser opts = { argc, argv, .namespace = "udevadm-settle" };
58
59 FOREACH_OPTION_OR_RETURN(c, &opts)
60 switch (c) {
61
62 OPTION_NAMESPACE("udevadm-settle"): {}
63
64 OPTION_COMMON_HELP:
65 return help();
66
67 OPTION('V', "version", NULL, "Show package version"):
68 return print_version();
69
70 OPTION('t', "timeout", "SEC", "Maximum time to wait for events"):
71 r = parse_sec(opts.arg, &arg_timeout_usec);
72 if (r < 0)
73 return log_error_errno(r, "Failed to parse timeout value '%s': %m", opts.arg);
74 break;
75
76 OPTION('E', "exit-if-exists", "FILE", "Stop waiting if file exists"):
77 if (!path_is_valid(opts.arg))
78 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid path: %s", opts.arg);
79
80 arg_exists = opts.arg;
81 break;
82
83 OPTION('s', "seq-start", "ARG", NULL): {} /* removed */
84 OPTION('e', "seq-end", "ARG", NULL): {} /* removed */
85 OPTION('q', "quiet", NULL, NULL): /* removed */
86 return log_info_errno(SYNTHETIC_ERRNO(EINVAL),
87 "Option -%c no longer supported.",
88 opts.opt->short_code);
89 }
90
91 return 1;
92 }
93
94 static int emit_deprecation_warning(void) {
95 _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
96 _cleanup_strv_free_ char **a = NULL;
97 _cleanup_free_ char *unit = NULL;
98 int r;
99
100 r = sd_pid_get_unit(0, &unit);
101 if (r < 0) {
102 log_debug_errno(r, "Failed to determine unit we run in, ignoring: %m");
103 return 0;
104 }
105
106 if (!streq(unit, "systemd-udev-settle.service"))
107 return 0;
108
109 r = bus_connect_system_systemd(&bus);
110 if (r < 0)
111 log_debug_errno(r, "Failed to open connection to systemd, skipping dependency queries: %m");
112 else {
113 _cleanup_strv_free_ char **b = NULL;
114 _cleanup_free_ char *unit_path = NULL;
115
116 unit_path = unit_dbus_path_from_name("systemd-udev-settle.service");
117 if (!unit_path)
118 return -ENOMEM;
119
120 (void) sd_bus_get_property_strv(
121 bus,
122 "org.freedesktop.systemd1",
123 unit_path,
124 "org.freedesktop.systemd1.Unit",
125 "WantedBy",
126 NULL,
127 &a);
128
129 (void) sd_bus_get_property_strv(
130 bus,
131 "org.freedesktop.systemd1",
132 unit_path,
133 "org.freedesktop.systemd1.Unit",
134 "RequiredBy",
135 NULL,
136 &b);
137
138 r = strv_extend_strv_consume(&a, TAKE_PTR(b), /* filter_duplicates= */ true);
139 if (r < 0)
140 return r;
141 }
142
143 if (strv_isempty(a))
144 /* Print a simple message if we cannot determine the dependencies */
145 log_notice("systemd-udev-settle.service is deprecated.");
146 else {
147 /* Print a longer, structured message if we can acquire the dependencies (this should be the
148 * common case). This is hooked up with a catalog entry and everything. */
149 _cleanup_free_ char *t = NULL;
150
151 t = strv_join(a, ", ");
152 if (!t)
153 return -ENOMEM;
154
155 log_struct(LOG_NOTICE,
156 LOG_MESSAGE("systemd-udev-settle.service is deprecated. Please fix %s not to pull it in.", t),
157 LOG_ITEM("OFFENDING_UNITS=%s", t),
158 LOG_MESSAGE_ID(SD_MESSAGE_SYSTEMD_UDEV_SETTLE_DEPRECATED_STR));
159 }
160
161 return 0;
162 }
163
164 static bool check(void) {
165 int r;
166
167 if (arg_exists) {
168 if (access(arg_exists, F_OK) >= 0)
169 return true;
170
171 if (errno != ENOENT)
172 log_warning_errno(errno, "Failed to check the existence of \"%s\", ignoring: %m", arg_exists);
173 }
174
175 /* exit if queue is empty */
176 r = udev_queue_is_empty();
177 if (r < 0)
178 log_warning_errno(r, "Failed to check if udev queue is empty, ignoring: %m");
179
180 return r > 0;
181 }
182
183 static int on_inotify(sd_event_source *s, const struct inotify_event *event, void *userdata) {
184 assert(s);
185
186 if (check())
187 return sd_event_exit(sd_event_source_get_event(s), 0);
188
189 return 0;
190 }
191
192 int verb_settle_main(int argc, char *argv[], uintptr_t _data, void *userdata) {
193 _cleanup_(sd_event_unrefp) sd_event *event = NULL;
194 int r;
195
196 r = parse_argv(argc, argv);
197 if (r <= 0)
198 return r;
199
200 if (running_in_chroot() > 0) {
201 log_info("Running in chroot, ignoring request.");
202 return 0;
203 }
204
205 (void) emit_deprecation_warning();
206
207 if (getuid() == 0) {
208 r = udev_ping(MAX(5 * USEC_PER_SEC, arg_timeout_usec), /* ignore_connection_failure= */ true);
209 if (r <= 0)
210 return r;
211 } else {
212 /* For non-privileged users, at least check if udevd is running. */
213 if (access("/run/udev/control", F_OK) < 0)
214 return log_error_errno(errno,
215 errno == ENOENT ? "systemd-udevd is not running." :
216 "Failed to check if /run/udev/control exists: %m");
217 }
218
219 r = sd_event_default(&event);
220 if (r < 0)
221 return log_error_errno(r, "Failed to get default sd-event object: %m");
222
223 r = sd_event_add_inotify(event, NULL, "/run/udev" , IN_DELETE, on_inotify, NULL);
224 if (r < 0)
225 return log_error_errno(r, "Failed to add inotify watch for /run/udev: %m");
226
227 if (arg_timeout_usec != USEC_INFINITY) {
228 r = sd_event_add_time_relative(event, NULL, CLOCK_BOOTTIME, arg_timeout_usec, 0,
229 NULL, INT_TO_PTR(-ETIMEDOUT));
230 if (r < 0)
231 return log_error_errno(r, "Failed to add timer event source: %m");
232 }
233
234 /* Check before entering the event loop, as the udev queue may be already empty. */
235 if (check())
236 return 0;
237
238 r = sd_event_loop(event);
239 if (r == -ETIMEDOUT)
240 return log_error_errno(r, "Timed out while waiting for udev queue to empty.");
241 if (r < 0)
242 return log_error_errno(r, "Event loop failed: %m");
243
244 return 0;
245 }