]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-device.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 Copyright © 2008-2012 Kay Sievers <kay@vrfy.org>
4 Copyright © 2015 Tom Gundersen <teg@jklm.no>
11 #include <linux/sockios.h>
18 #include <sys/ioctl.h>
19 #include <sys/socket.h>
24 #include "sd-device.h"
26 #include "alloc-util.h"
27 #include "device-private.h"
28 #include "device-util.h"
29 #include "libudev-device-internal.h"
30 #include "libudev-private.h"
31 #include "parse-util.h"
34 * SECTION:libudev-device
35 * @short_description: kernel sys devices
37 * Representation of kernel sys devices. Devices are uniquely identified
38 * by their syspath, every device has exactly one path in the kernel sys
39 * filesystem. Devices usually belong to a kernel subsystem, and have
40 * a unique name inside that subsystem.
44 * udev_device_get_seqnum:
45 * @udev_device: udev device
47 * This is only valid if the device was received through a monitor. Devices read from
48 * sys do not have a sequence number.
50 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
52 _public_
unsigned long long int udev_device_get_seqnum(struct udev_device
*udev_device
)
55 unsigned long long ret
;
58 assert_return_errno(udev_device
, 0, EINVAL
);
60 r
= sd_device_get_property_value(udev_device
->device
, "SEQNUM", &seqnum
);
68 r
= safe_atollu(seqnum
, &ret
);
78 * udev_device_get_devnum:
79 * @udev_device: udev device
81 * Get the device major/minor number.
83 * Returns: the dev_t number.
85 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
)
90 assert_return_errno(udev_device
, makedev(0, 0), EINVAL
);
92 r
= sd_device_get_devnum(udev_device
->device
, &devnum
);
102 * udev_device_get_driver:
103 * @udev_device: udev device
105 * Get the kernel driver name.
107 * Returns: the driver name string, or #NULL if there is no driver attached.
109 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
)
114 assert_return_errno(udev_device
, NULL
, EINVAL
);
116 r
= sd_device_get_driver(udev_device
->device
, &driver
);
126 * udev_device_get_devtype:
127 * @udev_device: udev device
129 * Retrieve the devtype string of the udev device.
131 * Returns: the devtype name of the udev device, or #NULL if it cannot be determined
133 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
)
138 assert_return_errno(udev_device
, NULL
, EINVAL
);
140 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
150 * udev_device_get_subsystem:
151 * @udev_device: udev device
153 * Retrieve the subsystem string of the udev device. The string does not
156 * Returns: the subsystem name of the udev device, or #NULL if it cannot be determined
158 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
)
160 const char *subsystem
;
163 assert_return_errno(udev_device
, NULL
, EINVAL
);
165 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
169 } else if (!subsystem
)
176 * udev_device_get_property_value:
177 * @udev_device: udev device
178 * @key: property name
180 * Get the value of a given property.
182 * Returns: the property string, or #NULL if there is no such property.
184 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
)
186 const char *value
= NULL
;
189 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
191 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
200 struct udev_device
*udev_device_new(struct udev
*udev
) {
201 struct udev_device
*udev_device
;
203 assert_return_errno(udev
, NULL
, EINVAL
);
205 udev_device
= new0(struct udev_device
, 1);
210 udev_device
->refcount
= 1;
211 udev_device
->udev
= udev
;
212 udev_list_init(udev
, &udev_device
->properties
, true);
213 udev_list_init(udev
, &udev_device
->tags
, true);
214 udev_list_init(udev
, &udev_device
->sysattrs
, true);
215 udev_list_init(udev
, &udev_device
->devlinks
, true);
221 * udev_device_new_from_syspath:
222 * @udev: udev library context
223 * @syspath: sys device path including sys directory
225 * Create new udev device, and fill in information from the sys
226 * device and the udev database entry. The syspath is the absolute
227 * path to the device, including the sys mount point.
229 * The initial refcount is 1, and needs to be decremented to
230 * release the resources of the udev device.
232 * Returns: a new udev device, or #NULL, if it does not exist
234 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
) {
235 struct udev_device
*udev_device
;
238 udev_device
= udev_device_new(udev
);
242 r
= sd_device_new_from_syspath(&udev_device
->device
, syspath
);
245 udev_device_unref(udev_device
);
253 * udev_device_new_from_devnum:
254 * @udev: udev library context
255 * @type: char or block device
256 * @devnum: device major/minor number
258 * Create new udev device, and fill in information from the sys
259 * device and the udev database entry. The device is looked-up
260 * by its major/minor number and type. Character and block device
261 * numbers are not unique across the two types.
263 * The initial refcount is 1, and needs to be decremented to
264 * release the resources of the udev device.
266 * Returns: a new udev device, or #NULL, if it does not exist
268 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
)
270 struct udev_device
*udev_device
;
273 udev_device
= udev_device_new(udev
);
277 r
= sd_device_new_from_devnum(&udev_device
->device
, type
, devnum
);
280 udev_device_unref(udev_device
);
288 * udev_device_new_from_device_id:
289 * @udev: udev library context
290 * @id: text string identifying a kernel device
292 * Create new udev device, and fill in information from the sys
293 * device and the udev database entry. The device is looked-up
294 * by a special string:
295 * b8:2 - block device major:minor
296 * c128:1 - char device major:minor
297 * n3 - network device ifindex
298 * +sound:card29 - kernel driver core subsystem:device name
300 * The initial refcount is 1, and needs to be decremented to
301 * release the resources of the udev device.
303 * Returns: a new udev device, or #NULL, if it does not exist
305 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
)
307 struct udev_device
*udev_device
;
310 udev_device
= udev_device_new(udev
);
314 r
= sd_device_new_from_device_id(&udev_device
->device
, id
);
317 udev_device_unref(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
)
341 struct udev_device
*udev_device
;
344 udev_device
= udev_device_new(udev
);
348 r
= sd_device_new_from_subsystem_sysname(&udev_device
->device
, subsystem
, sysname
);
351 udev_device_unref(udev_device
);
359 * udev_device_new_from_environment
360 * @udev: udev library context
362 * Create new udev device, and fill in information from the
363 * current process environment. This only works reliable if
364 * the process is called from a udev rule. It is usually used
365 * for tools executed from IMPORT= rules.
367 * The initial refcount is 1, and needs to be decremented to
368 * release the resources of the udev device.
370 * Returns: a new udev device, or #NULL, if it does not exist
372 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
)
374 struct udev_device
*udev_device
;
377 udev_device
= udev_device_new(udev
);
381 r
= device_new_from_strv(&udev_device
->device
, environ
);
384 udev_device_unref(udev_device
);
391 static struct udev_device
*device_new_from_parent(struct udev_device
*child
)
393 struct udev_device
*parent
;
396 assert_return_errno(child
, NULL
, EINVAL
);
398 parent
= udev_device_new(child
->udev
);
402 r
= sd_device_get_parent(child
->device
, &parent
->device
);
405 udev_device_unref(parent
);
409 /* the parent is unref'ed with the child, so take a ref from libudev as well */
410 sd_device_ref(parent
->device
);
416 * udev_device_get_parent:
417 * @udev_device: the device to start searching from
419 * Find the next parent device, and fill in information from the sys
420 * device and the udev database entry.
422 * Returned device is not referenced. It is attached to the child
423 * device, and will be cleaned up when the child device is cleaned up.
425 * It is not necessarily just the upper level directory, empty or not
426 * recognized sys directories are ignored.
428 * It can be called as many times as needed, without caring about
431 * Returns: a new udev device, or #NULL, if it no parent exist.
433 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
)
435 assert_return_errno(udev_device
, NULL
, EINVAL
);
437 if (!udev_device
->parent_set
) {
438 udev_device
->parent_set
= true;
439 udev_device
->parent
= device_new_from_parent(udev_device
);
442 /* TODO: errno will differ here in case parent == NULL */
443 return udev_device
->parent
;
447 * udev_device_get_parent_with_subsystem_devtype:
448 * @udev_device: udev device to start searching from
449 * @subsystem: the subsystem of the device
450 * @devtype: the type (DEVTYPE) of the device
452 * Find the next parent device, with a matching subsystem and devtype
453 * value, and fill in information from the sys device and the udev
456 * If devtype is #NULL, only subsystem is checked, and any devtype will
459 * Returned device is not referenced. It is attached to the child
460 * device, and will be cleaned up when the child device is cleaned up.
462 * It can be called as many times as needed, without caring about
465 * Returns: a new udev device, or #NULL if no matching parent exists.
467 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
)
472 assert_return_errno(udev_device
, NULL
, EINVAL
);
474 /* this relies on the fact that finding the subdevice of a parent or the
475 parent of a subdevice commute */
477 /* first find the correct sd_device */
478 r
= sd_device_get_parent_with_subsystem_devtype(udev_device
->device
, subsystem
, devtype
, &parent
);
484 /* then walk the chain of udev_device parents until the corresponding
486 while ((udev_device
= udev_device_get_parent(udev_device
))) {
487 if (udev_device
->device
== parent
)
496 * udev_device_get_udev:
497 * @udev_device: udev device
499 * Retrieve the udev library context the device was created with.
501 * Returns: the udev library context
503 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
)
505 assert_return_errno(udev_device
, NULL
, EINVAL
);
507 return udev_device
->udev
;
512 * @udev_device: udev device
514 * Take a reference of a udev device.
516 * Returns: the passed udev device
518 _public_
struct udev_device
*udev_device_ref(struct udev_device
*udev_device
)
521 udev_device
->refcount
++;
528 * @udev_device: udev device
530 * Drop a reference of a udev device. If the refcount reaches zero,
531 * the resources of the device will be released.
535 _public_
struct udev_device
*udev_device_unref(struct udev_device
*udev_device
)
537 if (udev_device
&& (-- udev_device
->refcount
) == 0) {
538 sd_device_unref(udev_device
->device
);
539 udev_device_unref(udev_device
->parent
);
541 udev_list_cleanup(&udev_device
->properties
);
542 udev_list_cleanup(&udev_device
->sysattrs
);
543 udev_list_cleanup(&udev_device
->tags
);
544 udev_list_cleanup(&udev_device
->devlinks
);
553 * udev_device_get_devpath:
554 * @udev_device: udev device
556 * Retrieve the kernel devpath value of the udev device. The path
557 * does not contain the sys mount point, and starts with a '/'.
559 * Returns: the devpath of the udev device
561 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
)
566 assert_return_errno(udev_device
, NULL
, EINVAL
);
568 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
578 * udev_device_get_syspath:
579 * @udev_device: udev device
581 * Retrieve the sys path of the udev device. The path is an
582 * absolute path and starts with the sys mount point.
584 * Returns: the sys path of the udev device
586 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
)
591 assert_return_errno(udev_device
, NULL
, EINVAL
);
593 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
603 * udev_device_get_sysname:
604 * @udev_device: udev device
606 * Get the kernel device name in /sys.
608 * Returns: the name string of the device
610 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
)
615 assert_return_errno(udev_device
, NULL
, EINVAL
);
617 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
627 * udev_device_get_sysnum:
628 * @udev_device: udev device
630 * Get the instance number of the device.
632 * Returns: the trailing number string of the device name
634 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
)
639 assert_return_errno(udev_device
, NULL
, EINVAL
);
641 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
651 * udev_device_get_devnode:
652 * @udev_device: udev device
654 * Retrieve the device node file name belonging to the udev device.
655 * The path is an absolute path, and starts with the device directory.
657 * Returns: the device node file name of the udev device, or #NULL if no device node exists
659 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
)
664 assert_return_errno(udev_device
, NULL
, EINVAL
);
666 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
676 * udev_device_get_devlinks_list_entry:
677 * @udev_device: udev device
679 * Retrieve the list of device links pointing to the device file of
680 * the udev device. The next list entry can be retrieved with
681 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
682 * The devlink path can be retrieved from the list entry by
683 * udev_list_entry_get_name(). The path is an absolute path, and starts with
684 * the device directory.
686 * Returns: the first entry of the device node link list
688 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
)
690 assert_return_errno(udev_device
, NULL
, EINVAL
);
692 if (device_get_devlinks_generation(udev_device
->device
) != udev_device
->devlinks_generation
||
693 !udev_device
->devlinks_read
) {
696 udev_list_cleanup(&udev_device
->devlinks
);
698 FOREACH_DEVICE_DEVLINK(udev_device
->device
, devlink
)
699 udev_list_entry_add(&udev_device
->devlinks
, devlink
, NULL
);
701 udev_device
->devlinks_read
= true;
702 udev_device
->devlinks_generation
= device_get_devlinks_generation(udev_device
->device
);
705 return udev_list_get_entry(&udev_device
->devlinks
);
709 * udev_device_get_event_properties_entry:
710 * @udev_device: udev device
712 * Retrieve the list of key/value device properties of the udev
713 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
714 * which returns #NULL if no more entries exist. The property name
715 * can be retrieved from the list entry by udev_list_entry_get_name(),
716 * the property value by udev_list_entry_get_value().
718 * Returns: the first entry of the property list
720 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
)
722 assert_return_errno(udev_device
, NULL
, EINVAL
);
724 if (device_get_properties_generation(udev_device
->device
) != udev_device
->properties_generation
||
725 !udev_device
->properties_read
) {
726 const char *key
, *value
;
728 udev_list_cleanup(&udev_device
->properties
);
730 FOREACH_DEVICE_PROPERTY(udev_device
->device
, key
, value
)
731 udev_list_entry_add(&udev_device
->properties
, key
, value
);
733 udev_device
->properties_read
= true;
734 udev_device
->properties_generation
= device_get_properties_generation(udev_device
->device
);
737 return udev_list_get_entry(&udev_device
->properties
);
741 * udev_device_get_action:
742 * @udev_device: udev device
744 * This is only valid if the device was received through a monitor. Devices read from
745 * sys do not have an action string. Usual actions are: add, remove, change, online,
748 * Returns: the kernel action value, or #NULL if there is no action value available.
750 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
) {
751 const char *action
= NULL
;
754 assert_return_errno(udev_device
, NULL
, EINVAL
);
756 r
= sd_device_get_property_value(udev_device
->device
, "ACTION", &action
);
757 if (r
< 0 && r
!= -ENOENT
) {
766 * udev_device_get_usec_since_initialized:
767 * @udev_device: udev device
769 * Return the number of microseconds passed since udev set up the
770 * device for the first time.
772 * This is only implemented for devices with need to store properties
773 * in the udev database. All other devices return 0 here.
775 * Returns: the number of microseconds since the device was first seen.
777 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
)
782 assert_return(udev_device
, -EINVAL
);
784 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
794 * udev_device_get_sysattr_value:
795 * @udev_device: udev device
796 * @sysattr: attribute name
798 * The retrieved value is cached in the device. Repeated calls will return the same
799 * value and not open the attribute again.
801 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
803 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
)
808 assert_return_errno(udev_device
, NULL
, EINVAL
);
810 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
820 * udev_device_set_sysattr_value:
821 * @udev_device: udev device
822 * @sysattr: attribute name
823 * @value: new value to be set
825 * Update the contents of the sys attribute and the cached value of the device.
827 * Returns: Negative error code on failure or 0 on success.
829 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, char *value
)
833 assert_return(udev_device
, -EINVAL
);
835 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
843 * udev_device_get_sysattr_list_entry:
844 * @udev_device: udev device
846 * Retrieve the list of available sysattrs, with value being empty;
847 * This just return all available sysfs attributes for a particular
848 * device without reading their values.
850 * Returns: the first entry of the property list
852 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
)
854 assert_return_errno(udev_device
, NULL
, EINVAL
);
856 if (!udev_device
->sysattrs_read
) {
859 udev_list_cleanup(&udev_device
->sysattrs
);
861 FOREACH_DEVICE_SYSATTR(udev_device
->device
, sysattr
)
862 udev_list_entry_add(&udev_device
->sysattrs
, sysattr
, NULL
);
864 udev_device
->sysattrs_read
= true;
867 return udev_list_get_entry(&udev_device
->sysattrs
);
871 * udev_device_get_is_initialized:
872 * @udev_device: udev device
874 * Check if udev has already handled the device and has set up
875 * device node permissions and context, or has renamed a network
878 * This is only implemented for devices with a device node
879 * or network interfaces. All other devices return 1 here.
881 * Returns: 1 if the device is set up. 0 otherwise.
883 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
)
887 assert_return(udev_device
, -EINVAL
);
889 r
= sd_device_get_is_initialized(udev_device
->device
, &initialized
);
900 * udev_device_get_tags_list_entry:
901 * @udev_device: udev device
903 * Retrieve the list of tags attached to the udev device. The next
904 * list entry can be retrieved with udev_list_entry_get_next(),
905 * which returns #NULL if no more entries exist. The tag string
906 * can be retrieved from the list entry by udev_list_entry_get_name().
908 * Returns: the first entry of the tag list
910 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
)
912 assert_return_errno(udev_device
, NULL
, EINVAL
);
914 if (device_get_tags_generation(udev_device
->device
) != udev_device
->tags_generation
||
915 !udev_device
->tags_read
) {
918 udev_list_cleanup(&udev_device
->tags
);
920 FOREACH_DEVICE_TAG(udev_device
->device
, tag
)
921 udev_list_entry_add(&udev_device
->tags
, tag
, NULL
);
923 udev_device
->tags_read
= true;
924 udev_device
->tags_generation
= device_get_tags_generation(udev_device
->device
);
927 return udev_list_get_entry(&udev_device
->tags
);
931 * udev_device_has_tag:
932 * @udev_device: udev device
935 * Check if a given device has a certain tag associated.
937 * Returns: 1 if the tag is found. 0 otherwise.
939 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
)
941 assert_return(udev_device
, 0);
943 return sd_device_has_tag(udev_device
->device
, tag
);