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_full_errno(r
== -ENOENT
? LOG_DEBUG
: LOG_ERR
, r
,
49 "Failed to write '%s' to '%s': %m", action
, filename
);
50 if (ret
== 0 && r
!= -ENOENT
)
56 r
= set_put_strdup(settle_set
, syspath
);
65 static int device_monitor_handler(sd_device_monitor
*m
, sd_device
*dev
, void *userdata
) {
66 _cleanup_free_
char *val
= NULL
;
67 Set
*settle_set
= userdata
;
73 if (sd_device_get_syspath(dev
, &syspath
) < 0)
77 printf("settle %s\n", syspath
);
79 val
= set_remove(settle_set
, syspath
);
81 log_debug("Got epoll event on syspath %s not present in syspath set", syspath
);
83 if (set_isempty(settle_set
))
84 return sd_event_exit(sd_device_monitor_get_event(m
), 0);
89 static char* keyval(const char *str
, const char **key
, const char **val
) {
96 pos
= strchr(buf
, '=');
108 static int help(void) {
109 printf("%s trigger [OPTIONS] DEVPATH\n\n"
110 "Request events from the kernel.\n\n"
111 " -h --help Show this help\n"
112 " -V --version Show package version\n"
113 " -v --verbose Print the list of devices while running\n"
114 " -n --dry-run Do not actually trigger the events\n"
115 " -t --type= Type of events to trigger\n"
116 " devices sysfs devices (default)\n"
117 " subsystems sysfs subsystems and drivers\n"
118 " -c --action=ACTION|help Event action value, default is \"change\"\n"
119 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
120 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
121 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
122 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
123 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
124 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
125 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
126 " --name-match=NAME Trigger devices with this /dev name\n"
127 " -b --parent-match=NAME Trigger devices with that parent device\n"
128 " -w --settle Wait for the triggered events to complete\n"
129 " --wait-daemon[=SECONDS] Wait for udevd daemon to be initialized\n"
130 " before triggering uevents\n"
131 , program_invocation_short_name
);
136 int trigger_main(int argc
, char *argv
[], void *userdata
) {
142 static const struct option options
[] = {
143 { "verbose", no_argument
, NULL
, 'v' },
144 { "dry-run", no_argument
, NULL
, 'n' },
145 { "type", required_argument
, NULL
, 't' },
146 { "action", required_argument
, NULL
, 'c' },
147 { "subsystem-match", required_argument
, NULL
, 's' },
148 { "subsystem-nomatch", required_argument
, NULL
, 'S' },
149 { "attr-match", required_argument
, NULL
, 'a' },
150 { "attr-nomatch", required_argument
, NULL
, 'A' },
151 { "property-match", required_argument
, NULL
, 'p' },
152 { "tag-match", required_argument
, NULL
, 'g' },
153 { "sysname-match", required_argument
, NULL
, 'y' },
154 { "name-match", required_argument
, NULL
, ARG_NAME
},
155 { "parent-match", required_argument
, NULL
, 'b' },
156 { "settle", no_argument
, NULL
, 'w' },
157 { "wait-daemon", optional_argument
, NULL
, ARG_PING
},
158 { "version", no_argument
, NULL
, 'V' },
159 { "help", no_argument
, NULL
, 'h' },
165 } device_type
= TYPE_DEVICES
;
166 const char *action
= "change";
167 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
168 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*m
= NULL
;
169 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
170 _cleanup_set_free_free_ Set
*settle_set
= NULL
;
171 usec_t ping_timeout_usec
= 5 * USEC_PER_SEC
;
172 bool settle
= false, ping
= false;
175 if (running_in_chroot() > 0) {
176 log_info("Running in chroot, ignoring request.");
180 r
= sd_device_enumerator_new(&e
);
184 r
= sd_device_enumerator_allow_uninitialized(e
);
188 while ((c
= getopt_long(argc
, argv
, "vnt:c:s:S:a:A:p:g:y:b:wVh", options
, NULL
)) >= 0) {
189 _cleanup_free_
char *buf
= NULL
;
190 const char *key
, *val
;
200 if (streq(optarg
, "devices"))
201 device_type
= TYPE_DEVICES
;
202 else if (streq(optarg
, "subsystems"))
203 device_type
= TYPE_SUBSYSTEMS
;
205 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown type --type=%s", optarg
);
208 if (streq(optarg
, "help")) {
209 dump_device_action_table();
212 if (device_action_from_string(optarg
) < 0)
213 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown action '%s'", optarg
);
218 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, true);
220 return log_error_errno(r
, "Failed to add subsystem match '%s': %m", optarg
);
223 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, false);
225 return log_error_errno(r
, "Failed to add negative subsystem match '%s': %m", optarg
);
228 buf
= keyval(optarg
, &key
, &val
);
231 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, true);
233 return log_error_errno(r
, "Failed to add sysattr match '%s=%s': %m", key
, val
);
236 buf
= keyval(optarg
, &key
, &val
);
239 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, false);
241 return log_error_errno(r
, "Failed to add negative sysattr match '%s=%s': %m", key
, val
);
244 buf
= keyval(optarg
, &key
, &val
);
247 r
= sd_device_enumerator_add_match_property(e
, key
, val
);
249 return log_error_errno(r
, "Failed to add property match '%s=%s': %m", key
, val
);
252 r
= sd_device_enumerator_add_match_tag(e
, optarg
);
254 return log_error_errno(r
, "Failed to add tag match '%s': %m", optarg
);
257 r
= sd_device_enumerator_add_match_sysname(e
, optarg
);
259 return log_error_errno(r
, "Failed to add sysname match '%s': %m", optarg
);
262 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
264 r
= find_device(optarg
, "/sys", &dev
);
266 return log_error_errno(r
, "Failed to open the device '%s': %m", optarg
);
268 r
= device_enumerator_add_match_parent_incremental(e
, dev
);
270 return log_error_errno(r
, "Failed to add parent match '%s': %m", optarg
);
278 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
280 r
= find_device(optarg
, "/dev/", &dev
);
282 return log_error_errno(r
, "Failed to open the device '%s': %m", optarg
);
284 r
= device_enumerator_add_match_parent_incremental(e
, dev
);
286 return log_error_errno(r
, "Failed to add parent match '%s': %m", optarg
);
293 r
= parse_sec(optarg
, &ping_timeout_usec
);
295 log_error_errno(r
, "Failed to parse timeout value '%s', ignoring: %m", optarg
);
301 return print_version();
307 assert_not_reached("Unknown option");
312 _cleanup_(udev_ctrl_unrefp
) struct udev_ctrl
*uctrl
= NULL
;
314 r
= udev_ctrl_new(&uctrl
);
316 return log_error_errno(r
, "Failed to initialize udev control: %m");
318 r
= udev_ctrl_send_ping(uctrl
);
320 return log_error_errno(r
, "Failed to connect to udev daemon: %m");
322 r
= udev_ctrl_wait(uctrl
, ping_timeout_usec
);
324 return log_error_errno(r
, "Failed to wait for daemon to reply: %m");
327 for (; optind
< argc
; optind
++) {
328 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
330 r
= find_device(argv
[optind
], NULL
, &dev
);
332 return log_error_errno(r
, "Failed to open the device '%s': %m", argv
[optind
]);
334 r
= device_enumerator_add_match_parent_incremental(e
, dev
);
336 return log_error_errno(r
, "Failed to add parent match '%s': %m", argv
[optind
]);
340 settle_set
= set_new(&string_hash_ops
);
344 r
= sd_event_default(&event
);
346 return log_error_errno(r
, "Failed to get default event: %m");
348 r
= sd_device_monitor_new(&m
);
350 return log_error_errno(r
, "Failed to create device monitor object: %m");
352 r
= sd_device_monitor_attach_event(m
, event
);
354 return log_error_errno(r
, "Failed to attach event to device monitor: %m");
356 r
= sd_device_monitor_start(m
, device_monitor_handler
, settle_set
);
358 return log_error_errno(r
, "Failed to start device monitor: %m");
361 switch (device_type
) {
362 case TYPE_SUBSYSTEMS
:
363 r
= device_enumerator_scan_subsystems(e
);
365 return log_error_errno(r
, "Failed to scan subsystems: %m");
368 r
= device_enumerator_scan_devices(e
);
370 return log_error_errno(r
, "Failed to scan devices: %m");
373 assert_not_reached("Unknown device type");
375 r
= exec_list(e
, action
, settle_set
);
379 if (event
&& !set_isempty(settle_set
)) {
380 r
= sd_event_loop(event
);
382 return log_error_errno(r
, "Event loop failed: %m");