1 /* SPDX-License-Identifier: GPL-2.0+ */
9 #include "device-enumerator-private.h"
11 #include "path-util.h"
13 #include "string-util.h"
16 #include "udevadm-util.h"
18 static bool arg_verbose
= false;
19 static bool arg_dry_run
= false;
21 static int exec_list(sd_device_enumerator
*e
, const char *action
, Set
*settle_set
) {
25 FOREACH_DEVICE_AND_SUBSYSTEM(e
, d
) {
26 _cleanup_free_
char *filename
= NULL
;
27 _cleanup_close_
int fd
= -1;
30 if (sd_device_get_syspath(d
, &syspath
) < 0)
34 printf("%s\n", syspath
);
38 filename
= path_join(NULL
, syspath
, "uevent");
42 fd
= open(filename
, O_WRONLY
|O_CLOEXEC
);
47 r
= set_put_strdup(settle_set
, syspath
);
52 if (write(fd
, action
, strlen(action
)) < 0)
53 log_debug_errno(errno
, "Failed to write '%s' to '%s', ignoring: %m", action
, filename
);
59 static int device_monitor_handler(sd_device_monitor
*m
, sd_device
*dev
, void *userdata
) {
60 Set
*settle_set
= userdata
;
66 if (sd_device_get_syspath(dev
, &syspath
) < 0)
70 printf("settle %s\n", syspath
);
72 if (!set_remove(settle_set
, syspath
))
73 log_debug("Got epoll event on syspath %s not present in syspath set", syspath
);
75 if (set_isempty(settle_set
))
76 return sd_event_exit(sd_device_monitor_get_event(m
), 0);
81 static char* keyval(const char *str
, const char **key
, const char **val
) {
88 pos
= strchr(buf
, '=');
100 static int help(void) {
101 printf("%s trigger [OPTIONS] DEVPATH\n\n"
102 "Request events from the kernel.\n\n"
103 " -h --help Show this help\n"
104 " -V --version Show package version\n"
105 " -v --verbose Print the list of devices while running\n"
106 " -n --dry-run Do not actually trigger the events\n"
107 " -t --type= Type of events to trigger\n"
108 " devices sysfs devices (default)\n"
109 " subsystems sysfs subsystems and drivers\n"
110 " -c --action=ACTION Event action value, default is \"change\"\n"
111 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
112 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
113 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
114 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
115 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
116 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
117 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
118 " --name-match=NAME Trigger devices with this /dev name\n"
119 " -b --parent-match=NAME Trigger devices with that parent device\n"
120 " -w --settle Wait for the triggered events to complete\n"
121 , program_invocation_short_name
);
126 int trigger_main(int argc
, char *argv
[], void *userdata
) {
131 static const struct option options
[] = {
132 { "verbose", no_argument
, NULL
, 'v' },
133 { "dry-run", no_argument
, NULL
, 'n' },
134 { "type", required_argument
, NULL
, 't' },
135 { "action", required_argument
, NULL
, 'c' },
136 { "subsystem-match", required_argument
, NULL
, 's' },
137 { "subsystem-nomatch", required_argument
, NULL
, 'S' },
138 { "attr-match", required_argument
, NULL
, 'a' },
139 { "attr-nomatch", required_argument
, NULL
, 'A' },
140 { "property-match", required_argument
, NULL
, 'p' },
141 { "tag-match", required_argument
, NULL
, 'g' },
142 { "sysname-match", required_argument
, NULL
, 'y' },
143 { "name-match", required_argument
, NULL
, ARG_NAME
},
144 { "parent-match", required_argument
, NULL
, 'b' },
145 { "settle", no_argument
, NULL
, 'w' },
146 { "version", no_argument
, NULL
, 'V' },
147 { "help", no_argument
, NULL
, 'h' },
153 } device_type
= TYPE_DEVICES
;
154 const char *action
= "change";
155 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
156 _cleanup_(sd_device_monitor_unrefp
) sd_device_monitor
*m
= NULL
;
157 _cleanup_(sd_event_unrefp
) sd_event
*event
= NULL
;
158 _cleanup_set_free_free_ Set
*settle_set
= NULL
;
162 r
= sd_device_enumerator_new(&e
);
166 r
= sd_device_enumerator_allow_uninitialized(e
);
170 while ((c
= getopt_long(argc
, argv
, "vnt:c:s:S:a:A:p:g:y:b:wVh", options
, NULL
)) >= 0) {
171 _cleanup_free_
char *buf
= NULL
;
172 const char *key
, *val
;
182 if (streq(optarg
, "devices"))
183 device_type
= TYPE_DEVICES
;
184 else if (streq(optarg
, "subsystems"))
185 device_type
= TYPE_SUBSYSTEMS
;
187 log_error("Unknown type --type=%s", optarg
);
192 if (STR_IN_SET(optarg
, "add", "remove", "change"))
195 log_error("Unknown action '%s'", optarg
);
201 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, true);
203 return log_error_errno(r
, "Failed to add subsystem match '%s': %m", optarg
);
206 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, false);
208 return log_error_errno(r
, "Failed to add negative subsystem match '%s': %m", optarg
);
211 buf
= keyval(optarg
, &key
, &val
);
214 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, true);
216 return log_error_errno(r
, "Failed to add sysattr match '%s=%s': %m", key
, val
);
219 buf
= keyval(optarg
, &key
, &val
);
222 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, false);
224 return log_error_errno(r
, "Failed to add negative sysattr match '%s=%s': %m", key
, val
);
227 buf
= keyval(optarg
, &key
, &val
);
230 r
= sd_device_enumerator_add_match_property(e
, key
, val
);
232 return log_error_errno(r
, "Failed to add property match '%s=%s': %m", key
, val
);
235 r
= sd_device_enumerator_add_match_tag(e
, optarg
);
237 return log_error_errno(r
, "Failed to add tag match '%s': %m", optarg
);
240 r
= sd_device_enumerator_add_match_sysname(e
, optarg
);
242 return log_error_errno(r
, "Failed to add sysname match '%s': %m", optarg
);
245 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
247 r
= find_device(optarg
, "/sys", &dev
);
249 return log_error_errno(r
, "Failed to open the device '%s': %m", optarg
);
251 r
= sd_device_enumerator_add_match_parent(e
, dev
);
253 return log_error_errno(r
, "Failed to add parent match '%s': %m", optarg
);
261 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
263 r
= find_device(optarg
, "/dev/", &dev
);
265 return log_error_errno(r
, "Failed to open the device '%s': %m", optarg
);
267 r
= sd_device_enumerator_add_match_parent(e
, dev
);
269 return log_error_errno(r
, "Failed to add parent match '%s': %m", optarg
);
274 return print_version();
280 assert_not_reached("Unknown option");
284 for (; optind
< argc
; optind
++) {
285 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
287 r
= find_device(argv
[optind
], NULL
, &dev
);
289 return log_error_errno(r
, "Failed to open the device '%s': %m", argv
[optind
]);
291 r
= sd_device_enumerator_add_match_parent(e
, dev
);
293 return log_error_errno(r
, "Failed to add parent match '%s': %m", argv
[optind
]);
297 settle_set
= set_new(&string_hash_ops
);
301 r
= sd_event_default(&event
);
303 return log_error_errno(r
, "Failed to get default event: %m");
305 r
= sd_device_monitor_new(&m
);
307 return log_error_errno(r
, "Failed to create device monitor object: %m");
309 r
= sd_device_monitor_attach_event(m
, event
, 0);
311 return log_error_errno(r
, "Failed to attach event to device monitor: %m");
313 r
= sd_device_monitor_start(m
, device_monitor_handler
, settle_set
, "udevadm-trigger-device-monitor");
315 return log_error_errno(r
, "Failed to start device monitor: %m");
318 switch (device_type
) {
319 case TYPE_SUBSYSTEMS
:
320 r
= device_enumerator_scan_subsystems(e
);
322 return log_error_errno(r
, "Failed to scan subsystems: %m");
325 r
= device_enumerator_scan_devices(e
);
327 return log_error_errno(r
, "Failed to scan devices: %m");
330 assert_not_reached("Unknown device type");
332 r
= exec_list(e
, action
, settle_set
);
336 if (event
&& !set_isempty(settle_set
)) {
337 r
= sd_event_loop(event
);
339 return log_error_errno(r
, "Event loop failed: %m");