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