1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 * Copyright (C) 2008-2009 Kay Sievers <kay@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include "string-util.h"
29 #include "udev-util.h"
31 #include "udevadm-util.h"
37 static void exec_list(struct udev_enumerate
*udev_enumerate
, const char *action
) {
38 struct udev_list_entry
*entry
;
40 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(udev_enumerate
)) {
41 char filename
[UTIL_PATH_SIZE
];
45 printf("%s\n", udev_list_entry_get_name(entry
));
48 strscpyl(filename
, sizeof(filename
), udev_list_entry_get_name(entry
), "/uevent", NULL
);
49 fd
= open(filename
, O_WRONLY
|O_CLOEXEC
);
52 if (write(fd
, action
, strlen(action
)) < 0)
53 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\n\n"
73 "Request events from the kernel.\n\n"
74 " -h --help Show this help\n"
75 " --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 , program_invocation_short_name
);
94 static int adm_trigger(struct udev
*udev
, int argc
, char *argv
[]) {
99 static const struct option options
[] = {
100 { "verbose", no_argument
, NULL
, 'v' },
101 { "dry-run", no_argument
, NULL
, 'n' },
102 { "type", required_argument
, NULL
, 't' },
103 { "action", required_argument
, NULL
, 'c' },
104 { "subsystem-match", required_argument
, NULL
, 's' },
105 { "subsystem-nomatch", required_argument
, NULL
, 'S' },
106 { "attr-match", required_argument
, NULL
, 'a' },
107 { "attr-nomatch", required_argument
, NULL
, 'A' },
108 { "property-match", required_argument
, NULL
, 'p' },
109 { "tag-match", required_argument
, NULL
, 'g' },
110 { "sysname-match", required_argument
, NULL
, 'y' },
111 { "name-match", required_argument
, NULL
, ARG_NAME
},
112 { "parent-match", required_argument
, NULL
, 'b' },
113 { "help", no_argument
, NULL
, 'h' },
119 } device_type
= TYPE_DEVICES
;
120 const char *action
= "change";
121 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*udev_enumerate
= NULL
;
124 udev_enumerate
= udev_enumerate_new(udev
);
125 if (udev_enumerate
== NULL
)
128 while ((c
= getopt_long(argc
, argv
, "vno:t:c:s:S:a:A:p:g:y:b:h", options
, NULL
)) >= 0) {
131 char buf
[UTIL_PATH_SIZE
];
141 if (streq(optarg
, "devices"))
142 device_type
= TYPE_DEVICES
;
143 else if (streq(optarg
, "subsystems"))
144 device_type
= TYPE_SUBSYSTEMS
;
146 log_error("unknown type --type=%s", optarg
);
151 if (!nulstr_contains("add\0" "remove\0" "change\0", optarg
)) {
152 log_error("unknown action '%s'", optarg
);
159 r
= udev_enumerate_add_match_subsystem(udev_enumerate
, optarg
);
161 log_error_errno(r
, "could not add subsystem match '%s': %m", optarg
);
166 r
= udev_enumerate_add_nomatch_subsystem(udev_enumerate
, optarg
);
168 log_error_errno(r
, "could not add negative subsystem match '%s': %m", optarg
);
173 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
174 r
= udev_enumerate_add_match_sysattr(udev_enumerate
, key
, val
);
176 log_error_errno(r
, "could not add sysattr match '%s=%s': %m", key
, val
);
181 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
182 r
= udev_enumerate_add_nomatch_sysattr(udev_enumerate
, key
, val
);
184 log_error_errno(r
, "could not add negative sysattr match '%s=%s': %m", key
, val
);
189 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
190 r
= udev_enumerate_add_match_property(udev_enumerate
, key
, val
);
192 log_error_errno(r
, "could not add property match '%s=%s': %m", key
, val
);
197 r
= udev_enumerate_add_match_tag(udev_enumerate
, optarg
);
199 log_error_errno(r
, "could not add tag match '%s': %m", optarg
);
204 r
= udev_enumerate_add_match_sysname(udev_enumerate
, optarg
);
206 log_error_errno(r
, "could not add sysname match '%s': %m", optarg
);
211 _cleanup_udev_device_unref_
struct udev_device
*dev
;
213 dev
= find_device(udev
, optarg
, "/sys");
215 log_error("unable to open the device '%s'", optarg
);
219 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
221 log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
228 _cleanup_udev_device_unref_
struct udev_device
*dev
;
230 dev
= find_device(udev
, optarg
, "/dev/");
232 log_error("unable to open the device '%s'", optarg
);
236 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
238 log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
250 assert_not_reached("Unknown option");
254 for (; optind
< argc
; optind
++) {
255 _cleanup_udev_device_unref_
struct udev_device
*dev
;
257 dev
= find_device(udev
, argv
[optind
], NULL
);
259 log_error("unable to open the device '%s'", argv
[optind
]);
263 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
265 log_error_errno(r
, "could not add tag match '%s': %m", optarg
);
270 switch (device_type
) {
271 case TYPE_SUBSYSTEMS
:
272 udev_enumerate_scan_subsystems(udev_enumerate
);
273 exec_list(udev_enumerate
, action
);
276 udev_enumerate_scan_devices(udev_enumerate
);
277 exec_list(udev_enumerate
, action
);
280 assert_not_reached("device_type");
284 const struct udevadm_cmd udevadm_trigger
= {
287 .help
= "Request events from the kernel",