1 /* SPDX-License-Identifier: GPL-2.0+ */
12 #include "device-enumerator-private.h"
14 #include "libudev-private.h"
16 #include "string-util.h"
19 #include "udevadm-util.h"
21 static bool arg_verbose
= false;
22 static bool arg_dry_run
= false;
24 static int exec_list(sd_device_enumerator
*e
, const char *action
, Set
*settle_set
) {
28 FOREACH_DEVICE_AND_SUBSYSTEM(e
, d
) {
29 char filename
[UTIL_PATH_SIZE
];
31 _cleanup_close_
int fd
= -1;
33 if (sd_device_get_syspath(d
, &syspath
) < 0)
37 printf("%s\n", syspath
);
41 strscpyl(filename
, sizeof(filename
), syspath
, "/uevent", NULL
);
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
, "error writing '%s' to '%s': %m", action
, filename
);
59 static const char *keyval(const char *str
, const char **val
, char *buf
, size_t size
) {
62 strscpy(buf
, size
,str
);
63 pos
= strchr(buf
, '=');
72 static int help(void) {
73 printf("%s trigger [OPTIONS] DEVPATH\n\n"
74 "Request events from the kernel.\n\n"
75 " -h --help Show this help\n"
76 " -V --version Show package version\n"
77 " -v --verbose Print the list of devices while running\n"
78 " -n --dry-run Do not actually trigger the events\n"
79 " -t --type= Type of events to trigger\n"
80 " devices sysfs devices (default)\n"
81 " subsystems sysfs subsystems and drivers\n"
82 " -c --action=ACTION Event action value, default is \"change\"\n"
83 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
84 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
85 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
86 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
87 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
88 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
89 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
90 " --name-match=NAME Trigger devices with this /dev name\n"
91 " -b --parent-match=NAME Trigger devices with that parent device\n"
92 " -w --settle Wait for the triggered events to complete\n"
93 , program_invocation_short_name
);
98 int trigger_main(int argc
, char *argv
[], void *userdata
) {
103 static const struct option options
[] = {
104 { "verbose", no_argument
, NULL
, 'v' },
105 { "dry-run", no_argument
, NULL
, 'n' },
106 { "type", required_argument
, NULL
, 't' },
107 { "action", required_argument
, NULL
, 'c' },
108 { "subsystem-match", required_argument
, NULL
, 's' },
109 { "subsystem-nomatch", required_argument
, NULL
, 'S' },
110 { "attr-match", required_argument
, NULL
, 'a' },
111 { "attr-nomatch", required_argument
, NULL
, 'A' },
112 { "property-match", required_argument
, NULL
, 'p' },
113 { "tag-match", required_argument
, NULL
, 'g' },
114 { "sysname-match", required_argument
, NULL
, 'y' },
115 { "name-match", required_argument
, NULL
, ARG_NAME
},
116 { "parent-match", required_argument
, NULL
, 'b' },
117 { "settle", no_argument
, NULL
, 'w' },
118 { "version", no_argument
, NULL
, 'V' },
119 { "help", no_argument
, NULL
, 'h' },
125 } device_type
= TYPE_DEVICES
;
126 const char *action
= "change";
127 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
128 _cleanup_(udev_monitor_unrefp
) struct udev_monitor
*udev_monitor
= NULL
;
129 _cleanup_close_
int fd_ep
= -1;
131 struct epoll_event ep_udev
;
133 _cleanup_set_free_free_ Set
*settle_set
= NULL
;
136 r
= sd_device_enumerator_new(&e
);
140 r
= sd_device_enumerator_allow_uninitialized(e
);
144 while ((c
= getopt_long(argc
, argv
, "vnt:c:s:S:a:A:p:g:y:b:wVh", options
, NULL
)) >= 0) {
147 char buf
[UTIL_PATH_SIZE
];
157 if (streq(optarg
, "devices"))
158 device_type
= TYPE_DEVICES
;
159 else if (streq(optarg
, "subsystems"))
160 device_type
= TYPE_SUBSYSTEMS
;
162 log_error("unknown type --type=%s", optarg
);
167 if (STR_IN_SET(optarg
, "add", "remove", "change"))
170 log_error("unknown action '%s'", optarg
);
176 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, true);
178 return log_error_errno(r
, "could not add subsystem match '%s': %m", optarg
);
181 r
= sd_device_enumerator_add_match_subsystem(e
, optarg
, false);
183 return log_error_errno(r
, "could not add negative subsystem match '%s': %m", optarg
);
186 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
187 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, true);
189 return log_error_errno(r
, "could not add sysattr match '%s=%s': %m", key
, val
);
192 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
193 r
= sd_device_enumerator_add_match_sysattr(e
, key
, val
, false);
195 return 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
= sd_device_enumerator_add_match_property(e
, key
, val
);
201 return log_error_errno(r
, "could not add property match '%s=%s': %m", key
, val
);
204 r
= sd_device_enumerator_add_match_tag(e
, optarg
);
206 return log_error_errno(r
, "could not add tag match '%s': %m", optarg
);
209 r
= sd_device_enumerator_add_match_sysname(e
, optarg
);
211 return log_error_errno(r
, "could not add sysname match '%s': %m", optarg
);
214 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
216 r
= find_device(optarg
, "/sys", &dev
);
218 return log_error_errno(r
, "unable to open the device '%s'", optarg
);
220 r
= sd_device_enumerator_add_match_parent(e
, dev
);
222 return log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
230 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
232 r
= find_device(optarg
, "/dev/", &dev
);
234 return log_error_errno(r
, "unable to open the device '%s'", optarg
);
236 r
= sd_device_enumerator_add_match_parent(e
, dev
);
238 return log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
243 return print_version();
249 assert_not_reached("Unknown option");
253 for (; optind
< argc
; optind
++) {
254 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
256 r
= find_device(argv
[optind
], NULL
, &dev
);
258 return log_error_errno(r
, "unable to open the device '%s'", argv
[optind
]);
260 r
= sd_device_enumerator_add_match_parent(e
, dev
);
262 return log_error_errno(r
, "could not add parent match '%s': %m", argv
[optind
]);
266 fd_ep
= epoll_create1(EPOLL_CLOEXEC
);
268 return log_error_errno(errno
, "error creating epoll fd: %m");
270 udev_monitor
= udev_monitor_new_from_netlink(NULL
, "udev");
272 return log_error_errno(errno
, "error: unable to create netlink socket: %m");
274 fd_udev
= udev_monitor_get_fd(udev_monitor
);
276 return log_error_errno(fd_udev
, "Failed to get udev_monitor fd: %m");
278 r
= udev_monitor_enable_receiving(udev_monitor
);
280 return log_error_errno(r
, "error: unable to subscribe to udev events: %m");
282 ep_udev
= (struct epoll_event
) {
286 if (epoll_ctl(fd_ep
, EPOLL_CTL_ADD
, fd_udev
, &ep_udev
) < 0)
287 return log_error_errno(errno
, "fail to add fd to epoll: %m");
289 settle_set
= set_new(&string_hash_ops
);
294 switch (device_type
) {
295 case TYPE_SUBSYSTEMS
:
296 r
= device_enumerator_scan_subsystems(e
);
298 return log_error_errno(r
, "Failed to scan subsystems: %m");
301 r
= device_enumerator_scan_devices(e
);
303 return log_error_errno(r
, "Failed to scan devices: %m");
306 assert_not_reached("device_type");
308 r
= exec_list(e
, action
, settle_set
);
312 while (!set_isempty(settle_set
)) {
314 struct epoll_event ev
[4];
317 fdcount
= epoll_wait(fd_ep
, ev
, ELEMENTSOF(ev
), -1);
320 log_error_errno(errno
, "error receiving uevent message: %m");
324 for (i
= 0; i
< fdcount
; i
++) {
325 _cleanup_(sd_device_unrefp
) sd_device
*dev
= NULL
;
326 const char *syspath
= NULL
;
328 if (!(ev
[i
].data
.fd
== fd_udev
&& ev
[i
].events
& EPOLLIN
))
331 if (udev_monitor_receive_sd_device(udev_monitor
, &dev
) < 0)
334 if (sd_device_get_syspath(dev
, &syspath
) < 0)
338 printf("settle %s\n", syspath
);
340 if (!set_remove(settle_set
, syspath
))
341 log_debug("Got epoll event on syspath %s not present in syspath set", syspath
);