]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-trigger.c
Merge pull request #2085 from fbuihuu/more-use-of-check-load-state
[thirdparty/systemd.git] / src / udev / udevadm-trigger.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /*
4 * Copyright (C) 2008-2009 Kay Sievers <kay@vrfy.org>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 #include <errno.h>
21 #include <fcntl.h>
22 #include <getopt.h>
23 #include <stddef.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <unistd.h>
27
28 #include "string-util.h"
29 #include "udev-util.h"
30 #include "udev.h"
31 #include "udevadm-util.h"
32 #include "util.h"
33
34 static int verbose;
35 static int dry_run;
36
37 static void exec_list(struct udev_enumerate *udev_enumerate, const char *action) {
38 struct udev_list_entry *entry;
39
40 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
41 char filename[UTIL_PATH_SIZE];
42 int fd;
43
44 if (verbose)
45 printf("%s\n", udev_list_entry_get_name(entry));
46 if (dry_run)
47 continue;
48 strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
49 fd = open(filename, O_WRONLY|O_CLOEXEC);
50 if (fd < 0)
51 continue;
52 if (write(fd, action, strlen(action)) < 0)
53 log_debug_errno(errno, "error writing '%s' to '%s': %m", action, filename);
54 close(fd);
55 }
56 }
57
58 static const char *keyval(const char *str, const char **val, char *buf, size_t size) {
59 char *pos;
60
61 strscpy(buf, size,str);
62 pos = strchr(buf, '=');
63 if (pos != NULL) {
64 pos[0] = 0;
65 pos++;
66 }
67 *val = pos;
68 return buf;
69 }
70
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);
92 }
93
94 static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
95 enum {
96 ARG_NAME = 0x100,
97 };
98
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' },
114 {}
115 };
116 enum {
117 TYPE_DEVICES,
118 TYPE_SUBSYSTEMS,
119 } device_type = TYPE_DEVICES;
120 const char *action = "change";
121 _cleanup_udev_enumerate_unref_ struct udev_enumerate *udev_enumerate = NULL;
122 int c, r;
123
124 udev_enumerate = udev_enumerate_new(udev);
125 if (udev_enumerate == NULL)
126 return 1;
127
128 while ((c = getopt_long(argc, argv, "vno:t:c:s:S:a:A:p:g:y:b:h", options, NULL)) >= 0) {
129 const char *key;
130 const char *val;
131 char buf[UTIL_PATH_SIZE];
132
133 switch (c) {
134 case 'v':
135 verbose = 1;
136 break;
137 case 'n':
138 dry_run = 1;
139 break;
140 case 't':
141 if (streq(optarg, "devices"))
142 device_type = TYPE_DEVICES;
143 else if (streq(optarg, "subsystems"))
144 device_type = TYPE_SUBSYSTEMS;
145 else {
146 log_error("unknown type --type=%s", optarg);
147 return 2;
148 }
149 break;
150 case 'c':
151 if (!nulstr_contains("add\0" "remove\0" "change\0", optarg)) {
152 log_error("unknown action '%s'", optarg);
153 return 2;
154 } else
155 action = optarg;
156
157 break;
158 case 's':
159 r = udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
160 if (r < 0) {
161 log_error_errno(r, "could not add subsystem match '%s': %m", optarg);
162 return 2;
163 }
164 break;
165 case 'S':
166 r = udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
167 if (r < 0) {
168 log_error_errno(r, "could not add negative subsystem match '%s': %m", optarg);
169 return 2;
170 }
171 break;
172 case 'a':
173 key = keyval(optarg, &val, buf, sizeof(buf));
174 r = udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
175 if (r < 0) {
176 log_error_errno(r, "could not add sysattr match '%s=%s': %m", key, val);
177 return 2;
178 }
179 break;
180 case 'A':
181 key = keyval(optarg, &val, buf, sizeof(buf));
182 r = udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
183 if (r < 0) {
184 log_error_errno(r, "could not add negative sysattr match '%s=%s': %m", key, val);
185 return 2;
186 }
187 break;
188 case 'p':
189 key = keyval(optarg, &val, buf, sizeof(buf));
190 r = udev_enumerate_add_match_property(udev_enumerate, key, val);
191 if (r < 0) {
192 log_error_errno(r, "could not add property match '%s=%s': %m", key, val);
193 return 2;
194 }
195 break;
196 case 'g':
197 r = udev_enumerate_add_match_tag(udev_enumerate, optarg);
198 if (r < 0) {
199 log_error_errno(r, "could not add tag match '%s': %m", optarg);
200 return 2;
201 }
202 break;
203 case 'y':
204 r = udev_enumerate_add_match_sysname(udev_enumerate, optarg);
205 if (r < 0) {
206 log_error_errno(r, "could not add sysname match '%s': %m", optarg);
207 return 2;
208 }
209 break;
210 case 'b': {
211 _cleanup_udev_device_unref_ struct udev_device *dev;
212
213 dev = find_device(udev, optarg, "/sys");
214 if (dev == NULL) {
215 log_error("unable to open the device '%s'", optarg);
216 return 2;
217 }
218
219 r = udev_enumerate_add_match_parent(udev_enumerate, dev);
220 if (r < 0) {
221 log_error_errno(r, "could not add parent match '%s': %m", optarg);
222 return 2;
223 }
224 break;
225 }
226
227 case ARG_NAME: {
228 _cleanup_udev_device_unref_ struct udev_device *dev;
229
230 dev = find_device(udev, optarg, "/dev/");
231 if (dev == NULL) {
232 log_error("unable to open the device '%s'", optarg);
233 return 2;
234 }
235
236 r = udev_enumerate_add_match_parent(udev_enumerate, dev);
237 if (r < 0) {
238 log_error_errno(r, "could not add parent match '%s': %m", optarg);
239 return 2;
240 }
241 break;
242 }
243
244 case 'h':
245 help();
246 return 0;
247 case '?':
248 return 1;
249 default:
250 assert_not_reached("Unknown option");
251 }
252 }
253
254 for (; optind < argc; optind++) {
255 _cleanup_udev_device_unref_ struct udev_device *dev;
256
257 dev = find_device(udev, argv[optind], NULL);
258 if (dev == NULL) {
259 log_error("unable to open the device '%s'", argv[optind]);
260 return 2;
261 }
262
263 r = udev_enumerate_add_match_parent(udev_enumerate, dev);
264 if (r < 0) {
265 log_error_errno(r, "could not add tag match '%s': %m", optarg);
266 return 2;
267 }
268 }
269
270 switch (device_type) {
271 case TYPE_SUBSYSTEMS:
272 udev_enumerate_scan_subsystems(udev_enumerate);
273 exec_list(udev_enumerate, action);
274 return 0;
275 case TYPE_DEVICES:
276 udev_enumerate_scan_devices(udev_enumerate);
277 exec_list(udev_enumerate, action);
278 return 0;
279 default:
280 assert_not_reached("device_type");
281 }
282 }
283
284 const struct udevadm_cmd udevadm_trigger = {
285 .name = "trigger",
286 .cmd = adm_trigger,
287 .help = "Request events from the kernel",
288 };