1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 /* Make sure the net/if.h header is included before any linux/ one */
9 #include <linux/sockios.h>
15 #include <sys/ioctl.h>
16 #include <sys/socket.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"
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.
44 * Opaque object representing one kernel sys device.
49 /* real device object */
55 struct udev_device
*parent
;
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;
66 bool current_tags_read
:1;
68 struct udev_list
*sysattrs
;
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.
81 _public_
unsigned long long udev_device_get_seqnum(struct udev_device
*udev_device
) {
84 assert_return_errno(udev_device
, 0, EINVAL
);
86 if (sd_device_get_seqnum(udev_device
->device
, &seqnum
) < 0)
93 * udev_device_get_devnum:
94 * @udev_device: udev device
96 * Get the device major/minor number.
98 * Returns: the dev_t number.
100 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
) {
104 assert_return_errno(udev_device
, makedev(0, 0), EINVAL
);
106 r
= sd_device_get_devnum(udev_device
->device
, &devnum
);
108 return makedev(0, 0);
110 return_with_errno(makedev(0, 0), r
);
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
) {
127 assert_return_errno(udev_device
, NULL
, EINVAL
);
129 r
= sd_device_get_driver(udev_device
->device
, &driver
);
131 return_with_errno(NULL
, r
);
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
) {
148 assert_return_errno(udev_device
, NULL
, EINVAL
);
150 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
154 return_with_errno(NULL
, r
);
160 * udev_device_get_subsystem:
161 * @udev_device: udev device
163 * Retrieve the subsystem string of the udev device. The string does not
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
;
172 assert_return_errno(udev_device
, NULL
, EINVAL
);
174 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
176 return_with_errno(NULL
, r
);
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
) {
194 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
196 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
198 return_with_errno(NULL
, r
);
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
;
209 properties
= udev_list_new(true);
211 return_with_errno(NULL
, ENOMEM
);
212 all_tags
= udev_list_new(true);
214 return_with_errno(NULL
, ENOMEM
);
215 current_tags
= udev_list_new(true);
217 return_with_errno(NULL
, ENOMEM
);
218 sysattrs
= udev_list_new(true);
220 return_with_errno(NULL
, ENOMEM
);
221 devlinks
= udev_list_new(true);
223 return_with_errno(NULL
, ENOMEM
);
225 udev_device
= new(struct udev_device
, 1);
227 return_with_errno(NULL
, ENOMEM
);
229 *udev_device
= (struct udev_device
) {
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
),
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
;
261 r
= sd_device_new_from_syspath(&device
, syspath
);
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
;
288 r
= sd_device_new_from_devnum(&device
, type
, devnum
);
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
;
317 r
= sd_device_new_from_device_id(&device
, id
);
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
;
343 r
= sd_device_new_from_subsystem_sysname(&device
, subsystem
, sysname
);
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
;
368 r
= device_new_from_strv(&device
, environ
);
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
) {
379 assert_return_errno(child
, NULL
, EINVAL
);
381 r
= sd_device_get_parent(child
->device
, &parent
);
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
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
428 * If devtype is #NULL, only subsystem is checked, and any devtype will
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
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
) {
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
);
451 return_with_errno(NULL
, r
);
453 /* then walk the chain of udev_device parents until the corresponding
455 while ((udev_device
= udev_device_get_parent(udev_device
)))
456 if (udev_device
->device
== parent
)
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
) {
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
);
493 * @udev_device: udev device
495 * Take a reference of a udev device.
497 * Returns: the passed udev device
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.
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
) {
524 assert_return_errno(udev_device
, NULL
, EINVAL
);
526 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
528 return_with_errno(NULL
, r
);
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
) {
546 assert_return_errno(udev_device
, NULL
, EINVAL
);
548 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
550 return_with_errno(NULL
, r
);
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
) {
567 assert_return_errno(udev_device
, NULL
, EINVAL
);
569 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
571 return_with_errno(NULL
, r
);
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
) {
588 assert_return_errno(udev_device
, NULL
, EINVAL
);
590 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
594 return_with_errno(NULL
, r
);
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
) {
612 assert_return_errno(udev_device
, NULL
, EINVAL
);
614 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
616 return_with_errno(NULL
, r
);
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,
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)
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
) {
719 assert_return(udev_device
, -EINVAL
);
721 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
723 return_with_errno(0, r
);
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
) {
742 assert_return_errno(udev_device
, NULL
, EINVAL
);
744 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
746 return_with_errno(NULL
, r
);
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
) {
764 assert_return(udev_device
, -EINVAL
);
766 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
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
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
) {
815 assert_return(udev_device
, -EINVAL
);
817 r
= sd_device_get_is_initialized(udev_device
->device
);
819 return_with_errno(0, 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
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
) {
895 return udev_device
->device
;