1 /* SPDX-License-Identifier: GPL-2.0+ */
16 #include "string-util.h"
17 #include "udev-util.h"
19 #include "udevadm-util.h"
25 static int exec_list(struct udev_enumerate
*udev_enumerate
, const char *action
, Set
*settle_set
) {
26 struct udev_list_entry
*entry
;
29 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(udev_enumerate
)) {
30 char filename
[UTIL_PATH_SIZE
];
32 _cleanup_close_
int fd
= -1;
34 syspath
= udev_list_entry_get_name(entry
);
36 printf("%s\n", syspath
);
40 strscpyl(filename
, sizeof(filename
), syspath
, "/uevent", NULL
);
41 fd
= open(filename
, O_WRONLY
|O_CLOEXEC
);
46 r
= set_put_strdup(settle_set
, syspath
);
51 if (write(fd
, action
, strlen(action
)) < 0)
52 log_debug_errno(errno
, "error writing '%s' to '%s': %m", action
, filename
);
58 static const char *keyval(const char *str
, const char **val
, char *buf
, size_t size
) {
61 strscpy(buf
, size
,str
);
62 pos
= strchr(buf
, '=');
71 static void help(void) {
72 printf("%s trigger [OPTIONS] DEVPATH\n\n"
73 "Request events from the kernel.\n\n"
74 " -h --help Show this help\n"
75 " -V --version Show package version\n"
76 " -v --verbose Print the list of devices while running\n"
77 " -n --dry-run Do not actually trigger the events\n"
78 " -t --type= Type of events to trigger\n"
79 " devices sysfs devices (default)\n"
80 " subsystems sysfs subsystems and drivers\n"
81 " -c --action=ACTION Event action value, default is \"change\"\n"
82 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
83 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
84 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
85 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
86 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
87 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
88 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
89 " --name-match=NAME Trigger devices with this /dev name\n"
90 " -b --parent-match=NAME Trigger devices with that parent device\n"
91 " -w --settle Wait for the triggered events to complete\n"
92 , program_invocation_short_name
);
95 static int adm_trigger(struct udev
*udev
, int argc
, char *argv
[]) {
100 static const struct option options
[] = {
101 { "verbose", no_argument
, NULL
, 'v' },
102 { "dry-run", no_argument
, NULL
, 'n' },
103 { "type", required_argument
, NULL
, 't' },
104 { "action", required_argument
, NULL
, 'c' },
105 { "subsystem-match", required_argument
, NULL
, 's' },
106 { "subsystem-nomatch", required_argument
, NULL
, 'S' },
107 { "attr-match", required_argument
, NULL
, 'a' },
108 { "attr-nomatch", required_argument
, NULL
, 'A' },
109 { "property-match", required_argument
, NULL
, 'p' },
110 { "tag-match", required_argument
, NULL
, 'g' },
111 { "sysname-match", required_argument
, NULL
, 'y' },
112 { "name-match", required_argument
, NULL
, ARG_NAME
},
113 { "parent-match", required_argument
, NULL
, 'b' },
114 { "settle", no_argument
, NULL
, 'w' },
115 { "version", no_argument
, NULL
, 'V' },
116 { "help", no_argument
, NULL
, 'h' },
122 } device_type
= TYPE_DEVICES
;
123 const char *action
= "change";
124 _cleanup_(udev_enumerate_unrefp
) struct udev_enumerate
*udev_enumerate
= NULL
;
125 _cleanup_(udev_monitor_unrefp
) struct udev_monitor
*udev_monitor
= NULL
;
126 _cleanup_close_
int fd_ep
= -1;
128 struct epoll_event ep_udev
;
130 _cleanup_set_free_free_ Set
*settle_set
= NULL
;
133 udev_enumerate
= udev_enumerate_new(udev
);
137 while ((c
= getopt_long(argc
, argv
, "vnt:c:s:S:a:A:p:g:y:b:wVh", options
, NULL
)) >= 0) {
140 char buf
[UTIL_PATH_SIZE
];
150 if (streq(optarg
, "devices"))
151 device_type
= TYPE_DEVICES
;
152 else if (streq(optarg
, "subsystems"))
153 device_type
= TYPE_SUBSYSTEMS
;
155 log_error("unknown type --type=%s", optarg
);
160 if (!STR_IN_SET(optarg
, "add", "remove", "change")) {
161 log_error("unknown action '%s'", optarg
);
168 r
= udev_enumerate_add_match_subsystem(udev_enumerate
, optarg
);
170 log_error_errno(r
, "could not add subsystem match '%s': %m", optarg
);
175 r
= udev_enumerate_add_nomatch_subsystem(udev_enumerate
, optarg
);
177 log_error_errno(r
, "could not add negative subsystem match '%s': %m", optarg
);
182 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
183 r
= udev_enumerate_add_match_sysattr(udev_enumerate
, key
, val
);
185 log_error_errno(r
, "could not add sysattr match '%s=%s': %m", key
, val
);
190 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
191 r
= udev_enumerate_add_nomatch_sysattr(udev_enumerate
, key
, val
);
193 log_error_errno(r
, "could not add negative sysattr match '%s=%s': %m", key
, val
);
198 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
199 r
= udev_enumerate_add_match_property(udev_enumerate
, key
, val
);
201 log_error_errno(r
, "could not add property match '%s=%s': %m", key
, val
);
206 r
= udev_enumerate_add_match_tag(udev_enumerate
, optarg
);
208 log_error_errno(r
, "could not add tag match '%s': %m", optarg
);
213 r
= udev_enumerate_add_match_sysname(udev_enumerate
, optarg
);
215 log_error_errno(r
, "could not add sysname match '%s': %m", optarg
);
220 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
;
222 dev
= find_device(udev
, optarg
, "/sys");
224 log_error("unable to open the device '%s'", optarg
);
228 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
230 log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
240 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
;
242 dev
= find_device(udev
, optarg
, "/dev/");
244 log_error("unable to open the device '%s'", optarg
);
248 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
250 log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
265 assert_not_reached("Unknown option");
269 for (; optind
< argc
; optind
++) {
270 _cleanup_(udev_device_unrefp
) struct udev_device
*dev
;
272 dev
= find_device(udev
, argv
[optind
], NULL
);
274 log_error("unable to open the device '%s'", argv
[optind
]);
278 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
280 log_error_errno(r
, "could not add tag match '%s': %m", optarg
);
286 fd_ep
= epoll_create1(EPOLL_CLOEXEC
);
288 log_error_errno(errno
, "error creating epoll fd: %m");
292 udev_monitor
= udev_monitor_new_from_netlink(udev
, "udev");
294 log_error("error: unable to create netlink socket");
297 fd_udev
= udev_monitor_get_fd(udev_monitor
);
299 if (udev_monitor_enable_receiving(udev_monitor
) < 0) {
300 log_error("error: unable to subscribe to udev events");
304 ep_udev
= (struct epoll_event
) { .events
= EPOLLIN
, .data
.fd
= fd_udev
};
305 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fd_udev
, &ep_udev
) < 0) {
306 log_error_errno(errno
, "fail to add fd to epoll: %m");
310 settle_set
= set_new(&string_hash_ops
);
317 switch (device_type
) {
318 case TYPE_SUBSYSTEMS
:
319 udev_enumerate_scan_subsystems(udev_enumerate
);
322 udev_enumerate_scan_devices(udev_enumerate
);
325 assert_not_reached("device_type");
327 r
= exec_list(udev_enumerate
, action
, settle_set
);
331 while (!set_isempty(settle_set
)) {
333 struct epoll_event ev
[4];
336 fdcount
= epoll_wait(fd_ep
, ev
, ELEMENTSOF(ev
), -1);
339 log_error_errno(errno
, "error receiving uevent message: %m");
343 for (i
= 0; i
< fdcount
; i
++) {
344 if (ev
[i
].data
.fd
== fd_udev
&& ev
[i
].events
& EPOLLIN
) {
345 _cleanup_(udev_device_unrefp
) struct udev_device
*device
;
346 const char *syspath
= NULL
;
348 device
= udev_monitor_receive_device(udev_monitor
);
352 syspath
= udev_device_get_syspath(device
);
354 printf("settle %s\n", syspath
);
355 if (!set_remove(settle_set
, syspath
))
356 log_debug("Got epoll event on syspath %s not present in syspath set", syspath
);
364 const struct udevadm_cmd udevadm_trigger
= {
367 .help
= "Request events from the kernel",