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