chiark / gitweb /
libudev: libudev.pc add Libs.private
[elogind.git] / udev / 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 (attr_filtered(dirname))
396                                         continue;
397                                 if (!subsystem_filtered("subsystem"))
398                                         device_list_insert(dirname);
399                                 if (subsystem_filtered("drivers"))
400                                         continue;
401                         }
402
403                         strlcat(dirname, subdir, sizeof(dirname));
404
405                         /* look for devices/drivers */
406                         dir2 = opendir(dirname);
407                         if (dir2 != NULL) {
408                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
409                                         char dirname2[PATH_SIZE];
410
411                                         if (dent2->d_name[0] == '.')
412                                                 continue;
413
414                                         strlcpy(dirname2, dirname, sizeof(dirname2));
415                                         strlcat(dirname2, "/", sizeof(dirname2));
416                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
417                                         if (attr_filtered(dirname2))
418                                                 continue;
419                                         device_list_insert(dirname2);
420                                 }
421                                 closedir(dir2);
422                         }
423                 }
424                 closedir(dir);
425         }
426 }
427
428 static void scan_block(void)
429 {
430         char base[PATH_SIZE];
431         DIR *dir;
432         struct dirent *dent;
433
434         if (subsystem_filtered("block"))
435                 return;
436
437         strlcpy(base, sysfs_path, sizeof(base));
438         strlcat(base, "/block", sizeof(base));
439
440         dir = opendir(base);
441         if (dir != NULL) {
442                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
443                         char dirname[PATH_SIZE];
444                         DIR *dir2;
445                         struct dirent *dent2;
446
447                         if (dent->d_name[0] == '.')
448                                 continue;
449
450                         strlcpy(dirname, base, sizeof(dirname));
451                         strlcat(dirname, "/", sizeof(dirname));
452                         strlcat(dirname, dent->d_name, sizeof(dirname));
453                         if (attr_filtered(dirname))
454                                 continue;
455                         if (device_list_insert(dirname) != 0)
456                                 continue;
457
458                         /* look for partitions */
459                         dir2 = opendir(dirname);
460                         if (dir2 != NULL) {
461                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
462                                         char dirname2[PATH_SIZE];
463
464                                         if (dent2->d_name[0] == '.')
465                                                 continue;
466
467                                         if (!strcmp(dent2->d_name,"device"))
468                                                 continue;
469
470                                         strlcpy(dirname2, dirname, sizeof(dirname2));
471                                         strlcat(dirname2, "/", sizeof(dirname2));
472                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
473                                         if (attr_filtered(dirname2))
474                                                 continue;
475                                         device_list_insert(dirname2);
476                                 }
477                                 closedir(dir2);
478                         }
479                 }
480                 closedir(dir);
481         }
482 }
483
484 static void scan_class(void)
485 {
486         char base[PATH_SIZE];
487         DIR *dir;
488         struct dirent *dent;
489
490         strlcpy(base, sysfs_path, sizeof(base));
491         strlcat(base, "/class", sizeof(base));
492
493         dir = opendir(base);
494         if (dir != NULL) {
495                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
496                         char dirname[PATH_SIZE];
497                         DIR *dir2;
498                         struct dirent *dent2;
499
500                         if (dent->d_name[0] == '.')
501                                 continue;
502
503                         if (subsystem_filtered(dent->d_name))
504                                 continue;
505
506                         strlcpy(dirname, base, sizeof(dirname));
507                         strlcat(dirname, "/", sizeof(dirname));
508                         strlcat(dirname, dent->d_name, sizeof(dirname));
509                         dir2 = opendir(dirname);
510                         if (dir2 != NULL) {
511                                 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
512                                         char dirname2[PATH_SIZE];
513
514                                         if (dent2->d_name[0] == '.')
515                                                 continue;
516
517                                         if (!strcmp(dent2->d_name, "device"))
518                                                 continue;
519
520                                         strlcpy(dirname2, dirname, sizeof(dirname2));
521                                         strlcat(dirname2, "/", sizeof(dirname2));
522                                         strlcat(dirname2, dent2->d_name, sizeof(dirname2));
523                                         if (attr_filtered(dirname2))
524                                                 continue;
525                                         device_list_insert(dirname2);
526                                 }
527                                 closedir(dir2);
528                         }
529                 }
530                 closedir(dir);
531         }
532 }
533
534 static void scan_failed(void)
535 {
536         char base[PATH_SIZE];
537         DIR *dir;
538         struct dirent *dent;
539
540         strlcpy(base, udev_root, sizeof(base));
541         strlcat(base, "/.udev/failed", sizeof(base));
542
543         dir = opendir(base);
544         if (dir != NULL) {
545                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
546                         char device[PATH_SIZE];
547                         size_t start;
548
549                         if (dent->d_name[0] == '.')
550                                 continue;
551
552                         start = strlcpy(device, sysfs_path, sizeof(device));
553                         if(start >= sizeof(device))
554                                 start = sizeof(device) - 1;
555                         strlcat(device, dent->d_name, sizeof(device));
556                         path_decode(&device[start]);
557                         device_list_insert(device);
558                 }
559                 closedir(dir);
560         }
561 }
562
563 int udevtrigger(int argc, char *argv[], char *envp[])
564 {
565         int failed = 0;
566         const char *sockpath = NULL;
567         int option;
568         const char *action = "add";
569         const char *env = NULL;
570         static const struct option options[] = {
571                 { "verbose", 0, NULL, 'v' },
572                 { "dry-run", 0, NULL, 'n' },
573                 { "retry-failed", 0, NULL, 'F' },
574                 { "socket", 1, NULL, 'o' },
575                 { "help", 0, NULL, 'h' },
576                 { "action", 1, NULL, 'c' },
577                 { "subsystem-match", 1, NULL, 's' },
578                 { "subsystem-nomatch", 1, NULL, 'S' },
579                 { "attr-match", 1, NULL, 'a' },
580                 { "attr-nomatch", 1, NULL, 'A' },
581                 { "env", 1, NULL, 'e' },
582                 {}
583         };
584
585         logging_init("udevtrigger");
586         udev_config_init();
587         dbg("version %s\n", VERSION);
588         sysfs_init();
589
590         while (1) {
591                 option = getopt_long(argc, argv, "vnFo:hce::s:S:a:A:", options, NULL);
592                 if (option == -1)
593                         break;
594
595                 switch (option) {
596                 case 'v':
597                         verbose = 1;
598                         break;
599                 case 'n':
600                         dry_run = 1;
601                         break;
602                 case 'F':
603                         failed = 1;
604                         break;
605                 case 'o':
606                         sockpath = optarg;
607                         break;
608                 case 'c':
609                         action = optarg;
610                         break;
611                 case 'e':
612                         if (strchr(optarg, '=') != NULL)
613                                 env = optarg;
614                         break;
615                 case 's':
616                         name_list_add(&filter_subsystem_match_list, optarg, 0);
617                         break;
618                 case 'S':
619                         name_list_add(&filter_subsystem_nomatch_list, optarg, 0);
620                         break;
621                 case 'a':
622                         name_list_add(&filter_attr_match_list, optarg, 0);
623                         break;
624                 case 'A':
625                         name_list_add(&filter_attr_nomatch_list, optarg, 0);
626                         break;
627                 case 'h':
628                         printf("Usage: udevadm trigger OPTIONS\n"
629                                "  --verbose                       print the list of devices while running\n"
630                                "  --dry-run                       do not actually trigger the events\n"
631                                "  --retry-failed                  trigger only the events which have been\n"
632                                "                                  marked as failed during a previous run\n"
633                                "  --socket=<socket path>          pass events to socket instead of triggering kernel events\n"
634                                "  --env=<KEY>=<value>             pass an additional key (works only with --socket=)\n"
635                                "  --subsystem-match=<subsystem>   trigger devices from a matching subystem\n"
636                                "  --subsystem-nomatch=<subsystem> exclude devices from a matching subystem\n"
637                                "  --attr-match=<file[=<value>]>   trigger devices with a matching sysfs\n"
638                                "                                  attribute\n"
639                                "  --attr-nomatch=<file[=<value>]> exclude devices with a matching sysfs\n"
640                                "                                  attribute\n"
641                                "  --help                          print this text\n"
642                                "\n");
643                         goto exit;
644                 default:
645                         goto exit;
646                 }
647         }
648
649         if (sockpath != NULL) {
650                 struct stat stats;
651
652                 sock = socket(AF_LOCAL, SOCK_DGRAM, 0);
653                 memset(&saddr, 0x00, sizeof(struct sockaddr_un));
654                 saddr.sun_family = AF_LOCAL;
655                 if (sockpath[0] == '@') {
656                         /* abstract namespace socket requested */
657                         strlcpy(&saddr.sun_path[1], &sockpath[1], sizeof(saddr.sun_path)-1);
658                         saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
659                 } else if (stat(sockpath, &stats) == 0 && S_ISSOCK(stats.st_mode)) {
660                         /* existing socket file */
661                         strlcpy(saddr.sun_path, sockpath, sizeof(saddr.sun_path));
662                         saddrlen = offsetof(struct sockaddr_un, sun_path) + strlen(saddr.sun_path);
663                 } else {
664                         /* no socket file, assume abstract namespace socket */
665                         strlcpy(&saddr.sun_path[1], sockpath, sizeof(saddr.sun_path)-1);
666                         saddrlen = offsetof(struct sockaddr_un, sun_path) + 1 + strlen(&saddr.sun_path[1]);
667                 }
668         } else if (env != NULL) {
669                 fprintf(stderr, "error: --env= only valid with --socket= option\n");
670                 goto exit;
671         }
672
673         if (failed) {
674                 scan_failed();
675                 exec_list(action, env);
676         } else {
677                 char base[PATH_SIZE];
678                 struct stat statbuf;
679
680                 /* if we have /sys/subsystem, forget all the old stuff */
681                 strlcpy(base, sysfs_path, sizeof(base));
682                 strlcat(base, "/subsystem", sizeof(base));
683                 if (stat(base, &statbuf) == 0) {
684                         scan_subsystem("subsystem", SCAN_SUBSYSTEM);
685                         exec_list(action, env);
686                         scan_subsystem("subsystem", SCAN_DEVICES);
687                         exec_list(action, env);
688                 } else {
689                         scan_subsystem("bus", SCAN_SUBSYSTEM);
690                         exec_list(action, env);
691                         scan_subsystem("bus", SCAN_DEVICES);
692                         scan_class();
693
694                         /* scan "block" if it isn't a "class" */
695                         strlcpy(base, sysfs_path, sizeof(base));
696                         strlcat(base, "/class/block", sizeof(base));
697                         if (stat(base, &statbuf) != 0)
698                                 scan_block();
699                         exec_list(action, env);
700                 }
701         }
702
703 exit:
704         name_list_cleanup(&filter_subsystem_match_list);
705         name_list_cleanup(&filter_subsystem_nomatch_list);
706         name_list_cleanup(&filter_attr_match_list);
707         name_list_cleanup(&filter_attr_nomatch_list);
708
709         if (sock >= 0)
710                 close(sock);
711         sysfs_cleanup();
712         logging_close();
713         return 0;
714 }