]>
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 "parse-util.h"
27 #include "time-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
) {
50 unsigned long long ret
;
53 assert_return_errno(udev_device
, 0, EINVAL
);
55 r
= sd_device_get_property_value(udev_device
->device
, "SEQNUM", &seqnum
);
59 return_with_errno(0, r
);
61 r
= safe_atollu(seqnum
, &ret
);
63 return_with_errno(0, r
);
69 * udev_device_get_devnum:
70 * @udev_device: udev device
72 * Get the device major/minor number.
74 * Returns: the dev_t number.
76 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
) {
80 assert_return_errno(udev_device
, makedev(0, 0), EINVAL
);
82 r
= sd_device_get_devnum(udev_device
->device
, &devnum
);
86 return_with_errno(makedev(0, 0), r
);
92 * udev_device_get_driver:
93 * @udev_device: udev device
95 * Get the kernel driver name.
97 * Returns: the driver name string, or #NULL if there is no driver attached.
99 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
) {
103 assert_return_errno(udev_device
, NULL
, EINVAL
);
105 r
= sd_device_get_driver(udev_device
->device
, &driver
);
107 return_with_errno(NULL
, r
);
113 * udev_device_get_devtype:
114 * @udev_device: udev device
116 * Retrieve the devtype string of the udev device.
118 * Returns: the devtype name of the udev device, or #NULL if it cannot be determined
120 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
) {
124 assert_return_errno(udev_device
, NULL
, EINVAL
);
126 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
130 return_with_errno(NULL
, r
);
136 * udev_device_get_subsystem:
137 * @udev_device: udev device
139 * Retrieve the subsystem string of the udev device. The string does not
142 * Returns: the subsystem name of the udev device, or #NULL if it cannot be determined
144 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
) {
145 const char *subsystem
;
148 assert_return_errno(udev_device
, NULL
, EINVAL
);
150 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
152 return_with_errno(NULL
, r
);
158 * udev_device_get_property_value:
159 * @udev_device: udev device
160 * @key: property name
162 * Get the value of a given property.
164 * Returns: the property string, or #NULL if there is no such property.
166 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
) {
170 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
172 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
174 return_with_errno(NULL
, r
);
179 struct udev_device
*udev_device_new(struct udev
*udev
, sd_device
*device
) {
180 struct udev_device
*udev_device
;
184 udev_device
= new(struct udev_device
, 1);
186 return_with_errno(NULL
, ENOMEM
);
188 *udev_device
= (struct udev_device
) {
191 .device
= sd_device_ref(device
),
194 udev_list_init(&udev_device
->properties
, true);
195 udev_list_init(&udev_device
->tags
, true);
196 udev_list_init(&udev_device
->sysattrs
, true);
197 udev_list_init(&udev_device
->devlinks
, true);
203 * udev_device_new_from_syspath:
204 * @udev: udev library context
205 * @syspath: sys device path including sys directory
207 * Create new udev device, and fill in information from the sys
208 * device and the udev database entry. The syspath is the absolute
209 * path to the device, including the sys mount point.
211 * The initial refcount is 1, and needs to be decremented to
212 * release the resources of the udev device.
214 * Returns: a new udev device, or #NULL, if it does not exist
216 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
) {
217 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
220 r
= sd_device_new_from_syspath(&device
, syspath
);
222 return_with_errno(NULL
, r
);
224 return udev_device_new(udev
, device
);
228 * udev_device_new_from_devnum:
229 * @udev: udev library context
230 * @type: char or block device
231 * @devnum: device major/minor number
233 * Create new udev device, and fill in information from the sys
234 * device and the udev database entry. The device is looked-up
235 * by its major/minor number and type. Character and block device
236 * numbers are not unique across the two types.
238 * The initial refcount is 1, and needs to be decremented to
239 * release the resources of the udev device.
241 * Returns: a new udev device, or #NULL, if it does not exist
243 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
) {
244 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
247 r
= sd_device_new_from_devnum(&device
, type
, devnum
);
249 return_with_errno(NULL
, r
);
251 return udev_device_new(udev
, device
);
255 * udev_device_new_from_device_id:
256 * @udev: udev library context
257 * @id: text string identifying a kernel device
259 * Create new udev device, and fill in information from the sys
260 * device and the udev database entry. The device is looked-up
261 * by a special string:
262 * b8:2 - block device major:minor
263 * c128:1 - char device major:minor
264 * n3 - network device ifindex
265 * +sound:card29 - kernel driver core subsystem:device name
267 * The initial refcount is 1, and needs to be decremented to
268 * release the resources of the udev device.
270 * Returns: a new udev device, or #NULL, if it does not exist
272 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
) {
273 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
276 r
= sd_device_new_from_device_id(&device
, id
);
278 return_with_errno(NULL
, r
);
280 return udev_device_new(udev
, device
);
284 * udev_device_new_from_subsystem_sysname:
285 * @udev: udev library context
286 * @subsystem: the subsystem of the device
287 * @sysname: the name of the device
289 * Create new udev device, and fill in information from the sys device
290 * and the udev database entry. The device is looked up by the subsystem
291 * and name string of the device, like "mem" / "zero", or "block" / "sda".
293 * The initial refcount is 1, and needs to be decremented to
294 * release the resources of the udev device.
296 * Returns: a new udev device, or #NULL, if it does not exist
298 _public_
struct udev_device
*udev_device_new_from_subsystem_sysname(struct udev
*udev
, const char *subsystem
, const char *sysname
) {
299 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
302 r
= sd_device_new_from_subsystem_sysname(&device
, subsystem
, sysname
);
304 return_with_errno(NULL
, r
);
306 return udev_device_new(udev
, device
);
310 * udev_device_new_from_environment
311 * @udev: udev library context
313 * Create new udev device, and fill in information from the
314 * current process environment. This only works reliable if
315 * the process is called from a udev rule. It is usually used
316 * for tools executed from IMPORT= rules.
318 * The initial refcount is 1, and needs to be decremented to
319 * release the resources of the udev device.
321 * Returns: a new udev device, or #NULL, if it does not exist
323 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
) {
324 _cleanup_(sd_device_unrefp
) sd_device
*device
= NULL
;
327 r
= device_new_from_strv(&device
, environ
);
329 return_with_errno(NULL
, r
);
331 return udev_device_new(udev
, device
);
334 static struct udev_device
*device_new_from_parent(struct udev_device
*child
) {
338 assert_return_errno(child
, NULL
, EINVAL
);
340 r
= sd_device_get_parent(child
->device
, &parent
);
342 return_with_errno(NULL
, r
);
344 return udev_device_new(child
->udev
, parent
);
348 * udev_device_get_parent:
349 * @udev_device: the device to start searching from
351 * Find the next parent device, and fill in information from the sys
352 * device and the udev database entry.
354 * Returned device is not referenced. It is attached to the child
355 * device, and will be cleaned up when the child device is cleaned up.
357 * It is not necessarily just the upper level directory, empty or not
358 * recognized sys directories are ignored.
360 * It can be called as many times as needed, without caring about
363 * Returns: a new udev device, or #NULL, if it no parent exist.
365 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
) {
366 assert_return_errno(udev_device
, NULL
, EINVAL
);
368 if (!udev_device
->parent_set
) {
369 udev_device
->parent_set
= true;
370 udev_device
->parent
= device_new_from_parent(udev_device
);
373 /* TODO: errno will differ here in case parent == NULL */
374 return udev_device
->parent
;
378 * udev_device_get_parent_with_subsystem_devtype:
379 * @udev_device: udev device to start searching from
380 * @subsystem: the subsystem of the device
381 * @devtype: the type (DEVTYPE) of the device
383 * Find the next parent device, with a matching subsystem and devtype
384 * value, and fill in information from the sys device and the udev
387 * If devtype is #NULL, only subsystem is checked, and any devtype will
390 * Returned device is not referenced. It is attached to the child
391 * device, and will be cleaned up when the child device is cleaned up.
393 * It can be called as many times as needed, without caring about
396 * Returns: a new udev device, or #NULL if no matching parent exists.
398 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
) {
402 assert_return_errno(udev_device
, NULL
, EINVAL
);
404 /* this relies on the fact that finding the subdevice of a parent or the
405 parent of a subdevice commute */
407 /* first find the correct sd_device */
408 r
= sd_device_get_parent_with_subsystem_devtype(udev_device
->device
, subsystem
, devtype
, &parent
);
410 return_with_errno(NULL
, r
);
412 /* then walk the chain of udev_device parents until the corresponding
414 while ((udev_device
= udev_device_get_parent(udev_device
)))
415 if (udev_device
->device
== parent
)
418 return_with_errno(NULL
, ENOENT
);
422 * udev_device_get_udev:
423 * @udev_device: udev device
425 * Retrieve the udev library context the device was created with.
427 * Returns: the udev library context
429 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
) {
430 assert_return_errno(udev_device
, NULL
, EINVAL
);
432 return udev_device
->udev
;
435 static struct udev_device
*udev_device_free(struct udev_device
*udev_device
) {
438 sd_device_unref(udev_device
->device
);
439 udev_device_unref(udev_device
->parent
);
441 udev_list_cleanup(&udev_device
->properties
);
442 udev_list_cleanup(&udev_device
->sysattrs
);
443 udev_list_cleanup(&udev_device
->tags
);
444 udev_list_cleanup(&udev_device
->devlinks
);
446 return mfree(udev_device
);
451 * @udev_device: udev device
453 * Take a reference of a udev device.
455 * Returns: the passed udev device
460 * @udev_device: udev device
462 * Drop a reference of a udev device. If the refcount reaches zero,
463 * the resources of the device will be released.
467 DEFINE_PUBLIC_TRIVIAL_REF_UNREF_FUNC(struct udev_device
, udev_device
, udev_device_free
);
470 * udev_device_get_devpath:
471 * @udev_device: udev device
473 * Retrieve the kernel devpath value of the udev device. The path
474 * does not contain the sys mount point, and starts with a '/'.
476 * Returns: the devpath of the udev device
478 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
) {
482 assert_return_errno(udev_device
, NULL
, EINVAL
);
484 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
486 return_with_errno(NULL
, r
);
492 * udev_device_get_syspath:
493 * @udev_device: udev device
495 * Retrieve the sys path of the udev device. The path is an
496 * absolute path and starts with the sys mount point.
498 * Returns: the sys path of the udev device
500 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
) {
504 assert_return_errno(udev_device
, NULL
, EINVAL
);
506 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
508 return_with_errno(NULL
, r
);
514 * udev_device_get_sysname:
515 * @udev_device: udev device
517 * Get the kernel device name in /sys.
519 * Returns: the name string of the device
521 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
) {
525 assert_return_errno(udev_device
, NULL
, EINVAL
);
527 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
529 return_with_errno(NULL
, r
);
535 * udev_device_get_sysnum:
536 * @udev_device: udev device
538 * Get the instance number of the device.
540 * Returns: the trailing number string of the device name
542 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
) {
546 assert_return_errno(udev_device
, NULL
, EINVAL
);
548 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
552 return_with_errno(NULL
, r
);
558 * udev_device_get_devnode:
559 * @udev_device: udev device
561 * Retrieve the device node file name belonging to the udev device.
562 * The path is an absolute path, and starts with the device directory.
564 * Returns: the device node file name of the udev device, or #NULL if no device node exists
566 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
) {
570 assert_return_errno(udev_device
, NULL
, EINVAL
);
572 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
574 return_with_errno(NULL
, r
);
580 * udev_device_get_devlinks_list_entry:
581 * @udev_device: udev device
583 * Retrieve the list of device links pointing to the device file of
584 * the udev device. The next list entry can be retrieved with
585 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
586 * The devlink path can be retrieved from the list entry by
587 * udev_list_entry_get_name(). The path is an absolute path, and starts with
588 * the device directory.
590 * Returns: the first entry of the device node link list
592 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
) {
593 assert_return_errno(udev_device
, NULL
, EINVAL
);
595 if (device_get_devlinks_generation(udev_device
->device
) != udev_device
->devlinks_generation
||
596 !udev_device
->devlinks_read
) {
599 udev_list_cleanup(&udev_device
->devlinks
);
601 FOREACH_DEVICE_DEVLINK(udev_device
->device
, devlink
)
602 if (!udev_list_entry_add(&udev_device
->devlinks
, devlink
, NULL
))
603 return_with_errno(NULL
, ENOMEM
);
605 udev_device
->devlinks_read
= true;
606 udev_device
->devlinks_generation
= device_get_devlinks_generation(udev_device
->device
);
609 return udev_list_get_entry(&udev_device
->devlinks
);
613 * udev_device_get_event_properties_entry:
614 * @udev_device: udev device
616 * Retrieve the list of key/value device properties of the udev
617 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
618 * which returns #NULL if no more entries exist. The property name
619 * can be retrieved from the list entry by udev_list_entry_get_name(),
620 * the property value by udev_list_entry_get_value().
622 * Returns: the first entry of the property list
624 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
) {
625 assert_return_errno(udev_device
, NULL
, EINVAL
);
627 if (device_get_properties_generation(udev_device
->device
) != udev_device
->properties_generation
||
628 !udev_device
->properties_read
) {
629 const char *key
, *value
;
631 udev_list_cleanup(&udev_device
->properties
);
633 FOREACH_DEVICE_PROPERTY(udev_device
->device
, key
, value
)
634 if (!udev_list_entry_add(&udev_device
->properties
, key
, value
))
635 return_with_errno(NULL
, ENOMEM
);
637 udev_device
->properties_read
= true;
638 udev_device
->properties_generation
= device_get_properties_generation(udev_device
->device
);
641 return udev_list_get_entry(&udev_device
->properties
);
645 * udev_device_get_action:
646 * @udev_device: udev device
648 * This is only valid if the device was received through a monitor. Devices read from
649 * sys do not have an action string. Usual actions are: add, remove, change, online,
652 * Returns: the kernel action value, or #NULL if there is no action value available.
654 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
) {
658 assert_return_errno(udev_device
, NULL
, EINVAL
);
660 r
= sd_device_get_property_value(udev_device
->device
, "ACTION", &action
);
664 return_with_errno(NULL
, r
);
670 * udev_device_get_usec_since_initialized:
671 * @udev_device: udev device
673 * Return the number of microseconds passed since udev set up the
674 * device for the first time.
676 * This is only implemented for devices with need to store properties
677 * in the udev database. All other devices return 0 here.
679 * Returns: the number of microseconds since the device was first seen.
681 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
) {
685 assert_return(udev_device
, -EINVAL
);
687 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
689 return_with_errno(0, r
);
695 * udev_device_get_sysattr_value:
696 * @udev_device: udev device
697 * @sysattr: attribute name
699 * The retrieved value is cached in the device. Repeated calls will return the same
700 * value and not open the attribute again.
702 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
704 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
) {
708 assert_return_errno(udev_device
, NULL
, EINVAL
);
710 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
712 return_with_errno(NULL
, r
);
718 * udev_device_set_sysattr_value:
719 * @udev_device: udev device
720 * @sysattr: attribute name
721 * @value: new value to be set
723 * Update the contents of the sys attribute and the cached value of the device.
725 * Returns: Negative error code on failure or 0 on success.
727 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, const char *value
) {
730 assert_return(udev_device
, -EINVAL
);
732 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
740 * udev_device_get_sysattr_list_entry:
741 * @udev_device: udev device
743 * Retrieve the list of available sysattrs, with value being empty;
744 * This just return all available sysfs attributes for a particular
745 * device without reading their values.
747 * Returns: the first entry of the property list
749 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
) {
750 assert_return_errno(udev_device
, NULL
, EINVAL
);
752 if (!udev_device
->sysattrs_read
) {
755 udev_list_cleanup(&udev_device
->sysattrs
);
757 FOREACH_DEVICE_SYSATTR(udev_device
->device
, sysattr
)
758 if (!udev_list_entry_add(&udev_device
->sysattrs
, sysattr
, NULL
))
759 return_with_errno(NULL
, ENOMEM
);
761 udev_device
->sysattrs_read
= true;
764 return udev_list_get_entry(&udev_device
->sysattrs
);
768 * udev_device_get_is_initialized:
769 * @udev_device: udev device
771 * Check if udev has already handled the device and has set up
772 * device node permissions and context, or has renamed a network
775 * This is only implemented for devices with a device node
776 * or network interfaces. All other devices return 1 here.
778 * Returns: 1 if the device is set up. 0 otherwise.
780 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
) {
783 assert_return(udev_device
, -EINVAL
);
785 r
= sd_device_get_is_initialized(udev_device
->device
);
787 return_with_errno(0, r
);
793 * udev_device_get_tags_list_entry:
794 * @udev_device: udev device
796 * Retrieve the list of tags attached to the udev device. The next
797 * list entry can be retrieved with udev_list_entry_get_next(),
798 * which returns #NULL if no more entries exist. The tag string
799 * can be retrieved from the list entry by udev_list_entry_get_name().
801 * Returns: the first entry of the tag list
803 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
) {
804 assert_return_errno(udev_device
, NULL
, EINVAL
);
806 if (device_get_tags_generation(udev_device
->device
) != udev_device
->tags_generation
||
807 !udev_device
->tags_read
) {
810 udev_list_cleanup(&udev_device
->tags
);
812 FOREACH_DEVICE_TAG(udev_device
->device
, tag
)
813 if (!udev_list_entry_add(&udev_device
->tags
, tag
, NULL
))
814 return_with_errno(NULL
, ENOMEM
);
816 udev_device
->tags_read
= true;
817 udev_device
->tags_generation
= device_get_tags_generation(udev_device
->device
);
820 return udev_list_get_entry(&udev_device
->tags
);
824 * udev_device_has_tag:
825 * @udev_device: udev device
828 * Check if a given device has a certain tag associated.
830 * Returns: 1 if the tag is found. 0 otherwise.
832 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
) {
833 assert_return(udev_device
, 0);
835 return sd_device_has_tag(udev_device
->device
, tag
) > 0;