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