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_set_devnode(struct udev_device
*udev_device
, const char *devnode
);
42 * SECTION:libudev-device
43 * @short_description: kernel sys devices
45 * Representation of kernel sys devices. Devices are uniquely identified
46 * by their syspath, every device has exactly one path in the kernel sys
47 * filesystem. Devices usually belong to a kernel subsystem, and have
48 * a unique name inside that subsystem.
54 * Opaque object representing one kernel sys device.
58 struct udev_device
*parent_device
;
75 size_t monitor_buf_len
;
76 struct udev_list devlinks_list
;
77 struct udev_list properties_list
;
78 struct udev_list sysattr_value_list
;
79 struct udev_list sysattr_list
;
80 struct udev_list tags_list
;
81 unsigned long long int seqnum
;
82 usec_t usec_initialized
;
92 bool devlinks_uptodate
;
100 bool sysattr_list_read
;
105 * udev_device_get_seqnum:
106 * @udev_device: udev device
108 * This is only valid if the device was received through a monitor. Devices read from
109 * sys do not have a sequence number.
111 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
113 _public_
unsigned long long int udev_device_get_seqnum(struct udev_device
*udev_device
)
115 if (udev_device
== NULL
)
117 return udev_device
->seqnum
;
120 static int udev_device_set_seqnum(struct udev_device
*udev_device
, unsigned long long int seqnum
)
124 udev_device
->seqnum
= seqnum
;
125 snprintf(num
, sizeof(num
), "%llu", seqnum
);
126 udev_device_add_property(udev_device
, "SEQNUM", num
);
130 int udev_device_get_ifindex(struct udev_device
*udev_device
)
132 if (!udev_device
->info_loaded
)
133 udev_device_read_uevent_file(udev_device
);
134 return udev_device
->ifindex
;
137 static int udev_device_set_ifindex(struct udev_device
*udev_device
, int ifindex
)
141 udev_device
->ifindex
= ifindex
;
142 snprintf(num
, sizeof(num
), "%u", ifindex
);
143 udev_device_add_property(udev_device
, "IFINDEX", num
);
148 * udev_device_get_devnum:
149 * @udev_device: udev device
151 * Get the device major/minor number.
153 * Returns: the dev_t number.
155 _public_ dev_t
udev_device_get_devnum(struct udev_device
*udev_device
)
157 if (udev_device
== NULL
)
158 return makedev(0, 0);
159 if (!udev_device
->info_loaded
)
160 udev_device_read_uevent_file(udev_device
);
161 return udev_device
->devnum
;
164 static int udev_device_set_devnum(struct udev_device
*udev_device
, dev_t devnum
)
168 udev_device
->devnum
= devnum
;
170 snprintf(num
, sizeof(num
), "%u", major(devnum
));
171 udev_device_add_property(udev_device
, "MAJOR", num
);
172 snprintf(num
, sizeof(num
), "%u", minor(devnum
));
173 udev_device_add_property(udev_device
, "MINOR", num
);
177 const char *udev_device_get_devpath_old(struct udev_device
*udev_device
)
179 return udev_device
->devpath_old
;
182 static int udev_device_set_devpath_old(struct udev_device
*udev_device
, const char *devpath_old
)
186 free(udev_device
->devpath_old
);
187 udev_device
->devpath_old
= strdup(devpath_old
);
188 if (udev_device
->devpath_old
== NULL
)
190 udev_device_add_property(udev_device
, "DEVPATH_OLD", udev_device
->devpath_old
);
192 pos
= strrchr(udev_device
->devpath_old
, '/');
199 * udev_device_get_driver:
200 * @udev_device: udev device
202 * Get the kernel driver name.
204 * Returns: the driver name string, or #NULL if there is no driver attached.
206 _public_
const char *udev_device_get_driver(struct udev_device
*udev_device
)
208 char driver
[UTIL_NAME_SIZE
];
210 if (udev_device
== NULL
)
212 if (!udev_device
->driver_set
) {
213 udev_device
->driver_set
= true;
214 if (util_get_sys_core_link_value(udev_device
->udev
, "driver", udev_device
->syspath
, driver
, sizeof(driver
)) > 0)
215 udev_device
->driver
= strdup(driver
);
217 return udev_device
->driver
;
220 static int udev_device_set_driver(struct udev_device
*udev_device
, const char *driver
)
222 free(udev_device
->driver
);
223 udev_device
->driver
= strdup(driver
);
224 if (udev_device
->driver
== NULL
)
226 udev_device
->driver_set
= true;
227 udev_device_add_property(udev_device
, "DRIVER", udev_device
->driver
);
232 * udev_device_get_devtype:
233 * @udev_device: udev device
235 * Retrieve the devtype string of the udev device.
237 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
239 _public_
const char *udev_device_get_devtype(struct udev_device
*udev_device
)
241 if (udev_device
== NULL
)
243 if (!udev_device
->devtype_set
) {
244 udev_device
->devtype_set
= true;
245 udev_device_read_uevent_file(udev_device
);
247 return udev_device
->devtype
;
250 static int udev_device_set_devtype(struct udev_device
*udev_device
, const char *devtype
)
252 free(udev_device
->devtype
);
253 udev_device
->devtype
= strdup(devtype
);
254 if (udev_device
->devtype
== NULL
)
256 udev_device
->devtype_set
= true;
257 udev_device_add_property(udev_device
, "DEVTYPE", udev_device
->devtype
);
261 int udev_device_set_subsystem(struct udev_device
*udev_device
, const char *subsystem
)
263 free(udev_device
->subsystem
);
264 udev_device
->subsystem
= strdup(subsystem
);
265 if (udev_device
->subsystem
== NULL
)
267 udev_device
->subsystem_set
= true;
268 udev_device_add_property(udev_device
, "SUBSYSTEM", udev_device
->subsystem
);
273 * udev_device_get_subsystem:
274 * @udev_device: udev device
276 * Retrieve the subsystem string of the udev device. The string does not
279 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
281 _public_
const char *udev_device_get_subsystem(struct udev_device
*udev_device
)
283 char subsystem
[UTIL_NAME_SIZE
];
285 if (udev_device
== NULL
)
287 if (!udev_device
->subsystem_set
) {
288 udev_device
->subsystem_set
= true;
289 /* read "subsystem" link */
290 if (util_get_sys_core_link_value(udev_device
->udev
, "subsystem", udev_device
->syspath
, subsystem
, sizeof(subsystem
)) > 0) {
291 udev_device_set_subsystem(udev_device
, subsystem
);
292 return udev_device
->subsystem
;
295 if (startswith(udev_device
->devpath
, "/module/")) {
296 udev_device_set_subsystem(udev_device
, "module");
297 return udev_device
->subsystem
;
299 if (strstr(udev_device
->devpath
, "/drivers/") != NULL
) {
300 udev_device_set_subsystem(udev_device
, "drivers");
301 return udev_device
->subsystem
;
303 if (startswith(udev_device
->devpath
, "/subsystem/") ||
304 startswith(udev_device
->devpath
, "/class/") ||
305 startswith(udev_device
->devpath
, "/bus/")) {
306 udev_device_set_subsystem(udev_device
, "subsystem");
307 return udev_device
->subsystem
;
310 return udev_device
->subsystem
;
313 mode_t
udev_device_get_devnode_mode(struct udev_device
*udev_device
)
315 if (!udev_device
->info_loaded
)
316 udev_device_read_uevent_file(udev_device
);
317 return udev_device
->devnode_mode
;
320 static int udev_device_set_devnode_mode(struct udev_device
*udev_device
, mode_t mode
)
324 udev_device
->devnode_mode
= mode
;
325 snprintf(num
, sizeof(num
), "%#o", mode
);
326 udev_device_add_property(udev_device
, "DEVMODE", num
);
330 uid_t
udev_device_get_devnode_uid(struct udev_device
*udev_device
)
332 if (!udev_device
->info_loaded
)
333 udev_device_read_uevent_file(udev_device
);
334 return udev_device
->devnode_uid
;
337 static int udev_device_set_devnode_uid(struct udev_device
*udev_device
, uid_t uid
)
341 udev_device
->devnode_uid
= uid
;
342 snprintf(num
, sizeof(num
), "%u", uid
);
343 udev_device_add_property(udev_device
, "DEVUID", num
);
347 gid_t
udev_device_get_devnode_gid(struct udev_device
*udev_device
)
349 if (!udev_device
->info_loaded
)
350 udev_device_read_uevent_file(udev_device
);
351 return udev_device
->devnode_gid
;
354 static int udev_device_set_devnode_gid(struct udev_device
*udev_device
, gid_t gid
)
358 udev_device
->devnode_gid
= gid
;
359 snprintf(num
, sizeof(num
), "%u", gid
);
360 udev_device_add_property(udev_device
, "DEVGID", num
);
364 struct udev_list_entry
*udev_device_add_property(struct udev_device
*udev_device
, const char *key
, const char *value
)
366 udev_device
->envp_uptodate
= false;
368 struct udev_list_entry
*list_entry
;
370 list_entry
= udev_device_get_properties_list_entry(udev_device
);
371 list_entry
= udev_list_entry_get_by_name(list_entry
, key
);
372 if (list_entry
!= NULL
)
373 udev_list_entry_delete(list_entry
);
376 return udev_list_entry_add(&udev_device
->properties_list
, key
, value
);
379 static struct udev_list_entry
*udev_device_add_property_from_string(struct udev_device
*udev_device
, const char *property
)
381 char name
[UTIL_LINE_SIZE
];
384 strscpy(name
, sizeof(name
), property
);
385 val
= strchr(name
, '=');
392 return udev_device_add_property(udev_device
, name
, val
);
396 * parse property string, and if needed, update internal values accordingly
398 * udev_device_add_property_from_string_parse_finish() needs to be
399 * called after adding properties, and its return value checked
401 * udev_device_set_info_loaded() needs to be set, to avoid trying
402 * to use a device without a DEVPATH set
404 void udev_device_add_property_from_string_parse(struct udev_device
*udev_device
, const char *property
)
406 if (startswith(property
, "DEVPATH=")) {
407 char path
[UTIL_PATH_SIZE
];
409 strscpyl(path
, sizeof(path
), "/sys", &property
[8], NULL
);
410 udev_device_set_syspath(udev_device
, path
);
411 } else if (startswith(property
, "SUBSYSTEM=")) {
412 udev_device_set_subsystem(udev_device
, &property
[10]);
413 } else if (startswith(property
, "DEVTYPE=")) {
414 udev_device_set_devtype(udev_device
, &property
[8]);
415 } else if (startswith(property
, "DEVNAME=")) {
416 udev_device_set_devnode(udev_device
, &property
[8]);
417 } else if (startswith(property
, "DEVLINKS=")) {
418 char devlinks
[UTIL_PATH_SIZE
];
422 strscpy(devlinks
, sizeof(devlinks
), &property
[9]);
424 next
= strchr(slink
, ' ');
425 while (next
!= NULL
) {
427 udev_device_add_devlink(udev_device
, slink
);
429 next
= strchr(slink
, ' ');
431 if (slink
[0] != '\0')
432 udev_device_add_devlink(udev_device
, slink
);
433 } else if (startswith(property
, "TAGS=")) {
434 char tags
[UTIL_PATH_SIZE
];
437 strscpy(tags
, sizeof(tags
), &property
[5]);
438 next
= strchr(tags
, ':');
441 while (next
[0] != '\0') {
445 next
= strchr(tag
, ':');
450 udev_device_add_tag(udev_device
, tag
);
453 } else if (startswith(property
, "USEC_INITIALIZED=")) {
454 udev_device_set_usec_initialized(udev_device
, strtoull(&property
[19], NULL
, 10));
455 } else if (startswith(property
, "DRIVER=")) {
456 udev_device_set_driver(udev_device
, &property
[7]);
457 } else if (startswith(property
, "ACTION=")) {
458 udev_device_set_action(udev_device
, &property
[7]);
459 } else if (startswith(property
, "MAJOR=")) {
460 udev_device
->maj
= strtoull(&property
[6], NULL
, 10);
461 } else if (startswith(property
, "MINOR=")) {
462 udev_device
->min
= strtoull(&property
[6], NULL
, 10);
463 } else if (startswith(property
, "DEVPATH_OLD=")) {
464 udev_device_set_devpath_old(udev_device
, &property
[12]);
465 } else if (startswith(property
, "SEQNUM=")) {
466 udev_device_set_seqnum(udev_device
, strtoull(&property
[7], NULL
, 10));
467 } else if (startswith(property
, "IFINDEX=")) {
468 udev_device_set_ifindex(udev_device
, strtoull(&property
[8], NULL
, 10));
469 } else if (startswith(property
, "DEVMODE=")) {
470 udev_device_set_devnode_mode(udev_device
, strtoul(&property
[8], NULL
, 8));
471 } else if (startswith(property
, "DEVUID=")) {
472 udev_device_set_devnode_uid(udev_device
, strtoul(&property
[7], NULL
, 10));
473 } else if (startswith(property
, "DEVGID=")) {
474 udev_device_set_devnode_gid(udev_device
, strtoul(&property
[7], NULL
, 10));
476 udev_device_add_property_from_string(udev_device
, property
);
480 int udev_device_add_property_from_string_parse_finish(struct udev_device
*udev_device
)
482 if (udev_device
->maj
> 0)
483 udev_device_set_devnum(udev_device
, makedev(udev_device
->maj
, udev_device
->min
));
484 udev_device
->maj
= 0;
485 udev_device
->min
= 0;
487 if (udev_device
->devpath
== NULL
|| udev_device
->subsystem
== NULL
)
493 * udev_device_get_property_value:
494 * @udev_device: udev device
495 * @key: property name
497 * Get the value of a given property.
499 * Returns: the property string, or #NULL if there is no such property.
501 _public_
const char *udev_device_get_property_value(struct udev_device
*udev_device
, const char *key
)
503 struct udev_list_entry
*list_entry
;
505 if (udev_device
== NULL
)
510 list_entry
= udev_device_get_properties_list_entry(udev_device
);
511 list_entry
= udev_list_entry_get_by_name(list_entry
, key
);
512 return udev_list_entry_get_value(list_entry
);
515 int udev_device_read_db(struct udev_device
*udev_device
, const char *dbfile
)
517 char filename
[UTIL_PATH_SIZE
];
518 char line
[UTIL_LINE_SIZE
];
521 /* providing a database file will always force-load it */
522 if (dbfile
== NULL
) {
525 if (udev_device
->db_loaded
)
527 udev_device
->db_loaded
= true;
529 id
= udev_device_get_id_filename(udev_device
);
532 strscpyl(filename
, sizeof(filename
), "/run/udev/data/", id
, NULL
);
536 f
= fopen(dbfile
, "re");
538 udev_dbg(udev_device
->udev
, "no db file to read %s: %m\n", dbfile
);
541 udev_device
->is_initialized
= true;
543 while (fgets(line
, sizeof(line
), f
)) {
546 struct udev_list_entry
*entry
;
555 strscpyl(filename
, sizeof(filename
), "/dev/", val
, NULL
);
556 udev_device_add_devlink(udev_device
, filename
);
559 udev_device_set_devlink_priority(udev_device
, atoi(val
));
562 entry
= udev_device_add_property_from_string(udev_device
, val
);
563 udev_list_entry_set_num(entry
, true);
566 udev_device_add_tag(udev_device
, val
);
569 udev_device_set_watch_handle(udev_device
, atoi(val
));
572 udev_device_set_usec_initialized(udev_device
, strtoull(val
, NULL
, 10));
578 udev_dbg(udev_device
->udev
, "device %p filled with db file data\n", udev_device
);
582 int udev_device_read_uevent_file(struct udev_device
*udev_device
)
584 char filename
[UTIL_PATH_SIZE
];
586 char line
[UTIL_LINE_SIZE
];
590 if (udev_device
->uevent_loaded
)
593 strscpyl(filename
, sizeof(filename
), udev_device
->syspath
, "/uevent", NULL
);
594 f
= fopen(filename
, "re");
597 udev_device
->uevent_loaded
= true;
599 while (fgets(line
, sizeof(line
), f
)) {
602 pos
= strchr(line
, '\n');
607 if (startswith(line
, "DEVTYPE=")) {
608 udev_device_set_devtype(udev_device
, &line
[8]);
611 if (startswith(line
, "IFINDEX=")) {
612 udev_device_set_ifindex(udev_device
, strtoull(&line
[8], NULL
, 10));
615 if (startswith(line
, "DEVNAME=")) {
616 udev_device_set_devnode(udev_device
, &line
[8]);
620 if (startswith(line
, "MAJOR="))
621 maj
= strtoull(&line
[6], NULL
, 10);
622 else if (startswith(line
, "MINOR="))
623 min
= strtoull(&line
[6], NULL
, 10);
624 else if (startswith(line
, "DEVMODE="))
625 udev_device
->devnode_mode
= strtoul(&line
[8], NULL
, 8);
627 udev_device_add_property_from_string(udev_device
, line
);
630 udev_device
->devnum
= makedev(maj
, min
);
635 void udev_device_set_info_loaded(struct udev_device
*device
)
637 device
->info_loaded
= true;
640 struct udev_device
*udev_device_new(struct udev
*udev
)
642 struct udev_device
*udev_device
;
643 struct udev_list_entry
*list_entry
;
648 udev_device
= calloc(1, sizeof(struct udev_device
));
649 if (udev_device
== NULL
)
651 udev_device
->refcount
= 1;
652 udev_device
->udev
= udev
;
653 udev_list_init(udev
, &udev_device
->devlinks_list
, true);
654 udev_list_init(udev
, &udev_device
->properties_list
, true);
655 udev_list_init(udev
, &udev_device
->sysattr_value_list
, true);
656 udev_list_init(udev
, &udev_device
->sysattr_list
, false);
657 udev_list_init(udev
, &udev_device
->tags_list
, true);
658 udev_device
->watch_handle
= -1;
659 /* copy global properties */
660 udev_list_entry_foreach(list_entry
, udev_get_properties_list_entry(udev
))
661 udev_device_add_property(udev_device
,
662 udev_list_entry_get_name(list_entry
),
663 udev_list_entry_get_value(list_entry
));
668 * udev_device_new_from_syspath:
669 * @udev: udev library context
670 * @syspath: sys device path including sys directory
672 * Create new udev device, and fill in information from the sys
673 * device and the udev database entry. The syspath is the absolute
674 * path to the device, including the sys mount point.
676 * The initial refcount is 1, and needs to be decremented to
677 * release the resources of the udev device.
679 * Returns: a new udev device, or #NULL, if it does not exist
681 _public_
struct udev_device
*udev_device_new_from_syspath(struct udev
*udev
, const char *syspath
)
684 char path
[UTIL_PATH_SIZE
];
687 struct udev_device
*udev_device
;
694 /* path starts in sys */
695 if (!startswith(syspath
, "/sys")) {
696 udev_dbg(udev
, "not in sys :%s\n", syspath
);
700 /* path is not a root directory */
701 subdir
= syspath
+ strlen("/sys");
702 pos
= strrchr(subdir
, '/');
703 if (pos
== NULL
|| pos
[1] == '\0' || pos
< &subdir
[2])
706 /* resolve possible symlink to real path */
707 strscpy(path
, sizeof(path
), syspath
);
708 util_resolve_sys_link(udev
, path
, sizeof(path
));
710 if (startswith(path
+ strlen("/sys"), "/devices/")) {
711 char file
[UTIL_PATH_SIZE
];
713 /* all "devices" require a "uevent" file */
714 strscpyl(file
, sizeof(file
), path
, "/uevent", NULL
);
715 if (stat(file
, &statbuf
) != 0)
718 /* everything else just needs to be a directory */
719 if (stat(path
, &statbuf
) != 0 || !S_ISDIR(statbuf
.st_mode
))
723 udev_device
= udev_device_new(udev
);
724 if (udev_device
== NULL
)
727 udev_device_set_syspath(udev_device
, path
);
728 udev_dbg(udev
, "device %p has devpath '%s'\n", udev_device
, udev_device_get_devpath(udev_device
));
734 * udev_device_new_from_devnum:
735 * @udev: udev library context
736 * @type: char or block device
737 * @devnum: device major/minor number
739 * Create new udev device, and fill in information from the sys
740 * device and the udev database entry. The device is looked-up
741 * by its major/minor number and type. Character and block device
742 * numbers are not unique across the two types.
744 * The initial refcount is 1, and needs to be decremented to
745 * release the resources of the udev device.
747 * Returns: a new udev device, or #NULL, if it does not exist
749 _public_
struct udev_device
*udev_device_new_from_devnum(struct udev
*udev
, char type
, dev_t devnum
)
751 char path
[UTIL_PATH_SIZE
];
752 const char *type_str
;
756 else if (type
== 'c')
761 /* use /sys/dev/{block,char}/<maj>:<min> link */
762 snprintf(path
, sizeof(path
), "/sys/dev/%s/%u:%u",
763 type_str
, major(devnum
), minor(devnum
));
764 return udev_device_new_from_syspath(udev
, path
);
768 * udev_device_new_from_device_id:
769 * @udev: udev library context
770 * @id: text string identifying a kernel device
772 * Create new udev device, and fill in information from the sys
773 * device and the udev database entry. The device is looked-up
774 * by a special string:
775 * b8:2 - block device major:minor
776 * c128:1 - char device major:minor
777 * n3 - network device ifindex
778 * +sound:card29 - kernel driver core subsystem:device name
780 * The initial refcount is 1, and needs to be decremented to
781 * release the resources of the udev device.
783 * Returns: a new udev device, or #NULL, if it does not exist
785 _public_
struct udev_device
*udev_device_new_from_device_id(struct udev
*udev
, const char *id
)
789 char subsys
[UTIL_PATH_SIZE
];
795 if (sscanf(id
, "%c%i:%i", &type
, &maj
, &min
) != 3)
797 return udev_device_new_from_devnum(udev
, type
, makedev(maj
, min
));
801 struct udev_device
*dev
;
804 ifindex
= strtoul(&id
[1], NULL
, 10);
808 sk
= socket(PF_INET
, SOCK_DGRAM
, 0);
811 memset(&ifr
, 0x00, sizeof(struct ifreq
));
812 ifr
.ifr_ifindex
= ifindex
;
813 if (ioctl(sk
, SIOCGIFNAME
, &ifr
) != 0) {
819 dev
= udev_device_new_from_subsystem_sysname(udev
, "net", ifr
.ifr_name
);
822 if (udev_device_get_ifindex(dev
) == ifindex
)
824 udev_device_unref(dev
);
828 strscpy(subsys
, sizeof(subsys
), &id
[1]);
829 sysname
= strchr(subsys
, ':');
833 sysname
= &sysname
[1];
834 return udev_device_new_from_subsystem_sysname(udev
, subsys
, sysname
);
841 * udev_device_new_from_subsystem_sysname:
842 * @udev: udev library context
843 * @subsystem: the subsystem of the device
844 * @sysname: the name of the device
846 * Create new udev device, and fill in information from the sys device
847 * and the udev database entry. The device is looked up by the subsystem
848 * and name string of the device, like "mem" / "zero", or "block" / "sda".
850 * The initial refcount is 1, and needs to be decremented to
851 * release the resources of the udev device.
853 * Returns: a new udev device, or #NULL, if it does not exist
855 _public_
struct udev_device
*udev_device_new_from_subsystem_sysname(struct udev
*udev
, const char *subsystem
, const char *sysname
)
857 char path
[UTIL_PATH_SIZE
];
860 if (streq(subsystem
, "subsystem")) {
861 strscpyl(path
, sizeof(path
), "/sys/subsystem/", sysname
, NULL
);
862 if (stat(path
, &statbuf
) == 0)
865 strscpyl(path
, sizeof(path
), "/sys/bus/", sysname
, NULL
);
866 if (stat(path
, &statbuf
) == 0)
869 strscpyl(path
, sizeof(path
), "/sys/class/", sysname
, NULL
);
870 if (stat(path
, &statbuf
) == 0)
875 if (streq(subsystem
, "module")) {
876 strscpyl(path
, sizeof(path
), "/sys/module/", sysname
, NULL
);
877 if (stat(path
, &statbuf
) == 0)
882 if (streq(subsystem
, "drivers")) {
883 char subsys
[UTIL_NAME_SIZE
];
886 strscpy(subsys
, sizeof(subsys
), sysname
);
887 driver
= strchr(subsys
, ':');
888 if (driver
!= NULL
) {
892 strscpyl(path
, sizeof(path
), "/sys/subsystem/", subsys
, "/drivers/", driver
, NULL
);
893 if (stat(path
, &statbuf
) == 0)
896 strscpyl(path
, sizeof(path
), "/sys/bus/", subsys
, "/drivers/", driver
, NULL
);
897 if (stat(path
, &statbuf
) == 0)
903 strscpyl(path
, sizeof(path
), "/sys/subsystem/", subsystem
, "/devices/", sysname
, NULL
);
904 if (stat(path
, &statbuf
) == 0)
907 strscpyl(path
, sizeof(path
), "/sys/bus/", subsystem
, "/devices/", sysname
, NULL
);
908 if (stat(path
, &statbuf
) == 0)
911 strscpyl(path
, sizeof(path
), "/sys/class/", subsystem
, "/", sysname
, NULL
);
912 if (stat(path
, &statbuf
) == 0)
917 return udev_device_new_from_syspath(udev
, path
);
921 * udev_device_new_from_environment
922 * @udev: udev library context
924 * Create new udev device, and fill in information from the
925 * current process environment. This only works reliable if
926 * the process is called from a udev rule. It is usually used
927 * for tools executed from IMPORT= rules.
929 * The initial refcount is 1, and needs to be decremented to
930 * release the resources of the udev device.
932 * Returns: a new udev device, or #NULL, if it does not exist
934 _public_
struct udev_device
*udev_device_new_from_environment(struct udev
*udev
)
937 struct udev_device
*udev_device
;
939 udev_device
= udev_device_new(udev
);
940 if (udev_device
== NULL
)
942 udev_device_set_info_loaded(udev_device
);
944 for (i
= 0; environ
[i
] != NULL
; i
++)
945 udev_device_add_property_from_string_parse(udev_device
, environ
[i
]);
947 if (udev_device_add_property_from_string_parse_finish(udev_device
) < 0) {
948 udev_dbg(udev
, "missing values, invalid device\n");
949 udev_device_unref(udev_device
);
956 static struct udev_device
*device_new_from_parent(struct udev_device
*udev_device
)
958 struct udev_device
*udev_device_parent
= NULL
;
959 char path
[UTIL_PATH_SIZE
];
962 strscpy(path
, sizeof(path
), udev_device
->syspath
);
963 subdir
= path
+ strlen("/sys/");
967 pos
= strrchr(subdir
, '/');
968 if (pos
== NULL
|| pos
< &subdir
[2])
971 udev_device_parent
= udev_device_new_from_syspath(udev_device
->udev
, path
);
972 if (udev_device_parent
!= NULL
)
973 return udev_device_parent
;
979 * udev_device_get_parent:
980 * @udev_device: the device to start searching from
982 * Find the next parent device, and fill in information from the sys
983 * device and the udev database entry.
985 * The returned the device is not referenced. It is attached to the
986 * child device, and will be cleaned up when the child device
989 * It is not necessarily just the upper level directory, empty or not
990 * recognized sys directories are ignored.
992 * It can be called as many times as needed, without caring about
995 * Returns: a new udev device, or #NULL, if it no parent exist.
997 _public_
struct udev_device
*udev_device_get_parent(struct udev_device
*udev_device
)
999 if (udev_device
== NULL
)
1001 if (!udev_device
->parent_set
) {
1002 udev_device
->parent_set
= true;
1003 udev_device
->parent_device
= device_new_from_parent(udev_device
);
1005 return udev_device
->parent_device
;
1009 * udev_device_get_parent_with_subsystem_devtype:
1010 * @udev_device: udev device to start searching from
1011 * @subsystem: the subsystem of the device
1012 * @devtype: the type (DEVTYPE) of the device
1014 * Find the next parent device, with a matching subsystem and devtype
1015 * value, and fill in information from the sys device and the udev
1018 * If devtype is #NULL, only subsystem is checked, and any devtype will
1021 * The returned the device is not referenced. It is attached to the
1022 * child device, and will be cleaned up when the child device
1025 * It can be called as many times as needed, without caring about
1028 * Returns: a new udev device, or #NULL if no matching parent exists.
1030 _public_
struct udev_device
*udev_device_get_parent_with_subsystem_devtype(struct udev_device
*udev_device
, const char *subsystem
, const char *devtype
)
1032 struct udev_device
*parent
;
1034 if (subsystem
== NULL
)
1037 parent
= udev_device_get_parent(udev_device
);
1038 while (parent
!= NULL
) {
1039 const char *parent_subsystem
;
1040 const char *parent_devtype
;
1042 parent_subsystem
= udev_device_get_subsystem(parent
);
1043 if (parent_subsystem
!= NULL
&& streq(parent_subsystem
, subsystem
)) {
1044 if (devtype
== NULL
)
1046 parent_devtype
= udev_device_get_devtype(parent
);
1047 if (parent_devtype
!= NULL
&& streq(parent_devtype
, devtype
))
1050 parent
= udev_device_get_parent(parent
);
1056 * udev_device_get_udev:
1057 * @udev_device: udev device
1059 * Retrieve the udev library context the device was created with.
1061 * Returns: the udev library context
1063 _public_
struct udev
*udev_device_get_udev(struct udev_device
*udev_device
)
1065 if (udev_device
== NULL
)
1067 return udev_device
->udev
;
1072 * @udev_device: udev device
1074 * Take a reference of a udev device.
1076 * Returns: the passed udev device
1078 _public_
struct udev_device
*udev_device_ref(struct udev_device
*udev_device
)
1080 if (udev_device
== NULL
)
1082 udev_device
->refcount
++;
1087 * udev_device_unref:
1088 * @udev_device: udev device
1090 * Drop a reference of a udev device. If the refcount reaches zero,
1091 * the resources of the device will be released.
1093 * Returns: the passed udev device if it has still an active reference, or #NULL otherwise.
1095 _public_
struct udev_device
*udev_device_unref(struct udev_device
*udev_device
)
1097 if (udev_device
== NULL
)
1099 udev_device
->refcount
--;
1100 if (udev_device
->refcount
> 0)
1102 if (udev_device
->parent_device
!= NULL
)
1103 udev_device_unref(udev_device
->parent_device
);
1104 free(udev_device
->syspath
);
1105 free(udev_device
->sysname
);
1106 free(udev_device
->devnode
);
1107 free(udev_device
->subsystem
);
1108 free(udev_device
->devtype
);
1109 udev_list_cleanup(&udev_device
->devlinks_list
);
1110 udev_list_cleanup(&udev_device
->properties_list
);
1111 udev_list_cleanup(&udev_device
->sysattr_value_list
);
1112 udev_list_cleanup(&udev_device
->sysattr_list
);
1113 udev_list_cleanup(&udev_device
->tags_list
);
1114 free(udev_device
->action
);
1115 free(udev_device
->driver
);
1116 free(udev_device
->devpath_old
);
1117 free(udev_device
->id_filename
);
1118 free(udev_device
->envp
);
1119 free(udev_device
->monitor_buf
);
1125 * udev_device_get_devpath:
1126 * @udev_device: udev device
1128 * Retrieve the kernel devpath value of the udev device. The path
1129 * does not contain the sys mount point, and starts with a '/'.
1131 * Returns: the devpath of the udev device
1133 _public_
const char *udev_device_get_devpath(struct udev_device
*udev_device
)
1135 if (udev_device
== NULL
)
1137 return udev_device
->devpath
;
1141 * udev_device_get_syspath:
1142 * @udev_device: udev device
1144 * Retrieve the sys path of the udev device. The path is an
1145 * absolute path and starts with the sys mount point.
1147 * Returns: the sys path of the udev device
1149 _public_
const char *udev_device_get_syspath(struct udev_device
*udev_device
)
1151 if (udev_device
== NULL
)
1153 return udev_device
->syspath
;
1157 * udev_device_get_sysname:
1158 * @udev_device: udev device
1160 * Get the kernel device name in /sys.
1162 * Returns: the name string of the device device
1164 _public_
const char *udev_device_get_sysname(struct udev_device
*udev_device
)
1166 if (udev_device
== NULL
)
1168 return udev_device
->sysname
;
1172 * udev_device_get_sysnum:
1173 * @udev_device: udev device
1175 * Get the instance number of the device.
1177 * Returns: the trailing number string of the device name
1179 _public_
const char *udev_device_get_sysnum(struct udev_device
*udev_device
)
1181 if (udev_device
== NULL
)
1183 return udev_device
->sysnum
;
1187 * udev_device_get_devnode:
1188 * @udev_device: udev device
1190 * Retrieve the device node file name belonging to the udev device.
1191 * The path is an absolute path, and starts with the device directory.
1193 * Returns: the device node file name of the udev device, or #NULL if no device node exists
1195 _public_
const char *udev_device_get_devnode(struct udev_device
*udev_device
)
1197 if (udev_device
== NULL
)
1199 if (udev_device
->devnode
!= NULL
)
1200 return udev_device
->devnode
;
1201 if (!udev_device
->info_loaded
)
1202 udev_device_read_uevent_file(udev_device
);
1203 return udev_device
->devnode
;
1207 * udev_device_get_devlinks_list_entry:
1208 * @udev_device: udev device
1210 * Retrieve the list of device links pointing to the device file of
1211 * the udev device. The next list entry can be retrieved with
1212 * udev_list_entry_get_next(), which returns #NULL if no more entries exist.
1213 * The devlink path can be retrieved from the list entry by
1214 * udev_list_entry_get_name(). The path is an absolute path, and starts with
1215 * the device directory.
1217 * Returns: the first entry of the device node link list
1219 _public_
struct udev_list_entry
*udev_device_get_devlinks_list_entry(struct udev_device
*udev_device
)
1221 if (udev_device
== NULL
)
1223 if (!udev_device
->info_loaded
)
1224 udev_device_read_db(udev_device
, NULL
);
1225 return udev_list_get_entry(&udev_device
->devlinks_list
);
1228 void udev_device_cleanup_devlinks_list(struct udev_device
*udev_device
)
1230 udev_device
->devlinks_uptodate
= false;
1231 udev_list_cleanup(&udev_device
->devlinks_list
);
1235 * udev_device_get_properties_list_entry:
1236 * @udev_device: udev device
1238 * Retrieve the list of key/value device properties of the udev
1239 * device. The next list entry can be retrieved with udev_list_entry_get_next(),
1240 * which returns #NULL if no more entries exist. The property name
1241 * can be retrieved from the list entry by udev_list_entry_get_name(),
1242 * the property value by udev_list_entry_get_value().
1244 * Returns: the first entry of the property list
1246 _public_
struct udev_list_entry
*udev_device_get_properties_list_entry(struct udev_device
*udev_device
)
1248 if (udev_device
== NULL
)
1250 if (!udev_device
->info_loaded
) {
1251 udev_device_read_uevent_file(udev_device
);
1252 udev_device_read_db(udev_device
, NULL
);
1254 if (!udev_device
->devlinks_uptodate
) {
1255 char symlinks
[UTIL_PATH_SIZE
];
1256 struct udev_list_entry
*list_entry
;
1258 udev_device
->devlinks_uptodate
= true;
1259 list_entry
= udev_device_get_devlinks_list_entry(udev_device
);
1260 if (list_entry
!= NULL
) {
1265 l
= strpcpyl(&s
, sizeof(symlinks
), udev_list_entry_get_name(list_entry
), NULL
);
1266 udev_list_entry_foreach(list_entry
, udev_list_entry_get_next(list_entry
))
1267 l
= strpcpyl(&s
, l
, " ", udev_list_entry_get_name(list_entry
), NULL
);
1268 udev_device_add_property(udev_device
, "DEVLINKS", symlinks
);
1271 if (!udev_device
->tags_uptodate
) {
1272 udev_device
->tags_uptodate
= true;
1273 if (udev_device_get_tags_list_entry(udev_device
) != NULL
) {
1274 char tags
[UTIL_PATH_SIZE
];
1275 struct udev_list_entry
*list_entry
;
1280 l
= strpcpyl(&s
, sizeof(tags
), ":", NULL
);
1281 udev_list_entry_foreach(list_entry
, udev_device_get_tags_list_entry(udev_device
))
1282 l
= strpcpyl(&s
, l
, udev_list_entry_get_name(list_entry
), ":", NULL
);
1283 udev_device_add_property(udev_device
, "TAGS", tags
);
1286 return udev_list_get_entry(&udev_device
->properties_list
);
1290 * udev_device_get_action:
1291 * @udev_device: udev device
1293 * This is only valid if the device was received through a monitor. Devices read from
1294 * sys do not have an action string. Usual actions are: add, remove, change, online,
1297 * Returns: the kernel action value, or #NULL if there is no action value available.
1299 _public_
const char *udev_device_get_action(struct udev_device
*udev_device
)
1301 if (udev_device
== NULL
)
1303 return udev_device
->action
;
1307 * udev_device_get_usec_since_initialized:
1308 * @udev_device: udev device
1310 * Return the number of microseconds passed since udev set up the
1311 * device for the first time.
1313 * This is only implemented for devices with need to store properties
1314 * in the udev database. All other devices return 0 here.
1316 * Returns: the number of microseconds since the device was first seen.
1318 _public_
unsigned long long int udev_device_get_usec_since_initialized(struct udev_device
*udev_device
)
1322 if (udev_device
== NULL
)
1324 if (!udev_device
->info_loaded
)
1325 udev_device_read_db(udev_device
, NULL
);
1326 if (udev_device
->usec_initialized
== 0)
1328 now_ts
= now(CLOCK_MONOTONIC
);
1331 return now_ts
- udev_device
->usec_initialized
;
1334 usec_t
udev_device_get_usec_initialized(struct udev_device
*udev_device
)
1336 return udev_device
->usec_initialized
;
1339 void udev_device_set_usec_initialized(struct udev_device
*udev_device
, usec_t usec_initialized
)
1343 udev_device
->usec_initialized
= usec_initialized
;
1344 snprintf(num
, sizeof(num
), "%llu", (unsigned long long)usec_initialized
);
1345 udev_device_add_property(udev_device
, "USEC_INITIALIZED", num
);
1349 * udev_device_get_sysattr_value:
1350 * @udev_device: udev device
1351 * @sysattr: attribute name
1353 * The retrieved value is cached in the device. Repeated calls will return the same
1354 * value and not open the attribute again.
1356 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1358 _public_
const char *udev_device_get_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
)
1360 struct udev_list_entry
*list_entry
;
1361 char path
[UTIL_PATH_SIZE
];
1363 struct stat statbuf
;
1366 const char *val
= NULL
;
1368 if (udev_device
== NULL
)
1370 if (sysattr
== NULL
)
1373 /* look for possibly already cached result */
1374 list_entry
= udev_list_get_entry(&udev_device
->sysattr_value_list
);
1375 list_entry
= udev_list_entry_get_by_name(list_entry
, sysattr
);
1376 if (list_entry
!= NULL
)
1377 return udev_list_entry_get_value(list_entry
);
1379 strscpyl(path
, sizeof(path
), udev_device_get_syspath(udev_device
), "/", sysattr
, NULL
);
1380 if (lstat(path
, &statbuf
) != 0) {
1381 udev_list_entry_add(&udev_device
->sysattr_value_list
, sysattr
, NULL
);
1385 if (S_ISLNK(statbuf
.st_mode
)) {
1387 * Some core links return only the last element of the target path,
1388 * these are just values, the paths should not be exposed.
1390 if (streq(sysattr
, "driver") ||
1391 streq(sysattr
, "subsystem") ||
1392 streq(sysattr
, "module")) {
1393 if (util_get_sys_core_link_value(udev_device
->udev
, sysattr
,
1394 udev_device
->syspath
, value
, sizeof(value
)) < 0)
1396 list_entry
= udev_list_entry_add(&udev_device
->sysattr_value_list
, sysattr
, value
);
1397 val
= udev_list_entry_get_value(list_entry
);
1404 /* skip directories */
1405 if (S_ISDIR(statbuf
.st_mode
))
1408 /* skip non-readable files */
1409 if ((statbuf
.st_mode
& S_IRUSR
) == 0)
1412 /* read attribute value */
1413 fd
= open(path
, O_RDONLY
|O_CLOEXEC
);
1416 size
= read(fd
, value
, sizeof(value
));
1420 if (size
== sizeof(value
))
1423 /* got a valid value, store it in cache and return it */
1425 util_remove_trailing_chars(value
, '\n');
1426 list_entry
= udev_list_entry_add(&udev_device
->sysattr_value_list
, sysattr
, value
);
1427 val
= udev_list_entry_get_value(list_entry
);
1433 * udev_device_set_sysattr_value:
1434 * @udev_device: udev device
1435 * @sysattr: attribute name
1436 * @value: new value to be set
1438 * Update the contents of the sys attribute and the cached value of the device.
1440 * Returns: Negative error code on failure or 0 on success.
1442 _public_
int udev_device_set_sysattr_value(struct udev_device
*udev_device
, const char *sysattr
, char *value
)
1444 struct udev_device
*dev
;
1445 char path
[UTIL_PATH_SIZE
];
1446 struct stat statbuf
;
1448 ssize_t size
, value_len
;
1451 if (udev_device
== NULL
)
1454 if (sysattr
== NULL
)
1459 value_len
= strlen(value
);
1461 strscpyl(path
, sizeof(path
), udev_device_get_syspath(dev
), "/", sysattr
, NULL
);
1462 if (lstat(path
, &statbuf
) != 0) {
1463 udev_list_entry_add(&dev
->sysattr_value_list
, sysattr
, NULL
);
1468 if (S_ISLNK(statbuf
.st_mode
)) {
1473 /* skip directories */
1474 if (S_ISDIR(statbuf
.st_mode
)) {
1479 /* skip non-readable files */
1480 if ((statbuf
.st_mode
& S_IRUSR
) == 0) {
1485 /* Value is limited to 4k */
1486 if (value_len
> 4096) {
1490 util_remove_trailing_chars(value
, '\n');
1492 /* write attribute value */
1493 fd
= open(path
, O_WRONLY
|O_CLOEXEC
);
1498 size
= write(fd
, value
, value_len
);
1504 if (size
< value_len
) {
1509 /* wrote a valid value, store it in cache and return it */
1510 udev_list_entry_add(&dev
->sysattr_value_list
, sysattr
, value
);
1512 if (dev
!= udev_device
)
1513 udev_device_unref(dev
);
1517 static int udev_device_sysattr_list_read(struct udev_device
*udev_device
)
1519 struct dirent
*dent
;
1523 if (udev_device
== NULL
)
1525 if (udev_device
->sysattr_list_read
)
1528 dir
= opendir(udev_device_get_syspath(udev_device
));
1532 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
1533 char path
[UTIL_PATH_SIZE
];
1534 struct stat statbuf
;
1536 /* only handle symlinks and regular files */
1537 if (dent
->d_type
!= DT_LNK
&& dent
->d_type
!= DT_REG
)
1540 strscpyl(path
, sizeof(path
), udev_device_get_syspath(udev_device
), "/", dent
->d_name
, NULL
);
1541 if (lstat(path
, &statbuf
) != 0)
1543 if ((statbuf
.st_mode
& S_IRUSR
) == 0)
1546 udev_list_entry_add(&udev_device
->sysattr_list
, dent
->d_name
, NULL
);
1551 udev_device
->sysattr_list_read
= true;
1557 * udev_device_get_sysattr_list_entry:
1558 * @udev_device: udev device
1560 * Retrieve the list of available sysattrs, with value being empty;
1561 * This just return all available sysfs attributes for a particular
1562 * device without reading their values.
1564 * Returns: the first entry of the property list
1566 _public_
struct udev_list_entry
*udev_device_get_sysattr_list_entry(struct udev_device
*udev_device
)
1568 if (!udev_device
->sysattr_list_read
) {
1570 ret
= udev_device_sysattr_list_read(udev_device
);
1575 return udev_list_get_entry(&udev_device
->sysattr_list
);
1578 int udev_device_set_syspath(struct udev_device
*udev_device
, const char *syspath
)
1583 free(udev_device
->syspath
);
1584 udev_device
->syspath
= strdup(syspath
);
1585 if (udev_device
->syspath
== NULL
)
1587 udev_device
->devpath
= udev_device
->syspath
+ strlen("/sys");
1588 udev_device_add_property(udev_device
, "DEVPATH", udev_device
->devpath
);
1590 pos
= strrchr(udev_device
->syspath
, '/');
1593 udev_device
->sysname
= strdup(&pos
[1]);
1594 if (udev_device
->sysname
== NULL
)
1597 /* some devices have '!' in their name, change that to '/' */
1599 while (udev_device
->sysname
[len
] != '\0') {
1600 if (udev_device
->sysname
[len
] == '!')
1601 udev_device
->sysname
[len
] = '/';
1605 /* trailing number */
1606 while (len
> 0 && isdigit(udev_device
->sysname
[--len
]))
1607 udev_device
->sysnum
= &udev_device
->sysname
[len
];
1609 /* sysname is completely numeric */
1611 udev_device
->sysnum
= NULL
;
1616 static int udev_device_set_devnode(struct udev_device
*udev_device
, const char *devnode
)
1618 free(udev_device
->devnode
);
1619 if (devnode
[0] != '/') {
1620 if (asprintf(&udev_device
->devnode
, "/dev/%s", devnode
) < 0)
1621 udev_device
->devnode
= NULL
;
1623 udev_device
->devnode
= strdup(devnode
);
1625 if (udev_device
->devnode
== NULL
)
1627 udev_device_add_property(udev_device
, "DEVNAME", udev_device
->devnode
);
1631 int udev_device_add_devlink(struct udev_device
*udev_device
, const char *devlink
)
1633 struct udev_list_entry
*list_entry
;
1635 udev_device
->devlinks_uptodate
= false;
1636 list_entry
= udev_list_entry_add(&udev_device
->devlinks_list
, devlink
, NULL
);
1637 if (list_entry
== NULL
)
1642 const char *udev_device_get_id_filename(struct udev_device
*udev_device
)
1644 if (udev_device
->id_filename
== NULL
) {
1645 if (udev_device_get_subsystem(udev_device
) == NULL
)
1648 if (major(udev_device_get_devnum(udev_device
)) > 0) {
1649 /* use dev_t -- b259:131072, c254:0 */
1650 if (asprintf(&udev_device
->id_filename
, "%c%u:%u",
1651 streq(udev_device_get_subsystem(udev_device
), "block") ? 'b' : 'c',
1652 major(udev_device_get_devnum(udev_device
)),
1653 minor(udev_device_get_devnum(udev_device
))) < 0)
1654 udev_device
->id_filename
= NULL
;
1655 } else if (udev_device_get_ifindex(udev_device
) > 0) {
1656 /* use netdev ifindex -- n3 */
1657 if (asprintf(&udev_device
->id_filename
, "n%u", udev_device_get_ifindex(udev_device
)) < 0)
1658 udev_device
->id_filename
= NULL
;
1661 * use $subsys:$syname -- pci:0000:00:1f.2
1662 * sysname() has '!' translated, get it from devpath
1664 const char *sysname
;
1665 sysname
= strrchr(udev_device
->devpath
, '/');
1666 if (sysname
== NULL
)
1668 sysname
= &sysname
[1];
1669 if (asprintf(&udev_device
->id_filename
, "+%s:%s", udev_device_get_subsystem(udev_device
), sysname
) < 0)
1670 udev_device
->id_filename
= NULL
;
1673 return udev_device
->id_filename
;
1677 * udev_device_get_is_initialized:
1678 * @udev_device: udev device
1680 * Check if udev has already handled the device and has set up
1681 * device node permissions and context, or has renamed a network
1684 * This is only implemented for devices with a device node
1685 * or network interfaces. All other devices return 1 here.
1687 * Returns: 1 if the device is set up. 0 otherwise.
1689 _public_
int udev_device_get_is_initialized(struct udev_device
*udev_device
)
1691 if (!udev_device
->info_loaded
)
1692 udev_device_read_db(udev_device
, NULL
);
1693 return udev_device
->is_initialized
;
1696 void udev_device_set_is_initialized(struct udev_device
*udev_device
)
1698 udev_device
->is_initialized
= true;
1701 int udev_device_add_tag(struct udev_device
*udev_device
, const char *tag
)
1703 if (strchr(tag
, ':') != NULL
|| strchr(tag
, ' ') != NULL
)
1705 udev_device
->tags_uptodate
= false;
1706 if (udev_list_entry_add(&udev_device
->tags_list
, tag
, NULL
) != NULL
)
1711 void udev_device_cleanup_tags_list(struct udev_device
*udev_device
)
1713 udev_device
->tags_uptodate
= false;
1714 udev_list_cleanup(&udev_device
->tags_list
);
1718 * udev_device_get_tags_list_entry:
1719 * @udev_device: udev device
1721 * Retrieve the list of tags attached to the udev device. The next
1722 * list entry can be retrieved with udev_list_entry_get_next(),
1723 * which returns #NULL if no more entries exist. The tag string
1724 * can be retrieved from the list entry by udev_list_entry_get_name().
1726 * Returns: the first entry of the tag list
1728 _public_
struct udev_list_entry
*udev_device_get_tags_list_entry(struct udev_device
*udev_device
)
1730 if (udev_device
== NULL
)
1732 if (!udev_device
->info_loaded
)
1733 udev_device_read_db(udev_device
, NULL
);
1734 return udev_list_get_entry(&udev_device
->tags_list
);
1738 * udev_device_has_tag:
1739 * @udev_device: udev device
1742 * Check if a given device has a certain tag associated.
1744 * Returns: 1 if the tag is found. 0 otherwise.
1746 _public_
int udev_device_has_tag(struct udev_device
*udev_device
, const char *tag
)
1748 struct udev_list_entry
*list_entry
;
1750 if (udev_device
== NULL
)
1752 if (!udev_device
->info_loaded
)
1753 udev_device_read_db(udev_device
, NULL
);
1754 list_entry
= udev_device_get_tags_list_entry(udev_device
);
1755 if (udev_list_entry_get_by_name(list_entry
, tag
) != NULL
)
1760 #define ENVP_SIZE 128
1761 #define MONITOR_BUF_SIZE 4096
1762 static int update_envp_monitor_buf(struct udev_device
*udev_device
)
1764 struct udev_list_entry
*list_entry
;
1769 /* monitor buffer of property strings */
1770 free(udev_device
->monitor_buf
);
1771 udev_device
->monitor_buf_len
= 0;
1772 udev_device
->monitor_buf
= malloc(MONITOR_BUF_SIZE
);
1773 if (udev_device
->monitor_buf
== NULL
)
1776 /* envp array, strings will point into monitor buffer */
1777 if (udev_device
->envp
== NULL
)
1778 udev_device
->envp
= malloc(sizeof(char *) * ENVP_SIZE
);
1779 if (udev_device
->envp
== NULL
)
1783 s
= udev_device
->monitor_buf
;
1784 l
= MONITOR_BUF_SIZE
;
1785 udev_list_entry_foreach(list_entry
, udev_device_get_properties_list_entry(udev_device
)) {
1788 key
= udev_list_entry_get_name(list_entry
);
1789 /* skip private variables */
1793 /* add string to envp array */
1794 udev_device
->envp
[i
++] = s
;
1795 if (i
+1 >= ENVP_SIZE
)
1798 /* add property string to monitor buffer */
1799 l
= strpcpyl(&s
, l
, key
, "=", udev_list_entry_get_value(list_entry
), NULL
);
1802 /* advance past the trailing '\0' that strpcpyl() guarantees */
1806 udev_device
->envp
[i
] = NULL
;
1807 udev_device
->monitor_buf_len
= s
- udev_device
->monitor_buf
;
1808 udev_device
->envp_uptodate
= true;
1812 char **udev_device_get_properties_envp(struct udev_device
*udev_device
)
1814 if (!udev_device
->envp_uptodate
)
1815 if (update_envp_monitor_buf(udev_device
) != 0)
1817 return udev_device
->envp
;
1820 ssize_t
udev_device_get_properties_monitor_buf(struct udev_device
*udev_device
, const char **buf
)
1822 if (!udev_device
->envp_uptodate
)
1823 if (update_envp_monitor_buf(udev_device
) != 0)
1825 *buf
= udev_device
->monitor_buf
;
1826 return udev_device
->monitor_buf_len
;
1829 int udev_device_set_action(struct udev_device
*udev_device
, const char *action
)
1831 free(udev_device
->action
);
1832 udev_device
->action
= strdup(action
);
1833 if (udev_device
->action
== NULL
)
1835 udev_device_add_property(udev_device
, "ACTION", udev_device
->action
);
1839 int udev_device_get_devlink_priority(struct udev_device
*udev_device
)
1841 if (!udev_device
->info_loaded
)
1842 udev_device_read_db(udev_device
, NULL
);
1843 return udev_device
->devlink_priority
;
1846 int udev_device_set_devlink_priority(struct udev_device
*udev_device
, int prio
)
1848 udev_device
->devlink_priority
= prio
;
1852 int udev_device_get_watch_handle(struct udev_device
*udev_device
)
1854 if (!udev_device
->info_loaded
)
1855 udev_device_read_db(udev_device
, NULL
);
1856 return udev_device
->watch_handle
;
1859 int udev_device_set_watch_handle(struct udev_device
*udev_device
, int handle
)
1861 udev_device
->watch_handle
= handle
;
1865 bool udev_device_get_db_persist(struct udev_device
*udev_device
)
1867 return udev_device
->db_persist
;
1870 void udev_device_set_db_persist(struct udev_device
*udev_device
)
1872 udev_device
->db_persist
= true;