2 This file is part of systemd.
4 Copyright 2008-2012 Kay Sievers <kay@vrfy.org>
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
34 #include <linux/sockios.h>
37 #include "libudev-private.h"
39 static int udev_device_read_uevent_file(struct udev_device
*udev_device
);
40 static int udev_device_read_db(struct udev_device
*udev_device
);
41 static int udev_device_set_devnode(struct udev_device
*udev_device
, const char *devnode
);
42 static struct udev_list_entry
*udev_device_add_property_internal(struct udev_device
*udev_device
, const char *key
, const char *value
);
45 * SECTION:libudev-device
46 * @short_description: kernel sys devices
48 * Representation of kernel sys devices. Devices are uniquely identified
49 * by their syspath, every device has exactly one path in the kernel sys
50 * filesystem. Devices usually belong to a kernel subsystem, and have
51 * a unique name inside that subsystem.
57 * Opaque object representing one kernel sys device.
61 struct udev_device
*parent_device
;
78 size_t monitor_buf_len
;
79 struct udev_list devlinks_list
;
80 struct udev_list properties_list
;
81 struct udev_list sysattr_value_list
;
82 struct udev_list sysattr_list
;
83 struct udev_list tags_list
;
84 unsigned long long int seqnum
;
85 usec_t usec_initialized
;
95 bool devlinks_uptodate
;
103 bool sysattr_list_read
;
108 * udev_device_get_seqnum:
109 * @udev_device: udev device
111 * This is only valid if the device was received through a monitor. Devices read from
112 * sys do not have a sequence number.
114 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
116 _public_
unsigned long long int udev_device_get_seqnum(struct udev_device
*udev_device
)
118 if (udev_device
== NULL
)
120 return udev_device
->seqnum
;
123 static int udev_device_set_seqnum(struct udev_device
*udev_device
, unsigned long long int seqnum
)
127 udev_device
->seqnum
= seqnum
;
128 snprintf(num
, sizeof(num
), "%llu", seqnum
);
129 udev_device_add_property_internal(udev_device
, "SEQNUM", num
);
133 int udev_device_get_ifindex(struct udev_device
*udev_device
)
135 if (!udev_device
->info_loaded
)
136 udev_device_read_uevent_file(udev_device
);
137 return udev_device
->ifindex
;
140 static int udev_device_set_ifindex(struct udev_device
*udev_device
, int ifindex
)
144 udev_device
->ifindex
= ifindex
;
145 snprintf(num
, sizeof(num
), "%d", ifindex
);
146 udev_device_add_property_internal(udev_device
, "IFINDEX", num
);
151 * udev_device_get_devnum:
152 * @udev_device: udev device
154 * Get the device major/minor number.
156 * Returns: the dev_t number.
158 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
)
160 if (udev_device
== NULL
)
161 return makedev(0, 0);
162 if (!udev_device
->info_loaded
)
163 udev_device_read_uevent_file(udev_device
);
164 return udev_device
->devnum
;
167 static int udev_device_set_devnum(struct udev_device
*udev_device
, dev_t devnum
)
171 udev_device
->devnum
= devnum
;
173 snprintf(num
, sizeof(num
), "%u", major(devnum
));
174 udev_device_add_property_internal(udev_device
, "MAJOR", num
);
175 snprintf(num
, sizeof(num
), "%u", minor(devnum
));
176 udev_device_add_property_internal(udev_device
, "MINOR", num
);
180 const char *udev_device_get_devpath_old(struct udev_device
*udev_device
)
182 return udev_device
->devpath_old
;
185 static int udev_device_set_devpath_old(struct udev_device
*udev_device
, const char *devpath_old
)
189 free(udev_device
->devpath_old
);
190 udev_device
->devpath_old
= strdup(devpath_old
);
191 if (udev_device
->devpath_old
== NULL
)
193 udev_device_add_property_internal(udev_device
, "DEVPATH_OLD", udev_device
->devpath_old
);
195 pos
= strrchr(udev_device
->devpath_old
, '/');
202 * udev_device_get_driver:
203 * @udev_device: udev device
205 * Get the kernel driver name.
207 * Returns: the driver name string, or #NULL if there is no driver attached.
209 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
)
211 char driver
[UTIL_NAME_SIZE
];
213 if (udev_device
== NULL
)
215 if (!udev_device
->driver_set
) {
216 udev_device
->driver_set
= true;
217 if (util_get_sys_core_link_value(udev_device
->udev
, "driver", udev_device
->syspath
, driver
, sizeof(driver
)) > 0)
218 udev_device
->driver
= strdup(driver
);
220 return udev_device
->driver
;
223 static int udev_device_set_driver(struct udev_device
*udev_device
, const char *driver
)
225 free(udev_device
->driver
);
226 udev_device
->driver
= strdup(driver
);
227 if (udev_device
->driver
== NULL
)
229 udev_device
->driver_set
= true;
230 udev_device_add_property_internal(udev_device
, "DRIVER", udev_device
->driver
);
235 * udev_device_get_devtype:
236 * @udev_device: udev device
238 * Retrieve the devtype string of the udev device.
240 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
242 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
)
244 if (udev_device
== NULL
)
246 if (!udev_device
->devtype_set
) {
247 udev_device
->devtype_set
= true;
248 udev_device_read_uevent_file(udev_device
);
250 return udev_device
->devtype
;
253 static int udev_device_set_devtype(struct udev_device
*udev_device
, const char *devtype
)
255 free(udev_device
->devtype
);
256 udev_device
->devtype
= strdup(devtype
);
257 if (udev_device
->devtype
== NULL
)
259 udev_device
->devtype_set
= true;
260 udev_device_add_property_internal(udev_device
, "DEVTYPE", udev_device
->devtype
);
264 static int udev_device_set_subsystem(struct udev_device
*udev_device
, const char *subsystem
)
266 free(udev_device
->subsystem
);
267 udev_device
->subsystem
= strdup(subsystem
);
268 if (udev_device
->subsystem
== NULL
)
270 udev_device
->subsystem_set
= true;
271 udev_device_add_property_internal(udev_device
, "SUBSYSTEM", udev_device
->subsystem
);
276 * udev_device_get_subsystem:
277 * @udev_device: udev device
279 * Retrieve the subsystem string of the udev device. The string does not
282 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
284 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
)
286 char subsystem
[UTIL_NAME_SIZE
];
288 if (udev_device
== NULL
)
290 if (!udev_device
->subsystem_set
) {
291 udev_device
->subsystem_set
= true;
292 /* read "subsystem" link */
293 if (util_get_sys_core_link_value(udev_device
->udev
, "subsystem", udev_device
->syspath
, subsystem
, sizeof(subsystem
)) > 0) {
294 udev_device_set_subsystem(udev_device
, subsystem
);
295 return udev_device
->subsystem
;
298 if (startswith(udev_device
->devpath
, "/module/")) {
299 udev_device_set_subsystem(udev_device
, "module");
300 return udev_device
->subsystem
;
302 if (strstr(udev_device
->devpath
, "/drivers/") != NULL
) {
303 udev_device_set_subsystem(udev_device
, "drivers");
304 return udev_device
->subsystem
;
306 if (startswith(udev_device
->devpath
, "/subsystem/") ||
307 startswith(udev_device
->devpath
, "/class/") ||
308 startswith(udev_device
->devpath
, "/bus/")) {
309 udev_device_set_subsystem(udev_device
, "subsystem");
310 return udev_device
->subsystem
;
313 return udev_device
->subsystem
;
316 mode_t
udev_device_get_devnode_mode(struct udev_device
*udev_device
)
318 if (!udev_device
->info_loaded
)
319 udev_device_read_uevent_file(udev_device
);
320 return udev_device
->devnode_mode
;
323 static int udev_device_set_devnode_mode(struct udev_device
*udev_device
, mode_t mode
)
327 udev_device
->devnode_mode
= mode
;
328 snprintf(num
, sizeof(num
), "%#o", mode
);
329 udev_device_add_property_internal(udev_device
, "DEVMODE", num
);
333 uid_t
udev_device_get_devnode_uid(struct udev_device
*udev_device
)
335 if (!udev_device
->info_loaded
)
336 udev_device_read_uevent_file(udev_device
);
337 return udev_device
->devnode_uid
;
340 static int udev_device_set_devnode_uid(struct udev_device
*udev_device
, uid_t uid
)
344 udev_device
->devnode_uid
= uid
;
345 snprintf(num
, sizeof(num
), "%u", uid
);
346 udev_device_add_property_internal(udev_device
, "DEVUID", num
);
350 gid_t
udev_device_get_devnode_gid(struct udev_device
*udev_device
)
352 if (!udev_device
->info_loaded
)
353 udev_device_read_uevent_file(udev_device
);
354 return udev_device
->devnode_gid
;
357 static int udev_device_set_devnode_gid(struct udev_device
*udev_device
, gid_t gid
)
361 udev_device
->devnode_gid
= gid
;
362 snprintf(num
, sizeof(num
), "%u", gid
);
363 udev_device_add_property_internal(udev_device
, "DEVGID", num
);
367 static struct udev_list_entry
*udev_device_add_property_internal(struct udev_device
*udev_device
, const char *key
, const char *value
)
369 udev_device
->envp_uptodate
= false;
371 struct udev_list_entry
*list_entry
;
373 list_entry
= udev_device_get_properties_list_entry(udev_device
);
374 list_entry
= udev_list_entry_get_by_name(list_entry
, key
);
375 if (list_entry
!= NULL
)
376 udev_list_entry_delete(list_entry
);
379 return udev_list_entry_add(&udev_device
->properties_list
, key
, value
);
383 int udev_device_add_property(struct udev_device
*udev_device
, const char *key
, const char *value
)
385 struct udev_list_entry
*property
;
387 property
= udev_device_add_property_internal(udev_device
, key
, value
);
389 /* store in db, skip private keys */
391 udev_list_entry_set_num(property
, true);
396 static struct udev_list_entry
*udev_device_add_property_from_string(struct udev_device
*udev_device
, const char *property
)
398 char name
[UTIL_LINE_SIZE
];
401 strscpy(name
, sizeof(name
), property
);
402 val
= strchr(name
, '=');
409 return udev_device_add_property_internal(udev_device
, name
, val
);
412 static int udev_device_set_syspath(struct udev_device
*udev_device
, const char *syspath
)
417 free(udev_device
->syspath
);
418 udev_device
->syspath
= strdup(syspath
);
419 if (udev_device
->syspath
== NULL
)
421 udev_device
->devpath
= udev_device
->syspath
+ strlen("/sys");
422 udev_device_add_property_internal(udev_device
, "DEVPATH", udev_device
->devpath
);
424 pos
= strrchr(udev_device
->syspath
, '/');
427 udev_device
->sysname
= strdup(&pos
[1]);
428 if (udev_device
->sysname
== NULL
)
431 /* some devices have '!' in their name, change that to '/' */
433 while (udev_device
->sysname
[len
] != '\0') {
434 if (udev_device
->sysname
[len
] == '!')
435 udev_device
->sysname
[len
] = '/';
439 /* trailing number */
440 while (len
> 0 && isdigit(udev_device
->sysname
[--len
]))
441 udev_device
->sysnum
= &udev_device
->sysname
[len
];
443 /* sysname is completely numeric */
445 udev_device
->sysnum
= NULL
;
450 static void udev_device_set_usec_initialized(struct udev_device
*udev_device
, usec_t usec_initialized
)
452 char num
[DECIMAL_STR_MAX(usec_t
)];
454 udev_device
->usec_initialized
= usec_initialized
;
455 snprintf(num
, sizeof(num
), USEC_FMT
, usec_initialized
);
456 udev_device_add_property_internal(udev_device
, "USEC_INITIALIZED", num
);
459 void udev_device_ensure_usec_initialized(struct udev_device
*udev_device
, struct udev_device
*old_device
)
461 if (old_device
&& old_device
->usec_initialized
!= 0)
462 udev_device_set_usec_initialized(udev_device
, old_device
->usec_initialized
);
464 udev_device_set_usec_initialized(udev_device
, now(CLOCK_MONOTONIC
));
467 static int udev_device_set_action(struct udev_device
*udev_device
, const char *action
)
469 free(udev_device
->action
);
470 udev_device
->action
= strdup(action
);
471 if (udev_device
->action
== NULL
)
473 udev_device_add_property_internal(udev_device
, "ACTION", udev_device
->action
);
478 * parse property string, and if needed, update internal values accordingly
480 * udev_device_add_property_from_string_parse_finish() needs to be
481 * called after adding properties, and its return value checked
483 * udev_device_set_info_loaded() needs to be set, to avoid trying
484 * to use a device without a DEVPATH set
486 static void udev_device_add_property_from_string_parse(struct udev_device
*udev_device
, const char *property
)
488 if (startswith(property
, "DEVPATH=")) {
489 char path
[UTIL_PATH_SIZE
];
491 strscpyl(path
, sizeof(path
), "/sys", &property
[8], NULL
);
492 udev_device_set_syspath(udev_device
, path
);
493 } else if (startswith(property
, "SUBSYSTEM=")) {
494 udev_device_set_subsystem(udev_device
, &property
[10]);
495 } else if (startswith(property
, "DEVTYPE=")) {
496 udev_device_set_devtype(udev_device
, &property
[8]);
497 } else if (startswith(property
, "DEVNAME=")) {
498 udev_device_set_devnode(udev_device
, &property
[8]);
499 } else if (startswith(property
, "DEVLINKS=")) {
500 char devlinks
[UTIL_PATH_SIZE
];
504 strscpy(devlinks
, sizeof(devlinks
), &property
[9]);
506 next
= strchr(slink
, ' ');
507 while (next
!= NULL
) {
509 udev_device_add_devlink(udev_device
, slink
);
511 next
= strchr(slink
, ' ');
513 if (slink
[0] != '\0')
514 udev_device_add_devlink(udev_device
, slink
);
515 } else if (startswith(property
, "TAGS=")) {
516 char tags
[UTIL_PATH_SIZE
];
519 strscpy(tags
, sizeof(tags
), &property
[5]);
520 next
= strchr(tags
, ':');
523 while (next
[0] != '\0') {
527 next
= strchr(tag
, ':');
532 udev_device_add_tag(udev_device
, tag
);
535 } else if (startswith(property
, "USEC_INITIALIZED=")) {
536 udev_device_set_usec_initialized(udev_device
, strtoull(&property
[19], NULL
, 10));
537 } else if (startswith(property
, "DRIVER=")) {
538 udev_device_set_driver(udev_device
, &property
[7]);
539 } else if (startswith(property
, "ACTION=")) {
540 udev_device_set_action(udev_device
, &property
[7]);
541 } else if (startswith(property
, "MAJOR=")) {
542 udev_device
->maj
= strtoull(&property
[6], NULL
, 10);
543 } else if (startswith(property
, "MINOR=")) {
544 udev_device
->min
= strtoull(&property
[6], NULL
, 10);
545 } else if (startswith(property
, "DEVPATH_OLD=")) {
546 udev_device_set_devpath_old(udev_device
, &property
[12]);
547 } else if (startswith(property
, "SEQNUM=")) {
548 udev_device_set_seqnum(udev_device
, strtoull(&property
[7], NULL
, 10));
549 } else if (startswith(property
, "IFINDEX=")) {
550 udev_device_set_ifindex(udev_device
, strtoull(&property
[8], NULL
, 10));
551 } else if (startswith(property
, "DEVMODE=")) {
552 udev_device_set_devnode_mode(udev_device
, strtoul(&property
[8], NULL
, 8));
553 } else if (startswith(property
, "DEVUID=")) {
554 udev_device_set_devnode_uid(udev_device
, strtoul(&property
[7], NULL
, 10));
555 } else if (startswith(property
, "DEVGID=")) {
556 udev_device_set_devnode_gid(udev_device
, strtoul(&property
[7], NULL
, 10));
558 udev_device_add_property_from_string(udev_device
, property
);
562 static int udev_device_add_property_from_string_parse_finish(struct udev_device
*udev_device
)
564 if (udev_device
->maj
> 0)
565 udev_device_set_devnum(udev_device
, makedev(udev_device
->maj
, udev_device
->min
));
566 udev_device
->maj
= 0;
567 udev_device
->min
= 0;
569 if (udev_device
->devpath
== NULL
|| udev_device
->subsystem
== NULL
)
575 * udev_device_get_property_value:
576 * @udev_device: udev device
577 * @key: property name
579 * Get the value of a given property.
581 * Returns: the property string, or #NULL if there is no such property.
583 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
)
585 struct udev_list_entry
*list_entry
;
587 if (udev_device
== NULL
)
592 list_entry
= udev_device_get_properties_list_entry(udev_device
);
593 list_entry
= udev_list_entry_get_by_name(list_entry
, key
);
594 return udev_list_entry_get_value(list_entry
);
597 static int udev_device_read_db(struct udev_device
*udev_device
)
599 char filename
[UTIL_PATH_SIZE
];
600 char line
[UTIL_LINE_SIZE
];
604 if (udev_device
->db_loaded
)
607 udev_device
->db_loaded
= true;
609 id
= udev_device_get_id_filename(udev_device
);
613 strscpyl(filename
, sizeof(filename
), "/run/udev/data/", id
, NULL
);
615 f
= fopen(filename
, "re");
617 return log_debug_errno(errno
, "no db file to read %s: %m", filename
);
619 /* devices with a database entry are initialized */
620 udev_device
->is_initialized
= true;
622 while (fgets(line
, sizeof(line
), f
)) {
625 struct udev_list_entry
*entry
;
634 strscpyl(filename
, sizeof(filename
), "/dev/", val
, NULL
);
635 udev_device_add_devlink(udev_device
, filename
);
638 udev_device_set_devlink_priority(udev_device
, atoi(val
));
641 entry
= udev_device_add_property_from_string(udev_device
, val
);
642 udev_list_entry_set_num(entry
, true);
645 udev_device_add_tag(udev_device
, val
);
648 udev_device_set_watch_handle(udev_device
, atoi(val
));
651 udev_device_set_usec_initialized(udev_device
, strtoull(val
, NULL
, 10));
657 log_trace("device %p filled with db file data", udev_device
);
661 static int udev_device_read_uevent_file(struct udev_device
*udev_device
)
663 char filename
[UTIL_PATH_SIZE
];
665 char line
[UTIL_LINE_SIZE
];
669 if (udev_device
->uevent_loaded
)
672 strscpyl(filename
, sizeof(filename
), udev_device
->syspath
, "/uevent", NULL
);
673 f
= fopen(filename
, "re");
676 udev_device
->uevent_loaded
= true;
678 while (fgets(line
, sizeof(line
), f
)) {
681 pos
= strchr(line
, '\n');
686 if (startswith(line
, "DEVTYPE=")) {
687 udev_device_set_devtype(udev_device
, &line
[8]);
690 if (startswith(line
, "IFINDEX=")) {
691 udev_device_set_ifindex(udev_device
, strtoull(&line
[8], NULL
, 10));
694 if (startswith(line
, "DEVNAME=")) {
695 udev_device_set_devnode(udev_device
, &line
[8]);
699 if (startswith(line
, "MAJOR="))
700 maj
= strtoull(&line
[6], NULL
, 10);
701 else if (startswith(line
, "MINOR="))
702 min
= strtoull(&line
[6], NULL
, 10);
703 else if (startswith(line
, "DEVMODE="))
704 udev_device
->devnode_mode
= strtoul(&line
[8], NULL
, 8);
706 udev_device_add_property_from_string(udev_device
, line
);
709 udev_device
->devnum
= makedev(maj
, min
);
714 void udev_device_set_info_loaded(struct udev_device
*device
)
716 device
->info_loaded
= true;
719 static struct udev_device
*udev_device_new(struct udev
*udev
)
721 struct udev_device
*udev_device
;
728 udev_device
= new0(struct udev_device
, 1);
729 if (udev_device
== NULL
) {
733 udev_device
->refcount
= 1;
734 udev_device
->udev
= udev
;
735 udev_list_init(udev
, &udev_device
->devlinks_list
, true);
736 udev_list_init(udev
, &udev_device
->properties_list
, true);
737 udev_list_init(udev
, &udev_device
->sysattr_value_list
, true);
738 udev_list_init(udev
, &udev_device
->sysattr_list
, false);
739 udev_list_init(udev
, &udev_device
->tags_list
, true);
740 udev_device
->watch_handle
= -1;
746 * udev_device_new_from_syspath:
747 * @udev: udev library context
748 * @syspath: sys device path including sys directory
750 * Create new udev device, and fill in information from the sys
751 * device and the udev database entry. The syspath is the absolute
752 * path to the device, including the sys mount point.
754 * The initial refcount is 1, and needs to be decremented to
755 * release the resources of the udev device.
757 * Returns: a new udev device, or #NULL, if it does not exist
759 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
)
762 char path
[UTIL_PATH_SIZE
];
765 struct udev_device
*udev_device
;
772 if (syspath
== NULL
) {
777 /* path starts in sys */
778 if (!startswith(syspath
, "/sys")) {
779 log_debug("not in sys :%s", syspath
);
784 /* path is not a root directory */
785 subdir
= syspath
+ strlen("/sys");
786 pos
= strrchr(subdir
, '/');
787 if (pos
== NULL
|| pos
[1] == '\0' || pos
< &subdir
[2]) {
792 /* resolve possible symlink to real path */
793 strscpy(path
, sizeof(path
), syspath
);
794 util_resolve_sys_link(udev
, path
, sizeof(path
));
796 if (startswith(path
+ strlen("/sys"), "/devices/")) {
797 char file
[UTIL_PATH_SIZE
];
799 /* all "devices" require a "uevent" file */
800 strscpyl(file
, sizeof(file
), path
, "/uevent", NULL
);
801 if (stat(file
, &statbuf
) != 0)
804 /* everything else just needs to be a directory */
805 if (stat(path
, &statbuf
) != 0)
808 if (!S_ISDIR(statbuf
.st_mode
)) {
814 udev_device
= udev_device_new(udev
);
815 if (udev_device
== NULL
)
818 udev_device_set_syspath(udev_device
, path
);
819 log_trace("device %p has devpath '%s'", udev_device
, udev_device_get_devpath(udev_device
));
825 * udev_device_new_from_devnum:
826 * @udev: udev library context
827 * @type: char or block device
828 * @devnum: device major/minor number
830 * Create new udev device, and fill in information from the sys
831 * device and the udev database entry. The device is looked-up
832 * by its major/minor number and type. Character and block device
833 * numbers are not unique across the two types.
835 * The initial refcount is 1, and needs to be decremented to
836 * release the resources of the udev device.
838 * Returns: a new udev device, or #NULL, if it does not exist
840 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
)
842 char path
[UTIL_PATH_SIZE
];
843 const char *type_str
;
847 else if (type
== 'c')
854 /* use /sys/dev/{block,char}/<maj>:<min> link */
855 snprintf(path
, sizeof(path
), "/sys/dev/%s/%u:%u",
856 type_str
, major(devnum
), minor(devnum
));
857 return udev_device_new_from_syspath(udev
, path
);
861 * udev_device_new_from_device_id:
862 * @udev: udev library context
863 * @id: text string identifying a kernel device
865 * Create new udev device, and fill in information from the sys
866 * device and the udev database entry. The device is looked-up
867 * by a special string:
868 * b8:2 - block device major:minor
869 * c128:1 - char device major:minor
870 * n3 - network device ifindex
871 * +sound:card29 - kernel driver core subsystem:device name
873 * The initial refcount is 1, and needs to be decremented to
874 * release the resources of the udev device.
876 * Returns: a new udev device, or #NULL, if it does not exist
878 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
)
882 char subsys
[UTIL_PATH_SIZE
];
888 if (sscanf(id
, "%c%i:%i", &type
, &maj
, &min
) != 3)
890 return udev_device_new_from_devnum(udev
, type
, makedev(maj
, min
));
894 struct udev_device
*dev
;
897 ifindex
= strtoul(&id
[1], NULL
, 10);
903 sk
= socket(PF_INET
, SOCK_DGRAM
, 0);
906 memzero(&ifr
, sizeof(struct ifreq
));
907 ifr
.ifr_ifindex
= ifindex
;
908 if (ioctl(sk
, SIOCGIFNAME
, &ifr
) != 0) {
914 dev
= udev_device_new_from_subsystem_sysname(udev
, "net", ifr
.ifr_name
);
917 if (udev_device_get_ifindex(dev
) == ifindex
)
920 /* this is racy, so we may end up with the wrong device */
921 udev_device_unref(dev
);
926 strscpy(subsys
, sizeof(subsys
), &id
[1]);
927 sysname
= strchr(subsys
, ':');
928 if (sysname
== NULL
) {
933 sysname
= &sysname
[1];
934 return udev_device_new_from_subsystem_sysname(udev
, subsys
, sysname
);
942 * udev_device_new_from_subsystem_sysname:
943 * @udev: udev library context
944 * @subsystem: the subsystem of the device
945 * @sysname: the name of the device
947 * Create new udev device, and fill in information from the sys device
948 * and the udev database entry. The device is looked up by the subsystem
949 * and name string of the device, like "mem" / "zero", or "block" / "sda".
951 * The initial refcount is 1, and needs to be decremented to
952 * release the resources of the udev device.
954 * Returns: a new udev device, or #NULL, if it does not exist
956 _public_
struct udev_device
*udev_device_new_from_subsystem_sysname(struct udev
*udev
, const char *subsystem
, const char *sysname
)
958 char path
[UTIL_PATH_SIZE
];
961 if (streq(subsystem
, "subsystem")) {
962 strscpyl(path
, sizeof(path
), "/sys/subsystem/", sysname
, NULL
);
963 if (stat(path
, &statbuf
) == 0)
966 strscpyl(path
, sizeof(path
), "/sys/bus/", sysname
, NULL
);
967 if (stat(path
, &statbuf
) == 0)
970 strscpyl(path
, sizeof(path
), "/sys/class/", sysname
, NULL
);
971 if (stat(path
, &statbuf
) == 0)
976 if (streq(subsystem
, "module")) {
977 strscpyl(path
, sizeof(path
), "/sys/module/", sysname
, NULL
);
978 if (stat(path
, &statbuf
) == 0)
983 if (streq(subsystem
, "drivers")) {
984 char subsys
[UTIL_NAME_SIZE
];
987 strscpy(subsys
, sizeof(subsys
), sysname
);
988 driver
= strchr(subsys
, ':');
989 if (driver
!= NULL
) {
993 strscpyl(path
, sizeof(path
), "/sys/subsystem/", subsys
, "/drivers/", driver
, NULL
);
994 if (stat(path
, &statbuf
) == 0)
997 strscpyl(path
, sizeof(path
), "/sys/bus/", subsys
, "/drivers/", driver
, NULL
);
998 if (stat(path
, &statbuf
) == 0)
1006 strscpyl(path
, sizeof(path
), "/sys/subsystem/", subsystem
, "/devices/", sysname
, NULL
);
1007 if (stat(path
, &statbuf
) == 0)
1010 strscpyl(path
, sizeof(path
), "/sys/bus/", subsystem
, "/devices/", sysname
, NULL
);
1011 if (stat(path
, &statbuf
) == 0)
1014 strscpyl(path
, sizeof(path
), "/sys/class/", subsystem
, "/", sysname
, NULL
);
1015 if (stat(path
, &statbuf
) == 0)
1020 return udev_device_new_from_syspath(udev
, path
);
1024 * udev_device_new_from_environment
1025 * @udev: udev library context
1027 * Create new udev device, and fill in information from the
1028 * current process environment. This only works reliable if
1029 * the process is called from a udev rule. It is usually used
1030 * for tools executed from IMPORT= rules.
1032 * The initial refcount is 1, and needs to be decremented to
1033 * release the resources of the udev device.
1035 * Returns: a new udev device, or #NULL, if it does not exist
1037 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
)
1040 struct udev_device
*udev_device
;
1042 udev_device
= udev_device_new(udev
);
1043 if (udev_device
== NULL
)
1045 udev_device_set_info_loaded(udev_device
);
1047 for (i
= 0; environ
[i
] != NULL
; i
++)
1048 udev_device_add_property_from_string_parse(udev_device
, environ
[i
]);
1050 if (udev_device_add_property_from_string_parse_finish(udev_device
) < 0) {
1051 log_debug("missing values, invalid device");
1052 udev_device_unref(udev_device
);
1059 static struct udev_device
*device_new_from_parent(struct udev_device
*udev_device
)
1061 struct udev_device
*udev_device_parent
= NULL
;
1062 char path
[UTIL_PATH_SIZE
];
1065 strscpy(path
, sizeof(path
), udev_device
->syspath
);
1066 subdir
= path
+ strlen("/sys/");
1070 pos
= strrchr(subdir
, '/');
1071 if (pos
== NULL
|| pos
< &subdir
[2])
1074 udev_device_parent
= udev_device_new_from_syspath(udev_device
->udev
, path
);
1075 if (udev_device_parent
!= NULL
)
1076 return udev_device_parent
;
1084 * udev_device_get_parent:
1085 * @udev_device: the device to start searching from
1087 * Find the next parent device, and fill in information from the sys
1088 * device and the udev database entry.
1090 * Returned device is not referenced. It is attached to the child
1091 * device, and will be cleaned up when the child device is cleaned up.
1093 * It is not necessarily just the upper level directory, empty or not
1094 * recognized sys directories are ignored.
1096 * It can be called as many times as needed, without caring about
1099 * Returns: a new udev device, or #NULL, if it no parent exist.
1101 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
)
1103 if (udev_device
== NULL
) {
1107 if (!udev_device
->parent_set
) {
1108 udev_device
->parent_set
= true;
1109 udev_device
->parent_device
= device_new_from_parent(udev_device
);
1111 return udev_device
->parent_device
;
1115 * udev_device_get_parent_with_subsystem_devtype:
1116 * @udev_device: udev device to start searching from
1117 * @subsystem: the subsystem of the device
1118 * @devtype: the type (DEVTYPE) of the device
1120 * Find the next parent device, with a matching subsystem and devtype
1121 * value, and fill in information from the sys device and the udev
1124 * If devtype is #NULL, only subsystem is checked, and any devtype will
1127 * Returned device is not referenced. It is attached to the child
1128 * device, and will be cleaned up when the child device is cleaned up.
1130 * It can be called as many times as needed, without caring about
1133 * Returns: a new udev device, or #NULL if no matching parent exists.
1135 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
)
1137 struct udev_device
*parent
;
1139 if (subsystem
== NULL
) {
1144 parent
= udev_device_get_parent(udev_device
);
1145 while (parent
!= NULL
) {
1146 const char *parent_subsystem
;
1147 const char *parent_devtype
;
1149 parent_subsystem
= udev_device_get_subsystem(parent
);
1150 if (parent_subsystem
!= NULL
&& streq(parent_subsystem
, subsystem
)) {
1151 if (devtype
== NULL
)
1153 parent_devtype
= udev_device_get_devtype(parent
);
1154 if (parent_devtype
!= NULL
&& streq(parent_devtype
, devtype
))
1157 parent
= udev_device_get_parent(parent
);
1167 * udev_device_get_udev:
1168 * @udev_device: udev device
1170 * Retrieve the udev library context the device was created with.
1172 * Returns: the udev library context
1174 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
)
1176 if (udev_device
== NULL
)
1178 return udev_device
->udev
;
1183 * @udev_device: udev device
1185 * Take a reference of a udev device.
1187 * Returns: the passed udev device
1189 _public_
struct udev_device
*udev_device_ref(struct udev_device
*udev_device
)
1191 if (udev_device
== NULL
)
1193 udev_device
->refcount
++;
1198 * udev_device_unref:
1199 * @udev_device: udev device
1201 * Drop a reference of a udev device. If the refcount reaches zero,
1202 * the resources of the device will be released.
1206 _public_
struct udev_device
*udev_device_unref(struct udev_device
*udev_device
)
1208 if (udev_device
== NULL
)
1210 udev_device
->refcount
--;
1211 if (udev_device
->refcount
> 0)
1213 if (udev_device
->parent_device
!= NULL
)
1214 udev_device_unref(udev_device
->parent_device
);
1215 free(udev_device
->syspath
);
1216 free(udev_device
->sysname
);
1217 free(udev_device
->devnode
);
1218 free(udev_device
->subsystem
);
1219 free(udev_device
->devtype
);
1220 udev_list_cleanup(&udev_device
->devlinks_list
);
1221 udev_list_cleanup(&udev_device
->properties_list
);
1222 udev_list_cleanup(&udev_device
->sysattr_value_list
);
1223 udev_list_cleanup(&udev_device
->sysattr_list
);
1224 udev_list_cleanup(&udev_device
->tags_list
);
1225 free(udev_device
->action
);
1226 free(udev_device
->driver
);
1227 free(udev_device
->devpath_old
);
1228 free(udev_device
->id_filename
);
1229 free(udev_device
->envp
);
1230 free(udev_device
->monitor_buf
);
1236 * udev_device_get_devpath:
1237 * @udev_device: udev device
1239 * Retrieve the kernel devpath value of the udev device. The path
1240 * does not contain the sys mount point, and starts with a '/'.
1242 * Returns: the devpath of the udev device
1244 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
)
1246 if (udev_device
== NULL
)
1248 return udev_device
->devpath
;
1252 * udev_device_get_syspath:
1253 * @udev_device: udev device
1255 * Retrieve the sys path of the udev device. The path is an
1256 * absolute path and starts with the sys mount point.
1258 * Returns: the sys path of the udev device
1260 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
)
1262 if (udev_device
== NULL
)
1264 return udev_device
->syspath
;
1268 * udev_device_get_sysname:
1269 * @udev_device: udev device
1271 * Get the kernel device name in /sys.
1273 * Returns: the name string of the device device
1275 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
)
1277 if (udev_device
== NULL
)
1279 return udev_device
->sysname
;
1283 * udev_device_get_sysnum:
1284 * @udev_device: udev device
1286 * Get the instance number of the device.
1288 * Returns: the trailing number string of the device name
1290 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
)
1292 if (udev_device
== NULL
)
1294 return udev_device
->sysnum
;
1298 * udev_device_get_devnode:
1299 * @udev_device: udev device
1301 * Retrieve the device node file name belonging to the udev device.
1302 * The path is an absolute path, and starts with the device directory.
1304 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1306 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
)
1308 if (udev_device
== NULL
)
1310 if (udev_device
->devnode
!= NULL
)
1311 return udev_device
->devnode
;
1312 if (!udev_device
->info_loaded
)
1313 udev_device_read_uevent_file(udev_device
);
1314 return udev_device
->devnode
;
1318 * udev_device_get_devlinks_list_entry:
1319 * @udev_device: udev device
1321 * Retrieve the list of device links pointing to the device file of
1322 * the udev device. The next list entry can be retrieved with
1323 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
1324 * The devlink path can be retrieved from the list entry by
1325 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1326 * the device directory.
1328 * Returns: the first entry of the device node link list
1330 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
)
1332 if (udev_device
== NULL
)
1334 if (!udev_device
->info_loaded
)
1335 udev_device_read_db(udev_device
);
1336 return udev_list_get_entry(&udev_device
->devlinks_list
);
1339 void udev_device_cleanup_devlinks_list(struct udev_device
*udev_device
)
1341 udev_device
->devlinks_uptodate
= false;
1342 udev_list_cleanup(&udev_device
->devlinks_list
);
1346 * udev_device_get_properties_list_entry:
1347 * @udev_device: udev device
1349 * Retrieve the list of key/value device properties of the udev
1350 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
1351 * which returns #NULL if no more entries exist. The property name
1352 * can be retrieved from the list entry by udev_list_entry_get_name(),
1353 * the property value by udev_list_entry_get_value().
1355 * Returns: the first entry of the property list
1357 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
)
1359 if (udev_device
== NULL
)
1361 if (!udev_device
->info_loaded
) {
1362 udev_device_read_uevent_file(udev_device
);
1363 udev_device_read_db(udev_device
);
1365 if (!udev_device
->devlinks_uptodate
) {
1366 char symlinks
[UTIL_PATH_SIZE
];
1367 struct udev_list_entry
*list_entry
;
1369 udev_device
->devlinks_uptodate
= true;
1370 list_entry
= udev_device_get_devlinks_list_entry(udev_device
);
1371 if (list_entry
!= NULL
) {
1376 l
= strpcpyl(&s
, sizeof(symlinks
), udev_list_entry_get_name(list_entry
), NULL
);
1377 udev_list_entry_foreach(list_entry
, udev_list_entry_get_next(list_entry
))
1378 l
= strpcpyl(&s
, l
, " ", udev_list_entry_get_name(list_entry
), NULL
);
1379 udev_device_add_property_internal(udev_device
, "DEVLINKS", symlinks
);
1382 if (!udev_device
->tags_uptodate
) {
1383 udev_device
->tags_uptodate
= true;
1384 if (udev_device_get_tags_list_entry(udev_device
) != NULL
) {
1385 char tags
[UTIL_PATH_SIZE
];
1386 struct udev_list_entry
*list_entry
;
1391 l
= strpcpyl(&s
, sizeof(tags
), ":", NULL
);
1392 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(udev_device
))
1393 l
= strpcpyl(&s
, l
, udev_list_entry_get_name(list_entry
), ":", NULL
);
1394 udev_device_add_property_internal(udev_device
, "TAGS", tags
);
1397 return udev_list_get_entry(&udev_device
->properties_list
);
1401 * udev_device_get_action:
1402 * @udev_device: udev device
1404 * This is only valid if the device was received through a monitor. Devices read from
1405 * sys do not have an action string. Usual actions are: add, remove, change, online,
1408 * Returns: the kernel action value, or #NULL if there is no action value available.
1410 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
)
1412 if (udev_device
== NULL
)
1414 return udev_device
->action
;
1418 * udev_device_get_usec_since_initialized:
1419 * @udev_device: udev device
1421 * Return the number of microseconds passed since udev set up the
1422 * device for the first time.
1424 * This is only implemented for devices with need to store properties
1425 * in the udev database. All other devices return 0 here.
1427 * Returns: the number of microseconds since the device was first seen.
1429 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
)
1433 if (udev_device
== NULL
)
1435 if (!udev_device
->info_loaded
)
1436 udev_device_read_db(udev_device
);
1437 if (udev_device
->usec_initialized
== 0)
1439 now_ts
= now(CLOCK_MONOTONIC
);
1442 return now_ts
- udev_device
->usec_initialized
;
1445 usec_t
udev_device_get_usec_initialized(struct udev_device
*udev_device
)
1447 return udev_device
->usec_initialized
;
1451 * udev_device_get_sysattr_value:
1452 * @udev_device: udev device
1453 * @sysattr: attribute name
1455 * The retrieved value is cached in the device. Repeated calls will return the same
1456 * value and not open the attribute again.
1458 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1460 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
)
1462 struct udev_list_entry
*list_entry
;
1463 char path
[UTIL_PATH_SIZE
];
1465 struct stat statbuf
;
1468 const char *val
= NULL
;
1470 if (udev_device
== NULL
)
1472 if (sysattr
== NULL
)
1475 /* look for possibly already cached result */
1476 list_entry
= udev_list_get_entry(&udev_device
->sysattr_value_list
);
1477 list_entry
= udev_list_entry_get_by_name(list_entry
, sysattr
);
1478 if (list_entry
!= NULL
)
1479 return udev_list_entry_get_value(list_entry
);
1481 strscpyl(path
, sizeof(path
), udev_device_get_syspath(udev_device
), "/", sysattr
, NULL
);
1482 if (lstat(path
, &statbuf
) != 0) {
1483 udev_list_entry_add(&udev_device
->sysattr_value_list
, sysattr
, NULL
);
1487 if (S_ISLNK(statbuf
.st_mode
)) {
1489 * Some core links return only the last element of the target path,
1490 * these are just values, the paths should not be exposed.
1492 if (streq(sysattr
, "driver") ||
1493 streq(sysattr
, "subsystem") ||
1494 streq(sysattr
, "module")) {
1495 if (util_get_sys_core_link_value(udev_device
->udev
, sysattr
,
1496 udev_device
->syspath
, value
, sizeof(value
)) < 0)
1498 list_entry
= udev_list_entry_add(&udev_device
->sysattr_value_list
, sysattr
, value
);
1499 val
= udev_list_entry_get_value(list_entry
);
1506 /* skip directories */
1507 if (S_ISDIR(statbuf
.st_mode
))
1510 /* skip non-readable files */
1511 if ((statbuf
.st_mode
& S_IRUSR
) == 0)
1514 /* read attribute value */
1515 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1518 size
= read(fd
, value
, sizeof(value
));
1522 if (size
== sizeof(value
))
1525 /* got a valid value, store it in cache and return it */
1527 util_remove_trailing_chars(value
, '\n');
1528 list_entry
= udev_list_entry_add(&udev_device
->sysattr_value_list
, sysattr
, value
);
1529 val
= udev_list_entry_get_value(list_entry
);
1535 * udev_device_set_sysattr_value:
1536 * @udev_device: udev device
1537 * @sysattr: attribute name
1538 * @value: new value to be set
1540 * Update the contents of the sys attribute and the cached value of the device.
1542 * Returns: Negative error code on failure or 0 on success.
1544 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, char *value
)
1546 struct udev_device
*dev
;
1547 char path
[UTIL_PATH_SIZE
];
1548 struct stat statbuf
;
1550 ssize_t size
, value_len
;
1553 if (udev_device
== NULL
)
1556 if (sysattr
== NULL
)
1561 value_len
= strlen(value
);
1563 strscpyl(path
, sizeof(path
), udev_device_get_syspath(dev
), "/", sysattr
, NULL
);
1564 if (lstat(path
, &statbuf
) != 0) {
1565 udev_list_entry_add(&dev
->sysattr_value_list
, sysattr
, NULL
);
1570 if (S_ISLNK(statbuf
.st_mode
)) {
1575 /* skip directories */
1576 if (S_ISDIR(statbuf
.st_mode
)) {
1581 /* skip non-readable files */
1582 if ((statbuf
.st_mode
& S_IRUSR
) == 0) {
1587 /* Value is limited to 4k */
1588 if (value_len
> 4096) {
1592 util_remove_trailing_chars(value
, '\n');
1594 /* write attribute value */
1595 fd
= open(path
, O_WRONLY
|O_CLOEXEC
);
1600 size
= write(fd
, value
, value_len
);
1606 if (size
< value_len
) {
1611 /* wrote a valid value, store it in cache and return it */
1612 udev_list_entry_add(&dev
->sysattr_value_list
, sysattr
, value
);
1614 if (dev
!= udev_device
)
1615 udev_device_unref(dev
);
1619 static int udev_device_sysattr_list_read(struct udev_device
*udev_device
)
1621 struct dirent
*dent
;
1625 if (udev_device
== NULL
)
1627 if (udev_device
->sysattr_list_read
)
1630 dir
= opendir(udev_device_get_syspath(udev_device
));
1634 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
1635 char path
[UTIL_PATH_SIZE
];
1636 struct stat statbuf
;
1638 /* only handle symlinks and regular files */
1639 if (dent
->d_type
!= DT_LNK
&& dent
->d_type
!= DT_REG
)
1642 strscpyl(path
, sizeof(path
), udev_device_get_syspath(udev_device
), "/", dent
->d_name
, NULL
);
1643 if (lstat(path
, &statbuf
) != 0)
1645 if ((statbuf
.st_mode
& S_IRUSR
) == 0)
1648 udev_list_entry_add(&udev_device
->sysattr_list
, dent
->d_name
, NULL
);
1653 udev_device
->sysattr_list_read
= true;
1659 * udev_device_get_sysattr_list_entry:
1660 * @udev_device: udev device
1662 * Retrieve the list of available sysattrs, with value being empty;
1663 * This just return all available sysfs attributes for a particular
1664 * device without reading their values.
1666 * Returns: the first entry of the property list
1668 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
)
1670 if (!udev_device
->sysattr_list_read
) {
1672 ret
= udev_device_sysattr_list_read(udev_device
);
1677 return udev_list_get_entry(&udev_device
->sysattr_list
);
1680 static int udev_device_set_devnode(struct udev_device
*udev_device
, const char *devnode
)
1682 free(udev_device
->devnode
);
1683 if (devnode
[0] != '/') {
1684 if (asprintf(&udev_device
->devnode
, "/dev/%s", devnode
) < 0)
1685 udev_device
->devnode
= NULL
;
1687 udev_device
->devnode
= strdup(devnode
);
1689 if (udev_device
->devnode
== NULL
)
1691 udev_device_add_property_internal(udev_device
, "DEVNAME", udev_device
->devnode
);
1695 int udev_device_add_devlink(struct udev_device
*udev_device
, const char *devlink
)
1697 struct udev_list_entry
*list_entry
;
1699 udev_device
->devlinks_uptodate
= false;
1700 list_entry
= udev_list_entry_add(&udev_device
->devlinks_list
, devlink
, NULL
);
1701 if (list_entry
== NULL
)
1706 const char *udev_device_get_id_filename(struct udev_device
*udev_device
)
1708 if (udev_device
->id_filename
== NULL
) {
1709 if (udev_device_get_subsystem(udev_device
) == NULL
)
1712 if (major(udev_device_get_devnum(udev_device
)) > 0) {
1713 /* use dev_t -- b259:131072, c254:0 */
1714 if (asprintf(&udev_device
->id_filename
, "%c%u:%u",
1715 streq(udev_device_get_subsystem(udev_device
), "block") ? 'b' : 'c',
1716 major(udev_device_get_devnum(udev_device
)),
1717 minor(udev_device_get_devnum(udev_device
))) < 0)
1718 udev_device
->id_filename
= NULL
;
1719 } else if (udev_device_get_ifindex(udev_device
) > 0) {
1720 /* use netdev ifindex -- n3 */
1721 if (asprintf(&udev_device
->id_filename
, "n%i", udev_device_get_ifindex(udev_device
)) < 0)
1722 udev_device
->id_filename
= NULL
;
1725 * use $subsys:$syname -- pci:0000:00:1f.2
1726 * sysname() has '!' translated, get it from devpath
1728 const char *sysname
;
1729 sysname
= strrchr(udev_device
->devpath
, '/');
1730 if (sysname
== NULL
)
1732 sysname
= &sysname
[1];
1733 if (asprintf(&udev_device
->id_filename
, "+%s:%s", udev_device_get_subsystem(udev_device
), sysname
) < 0)
1734 udev_device
->id_filename
= NULL
;
1737 return udev_device
->id_filename
;
1741 * udev_device_get_is_initialized:
1742 * @udev_device: udev device
1744 * Check if udev has already handled the device and has set up
1745 * device node permissions and context, or has renamed a network
1748 * This is only implemented for devices with a device node
1749 * or network interfaces. All other devices return 1 here.
1751 * Returns: 1 if the device is set up. 0 otherwise.
1753 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
)
1755 if (!udev_device
->info_loaded
)
1756 udev_device_read_db(udev_device
);
1757 return udev_device
->is_initialized
;
1760 void udev_device_set_is_initialized(struct udev_device
*udev_device
)
1762 udev_device
->is_initialized
= true;
1765 static bool is_valid_tag(const char *tag
)
1767 return !strchr(tag
, ':') && !strchr(tag
, ' ');
1770 int udev_device_add_tag(struct udev_device
*udev_device
, const char *tag
)
1772 if (!is_valid_tag(tag
))
1774 udev_device
->tags_uptodate
= false;
1775 if (udev_list_entry_add(&udev_device
->tags_list
, tag
, NULL
) != NULL
)
1780 void udev_device_remove_tag(struct udev_device
*udev_device
, const char *tag
)
1782 struct udev_list_entry
*e
;
1784 if (!is_valid_tag(tag
))
1786 e
= udev_list_get_entry(&udev_device
->tags_list
);
1787 e
= udev_list_entry_get_by_name(e
, tag
);
1789 udev_device
->tags_uptodate
= false;
1790 udev_list_entry_delete(e
);
1794 void udev_device_cleanup_tags_list(struct udev_device
*udev_device
)
1796 udev_device
->tags_uptodate
= false;
1797 udev_list_cleanup(&udev_device
->tags_list
);
1801 * udev_device_get_tags_list_entry:
1802 * @udev_device: udev device
1804 * Retrieve the list of tags attached to the udev device. The next
1805 * list entry can be retrieved with udev_list_entry_get_next(),
1806 * which returns #NULL if no more entries exist. The tag string
1807 * can be retrieved from the list entry by udev_list_entry_get_name().
1809 * Returns: the first entry of the tag list
1811 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
)
1813 if (udev_device
== NULL
)
1815 if (!udev_device
->info_loaded
)
1816 udev_device_read_db(udev_device
);
1817 return udev_list_get_entry(&udev_device
->tags_list
);
1821 * udev_device_has_tag:
1822 * @udev_device: udev device
1825 * Check if a given device has a certain tag associated.
1827 * Returns: 1 if the tag is found. 0 otherwise.
1829 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
)
1831 struct udev_list_entry
*list_entry
;
1833 if (udev_device
== NULL
)
1835 if (!udev_device
->info_loaded
)
1836 udev_device_read_db(udev_device
);
1837 list_entry
= udev_device_get_tags_list_entry(udev_device
);
1838 if (udev_list_entry_get_by_name(list_entry
, tag
) != NULL
)
1843 #define ENVP_SIZE 128
1844 #define MONITOR_BUF_SIZE 4096
1845 static int update_envp_monitor_buf(struct udev_device
*udev_device
)
1847 struct udev_list_entry
*list_entry
;
1852 /* monitor buffer of property strings */
1853 free(udev_device
->monitor_buf
);
1854 udev_device
->monitor_buf_len
= 0;
1855 udev_device
->monitor_buf
= malloc(MONITOR_BUF_SIZE
);
1856 if (udev_device
->monitor_buf
== NULL
)
1859 /* envp array, strings will point into monitor buffer */
1860 if (udev_device
->envp
== NULL
)
1861 udev_device
->envp
= malloc(sizeof(char *) * ENVP_SIZE
);
1862 if (udev_device
->envp
== NULL
)
1866 s
= udev_device
->monitor_buf
;
1867 l
= MONITOR_BUF_SIZE
;
1868 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(udev_device
)) {
1871 key
= udev_list_entry_get_name(list_entry
);
1872 /* skip private variables */
1876 /* add string to envp array */
1877 udev_device
->envp
[i
++] = s
;
1878 if (i
+1 >= ENVP_SIZE
)
1881 /* add property string to monitor buffer */
1882 l
= strpcpyl(&s
, l
, key
, "=", udev_list_entry_get_value(list_entry
), NULL
);
1885 /* advance past the trailing '\0' that strpcpyl() guarantees */
1889 udev_device
->envp
[i
] = NULL
;
1890 udev_device
->monitor_buf_len
= s
- udev_device
->monitor_buf
;
1891 udev_device
->envp_uptodate
= true;
1895 char **udev_device_get_properties_envp(struct udev_device
*udev_device
)
1897 if (!udev_device
->envp_uptodate
)
1898 if (update_envp_monitor_buf(udev_device
) != 0)
1900 return udev_device
->envp
;
1903 ssize_t
udev_device_get_properties_monitor_buf(struct udev_device
*udev_device
, const char **buf
)
1905 if (!udev_device
->envp_uptodate
)
1906 if (update_envp_monitor_buf(udev_device
) != 0)
1908 *buf
= udev_device
->monitor_buf
;
1909 return udev_device
->monitor_buf_len
;
1912 int udev_device_get_devlink_priority(struct udev_device
*udev_device
)
1914 if (!udev_device
->info_loaded
)
1915 udev_device_read_db(udev_device
);
1916 return udev_device
->devlink_priority
;
1919 int udev_device_set_devlink_priority(struct udev_device
*udev_device
, int prio
)
1921 udev_device
->devlink_priority
= prio
;
1925 int udev_device_get_watch_handle(struct udev_device
*udev_device
)
1927 if (!udev_device
->info_loaded
)
1928 udev_device_read_db(udev_device
);
1929 return udev_device
->watch_handle
;
1932 int udev_device_set_watch_handle(struct udev_device
*udev_device
, int handle
)
1934 udev_device
->watch_handle
= handle
;
1938 bool udev_device_get_db_persist(struct udev_device
*udev_device
)
1940 return udev_device
->db_persist
;
1943 void udev_device_set_db_persist(struct udev_device
*udev_device
)
1945 udev_device
->db_persist
= true;
1948 int udev_device_rename(struct udev_device
*udev_device
, const char *name
)
1950 _cleanup_free_
char *dirname
= NULL
;
1951 const char *interface
;
1955 if (udev_device
== NULL
|| name
== NULL
)
1958 dirname
= dirname_malloc(udev_device
->syspath
);
1962 new_syspath
= strjoina(dirname
, "/", name
);
1964 r
= udev_device_set_syspath(udev_device
, new_syspath
);
1968 interface
= udev_device_get_property_value(udev_device
, "INTERFACE");
1970 /* like DEVPATH_OLD, INTERFACE_OLD is not saved to the db, but only stays around for the current event */
1971 udev_device_add_property_internal(udev_device
, "INTERFACE_OLD", interface
);
1972 udev_device_add_property_internal(udev_device
, "INTERFACE", name
);
1978 struct udev_device
*udev_device_shallow_clone(struct udev_device
*old_device
)
1980 struct udev_device
*device
;
1982 if (old_device
== NULL
)
1985 device
= udev_device_new(old_device
->udev
);
1992 udev_device_set_syspath(device
, udev_device_get_syspath(old_device
));
1993 udev_device_set_subsystem(device
, udev_device_get_subsystem(old_device
));
1994 udev_device_set_devnum(device
, udev_device_get_devnum(old_device
));
1999 struct udev_device
*udev_device_clone_with_db(struct udev_device
*old_device
)
2001 struct udev_device
*device
;
2003 device
= udev_device_shallow_clone(old_device
);
2007 udev_device_read_db(device
);
2008 udev_device_set_info_loaded(device
);
2013 struct udev_device
*udev_device_new_from_nulstr(struct udev
*udev
, char *nulstr
, ssize_t buflen
) {
2014 struct udev_device
*device
;
2017 if (nulstr
== NULL
|| buflen
<= 0) {
2023 device
= udev_device_new(udev
);
2030 udev_device_set_info_loaded(device
);
2032 while (bufpos
< buflen
) {
2036 key
= nulstr
+ bufpos
;
2037 keylen
= strlen(key
);
2041 bufpos
+= keylen
+ 1;
2042 udev_device_add_property_from_string_parse(device
, key
);
2045 if (udev_device_add_property_from_string_parse_finish(device
) < 0) {
2046 log_debug("missing values, invalid device");
2048 udev_device_unref(device
);
2058 struct udev_device
*udev_device_new_from_synthetic_event(struct udev
*udev
, const char *syspath
, const char *action
) {
2059 struct udev_device
*ret
;
2067 ret
= udev_device_new_from_syspath(udev
, syspath
);
2071 r
= udev_device_read_uevent_file(ret
);
2073 udev_device_unref(ret
);
2078 r
= udev_device_set_action(ret
, action
);
2080 udev_device_unref(ret
);
2088 int udev_device_copy_properties(struct udev_device
*dst
, struct udev_device
*src
) {
2089 struct udev_list_entry
*entry
;
2091 for ((entry
= udev_device_get_properties_list_entry(src
)); entry
; entry
= udev_list_entry_get_next(entry
)) {
2092 const char *key
, *value
;
2094 key
= udev_list_entry_get_name(entry
);
2095 value
= udev_list_entry_get_value(entry
);
2097 udev_device_add_property(dst
, key
, value
);