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;
46 struct udev_list_node devlinks_list;
47 int devlinks_uptodate;
48 struct udev_list_node properties_list;
59 unsigned long long int seqnum;
60 int num_fake_partitions;
63 struct udev_list_node attr_list;
67 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
71 /* translate to location of db file */
72 util_strlcpy(filename, udev_get_dev_path(udev), len);
73 start = util_strlcat(filename, "/.udev/db/", len);
74 util_strlcat(filename, devpath, len);
75 return util_path_encode(&filename[start], len - start);
78 static int device_read_db(struct udev_device *udev_device)
81 char filename[UTIL_PATH_SIZE];
82 char line[UTIL_LINE_SIZE];
85 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
87 if (lstat(filename, &stats) != 0) {
88 info(udev_device->udev, "no db file to read %s: %m\n", filename);
91 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
92 char target[UTIL_PATH_SIZE];
93 char devnode[UTIL_PATH_SIZE];
97 target_len = readlink(filename, target, sizeof(target));
99 target[target_len] = '\0';
101 info(udev_device->udev, "error reading db link %s: %m\n", filename);
105 next = strchr(target, ' ');
110 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
111 util_strlcat(devnode, "/", sizeof(devnode));
112 util_strlcat(devnode, target, sizeof(devnode));
113 udev_device_set_devnode(udev_device, devnode);
114 while (next != NULL) {
115 char devlink[UTIL_PATH_SIZE];
119 next = strchr(next, ' ');
124 util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
125 util_strlcat(devlink, "/", sizeof(devlink));
126 util_strlcat(devlink, lnk, sizeof(devlink));
127 udev_device_add_devlink(udev_device, devlink);
129 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
133 f = fopen(filename, "r");
135 info(udev_device->udev, "error reading db file %s: %m\n", filename);
138 while (fgets(line, sizeof(line), f)) {
149 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
150 util_strlcat(filename, "/", sizeof(filename));
151 util_strlcat(filename, val, sizeof(filename));
152 udev_device_set_devnode(udev_device, filename);
155 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
156 util_strlcat(filename, "/", sizeof(filename));
157 util_strlcat(filename, val, sizeof(filename));
158 udev_device_add_devlink(udev_device, filename);
161 udev_device_set_devlink_priority(udev_device, atoi(val));
164 udev_device_set_event_timeout(udev_device, atoi(val));
167 udev_device_set_num_fake_partitions(udev_device, atoi(val));
170 udev_device_set_ignore_remove(udev_device, atoi(val));
173 udev_device_add_property_from_string(udev_device, val);
179 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
183 int udev_device_read_uevent_file(struct udev_device *udev_device)
185 char filename[UTIL_PATH_SIZE];
187 char line[UTIL_LINE_SIZE];
191 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
192 util_strlcat(filename, "/uevent", sizeof(filename));
193 f = fopen(filename, "r");
197 while (fgets(line, sizeof(line), f)) {
200 pos = strchr(line, '\n');
205 if (strncmp(line, "MAJOR=", 6) == 0)
206 maj = strtoull(&line[6], NULL, 10);
207 else if (strncmp(line, "MINOR=", 6) == 0)
208 min = strtoull(&line[6], NULL, 10);
210 udev_device_add_property_from_string(udev_device, line);
213 udev_device->devnum = makedev(maj, min);
219 static void device_load_info(struct udev_device *device)
221 device->info_loaded = 1;
222 udev_device_read_uevent_file(device);
223 device_read_db(device);
226 void udev_device_set_info_loaded(struct udev_device *device)
228 device->info_loaded = 1;
231 struct udev_device *device_new(struct udev *udev)
233 struct udev_device *udev_device;
238 udev_device = malloc(sizeof(struct udev_device));
239 if (udev_device == NULL)
241 memset(udev_device, 0x00, sizeof(struct udev_device));
242 udev_device->refcount = 1;
243 udev_device->udev = udev;
244 udev_list_init(&udev_device->devlinks_list);
245 udev_list_init(&udev_device->properties_list);
246 udev_list_init(&udev_device->attr_list);
247 udev_device->event_timeout = -1;
248 info(udev_device->udev, "udev_device: %p created\n", udev_device);
253 * udev_device_new_from_syspath:
254 * @udev: udev library context
255 * @syspath: sys device path including sys directory
257 * Create new udev device, and fill in information from the sys
258 * device and the udev database entry. The sypath is the absolute
259 * path to the device, including the sys mount point.
261 * The initial refcount is 1, and needs to be decremented to
262 * release the ressources of the udev device.
264 * Returns: a new udev device, or #NULL, if it does not exist
266 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
270 char path[UTIL_PATH_SIZE];
273 struct udev_device *udev_device;
280 /* path starts in sys */
281 len = strlen(udev_get_sys_path(udev));
282 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
283 info(udev, "not in sys :%s\n", syspath);
287 /* path is not a root directory */
288 subdir = &syspath[len+1];
289 pos = strrchr(subdir, '/');
290 if (pos == NULL || pos < &subdir[2]) {
291 info(udev, "not a subdir :%s\n", syspath);
295 /* resolve possible symlink to real path */
296 util_strlcpy(path, syspath, sizeof(path));
297 util_resolve_sys_link(udev, path, sizeof(path));
299 /* try to resolve the silly block layout if needed */
300 if (strncmp(&path[len], "/block/", 7) == 0) {
301 char block[UTIL_PATH_SIZE];
302 char part[UTIL_PATH_SIZE];
304 util_strlcpy(block, path, sizeof(block));
305 pos = strrchr(block, '/');
308 util_strlcpy(part, pos, sizeof(part));
310 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
311 util_strlcpy(path, block, sizeof(path));
312 util_strlcat(path, part, sizeof(path));
316 /* path exists in sys */
317 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
318 strncmp(&syspath[len], "/class/", 7) == 0 ||
319 strncmp(&syspath[len], "/block/", 7) == 0) {
320 char file[UTIL_PATH_SIZE];
322 /* all "devices" require a "uevent" file */
323 util_strlcpy(file, path, sizeof(file));
324 util_strlcat(file, "/uevent", sizeof(file));
325 if (stat(file, &statbuf) != 0) {
326 info(udev, "not a device: %s\n", syspath);
330 /* everything else just needs to be a directory */
331 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
332 info(udev, "directory not found: %s\n", syspath);
337 udev_device = device_new(udev);
338 if (udev_device == NULL)
341 udev_device_set_syspath(udev_device, path);
342 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
347 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
349 char path[UTIL_PATH_SIZE];
350 const char *type_str;
351 struct udev_enumerate *udev_enumerate;
352 struct udev_list_entry *list_entry;
353 struct udev_device *device = NULL;
357 else if (type == 'c')
362 /* /sys/dev/{block,char}/<maj>:<min> link */
363 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
364 type_str, major(devnum), minor(devnum));
365 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
366 return udev_device_new_from_syspath(udev, path);
368 udev_enumerate = udev_enumerate_new(udev);
369 if (udev_enumerate == NULL)
372 /* fallback to search sys devices for the major/minor */
374 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
375 else if (type == 'c')
376 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
377 udev_enumerate_scan_devices(udev_enumerate);
378 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
379 struct udev_device *device_loop;
381 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
382 if (device_loop != NULL) {
383 if (udev_device_get_devnum(device_loop) == devnum) {
384 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
386 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
388 device = device_loop;
391 udev_device_unref(device_loop);
394 udev_enumerate_unref(udev_enumerate);
398 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
401 char path_full[UTIL_PATH_SIZE];
405 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
406 path = &path_full[sys_path_len];
408 if (strcmp(subsystem, "subsystem") == 0) {
409 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
410 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
411 if (stat(path_full, &statbuf) == 0)
414 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
415 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
416 if (stat(path_full, &statbuf) == 0)
419 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
420 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
421 if (stat(path_full, &statbuf) == 0)
426 if (strcmp(subsystem, "module") == 0) {
427 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
428 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
429 if (stat(path_full, &statbuf) == 0)
434 if (strcmp(subsystem, "drivers") == 0) {
435 char subsys[UTIL_NAME_SIZE];
438 util_strlcpy(subsys, sysname, sizeof(subsys));
439 driver = strchr(subsys, ':');
440 if (driver != NULL) {
443 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
444 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
445 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
446 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
447 if (stat(path_full, &statbuf) == 0)
450 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
451 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
452 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
453 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
454 if (stat(path_full, &statbuf) == 0)
460 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
461 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
462 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
463 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
464 if (stat(path_full, &statbuf) == 0)
467 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
468 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
469 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
470 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
471 if (stat(path_full, &statbuf) == 0)
474 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
475 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
476 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
477 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
478 if (stat(path_full, &statbuf) == 0)
483 return udev_device_new_from_syspath(udev, path_full);
486 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
488 struct udev_device *udev_device_parent = NULL;
489 char path[UTIL_PATH_SIZE];
492 /* follow "device" link in deprecated sys layout */
493 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
494 strncmp(udev_device->devpath, "/block/", 7) == 0) {
495 util_strlcpy(path, udev_device->syspath, sizeof(path));
496 util_strlcat(path, "/device", sizeof(path));
497 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
498 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
499 return udev_device_parent;
502 util_strlcpy(path, udev_device->syspath, sizeof(path));
503 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
507 pos = strrchr(subdir, '/');
508 if (pos == NULL || pos < &subdir[2])
511 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
512 if (udev_device_parent != NULL)
513 return udev_device_parent;
518 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
520 if (udev_device == NULL)
522 if (!udev_device->parent_set) {
523 udev_device->parent_set = 1;
524 udev_device->parent_device = device_new_from_parent(udev_device);
526 if (udev_device->parent_device != NULL)
527 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
528 return udev_device->parent_device;
531 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
533 struct udev_device *parent;
535 parent = udev_device_get_parent(udev_device);
536 while (parent != NULL) {
537 const char *parent_subsystem;
539 parent_subsystem = udev_device_get_subsystem(parent);
540 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
542 parent = udev_device_get_parent(parent);
548 * udev_device_get_udev:
549 * @udev_device: udev device
551 * Retrieve the udev library context the device was created with.
553 * Returns: the udev library context
555 struct udev *udev_device_get_udev(struct udev_device *udev_device)
557 if (udev_device == NULL)
559 return udev_device->udev;
564 * @udev_device: udev device
566 * Take a reference of a udev device.
568 * Returns: the passed udev device
570 struct udev_device *udev_device_ref(struct udev_device *udev_device)
572 if (udev_device == NULL)
574 udev_device->refcount++;
580 * @udev_device: udev device
582 * Drop a reference of a udev device. If the refcount reaches zero,
583 * the ressources of the device will be released.
586 void udev_device_unref(struct udev_device *udev_device)
590 if (udev_device == NULL)
592 udev_device->refcount--;
593 if (udev_device->refcount > 0)
595 if (udev_device->parent_device != NULL)
596 udev_device_unref(udev_device->parent_device);
597 free(udev_device->syspath);
598 free(udev_device->sysname);
599 free(udev_device->devnode);
600 free(udev_device->subsystem);
601 udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list);
602 udev_list_cleanup(udev_device->udev, &udev_device->properties_list);
603 free(udev_device->action);
604 free(udev_device->driver);
605 free(udev_device->devpath_old);
606 free(udev_device->physdevpath);
607 udev_list_cleanup(udev_device->udev, &udev_device->attr_list);
608 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
609 free(udev_device->envp[i]);
610 info(udev_device->udev, "udev_device: %p released\n", udev_device);
615 * udev_device_get_devpath:
616 * @udev_device: udev device
618 * Retrieve the kernel devpath value of the udev device. The path
619 * does not contain the sys mount point, and starts with a '/'.
621 * Returns: the devpath of the udev device
623 const char *udev_device_get_devpath(struct udev_device *udev_device)
625 if (udev_device == NULL)
627 return udev_device->devpath;
631 * udev_device_get_syspath:
632 * @udev_device: udev device
634 * Retrieve the sys path of the udev device. The path is an
635 * absolute path and starts with the sys mount point.
637 * Returns: the sys path of the udev device
639 const char *udev_device_get_syspath(struct udev_device *udev_device)
641 if (udev_device == NULL)
643 return udev_device->syspath;
646 const char *udev_device_get_sysname(struct udev_device *udev_device)
648 if (udev_device == NULL)
650 return udev_device->sysname;
653 const char *udev_device_get_sysnum(struct udev_device *udev_device)
655 if (udev_device == NULL)
657 return udev_device->sysnum;
661 * udev_device_get_devnode:
662 * @udev_device: udev device
664 * Retrieve the device node file name belonging to the udev device.
665 * The path is an absolute path, and starts with the device directory.
667 * Returns: the device node file name of the udev device, or #NULL if no device node exists
669 const char *udev_device_get_devnode(struct udev_device *udev_device)
671 if (udev_device == NULL)
673 if (!udev_device->info_loaded)
674 device_load_info(udev_device);
675 return udev_device->devnode;
679 * udev_device_get_subsystem:
680 * @udev_device: udev device
682 * Retrieve the subsystem string of the udev device. The string does not
685 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
687 const char *udev_device_get_subsystem(struct udev_device *udev_device)
689 char subsystem[UTIL_NAME_SIZE];
691 if (udev_device == NULL)
693 if (!udev_device->subsystem_set) {
694 udev_device->subsystem_set = 1;
695 /* read "subsytem" link */
696 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
697 udev_device_set_subsystem(udev_device, subsystem);
698 return udev_device->subsystem;
701 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
702 udev_device_set_subsystem(udev_device, "module");
703 return udev_device->subsystem;
705 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
706 udev_device_set_subsystem(udev_device, "drivers");
707 return udev_device->subsystem;
709 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
710 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
711 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
712 udev_device_set_subsystem(udev_device, "subsystem");
713 return udev_device->subsystem;
716 return udev_device->subsystem;
720 * udev_device_get_devlinks_list_entry:
721 * @udev_device: udev device
723 * Retrieve the list of device links pointing to the device file of
724 * the udev device. The next list entry can be retrieved with
725 * udev_list_entry_next(), which returns #NULL if no more entries exist.
726 * The devlink path can be retrieved from the list entry by
727 * udev_list_entry_get_name(). The path is an absolute path, and starts with
728 * the device directory.
730 * Returns: the first entry of the device node link list
732 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
734 if (udev_device == NULL)
736 if (!udev_device->info_loaded)
737 device_load_info(udev_device);
738 return udev_list_get_entry(&udev_device->devlinks_list);
741 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
743 udev_device->devlinks_uptodate = 0;
744 udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list);
748 * udev_device_get_properties_list_entry:
749 * @udev_device: udev device
751 * Retrieve the list of key/value device properties of the udev
752 * device. The next list entry can be retrieved with udev_list_entry_next(),
753 * which returns #NULL if no more entries exist. The property name
754 * can be retrieved from the list entry by udev_list_get_name(),
755 * the property value by udev_list_get_value().
757 * Returns: the first entry of the property list
759 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
761 if (udev_device == NULL)
763 if (!udev_device->info_loaded)
764 device_load_info(udev_device);
765 if (!udev_device->devlinks_uptodate) {
766 char symlinks[UTIL_PATH_SIZE];
767 struct udev_list_entry *list_entry;
769 udev_device->devlinks_uptodate = 1;
770 list_entry = udev_device_get_devlinks_list_entry(udev_device);
771 if (list_entry != NULL) {
772 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
773 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
774 util_strlcat(symlinks, " ", sizeof(symlinks));
775 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
777 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
780 return udev_list_get_entry(&udev_device->properties_list);
783 const char *udev_device_get_driver(struct udev_device *udev_device)
785 char driver[UTIL_NAME_SIZE];
787 if (udev_device == NULL)
789 if (!udev_device->driver_set) {
790 udev_device->driver_set = 1;
791 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
792 udev_device->driver = strdup(driver);
794 return udev_device->driver;
797 dev_t udev_device_get_devnum(struct udev_device *udev_device)
799 if (udev_device == NULL)
800 return makedev(0, 0);
801 if (!udev_device->info_loaded)
802 device_load_info(udev_device);
803 return udev_device->devnum;
806 const char *udev_device_get_action(struct udev_device *udev_device)
808 if (udev_device == NULL)
810 return udev_device->action;
813 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
815 if (udev_device == NULL)
817 return udev_device->seqnum;
820 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
822 struct udev_list_entry *list_entry;
823 char path[UTIL_PATH_SIZE];
824 char value[UTIL_NAME_SIZE];
828 const char *val = NULL;
830 if (udev_device == NULL)
835 /* look for possibly already cached result */
836 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->attr_list)) {
837 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
838 info(udev_device->udev, "got '%s' (%s) from cache\n",
839 attr, udev_list_entry_get_value(list_entry));
840 return udev_list_entry_get_value(list_entry);
844 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
845 util_strlcat(path, "/", sizeof(path));
846 util_strlcat(path, attr, sizeof(path));
848 if (lstat(path, &statbuf) != 0) {
849 info(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
850 udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, NULL, 0, 0);
854 if (S_ISLNK(statbuf.st_mode)) {
855 /* links return the last element of the target path */
856 char target[UTIL_NAME_SIZE];
860 len = readlink(path, target, sizeof(target));
863 pos = strrchr(target, '/');
866 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
867 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
868 val = udev_list_entry_get_value(list_entry);
874 /* skip directories */
875 if (S_ISDIR(statbuf.st_mode))
878 /* skip non-readable files */
879 if ((statbuf.st_mode & S_IRUSR) == 0)
882 /* read attribute value */
883 fd = open(path, O_RDONLY);
885 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
888 size = read(fd, value, sizeof(value));
892 if (size == sizeof(value))
895 /* got a valid value, store it in cache and return it */
897 util_remove_trailing_chars(value, '\n');
898 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
899 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
900 val = udev_list_entry_get_value(list_entry);
905 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
910 free(udev_device->syspath);
911 udev_device->syspath = strdup(syspath);
912 if (udev_device->syspath == NULL)
914 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
915 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
917 pos = strrchr(udev_device->syspath, '/');
920 udev_device->sysname = strdup(&pos[1]);
921 if (udev_device->sysname == NULL)
924 /* some devices have '!' in their name, change that to '/' */
926 while (udev_device->sysname[len] != '\0') {
927 if (udev_device->sysname[len] == '!')
928 udev_device->sysname[len] = '/';
932 /* trailing number */
933 while (isdigit(udev_device->sysname[--len]))
934 udev_device->sysnum = &udev_device->sysname[len];
938 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
940 free(udev_device->subsystem);
941 udev_device->subsystem = strdup(subsystem);
942 if (udev_device->subsystem == NULL)
944 udev_device->subsystem_set = 1;
945 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
949 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
951 free(udev_device->devnode);
952 udev_device->devnode = strdup(devnode);
955 if (udev_device->devnode == NULL)
957 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
961 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
963 udev_device->devlinks_uptodate = 0;
964 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
969 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
971 udev_device->envp_uptodate = 0;
972 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
975 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
977 char name[UTIL_PATH_SIZE];
980 strncpy(name, property, sizeof(name));
981 val = strchr(name, '=');
988 return udev_device_add_property(udev_device, name, val);
991 char **udev_device_get_properties_envp(struct udev_device *udev_device)
993 if (!udev_device->envp_uptodate) {
995 struct udev_list_entry *list_entry;
997 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
998 free(udev_device->envp[i]);
1000 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1001 asprintf(&udev_device->envp[i++], "%s=%s",
1002 udev_list_entry_get_name(list_entry),
1003 udev_list_entry_get_value(list_entry));
1004 if (i+1 >= ARRAY_SIZE(udev_device->envp))
1007 udev_device->envp[i] = NULL;
1008 info(udev_device->udev, "constructed envp from %u properties\n", i);
1009 udev_device->envp_uptodate = 1;
1011 return udev_device->envp;
1014 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1016 free(udev_device->action);
1017 udev_device->action = strdup(action);
1018 if (udev_device->action == NULL)
1020 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1024 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1026 free(udev_device->driver);
1027 udev_device->driver = strdup(driver);
1028 if (udev_device->driver == NULL)
1030 udev_device->driver_set = 1;
1031 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1035 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1037 return udev_device->devpath_old;
1040 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1042 udev_device->devpath_old = strdup(devpath_old);
1043 if (udev_device->devpath_old == NULL)
1045 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1049 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1051 return udev_device->physdevpath;
1054 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1056 udev_device->physdevpath = strdup(physdevpath);
1057 if (udev_device->physdevpath == NULL)
1062 int udev_device_get_timeout(struct udev_device *udev_device)
1064 return udev_device->timeout;
1067 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1069 udev_device->timeout = timeout;
1072 int udev_device_get_event_timeout(struct udev_device *udev_device)
1074 if (!udev_device->info_loaded)
1075 device_load_info(udev_device);
1076 return udev_device->event_timeout;
1079 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1081 udev_device->event_timeout = event_timeout;
1085 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1089 udev_device->seqnum = seqnum;
1090 snprintf(num, sizeof(num), "%llu", seqnum);
1091 udev_device_add_property(udev_device, "SEQNUM", num);
1095 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1099 udev_device->devnum = devnum;
1101 snprintf(num, sizeof(num), "%u", major(devnum));
1102 udev_device_add_property(udev_device, "MAJOR", num);
1103 snprintf(num, sizeof(num), "%u", minor(devnum));
1104 udev_device_add_property(udev_device, "MINOR", num);
1108 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1110 if (!udev_device->info_loaded)
1111 device_load_info(udev_device);
1112 return udev_device->num_fake_partitions;
1115 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1117 udev_device->num_fake_partitions = num;
1121 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1123 if (!udev_device->info_loaded)
1124 device_load_info(udev_device);
1125 return udev_device->devlink_priority;
1128 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1130 udev_device->devlink_priority = prio;
1134 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1136 if (!udev_device->info_loaded)
1137 device_load_info(udev_device);
1138 return udev_device->ignore_remove;
1141 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1143 udev_device->ignore_remove = ignore;