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"
39 struct udev_device *parent_device;
48 struct udev_list_node devlinks_list;
49 int devlinks_uptodate;
50 struct udev_list_node properties_list;
61 unsigned long long int seqnum;
62 int num_fake_partitions;
65 struct udev_list_node sysattr_list;
69 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
73 /* translate to location of db file */
74 util_strlcpy(filename, udev_get_dev_path(udev), len);
75 start = util_strlcat(filename, "/.udev/db/", len);
76 util_strlcat(filename, devpath, len);
77 return util_path_encode(&filename[start], len - start);
80 int udev_device_read_db(struct udev_device *udev_device)
83 char filename[UTIL_PATH_SIZE];
84 char line[UTIL_LINE_SIZE];
87 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
89 if (lstat(filename, &stats) != 0) {
90 info(udev_device->udev, "no db file to read %s: %m\n", filename);
93 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
94 char target[UTIL_PATH_SIZE];
95 char devnode[UTIL_PATH_SIZE];
99 target_len = readlink(filename, target, sizeof(target));
101 target[target_len] = '\0';
103 info(udev_device->udev, "error reading db link %s: %m\n", filename);
107 next = strchr(target, ' ');
112 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
113 util_strlcat(devnode, "/", sizeof(devnode));
114 util_strlcat(devnode, target, sizeof(devnode));
115 udev_device_set_devnode(udev_device, devnode);
116 while (next != NULL) {
117 char devlink[UTIL_PATH_SIZE];
121 next = strchr(next, ' ');
126 util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
127 util_strlcat(devlink, "/", sizeof(devlink));
128 util_strlcat(devlink, lnk, sizeof(devlink));
129 udev_device_add_devlink(udev_device, devlink);
131 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
135 f = fopen(filename, "r");
137 info(udev_device->udev, "error reading db file %s: %m\n", filename);
140 while (fgets(line, sizeof(line), f)) {
151 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
152 util_strlcat(filename, "/", sizeof(filename));
153 util_strlcat(filename, val, sizeof(filename));
154 udev_device_set_devnode(udev_device, filename);
157 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
158 util_strlcat(filename, "/", sizeof(filename));
159 util_strlcat(filename, val, sizeof(filename));
160 udev_device_add_devlink(udev_device, filename);
163 udev_device_set_devlink_priority(udev_device, atoi(val));
166 udev_device_set_event_timeout(udev_device, atoi(val));
169 udev_device_set_num_fake_partitions(udev_device, atoi(val));
172 udev_device_set_ignore_remove(udev_device, atoi(val));
175 udev_device_add_property_from_string(udev_device, val);
181 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
185 int udev_device_read_uevent_file(struct udev_device *udev_device)
187 char filename[UTIL_PATH_SIZE];
189 char line[UTIL_LINE_SIZE];
193 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
194 util_strlcat(filename, "/uevent", sizeof(filename));
195 f = fopen(filename, "r");
199 while (fgets(line, sizeof(line), f)) {
202 pos = strchr(line, '\n');
207 if (strncmp(line, "MAJOR=", 6) == 0)
208 maj = strtoull(&line[6], NULL, 10);
209 else if (strncmp(line, "MINOR=", 6) == 0)
210 min = strtoull(&line[6], NULL, 10);
212 udev_device_add_property_from_string(udev_device, line);
215 udev_device->devnum = makedev(maj, min);
221 static void device_load_info(struct udev_device *device)
223 device->info_loaded = 1;
224 udev_device_read_uevent_file(device);
225 udev_device_read_db(device);
228 void udev_device_set_info_loaded(struct udev_device *device)
230 device->info_loaded = 1;
233 struct udev_device *device_new(struct udev *udev)
235 struct udev_device *udev_device;
236 struct udev_list_entry *list_entry;
241 udev_device = calloc(1, sizeof(struct udev_device));
242 if (udev_device == NULL)
244 udev_device->refcount = 1;
245 udev_device->udev = udev;
246 udev_list_init(&udev_device->devlinks_list);
247 udev_list_init(&udev_device->properties_list);
248 udev_list_init(&udev_device->sysattr_list);
249 udev_device->event_timeout = -1;
250 /* copy global properties */
251 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
252 udev_device_add_property(udev_device,
253 udev_list_entry_get_name(list_entry),
254 udev_list_entry_get_value(list_entry));
255 info(udev_device->udev, "udev_device: %p created\n", udev_device);
260 * udev_device_new_from_syspath:
261 * @udev: udev library context
262 * @syspath: sys device path including sys directory
264 * Create new udev device, and fill in information from the sys
265 * device and the udev database entry. The sypath is the absolute
266 * path to the device, including the sys mount point.
268 * The initial refcount is 1, and needs to be decremented to
269 * release the ressources of the udev device.
271 * Returns: a new udev device, or #NULL, if it does not exist
273 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
277 char path[UTIL_PATH_SIZE];
280 struct udev_device *udev_device;
287 /* path starts in sys */
288 len = strlen(udev_get_sys_path(udev));
289 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
290 info(udev, "not in sys :%s\n", syspath);
294 /* path is not a root directory */
295 subdir = &syspath[len+1];
296 pos = strrchr(subdir, '/');
297 if (pos == NULL || pos < &subdir[2]) {
298 info(udev, "not a subdir :%s\n", syspath);
302 /* resolve possible symlink to real path */
303 util_strlcpy(path, syspath, sizeof(path));
304 util_resolve_sys_link(udev, path, sizeof(path));
306 /* try to resolve the silly block layout if needed */
307 if (strncmp(&path[len], "/block/", 7) == 0) {
308 char block[UTIL_PATH_SIZE];
309 char part[UTIL_PATH_SIZE];
311 util_strlcpy(block, path, sizeof(block));
312 pos = strrchr(block, '/');
315 util_strlcpy(part, pos, sizeof(part));
317 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
318 util_strlcpy(path, block, sizeof(path));
319 util_strlcat(path, part, sizeof(path));
323 /* path exists in sys */
324 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
325 strncmp(&syspath[len], "/class/", 7) == 0 ||
326 strncmp(&syspath[len], "/block/", 7) == 0) {
327 char file[UTIL_PATH_SIZE];
329 /* all "devices" require a "uevent" file */
330 util_strlcpy(file, path, sizeof(file));
331 util_strlcat(file, "/uevent", sizeof(file));
332 if (stat(file, &statbuf) != 0) {
333 info(udev, "not a device: %s\n", syspath);
337 /* everything else just needs to be a directory */
338 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
339 info(udev, "directory not found: %s\n", syspath);
344 udev_device = device_new(udev);
345 if (udev_device == NULL)
348 udev_device_set_syspath(udev_device, path);
349 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
354 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
356 char path[UTIL_PATH_SIZE];
357 const char *type_str;
358 struct udev_enumerate *udev_enumerate;
359 struct udev_list_entry *list_entry;
360 struct udev_device *device = NULL;
364 else if (type == 'c')
369 /* /sys/dev/{block,char}/<maj>:<min> link */
370 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
371 type_str, major(devnum), minor(devnum));
372 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
373 return udev_device_new_from_syspath(udev, path);
375 udev_enumerate = udev_enumerate_new(udev);
376 if (udev_enumerate == NULL)
379 /* fallback to search sys devices for the major/minor */
381 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
382 else if (type == 'c')
383 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
384 udev_enumerate_scan_devices(udev_enumerate);
385 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
386 struct udev_device *device_loop;
388 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
389 if (device_loop != NULL) {
390 if (udev_device_get_devnum(device_loop) == devnum) {
391 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
393 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
395 device = device_loop;
398 udev_device_unref(device_loop);
401 udev_enumerate_unref(udev_enumerate);
405 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
408 char path_full[UTIL_PATH_SIZE];
412 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
413 path = &path_full[sys_path_len];
415 if (strcmp(subsystem, "subsystem") == 0) {
416 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
417 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
418 if (stat(path_full, &statbuf) == 0)
421 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
422 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
423 if (stat(path_full, &statbuf) == 0)
426 util_strlcpy(path, "/class/", 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, "module") == 0) {
434 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
435 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
436 if (stat(path_full, &statbuf) == 0)
441 if (strcmp(subsystem, "drivers") == 0) {
442 char subsys[UTIL_NAME_SIZE];
445 util_strlcpy(subsys, sysname, sizeof(subsys));
446 driver = strchr(subsys, ':');
447 if (driver != NULL) {
450 util_strlcpy(path, "/subsystem/", 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)
457 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
458 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
459 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
460 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
461 if (stat(path_full, &statbuf) == 0)
467 util_strlcpy(path, "/subsystem/", 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, "/bus/", sizeof(path_full) - sys_path_len);
475 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
476 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
477 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
478 if (stat(path_full, &statbuf) == 0)
481 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
482 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
483 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
484 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
485 if (stat(path_full, &statbuf) == 0)
490 return udev_device_new_from_syspath(udev, path_full);
493 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
495 struct udev_device *udev_device_parent = NULL;
496 char path[UTIL_PATH_SIZE];
499 /* follow "device" link in deprecated sys layout */
500 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
501 strncmp(udev_device->devpath, "/block/", 7) == 0) {
502 util_strlcpy(path, udev_device->syspath, sizeof(path));
503 util_strlcat(path, "/device", sizeof(path));
504 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
505 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
506 return udev_device_parent;
509 util_strlcpy(path, udev_device->syspath, sizeof(path));
510 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
514 pos = strrchr(subdir, '/');
515 if (pos == NULL || pos < &subdir[2])
518 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
519 if (udev_device_parent != NULL)
520 return udev_device_parent;
525 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
527 if (udev_device == NULL)
529 if (!udev_device->parent_set) {
530 udev_device->parent_set = 1;
531 udev_device->parent_device = device_new_from_parent(udev_device);
533 if (udev_device->parent_device != NULL)
534 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
535 return udev_device->parent_device;
538 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
540 struct udev_device *parent;
542 parent = udev_device_get_parent(udev_device);
543 while (parent != NULL) {
544 const char *parent_subsystem;
546 parent_subsystem = udev_device_get_subsystem(parent);
547 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
549 parent = udev_device_get_parent(parent);
555 * udev_device_get_udev:
556 * @udev_device: udev device
558 * Retrieve the udev library context the device was created with.
560 * Returns: the udev library context
562 struct udev *udev_device_get_udev(struct udev_device *udev_device)
564 if (udev_device == NULL)
566 return udev_device->udev;
571 * @udev_device: udev device
573 * Take a reference of a udev device.
575 * Returns: the passed udev device
577 struct udev_device *udev_device_ref(struct udev_device *udev_device)
579 if (udev_device == NULL)
581 udev_device->refcount++;
587 * @udev_device: udev device
589 * Drop a reference of a udev device. If the refcount reaches zero,
590 * the ressources of the device will be released.
593 void udev_device_unref(struct udev_device *udev_device)
597 if (udev_device == NULL)
599 udev_device->refcount--;
600 if (udev_device->refcount > 0)
602 if (udev_device->parent_device != NULL)
603 udev_device_unref(udev_device->parent_device);
604 free(udev_device->syspath);
605 free(udev_device->sysname);
606 free(udev_device->devnode);
607 free(udev_device->subsystem);
608 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
609 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
610 free(udev_device->action);
611 free(udev_device->driver);
612 free(udev_device->devpath_old);
613 free(udev_device->physdevpath);
614 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
615 if (udev_device->envp != NULL) {
616 for (i = 0; i < ENVP_SIZE && udev_device->envp[i] != NULL; i++)
617 free(udev_device->envp[i]);
618 free(udev_device->envp);
620 info(udev_device->udev, "udev_device: %p released\n", udev_device);
625 * udev_device_get_devpath:
626 * @udev_device: udev device
628 * Retrieve the kernel devpath value of the udev device. The path
629 * does not contain the sys mount point, and starts with a '/'.
631 * Returns: the devpath of the udev device
633 const char *udev_device_get_devpath(struct udev_device *udev_device)
635 if (udev_device == NULL)
637 return udev_device->devpath;
641 * udev_device_get_syspath:
642 * @udev_device: udev device
644 * Retrieve the sys path of the udev device. The path is an
645 * absolute path and starts with the sys mount point.
647 * Returns: the sys path of the udev device
649 const char *udev_device_get_syspath(struct udev_device *udev_device)
651 if (udev_device == NULL)
653 return udev_device->syspath;
656 const char *udev_device_get_sysname(struct udev_device *udev_device)
658 if (udev_device == NULL)
660 return udev_device->sysname;
663 const char *udev_device_get_sysnum(struct udev_device *udev_device)
665 if (udev_device == NULL)
667 return udev_device->sysnum;
671 * udev_device_get_devnode:
672 * @udev_device: udev device
674 * Retrieve the device node file name belonging to the udev device.
675 * The path is an absolute path, and starts with the device directory.
677 * Returns: the device node file name of the udev device, or #NULL if no device node exists
679 const char *udev_device_get_devnode(struct udev_device *udev_device)
681 if (udev_device == NULL)
683 if (!udev_device->info_loaded)
684 device_load_info(udev_device);
685 return udev_device->devnode;
689 * udev_device_get_subsystem:
690 * @udev_device: udev device
692 * Retrieve the subsystem string of the udev device. The string does not
695 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
697 const char *udev_device_get_subsystem(struct udev_device *udev_device)
699 char subsystem[UTIL_NAME_SIZE];
701 if (udev_device == NULL)
703 if (!udev_device->subsystem_set) {
704 udev_device->subsystem_set = 1;
705 /* read "subsytem" link */
706 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
707 udev_device_set_subsystem(udev_device, subsystem);
708 return udev_device->subsystem;
711 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
712 udev_device_set_subsystem(udev_device, "module");
713 return udev_device->subsystem;
715 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
716 udev_device_set_subsystem(udev_device, "drivers");
717 return udev_device->subsystem;
719 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
720 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
721 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
722 udev_device_set_subsystem(udev_device, "subsystem");
723 return udev_device->subsystem;
726 return udev_device->subsystem;
730 * udev_device_get_devlinks_list_entry:
731 * @udev_device: udev device
733 * Retrieve the list of device links pointing to the device file of
734 * the udev device. The next list entry can be retrieved with
735 * udev_list_entry_next(), which returns #NULL if no more entries exist.
736 * The devlink path can be retrieved from the list entry by
737 * udev_list_entry_get_name(). The path is an absolute path, and starts with
738 * the device directory.
740 * Returns: the first entry of the device node link list
742 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
744 if (udev_device == NULL)
746 if (!udev_device->info_loaded)
747 device_load_info(udev_device);
748 return udev_list_get_entry(&udev_device->devlinks_list);
751 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
753 udev_device->devlinks_uptodate = 0;
754 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
758 * udev_device_get_properties_list_entry:
759 * @udev_device: udev device
761 * Retrieve the list of key/value device properties of the udev
762 * device. The next list entry can be retrieved with udev_list_entry_next(),
763 * which returns #NULL if no more entries exist. The property name
764 * can be retrieved from the list entry by udev_list_get_name(),
765 * the property value by udev_list_get_value().
767 * Returns: the first entry of the property list
769 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
771 if (udev_device == NULL)
773 if (!udev_device->info_loaded)
774 device_load_info(udev_device);
775 if (!udev_device->devlinks_uptodate) {
776 char symlinks[UTIL_PATH_SIZE];
777 struct udev_list_entry *list_entry;
779 udev_device->devlinks_uptodate = 1;
780 list_entry = udev_device_get_devlinks_list_entry(udev_device);
781 if (list_entry != NULL) {
782 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
783 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
784 util_strlcat(symlinks, " ", sizeof(symlinks));
785 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
787 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
790 return udev_list_get_entry(&udev_device->properties_list);
793 const char *udev_device_get_driver(struct udev_device *udev_device)
795 char driver[UTIL_NAME_SIZE];
797 if (udev_device == NULL)
799 if (!udev_device->driver_set) {
800 udev_device->driver_set = 1;
801 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
802 udev_device->driver = strdup(driver);
804 return udev_device->driver;
807 dev_t udev_device_get_devnum(struct udev_device *udev_device)
809 if (udev_device == NULL)
810 return makedev(0, 0);
811 if (!udev_device->info_loaded)
812 device_load_info(udev_device);
813 return udev_device->devnum;
816 const char *udev_device_get_action(struct udev_device *udev_device)
818 if (udev_device == NULL)
820 return udev_device->action;
823 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
825 if (udev_device == NULL)
827 return udev_device->seqnum;
830 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
832 struct udev_list_entry *list_entry;
833 char path[UTIL_PATH_SIZE];
834 char value[UTIL_NAME_SIZE];
838 const char *val = NULL;
840 if (udev_device == NULL)
845 /* look for possibly already cached result */
846 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
847 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
848 info(udev_device->udev, "got '%s' (%s) from cache\n",
849 sysattr, udev_list_entry_get_value(list_entry));
850 return udev_list_entry_get_value(list_entry);
854 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
855 util_strlcat(path, "/", sizeof(path));
856 util_strlcat(path, sysattr, sizeof(path));
858 if (lstat(path, &statbuf) != 0) {
859 info(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
860 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
864 if (S_ISLNK(statbuf.st_mode)) {
865 /* links return the last element of the target path */
866 char target[UTIL_NAME_SIZE];
870 len = readlink(path, target, sizeof(target));
873 pos = strrchr(target, '/');
876 info(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
877 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
878 val = udev_list_entry_get_value(list_entry);
884 /* skip directories */
885 if (S_ISDIR(statbuf.st_mode))
888 /* skip non-readable files */
889 if ((statbuf.st_mode & S_IRUSR) == 0)
892 /* read attribute value */
893 fd = open(path, O_RDONLY);
895 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
898 size = read(fd, value, sizeof(value));
902 if (size == sizeof(value))
905 /* got a valid value, store it in cache and return it */
907 util_remove_trailing_chars(value, '\n');
908 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
909 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
910 val = udev_list_entry_get_value(list_entry);
915 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
920 free(udev_device->syspath);
921 udev_device->syspath = strdup(syspath);
922 if (udev_device->syspath == NULL)
924 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
925 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
927 pos = strrchr(udev_device->syspath, '/');
930 udev_device->sysname = strdup(&pos[1]);
931 if (udev_device->sysname == NULL)
934 /* some devices have '!' in their name, change that to '/' */
936 while (udev_device->sysname[len] != '\0') {
937 if (udev_device->sysname[len] == '!')
938 udev_device->sysname[len] = '/';
942 /* trailing number */
943 while (len > 0 && isdigit(udev_device->sysname[--len]))
944 udev_device->sysnum = &udev_device->sysname[len];
946 /* sysname is completely numeric */
948 udev_device->sysnum = NULL;
953 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
955 free(udev_device->subsystem);
956 udev_device->subsystem = strdup(subsystem);
957 if (udev_device->subsystem == NULL)
959 udev_device->subsystem_set = 1;
960 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
964 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
966 free(udev_device->devnode);
967 udev_device->devnode = strdup(devnode);
970 if (udev_device->devnode == NULL)
972 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
976 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
978 udev_device->devlinks_uptodate = 0;
979 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
984 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
986 udev_device->envp_uptodate = 0;
988 struct udev_list_entry *list_entry;
990 list_entry = udev_device_get_properties_list_entry(udev_device);
991 list_entry = udev_list_entry_get_by_name(list_entry, key);
992 if (list_entry != NULL)
993 udev_list_entry_remove(list_entry);
996 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
999 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1001 char name[UTIL_PATH_SIZE];
1004 util_strlcpy(name, property, sizeof(name));
1005 val = strchr(name, '=');
1012 return udev_device_add_property(udev_device, name, val);
1015 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1017 if (!udev_device->envp_uptodate) {
1019 struct udev_list_entry *list_entry;
1021 if (udev_device->envp) {
1022 for (i = 0; i < ENVP_SIZE && udev_device->envp[i] != NULL; i++)
1023 free(udev_device->envp[i]);
1025 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1028 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1029 asprintf(&udev_device->envp[i++], "%s=%s",
1030 udev_list_entry_get_name(list_entry),
1031 udev_list_entry_get_value(list_entry));
1032 if (i+1 >= ENVP_SIZE)
1035 udev_device->envp[i] = NULL;
1036 info(udev_device->udev, "constructed envp from %u properties\n", i);
1037 udev_device->envp_uptodate = 1;
1039 return udev_device->envp;
1042 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1044 free(udev_device->action);
1045 udev_device->action = strdup(action);
1046 if (udev_device->action == NULL)
1048 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1052 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1054 free(udev_device->driver);
1055 udev_device->driver = strdup(driver);
1056 if (udev_device->driver == NULL)
1058 udev_device->driver_set = 1;
1059 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1063 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1065 return udev_device->devpath_old;
1068 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1070 udev_device->devpath_old = strdup(devpath_old);
1071 if (udev_device->devpath_old == NULL)
1073 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1077 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1079 return udev_device->physdevpath;
1082 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1084 udev_device->physdevpath = strdup(physdevpath);
1085 if (udev_device->physdevpath == NULL)
1090 int udev_device_get_timeout(struct udev_device *udev_device)
1092 return udev_device->timeout;
1095 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1097 udev_device->timeout = timeout;
1100 int udev_device_get_event_timeout(struct udev_device *udev_device)
1102 if (!udev_device->info_loaded)
1103 device_load_info(udev_device);
1104 return udev_device->event_timeout;
1107 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1109 udev_device->event_timeout = event_timeout;
1113 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1117 udev_device->seqnum = seqnum;
1118 snprintf(num, sizeof(num), "%llu", seqnum);
1119 udev_device_add_property(udev_device, "SEQNUM", num);
1123 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1127 udev_device->devnum = devnum;
1129 snprintf(num, sizeof(num), "%u", major(devnum));
1130 udev_device_add_property(udev_device, "MAJOR", num);
1131 snprintf(num, sizeof(num), "%u", minor(devnum));
1132 udev_device_add_property(udev_device, "MINOR", num);
1136 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1138 if (!udev_device->info_loaded)
1139 device_load_info(udev_device);
1140 return udev_device->num_fake_partitions;
1143 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1145 udev_device->num_fake_partitions = num;
1149 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1151 if (!udev_device->info_loaded)
1152 device_load_info(udev_device);
1153 return udev_device->devlink_priority;
1156 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1158 udev_device->devlink_priority = prio;
1162 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1164 if (!udev_device->info_loaded)
1165 device_load_info(udev_device);
1166 return udev_device->ignore_remove;
1169 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1171 udev_device->ignore_remove = ignore;