chiark / gitweb /
fix compiler warning
[elogind.git] / src / libudev-enumerate.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <stddef.h>
15 #include <unistd.h>
16 #include <errno.h>
17 #include <string.h>
18 #include <dirent.h>
19 #include <fnmatch.h>
20 #include <stdbool.h>
21 #include <sys/stat.h>
22 #include <sys/param.h>
23
24 #include "libudev.h"
25 #include "libudev-private.h"
26
27 /**
28  * SECTION:libudev-enumerate
29  * @short_description: lookup and sort sys devices
30  *
31  * Lookup devices in the sys filesystem, filter devices by properties,
32  * and return a sorted list of devices.
33  */
34
35 struct syspath {
36         char *syspath;
37         size_t len;
38 };
39
40 /**
41  * udev_enumerate:
42  *
43  * Opaque object representing one device lookup/sort context.
44  */
45 struct udev_enumerate {
46         struct udev *udev;
47         int refcount;
48         struct udev_list sysattr_match_list;
49         struct udev_list sysattr_nomatch_list;
50         struct udev_list subsystem_match_list;
51         struct udev_list subsystem_nomatch_list;
52         struct udev_list sysname_match_list;
53         struct udev_list properties_match_list;
54         struct udev_list tags_match_list;
55         struct udev_device *parent_match;
56         struct udev_list devices_list;
57         struct syspath *devices;
58         unsigned int devices_cur;
59         unsigned int devices_max;
60         bool devices_uptodate:1;
61         bool match_is_initialized;
62 };
63
64 /**
65  * udev_enumerate_new:
66  * @udev: udev library context
67  *
68  * Returns: an enumeration context
69  **/
70 UDEV_EXPORT struct udev_enumerate *udev_enumerate_new(struct udev *udev)
71 {
72         struct udev_enumerate *udev_enumerate;
73
74         udev_enumerate = calloc(1, sizeof(struct udev_enumerate));
75         if (udev_enumerate == NULL)
76                 return NULL;
77         udev_enumerate->refcount = 1;
78         udev_enumerate->udev = udev;
79         udev_list_init(udev, &udev_enumerate->sysattr_match_list, false);
80         udev_list_init(udev, &udev_enumerate->sysattr_nomatch_list, false);
81         udev_list_init(udev, &udev_enumerate->subsystem_match_list, true);
82         udev_list_init(udev, &udev_enumerate->subsystem_nomatch_list, true);
83         udev_list_init(udev, &udev_enumerate->sysname_match_list, true);
84         udev_list_init(udev, &udev_enumerate->properties_match_list, false);
85         udev_list_init(udev, &udev_enumerate->tags_match_list, true);
86         udev_list_init(udev, &udev_enumerate->devices_list, false);
87         return udev_enumerate;
88 }
89
90 /**
91  * udev_enumerate_ref:
92  * @udev_enumerate: context
93  *
94  * Take a reference of a enumeration context.
95  *
96  * Returns: the passed enumeration context
97  **/
98 UDEV_EXPORT struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_enumerate)
99 {
100         if (udev_enumerate == NULL)
101                 return NULL;
102         udev_enumerate->refcount++;
103         return udev_enumerate;
104 }
105
106 /**
107  * udev_enumerate_unref:
108  * @udev_enumerate: context
109  *
110  * Drop a reference of an enumeration context. If the refcount reaches zero,
111  * all resources of the enumeration context will be released.
112  **/
113 UDEV_EXPORT void udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
114 {
115         unsigned int i;
116
117         if (udev_enumerate == NULL)
118                 return;
119         udev_enumerate->refcount--;
120         if (udev_enumerate->refcount > 0)
121                 return;
122         udev_list_cleanup(&udev_enumerate->sysattr_match_list);
123         udev_list_cleanup(&udev_enumerate->sysattr_nomatch_list);
124         udev_list_cleanup(&udev_enumerate->subsystem_match_list);
125         udev_list_cleanup(&udev_enumerate->subsystem_nomatch_list);
126         udev_list_cleanup(&udev_enumerate->sysname_match_list);
127         udev_list_cleanup(&udev_enumerate->properties_match_list);
128         udev_list_cleanup(&udev_enumerate->tags_match_list);
129         udev_device_unref(udev_enumerate->parent_match);
130         udev_list_cleanup(&udev_enumerate->devices_list);
131         for (i = 0; i < udev_enumerate->devices_cur; i++)
132                 free(udev_enumerate->devices[i].syspath);
133         free(udev_enumerate->devices);
134         free(udev_enumerate);
135 }
136
137 /**
138  * udev_enumerate_get_udev:
139  * @udev_enumerate: context
140  *
141  * Returns: the udev library context.
142  */
143 UDEV_EXPORT struct udev *udev_enumerate_get_udev(struct udev_enumerate *udev_enumerate)
144 {
145         if (udev_enumerate == NULL)
146                 return NULL;
147         return udev_enumerate->udev;
148 }
149
150 static int syspath_add(struct udev_enumerate *udev_enumerate, const char *syspath)
151 {
152         char *path;
153         struct syspath *entry;
154
155         /* double array size if needed */
156         if (udev_enumerate->devices_cur >= udev_enumerate->devices_max) {
157                 struct syspath *buf;
158                 unsigned int add;
159
160                 add = udev_enumerate->devices_max;
161                 if (add < 1024)
162                         add = 1024;
163                 buf = realloc(udev_enumerate->devices, (udev_enumerate->devices_max + add) * sizeof(struct syspath));
164                 if (buf == NULL)
165                         return -ENOMEM;
166                 udev_enumerate->devices = buf;
167                 udev_enumerate->devices_max += add;
168         }
169
170         path = strdup(syspath);
171         if (path == NULL)
172                 return -ENOMEM;
173         entry = &udev_enumerate->devices[udev_enumerate->devices_cur];
174         entry->syspath = path;
175         entry->len = strlen(path);
176         udev_enumerate->devices_cur++;
177         udev_enumerate->devices_uptodate = false;
178         return 0;
179 }
180
181 static int syspath_cmp(const void *p1, const void *p2)
182 {
183         const struct syspath *path1 = p1;
184         const struct syspath *path2 = p2;
185         size_t len;
186         int ret;
187
188         len = MIN(path1->len, path2->len);
189         ret = memcmp(path1->syspath, path2->syspath, len);
190         if (ret == 0) {
191                 if (path1->len < path2->len)
192                         ret = -1;
193                 else if (path1->len > path2->len)
194                         ret = 1;
195         }
196         return ret;
197 }
198
199 /* For devices that should be moved to the absolute end of the list */
200 static bool devices_delay_end(struct udev *udev, const char *syspath)
201 {
202         static const char *delay_device_list[] = {
203                 "/block/md",
204                 "/block/dm-",
205                 NULL
206         };
207         size_t len;
208         int i;
209
210         len = strlen(udev_get_sys_path(udev));
211         for (i = 0; delay_device_list[i] != NULL; i++) {
212                 if (strstr(&syspath[len], delay_device_list[i]) != NULL) {
213                         dbg(udev, "delaying: %s\n", syspath);
214                         return true;
215                 }
216         }
217         return false;
218 }
219
220 /* For devices that should just be moved a little bit later, just
221  * before the point where some common path prefix changes. Returns the
222  * number of characters that make up that common prefix */
223 static size_t devices_delay_later(struct udev *udev, const char *syspath)
224 {
225         const char *c;
226
227         /* For sound cards the control device must be enumerated last
228          * to make sure it's the final device node that gets ACLs
229          * applied. Applications rely on this fact and use ACL changes
230          * on the control node as an indicator that the ACL change of
231          * the entire sound card completed. The kernel makes this
232          * guarantee when creating those devices, and hence we should
233          * too when enumerating them. */
234
235         if ((c = strstr(syspath, "/sound/card"))) {
236                 c += 11;
237                 c += strcspn(c, "/");
238
239                 if (strncmp(c, "/controlC", 9) == 0)
240                         return c - syspath + 1;
241         }
242
243         return 0;
244 }
245
246 /**
247  * udev_enumerate_get_list_entry:
248  * @udev_enumerate: context
249  *
250  * Returns: the first entry of the sorted list of device paths.
251  */
252 UDEV_EXPORT struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
253 {
254         if (udev_enumerate == NULL)
255                 return NULL;
256         if (!udev_enumerate->devices_uptodate) {
257                 unsigned int i;
258                 unsigned int max;
259                 struct syspath *prev = NULL, *move_later = NULL;
260                 size_t move_later_prefix = 0;
261
262                 udev_list_cleanup(&udev_enumerate->devices_list);
263                 qsort(udev_enumerate->devices, udev_enumerate->devices_cur, sizeof(struct syspath), syspath_cmp);
264
265                 max = udev_enumerate->devices_cur;
266                 for (i = 0; i < max; i++) {
267                         struct syspath *entry = &udev_enumerate->devices[i];
268
269                         /* skip duplicated entries */
270                         if (prev != NULL &&
271                             entry->len == prev->len &&
272                             memcmp(entry->syspath, prev->syspath, entry->len) == 0)
273                                 continue;
274                         prev = entry;
275
276                         /* skip to be delayed devices, and add them to the end of the list */
277                         if (devices_delay_end(udev_enumerate->udev, entry->syspath)) {
278                                 syspath_add(udev_enumerate, entry->syspath);
279                                 /* need to update prev here for the case realloc() gives a different address */
280                                 prev = &udev_enumerate->devices[i];
281                                 continue;
282                         }
283
284                         /* skip to be delayed devices, and move the to
285                          * the point where the prefix changes. We can
286                          * only move one item at a time. */
287                         if (!move_later) {
288                                 move_later_prefix = devices_delay_later(udev_enumerate->udev, entry->syspath);
289
290                                 if (move_later_prefix > 0) {
291                                         move_later = entry;
292                                         continue;
293                                 }
294                         }
295
296                         if (move_later &&
297                             strncmp(entry->syspath, move_later->syspath, move_later_prefix) != 0) {
298
299                                 udev_list_entry_add(&udev_enumerate->devices_list, move_later->syspath, NULL);
300                                 move_later = NULL;
301                         }
302
303                         udev_list_entry_add(&udev_enumerate->devices_list, entry->syspath, NULL);
304                 }
305
306                 if (move_later)
307                         udev_list_entry_add(&udev_enumerate->devices_list, move_later->syspath, NULL);
308
309                 /* add and cleanup delayed devices from end of list */
310                 for (i = max; i < udev_enumerate->devices_cur; i++) {
311                         struct syspath *entry = &udev_enumerate->devices[i];
312
313                         udev_list_entry_add(&udev_enumerate->devices_list, entry->syspath, NULL);
314                         free(entry->syspath);
315                 }
316                 udev_enumerate->devices_cur = max;
317
318                 udev_enumerate->devices_uptodate = true;
319         }
320         return udev_list_get_entry(&udev_enumerate->devices_list);
321 }
322
323 /**
324  * udev_enumerate_add_match_subsystem:
325  * @udev_enumerate: context
326  * @subsystem: filter for a subsystem of the device to include in the list
327  *
328  * Returns: 0 on success, otherwise a negative error value.
329  */
330 UDEV_EXPORT int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
331 {
332         if (udev_enumerate == NULL)
333                 return -EINVAL;
334         if (subsystem == NULL)
335                 return 0;
336         if (udev_list_entry_add(&udev_enumerate->subsystem_match_list, subsystem, NULL) == NULL)
337                 return -ENOMEM;
338         return 0;
339 }
340
341 /**
342  * udev_enumerate_add_nomatch_subsystem:
343  * @udev_enumerate: context
344  * @subsystem: filter for a subsystem of the device to exclude from the list
345  *
346  * Returns: 0 on success, otherwise a negative error value.
347  */
348 UDEV_EXPORT int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
349 {
350         if (udev_enumerate == NULL)
351                 return -EINVAL;
352         if (subsystem == NULL)
353                 return 0;
354         if (udev_list_entry_add(&udev_enumerate->subsystem_nomatch_list, subsystem, NULL) == NULL)
355                 return -ENOMEM;
356         return 0;
357 }
358
359 /**
360  * udev_enumerate_add_match_sysattr:
361  * @udev_enumerate: context
362  * @sysattr: filter for a sys attribute at the device to include in the list
363  * @value: optional value of the sys attribute
364  *
365  * Returns: 0 on success, otherwise a negative error value.
366  */
367 UDEV_EXPORT int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
368 {
369         if (udev_enumerate == NULL)
370                 return -EINVAL;
371         if (sysattr == NULL)
372                 return 0;
373         if (udev_list_entry_add(&udev_enumerate->sysattr_match_list, sysattr, value) == NULL)
374                 return -ENOMEM;
375         return 0;
376 }
377
378 /**
379  * udev_enumerate_add_nomatch_sysattr:
380  * @udev_enumerate: context
381  * @sysattr: filter for a sys attribute at the device to exclude from the list
382  * @value: optional value of the sys attribute
383  *
384  * Returns: 0 on success, otherwise a negative error value.
385  */
386 UDEV_EXPORT int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
387 {
388         if (udev_enumerate == NULL)
389                 return -EINVAL;
390         if (sysattr == NULL)
391                 return 0;
392         if (udev_list_entry_add(&udev_enumerate->sysattr_nomatch_list, sysattr, value) == NULL)
393                 return -ENOMEM;
394         return 0;
395 }
396
397 static int match_sysattr_value(struct udev_device *dev, const char *sysattr, const char *match_val)
398 {
399         const char *val = NULL;
400         bool match = false;
401
402         val = udev_device_get_sysattr_value(dev, sysattr);
403         if (val == NULL)
404                 goto exit;
405         if (match_val == NULL) {
406                 match = true;
407                 goto exit;
408         }
409         if (fnmatch(match_val, val, 0) == 0) {
410                 match = true;
411                 goto exit;
412         }
413 exit:
414         return match;
415 }
416
417 /**
418  * udev_enumerate_add_match_property:
419  * @udev_enumerate: context
420  * @property: filter for a property of the device to include in the list
421  * @value: value of the property
422  *
423  * Returns: 0 on success, otherwise a negative error value.
424  */
425 UDEV_EXPORT int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value)
426 {
427         if (udev_enumerate == NULL)
428                 return -EINVAL;
429         if (property == NULL)
430                 return 0;
431         if (udev_list_entry_add(&udev_enumerate->properties_match_list, property, value) == NULL)
432                 return -ENOMEM;
433         return 0;
434 }
435
436 /**
437  * udev_enumerate_add_match_tag:
438  * @udev_enumerate: context
439  * @tag: filter for a tag of the device to include in the list
440  *
441  * Returns: 0 on success, otherwise a negative error value.
442  */
443 UDEV_EXPORT int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, const char *tag)
444 {
445         if (udev_enumerate == NULL)
446                 return -EINVAL;
447         if (tag == NULL)
448                 return 0;
449         if (udev_list_entry_add(&udev_enumerate->tags_match_list, tag, NULL) == NULL)
450                 return -ENOMEM;
451         return 0;
452 }
453
454 /**
455  * udev_enumerate_add_match_parent:
456  * @udev_enumerate: context
457  * @parent: parent device where to start searching
458  *
459  * Return the devices on the subtree of one given device. The parent
460  * itself is included in the list.
461  *
462  * A reference for the device is held until the udev_enumerate context
463  * is cleaned up.
464  *
465  * Returns: 0 on success, otherwise a negative error value.
466  */
467 UDEV_EXPORT int udev_enumerate_add_match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *parent)
468 {
469         if (udev_enumerate == NULL)
470                 return -EINVAL;
471         if (parent == NULL)
472                 return 0;
473         if (udev_enumerate->parent_match != NULL)
474                 udev_device_unref(udev_enumerate->parent_match);
475         udev_enumerate->parent_match = udev_device_ref(parent);
476         return 0;
477 }
478
479 /**
480  * udev_enumerate_add_match_is_initialized:
481  * @udev_enumerate: context
482  *
483  * Match only devices which udev has set up already. This makes
484  * sure, that the device node permissions and context are properly set
485  * and that network devices are fully renamed.
486  *
487  * Usually, devices which are found in the kernel but not already
488  * handled by udev, have still pending events. Services should subscribe
489  * to monitor events and wait for these devices to become ready, instead
490  * of using uninitialized devices.
491  *
492  * For now, this will not affect devices which do not have a device node
493  * and are not network interfaces.
494  *
495  * Returns: 0 on success, otherwise a negative error value.
496  */
497 UDEV_EXPORT int udev_enumerate_add_match_is_initialized(struct udev_enumerate *udev_enumerate)
498 {
499         if (udev_enumerate == NULL)
500                 return -EINVAL;
501         udev_enumerate->match_is_initialized = true;
502         return 0;
503 }
504
505 /**
506  * udev_enumerate_add_match_sysname:
507  * @udev_enumerate: context
508  * @sysname: filter for the name of the device to include in the list
509  *
510  * Returns: 0 on success, otherwise a negative error value.
511  */
512 UDEV_EXPORT int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
513 {
514         if (udev_enumerate == NULL)
515                 return -EINVAL;
516         if (sysname == NULL)
517                 return 0;
518         if (udev_list_entry_add(&udev_enumerate->sysname_match_list, sysname, NULL) == NULL)
519                 return -ENOMEM;
520         return 0;
521 }
522
523 static bool match_sysattr(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
524 {
525         struct udev_list_entry *list_entry;
526
527         /* skip list */
528         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysattr_nomatch_list)) {
529                 if (match_sysattr_value(dev, udev_list_entry_get_name(list_entry),
530                                         udev_list_entry_get_value(list_entry)))
531                         return false;
532         }
533         /* include list */
534         if (udev_list_get_entry(&udev_enumerate->sysattr_match_list) != NULL) {
535                 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysattr_match_list)) {
536                         /* anything that does not match, will make it FALSE */
537                         if (!match_sysattr_value(dev, udev_list_entry_get_name(list_entry),
538                                                  udev_list_entry_get_value(list_entry)))
539                                 return false;
540                 }
541                 return true;
542         }
543         return true;
544 }
545
546 static bool match_property(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
547 {
548         struct udev_list_entry *list_entry;
549         bool match = false;
550
551         /* no match always matches */
552         if (udev_list_get_entry(&udev_enumerate->properties_match_list) == NULL)
553                 return true;
554
555         /* loop over matches */
556         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->properties_match_list)) {
557                 const char *match_key = udev_list_entry_get_name(list_entry);
558                 const char *match_value = udev_list_entry_get_value(list_entry);
559                 struct udev_list_entry *property_entry;
560
561                 /* loop over device properties */
562                 udev_list_entry_foreach(property_entry, udev_device_get_properties_list_entry(dev)) {
563                         const char *dev_key = udev_list_entry_get_name(property_entry);
564                         const char *dev_value = udev_list_entry_get_value(property_entry);
565
566                         if (fnmatch(match_key, dev_key, 0) != 0)
567                                 continue;
568                         if (match_value == NULL && dev_value == NULL) {
569                                 match = true;
570                                 goto out;
571                         }
572                         if (match_value == NULL || dev_value == NULL)
573                                 continue;
574                         if (fnmatch(match_value, dev_value, 0) == 0) {
575                                 match = true;
576                                 goto out;
577                         }
578                 }
579         }
580 out:
581         return match;
582 }
583
584 static bool match_tag(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
585 {
586         struct udev_list_entry *list_entry;
587
588         /* no match always matches */
589         if (udev_list_get_entry(&udev_enumerate->tags_match_list) == NULL)
590                 return true;
591
592         /* loop over matches */
593         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->tags_match_list))
594                 if (!udev_device_has_tag(dev, udev_list_entry_get_name(list_entry)))
595                         return false;
596
597         return true;
598 }
599
600 static bool match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
601 {
602         const char *parent;
603
604         if (udev_enumerate->parent_match == NULL)
605                 return true;
606
607         parent = udev_device_get_devpath(udev_enumerate->parent_match);
608         return strncmp(parent, udev_device_get_devpath(dev), strlen(parent)) == 0;
609 }
610
611 static bool match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
612 {
613         struct udev_list_entry *list_entry;
614
615         if (udev_list_get_entry(&udev_enumerate->sysname_match_list) == NULL)
616                 return true;
617
618         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->sysname_match_list)) {
619                 if (fnmatch(udev_list_entry_get_name(list_entry), sysname, 0) != 0)
620                         continue;
621                 return true;
622         }
623         return false;
624 }
625
626 static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
627                                     const char *basedir, const char *subdir1, const char *subdir2)
628 {
629         struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
630         char path[UTIL_PATH_SIZE];
631         size_t l;
632         char *s;
633         DIR *dir;
634         struct dirent *dent;
635
636         s = path;
637         l = util_strpcpyl(&s, sizeof(path), udev_get_sys_path(udev), "/", basedir, NULL);
638         if (subdir1 != NULL)
639                 l = util_strpcpyl(&s, l, "/", subdir1, NULL);
640         if (subdir2 != NULL)
641                 util_strpcpyl(&s, l, "/", subdir2, NULL);
642         dir = opendir(path);
643         if (dir == NULL)
644                 return -ENOENT;
645         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
646                 char syspath[UTIL_PATH_SIZE];
647                 struct udev_device *dev;
648
649                 if (dent->d_name[0] == '.')
650                         continue;
651
652                 if (!match_sysname(udev_enumerate, dent->d_name))
653                         continue;
654
655                 util_strscpyl(syspath, sizeof(syspath), path, "/", dent->d_name, NULL);
656                 dev = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
657                 if (dev == NULL)
658                         continue;
659
660                 if (udev_enumerate->match_is_initialized) {
661                         /*
662                          * All devices with a device node or network interfaces
663                          * possibly need udev to adjust the device node permission
664                          * or context, or rename the interface before it can be
665                          * reliably used from other processes.
666                          *
667                          * For now, we can only check these types of devices, we
668                          * might not store a database, and have no way to find out
669                          * for all other types of devices.
670                          */
671                         if (!udev_device_get_is_initialized(dev) &&
672                             (major(udev_device_get_devnum(dev)) > 0 || udev_device_get_ifindex(dev) > 0))
673                                 goto nomatch;
674                 }
675                 if (!match_parent(udev_enumerate, dev))
676                         goto nomatch;
677                 if (!match_tag(udev_enumerate, dev))
678                         goto nomatch;
679                 if (!match_property(udev_enumerate, dev))
680                         goto nomatch;
681                 if (!match_sysattr(udev_enumerate, dev))
682                         goto nomatch;
683
684                 syspath_add(udev_enumerate, udev_device_get_syspath(dev));
685 nomatch:
686                 udev_device_unref(dev);
687         }
688         closedir(dir);
689         return 0;
690 }
691
692 static bool match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
693 {
694         struct udev_list_entry *list_entry;
695
696         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_nomatch_list)) {
697                 if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
698                         return false;
699         }
700         if (udev_list_get_entry(&udev_enumerate->subsystem_match_list) != NULL) {
701                 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_match_list)) {
702                         if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
703                                 return true;
704                 }
705                 return false;
706         }
707         return true;
708 }
709
710 static int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir, const char *subdir, const char *subsystem)
711 {
712         struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
713
714         char path[UTIL_PATH_SIZE];
715         DIR *dir;
716         struct dirent *dent;
717
718         util_strscpyl(path, sizeof(path), udev_get_sys_path(udev), "/", basedir, NULL);
719         dir = opendir(path);
720         if (dir == NULL)
721                 return -1;
722         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
723                 if (dent->d_name[0] == '.')
724                         continue;
725                 if (!match_subsystem(udev_enumerate, subsystem != NULL ? subsystem : dent->d_name))
726                         continue;
727                 scan_dir_and_add_devices(udev_enumerate, basedir, dent->d_name, subdir);
728         }
729         closedir(dir);
730         return 0;
731 }
732
733 /**
734  * udev_enumerate_add_syspath:
735  * @udev_enumerate: context
736  * @syspath: path of a device
737  *
738  * Add a device to the list of devices, to retrieve it back sorted in dependency order.
739  *
740  * Returns: 0 on success, otherwise a negative error value.
741  */
742 UDEV_EXPORT int udev_enumerate_add_syspath(struct udev_enumerate *udev_enumerate, const char *syspath)
743 {
744         struct udev_device *udev_device;
745
746         if (udev_enumerate == NULL)
747                 return -EINVAL;
748         if (syspath == NULL)
749                 return 0;
750         /* resolve to real syspath */
751         udev_device = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
752         if (udev_device == NULL)
753                 return -EINVAL;
754         syspath_add(udev_enumerate, udev_device_get_syspath(udev_device));
755         udev_device_unref(udev_device);
756         return 0;
757 }
758
759 static int scan_devices_tags(struct udev_enumerate *udev_enumerate)
760 {
761         struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
762         struct udev_list_entry *list_entry;
763
764         /* scan only tagged devices, use tags reverse-index, instead of searching all devices in /sys */
765         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->tags_match_list)) {
766                 DIR *dir;
767                 struct dirent *dent;
768                 char path[UTIL_PATH_SIZE];
769
770                 util_strscpyl(path, sizeof(path), udev_get_run_path(udev), "/tags/",
771                               udev_list_entry_get_name(list_entry), NULL);
772                 dir = opendir(path);
773                 if (dir == NULL)
774                         continue;
775                 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
776                         struct udev_device *dev;
777
778                         if (dent->d_name[0] == '.')
779                                 continue;
780
781                         dev = udev_device_new_from_id_filename(udev_enumerate->udev, dent->d_name);
782                         if (dev == NULL)
783                                 continue;
784
785                         if (!match_subsystem(udev_enumerate, udev_device_get_subsystem(dev)))
786                                 goto nomatch;
787                         if (!match_sysname(udev_enumerate, udev_device_get_sysname(dev)))
788                                 goto nomatch;
789                         if (!match_parent(udev_enumerate, dev))
790                                 goto nomatch;
791                         if (!match_property(udev_enumerate, dev))
792                                 goto nomatch;
793                         if (!match_sysattr(udev_enumerate, dev))
794                                 goto nomatch;
795
796                         syspath_add(udev_enumerate, udev_device_get_syspath(dev));
797 nomatch:
798                         udev_device_unref(dev);
799                 }
800                 closedir(dir);
801         }
802         return 0;
803 }
804
805 static int parent_add_child(struct udev_enumerate *enumerate, const char *path)
806 {
807         struct udev_device *dev;
808
809         dev = udev_device_new_from_syspath(enumerate->udev, path);
810         if (dev == NULL)
811                 return -ENODEV;
812
813         if (!match_subsystem(enumerate, udev_device_get_subsystem(dev)))
814                 return 0;
815         if (!match_sysname(enumerate, udev_device_get_sysname(dev)))
816                 return 0;
817         if (!match_property(enumerate, dev))
818                 return 0;
819         if (!match_sysattr(enumerate, dev))
820                 return 0;
821
822         syspath_add(enumerate, udev_device_get_syspath(dev));
823         udev_device_unref(dev);
824         return 1;
825 }
826
827 static int parent_crawl_children(struct udev_enumerate *enumerate, const char *path, int maxdepth)
828 {
829         DIR *d;
830         struct dirent *dent;
831
832         d = opendir(path);
833         if (d == NULL)
834                 return -errno;
835
836         for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
837                 char *child;
838
839                 if (dent->d_name[0] == '.')
840                         continue;
841                 if (dent->d_type != DT_DIR)
842                         continue;
843                 if (asprintf(&child, "%s/%s", path, dent->d_name) < 0)
844                         continue;
845                 parent_add_child(enumerate, child);
846                 if (maxdepth > 0)
847                         parent_crawl_children(enumerate, child, maxdepth-1);
848                 free(child);
849         }
850
851         closedir(d);
852         return 0;
853 }
854
855 static int scan_devices_children(struct udev_enumerate *enumerate)
856 {
857         const char *path;
858
859         path = udev_device_get_syspath(enumerate->parent_match);
860         parent_add_child(enumerate, path);
861         return parent_crawl_children(enumerate, path, 256);
862 }
863
864 static int scan_devices_all(struct udev_enumerate *udev_enumerate)
865 {
866         struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
867         char base[UTIL_PATH_SIZE];
868         struct stat statbuf;
869
870         util_strscpyl(base, sizeof(base), udev_get_sys_path(udev), "/subsystem", NULL);
871         if (stat(base, &statbuf) == 0) {
872                 /* we have /subsystem/, forget all the old stuff */
873                 dbg(udev, "searching '/subsystem/*/devices/*' dir\n");
874                 scan_dir(udev_enumerate, "subsystem", "devices", NULL);
875         } else {
876                 dbg(udev, "searching '/bus/*/devices/*' dir\n");
877                 scan_dir(udev_enumerate, "bus", "devices", NULL);
878                 dbg(udev, "searching '/class/*' dir\n");
879                 scan_dir(udev_enumerate, "class", NULL, NULL);
880         }
881         return 0;
882 }
883
884 /**
885  * udev_enumerate_scan_devices:
886  * @udev_enumerate: udev enumeration context
887  *
888  * Returns: 0 on success, otherwise a negative error value.
889  **/
890 UDEV_EXPORT int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
891 {
892         if (udev_enumerate == NULL)
893                 return -EINVAL;
894
895         /* efficiently lookup tags only, we maintain a reverse-index */
896         if (udev_list_get_entry(&udev_enumerate->tags_match_list) != NULL)
897                 return scan_devices_tags(udev_enumerate);
898
899         /* walk the subtree of one parent device only */
900         if (udev_enumerate->parent_match != NULL)
901                 return scan_devices_children(udev_enumerate);
902
903         /* scan devices of all subsystems */
904         return scan_devices_all(udev_enumerate);
905 }
906
907 /**
908  * udev_enumerate_scan_subsystems:
909  * @udev_enumerate: udev enumeration context
910  *
911  * Returns: 0 on success, otherwise a negative error value.
912  **/
913 UDEV_EXPORT int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate)
914 {
915         struct udev *udev = udev_enumerate_get_udev(udev_enumerate);
916         char base[UTIL_PATH_SIZE];
917         struct stat statbuf;
918         const char *subsysdir;
919
920         if (udev_enumerate == NULL)
921                 return -EINVAL;
922
923         /* all kernel modules */
924         if (match_subsystem(udev_enumerate, "module")) {
925                 dbg(udev, "searching 'modules/*' dir\n");
926                 scan_dir_and_add_devices(udev_enumerate, "module", NULL, NULL);
927         }
928
929         util_strscpyl(base, sizeof(base), udev_get_sys_path(udev), "/subsystem", NULL);
930         if (stat(base, &statbuf) == 0)
931                 subsysdir = "subsystem";
932         else
933                 subsysdir = "bus";
934
935         /* all subsystems (only buses support coldplug) */
936         if (match_subsystem(udev_enumerate, "subsystem")) {
937                 dbg(udev, "searching '%s/*' dir\n", subsysdir);
938                 scan_dir_and_add_devices(udev_enumerate, subsysdir, NULL, NULL);
939         }
940
941         /* all subsystem drivers */
942         if (match_subsystem(udev_enumerate, "drivers")) {
943                 dbg(udev, "searching '%s/*/drivers/*' dir\n", subsysdir);
944                 scan_dir(udev_enumerate, subsysdir, "drivers", "drivers");
945         }
946         return 0;
947 }