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