chiark / gitweb /
test: fix some tests when running inside a container
[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 <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
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
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
75 static void help(void) {
76         printf("Usage: udevadm trigger OPTIONS\n"
77                "  -v,--verbose                       print the list of devices while running\n"
78                "  -n,--dry-run                       do not actually trigger the events\n"
79                "  -t,--type=                         type of events to trigger\n"
80                "          devices                       sys devices (default)\n"
81                "          subsystems                    sys subsystems and drivers\n"
82                "  -c,--action=<action>               event action value, default is \"change\"\n"
83                "  -s,--subsystem-match=<subsystem>   trigger devices from a matching subsystem\n"
84                "  -S,--subsystem-nomatch=<subsystem> exclude devices from a matching subsystem\n"
85                "  -a,--attr-match=<file[=<value>]>   trigger devices with a matching attribute\n"
86                "  -A,--attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
87                "  -p,--property-match=<key>=<value>  trigger devices with a matching property\n"
88                "  -g,--tag-match=<key>=<value>       trigger devices with a matching property\n"
89                "  -y,--sysname-match=<name>          trigger devices with this /sys path\n"
90                "     --name-match=<name>             trigger devices with this /dev 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         enum {
97                 ARG_NAME = 0x100,
98         };
99
100         static const struct option options[] = {
101                 { "verbose",           no_argument,       NULL, 'v'      },
102                 { "dry-run",           no_argument,       NULL, 'n'      },
103                 { "type",              required_argument, NULL, 't'      },
104                 { "action",            required_argument, NULL, 'c'      },
105                 { "subsystem-match",   required_argument, NULL, 's'      },
106                 { "subsystem-nomatch", required_argument, NULL, 'S'      },
107                 { "attr-match",        required_argument, NULL, 'a'      },
108                 { "attr-nomatch",      required_argument, NULL, 'A'      },
109                 { "property-match",    required_argument, NULL, 'p'      },
110                 { "tag-match",         required_argument, NULL, 'g'      },
111                 { "sysname-match",     required_argument, NULL, 'y'      },
112                 { "name-match",        required_argument, NULL, ARG_NAME },
113                 { "parent-match",      required_argument, NULL, 'b'      },
114                 { "help",              no_argument,       NULL, 'h'      },
115                 {}
116         };
117         enum {
118                 TYPE_DEVICES,
119                 TYPE_SUBSYSTEMS,
120         } device_type = TYPE_DEVICES;
121         const char *action = "change";
122         _cleanup_udev_enumerate_unref_ struct udev_enumerate *udev_enumerate = NULL;
123         int c;
124
125         udev_enumerate = udev_enumerate_new(udev);
126         if (udev_enumerate == NULL)
127                 return 1;
128
129         while ((c = getopt_long(argc, argv, "vno:t:c:s:S:a:A:p:g:y:b:h", options, NULL)) >= 0) {
130                 const char *key;
131                 const char *val;
132                 char buf[UTIL_PATH_SIZE];
133
134                 switch (c) {
135                 case 'v':
136                         verbose = 1;
137                         break;
138                 case 'n':
139                         dry_run = 1;
140                         break;
141                 case 't':
142                         if (streq(optarg, "devices"))
143                                 device_type = TYPE_DEVICES;
144                         else if (streq(optarg, "subsystems"))
145                                 device_type = TYPE_SUBSYSTEMS;
146                         else {
147                                 log_error("unknown type --type=%s", optarg);
148                                 return 2;
149                         }
150                         break;
151                 case 'c':
152                         if (!nulstr_contains("add\0" "remove\0" "change\0", optarg)) {
153                                 log_error("unknown action '%s'", optarg);
154                                 return 2;
155                         } else
156                                 action = optarg;
157
158                         break;
159                 case 's':
160                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
161                         break;
162                 case 'S':
163                         udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
164                         break;
165                 case 'a':
166                         key = keyval(optarg, &val, buf, sizeof(buf));
167                         udev_enumerate_add_match_sysattr(udev_enumerate, key, val);
168                         break;
169                 case 'A':
170                         key = keyval(optarg, &val, buf, sizeof(buf));
171                         udev_enumerate_add_nomatch_sysattr(udev_enumerate, key, val);
172                         break;
173                 case 'p':
174                         key = keyval(optarg, &val, buf, sizeof(buf));
175                         udev_enumerate_add_match_property(udev_enumerate, key, val);
176                         break;
177                 case 'g':
178                         udev_enumerate_add_match_tag(udev_enumerate, optarg);
179                         break;
180                 case 'y':
181                         udev_enumerate_add_match_sysname(udev_enumerate, optarg);
182                         break;
183                 case 'b': {
184                         _cleanup_udev_device_unref_ struct udev_device *dev;
185
186                         dev = find_device(udev, optarg, "/sys");
187                         if (dev == NULL) {
188                                 log_error("unable to open the device '%s'", optarg);
189                                 return 2;
190                         }
191
192                         udev_enumerate_add_match_parent(udev_enumerate, dev);
193                         break;
194                 }
195
196                 case ARG_NAME: {
197                         _cleanup_udev_device_unref_ struct udev_device *dev;
198
199                         dev = find_device(udev, optarg, "/dev/");
200                         if (dev == NULL) {
201                                 log_error("unable to open the device '%s'", optarg);
202                                 return 2;
203                         }
204
205                         udev_enumerate_add_match_parent(udev_enumerate, dev);
206                         break;
207                 }
208
209                 case 'h':
210                         help();
211                         return 0;
212                 case '?':
213                         return 1;
214                 default:
215                         assert_not_reached("Unknown option");
216                 }
217         }
218
219         for (; optind < argc; optind++) {
220                 _cleanup_udev_device_unref_ struct udev_device *dev;
221
222                 dev = find_device(udev, argv[optind], NULL);
223                 if (dev == NULL) {
224                         log_error("unable to open the device '%s'", argv[optind]);
225                         return 2;
226                 }
227
228                 udev_enumerate_add_match_parent(udev_enumerate, dev);
229         }
230
231         switch (device_type) {
232         case TYPE_SUBSYSTEMS:
233                 udev_enumerate_scan_subsystems(udev_enumerate);
234                 exec_list(udev_enumerate, action);
235                 return 0;
236         case TYPE_DEVICES:
237                 udev_enumerate_scan_devices(udev_enumerate);
238                 exec_list(udev_enumerate, action);
239                 return 0;
240         default:
241                 assert_not_reached("device_type");
242         }
243 }
244
245 const struct udevadm_cmd udevadm_trigger = {
246         .name = "trigger",
247         .cmd = adm_trigger,
248         .help = "request events from the kernel",
249 };