]>
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"
44 #include "parse-util.h"
47 * SECTION:libudev-device
48 * @short_description: kernel sys devices
50 * Representation of kernel sys devices. Devices are uniquely identified
51 * by their syspath, every device has exactly one path in the kernel sys
52 * filesystem. Devices usually belong to a kernel subsystem, and have
53 * a unique name inside that subsystem.
57 * udev_device_get_seqnum:
58 * @udev_device: udev device
60 * This is only valid if the device was received through a monitor. Devices read from
61 * sys do not have a sequence number.
63 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
65 _public_
unsigned long long int udev_device_get_seqnum(struct udev_device
*udev_device
)
68 unsigned long long ret
;
71 assert_return_errno(udev_device
, 0, EINVAL
);
73 r
= sd_device_get_property_value(udev_device
->device
, "SEQNUM", &seqnum
);
81 r
= safe_atollu(seqnum
, &ret
);
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
)
103 assert_return_errno(udev_device
, makedev(0, 0), EINVAL
);
105 r
= sd_device_get_devnum(udev_device
->device
, &devnum
);
108 return makedev(0, 0);
115 * udev_device_get_driver:
116 * @udev_device: udev device
118 * Get the kernel driver name.
120 * Returns: the driver name string, or #NULL if there is no driver attached.
122 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
)
127 assert_return_errno(udev_device
, NULL
, EINVAL
);
129 r
= sd_device_get_driver(udev_device
->device
, &driver
);
139 * udev_device_get_devtype:
140 * @udev_device: udev device
142 * Retrieve the devtype string of the udev device.
144 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
146 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
)
151 assert_return_errno(udev_device
, NULL
, EINVAL
);
153 r
= sd_device_get_devtype(udev_device
->device
, &devtype
);
163 * udev_device_get_subsystem:
164 * @udev_device: udev device
166 * Retrieve the subsystem string of the udev device. The string does not
169 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
171 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
)
173 const char *subsystem
;
176 assert_return_errno(udev_device
, NULL
, EINVAL
);
178 r
= sd_device_get_subsystem(udev_device
->device
, &subsystem
);
182 } else if (!subsystem
)
189 * udev_device_get_property_value:
190 * @udev_device: udev device
191 * @key: property name
193 * Get the value of a given property.
195 * Returns: the property string, or #NULL if there is no such property.
197 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
)
199 const char *value
= NULL
;
202 assert_return_errno(udev_device
&& key
, NULL
, EINVAL
);
204 r
= sd_device_get_property_value(udev_device
->device
, key
, &value
);
213 struct udev_device
*udev_device_new(struct udev
*udev
) {
214 struct udev_device
*udev_device
;
216 assert_return_errno(udev
, NULL
, EINVAL
);
218 udev_device
= new0(struct udev_device
, 1);
223 udev_device
->refcount
= 1;
224 udev_device
->udev
= udev
;
225 udev_list_init(udev
, &udev_device
->properties
, true);
226 udev_list_init(udev
, &udev_device
->tags
, true);
227 udev_list_init(udev
, &udev_device
->sysattrs
, true);
228 udev_list_init(udev
, &udev_device
->devlinks
, true);
234 * udev_device_new_from_syspath:
235 * @udev: udev library context
236 * @syspath: sys device path including sys directory
238 * Create new udev device, and fill in information from the sys
239 * device and the udev database entry. The syspath is the absolute
240 * path to the device, including the sys mount point.
242 * The initial refcount is 1, and needs to be decremented to
243 * release the resources of the udev device.
245 * Returns: a new udev device, or #NULL, if it does not exist
247 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
) {
248 struct udev_device
*udev_device
;
251 udev_device
= udev_device_new(udev
);
255 r
= sd_device_new_from_syspath(&udev_device
->device
, syspath
);
258 udev_device_unref(udev_device
);
266 * udev_device_new_from_devnum:
267 * @udev: udev library context
268 * @type: char or block device
269 * @devnum: device major/minor number
271 * Create new udev device, and fill in information from the sys
272 * device and the udev database entry. The device is looked-up
273 * by its major/minor number and type. Character and block device
274 * numbers are not unique across the two types.
276 * The initial refcount is 1, and needs to be decremented to
277 * release the resources of the udev device.
279 * Returns: a new udev device, or #NULL, if it does not exist
281 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
)
283 struct udev_device
*udev_device
;
286 udev_device
= udev_device_new(udev
);
290 r
= sd_device_new_from_devnum(&udev_device
->device
, type
, devnum
);
293 udev_device_unref(udev_device
);
301 * udev_device_new_from_device_id:
302 * @udev: udev library context
303 * @id: text string identifying a kernel device
305 * Create new udev device, and fill in information from the sys
306 * device and the udev database entry. The device is looked-up
307 * by a special string:
308 * b8:2 - block device major:minor
309 * c128:1 - char device major:minor
310 * n3 - network device ifindex
311 * +sound:card29 - kernel driver core subsystem:device name
313 * The initial refcount is 1, and needs to be decremented to
314 * release the resources of the udev device.
316 * Returns: a new udev device, or #NULL, if it does not exist
318 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
)
320 struct udev_device
*udev_device
;
323 udev_device
= udev_device_new(udev
);
327 r
= sd_device_new_from_device_id(&udev_device
->device
, id
);
330 udev_device_unref(udev_device
);
338 * udev_device_new_from_subsystem_sysname:
339 * @udev: udev library context
340 * @subsystem: the subsystem of the device
341 * @sysname: the name of the device
343 * Create new udev device, and fill in information from the sys device
344 * and the udev database entry. The device is looked up by the subsystem
345 * and name string of the device, like "mem" / "zero", or "block" / "sda".
347 * The initial refcount is 1, and needs to be decremented to
348 * release the resources of the udev device.
350 * Returns: a new udev device, or #NULL, if it does not exist
352 _public_
struct udev_device
*udev_device_new_from_subsystem_sysname(struct udev
*udev
, const char *subsystem
, const char *sysname
)
354 struct udev_device
*udev_device
;
357 udev_device
= udev_device_new(udev
);
361 r
= sd_device_new_from_subsystem_sysname(&udev_device
->device
, subsystem
, sysname
);
364 udev_device_unref(udev_device
);
372 * udev_device_new_from_environment
373 * @udev: udev library context
375 * Create new udev device, and fill in information from the
376 * current process environment. This only works reliable if
377 * the process is called from a udev rule. It is usually used
378 * for tools executed from IMPORT= rules.
380 * The initial refcount is 1, and needs to be decremented to
381 * release the resources of the udev device.
383 * Returns: a new udev device, or #NULL, if it does not exist
385 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
)
387 struct udev_device
*udev_device
;
390 udev_device
= udev_device_new(udev
);
394 r
= device_new_from_strv(&udev_device
->device
, environ
);
397 udev_device_unref(udev_device
);
404 static struct udev_device
*device_new_from_parent(struct udev_device
*child
)
406 struct udev_device
*parent
;
409 assert_return_errno(child
, NULL
, EINVAL
);
411 parent
= udev_device_new(child
->udev
);
415 r
= sd_device_get_parent(child
->device
, &parent
->device
);
418 udev_device_unref(parent
);
422 /* the parent is unref'ed with the child, so take a ref from libudev as well */
423 sd_device_ref(parent
->device
);
429 * udev_device_get_parent:
430 * @udev_device: the device to start searching from
432 * Find the next parent device, and fill in information from the sys
433 * device and the udev database entry.
435 * Returned device is not referenced. It is attached to the child
436 * device, and will be cleaned up when the child device is cleaned up.
438 * It is not necessarily just the upper level directory, empty or not
439 * recognized sys directories are ignored.
441 * It can be called as many times as needed, without caring about
444 * Returns: a new udev device, or #NULL, if it no parent exist.
446 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
)
448 assert_return_errno(udev_device
, NULL
, EINVAL
);
450 if (!udev_device
->parent_set
) {
451 udev_device
->parent_set
= true;
452 udev_device
->parent
= device_new_from_parent(udev_device
);
455 /* TODO: errno will differ here in case parent == NULL */
456 return udev_device
->parent
;
460 * udev_device_get_parent_with_subsystem_devtype:
461 * @udev_device: udev device to start searching from
462 * @subsystem: the subsystem of the device
463 * @devtype: the type (DEVTYPE) of the device
465 * Find the next parent device, with a matching subsystem and devtype
466 * value, and fill in information from the sys device and the udev
469 * If devtype is #NULL, only subsystem is checked, and any devtype will
472 * Returned device is not referenced. It is attached to the child
473 * device, and will be cleaned up when the child device is cleaned up.
475 * It can be called as many times as needed, without caring about
478 * Returns: a new udev device, or #NULL if no matching parent exists.
480 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
)
485 assert_return_errno(udev_device
, NULL
, EINVAL
);
487 /* this relies on the fact that finding the subdevice of a parent or the
488 parent of a subdevice commute */
490 /* first find the correct sd_device */
491 r
= sd_device_get_parent_with_subsystem_devtype(udev_device
->device
, subsystem
, devtype
, &parent
);
497 /* then walk the chain of udev_device parents until the correspanding
499 while ((udev_device
= udev_device_get_parent(udev_device
))) {
500 if (udev_device
->device
== parent
)
509 * udev_device_get_udev:
510 * @udev_device: udev device
512 * Retrieve the udev library context the device was created with.
514 * Returns: the udev library context
516 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
)
518 assert_return_errno(udev_device
, NULL
, EINVAL
);
520 return udev_device
->udev
;
525 * @udev_device: udev device
527 * Take a reference of a udev device.
529 * Returns: the passed udev device
531 _public_
struct udev_device
*udev_device_ref(struct udev_device
*udev_device
)
534 udev_device
->refcount
++;
541 * @udev_device: udev device
543 * Drop a reference of a udev device. If the refcount reaches zero,
544 * the resources of the device will be released.
548 _public_
struct udev_device
*udev_device_unref(struct udev_device
*udev_device
)
550 if (udev_device
&& (-- udev_device
->refcount
) == 0) {
551 sd_device_unref(udev_device
->device
);
552 udev_device_unref(udev_device
->parent
);
554 udev_list_cleanup(&udev_device
->properties
);
555 udev_list_cleanup(&udev_device
->sysattrs
);
556 udev_list_cleanup(&udev_device
->tags
);
557 udev_list_cleanup(&udev_device
->devlinks
);
566 * udev_device_get_devpath:
567 * @udev_device: udev device
569 * Retrieve the kernel devpath value of the udev device. The path
570 * does not contain the sys mount point, and starts with a '/'.
572 * Returns: the devpath of the udev device
574 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
)
579 assert_return_errno(udev_device
, NULL
, EINVAL
);
581 r
= sd_device_get_devpath(udev_device
->device
, &devpath
);
591 * udev_device_get_syspath:
592 * @udev_device: udev device
594 * Retrieve the sys path of the udev device. The path is an
595 * absolute path and starts with the sys mount point.
597 * Returns: the sys path of the udev device
599 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
)
604 assert_return_errno(udev_device
, NULL
, EINVAL
);
606 r
= sd_device_get_syspath(udev_device
->device
, &syspath
);
616 * udev_device_get_sysname:
617 * @udev_device: udev device
619 * Get the kernel device name in /sys.
621 * Returns: the name string of the device device
623 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
)
628 assert_return_errno(udev_device
, NULL
, EINVAL
);
630 r
= sd_device_get_sysname(udev_device
->device
, &sysname
);
640 * udev_device_get_sysnum:
641 * @udev_device: udev device
643 * Get the instance number of the device.
645 * Returns: the trailing number string of the device name
647 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
)
652 assert_return_errno(udev_device
, NULL
, EINVAL
);
654 r
= sd_device_get_sysnum(udev_device
->device
, &sysnum
);
664 * udev_device_get_devnode:
665 * @udev_device: udev device
667 * Retrieve the device node file name belonging to the udev device.
668 * The path is an absolute path, and starts with the device directory.
670 * Returns: the device node file name of the udev device, or #NULL if no device node exists
672 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
)
677 assert_return_errno(udev_device
, NULL
, EINVAL
);
679 r
= sd_device_get_devname(udev_device
->device
, &devnode
);
689 * udev_device_get_devlinks_list_entry:
690 * @udev_device: udev device
692 * Retrieve the list of device links pointing to the device file of
693 * the udev device. The next list entry can be retrieved with
694 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
695 * The devlink path can be retrieved from the list entry by
696 * udev_list_entry_get_name(). The path is an absolute path, and starts with
697 * the device directory.
699 * Returns: the first entry of the device node link list
701 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
)
703 assert_return_errno(udev_device
, NULL
, EINVAL
);
705 if (device_get_devlinks_generation(udev_device
->device
) != udev_device
->devlinks_generation
||
706 !udev_device
->devlinks_read
) {
709 udev_list_cleanup(&udev_device
->devlinks
);
711 FOREACH_DEVICE_DEVLINK(udev_device
->device
, devlink
)
712 udev_list_entry_add(&udev_device
->devlinks
, devlink
, NULL
);
714 udev_device
->devlinks_read
= true;
715 udev_device
->devlinks_generation
= device_get_devlinks_generation(udev_device
->device
);
718 return udev_list_get_entry(&udev_device
->devlinks
);
722 * udev_device_get_event_properties_entry:
723 * @udev_device: udev device
725 * Retrieve the list of key/value device properties of the udev
726 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
727 * which returns #NULL if no more entries exist. The property name
728 * can be retrieved from the list entry by udev_list_entry_get_name(),
729 * the property value by udev_list_entry_get_value().
731 * Returns: the first entry of the property list
733 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
)
735 assert_return_errno(udev_device
, NULL
, EINVAL
);
737 if (device_get_properties_generation(udev_device
->device
) != udev_device
->properties_generation
||
738 !udev_device
->properties_read
) {
739 const char *key
, *value
;
741 udev_list_cleanup(&udev_device
->properties
);
743 FOREACH_DEVICE_PROPERTY(udev_device
->device
, key
, value
)
744 udev_list_entry_add(&udev_device
->properties
, key
, value
);
746 udev_device
->properties_read
= true;
747 udev_device
->properties_generation
= device_get_properties_generation(udev_device
->device
);
750 return udev_list_get_entry(&udev_device
->properties
);
754 * udev_device_get_action:
755 * @udev_device: udev device
757 * This is only valid if the device was received through a monitor. Devices read from
758 * sys do not have an action string. Usual actions are: add, remove, change, online,
761 * Returns: the kernel action value, or #NULL if there is no action value available.
763 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
) {
764 const char *action
= NULL
;
767 assert_return_errno(udev_device
, NULL
, EINVAL
);
769 r
= sd_device_get_property_value(udev_device
->device
, "ACTION", &action
);
770 if (r
< 0 && r
!= -ENOENT
) {
779 * udev_device_get_usec_since_initialized:
780 * @udev_device: udev device
782 * Return the number of microseconds passed since udev set up the
783 * device for the first time.
785 * This is only implemented for devices with need to store properties
786 * in the udev database. All other devices return 0 here.
788 * Returns: the number of microseconds since the device was first seen.
790 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
)
795 assert_return(udev_device
, -EINVAL
);
797 r
= sd_device_get_usec_since_initialized(udev_device
->device
, &ts
);
807 * udev_device_get_sysattr_value:
808 * @udev_device: udev device
809 * @sysattr: attribute name
811 * The retrieved value is cached in the device. Repeated calls will return the same
812 * value and not open the attribute again.
814 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
816 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
)
821 assert_return_errno(udev_device
, NULL
, EINVAL
);
823 r
= sd_device_get_sysattr_value(udev_device
->device
, sysattr
, &value
);
833 * udev_device_set_sysattr_value:
834 * @udev_device: udev device
835 * @sysattr: attribute name
836 * @value: new value to be set
838 * Update the contents of the sys attribute and the cached value of the device.
840 * Returns: Negative error code on failure or 0 on success.
842 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, char *value
)
846 assert_return(udev_device
, -EINVAL
);
848 r
= sd_device_set_sysattr_value(udev_device
->device
, sysattr
, value
);
856 * udev_device_get_sysattr_list_entry:
857 * @udev_device: udev device
859 * Retrieve the list of available sysattrs, with value being empty;
860 * This just return all available sysfs attributes for a particular
861 * device without reading their values.
863 * Returns: the first entry of the property list
865 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
)
867 assert_return_errno(udev_device
, NULL
, EINVAL
);
869 if (!udev_device
->sysattrs_read
) {
872 udev_list_cleanup(&udev_device
->sysattrs
);
874 FOREACH_DEVICE_SYSATTR(udev_device
->device
, sysattr
)
875 udev_list_entry_add(&udev_device
->sysattrs
, sysattr
, NULL
);
877 udev_device
->sysattrs_read
= true;
880 return udev_list_get_entry(&udev_device
->sysattrs
);
884 * udev_device_get_is_initialized:
885 * @udev_device: udev device
887 * Check if udev has already handled the device and has set up
888 * device node permissions and context, or has renamed a network
891 * This is only implemented for devices with a device node
892 * or network interfaces. All other devices return 1 here.
894 * Returns: 1 if the device is set up. 0 otherwise.
896 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
)
900 assert_return(udev_device
, -EINVAL
);
902 r
= sd_device_get_is_initialized(udev_device
->device
, &initialized
);
913 * udev_device_get_tags_list_entry:
914 * @udev_device: udev device
916 * Retrieve the list of tags attached to the udev device. The next
917 * list entry can be retrieved with udev_list_entry_get_next(),
918 * which returns #NULL if no more entries exist. The tag string
919 * can be retrieved from the list entry by udev_list_entry_get_name().
921 * Returns: the first entry of the tag list
923 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
)
925 assert_return_errno(udev_device
, NULL
, EINVAL
);
927 if (device_get_tags_generation(udev_device
->device
) != udev_device
->tags_generation
||
928 !udev_device
->tags_read
) {
931 udev_list_cleanup(&udev_device
->tags
);
933 FOREACH_DEVICE_TAG(udev_device
->device
, tag
)
934 udev_list_entry_add(&udev_device
->tags
, tag
, NULL
);
936 udev_device
->tags_read
= true;
937 udev_device
->tags_generation
= device_get_tags_generation(udev_device
->device
);
940 return udev_list_get_entry(&udev_device
->tags
);
944 * udev_device_has_tag:
945 * @udev_device: udev device
948 * Check if a given device has a certain tag associated.
950 * Returns: 1 if the tag is found. 0 otherwise.
952 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
)
954 assert_return(udev_device
, 0);
956 return sd_device_has_tag(udev_device
->device
, tag
);