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