| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
|
| 5 |
#include "sd-device.h" |
| 6 |
#include "sd-event.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "device-enumerator-private.h" |
| 10 |
#include "device-private.h" |
| 11 |
#include "device-util.h" |
| 12 |
#include "format-table.h" |
| 13 |
#include "help-util.h" |
| 14 |
#include "id128-util.h" |
| 15 |
#include "options.h" |
| 16 |
#include "set.h" |
| 17 |
#include "static-destruct.h" |
| 18 |
#include "string-table.h" |
| 19 |
#include "strv.h" |
| 20 |
#include "time-util.h" |
| 21 |
#include "udevadm.h" |
| 22 |
#include "udevadm-util.h" |
| 23 |
#include "virt.h" |
| 24 |
|
| 25 |
typedef enum { |
| 26 |
SCAN_TYPE_DEVICES, |
| 27 |
SCAN_TYPE_SUBSYSTEMS, |
| 28 |
SCAN_TYPE_ALL, |
| 29 |
_SCAN_TYPE_MAX, |
| 30 |
_SCAN_TYPE_INVALID = -EINVAL, |
| 31 |
} ScanType; |
| 32 |
|
| 33 |
static bool arg_verbose = false; |
| 34 |
static bool arg_dry_run = false; |
| 35 |
static bool arg_quiet = false; |
| 36 |
static bool arg_uuid = false; |
| 37 |
static bool arg_settle = false; |
| 38 |
static ScanType arg_scan_type = SCAN_TYPE_DEVICES; |
| 39 |
static sd_device_action_t arg_action = SD_DEVICE_CHANGE; |
| 40 |
static char **arg_devices = NULL; |
| 41 |
static char **arg_attr_match = NULL; |
| 42 |
static char **arg_attr_nomatch = NULL; |
| 43 |
static char **arg_name_match = NULL; |
| 44 |
static char **arg_parent_match = NULL; |
| 45 |
static char **arg_property_match = NULL; |
| 46 |
static char **arg_subsystem_match = NULL; |
| 47 |
static char **arg_subsystem_nomatch = NULL; |
| 48 |
static char **arg_sysname_match = NULL; |
| 49 |
static char **arg_tag_match = NULL; |
| 50 |
static char **arg_prioritized_subsystems = NULL; |
| 51 |
static int arg_initialized_match = -1; |
| 52 |
static bool arg_include_parents = false; |
| 53 |
static bool arg_ping = false; |
| 54 |
static usec_t arg_ping_timeout_usec = 5 * USEC_PER_SEC; |
| 55 |
|
| 56 |
STATIC_DESTRUCTOR_REGISTER(arg_devices, strv_freep); |
| 57 |
STATIC_DESTRUCTOR_REGISTER(arg_attr_match, strv_freep); |
| 58 |
STATIC_DESTRUCTOR_REGISTER(arg_attr_nomatch, strv_freep); |
| 59 |
STATIC_DESTRUCTOR_REGISTER(arg_name_match, strv_freep); |
| 60 |
STATIC_DESTRUCTOR_REGISTER(arg_parent_match, strv_freep); |
| 61 |
STATIC_DESTRUCTOR_REGISTER(arg_property_match, strv_freep); |
| 62 |
STATIC_DESTRUCTOR_REGISTER(arg_subsystem_match, strv_freep); |
| 63 |
STATIC_DESTRUCTOR_REGISTER(arg_subsystem_nomatch, strv_freep); |
| 64 |
STATIC_DESTRUCTOR_REGISTER(arg_sysname_match, strv_freep); |
| 65 |
STATIC_DESTRUCTOR_REGISTER(arg_tag_match, strv_freep); |
| 66 |
STATIC_DESTRUCTOR_REGISTER(arg_prioritized_subsystems, strv_freep); |
| 67 |
|
| 68 |
static const char *scan_type_table[_SCAN_TYPE_MAX] = { |
| 69 |
[SCAN_TYPE_DEVICES] = "devices", |
| 70 |
[SCAN_TYPE_SUBSYSTEMS] = "subsystems", |
| 71 |
[SCAN_TYPE_ALL] = "all", |
| 72 |
}; |
| 73 |
|
| 74 |
DEFINE_PRIVATE_STRING_TABLE_LOOKUP_FROM_STRING(scan_type, ScanType); |
| 75 |
|
| 76 |
static int exec_list( |
| 77 |
sd_device_enumerator *e, |
| 78 |
sd_device_action_t action, |
| 79 |
Set *settle_ids) { |
| 80 |
|
| 81 |
const char *action_str = device_action_to_string(action); |
| 82 |
int r, ret = 0; |
| 83 |
|
| 84 |
assert(e); |
| 85 |
|
| 86 |
sd_device *d; |
| 87 |
FOREACH_DEVICE_AND_SUBSYSTEM(e, d) { |
| 88 |
|
| 89 |
const char *syspath; |
| 90 |
r = sd_device_get_syspath(d, &syspath); |
| 91 |
if (r < 0) { |
| 92 |
log_debug_errno(r, "Failed to get syspath of enumerated devices, ignoring: %m"); |
| 93 |
continue; |
| 94 |
} |
| 95 |
|
| 96 |
if (arg_verbose) |
| 97 |
printf("%s\n", syspath); |
| 98 |
|
| 99 |
if (arg_dry_run) |
| 100 |
continue; |
| 101 |
|
| 102 |
sd_id128_t id; |
| 103 |
r = sd_device_trigger_with_uuid(d, action, &id); |
| 104 |
if (r < 0) { |
| 105 |
/* ENOENT may be returned when a device does not have /uevent or is already |
| 106 |
* removed. Hence, this is logged at debug level and ignored. |
| 107 |
* |
| 108 |
* ENODEV may be returned by some buggy device drivers e.g. /sys/devices/vio. |
| 109 |
* See, |
| 110 |
* https://github.com/systemd/systemd/issues/13652#issuecomment-535129791 and |
| 111 |
* https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1845319. |
| 112 |
* So, this error is ignored, but logged at warning level to encourage people to |
| 113 |
* fix the driver. |
| 114 |
* |
| 115 |
* EROFS is returned when /sys is read only. In that case, all subsequent |
| 116 |
* writes will also fail, hence return immediately. |
| 117 |
* |
| 118 |
* EACCES or EPERM may be returned when this is invoked by non-privileged user. |
| 119 |
* We do NOT return immediately, but continue operation and propagate the error. |
| 120 |
* Why? Some device can be owned by a user, e.g., network devices configured in |
| 121 |
* a network namespace. See, https://github.com/systemd/systemd/pull/18559 and |
| 122 |
* https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=ebb4a4bf76f164457184a3f43ebc1552416bc823 |
| 123 |
* |
| 124 |
* All other errors are logged at error level, but let's continue the operation, |
| 125 |
* and propagate the error. |
| 126 |
*/ |
| 127 |
|
| 128 |
bool ignore = IN_SET(r, -ENOENT, -ENODEV); |
| 129 |
int level = |
| 130 |
arg_quiet ? LOG_DEBUG : |
| 131 |
r == -ENOENT ? LOG_DEBUG : |
| 132 |
r == -ENODEV ? LOG_WARNING : LOG_ERR; |
| 133 |
|
| 134 |
log_device_full_errno(d, level, r, |
| 135 |
"Failed to write '%s' to '%s/uevent'%s: %m", |
| 136 |
action_str, syspath, ignore ? ", ignoring" : ""); |
| 137 |
|
| 138 |
if (r == -EROFS) |
| 139 |
return r; |
| 140 |
if (!ignore) |
| 141 |
RET_GATHER(ret, r); |
| 142 |
continue; |
| 143 |
} else |
| 144 |
log_device_debug(d, "Triggered device with action '%s'.", action_str); |
| 145 |
|
| 146 |
/* If the user asked for it, write event UUID to stdout */ |
| 147 |
if (arg_uuid) |
| 148 |
printf(SD_ID128_UUID_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(id)); |
| 149 |
|
| 150 |
if (settle_ids) { |
| 151 |
sd_id128_t *dup = newdup(sd_id128_t, &id, 1); |
| 152 |
if (!dup) |
| 153 |
return log_oom(); |
| 154 |
|
| 155 |
r = set_consume(settle_ids, dup); |
| 156 |
if (r < 0) |
| 157 |
return log_oom(); |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
return ret; |
| 162 |
} |
| 163 |
|
| 164 |
static int device_monitor_handler(sd_device_monitor *m, sd_device *dev, void *userdata) { |
| 165 |
Set *settle_ids = ASSERT_PTR(userdata); |
| 166 |
int r; |
| 167 |
|
| 168 |
assert(dev); |
| 169 |
|
| 170 |
sd_id128_t id; |
| 171 |
r = sd_device_get_trigger_uuid(dev, &id); |
| 172 |
if (r < 0) { |
| 173 |
log_device_debug_errno(dev, r, "Got uevent without UUID, ignoring: %m"); |
| 174 |
return 0; |
| 175 |
} |
| 176 |
|
| 177 |
_cleanup_free_ sd_id128_t *saved = set_remove(settle_ids, &id); |
| 178 |
if (!saved) { |
| 179 |
log_device_debug(dev, "Got uevent with unexpected UUID, ignoring."); |
| 180 |
return 0; |
| 181 |
} |
| 182 |
|
| 183 |
if (arg_verbose) { |
| 184 |
const char *syspath; |
| 185 |
|
| 186 |
r = sd_device_get_syspath(dev, &syspath); |
| 187 |
if (r < 0) |
| 188 |
log_device_debug_errno(dev, r, "Failed to get syspath of device event, ignoring: %m"); |
| 189 |
else |
| 190 |
printf("settle %s\n", syspath); |
| 191 |
} |
| 192 |
|
| 193 |
if (arg_uuid) |
| 194 |
printf("settle " SD_ID128_UUID_FORMAT_STR "\n", SD_ID128_FORMAT_VAL(id)); |
| 195 |
|
| 196 |
if (set_isempty(settle_ids)) |
| 197 |
return sd_event_exit(sd_device_monitor_get_event(m), 0); |
| 198 |
|
| 199 |
return 0; |
| 200 |
} |
| 201 |
|
| 202 |
static int add_device_match(sd_device_enumerator *e, const char *s, const char *prefix) { |
| 203 |
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; |
| 204 |
int r; |
| 205 |
|
| 206 |
assert(e); |
| 207 |
assert(s); |
| 208 |
|
| 209 |
r = find_device(s, prefix, &dev); |
| 210 |
if (r < 0) |
| 211 |
return log_error_errno(r, "Failed to open the device '%s': %m", s); |
| 212 |
|
| 213 |
r = device_enumerator_add_match_parent_incremental(e, dev); |
| 214 |
if (r < 0) |
| 215 |
return log_error_errno(r, "Failed to add parent match '%s': %m", s); |
| 216 |
|
| 217 |
return 0; |
| 218 |
} |
| 219 |
|
| 220 |
static int setup_matches(sd_device_enumerator *e) { |
| 221 |
int r; |
| 222 |
|
| 223 |
assert(e); |
| 224 |
|
| 225 |
STRV_FOREACH(d, arg_devices) { |
| 226 |
r = add_device_match(e, *d, /* prefix= */ NULL); |
| 227 |
if (r < 0) |
| 228 |
return r; |
| 229 |
} |
| 230 |
|
| 231 |
STRV_FOREACH(n, arg_name_match) { |
| 232 |
r = add_device_match(e, *n, "/dev/"); |
| 233 |
if (r < 0) |
| 234 |
return r; |
| 235 |
} |
| 236 |
|
| 237 |
STRV_FOREACH(p, arg_parent_match) { |
| 238 |
r = add_device_match(e, *p, "/sys/"); |
| 239 |
if (r < 0) |
| 240 |
return r; |
| 241 |
} |
| 242 |
|
| 243 |
STRV_FOREACH(s, arg_subsystem_match) { |
| 244 |
r = sd_device_enumerator_add_match_subsystem(e, *s, /* match= */ true); |
| 245 |
if (r < 0) |
| 246 |
return log_error_errno(r, "Failed to add subsystem match '%s': %m", *s); |
| 247 |
} |
| 248 |
|
| 249 |
STRV_FOREACH(s, arg_subsystem_nomatch) { |
| 250 |
r = sd_device_enumerator_add_match_subsystem(e, *s, /* match= */ false); |
| 251 |
if (r < 0) |
| 252 |
return log_error_errno(r, "Failed to add negative subsystem match '%s': %m", *s); |
| 253 |
} |
| 254 |
|
| 255 |
STRV_FOREACH(a, arg_attr_match) { |
| 256 |
_cleanup_free_ char *k = NULL, *v = NULL; |
| 257 |
|
| 258 |
r = parse_key_value_argument(*a, /* require_value= */ false, &k, &v); |
| 259 |
if (r < 0) |
| 260 |
return r; |
| 261 |
|
| 262 |
r = sd_device_enumerator_add_match_sysattr(e, k, v, /* match= */ true); |
| 263 |
if (r < 0) |
| 264 |
return log_error_errno(r, "Failed to add sysattr match '%s=%s': %m", k, v); |
| 265 |
} |
| 266 |
|
| 267 |
STRV_FOREACH(a, arg_attr_nomatch) { |
| 268 |
_cleanup_free_ char *k = NULL, *v = NULL; |
| 269 |
|
| 270 |
r = parse_key_value_argument(*a, /* require_value= */ false, &k, &v); |
| 271 |
if (r < 0) |
| 272 |
return r; |
| 273 |
|
| 274 |
r = sd_device_enumerator_add_match_sysattr(e, k, v, /* match= */ false); |
| 275 |
if (r < 0) |
| 276 |
return log_error_errno(r, "Failed to add negative sysattr match '%s=%s': %m", k, v); |
| 277 |
} |
| 278 |
|
| 279 |
STRV_FOREACH(p, arg_property_match) { |
| 280 |
_cleanup_free_ char *k = NULL, *v = NULL; |
| 281 |
|
| 282 |
r = parse_key_value_argument(*p, /* require_value= */ true, &k, &v); |
| 283 |
if (r < 0) |
| 284 |
return r; |
| 285 |
|
| 286 |
r = sd_device_enumerator_add_match_property(e, k, v); |
| 287 |
if (r < 0) |
| 288 |
return log_error_errno(r, "Failed to add property match '%s=%s': %m", k, v); |
| 289 |
} |
| 290 |
|
| 291 |
STRV_FOREACH(t, arg_tag_match) { |
| 292 |
r = sd_device_enumerator_add_match_tag(e, *t); |
| 293 |
if (r < 0) |
| 294 |
return log_error_errno(r, "Failed to add tag match '%s': %m", *t); |
| 295 |
} |
| 296 |
|
| 297 |
STRV_FOREACH(s, arg_sysname_match) { |
| 298 |
r = sd_device_enumerator_add_match_sysname(e, *s); |
| 299 |
if (r < 0) |
| 300 |
return log_error_errno(r, "Failed to add sysname match '%s': %m", *s); |
| 301 |
} |
| 302 |
|
| 303 |
STRV_FOREACH(p, arg_prioritized_subsystems) { |
| 304 |
r = device_enumerator_add_prioritized_subsystem(e, *p); |
| 305 |
if (r < 0) |
| 306 |
return log_error_errno(r, "Failed to add prioritized subsystem '%s': %m", *p); |
| 307 |
} |
| 308 |
|
| 309 |
if (arg_initialized_match != -1) { |
| 310 |
r = device_enumerator_add_match_is_initialized(e, arg_initialized_match); |
| 311 |
if (r < 0) |
| 312 |
return log_error_errno(r, "Failed to set initialized filter: %m"); |
| 313 |
} |
| 314 |
|
| 315 |
if (arg_include_parents) { |
| 316 |
r = sd_device_enumerator_add_all_parents(e); |
| 317 |
if (r < 0) |
| 318 |
return log_error_errno(r, "Failed to always include all parents: %m"); |
| 319 |
} |
| 320 |
|
| 321 |
return 0; |
| 322 |
} |
| 323 |
|
| 324 |
static int help(void) { |
| 325 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 326 |
int r; |
| 327 |
|
| 328 |
r = option_parser_get_help_table_ns("udevadm-trigger", &options); |
| 329 |
if (r < 0) |
| 330 |
return r; |
| 331 |
|
| 332 |
help_cmdline("trigger [OPTIONS] DEVPATH"); |
| 333 |
help_abstract("Request events from the kernel."); |
| 334 |
help_section("Options"); |
| 335 |
r = table_print_or_warn(options); |
| 336 |
if (r < 0) |
| 337 |
return r; |
| 338 |
|
| 339 |
help_man_page_reference("udevadm", "8"); |
| 340 |
return 0; |
| 341 |
} |
| 342 |
|
| 343 |
static int parse_argv(int argc, char *argv[]) { |
| 344 |
int r; |
| 345 |
|
| 346 |
assert(argc >= 0); |
| 347 |
assert(argv); |
| 348 |
|
| 349 |
OptionParser opts = { argc, argv, .namespace = "udevadm-trigger" }; |
| 350 |
|
| 351 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 352 |
switch (c) { |
| 353 |
|
| 354 |
OPTION_NAMESPACE("udevadm-trigger"): {} |
| 355 |
|
| 356 |
OPTION_COMMON_HELP: |
| 357 |
return help(); |
| 358 |
|
| 359 |
OPTION('V', "version", NULL, "Show package version"): |
| 360 |
return print_version(); |
| 361 |
|
| 362 |
OPTION('v', "verbose", NULL, "Print the list of devices while running"): |
| 363 |
arg_verbose = true; |
| 364 |
break; |
| 365 |
|
| 366 |
OPTION('n', "dry-run", NULL, "Do not actually trigger the events"): |
| 367 |
arg_dry_run = true; |
| 368 |
break; |
| 369 |
|
| 370 |
OPTION('q', "quiet", NULL, "Suppress error logging in triggering events"): |
| 371 |
arg_quiet = true; |
| 372 |
break; |
| 373 |
|
| 374 |
OPTION('t', "type", "TYPE", "Type of sysfs events to trigger:"): {} |
| 375 |
OPTION_HELP_VERBATIM(" devices", "- devices (default)"): {} |
| 376 |
OPTION_HELP_VERBATIM(" subsystems", "- subsystems and drivers"): {} |
| 377 |
OPTION_HELP_VERBATIM(" all", "- devices, subsystems, and drivers"): |
| 378 |
arg_scan_type = scan_type_from_string(opts.arg); |
| 379 |
if (arg_scan_type < 0) |
| 380 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Unknown type --type=%s", opts.arg); |
| 381 |
break; |
| 382 |
|
| 383 |
OPTION('c', "action", "ACTION|help", "Event action value, default is \"change\""): |
| 384 |
r = parse_device_action(opts.arg, &arg_action); |
| 385 |
if (r <= 0) |
| 386 |
return r; |
| 387 |
break; |
| 388 |
|
| 389 |
OPTION('s', "subsystem-match", "SUBSYSTEM", |
| 390 |
"Trigger devices from a matching subsystem"): |
| 391 |
r = strv_extend(&arg_subsystem_match, opts.arg); |
| 392 |
if (r < 0) |
| 393 |
return log_oom(); |
| 394 |
break; |
| 395 |
|
| 396 |
OPTION('S', "subsystem-nomatch", "SUBSYSTEM", |
| 397 |
"Exclude devices from a matching subsystem"): |
| 398 |
r = strv_extend(&arg_subsystem_nomatch, opts.arg); |
| 399 |
if (r < 0) |
| 400 |
return log_oom(); |
| 401 |
break; |
| 402 |
|
| 403 |
OPTION('a', "attr-match", "FILE[=VALUE]", |
| 404 |
"Trigger devices with a matching attribute"): |
| 405 |
r = strv_extend(&arg_attr_match, opts.arg); |
| 406 |
if (r < 0) |
| 407 |
return log_oom(); |
| 408 |
break; |
| 409 |
|
| 410 |
OPTION('A', "attr-nomatch", "FILE[=VALUE]", |
| 411 |
"Exclude devices with a matching attribute"): |
| 412 |
r = strv_extend(&arg_attr_nomatch, opts.arg); |
| 413 |
if (r < 0) |
| 414 |
return log_oom(); |
| 415 |
break; |
| 416 |
|
| 417 |
OPTION('p', "property-match", "KEY=VALUE", |
| 418 |
"Trigger devices with a matching property"): |
| 419 |
r = strv_extend(&arg_property_match, opts.arg); |
| 420 |
if (r < 0) |
| 421 |
return log_oom(); |
| 422 |
break; |
| 423 |
|
| 424 |
OPTION('g', "tag-match", "TAG", "Trigger devices with a matching tag"): |
| 425 |
r = strv_extend(&arg_tag_match, opts.arg); |
| 426 |
if (r < 0) |
| 427 |
return log_oom(); |
| 428 |
break; |
| 429 |
|
| 430 |
OPTION('y', "sysname-match", "NAME", "Trigger devices with this /sys path"): |
| 431 |
r = strv_extend(&arg_sysname_match, opts.arg); |
| 432 |
if (r < 0) |
| 433 |
return log_oom(); |
| 434 |
break; |
| 435 |
|
| 436 |
OPTION_LONG("name-match", "NAME", "Trigger devices with this /dev name"): |
| 437 |
r = strv_extend(&arg_name_match, opts.arg); |
| 438 |
if (r < 0) |
| 439 |
return log_oom(); |
| 440 |
break; |
| 441 |
|
| 442 |
OPTION('b', "parent-match", "NAME", "Trigger devices with that parent device"): |
| 443 |
r = strv_extend(&arg_parent_match, opts.arg); |
| 444 |
if (r < 0) |
| 445 |
return log_oom(); |
| 446 |
break; |
| 447 |
|
| 448 |
OPTION_LONG("include-parents", NULL, "Trigger parent devices of found devices"): |
| 449 |
arg_include_parents = true; |
| 450 |
break; |
| 451 |
|
| 452 |
OPTION_LONG("initialized-match", NULL, |
| 453 |
"Trigger devices that are already initialized"): |
| 454 |
arg_initialized_match = MATCH_INITIALIZED_YES; |
| 455 |
break; |
| 456 |
|
| 457 |
OPTION_LONG("initialized-nomatch", NULL, |
| 458 |
"Trigger devices that are not initialized yet"): |
| 459 |
arg_initialized_match = MATCH_INITIALIZED_NO; |
| 460 |
break; |
| 461 |
|
| 462 |
OPTION('w', "settle", NULL, "Wait for the triggered events to complete"): |
| 463 |
arg_settle = true; |
| 464 |
break; |
| 465 |
|
| 466 |
OPTION_LONG_FLAGS(OPTION_OPTIONAL_ARG, "wait-daemon", "SECONDS", |
| 467 |
"Wait for udevd daemon to be initialized before triggering uevents"): |
| 468 |
arg_ping = true; |
| 469 |
if (opts.arg) { |
| 470 |
r = parse_sec(opts.arg, &arg_ping_timeout_usec); |
| 471 |
if (r < 0) |
| 472 |
log_error_errno(r, "Failed to parse timeout value '%s', ignoring: %m", opts.arg); |
| 473 |
} |
| 474 |
break; |
| 475 |
|
| 476 |
OPTION_LONG("uuid", NULL, "Print synthetic uevent UUID"): |
| 477 |
arg_uuid = true; |
| 478 |
break; |
| 479 |
|
| 480 |
OPTION_LONG("prioritized-subsystem", "SUBSYSTEM[,SUBSYSTEM…]", |
| 481 |
"Trigger devices from a matching subsystem first"): |
| 482 |
r = strv_split_and_extend(&arg_prioritized_subsystems, opts.arg, ",", /* filter_duplicates= */ false); |
| 483 |
if (r < 0) |
| 484 |
return log_oom(); |
| 485 |
break; |
| 486 |
} |
| 487 |
|
| 488 |
r = strv_extend_strv(&arg_devices, option_parser_get_args(&opts), /* filter_duplicates= */ false); |
| 489 |
if (r < 0) |
| 490 |
return log_error_errno(r, "Failed to build argument list: %m"); |
| 491 |
|
| 492 |
return 1; |
| 493 |
} |
| 494 |
|
| 495 |
int verb_trigger_main(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 496 |
_cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
| 497 |
_cleanup_(sd_device_monitor_unrefp) sd_device_monitor *m = NULL; |
| 498 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 499 |
_cleanup_set_free_ Set *settle_ids = NULL; |
| 500 |
int r; |
| 501 |
|
| 502 |
if (running_in_chroot() > 0) { |
| 503 |
log_info("Running in chroot, ignoring request."); |
| 504 |
return 0; |
| 505 |
} |
| 506 |
|
| 507 |
r = parse_argv(argc, argv); |
| 508 |
if (r <= 0) |
| 509 |
return r; |
| 510 |
|
| 511 |
r = sd_device_enumerator_new(&e); |
| 512 |
if (r < 0) |
| 513 |
return r; |
| 514 |
|
| 515 |
r = sd_device_enumerator_allow_uninitialized(e); |
| 516 |
if (r < 0) |
| 517 |
return r; |
| 518 |
|
| 519 |
r = setup_matches(e); |
| 520 |
if (r < 0) |
| 521 |
return r; |
| 522 |
|
| 523 |
if (arg_ping) { |
| 524 |
r = udev_ping(arg_ping_timeout_usec, /* ignore_connection_failure= */ false); |
| 525 |
if (r < 0) |
| 526 |
return r; |
| 527 |
assert(r > 0); |
| 528 |
} |
| 529 |
|
| 530 |
if (arg_settle) { |
| 531 |
r = sd_event_default(&event); |
| 532 |
if (r < 0) |
| 533 |
return log_error_errno(r, "Failed to get default event: %m"); |
| 534 |
|
| 535 |
r = sd_device_monitor_new(&m); |
| 536 |
if (r < 0) |
| 537 |
return log_error_errno(r, "Failed to create device monitor object: %m"); |
| 538 |
|
| 539 |
r = sd_device_monitor_attach_event(m, event); |
| 540 |
if (r < 0) |
| 541 |
return log_error_errno(r, "Failed to attach event to device monitor: %m"); |
| 542 |
|
| 543 |
settle_ids = set_new(&id128_hash_ops_free); |
| 544 |
if (!settle_ids) |
| 545 |
return log_oom(); |
| 546 |
|
| 547 |
r = sd_device_monitor_start(m, device_monitor_handler, settle_ids); |
| 548 |
if (r < 0) |
| 549 |
return log_error_errno(r, "Failed to start device monitor: %m"); |
| 550 |
} |
| 551 |
|
| 552 |
switch (arg_scan_type) { |
| 553 |
case SCAN_TYPE_SUBSYSTEMS: |
| 554 |
r = device_enumerator_scan_subsystems(e); |
| 555 |
if (r < 0) |
| 556 |
return log_error_errno(r, "Failed to scan subsystems: %m"); |
| 557 |
break; |
| 558 |
case SCAN_TYPE_DEVICES: |
| 559 |
r = device_enumerator_scan_devices(e); |
| 560 |
if (r < 0) |
| 561 |
return log_error_errno(r, "Failed to scan devices: %m"); |
| 562 |
break; |
| 563 |
case SCAN_TYPE_ALL: |
| 564 |
r = device_enumerator_scan_devices_and_subsystems(e); |
| 565 |
if (r < 0) |
| 566 |
return log_error_errno(r, "Failed to scan devices and subsystems: %m"); |
| 567 |
break; |
| 568 |
default: |
| 569 |
assert_not_reached(); |
| 570 |
} |
| 571 |
|
| 572 |
r = exec_list(e, arg_action, settle_ids); |
| 573 |
if (r < 0) |
| 574 |
return r; |
| 575 |
|
| 576 |
if (set_isempty(settle_ids)) |
| 577 |
return 0; |
| 578 |
|
| 579 |
r = sd_event_loop(event); |
| 580 |
if (r < 0) |
| 581 |
return log_error_errno(r, "Event loop failed: %m"); |
| 582 |
|
| 583 |
return 0; |
| 584 |
} |