]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/libudev/libudev-device.c
2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
5 Copyright 2015 Tom Gundersen <teg@jklm.no>
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
25 #include <linux/sockios.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
38 #include "sd-device.h"
40 #include "device-private.h"
41 #include "device-util.h"
42 #include "libudev-device-internal.h"
43 #include "libudev-private.h"
46 * SECTION:libudev-device
47 * @short_description: kernel sys devices
49 * Representation of kernel sys devices. Devices are uniquely identified
50 * by their syspath, every device has exactly one path in the kernel sys
51 * filesystem. Devices usually belong to a kernel subsystem, and have
52 * a unique name inside that subsystem.
56 * udev_device_get_seqnum:
57 * @udev_device: udev device
59 * This is only valid if the device was received through a monitor. Devices read from
60 * sys do not have a sequence number.
62 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
64 _public_
unsigned long long int udev_device_get_seqnum(struct udev_device
*udev_device
)
67 unsigned long long ret
;
70 assert_return_errno(udev_device
, 0, EINVAL
);
72 r
= sd_device_get_property_value(udev_device
->device
, "SEQNUM", &seqnum
);
80 r
= safe_atollu(seqnum
, &ret
);
90 * udev_device_get_devnum:
91 * @udev_device: udev device
93 * Get the device major/minor number.
95 * Returns: the dev_t number.
97 _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
);
107 return makedev(0, 0);
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
)
126 assert_return_errno(udev_device
, NULL
, EINVAL
);
128 r
= sd_device_get_driver(udev_device
->device
, &driver
);
138 * udev_device_get_devtype:
139 * @udev_device: udev device
141 * Retrieve the devtype string of the udev device.
143 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
145 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
)
150 assert_return_errno(udev_device
, NULL
, EINVAL
);
152 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
162 * udev_device_get_subsystem:
163 * @udev_device: udev device
165 * Retrieve the subsystem string of the udev device. The string does not
168 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
170 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
)
172 const char *subsystem
;
175 assert_return_errno(udev_device
, NULL
, EINVAL
);
177 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
181 } else if (!subsystem
)
188 * udev_device_get_property_value:
189 * @udev_device: udev device
190 * @key: property name
192 * Get the value of a given property.
194 * Returns: the property string, or #NULL if there is no such property.
196 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
)
198 const char *value
= NULL
;
201 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
203 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
212 struct udev_device
*udev_device_new(struct udev
*udev
) {
213 struct udev_device
*udev_device
;
215 assert_return_errno(udev
, NULL
, EINVAL
);
217 udev_device
= new0(struct udev_device
, 1);
222 udev_device
->refcount
= 1;
223 udev_device
->udev
= udev
;
224 udev_list_init(udev
, &udev_device
->properties
, true);
225 udev_list_init(udev
, &udev_device
->tags
, true);
226 udev_list_init(udev
, &udev_device
->sysattrs
, true);
227 udev_list_init(udev
, &udev_device
->devlinks
, true);
233 * udev_device_new_from_syspath:
234 * @udev: udev library context
235 * @syspath: sys device path including sys directory
237 * Create new udev device, and fill in information from the sys
238 * device and the udev database entry. The syspath is the absolute
239 * path to the device, including the sys mount point.
241 * The initial refcount is 1, and needs to be decremented to
242 * release the resources of the udev device.
244 * Returns: a new udev device, or #NULL, if it does not exist
246 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
) {
247 struct udev_device
*udev_device
;
250 udev_device
= udev_device_new(udev
);
254 r
= sd_device_new_from_syspath(&udev_device
->device
, syspath
);
257 udev_device_unref(udev_device
);
265 * udev_device_new_from_devnum:
266 * @udev: udev library context
267 * @type: char or block device
268 * @devnum: device major/minor number
270 * Create new udev device, and fill in information from the sys
271 * device and the udev database entry. The device is looked-up
272 * by its major/minor number and type. Character and block device
273 * numbers are not unique across the two types.
275 * The initial refcount is 1, and needs to be decremented to
276 * release the resources of the udev device.
278 * Returns: a new udev device, or #NULL, if it does not exist
280 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
)
282 struct udev_device
*udev_device
;
285 udev_device
= udev_device_new(udev
);
289 r
= sd_device_new_from_devnum(&udev_device
->device
, type
, devnum
);
292 udev_device_unref(udev_device
);
300 * udev_device_new_from_device_id:
301 * @udev: udev library context
302 * @id: text string identifying a kernel device
304 * Create new udev device, and fill in information from the sys
305 * device and the udev database entry. The device is looked-up
306 * by a special string:
307 * b8:2 - block device major:minor
308 * c128:1 - char device major:minor
309 * n3 - network device ifindex
310 * +sound:card29 - kernel driver core subsystem:device name
312 * The initial refcount is 1, and needs to be decremented to
313 * release the resources of the udev device.
315 * Returns: a new udev device, or #NULL, if it does not exist
317 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
)
319 struct udev_device
*udev_device
;
322 udev_device
= udev_device_new(udev
);
326 r
= sd_device_new_from_device_id(&udev_device
->device
, id
);
329 udev_device_unref(udev_device
);
337 * udev_device_new_from_subsystem_sysname:
338 * @udev: udev library context
339 * @subsystem: the subsystem of the device
340 * @sysname: the name of the device
342 * Create new udev device, and fill in information from the sys device
343 * and the udev database entry. The device is looked up by the subsystem
344 * and name string of the device, like "mem" / "zero", or "block" / "sda".
346 * The initial refcount is 1, and needs to be decremented to
347 * release the resources of the udev device.
349 * Returns: a new udev device, or #NULL, if it does not exist
351 _public_
struct udev_device
*udev_device_new_from_subsystem_sysname(struct udev
*udev
, const char *subsystem
, const char *sysname
)
353 struct udev_device
*udev_device
;
356 udev_device
= udev_device_new(udev
);
360 r
= sd_device_new_from_subsystem_sysname(&udev_device
->device
, subsystem
, sysname
);
363 udev_device_unref(udev_device
);
371 * udev_device_new_from_environment
372 * @udev: udev library context
374 * Create new udev device, and fill in information from the
375 * current process environment. This only works reliable if
376 * the process is called from a udev rule. It is usually used
377 * for tools executed from IMPORT= rules.
379 * The initial refcount is 1, and needs to be decremented to
380 * release the resources of the udev device.
382 * Returns: a new udev device, or #NULL, if it does not exist
384 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
)
386 struct udev_device
*udev_device
;
389 udev_device
= udev_device_new(udev
);
393 r
= device_new_from_strv(&udev_device
->device
, environ
);
396 udev_device_unref(udev_device
);
403 static struct udev_device
*device_new_from_parent(struct udev_device
*child
)
405 struct udev_device
*parent
;
408 assert_return_errno(child
, NULL
, EINVAL
);
410 parent
= udev_device_new(child
->udev
);
414 r
= sd_device_get_parent(child
->device
, &parent
->device
);
417 udev_device_unref(parent
);
421 /* the parent is unref'ed with the child, so take a ref from libudev as well */
422 sd_device_ref(parent
->device
);
428 * udev_device_get_parent:
429 * @udev_device: the device to start searching from
431 * Find the next parent device, and fill in information from the sys
432 * device and the udev database entry.
434 * Returned device is not referenced. It is attached to the child
435 * device, and will be cleaned up when the child device is cleaned up.
437 * It is not necessarily just the upper level directory, empty or not
438 * recognized sys directories are ignored.
440 * It can be called as many times as needed, without caring about
443 * Returns: a new udev device, or #NULL, if it no parent exist.
445 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
)
447 assert_return_errno(udev_device
, NULL
, EINVAL
);
449 if (!udev_device
->parent_set
) {
450 udev_device
->parent_set
= true;
451 udev_device
->parent
= device_new_from_parent(udev_device
);
454 /* TODO: errno will differ here in case parent == NULL */
455 return udev_device
->parent
;
459 * udev_device_get_parent_with_subsystem_devtype:
460 * @udev_device: udev device to start searching from
461 * @subsystem: the subsystem of the device
462 * @devtype: the type (DEVTYPE) of the device
464 * Find the next parent device, with a matching subsystem and devtype
465 * value, and fill in information from the sys device and the udev
468 * If devtype is #NULL, only subsystem is checked, and any devtype will
471 * Returned device is not referenced. It is attached to the child
472 * device, and will be cleaned up when the child device is cleaned up.
474 * It can be called as many times as needed, without caring about
477 * Returns: a new udev device, or #NULL if no matching parent exists.
479 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
)
484 assert_return_errno(udev_device
, NULL
, EINVAL
);
486 /* this relies on the fact that finding the subdevice of a parent or the
487 parent of a subdevice commute */
489 /* first find the correct sd_device */
490 r
= sd_device_get_parent_with_subsystem_devtype(udev_device
->device
, subsystem
, devtype
, &parent
);
496 /* then walk the chain of udev_device parents until the correspanding
498 while ((udev_device
= udev_device_get_parent(udev_device
))) {
499 if (udev_device
->device
== parent
)
508 * udev_device_get_udev:
509 * @udev_device: udev device
511 * Retrieve the udev library context the device was created with.
513 * Returns: the udev library context
515 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
)
517 assert_return_errno(udev_device
, NULL
, EINVAL
);
519 return udev_device
->udev
;
524 * @udev_device: udev device
526 * Take a reference of a udev device.
528 * Returns: the passed udev device
530 _public_
struct udev_device
*udev_device_ref(struct udev_device
*udev_device
)
533 udev_device
->refcount
++;
540 * @udev_device: udev device
542 * Drop a reference of a udev device. If the refcount reaches zero,
543 * the resources of the device will be released.
547 _public_
struct udev_device
*udev_device_unref(struct udev_device
*udev_device
)
549 if (udev_device
&& (-- udev_device
->refcount
) == 0) {
550 sd_device_unref(udev_device
->device
);
551 udev_device_unref(udev_device
->parent
);
553 udev_list_cleanup(&udev_device
->properties
);
554 udev_list_cleanup(&udev_device
->sysattrs
);
555 udev_list_cleanup(&udev_device
->tags
);
556 udev_list_cleanup(&udev_device
->devlinks
);
565 * udev_device_get_devpath:
566 * @udev_device: udev device
568 * Retrieve the kernel devpath value of the udev device. The path
569 * does not contain the sys mount point, and starts with a '/'.
571 * Returns: the devpath of the udev device
573 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
)
578 assert_return_errno(udev_device
, NULL
, EINVAL
);
580 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
590 * udev_device_get_syspath:
591 * @udev_device: udev device
593 * Retrieve the sys path of the udev device. The path is an
594 * absolute path and starts with the sys mount point.
596 * Returns: the sys path of the udev device
598 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
)
603 assert_return_errno(udev_device
, NULL
, EINVAL
);
605 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
615 * udev_device_get_sysname:
616 * @udev_device: udev device
618 * Get the kernel device name in /sys.
620 * Returns: the name string of the device device
622 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
)
627 assert_return_errno(udev_device
, NULL
, EINVAL
);
629 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
639 * udev_device_get_sysnum:
640 * @udev_device: udev device
642 * Get the instance number of the device.
644 * Returns: the trailing number string of the device name
646 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
)
651 assert_return_errno(udev_device
, NULL
, EINVAL
);
653 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
663 * udev_device_get_devnode:
664 * @udev_device: udev device
666 * Retrieve the device node file name belonging to the udev device.
667 * The path is an absolute path, and starts with the device directory.
669 * Returns: the device node file name of the udev device, or #NULL if no device node exists
671 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
)
676 assert_return_errno(udev_device
, NULL
, EINVAL
);
678 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
688 * udev_device_get_devlinks_list_entry:
689 * @udev_device: udev device
691 * Retrieve the list of device links pointing to the device file of
692 * the udev device. The next list entry can be retrieved with
693 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
694 * The devlink path can be retrieved from the list entry by
695 * udev_list_entry_get_name(). The path is an absolute path, and starts with
696 * the device directory.
698 * Returns: the first entry of the device node link list
700 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
)
702 assert_return_errno(udev_device
, NULL
, EINVAL
);
704 if (device_get_devlinks_generation(udev_device
->device
) != udev_device
->devlinks_generation
||
705 !udev_device
->devlinks_read
) {
708 udev_list_cleanup(&udev_device
->devlinks
);
710 FOREACH_DEVICE_DEVLINK(udev_device
->device
, devlink
)
711 udev_list_entry_add(&udev_device
->devlinks
, devlink
, NULL
);
713 udev_device
->devlinks_read
= true;
714 udev_device
->devlinks_generation
= device_get_devlinks_generation(udev_device
->device
);
717 return udev_list_get_entry(&udev_device
->devlinks
);
721 * udev_device_get_event_properties_entry:
722 * @udev_device: udev device
724 * Retrieve the list of key/value device properties of the udev
725 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
726 * which returns #NULL if no more entries exist. The property name
727 * can be retrieved from the list entry by udev_list_entry_get_name(),
728 * the property value by udev_list_entry_get_value().
730 * Returns: the first entry of the property list
732 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
)
734 assert_return_errno(udev_device
, NULL
, EINVAL
);
736 if (device_get_properties_generation(udev_device
->device
) != udev_device
->properties_generation
||
737 !udev_device
->properties_read
) {
738 const char *key
, *value
;
740 udev_list_cleanup(&udev_device
->properties
);
742 FOREACH_DEVICE_PROPERTY(udev_device
->device
, key
, value
)
743 udev_list_entry_add(&udev_device
->properties
, key
, value
);
745 udev_device
->properties_read
= true;
746 udev_device
->properties_generation
= device_get_properties_generation(udev_device
->device
);
749 return udev_list_get_entry(&udev_device
->properties
);
753 * udev_device_get_action:
754 * @udev_device: udev device
756 * This is only valid if the device was received through a monitor. Devices read from
757 * sys do not have an action string. Usual actions are: add, remove, change, online,
760 * Returns: the kernel action value, or #NULL if there is no action value available.
762 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
) {
763 const char *action
= NULL
;
766 assert_return_errno(udev_device
, NULL
, EINVAL
);
768 r
= sd_device_get_property_value(udev_device
->device
, "ACTION", &action
);
769 if (r
< 0 && r
!= -ENOENT
) {
778 * udev_device_get_usec_since_initialized:
779 * @udev_device: udev device
781 * Return the number of microseconds passed since udev set up the
782 * device for the first time.
784 * This is only implemented for devices with need to store properties
785 * in the udev database. All other devices return 0 here.
787 * Returns: the number of microseconds since the device was first seen.
789 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
)
794 assert_return(udev_device
, -EINVAL
);
796 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
806 * udev_device_get_sysattr_value:
807 * @udev_device: udev device
808 * @sysattr: attribute name
810 * The retrieved value is cached in the device. Repeated calls will return the same
811 * value and not open the attribute again.
813 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
815 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
)
820 assert_return_errno(udev_device
, NULL
, EINVAL
);
822 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
832 * udev_device_set_sysattr_value:
833 * @udev_device: udev device
834 * @sysattr: attribute name
835 * @value: new value to be set
837 * Update the contents of the sys attribute and the cached value of the device.
839 * Returns: Negative error code on failure or 0 on success.
841 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, char *value
)
845 assert_return(udev_device
, -EINVAL
);
847 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
855 * udev_device_get_sysattr_list_entry:
856 * @udev_device: udev device
858 * Retrieve the list of available sysattrs, with value being empty;
859 * This just return all available sysfs attributes for a particular
860 * device without reading their values.
862 * Returns: the first entry of the property list
864 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
)
866 assert_return_errno(udev_device
, NULL
, EINVAL
);
868 if (!udev_device
->sysattrs_read
) {
871 udev_list_cleanup(&udev_device
->sysattrs
);
873 FOREACH_DEVICE_SYSATTR(udev_device
->device
, sysattr
)
874 udev_list_entry_add(&udev_device
->sysattrs
, sysattr
, NULL
);
876 udev_device
->sysattrs_read
= true;
879 return udev_list_get_entry(&udev_device
->sysattrs
);
883 * udev_device_get_is_initialized:
884 * @udev_device: udev device
886 * Check if udev has already handled the device and has set up
887 * device node permissions and context, or has renamed a network
890 * This is only implemented for devices with a device node
891 * or network interfaces. All other devices return 1 here.
893 * Returns: 1 if the device is set up. 0 otherwise.
895 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
)
899 assert_return(udev_device
, -EINVAL
);
901 r
= sd_device_get_is_initialized(udev_device
->device
, &initialized
);
912 * udev_device_get_tags_list_entry:
913 * @udev_device: udev device
915 * Retrieve the list of tags attached to the udev device. The next
916 * list entry can be retrieved with udev_list_entry_get_next(),
917 * which returns #NULL if no more entries exist. The tag string
918 * can be retrieved from the list entry by udev_list_entry_get_name().
920 * Returns: the first entry of the tag list
922 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
)
924 assert_return_errno(udev_device
, NULL
, EINVAL
);
926 if (device_get_tags_generation(udev_device
->device
) != udev_device
->tags_generation
||
927 !udev_device
->tags_read
) {
930 udev_list_cleanup(&udev_device
->tags
);
932 FOREACH_DEVICE_TAG(udev_device
->device
, tag
)
933 udev_list_entry_add(&udev_device
->tags
, tag
, NULL
);
935 udev_device
->tags_read
= true;
936 udev_device
->tags_generation
= device_get_tags_generation(udev_device
->device
);
939 return udev_list_get_entry(&udev_device
->tags
);
943 * udev_device_has_tag:
944 * @udev_device: udev device
947 * Check if a given device has a certain tag associated.
949 * Returns: 1 if the tag is found. 0 otherwise.
951 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
)
953 assert_return(udev_device
, 0);
955 return sd_device_has_tag(udev_device
->device
, tag
);