]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include <linux/sockios.h>
14 #include <sys/ioctl.h>
15 #include <sys/socket.h>
20 #include "sd-device.h"
22 #include "alloc-util.h"
23 #include "device-private.h"
24 #include "device-util.h"
25 #include "libudev-device-internal.h"
26 #include "libudev-private.h"
27 #include "parse-util.h"
30 * SECTION:libudev-device
31 * @short_description: kernel sys devices
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.
40 * udev_device_get_seqnum:
41 * @udev_device: udev device
43 * This is only valid if the device was received through a monitor. Devices read from
44 * sys do not have a sequence number.
46 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
48 _public_
unsigned long long int udev_device_get_seqnum(struct udev_device
*udev_device
)
51 unsigned long long ret
;
54 assert_return_errno(udev_device
, 0, EINVAL
);
56 r
= sd_device_get_property_value(udev_device
->device
, "SEQNUM", &seqnum
);
64 r
= safe_atollu(seqnum
, &ret
);
74 * udev_device_get_devnum:
75 * @udev_device: udev device
77 * Get the device major/minor number.
79 * Returns: the dev_t number.
81 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
)
86 assert_return_errno(udev_device
, makedev(0, 0), EINVAL
);
88 r
= sd_device_get_devnum(udev_device
->device
, &devnum
);
98 * udev_device_get_driver:
99 * @udev_device: udev device
101 * Get the kernel driver name.
103 * Returns: the driver name string, or #NULL if there is no driver attached.
105 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
)
110 assert_return_errno(udev_device
, NULL
, EINVAL
);
112 r
= sd_device_get_driver(udev_device
->device
, &driver
);
122 * udev_device_get_devtype:
123 * @udev_device: udev device
125 * Retrieve the devtype string of the udev device.
127 * Returns: the devtype name of the udev device, or #NULL if it cannot be determined
129 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
)
134 assert_return_errno(udev_device
, NULL
, EINVAL
);
136 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
146 * udev_device_get_subsystem:
147 * @udev_device: udev device
149 * Retrieve the subsystem string of the udev device. The string does not
152 * Returns: the subsystem name of the udev device, or #NULL if it cannot be determined
154 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
)
156 const char *subsystem
;
159 assert_return_errno(udev_device
, NULL
, EINVAL
);
161 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
165 } else if (!subsystem
)
172 * udev_device_get_property_value:
173 * @udev_device: udev device
174 * @key: property name
176 * Get the value of a given property.
178 * Returns: the property string, or #NULL if there is no such property.
180 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
)
182 const char *value
= NULL
;
185 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
187 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
196 struct udev_device
*udev_device_new(struct udev
*udev
) {
197 struct udev_device
*udev_device
;
199 udev_device
= new0(struct udev_device
, 1);
204 udev_device
->refcount
= 1;
205 udev_device
->udev
= udev
;
206 udev_list_init(udev
, &udev_device
->properties
, true);
207 udev_list_init(udev
, &udev_device
->tags
, true);
208 udev_list_init(udev
, &udev_device
->sysattrs
, true);
209 udev_list_init(udev
, &udev_device
->devlinks
, true);
215 * udev_device_new_from_syspath:
216 * @udev: udev library context
217 * @syspath: sys device path including sys directory
219 * Create new udev device, and fill in information from the sys
220 * device and the udev database entry. The syspath is the absolute
221 * path to the device, including the sys mount point.
223 * The initial refcount is 1, and needs to be decremented to
224 * release the resources of the udev device.
226 * Returns: a new udev device, or #NULL, if it does not exist
228 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
) {
229 struct udev_device
*udev_device
;
232 udev_device
= udev_device_new(udev
);
236 r
= sd_device_new_from_syspath(&udev_device
->device
, syspath
);
239 udev_device_unref(udev_device
);
247 * udev_device_new_from_devnum:
248 * @udev: udev library context
249 * @type: char or block device
250 * @devnum: device major/minor number
252 * Create new udev device, and fill in information from the sys
253 * device and the udev database entry. The device is looked-up
254 * by its major/minor number and type. Character and block device
255 * numbers are not unique across the two types.
257 * The initial refcount is 1, and needs to be decremented to
258 * release the resources of the udev device.
260 * Returns: a new udev device, or #NULL, if it does not exist
262 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
)
264 struct udev_device
*udev_device
;
267 udev_device
= udev_device_new(udev
);
271 r
= sd_device_new_from_devnum(&udev_device
->device
, type
, devnum
);
274 udev_device_unref(udev_device
);
282 * udev_device_new_from_device_id:
283 * @udev: udev library context
284 * @id: text string identifying a kernel device
286 * Create new udev device, and fill in information from the sys
287 * device and the udev database entry. The device is looked-up
288 * by a special string:
289 * b8:2 - block device major:minor
290 * c128:1 - char device major:minor
291 * n3 - network device ifindex
292 * +sound:card29 - kernel driver core subsystem:device name
294 * The initial refcount is 1, and needs to be decremented to
295 * release the resources of the udev device.
297 * Returns: a new udev device, or #NULL, if it does not exist
299 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
)
301 struct udev_device
*udev_device
;
304 udev_device
= udev_device_new(udev
);
308 r
= sd_device_new_from_device_id(&udev_device
->device
, id
);
311 udev_device_unref(udev_device
);
319 * udev_device_new_from_subsystem_sysname:
320 * @udev: udev library context
321 * @subsystem: the subsystem of the device
322 * @sysname: the name of the device
324 * Create new udev device, and fill in information from the sys device
325 * and the udev database entry. The device is looked up by the subsystem
326 * and name string of the device, like "mem" / "zero", or "block" / "sda".
328 * The initial refcount is 1, and needs to be decremented to
329 * release the resources of the udev device.
331 * Returns: a new udev device, or #NULL, if it does not exist
333 _public_
struct udev_device
*udev_device_new_from_subsystem_sysname(struct udev
*udev
, const char *subsystem
, const char *sysname
)
335 struct udev_device
*udev_device
;
338 udev_device
= udev_device_new(udev
);
342 r
= sd_device_new_from_subsystem_sysname(&udev_device
->device
, subsystem
, sysname
);
345 udev_device_unref(udev_device
);
353 * udev_device_new_from_environment
354 * @udev: udev library context
356 * Create new udev device, and fill in information from the
357 * current process environment. This only works reliable if
358 * the process is called from a udev rule. It is usually used
359 * for tools executed from IMPORT= rules.
361 * The initial refcount is 1, and needs to be decremented to
362 * release the resources of the udev device.
364 * Returns: a new udev device, or #NULL, if it does not exist
366 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
)
368 struct udev_device
*udev_device
;
371 udev_device
= udev_device_new(udev
);
375 r
= device_new_from_strv(&udev_device
->device
, environ
);
378 udev_device_unref(udev_device
);
385 static struct udev_device
*device_new_from_parent(struct udev_device
*child
)
387 struct udev_device
*parent
;
390 assert_return_errno(child
, NULL
, EINVAL
);
392 parent
= udev_device_new(child
->udev
);
396 r
= sd_device_get_parent(child
->device
, &parent
->device
);
399 udev_device_unref(parent
);
403 /* the parent is unref'ed with the child, so take a ref from libudev as well */
404 sd_device_ref(parent
->device
);
410 * udev_device_get_parent:
411 * @udev_device: the device to start searching from
413 * Find the next parent device, and fill in information from the sys
414 * device and the udev database entry.
416 * Returned device is not referenced. It is attached to the child
417 * device, and will be cleaned up when the child device is cleaned up.
419 * It is not necessarily just the upper level directory, empty or not
420 * recognized sys directories are ignored.
422 * It can be called as many times as needed, without caring about
425 * Returns: a new udev device, or #NULL, if it no parent exist.
427 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
)
429 assert_return_errno(udev_device
, NULL
, EINVAL
);
431 if (!udev_device
->parent_set
) {
432 udev_device
->parent_set
= true;
433 udev_device
->parent
= device_new_from_parent(udev_device
);
436 /* TODO: errno will differ here in case parent == NULL */
437 return udev_device
->parent
;
441 * udev_device_get_parent_with_subsystem_devtype:
442 * @udev_device: udev device to start searching from
443 * @subsystem: the subsystem of the device
444 * @devtype: the type (DEVTYPE) of the device
446 * Find the next parent device, with a matching subsystem and devtype
447 * value, and fill in information from the sys device and the udev
450 * If devtype is #NULL, only subsystem is checked, and any devtype will
453 * Returned device is not referenced. It is attached to the child
454 * device, and will be cleaned up when the child device is cleaned up.
456 * It can be called as many times as needed, without caring about
459 * Returns: a new udev device, or #NULL if no matching parent exists.
461 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
)
466 assert_return_errno(udev_device
, NULL
, EINVAL
);
468 /* this relies on the fact that finding the subdevice of a parent or the
469 parent of a subdevice commute */
471 /* first find the correct sd_device */
472 r
= sd_device_get_parent_with_subsystem_devtype(udev_device
->device
, subsystem
, devtype
, &parent
);
478 /* then walk the chain of udev_device parents until the corresponding
480 while ((udev_device
= udev_device_get_parent(udev_device
))) {
481 if (udev_device
->device
== parent
)
490 * udev_device_get_udev:
491 * @udev_device: udev device
493 * Retrieve the udev library context the device was created with.
495 * Returns: the udev library context
497 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
)
499 assert_return_errno(udev_device
, NULL
, EINVAL
);
501 return udev_device
->udev
;
506 * @udev_device: udev device
508 * Take a reference of a udev device.
510 * Returns: the passed udev device
512 _public_
struct udev_device
*udev_device_ref(struct udev_device
*udev_device
)
515 udev_device
->refcount
++;
522 * @udev_device: udev device
524 * Drop a reference of a udev device. If the refcount reaches zero,
525 * the resources of the device will be released.
529 _public_
struct udev_device
*udev_device_unref(struct udev_device
*udev_device
)
531 if (udev_device
&& (-- udev_device
->refcount
) == 0) {
532 sd_device_unref(udev_device
->device
);
533 udev_device_unref(udev_device
->parent
);
535 udev_list_cleanup(&udev_device
->properties
);
536 udev_list_cleanup(&udev_device
->sysattrs
);
537 udev_list_cleanup(&udev_device
->tags
);
538 udev_list_cleanup(&udev_device
->devlinks
);
547 * udev_device_get_devpath:
548 * @udev_device: udev device
550 * Retrieve the kernel devpath value of the udev device. The path
551 * does not contain the sys mount point, and starts with a '/'.
553 * Returns: the devpath of the udev device
555 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
)
560 assert_return_errno(udev_device
, NULL
, EINVAL
);
562 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
572 * udev_device_get_syspath:
573 * @udev_device: udev device
575 * Retrieve the sys path of the udev device. The path is an
576 * absolute path and starts with the sys mount point.
578 * Returns: the sys path of the udev device
580 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
)
585 assert_return_errno(udev_device
, NULL
, EINVAL
);
587 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
597 * udev_device_get_sysname:
598 * @udev_device: udev device
600 * Get the kernel device name in /sys.
602 * Returns: the name string of the device
604 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
)
609 assert_return_errno(udev_device
, NULL
, EINVAL
);
611 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
621 * udev_device_get_sysnum:
622 * @udev_device: udev device
624 * Get the instance number of the device.
626 * Returns: the trailing number string of the device name
628 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
)
633 assert_return_errno(udev_device
, NULL
, EINVAL
);
635 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
645 * udev_device_get_devnode:
646 * @udev_device: udev device
648 * Retrieve the device node file name belonging to the udev device.
649 * The path is an absolute path, and starts with the device directory.
651 * Returns: the device node file name of the udev device, or #NULL if no device node exists
653 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
)
658 assert_return_errno(udev_device
, NULL
, EINVAL
);
660 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
670 * udev_device_get_devlinks_list_entry:
671 * @udev_device: udev device
673 * Retrieve the list of device links pointing to the device file of
674 * the udev device. The next list entry can be retrieved with
675 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
676 * The devlink path can be retrieved from the list entry by
677 * udev_list_entry_get_name(). The path is an absolute path, and starts with
678 * the device directory.
680 * Returns: the first entry of the device node link list
682 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
)
684 assert_return_errno(udev_device
, NULL
, EINVAL
);
686 if (device_get_devlinks_generation(udev_device
->device
) != udev_device
->devlinks_generation
||
687 !udev_device
->devlinks_read
) {
690 udev_list_cleanup(&udev_device
->devlinks
);
692 FOREACH_DEVICE_DEVLINK(udev_device
->device
, devlink
)
693 udev_list_entry_add(&udev_device
->devlinks
, devlink
, NULL
);
695 udev_device
->devlinks_read
= true;
696 udev_device
->devlinks_generation
= device_get_devlinks_generation(udev_device
->device
);
699 return udev_list_get_entry(&udev_device
->devlinks
);
703 * udev_device_get_event_properties_entry:
704 * @udev_device: udev device
706 * Retrieve the list of key/value device properties of the udev
707 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
708 * which returns #NULL if no more entries exist. The property name
709 * can be retrieved from the list entry by udev_list_entry_get_name(),
710 * the property value by udev_list_entry_get_value().
712 * Returns: the first entry of the property list
714 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
)
716 assert_return_errno(udev_device
, NULL
, EINVAL
);
718 if (device_get_properties_generation(udev_device
->device
) != udev_device
->properties_generation
||
719 !udev_device
->properties_read
) {
720 const char *key
, *value
;
722 udev_list_cleanup(&udev_device
->properties
);
724 FOREACH_DEVICE_PROPERTY(udev_device
->device
, key
, value
)
725 udev_list_entry_add(&udev_device
->properties
, key
, value
);
727 udev_device
->properties_read
= true;
728 udev_device
->properties_generation
= device_get_properties_generation(udev_device
->device
);
731 return udev_list_get_entry(&udev_device
->properties
);
735 * udev_device_get_action:
736 * @udev_device: udev device
738 * This is only valid if the device was received through a monitor. Devices read from
739 * sys do not have an action string. Usual actions are: add, remove, change, online,
742 * Returns: the kernel action value, or #NULL if there is no action value available.
744 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
) {
745 const char *action
= NULL
;
748 assert_return_errno(udev_device
, NULL
, EINVAL
);
750 r
= sd_device_get_property_value(udev_device
->device
, "ACTION", &action
);
751 if (r
< 0 && r
!= -ENOENT
) {
760 * udev_device_get_usec_since_initialized:
761 * @udev_device: udev device
763 * Return the number of microseconds passed since udev set up the
764 * device for the first time.
766 * This is only implemented for devices with need to store properties
767 * in the udev database. All other devices return 0 here.
769 * Returns: the number of microseconds since the device was first seen.
771 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
)
776 assert_return(udev_device
, -EINVAL
);
778 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
788 * udev_device_get_sysattr_value:
789 * @udev_device: udev device
790 * @sysattr: attribute name
792 * The retrieved value is cached in the device. Repeated calls will return the same
793 * value and not open the attribute again.
795 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
797 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
)
802 assert_return_errno(udev_device
, NULL
, EINVAL
);
804 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
814 * udev_device_set_sysattr_value:
815 * @udev_device: udev device
816 * @sysattr: attribute name
817 * @value: new value to be set
819 * Update the contents of the sys attribute and the cached value of the device.
821 * Returns: Negative error code on failure or 0 on success.
823 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, char *value
)
827 assert_return(udev_device
, -EINVAL
);
829 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
837 * udev_device_get_sysattr_list_entry:
838 * @udev_device: udev device
840 * Retrieve the list of available sysattrs, with value being empty;
841 * This just return all available sysfs attributes for a particular
842 * device without reading their values.
844 * Returns: the first entry of the property list
846 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
)
848 assert_return_errno(udev_device
, NULL
, EINVAL
);
850 if (!udev_device
->sysattrs_read
) {
853 udev_list_cleanup(&udev_device
->sysattrs
);
855 FOREACH_DEVICE_SYSATTR(udev_device
->device
, sysattr
)
856 udev_list_entry_add(&udev_device
->sysattrs
, sysattr
, NULL
);
858 udev_device
->sysattrs_read
= true;
861 return udev_list_get_entry(&udev_device
->sysattrs
);
865 * udev_device_get_is_initialized:
866 * @udev_device: udev device
868 * Check if udev has already handled the device and has set up
869 * device node permissions and context, or has renamed a network
872 * This is only implemented for devices with a device node
873 * or network interfaces. All other devices return 1 here.
875 * Returns: 1 if the device is set up. 0 otherwise.
877 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
)
881 assert_return(udev_device
, -EINVAL
);
883 r
= sd_device_get_is_initialized(udev_device
->device
, &initialized
);
894 * udev_device_get_tags_list_entry:
895 * @udev_device: udev device
897 * Retrieve the list of tags attached to the udev device. The next
898 * list entry can be retrieved with udev_list_entry_get_next(),
899 * which returns #NULL if no more entries exist. The tag string
900 * can be retrieved from the list entry by udev_list_entry_get_name().
902 * Returns: the first entry of the tag list
904 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
)
906 assert_return_errno(udev_device
, NULL
, EINVAL
);
908 if (device_get_tags_generation(udev_device
->device
) != udev_device
->tags_generation
||
909 !udev_device
->tags_read
) {
912 udev_list_cleanup(&udev_device
->tags
);
914 FOREACH_DEVICE_TAG(udev_device
->device
, tag
)
915 udev_list_entry_add(&udev_device
->tags
, tag
, NULL
);
917 udev_device
->tags_read
= true;
918 udev_device
->tags_generation
= device_get_tags_generation(udev_device
->device
);
921 return udev_list_get_entry(&udev_device
->tags
);
925 * udev_device_has_tag:
926 * @udev_device: udev device
929 * Check if a given device has a certain tag associated.
931 * Returns: 1 if the tag is found. 0 otherwise.
933 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
)
935 assert_return(udev_device
, 0);
937 return sd_device_has_tag(udev_device
->device
, tag
);