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