2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
32 #include "libudev-private.h"
37 struct udev_device *parent_device;
44 struct udev_list_node devlinks_list;
45 int devlinks_uptodate;
46 struct udev_list_node properties_list;
56 unsigned long long int seqnum;
57 int num_fake_partitions;
60 struct udev_list_node attr_list;
64 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
68 /* translate to location of db file */
69 util_strlcpy(filename, udev_get_dev_path(udev), len);
70 start = util_strlcat(filename, "/.udev/db/", len);
71 util_strlcat(filename, devpath, len);
72 return util_path_encode(&filename[start], len - start);
75 static int device_read_db(struct udev_device *udev_device)
78 char filename[UTIL_PATH_SIZE];
79 char line[UTIL_LINE_SIZE];
82 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
84 if (lstat(filename, &stats) != 0) {
85 info(udev_device->udev, "no db file to read %s: %m\n", filename);
88 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
89 char target[UTIL_PATH_SIZE];
90 char devnode[UTIL_PATH_SIZE];
94 target_len = readlink(filename, target, sizeof(target));
96 target[target_len] = '\0';
98 info(udev_device->udev, "error reading db link %s: %m\n", filename);
102 next = strchr(target, ' ');
107 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
108 util_strlcat(devnode, "/", sizeof(devnode));
109 util_strlcat(devnode, target, sizeof(devnode));
110 udev_device_set_devnode(udev_device, devnode);
111 while (next != NULL) {
112 char devlink[UTIL_PATH_SIZE];
116 next = strchr(next, ' ');
121 util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
122 util_strlcat(devlink, "/", sizeof(devlink));
123 util_strlcat(devlink, lnk, sizeof(devlink));
124 udev_device_add_devlink(udev_device, devlink);
126 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
130 f = fopen(filename, "r");
132 info(udev_device->udev, "error reading db file %s: %m\n", filename);
135 while (fgets(line, sizeof(line), f)) {
146 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
147 util_strlcat(filename, "/", sizeof(filename));
148 util_strlcat(filename, val, sizeof(filename));
149 udev_device_set_devnode(udev_device, filename);
152 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
153 util_strlcat(filename, "/", sizeof(filename));
154 util_strlcat(filename, val, sizeof(filename));
155 udev_device_add_devlink(udev_device, filename);
158 udev_device_set_devlink_priority(udev_device, atoi(val));
161 udev_device_set_event_timeout(udev_device, atoi(val));
164 udev_device_set_num_fake_partitions(udev_device, atoi(val));
167 udev_device_set_ignore_remove(udev_device, atoi(val));
170 udev_device_add_property_from_string(udev_device, val);
176 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
180 int udev_device_read_uevent_file(struct udev_device *udev_device)
182 char filename[UTIL_PATH_SIZE];
184 char line[UTIL_LINE_SIZE];
188 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
189 util_strlcat(filename, "/uevent", sizeof(filename));
190 f = fopen(filename, "r");
194 while (fgets(line, sizeof(line), f)) {
197 pos = strchr(line, '\n');
202 if (strncmp(line, "MAJOR=", 6) == 0)
203 maj = strtoull(&line[6], NULL, 10);
204 else if (strncmp(line, "MINOR=", 6) == 0)
205 min = strtoull(&line[6], NULL, 10);
207 udev_device_add_property_from_string(udev_device, line);
210 udev_device->devnum = makedev(maj, min);
216 static void device_load_info(struct udev_device *device)
218 device->info_loaded = 1;
219 udev_device_read_uevent_file(device);
220 device_read_db(device);
223 void udev_device_set_info_loaded(struct udev_device *device)
225 device->info_loaded = 1;
228 struct udev_device *device_new(struct udev *udev)
230 struct udev_device *udev_device;
235 udev_device = malloc(sizeof(struct udev_device));
236 if (udev_device == NULL)
238 memset(udev_device, 0x00, sizeof(struct udev_device));
239 udev_device->refcount = 1;
240 udev_device->udev = udev;
241 udev_list_init(&udev_device->devlinks_list);
242 udev_list_init(&udev_device->properties_list);
243 udev_list_init(&udev_device->attr_list);
244 udev_device->event_timeout = -1;
245 info(udev_device->udev, "udev_device: %p created\n", udev_device);
250 * udev_device_new_from_syspath:
251 * @udev: udev library context
252 * @syspath: sys device path including sys directory
254 * Create new udev device, and fill in information from the sys
255 * device and the udev database entry. The sypath is the absolute
256 * path to the device, including the sys mount point.
258 * The initial refcount is 1, and needs to be decremented to
259 * release the ressources of the udev device.
261 * Returns: a new udev device, or #NULL, if it does not exist
263 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
267 char path[UTIL_PATH_SIZE];
270 struct udev_device *udev_device;
277 /* path starts in sys */
278 len = strlen(udev_get_sys_path(udev));
279 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
280 info(udev, "not in sys :%s\n", syspath);
284 /* path is not a root directory */
285 subdir = &syspath[len+1];
286 pos = strrchr(subdir, '/');
287 if (pos == NULL || pos < &subdir[2]) {
288 info(udev, "not a subdir :%s\n", syspath);
292 /* resolve possible symlink to real path */
293 util_strlcpy(path, syspath, sizeof(path));
294 util_resolve_sys_link(udev, path, sizeof(path));
296 /* try to resolve the silly block layout if needed */
297 if (strncmp(&path[len], "/block/", 7) == 0) {
298 char block[UTIL_PATH_SIZE];
299 char part[UTIL_PATH_SIZE];
301 util_strlcpy(block, path, sizeof(block));
302 pos = strrchr(block, '/');
305 util_strlcpy(part, pos, sizeof(part));
307 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
308 util_strlcpy(path, block, sizeof(path));
309 util_strlcat(path, part, sizeof(path));
313 /* path exists in sys */
314 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
315 strncmp(&syspath[len], "/class/", 7) == 0 ||
316 strncmp(&syspath[len], "/block/", 7) == 0) {
317 char file[UTIL_PATH_SIZE];
319 /* all "devices" require a "uevent" file */
320 util_strlcpy(file, path, sizeof(file));
321 util_strlcat(file, "/uevent", sizeof(file));
322 if (stat(file, &statbuf) != 0) {
323 info(udev, "not a device: %s\n", syspath);
327 /* everything else just needs to be a directory */
328 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
329 info(udev, "directory not found: %s\n", syspath);
334 udev_device = device_new(udev);
335 if (udev_device == NULL)
338 udev_device_set_syspath(udev_device, path);
339 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
344 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
346 char path[UTIL_PATH_SIZE];
347 const char *type_str;
348 struct udev_enumerate *udev_enumerate;
349 struct udev_list_entry *list_entry;
350 struct udev_device *device = NULL;
354 else if (type == 'c')
359 /* /sys/dev/{block,char}/<maj>:<min> link */
360 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
361 type_str, major(devnum), minor(devnum));
362 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
363 return udev_device_new_from_syspath(udev, path);
365 udev_enumerate = udev_enumerate_new(udev);
366 if (udev_enumerate == NULL)
369 /* fallback to search sys devices for the major/minor */
371 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
372 else if (type == 'c')
373 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
374 udev_enumerate_scan_devices(udev_enumerate);
375 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
376 struct udev_device *device_loop;
378 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
379 if (device_loop != NULL) {
380 if (udev_device_get_devnum(device_loop) == devnum) {
381 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
383 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
385 device = device_loop;
388 udev_device_unref(device_loop);
391 udev_enumerate_unref(udev_enumerate);
395 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
398 char path_full[UTIL_PATH_SIZE];
402 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
403 path = &path_full[sys_path_len];
405 if (strcmp(subsystem, "subsystem") == 0) {
406 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
407 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
408 if (stat(path_full, &statbuf) == 0)
411 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
412 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
413 if (stat(path_full, &statbuf) == 0)
416 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
417 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
418 if (stat(path_full, &statbuf) == 0)
423 if (strcmp(subsystem, "module") == 0) {
424 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
425 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
426 if (stat(path_full, &statbuf) == 0)
431 if (strcmp(subsystem, "drivers") == 0) {
432 char subsys[UTIL_NAME_SIZE];
435 util_strlcpy(subsys, sysname, sizeof(subsys));
436 driver = strchr(subsys, ':');
437 if (driver != NULL) {
440 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
441 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
442 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
443 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
444 if (stat(path_full, &statbuf) == 0)
447 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
448 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
449 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
450 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
451 if (stat(path_full, &statbuf) == 0)
457 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
458 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
459 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
460 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
461 if (stat(path_full, &statbuf) == 0)
464 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
465 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
466 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
467 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
468 if (stat(path_full, &statbuf) == 0)
471 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
472 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
473 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
474 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
475 if (stat(path_full, &statbuf) == 0)
480 return udev_device_new_from_syspath(udev, path_full);
483 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
485 struct udev_device *udev_device_parent = NULL;
486 char path[UTIL_PATH_SIZE];
489 /* follow "device" link in deprecated sys layout */
490 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
491 strncmp(udev_device->devpath, "/block/", 7) == 0) {
492 util_strlcpy(path, udev_device->syspath, sizeof(path));
493 util_strlcat(path, "/device", sizeof(path));
494 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
495 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
496 return udev_device_parent;
499 util_strlcpy(path, udev_device->syspath, sizeof(path));
500 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
504 pos = strrchr(subdir, '/');
505 if (pos == NULL || pos < &subdir[2])
508 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
509 if (udev_device_parent != NULL)
510 return udev_device_parent;
515 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
517 if (udev_device == NULL)
520 if (udev_device->parent_device != NULL) {
521 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
522 return udev_device->parent_device;
524 udev_device->parent_device = device_new_from_parent(udev_device);
525 return udev_device->parent_device;
528 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
530 struct udev_device *parent;
532 parent = udev_device_get_parent(udev_device);
533 while (parent != NULL) {
534 const char *parent_subsystem;
536 parent_subsystem = udev_device_get_subsystem(parent);
537 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
539 parent = udev_device_get_parent(parent);
545 * udev_device_get_udev:
546 * @udev_device: udev device
548 * Retrieve the udev library context the device was created with.
550 * Returns: the udev library context
552 struct udev *udev_device_get_udev(struct udev_device *udev_device)
554 if (udev_device == NULL)
556 return udev_device->udev;
561 * @udev_device: udev device
563 * Take a reference of a udev device.
565 * Returns: the passed udev device
567 struct udev_device *udev_device_ref(struct udev_device *udev_device)
569 if (udev_device == NULL)
571 udev_device->refcount++;
577 * @udev_device: udev device
579 * Drop a reference of a udev device. If the refcount reaches zero,
580 * the ressources of the device will be released.
583 void udev_device_unref(struct udev_device *udev_device)
587 if (udev_device == NULL)
589 udev_device->refcount--;
590 if (udev_device->refcount > 0)
592 if (udev_device->parent_device != NULL)
593 udev_device_unref(udev_device->parent_device);
594 free(udev_device->syspath);
595 free(udev_device->sysname);
596 free(udev_device->devnode);
597 free(udev_device->subsystem);
598 udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list);
599 udev_list_cleanup(udev_device->udev, &udev_device->properties_list);
600 free(udev_device->action);
601 free(udev_device->driver);
602 free(udev_device->devpath_old);
603 free(udev_device->physdevpath);
604 udev_list_cleanup(udev_device->udev, &udev_device->attr_list);
605 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
606 free(udev_device->envp[i]);
607 info(udev_device->udev, "udev_device: %p released\n", udev_device);
612 * udev_device_get_devpath:
613 * @udev_device: udev device
615 * Retrieve the kernel devpath value of the udev device. The path
616 * does not contain the sys mount point, and starts with a '/'.
618 * Returns: the devpath of the udev device
620 const char *udev_device_get_devpath(struct udev_device *udev_device)
622 if (udev_device == NULL)
624 return udev_device->devpath;
628 * udev_device_get_syspath:
629 * @udev_device: udev device
631 * Retrieve the sys path of the udev device. The path is an
632 * absolute path and starts with the sys mount point.
634 * Returns: the sys path of the udev device
636 const char *udev_device_get_syspath(struct udev_device *udev_device)
638 if (udev_device == NULL)
640 return udev_device->syspath;
643 const char *udev_device_get_sysname(struct udev_device *udev_device)
645 if (udev_device == NULL)
647 return udev_device->sysname;
650 const char *udev_device_get_sysnum(struct udev_device *udev_device)
652 if (udev_device == NULL)
654 return udev_device->sysnum;
658 * udev_device_get_devnode:
659 * @udev_device: udev device
661 * Retrieve the device node file name belonging to the udev device.
662 * The path is an absolute path, and starts with the device directory.
664 * Returns: the device node file name of the udev device, or #NULL if no device node exists
666 const char *udev_device_get_devnode(struct udev_device *udev_device)
668 if (udev_device == NULL)
670 if (!udev_device->info_loaded)
671 device_load_info(udev_device);
672 return udev_device->devnode;
676 * udev_device_get_subsystem:
677 * @udev_device: udev device
679 * Retrieve the subsystem string of the udev device. The string does not
682 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
684 const char *udev_device_get_subsystem(struct udev_device *udev_device)
686 char subsystem[UTIL_NAME_SIZE];
688 if (udev_device == NULL)
690 if (udev_device->subsystem != NULL)
691 return udev_device->subsystem;
693 /* read "subsytem" link */
694 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
695 udev_device->subsystem = strdup(subsystem);
696 return udev_device->subsystem;
700 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
701 udev_device->subsystem = strdup("module");
702 return udev_device->subsystem;
704 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
705 udev_device->subsystem = strdup("drivers");
706 return udev_device->subsystem;
708 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
709 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
710 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
711 udev_device->subsystem = strdup("subsystem");
712 return udev_device->subsystem;
718 * udev_device_get_devlinks_list_entry:
719 * @udev_device: udev device
721 * Retrieve the list of device links pointing to the device file of
722 * the udev device. The next list entry can be retrieved with
723 * udev_list_entry_next(), which returns #NULL if no more entries exist.
724 * The devlink path can be retrieved from the list entry by
725 * udev_list_entry_get_name(). The path is an absolute path, and starts with
726 * the device directory.
728 * Returns: the first entry of the device node link list
730 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
732 if (udev_device == NULL)
734 if (!udev_device->info_loaded)
735 device_load_info(udev_device);
736 return udev_list_get_entry(&udev_device->devlinks_list);
739 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
741 udev_device->devlinks_uptodate = 0;
742 udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list);
746 * udev_device_get_properties_list_entry:
747 * @udev_device: udev device
749 * Retrieve the list of key/value device properties of the udev
750 * device. The next list entry can be retrieved with udev_list_entry_next(),
751 * which returns #NULL if no more entries exist. The property name
752 * can be retrieved from the list entry by udev_list_get_name(),
753 * the property value by udev_list_get_value().
755 * Returns: the first entry of the property list
757 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
759 if (udev_device == NULL)
761 if (!udev_device->info_loaded)
762 device_load_info(udev_device);
763 if (!udev_device->devlinks_uptodate) {
764 char symlinks[UTIL_PATH_SIZE];
765 struct udev_list_entry *list_entry;
767 udev_device->devlinks_uptodate = 1;
768 list_entry = udev_device_get_devlinks_list_entry(udev_device);
769 if (list_entry != NULL) {
770 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
771 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
772 util_strlcat(symlinks, " ", sizeof(symlinks));
773 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
775 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
778 return udev_list_get_entry(&udev_device->properties_list);
781 const char *udev_device_get_driver(struct udev_device *udev_device)
783 char driver[UTIL_NAME_SIZE];
785 if (udev_device == NULL)
787 if (udev_device->driver != NULL)
788 return udev_device->driver;
789 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
791 udev_device->driver = strdup(driver);
792 return udev_device->driver;
795 dev_t udev_device_get_devnum(struct udev_device *udev_device)
797 if (udev_device == NULL)
798 return makedev(0, 0);
799 if (!udev_device->info_loaded)
800 device_load_info(udev_device);
801 return udev_device->devnum;
804 const char *udev_device_get_action(struct udev_device *udev_device)
806 if (udev_device == NULL)
808 return udev_device->action;
811 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
813 if (udev_device == NULL)
815 return udev_device->seqnum;
818 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
820 struct udev_list_entry *list_entry;
821 char path[UTIL_PATH_SIZE];
822 char value[UTIL_NAME_SIZE];
826 const char *val = NULL;
828 if (udev_device == NULL)
833 /* look for possibly already cached result */
834 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->attr_list)) {
835 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
836 info(udev_device->udev, "got '%s' (%s) from cache\n",
837 attr, udev_list_entry_get_value(list_entry));
838 return udev_list_entry_get_value(list_entry);
842 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
843 util_strlcat(path, "/", sizeof(path));
844 util_strlcat(path, attr, sizeof(path));
846 if (lstat(path, &statbuf) != 0) {
847 info(udev_device->udev, "stat '%s' failed: %m\n", path);
851 if (S_ISLNK(statbuf.st_mode)) {
852 /* links return the last element of the target path */
853 char target[UTIL_NAME_SIZE];
857 len = readlink(path, target, sizeof(target));
860 pos = strrchr(target, '/');
863 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
864 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
865 val = udev_list_entry_get_value(list_entry);
871 /* skip directories */
872 if (S_ISDIR(statbuf.st_mode))
875 /* skip non-readable files */
876 if ((statbuf.st_mode & S_IRUSR) == 0)
879 /* read attribute value */
880 fd = open(path, O_RDONLY);
882 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
885 size = read(fd, value, sizeof(value));
889 if (size == sizeof(value))
892 /* got a valid value, store it in cache and return it */
894 util_remove_trailing_chars(value, '\n');
895 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
896 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
897 val = udev_list_entry_get_value(list_entry);
902 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
907 free(udev_device->syspath);
908 udev_device->syspath = strdup(syspath);
909 if (udev_device->syspath == NULL)
911 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
912 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
914 pos = strrchr(udev_device->syspath, '/');
917 udev_device->sysname = strdup(&pos[1]);
918 if (udev_device->sysname == NULL)
921 /* some devices have '!' in their name, change that to '/' */
923 while (udev_device->sysname[len] != '\0') {
924 if (udev_device->sysname[len] == '!')
925 udev_device->sysname[len] = '/';
929 /* trailing number */
930 while (isdigit(udev_device->sysname[--len]))
931 udev_device->sysnum = &udev_device->sysname[len];
935 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
937 free(udev_device->subsystem);
938 udev_device->subsystem = strdup(subsystem);
939 if (udev_device->subsystem == NULL)
941 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
945 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
947 free(udev_device->devnode);
948 udev_device->devnode = strdup(devnode);
951 if (udev_device->devnode == NULL)
953 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
957 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
959 udev_device->devlinks_uptodate = 0;
960 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
965 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
967 udev_device->envp_uptodate = 0;
968 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
971 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
973 char name[UTIL_PATH_SIZE];
976 strncpy(name, property, sizeof(name));
977 val = strchr(name, '=');
984 return udev_device_add_property(udev_device, name, val);
987 char **udev_device_get_properties_envp(struct udev_device *udev_device)
989 if (!udev_device->envp_uptodate) {
991 struct udev_list_entry *list_entry;
993 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
994 free(udev_device->envp[i]);
996 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
997 asprintf(&udev_device->envp[i++], "%s=%s",
998 udev_list_entry_get_name(list_entry),
999 udev_list_entry_get_value(list_entry));
1000 if (i+1 >= ARRAY_SIZE(udev_device->envp))
1003 udev_device->envp[i] = NULL;
1004 info(udev_device->udev, "constructed envp from %u properties\n", i);
1005 udev_device->envp_uptodate = 1;
1007 return udev_device->envp;
1010 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1012 free(udev_device->action);
1013 udev_device->action = strdup(action);
1014 if (udev_device->action == NULL)
1016 udev_device_add_property(udev_device, "ACTION", action);
1020 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1022 free(udev_device->driver);
1023 udev_device->driver = strdup(driver);
1024 if (udev_device->driver == NULL)
1029 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1031 return udev_device->devpath_old;
1034 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1036 udev_device->devpath_old = strdup(devpath_old);
1037 if (udev_device->devpath_old == NULL)
1042 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1044 return udev_device->physdevpath;
1047 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1049 udev_device->physdevpath = strdup(physdevpath);
1050 if (udev_device->physdevpath == NULL)
1055 int udev_device_get_timeout(struct udev_device *udev_device)
1057 return udev_device->timeout;
1060 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1062 udev_device->timeout = timeout;
1065 int udev_device_get_event_timeout(struct udev_device *udev_device)
1067 if (!udev_device->info_loaded)
1068 device_load_info(udev_device);
1069 return udev_device->event_timeout;
1072 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1074 udev_device->event_timeout = event_timeout;
1078 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1080 udev_device->seqnum = seqnum;
1084 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1086 udev_device->devnum = devnum;
1090 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1092 if (!udev_device->info_loaded)
1093 device_load_info(udev_device);
1094 return udev_device->num_fake_partitions;
1097 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1099 udev_device->num_fake_partitions = num;
1103 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1105 if (!udev_device->info_loaded)
1106 device_load_info(udev_device);
1107 return udev_device->devlink_priority;
1110 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1112 udev_device->devlink_priority = prio;
1116 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1118 if (!udev_device->info_loaded)
1119 device_load_info(udev_device);
1120 return udev_device->ignore_remove;
1123 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1125 udev_device->ignore_remove = ignore;