chiark / gitweb /
udevadm,scsi_id: add short options to help strings and to the man page
[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 void help(void) {
77         printf("Usage: udevadm trigger OPTIONS\n"
78                "  -v,--verbose                       print the list of devices while running\n"
79                "  -n,--dry-run                       do not actually trigger the events\n"
80                "  -t,--type=                         type of events to trigger\n"
81                "          devices                       sys devices (default)\n"
82                "          subsystems                    sys subsystems and drivers\n"
83                "  -c,--action=<action>               event action value, default is \"change\"\n"
84                "  -s,--subsystem-match=<subsystem>   trigger devices from a matching subsystem\n"
85                "  -S,--subsystem-nomatch=<subsystem> exclude devices from a matching subsystem\n"
86                "  -a,--attr-match=<file[=<value>]>   trigger devices with a matching attribute\n"
87                "  -A,--attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
88                "  -p,--property-match=<key>=<value>  trigger devices with a matching property\n"
89                "  -g,--tag-match=<key>=<value>       trigger devices with a matching property\n"
90                "  -y,--sysname-match=<name>          trigger devices with a matching name\n"
91                "  -b,--parent-match=<name>           trigger devices with that parent device\n"
92                "  -h,--help\n\n");
93 }
94
95 static int adm_trigger(struct udev *udev, int argc, char *argv[])
96 {
97         static const struct option options[] = {
98                 { "verbose",           no_argument,       NULL, 'v' },
99                 { "dry-run",           no_argument,       NULL, 'n' },
100                 { "type",              required_argument, NULL, 't' },
101                 { "action",            required_argument, NULL, 'c' },
102                 { "subsystem-match",   required_argument, NULL, 's' },
103                 { "subsystem-nomatch", required_argument, NULL, 'S' },
104                 { "attr-match",        required_argument, NULL, 'a' },
105                 { "attr-nomatch",      required_argument, NULL, 'A' },
106                 { "property-match",    required_argument, NULL, 'p' },
107                 { "tag-match",         required_argument, NULL, 'g' },
108                 { "sysname-match",     required_argument, NULL, 'y' },
109                 { "parent-match",      required_argument, NULL, 'b' },
110                 { "help",              no_argument,       NULL, 'h' },
111                 {}
112         };
113         enum {
114                 TYPE_DEVICES,
115                 TYPE_SUBSYSTEMS,
116         } device_type = TYPE_DEVICES;
117         const char *action = "change";
118         struct udev_enumerate *udev_enumerate;
119         int rc = 0;
120         int c;
121
122         udev_enumerate = udev_enumerate_new(udev);
123         if (udev_enumerate == NULL) {
124                 rc = 1;
125                 goto exit;
126         }
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\n", optarg);
147                                 rc = 2;
148                                 goto exit;
149                         }
150                         break;
151                 case 'c':
152                         if (!nulstr_contains("add\0" "remove\0" "change\0", optarg)) {
153                                 log_error("unknown action '%s'\n", optarg);
154                                 rc = 2;
155                                 goto exit;
156                         } else {
157                                 action = optarg;
158                         }
159                         break;
160                 case 's':
161                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
162                         break;
163                 case 'S':
164                         udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
165                         break;
166                 case 'a':
167                         key = keyval(optarg, &val, buf, sizeof(buf));
168                         udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
169                         break;
170                 case 'A':
171                         key = keyval(optarg, &val, buf, sizeof(buf));
172                         udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
173                         break;
174                 case 'p':
175                         key = keyval(optarg, &val, buf, sizeof(buf));
176                         udev_enumerate_add_match_property(udev_enumerate, key, val);
177                         break;
178                 case 'g':
179                         udev_enumerate_add_match_tag(udev_enumerate, optarg);
180                         break;
181                 case 'y':
182                         udev_enumerate_add_match_sysname(udev_enumerate, optarg);
183                         break;
184                 case 'b': {
185                         char path[UTIL_PATH_SIZE];
186                         struct udev_device *dev;
187
188                         /* add sys dir if needed */
189                         if (!startswith(optarg, "/sys"))
190                                 strscpyl(path, sizeof(path), "/sys", optarg, NULL);
191                         else
192                                 strscpy(path, sizeof(path), optarg);
193                         util_remove_trailing_chars(path, '/');
194                         dev = udev_device_new_from_syspath(udev, path);
195                         if (dev == NULL) {
196                                 log_error("unable to open the device '%s'\n", optarg);
197                                 rc = 2;
198                                 goto exit;
199                         }
200                         udev_enumerate_add_match_parent(udev_enumerate, dev);
201                         /* drop reference immediately, enumerate pins the device as long as needed */
202                         udev_device_unref(dev);
203                         break;
204                 }
205                 case 'h':
206                         help();
207                         goto exit;
208                 case '?':
209                         rc = 1;
210                         goto exit;
211                 default:
212                         assert_not_reached("Unknown option");
213                 }
214         }
215
216         if (optind < argc) {
217                 fprintf(stderr, "Extraneous argument: '%s'\n", argv[optind]);
218                 return 1;
219         }
220
221         switch (device_type) {
222         case TYPE_SUBSYSTEMS:
223                 udev_enumerate_scan_subsystems(udev_enumerate);
224                 exec_list(udev_enumerate, action);
225                 goto exit;
226         case TYPE_DEVICES:
227                 udev_enumerate_scan_devices(udev_enumerate);
228                 exec_list(udev_enumerate, action);
229                 goto exit;
230         default:
231                 assert_not_reached("device_type");
232         }
233 exit:
234         udev_enumerate_unref(udev_enumerate);
235         return rc;
236 }
237
238 const struct udevadm_cmd udevadm_trigger = {
239         .name = "trigger",
240         .cmd = adm_trigger,
241         .help = "request events from the kernel",
242 };