]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/libudev/libudev-enumerate.c
libudev: always return NULL in _unref() APIs
[thirdparty/systemd.git] / src / libudev / libudev-enumerate.c
CommitLineData
88a6477e
KS
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***/
eb1f0e66 19
eb1f0e66
KS
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>
c97f839e 27#include <fnmatch.h>
871a36bd 28#include <stdbool.h>
eb1f0e66 29#include <sys/stat.h>
871a36bd 30#include <sys/param.h>
eb1f0e66
KS
31
32#include "libudev.h"
33#include "libudev-private.h"
eb1f0e66 34
ce1d6d7f
KS
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,
a7c140c7 40 * and return a sorted list of devices.
ce1d6d7f
KS
41 */
42
871a36bd 43struct syspath {
912541b0
KS
44 char *syspath;
45 size_t len;
871a36bd
KS
46};
47
ce1d6d7f
KS
48/**
49 * udev_enumerate:
50 *
51 * Opaque object representing one device lookup/sort context.
52 */
bf7ad0ea 53struct udev_enumerate {
912541b0
KS
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;
bf7ad0ea
KS
70};
71
c97f839e
KS
72/**
73 * udev_enumerate_new:
74 * @udev: udev library context
75 *
21dbe43a
KS
76 * Create an enumeration context to scan /sys.
77 *
78 * Returns: an enumeration context.
c97f839e 79 **/
54cf0b7f 80_public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev)
c97f839e 81{
912541b0 82 struct udev_enumerate *udev_enumerate;
c97f839e 83
e6889307
MT
84 if (udev == NULL)
85 return NULL;
912541b0
KS
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;
c97f839e
KS
100}
101
ce1d6d7f
KS
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 **/
54cf0b7f 110_public_ struct udev_enumerate *udev_enumerate_ref(struct udev_enumerate *udev_enumerate)
bf7ad0ea 111{
912541b0
KS
112 if (udev_enumerate == NULL)
113 return NULL;
114 udev_enumerate->refcount++;
115 return udev_enumerate;
bf7ad0ea
KS
116}
117
ce1d6d7f
KS
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.
c1959569 124 *
725d7e6c 125 * Returns: #NULL
ce1d6d7f 126 **/
20bbd54f 127_public_ struct udev_enumerate *udev_enumerate_unref(struct udev_enumerate *udev_enumerate)
bf7ad0ea 128{
912541b0
KS
129 unsigned int i;
130
131 if (udev_enumerate == NULL)
20bbd54f 132 return NULL;
912541b0
KS
133 udev_enumerate->refcount--;
134 if (udev_enumerate->refcount > 0)
725d7e6c 135 return NULL;
912541b0
KS
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);
20bbd54f 149 return NULL;
bf7ad0ea
KS
150}
151
ce1d6d7f
KS
152/**
153 * udev_enumerate_get_udev:
154 * @udev_enumerate: context
155 *
21dbe43a
KS
156 * Get the udev library context.
157 *
158 * Returns: a pointer to the context.
ce1d6d7f 159 */
54cf0b7f 160_public_ struct udev *udev_enumerate_get_udev(struct udev_enumerate *udev_enumerate)
bc8184ed 161{
912541b0
KS
162 if (udev_enumerate == NULL)
163 return NULL;
164 return udev_enumerate->udev;
bc8184ed
KS
165}
166
871a36bd
KS
167static int syspath_add(struct udev_enumerate *udev_enumerate, const char *syspath)
168{
912541b0
KS
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;
871a36bd
KS
196}
197
198static int syspath_cmp(const void *p1, const void *p2)
199{
912541b0
KS
200 const struct syspath *path1 = p1;
201 const struct syspath *path2 = p2;
202 size_t len;
203 int ret;
871a36bd 204
912541b0
KS
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;
871a36bd
KS
214}
215
3bf76824 216/* For devices that should be moved to the absolute end of the list */
28460195 217static bool devices_delay_end(struct udev *udev, const char *syspath)
871a36bd 218{
912541b0
KS
219 static const char *delay_device_list[] = {
220 "/block/md",
221 "/block/dm-",
222 NULL
223 };
912541b0 224 int i;
871a36bd 225
912541b0 226 for (i = 0; delay_device_list[i] != NULL; i++) {
6ada823a 227 if (strstr(syspath + strlen("/sys"), delay_device_list[i]) != NULL)
912541b0 228 return true;
912541b0
KS
229 }
230 return false;
871a36bd
KS
231}
232
3bf76824
LP
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 */
236static size_t devices_delay_later(struct udev *udev, const char *syspath)
237{
912541b0 238 const char *c;
3bf76824 239
912541b0
KS
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. */
3bf76824 247
912541b0
KS
248 if ((c = strstr(syspath, "/sound/card"))) {
249 c += 11;
250 c += strcspn(c, "/");
3bf76824 251
33502ffe 252 if (startswith(c, "/controlC"))
912541b0
KS
253 return c - syspath + 1;
254 }
3bf76824 255
912541b0 256 return 0;
3bf76824
LP
257}
258
a7c140c7
KS
259/**
260 * udev_enumerate_get_list_entry:
261 * @udev_enumerate: context
262 *
21dbe43a
KS
263 * Get the first entry of the sorted list of device paths.
264 *
265 * Returns: a udev_list_entry.
a7c140c7 266 */
54cf0b7f 267_public_ struct udev_list_entry *udev_enumerate_get_list_entry(struct udev_enumerate *udev_enumerate)
bf7ad0ea 268{
912541b0
KS
269 if (udev_enumerate == NULL)
270 return NULL;
271 if (!udev_enumerate->devices_uptodate) {
272 unsigned int i;
763a24a3 273 int move_later = -1;
912541b0 274 unsigned int max;
763a24a3 275 struct syspath *prev = NULL;
912541b0
KS
276 size_t move_later_prefix = 0;
277
278 udev_list_cleanup(&udev_enumerate->devices_list);
7ff7394d 279 qsort_safe(udev_enumerate->devices, udev_enumerate->devices_cur, sizeof(struct syspath), syspath_cmp);
912541b0
KS
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. */
6faa3dcb 303 if (move_later == -1) {
912541b0
KS
304 move_later_prefix = devices_delay_later(udev_enumerate->udev, entry->syspath);
305
306 if (move_later_prefix > 0) {
763a24a3 307 move_later = i;
912541b0
KS
308 continue;
309 }
310 }
311
763a24a3
HH
312 if ((move_later >= 0) &&
313 !strneq(entry->syspath, udev_enumerate->devices[move_later].syspath, move_later_prefix)) {
912541b0 314
763a24a3
HH
315 udev_list_entry_add(&udev_enumerate->devices_list,
316 udev_enumerate->devices[move_later].syspath, NULL);
317 move_later = -1;
912541b0
KS
318 }
319
320 udev_list_entry_add(&udev_enumerate->devices_list, entry->syspath, NULL);
321 }
322
763a24a3
HH
323 if (move_later >= 0)
324 udev_list_entry_add(&udev_enumerate->devices_list,
325 udev_enumerate->devices[move_later].syspath, NULL);
912541b0
KS
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);
bf7ad0ea
KS
339}
340
a7c140c7
KS
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 *
21dbe43a
KS
346 * Match only devices belonging to a certain kernel subsystem.
347 *
a7c140c7
KS
348 * Returns: 0 on success, otherwise a negative error value.
349 */
54cf0b7f 350_public_ int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
eb1f0e66 351{
912541b0
KS
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;
c97f839e
KS
359}
360
a7c140c7
KS
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 *
21dbe43a
KS
366 * Match only devices not belonging to a certain kernel subsystem.
367 *
a7c140c7
KS
368 * Returns: 0 on success, otherwise a negative error value.
369 */
54cf0b7f 370_public_ int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
c97f839e 371{
912541b0
KS
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;
c97f839e
KS
379}
380
a7c140c7
KS
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 *
21dbe43a
KS
387 * Match only devices with a certain /sys device attribute.
388 *
a7c140c7
KS
389 * Returns: 0 on success, otherwise a negative error value.
390 */
54cf0b7f 391_public_ int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
c97f839e 392{
912541b0
KS
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;
c97f839e
KS
400}
401
a7c140c7
KS
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 *
21dbe43a
KS
408 * Match only devices not having a certain /sys device attribute.
409 *
a7c140c7
KS
410 * Returns: 0 on success, otherwise a negative error value.
411 */
54cf0b7f 412_public_ int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
c97f839e 413{
912541b0
KS
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;
c97f839e
KS
421}
422
28460195 423static int match_sysattr_value(struct udev_device *dev, const char *sysattr, const char *match_val)
c97f839e 424{
912541b0
KS
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 }
c97f839e 439exit:
912541b0 440 return match;
c97f839e
KS
441}
442
a7c140c7
KS
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 *
21dbe43a
KS
449 * Match only devices with a certain property.
450 *
a7c140c7
KS
451 * Returns: 0 on success, otherwise a negative error value.
452 */
54cf0b7f 453_public_ int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value)
f0893502 454{
912541b0
KS
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;
f0893502
KS
462}
463
28460195
KS
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 *
21dbe43a
KS
469 * Match only devices with a certain tag.
470 *
28460195
KS
471 * Returns: 0 on success, otherwise a negative error value.
472 */
54cf0b7f 473_public_ int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, const char *tag)
28460195 474{
912541b0
KS
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;
28460195
KS
482}
483
b05211fa
KS
484/**
485 * udev_enumerate_add_match_parent:
486 * @udev_enumerate: context
a07e0114 487 * @parent: parent device where to start searching
b05211fa 488 *
a07e0114
KS
489 * Return the devices on the subtree of one given device. The parent
490 * itself is included in the list.
b05211fa
KS
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 */
54cf0b7f 497_public_ int udev_enumerate_add_match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *parent)
b05211fa 498{
912541b0
KS
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;
b05211fa
KS
507}
508
48a0170b
KS
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 */
54cf0b7f 527_public_ int udev_enumerate_add_match_is_initialized(struct udev_enumerate *udev_enumerate)
48a0170b 528{
912541b0
KS
529 if (udev_enumerate == NULL)
530 return -EINVAL;
531 udev_enumerate->match_is_initialized = true;
532 return 0;
48a0170b
KS
533}
534
cf5bd040
KS
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 *
21dbe43a
KS
540 * Match only devices with a given /sys device name.
541 *
cf5bd040
KS
542 * Returns: 0 on success, otherwise a negative error value.
543 */
54cf0b7f 544_public_ int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
cf5bd040 545{
912541b0
KS
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;
cf5bd040
KS
553}
554
28460195 555static bool match_sysattr(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
c97f839e 556{
912541b0
KS
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;
c97f839e
KS
576}
577
28460195 578static bool match_property(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
f0893502 579{
912541b0
KS
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 }
f0893502 612out:
912541b0 613 return match;
f0893502
KS
614}
615
28460195
KS
616static bool match_tag(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
617{
912541b0 618 struct udev_list_entry *list_entry;
28460195 619
912541b0
KS
620 /* no match always matches */
621 if (udev_list_get_entry(&udev_enumerate->tags_match_list) == NULL)
622 return true;
28460195 623
912541b0
KS
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;
28460195 628
912541b0 629 return true;
28460195
KS
630}
631
b05211fa
KS
632static bool match_parent(struct udev_enumerate *udev_enumerate, struct udev_device *dev)
633{
912541b0
KS
634 if (udev_enumerate->parent_match == NULL)
635 return true;
b05211fa 636
33502ffe 637 return startswith(udev_device_get_devpath(dev), udev_device_get_devpath(udev_enumerate->parent_match));
b05211fa
KS
638}
639
28460195 640static bool match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
cf5bd040 641{
912541b0 642 struct udev_list_entry *list_entry;
cf5bd040 643
912541b0
KS
644 if (udev_list_get_entry(&udev_enumerate->sysname_match_list) == NULL)
645 return true;
cf5bd040 646
912541b0
KS
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;
cf5bd040
KS
653}
654
cabfd8d0 655static int scan_dir_and_add_devices(struct udev_enumerate *udev_enumerate,
912541b0
KS
656 const char *basedir, const char *subdir1, const char *subdir2)
657{
912541b0
KS
658 char path[UTIL_PATH_SIZE];
659 size_t l;
660 char *s;
661 DIR *dir;
662 struct dirent *dent;
663
664 s = path;
d5a89d7d 665 l = strpcpyl(&s, sizeof(path), "/sys/", basedir, NULL);
912541b0 666 if (subdir1 != NULL)
d5a89d7d 667 l = strpcpyl(&s, l, "/", subdir1, NULL);
912541b0 668 if (subdir2 != NULL)
d5a89d7d 669 strpcpyl(&s, l, "/", subdir2, NULL);
912541b0
KS
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
d5a89d7d 683 strscpyl(syspath, sizeof(syspath), path, "/", dent->d_name, NULL);
912541b0
KS
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));
28460195 713nomatch:
912541b0
KS
714 udev_device_unref(dev);
715 }
716 closedir(dir);
717 return 0;
eb1f0e66
KS
718}
719
28460195 720static bool match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
eb1f0e66 721{
912541b0 722 struct udev_list_entry *list_entry;
c97f839e 723
756c9a24
KS
724 if (!subsystem)
725 return false;
f9e84da6 726
912541b0
KS
727 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_nomatch_list)) {
728 if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
729 return false;
730 }
756c9a24 731
912541b0
KS
732 if (udev_list_get_entry(&udev_enumerate->subsystem_match_list) != NULL) {
733 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->subsystem_match_list)) {
734 if (fnmatch(udev_list_entry_get_name(list_entry), subsystem, 0) == 0)
735 return true;
736 }
737 return false;
738 }
756c9a24 739
912541b0 740 return true;
c97f839e
KS
741}
742
cabfd8d0 743static int scan_dir(struct udev_enumerate *udev_enumerate, const char *basedir, const char *subdir, const char *subsystem)
c97f839e 744{
912541b0
KS
745 char path[UTIL_PATH_SIZE];
746 DIR *dir;
747 struct dirent *dent;
c97f839e 748
d5a89d7d 749 strscpyl(path, sizeof(path), "/sys/", basedir, NULL);
912541b0
KS
750 dir = opendir(path);
751 if (dir == NULL)
752 return -1;
753 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
754 if (dent->d_name[0] == '.')
755 continue;
756 if (!match_subsystem(udev_enumerate, subsystem != NULL ? subsystem : dent->d_name))
757 continue;
758 scan_dir_and_add_devices(udev_enumerate, basedir, dent->d_name, subdir);
759 }
760 closedir(dir);
761 return 0;
eb1f0e66
KS
762}
763
a7c140c7
KS
764/**
765 * udev_enumerate_add_syspath:
766 * @udev_enumerate: context
767 * @syspath: path of a device
768 *
769 * Add a device to the list of devices, to retrieve it back sorted in dependency order.
770 *
771 * Returns: 0 on success, otherwise a negative error value.
772 */
54cf0b7f 773_public_ int udev_enumerate_add_syspath(struct udev_enumerate *udev_enumerate, const char *syspath)
13ddea81 774{
912541b0 775 struct udev_device *udev_device;
13ddea81 776
912541b0
KS
777 if (udev_enumerate == NULL)
778 return -EINVAL;
779 if (syspath == NULL)
780 return 0;
781 /* resolve to real syspath */
782 udev_device = udev_device_new_from_syspath(udev_enumerate->udev, syspath);
783 if (udev_device == NULL)
784 return -EINVAL;
785 syspath_add(udev_enumerate, udev_device_get_syspath(udev_device));
786 udev_device_unref(udev_device);
787 return 0;
6f67f1df
KS
788}
789
b05211fa 790static int scan_devices_tags(struct udev_enumerate *udev_enumerate)
eb1f0e66 791{
912541b0
KS
792 struct udev_list_entry *list_entry;
793
794 /* scan only tagged devices, use tags reverse-index, instead of searching all devices in /sys */
795 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_enumerate->tags_match_list)) {
796 DIR *dir;
797 struct dirent *dent;
798 char path[UTIL_PATH_SIZE];
799
d5a89d7d 800 strscpyl(path, sizeof(path), "/run/udev/tags/", udev_list_entry_get_name(list_entry), NULL);
912541b0
KS
801 dir = opendir(path);
802 if (dir == NULL)
803 continue;
804 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
805 struct udev_device *dev;
806
807 if (dent->d_name[0] == '.')
808 continue;
809
dbf61afb 810 dev = udev_device_new_from_device_id(udev_enumerate->udev, dent->d_name);
912541b0
KS
811 if (dev == NULL)
812 continue;
813
814 if (!match_subsystem(udev_enumerate, udev_device_get_subsystem(dev)))
815 goto nomatch;
816 if (!match_sysname(udev_enumerate, udev_device_get_sysname(dev)))
817 goto nomatch;
818 if (!match_parent(udev_enumerate, dev))
819 goto nomatch;
820 if (!match_property(udev_enumerate, dev))
821 goto nomatch;
822 if (!match_sysattr(udev_enumerate, dev))
823 goto nomatch;
824
825 syspath_add(udev_enumerate, udev_device_get_syspath(dev));
19e47d97 826nomatch:
912541b0
KS
827 udev_device_unref(dev);
828 }
829 closedir(dir);
830 }
831 return 0;
b05211fa
KS
832}
833
834static int parent_add_child(struct udev_enumerate *enumerate, const char *path)
835{
912541b0 836 struct udev_device *dev;
51cc0757 837 int r = 0;
b05211fa 838
912541b0
KS
839 dev = udev_device_new_from_syspath(enumerate->udev, path);
840 if (dev == NULL)
841 return -ENODEV;
b05211fa 842
912541b0 843 if (!match_subsystem(enumerate, udev_device_get_subsystem(dev)))
51cc0757 844 goto nomatch;
912541b0 845 if (!match_sysname(enumerate, udev_device_get_sysname(dev)))
51cc0757 846 goto nomatch;
912541b0 847 if (!match_property(enumerate, dev))
51cc0757 848 goto nomatch;
912541b0 849 if (!match_sysattr(enumerate, dev))
51cc0757 850 goto nomatch;
b05211fa 851
912541b0 852 syspath_add(enumerate, udev_device_get_syspath(dev));
51cc0757
DH
853 r = 1;
854
855nomatch:
912541b0 856 udev_device_unref(dev);
51cc0757 857 return r;
b05211fa
KS
858}
859
860static int parent_crawl_children(struct udev_enumerate *enumerate, const char *path, int maxdepth)
861{
912541b0
KS
862 DIR *d;
863 struct dirent *dent;
b05211fa 864
912541b0
KS
865 d = opendir(path);
866 if (d == NULL)
867 return -errno;
b05211fa 868
912541b0
KS
869 for (dent = readdir(d); dent != NULL; dent = readdir(d)) {
870 char *child;
b05211fa 871
912541b0
KS
872 if (dent->d_name[0] == '.')
873 continue;
874 if (dent->d_type != DT_DIR)
875 continue;
876 if (asprintf(&child, "%s/%s", path, dent->d_name) < 0)
877 continue;
878 parent_add_child(enumerate, child);
879 if (maxdepth > 0)
880 parent_crawl_children(enumerate, child, maxdepth-1);
881 free(child);
882 }
b05211fa 883
912541b0
KS
884 closedir(d);
885 return 0;
b05211fa
KS
886}
887
6ed03d1e
KS
888static int scan_devices_children(struct udev_enumerate *enumerate)
889{
912541b0 890 const char *path;
6ed03d1e 891
912541b0
KS
892 path = udev_device_get_syspath(enumerate->parent_match);
893 parent_add_child(enumerate, path);
894 return parent_crawl_children(enumerate, path, 256);
6ed03d1e
KS
895}
896
b05211fa
KS
897static int scan_devices_all(struct udev_enumerate *udev_enumerate)
898{
912541b0 899 struct stat statbuf;
b05211fa 900
6ada823a 901 if (stat("/sys/subsystem", &statbuf) == 0) {
912541b0 902 /* we have /subsystem/, forget all the old stuff */
912541b0
KS
903 scan_dir(udev_enumerate, "subsystem", "devices", NULL);
904 } else {
912541b0 905 scan_dir(udev_enumerate, "bus", "devices", NULL);
912541b0
KS
906 scan_dir(udev_enumerate, "class", NULL, NULL);
907 }
908 return 0;
eb1f0e66 909}
bc8184ed 910
b05211fa
KS
911/**
912 * udev_enumerate_scan_devices:
913 * @udev_enumerate: udev enumeration context
914 *
21dbe43a
KS
915 * Scan /sys for all devices which match the given filters. No matches
916 * will return all currently available devices.
917 *
b05211fa
KS
918 * Returns: 0 on success, otherwise a negative error value.
919 **/
54cf0b7f 920_public_ int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
b05211fa 921{
912541b0
KS
922 if (udev_enumerate == NULL)
923 return -EINVAL;
b05211fa 924
912541b0
KS
925 /* efficiently lookup tags only, we maintain a reverse-index */
926 if (udev_list_get_entry(&udev_enumerate->tags_match_list) != NULL)
927 return scan_devices_tags(udev_enumerate);
b05211fa 928
912541b0
KS
929 /* walk the subtree of one parent device only */
930 if (udev_enumerate->parent_match != NULL)
931 return scan_devices_children(udev_enumerate);
b05211fa 932
912541b0
KS
933 /* scan devices of all subsystems */
934 return scan_devices_all(udev_enumerate);
b05211fa
KS
935}
936
438d4c3c
KS
937/**
938 * udev_enumerate_scan_subsystems:
939 * @udev_enumerate: udev enumeration context
940 *
21dbe43a
KS
941 * Scan /sys for all kernel subsystems, including buses, classes, drivers.
942 *
a7c140c7 943 * Returns: 0 on success, otherwise a negative error value.
438d4c3c 944 **/
54cf0b7f 945_public_ int udev_enumerate_scan_subsystems(struct udev_enumerate *udev_enumerate)
bc8184ed 946{
912541b0
KS
947 struct stat statbuf;
948 const char *subsysdir;
949
950 if (udev_enumerate == NULL)
951 return -EINVAL;
952
953 /* all kernel modules */
baa30fbc 954 if (match_subsystem(udev_enumerate, "module"))
912541b0 955 scan_dir_and_add_devices(udev_enumerate, "module", NULL, NULL);
912541b0 956
6ada823a 957 if (stat("/sys/subsystem", &statbuf) == 0)
912541b0
KS
958 subsysdir = "subsystem";
959 else
960 subsysdir = "bus";
961
962 /* all subsystems (only buses support coldplug) */
baa30fbc 963 if (match_subsystem(udev_enumerate, "subsystem"))
912541b0 964 scan_dir_and_add_devices(udev_enumerate, subsysdir, NULL, NULL);
912541b0
KS
965
966 /* all subsystem drivers */
baa30fbc 967 if (match_subsystem(udev_enumerate, "drivers"))
912541b0 968 scan_dir(udev_enumerate, subsysdir, "drivers", "drivers");
912541b0 969 return 0;
bc8184ed 970}