1 /*
2 * Copyright (C) 2008-2009 Kay Sievers <kay@vrfy.org>
3 *
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.
8 *
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.
13 *
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/>.
16 */
17
18 #include <stdlib.h>
19 #include <stddef.h>
20 #include <string.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <getopt.h>
24 #include <errno.h>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <fnmatch.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <sys/un.h>
32
33 #include "udev.h"
34 #include "udev-util.h"
35 #include "udevadm-util.h"
36 #include "util.h"
37
38 static int verbose;
39 static int dry_run;
40
exec_list(struct udev_enumerate * udev_enumerate,const char * action)41 static void exec_list(struct udev_enumerate *udev_enumerate, const char *action) {
42 struct udev_list_entry *entry;
43
44 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(udev_enumerate)) {
45 char filename[UTIL_PATH_SIZE];
46 int fd;
47
48 if (verbose)
49 printf("%s\n", udev_list_entry_get_name(entry));
50 if (dry_run)
51 continue;
52 strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
53 fd = open(filename, O_WRONLY|O_CLOEXEC);
54 if (fd < 0)
55 continue;
56 if (write(fd, action, strlen(action)) < 0)
57 log_debug_errno(errno, "error writing '%s' to '%s': %m", action, filename);
58 close(fd);
59 }
60 }
61
keyval(const char * str,const char ** val,char * buf,size_t size)62 static const char *keyval(const char *str, const char **val, char *buf, size_t size) {
63 char *pos;
64
65 strscpy(buf, size,str);
66 pos = strchr(buf, '=');
67 if (pos != NULL) {
68 pos[0] = 0;
69 pos++;
70 }
71 *val = pos;
72 return buf;
73 }
74
help(void)75 static void help(void) {
76 printf("%s trigger OPTIONS\n\n"
77 "Request events from the kernel.\n\n"
78 " -h --help Show this help\n"
79 " --version Show package version\n"
80 " -v --verbose Print the list of devices while running\n"
81 " -n --dry-run Do not actually trigger the events\n"
82 " -t --type= Type of events to trigger\n"
83 " devices sysfs devices (default)\n"
84 " subsystems sysfs subsystems and drivers\n"
85 " -c --action=ACTION Event action value, default is \"change\"\n"
86 " -s --subsystem-match=SUBSYSTEM Trigger devices from a matching subsystem\n"
87 " -S --subsystem-nomatch=SUBSYSTEM Exclude devices from a matching subsystem\n"
88 " -a --attr-match=FILE[=VALUE] Trigger devices with a matching attribute\n"
89 " -A --attr-nomatch=FILE[=VALUE] Exclude devices with a matching attribute\n"
90 " -p --property-match=KEY=VALUE Trigger devices with a matching property\n"
91 " -g --tag-match=KEY=VALUE Trigger devices with a matching property\n"
92 " -y --sysname-match=NAME Trigger devices with this /sys path\n"
93 " --name-match=NAME Trigger devices with this /dev name\n"
94 " -b --parent-match=NAME Trigger devices with that parent device\n"
95 , program_invocation_short_name);
96 }
97
adm_trigger(struct udev * udev,int argc,char * argv[])98 static int adm_trigger(struct udev *udev, int argc, char *argv[]) {
99 enum {
100 ARG_NAME = 0x100,
101 };
102
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 { "help", no_argument, NULL, 'h' },
118 {}
119 };
120 enum {
121 TYPE_DEVICES,
122 TYPE_SUBSYSTEMS,
123 } device_type = TYPE_DEVICES;
124 const char *action = "change";
125 _cleanup_udev_enumerate_unref_ struct udev_enumerate *udev_enumerate = NULL;
126 int c;
127
128 udev_enumerate = udev_enumerate_new(udev);
129 if (udev_enumerate == NULL)
130 return 1;
131
132 while ((c = getopt_long(argc, argv, "vno:t:c:s:S:a:A:p:g:y:b:h", options, NULL)) >= 0) {
133 const char *key;
134 const char *val;
135 char buf[UTIL_PATH_SIZE];
136
137 switch (c) {
138 case 'v':
139 verbose = 1;
140 break;
141 case 'n':
142 dry_run = 1;
143 break;
144 case 't':
145 if (streq(optarg, "devices"))
146 device_type = TYPE_DEVICES;
147 else if (streq(optarg, "subsystems"))
148 device_type = TYPE_SUBSYSTEMS;
149 else {
150 log_error("unknown type --type=%s", optarg);
151 return 2;
152 }
153 break;
154 case 'c':
155 if (!nulstr_contains("add\0" "remove\0" "change\0", optarg)) {
156 log_error("unknown action '%s'", optarg);
157 return 2;
158 } else
159 action = optarg;
160
161 break;
162 case 's':
163 udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
164 break;
165 case 'S':
166 udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
167 break;
168 case 'a':
169 key = keyval(optarg, &val, buf, sizeof(buf));
170 udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
171 break;
172 case 'A':
173 key = keyval(optarg, &val, buf, sizeof(buf));
174 udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
175 break;
176 case 'p':
177 key = keyval(optarg, &val, buf, sizeof(buf));
178 udev_enumerate_add_match_property(udev_enumerate, key, val);
179 break;
180 case 'g':
181 udev_enumerate_add_match_tag(udev_enumerate, optarg);
182 break;
183 case 'y':
184 udev_enumerate_add_match_sysname(udev_enumerate, optarg);
185 break;
186 case 'b': {
187 _cleanup_udev_device_unref_ struct udev_device *dev;
188
189 dev = find_device(udev, optarg, "/sys");
190 if (dev == NULL) {
191 log_error("unable to open the device '%s'", optarg);
192 return 2;
193 }
194
195 udev_enumerate_add_match_parent(udev_enumerate, dev);
196 break;
197 }
198
199 case ARG_NAME: {
200 _cleanup_udev_device_unref_ struct udev_device *dev;
201
202 dev = find_device(udev, optarg, "/dev/");
203 if (dev == NULL) {
204 log_error("unable to open the device '%s'", optarg);
205 return 2;
206 }
207
208 udev_enumerate_add_match_parent(udev_enumerate, dev);
209 break;
210 }
211
212 case 'h':
213 help();
214 return 0;
215 case '?':
216 return 1;
217 default:
218 assert_not_reached("Unknown option");
219 }
220 }
221
222 for (; optind < argc; optind++) {
223 _cleanup_udev_device_unref_ struct udev_device *dev;
224
225 dev = find_device(udev, argv[optind], NULL);
226 if (dev == NULL) {
227 log_error("unable to open the device '%s'", argv[optind]);
228 return 2;
229 }
230
231 udev_enumerate_add_match_parent(udev_enumerate, dev);
232 }
233
234 switch (device_type) {
235 case TYPE_SUBSYSTEMS:
236 udev_enumerate_scan_subsystems(udev_enumerate);
237 exec_list(udev_enumerate, action);
238 return 0;
239 case TYPE_DEVICES:
240 udev_enumerate_scan_devices(udev_enumerate);
241 exec_list(udev_enumerate, action);
242 return 0;
243 default:
244 assert_not_reached("device_type");
245 }
246 }
247
248 const struct udevadm_cmd udevadm_trigger = {
249 .name = "trigger",
250 .cmd = adm_trigger,
251 .help = "Request events from the kernel",
252 };
253