]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-device.c
Merge pull request #8700 from keszybz/hibernation
[thirdparty/systemd.git] / src / libudev / libudev-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 Copyright 2015 Tom Gundersen <teg@jklm.no>
7 ***/
8
9 #include <ctype.h>
10 #include <dirent.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <linux/sockios.h>
14 #include <net/if.h>
15 #include <stdbool.h>
16 #include <stddef.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24
25 #include "libudev.h"
26 #include "sd-device.h"
27
28 #include "alloc-util.h"
29 #include "device-private.h"
30 #include "device-util.h"
31 #include "libudev-device-internal.h"
32 #include "libudev-private.h"
33 #include "parse-util.h"
34
35 /**
36 * SECTION:libudev-device
37 * @short_description: kernel sys devices
38 *
39 * Representation of kernel sys devices. Devices are uniquely identified
40 * by their syspath, every device has exactly one path in the kernel sys
41 * filesystem. Devices usually belong to a kernel subsystem, and have
42 * a unique name inside that subsystem.
43 */
44
45 /**
46 * udev_device_get_seqnum:
47 * @udev_device: udev device
48 *
49 * This is only valid if the device was received through a monitor. Devices read from
50 * sys do not have a sequence number.
51 *
52 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
53 **/
54 _public_ unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
55 {
56 const char *seqnum;
57 unsigned long long ret;
58 int r;
59
60 assert_return_errno(udev_device, 0, EINVAL);
61
62 r = sd_device_get_property_value(udev_device->device, "SEQNUM", &seqnum);
63 if (r == -ENOENT)
64 return 0;
65 else if (r < 0) {
66 errno = -r;
67 return 0;
68 }
69
70 r = safe_atollu(seqnum, &ret);
71 if (r < 0) {
72 errno = -r;
73 return 0;
74 }
75
76 return ret;
77 }
78
79 /**
80 * udev_device_get_devnum:
81 * @udev_device: udev device
82 *
83 * Get the device major/minor number.
84 *
85 * Returns: the dev_t number.
86 **/
87 _public_ dev_t udev_device_get_devnum(struct udev_device *udev_device)
88 {
89 dev_t devnum;
90 int r;
91
92 assert_return_errno(udev_device, makedev(0, 0), EINVAL);
93
94 r = sd_device_get_devnum(udev_device->device, &devnum);
95 if (r < 0) {
96 errno = -r;
97 return makedev(0, 0);
98 }
99
100 return devnum;
101 }
102
103 /**
104 * udev_device_get_driver:
105 * @udev_device: udev device
106 *
107 * Get the kernel driver name.
108 *
109 * Returns: the driver name string, or #NULL if there is no driver attached.
110 **/
111 _public_ const char *udev_device_get_driver(struct udev_device *udev_device)
112 {
113 const char *driver;
114 int r;
115
116 assert_return_errno(udev_device, NULL, EINVAL);
117
118 r = sd_device_get_driver(udev_device->device, &driver);
119 if (r < 0) {
120 errno = -r;
121 return NULL;
122 }
123
124 return driver;
125 }
126
127 /**
128 * udev_device_get_devtype:
129 * @udev_device: udev device
130 *
131 * Retrieve the devtype string of the udev device.
132 *
133 * Returns: the devtype name of the udev device, or #NULL if it cannot be determined
134 **/
135 _public_ const char *udev_device_get_devtype(struct udev_device *udev_device)
136 {
137 const char *devtype;
138 int r;
139
140 assert_return_errno(udev_device, NULL, EINVAL);
141
142 r = sd_device_get_devtype(udev_device->device, &devtype);
143 if (r < 0) {
144 errno = -r;
145 return NULL;
146 }
147
148 return devtype;
149 }
150
151 /**
152 * udev_device_get_subsystem:
153 * @udev_device: udev device
154 *
155 * Retrieve the subsystem string of the udev device. The string does not
156 * contain any "/".
157 *
158 * Returns: the subsystem name of the udev device, or #NULL if it cannot be determined
159 **/
160 _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device)
161 {
162 const char *subsystem;
163 int r;
164
165 assert_return_errno(udev_device, NULL, EINVAL);
166
167 r = sd_device_get_subsystem(udev_device->device, &subsystem);
168 if (r < 0) {
169 errno = -r;
170 return NULL;
171 } else if (!subsystem)
172 errno = ENODATA;
173
174 return subsystem;
175 }
176
177 /**
178 * udev_device_get_property_value:
179 * @udev_device: udev device
180 * @key: property name
181 *
182 * Get the value of a given property.
183 *
184 * Returns: the property string, or #NULL if there is no such property.
185 **/
186 _public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
187 {
188 const char *value = NULL;
189 int r;
190
191 assert_return_errno(udev_device && key, NULL, EINVAL);
192
193 r = sd_device_get_property_value(udev_device->device, key, &value);
194 if (r < 0) {
195 errno = -r;
196 return NULL;
197 }
198
199 return value;
200 }
201
202 struct udev_device *udev_device_new(struct udev *udev) {
203 struct udev_device *udev_device;
204
205 assert_return_errno(udev, NULL, EINVAL);
206
207 udev_device = new0(struct udev_device, 1);
208 if (!udev_device) {
209 errno = ENOMEM;
210 return NULL;
211 }
212 udev_device->refcount = 1;
213 udev_device->udev = udev;
214 udev_list_init(udev, &udev_device->properties, true);
215 udev_list_init(udev, &udev_device->tags, true);
216 udev_list_init(udev, &udev_device->sysattrs, true);
217 udev_list_init(udev, &udev_device->devlinks, true);
218
219 return udev_device;
220 }
221
222 /**
223 * udev_device_new_from_syspath:
224 * @udev: udev library context
225 * @syspath: sys device path including sys directory
226 *
227 * Create new udev device, and fill in information from the sys
228 * device and the udev database entry. The syspath is the absolute
229 * path to the device, including the sys mount point.
230 *
231 * The initial refcount is 1, and needs to be decremented to
232 * release the resources of the udev device.
233 *
234 * Returns: a new udev device, or #NULL, if it does not exist
235 **/
236 _public_ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath) {
237 struct udev_device *udev_device;
238 int r;
239
240 udev_device = udev_device_new(udev);
241 if (!udev_device)
242 return NULL;
243
244 r = sd_device_new_from_syspath(&udev_device->device, syspath);
245 if (r < 0) {
246 errno = -r;
247 udev_device_unref(udev_device);
248 return NULL;
249 }
250
251 return udev_device;
252 }
253
254 /**
255 * udev_device_new_from_devnum:
256 * @udev: udev library context
257 * @type: char or block device
258 * @devnum: device major/minor number
259 *
260 * Create new udev device, and fill in information from the sys
261 * device and the udev database entry. The device is looked-up
262 * by its major/minor number and type. Character and block device
263 * numbers are not unique across the two types.
264 *
265 * The initial refcount is 1, and needs to be decremented to
266 * release the resources of the udev device.
267 *
268 * Returns: a new udev device, or #NULL, if it does not exist
269 **/
270 _public_ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
271 {
272 struct udev_device *udev_device;
273 int r;
274
275 udev_device = udev_device_new(udev);
276 if (!udev_device)
277 return NULL;
278
279 r = sd_device_new_from_devnum(&udev_device->device, type, devnum);
280 if (r < 0) {
281 errno = -r;
282 udev_device_unref(udev_device);
283 return NULL;
284 }
285
286 return udev_device;
287 }
288
289 /**
290 * udev_device_new_from_device_id:
291 * @udev: udev library context
292 * @id: text string identifying a kernel device
293 *
294 * Create new udev device, and fill in information from the sys
295 * device and the udev database entry. The device is looked-up
296 * by a special string:
297 * b8:2 - block device major:minor
298 * c128:1 - char device major:minor
299 * n3 - network device ifindex
300 * +sound:card29 - kernel driver core subsystem:device name
301 *
302 * The initial refcount is 1, and needs to be decremented to
303 * release the resources of the udev device.
304 *
305 * Returns: a new udev device, or #NULL, if it does not exist
306 **/
307 _public_ struct udev_device *udev_device_new_from_device_id(struct udev *udev, const char *id)
308 {
309 struct udev_device *udev_device;
310 int r;
311
312 udev_device = udev_device_new(udev);
313 if (!udev_device)
314 return NULL;
315
316 r = sd_device_new_from_device_id(&udev_device->device, id);
317 if (r < 0) {
318 errno = -r;
319 udev_device_unref(udev_device);
320 return NULL;
321 }
322
323 return udev_device;
324 }
325
326 /**
327 * udev_device_new_from_subsystem_sysname:
328 * @udev: udev library context
329 * @subsystem: the subsystem of the device
330 * @sysname: the name of the device
331 *
332 * Create new udev device, and fill in information from the sys device
333 * and the udev database entry. The device is looked up by the subsystem
334 * and name string of the device, like "mem" / "zero", or "block" / "sda".
335 *
336 * The initial refcount is 1, and needs to be decremented to
337 * release the resources of the udev device.
338 *
339 * Returns: a new udev device, or #NULL, if it does not exist
340 **/
341 _public_ struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
342 {
343 struct udev_device *udev_device;
344 int r;
345
346 udev_device = udev_device_new(udev);
347 if (!udev_device)
348 return NULL;
349
350 r = sd_device_new_from_subsystem_sysname(&udev_device->device, subsystem, sysname);
351 if (r < 0) {
352 errno = -r;
353 udev_device_unref(udev_device);
354 return NULL;
355 }
356
357 return udev_device;
358 }
359
360 /**
361 * udev_device_new_from_environment
362 * @udev: udev library context
363 *
364 * Create new udev device, and fill in information from the
365 * current process environment. This only works reliable if
366 * the process is called from a udev rule. It is usually used
367 * for tools executed from IMPORT= rules.
368 *
369 * The initial refcount is 1, and needs to be decremented to
370 * release the resources of the udev device.
371 *
372 * Returns: a new udev device, or #NULL, if it does not exist
373 **/
374 _public_ struct udev_device *udev_device_new_from_environment(struct udev *udev)
375 {
376 struct udev_device *udev_device;
377 int r;
378
379 udev_device = udev_device_new(udev);
380 if (!udev_device)
381 return NULL;
382
383 r = device_new_from_strv(&udev_device->device, environ);
384 if (r < 0) {
385 errno = -r;
386 udev_device_unref(udev_device);
387 return NULL;
388 }
389
390 return udev_device;
391 }
392
393 static struct udev_device *device_new_from_parent(struct udev_device *child)
394 {
395 struct udev_device *parent;
396 int r;
397
398 assert_return_errno(child, NULL, EINVAL);
399
400 parent = udev_device_new(child->udev);
401 if (!parent)
402 return NULL;
403
404 r = sd_device_get_parent(child->device, &parent->device);
405 if (r < 0) {
406 errno = -r;
407 udev_device_unref(parent);
408 return NULL;
409 }
410
411 /* the parent is unref'ed with the child, so take a ref from libudev as well */
412 sd_device_ref(parent->device);
413
414 return parent;
415 }
416
417 /**
418 * udev_device_get_parent:
419 * @udev_device: the device to start searching from
420 *
421 * Find the next parent device, and fill in information from the sys
422 * device and the udev database entry.
423 *
424 * Returned device is not referenced. It is attached to the child
425 * device, and will be cleaned up when the child device is cleaned up.
426 *
427 * It is not necessarily just the upper level directory, empty or not
428 * recognized sys directories are ignored.
429 *
430 * It can be called as many times as needed, without caring about
431 * references.
432 *
433 * Returns: a new udev device, or #NULL, if it no parent exist.
434 **/
435 _public_ struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
436 {
437 assert_return_errno(udev_device, NULL, EINVAL);
438
439 if (!udev_device->parent_set) {
440 udev_device->parent_set = true;
441 udev_device->parent = device_new_from_parent(udev_device);
442 }
443
444 /* TODO: errno will differ here in case parent == NULL */
445 return udev_device->parent;
446 }
447
448 /**
449 * udev_device_get_parent_with_subsystem_devtype:
450 * @udev_device: udev device to start searching from
451 * @subsystem: the subsystem of the device
452 * @devtype: the type (DEVTYPE) of the device
453 *
454 * Find the next parent device, with a matching subsystem and devtype
455 * value, and fill in information from the sys device and the udev
456 * database entry.
457 *
458 * If devtype is #NULL, only subsystem is checked, and any devtype will
459 * match.
460 *
461 * Returned device is not referenced. It is attached to the child
462 * device, and will be cleaned up when the child device is cleaned up.
463 *
464 * It can be called as many times as needed, without caring about
465 * references.
466 *
467 * Returns: a new udev device, or #NULL if no matching parent exists.
468 **/
469 _public_ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
470 {
471 sd_device *parent;
472 int r;
473
474 assert_return_errno(udev_device, NULL, EINVAL);
475
476 /* this relies on the fact that finding the subdevice of a parent or the
477 parent of a subdevice commute */
478
479 /* first find the correct sd_device */
480 r = sd_device_get_parent_with_subsystem_devtype(udev_device->device, subsystem, devtype, &parent);
481 if (r < 0) {
482 errno = -r;
483 return NULL;
484 }
485
486 /* then walk the chain of udev_device parents until the corresponding
487 one is found */
488 while ((udev_device = udev_device_get_parent(udev_device))) {
489 if (udev_device->device == parent)
490 return udev_device;
491 }
492
493 errno = ENOENT;
494 return NULL;
495 }
496
497 /**
498 * udev_device_get_udev:
499 * @udev_device: udev device
500 *
501 * Retrieve the udev library context the device was created with.
502 *
503 * Returns: the udev library context
504 **/
505 _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device)
506 {
507 assert_return_errno(udev_device, NULL, EINVAL);
508
509 return udev_device->udev;
510 }
511
512 /**
513 * udev_device_ref:
514 * @udev_device: udev device
515 *
516 * Take a reference of a udev device.
517 *
518 * Returns: the passed udev device
519 **/
520 _public_ struct udev_device *udev_device_ref(struct udev_device *udev_device)
521 {
522 if (udev_device)
523 udev_device->refcount++;
524
525 return udev_device;
526 }
527
528 /**
529 * udev_device_unref:
530 * @udev_device: udev device
531 *
532 * Drop a reference of a udev device. If the refcount reaches zero,
533 * the resources of the device will be released.
534 *
535 * Returns: #NULL
536 **/
537 _public_ struct udev_device *udev_device_unref(struct udev_device *udev_device)
538 {
539 if (udev_device && (-- udev_device->refcount) == 0) {
540 sd_device_unref(udev_device->device);
541 udev_device_unref(udev_device->parent);
542
543 udev_list_cleanup(&udev_device->properties);
544 udev_list_cleanup(&udev_device->sysattrs);
545 udev_list_cleanup(&udev_device->tags);
546 udev_list_cleanup(&udev_device->devlinks);
547
548 free(udev_device);
549 }
550
551 return NULL;
552 }
553
554 /**
555 * udev_device_get_devpath:
556 * @udev_device: udev device
557 *
558 * Retrieve the kernel devpath value of the udev device. The path
559 * does not contain the sys mount point, and starts with a '/'.
560 *
561 * Returns: the devpath of the udev device
562 **/
563 _public_ const char *udev_device_get_devpath(struct udev_device *udev_device)
564 {
565 const char *devpath;
566 int r;
567
568 assert_return_errno(udev_device, NULL, EINVAL);
569
570 r = sd_device_get_devpath(udev_device->device, &devpath);
571 if (r < 0) {
572 errno = -r;
573 return NULL;
574 }
575
576 return devpath;
577 }
578
579 /**
580 * udev_device_get_syspath:
581 * @udev_device: udev device
582 *
583 * Retrieve the sys path of the udev device. The path is an
584 * absolute path and starts with the sys mount point.
585 *
586 * Returns: the sys path of the udev device
587 **/
588 _public_ const char *udev_device_get_syspath(struct udev_device *udev_device)
589 {
590 const char *syspath;
591 int r;
592
593 assert_return_errno(udev_device, NULL, EINVAL);
594
595 r = sd_device_get_syspath(udev_device->device, &syspath);
596 if (r < 0) {
597 errno = -r;
598 return NULL;
599 }
600
601 return syspath;
602 }
603
604 /**
605 * udev_device_get_sysname:
606 * @udev_device: udev device
607 *
608 * Get the kernel device name in /sys.
609 *
610 * Returns: the name string of the device
611 **/
612 _public_ const char *udev_device_get_sysname(struct udev_device *udev_device)
613 {
614 const char *sysname;
615 int r;
616
617 assert_return_errno(udev_device, NULL, EINVAL);
618
619 r = sd_device_get_sysname(udev_device->device, &sysname);
620 if (r < 0) {
621 errno = -r;
622 return NULL;
623 }
624
625 return sysname;
626 }
627
628 /**
629 * udev_device_get_sysnum:
630 * @udev_device: udev device
631 *
632 * Get the instance number of the device.
633 *
634 * Returns: the trailing number string of the device name
635 **/
636 _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device)
637 {
638 const char *sysnum;
639 int r;
640
641 assert_return_errno(udev_device, NULL, EINVAL);
642
643 r = sd_device_get_sysnum(udev_device->device, &sysnum);
644 if (r < 0) {
645 errno = -r;
646 return NULL;
647 }
648
649 return sysnum;
650 }
651
652 /**
653 * udev_device_get_devnode:
654 * @udev_device: udev device
655 *
656 * Retrieve the device node file name belonging to the udev device.
657 * The path is an absolute path, and starts with the device directory.
658 *
659 * Returns: the device node file name of the udev device, or #NULL if no device node exists
660 **/
661 _public_ const char *udev_device_get_devnode(struct udev_device *udev_device)
662 {
663 const char *devnode;
664 int r;
665
666 assert_return_errno(udev_device, NULL, EINVAL);
667
668 r = sd_device_get_devname(udev_device->device, &devnode);
669 if (r < 0) {
670 errno = -r;
671 return NULL;
672 }
673
674 return devnode;
675 }
676
677 /**
678 * udev_device_get_devlinks_list_entry:
679 * @udev_device: udev device
680 *
681 * Retrieve the list of device links pointing to the device file of
682 * the udev device. The next list entry can be retrieved with
683 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
684 * The devlink path can be retrieved from the list entry by
685 * udev_list_entry_get_name(). The path is an absolute path, and starts with
686 * the device directory.
687 *
688 * Returns: the first entry of the device node link list
689 **/
690 _public_ struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
691 {
692 assert_return_errno(udev_device, NULL, EINVAL);
693
694 if (device_get_devlinks_generation(udev_device->device) != udev_device->devlinks_generation ||
695 !udev_device->devlinks_read) {
696 const char *devlink;
697
698 udev_list_cleanup(&udev_device->devlinks);
699
700 FOREACH_DEVICE_DEVLINK(udev_device->device, devlink)
701 udev_list_entry_add(&udev_device->devlinks, devlink, NULL);
702
703 udev_device->devlinks_read = true;
704 udev_device->devlinks_generation = device_get_devlinks_generation(udev_device->device);
705 }
706
707 return udev_list_get_entry(&udev_device->devlinks);
708 }
709
710 /**
711 * udev_device_get_event_properties_entry:
712 * @udev_device: udev device
713 *
714 * Retrieve the list of key/value device properties of the udev
715 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
716 * which returns #NULL if no more entries exist. The property name
717 * can be retrieved from the list entry by udev_list_entry_get_name(),
718 * the property value by udev_list_entry_get_value().
719 *
720 * Returns: the first entry of the property list
721 **/
722 _public_ struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
723 {
724 assert_return_errno(udev_device, NULL, EINVAL);
725
726 if (device_get_properties_generation(udev_device->device) != udev_device->properties_generation ||
727 !udev_device->properties_read) {
728 const char *key, *value;
729
730 udev_list_cleanup(&udev_device->properties);
731
732 FOREACH_DEVICE_PROPERTY(udev_device->device, key, value)
733 udev_list_entry_add(&udev_device->properties, key, value);
734
735 udev_device->properties_read = true;
736 udev_device->properties_generation = device_get_properties_generation(udev_device->device);
737 }
738
739 return udev_list_get_entry(&udev_device->properties);
740 }
741
742 /**
743 * udev_device_get_action:
744 * @udev_device: udev device
745 *
746 * This is only valid if the device was received through a monitor. Devices read from
747 * sys do not have an action string. Usual actions are: add, remove, change, online,
748 * offline.
749 *
750 * Returns: the kernel action value, or #NULL if there is no action value available.
751 **/
752 _public_ const char *udev_device_get_action(struct udev_device *udev_device) {
753 const char *action = NULL;
754 int r;
755
756 assert_return_errno(udev_device, NULL, EINVAL);
757
758 r = sd_device_get_property_value(udev_device->device, "ACTION", &action);
759 if (r < 0 && r != -ENOENT) {
760 errno = -r;
761 return NULL;
762 }
763
764 return action;
765 }
766
767 /**
768 * udev_device_get_usec_since_initialized:
769 * @udev_device: udev device
770 *
771 * Return the number of microseconds passed since udev set up the
772 * device for the first time.
773 *
774 * This is only implemented for devices with need to store properties
775 * in the udev database. All other devices return 0 here.
776 *
777 * Returns: the number of microseconds since the device was first seen.
778 **/
779 _public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
780 {
781 usec_t ts;
782 int r;
783
784 assert_return(udev_device, -EINVAL);
785
786 r = sd_device_get_usec_since_initialized(udev_device->device, &ts);
787 if (r < 0) {
788 errno = EINVAL;
789 return 0;
790 }
791
792 return ts;
793 }
794
795 /**
796 * udev_device_get_sysattr_value:
797 * @udev_device: udev device
798 * @sysattr: attribute name
799 *
800 * The retrieved value is cached in the device. Repeated calls will return the same
801 * value and not open the attribute again.
802 *
803 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
804 **/
805 _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
806 {
807 const char *value;
808 int r;
809
810 assert_return_errno(udev_device, NULL, EINVAL);
811
812 r = sd_device_get_sysattr_value(udev_device->device, sysattr, &value);
813 if (r < 0) {
814 errno = -r;
815 return NULL;
816 }
817
818 return value;
819 }
820
821 /**
822 * udev_device_set_sysattr_value:
823 * @udev_device: udev device
824 * @sysattr: attribute name
825 * @value: new value to be set
826 *
827 * Update the contents of the sys attribute and the cached value of the device.
828 *
829 * Returns: Negative error code on failure or 0 on success.
830 **/
831 _public_ int udev_device_set_sysattr_value(struct udev_device *udev_device, const char *sysattr, char *value)
832 {
833 int r;
834
835 assert_return(udev_device, -EINVAL);
836
837 r = sd_device_set_sysattr_value(udev_device->device, sysattr, value);
838 if (r < 0)
839 return r;
840
841 return 0;
842 }
843
844 /**
845 * udev_device_get_sysattr_list_entry:
846 * @udev_device: udev device
847 *
848 * Retrieve the list of available sysattrs, with value being empty;
849 * This just return all available sysfs attributes for a particular
850 * device without reading their values.
851 *
852 * Returns: the first entry of the property list
853 **/
854 _public_ struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
855 {
856 assert_return_errno(udev_device, NULL, EINVAL);
857
858 if (!udev_device->sysattrs_read) {
859 const char *sysattr;
860
861 udev_list_cleanup(&udev_device->sysattrs);
862
863 FOREACH_DEVICE_SYSATTR(udev_device->device, sysattr)
864 udev_list_entry_add(&udev_device->sysattrs, sysattr, NULL);
865
866 udev_device->sysattrs_read = true;
867 }
868
869 return udev_list_get_entry(&udev_device->sysattrs);
870 }
871
872 /**
873 * udev_device_get_is_initialized:
874 * @udev_device: udev device
875 *
876 * Check if udev has already handled the device and has set up
877 * device node permissions and context, or has renamed a network
878 * device.
879 *
880 * This is only implemented for devices with a device node
881 * or network interfaces. All other devices return 1 here.
882 *
883 * Returns: 1 if the device is set up. 0 otherwise.
884 **/
885 _public_ int udev_device_get_is_initialized(struct udev_device *udev_device)
886 {
887 int r, initialized;
888
889 assert_return(udev_device, -EINVAL);
890
891 r = sd_device_get_is_initialized(udev_device->device, &initialized);
892 if (r < 0) {
893 errno = -r;
894
895 return 0;
896 }
897
898 return initialized;
899 }
900
901 /**
902 * udev_device_get_tags_list_entry:
903 * @udev_device: udev device
904 *
905 * Retrieve the list of tags attached to the udev device. The next
906 * list entry can be retrieved with udev_list_entry_get_next(),
907 * which returns #NULL if no more entries exist. The tag string
908 * can be retrieved from the list entry by udev_list_entry_get_name().
909 *
910 * Returns: the first entry of the tag list
911 **/
912 _public_ struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
913 {
914 assert_return_errno(udev_device, NULL, EINVAL);
915
916 if (device_get_tags_generation(udev_device->device) != udev_device->tags_generation ||
917 !udev_device->tags_read) {
918 const char *tag;
919
920 udev_list_cleanup(&udev_device->tags);
921
922 FOREACH_DEVICE_TAG(udev_device->device, tag)
923 udev_list_entry_add(&udev_device->tags, tag, NULL);
924
925 udev_device->tags_read = true;
926 udev_device->tags_generation = device_get_tags_generation(udev_device->device);
927 }
928
929 return udev_list_get_entry(&udev_device->tags);
930 }
931
932 /**
933 * udev_device_has_tag:
934 * @udev_device: udev device
935 * @tag: tag name
936 *
937 * Check if a given device has a certain tag associated.
938 *
939 * Returns: 1 if the tag is found. 0 otherwise.
940 **/
941 _public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
942 {
943 assert_return(udev_device, 0);
944
945 return sd_device_has_tag(udev_device->device, tag);
946 }