]>
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-list-internal.h"
27 #include "parse-util.h"
28 #include "time-util.h"
31 * SECTION:libudev-device
32 * @short_description: kernel sys devices
34 * Representation of kernel sys devices. Devices are uniquely identified
35 * by their syspath, every device has exactly one path in the kernel sys
36 * filesystem. Devices usually belong to a kernel subsystem, and have
37 * a unique name inside that subsystem.
43 * Opaque object representing one kernel sys device.
48 /* real device object */
54 struct udev_device
*parent
;
57 struct udev_list
*properties
;
58 uint64_t properties_generation
;
59 struct udev_list
*tags
;
60 uint64_t tags_generation
;
61 struct udev_list
*devlinks
;
62 uint64_t devlinks_generation
;
63 bool properties_read
:1;
66 struct udev_list
*sysattrs
;
71 * udev_device_get_seqnum:
72 * @udev_device: udev device
74 * This is only valid if the device was received through a monitor. Devices read from
75 * sys do not have a sequence number.
77 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
79 _public_
unsigned long long udev_device_get_seqnum(struct udev_device
*udev_device
) {
82 assert_return_errno(udev_device
, 0, EINVAL
);
84 if (device_get_seqnum(udev_device
->device
, &seqnum
) < 0)
91 * udev_device_get_devnum:
92 * @udev_device: udev device
94 * Get the device major/minor number.
96 * Returns: the dev_t number.
98 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
) {
102 assert_return_errno(udev_device
, makedev(0, 0), EINVAL
);
104 r
= sd_device_get_devnum(udev_device
->device
, &devnum
);
106 return makedev(0, 0);
108 return_with_errno(makedev(0, 0), r
);
114 * udev_device_get_driver:
115 * @udev_device: udev device
117 * Get the kernel driver name.
119 * Returns: the driver name string, or #NULL if there is no driver attached.
121 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
) {
125 assert_return_errno(udev_device
, NULL
, EINVAL
);
127 r
= sd_device_get_driver(udev_device
->device
, &driver
);
129 return_with_errno(NULL
, r
);
135 * udev_device_get_devtype:
136 * @udev_device: udev device
138 * Retrieve the devtype string of the udev device.
140 * Returns: the devtype name of the udev device, or #NULL if it cannot be determined
142 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
) {
146 assert_return_errno(udev_device
, NULL
, EINVAL
);
148 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
152 return_with_errno(NULL
, r
);
158 * udev_device_get_subsystem:
159 * @udev_device: udev device
161 * Retrieve the subsystem string of the udev device. The string does not
164 * Returns: the subsystem name of the udev device, or #NULL if it cannot be determined
166 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
) {
167 const char *subsystem
;
170 assert_return_errno(udev_device
, NULL
, EINVAL
);
172 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
174 return_with_errno(NULL
, r
);
180 * udev_device_get_property_value:
181 * @udev_device: udev device
182 * @key: property name
184 * Get the value of a given property.
186 * Returns: the property string, or #NULL if there is no such property.
188 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
) {
192 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
194 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
196 return_with_errno(NULL
, r
);
201 struct udev_device
*udev_device_new(struct udev
*udev
, sd_device
*device
) {
202 _cleanup_(udev_list_freep
) struct udev_list
*properties
= NULL
, *tags
= NULL
, *sysattrs
= NULL
, *devlinks
= NULL
;
203 struct udev_device
*udev_device
;
207 properties
= udev_list_new(true);
209 return_with_errno(NULL
, ENOMEM
);
210 tags
= udev_list_new(true);
212 return_with_errno(NULL
, ENOMEM
);
213 sysattrs
= udev_list_new(true);
215 return_with_errno(NULL
, ENOMEM
);
216 devlinks
= udev_list_new(true);
218 return_with_errno(NULL
, ENOMEM
);
220 udev_device
= new(struct udev_device
, 1);
222 return_with_errno(NULL
, ENOMEM
);
224 *udev_device
= (struct udev_device
) {
227 .device
= sd_device_ref(device
),
228 .properties
= TAKE_PTR(properties
),
229 .tags
= TAKE_PTR(tags
),
230 .sysattrs
= TAKE_PTR(sysattrs
),
231 .devlinks
= TAKE_PTR(devlinks
),
238 * udev_device_new_from_syspath:
239 * @udev: udev library context
240 * @syspath: sys device path including sys directory
242 * Create new udev device, and fill in information from the sys
243 * device and the udev database entry. The syspath is the absolute
244 * path to the device, including the sys mount point.
246 * The initial refcount is 1, and needs to be decremented to
247 * release the resources of the udev device.
249 * Returns: a new udev device, or #NULL, if it does not exist
251 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
) {
252 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
255 r
= sd_device_new_from_syspath(&device
, syspath
);
257 return_with_errno(NULL
, r
);
259 return udev_device_new(udev
, device
);
263 * udev_device_new_from_devnum:
264 * @udev: udev library context
265 * @type: char or block device
266 * @devnum: device major/minor number
268 * Create new udev device, and fill in information from the sys
269 * device and the udev database entry. The device is looked-up
270 * by its major/minor number and type. Character and block device
271 * numbers are not unique across the two types.
273 * The initial refcount is 1, and needs to be decremented to
274 * release the resources of the udev device.
276 * Returns: a new udev device, or #NULL, if it does not exist
278 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
) {
279 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
282 r
= sd_device_new_from_devnum(&device
, type
, devnum
);
284 return_with_errno(NULL
, r
);
286 return udev_device_new(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
) {
308 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
311 r
= sd_device_new_from_device_id(&device
, id
);
313 return_with_errno(NULL
, r
);
315 return udev_device_new(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
) {
334 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
337 r
= sd_device_new_from_subsystem_sysname(&device
, subsystem
, sysname
);
339 return_with_errno(NULL
, r
);
341 return udev_device_new(udev
, device
);
345 * udev_device_new_from_environment
346 * @udev: udev library context
348 * Create new udev device, and fill in information from the
349 * current process environment. This only works reliable if
350 * the process is called from a udev rule. It is usually used
351 * for tools executed from IMPORT= rules.
353 * The initial refcount is 1, and needs to be decremented to
354 * release the resources of the udev device.
356 * Returns: a new udev device, or #NULL, if it does not exist
358 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
) {
359 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
362 r
= device_new_from_strv(&device
, environ
);
364 return_with_errno(NULL
, r
);
366 return udev_device_new(udev
, device
);
369 static struct udev_device
*device_new_from_parent(struct udev_device
*child
) {
373 assert_return_errno(child
, NULL
, EINVAL
);
375 r
= sd_device_get_parent(child
->device
, &parent
);
377 return_with_errno(NULL
, r
);
379 return udev_device_new(child
->udev
, parent
);
383 * udev_device_get_parent:
384 * @udev_device: the device to start searching from
386 * Find the next parent device, and fill in information from the sys
387 * device and the udev database entry.
389 * Returned device is not referenced. It is attached to the child
390 * device, and will be cleaned up when the child device is cleaned up.
392 * It is not necessarily just the upper level directory, empty or not
393 * recognized sys directories are ignored.
395 * It can be called as many times as needed, without caring about
398 * Returns: a new udev device, or #NULL, if it no parent exist.
400 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
) {
401 assert_return_errno(udev_device
, NULL
, EINVAL
);
403 if (!udev_device
->parent_set
) {
404 udev_device
->parent_set
= true;
405 udev_device
->parent
= device_new_from_parent(udev_device
);
408 /* TODO: errno will differ here in case parent == NULL */
409 return udev_device
->parent
;
413 * udev_device_get_parent_with_subsystem_devtype:
414 * @udev_device: udev device to start searching from
415 * @subsystem: the subsystem of the device
416 * @devtype: the type (DEVTYPE) of the device
418 * Find the next parent device, with a matching subsystem and devtype
419 * value, and fill in information from the sys device and the udev
422 * If devtype is #NULL, only subsystem is checked, and any devtype will
425 * Returned device is not referenced. It is attached to the child
426 * device, and will be cleaned up when the child device is cleaned up.
428 * It can be called as many times as needed, without caring about
431 * Returns: a new udev device, or #NULL if no matching parent exists.
433 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
) {
437 assert_return_errno(udev_device
, NULL
, EINVAL
);
439 /* this relies on the fact that finding the subdevice of a parent or the
440 parent of a subdevice commute */
442 /* first find the correct sd_device */
443 r
= sd_device_get_parent_with_subsystem_devtype(udev_device
->device
, subsystem
, devtype
, &parent
);
445 return_with_errno(NULL
, r
);
447 /* then walk the chain of udev_device parents until the corresponding
449 while ((udev_device
= udev_device_get_parent(udev_device
)))
450 if (udev_device
->device
== parent
)
453 return_with_errno(NULL
, ENOENT
);
457 * udev_device_get_udev:
458 * @udev_device: udev device
460 * Retrieve the udev library context the device was created with.
462 * Returns: the udev library context
464 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
) {
465 assert_return_errno(udev_device
, NULL
, EINVAL
);
467 return udev_device
->udev
;
470 static struct udev_device
*udev_device_free(struct udev_device
*udev_device
) {
473 sd_device_unref(udev_device
->device
);
474 udev_device_unref(udev_device
->parent
);
476 udev_list_free(udev_device
->properties
);
477 udev_list_free(udev_device
->sysattrs
);
478 udev_list_free(udev_device
->tags
);
479 udev_list_free(udev_device
->devlinks
);
481 return mfree(udev_device
);
486 * @udev_device: udev device
488 * Take a reference of a udev device.
490 * Returns: the passed udev device
495 * @udev_device: udev device
497 * Drop a reference of a udev device. If the refcount reaches zero,
498 * the resources of the device will be released.
502 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_device
, udev_device
, udev_device_free
);
505 * udev_device_get_devpath:
506 * @udev_device: udev device
508 * Retrieve the kernel devpath value of the udev device. The path
509 * does not contain the sys mount point, and starts with a '/'.
511 * Returns: the devpath of the udev device
513 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
) {
517 assert_return_errno(udev_device
, NULL
, EINVAL
);
519 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
521 return_with_errno(NULL
, r
);
527 * udev_device_get_syspath:
528 * @udev_device: udev device
530 * Retrieve the sys path of the udev device. The path is an
531 * absolute path and starts with the sys mount point.
533 * Returns: the sys path of the udev device
535 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
) {
539 assert_return_errno(udev_device
, NULL
, EINVAL
);
541 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
543 return_with_errno(NULL
, r
);
549 * udev_device_get_sysname:
550 * @udev_device: udev device
552 * Get the kernel device name in /sys.
554 * Returns: the name string of the device
556 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
) {
560 assert_return_errno(udev_device
, NULL
, EINVAL
);
562 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
564 return_with_errno(NULL
, r
);
570 * udev_device_get_sysnum:
571 * @udev_device: udev device
573 * Get the instance number of the device.
575 * Returns: the trailing number string of the device name
577 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
) {
581 assert_return_errno(udev_device
, NULL
, EINVAL
);
583 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
587 return_with_errno(NULL
, r
);
593 * udev_device_get_devnode:
594 * @udev_device: udev device
596 * Retrieve the device node file name belonging to the udev device.
597 * The path is an absolute path, and starts with the device directory.
599 * Returns: the device node file name of the udev device, or #NULL if no device node exists
601 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
) {
605 assert_return_errno(udev_device
, NULL
, EINVAL
);
607 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
609 return_with_errno(NULL
, r
);
615 * udev_device_get_devlinks_list_entry:
616 * @udev_device: udev device
618 * Retrieve the list of device links pointing to the device file of
619 * the udev device. The next list entry can be retrieved with
620 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
621 * The devlink path can be retrieved from the list entry by
622 * udev_list_entry_get_name(). The path is an absolute path, and starts with
623 * the device directory.
625 * Returns: the first entry of the device node link list
627 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
) {
628 assert_return_errno(udev_device
, NULL
, EINVAL
);
630 if (device_get_devlinks_generation(udev_device
->device
) != udev_device
->devlinks_generation
||
631 !udev_device
->devlinks_read
) {
634 udev_list_cleanup(udev_device
->devlinks
);
636 FOREACH_DEVICE_DEVLINK(udev_device
->device
, devlink
)
637 if (!udev_list_entry_add(udev_device
->devlinks
, devlink
, NULL
))
638 return_with_errno(NULL
, ENOMEM
);
640 udev_device
->devlinks_read
= true;
641 udev_device
->devlinks_generation
= device_get_devlinks_generation(udev_device
->device
);
644 return udev_list_get_entry(udev_device
->devlinks
);
648 * udev_device_get_event_properties_entry:
649 * @udev_device: udev device
651 * Retrieve the list of key/value device properties of the udev
652 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
653 * which returns #NULL if no more entries exist. The property name
654 * can be retrieved from the list entry by udev_list_entry_get_name(),
655 * the property value by udev_list_entry_get_value().
657 * Returns: the first entry of the property list
659 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
) {
660 assert_return_errno(udev_device
, NULL
, EINVAL
);
662 if (device_get_properties_generation(udev_device
->device
) != udev_device
->properties_generation
||
663 !udev_device
->properties_read
) {
664 const char *key
, *value
;
666 udev_list_cleanup(udev_device
->properties
);
668 FOREACH_DEVICE_PROPERTY(udev_device
->device
, key
, value
)
669 if (!udev_list_entry_add(udev_device
->properties
, key
, value
))
670 return_with_errno(NULL
, ENOMEM
);
672 udev_device
->properties_read
= true;
673 udev_device
->properties_generation
= device_get_properties_generation(udev_device
->device
);
676 return udev_list_get_entry(udev_device
->properties
);
680 * udev_device_get_action:
681 * @udev_device: udev device
683 * This is only valid if the device was received through a monitor. Devices read from
684 * sys do not have an action string. Usual actions are: add, remove, change, online,
687 * Returns: the kernel action value, or #NULL if there is no action value available.
689 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
) {
692 assert_return_errno(udev_device
, NULL
, EINVAL
);
694 if (device_get_action(udev_device
->device
, &action
) < 0)
697 return device_action_to_string(action
);
701 * udev_device_get_usec_since_initialized:
702 * @udev_device: udev device
704 * Return the number of microseconds passed since udev set up the
705 * device for the first time.
707 * This is only implemented for devices with need to store properties
708 * in the udev database. All other devices return 0 here.
710 * Returns: the number of microseconds since the device was first seen.
712 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
) {
716 assert_return(udev_device
, -EINVAL
);
718 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
720 return_with_errno(0, r
);
726 * udev_device_get_sysattr_value:
727 * @udev_device: udev device
728 * @sysattr: attribute name
730 * The retrieved value is cached in the device. Repeated calls will return the same
731 * value and not open the attribute again.
733 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
735 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
) {
739 assert_return_errno(udev_device
, NULL
, EINVAL
);
741 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
743 return_with_errno(NULL
, r
);
749 * udev_device_set_sysattr_value:
750 * @udev_device: udev device
751 * @sysattr: attribute name
752 * @value: new value to be set
754 * Update the contents of the sys attribute and the cached value of the device.
756 * Returns: Negative error code on failure or 0 on success.
758 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, const char *value
) {
761 assert_return(udev_device
, -EINVAL
);
763 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
771 * udev_device_get_sysattr_list_entry:
772 * @udev_device: udev device
774 * Retrieve the list of available sysattrs, with value being empty;
775 * This just return all available sysfs attributes for a particular
776 * device without reading their values.
778 * Returns: the first entry of the property list
780 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
) {
781 assert_return_errno(udev_device
, NULL
, EINVAL
);
783 if (!udev_device
->sysattrs_read
) {
786 udev_list_cleanup(udev_device
->sysattrs
);
788 FOREACH_DEVICE_SYSATTR(udev_device
->device
, sysattr
)
789 if (!udev_list_entry_add(udev_device
->sysattrs
, sysattr
, NULL
))
790 return_with_errno(NULL
, ENOMEM
);
792 udev_device
->sysattrs_read
= true;
795 return udev_list_get_entry(udev_device
->sysattrs
);
799 * udev_device_get_is_initialized:
800 * @udev_device: udev device
802 * Check if udev has already handled the device and has set up
803 * device node permissions and context, or has renamed a network
806 * This is only implemented for devices with a device node
807 * or network interfaces. All other devices return 1 here.
809 * Returns: 1 if the device is set up. 0 otherwise.
811 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
) {
814 assert_return(udev_device
, -EINVAL
);
816 r
= sd_device_get_is_initialized(udev_device
->device
);
818 return_with_errno(0, r
);
824 * udev_device_get_tags_list_entry:
825 * @udev_device: udev device
827 * Retrieve the list of tags attached to the udev device. The next
828 * list entry can be retrieved with udev_list_entry_get_next(),
829 * which returns #NULL if no more entries exist. The tag string
830 * can be retrieved from the list entry by udev_list_entry_get_name().
832 * Returns: the first entry of the tag list
834 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
) {
835 assert_return_errno(udev_device
, NULL
, EINVAL
);
837 if (device_get_tags_generation(udev_device
->device
) != udev_device
->tags_generation
||
838 !udev_device
->tags_read
) {
841 udev_list_cleanup(udev_device
->tags
);
843 FOREACH_DEVICE_TAG(udev_device
->device
, tag
)
844 if (!udev_list_entry_add(udev_device
->tags
, tag
, NULL
))
845 return_with_errno(NULL
, ENOMEM
);
847 udev_device
->tags_read
= true;
848 udev_device
->tags_generation
= device_get_tags_generation(udev_device
->device
);
851 return udev_list_get_entry(udev_device
->tags
);
855 * udev_device_has_tag:
856 * @udev_device: udev device
859 * Check if a given device has a certain tag associated.
861 * Returns: 1 if the tag is found. 0 otherwise.
863 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
) {
864 assert_return(udev_device
, 0);
866 return sd_device_has_tag(udev_device
->device
, tag
) > 0;
869 sd_device
*udev_device_get_sd_device(struct udev_device
*udev_device
) {
872 return udev_device
->device
;