1 /* SPDX-License-Identifier: GPL-2.0+ */
9 #include "device-enumerator-private.h"
10 #include "device-private.h"
13 #include "path-util.h"
14 #include "process-util.h"
16 #include "string-util.h"
19 #include "udevadm-util.h"
20 #include "udev-ctrl.h"
23 static bool arg_verbose
= false;
24 static bool arg_dry_run
= false;
26 static int exec_list(sd_device_enumerator
*e
, const char *action
, Set
*settle_set
) {
30 FOREACH_DEVICE_AND_SUBSYSTEM(e
, d
) {
31 _cleanup_free_
char *filename
= NULL
;
34 if (sd_device_get_syspath(d
, &syspath
) < 0)
38 printf("%s\n", syspath
);
42 filename
= path_join(syspath
, "uevent");
46 r
= write_string_file(filename
, action
, WRITE_STRING_FILE_DISABLE_BUFFER
);
48 log_debug_errno(r
, "Failed to write '%s' to '%s', ignoring: %m", action
, filename
);
53 r
= set_put_strdup(settle_set
, syspath
);
62 static int device_monitor_handler(sd_device_monitor
*m
, sd_device
*dev
, void *userdata
) {
63 _cleanup_free_
char *val
= NULL
;
64 Set
*settle_set
= userdata
;
70 if (sd_device_get_syspath(dev
, &syspath
) < 0)
74 printf("settle %s\n", syspath
);
76 val
= set_remove(settle_set
, syspath
);
78 log_debug("Got epoll event on syspath %s not present in syspath set", syspath
);
80 if (set_isempty(settle_set
))
81 return sd_event_exit(sd_device_monitor_get_event(m
), 0);
86 static char* keyval(const char *str
, const char **key
, const char **val
) {
93 pos
= strchr(buf
, '=');
105 static int help(void) {
106 printf("%s trigger [OPTIONS] DEVPATH\n\n"
107 "Request events from the kernel.\n\n"
108 " -h --help Show this help\n"
109 " -V --version Show package version\n"
110 " -v --verbose Print the list of devices while running\n"
111 " -n --dry-run Do not actually trigger the events\n"
112 " -t --type= Type of events to trigger\n"
113 " devices sysfs devices (default)\n"
114 " subsystems sysfs subsystems and drivers\n"
115 " -c --action=ACTION Event action value, default is \"change\"\n"
116 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
117 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
118 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
119 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
120 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
121 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
122 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
123 " --name-match=NAME Trigger devices with this /dev name\n"
124 " -b --parent-match=NAME Trigger devices with that parent device\n"
125 " -w --settle Wait for the triggered events to complete\n"
126 " --wait-daemon[=SECONDS] Wait for udevd daemon to be initialized\n"
127 " before triggering uevents\n"
128 , program_invocation_short_name
);
133 int trigger_main(int argc
, char *argv
[], void *userdata
) {
139 static const struct option options
[] = {
140 { "verbose", no_argument
, NULL
, 'v' },
141 { "dry-run", no_argument
, NULL
, 'n' },
142 { "type", required_argument
, NULL
, 't' },
143 { "action", required_argument
, NULL
, 'c' },
144 { "subsystem-match", required_argument
, NULL
, 's' },
145 { "subsystem-nomatch", required_argument
, NULL
, 'S' },
146 { "attr-match", required_argument
, NULL
, 'a' },
147 { "attr-nomatch", required_argument
, NULL
, 'A' },
148 { "property-match", required_argument
, NULL
, 'p' },
149 { "tag-match", required_argument
, NULL
, 'g' },
150 { "sysname-match", required_argument
, NULL
, 'y' },
151 { "name-match", required_argument
, NULL
, ARG_NAME
},
152 { "parent-match", required_argument
, NULL
, 'b' },
153 { "settle", no_argument
, NULL
, 'w' },
154 { "wait-daemon", optional_argument
, NULL
, ARG_PING
},
155 { "version", no_argument
, NULL
, 'V' },
156 { "help", no_argument
, NULL
, 'h' },
162 } device_type
= TYPE_DEVICES
;
163 const char *action
= "change";
164 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
165 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*m
= NULL
;
166 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
167 _cleanup_set_free_free_ Set
*settle_set
= NULL
;
168 usec_t ping_timeout_usec
= 5 * USEC_PER_SEC
;
169 bool settle
= false, ping
= false;
172 if (running_in_chroot() > 0) {
173 log_info("Running in chroot, ignoring request.");
177 r
= sd_device_enumerator_new(&e
);
181 r
= sd_device_enumerator_allow_uninitialized(e
);
185 while ((c
= getopt_long(argc
, argv
, "vnt:c:s:S:a:A:p:g:y:b:wVh", options
, NULL
)) >= 0) {
186 _cleanup_free_
char *buf
= NULL
;
187 const char *key
, *val
;
197 if (streq(optarg
, "devices"))
198 device_type
= TYPE_DEVICES
;
199 else if (streq(optarg
, "subsystems"))
200 device_type
= TYPE_SUBSYSTEMS
;
202 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown type --type=%s", optarg
);
205 if (device_action_from_string(optarg
) < 0)
206 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown action '%s'", optarg
);
211 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, true);
213 return log_error_errno(r
, "Failed to add subsystem match '%s': %m", optarg
);
216 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, false);
218 return log_error_errno(r
, "Failed to add negative subsystem match '%s': %m", optarg
);
221 buf
= keyval(optarg
, &key
, &val
);
224 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, true);
226 return log_error_errno(r
, "Failed to add sysattr match '%s=%s': %m", key
, val
);
229 buf
= keyval(optarg
, &key
, &val
);
232 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, false);
234 return log_error_errno(r
, "Failed to add negative sysattr match '%s=%s': %m", key
, val
);
237 buf
= keyval(optarg
, &key
, &val
);
240 r
= sd_device_enumerator_add_match_property(e
, key
, val
);
242 return log_error_errno(r
, "Failed to add property match '%s=%s': %m", key
, val
);
245 r
= sd_device_enumerator_add_match_tag(e
, optarg
);
247 return log_error_errno(r
, "Failed to add tag match '%s': %m", optarg
);
250 r
= sd_device_enumerator_add_match_sysname(e
, optarg
);
252 return log_error_errno(r
, "Failed to add sysname match '%s': %m", optarg
);
255 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
257 r
= find_device(optarg
, "/sys", &dev
);
259 return log_error_errno(r
, "Failed to open the device '%s': %m", optarg
);
261 r
= device_enumerator_add_match_parent_incremental(e
, dev
);
263 return log_error_errno(r
, "Failed to add parent match '%s': %m", optarg
);
271 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
273 r
= find_device(optarg
, "/dev/", &dev
);
275 return log_error_errno(r
, "Failed to open the device '%s': %m", optarg
);
277 r
= device_enumerator_add_match_parent_incremental(e
, dev
);
279 return log_error_errno(r
, "Failed to add parent match '%s': %m", optarg
);
286 r
= parse_sec(optarg
, &ping_timeout_usec
);
288 log_error_errno(r
, "Failed to parse timeout value '%s', ignoring: %m", optarg
);
294 return print_version();
300 assert_not_reached("Unknown option");
304 if (!arg_dry_run
|| ping
) {
311 _cleanup_(udev_ctrl_unrefp
) struct udev_ctrl
*uctrl
= NULL
;
313 r
= udev_ctrl_new(&uctrl
);
315 return log_error_errno(r
, "Failed to initialize udev control: %m");
317 r
= udev_ctrl_send_ping(uctrl
);
319 return log_error_errno(r
, "Failed to connect to udev daemon: %m");
321 r
= udev_ctrl_wait(uctrl
, ping_timeout_usec
);
323 return log_error_errno(r
, "Failed to wait for daemon to reply: %m");
326 for (; optind
< argc
; optind
++) {
327 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
329 r
= find_device(argv
[optind
], NULL
, &dev
);
331 return log_error_errno(r
, "Failed to open the device '%s': %m", argv
[optind
]);
333 r
= device_enumerator_add_match_parent_incremental(e
, dev
);
335 return log_error_errno(r
, "Failed to add parent match '%s': %m", argv
[optind
]);
339 settle_set
= set_new(&string_hash_ops
);
343 r
= sd_event_default(&event
);
345 return log_error_errno(r
, "Failed to get default event: %m");
347 r
= sd_device_monitor_new(&m
);
349 return log_error_errno(r
, "Failed to create device monitor object: %m");
351 r
= sd_device_monitor_attach_event(m
, event
);
353 return log_error_errno(r
, "Failed to attach event to device monitor: %m");
355 r
= sd_device_monitor_start(m
, device_monitor_handler
, settle_set
);
357 return log_error_errno(r
, "Failed to start device monitor: %m");
360 switch (device_type
) {
361 case TYPE_SUBSYSTEMS
:
362 r
= device_enumerator_scan_subsystems(e
);
364 return log_error_errno(r
, "Failed to scan subsystems: %m");
367 r
= device_enumerator_scan_devices(e
);
369 return log_error_errno(r
, "Failed to scan devices: %m");
372 assert_not_reached("Unknown device type");
374 r
= exec_list(e
, action
, settle_set
);
378 if (event
&& !set_isempty(settle_set
)) {
379 r
= sd_event_loop(event
);
381 return log_error_errno(r
, "Event loop failed: %m");