chiark / gitweb /
udevadm: trigger: use libudev
[elogind.git] / udev / udevadm-trigger.c
1 /*
2  * Copyright (C) 2008 Kay Sievers <kayi.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_strlcpy(filename, udev_list_entry_get_name(entry), sizeof(filename));
53                 util_strlcat(filename, "/uevent", sizeof(filename));
54                 fd = open(filename, O_WRONLY);
55                 if (fd < 0) {
56                         dbg(udev, "error on opening %s: %m\n", filename);
57                         continue;
58                 }
59                 if (write(fd, action, strlen(action)) < 0)
60                         info(udev, "error writing '%s' to '%s': %m\n", action, filename);
61                 close(fd);
62         }
63 }
64
65 static int enumerate_scan_failed(struct udev_enumerate *udev_enumerate)
66 {
67         struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
68         char base[UTIL_PATH_SIZE];
69         DIR *dir;
70         struct dirent *dent;
71
72         util_strlcpy(base, udev_get_dev_path(udev), sizeof(base));
73         util_strlcat(base, "/.udev/failed", sizeof(base));
74
75         dir = opendir(base);
76         if (dir != NULL) {
77                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
78                         char syspath[UTIL_PATH_SIZE];
79                         size_t start;
80                         struct udev_device *device;
81
82                         if (dent->d_name[0] == '.')
83                                 continue;
84                         start = util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath));
85                         util_strlcat(syspath, dent->d_name, sizeof(syspath));
86                         util_path_decode(&syspath[start]);
87                         device = udev_device_new_from_syspath(udev, syspath);
88                         if (device == NULL)
89                                 continue;
90                         udev_enumerate_add_device(udev_enumerate, device);
91                         udev_device_unref(device);
92                 }
93                 closedir(dir);
94         }
95         return 0;
96 }
97
98 int udevadm_trigger(struct udev *udev, int argc, char *argv[])
99 {
100         static const struct option options[] = {
101                 { "verbose", 0, NULL, 'v' },
102                 { "dry-run", 0, NULL, 'n' },
103                 { "type", 0, NULL, 't' },
104                 { "retry-failed", 0, NULL, 'F' },
105                 { "action", 1, NULL, 'c' },
106                 { "subsystem-match", 1, NULL, 's' },
107                 { "subsystem-nomatch", 1, NULL, 'S' },
108                 { "attr-match", 1, NULL, 'a' },
109                 { "attr-nomatch", 1, NULL, 'A' },
110                 { "help", 0, NULL, 'h' },
111                 {}
112         };
113         enum {
114                 TYPE_DEVICES,
115                 TYPE_SUBSYSTEMS,
116                 TYPE_FAILED,
117         } device_type = TYPE_DEVICES;
118         const char *action = "add";
119         struct udev_enumerate *udev_enumerate;
120         int rc = 0;
121
122         dbg(udev, "version %s\n", VERSION);
123         udev_enumerate = udev_enumerate_new(udev);
124         if (udev_enumerate == NULL) {
125                 rc = 1;
126                 goto exit;
127         }
128
129         while (1) {
130                 int option;
131                 char attr[UTIL_PATH_SIZE];
132                 char *val;
133
134                 option = getopt_long(argc, argv, "vnFo:t:hce::s:S:a:A:", options, NULL);
135                 if (option == -1)
136                         break;
137
138                 switch (option) {
139                 case 'v':
140                         verbose = 1;
141                         break;
142                 case 'n':
143                         dry_run = 1;
144                         break;
145                 case 't':
146                         if (strcmp(optarg, "devices") == 0) {
147                                 device_type = TYPE_DEVICES;
148                         } else if (strcmp(optarg, "subsystems") == 0) {
149                                 device_type = TYPE_SUBSYSTEMS;
150                         } else if (strcmp(optarg, "failed") == 0) {
151                                 device_type = TYPE_FAILED;
152                         } else {
153                                 fprintf(stderr, "unknown type --type=%s\n", optarg);
154                                 err(udev, "unknown type --type=%s\n", optarg);
155                                 rc = 2;
156                                 goto exit;
157                         }
158                         break;
159                 case 'F':
160                         device_type = TYPE_FAILED;
161                         break;
162                 case 'c':
163                         action = optarg;
164                         break;
165                 case 's':
166                         udev_enumerate_add_match_subsystem(udev_enumerate, optarg);
167                         break;
168                 case 'S':
169                         udev_enumerate_add_nomatch_subsystem(udev_enumerate, optarg);
170                         break;
171                 case 'a':
172                         util_strlcpy(attr, optarg, sizeof(attr));
173                         val = strchr(attr, '=');
174                         if (val != NULL) {
175                                 val[0] = 0;
176                                 val = &val[1];
177                         }
178                         udev_enumerate_add_match_attr(udev_enumerate, attr, val);
179                         break;
180                 case 'A':
181                         util_strlcpy(attr, optarg, sizeof(attr));
182                         val = strchr(attr, '=');
183                         if (val != NULL) {
184                                 val[0] = 0;
185                                 val = &val[1];
186                         }
187                         udev_enumerate_add_nomatch_attr(udev_enumerate, attr, val);
188                         break;
189                 case 'h':
190                         printf("Usage: udevadm trigger OPTIONS\n"
191                                "  --verbose                       print the list of devices while running\n"
192                                "  --dry-run                       do not actually trigger the events\n"
193                                "  --type=                         type of events to trigger\n"
194                                "      devices                       sys devices\n"
195                                "      subsystems                    sys subsystems and drivers\n"
196                                "      failed                        trigger only the events which have been\n"
197                                "                                    marked as failed during a previous run\n"
198                                "  --subsystem-match=<subsystem>   trigger devices from a matching subystem\n"
199                                "  --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
200                                "  --attr-match=<file[=<value>]>   trigger devices with a matching attribute\n"
201                                "  --attr-nomatch=<file[=<value>]> exclude devices with a matching attribute\n"
202                                "  --help                          print this text\n"
203                                "\n");
204                         goto exit;
205                 default:
206                         goto exit;
207                 }
208         }
209
210         switch (device_type) {
211         case TYPE_FAILED:
212                 enumerate_scan_failed(udev_enumerate);
213                 exec_list(udev_enumerate, action);
214                 goto exit;
215         case TYPE_SUBSYSTEMS:
216                 udev_enumerate_scan_subsystems(udev_enumerate);
217                 exec_list(udev_enumerate, action);
218                 goto exit;
219         case TYPE_DEVICES:
220                 udev_enumerate_scan_devices(udev_enumerate);
221                 exec_list(udev_enumerate, action);
222                 goto exit;
223         default:
224                 goto exit;
225         }
226 exit:
227         udev_enumerate_unref(udev_enumerate);
228         return rc;
229 }