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/>.
31 #include "libudev-private.h"
36 struct udev_device *parent_device;
42 struct list_node devlink_list;
43 struct list_node properties_list;
51 unsigned long long int seqnum;
52 int num_fake_partitions;
55 struct list_node attr_list;
59 static size_t syspath_to_db_path(struct udev_device *udev_device, char *filename, size_t len)
63 /* translate to location of db file */
64 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), len);
65 start = util_strlcat(filename, "/.udev/db/", len);
66 util_strlcat(filename, udev_device->devpath, len);
67 return util_path_encode(&filename[start], len - start);
70 static int device_read_db(struct udev_device *udev_device)
73 char filename[UTIL_PATH_SIZE];
74 char line[UTIL_LINE_SIZE];
77 syspath_to_db_path(udev_device, filename, sizeof(filename));
79 if (lstat(filename, &stats) != 0) {
80 info(udev_device->udev, "no db file to read %s: %m\n", filename);
83 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
84 char target[UTIL_PATH_SIZE];
87 target_len = readlink(filename, target, sizeof(target));
89 target[target_len] = '\0';
91 info(udev_device->udev, "error reading db link %s: %m\n", filename);
94 if (asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
96 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
100 f = fopen(filename, "r");
102 info(udev_device->udev, "error reading db file %s: %m\n", filename);
105 while (fgets(line, sizeof(line), f)) {
117 asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), val);
120 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
121 util_strlcat(filename, "/", sizeof(filename));
122 util_strlcat(filename, val, sizeof(filename));
123 device_add_devlink(udev_device, filename);
126 device_set_devlink_priority(udev_device, atoi(val));
129 device_set_event_timeout(udev_device, atoi(val));
132 device_set_num_fake_partitions(udev_device, atoi(val));
135 device_set_ignore_remove(udev_device, atoi(val));
138 device_add_property_from_string(udev_device, val);
144 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
148 static int device_read_uevent_file(struct udev_device *udev_device)
150 char filename[UTIL_PATH_SIZE];
152 char line[UTIL_LINE_SIZE];
156 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
157 util_strlcat(filename, "/uevent", sizeof(filename));
158 f = fopen(filename, "r");
162 while (fgets(line, sizeof(line), f)) {
165 pos = strchr(line, '\n');
170 if (strncmp(line, "MAJOR=", 6) == 0)
171 maj = strtoull(&line[6], NULL, 10);
172 else if (strncmp(line, "MINOR=", 6) == 0)
173 min = strtoull(&line[6], NULL, 10);
175 device_add_property_from_string(udev_device, line);
178 udev_device->devnum = makedev(maj, min);
184 static void device_load_info(struct udev_device *device)
186 device_read_uevent_file(device);
187 device_read_db(device);
188 device->info_loaded = 1;
191 void device_set_info_loaded(struct udev_device *device)
193 device->info_loaded = 1;
196 struct udev_device *device_new(struct udev *udev)
198 struct udev_device *udev_device;
203 udev_device = malloc(sizeof(struct udev_device));
204 if (udev_device == NULL)
206 memset(udev_device, 0x00, sizeof(struct udev_device));
207 udev_device->refcount = 1;
208 udev_device->udev = udev;
209 list_init(&udev_device->devlink_list);
210 list_init(&udev_device->properties_list);
211 list_init(&udev_device->attr_list);
212 info(udev_device->udev, "udev_device: %p created\n", udev_device);
217 * udev_device_new_from_syspath:
218 * @udev: udev library context
219 * @syspath: sys device path including sys directory
221 * Create new udev device, and fill in information from the sys
222 * device and the udev database entry. The sypath is the absolute
223 * path to the device, including the sys mount point.
225 * The initial refcount is 1, and needs to be decremented to
226 * release the ressources of the udev device.
228 * Returns: a new udev device, or #NULL, if it does not exist
230 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
234 char path[UTIL_PATH_SIZE];
237 struct udev_device *udev_device;
244 /* path starts in sys */
245 len = strlen(udev_get_sys_path(udev));
246 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
247 info(udev, "not in sys :%s\n", syspath);
251 /* path is not a root directory */
252 subdir = &syspath[len+1];
253 pos = strrchr(subdir, '/');
254 if (pos == NULL || pos < &subdir[2]) {
255 info(udev, "not a subdir :%s\n", syspath);
259 /* resolve possible symlink to real path */
260 util_strlcpy(path, syspath, sizeof(path));
261 util_resolve_sys_link(udev, path, sizeof(path));
263 /* try to resolve the silly block layout if needed */
264 if (strncmp(&path[len], "/block/", 7) == 0) {
265 char block[UTIL_PATH_SIZE];
266 char part[UTIL_PATH_SIZE];
268 util_strlcpy(block, path, sizeof(block));
269 pos = strrchr(block, '/');
272 util_strlcpy(part, pos, sizeof(part));
274 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
275 util_strlcpy(path, block, sizeof(path));
276 util_strlcat(path, part, sizeof(path));
280 /* path exists in sys */
281 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
282 strncmp(&syspath[len], "/class/", 7) == 0 ||
283 strncmp(&syspath[len], "/block/", 7) == 0) {
284 char file[UTIL_PATH_SIZE];
286 /* all "devices" require a "uevent" file */
287 util_strlcpy(file, path, sizeof(file));
288 util_strlcat(file, "/uevent", sizeof(file));
289 if (stat(file, &statbuf) != 0) {
290 info(udev, "not a device: %s\n", syspath);
294 /* everything else just needs to be a directory */
295 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
296 info(udev, "directory not found: %s\n", syspath);
301 udev_device = device_new(udev);
302 if (udev_device == NULL)
305 device_set_syspath(udev_device, path);
306 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
311 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
313 char path[UTIL_PATH_SIZE];
314 const char *type_str;
315 struct udev_enumerate *udev_enumerate;
316 struct udev_list_entry *list_entry;
317 struct udev_device *device = NULL;
321 else if (type == 'c')
326 /* /sys/dev/{block,char}/<maj>:<min> link */
327 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
328 type_str, major(devnum), minor(devnum));
329 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
330 return udev_device_new_from_syspath(udev, path);
332 udev_enumerate = udev_enumerate_new(udev);
333 if (udev_enumerate == NULL)
336 /* fallback to search sys devices for the major/minor */
338 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
339 else if (type == 'c')
340 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
341 udev_enumerate_scan_devices(udev_enumerate);
342 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
343 struct udev_device *device_loop;
345 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
346 if (device_loop != NULL) {
347 if (udev_device_get_devnum(device_loop) == devnum) {
348 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
350 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
352 device = device_loop;
355 udev_device_unref(device_loop);
358 udev_enumerate_unref(udev_enumerate);
362 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
364 struct udev_device *udev_device_parent = NULL;
365 char path[UTIL_PATH_SIZE];
368 /* follow "device" link in deprecated sys layout */
369 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
370 strncmp(udev_device->devpath, "/block/", 7) == 0) {
371 util_strlcpy(path, udev_device->syspath, sizeof(path));
372 util_strlcat(path, "/device", sizeof(path));
373 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
374 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
375 return udev_device_parent;
378 util_strlcpy(path, udev_device->syspath, sizeof(path));
379 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
383 pos = strrchr(subdir, '/');
384 if (pos == NULL || pos < &subdir[2])
387 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
388 if (udev_device_parent != NULL)
389 return udev_device_parent;
394 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
396 if (udev_device == NULL)
399 if (udev_device->parent_device != NULL) {
400 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
401 return udev_device->parent_device;
403 udev_device->parent_device = device_new_from_parent(udev_device);
404 return udev_device->parent_device;
407 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
409 struct udev_device *parent;
411 parent = udev_device_get_parent(udev_device);
412 while (parent != NULL) {
413 const char *parent_subsystem;
415 parent_subsystem = udev_device_get_subsystem(parent);
416 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
418 parent = udev_device_get_parent(parent);
424 * udev_device_get_udev:
425 * @udev_device: udev device
427 * Retrieve the udev library context the device was created with.
429 * Returns: the udev library context
431 struct udev *udev_device_get_udev(struct udev_device *udev_device)
433 if (udev_device == NULL)
435 return udev_device->udev;
440 * @udev_device: udev device
442 * Take a reference of a udev device.
444 * Returns: the passed udev device
446 struct udev_device *udev_device_ref(struct udev_device *udev_device)
448 if (udev_device == NULL)
450 udev_device->refcount++;
456 * @udev_device: udev device
458 * Drop a reference of a udev device. If the refcount reaches zero,
459 * the ressources of the device will be released.
462 void udev_device_unref(struct udev_device *udev_device)
464 if (udev_device == NULL)
466 udev_device->refcount--;
467 if (udev_device->refcount > 0)
469 if (udev_device->parent_device != NULL)
470 udev_device_unref(udev_device->parent_device);
471 free(udev_device->syspath);
472 free(udev_device->devnode);
473 free(udev_device->subsystem);
474 list_cleanup(udev_device->udev, &udev_device->devlink_list);
475 list_cleanup(udev_device->udev, &udev_device->properties_list);
476 free(udev_device->action);
477 free(udev_device->driver);
478 free(udev_device->devpath_old);
479 free(udev_device->physdevpath);
480 list_cleanup(udev_device->udev, &udev_device->attr_list);
481 info(udev_device->udev, "udev_device: %p released\n", udev_device);
486 * udev_device_get_devpath:
487 * @udev_device: udev device
489 * Retrieve the kernel devpath value of the udev device. The path
490 * does not contain the sys mount point, and starts with a '/'.
492 * Returns: the devpath of the udev device
494 const char *udev_device_get_devpath(struct udev_device *udev_device)
496 if (udev_device == NULL)
498 return udev_device->devpath;
502 * udev_device_get_syspath:
503 * @udev_device: udev device
505 * Retrieve the sys path of the udev device. The path is an
506 * absolute path and starts with the sys mount point.
508 * Returns: the sys path of the udev device
510 const char *udev_device_get_syspath(struct udev_device *udev_device)
512 if (udev_device == NULL)
514 return udev_device->syspath;
517 const char *udev_device_get_sysname(struct udev_device *udev_device)
519 if (udev_device == NULL)
521 return udev_device->sysname;
525 * udev_device_get_devnode:
526 * @udev_device: udev device
528 * Retrieve the device node file name belonging to the udev device.
529 * The path is an absolute path, and starts with the device directory.
531 * Returns: the device node file name of the udev device, or #NULL if no device node exists
533 const char *udev_device_get_devnode(struct udev_device *udev_device)
535 if (udev_device == NULL)
537 if (!udev_device->info_loaded)
538 device_load_info(udev_device);
539 return udev_device->devnode;
543 * udev_device_get_subsystem:
544 * @udev_device: udev device
546 * Retrieve the subsystem string of the udev device. The string does not
549 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
551 const char *udev_device_get_subsystem(struct udev_device *udev_device)
553 char subsystem[UTIL_NAME_SIZE];
555 if (udev_device == NULL)
557 if (udev_device->subsystem != NULL)
558 return udev_device->subsystem;
560 /* read "subsytem" link */
561 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
562 udev_device->subsystem = strdup(subsystem);
563 return udev_device->subsystem;
567 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
568 udev_device->subsystem = strdup("module");
569 return udev_device->subsystem;
571 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
572 udev_device->subsystem = strdup("drivers");
573 return udev_device->subsystem;
575 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
576 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
577 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
578 udev_device->subsystem = strdup("subsystem");
579 return udev_device->subsystem;
585 * udev_device_get_devlinks_list_entry:
586 * @udev_device: udev device
588 * Retrieve the list of device links pointing to the device file of
589 * the udev device. The next list entry can be retrieved with
590 * udev_list_entry_next(), which returns #NULL if no more entries exist.
591 * The devlink path can be retrieved from the list entry by
592 * udev_list_entry_get_name(). The path is an absolute path, and starts with
593 * the device directory.
595 * Returns: the first entry of the device node link list
597 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
599 if (udev_device == NULL)
601 if (!udev_device->info_loaded)
602 device_load_info(udev_device);
603 return list_get_entry(&udev_device->devlink_list);
607 * udev_device_get_properties_list_entry:
608 * @udev_device: udev device
610 * Retrieve the list of key/value device properties of the udev
611 * device. The next list entry can be retrieved with udev_list_entry_next(),
612 * which returns #NULL if no more entries exist. The property name
613 * can be retrieved from the list entry by udev_list_get_name(),
614 * the property value by udev_list_get_value().
616 * Returns: the first entry of the property list
618 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
620 if (udev_device == NULL)
622 if (!udev_device->info_loaded)
623 device_load_info(udev_device);
624 return list_get_entry(&udev_device->properties_list);
627 const char *udev_device_get_driver(struct udev_device *udev_device)
629 char driver[UTIL_NAME_SIZE];
631 if (udev_device == NULL)
633 if (udev_device->driver != NULL)
634 return udev_device->driver;
635 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
637 udev_device->driver = strdup(driver);
638 return udev_device->driver;
641 dev_t udev_device_get_devnum(struct udev_device *udev_device)
643 if (udev_device == NULL)
644 return makedev(0, 0);
645 if (!udev_device->info_loaded)
646 device_load_info(udev_device);
647 return udev_device->devnum;
650 const char *udev_device_get_action(struct udev_device *udev_device)
652 if (udev_device == NULL)
654 return udev_device->action;
657 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
659 if (udev_device == NULL)
661 return udev_device->seqnum;
664 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
666 struct udev_list_entry *list_entry;
667 char path[UTIL_PATH_SIZE];
668 char value[UTIL_NAME_SIZE];
672 const char *val = NULL;
674 /* look for possibly already cached result */
675 udev_list_entry_foreach(list_entry, list_get_entry(&udev_device->attr_list)) {
676 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
677 info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list_entry));
678 return udev_list_entry_get_value(list_entry);
682 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
683 util_strlcat(path, "/", sizeof(path));
684 util_strlcat(path, attr, sizeof(path));
686 if (lstat(path, &statbuf) != 0) {
687 info(udev_device->udev, "stat '%s' failed: %m\n", path);
691 if (S_ISLNK(statbuf.st_mode)) {
692 /* links return the last element of the target path */
693 char target[UTIL_NAME_SIZE];
697 len = readlink(path, target, sizeof(target));
700 pos = strrchr(target, '/');
703 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
704 list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
705 val = udev_list_entry_get_value(list_entry);
711 /* skip directories */
712 if (S_ISDIR(statbuf.st_mode))
715 /* skip non-readable files */
716 if ((statbuf.st_mode & S_IRUSR) == 0)
719 /* read attribute value */
720 fd = open(path, O_RDONLY);
722 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
725 size = read(fd, value, sizeof(value));
729 if (size == sizeof(value))
732 /* got a valid value, store it in cache and return it */
734 util_remove_trailing_chars(value, '\n');
735 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
736 list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
737 val = udev_list_entry_get_value(list_entry);
741 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
745 udev_device->syspath = strdup(syspath);
746 if (udev_device->syspath == NULL)
748 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
749 pos = strrchr(udev_device->syspath, '/');
752 udev_device->sysname = &pos[1];
756 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
758 udev_device->subsystem = strdup(subsystem);
759 if (udev_device->subsystem == NULL)
764 int device_set_devnode(struct udev_device *udev_device, const char *devnode)
766 udev_device->devnode = strdup(devnode);
767 if (udev_device->devnode == NULL)
772 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
774 if (list_entry_add(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 1, 0) == NULL)
779 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
781 if (list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0) == NULL)
786 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
788 char name[UTIL_PATH_SIZE];
791 strncpy(name, property, sizeof(name));
792 val = strchr(name, '=');
799 device_add_property(udev_device, name, val);
803 int device_set_action(struct udev_device *udev_device, const char *action)
805 udev_device->action = strdup(action);
806 if (udev_device->action == NULL)
811 int device_set_driver(struct udev_device *udev_device, const char *driver)
813 udev_device->driver = strdup(driver);
814 if (udev_device->driver == NULL)
819 const char *device_get_devpath_old(struct udev_device *udev_device)
821 return udev_device->devpath_old;
824 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
826 udev_device->devpath_old = strdup(devpath_old);
827 if (udev_device->devpath_old == NULL)
832 const char *device_get_physdevpath(struct udev_device *udev_device)
834 return udev_device->physdevpath;
837 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
839 udev_device->physdevpath = strdup(physdevpath);
840 if (udev_device->physdevpath == NULL)
845 int device_get_timeout(struct udev_device *udev_device)
847 return udev_device->timeout;
850 int device_set_timeout(struct udev_device *udev_device, int timeout)
852 udev_device->timeout = timeout;
855 int device_get_event_timeout(struct udev_device *udev_device)
857 if (!udev_device->info_loaded)
858 device_load_info(udev_device);
859 return udev_device->event_timeout;
862 int device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
864 udev_device->event_timeout = event_timeout;
868 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
870 udev_device->seqnum = seqnum;
874 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
876 udev_device->devnum = devnum;
880 int device_get_num_fake_partitions(struct udev_device *udev_device)
882 if (!udev_device->info_loaded)
883 device_load_info(udev_device);
884 return udev_device->num_fake_partitions;
887 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
889 udev_device->num_fake_partitions = num;
893 int device_get_devlink_priority(struct udev_device *udev_device)
895 if (!udev_device->info_loaded)
896 device_load_info(udev_device);
897 return udev_device->devlink_priority;
900 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
902 udev_device->devlink_priority = prio;
906 int device_get_ignore_remove(struct udev_device *udev_device)
908 if (!udev_device->info_loaded)
909 device_load_info(udev_device);
910 return udev_device->ignore_remove;
913 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
915 udev_device->ignore_remove = ignore;