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"
38 struct udev_device *parent_device;
51 size_t monitor_buf_len;
52 struct udev_list_node devlinks_list;
53 struct udev_list_node properties_list;
54 struct udev_list_node sysattr_list;
55 unsigned long long int seqnum;
58 int num_fake_partitions;
62 unsigned int parent_set:1;
63 unsigned int subsystem_set:1;
64 unsigned int devlinks_uptodate:1;
65 unsigned int envp_uptodate:1;
66 unsigned int driver_set:1;
67 unsigned int info_loaded:1;
68 unsigned int ignore_remove:1;
71 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
75 /* translate to location of db file */
76 util_strlcpy(filename, udev_get_dev_path(udev), len);
77 start = util_strlcat(filename, "/.udev/db/", len);
78 util_strlcat(filename, devpath, len);
79 return util_path_encode(&filename[start], len - start);
82 int udev_device_read_db(struct udev_device *udev_device)
85 char filename[UTIL_PATH_SIZE];
86 char line[UTIL_LINE_SIZE];
89 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
91 if (lstat(filename, &stats) != 0) {
92 info(udev_device->udev, "no db file to read %s: %m\n", filename);
95 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
96 char target[UTIL_PATH_SIZE];
97 char devnode[UTIL_PATH_SIZE];
101 target_len = readlink(filename, target, sizeof(target));
103 target[target_len] = '\0';
105 info(udev_device->udev, "error reading db link %s: %m\n", filename);
109 next = strchr(target, ' ');
114 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
115 util_strlcat(devnode, "/", sizeof(devnode));
116 util_strlcat(devnode, target, sizeof(devnode));
117 udev_device_set_devnode(udev_device, devnode);
118 while (next != NULL) {
119 char devlink[UTIL_PATH_SIZE];
123 next = strchr(next, ' ');
128 util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
129 util_strlcat(devlink, "/", sizeof(devlink));
130 util_strlcat(devlink, lnk, sizeof(devlink));
131 udev_device_add_devlink(udev_device, devlink);
133 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
137 f = fopen(filename, "r");
139 info(udev_device->udev, "error reading db file %s: %m\n", filename);
142 while (fgets(line, sizeof(line), f)) {
153 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
154 util_strlcat(filename, "/", sizeof(filename));
155 util_strlcat(filename, val, sizeof(filename));
156 udev_device_set_devnode(udev_device, filename);
159 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
160 util_strlcat(filename, "/", sizeof(filename));
161 util_strlcat(filename, val, sizeof(filename));
162 udev_device_add_devlink(udev_device, filename);
165 udev_device_set_devlink_priority(udev_device, atoi(val));
168 udev_device_set_event_timeout(udev_device, atoi(val));
171 udev_device_set_num_fake_partitions(udev_device, atoi(val));
174 udev_device_set_ignore_remove(udev_device, atoi(val));
177 udev_device_add_property_from_string(udev_device, val);
183 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
187 int udev_device_read_uevent_file(struct udev_device *udev_device)
189 char filename[UTIL_PATH_SIZE];
191 char line[UTIL_LINE_SIZE];
195 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
196 util_strlcat(filename, "/uevent", sizeof(filename));
197 f = fopen(filename, "r");
201 while (fgets(line, sizeof(line), f)) {
204 pos = strchr(line, '\n');
209 if (strncmp(line, "MAJOR=", 6) == 0)
210 maj = strtoull(&line[6], NULL, 10);
211 else if (strncmp(line, "MINOR=", 6) == 0)
212 min = strtoull(&line[6], NULL, 10);
214 udev_device_add_property_from_string(udev_device, line);
217 udev_device->devnum = makedev(maj, min);
223 static void device_load_info(struct udev_device *device)
225 device->info_loaded = 1;
226 udev_device_read_uevent_file(device);
227 udev_device_read_db(device);
230 void udev_device_set_info_loaded(struct udev_device *device)
232 device->info_loaded = 1;
235 struct udev_device *device_new(struct udev *udev)
237 struct udev_device *udev_device;
238 struct udev_list_entry *list_entry;
243 udev_device = calloc(1, sizeof(struct udev_device));
244 if (udev_device == NULL)
246 udev_device->refcount = 1;
247 udev_device->udev = udev;
248 udev_list_init(&udev_device->devlinks_list);
249 udev_list_init(&udev_device->properties_list);
250 udev_list_init(&udev_device->sysattr_list);
251 udev_device->event_timeout = -1;
252 /* copy global properties */
253 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
254 udev_device_add_property(udev_device,
255 udev_list_entry_get_name(list_entry),
256 udev_list_entry_get_value(list_entry));
257 info(udev_device->udev, "udev_device: %p created\n", udev_device);
262 * udev_device_new_from_syspath:
263 * @udev: udev library context
264 * @syspath: sys device path including sys directory
266 * Create new udev device, and fill in information from the sys
267 * device and the udev database entry. The sypath is the absolute
268 * path to the device, including the sys mount point.
270 * The initial refcount is 1, and needs to be decremented to
271 * release the ressources of the udev device.
273 * Returns: a new udev device, or #NULL, if it does not exist
275 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
279 char path[UTIL_PATH_SIZE];
282 struct udev_device *udev_device;
289 /* path starts in sys */
290 len = strlen(udev_get_sys_path(udev));
291 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
292 info(udev, "not in sys :%s\n", syspath);
296 /* path is not a root directory */
297 subdir = &syspath[len+1];
298 pos = strrchr(subdir, '/');
299 if (pos == NULL || pos < &subdir[2]) {
300 info(udev, "not a subdir :%s\n", syspath);
304 /* resolve possible symlink to real path */
305 util_strlcpy(path, syspath, sizeof(path));
306 util_resolve_sys_link(udev, path, sizeof(path));
308 /* try to resolve the silly block layout if needed */
309 if (strncmp(&path[len], "/block/", 7) == 0) {
310 char block[UTIL_PATH_SIZE];
311 char part[UTIL_PATH_SIZE];
313 util_strlcpy(block, path, sizeof(block));
314 pos = strrchr(block, '/');
317 util_strlcpy(part, pos, sizeof(part));
319 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
320 util_strlcpy(path, block, sizeof(path));
321 util_strlcat(path, part, sizeof(path));
325 /* path exists in sys */
326 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
327 strncmp(&syspath[len], "/class/", 7) == 0 ||
328 strncmp(&syspath[len], "/block/", 7) == 0) {
329 char file[UTIL_PATH_SIZE];
331 /* all "devices" require a "uevent" file */
332 util_strlcpy(file, path, sizeof(file));
333 util_strlcat(file, "/uevent", sizeof(file));
334 if (stat(file, &statbuf) != 0) {
335 info(udev, "not a device: %s\n", syspath);
339 /* everything else just needs to be a directory */
340 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
341 info(udev, "directory not found: %s\n", syspath);
346 udev_device = device_new(udev);
347 if (udev_device == NULL)
350 udev_device_set_syspath(udev_device, path);
351 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
356 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
358 char path[UTIL_PATH_SIZE];
359 const char *type_str;
360 struct udev_enumerate *udev_enumerate;
361 struct udev_list_entry *list_entry;
362 struct udev_device *device = NULL;
366 else if (type == 'c')
371 /* /sys/dev/{block,char}/<maj>:<min> link */
372 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
373 type_str, major(devnum), minor(devnum));
374 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
375 return udev_device_new_from_syspath(udev, path);
377 udev_enumerate = udev_enumerate_new(udev);
378 if (udev_enumerate == NULL)
381 /* fallback to search sys devices for the major/minor */
383 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
384 else if (type == 'c')
385 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
386 udev_enumerate_scan_devices(udev_enumerate);
387 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
388 struct udev_device *device_loop;
390 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
391 if (device_loop != NULL) {
392 if (udev_device_get_devnum(device_loop) == devnum) {
393 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
395 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
397 device = device_loop;
400 udev_device_unref(device_loop);
403 udev_enumerate_unref(udev_enumerate);
407 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
410 char path_full[UTIL_PATH_SIZE];
414 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
415 path = &path_full[sys_path_len];
417 if (strcmp(subsystem, "subsystem") == 0) {
418 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
419 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
420 if (stat(path_full, &statbuf) == 0)
423 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
424 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
425 if (stat(path_full, &statbuf) == 0)
428 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
429 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
430 if (stat(path_full, &statbuf) == 0)
435 if (strcmp(subsystem, "module") == 0) {
436 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
437 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
438 if (stat(path_full, &statbuf) == 0)
443 if (strcmp(subsystem, "drivers") == 0) {
444 char subsys[UTIL_NAME_SIZE];
447 util_strlcpy(subsys, sysname, sizeof(subsys));
448 driver = strchr(subsys, ':');
449 if (driver != NULL) {
452 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
453 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
454 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
455 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
456 if (stat(path_full, &statbuf) == 0)
459 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
460 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
461 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
462 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
463 if (stat(path_full, &statbuf) == 0)
469 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
470 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
471 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
472 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
473 if (stat(path_full, &statbuf) == 0)
476 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
477 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
478 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
479 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
480 if (stat(path_full, &statbuf) == 0)
483 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
484 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
485 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
486 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
487 if (stat(path_full, &statbuf) == 0)
492 return udev_device_new_from_syspath(udev, path_full);
495 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
497 struct udev_device *udev_device_parent = NULL;
498 char path[UTIL_PATH_SIZE];
501 /* follow "device" link in deprecated sys layout */
502 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
503 strncmp(udev_device->devpath, "/block/", 7) == 0) {
504 util_strlcpy(path, udev_device->syspath, sizeof(path));
505 util_strlcat(path, "/device", sizeof(path));
506 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
507 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
508 return udev_device_parent;
511 util_strlcpy(path, udev_device->syspath, sizeof(path));
512 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
516 pos = strrchr(subdir, '/');
517 if (pos == NULL || pos < &subdir[2])
520 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
521 if (udev_device_parent != NULL)
522 return udev_device_parent;
527 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
529 if (udev_device == NULL)
531 if (!udev_device->parent_set) {
532 udev_device->parent_set = 1;
533 udev_device->parent_device = device_new_from_parent(udev_device);
535 if (udev_device->parent_device != NULL)
536 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
537 return udev_device->parent_device;
540 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
542 struct udev_device *parent;
544 parent = udev_device_get_parent(udev_device);
545 while (parent != NULL) {
546 const char *parent_subsystem;
548 parent_subsystem = udev_device_get_subsystem(parent);
549 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
551 parent = udev_device_get_parent(parent);
557 * udev_device_get_udev:
558 * @udev_device: udev device
560 * Retrieve the udev library context the device was created with.
562 * Returns: the udev library context
564 struct udev *udev_device_get_udev(struct udev_device *udev_device)
566 if (udev_device == NULL)
568 return udev_device->udev;
573 * @udev_device: udev device
575 * Take a reference of a udev device.
577 * Returns: the passed udev device
579 struct udev_device *udev_device_ref(struct udev_device *udev_device)
581 if (udev_device == NULL)
583 udev_device->refcount++;
589 * @udev_device: udev device
591 * Drop a reference of a udev device. If the refcount reaches zero,
592 * the ressources of the device will be released.
595 void udev_device_unref(struct udev_device *udev_device)
599 if (udev_device == NULL)
601 udev_device->refcount--;
602 if (udev_device->refcount > 0)
604 if (udev_device->parent_device != NULL)
605 udev_device_unref(udev_device->parent_device);
606 free(udev_device->syspath);
607 free(udev_device->sysname);
608 free(udev_device->devnode);
609 free(udev_device->subsystem);
610 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
611 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
612 free(udev_device->action);
613 free(udev_device->driver);
614 free(udev_device->devpath_old);
615 free(udev_device->physdevpath);
616 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
617 if (udev_device->envp != NULL) {
618 for (i = 0; i < ENVP_SIZE && udev_device->envp[i] != NULL; i++)
619 free(udev_device->envp[i]);
620 free(udev_device->envp);
622 free(udev_device->monitor_buf);
623 info(udev_device->udev, "udev_device: %p released\n", udev_device);
628 * udev_device_get_devpath:
629 * @udev_device: udev device
631 * Retrieve the kernel devpath value of the udev device. The path
632 * does not contain the sys mount point, and starts with a '/'.
634 * Returns: the devpath of the udev device
636 const char *udev_device_get_devpath(struct udev_device *udev_device)
638 if (udev_device == NULL)
640 return udev_device->devpath;
644 * udev_device_get_syspath:
645 * @udev_device: udev device
647 * Retrieve the sys path of the udev device. The path is an
648 * absolute path and starts with the sys mount point.
650 * Returns: the sys path of the udev device
652 const char *udev_device_get_syspath(struct udev_device *udev_device)
654 if (udev_device == NULL)
656 return udev_device->syspath;
659 const char *udev_device_get_sysname(struct udev_device *udev_device)
661 if (udev_device == NULL)
663 return udev_device->sysname;
666 const char *udev_device_get_sysnum(struct udev_device *udev_device)
668 if (udev_device == NULL)
670 return udev_device->sysnum;
674 * udev_device_get_devnode:
675 * @udev_device: udev device
677 * Retrieve the device node file name belonging to the udev device.
678 * The path is an absolute path, and starts with the device directory.
680 * Returns: the device node file name of the udev device, or #NULL if no device node exists
682 const char *udev_device_get_devnode(struct udev_device *udev_device)
684 if (udev_device == NULL)
686 if (!udev_device->info_loaded)
687 device_load_info(udev_device);
688 return udev_device->devnode;
692 * udev_device_get_subsystem:
693 * @udev_device: udev device
695 * Retrieve the subsystem string of the udev device. The string does not
698 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
700 const char *udev_device_get_subsystem(struct udev_device *udev_device)
702 char subsystem[UTIL_NAME_SIZE];
704 if (udev_device == NULL)
706 if (!udev_device->subsystem_set) {
707 udev_device->subsystem_set = 1;
708 /* read "subsytem" link */
709 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
710 udev_device_set_subsystem(udev_device, subsystem);
711 return udev_device->subsystem;
714 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
715 udev_device_set_subsystem(udev_device, "module");
716 return udev_device->subsystem;
718 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
719 udev_device_set_subsystem(udev_device, "drivers");
720 return udev_device->subsystem;
722 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
723 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
724 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
725 udev_device_set_subsystem(udev_device, "subsystem");
726 return udev_device->subsystem;
729 return udev_device->subsystem;
733 * udev_device_get_devlinks_list_entry:
734 * @udev_device: udev device
736 * Retrieve the list of device links pointing to the device file of
737 * the udev device. The next list entry can be retrieved with
738 * udev_list_entry_next(), which returns #NULL if no more entries exist.
739 * The devlink path can be retrieved from the list entry by
740 * udev_list_entry_get_name(). The path is an absolute path, and starts with
741 * the device directory.
743 * Returns: the first entry of the device node link list
745 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
747 if (udev_device == NULL)
749 if (!udev_device->info_loaded)
750 device_load_info(udev_device);
751 return udev_list_get_entry(&udev_device->devlinks_list);
754 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
756 udev_device->devlinks_uptodate = 0;
757 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
761 * udev_device_get_properties_list_entry:
762 * @udev_device: udev device
764 * Retrieve the list of key/value device properties of the udev
765 * device. The next list entry can be retrieved with udev_list_entry_next(),
766 * which returns #NULL if no more entries exist. The property name
767 * can be retrieved from the list entry by udev_list_get_name(),
768 * the property value by udev_list_get_value().
770 * Returns: the first entry of the property list
772 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
774 if (udev_device == NULL)
776 if (!udev_device->info_loaded)
777 device_load_info(udev_device);
778 if (!udev_device->devlinks_uptodate) {
779 char symlinks[UTIL_PATH_SIZE];
780 struct udev_list_entry *list_entry;
782 udev_device->devlinks_uptodate = 1;
783 list_entry = udev_device_get_devlinks_list_entry(udev_device);
784 if (list_entry != NULL) {
785 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
786 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
787 util_strlcat(symlinks, " ", sizeof(symlinks));
788 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
790 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
793 return udev_list_get_entry(&udev_device->properties_list);
796 const char *udev_device_get_driver(struct udev_device *udev_device)
798 char driver[UTIL_NAME_SIZE];
800 if (udev_device == NULL)
802 if (!udev_device->driver_set) {
803 udev_device->driver_set = 1;
804 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
805 udev_device->driver = strdup(driver);
807 return udev_device->driver;
810 dev_t udev_device_get_devnum(struct udev_device *udev_device)
812 if (udev_device == NULL)
813 return makedev(0, 0);
814 if (!udev_device->info_loaded)
815 device_load_info(udev_device);
816 return udev_device->devnum;
819 const char *udev_device_get_action(struct udev_device *udev_device)
821 if (udev_device == NULL)
823 return udev_device->action;
826 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
828 if (udev_device == NULL)
830 return udev_device->seqnum;
833 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
835 struct udev_list_entry *list_entry;
836 char path[UTIL_PATH_SIZE];
837 char value[UTIL_NAME_SIZE];
841 const char *val = NULL;
843 if (udev_device == NULL)
848 /* look for possibly already cached result */
849 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
850 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
851 info(udev_device->udev, "got '%s' (%s) from cache\n",
852 sysattr, udev_list_entry_get_value(list_entry));
853 return udev_list_entry_get_value(list_entry);
857 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
858 util_strlcat(path, "/", sizeof(path));
859 util_strlcat(path, sysattr, sizeof(path));
861 if (lstat(path, &statbuf) != 0) {
862 info(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
863 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
867 if (S_ISLNK(statbuf.st_mode)) {
868 /* links return the last element of the target path */
869 char target[UTIL_NAME_SIZE];
873 len = readlink(path, target, sizeof(target));
876 pos = strrchr(target, '/');
879 info(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
880 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
881 val = udev_list_entry_get_value(list_entry);
887 /* skip directories */
888 if (S_ISDIR(statbuf.st_mode))
891 /* skip non-readable files */
892 if ((statbuf.st_mode & S_IRUSR) == 0)
895 /* read attribute value */
896 fd = open(path, O_RDONLY);
898 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
901 size = read(fd, value, sizeof(value));
905 if (size == sizeof(value))
908 /* got a valid value, store it in cache and return it */
910 util_remove_trailing_chars(value, '\n');
911 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
912 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
913 val = udev_list_entry_get_value(list_entry);
918 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
923 free(udev_device->syspath);
924 udev_device->syspath = strdup(syspath);
925 if (udev_device->syspath == NULL)
927 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
928 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
930 pos = strrchr(udev_device->syspath, '/');
933 udev_device->sysname = strdup(&pos[1]);
934 if (udev_device->sysname == NULL)
937 /* some devices have '!' in their name, change that to '/' */
939 while (udev_device->sysname[len] != '\0') {
940 if (udev_device->sysname[len] == '!')
941 udev_device->sysname[len] = '/';
945 /* trailing number */
946 while (len > 0 && isdigit(udev_device->sysname[--len]))
947 udev_device->sysnum = &udev_device->sysname[len];
949 /* sysname is completely numeric */
951 udev_device->sysnum = NULL;
956 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
958 free(udev_device->subsystem);
959 udev_device->subsystem = strdup(subsystem);
960 if (udev_device->subsystem == NULL)
962 udev_device->subsystem_set = 1;
963 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
967 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
969 free(udev_device->devnode);
970 udev_device->devnode = strdup(devnode);
973 if (udev_device->devnode == NULL)
975 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
979 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
981 udev_device->devlinks_uptodate = 0;
982 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
987 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
989 udev_device->monitor_buf_len = 0;
990 udev_device->envp_uptodate = 0;
992 struct udev_list_entry *list_entry;
994 list_entry = udev_device_get_properties_list_entry(udev_device);
995 list_entry = udev_list_entry_get_by_name(list_entry, key);
996 if (list_entry != NULL)
997 udev_list_entry_remove(list_entry);
1000 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1003 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1005 char name[UTIL_PATH_SIZE];
1008 util_strlcpy(name, property, sizeof(name));
1009 val = strchr(name, '=');
1016 return udev_device_add_property(udev_device, name, val);
1019 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1021 if (!udev_device->envp_uptodate) {
1023 struct udev_list_entry *list_entry;
1025 if (udev_device->envp != NULL) {
1026 for (i = 0; i < ENVP_SIZE && udev_device->envp[i] != NULL; i++)
1027 free(udev_device->envp[i]);
1029 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1033 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1034 asprintf(&udev_device->envp[i++], "%s=%s",
1035 udev_list_entry_get_name(list_entry),
1036 udev_list_entry_get_value(list_entry));
1037 if (i+1 >= ENVP_SIZE)
1040 udev_device->envp[i] = NULL;
1041 info(udev_device->udev, "constructed envp from %u properties\n", i);
1042 udev_device->envp_uptodate = 1;
1044 return udev_device->envp;
1047 #define MONITOR_BUF_SIZE 4096
1048 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1050 if (udev_device->monitor_buf_len == 0) {
1052 struct udev_list_entry *list_entry;
1056 free(udev_device->monitor_buf);
1057 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1058 if (udev_device->monitor_buf == NULL)
1061 /* header <action>@<devpath> */
1062 action = udev_device_get_action(udev_device);
1065 bufpos = util_strlcpy(udev_device->monitor_buf, action, MONITOR_BUF_SIZE);
1066 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "@", MONITOR_BUF_SIZE-bufpos);
1067 if (len >= MONITOR_BUF_SIZE-bufpos)
1070 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1071 udev_device_get_devpath(udev_device),
1072 MONITOR_BUF_SIZE-bufpos);
1073 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1077 /* property strings */
1078 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1079 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1080 udev_list_entry_get_name(list_entry), MONITOR_BUF_SIZE-bufpos);
1081 if (len >= MONITOR_BUF_SIZE-bufpos)
1084 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "=", MONITOR_BUF_SIZE-bufpos);
1085 if (len >= MONITOR_BUF_SIZE-bufpos)
1088 len = util_strlcpy(&udev_device->monitor_buf[bufpos], udev_list_entry_get_value(list_entry),
1089 MONITOR_BUF_SIZE-bufpos);
1090 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1094 udev_device->monitor_buf_len = bufpos;
1096 *buf = udev_device->monitor_buf;
1097 return udev_device->monitor_buf_len;
1100 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1102 free(udev_device->action);
1103 udev_device->action = strdup(action);
1104 if (udev_device->action == NULL)
1106 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1110 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1112 free(udev_device->driver);
1113 udev_device->driver = strdup(driver);
1114 if (udev_device->driver == NULL)
1116 udev_device->driver_set = 1;
1117 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1121 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1123 return udev_device->devpath_old;
1126 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1128 udev_device->devpath_old = strdup(devpath_old);
1129 if (udev_device->devpath_old == NULL)
1131 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1135 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1137 return udev_device->physdevpath;
1140 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1142 udev_device->physdevpath = strdup(physdevpath);
1143 if (udev_device->physdevpath == NULL)
1148 int udev_device_get_timeout(struct udev_device *udev_device)
1150 return udev_device->timeout;
1153 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1155 udev_device->timeout = timeout;
1158 int udev_device_get_event_timeout(struct udev_device *udev_device)
1160 if (!udev_device->info_loaded)
1161 device_load_info(udev_device);
1162 return udev_device->event_timeout;
1165 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1167 udev_device->event_timeout = event_timeout;
1171 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1175 udev_device->seqnum = seqnum;
1176 snprintf(num, sizeof(num), "%llu", seqnum);
1177 udev_device_add_property(udev_device, "SEQNUM", num);
1181 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1185 udev_device->devnum = devnum;
1187 snprintf(num, sizeof(num), "%u", major(devnum));
1188 udev_device_add_property(udev_device, "MAJOR", num);
1189 snprintf(num, sizeof(num), "%u", minor(devnum));
1190 udev_device_add_property(udev_device, "MINOR", num);
1194 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1196 if (!udev_device->info_loaded)
1197 device_load_info(udev_device);
1198 return udev_device->num_fake_partitions;
1201 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1203 udev_device->num_fake_partitions = num;
1207 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1209 if (!udev_device->info_loaded)
1210 device_load_info(udev_device);
1211 return udev_device->devlink_priority;
1214 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1216 udev_device->devlink_priority = prio;
1220 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1222 if (!udev_device->info_loaded)
1223 device_load_info(udev_device);
1224 return udev_device->ignore_remove;
1227 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1229 udev_device->ignore_remove = ignore;