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 sysattr_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 int udev_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 udev_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;
234 struct udev_list_entry *list_entry;
239 udev_device = calloc(1, sizeof(struct udev_device));
240 if (udev_device == NULL)
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->sysattr_list);
247 udev_device->event_timeout = -1;
248 /* copy global properties */
249 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
250 udev_device_add_property(udev_device,
251 udev_list_entry_get_name(list_entry),
252 udev_list_entry_get_value(list_entry));
253 info(udev_device->udev, "udev_device: %p created\n", udev_device);
258 * udev_device_new_from_syspath:
259 * @udev: udev library context
260 * @syspath: sys device path including sys directory
262 * Create new udev device, and fill in information from the sys
263 * device and the udev database entry. The sypath is the absolute
264 * path to the device, including the sys mount point.
266 * The initial refcount is 1, and needs to be decremented to
267 * release the ressources of the udev device.
269 * Returns: a new udev device, or #NULL, if it does not exist
271 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
275 char path[UTIL_PATH_SIZE];
278 struct udev_device *udev_device;
285 /* path starts in sys */
286 len = strlen(udev_get_sys_path(udev));
287 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
288 info(udev, "not in sys :%s\n", syspath);
292 /* path is not a root directory */
293 subdir = &syspath[len+1];
294 pos = strrchr(subdir, '/');
295 if (pos == NULL || pos < &subdir[2]) {
296 info(udev, "not a subdir :%s\n", syspath);
300 /* resolve possible symlink to real path */
301 util_strlcpy(path, syspath, sizeof(path));
302 util_resolve_sys_link(udev, path, sizeof(path));
304 /* try to resolve the silly block layout if needed */
305 if (strncmp(&path[len], "/block/", 7) == 0) {
306 char block[UTIL_PATH_SIZE];
307 char part[UTIL_PATH_SIZE];
309 util_strlcpy(block, path, sizeof(block));
310 pos = strrchr(block, '/');
313 util_strlcpy(part, pos, sizeof(part));
315 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
316 util_strlcpy(path, block, sizeof(path));
317 util_strlcat(path, part, sizeof(path));
321 /* path exists in sys */
322 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
323 strncmp(&syspath[len], "/class/", 7) == 0 ||
324 strncmp(&syspath[len], "/block/", 7) == 0) {
325 char file[UTIL_PATH_SIZE];
327 /* all "devices" require a "uevent" file */
328 util_strlcpy(file, path, sizeof(file));
329 util_strlcat(file, "/uevent", sizeof(file));
330 if (stat(file, &statbuf) != 0) {
331 info(udev, "not a device: %s\n", syspath);
335 /* everything else just needs to be a directory */
336 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
337 info(udev, "directory not found: %s\n", syspath);
342 udev_device = device_new(udev);
343 if (udev_device == NULL)
346 udev_device_set_syspath(udev_device, path);
347 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
352 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
354 char path[UTIL_PATH_SIZE];
355 const char *type_str;
356 struct udev_enumerate *udev_enumerate;
357 struct udev_list_entry *list_entry;
358 struct udev_device *device = NULL;
362 else if (type == 'c')
367 /* /sys/dev/{block,char}/<maj>:<min> link */
368 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
369 type_str, major(devnum), minor(devnum));
370 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
371 return udev_device_new_from_syspath(udev, path);
373 udev_enumerate = udev_enumerate_new(udev);
374 if (udev_enumerate == NULL)
377 /* fallback to search sys devices for the major/minor */
379 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
380 else if (type == 'c')
381 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
382 udev_enumerate_scan_devices(udev_enumerate);
383 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
384 struct udev_device *device_loop;
386 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
387 if (device_loop != NULL) {
388 if (udev_device_get_devnum(device_loop) == devnum) {
389 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
391 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
393 device = device_loop;
396 udev_device_unref(device_loop);
399 udev_enumerate_unref(udev_enumerate);
403 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
406 char path_full[UTIL_PATH_SIZE];
410 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
411 path = &path_full[sys_path_len];
413 if (strcmp(subsystem, "subsystem") == 0) {
414 util_strlcpy(path, "/subsystem/", 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, "/bus/", sizeof(path_full) - sys_path_len);
420 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
421 if (stat(path_full, &statbuf) == 0)
424 util_strlcpy(path, "/class/", 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, "module") == 0) {
432 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
433 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
434 if (stat(path_full, &statbuf) == 0)
439 if (strcmp(subsystem, "drivers") == 0) {
440 char subsys[UTIL_NAME_SIZE];
443 util_strlcpy(subsys, sysname, sizeof(subsys));
444 driver = strchr(subsys, ':');
445 if (driver != NULL) {
448 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
449 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
450 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
451 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
452 if (stat(path_full, &statbuf) == 0)
455 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
456 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
457 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
458 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
459 if (stat(path_full, &statbuf) == 0)
465 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
466 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
467 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
468 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
469 if (stat(path_full, &statbuf) == 0)
472 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
473 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
474 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
475 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
476 if (stat(path_full, &statbuf) == 0)
479 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
480 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
481 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
482 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
483 if (stat(path_full, &statbuf) == 0)
488 return udev_device_new_from_syspath(udev, path_full);
491 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
493 struct udev_device *udev_device_parent = NULL;
494 char path[UTIL_PATH_SIZE];
497 /* follow "device" link in deprecated sys layout */
498 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
499 strncmp(udev_device->devpath, "/block/", 7) == 0) {
500 util_strlcpy(path, udev_device->syspath, sizeof(path));
501 util_strlcat(path, "/device", sizeof(path));
502 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
503 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
504 return udev_device_parent;
507 util_strlcpy(path, udev_device->syspath, sizeof(path));
508 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
512 pos = strrchr(subdir, '/');
513 if (pos == NULL || pos < &subdir[2])
516 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
517 if (udev_device_parent != NULL)
518 return udev_device_parent;
523 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
525 if (udev_device == NULL)
527 if (!udev_device->parent_set) {
528 udev_device->parent_set = 1;
529 udev_device->parent_device = device_new_from_parent(udev_device);
531 if (udev_device->parent_device != NULL)
532 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
533 return udev_device->parent_device;
536 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
538 struct udev_device *parent;
540 parent = udev_device_get_parent(udev_device);
541 while (parent != NULL) {
542 const char *parent_subsystem;
544 parent_subsystem = udev_device_get_subsystem(parent);
545 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
547 parent = udev_device_get_parent(parent);
553 * udev_device_get_udev:
554 * @udev_device: udev device
556 * Retrieve the udev library context the device was created with.
558 * Returns: the udev library context
560 struct udev *udev_device_get_udev(struct udev_device *udev_device)
562 if (udev_device == NULL)
564 return udev_device->udev;
569 * @udev_device: udev device
571 * Take a reference of a udev device.
573 * Returns: the passed udev device
575 struct udev_device *udev_device_ref(struct udev_device *udev_device)
577 if (udev_device == NULL)
579 udev_device->refcount++;
585 * @udev_device: udev device
587 * Drop a reference of a udev device. If the refcount reaches zero,
588 * the ressources of the device will be released.
591 void udev_device_unref(struct udev_device *udev_device)
595 if (udev_device == NULL)
597 udev_device->refcount--;
598 if (udev_device->refcount > 0)
600 if (udev_device->parent_device != NULL)
601 udev_device_unref(udev_device->parent_device);
602 free(udev_device->syspath);
603 free(udev_device->sysname);
604 free(udev_device->devnode);
605 free(udev_device->subsystem);
606 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
607 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
608 free(udev_device->action);
609 free(udev_device->driver);
610 free(udev_device->devpath_old);
611 free(udev_device->physdevpath);
612 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
613 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
614 free(udev_device->envp[i]);
615 info(udev_device->udev, "udev_device: %p released\n", udev_device);
620 * udev_device_get_devpath:
621 * @udev_device: udev device
623 * Retrieve the kernel devpath value of the udev device. The path
624 * does not contain the sys mount point, and starts with a '/'.
626 * Returns: the devpath of the udev device
628 const char *udev_device_get_devpath(struct udev_device *udev_device)
630 if (udev_device == NULL)
632 return udev_device->devpath;
636 * udev_device_get_syspath:
637 * @udev_device: udev device
639 * Retrieve the sys path of the udev device. The path is an
640 * absolute path and starts with the sys mount point.
642 * Returns: the sys path of the udev device
644 const char *udev_device_get_syspath(struct udev_device *udev_device)
646 if (udev_device == NULL)
648 return udev_device->syspath;
651 const char *udev_device_get_sysname(struct udev_device *udev_device)
653 if (udev_device == NULL)
655 return udev_device->sysname;
658 const char *udev_device_get_sysnum(struct udev_device *udev_device)
660 if (udev_device == NULL)
662 return udev_device->sysnum;
666 * udev_device_get_devnode:
667 * @udev_device: udev device
669 * Retrieve the device node file name belonging to the udev device.
670 * The path is an absolute path, and starts with the device directory.
672 * Returns: the device node file name of the udev device, or #NULL if no device node exists
674 const char *udev_device_get_devnode(struct udev_device *udev_device)
676 if (udev_device == NULL)
678 if (!udev_device->info_loaded)
679 device_load_info(udev_device);
680 return udev_device->devnode;
684 * udev_device_get_subsystem:
685 * @udev_device: udev device
687 * Retrieve the subsystem string of the udev device. The string does not
690 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
692 const char *udev_device_get_subsystem(struct udev_device *udev_device)
694 char subsystem[UTIL_NAME_SIZE];
696 if (udev_device == NULL)
698 if (!udev_device->subsystem_set) {
699 udev_device->subsystem_set = 1;
700 /* read "subsytem" link */
701 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
702 udev_device_set_subsystem(udev_device, subsystem);
703 return udev_device->subsystem;
706 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
707 udev_device_set_subsystem(udev_device, "module");
708 return udev_device->subsystem;
710 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
711 udev_device_set_subsystem(udev_device, "drivers");
712 return udev_device->subsystem;
714 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
715 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
716 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
717 udev_device_set_subsystem(udev_device, "subsystem");
718 return udev_device->subsystem;
721 return udev_device->subsystem;
725 * udev_device_get_devlinks_list_entry:
726 * @udev_device: udev device
728 * Retrieve the list of device links pointing to the device file of
729 * the udev device. The next list entry can be retrieved with
730 * udev_list_entry_next(), which returns #NULL if no more entries exist.
731 * The devlink path can be retrieved from the list entry by
732 * udev_list_entry_get_name(). The path is an absolute path, and starts with
733 * the device directory.
735 * Returns: the first entry of the device node link list
737 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
739 if (udev_device == NULL)
741 if (!udev_device->info_loaded)
742 device_load_info(udev_device);
743 return udev_list_get_entry(&udev_device->devlinks_list);
746 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
748 udev_device->devlinks_uptodate = 0;
749 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
753 * udev_device_get_properties_list_entry:
754 * @udev_device: udev device
756 * Retrieve the list of key/value device properties of the udev
757 * device. The next list entry can be retrieved with udev_list_entry_next(),
758 * which returns #NULL if no more entries exist. The property name
759 * can be retrieved from the list entry by udev_list_get_name(),
760 * the property value by udev_list_get_value().
762 * Returns: the first entry of the property list
764 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
766 if (udev_device == NULL)
768 if (!udev_device->info_loaded)
769 device_load_info(udev_device);
770 if (!udev_device->devlinks_uptodate) {
771 char symlinks[UTIL_PATH_SIZE];
772 struct udev_list_entry *list_entry;
774 udev_device->devlinks_uptodate = 1;
775 list_entry = udev_device_get_devlinks_list_entry(udev_device);
776 if (list_entry != NULL) {
777 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
778 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
779 util_strlcat(symlinks, " ", sizeof(symlinks));
780 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
782 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
785 return udev_list_get_entry(&udev_device->properties_list);
788 const char *udev_device_get_driver(struct udev_device *udev_device)
790 char driver[UTIL_NAME_SIZE];
792 if (udev_device == NULL)
794 if (!udev_device->driver_set) {
795 udev_device->driver_set = 1;
796 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
797 udev_device->driver = strdup(driver);
799 return udev_device->driver;
802 dev_t udev_device_get_devnum(struct udev_device *udev_device)
804 if (udev_device == NULL)
805 return makedev(0, 0);
806 if (!udev_device->info_loaded)
807 device_load_info(udev_device);
808 return udev_device->devnum;
811 const char *udev_device_get_action(struct udev_device *udev_device)
813 if (udev_device == NULL)
815 return udev_device->action;
818 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
820 if (udev_device == NULL)
822 return udev_device->seqnum;
825 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
827 struct udev_list_entry *list_entry;
828 char path[UTIL_PATH_SIZE];
829 char value[UTIL_NAME_SIZE];
833 const char *val = NULL;
835 if (udev_device == NULL)
840 /* look for possibly already cached result */
841 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
842 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
843 info(udev_device->udev, "got '%s' (%s) from cache\n",
844 sysattr, udev_list_entry_get_value(list_entry));
845 return udev_list_entry_get_value(list_entry);
849 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
850 util_strlcat(path, "/", sizeof(path));
851 util_strlcat(path, sysattr, sizeof(path));
853 if (lstat(path, &statbuf) != 0) {
854 info(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
855 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
859 if (S_ISLNK(statbuf.st_mode)) {
860 /* links return the last element of the target path */
861 char target[UTIL_NAME_SIZE];
865 len = readlink(path, target, sizeof(target));
868 pos = strrchr(target, '/');
871 info(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
872 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
873 val = udev_list_entry_get_value(list_entry);
879 /* skip directories */
880 if (S_ISDIR(statbuf.st_mode))
883 /* skip non-readable files */
884 if ((statbuf.st_mode & S_IRUSR) == 0)
887 /* read attribute value */
888 fd = open(path, O_RDONLY);
890 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
893 size = read(fd, value, sizeof(value));
897 if (size == sizeof(value))
900 /* got a valid value, store it in cache and return it */
902 util_remove_trailing_chars(value, '\n');
903 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
904 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
905 val = udev_list_entry_get_value(list_entry);
910 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
915 free(udev_device->syspath);
916 udev_device->syspath = strdup(syspath);
917 if (udev_device->syspath == NULL)
919 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
920 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
922 pos = strrchr(udev_device->syspath, '/');
925 udev_device->sysname = strdup(&pos[1]);
926 if (udev_device->sysname == NULL)
929 /* some devices have '!' in their name, change that to '/' */
931 while (udev_device->sysname[len] != '\0') {
932 if (udev_device->sysname[len] == '!')
933 udev_device->sysname[len] = '/';
937 /* trailing number */
938 while (len > 0 && isdigit(udev_device->sysname[--len]))
939 udev_device->sysnum = &udev_device->sysname[len];
941 /* sysname is completely numeric */
943 udev_device->sysnum = NULL;
948 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
950 free(udev_device->subsystem);
951 udev_device->subsystem = strdup(subsystem);
952 if (udev_device->subsystem == NULL)
954 udev_device->subsystem_set = 1;
955 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
959 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
961 free(udev_device->devnode);
962 udev_device->devnode = strdup(devnode);
965 if (udev_device->devnode == NULL)
967 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
971 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
973 udev_device->devlinks_uptodate = 0;
974 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
979 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
981 udev_device->envp_uptodate = 0;
983 struct udev_list_entry *list_entry;
985 list_entry = udev_device_get_properties_list_entry(udev_device);
986 list_entry = udev_list_entry_get_by_name(list_entry, key);
987 if (list_entry != NULL)
988 udev_list_entry_remove(list_entry);
991 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
994 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
996 char name[UTIL_PATH_SIZE];
999 strncpy(name, property, sizeof(name));
1000 val = strchr(name, '=');
1007 return udev_device_add_property(udev_device, name, val);
1010 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1012 if (!udev_device->envp_uptodate) {
1014 struct udev_list_entry *list_entry;
1016 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
1017 free(udev_device->envp[i]);
1019 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1020 asprintf(&udev_device->envp[i++], "%s=%s",
1021 udev_list_entry_get_name(list_entry),
1022 udev_list_entry_get_value(list_entry));
1023 if (i+1 >= ARRAY_SIZE(udev_device->envp))
1026 udev_device->envp[i] = NULL;
1027 info(udev_device->udev, "constructed envp from %u properties\n", i);
1028 udev_device->envp_uptodate = 1;
1030 return udev_device->envp;
1033 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1035 free(udev_device->action);
1036 udev_device->action = strdup(action);
1037 if (udev_device->action == NULL)
1039 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1043 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1045 free(udev_device->driver);
1046 udev_device->driver = strdup(driver);
1047 if (udev_device->driver == NULL)
1049 udev_device->driver_set = 1;
1050 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1054 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1056 return udev_device->devpath_old;
1059 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1061 udev_device->devpath_old = strdup(devpath_old);
1062 if (udev_device->devpath_old == NULL)
1064 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1068 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1070 return udev_device->physdevpath;
1073 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1075 udev_device->physdevpath = strdup(physdevpath);
1076 if (udev_device->physdevpath == NULL)
1081 int udev_device_get_timeout(struct udev_device *udev_device)
1083 return udev_device->timeout;
1086 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1088 udev_device->timeout = timeout;
1091 int udev_device_get_event_timeout(struct udev_device *udev_device)
1093 if (!udev_device->info_loaded)
1094 device_load_info(udev_device);
1095 return udev_device->event_timeout;
1098 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1100 udev_device->event_timeout = event_timeout;
1104 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1108 udev_device->seqnum = seqnum;
1109 snprintf(num, sizeof(num), "%llu", seqnum);
1110 udev_device_add_property(udev_device, "SEQNUM", num);
1114 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1118 udev_device->devnum = devnum;
1120 snprintf(num, sizeof(num), "%u", major(devnum));
1121 udev_device_add_property(udev_device, "MAJOR", num);
1122 snprintf(num, sizeof(num), "%u", minor(devnum));
1123 udev_device_add_property(udev_device, "MINOR", num);
1127 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1129 if (!udev_device->info_loaded)
1130 device_load_info(udev_device);
1131 return udev_device->num_fake_partitions;
1134 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1136 udev_device->num_fake_partitions = num;
1140 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1142 if (!udev_device->info_loaded)
1143 device_load_info(udev_device);
1144 return udev_device->devlink_priority;
1147 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1149 udev_device->devlink_priority = prio;
1153 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1155 if (!udev_device->info_loaded)
1156 device_load_info(udev_device);
1157 return udev_device->ignore_remove;
1160 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1162 udev_device->ignore_remove = ignore;