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