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 = malloc(sizeof(struct udev_device));
240 if (udev_device == NULL)
242 memset(udev_device, 0x00, sizeof(struct udev_device));
243 udev_device->refcount = 1;
244 udev_device->udev = udev;
245 udev_list_init(&udev_device->devlinks_list);
246 udev_list_init(&udev_device->properties_list);
247 udev_list_init(&udev_device->sysattr_list);
248 udev_device->event_timeout = -1;
249 /* copy global properties */
250 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
251 udev_device_add_property(udev_device,
252 udev_list_entry_get_name(list_entry),
253 udev_list_entry_get_value(list_entry));
254 info(udev_device->udev, "udev_device: %p created\n", udev_device);
259 * udev_device_new_from_syspath:
260 * @udev: udev library context
261 * @syspath: sys device path including sys directory
263 * Create new udev device, and fill in information from the sys
264 * device and the udev database entry. The sypath is the absolute
265 * path to the device, including the sys mount point.
267 * The initial refcount is 1, and needs to be decremented to
268 * release the ressources of the udev device.
270 * Returns: a new udev device, or #NULL, if it does not exist
272 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
276 char path[UTIL_PATH_SIZE];
279 struct udev_device *udev_device;
286 /* path starts in sys */
287 len = strlen(udev_get_sys_path(udev));
288 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
289 info(udev, "not in sys :%s\n", syspath);
293 /* path is not a root directory */
294 subdir = &syspath[len+1];
295 pos = strrchr(subdir, '/');
296 if (pos == NULL || pos < &subdir[2]) {
297 info(udev, "not a subdir :%s\n", syspath);
301 /* resolve possible symlink to real path */
302 util_strlcpy(path, syspath, sizeof(path));
303 util_resolve_sys_link(udev, path, sizeof(path));
305 /* try to resolve the silly block layout if needed */
306 if (strncmp(&path[len], "/block/", 7) == 0) {
307 char block[UTIL_PATH_SIZE];
308 char part[UTIL_PATH_SIZE];
310 util_strlcpy(block, path, sizeof(block));
311 pos = strrchr(block, '/');
314 util_strlcpy(part, pos, sizeof(part));
316 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
317 util_strlcpy(path, block, sizeof(path));
318 util_strlcat(path, part, sizeof(path));
322 /* path exists in sys */
323 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
324 strncmp(&syspath[len], "/class/", 7) == 0 ||
325 strncmp(&syspath[len], "/block/", 7) == 0) {
326 char file[UTIL_PATH_SIZE];
328 /* all "devices" require a "uevent" file */
329 util_strlcpy(file, path, sizeof(file));
330 util_strlcat(file, "/uevent", sizeof(file));
331 if (stat(file, &statbuf) != 0) {
332 info(udev, "not a device: %s\n", syspath);
336 /* everything else just needs to be a directory */
337 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
338 info(udev, "directory not found: %s\n", syspath);
343 udev_device = device_new(udev);
344 if (udev_device == NULL)
347 udev_device_set_syspath(udev_device, path);
348 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
353 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
355 char path[UTIL_PATH_SIZE];
356 const char *type_str;
357 struct udev_enumerate *udev_enumerate;
358 struct udev_list_entry *list_entry;
359 struct udev_device *device = NULL;
363 else if (type == 'c')
368 /* /sys/dev/{block,char}/<maj>:<min> link */
369 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
370 type_str, major(devnum), minor(devnum));
371 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
372 return udev_device_new_from_syspath(udev, path);
374 udev_enumerate = udev_enumerate_new(udev);
375 if (udev_enumerate == NULL)
378 /* fallback to search sys devices for the major/minor */
380 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
381 else if (type == 'c')
382 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
383 udev_enumerate_scan_devices(udev_enumerate);
384 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
385 struct udev_device *device_loop;
387 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
388 if (device_loop != NULL) {
389 if (udev_device_get_devnum(device_loop) == devnum) {
390 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
392 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
394 device = device_loop;
397 udev_device_unref(device_loop);
400 udev_enumerate_unref(udev_enumerate);
404 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
407 char path_full[UTIL_PATH_SIZE];
411 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
412 path = &path_full[sys_path_len];
414 if (strcmp(subsystem, "subsystem") == 0) {
415 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
416 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
417 if (stat(path_full, &statbuf) == 0)
420 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
421 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
422 if (stat(path_full, &statbuf) == 0)
425 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
426 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
427 if (stat(path_full, &statbuf) == 0)
432 if (strcmp(subsystem, "module") == 0) {
433 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
434 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
435 if (stat(path_full, &statbuf) == 0)
440 if (strcmp(subsystem, "drivers") == 0) {
441 char subsys[UTIL_NAME_SIZE];
444 util_strlcpy(subsys, sysname, sizeof(subsys));
445 driver = strchr(subsys, ':');
446 if (driver != NULL) {
449 util_strlcpy(path, "/subsystem/", 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)
456 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
457 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
458 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
459 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
460 if (stat(path_full, &statbuf) == 0)
466 util_strlcpy(path, "/subsystem/", 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, "/bus/", sizeof(path_full) - sys_path_len);
474 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
475 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
476 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
477 if (stat(path_full, &statbuf) == 0)
480 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
481 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
482 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
483 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
484 if (stat(path_full, &statbuf) == 0)
489 return udev_device_new_from_syspath(udev, path_full);
492 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
494 struct udev_device *udev_device_parent = NULL;
495 char path[UTIL_PATH_SIZE];
498 /* follow "device" link in deprecated sys layout */
499 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
500 strncmp(udev_device->devpath, "/block/", 7) == 0) {
501 util_strlcpy(path, udev_device->syspath, sizeof(path));
502 util_strlcat(path, "/device", sizeof(path));
503 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
504 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
505 return udev_device_parent;
508 util_strlcpy(path, udev_device->syspath, sizeof(path));
509 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
513 pos = strrchr(subdir, '/');
514 if (pos == NULL || pos < &subdir[2])
517 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
518 if (udev_device_parent != NULL)
519 return udev_device_parent;
524 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
526 if (udev_device == NULL)
528 if (!udev_device->parent_set) {
529 udev_device->parent_set = 1;
530 udev_device->parent_device = device_new_from_parent(udev_device);
532 if (udev_device->parent_device != NULL)
533 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
534 return udev_device->parent_device;
537 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
539 struct udev_device *parent;
541 parent = udev_device_get_parent(udev_device);
542 while (parent != NULL) {
543 const char *parent_subsystem;
545 parent_subsystem = udev_device_get_subsystem(parent);
546 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
548 parent = udev_device_get_parent(parent);
554 * udev_device_get_udev:
555 * @udev_device: udev device
557 * Retrieve the udev library context the device was created with.
559 * Returns: the udev library context
561 struct udev *udev_device_get_udev(struct udev_device *udev_device)
563 if (udev_device == NULL)
565 return udev_device->udev;
570 * @udev_device: udev device
572 * Take a reference of a udev device.
574 * Returns: the passed udev device
576 struct udev_device *udev_device_ref(struct udev_device *udev_device)
578 if (udev_device == NULL)
580 udev_device->refcount++;
586 * @udev_device: udev device
588 * Drop a reference of a udev device. If the refcount reaches zero,
589 * the ressources of the device will be released.
592 void udev_device_unref(struct udev_device *udev_device)
596 if (udev_device == NULL)
598 udev_device->refcount--;
599 if (udev_device->refcount > 0)
601 if (udev_device->parent_device != NULL)
602 udev_device_unref(udev_device->parent_device);
603 free(udev_device->syspath);
604 free(udev_device->sysname);
605 free(udev_device->devnode);
606 free(udev_device->subsystem);
607 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
608 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
609 free(udev_device->action);
610 free(udev_device->driver);
611 free(udev_device->devpath_old);
612 free(udev_device->physdevpath);
613 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
614 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
615 free(udev_device->envp[i]);
616 info(udev_device->udev, "udev_device: %p released\n", udev_device);
621 * udev_device_get_devpath:
622 * @udev_device: udev device
624 * Retrieve the kernel devpath value of the udev device. The path
625 * does not contain the sys mount point, and starts with a '/'.
627 * Returns: the devpath of the udev device
629 const char *udev_device_get_devpath(struct udev_device *udev_device)
631 if (udev_device == NULL)
633 return udev_device->devpath;
637 * udev_device_get_syspath:
638 * @udev_device: udev device
640 * Retrieve the sys path of the udev device. The path is an
641 * absolute path and starts with the sys mount point.
643 * Returns: the sys path of the udev device
645 const char *udev_device_get_syspath(struct udev_device *udev_device)
647 if (udev_device == NULL)
649 return udev_device->syspath;
652 const char *udev_device_get_sysname(struct udev_device *udev_device)
654 if (udev_device == NULL)
656 return udev_device->sysname;
659 const char *udev_device_get_sysnum(struct udev_device *udev_device)
661 if (udev_device == NULL)
663 return udev_device->sysnum;
667 * udev_device_get_devnode:
668 * @udev_device: udev device
670 * Retrieve the device node file name belonging to the udev device.
671 * The path is an absolute path, and starts with the device directory.
673 * Returns: the device node file name of the udev device, or #NULL if no device node exists
675 const char *udev_device_get_devnode(struct udev_device *udev_device)
677 if (udev_device == NULL)
679 if (!udev_device->info_loaded)
680 device_load_info(udev_device);
681 return udev_device->devnode;
685 * udev_device_get_subsystem:
686 * @udev_device: udev device
688 * Retrieve the subsystem string of the udev device. The string does not
691 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
693 const char *udev_device_get_subsystem(struct udev_device *udev_device)
695 char subsystem[UTIL_NAME_SIZE];
697 if (udev_device == NULL)
699 if (!udev_device->subsystem_set) {
700 udev_device->subsystem_set = 1;
701 /* read "subsytem" link */
702 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
703 udev_device_set_subsystem(udev_device, subsystem);
704 return udev_device->subsystem;
707 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
708 udev_device_set_subsystem(udev_device, "module");
709 return udev_device->subsystem;
711 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
712 udev_device_set_subsystem(udev_device, "drivers");
713 return udev_device->subsystem;
715 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
716 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
717 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
718 udev_device_set_subsystem(udev_device, "subsystem");
719 return udev_device->subsystem;
722 return udev_device->subsystem;
726 * udev_device_get_devlinks_list_entry:
727 * @udev_device: udev device
729 * Retrieve the list of device links pointing to the device file of
730 * the udev device. The next list entry can be retrieved with
731 * udev_list_entry_next(), which returns #NULL if no more entries exist.
732 * The devlink path can be retrieved from the list entry by
733 * udev_list_entry_get_name(). The path is an absolute path, and starts with
734 * the device directory.
736 * Returns: the first entry of the device node link list
738 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
740 if (udev_device == NULL)
742 if (!udev_device->info_loaded)
743 device_load_info(udev_device);
744 return udev_list_get_entry(&udev_device->devlinks_list);
747 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
749 udev_device->devlinks_uptodate = 0;
750 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
754 * udev_device_get_properties_list_entry:
755 * @udev_device: udev device
757 * Retrieve the list of key/value device properties of the udev
758 * device. The next list entry can be retrieved with udev_list_entry_next(),
759 * which returns #NULL if no more entries exist. The property name
760 * can be retrieved from the list entry by udev_list_get_name(),
761 * the property value by udev_list_get_value().
763 * Returns: the first entry of the property list
765 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
767 if (udev_device == NULL)
769 if (!udev_device->info_loaded)
770 device_load_info(udev_device);
771 if (!udev_device->devlinks_uptodate) {
772 char symlinks[UTIL_PATH_SIZE];
773 struct udev_list_entry *list_entry;
775 udev_device->devlinks_uptodate = 1;
776 list_entry = udev_device_get_devlinks_list_entry(udev_device);
777 if (list_entry != NULL) {
778 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
779 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
780 util_strlcat(symlinks, " ", sizeof(symlinks));
781 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
783 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
786 return udev_list_get_entry(&udev_device->properties_list);
789 const char *udev_device_get_driver(struct udev_device *udev_device)
791 char driver[UTIL_NAME_SIZE];
793 if (udev_device == NULL)
795 if (!udev_device->driver_set) {
796 udev_device->driver_set = 1;
797 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
798 udev_device->driver = strdup(driver);
800 return udev_device->driver;
803 dev_t udev_device_get_devnum(struct udev_device *udev_device)
805 if (udev_device == NULL)
806 return makedev(0, 0);
807 if (!udev_device->info_loaded)
808 device_load_info(udev_device);
809 return udev_device->devnum;
812 const char *udev_device_get_action(struct udev_device *udev_device)
814 if (udev_device == NULL)
816 return udev_device->action;
819 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
821 if (udev_device == NULL)
823 return udev_device->seqnum;
826 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
828 struct udev_list_entry *list_entry;
829 char path[UTIL_PATH_SIZE];
830 char value[UTIL_NAME_SIZE];
834 const char *val = NULL;
836 if (udev_device == NULL)
841 /* look for possibly already cached result */
842 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
843 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
844 info(udev_device->udev, "got '%s' (%s) from cache\n",
845 sysattr, udev_list_entry_get_value(list_entry));
846 return udev_list_entry_get_value(list_entry);
850 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
851 util_strlcat(path, "/", sizeof(path));
852 util_strlcat(path, sysattr, sizeof(path));
854 if (lstat(path, &statbuf) != 0) {
855 info(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
856 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
860 if (S_ISLNK(statbuf.st_mode)) {
861 /* links return the last element of the target path */
862 char target[UTIL_NAME_SIZE];
866 len = readlink(path, target, sizeof(target));
869 pos = strrchr(target, '/');
872 info(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
873 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
874 val = udev_list_entry_get_value(list_entry);
880 /* skip directories */
881 if (S_ISDIR(statbuf.st_mode))
884 /* skip non-readable files */
885 if ((statbuf.st_mode & S_IRUSR) == 0)
888 /* read attribute value */
889 fd = open(path, O_RDONLY);
891 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
894 size = read(fd, value, sizeof(value));
898 if (size == sizeof(value))
901 /* got a valid value, store it in cache and return it */
903 util_remove_trailing_chars(value, '\n');
904 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
905 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
906 val = udev_list_entry_get_value(list_entry);
911 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
916 free(udev_device->syspath);
917 udev_device->syspath = strdup(syspath);
918 if (udev_device->syspath == NULL)
920 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
921 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
923 pos = strrchr(udev_device->syspath, '/');
926 udev_device->sysname = strdup(&pos[1]);
927 if (udev_device->sysname == NULL)
930 /* some devices have '!' in their name, change that to '/' */
932 while (udev_device->sysname[len] != '\0') {
933 if (udev_device->sysname[len] == '!')
934 udev_device->sysname[len] = '/';
938 /* trailing number */
939 while (len > 0 && isdigit(udev_device->sysname[--len]))
940 udev_device->sysnum = &udev_device->sysname[len];
942 /* sysname is completely numeric */
944 udev_device->sysnum = NULL;
949 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
951 free(udev_device->subsystem);
952 udev_device->subsystem = strdup(subsystem);
953 if (udev_device->subsystem == NULL)
955 udev_device->subsystem_set = 1;
956 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
960 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
962 free(udev_device->devnode);
963 udev_device->devnode = strdup(devnode);
966 if (udev_device->devnode == NULL)
968 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
972 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
974 udev_device->devlinks_uptodate = 0;
975 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
980 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
982 udev_device->envp_uptodate = 0;
984 struct udev_list_entry *list_entry;
986 list_entry = udev_device_get_properties_list_entry(udev_device);
987 list_entry = udev_list_entry_get_by_name(list_entry, key);
988 if (list_entry != NULL)
989 udev_list_entry_remove(list_entry);
992 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
995 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
997 char name[UTIL_PATH_SIZE];
1000 strncpy(name, property, sizeof(name));
1001 val = strchr(name, '=');
1008 return udev_device_add_property(udev_device, name, val);
1011 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1013 if (!udev_device->envp_uptodate) {
1015 struct udev_list_entry *list_entry;
1017 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
1018 free(udev_device->envp[i]);
1020 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1021 asprintf(&udev_device->envp[i++], "%s=%s",
1022 udev_list_entry_get_name(list_entry),
1023 udev_list_entry_get_value(list_entry));
1024 if (i+1 >= ARRAY_SIZE(udev_device->envp))
1027 udev_device->envp[i] = NULL;
1028 info(udev_device->udev, "constructed envp from %u properties\n", i);
1029 udev_device->envp_uptodate = 1;
1031 return udev_device->envp;
1034 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1036 free(udev_device->action);
1037 udev_device->action = strdup(action);
1038 if (udev_device->action == NULL)
1040 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1044 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1046 free(udev_device->driver);
1047 udev_device->driver = strdup(driver);
1048 if (udev_device->driver == NULL)
1050 udev_device->driver_set = 1;
1051 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1055 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1057 return udev_device->devpath_old;
1060 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1062 udev_device->devpath_old = strdup(devpath_old);
1063 if (udev_device->devpath_old == NULL)
1065 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1069 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1071 return udev_device->physdevpath;
1074 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1076 udev_device->physdevpath = strdup(physdevpath);
1077 if (udev_device->physdevpath == NULL)
1082 int udev_device_get_timeout(struct udev_device *udev_device)
1084 return udev_device->timeout;
1087 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1089 udev_device->timeout = timeout;
1092 int udev_device_get_event_timeout(struct udev_device *udev_device)
1094 if (!udev_device->info_loaded)
1095 device_load_info(udev_device);
1096 return udev_device->event_timeout;
1099 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1101 udev_device->event_timeout = event_timeout;
1105 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1109 udev_device->seqnum = seqnum;
1110 snprintf(num, sizeof(num), "%llu", seqnum);
1111 udev_device_add_property(udev_device, "SEQNUM", num);
1115 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1119 udev_device->devnum = devnum;
1121 snprintf(num, sizeof(num), "%u", major(devnum));
1122 udev_device_add_property(udev_device, "MAJOR", num);
1123 snprintf(num, sizeof(num), "%u", minor(devnum));
1124 udev_device_add_property(udev_device, "MINOR", num);
1128 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1130 if (!udev_device->info_loaded)
1131 device_load_info(udev_device);
1132 return udev_device->num_fake_partitions;
1135 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1137 udev_device->num_fake_partitions = num;
1141 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1143 if (!udev_device->info_loaded)
1144 device_load_info(udev_device);
1145 return udev_device->devlink_priority;
1148 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1150 udev_device->devlink_priority = prio;
1154 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1156 if (!udev_device->info_loaded)
1157 device_load_info(udev_device);
1158 return udev_device->ignore_remove;
1161 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1163 udev_device->ignore_remove = ignore;