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