basic/linux: update kernel headers from v6.14-rc1
[systemd.io.git] / src / libudev / libudev-device.c
blob50e2459a06365df607e7dee8bad504f92142a8e6
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
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>
20 #include "libudev.h"
21 #include "sd-device.h"
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"
31 /**
32 * SECTION:libudev-device
33 * @short_description: kernel sys devices
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.
41 /**
42 * udev_device:
44 * Opaque object representing one kernel sys device.
46 struct udev_device {
47 struct udev *udev;
49 /* real device object */
50 sd_device *device;
52 /* legacy */
53 unsigned n_ref;
55 struct udev_device *parent;
56 bool parent_set;
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;
72 /**
73 * udev_device_get_seqnum:
74 * @udev_device: udev device
76 * This is only valid if the device was received through a monitor. Devices read from
77 * sys do not have a sequence number.
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;
84 assert_return_errno(udev_device, 0, EINVAL);
86 if (sd_device_get_seqnum(udev_device->device, &seqnum) < 0)
87 return 0;
89 return seqnum;
92 /**
93 * udev_device_get_devnum:
94 * @udev_device: udev device
96 * Get the device major/minor number.
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;
104 assert_return_errno(udev_device, makedev(0, 0), EINVAL);
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);
112 return devnum;
116 * udev_device_get_driver:
117 * @udev_device: udev device
119 * Get the kernel driver name.
121 * Returns: the driver name string, or #NULL if there is no driver attached.
123 _public_ const char *udev_device_get_driver(struct udev_device *udev_device) {
124 const char *driver;
125 int r;
127 assert_return_errno(udev_device, NULL, EINVAL);
129 r = sd_device_get_driver(udev_device->device, &driver);
130 if (r < 0)
131 return_with_errno(NULL, r);
133 return driver;
137 * udev_device_get_devtype:
138 * @udev_device: udev device
140 * Retrieve the devtype string of the udev device.
142 * Returns: the devtype name of the udev device, or #NULL if it cannot be determined
144 _public_ const char *udev_device_get_devtype(struct udev_device *udev_device) {
145 const char *devtype;
146 int r;
148 assert_return_errno(udev_device, NULL, EINVAL);
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);
156 return devtype;
160 * udev_device_get_subsystem:
161 * @udev_device: udev device
163 * Retrieve the subsystem string of the udev device. The string does not
164 * contain any "/".
166 * Returns: the subsystem name of the udev device, or #NULL if it cannot be determined
168 _public_ const char *udev_device_get_subsystem(struct udev_device *udev_device) {
169 const char *subsystem;
170 int r;
172 assert_return_errno(udev_device, NULL, EINVAL);
174 r = sd_device_get_subsystem(udev_device->device, &subsystem);
175 if (r < 0)
176 return_with_errno(NULL, r);
178 return subsystem;
182 * udev_device_get_property_value:
183 * @udev_device: udev device
184 * @key: property name
186 * Get the value of a given property.
188 * Returns: the property string, or #NULL if there is no such property.
190 _public_ const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key) {
191 const char *value;
192 int r;
194 assert_return_errno(udev_device && key, NULL, EINVAL);
196 r = sd_device_get_property_value(udev_device->device, key, &value);
197 if (r < 0)
198 return_with_errno(NULL, r);
200 return value;
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;
207 assert(device);
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);
225 udev_device = new(struct udev_device, 1);
226 if (!udev_device)
227 return_with_errno(NULL, ENOMEM);
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),
240 return udev_device;
244 * udev_device_new_from_syspath:
245 * @udev: udev library context
246 * @syspath: sys device path including sys directory
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.
252 * The initial refcount is 1, and needs to be decremented to
253 * release the resources of the udev device.
255 * Returns: a new udev device, or #NULL, if it does not exist
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;
261 r = sd_device_new_from_syspath(&device, syspath);
262 if (r < 0)
263 return_with_errno(NULL, r);
265 return udev_device_new(udev, device);
269 * udev_device_new_from_devnum:
270 * @udev: udev library context
271 * @type: char or block device
272 * @devnum: device major/minor number
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.
279 * The initial refcount is 1, and needs to be decremented to
280 * release the resources of the udev device.
282 * Returns: a new udev device, or #NULL, if it does not exist
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;
288 r = sd_device_new_from_devnum(&device, type, devnum);
289 if (r < 0)
290 return_with_errno(NULL, r);
292 return udev_device_new(udev, device);
296 * udev_device_new_from_device_id:
297 * @udev: udev library context
298 * @id: text string identifying a kernel device
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
308 * The initial refcount is 1, and needs to be decremented to
309 * release the resources of the udev device.
311 * Returns: a new udev device, or #NULL, if it does not exist
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;
317 r = sd_device_new_from_device_id(&device, id);
318 if (r < 0)
319 return_with_errno(NULL, r);
321 return udev_device_new(udev, device);
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
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".
334 * The initial refcount is 1, and needs to be decremented to
335 * release the resources of the udev device.
337 * Returns: a new udev device, or #NULL, if it does not exist
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;
343 r = sd_device_new_from_subsystem_sysname(&device, subsystem, sysname);
344 if (r < 0)
345 return_with_errno(NULL, r);
347 return udev_device_new(udev, device);
351 * udev_device_new_from_environment
352 * @udev: udev library context
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.
359 * The initial refcount is 1, and needs to be decremented to
360 * release the resources of the udev device.
362 * Returns: a new udev device, or #NULL, if it does not exist
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;
368 r = device_new_from_strv(&device, environ);
369 if (r < 0)
370 return_with_errno(NULL, r);
372 return udev_device_new(udev, device);
375 static struct udev_device *device_new_from_parent(struct udev_device *child) {
376 sd_device *parent;
377 int r;
379 assert_return_errno(child, NULL, EINVAL);
381 r = sd_device_get_parent(child->device, &parent);
382 if (r < 0)
383 return_with_errno(NULL, r);
385 return udev_device_new(child->udev, parent);
389 * udev_device_get_parent:
390 * @udev_device: the device to start searching from
392 * Find the next parent device, and fill in information from the sys
393 * device and the udev database entry.
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.
398 * It is not necessarily just the upper level directory, empty or not
399 * recognized sys directories are ignored.
401 * It can be called as many times as needed, without caring about
402 * references.
404 * Returns: a new udev device, or #NULL, if it no parent exist.
406 _public_ struct udev_device *udev_device_get_parent(struct udev_device *udev_device) {
407 assert_return_errno(udev_device, NULL, EINVAL);
409 if (!udev_device->parent_set) {
410 udev_device->parent_set = true;
411 udev_device->parent = device_new_from_parent(udev_device);
414 /* TODO: errno will differ here in case parent == NULL */
415 return udev_device->parent;
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
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.
428 * If devtype is #NULL, only subsystem is checked, and any devtype will
429 * match.
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.
434 * It can be called as many times as needed, without caring about
435 * references.
437 * Returns: a new udev device, or #NULL if no matching parent exists.
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;
443 assert_return_errno(udev_device, NULL, EINVAL);
445 /* this relies on the fact that finding the subdevice of a parent or the
446 parent of a subdevice commute */
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);
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;
459 return_with_errno(NULL, ENOENT);
463 * udev_device_get_udev:
464 * @udev_device: udev device
466 * Retrieve the udev library context the device was created with.
468 * Returns: the udev library context
470 _public_ struct udev *udev_device_get_udev(struct udev_device *udev_device) {
471 assert_return_errno(udev_device, NULL, EINVAL);
473 return udev_device->udev;
476 static struct udev_device *udev_device_free(struct udev_device *udev_device) {
477 assert(udev_device);
479 sd_device_unref(udev_device->device);
480 udev_device_unref(udev_device->parent);
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);
488 return mfree(udev_device);
492 * udev_device_ref:
493 * @udev_device: udev device
495 * Take a reference of a udev device.
497 * Returns: the passed udev device
501 * udev_device_unref:
502 * @udev_device: udev device
504 * Drop a reference of a udev device. If the refcount reaches zero,
505 * the resources of the device will be released.
507 * Returns: #NULL
509 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_device, udev_device, udev_device_free);
512 * udev_device_get_devpath:
513 * @udev_device: udev device
515 * Retrieve the kernel devpath value of the udev device. The path
516 * does not contain the sys mount point, and starts with a '/'.
518 * Returns: the devpath of the udev device
520 _public_ const char *udev_device_get_devpath(struct udev_device *udev_device) {
521 const char *devpath;
522 int r;
524 assert_return_errno(udev_device, NULL, EINVAL);
526 r = sd_device_get_devpath(udev_device->device, &devpath);
527 if (r < 0)
528 return_with_errno(NULL, r);
530 return devpath;
534 * udev_device_get_syspath:
535 * @udev_device: udev device
537 * Retrieve the sys path of the udev device. The path is an
538 * absolute path and starts with the sys mount point.
540 * Returns: the sys path of the udev device
542 _public_ const char *udev_device_get_syspath(struct udev_device *udev_device) {
543 const char *syspath;
544 int r;
546 assert_return_errno(udev_device, NULL, EINVAL);
548 r = sd_device_get_syspath(udev_device->device, &syspath);
549 if (r < 0)
550 return_with_errno(NULL, r);
552 return syspath;
556 * udev_device_get_sysname:
557 * @udev_device: udev device
559 * Get the kernel device name in /sys.
561 * Returns: the name string of the device
563 _public_ const char *udev_device_get_sysname(struct udev_device *udev_device) {
564 const char *sysname;
565 int r;
567 assert_return_errno(udev_device, NULL, EINVAL);
569 r = sd_device_get_sysname(udev_device->device, &sysname);
570 if (r < 0)
571 return_with_errno(NULL, r);
573 return sysname;
577 * udev_device_get_sysnum:
578 * @udev_device: udev device
580 * Get the instance number of the device.
582 * Returns: the trailing number string of the device name
584 _public_ const char *udev_device_get_sysnum(struct udev_device *udev_device) {
585 const char *sysnum;
586 int r;
588 assert_return_errno(udev_device, NULL, EINVAL);
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);
596 return sysnum;
600 * udev_device_get_devnode:
601 * @udev_device: udev device
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.
606 * Returns: the device node file name of the udev device, or #NULL if no device node exists
608 _public_ const char *udev_device_get_devnode(struct udev_device *udev_device) {
609 const char *devnode;
610 int r;
612 assert_return_errno(udev_device, NULL, EINVAL);
614 r = sd_device_get_devname(udev_device->device, &devnode);
615 if (r < 0)
616 return_with_errno(NULL, r);
618 return devnode;
622 * udev_device_get_devlinks_list_entry:
623 * @udev_device: udev device
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.
632 * Returns: the first entry of the device node link list
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);
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);
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);
645 udev_device->devlinks_read = true;
646 udev_device->devlinks_generation = device_get_devlinks_generation(udev_device->device);
649 return udev_list_get_entry(udev_device->devlinks);
653 * udev_device_get_event_properties_entry:
654 * @udev_device: udev device
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().
662 * Returns: the first entry of the property list
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);
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);
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);
675 udev_device->properties_read = true;
676 udev_device->properties_generation = device_get_properties_generation(udev_device->device);
679 return udev_list_get_entry(udev_device->properties);
683 * udev_device_get_action:
684 * @udev_device: udev device
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.
690 * Returns: the kernel action value, or #NULL if there is no action value available.
692 _public_ const char *udev_device_get_action(struct udev_device *udev_device) {
693 sd_device_action_t action;
695 assert_return_errno(udev_device, NULL, EINVAL);
697 if (sd_device_get_action(udev_device->device, &action) < 0)
698 return NULL;
700 return device_action_to_string(action);
704 * udev_device_get_usec_since_initialized:
705 * @udev_device: udev device
707 * Return the number of microseconds passed since udev set up the
708 * device for the first time.
710 * This is only implemented for devices with need to store properties
711 * in the udev database. All other devices return 0 here.
713 * Returns: the number of microseconds since the device was first seen.
715 _public_ unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device) {
716 usec_t ts;
717 int r;
719 assert_return(udev_device, -EINVAL);
721 r = sd_device_get_usec_since_initialized(udev_device->device, &ts);
722 if (r < 0)
723 return_with_errno(0, r);
725 return ts;
729 * udev_device_get_sysattr_value:
730 * @udev_device: udev device
731 * @sysattr: attribute name
733 * The retrieved value is cached in the device. Repeated calls will return the same
734 * value and not open the attribute again.
736 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
738 _public_ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr) {
739 const char *value;
740 int r;
742 assert_return_errno(udev_device, NULL, EINVAL);
744 r = sd_device_get_sysattr_value(udev_device->device, sysattr, &value);
745 if (r < 0)
746 return_with_errno(NULL, r);
748 return value;
752 * udev_device_set_sysattr_value:
753 * @udev_device: udev device
754 * @sysattr: attribute name
755 * @value: new value to be set
757 * Update the contents of the sys attribute and the cached value of the device.
759 * Returns: Negative error code on failure or 0 on success.
761 _public_ int udev_device_set_sysattr_value(struct udev_device *udev_device, const char *sysattr, const char *value) {
762 int r;
764 assert_return(udev_device, -EINVAL);
766 r = sd_device_set_sysattr_value(udev_device->device, sysattr, value);
767 if (r < 0)
768 return r;
770 return 0;
774 * udev_device_get_sysattr_list_entry:
775 * @udev_device: udev device
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.
781 * Returns: the first entry of the property list
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);
786 if (!udev_device->sysattrs_read) {
787 udev_list_cleanup(udev_device->sysattrs);
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);
793 udev_device->sysattrs_read = true;
796 return udev_list_get_entry(udev_device->sysattrs);
800 * udev_device_get_is_initialized:
801 * @udev_device: udev device
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.
807 * This is only implemented for devices with a device node
808 * or network interfaces. All other devices return 1 here.
810 * Returns: 1 if the device is set up. 0 otherwise.
812 _public_ int udev_device_get_is_initialized(struct udev_device *udev_device) {
813 int r;
815 assert_return(udev_device, -EINVAL);
817 r = sd_device_get_is_initialized(udev_device->device);
818 if (r < 0)
819 return_with_errno(0, r);
821 return r;
825 * udev_device_get_tags_list_entry:
826 * @udev_device: udev device
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().
833 * Returns: the first entry of the tag list
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);
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);
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);
846 udev_device->all_tags_read = true;
847 udev_device->all_tags_generation = device_get_tags_generation(udev_device->device);
850 return udev_list_get_entry(udev_device->all_tags);
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);
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);
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);
864 udev_device->current_tags_read = true;
865 udev_device->current_tags_generation = device_get_tags_generation(udev_device->device);
868 return udev_list_get_entry(udev_device->current_tags);
872 * udev_device_has_tag:
873 * @udev_device: udev device
874 * @tag: tag name
876 * Check if a given device has a certain tag associated.
878 * Returns: 1 if the tag is found. 0 otherwise.
880 _public_ int udev_device_has_tag(struct udev_device *udev_device, const char *tag) {
881 assert_return(udev_device, 0);
883 return sd_device_has_tag(udev_device->device, tag) > 0;
886 _public_ int udev_device_has_current_tag(struct udev_device *udev_device, const char *tag) {
887 assert_return(udev_device, 0);
889 return sd_device_has_current_tag(udev_device->device, tag) > 0;
892 sd_device *udev_device_get_sd_device(struct udev_device *udev_device) {
893 assert(udev_device);
895 return udev_device->device;