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_head link_list;
43 struct list_head properties_list;
50 unsigned long long int seqnum;
51 int num_fake_partitions;
54 struct list_head attr_list;
57 static size_t syspath_to_db_path(struct udev_device *udev_device, char *filename, size_t len)
61 /* translate to location of db file */
62 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), len);
63 start = util_strlcat(filename, "/.udev/db/", len);
64 util_strlcat(filename, udev_device->devpath, len);
65 return util_path_encode(&filename[start], len - start);
68 static int device_read_uevent_file(struct udev_device *udev_device)
70 char filename[UTIL_PATH_SIZE];
72 char line[UTIL_LINE_SIZE];
76 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
77 util_strlcat(filename, "/uevent", sizeof(filename));
78 f = fopen(filename, "r");
82 while (fgets(line, sizeof(line), f)) {
85 pos = strchr(line, '\n');
90 if (strncmp(line, "MAJOR=", 6) == 0)
91 maj = strtoull(&line[6], NULL, 10);
92 else if (strncmp(line, "MINOR=", 6) == 0)
93 min = strtoull(&line[6], NULL, 10);
95 device_add_property_from_string(udev_device, line);
98 udev_device->devnum = makedev(maj, min);
104 static int device_read_db(struct udev_device *udev_device)
107 char filename[UTIL_PATH_SIZE];
108 char line[UTIL_LINE_SIZE];
111 syspath_to_db_path(udev_device, filename, sizeof(filename));
113 if (lstat(filename, &stats) != 0) {
114 info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno));
117 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
118 char target[UTIL_PATH_SIZE];
121 target_len = readlink(filename, target, sizeof(target));
123 target[target_len] = '\0';
125 info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
128 if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
130 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devname);
134 f = fopen(filename, "r");
136 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
139 while (fgets(line, sizeof(line), f)) {
151 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
154 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
155 util_strlcat(filename, "/", sizeof(filename));
156 util_strlcat(filename, val, sizeof(filename));
157 device_add_devlink(udev_device, filename);
160 device_set_devlink_priority(udev_device, atoi(val));
163 device_set_timeout(udev_device, atoi(val));
166 device_set_num_fake_partitions(udev_device, atoi(val));
169 device_set_ignore_remove(udev_device, atoi(val));
172 device_add_property_from_string(udev_device, val);
178 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
182 struct udev_device *device_init(struct udev *udev)
184 struct udev_device *udev_device;
189 udev_device = malloc(sizeof(struct udev_device));
190 if (udev_device == NULL)
192 memset(udev_device, 0x00, sizeof(struct udev_device));
193 udev_device->refcount = 1;
194 udev_device->udev = udev;
195 INIT_LIST_HEAD(&udev_device->link_list);
196 INIT_LIST_HEAD(&udev_device->properties_list);
197 INIT_LIST_HEAD(&udev_device->attr_list);
198 info(udev_device->udev, "udev_device: %p created\n", udev_device);
203 * udev_device_new_from_syspath:
204 * @udev: udev library context
205 * @syspath: sys device path including sys directory
207 * Create new udev device, and fill in information from the sys
208 * device and the udev database entry. The sypath is the absolute
209 * path to the device, including the sys mount point.
211 * The initial refcount is 1, and needs to be decremented to
212 * release the ressources of the udev device.
214 * Returns: a new udev device, or #NULL, if it does not exist
216 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
218 char path[UTIL_PATH_SIZE];
220 struct udev_device *udev_device;
227 util_strlcpy(path, syspath, sizeof(path));
228 util_strlcat(path, "/uevent", sizeof(path));
229 if (stat(path, &statbuf) != 0) {
230 info(udev, "not a device :%s\n", syspath);
234 udev_device = device_init(udev);
235 if (udev_device == NULL)
238 /* resolve possible symlink to real path */
239 util_strlcpy(path, syspath, sizeof(path));
240 util_resolve_sys_link(udev, path, sizeof(path));
241 device_set_syspath(udev_device, path);
242 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
244 device_read_uevent_file(udev_device);
245 device_read_db(udev_device);
249 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
251 char path[UTIL_PATH_SIZE];
252 const char *type_str;
253 struct udev_enumerate *enumerate;
254 struct udev_list *list;
255 struct udev_device *device = NULL;
259 else if (type == 'c')
264 /* /sys/dev/{block,char}/<maj>:<min> links */
265 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
266 type_str, major(devnum), minor(devnum));
267 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
268 return udev_device_new_from_syspath(udev, path);
270 /* fallback to search all sys devices for the major/minor */
271 enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
272 if (enumerate == NULL)
274 list = udev_enumerate_get_list(enumerate);
275 while (list != NULL) {
276 struct udev_device *device_loop;
278 device_loop = udev_device_new_from_syspath(udev, udev_list_get_name(list));
279 if (device_loop != NULL) {
280 if (udev_device_get_devnum(device_loop) == devnum) {
281 device = device_loop;
284 udev_device_unref(device_loop);
286 list = udev_list_get_next(list);
288 udev_enumerate_unref(enumerate);
292 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
294 struct udev_device *udev_device_parent = NULL;
295 char path[UTIL_PATH_SIZE];
298 if (udev_device == NULL)
301 util_strlcpy(path, udev_device->syspath, sizeof(path));
303 pos = strrchr(path, '/');
304 if (pos == path || pos == NULL)
307 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
308 if (udev_device_parent != NULL)
309 return udev_device_parent;
312 /* follow "device" link in deprecated sys /sys/class/ layout */
313 if (strncmp(udev_device->devpath, "/class/", 7) == 0) {
314 util_strlcpy(path, udev_device->syspath, sizeof(path));
315 util_strlcat(path, "/device", sizeof(path));
316 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
317 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
318 if (udev_device_parent != NULL)
319 return udev_device_parent;
325 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
327 if (udev_device->parent_device != NULL) {
328 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
329 return udev_device->parent_device;
331 udev_device->parent_device = device_new_from_parent(udev_device);
332 return udev_device->parent_device;
336 * udev_device_get_udev:
337 * @udev_device: udev device
339 * Retrieve the udev library context the device was created with.
341 * Returns: the udev library context
343 struct udev *udev_device_get_udev(struct udev_device *udev_device)
345 if (udev_device == NULL)
347 return udev_device->udev;
352 * @udev_device: udev device
354 * Take a reference of a udev device.
356 * Returns: the passed udev device
358 struct udev_device *udev_device_ref(struct udev_device *udev_device)
360 if (udev_device == NULL)
362 udev_device->refcount++;
368 * @udev_device: udev device
370 * Drop a reference of a udev device. If the refcount reaches zero,
371 * the ressources of the device will be released.
374 void udev_device_unref(struct udev_device *udev_device)
376 if (udev_device == NULL)
378 udev_device->refcount--;
379 if (udev_device->refcount > 0)
381 if (udev_device->parent_device != NULL)
382 udev_device_unref(udev_device->parent_device);
383 free(udev_device->syspath);
384 free(udev_device->devname);
385 free(udev_device->subsystem);
386 list_cleanup(udev_device->udev, &udev_device->link_list);
387 list_cleanup(udev_device->udev, &udev_device->properties_list);
388 free(udev_device->action);
389 free(udev_device->driver);
390 free(udev_device->devpath_old);
391 free(udev_device->physdevpath);
392 list_cleanup(udev_device->udev, &udev_device->attr_list);
393 info(udev_device->udev, "udev_device: %p released\n", udev_device);
398 * udev_device_get_devpath:
399 * @udev_device: udev device
401 * Retrieve the kernel devpath value of the udev device. The path
402 * does not contain the sys mount point, and starts with a '/'.
404 * Returns: the devpath of the udev device
406 const char *udev_device_get_devpath(struct udev_device *udev_device)
408 if (udev_device == NULL)
410 return udev_device->devpath;
414 * udev_device_get_syspath:
415 * @udev_device: udev device
417 * Retrieve the sys path of the udev device. The path is an
418 * absolute path and starts with the sys mount point.
420 * Returns: the sys path of the udev device
422 const char *udev_device_get_syspath(struct udev_device *udev_device)
424 if (udev_device == NULL)
426 return udev_device->syspath;
429 const char *udev_device_get_sysname(struct udev_device *udev_device)
431 if (udev_device == NULL)
433 return udev_device->sysname;
437 * udev_device_get_devnode:
438 * @udev_device: udev device
440 * Retrieve the device node file name belonging to the udev device.
441 * The path is an absolute path, and starts with the device directory.
443 * Returns: the device node file name of the udev device, or #NULL if no device node exists
445 const char *udev_device_get_devnode(struct udev_device *udev_device)
447 if (udev_device == NULL)
449 return udev_device->devname;
453 * udev_device_get_subsystem:
454 * @udev_device: udev device
456 * Retrieve the subsystem string of the udev device. The string does not
459 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
461 const char *udev_device_get_subsystem(struct udev_device *udev_device)
463 char subsystem[UTIL_NAME_SIZE];
465 if (udev_device == NULL)
467 if (udev_device->subsystem != NULL)
468 return udev_device->subsystem;
470 /* read "subsytem" link */
471 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
472 udev_device->subsystem = strdup(subsystem);
473 return udev_device->subsystem;
477 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
478 udev_device->subsystem = strdup("module");
479 return udev_device->subsystem;
481 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
482 udev_device->subsystem = strdup("drivers");
483 return udev_device->subsystem;
485 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
486 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
487 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
488 udev_device->subsystem = strdup("subsystem");
489 return udev_device->subsystem;
495 * udev_device_get_devlinks_list:
496 * @udev_device: udev device
498 * Retrieve the list of device links pointing to the device file of
499 * the udev device. The next list entry can be retrieved with
500 * udev_list_next(), which returns #NULL if no more entries exist.
501 * The devlink path can be retrieved from the list entry by
502 * udev_list_get_name(). The path is an absolute path, and starts with
503 * the device directory.
505 * Returns: the first entry of the device node link list
507 struct udev_list *udev_device_get_devlinks_list(struct udev_device *udev_device)
509 return list_get_entry(&udev_device->link_list);
513 * udev_device_get_properties_list:
514 * @udev_device: udev device
516 * Retrieve the list of key/value device properties of the udev
517 * device. The next list entry can be retrieved with udev_list_next(),
518 * which returns #NULL if no more entries exist. The property name
519 * can be retrieved from the list entry by udev_list_get_name(),
520 * the property value by udev_list_get_value().
522 * Returns: the first entry of the property list
524 struct udev_list *udev_device_get_properties_list(struct udev_device *udev_device)
526 return list_get_entry(&udev_device->properties_list);
529 const char *udev_device_get_driver(struct udev_device *udev_device)
531 char driver[UTIL_NAME_SIZE];
533 if (udev_device == NULL)
535 if (udev_device->driver != NULL)
536 return udev_device->driver;
537 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
539 udev_device->driver = strdup(driver);
540 return udev_device->driver;
543 dev_t udev_device_get_devnum(struct udev_device *udev_device)
545 if (udev_device == NULL)
546 return makedev(0, 0);
547 return udev_device->devnum;
550 const char *udev_device_get_action(struct udev_device *udev_device)
552 if (udev_device == NULL)
554 return udev_device->action;
557 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
559 if (udev_device == NULL)
561 return udev_device->seqnum;
564 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
566 struct udev_list *list;
567 char path[UTIL_PATH_SIZE];
568 char value[UTIL_NAME_SIZE];
572 const char *val = NULL;
574 /* look for possibly already cached result */
575 list = list_get_entry(&udev_device->attr_list);
576 while (list != NULL) {
577 if (strcmp(udev_list_get_name(list), attr) == 0) {
578 info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_get_value(list));
579 return udev_list_get_name(list);
581 list = udev_list_get_next(list);
584 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
585 util_strlcat(path, "/", sizeof(path));
586 util_strlcat(path, attr, sizeof(path));
588 if (lstat(path, &statbuf) != 0) {
589 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
593 if (S_ISLNK(statbuf.st_mode)) {
594 /* links return the last element of the target path */
595 char target[UTIL_NAME_SIZE];
599 len = readlink(path, target, sizeof(target));
602 pos = strrchr(target, '/');
605 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
606 list = list_insert(udev_device->udev, &udev_device->attr_list, attr, pos, 0);
607 val = udev_list_get_value(list);
613 /* skip directories */
614 if (S_ISDIR(statbuf.st_mode))
617 /* skip non-readable files */
618 if ((statbuf.st_mode & S_IRUSR) == 0)
621 /* read attribute value */
622 fd = open(path, O_RDONLY);
624 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
627 size = read(fd, value, sizeof(value));
631 if (size == sizeof(value))
634 /* got a valid value, store it in cache and return it */
636 util_remove_trailing_chars(value, '\n');
637 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
638 list = list_insert(udev_device->udev, &udev_device->attr_list, attr, value, 0);
639 val = udev_list_get_value(list);
643 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
647 udev_device->syspath = strdup(syspath);
648 if (udev_device->syspath == NULL)
650 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
651 pos = strrchr(udev_device->syspath, '/');
654 udev_device->sysname = &pos[1];
658 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
660 udev_device->subsystem = strdup(subsystem);
661 if (udev_device->subsystem == NULL)
666 int device_set_devname(struct udev_device *udev_device, const char *devname)
668 udev_device->devname = strdup(devname);
669 if (udev_device->devname == NULL)
674 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
676 if (list_insert(udev_device->udev, &udev_device->link_list, devlink, NULL, 0) == NULL)
681 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
683 if (list_insert(udev_device->udev, &udev_device->properties_list, key, value, 0) == NULL)
688 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
690 char name[UTIL_PATH_SIZE];
693 strncpy(name, property, sizeof(name));
694 val = strchr(name, '=');
701 device_add_property(udev_device, name, val);
705 int device_set_action(struct udev_device *udev_device, const char *action)
707 udev_device->action = strdup(action);
708 if (udev_device->action == NULL)
713 int device_set_driver(struct udev_device *udev_device, const char *driver)
715 udev_device->driver = strdup(driver);
716 if (udev_device->driver == NULL)
721 const char *device_get_devpath_old(struct udev_device *udev_device)
723 if (udev_device == NULL)
725 return udev_device->devpath_old;
728 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
730 udev_device->devpath_old = strdup(devpath_old);
731 if (udev_device->devpath_old == NULL)
736 const char *device_get_physdevpath(struct udev_device *udev_device)
738 if (udev_device == NULL)
740 return udev_device->physdevpath;
743 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
745 udev_device->physdevpath = strdup(physdevpath);
746 if (udev_device->physdevpath == NULL)
751 int device_get_timeout(struct udev_device *udev_device)
753 if (udev_device == NULL)
755 return udev_device->timeout;
758 int device_set_timeout(struct udev_device *udev_device, int timeout)
760 udev_device->timeout = timeout;
764 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
766 udev_device->seqnum = seqnum;
770 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
772 udev_device->devnum = devnum;
776 int device_get_num_fake_partitions(struct udev_device *udev_device)
778 if (udev_device == NULL)
780 return udev_device->num_fake_partitions;
783 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
785 udev_device->num_fake_partitions = num;
789 int device_get_devlink_priority(struct udev_device *udev_device)
791 if (udev_device == NULL)
793 return udev_device->devlink_priority;
796 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
798 udev_device->devlink_priority = prio;
802 int device_get_ignore_remove(struct udev_device *udev_device)
804 if (udev_device == NULL)
806 return udev_device->ignore_remove;
809 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
811 udev_device->ignore_remove = ignore;