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