1 /* SPDX-License-Identifier: GPL-2.0+ */
13 #include "string-util.h"
15 #include "udevadm-util.h"
21 static int exec_list(struct udev_enumerate
*udev_enumerate
, const char *action
, Set
*settle_set
) {
22 struct udev_list_entry
*entry
;
25 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(udev_enumerate
)) {
26 char filename
[UTIL_PATH_SIZE
];
28 _cleanup_close_
int fd
= -1;
30 syspath
= udev_list_entry_get_name(entry
);
32 printf("%s\n", syspath
);
36 strscpyl(filename
, sizeof(filename
), syspath
, "/uevent", NULL
);
37 fd
= open(filename
, O_WRONLY
|O_CLOEXEC
);
42 r
= set_put_strdup(settle_set
, syspath
);
47 if (write(fd
, action
, strlen(action
)) < 0)
48 log_debug_errno(errno
, "error writing '%s' to '%s': %m", action
, filename
);
54 static const char *keyval(const char *str
, const char **val
, char *buf
, size_t size
) {
57 strscpy(buf
, size
,str
);
58 pos
= strchr(buf
, '=');
67 static void help(void) {
68 printf("%s trigger [OPTIONS] DEVPATH\n\n"
69 "Request events from the kernel.\n\n"
70 " -h --help Show this help\n"
71 " -V --version Show package version\n"
72 " -v --verbose Print the list of devices while running\n"
73 " -n --dry-run Do not actually trigger the events\n"
74 " -t --type= Type of events to trigger\n"
75 " devices sysfs devices (default)\n"
76 " subsystems sysfs subsystems and drivers\n"
77 " -c --action=ACTION Event action value, default is \"change\"\n"
78 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
79 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
80 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
81 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
82 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
83 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
84 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
85 " --name-match=NAME Trigger devices with this /dev name\n"
86 " -b --parent-match=NAME Trigger devices with that parent device\n"
87 " -w --settle Wait for the triggered events to complete\n"
88 , program_invocation_short_name
);
91 static int adm_trigger(int argc
, char *argv
[]) {
96 static const struct option options
[] = {
97 { "verbose", no_argument
, NULL
, 'v' },
98 { "dry-run", no_argument
, NULL
, 'n' },
99 { "type", required_argument
, NULL
, 't' },
100 { "action", required_argument
, NULL
, 'c' },
101 { "subsystem-match", required_argument
, NULL
, 's' },
102 { "subsystem-nomatch", required_argument
, NULL
, 'S' },
103 { "attr-match", required_argument
, NULL
, 'a' },
104 { "attr-nomatch", required_argument
, NULL
, 'A' },
105 { "property-match", required_argument
, NULL
, 'p' },
106 { "tag-match", required_argument
, NULL
, 'g' },
107 { "sysname-match", required_argument
, NULL
, 'y' },
108 { "name-match", required_argument
, NULL
, ARG_NAME
},
109 { "parent-match", required_argument
, NULL
, 'b' },
110 { "settle", no_argument
, NULL
, 'w' },
111 { "version", no_argument
, NULL
, 'V' },
112 { "help", no_argument
, NULL
, 'h' },
118 } device_type
= TYPE_DEVICES
;
119 const char *action
= "change";
120 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*udev_enumerate
= NULL
;
121 _cleanup_(udev_monitor_unrefp
) struct udev_monitor
*udev_monitor
= NULL
;
122 _cleanup_close_
int fd_ep
= -1;
124 struct epoll_event ep_udev
;
126 _cleanup_set_free_free_ Set
*settle_set
= NULL
;
129 udev_enumerate
= udev_enumerate_new(NULL
);
133 while ((c
= getopt_long(argc
, argv
, "vnt:c:s:S:a:A:p:g:y:b:wVh", options
, NULL
)) >= 0) {
136 char buf
[UTIL_PATH_SIZE
];
146 if (streq(optarg
, "devices"))
147 device_type
= TYPE_DEVICES
;
148 else if (streq(optarg
, "subsystems"))
149 device_type
= TYPE_SUBSYSTEMS
;
151 log_error("unknown type --type=%s", optarg
);
156 if (!STR_IN_SET(optarg
, "add", "remove", "change")) {
157 log_error("unknown action '%s'", optarg
);
164 r
= udev_enumerate_add_match_subsystem(udev_enumerate
, optarg
);
166 log_error_errno(r
, "could not add subsystem match '%s': %m", optarg
);
171 r
= udev_enumerate_add_nomatch_subsystem(udev_enumerate
, optarg
);
173 log_error_errno(r
, "could not add negative subsystem match '%s': %m", optarg
);
178 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
179 r
= udev_enumerate_add_match_sysattr(udev_enumerate
, key
, val
);
181 log_error_errno(r
, "could not add sysattr match '%s=%s': %m", key
, val
);
186 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
187 r
= udev_enumerate_add_nomatch_sysattr(udev_enumerate
, key
, val
);
189 log_error_errno(r
, "could not add negative sysattr match '%s=%s': %m", key
, val
);
194 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
195 r
= udev_enumerate_add_match_property(udev_enumerate
, key
, val
);
197 log_error_errno(r
, "could not add property match '%s=%s': %m", key
, val
);
202 r
= udev_enumerate_add_match_tag(udev_enumerate
, optarg
);
204 log_error_errno(r
, "could not add tag match '%s': %m", optarg
);
209 r
= udev_enumerate_add_match_sysname(udev_enumerate
, optarg
);
211 log_error_errno(r
, "could not add sysname match '%s': %m", optarg
);
216 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
;
218 dev
= find_device(optarg
, "/sys");
220 log_error("unable to open the device '%s'", optarg
);
224 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
226 log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
236 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
;
238 dev
= find_device(optarg
, "/dev/");
240 log_error("unable to open the device '%s'", optarg
);
244 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
246 log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
261 assert_not_reached("Unknown option");
265 for (; optind
< argc
; optind
++) {
266 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
;
268 dev
= find_device(argv
[optind
], NULL
);
270 log_error("unable to open the device '%s'", argv
[optind
]);
274 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
276 log_error_errno(r
, "could not add tag match '%s': %m", optarg
);
282 fd_ep
= epoll_create1(EPOLL_CLOEXEC
);
284 log_error_errno(errno
, "error creating epoll fd: %m");
288 udev_monitor
= udev_monitor_new_from_netlink(NULL
, "udev");
290 log_error("error: unable to create netlink socket");
293 fd_udev
= udev_monitor_get_fd(udev_monitor
);
295 if (udev_monitor_enable_receiving(udev_monitor
) < 0) {
296 log_error("error: unable to subscribe to udev events");
300 ep_udev
= (struct epoll_event
) { .events
= EPOLLIN
, .data
.fd
= fd_udev
};
301 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fd_udev
, &ep_udev
) < 0) {
302 log_error_errno(errno
, "fail to add fd to epoll: %m");
306 settle_set
= set_new(&string_hash_ops
);
313 switch (device_type
) {
314 case TYPE_SUBSYSTEMS
:
315 udev_enumerate_scan_subsystems(udev_enumerate
);
318 udev_enumerate_scan_devices(udev_enumerate
);
321 assert_not_reached("device_type");
323 r
= exec_list(udev_enumerate
, action
, settle_set
);
327 while (!set_isempty(settle_set
)) {
329 struct epoll_event ev
[4];
332 fdcount
= epoll_wait(fd_ep
, ev
, ELEMENTSOF(ev
), -1);
335 log_error_errno(errno
, "error receiving uevent message: %m");
339 for (i
= 0; i
< fdcount
; i
++) {
340 if (ev
[i
].data
.fd
== fd_udev
&& ev
[i
].events
& EPOLLIN
) {
341 _cleanup_(udev_device_unrefp
) struct udev_device
*device
;
342 const char *syspath
= NULL
;
344 device
= udev_monitor_receive_device(udev_monitor
);
348 syspath
= udev_device_get_syspath(device
);
350 printf("settle %s\n", syspath
);
351 if (!set_remove(settle_set
, syspath
))
352 log_debug("Got epoll event on syspath %s not present in syspath set", syspath
);
360 const struct udevadm_cmd udevadm_trigger
= {
363 .help
= "Request events from the kernel",