Changelog for beta 9013V1.1w
[tomato.git] / release / src / router / udev / udevtrigger.c.orig
blob3b5be2006e5de952bd2343e5489b8b22fa6bdae6
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  */
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>
34 #include "udev.h"
35 #include "udevd.h"
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);
45 #ifdef USE_LOG
46 void log_message(int priority, const char *format, ...)
48         va_list args;
50         if (priority > udev_log_priority)
51                 return;
53         va_start(args, format);
54         vsyslog(priority, format, args);
55         va_end(args);
57 #endif
59 /* devices that should run last cause of their dependencies */
60 static int delay_device(const char *devpath)
62         static const char *delay_device_list[] = {
63                 "*/md*",
64                 "*/dm-*",
65                 NULL
66         };
67         int i;
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;
75 static int device_list_insert(const char *path)
77         char filename[PATH_SIZE];
78         char devpath[PATH_SIZE];
79         struct stat statbuf;
81         dbg("add '%s'" , path);
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;
91         strlcpy(devpath, &path[strlen(sysfs_path)], sizeof(devpath));
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;
100         name_list_add(&device_list, devpath, 1);
101         return 0;
104 static void trigger_uevent(const char *devpath)
106         char filename[PATH_SIZE];
107         int fd;
109         strlcpy(filename, sysfs_path, sizeof(filename));
110         strlcat(filename, devpath, sizeof(filename));
111         strlcat(filename, "/uevent", sizeof(filename));
113         if (verbose)
114                 printf("%s\n", devpath);
116         if (dry_run)
117                 return;
119         fd = open(filename, O_WRONLY);
120         if (fd < 0) {
121                 dbg("error on opening %s: %s", filename, strerror(errno));
122                 return;
123         }
125         if (write(fd, "add", 3) < 0)
126                 info("error on triggering %s: %s", filename, strerror(errno));
128         close(fd);
131 static void exec_list(void)
133         struct name_entry *loop_device;
134         struct name_entry *tmp_device;
136         list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
137                 if (delay_device(loop_device->name))
138                         continue;
140                 trigger_uevent(loop_device->name);
141                 list_del(&loop_device->node);
142                 free(loop_device);
143         }
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         }
153 static int subsystem_filtered(const char *subsystem)
155         struct name_entry *loop_name;
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;
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         }
170         return 0;
173 static int attr_match(const char *path, const char *attr_value)
175         char attr[NAME_SIZE];
176         char file[PATH_SIZE];
177         char *match_value;
179         strlcpy(attr, attr_value, sizeof(attr));
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         }
188         strlcpy(file, path, sizeof(file));
189         strlcat(file, "/", sizeof(file));
190         strlcat(file, attr, sizeof(file));
192         if (match_value != NULL) {
193                 /* match file content */
194                 char value[NAME_SIZE];
195                 int fd;
196                 ssize_t size;
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');
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;
215                 if (stat(file, &statbuf) == 0)
216                         return 1;
217         }
218         return 0;
221 static int attr_filtered(const char *path)
223         struct name_entry *loop_name;
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;
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;
240 static void scan_subsystem(const char *subsys)
242         char base[PATH_SIZE];
243         DIR *dir;
244         struct dirent *dent;
246         strlcpy(base, sysfs_path, sizeof(base));
247         strlcat(base, "/", sizeof(base));
248         strlcat(base, subsys, sizeof(base));
250         dir = opendir(base);
251         if (dir != NULL) {
252                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
253                         char dirname[PATH_SIZE];
254                         DIR *dir2;
255                         struct dirent *dent2;
257                         if (dent->d_name[0] == '.')
258                                 continue;
260                         if (subsystem_filtered(dent->d_name))
261                                 continue;
263                         strlcpy(dirname, base, sizeof(dirname));
264                         strlcat(dirname, "/", sizeof(dirname));
265                         strlcat(dirname, dent->d_name, sizeof(dirname));
266                         strlcat(dirname, "/devices", sizeof(dirname));
268                         /* look for devices */
269                         dir2 = opendir(dirname);
270                         if (dir2 != NULL) {
271                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
272                                         char dirname2[PATH_SIZE];
274                                         if (dent2->d_name[0] == '.')
275                                                 continue;
277                                         strlcpy(dirname2, dirname, sizeof(dirname2));
278                                         strlcat(dirname2, "/", sizeof(dirname2));
279                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
280                                         if (attr_filtered(dirname2))
281                                                 continue;
282                                         device_list_insert(dirname2);
283                                 }
284                                 closedir(dir2);
285                         }
286                 }
287                 closedir(dir);
288         }
291 static void scan_block(void)
293         char base[PATH_SIZE];
294         DIR *dir;
295         struct dirent *dent;
297         if (subsystem_filtered("block"))
298                 return;
300         strlcpy(base, sysfs_path, sizeof(base));
301         strlcat(base, "/block", sizeof(base));
303         dir = opendir(base);
304         if (dir != NULL) {
305                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
306                         char dirname[PATH_SIZE];
307                         DIR *dir2;
308                         struct dirent *dent2;
310                         if (dent->d_name[0] == '.')
311                                 continue;
313                         strlcpy(dirname, base, sizeof(dirname));
314                         strlcat(dirname, "/", sizeof(dirname));
315                         strlcat(dirname, dent->d_name, sizeof(dirname));
316                         if (attr_filtered(dirname))
317                                 continue;
318                         if (device_list_insert(dirname) != 0)
319                                 continue;
321                         /* look for partitions */
322                         dir2 = opendir(dirname);
323                         if (dir2 != NULL) {
324                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
325                                         char dirname2[PATH_SIZE];
327                                         if (dent2->d_name[0] == '.')
328                                                 continue;
330                                         if (!strcmp(dent2->d_name,"device"))
331                                                 continue;
333                                         strlcpy(dirname2, dirname, sizeof(dirname2));
334                                         strlcat(dirname2, "/", sizeof(dirname2));
335                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
336                                         if (attr_filtered(dirname2))
337                                                 continue;
338                                         device_list_insert(dirname2);
339                                 }
340                                 closedir(dir2);
341                         }
342                 }
343                 closedir(dir);
344         }
347 static void scan_class(void)
349         char base[PATH_SIZE];
350         DIR *dir;
351         struct dirent *dent;
353         strlcpy(base, sysfs_path, sizeof(base));
354         strlcat(base, "/class", sizeof(base));
356         dir = opendir(base);
357         if (dir != NULL) {
358                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
359                         char dirname[PATH_SIZE];
360                         DIR *dir2;
361                         struct dirent *dent2;
363                         if (dent->d_name[0] == '.')
364                                 continue;
366                         if (subsystem_filtered(dent->d_name))
367                                 continue;
369                         strlcpy(dirname, base, sizeof(dirname));
370                         strlcat(dirname, "/", sizeof(dirname));
371                         strlcat(dirname, dent->d_name, sizeof(dirname));
372                         dir2 = opendir(dirname);
373                         if (dir2 != NULL) {
374                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
375                                         char dirname2[PATH_SIZE];
377                                         if (dent2->d_name[0] == '.')
378                                                 continue;
380                                         if (!strcmp(dent2->d_name, "device"))
381                                                 continue;
383                                         strlcpy(dirname2, dirname, sizeof(dirname2));
384                                         strlcat(dirname2, "/", sizeof(dirname2));
385                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
386                                         if (attr_filtered(dirname2))
387                                                 continue;
388                                         device_list_insert(dirname2);
389                                 }
390                                 closedir(dir2);
391                         }
392                 }
393                 closedir(dir);
394         }
397 static void scan_failed(void)
399         char base[PATH_SIZE];
400         DIR *dir;
401         struct dirent *dent;
403         strlcpy(base, udev_root, sizeof(base));
404         strlcat(base, "/" EVENT_FAILED_DIR, sizeof(base));
406         dir = opendir(base);
407         if (dir != NULL) {
408                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
409                         char device[PATH_SIZE];
410                         size_t start;
412                         if (dent->d_name[0] == '.')
413                                 continue;
415                         start = strlcpy(device, sysfs_path, sizeof(device));
416                         strlcat(device, dent->d_name, sizeof(device));
417                         path_decode(&device[start]);
418                         device_list_insert(device);
419                 }
420                 closedir(dir);
421         }
424 int main(int argc, char *argv[], char *envp[])
426         int failed = 0;
427         int option;
428         static const struct option options[] = {
429                 { "verbose", 0, NULL, 'v' },
430                 { "dry-run", 0, NULL, 'n' },
431                 { "retry-failed", 0, NULL, 'F' },
432                 { "help", 0, NULL, 'h' },
433                 { "subsystem-match", 1, NULL, 's' },
434                 { "subsystem-nomatch", 1, NULL, 'S' },
435                 { "attr-match", 1, NULL, 'a' },
436                 { "attr-nomatch", 1, NULL, 'A' },
437                 {}
438         };
440         logging_init("udevtrigger");
441         udev_config_init();
442         dbg("version %s", UDEV_VERSION);
443         sysfs_init();
445         while (1) {
446                 option = getopt_long(argc, argv, "vnFhs:S:a:A:", options, NULL);
447                 if (option == -1)
448                         break;
450                 switch (option) {
451                 case 'v':
452                         verbose = 1;
453                         break;
454                 case 'n':
455                         dry_run = 1;
456                         break;
457                 case 'F':
458                         failed = 1;
459                         break;
460                 case 's':
461                         name_list_add(&filter_subsystem_match_list, optarg, 0);
462                         break;
463                 case 'S':
464                         name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
465                         break;
466                 case 'a':
467                         name_list_add(&filter_attr_match_list, optarg, 0);
468                         break;
469                 case 'A':
470                         name_list_add(&filter_attr_nomatch_list, optarg, 0);
471                         break;
472                 case 'h':
473                         printf("Usage: udevtrigger OPTIONS\n"
474                                "  --verbose                       print the list of devices while running\n"
475                                "  --dry-run                       do not actually trigger the events\n"
476                                "  --retry-failed                  trigger only the events which have been\n"
477                                "                                  marked as failed during a previous run\n"
478                                "  --subsystem-match=<subsystem>   trigger devices from a matching subystem\n"
479                                "  --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
480                                "  --attr-match=<file[=<value>]>   trigger devices with a matching sysfs\n"
481                                "                                  attribute\n"
482                                "  --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
483                                "                                  attribute\n"
484                                "  --help                          print this text\n"
485                                "\n");
486                         goto exit;
487                 default:
488                         goto exit;
489                 }
490         }
492         if (failed)
493                 scan_failed();
494         else {
495                 char base[PATH_SIZE];
496                 struct stat statbuf;
498                 /* if we have /sys/subsystem, forget all the old stuff */
499                 strlcpy(base, sysfs_path, sizeof(base));
500                 strlcat(base, "/subsystem", sizeof(base));
501                 if (stat(base, &statbuf) == 0)
502                         scan_subsystem("subsystem");
503                 else {
504                         scan_subsystem("bus");
505                         scan_class();
507                         /* scan "block" if it isn't a "class" */
508                         strlcpy(base, sysfs_path, sizeof(base));
509                         strlcat(base, "/class/block", sizeof(base));
510                         if (stat(base, &statbuf) != 0)
511                                 scan_block();
512                 }
513         }
514         exec_list();
516 exit:
517         name_list_cleanup(&filter_subsystem_match_list);
518         name_list_cleanup(&filter_subsystem_nomatch_list);
519         name_list_cleanup(&filter_attr_match_list);
520         name_list_cleanup(&filter_attr_nomatch_list);
522         sysfs_cleanup();
523         logging_close();
524         return 0;