chiark / gitweb /
udevtrigger: trigger devices sorted by their dependency
[elogind.git] / udevtrigger.c
1 /*
2  * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
3  * Copyright (C) 2006 Hannes Reinecke <hare@suse.de>
4  *
5  *      This program is free software; you can redistribute it and/or modify it
6  *      under the terms of the GNU General Public License as published by the
7  *      Free Software Foundation version 2 of the License.
8  * 
9  *      This program is distributed in the hope that it will be useful, but
10  *      WITHOUT ANY WARRANTY; without even the implied warranty of
11  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  *      General Public License for more details.
13  * 
14  *      You should have received a copy of the GNU General Public License along
15  *      with this program; if not, write to the Free Software Foundation, Inc.,
16  *      51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  *
18  */
19
20 #include <stdlib.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <getopt.h>
26 #include <errno.h>
27 #include <dirent.h>
28 #include <fcntl.h>
29 #include <syslog.h>
30 #include <fnmatch.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33
34 #include "udev.h"
35 #include "udevd.h"
36
37 static int verbose;
38 static int dry_run;
39 LIST_HEAD(device_list);
40 LIST_HEAD(filter_subsystem_match_list);
41 LIST_HEAD(filter_subsystem_nomatch_list);
42 LIST_HEAD(filter_attr_match_list);
43 LIST_HEAD(filter_attr_nomatch_list);
44
45 #ifdef USE_LOG
46 void log_message(int priority, const char *format, ...)
47 {
48         va_list args;
49
50         if (priority > udev_log_priority)
51                 return;
52
53         va_start(args, format);
54         vsyslog(priority, format, args);
55         va_end(args);
56 }
57 #endif
58
59 /* devices that should run last cause of their dependencies */
60 static int delay_device(const char *devpath)
61 {
62         static const char *delay_device_list[] = {
63                 "*/md*",
64                 "*/dm-*",
65                 NULL
66         };
67         int i;
68
69         for (i = 0; delay_device_list[i] != NULL; i++)
70                 if (fnmatch(delay_device_list[i], devpath, 0) == 0)
71                         return 1;
72         return 0;
73 }
74
75 static int device_list_insert(const char *path)
76 {
77         char filename[PATH_SIZE];
78         char devpath[PATH_SIZE];
79         struct stat statbuf;
80
81         dbg("add '%s'" , path);
82
83         /* we only have a device, if we have an uevent file */
84         strlcpy(filename, path, sizeof(filename));
85         strlcat(filename, "/uevent", sizeof(filename));
86         if (stat(filename, &statbuf) < 0)
87                 return -1;
88         if (!(statbuf.st_mode & S_IWUSR))
89                 return -1;
90
91         strlcpy(devpath, &path[strlen(sysfs_path)], sizeof(devpath));
92
93         /* resolve possible link to real target */
94         if (lstat(path, &statbuf) < 0)
95                 return -1;
96         if (S_ISLNK(statbuf.st_mode))
97                 if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
98                         return -1;
99
100         name_list_add(&device_list, devpath, 1);
101         return 0;
102 }
103
104 static void trigger_uevent(const char *devpath)
105 {
106         char filename[PATH_SIZE];
107         int fd;
108
109         strlcpy(filename, sysfs_path, sizeof(filename));
110         strlcat(filename, devpath, sizeof(filename));
111         strlcat(filename, "/uevent", sizeof(filename));
112
113         if (verbose)
114                 printf("%s\n", devpath);
115
116         if (dry_run)
117                 return;
118
119         fd = open(filename, O_WRONLY);
120         if (fd < 0) {
121                 dbg("error on opening %s: %s\n", filename, strerror(errno));
122                 return;
123         }
124
125         if (write(fd, "add", 3) < 0)
126                 info("error on triggering %s: %s\n", filename, strerror(errno));
127
128         close(fd);
129 }
130
131 static void exec_list(void)
132 {
133         struct name_entry *loop_device;
134         struct name_entry *tmp_device;
135
136         list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
137                 if (delay_device(loop_device->name))
138                         continue;
139
140                 trigger_uevent(loop_device->name);
141                 list_del(&loop_device->node);
142                 free(loop_device);
143         }
144
145         /* trigger remaining delayed devices */
146         list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
147                 trigger_uevent(loop_device->name);
148                 list_del(&loop_device->node);
149                 free(loop_device);
150         }
151 }
152
153 static int subsystem_filtered(const char *subsystem)
154 {
155         struct name_entry *loop_name;
156
157         /* skip devices matching the listed subsystems */
158         list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
159                 if (fnmatch(loop_name->name, subsystem, 0) == 0)
160                         return 1;
161
162         /* skip devices not matching the listed subsystems */
163         if (!list_empty(&filter_subsystem_match_list)) {
164                 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
165                         if (fnmatch(loop_name->name, subsystem, 0) == 0)
166                                 return 0;
167                 return 1;
168         }
169
170         return 0;
171 }
172
173 static int attr_match(const char *path, const char *attr_value)
174 {
175         char attr[NAME_SIZE];
176         char file[PATH_SIZE];
177         char *match_value;
178
179         strlcpy(attr, attr_value, sizeof(attr));
180
181         /* separate attr and match value */
182         match_value = strchr(attr, '=');
183         if (match_value != NULL) {
184                 match_value[0] = '\0';
185                 match_value = &match_value[1];
186         }
187
188         strlcpy(file, path, sizeof(file));
189         strlcat(file, "/", sizeof(file));
190         strlcat(file, attr, sizeof(file));
191
192         if (match_value != NULL) {
193                 /* match file content */
194                 char value[NAME_SIZE];
195                 int fd;
196                 ssize_t size;
197
198                 fd = open(file, O_RDONLY);
199                 if (fd < 0)
200                         return 0;
201                 size = read(fd, value, sizeof(value));
202                 close(fd);
203                 if (size < 0)
204                         return 0;
205                 value[size] = '\0';
206                 remove_trailing_chars(value, '\n');
207
208                 /* match if attribute value matches */
209                 if (fnmatch(match_value, value, 0) == 0)
210                         return 1;
211         } else {
212                 /* match if attribute exists */
213                 struct stat statbuf;
214
215                 if (stat(file, &statbuf) == 0)
216                         return 1;
217         }
218         return 0;
219 }
220
221 static int attr_filtered(const char *path)
222 {
223         struct name_entry *loop_name;
224
225         /* skip devices matching the listed sysfs attributes */
226         list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
227                 if (attr_match(path, loop_name->name))
228                         return 1;
229
230         /* skip devices not matching the listed sysfs attributes */
231         if (!list_empty(&filter_attr_match_list)) {
232                 list_for_each_entry(loop_name, &filter_attr_match_list, node)
233                         if (attr_match(path, loop_name->name))
234                                 return 0;
235                 return 1;
236         }
237         return 0;
238 }
239
240 static void scan_bus(void)
241 {
242         char base[PATH_SIZE];
243         DIR *dir;
244         struct dirent *dent;
245
246         strlcpy(base, sysfs_path, sizeof(base));
247         strlcat(base, "/bus", sizeof(base));
248
249         dir = opendir(base);
250         if (dir != NULL) {
251                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
252                         char dirname[PATH_SIZE];
253                         DIR *dir2;
254                         struct dirent *dent2;
255
256                         if (dent->d_name[0] == '.')
257                                 continue;
258
259                         if (subsystem_filtered(dent->d_name))
260                                 continue;
261
262                         strlcpy(dirname, base, sizeof(dirname));
263                         strlcat(dirname, "/", sizeof(dirname));
264                         strlcat(dirname, dent->d_name, sizeof(dirname));
265                         strlcat(dirname, "/devices", sizeof(dirname));
266
267                         /* look for devices */
268                         dir2 = opendir(dirname);
269                         if (dir2 != NULL) {
270                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
271                                         char dirname2[PATH_SIZE];
272
273                                         if (dent2->d_name[0] == '.')
274                                                 continue;
275
276                                         strlcpy(dirname2, dirname, sizeof(dirname2));
277                                         strlcat(dirname2, "/", sizeof(dirname2));
278                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
279                                         if (attr_filtered(dirname2))
280                                                 continue;
281                                         device_list_insert(dirname2);
282                                 }
283                                 closedir(dir2);
284                         }
285                 }
286                 closedir(dir);
287         }
288 }
289
290 static void scan_block(void)
291 {
292         char base[PATH_SIZE];
293         DIR *dir;
294         struct dirent *dent;
295         struct stat statbuf;
296
297         /* skip if "block" is already a "class" */
298         strlcpy(base, sysfs_path, sizeof(base));
299         strlcat(base, "/class/block", sizeof(base));
300         if (stat(base, &statbuf) == 0)
301                 return;
302
303         if (subsystem_filtered("block"))
304                 return;
305
306         strlcpy(base, sysfs_path, sizeof(base));
307         strlcat(base, "/block", sizeof(base));
308
309         dir = opendir(base);
310         if (dir != NULL) {
311                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
312                         char dirname[PATH_SIZE];
313                         DIR *dir2;
314                         struct dirent *dent2;
315
316                         if (dent->d_name[0] == '.')
317                                 continue;
318
319                         strlcpy(dirname, base, sizeof(dirname));
320                         strlcat(dirname, "/", sizeof(dirname));
321                         strlcat(dirname, dent->d_name, sizeof(dirname));
322                         if (attr_filtered(dirname))
323                                 continue;
324                         if (device_list_insert(dirname) != 0);
325                                 continue;
326
327                         /* look for partitions */
328                         dir2 = opendir(dirname);
329                         if (dir2 != NULL) {
330                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
331                                         char dirname2[PATH_SIZE];
332
333                                         if (dent2->d_name[0] == '.')
334                                                 continue;
335
336                                         if (!strcmp(dent2->d_name,"device"))
337                                                 continue;
338
339                                         strlcpy(dirname2, dirname, sizeof(dirname2));
340                                         strlcat(dirname2, "/", sizeof(dirname2));
341                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
342                                         if (attr_filtered(dirname2))
343                                                 continue;
344                                         device_list_insert(dirname2);
345                                 }
346                                 closedir(dir2);
347                         }
348                 }
349                 closedir(dir);
350         }
351 }
352
353 static void scan_class(void)
354 {
355         char base[PATH_SIZE];
356         DIR *dir;
357         struct dirent *dent;
358
359         strlcpy(base, sysfs_path, sizeof(base));
360         strlcat(base, "/class", sizeof(base));
361
362         dir = opendir(base);
363         if (dir != NULL) {
364                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
365                         char dirname[PATH_SIZE];
366                         DIR *dir2;
367                         struct dirent *dent2;
368
369                         if (dent->d_name[0] == '.')
370                                 continue;
371
372                         if (subsystem_filtered(dent->d_name))
373                                 continue;
374
375                         strlcpy(dirname, base, sizeof(dirname));
376                         strlcat(dirname, "/", sizeof(dirname));
377                         strlcat(dirname, dent->d_name, sizeof(dirname));
378                         dir2 = opendir(dirname);
379                         if (dir2 != NULL) {
380                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
381                                         char dirname2[PATH_SIZE];
382
383                                         if (dent2->d_name[0] == '.')
384                                                 continue;
385
386                                         if (!strcmp(dent2->d_name, "device"))
387                                                 continue;
388
389                                         strlcpy(dirname2, dirname, sizeof(dirname2));
390                                         strlcat(dirname2, "/", sizeof(dirname2));
391                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
392                                         if (attr_filtered(dirname2))
393                                                 continue;
394                                         device_list_insert(dirname2);
395                                 }
396                                 closedir(dir2);
397                         }
398                 }
399                 closedir(dir);
400         }
401 }
402
403 static void scan_failed(void)
404 {
405         char base[PATH_SIZE];
406         DIR *dir;
407         struct dirent *dent;
408
409         strlcpy(base, udev_root, sizeof(base));
410         strlcat(base, "/" EVENT_FAILED_DIR, sizeof(base));
411
412         dir = opendir(base);
413         if (dir != NULL) {
414                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
415                         char device[PATH_SIZE];
416                         size_t start, end, i;
417
418                         if (dent->d_name[0] == '.')
419                                 continue;
420
421                         strlcpy(device, sysfs_path, sizeof(device));
422                         start = strlcat(device, "/", sizeof(device));
423                         end = strlcat(device, dent->d_name, sizeof(device));
424                         if (end > sizeof(device))
425                                 end = sizeof(device);
426
427                         /* replace PATH_TO_NAME_CHAR with '/' */
428                         for (i = start; i < end; i++)
429                                 if (device[i] == PATH_TO_NAME_CHAR)
430                                         device[i] = '/';
431
432                         device_list_insert(device);
433                 }
434                 closedir(dir);
435         }
436 }
437
438 int main(int argc, char *argv[], char *envp[])
439 {
440         int failed = 0;
441         int option;
442         static const struct option options[] = {
443                 { "verbose", 0, NULL, 'v' },
444                 { "dry-run", 0, NULL, 'n' },
445                 { "retry-failed", 0, NULL, 'F' },
446                 { "help", 0, NULL, 'h' },
447                 { "subsystem-match", 1, NULL, 's' },
448                 { "subsystem-nomatch", 1, NULL, 'S' },
449                 { "attr-match", 1, NULL, 'a' },
450                 { "attr-nomatch", 1, NULL, 'A' },
451                 {}
452         };
453
454         logging_init("udevtrigger");
455         udev_config_init();
456         dbg("version %s", UDEV_VERSION);
457         sysfs_init();
458
459         while (1) {
460                 option = getopt_long(argc, argv, "vnFhs:S:a:A:", options, NULL);
461                 if (option == -1)
462                         break;
463
464                 switch (option) {
465                 case 'v':
466                         verbose = 1;
467                         break;
468                 case 'n':
469                         dry_run = 1;
470                         break;
471                 case 'F':
472                         failed = 1;
473                         break;
474                 case 's':
475                         name_list_add(&filter_subsystem_match_list, optarg, 0);
476                         break;
477                 case 'S':
478                         name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
479                         break;
480                 case 'a':
481                         name_list_add(&filter_attr_match_list, optarg, 0);
482                         break;
483                 case 'A':
484                         name_list_add(&filter_attr_nomatch_list, optarg, 0);
485                         break;
486                 case 'h':
487                         printf("Usage: udevtrigger OPTIONS\n"
488                                "  --verbose                       print the list of devices while running\n"
489                                "  --dry-run                       do not actually trigger the events\n"
490                                "  --retry-failed                  trigger only the events which have been\n"
491                                "                                  marked as failed during a previous run\n"
492                                "  --subsystem-match=<subsystem>   trigger devices from a matching subystem\n"
493                                "  --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
494                                "  --attr-match=<file[=<value>]>   trigger devices with a matching sysfs\n"
495                                "                                  attribute\n"
496                                "  --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
497                                "                                  attribute\n"
498                                "  --help                          print this text\n"
499                                "\n");
500                         goto exit;
501                 default:
502                         goto exit;
503                 }
504         }
505
506         if (failed)
507                 scan_failed();
508         else {
509                 scan_bus();
510                 scan_class();
511                 scan_block();
512         }
513         exec_list();
514
515 exit:
516         name_list_cleanup(&filter_subsystem_match_list);
517         name_list_cleanup(&filter_subsystem_nomatch_list);
518         name_list_cleanup(&filter_attr_match_list);
519         name_list_cleanup(&filter_attr_nomatch_list);
520
521         sysfs_cleanup();
522         logging_close();
523         return 0;
524 }