]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 Copyright 2015 Tom Gundersen <teg@jklm.no>
13 #include <linux/sockios.h>
20 #include <sys/ioctl.h>
21 #include <sys/socket.h>
26 #include "sd-device.h"
28 #include "alloc-util.h"
29 #include "device-private.h"
30 #include "device-util.h"
31 #include "libudev-device-internal.h"
32 #include "libudev-private.h"
33 #include "parse-util.h"
36 * SECTION:libudev-device
37 * @short_description: kernel sys devices
39 * Representation of kernel sys devices. Devices are uniquely identified
40 * by their syspath, every device has exactly one path in the kernel sys
41 * filesystem. Devices usually belong to a kernel subsystem, and have
42 * a unique name inside that subsystem.
46 * udev_device_get_seqnum:
47 * @udev_device: udev device
49 * This is only valid if the device was received through a monitor. Devices read from
50 * sys do not have a sequence number.
52 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
54 _public_
unsigned long long int udev_device_get_seqnum(struct udev_device
*udev_device
)
57 unsigned long long ret
;
60 assert_return_errno(udev_device
, 0, EINVAL
);
62 r
= sd_device_get_property_value(udev_device
->device
, "SEQNUM", &seqnum
);
70 r
= safe_atollu(seqnum
, &ret
);
80 * udev_device_get_devnum:
81 * @udev_device: udev device
83 * Get the device major/minor number.
85 * Returns: the dev_t number.
87 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
)
92 assert_return_errno(udev_device
, makedev(0, 0), EINVAL
);
94 r
= sd_device_get_devnum(udev_device
->device
, &devnum
);
104 * udev_device_get_driver:
105 * @udev_device: udev device
107 * Get the kernel driver name.
109 * Returns: the driver name string, or #NULL if there is no driver attached.
111 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
)
116 assert_return_errno(udev_device
, NULL
, EINVAL
);
118 r
= sd_device_get_driver(udev_device
->device
, &driver
);
128 * udev_device_get_devtype:
129 * @udev_device: udev device
131 * Retrieve the devtype string of the udev device.
133 * Returns: the devtype name of the udev device, or #NULL if it cannot be determined
135 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
)
140 assert_return_errno(udev_device
, NULL
, EINVAL
);
142 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
152 * udev_device_get_subsystem:
153 * @udev_device: udev device
155 * Retrieve the subsystem string of the udev device. The string does not
158 * Returns: the subsystem name of the udev device, or #NULL if it cannot be determined
160 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
)
162 const char *subsystem
;
165 assert_return_errno(udev_device
, NULL
, EINVAL
);
167 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
171 } else if (!subsystem
)
178 * udev_device_get_property_value:
179 * @udev_device: udev device
180 * @key: property name
182 * Get the value of a given property.
184 * Returns: the property string, or #NULL if there is no such property.
186 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
)
188 const char *value
= NULL
;
191 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
193 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
202 struct udev_device
*udev_device_new(struct udev
*udev
) {
203 struct udev_device
*udev_device
;
205 assert_return_errno(udev
, NULL
, EINVAL
);
207 udev_device
= new0(struct udev_device
, 1);
212 udev_device
->refcount
= 1;
213 udev_device
->udev
= udev
;
214 udev_list_init(udev
, &udev_device
->properties
, true);
215 udev_list_init(udev
, &udev_device
->tags
, true);
216 udev_list_init(udev
, &udev_device
->sysattrs
, true);
217 udev_list_init(udev
, &udev_device
->devlinks
, true);
223 * udev_device_new_from_syspath:
224 * @udev: udev library context
225 * @syspath: sys device path including sys directory
227 * Create new udev device, and fill in information from the sys
228 * device and the udev database entry. The syspath is the absolute
229 * path to the device, including the sys mount point.
231 * The initial refcount is 1, and needs to be decremented to
232 * release the resources of the udev device.
234 * Returns: a new udev device, or #NULL, if it does not exist
236 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
) {
237 struct udev_device
*udev_device
;
240 udev_device
= udev_device_new(udev
);
244 r
= sd_device_new_from_syspath(&udev_device
->device
, syspath
);
247 udev_device_unref(udev_device
);
255 * udev_device_new_from_devnum:
256 * @udev: udev library context
257 * @type: char or block device
258 * @devnum: device major/minor number
260 * Create new udev device, and fill in information from the sys
261 * device and the udev database entry. The device is looked-up
262 * by its major/minor number and type. Character and block device
263 * numbers are not unique across the two types.
265 * The initial refcount is 1, and needs to be decremented to
266 * release the resources of the udev device.
268 * Returns: a new udev device, or #NULL, if it does not exist
270 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
)
272 struct udev_device
*udev_device
;
275 udev_device
= udev_device_new(udev
);
279 r
= sd_device_new_from_devnum(&udev_device
->device
, type
, devnum
);
282 udev_device_unref(udev_device
);
290 * udev_device_new_from_device_id:
291 * @udev: udev library context
292 * @id: text string identifying a kernel device
294 * Create new udev device, and fill in information from the sys
295 * device and the udev database entry. The device is looked-up
296 * by a special string:
297 * b8:2 - block device major:minor
298 * c128:1 - char device major:minor
299 * n3 - network device ifindex
300 * +sound:card29 - kernel driver core subsystem:device name
302 * The initial refcount is 1, and needs to be decremented to
303 * release the resources of the udev device.
305 * Returns: a new udev device, or #NULL, if it does not exist
307 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
)
309 struct udev_device
*udev_device
;
312 udev_device
= udev_device_new(udev
);
316 r
= sd_device_new_from_device_id(&udev_device
->device
, id
);
319 udev_device_unref(udev_device
);
327 * udev_device_new_from_subsystem_sysname:
328 * @udev: udev library context
329 * @subsystem: the subsystem of the device
330 * @sysname: the name of the device
332 * Create new udev device, and fill in information from the sys device
333 * and the udev database entry. The device is looked up by the subsystem
334 * and name string of the device, like "mem" / "zero", or "block" / "sda".
336 * The initial refcount is 1, and needs to be decremented to
337 * release the resources of the udev device.
339 * Returns: a new udev device, or #NULL, if it does not exist
341 _public_
struct udev_device
*udev_device_new_from_subsystem_sysname(struct udev
*udev
, const char *subsystem
, const char *sysname
)
343 struct udev_device
*udev_device
;
346 udev_device
= udev_device_new(udev
);
350 r
= sd_device_new_from_subsystem_sysname(&udev_device
->device
, subsystem
, sysname
);
353 udev_device_unref(udev_device
);
361 * udev_device_new_from_environment
362 * @udev: udev library context
364 * Create new udev device, and fill in information from the
365 * current process environment. This only works reliable if
366 * the process is called from a udev rule. It is usually used
367 * for tools executed from IMPORT= rules.
369 * The initial refcount is 1, and needs to be decremented to
370 * release the resources of the udev device.
372 * Returns: a new udev device, or #NULL, if it does not exist
374 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
)
376 struct udev_device
*udev_device
;
379 udev_device
= udev_device_new(udev
);
383 r
= device_new_from_strv(&udev_device
->device
, environ
);
386 udev_device_unref(udev_device
);
393 static struct udev_device
*device_new_from_parent(struct udev_device
*child
)
395 struct udev_device
*parent
;
398 assert_return_errno(child
, NULL
, EINVAL
);
400 parent
= udev_device_new(child
->udev
);
404 r
= sd_device_get_parent(child
->device
, &parent
->device
);
407 udev_device_unref(parent
);
411 /* the parent is unref'ed with the child, so take a ref from libudev as well */
412 sd_device_ref(parent
->device
);
418 * udev_device_get_parent:
419 * @udev_device: the device to start searching from
421 * Find the next parent device, and fill in information from the sys
422 * device and the udev database entry.
424 * Returned device is not referenced. It is attached to the child
425 * device, and will be cleaned up when the child device is cleaned up.
427 * It is not necessarily just the upper level directory, empty or not
428 * recognized sys directories are ignored.
430 * It can be called as many times as needed, without caring about
433 * Returns: a new udev device, or #NULL, if it no parent exist.
435 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
)
437 assert_return_errno(udev_device
, NULL
, EINVAL
);
439 if (!udev_device
->parent_set
) {
440 udev_device
->parent_set
= true;
441 udev_device
->parent
= device_new_from_parent(udev_device
);
444 /* TODO: errno will differ here in case parent == NULL */
445 return udev_device
->parent
;
449 * udev_device_get_parent_with_subsystem_devtype:
450 * @udev_device: udev device to start searching from
451 * @subsystem: the subsystem of the device
452 * @devtype: the type (DEVTYPE) of the device
454 * Find the next parent device, with a matching subsystem and devtype
455 * value, and fill in information from the sys device and the udev
458 * If devtype is #NULL, only subsystem is checked, and any devtype will
461 * Returned device is not referenced. It is attached to the child
462 * device, and will be cleaned up when the child device is cleaned up.
464 * It can be called as many times as needed, without caring about
467 * Returns: a new udev device, or #NULL if no matching parent exists.
469 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
)
474 assert_return_errno(udev_device
, NULL
, EINVAL
);
476 /* this relies on the fact that finding the subdevice of a parent or the
477 parent of a subdevice commute */
479 /* first find the correct sd_device */
480 r
= sd_device_get_parent_with_subsystem_devtype(udev_device
->device
, subsystem
, devtype
, &parent
);
486 /* then walk the chain of udev_device parents until the corresponding
488 while ((udev_device
= udev_device_get_parent(udev_device
))) {
489 if (udev_device
->device
== parent
)
498 * udev_device_get_udev:
499 * @udev_device: udev device
501 * Retrieve the udev library context the device was created with.
503 * Returns: the udev library context
505 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
)
507 assert_return_errno(udev_device
, NULL
, EINVAL
);
509 return udev_device
->udev
;
514 * @udev_device: udev device
516 * Take a reference of a udev device.
518 * Returns: the passed udev device
520 _public_
struct udev_device
*udev_device_ref(struct udev_device
*udev_device
)
523 udev_device
->refcount
++;
530 * @udev_device: udev device
532 * Drop a reference of a udev device. If the refcount reaches zero,
533 * the resources of the device will be released.
537 _public_
struct udev_device
*udev_device_unref(struct udev_device
*udev_device
)
539 if (udev_device
&& (-- udev_device
->refcount
) == 0) {
540 sd_device_unref(udev_device
->device
);
541 udev_device_unref(udev_device
->parent
);
543 udev_list_cleanup(&udev_device
->properties
);
544 udev_list_cleanup(&udev_device
->sysattrs
);
545 udev_list_cleanup(&udev_device
->tags
);
546 udev_list_cleanup(&udev_device
->devlinks
);
555 * udev_device_get_devpath:
556 * @udev_device: udev device
558 * Retrieve the kernel devpath value of the udev device. The path
559 * does not contain the sys mount point, and starts with a '/'.
561 * Returns: the devpath of the udev device
563 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
)
568 assert_return_errno(udev_device
, NULL
, EINVAL
);
570 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
580 * udev_device_get_syspath:
581 * @udev_device: udev device
583 * Retrieve the sys path of the udev device. The path is an
584 * absolute path and starts with the sys mount point.
586 * Returns: the sys path of the udev device
588 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
)
593 assert_return_errno(udev_device
, NULL
, EINVAL
);
595 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
605 * udev_device_get_sysname:
606 * @udev_device: udev device
608 * Get the kernel device name in /sys.
610 * Returns: the name string of the device
612 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
)
617 assert_return_errno(udev_device
, NULL
, EINVAL
);
619 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
629 * udev_device_get_sysnum:
630 * @udev_device: udev device
632 * Get the instance number of the device.
634 * Returns: the trailing number string of the device name
636 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
)
641 assert_return_errno(udev_device
, NULL
, EINVAL
);
643 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
653 * udev_device_get_devnode:
654 * @udev_device: udev device
656 * Retrieve the device node file name belonging to the udev device.
657 * The path is an absolute path, and starts with the device directory.
659 * Returns: the device node file name of the udev device, or #NULL if no device node exists
661 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
)
666 assert_return_errno(udev_device
, NULL
, EINVAL
);
668 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
678 * udev_device_get_devlinks_list_entry:
679 * @udev_device: udev device
681 * Retrieve the list of device links pointing to the device file of
682 * the udev device. The next list entry can be retrieved with
683 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
684 * The devlink path can be retrieved from the list entry by
685 * udev_list_entry_get_name(). The path is an absolute path, and starts with
686 * the device directory.
688 * Returns: the first entry of the device node link list
690 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
)
692 assert_return_errno(udev_device
, NULL
, EINVAL
);
694 if (device_get_devlinks_generation(udev_device
->device
) != udev_device
->devlinks_generation
||
695 !udev_device
->devlinks_read
) {
698 udev_list_cleanup(&udev_device
->devlinks
);
700 FOREACH_DEVICE_DEVLINK(udev_device
->device
, devlink
)
701 udev_list_entry_add(&udev_device
->devlinks
, devlink
, NULL
);
703 udev_device
->devlinks_read
= true;
704 udev_device
->devlinks_generation
= device_get_devlinks_generation(udev_device
->device
);
707 return udev_list_get_entry(&udev_device
->devlinks
);
711 * udev_device_get_event_properties_entry:
712 * @udev_device: udev device
714 * Retrieve the list of key/value device properties of the udev
715 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
716 * which returns #NULL if no more entries exist. The property name
717 * can be retrieved from the list entry by udev_list_entry_get_name(),
718 * the property value by udev_list_entry_get_value().
720 * Returns: the first entry of the property list
722 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
)
724 assert_return_errno(udev_device
, NULL
, EINVAL
);
726 if (device_get_properties_generation(udev_device
->device
) != udev_device
->properties_generation
||
727 !udev_device
->properties_read
) {
728 const char *key
, *value
;
730 udev_list_cleanup(&udev_device
->properties
);
732 FOREACH_DEVICE_PROPERTY(udev_device
->device
, key
, value
)
733 udev_list_entry_add(&udev_device
->properties
, key
, value
);
735 udev_device
->properties_read
= true;
736 udev_device
->properties_generation
= device_get_properties_generation(udev_device
->device
);
739 return udev_list_get_entry(&udev_device
->properties
);
743 * udev_device_get_action:
744 * @udev_device: udev device
746 * This is only valid if the device was received through a monitor. Devices read from
747 * sys do not have an action string. Usual actions are: add, remove, change, online,
750 * Returns: the kernel action value, or #NULL if there is no action value available.
752 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
) {
753 const char *action
= NULL
;
756 assert_return_errno(udev_device
, NULL
, EINVAL
);
758 r
= sd_device_get_property_value(udev_device
->device
, "ACTION", &action
);
759 if (r
< 0 && r
!= -ENOENT
) {
768 * udev_device_get_usec_since_initialized:
769 * @udev_device: udev device
771 * Return the number of microseconds passed since udev set up the
772 * device for the first time.
774 * This is only implemented for devices with need to store properties
775 * in the udev database. All other devices return 0 here.
777 * Returns: the number of microseconds since the device was first seen.
779 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
)
784 assert_return(udev_device
, -EINVAL
);
786 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
796 * udev_device_get_sysattr_value:
797 * @udev_device: udev device
798 * @sysattr: attribute name
800 * The retrieved value is cached in the device. Repeated calls will return the same
801 * value and not open the attribute again.
803 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
805 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
)
810 assert_return_errno(udev_device
, NULL
, EINVAL
);
812 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
822 * udev_device_set_sysattr_value:
823 * @udev_device: udev device
824 * @sysattr: attribute name
825 * @value: new value to be set
827 * Update the contents of the sys attribute and the cached value of the device.
829 * Returns: Negative error code on failure or 0 on success.
831 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, char *value
)
835 assert_return(udev_device
, -EINVAL
);
837 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
845 * udev_device_get_sysattr_list_entry:
846 * @udev_device: udev device
848 * Retrieve the list of available sysattrs, with value being empty;
849 * This just return all available sysfs attributes for a particular
850 * device without reading their values.
852 * Returns: the first entry of the property list
854 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
)
856 assert_return_errno(udev_device
, NULL
, EINVAL
);
858 if (!udev_device
->sysattrs_read
) {
861 udev_list_cleanup(&udev_device
->sysattrs
);
863 FOREACH_DEVICE_SYSATTR(udev_device
->device
, sysattr
)
864 udev_list_entry_add(&udev_device
->sysattrs
, sysattr
, NULL
);
866 udev_device
->sysattrs_read
= true;
869 return udev_list_get_entry(&udev_device
->sysattrs
);
873 * udev_device_get_is_initialized:
874 * @udev_device: udev device
876 * Check if udev has already handled the device and has set up
877 * device node permissions and context, or has renamed a network
880 * This is only implemented for devices with a device node
881 * or network interfaces. All other devices return 1 here.
883 * Returns: 1 if the device is set up. 0 otherwise.
885 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
)
889 assert_return(udev_device
, -EINVAL
);
891 r
= sd_device_get_is_initialized(udev_device
->device
, &initialized
);
902 * udev_device_get_tags_list_entry:
903 * @udev_device: udev device
905 * Retrieve the list of tags attached to the udev device. The next
906 * list entry can be retrieved with udev_list_entry_get_next(),
907 * which returns #NULL if no more entries exist. The tag string
908 * can be retrieved from the list entry by udev_list_entry_get_name().
910 * Returns: the first entry of the tag list
912 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
)
914 assert_return_errno(udev_device
, NULL
, EINVAL
);
916 if (device_get_tags_generation(udev_device
->device
) != udev_device
->tags_generation
||
917 !udev_device
->tags_read
) {
920 udev_list_cleanup(&udev_device
->tags
);
922 FOREACH_DEVICE_TAG(udev_device
->device
, tag
)
923 udev_list_entry_add(&udev_device
->tags
, tag
, NULL
);
925 udev_device
->tags_read
= true;
926 udev_device
->tags_generation
= device_get_tags_generation(udev_device
->device
);
929 return udev_list_get_entry(&udev_device
->tags
);
933 * udev_device_has_tag:
934 * @udev_device: udev device
937 * Check if a given device has a certain tag associated.
939 * Returns: 1 if the tag is found. 0 otherwise.
941 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
)
943 assert_return(udev_device
, 0);
945 return sd_device_has_tag(udev_device
->device
, tag
);