chiark / gitweb /
rules: update Fedora
[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, const char *action)
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", filename, strerror(errno));
122                 return;
123         }
124
125         if (write(fd, action, strlen(action)) < 0)
126                 info("error writing '%s' to '%s': %s", action, filename, strerror(errno));
127
128         close(fd);
129 }
130
131 static void exec_list(const char *action)
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, action);
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, action);
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 enum scan_type {
241         SCAN_DEVICES,
242         SCAN_SUBSYSTEM,
243 };
244
245 static void scan_subsystem(const char *subsys, enum scan_type scan)
246 {
247         char base[PATH_SIZE];
248         DIR *dir;
249         struct dirent *dent;
250         const char *subdir;
251
252         if (scan == SCAN_DEVICES)
253                 subdir = "/devices";
254         else if (scan == SCAN_SUBSYSTEM)
255                 subdir = "/drivers";
256         else
257                 return;
258
259         strlcpy(base, sysfs_path, sizeof(base));
260         strlcat(base, "/", sizeof(base));
261         strlcat(base, subsys, sizeof(base));
262
263         dir = opendir(base);
264         if (dir != NULL) {
265                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
266                         char dirname[PATH_SIZE];
267                         DIR *dir2;
268                         struct dirent *dent2;
269
270                         if (dent->d_name[0] == '.')
271                                 continue;
272
273                         if (scan == SCAN_DEVICES)
274                                 if (subsystem_filtered(dent->d_name))
275                                         continue;
276
277                         strlcpy(dirname, base, sizeof(dirname));
278                         strlcat(dirname, "/", sizeof(dirname));
279                         strlcat(dirname, dent->d_name, sizeof(dirname));
280
281                         if (scan == SCAN_SUBSYSTEM) {
282                                 if (!subsystem_filtered("subsystem"))
283                                         device_list_insert(dirname);
284                                 if (subsystem_filtered("drivers"))
285                                         continue;
286                         }
287
288                         strlcat(dirname, subdir, sizeof(dirname));
289
290                         /* look for devices/drivers */
291                         dir2 = opendir(dirname);
292                         if (dir2 != NULL) {
293                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
294                                         char dirname2[PATH_SIZE];
295
296                                         if (dent2->d_name[0] == '.')
297                                                 continue;
298
299                                         strlcpy(dirname2, dirname, sizeof(dirname2));
300                                         strlcat(dirname2, "/", sizeof(dirname2));
301                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
302                                         if (attr_filtered(dirname2))
303                                                 continue;
304                                         device_list_insert(dirname2);
305                                 }
306                                 closedir(dir2);
307                         }
308                 }
309                 closedir(dir);
310         }
311 }
312
313 static void scan_block(void)
314 {
315         char base[PATH_SIZE];
316         DIR *dir;
317         struct dirent *dent;
318
319         if (subsystem_filtered("block"))
320                 return;
321
322         strlcpy(base, sysfs_path, sizeof(base));
323         strlcat(base, "/block", sizeof(base));
324
325         dir = opendir(base);
326         if (dir != NULL) {
327                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
328                         char dirname[PATH_SIZE];
329                         DIR *dir2;
330                         struct dirent *dent2;
331
332                         if (dent->d_name[0] == '.')
333                                 continue;
334
335                         strlcpy(dirname, base, sizeof(dirname));
336                         strlcat(dirname, "/", sizeof(dirname));
337                         strlcat(dirname, dent->d_name, sizeof(dirname));
338                         if (attr_filtered(dirname))
339                                 continue;
340                         if (device_list_insert(dirname) != 0)
341                                 continue;
342
343                         /* look for partitions */
344                         dir2 = opendir(dirname);
345                         if (dir2 != NULL) {
346                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
347                                         char dirname2[PATH_SIZE];
348
349                                         if (dent2->d_name[0] == '.')
350                                                 continue;
351
352                                         if (!strcmp(dent2->d_name,"device"))
353                                                 continue;
354
355                                         strlcpy(dirname2, dirname, sizeof(dirname2));
356                                         strlcat(dirname2, "/", sizeof(dirname2));
357                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
358                                         if (attr_filtered(dirname2))
359                                                 continue;
360                                         device_list_insert(dirname2);
361                                 }
362                                 closedir(dir2);
363                         }
364                 }
365                 closedir(dir);
366         }
367 }
368
369 static void scan_class(void)
370 {
371         char base[PATH_SIZE];
372         DIR *dir;
373         struct dirent *dent;
374
375         strlcpy(base, sysfs_path, sizeof(base));
376         strlcat(base, "/class", sizeof(base));
377
378         dir = opendir(base);
379         if (dir != NULL) {
380                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
381                         char dirname[PATH_SIZE];
382                         DIR *dir2;
383                         struct dirent *dent2;
384
385                         if (dent->d_name[0] == '.')
386                                 continue;
387
388                         if (subsystem_filtered(dent->d_name))
389                                 continue;
390
391                         strlcpy(dirname, base, sizeof(dirname));
392                         strlcat(dirname, "/", sizeof(dirname));
393                         strlcat(dirname, dent->d_name, sizeof(dirname));
394                         dir2 = opendir(dirname);
395                         if (dir2 != NULL) {
396                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
397                                         char dirname2[PATH_SIZE];
398
399                                         if (dent2->d_name[0] == '.')
400                                                 continue;
401
402                                         if (!strcmp(dent2->d_name, "device"))
403                                                 continue;
404
405                                         strlcpy(dirname2, dirname, sizeof(dirname2));
406                                         strlcat(dirname2, "/", sizeof(dirname2));
407                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
408                                         if (attr_filtered(dirname2))
409                                                 continue;
410                                         device_list_insert(dirname2);
411                                 }
412                                 closedir(dir2);
413                         }
414                 }
415                 closedir(dir);
416         }
417 }
418
419 static void scan_failed(void)
420 {
421         char base[PATH_SIZE];
422         DIR *dir;
423         struct dirent *dent;
424
425         strlcpy(base, udev_root, sizeof(base));
426         strlcat(base, "/" EVENT_FAILED_DIR, sizeof(base));
427
428         dir = opendir(base);
429         if (dir != NULL) {
430                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
431                         char device[PATH_SIZE];
432                         size_t start;
433
434                         if (dent->d_name[0] == '.')
435                                 continue;
436
437                         start = strlcpy(device, sysfs_path, sizeof(device));
438                         strlcat(device, dent->d_name, sizeof(device));
439                         path_decode(&device[start]);
440                         device_list_insert(device);
441                 }
442                 closedir(dir);
443         }
444 }
445
446 int main(int argc, char *argv[], char *envp[])
447 {
448         int failed = 0;
449         int option;
450         const char *action = "add";
451         static const struct option options[] = {
452                 { "verbose", 0, NULL, 'v' },
453                 { "dry-run", 0, NULL, 'n' },
454                 { "retry-failed", 0, NULL, 'F' },
455                 { "help", 0, NULL, 'h' },
456                 { "action", 1, NULL, 'c' },
457                 { "subsystem-match", 1, NULL, 's' },
458                 { "subsystem-nomatch", 1, NULL, 'S' },
459                 { "attr-match", 1, NULL, 'a' },
460                 { "attr-nomatch", 1, NULL, 'A' },
461                 {}
462         };
463
464         logging_init("udevtrigger");
465         udev_config_init();
466         dbg("version %s", UDEV_VERSION);
467         sysfs_init();
468
469         while (1) {
470                 option = getopt_long(argc, argv, "vnFhc:s:S:a:A:", options, NULL);
471                 if (option == -1)
472                         break;
473
474                 switch (option) {
475                 case 'v':
476                         verbose = 1;
477                         break;
478                 case 'n':
479                         dry_run = 1;
480                         break;
481                 case 'F':
482                         failed = 1;
483                         break;
484                 case 'c':
485                         action = optarg;
486                         break;
487                 case 's':
488                         name_list_add(&filter_subsystem_match_list, optarg, 0);
489                         break;
490                 case 'S':
491                         name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
492                         break;
493                 case 'a':
494                         name_list_add(&filter_attr_match_list, optarg, 0);
495                         break;
496                 case 'A':
497                         name_list_add(&filter_attr_nomatch_list, optarg, 0);
498                         break;
499                 case 'h':
500                         printf("Usage: udevtrigger OPTIONS\n"
501                                "  --verbose                       print the list of devices while running\n"
502                                "  --dry-run                       do not actually trigger the events\n"
503                                "  --retry-failed                  trigger only the events which have been\n"
504                                "                                  marked as failed during a previous run\n"
505                                "  --subsystem-match=<subsystem>   trigger devices from a matching subystem\n"
506                                "  --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
507                                "  --attr-match=<file[=<value>]>   trigger devices with a matching sysfs\n"
508                                "                                  attribute\n"
509                                "  --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
510                                "                                  attribute\n"
511                                "  --help                          print this text\n"
512                                "\n");
513                         goto exit;
514                 default:
515                         goto exit;
516                 }
517         }
518
519         if (failed) {
520                 scan_failed();
521                 exec_list(action);
522         } else {
523                 char base[PATH_SIZE];
524                 struct stat statbuf;
525
526                 /* if we have /sys/subsystem, forget all the old stuff */
527                 strlcpy(base, sysfs_path, sizeof(base));
528                 strlcat(base, "/subsystem", sizeof(base));
529                 if (stat(base, &statbuf) == 0) {
530                         scan_subsystem("subsystem", SCAN_SUBSYSTEM);
531                         exec_list(action);
532                         scan_subsystem("subsystem", SCAN_DEVICES);
533                         exec_list(action);
534                 } else {
535                         scan_subsystem("bus", SCAN_SUBSYSTEM);
536                         exec_list(action);
537                         scan_subsystem("bus", SCAN_DEVICES);
538                         scan_class();
539
540                         /* scan "block" if it isn't a "class" */
541                         strlcpy(base, sysfs_path, sizeof(base));
542                         strlcat(base, "/class/block", sizeof(base));
543                         if (stat(base, &statbuf) != 0)
544                                 scan_block();
545                         exec_list(action);
546                 }
547         }
548
549 exit:
550         name_list_cleanup(&filter_subsystem_match_list);
551         name_list_cleanup(&filter_subsystem_nomatch_list);
552         name_list_cleanup(&filter_attr_match_list);
553         name_list_cleanup(&filter_attr_nomatch_list);
554
555         sysfs_cleanup();
556         logging_close();
557         return 0;
558 }