]>
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>
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <linux/sockios.h>
33 #include <sys/ioctl.h>
34 #include <sys/socket.h>
39 #include "sd-device.h"
41 #include "alloc-util.h"
42 #include "device-private.h"
43 #include "device-util.h"
44 #include "libudev-device-internal.h"
45 #include "libudev-private.h"
46 #include "parse-util.h"
49 * SECTION:libudev-device
50 * @short_description: kernel sys devices
52 * Representation of kernel sys devices. Devices are uniquely identified
53 * by their syspath, every device has exactly one path in the kernel sys
54 * filesystem. Devices usually belong to a kernel subsystem, and have
55 * a unique name inside that subsystem.
59 * udev_device_get_seqnum:
60 * @udev_device: udev device
62 * This is only valid if the device was received through a monitor. Devices read from
63 * sys do not have a sequence number.
65 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
67 _public_
unsigned long long int udev_device_get_seqnum(struct udev_device
*udev_device
)
70 unsigned long long ret
;
73 assert_return_errno(udev_device
, 0, EINVAL
);
75 r
= sd_device_get_property_value(udev_device
->device
, "SEQNUM", &seqnum
);
83 r
= safe_atollu(seqnum
, &ret
);
93 * udev_device_get_devnum:
94 * @udev_device: udev device
96 * Get the device major/minor number.
98 * Returns: the dev_t number.
100 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
)
105 assert_return_errno(udev_device
, makedev(0, 0), EINVAL
);
107 r
= sd_device_get_devnum(udev_device
->device
, &devnum
);
110 return makedev(0, 0);
117 * udev_device_get_driver:
118 * @udev_device: udev device
120 * Get the kernel driver name.
122 * Returns: the driver name string, or #NULL if there is no driver attached.
124 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
)
129 assert_return_errno(udev_device
, NULL
, EINVAL
);
131 r
= sd_device_get_driver(udev_device
->device
, &driver
);
141 * udev_device_get_devtype:
142 * @udev_device: udev device
144 * Retrieve the devtype string of the udev device.
146 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
148 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
)
153 assert_return_errno(udev_device
, NULL
, EINVAL
);
155 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
165 * udev_device_get_subsystem:
166 * @udev_device: udev device
168 * Retrieve the subsystem string of the udev device. The string does not
171 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
173 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
)
175 const char *subsystem
;
178 assert_return_errno(udev_device
, NULL
, EINVAL
);
180 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
184 } else if (!subsystem
)
191 * udev_device_get_property_value:
192 * @udev_device: udev device
193 * @key: property name
195 * Get the value of a given property.
197 * Returns: the property string, or #NULL if there is no such property.
199 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
)
201 const char *value
= NULL
;
204 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
206 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
215 struct udev_device
*udev_device_new(struct udev
*udev
) {
216 struct udev_device
*udev_device
;
218 assert_return_errno(udev
, NULL
, EINVAL
);
220 udev_device
= new0(struct udev_device
, 1);
225 udev_device
->refcount
= 1;
226 udev_device
->udev
= udev
;
227 udev_list_init(udev
, &udev_device
->properties
, true);
228 udev_list_init(udev
, &udev_device
->tags
, true);
229 udev_list_init(udev
, &udev_device
->sysattrs
, true);
230 udev_list_init(udev
, &udev_device
->devlinks
, true);
236 * udev_device_new_from_syspath:
237 * @udev: udev library context
238 * @syspath: sys device path including sys directory
240 * Create new udev device, and fill in information from the sys
241 * device and the udev database entry. The syspath is the absolute
242 * path to the device, including the sys mount point.
244 * The initial refcount is 1, and needs to be decremented to
245 * release the resources of the udev device.
247 * Returns: a new udev device, or #NULL, if it does not exist
249 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
) {
250 struct udev_device
*udev_device
;
253 udev_device
= udev_device_new(udev
);
257 r
= sd_device_new_from_syspath(&udev_device
->device
, syspath
);
260 udev_device_unref(udev_device
);
268 * udev_device_new_from_devnum:
269 * @udev: udev library context
270 * @type: char or block device
271 * @devnum: device major/minor number
273 * Create new udev device, and fill in information from the sys
274 * device and the udev database entry. The device is looked-up
275 * by its major/minor number and type. Character and block device
276 * numbers are not unique across the two types.
278 * The initial refcount is 1, and needs to be decremented to
279 * release the resources of the udev device.
281 * Returns: a new udev device, or #NULL, if it does not exist
283 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
)
285 struct udev_device
*udev_device
;
288 udev_device
= udev_device_new(udev
);
292 r
= sd_device_new_from_devnum(&udev_device
->device
, type
, devnum
);
295 udev_device_unref(udev_device
);
303 * udev_device_new_from_device_id:
304 * @udev: udev library context
305 * @id: text string identifying a kernel device
307 * Create new udev device, and fill in information from the sys
308 * device and the udev database entry. The device is looked-up
309 * by a special string:
310 * b8:2 - block device major:minor
311 * c128:1 - char device major:minor
312 * n3 - network device ifindex
313 * +sound:card29 - kernel driver core subsystem:device name
315 * The initial refcount is 1, and needs to be decremented to
316 * release the resources of the udev device.
318 * Returns: a new udev device, or #NULL, if it does not exist
320 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
)
322 struct udev_device
*udev_device
;
325 udev_device
= udev_device_new(udev
);
329 r
= sd_device_new_from_device_id(&udev_device
->device
, id
);
332 udev_device_unref(udev_device
);
340 * udev_device_new_from_subsystem_sysname:
341 * @udev: udev library context
342 * @subsystem: the subsystem of the device
343 * @sysname: the name of the device
345 * Create new udev device, and fill in information from the sys device
346 * and the udev database entry. The device is looked up by the subsystem
347 * and name string of the device, like "mem" / "zero", or "block" / "sda".
349 * The initial refcount is 1, and needs to be decremented to
350 * release the resources of the udev device.
352 * Returns: a new udev device, or #NULL, if it does not exist
354 _public_
struct udev_device
*udev_device_new_from_subsystem_sysname(struct udev
*udev
, const char *subsystem
, const char *sysname
)
356 struct udev_device
*udev_device
;
359 udev_device
= udev_device_new(udev
);
363 r
= sd_device_new_from_subsystem_sysname(&udev_device
->device
, subsystem
, sysname
);
366 udev_device_unref(udev_device
);
374 * udev_device_new_from_environment
375 * @udev: udev library context
377 * Create new udev device, and fill in information from the
378 * current process environment. This only works reliable if
379 * the process is called from a udev rule. It is usually used
380 * for tools executed from IMPORT= rules.
382 * The initial refcount is 1, and needs to be decremented to
383 * release the resources of the udev device.
385 * Returns: a new udev device, or #NULL, if it does not exist
387 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
)
389 struct udev_device
*udev_device
;
392 udev_device
= udev_device_new(udev
);
396 r
= device_new_from_strv(&udev_device
->device
, environ
);
399 udev_device_unref(udev_device
);
406 static struct udev_device
*device_new_from_parent(struct udev_device
*child
)
408 struct udev_device
*parent
;
411 assert_return_errno(child
, NULL
, EINVAL
);
413 parent
= udev_device_new(child
->udev
);
417 r
= sd_device_get_parent(child
->device
, &parent
->device
);
420 udev_device_unref(parent
);
424 /* the parent is unref'ed with the child, so take a ref from libudev as well */
425 sd_device_ref(parent
->device
);
431 * udev_device_get_parent:
432 * @udev_device: the device to start searching from
434 * Find the next parent device, and fill in information from the sys
435 * device and the udev database entry.
437 * Returned device is not referenced. It is attached to the child
438 * device, and will be cleaned up when the child device is cleaned up.
440 * It is not necessarily just the upper level directory, empty or not
441 * recognized sys directories are ignored.
443 * It can be called as many times as needed, without caring about
446 * Returns: a new udev device, or #NULL, if it no parent exist.
448 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
)
450 assert_return_errno(udev_device
, NULL
, EINVAL
);
452 if (!udev_device
->parent_set
) {
453 udev_device
->parent_set
= true;
454 udev_device
->parent
= device_new_from_parent(udev_device
);
457 /* TODO: errno will differ here in case parent == NULL */
458 return udev_device
->parent
;
462 * udev_device_get_parent_with_subsystem_devtype:
463 * @udev_device: udev device to start searching from
464 * @subsystem: the subsystem of the device
465 * @devtype: the type (DEVTYPE) of the device
467 * Find the next parent device, with a matching subsystem and devtype
468 * value, and fill in information from the sys device and the udev
471 * If devtype is #NULL, only subsystem is checked, and any devtype will
474 * Returned device is not referenced. It is attached to the child
475 * device, and will be cleaned up when the child device is cleaned up.
477 * It can be called as many times as needed, without caring about
480 * Returns: a new udev device, or #NULL if no matching parent exists.
482 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
)
487 assert_return_errno(udev_device
, NULL
, EINVAL
);
489 /* this relies on the fact that finding the subdevice of a parent or the
490 parent of a subdevice commute */
492 /* first find the correct sd_device */
493 r
= sd_device_get_parent_with_subsystem_devtype(udev_device
->device
, subsystem
, devtype
, &parent
);
499 /* then walk the chain of udev_device parents until the corresponding
501 while ((udev_device
= udev_device_get_parent(udev_device
))) {
502 if (udev_device
->device
== parent
)
511 * udev_device_get_udev:
512 * @udev_device: udev device
514 * Retrieve the udev library context the device was created with.
516 * Returns: the udev library context
518 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
)
520 assert_return_errno(udev_device
, NULL
, EINVAL
);
522 return udev_device
->udev
;
527 * @udev_device: udev device
529 * Take a reference of a udev device.
531 * Returns: the passed udev device
533 _public_
struct udev_device
*udev_device_ref(struct udev_device
*udev_device
)
536 udev_device
->refcount
++;
543 * @udev_device: udev device
545 * Drop a reference of a udev device. If the refcount reaches zero,
546 * the resources of the device will be released.
550 _public_
struct udev_device
*udev_device_unref(struct udev_device
*udev_device
)
552 if (udev_device
&& (-- udev_device
->refcount
) == 0) {
553 sd_device_unref(udev_device
->device
);
554 udev_device_unref(udev_device
->parent
);
556 udev_list_cleanup(&udev_device
->properties
);
557 udev_list_cleanup(&udev_device
->sysattrs
);
558 udev_list_cleanup(&udev_device
->tags
);
559 udev_list_cleanup(&udev_device
->devlinks
);
568 * udev_device_get_devpath:
569 * @udev_device: udev device
571 * Retrieve the kernel devpath value of the udev device. The path
572 * does not contain the sys mount point, and starts with a '/'.
574 * Returns: the devpath of the udev device
576 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
)
581 assert_return_errno(udev_device
, NULL
, EINVAL
);
583 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
593 * udev_device_get_syspath:
594 * @udev_device: udev device
596 * Retrieve the sys path of the udev device. The path is an
597 * absolute path and starts with the sys mount point.
599 * Returns: the sys path of the udev device
601 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
)
606 assert_return_errno(udev_device
, NULL
, EINVAL
);
608 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
618 * udev_device_get_sysname:
619 * @udev_device: udev device
621 * Get the kernel device name in /sys.
623 * Returns: the name string of the device
625 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
)
630 assert_return_errno(udev_device
, NULL
, EINVAL
);
632 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
642 * udev_device_get_sysnum:
643 * @udev_device: udev device
645 * Get the instance number of the device.
647 * Returns: the trailing number string of the device name
649 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
)
654 assert_return_errno(udev_device
, NULL
, EINVAL
);
656 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
666 * udev_device_get_devnode:
667 * @udev_device: udev device
669 * Retrieve the device node file name belonging to the udev device.
670 * The path is an absolute path, and starts with the device directory.
672 * Returns: the device node file name of the udev device, or #NULL if no device node exists
674 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
)
679 assert_return_errno(udev_device
, NULL
, EINVAL
);
681 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
691 * udev_device_get_devlinks_list_entry:
692 * @udev_device: udev device
694 * Retrieve the list of device links pointing to the device file of
695 * the udev device. The next list entry can be retrieved with
696 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
697 * The devlink path can be retrieved from the list entry by
698 * udev_list_entry_get_name(). The path is an absolute path, and starts with
699 * the device directory.
701 * Returns: the first entry of the device node link list
703 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
)
705 assert_return_errno(udev_device
, NULL
, EINVAL
);
707 if (device_get_devlinks_generation(udev_device
->device
) != udev_device
->devlinks_generation
||
708 !udev_device
->devlinks_read
) {
711 udev_list_cleanup(&udev_device
->devlinks
);
713 FOREACH_DEVICE_DEVLINK(udev_device
->device
, devlink
)
714 udev_list_entry_add(&udev_device
->devlinks
, devlink
, NULL
);
716 udev_device
->devlinks_read
= true;
717 udev_device
->devlinks_generation
= device_get_devlinks_generation(udev_device
->device
);
720 return udev_list_get_entry(&udev_device
->devlinks
);
724 * udev_device_get_event_properties_entry:
725 * @udev_device: udev device
727 * Retrieve the list of key/value device properties of the udev
728 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
729 * which returns #NULL if no more entries exist. The property name
730 * can be retrieved from the list entry by udev_list_entry_get_name(),
731 * the property value by udev_list_entry_get_value().
733 * Returns: the first entry of the property list
735 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
)
737 assert_return_errno(udev_device
, NULL
, EINVAL
);
739 if (device_get_properties_generation(udev_device
->device
) != udev_device
->properties_generation
||
740 !udev_device
->properties_read
) {
741 const char *key
, *value
;
743 udev_list_cleanup(&udev_device
->properties
);
745 FOREACH_DEVICE_PROPERTY(udev_device
->device
, key
, value
)
746 udev_list_entry_add(&udev_device
->properties
, key
, value
);
748 udev_device
->properties_read
= true;
749 udev_device
->properties_generation
= device_get_properties_generation(udev_device
->device
);
752 return udev_list_get_entry(&udev_device
->properties
);
756 * udev_device_get_action:
757 * @udev_device: udev device
759 * This is only valid if the device was received through a monitor. Devices read from
760 * sys do not have an action string. Usual actions are: add, remove, change, online,
763 * Returns: the kernel action value, or #NULL if there is no action value available.
765 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
) {
766 const char *action
= NULL
;
769 assert_return_errno(udev_device
, NULL
, EINVAL
);
771 r
= sd_device_get_property_value(udev_device
->device
, "ACTION", &action
);
772 if (r
< 0 && r
!= -ENOENT
) {
781 * udev_device_get_usec_since_initialized:
782 * @udev_device: udev device
784 * Return the number of microseconds passed since udev set up the
785 * device for the first time.
787 * This is only implemented for devices with need to store properties
788 * in the udev database. All other devices return 0 here.
790 * Returns: the number of microseconds since the device was first seen.
792 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
)
797 assert_return(udev_device
, -EINVAL
);
799 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
809 * udev_device_get_sysattr_value:
810 * @udev_device: udev device
811 * @sysattr: attribute name
813 * The retrieved value is cached in the device. Repeated calls will return the same
814 * value and not open the attribute again.
816 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
818 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
)
823 assert_return_errno(udev_device
, NULL
, EINVAL
);
825 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
835 * udev_device_set_sysattr_value:
836 * @udev_device: udev device
837 * @sysattr: attribute name
838 * @value: new value to be set
840 * Update the contents of the sys attribute and the cached value of the device.
842 * Returns: Negative error code on failure or 0 on success.
844 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, char *value
)
848 assert_return(udev_device
, -EINVAL
);
850 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
858 * udev_device_get_sysattr_list_entry:
859 * @udev_device: udev device
861 * Retrieve the list of available sysattrs, with value being empty;
862 * This just return all available sysfs attributes for a particular
863 * device without reading their values.
865 * Returns: the first entry of the property list
867 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
)
869 assert_return_errno(udev_device
, NULL
, EINVAL
);
871 if (!udev_device
->sysattrs_read
) {
874 udev_list_cleanup(&udev_device
->sysattrs
);
876 FOREACH_DEVICE_SYSATTR(udev_device
->device
, sysattr
)
877 udev_list_entry_add(&udev_device
->sysattrs
, sysattr
, NULL
);
879 udev_device
->sysattrs_read
= true;
882 return udev_list_get_entry(&udev_device
->sysattrs
);
886 * udev_device_get_is_initialized:
887 * @udev_device: udev device
889 * Check if udev has already handled the device and has set up
890 * device node permissions and context, or has renamed a network
893 * This is only implemented for devices with a device node
894 * or network interfaces. All other devices return 1 here.
896 * Returns: 1 if the device is set up. 0 otherwise.
898 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
)
902 assert_return(udev_device
, -EINVAL
);
904 r
= sd_device_get_is_initialized(udev_device
->device
, &initialized
);
915 * udev_device_get_tags_list_entry:
916 * @udev_device: udev device
918 * Retrieve the list of tags attached to the udev device. The next
919 * list entry can be retrieved with udev_list_entry_get_next(),
920 * which returns #NULL if no more entries exist. The tag string
921 * can be retrieved from the list entry by udev_list_entry_get_name().
923 * Returns: the first entry of the tag list
925 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
)
927 assert_return_errno(udev_device
, NULL
, EINVAL
);
929 if (device_get_tags_generation(udev_device
->device
) != udev_device
->tags_generation
||
930 !udev_device
->tags_read
) {
933 udev_list_cleanup(&udev_device
->tags
);
935 FOREACH_DEVICE_TAG(udev_device
->device
, tag
)
936 udev_list_entry_add(&udev_device
->tags
, tag
, NULL
);
938 udev_device
->tags_read
= true;
939 udev_device
->tags_generation
= device_get_tags_generation(udev_device
->device
);
942 return udev_list_get_entry(&udev_device
->tags
);
946 * udev_device_has_tag:
947 * @udev_device: udev device
950 * Check if a given device has a certain tag associated.
952 * Returns: 1 if the tag is found. 0 otherwise.
954 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
)
956 assert_return(udev_device
, 0);
958 return sd_device_has_tag(udev_device
->device
, tag
);