| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <sys/inotify.h> |
| 4 |
#include <unistd.h> |
| 5 |
|
| 6 |
#include "sd-event.h" |
| 7 |
|
| 8 |
#include "device-monitor-private.h" |
| 9 |
#include "device-util.h" |
| 10 |
#include "event-util.h" |
| 11 |
#include "format-table.h" |
| 12 |
#include "fs-util.h" |
| 13 |
#include "help-util.h" |
| 14 |
#include "options.h" |
| 15 |
#include "parse-util.h" |
| 16 |
#include "path-util.h" |
| 17 |
#include "static-destruct.h" |
| 18 |
#include "string-table.h" |
| 19 |
#include "string-util.h" |
| 20 |
#include "strv.h" |
| 21 |
#include "time-util.h" |
| 22 |
#include "udev-util.h" |
| 23 |
#include "udevadm.h" |
| 24 |
|
| 25 |
typedef enum WaitUntil { |
| 26 |
WAIT_UNTIL_INITIALIZED, |
| 27 |
WAIT_UNTIL_ADDED, |
| 28 |
WAIT_UNTIL_REMOVED, |
| 29 |
_WAIT_UNTIL_MAX, |
| 30 |
_WAIT_UNTIL_INVALID = -EINVAL, |
| 31 |
} WaitUntil; |
| 32 |
|
| 33 |
static WaitUntil arg_wait_until = WAIT_UNTIL_INITIALIZED; |
| 34 |
static usec_t arg_timeout_usec = USEC_INFINITY; |
| 35 |
static bool arg_settle = false; |
| 36 |
static char **arg_devices = NULL; |
| 37 |
|
| 38 |
STATIC_DESTRUCTOR_REGISTER(arg_devices, strv_freep); |
| 39 |
|
| 40 |
static const char * const wait_until_table[_WAIT_UNTIL_MAX] = { |
| 41 |
[WAIT_UNTIL_INITIALIZED] = "initialized", |
| 42 |
[WAIT_UNTIL_ADDED] = "added", |
| 43 |
[WAIT_UNTIL_REMOVED] = "removed", |
| 44 |
}; |
| 45 |
|
| 46 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_TO_STRING(wait_until, WaitUntil); |
| 47 |
|
| 48 |
static int check_device(const char *path) { |
| 49 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 50 |
int r; |
| 51 |
|
| 52 |
assert(path); |
| 53 |
|
| 54 |
if (arg_wait_until == WAIT_UNTIL_REMOVED) { |
| 55 |
r = access_nofollow(path, F_OK); |
| 56 |
if (r == -ENOENT) |
| 57 |
return true; |
| 58 |
if (r < 0) |
| 59 |
return r; |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
r = sd_device_new_from_path(&dev, path); |
| 64 |
if (r == -ENODEV) |
| 65 |
return false; |
| 66 |
if (r < 0) |
| 67 |
return r; |
| 68 |
|
| 69 |
if (arg_wait_until == WAIT_UNTIL_INITIALIZED) |
| 70 |
return device_is_processed(dev); |
| 71 |
|
| 72 |
return true; |
| 73 |
} |
| 74 |
|
| 75 |
static bool check(void) { |
| 76 |
int r; |
| 77 |
|
| 78 |
if (arg_settle) { |
| 79 |
r = udev_queue_is_empty(); |
| 80 |
if (r == 0) |
| 81 |
return false; |
| 82 |
if (r < 0) |
| 83 |
log_warning_errno(r, "Failed to check if udev queue is empty, assuming empty: %m"); |
| 84 |
} |
| 85 |
|
| 86 |
STRV_FOREACH(p, arg_devices) { |
| 87 |
r = check_device(*p); |
| 88 |
if (r <= 0) { |
| 89 |
if (r < 0) |
| 90 |
log_warning_errno(r, "Failed to check if device \"%s\" is %s, assuming not %s: %m", |
| 91 |
*p, |
| 92 |
wait_until_to_string(arg_wait_until), |
| 93 |
wait_until_to_string(arg_wait_until)); |
| 94 |
return false; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
return true; |
| 99 |
} |
| 100 |
|
| 101 |
static int check_and_exit(sd_event *event) { |
| 102 |
int r; |
| 103 |
|
| 104 |
assert(event); |
| 105 |
|
| 106 |
if (check()) { |
| 107 |
r = sd_event_exit(event, 0); |
| 108 |
if (r < 0) |
| 109 |
return r; |
| 110 |
|
| 111 |
return 1; |
| 112 |
} |
| 113 |
|
| 114 |
return 0; |
| 115 |
} |
| 116 |
|
| 117 |
static int device_monitor_handler(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
| 118 |
const char *name; |
| 119 |
int r; |
| 120 |
|
| 121 |
assert(monitor); |
| 122 |
assert(device); |
| 123 |
|
| 124 |
if (device_for_action(device, SD_DEVICE_REMOVE) != (arg_wait_until == WAIT_UNTIL_REMOVED)) |
| 125 |
return 0; |
| 126 |
|
| 127 |
if (arg_wait_until == WAIT_UNTIL_REMOVED) |
| 128 |
/* On removed event, the received device may not contain enough information. |
| 129 |
* Let's unconditionally check all requested devices are removed. */ |
| 130 |
return check_and_exit(sd_device_monitor_get_event(monitor)); |
| 131 |
|
| 132 |
/* For other events, at first check if the received device matches with the requested devices, |
| 133 |
* to avoid calling check() so many times within a short time. */ |
| 134 |
|
| 135 |
r = sd_device_get_sysname(device, &name); |
| 136 |
if (r < 0) { |
| 137 |
log_device_warning_errno(device, r, "Failed to get sysname of received device, ignoring: %m"); |
| 138 |
return 0; |
| 139 |
} |
| 140 |
|
| 141 |
STRV_FOREACH(p, arg_devices) { |
| 142 |
const char *s; |
| 143 |
|
| 144 |
if (!path_startswith(*p, "/sys")) |
| 145 |
continue; |
| 146 |
|
| 147 |
r = path_find_last_component(*p, false, NULL, &s); |
| 148 |
if (r < 0) { |
| 149 |
log_warning_errno(r, "Failed to extract filename from \"%s\", ignoring: %m", *p); |
| 150 |
continue; |
| 151 |
} |
| 152 |
if (r == 0) |
| 153 |
continue; |
| 154 |
|
| 155 |
if (strneq(s, name, r)) |
| 156 |
return check_and_exit(sd_device_monitor_get_event(monitor)); |
| 157 |
} |
| 158 |
|
| 159 |
r = sd_device_get_devname(device, &name); |
| 160 |
if (r < 0) { |
| 161 |
if (r != -ENOENT) |
| 162 |
log_device_warning_errno(device, r, "Failed to get devname of received device, ignoring: %m"); |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
if (path_strv_contains(arg_devices, name)) |
| 167 |
return check_and_exit(sd_device_monitor_get_event(monitor)); |
| 168 |
|
| 169 |
FOREACH_DEVICE_DEVLINK(device, link) |
| 170 |
if (path_strv_contains(arg_devices, link)) |
| 171 |
return check_and_exit(sd_device_monitor_get_event(monitor)); |
| 172 |
|
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
static int setup_monitor(sd_event *event, MonitorNetlinkGroup group, const char *description, sd_device_monitor **ret) { |
| 177 |
_cleanup_(sd_device_monitor_unrefp) sd_device_monitor *monitor = NULL; |
| 178 |
int r; |
| 179 |
|
| 180 |
assert(event); |
| 181 |
assert(ret); |
| 182 |
|
| 183 |
r = device_monitor_new_full(&monitor, group, -EBADF); |
| 184 |
if (r < 0) |
| 185 |
return r; |
| 186 |
|
| 187 |
r = sd_device_monitor_attach_event(monitor, event); |
| 188 |
if (r < 0) |
| 189 |
return r; |
| 190 |
|
| 191 |
r = sd_device_monitor_set_description(monitor, description); |
| 192 |
if (r < 0) |
| 193 |
return r; |
| 194 |
|
| 195 |
r = sd_device_monitor_start(monitor, device_monitor_handler, NULL); |
| 196 |
if (r < 0) |
| 197 |
return r; |
| 198 |
|
| 199 |
*ret = TAKE_PTR(monitor); |
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
static int on_inotify(sd_event_source *s, const struct inotify_event *event, void *userdata) { |
| 204 |
return check_and_exit(sd_event_source_get_event(s)); |
| 205 |
} |
| 206 |
|
| 207 |
static int setup_inotify(sd_event *event) { |
| 208 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 209 |
int r; |
| 210 |
|
| 211 |
assert(event); |
| 212 |
|
| 213 |
if (!arg_settle) |
| 214 |
return 0; |
| 215 |
|
| 216 |
r = sd_event_add_inotify(event, &s, "/run/udev" , IN_CREATE | IN_DELETE, on_inotify, NULL); |
| 217 |
if (r < 0) |
| 218 |
return r; |
| 219 |
|
| 220 |
r = sd_event_source_set_description(s, "inotify-event-source"); |
| 221 |
if (r < 0) |
| 222 |
return r; |
| 223 |
|
| 224 |
return sd_event_source_set_floating(s, true); |
| 225 |
} |
| 226 |
|
| 227 |
static int setup_timer(sd_event *event) { |
| 228 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 229 |
int r; |
| 230 |
|
| 231 |
assert(event); |
| 232 |
|
| 233 |
if (arg_timeout_usec == USEC_INFINITY) |
| 234 |
return 0; |
| 235 |
|
| 236 |
r = sd_event_add_time_relative(event, &s, CLOCK_BOOTTIME, arg_timeout_usec, 0, |
| 237 |
NULL, INT_TO_PTR(-ETIMEDOUT)); |
| 238 |
if (r < 0) |
| 239 |
return r; |
| 240 |
|
| 241 |
r = sd_event_source_set_description(s, "timeout-event-source"); |
| 242 |
if (r < 0) |
| 243 |
return r; |
| 244 |
|
| 245 |
return sd_event_source_set_floating(s, true); |
| 246 |
} |
| 247 |
|
| 248 |
static int reset_timer(sd_event *e, sd_event_source **s); |
| 249 |
|
| 250 |
static int on_periodic_timer(sd_event_source *s, uint64_t usec, void *userdata) { |
| 251 |
static unsigned counter = 0; |
| 252 |
sd_event *e; |
| 253 |
int r; |
| 254 |
|
| 255 |
assert(s); |
| 256 |
|
| 257 |
e = sd_event_source_get_event(s); |
| 258 |
|
| 259 |
/* Even if all devices exists, we try to wait for uevents to be emitted from kernel. */ |
| 260 |
if (check()) |
| 261 |
counter++; |
| 262 |
else |
| 263 |
counter = 0; |
| 264 |
|
| 265 |
if (counter >= 2) { |
| 266 |
log_debug("All requested devices popped up without receiving kernel uevents."); |
| 267 |
return sd_event_exit(e, 0); |
| 268 |
} |
| 269 |
|
| 270 |
r = reset_timer(e, &s); |
| 271 |
if (r < 0) |
| 272 |
log_warning_errno(r, "Failed to reset periodic timer event source, ignoring: %m"); |
| 273 |
|
| 274 |
return 0; |
| 275 |
} |
| 276 |
|
| 277 |
static int reset_timer(sd_event *e, sd_event_source **s) { |
| 278 |
return event_reset_time_relative(e, s, CLOCK_BOOTTIME, 250 * USEC_PER_MSEC, 0, |
| 279 |
on_periodic_timer, NULL, 0, "periodic-timer-event-source", false); |
| 280 |
} |
| 281 |
|
| 282 |
static int setup_periodic_timer(sd_event *event) { |
| 283 |
_cleanup_(sd_event_source_unrefp) sd_event_source *s = NULL; |
| 284 |
int r; |
| 285 |
|
| 286 |
assert(event); |
| 287 |
|
| 288 |
r = reset_timer(event, &s); |
| 289 |
if (r < 0) |
| 290 |
return r; |
| 291 |
|
| 292 |
/* Set the lower priority than device monitor, to make uevents always dispatched first. */ |
| 293 |
r = sd_event_source_set_priority(s, SD_EVENT_PRIORITY_NORMAL + 1); |
| 294 |
if (r < 0) |
| 295 |
return r; |
| 296 |
|
| 297 |
return sd_event_source_set_floating(s, true); |
| 298 |
} |
| 299 |
|
| 300 |
static int help(void) { |
| 301 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 302 |
int r; |
| 303 |
|
| 304 |
r = option_parser_get_help_table_ns("udevadm-wait", &options); |
| 305 |
if (r < 0) |
| 306 |
return r; |
| 307 |
|
| 308 |
help_cmdline("wait [OPTIONS] DEVICE [DEVICE…]"); |
| 309 |
help_abstract("Wait for devices or device symlinks being created."); |
| 310 |
help_section("Options"); |
| 311 |
r = table_print_or_warn(options); |
| 312 |
if (r < 0) |
| 313 |
return r; |
| 314 |
|
| 315 |
help_man_page_reference("udevadm", "8"); |
| 316 |
return 0; |
| 317 |
} |
| 318 |
|
| 319 |
static int parse_argv(int argc, char *argv[]) { |
| 320 |
int r; |
| 321 |
|
| 322 |
assert(argc >= 0); |
| 323 |
assert(argv); |
| 324 |
|
| 325 |
OptionParser opts = { argc, argv, .namespace = "udevadm-wait" }; |
| 326 |
|
| 327 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 328 |
switch (c) { |
| 329 |
|
| 330 |
OPTION_NAMESPACE("udevadm-wait"): {} |
| 331 |
|
| 332 |
OPTION_COMMON_HELP: |
| 333 |
return help(); |
| 334 |
|
| 335 |
OPTION('V', "version", NULL, "Show package version"): |
| 336 |
return print_version(); |
| 337 |
|
| 338 |
OPTION('t', "timeout", "SEC", "Maximum time to wait for the device"): |
| 339 |
r = parse_sec(opts.arg, &arg_timeout_usec); |
| 340 |
if (r < 0) |
| 341 |
return log_error_errno(r, "Failed to parse -t/--timeout= parameter: %s", opts.arg); |
| 342 |
break; |
| 343 |
|
| 344 |
OPTION_LONG("initialized", "BOOL", |
| 345 |
"Wait for devices being initialized by systemd-udevd"): |
| 346 |
r = parse_boolean(opts.arg); |
| 347 |
if (r < 0) |
| 348 |
return log_error_errno(r, "Failed to parse --initialized= parameter: %s", opts.arg); |
| 349 |
arg_wait_until = r ? WAIT_UNTIL_INITIALIZED : WAIT_UNTIL_ADDED; |
| 350 |
break; |
| 351 |
|
| 352 |
OPTION_LONG("removed", NULL, "Wait for devices being removed"): |
| 353 |
arg_wait_until = WAIT_UNTIL_REMOVED; |
| 354 |
break; |
| 355 |
|
| 356 |
OPTION_LONG("settle", NULL, "Also wait for all queued events being processed"): |
| 357 |
arg_settle = true; |
| 358 |
break; |
| 359 |
} |
| 360 |
|
| 361 |
char **args = option_parser_get_args(&opts); |
| 362 |
if (strv_isempty(args)) |
| 363 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 364 |
"Too few arguments, expected at least one device path or device symlink."); |
| 365 |
|
| 366 |
arg_devices = strv_copy(args); |
| 367 |
if (!arg_devices) |
| 368 |
return log_oom(); |
| 369 |
|
| 370 |
return 1; /* work to do */ |
| 371 |
} |
| 372 |
|
| 373 |
int verb_wait_main(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 374 |
_cleanup_(sd_device_monitor_unrefp) sd_device_monitor *udev_monitor = NULL, *kernel_monitor = NULL; |
| 375 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 376 |
int r; |
| 377 |
|
| 378 |
r = parse_argv(argc, argv); |
| 379 |
if (r <= 0) |
| 380 |
return r; |
| 381 |
|
| 382 |
STRV_FOREACH(p, arg_devices) { |
| 383 |
path_simplify(*p); |
| 384 |
|
| 385 |
if (!path_is_safe(*p)) |
| 386 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 387 |
"Device path cannot contain \"..\"."); |
| 388 |
|
| 389 |
if (!is_device_path(*p)) |
| 390 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 391 |
"Specified path \"%s\" does not start with \"/dev/\" or \"/sys/\".", *p); |
| 392 |
} |
| 393 |
|
| 394 |
/* Check before configuring event sources, as devices may be already initialized. */ |
| 395 |
if (check()) |
| 396 |
return 0; |
| 397 |
|
| 398 |
r = sd_event_default(&event); |
| 399 |
if (r < 0) |
| 400 |
return log_error_errno(r, "Failed to initialize sd-event: %m"); |
| 401 |
|
| 402 |
r = setup_timer(event); |
| 403 |
if (r < 0) |
| 404 |
return log_error_errno(r, "Failed to set up timeout: %m"); |
| 405 |
|
| 406 |
r = setup_inotify(event); |
| 407 |
if (r < 0) |
| 408 |
return log_error_errno(r, "Failed to set up inotify: %m"); |
| 409 |
|
| 410 |
r = setup_monitor(event, MONITOR_GROUP_UDEV, "udev-uevent-monitor-event-source", &udev_monitor); |
| 411 |
if (r < 0) |
| 412 |
return log_error_errno(r, "Failed to set up udev uevent monitor: %m"); |
| 413 |
|
| 414 |
if (arg_wait_until == WAIT_UNTIL_ADDED) { |
| 415 |
/* If --initialized=no is specified, it is not necessary to wait uevents for the specified |
| 416 |
* devices to be processed by udevd. Hence, let's listen on the kernel's uevent stream. Then, |
| 417 |
* we may be able to finish this program earlier when udevd is very busy. |
| 418 |
* Note, we still need to also setup udev monitor, as this may be invoked with a devlink |
| 419 |
* (e.g. /dev/disk/by-id/foo). In that case, the devlink may not exist when we received a |
| 420 |
* uevent from kernel, as the udevd may not finish to process the uevent yet. Hence, we need |
| 421 |
* to wait until the event is processed by udevd. */ |
| 422 |
r = setup_monitor(event, MONITOR_GROUP_KERNEL, "kernel-uevent-monitor-event-source", &kernel_monitor); |
| 423 |
if (r < 0) |
| 424 |
return log_error_errno(r, "Failed to set up kernel uevent monitor: %m"); |
| 425 |
|
| 426 |
/* This is a workaround for issues #24360 and #24450. |
| 427 |
* For some reasons, the kernel sometimes does not emit uevents for loop block device on |
| 428 |
* attach. Hence, without the periodic timer, no event source for this program will be |
| 429 |
* triggered, and this will be timed out. |
| 430 |
* Theoretically, inotify watch may be better, but this program typically expected to run in |
| 431 |
* a short time. Hence, let's use the simpler periodic timer event source here. */ |
| 432 |
r = setup_periodic_timer(event); |
| 433 |
if (r < 0) |
| 434 |
return log_error_errno(r, "Failed to set up periodic timer: %m"); |
| 435 |
} |
| 436 |
|
| 437 |
/* Check before entering the event loop, as devices may be initialized during setting up event sources. */ |
| 438 |
if (check()) |
| 439 |
return 0; |
| 440 |
|
| 441 |
r = sd_event_loop(event); |
| 442 |
if (r == -ETIMEDOUT) |
| 443 |
return log_error_errno(r, "Timed out for waiting devices being %s.", |
| 444 |
wait_until_to_string(arg_wait_until)); |
| 445 |
if (r < 0) |
| 446 |
return log_error_errno(r, "Event loop failed: %m"); |
| 447 |
|
| 448 |
return 0; |
| 449 |
} |