2 * Copyright (C) 2008-2009 Kay Sievers <kay@vrfy.org>
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
27 #include "udev-util.h"
28 #include "udevadm-util.h"
34 static void exec_list(struct udev_enumerate
*udev_enumerate
, const char *action
) {
35 struct udev_list_entry
*entry
;
37 udev_list_entry_foreach(entry
, udev_enumerate_get_list_entry(udev_enumerate
)) {
38 char filename
[UTIL_PATH_SIZE
];
42 printf("%s\n", udev_list_entry_get_name(entry
));
45 strscpyl(filename
, sizeof(filename
), udev_list_entry_get_name(entry
), "/uevent", NULL
);
46 fd
= open(filename
, O_WRONLY
|O_CLOEXEC
);
49 if (write(fd
, action
, strlen(action
)) < 0)
50 log_debug_errno(errno
, "error writing '%s' to '%s': %m", action
, filename
);
55 static const char *keyval(const char *str
, const char **val
, char *buf
, size_t size
) {
58 strscpy(buf
, size
,str
);
59 pos
= strchr(buf
, '=');
68 static void help(void) {
69 printf("%s trigger OPTIONS\n\n"
70 "Request events from the kernel.\n\n"
71 " -h --help Show this help\n"
72 " --version Show package version\n"
73 " -v --verbose Print the list of devices while running\n"
74 " -n --dry-run Do not actually trigger the events\n"
75 " -t --type= Type of events to trigger\n"
76 " devices sysfs devices (default)\n"
77 " subsystems sysfs subsystems and drivers\n"
78 " -c --action=ACTION Event action value, default is \"change\"\n"
79 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
80 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
81 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
82 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
83 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
84 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
85 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
86 " --name-match=NAME Trigger devices with this /dev name\n"
87 " -b --parent-match=NAME Trigger devices with that parent device\n"
88 , program_invocation_short_name
);
91 static int adm_trigger(struct udev
*udev
, 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 { "help", no_argument
, NULL
, 'h' },
116 } device_type
= TYPE_DEVICES
;
117 const char *action
= "change";
118 _cleanup_udev_enumerate_unref_
struct udev_enumerate
*udev_enumerate
= NULL
;
121 udev_enumerate
= udev_enumerate_new(udev
);
122 if (udev_enumerate
== NULL
)
125 while ((c
= getopt_long(argc
, argv
, "vno:t:c:s:S:a:A:p:g:y:b:h", options
, NULL
)) >= 0) {
128 char buf
[UTIL_PATH_SIZE
];
138 if (streq(optarg
, "devices"))
139 device_type
= TYPE_DEVICES
;
140 else if (streq(optarg
, "subsystems"))
141 device_type
= TYPE_SUBSYSTEMS
;
143 log_error("unknown type --type=%s", optarg
);
148 if (!nulstr_contains("add\0" "remove\0" "change\0", optarg
)) {
149 log_error("unknown action '%s'", optarg
);
156 r
= udev_enumerate_add_match_subsystem(udev_enumerate
, optarg
);
158 log_error_errno(r
, "could not add subsystem match '%s': %m", optarg
);
163 r
= udev_enumerate_add_nomatch_subsystem(udev_enumerate
, optarg
);
165 log_error_errno(r
, "could not add negative subsystem match '%s': %m", optarg
);
170 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
171 r
= udev_enumerate_add_match_sysattr(udev_enumerate
, key
, val
);
173 log_error_errno(r
, "could not add sysattr match '%s=%s': %m", key
, val
);
178 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
179 r
= udev_enumerate_add_nomatch_sysattr(udev_enumerate
, key
, val
);
181 log_error_errno(r
, "could not add negative sysattr match '%s=%s': %m", key
, val
);
186 key
= keyval(optarg
, &val
, buf
, sizeof(buf
));
187 r
= udev_enumerate_add_match_property(udev_enumerate
, key
, val
);
189 log_error_errno(r
, "could not add property match '%s=%s': %m", key
, val
);
194 r
= udev_enumerate_add_match_tag(udev_enumerate
, optarg
);
196 log_error_errno(r
, "could not add tag match '%s': %m", optarg
);
201 r
= udev_enumerate_add_match_sysname(udev_enumerate
, optarg
);
203 log_error_errno(r
, "could not add sysname match '%s': %m", optarg
);
208 _cleanup_udev_device_unref_
struct udev_device
*dev
;
210 dev
= find_device(udev
, optarg
, "/sys");
212 log_error("unable to open the device '%s'", optarg
);
216 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
218 log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
225 _cleanup_udev_device_unref_
struct udev_device
*dev
;
227 dev
= find_device(udev
, optarg
, "/dev/");
229 log_error("unable to open the device '%s'", optarg
);
233 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
235 log_error_errno(r
, "could not add parent match '%s': %m", optarg
);
247 assert_not_reached("Unknown option");
251 for (; optind
< argc
; optind
++) {
252 _cleanup_udev_device_unref_
struct udev_device
*dev
;
254 dev
= find_device(udev
, argv
[optind
], NULL
);
256 log_error("unable to open the device '%s'", argv
[optind
]);
260 r
= udev_enumerate_add_match_parent(udev_enumerate
, dev
);
262 log_error_errno(r
, "could not add tag match '%s': %m", optarg
);
267 switch (device_type
) {
268 case TYPE_SUBSYSTEMS
:
269 udev_enumerate_scan_subsystems(udev_enumerate
);
270 exec_list(udev_enumerate
, action
);
273 udev_enumerate_scan_devices(udev_enumerate
);
274 exec_list(udev_enumerate
, action
);
277 assert_not_reached("device_type");
281 const struct udevadm_cmd udevadm_trigger
= {
284 .help
= "Request events from the kernel",