]>
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 "alloc-util.h"
41 #include "device-private.h"
42 #include "device-util.h"
43 #include "libudev-device-internal.h"
44 #include "libudev-private.h"
45 #include "parse-util.h"
48 * SECTION:libudev-device
49 * @short_description: kernel sys devices
51 * Representation of kernel sys devices. Devices are uniquely identified
52 * by their syspath, every device has exactly one path in the kernel sys
53 * filesystem. Devices usually belong to a kernel subsystem, and have
54 * a unique name inside that subsystem.
58 * udev_device_get_seqnum:
59 * @udev_device: udev device
61 * This is only valid if the device was received through a monitor. Devices read from
62 * sys do not have a sequence number.
64 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
66 _public_
unsigned long long int udev_device_get_seqnum(struct udev_device
*udev_device
)
69 unsigned long long ret
;
72 assert_return_errno(udev_device
, 0, EINVAL
);
74 r
= sd_device_get_property_value(udev_device
->device
, "SEQNUM", &seqnum
);
82 r
= safe_atollu(seqnum
, &ret
);
92 * udev_device_get_devnum:
93 * @udev_device: udev device
95 * Get the device major/minor number.
97 * Returns: the dev_t number.
99 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
)
104 assert_return_errno(udev_device
, makedev(0, 0), EINVAL
);
106 r
= sd_device_get_devnum(udev_device
->device
, &devnum
);
109 return makedev(0, 0);
116 * udev_device_get_driver:
117 * @udev_device: udev device
119 * Get the kernel driver name.
121 * Returns: the driver name string, or #NULL if there is no driver attached.
123 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
)
128 assert_return_errno(udev_device
, NULL
, EINVAL
);
130 r
= sd_device_get_driver(udev_device
->device
, &driver
);
140 * udev_device_get_devtype:
141 * @udev_device: udev device
143 * Retrieve the devtype string of the udev device.
145 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
147 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
)
152 assert_return_errno(udev_device
, NULL
, EINVAL
);
154 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
164 * udev_device_get_subsystem:
165 * @udev_device: udev device
167 * Retrieve the subsystem string of the udev device. The string does not
170 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
172 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
)
174 const char *subsystem
;
177 assert_return_errno(udev_device
, NULL
, EINVAL
);
179 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
183 } else if (!subsystem
)
190 * udev_device_get_property_value:
191 * @udev_device: udev device
192 * @key: property name
194 * Get the value of a given property.
196 * Returns: the property string, or #NULL if there is no such property.
198 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
)
200 const char *value
= NULL
;
203 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
205 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
214 struct udev_device
*udev_device_new(struct udev
*udev
) {
215 struct udev_device
*udev_device
;
217 assert_return_errno(udev
, NULL
, EINVAL
);
219 udev_device
= new0(struct udev_device
, 1);
224 udev_device
->refcount
= 1;
225 udev_device
->udev
= udev
;
226 udev_list_init(udev
, &udev_device
->properties
, true);
227 udev_list_init(udev
, &udev_device
->tags
, true);
228 udev_list_init(udev
, &udev_device
->sysattrs
, true);
229 udev_list_init(udev
, &udev_device
->devlinks
, true);
235 * udev_device_new_from_syspath:
236 * @udev: udev library context
237 * @syspath: sys device path including sys directory
239 * Create new udev device, and fill in information from the sys
240 * device and the udev database entry. The syspath is the absolute
241 * path to the device, including the sys mount point.
243 * The initial refcount is 1, and needs to be decremented to
244 * release the resources of the udev device.
246 * Returns: a new udev device, or #NULL, if it does not exist
248 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
) {
249 struct udev_device
*udev_device
;
252 udev_device
= udev_device_new(udev
);
256 r
= sd_device_new_from_syspath(&udev_device
->device
, syspath
);
259 udev_device_unref(udev_device
);
267 * udev_device_new_from_devnum:
268 * @udev: udev library context
269 * @type: char or block device
270 * @devnum: device major/minor number
272 * Create new udev device, and fill in information from the sys
273 * device and the udev database entry. The device is looked-up
274 * by its major/minor number and type. Character and block device
275 * numbers are not unique across the two types.
277 * The initial refcount is 1, and needs to be decremented to
278 * release the resources of the udev device.
280 * Returns: a new udev device, or #NULL, if it does not exist
282 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
)
284 struct udev_device
*udev_device
;
287 udev_device
= udev_device_new(udev
);
291 r
= sd_device_new_from_devnum(&udev_device
->device
, type
, devnum
);
294 udev_device_unref(udev_device
);
302 * udev_device_new_from_device_id:
303 * @udev: udev library context
304 * @id: text string identifying a kernel device
306 * Create new udev device, and fill in information from the sys
307 * device and the udev database entry. The device is looked-up
308 * by a special string:
309 * b8:2 - block device major:minor
310 * c128:1 - char device major:minor
311 * n3 - network device ifindex
312 * +sound:card29 - kernel driver core subsystem:device name
314 * The initial refcount is 1, and needs to be decremented to
315 * release the resources of the udev device.
317 * Returns: a new udev device, or #NULL, if it does not exist
319 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
)
321 struct udev_device
*udev_device
;
324 udev_device
= udev_device_new(udev
);
328 r
= sd_device_new_from_device_id(&udev_device
->device
, id
);
331 udev_device_unref(udev_device
);
339 * udev_device_new_from_subsystem_sysname:
340 * @udev: udev library context
341 * @subsystem: the subsystem of the device
342 * @sysname: the name of the device
344 * Create new udev device, and fill in information from the sys device
345 * and the udev database entry. The device is looked up by the subsystem
346 * and name string of the device, like "mem" / "zero", or "block" / "sda".
348 * The initial refcount is 1, and needs to be decremented to
349 * release the resources of the udev device.
351 * Returns: a new udev device, or #NULL, if it does not exist
353 _public_
struct udev_device
*udev_device_new_from_subsystem_sysname(struct udev
*udev
, const char *subsystem
, const char *sysname
)
355 struct udev_device
*udev_device
;
358 udev_device
= udev_device_new(udev
);
362 r
= sd_device_new_from_subsystem_sysname(&udev_device
->device
, subsystem
, sysname
);
365 udev_device_unref(udev_device
);
373 * udev_device_new_from_environment
374 * @udev: udev library context
376 * Create new udev device, and fill in information from the
377 * current process environment. This only works reliable if
378 * the process is called from a udev rule. It is usually used
379 * for tools executed from IMPORT= rules.
381 * The initial refcount is 1, and needs to be decremented to
382 * release the resources of the udev device.
384 * Returns: a new udev device, or #NULL, if it does not exist
386 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
)
388 struct udev_device
*udev_device
;
391 udev_device
= udev_device_new(udev
);
395 r
= device_new_from_strv(&udev_device
->device
, environ
);
398 udev_device_unref(udev_device
);
405 static struct udev_device
*device_new_from_parent(struct udev_device
*child
)
407 struct udev_device
*parent
;
410 assert_return_errno(child
, NULL
, EINVAL
);
412 parent
= udev_device_new(child
->udev
);
416 r
= sd_device_get_parent(child
->device
, &parent
->device
);
419 udev_device_unref(parent
);
423 /* the parent is unref'ed with the child, so take a ref from libudev as well */
424 sd_device_ref(parent
->device
);
430 * udev_device_get_parent:
431 * @udev_device: the device to start searching from
433 * Find the next parent device, and fill in information from the sys
434 * device and the udev database entry.
436 * Returned device is not referenced. It is attached to the child
437 * device, and will be cleaned up when the child device is cleaned up.
439 * It is not necessarily just the upper level directory, empty or not
440 * recognized sys directories are ignored.
442 * It can be called as many times as needed, without caring about
445 * Returns: a new udev device, or #NULL, if it no parent exist.
447 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
)
449 assert_return_errno(udev_device
, NULL
, EINVAL
);
451 if (!udev_device
->parent_set
) {
452 udev_device
->parent_set
= true;
453 udev_device
->parent
= device_new_from_parent(udev_device
);
456 /* TODO: errno will differ here in case parent == NULL */
457 return udev_device
->parent
;
461 * udev_device_get_parent_with_subsystem_devtype:
462 * @udev_device: udev device to start searching from
463 * @subsystem: the subsystem of the device
464 * @devtype: the type (DEVTYPE) of the device
466 * Find the next parent device, with a matching subsystem and devtype
467 * value, and fill in information from the sys device and the udev
470 * If devtype is #NULL, only subsystem is checked, and any devtype will
473 * Returned device is not referenced. It is attached to the child
474 * device, and will be cleaned up when the child device is cleaned up.
476 * It can be called as many times as needed, without caring about
479 * Returns: a new udev device, or #NULL if no matching parent exists.
481 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
)
486 assert_return_errno(udev_device
, NULL
, EINVAL
);
488 /* this relies on the fact that finding the subdevice of a parent or the
489 parent of a subdevice commute */
491 /* first find the correct sd_device */
492 r
= sd_device_get_parent_with_subsystem_devtype(udev_device
->device
, subsystem
, devtype
, &parent
);
498 /* then walk the chain of udev_device parents until the corresponding
500 while ((udev_device
= udev_device_get_parent(udev_device
))) {
501 if (udev_device
->device
== parent
)
510 * udev_device_get_udev:
511 * @udev_device: udev device
513 * Retrieve the udev library context the device was created with.
515 * Returns: the udev library context
517 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
)
519 assert_return_errno(udev_device
, NULL
, EINVAL
);
521 return udev_device
->udev
;
526 * @udev_device: udev device
528 * Take a reference of a udev device.
530 * Returns: the passed udev device
532 _public_
struct udev_device
*udev_device_ref(struct udev_device
*udev_device
)
535 udev_device
->refcount
++;
542 * @udev_device: udev device
544 * Drop a reference of a udev device. If the refcount reaches zero,
545 * the resources of the device will be released.
549 _public_
struct udev_device
*udev_device_unref(struct udev_device
*udev_device
)
551 if (udev_device
&& (-- udev_device
->refcount
) == 0) {
552 sd_device_unref(udev_device
->device
);
553 udev_device_unref(udev_device
->parent
);
555 udev_list_cleanup(&udev_device
->properties
);
556 udev_list_cleanup(&udev_device
->sysattrs
);
557 udev_list_cleanup(&udev_device
->tags
);
558 udev_list_cleanup(&udev_device
->devlinks
);
567 * udev_device_get_devpath:
568 * @udev_device: udev device
570 * Retrieve the kernel devpath value of the udev device. The path
571 * does not contain the sys mount point, and starts with a '/'.
573 * Returns: the devpath of the udev device
575 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
)
580 assert_return_errno(udev_device
, NULL
, EINVAL
);
582 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
592 * udev_device_get_syspath:
593 * @udev_device: udev device
595 * Retrieve the sys path of the udev device. The path is an
596 * absolute path and starts with the sys mount point.
598 * Returns: the sys path of the udev device
600 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
)
605 assert_return_errno(udev_device
, NULL
, EINVAL
);
607 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
617 * udev_device_get_sysname:
618 * @udev_device: udev device
620 * Get the kernel device name in /sys.
622 * Returns: the name string of the device
624 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
)
629 assert_return_errno(udev_device
, NULL
, EINVAL
);
631 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
641 * udev_device_get_sysnum:
642 * @udev_device: udev device
644 * Get the instance number of the device.
646 * Returns: the trailing number string of the device name
648 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
)
653 assert_return_errno(udev_device
, NULL
, EINVAL
);
655 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
665 * udev_device_get_devnode:
666 * @udev_device: udev device
668 * Retrieve the device node file name belonging to the udev device.
669 * The path is an absolute path, and starts with the device directory.
671 * Returns: the device node file name of the udev device, or #NULL if no device node exists
673 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
)
678 assert_return_errno(udev_device
, NULL
, EINVAL
);
680 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
690 * udev_device_get_devlinks_list_entry:
691 * @udev_device: udev device
693 * Retrieve the list of device links pointing to the device file of
694 * the udev device. The next list entry can be retrieved with
695 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
696 * The devlink path can be retrieved from the list entry by
697 * udev_list_entry_get_name(). The path is an absolute path, and starts with
698 * the device directory.
700 * Returns: the first entry of the device node link list
702 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
)
704 assert_return_errno(udev_device
, NULL
, EINVAL
);
706 if (device_get_devlinks_generation(udev_device
->device
) != udev_device
->devlinks_generation
||
707 !udev_device
->devlinks_read
) {
710 udev_list_cleanup(&udev_device
->devlinks
);
712 FOREACH_DEVICE_DEVLINK(udev_device
->device
, devlink
)
713 udev_list_entry_add(&udev_device
->devlinks
, devlink
, NULL
);
715 udev_device
->devlinks_read
= true;
716 udev_device
->devlinks_generation
= device_get_devlinks_generation(udev_device
->device
);
719 return udev_list_get_entry(&udev_device
->devlinks
);
723 * udev_device_get_event_properties_entry:
724 * @udev_device: udev device
726 * Retrieve the list of key/value device properties of the udev
727 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
728 * which returns #NULL if no more entries exist. The property name
729 * can be retrieved from the list entry by udev_list_entry_get_name(),
730 * the property value by udev_list_entry_get_value().
732 * Returns: the first entry of the property list
734 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
)
736 assert_return_errno(udev_device
, NULL
, EINVAL
);
738 if (device_get_properties_generation(udev_device
->device
) != udev_device
->properties_generation
||
739 !udev_device
->properties_read
) {
740 const char *key
, *value
;
742 udev_list_cleanup(&udev_device
->properties
);
744 FOREACH_DEVICE_PROPERTY(udev_device
->device
, key
, value
)
745 udev_list_entry_add(&udev_device
->properties
, key
, value
);
747 udev_device
->properties_read
= true;
748 udev_device
->properties_generation
= device_get_properties_generation(udev_device
->device
);
751 return udev_list_get_entry(&udev_device
->properties
);
755 * udev_device_get_action:
756 * @udev_device: udev device
758 * This is only valid if the device was received through a monitor. Devices read from
759 * sys do not have an action string. Usual actions are: add, remove, change, online,
762 * Returns: the kernel action value, or #NULL if there is no action value available.
764 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
) {
765 const char *action
= NULL
;
768 assert_return_errno(udev_device
, NULL
, EINVAL
);
770 r
= sd_device_get_property_value(udev_device
->device
, "ACTION", &action
);
771 if (r
< 0 && r
!= -ENOENT
) {
780 * udev_device_get_usec_since_initialized:
781 * @udev_device: udev device
783 * Return the number of microseconds passed since udev set up the
784 * device for the first time.
786 * This is only implemented for devices with need to store properties
787 * in the udev database. All other devices return 0 here.
789 * Returns: the number of microseconds since the device was first seen.
791 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
)
796 assert_return(udev_device
, -EINVAL
);
798 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
808 * udev_device_get_sysattr_value:
809 * @udev_device: udev device
810 * @sysattr: attribute name
812 * The retrieved value is cached in the device. Repeated calls will return the same
813 * value and not open the attribute again.
815 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
817 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
)
822 assert_return_errno(udev_device
, NULL
, EINVAL
);
824 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
834 * udev_device_set_sysattr_value:
835 * @udev_device: udev device
836 * @sysattr: attribute name
837 * @value: new value to be set
839 * Update the contents of the sys attribute and the cached value of the device.
841 * Returns: Negative error code on failure or 0 on success.
843 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, char *value
)
847 assert_return(udev_device
, -EINVAL
);
849 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
857 * udev_device_get_sysattr_list_entry:
858 * @udev_device: udev device
860 * Retrieve the list of available sysattrs, with value being empty;
861 * This just return all available sysfs attributes for a particular
862 * device without reading their values.
864 * Returns: the first entry of the property list
866 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
)
868 assert_return_errno(udev_device
, NULL
, EINVAL
);
870 if (!udev_device
->sysattrs_read
) {
873 udev_list_cleanup(&udev_device
->sysattrs
);
875 FOREACH_DEVICE_SYSATTR(udev_device
->device
, sysattr
)
876 udev_list_entry_add(&udev_device
->sysattrs
, sysattr
, NULL
);
878 udev_device
->sysattrs_read
= true;
881 return udev_list_get_entry(&udev_device
->sysattrs
);
885 * udev_device_get_is_initialized:
886 * @udev_device: udev device
888 * Check if udev has already handled the device and has set up
889 * device node permissions and context, or has renamed a network
892 * This is only implemented for devices with a device node
893 * or network interfaces. All other devices return 1 here.
895 * Returns: 1 if the device is set up. 0 otherwise.
897 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
)
901 assert_return(udev_device
, -EINVAL
);
903 r
= sd_device_get_is_initialized(udev_device
->device
, &initialized
);
914 * udev_device_get_tags_list_entry:
915 * @udev_device: udev device
917 * Retrieve the list of tags attached to the udev device. The next
918 * list entry can be retrieved with udev_list_entry_get_next(),
919 * which returns #NULL if no more entries exist. The tag string
920 * can be retrieved from the list entry by udev_list_entry_get_name().
922 * Returns: the first entry of the tag list
924 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
)
926 assert_return_errno(udev_device
, NULL
, EINVAL
);
928 if (device_get_tags_generation(udev_device
->device
) != udev_device
->tags_generation
||
929 !udev_device
->tags_read
) {
932 udev_list_cleanup(&udev_device
->tags
);
934 FOREACH_DEVICE_TAG(udev_device
->device
, tag
)
935 udev_list_entry_add(&udev_device
->tags
, tag
, NULL
);
937 udev_device
->tags_read
= true;
938 udev_device
->tags_generation
= device_get_tags_generation(udev_device
->device
);
941 return udev_list_get_entry(&udev_device
->tags
);
945 * udev_device_has_tag:
946 * @udev_device: udev device
949 * Check if a given device has a certain tag associated.
951 * Returns: 1 if the tag is found. 0 otherwise.
953 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
)
955 assert_return(udev_device
, 0);
957 return sd_device_has_tag(udev_device
->device
, tag
);