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