chiark / gitweb /
7584e02a67dbeee9ce6e097b64f868a9afbf70b7
[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 #include <sys/socket.h>
34 #include <sys/un.h>
35
36 #include "udev.h"
37 #include "udevd.h"
38 #include "udev_rules.h"
39
40 static int verbose;
41 static int dry_run;
42 LIST_HEAD(device_list);
43 LIST_HEAD(filter_subsystem_match_list);
44 LIST_HEAD(filter_subsystem_nomatch_list);
45 LIST_HEAD(filter_attr_match_list);
46 LIST_HEAD(filter_attr_nomatch_list);
47 static int sock = -1;
48 static struct sockaddr_un saddr;
49 static socklen_t saddrlen;
50
51 /* devices that should run last cause of their dependencies */
52 static int delay_device(const char *devpath)
53 {
54         static const char *delay_device_list[] = {
55                 "*/md*",
56                 "*/dm-*",
57                 NULL
58         };
59         int i;
60
61         for (i = 0; delay_device_list[i] != NULL; i++)
62                 if (fnmatch(delay_device_list[i], devpath, 0) == 0)
63                         return 1;
64         return 0;
65 }
66
67 static int device_list_insert(const char *path)
68 {
69         char filename[PATH_SIZE];
70         char devpath[PATH_SIZE];
71         struct stat statbuf;
72
73         dbg("add '%s'" , path);
74
75         /* we only have a device, if we have an uevent file */
76         strlcpy(filename, path, sizeof(filename));
77         strlcat(filename, "/uevent", sizeof(filename));
78         if (stat(filename, &statbuf) < 0)
79                 return -1;
80         if (!(statbuf.st_mode & S_IWUSR))
81                 return -1;
82
83         strlcpy(devpath, &path[strlen(sysfs_path)], sizeof(devpath));
84
85         /* resolve possible link to real target */
86         if (lstat(path, &statbuf) < 0)
87                 return -1;
88         if (S_ISLNK(statbuf.st_mode))
89                 if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
90                         return -1;
91
92         name_list_add(&device_list, devpath, 1);
93         return 0;
94 }
95
96 static void trigger_uevent(const char *devpath, const char *action)
97 {
98         char filename[PATH_SIZE];
99         int fd;
100
101         strlcpy(filename, sysfs_path, sizeof(filename));
102         strlcat(filename, devpath, sizeof(filename));
103         strlcat(filename, "/uevent", sizeof(filename));
104
105         if (verbose)
106                 printf("%s\n", devpath);
107
108         if (dry_run)
109                 return;
110
111         fd = open(filename, O_WRONLY);
112         if (fd < 0) {
113                 dbg("error on opening %s: %s", filename, strerror(errno));
114                 return;
115         }
116
117         if (write(fd, action, strlen(action)) < 0)
118                 info("error writing '%s' to '%s': %s", action, filename, strerror(errno));
119
120         close(fd);
121 }
122
123 static int pass_to_socket(const char *devpath, const char *action)
124 {
125         struct udevice udev;
126         struct name_entry *name_loop;
127         char buf[4096];
128         size_t bufpos = 0;
129         ssize_t count;
130         char path[PATH_SIZE];
131         int fd;
132         char link_target[PATH_SIZE];
133         int len;
134         int err = 0;
135
136         udev_device_init(&udev);
137         udev_db_get_device(&udev, devpath);
138
139         /* add header */
140         bufpos = snprintf(buf, sizeof(buf)-1, "%s@%s", action, devpath);
141         bufpos++;
142
143         /* add standard keys */
144         bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVPATH=%s", devpath);
145         bufpos++;
146         bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "ACTION=%s", action);
147         bufpos++;
148
149         /* add subsystem */
150         strlcpy(path, sysfs_path, sizeof(path));
151         strlcat(path, devpath, sizeof(path));
152         strlcat(path, "/subsystem", sizeof(path));
153         len = readlink(path, link_target, sizeof(link_target));
154         if (len > 0) {
155                 char *pos;
156
157                 link_target[len] = '\0';
158                 pos = strrchr(link_target, '/');
159                 if (pos != NULL) {
160                         bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "SUBSYSTEM=%s", &pos[1]);
161                         bufpos++;
162                 }
163         }
164
165         /* add symlinks and node name */
166         path[0] = '\0';
167         list_for_each_entry(name_loop, &udev.symlink_list, node) {
168                 strlcat(path, udev_root, sizeof(path));
169                 strlcat(path, "/", sizeof(path));
170                 strlcat(path, name_loop->name, sizeof(path));
171                 strlcat(path, " ", sizeof(path));
172         }
173         remove_trailing_chars(path, ' ');
174         if (path[0] != '\0') {
175                 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVLINKS=%s", path);
176                 bufpos++;
177         }
178         if (udev.name[0] != '\0') {
179                 strlcpy(path, udev_root, sizeof(path));
180                 strlcat(path, "/", sizeof(path));
181                 strlcat(path, udev.name, sizeof(path));
182                 bufpos += snprintf(&buf[bufpos], sizeof(buf)-1, "DEVNAME=%s", path);
183                 bufpos++;
184         }
185
186         /* add keys from device "uevent" file */
187         strlcpy(path, sysfs_path, sizeof(path));
188         strlcat(path, devpath, sizeof(path));
189         strlcat(path, "/uevent", sizeof(path));
190         fd = open(path, O_RDONLY);
191         if (fd >= 0) {
192                 char value[4096];
193
194                 count = read(fd, value, sizeof(value));
195                 close(fd);
196                 if (count > 0) {
197                         char *key;
198
199                         value[count] = '\0';
200                         key = value;
201                         while (key[0] != '\0') {
202                                 char *next;
203
204                                 next = strchr(key, '\n');
205                                 if (next == NULL)
206                                         break;
207                                 next[0] = '\0';
208                                 bufpos += strlcpy(&buf[bufpos], key, sizeof(buf) - bufpos-1);
209                                 bufpos++;
210                                 key = &next[1];
211                         }
212                 }
213         }
214
215         /* add keys from database */
216         list_for_each_entry(name_loop, &udev.env_list, node) {
217                 bufpos += strlcpy(&buf[bufpos], name_loop->name, sizeof(buf) - bufpos-1);
218                 bufpos++;
219         }
220         if (bufpos > sizeof(buf))
221                 bufpos = sizeof(buf);
222
223         count = sendto(sock, &buf, bufpos, 0, (struct sockaddr *)&saddr, saddrlen);
224         if (count < 0)
225                 err = -1;
226
227         return err;
228 }
229
230 static void exec_list(const char *action)
231 {
232         struct name_entry *loop_device;
233         struct name_entry *tmp_device;
234
235         list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
236                 if (delay_device(loop_device->name))
237                         continue;
238                 if (sock)
239                         pass_to_socket(loop_device->name, action);
240                 else
241                         trigger_uevent(loop_device->name, action);
242                 list_del(&loop_device->node);
243                 free(loop_device);
244         }
245
246         /* trigger remaining delayed devices */
247         list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
248                 if (sock)
249                         pass_to_socket(loop_device->name, action);
250                 else
251                         trigger_uevent(loop_device->name, action);
252                 list_del(&loop_device->node);
253                 free(loop_device);
254         }
255 }
256
257 static int subsystem_filtered(const char *subsystem)
258 {
259         struct name_entry *loop_name;
260
261         /* skip devices matching the listed subsystems */
262         list_for_each_entry(loop_name, &filter_subsystem_nomatch_list, node)
263                 if (fnmatch(loop_name->name, subsystem, 0) == 0)
264                         return 1;
265
266         /* skip devices not matching the listed subsystems */
267         if (!list_empty(&filter_subsystem_match_list)) {
268                 list_for_each_entry(loop_name, &filter_subsystem_match_list, node)
269                         if (fnmatch(loop_name->name, subsystem, 0) == 0)
270                                 return 0;
271                 return 1;
272         }
273
274         return 0;
275 }
276
277 static int attr_match(const char *path, const char *attr_value)
278 {
279         char attr[NAME_SIZE];
280         char file[PATH_SIZE];
281         char *match_value;
282
283         strlcpy(attr, attr_value, sizeof(attr));
284
285         /* separate attr and match value */
286         match_value = strchr(attr, '=');
287         if (match_value != NULL) {
288                 match_value[0] = '\0';
289                 match_value = &match_value[1];
290         }
291
292         strlcpy(file, path, sizeof(file));
293         strlcat(file, "/", sizeof(file));
294         strlcat(file, attr, sizeof(file));
295
296         if (match_value != NULL) {
297                 /* match file content */
298                 char value[NAME_SIZE];
299                 int fd;
300                 ssize_t size;
301
302                 fd = open(file, O_RDONLY);
303                 if (fd < 0)
304                         return 0;
305                 size = read(fd, value, sizeof(value));
306                 close(fd);
307                 if (size < 0)
308                         return 0;
309                 value[size] = '\0';
310                 remove_trailing_chars(value, '\n');
311
312                 /* match if attribute value matches */
313                 if (fnmatch(match_value, value, 0) == 0)
314                         return 1;
315         } else {
316                 /* match if attribute exists */
317                 struct stat statbuf;
318
319                 if (stat(file, &statbuf) == 0)
320                         return 1;
321         }
322         return 0;
323 }
324
325 static int attr_filtered(const char *path)
326 {
327         struct name_entry *loop_name;
328
329         /* skip devices matching the listed sysfs attributes */
330         list_for_each_entry(loop_name, &filter_attr_nomatch_list, node)
331                 if (attr_match(path, loop_name->name))
332                         return 1;
333
334         /* skip devices not matching the listed sysfs attributes */
335         if (!list_empty(&filter_attr_match_list)) {
336                 list_for_each_entry(loop_name, &filter_attr_match_list, node)
337                         if (attr_match(path, loop_name->name))
338                                 return 0;
339                 return 1;
340         }
341         return 0;
342 }
343
344 enum scan_type {
345         SCAN_DEVICES,
346         SCAN_SUBSYSTEM,
347 };
348
349 static void scan_subsystem(const char *subsys, enum scan_type scan)
350 {
351         char base[PATH_SIZE];
352         DIR *dir;
353         struct dirent *dent;
354         const char *subdir;
355
356         if (scan == SCAN_DEVICES)
357                 subdir = "/devices";
358         else if (scan == SCAN_SUBSYSTEM)
359                 subdir = "/drivers";
360         else
361                 return;
362
363         strlcpy(base, sysfs_path, sizeof(base));
364         strlcat(base, "/", sizeof(base));
365         strlcat(base, subsys, sizeof(base));
366
367         dir = opendir(base);
368         if (dir != NULL) {
369                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
370                         char dirname[PATH_SIZE];
371                         DIR *dir2;
372                         struct dirent *dent2;
373
374                         if (dent->d_name[0] == '.')
375                                 continue;
376
377                         if (scan == SCAN_DEVICES)
378                                 if (subsystem_filtered(dent->d_name))
379                                         continue;
380
381                         strlcpy(dirname, base, sizeof(dirname));
382                         strlcat(dirname, "/", sizeof(dirname));
383                         strlcat(dirname, dent->d_name, sizeof(dirname));
384
385                         if (scan == SCAN_SUBSYSTEM) {
386                                 if (!subsystem_filtered("subsystem"))
387                                         device_list_insert(dirname);
388                                 if (subsystem_filtered("drivers"))
389                                         continue;
390                         }
391
392                         strlcat(dirname, subdir, sizeof(dirname));
393
394                         /* look for devices/drivers */
395                         dir2 = opendir(dirname);
396                         if (dir2 != NULL) {
397                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
398                                         char dirname2[PATH_SIZE];
399
400                                         if (dent2->d_name[0] == '.')
401                                                 continue;
402
403                                         strlcpy(dirname2, dirname, sizeof(dirname2));
404                                         strlcat(dirname2, "/", sizeof(dirname2));
405                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
406                                         if (attr_filtered(dirname2))
407                                                 continue;
408                                         device_list_insert(dirname2);
409                                 }
410                                 closedir(dir2);
411                         }
412                 }
413                 closedir(dir);
414         }
415 }
416
417 static void scan_block(void)
418 {
419         char base[PATH_SIZE];
420         DIR *dir;
421         struct dirent *dent;
422
423         if (subsystem_filtered("block"))
424                 return;
425
426         strlcpy(base, sysfs_path, sizeof(base));
427         strlcat(base, "/block", sizeof(base));
428
429         dir = opendir(base);
430         if (dir != NULL) {
431                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
432                         char dirname[PATH_SIZE];
433                         DIR *dir2;
434                         struct dirent *dent2;
435
436                         if (dent->d_name[0] == '.')
437                                 continue;
438
439                         strlcpy(dirname, base, sizeof(dirname));
440                         strlcat(dirname, "/", sizeof(dirname));
441                         strlcat(dirname, dent->d_name, sizeof(dirname));
442                         if (attr_filtered(dirname))
443                                 continue;
444                         if (device_list_insert(dirname) != 0)
445                                 continue;
446
447                         /* look for partitions */
448                         dir2 = opendir(dirname);
449                         if (dir2 != NULL) {
450                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
451                                         char dirname2[PATH_SIZE];
452
453                                         if (dent2->d_name[0] == '.')
454                                                 continue;
455
456                                         if (!strcmp(dent2->d_name,"device"))
457                                                 continue;
458
459                                         strlcpy(dirname2, dirname, sizeof(dirname2));
460                                         strlcat(dirname2, "/", sizeof(dirname2));
461                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
462                                         if (attr_filtered(dirname2))
463                                                 continue;
464                                         device_list_insert(dirname2);
465                                 }
466                                 closedir(dir2);
467                         }
468                 }
469                 closedir(dir);
470         }
471 }
472
473 static void scan_class(void)
474 {
475         char base[PATH_SIZE];
476         DIR *dir;
477         struct dirent *dent;
478
479         strlcpy(base, sysfs_path, sizeof(base));
480         strlcat(base, "/class", sizeof(base));
481
482         dir = opendir(base);
483         if (dir != NULL) {
484                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
485                         char dirname[PATH_SIZE];
486                         DIR *dir2;
487                         struct dirent *dent2;
488
489                         if (dent->d_name[0] == '.')
490                                 continue;
491
492                         if (subsystem_filtered(dent->d_name))
493                                 continue;
494
495                         strlcpy(dirname, base, sizeof(dirname));
496                         strlcat(dirname, "/", sizeof(dirname));
497                         strlcat(dirname, dent->d_name, sizeof(dirname));
498                         dir2 = opendir(dirname);
499                         if (dir2 != NULL) {
500                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
501                                         char dirname2[PATH_SIZE];
502
503                                         if (dent2->d_name[0] == '.')
504                                                 continue;
505
506                                         if (!strcmp(dent2->d_name, "device"))
507                                                 continue;
508
509                                         strlcpy(dirname2, dirname, sizeof(dirname2));
510                                         strlcat(dirname2, "/", sizeof(dirname2));
511                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
512                                         if (attr_filtered(dirname2))
513                                                 continue;
514                                         device_list_insert(dirname2);
515                                 }
516                                 closedir(dir2);
517                         }
518                 }
519                 closedir(dir);
520         }
521 }
522
523 static void scan_failed(void)
524 {
525         char base[PATH_SIZE];
526         DIR *dir;
527         struct dirent *dent;
528
529         strlcpy(base, udev_root, sizeof(base));
530         strlcat(base, "/" EVENT_FAILED_DIR, sizeof(base));
531
532         dir = opendir(base);
533         if (dir != NULL) {
534                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
535                         char device[PATH_SIZE];
536                         size_t start;
537
538                         if (dent->d_name[0] == '.')
539                                 continue;
540
541                         start = strlcpy(device, sysfs_path, sizeof(device));
542                         if(start >= sizeof(device))
543                                 start = sizeof(device) - 1;
544                         strlcat(device, dent->d_name, sizeof(device));
545                         path_decode(&device[start]);
546                         device_list_insert(device);
547                 }
548                 closedir(dir);
549         }
550 }
551
552 int udevtrigger(int argc, char *argv[], char *envp[])
553 {
554         int failed = 0;
555         const char *sockpath = NULL;
556         int option;
557         const char *action = "add";
558         static const struct option options[] = {
559                 { "verbose", 0, NULL, 'v' },
560                 { "dry-run", 0, NULL, 'n' },
561                 { "retry-failed", 0, NULL, 'F' },
562                 { "socket", 1, NULL, 'o' },
563                 { "help", 0, NULL, 'h' },
564                 { "action", 1, NULL, 'c' },
565                 { "subsystem-match", 1, NULL, 's' },
566                 { "subsystem-nomatch", 1, NULL, 'S' },
567                 { "attr-match", 1, NULL, 'a' },
568                 { "attr-nomatch", 1, NULL, 'A' },
569                 {}
570         };
571
572         logging_init("udevtrigger");
573         udev_config_init();
574         dbg("version %s", UDEV_VERSION);
575         sysfs_init();
576
577         while (1) {
578                 option = getopt_long(argc, argv, "vnFo:hc:s:S:a:A:", options, NULL);
579                 if (option == -1)
580                         break;
581
582                 switch (option) {
583                 case 'v':
584                         verbose = 1;
585                         break;
586                 case 'n':
587                         dry_run = 1;
588                         break;
589                 case 'F':
590                         failed = 1;
591                         break;
592                 case 'o':
593                         sockpath = optarg;
594                         break;
595                 case 'c':
596                         action = optarg;
597                         break;
598                 case 's':
599                         name_list_add(&filter_subsystem_match_list, optarg, 0);
600                         break;
601                 case 'S':
602                         name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
603                         break;
604                 case 'a':
605                         name_list_add(&filter_attr_match_list, optarg, 0);
606                         break;
607                 case 'A':
608                         name_list_add(&filter_attr_nomatch_list, optarg, 0);
609                         break;
610                 case 'h':
611                         printf("Usage: udevadm trigger OPTIONS\n"
612                                "  --verbose                       print the list of devices while running\n"
613                                "  --dry-run                       do not actually trigger the events\n"
614                                "  --retry-failed                  trigger only the events which have been\n"
615                                "                                  marked as failed during a previous run\n"
616                                "  --subsystem-match=<subsystem>   trigger devices from a matching subystem\n"
617                                "  --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
618                                "  --attr-match=<file[=<value>]>   trigger devices with a matching sysfs\n"
619                                "                                  attribute\n"
620                                "  --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
621                                "                                  attribute\n"
622                                "  --help                          print this text\n"
623                                "\n");
624                         goto exit;
625                 default:
626                         goto exit;
627                 }
628         }
629
630         if (sockpath != NULL) {
631                 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
632                 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
633                 saddr.sun_family = AF_LOCAL;
634                 /* abstract namespace only */
635                 strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1);
636                 saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path+1) + 1;
637         }
638
639         if (failed) {
640                 scan_failed();
641                 exec_list(action);
642         } else {
643                 char base[PATH_SIZE];
644                 struct stat statbuf;
645
646                 /* if we have /sys/subsystem, forget all the old stuff */
647                 strlcpy(base, sysfs_path, sizeof(base));
648                 strlcat(base, "/subsystem", sizeof(base));
649                 if (stat(base, &statbuf) == 0) {
650                         scan_subsystem("subsystem", SCAN_SUBSYSTEM);
651                         exec_list(action);
652                         scan_subsystem("subsystem", SCAN_DEVICES);
653                         exec_list(action);
654                 } else {
655                         scan_subsystem("bus", SCAN_SUBSYSTEM);
656                         exec_list(action);
657                         scan_subsystem("bus", SCAN_DEVICES);
658                         scan_class();
659
660                         /* scan "block" if it isn't a "class" */
661                         strlcpy(base, sysfs_path, sizeof(base));
662                         strlcat(base, "/class/block", sizeof(base));
663                         if (stat(base, &statbuf) != 0)
664                                 scan_block();
665                         exec_list(action);
666                 }
667         }
668
669 exit:
670         name_list_cleanup(&filter_subsystem_match_list);
671         name_list_cleanup(&filter_subsystem_nomatch_list);
672         name_list_cleanup(&filter_attr_match_list);
673         name_list_cleanup(&filter_attr_nomatch_list);
674
675         if (sock >= 0)
676                 close(sock);
677         sysfs_cleanup();
678         logging_close();
679         return 0;
680 }