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