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 bool ignore
= IN_SET(r
, -ENOENT
, -EACCES
, -ENODEV
, -EROFS
);
50 log_full_errno(ignore
? LOG_DEBUG
: LOG_ERR
, r
,
51 "Failed to write '%s' to '%s'%s: %m",
52 action
, filename
, ignore
? ", ignoring" : "");
54 return 0; /* Read only filesystem. Return earlier. */
55 if (ret
== 0 && !ignore
)
61 r
= set_put_strdup(settle_set
, syspath
);
70 static int device_monitor_handler(sd_device_monitor
*m
, sd_device
*dev
, void *userdata
) {
71 _cleanup_free_
char *val
= NULL
;
72 Set
*settle_set
= userdata
;
78 if (sd_device_get_syspath(dev
, &syspath
) < 0)
82 printf("settle %s\n", syspath
);
84 val
= set_remove(settle_set
, syspath
);
86 log_debug("Got epoll event on syspath %s not present in syspath set", syspath
);
88 if (set_isempty(settle_set
))
89 return sd_event_exit(sd_device_monitor_get_event(m
), 0);
94 static char* keyval(const char *str
, const char **key
, const char **val
) {
101 pos
= strchr(buf
, '=');
113 static int help(void) {
114 printf("%s trigger [OPTIONS] DEVPATH\n\n"
115 "Request events from the kernel.\n\n"
116 " -h --help Show this help\n"
117 " -V --version Show package version\n"
118 " -v --verbose Print the list of devices while running\n"
119 " -n --dry-run Do not actually trigger the events\n"
120 " -t --type= Type of events to trigger\n"
121 " devices sysfs devices (default)\n"
122 " subsystems sysfs subsystems and drivers\n"
123 " -c --action=ACTION|help Event action value, default is \"change\"\n"
124 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
125 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
126 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
127 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
128 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
129 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
130 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
131 " --name-match=NAME Trigger devices with this /dev name\n"
132 " -b --parent-match=NAME Trigger devices with that parent device\n"
133 " -w --settle Wait for the triggered events to complete\n"
134 " --wait-daemon[=SECONDS] Wait for udevd daemon to be initialized\n"
135 " before triggering uevents\n"
136 , program_invocation_short_name
);
141 int trigger_main(int argc
, char *argv
[], void *userdata
) {
147 static const struct option options
[] = {
148 { "verbose", no_argument
, NULL
, 'v' },
149 { "dry-run", no_argument
, NULL
, 'n' },
150 { "type", required_argument
, NULL
, 't' },
151 { "action", required_argument
, NULL
, 'c' },
152 { "subsystem-match", required_argument
, NULL
, 's' },
153 { "subsystem-nomatch", required_argument
, NULL
, 'S' },
154 { "attr-match", required_argument
, NULL
, 'a' },
155 { "attr-nomatch", required_argument
, NULL
, 'A' },
156 { "property-match", required_argument
, NULL
, 'p' },
157 { "tag-match", required_argument
, NULL
, 'g' },
158 { "sysname-match", required_argument
, NULL
, 'y' },
159 { "name-match", required_argument
, NULL
, ARG_NAME
},
160 { "parent-match", required_argument
, NULL
, 'b' },
161 { "settle", no_argument
, NULL
, 'w' },
162 { "wait-daemon", optional_argument
, NULL
, ARG_PING
},
163 { "version", no_argument
, NULL
, 'V' },
164 { "help", no_argument
, NULL
, 'h' },
170 } device_type
= TYPE_DEVICES
;
171 const char *action
= "change";
172 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
173 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*m
= NULL
;
174 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
175 _cleanup_set_free_ Set
*settle_set
= NULL
;
176 usec_t ping_timeout_usec
= 5 * USEC_PER_SEC
;
177 bool settle
= false, ping
= false;
180 if (running_in_chroot() > 0) {
181 log_info("Running in chroot, ignoring request.");
185 r
= sd_device_enumerator_new(&e
);
189 r
= sd_device_enumerator_allow_uninitialized(e
);
193 while ((c
= getopt_long(argc
, argv
, "vnt:c:s:S:a:A:p:g:y:b:wVh", options
, NULL
)) >= 0) {
194 _cleanup_free_
char *buf
= NULL
;
195 const char *key
, *val
;
205 if (streq(optarg
, "devices"))
206 device_type
= TYPE_DEVICES
;
207 else if (streq(optarg
, "subsystems"))
208 device_type
= TYPE_SUBSYSTEMS
;
210 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown type --type=%s", optarg
);
213 if (streq(optarg
, "help")) {
214 dump_device_action_table();
217 if (device_action_from_string(optarg
) < 0)
218 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Unknown action '%s'", optarg
);
223 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, true);
225 return log_error_errno(r
, "Failed to add subsystem match '%s': %m", optarg
);
228 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, false);
230 return log_error_errno(r
, "Failed to add negative subsystem match '%s': %m", optarg
);
233 buf
= keyval(optarg
, &key
, &val
);
236 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, true);
238 return log_error_errno(r
, "Failed to add sysattr match '%s=%s': %m", key
, val
);
241 buf
= keyval(optarg
, &key
, &val
);
244 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, false);
246 return log_error_errno(r
, "Failed to add negative sysattr match '%s=%s': %m", key
, val
);
249 buf
= keyval(optarg
, &key
, &val
);
252 r
= sd_device_enumerator_add_match_property(e
, key
, val
);
254 return log_error_errno(r
, "Failed to add property match '%s=%s': %m", key
, val
);
257 r
= sd_device_enumerator_add_match_tag(e
, optarg
);
259 return log_error_errno(r
, "Failed to add tag match '%s': %m", optarg
);
262 r
= sd_device_enumerator_add_match_sysname(e
, optarg
);
264 return log_error_errno(r
, "Failed to add sysname match '%s': %m", optarg
);
267 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
269 r
= find_device(optarg
, "/sys", &dev
);
271 return log_error_errno(r
, "Failed to open the device '%s': %m", optarg
);
273 r
= device_enumerator_add_match_parent_incremental(e
, dev
);
275 return log_error_errno(r
, "Failed to add parent match '%s': %m", optarg
);
283 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
285 r
= find_device(optarg
, "/dev/", &dev
);
287 return log_error_errno(r
, "Failed to open the device '%s': %m", optarg
);
289 r
= device_enumerator_add_match_parent_incremental(e
, dev
);
291 return log_error_errno(r
, "Failed to add parent match '%s': %m", optarg
);
298 r
= parse_sec(optarg
, &ping_timeout_usec
);
300 log_error_errno(r
, "Failed to parse timeout value '%s', ignoring: %m", optarg
);
306 return print_version();
312 assert_not_reached("Unknown option");
317 _cleanup_(udev_ctrl_unrefp
) struct udev_ctrl
*uctrl
= NULL
;
319 r
= udev_ctrl_new(&uctrl
);
321 return log_error_errno(r
, "Failed to initialize udev control: %m");
323 r
= udev_ctrl_send_ping(uctrl
);
325 return log_error_errno(r
, "Failed to connect to udev daemon: %m");
327 r
= udev_ctrl_wait(uctrl
, ping_timeout_usec
);
329 return log_error_errno(r
, "Failed to wait for daemon to reply: %m");
332 for (; optind
< argc
; optind
++) {
333 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
335 r
= find_device(argv
[optind
], NULL
, &dev
);
337 return log_error_errno(r
, "Failed to open the device '%s': %m", argv
[optind
]);
339 r
= device_enumerator_add_match_parent_incremental(e
, dev
);
341 return log_error_errno(r
, "Failed to add parent match '%s': %m", argv
[optind
]);
345 settle_set
= set_new(&string_hash_ops_free
);
349 r
= sd_event_default(&event
);
351 return log_error_errno(r
, "Failed to get default event: %m");
353 r
= sd_device_monitor_new(&m
);
355 return log_error_errno(r
, "Failed to create device monitor object: %m");
357 r
= sd_device_monitor_attach_event(m
, event
);
359 return log_error_errno(r
, "Failed to attach event to device monitor: %m");
361 r
= sd_device_monitor_start(m
, device_monitor_handler
, settle_set
);
363 return log_error_errno(r
, "Failed to start device monitor: %m");
366 switch (device_type
) {
367 case TYPE_SUBSYSTEMS
:
368 r
= device_enumerator_scan_subsystems(e
);
370 return log_error_errno(r
, "Failed to scan subsystems: %m");
373 r
= device_enumerator_scan_devices(e
);
375 return log_error_errno(r
, "Failed to scan devices: %m");
378 assert_not_reached("Unknown device type");
381 r
= exec_list(e
, action
, settle
? &settle_set
: NULL
);
385 if (event
&& !set_isempty(settle_set
)) {
386 r
= sd_event_loop(event
);
388 return log_error_errno(r
, "Event loop failed: %m");