]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/udev/udevadm-trigger.c
move imported udev into place
[thirdparty/systemd.git] / src / udev / udevadm-trigger.c
1 /*
2 * Copyright (C) 2008-2009 Kay Sievers <kay.sievers@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 <syslog.h>
28 #include <fnmatch.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <sys/un.h>
33
34 #include "udev.h"
35
36 static int verbose;
37 static int dry_run;
38
39 static void exec_list(struct udev_enumerate *udev_enumerate, const char *action)
40 {
41 struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
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 util_strscpyl(filename, sizeof(filename), udev_list_entry_get_name(entry), "/uevent", NULL);
53 fd = open(filename, O_WRONLY);
54 if (fd < 0) {
55 dbg(udev, "error on opening %s: %m\n", filename);
56 continue;
57 }
58 if (write(fd, action, strlen(action)) < 0)
59 info(udev, "error writing '%s' to '%s': %m\n", action, filename);
60 close(fd);
61 }
62 }
63
64 static const char *keyval(const char *str, const char **val, char *buf, size_t size)
65 {
66 char *pos;
67
68 util_strscpy(buf, size,str);
69 pos = strchr(buf, '=');
70 if (pos != NULL) {
71 pos[0] = 0;
72 pos++;
73 }
74 *val = pos;
75 return buf;
76 }
77
78 static int adm_trigger(struct udev *udev, int argc, char *argv[])
79 {
80 static const struct option options[] = {
81 { "verbose", no_argument, NULL, 'v' },
82 { "dry-run", no_argument, NULL, 'n' },
83 { "type", required_argument, NULL, 't' },
84 { "action", required_argument, NULL, 'c' },
85 { "subsystem-match", required_argument, NULL, 's' },
86 { "subsystem-nomatch", required_argument, NULL, 'S' },
87 { "attr-match", required_argument, NULL, 'a' },
88 { "attr-nomatch", required_argument, NULL, 'A' },
89 { "property-match", required_argument, NULL, 'p' },
90 { "tag-match", required_argument, NULL, 'g' },
91 { "sysname-match", required_argument, NULL, 'y' },
92 { "parent-match", required_argument, NULL, 'b' },
93 { "help", no_argument, NULL, 'h' },
94 {}
95 };
96 enum {
97 TYPE_DEVICES,
98 TYPE_SUBSYSTEMS,
99 } device_type = TYPE_DEVICES;
100 const char *action = "change";
101 struct udev_enumerate *udev_enumerate;
102 int rc = 0;
103
104 dbg(udev, "version %s\n", VERSION);
105 udev_enumerate = udev_enumerate_new(udev);
106 if (udev_enumerate == NULL) {
107 rc = 1;
108 goto exit;
109 }
110
111 for (;;) {
112 int option;
113 const char *key;
114 const char *val;
115 char buf[UTIL_PATH_SIZE];
116
117 option = getopt_long(argc, argv, "vng:o:t:hc:p:s:S:a:A:y:b:", options, NULL);
118 if (option == -1)
119 break;
120
121 switch (option) {
122 case 'v':
123 verbose = 1;
124 break;
125 case 'n':
126 dry_run = 1;
127 break;
128 case 't':
129 if (strcmp(optarg, "devices") == 0) {
130 device_type = TYPE_DEVICES;
131 } else if (strcmp(optarg, "subsystems") == 0) {
132 device_type = TYPE_SUBSYSTEMS;
133 } else {
134 err(udev, "unknown type --type=%s\n", optarg);
135 rc = 2;
136 goto exit;
137 }
138 break;
139 case 'c':
140 action = optarg;
141 break;
142 case 's':
143 udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
144 break;
145 case 'S':
146 udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
147 break;
148 case 'a':
149 key = keyval(optarg, &val, buf, sizeof(buf));
150 udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
151 break;
152 case 'A':
153 key = keyval(optarg, &val, buf, sizeof(buf));
154 udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
155 break;
156 case 'p':
157 key = keyval(optarg, &val, buf, sizeof(buf));
158 udev_enumerate_add_match_property(udev_enumerate, key, val);
159 break;
160 case 'g':
161 udev_enumerate_add_match_tag(udev_enumerate, optarg);
162 break;
163 case 'y':
164 udev_enumerate_add_match_sysname(udev_enumerate, optarg);
165 break;
166 case 'b': {
167 char path[UTIL_PATH_SIZE];
168 struct udev_device *dev;
169
170 /* add sys dir if needed */
171 if (strncmp(optarg, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0)
172 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev), optarg, NULL);
173 else
174 util_strscpy(path, sizeof(path), optarg);
175 util_remove_trailing_chars(path, '/');
176 dev = udev_device_new_from_syspath(udev, path);
177 if (dev == NULL) {
178 err(udev, "unable to open the device '%s'\n", optarg);
179 rc = 2;
180 goto exit;
181 }
182 udev_enumerate_add_match_parent(udev_enumerate, dev);
183 /* drop reference immediately, enumerate pins the device as long as needed */
184 udev_device_unref(dev);
185 break;
186 }
187 case 'h':
188 printf("Usage: udevadm trigger OPTIONS\n"
189 " --verbose print the list of devices while running\n"
190 " --dry-run do not actually trigger the events\n"
191 " --type= type of events to trigger\n"
192 " devices sys devices (default)\n"
193 " subsystems sys subsystems and drivers\n"
194 " --action=<action> event action value, default is \"change\"\n"
195 " --subsystem-match=<subsystem> trigger devices from a matching subsystem\n"
196 " --subsystem-nomatch=<subsystem> exclude devices from a matching subsystem\n"
197 " --attr-match=<file[=<value>]> trigger devices with a matching attribute\n"
198 " --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
199 " --property-match=<key>=<value> trigger devices with a matching property\n"
200 " --tag-match=<key>=<value> trigger devices with a matching property\n"
201 " --sysname-match=<name> trigger devices with a matching name\n"
202 " --parent-match=<name> trigger devices with that parent device\n"
203 " --help\n\n");
204 goto exit;
205 default:
206 rc = 1;
207 goto exit;
208 }
209 }
210
211 switch (device_type) {
212 case TYPE_SUBSYSTEMS:
213 udev_enumerate_scan_subsystems(udev_enumerate);
214 exec_list(udev_enumerate, action);
215 goto exit;
216 case TYPE_DEVICES:
217 udev_enumerate_scan_devices(udev_enumerate);
218 exec_list(udev_enumerate, action);
219 goto exit;
220 default:
221 goto exit;
222 }
223 exit:
224 udev_enumerate_unref(udev_enumerate);
225 return rc;
226 }
227
228 const struct udevadm_cmd udevadm_trigger = {
229 .name = "trigger",
230 .cmd = adm_trigger,
231 .help = "request events from the kernel",
232 };