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;
45 struct udev_list_node devlinks_list;
46 int devlinks_uptodate;
47 struct udev_list_node properties_list;
58 unsigned long long int seqnum;
59 int num_fake_partitions;
62 struct udev_list_node attr_list;
66 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
70 /* translate to location of db file */
71 util_strlcpy(filename, udev_get_dev_path(udev), len);
72 start = util_strlcat(filename, "/.udev/db/", len);
73 util_strlcat(filename, devpath, len);
74 return util_path_encode(&filename[start], len - start);
77 static int device_read_db(struct udev_device *udev_device)
80 char filename[UTIL_PATH_SIZE];
81 char line[UTIL_LINE_SIZE];
84 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
86 if (lstat(filename, &stats) != 0) {
87 info(udev_device->udev, "no db file to read %s: %m\n", filename);
90 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
91 char target[UTIL_PATH_SIZE];
92 char devnode[UTIL_PATH_SIZE];
96 target_len = readlink(filename, target, sizeof(target));
98 target[target_len] = '\0';
100 info(udev_device->udev, "error reading db link %s: %m\n", filename);
104 next = strchr(target, ' ');
109 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
110 util_strlcat(devnode, "/", sizeof(devnode));
111 util_strlcat(devnode, target, sizeof(devnode));
112 udev_device_set_devnode(udev_device, devnode);
113 while (next != NULL) {
114 char devlink[UTIL_PATH_SIZE];
118 next = strchr(next, ' ');
123 util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
124 util_strlcat(devlink, "/", sizeof(devlink));
125 util_strlcat(devlink, lnk, sizeof(devlink));
126 udev_device_add_devlink(udev_device, devlink);
128 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
132 f = fopen(filename, "r");
134 info(udev_device->udev, "error reading db file %s: %m\n", filename);
137 while (fgets(line, sizeof(line), f)) {
148 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
149 util_strlcat(filename, "/", sizeof(filename));
150 util_strlcat(filename, val, sizeof(filename));
151 udev_device_set_devnode(udev_device, filename);
154 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
155 util_strlcat(filename, "/", sizeof(filename));
156 util_strlcat(filename, val, sizeof(filename));
157 udev_device_add_devlink(udev_device, filename);
160 udev_device_set_devlink_priority(udev_device, atoi(val));
163 udev_device_set_event_timeout(udev_device, atoi(val));
166 udev_device_set_num_fake_partitions(udev_device, atoi(val));
169 udev_device_set_ignore_remove(udev_device, atoi(val));
172 udev_device_add_property_from_string(udev_device, val);
178 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
182 int udev_device_read_uevent_file(struct udev_device *udev_device)
184 char filename[UTIL_PATH_SIZE];
186 char line[UTIL_LINE_SIZE];
190 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
191 util_strlcat(filename, "/uevent", sizeof(filename));
192 f = fopen(filename, "r");
196 while (fgets(line, sizeof(line), f)) {
199 pos = strchr(line, '\n');
204 if (strncmp(line, "MAJOR=", 6) == 0)
205 maj = strtoull(&line[6], NULL, 10);
206 else if (strncmp(line, "MINOR=", 6) == 0)
207 min = strtoull(&line[6], NULL, 10);
209 udev_device_add_property_from_string(udev_device, line);
212 udev_device->devnum = makedev(maj, min);
218 static void device_load_info(struct udev_device *device)
220 device->info_loaded = 1;
221 udev_device_read_uevent_file(device);
222 device_read_db(device);
225 void udev_device_set_info_loaded(struct udev_device *device)
227 device->info_loaded = 1;
230 struct udev_device *device_new(struct udev *udev)
232 struct udev_device *udev_device;
237 udev_device = malloc(sizeof(struct udev_device));
238 if (udev_device == NULL)
240 memset(udev_device, 0x00, sizeof(struct udev_device));
241 udev_device->refcount = 1;
242 udev_device->udev = udev;
243 udev_list_init(&udev_device->devlinks_list);
244 udev_list_init(&udev_device->properties_list);
245 udev_list_init(&udev_device->attr_list);
246 udev_device->event_timeout = -1;
247 info(udev_device->udev, "udev_device: %p created\n", udev_device);
252 * udev_device_new_from_syspath:
253 * @udev: udev library context
254 * @syspath: sys device path including sys directory
256 * Create new udev device, and fill in information from the sys
257 * device and the udev database entry. The sypath is the absolute
258 * path to the device, including the sys mount point.
260 * The initial refcount is 1, and needs to be decremented to
261 * release the ressources of the udev device.
263 * Returns: a new udev device, or #NULL, if it does not exist
265 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
269 char path[UTIL_PATH_SIZE];
272 struct udev_device *udev_device;
279 /* path starts in sys */
280 len = strlen(udev_get_sys_path(udev));
281 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
282 info(udev, "not in sys :%s\n", syspath);
286 /* path is not a root directory */
287 subdir = &syspath[len+1];
288 pos = strrchr(subdir, '/');
289 if (pos == NULL || pos < &subdir[2]) {
290 info(udev, "not a subdir :%s\n", syspath);
294 /* resolve possible symlink to real path */
295 util_strlcpy(path, syspath, sizeof(path));
296 util_resolve_sys_link(udev, path, sizeof(path));
298 /* try to resolve the silly block layout if needed */
299 if (strncmp(&path[len], "/block/", 7) == 0) {
300 char block[UTIL_PATH_SIZE];
301 char part[UTIL_PATH_SIZE];
303 util_strlcpy(block, path, sizeof(block));
304 pos = strrchr(block, '/');
307 util_strlcpy(part, pos, sizeof(part));
309 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
310 util_strlcpy(path, block, sizeof(path));
311 util_strlcat(path, part, sizeof(path));
315 /* path exists in sys */
316 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
317 strncmp(&syspath[len], "/class/", 7) == 0 ||
318 strncmp(&syspath[len], "/block/", 7) == 0) {
319 char file[UTIL_PATH_SIZE];
321 /* all "devices" require a "uevent" file */
322 util_strlcpy(file, path, sizeof(file));
323 util_strlcat(file, "/uevent", sizeof(file));
324 if (stat(file, &statbuf) != 0) {
325 info(udev, "not a device: %s\n", syspath);
329 /* everything else just needs to be a directory */
330 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
331 info(udev, "directory not found: %s\n", syspath);
336 udev_device = device_new(udev);
337 if (udev_device == NULL)
340 udev_device_set_syspath(udev_device, path);
341 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
346 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
348 char path[UTIL_PATH_SIZE];
349 const char *type_str;
350 struct udev_enumerate *udev_enumerate;
351 struct udev_list_entry *list_entry;
352 struct udev_device *device = NULL;
356 else if (type == 'c')
361 /* /sys/dev/{block,char}/<maj>:<min> link */
362 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
363 type_str, major(devnum), minor(devnum));
364 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
365 return udev_device_new_from_syspath(udev, path);
367 udev_enumerate = udev_enumerate_new(udev);
368 if (udev_enumerate == NULL)
371 /* fallback to search sys devices for the major/minor */
373 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
374 else if (type == 'c')
375 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
376 udev_enumerate_scan_devices(udev_enumerate);
377 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
378 struct udev_device *device_loop;
380 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
381 if (device_loop != NULL) {
382 if (udev_device_get_devnum(device_loop) == devnum) {
383 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
385 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
387 device = device_loop;
390 udev_device_unref(device_loop);
393 udev_enumerate_unref(udev_enumerate);
397 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
400 char path_full[UTIL_PATH_SIZE];
404 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
405 path = &path_full[sys_path_len];
407 if (strcmp(subsystem, "subsystem") == 0) {
408 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
409 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
410 if (stat(path_full, &statbuf) == 0)
413 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
414 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
415 if (stat(path_full, &statbuf) == 0)
418 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
419 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
420 if (stat(path_full, &statbuf) == 0)
425 if (strcmp(subsystem, "module") == 0) {
426 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
427 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
428 if (stat(path_full, &statbuf) == 0)
433 if (strcmp(subsystem, "drivers") == 0) {
434 char subsys[UTIL_NAME_SIZE];
437 util_strlcpy(subsys, sysname, sizeof(subsys));
438 driver = strchr(subsys, ':');
439 if (driver != NULL) {
442 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
443 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
444 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
445 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
446 if (stat(path_full, &statbuf) == 0)
449 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
450 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
451 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
452 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
453 if (stat(path_full, &statbuf) == 0)
459 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
460 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
461 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
462 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
463 if (stat(path_full, &statbuf) == 0)
466 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
467 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
468 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
469 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
470 if (stat(path_full, &statbuf) == 0)
473 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
474 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
475 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
476 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
477 if (stat(path_full, &statbuf) == 0)
482 return udev_device_new_from_syspath(udev, path_full);
485 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
487 struct udev_device *udev_device_parent = NULL;
488 char path[UTIL_PATH_SIZE];
491 /* follow "device" link in deprecated sys layout */
492 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
493 strncmp(udev_device->devpath, "/block/", 7) == 0) {
494 util_strlcpy(path, udev_device->syspath, sizeof(path));
495 util_strlcat(path, "/device", sizeof(path));
496 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
497 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
498 return udev_device_parent;
501 util_strlcpy(path, udev_device->syspath, sizeof(path));
502 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
506 pos = strrchr(subdir, '/');
507 if (pos == NULL || pos < &subdir[2])
510 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
511 if (udev_device_parent != NULL)
512 return udev_device_parent;
517 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
519 if (udev_device == NULL)
522 if (udev_device->parent_device != NULL) {
523 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
524 return udev_device->parent_device;
526 udev_device->parent_device = device_new_from_parent(udev_device);
527 return udev_device->parent_device;
530 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
532 struct udev_device *parent;
534 parent = udev_device_get_parent(udev_device);
535 while (parent != NULL) {
536 const char *parent_subsystem;
538 parent_subsystem = udev_device_get_subsystem(parent);
539 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
541 parent = udev_device_get_parent(parent);
547 * udev_device_get_udev:
548 * @udev_device: udev device
550 * Retrieve the udev library context the device was created with.
552 * Returns: the udev library context
554 struct udev *udev_device_get_udev(struct udev_device *udev_device)
556 if (udev_device == NULL)
558 return udev_device->udev;
563 * @udev_device: udev device
565 * Take a reference of a udev device.
567 * Returns: the passed udev device
569 struct udev_device *udev_device_ref(struct udev_device *udev_device)
571 if (udev_device == NULL)
573 udev_device->refcount++;
579 * @udev_device: udev device
581 * Drop a reference of a udev device. If the refcount reaches zero,
582 * the ressources of the device will be released.
585 void udev_device_unref(struct udev_device *udev_device)
589 if (udev_device == NULL)
591 udev_device->refcount--;
592 if (udev_device->refcount > 0)
594 if (udev_device->parent_device != NULL)
595 udev_device_unref(udev_device->parent_device);
596 free(udev_device->syspath);
597 free(udev_device->sysname);
598 free(udev_device->devnode);
599 free(udev_device->subsystem);
600 udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list);
601 udev_list_cleanup(udev_device->udev, &udev_device->properties_list);
602 free(udev_device->action);
603 free(udev_device->driver);
604 free(udev_device->devpath_old);
605 free(udev_device->physdevpath);
606 udev_list_cleanup(udev_device->udev, &udev_device->attr_list);
607 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
608 free(udev_device->envp[i]);
609 info(udev_device->udev, "udev_device: %p released\n", udev_device);
614 * udev_device_get_devpath:
615 * @udev_device: udev device
617 * Retrieve the kernel devpath value of the udev device. The path
618 * does not contain the sys mount point, and starts with a '/'.
620 * Returns: the devpath of the udev device
622 const char *udev_device_get_devpath(struct udev_device *udev_device)
624 if (udev_device == NULL)
626 return udev_device->devpath;
630 * udev_device_get_syspath:
631 * @udev_device: udev device
633 * Retrieve the sys path of the udev device. The path is an
634 * absolute path and starts with the sys mount point.
636 * Returns: the sys path of the udev device
638 const char *udev_device_get_syspath(struct udev_device *udev_device)
640 if (udev_device == NULL)
642 return udev_device->syspath;
645 const char *udev_device_get_sysname(struct udev_device *udev_device)
647 if (udev_device == NULL)
649 return udev_device->sysname;
652 const char *udev_device_get_sysnum(struct udev_device *udev_device)
654 if (udev_device == NULL)
656 return udev_device->sysnum;
660 * udev_device_get_devnode:
661 * @udev_device: udev device
663 * Retrieve the device node file name belonging to the udev device.
664 * The path is an absolute path, and starts with the device directory.
666 * Returns: the device node file name of the udev device, or #NULL if no device node exists
668 const char *udev_device_get_devnode(struct udev_device *udev_device)
670 if (udev_device == NULL)
672 if (!udev_device->info_loaded)
673 device_load_info(udev_device);
674 return udev_device->devnode;
678 * udev_device_get_subsystem:
679 * @udev_device: udev device
681 * Retrieve the subsystem string of the udev device. The string does not
684 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
686 const char *udev_device_get_subsystem(struct udev_device *udev_device)
688 char subsystem[UTIL_NAME_SIZE];
690 if (udev_device == NULL)
692 if (!udev_device->subsystem_set) {
693 udev_device->subsystem_set = 1;
694 /* read "subsytem" link */
695 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
696 udev_device_set_subsystem(udev_device, subsystem);
697 return udev_device->subsystem;
700 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
701 udev_device_set_subsystem(udev_device, "module");
702 return udev_device->subsystem;
704 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
705 udev_device_set_subsystem(udev_device, "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_set_subsystem(udev_device, "subsystem");
712 return udev_device->subsystem;
715 return udev_device->subsystem;
719 * udev_device_get_devlinks_list_entry:
720 * @udev_device: udev device
722 * Retrieve the list of device links pointing to the device file of
723 * the udev device. The next list entry can be retrieved with
724 * udev_list_entry_next(), which returns #NULL if no more entries exist.
725 * The devlink path can be retrieved from the list entry by
726 * udev_list_entry_get_name(). The path is an absolute path, and starts with
727 * the device directory.
729 * Returns: the first entry of the device node link list
731 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
733 if (udev_device == NULL)
735 if (!udev_device->info_loaded)
736 device_load_info(udev_device);
737 return udev_list_get_entry(&udev_device->devlinks_list);
740 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
742 udev_device->devlinks_uptodate = 0;
743 udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list);
747 * udev_device_get_properties_list_entry:
748 * @udev_device: udev device
750 * Retrieve the list of key/value device properties of the udev
751 * device. The next list entry can be retrieved with udev_list_entry_next(),
752 * which returns #NULL if no more entries exist. The property name
753 * can be retrieved from the list entry by udev_list_get_name(),
754 * the property value by udev_list_get_value().
756 * Returns: the first entry of the property list
758 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
760 if (udev_device == NULL)
762 if (!udev_device->info_loaded)
763 device_load_info(udev_device);
764 if (!udev_device->devlinks_uptodate) {
765 char symlinks[UTIL_PATH_SIZE];
766 struct udev_list_entry *list_entry;
768 udev_device->devlinks_uptodate = 1;
769 list_entry = udev_device_get_devlinks_list_entry(udev_device);
770 if (list_entry != NULL) {
771 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
772 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
773 util_strlcat(symlinks, " ", sizeof(symlinks));
774 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
776 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
779 return udev_list_get_entry(&udev_device->properties_list);
782 const char *udev_device_get_driver(struct udev_device *udev_device)
784 char driver[UTIL_NAME_SIZE];
786 if (udev_device == NULL)
788 if (!udev_device->driver_set) {
789 udev_device->driver_set = 1;
790 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
791 udev_device->driver = strdup(driver);
793 return udev_device->driver;
796 dev_t udev_device_get_devnum(struct udev_device *udev_device)
798 if (udev_device == NULL)
799 return makedev(0, 0);
800 if (!udev_device->info_loaded)
801 device_load_info(udev_device);
802 return udev_device->devnum;
805 const char *udev_device_get_action(struct udev_device *udev_device)
807 if (udev_device == NULL)
809 return udev_device->action;
812 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
814 if (udev_device == NULL)
816 return udev_device->seqnum;
819 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
821 struct udev_list_entry *list_entry;
822 char path[UTIL_PATH_SIZE];
823 char value[UTIL_NAME_SIZE];
827 const char *val = NULL;
829 if (udev_device == NULL)
834 /* look for possibly already cached result */
835 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->attr_list)) {
836 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
837 info(udev_device->udev, "got '%s' (%s) from cache\n",
838 attr, udev_list_entry_get_value(list_entry));
839 return udev_list_entry_get_value(list_entry);
843 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
844 util_strlcat(path, "/", sizeof(path));
845 util_strlcat(path, attr, sizeof(path));
847 if (lstat(path, &statbuf) != 0) {
848 info(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
849 udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, NULL, 0, 0);
853 if (S_ISLNK(statbuf.st_mode)) {
854 /* links return the last element of the target path */
855 char target[UTIL_NAME_SIZE];
859 len = readlink(path, target, sizeof(target));
862 pos = strrchr(target, '/');
865 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
866 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
867 val = udev_list_entry_get_value(list_entry);
873 /* skip directories */
874 if (S_ISDIR(statbuf.st_mode))
877 /* skip non-readable files */
878 if ((statbuf.st_mode & S_IRUSR) == 0)
881 /* read attribute value */
882 fd = open(path, O_RDONLY);
884 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
887 size = read(fd, value, sizeof(value));
891 if (size == sizeof(value))
894 /* got a valid value, store it in cache and return it */
896 util_remove_trailing_chars(value, '\n');
897 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
898 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
899 val = udev_list_entry_get_value(list_entry);
904 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
909 free(udev_device->syspath);
910 udev_device->syspath = strdup(syspath);
911 if (udev_device->syspath == NULL)
913 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
914 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
916 pos = strrchr(udev_device->syspath, '/');
919 udev_device->sysname = strdup(&pos[1]);
920 if (udev_device->sysname == NULL)
923 /* some devices have '!' in their name, change that to '/' */
925 while (udev_device->sysname[len] != '\0') {
926 if (udev_device->sysname[len] == '!')
927 udev_device->sysname[len] = '/';
931 /* trailing number */
932 while (isdigit(udev_device->sysname[--len]))
933 udev_device->sysnum = &udev_device->sysname[len];
937 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
939 free(udev_device->subsystem);
940 udev_device->subsystem = strdup(subsystem);
941 if (udev_device->subsystem == NULL)
943 udev_device->subsystem_set = 1;
944 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
948 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
950 free(udev_device->devnode);
951 udev_device->devnode = strdup(devnode);
954 if (udev_device->devnode == NULL)
956 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
960 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
962 udev_device->devlinks_uptodate = 0;
963 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
968 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
970 udev_device->envp_uptodate = 0;
971 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
974 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
976 char name[UTIL_PATH_SIZE];
979 strncpy(name, property, sizeof(name));
980 val = strchr(name, '=');
987 return udev_device_add_property(udev_device, name, val);
990 char **udev_device_get_properties_envp(struct udev_device *udev_device)
992 if (!udev_device->envp_uptodate) {
994 struct udev_list_entry *list_entry;
996 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
997 free(udev_device->envp[i]);
999 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1000 asprintf(&udev_device->envp[i++], "%s=%s",
1001 udev_list_entry_get_name(list_entry),
1002 udev_list_entry_get_value(list_entry));
1003 if (i+1 >= ARRAY_SIZE(udev_device->envp))
1006 udev_device->envp[i] = NULL;
1007 info(udev_device->udev, "constructed envp from %u properties\n", i);
1008 udev_device->envp_uptodate = 1;
1010 return udev_device->envp;
1013 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1015 free(udev_device->action);
1016 udev_device->action = strdup(action);
1017 if (udev_device->action == NULL)
1019 udev_device_add_property(udev_device, "ACTION", action);
1023 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1025 free(udev_device->driver);
1026 udev_device->driver = strdup(driver);
1027 if (udev_device->driver == NULL)
1029 udev_device->driver_set = 1;
1033 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1035 return udev_device->devpath_old;
1038 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1040 udev_device->devpath_old = strdup(devpath_old);
1041 if (udev_device->devpath_old == NULL)
1046 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1048 return udev_device->physdevpath;
1051 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1053 udev_device->physdevpath = strdup(physdevpath);
1054 if (udev_device->physdevpath == NULL)
1059 int udev_device_get_timeout(struct udev_device *udev_device)
1061 return udev_device->timeout;
1064 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1066 udev_device->timeout = timeout;
1069 int udev_device_get_event_timeout(struct udev_device *udev_device)
1071 if (!udev_device->info_loaded)
1072 device_load_info(udev_device);
1073 return udev_device->event_timeout;
1076 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1078 udev_device->event_timeout = event_timeout;
1082 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1084 udev_device->seqnum = seqnum;
1088 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1090 udev_device->devnum = devnum;
1094 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1096 if (!udev_device->info_loaded)
1097 device_load_info(udev_device);
1098 return udev_device->num_fake_partitions;
1101 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1103 udev_device->num_fake_partitions = num;
1107 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1109 if (!udev_device->info_loaded)
1110 device_load_info(udev_device);
1111 return udev_device->devlink_priority;
1114 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1116 udev_device->devlink_priority = prio;
1120 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1122 if (!udev_device->info_loaded)
1123 device_load_info(udev_device);
1124 return udev_device->ignore_remove;
1127 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1129 udev_device->ignore_remove = ignore;