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 info(udev_device->udev, "found a symlink as db file\n");
122 target_len = readlink(filename, target, sizeof(target));
124 target[target_len] = '\0';
126 info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
129 dbg(udev_device->udev, "db link points to '%s'\n", target);
130 if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
135 f = fopen(filename, "r");
137 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
140 while (fgets(line, sizeof(line), f)) {
152 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
155 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
156 util_strlcat(filename, "/", sizeof(filename));
157 util_strlcat(filename, val, sizeof(filename));
158 device_add_devlink(udev_device, filename);
161 device_set_devlink_priority(udev_device, atoi(val));
164 device_set_timeout(udev_device, atoi(val));
167 device_set_num_fake_partitions(udev_device, atoi(val));
170 device_set_ignore_remove(udev_device, atoi(val));
173 device_add_property_from_string(udev_device, val);
179 info(udev_device->udev, "device %p filled with udev database data\n", udev_device);
183 struct udev_device *device_init(struct udev *udev)
185 struct udev_device *udev_device;
190 udev_device = malloc(sizeof(struct udev_device));
191 if (udev_device == NULL)
193 memset(udev_device, 0x00, sizeof(struct udev_device));
194 udev_device->refcount = 1;
195 udev_device->udev = udev;
196 INIT_LIST_HEAD(&udev_device->link_list);
197 INIT_LIST_HEAD(&udev_device->properties_list);
198 INIT_LIST_HEAD(&udev_device->attr_list);
199 info(udev_device->udev, "udev_device: %p created\n", udev_device);
204 * udev_device_new_from_syspath:
205 * @udev: udev library context
206 * @syspath: sys device path including sys directory
208 * Create new udev device, and fill in information from the sys
209 * device and the udev database entry. The sypath is the absolute
210 * path to the device, including the sys mount point.
212 * The initial refcount is 1, and needs to be decremented to
213 * release the ressources of the udev device.
215 * Returns: a new udev device, or #NULL, if it does not exist
217 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
219 char path[UTIL_PATH_SIZE];
221 struct udev_device *udev_device;
228 util_strlcpy(path, syspath, sizeof(path));
229 util_strlcat(path, "/uevent", sizeof(path));
230 if (stat(path, &statbuf) != 0) {
231 info(udev, "not a device :%s\n", syspath);
235 udev_device = device_init(udev);
236 if (udev_device == NULL)
239 /* resolve possible symlink to real path */
240 util_strlcpy(path, syspath, sizeof(path));
241 util_resolve_sys_link(udev, path, sizeof(path));
242 device_set_syspath(udev_device, path);
243 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
245 device_read_uevent_file(udev_device);
246 device_read_db(udev_device);
250 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
252 char path[UTIL_PATH_SIZE];
253 const char *type_str;
254 struct udev_enumerate *enumerate;
255 struct udev_list *list;
256 struct udev_device *device = NULL;
260 else if (type == 'c')
265 /* /sys/dev/{block,char}/<maj>:<min> links */
266 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
267 type_str, major(devnum), minor(devnum));
268 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
269 return udev_device_new_from_syspath(udev, path);
271 /* fallback to search all sys devices for the major/minor */
272 enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
273 if (enumerate == NULL)
275 list = udev_enumerate_get_devices_list(enumerate);
276 while (list != NULL) {
277 struct udev_device *device_loop;
279 device_loop = udev_device_new_from_syspath(udev, udev_list_get_name(list));
280 if (device_loop != NULL) {
281 if (udev_device_get_devnum(device_loop) == devnum) {
282 device = device_loop;
285 udev_device_unref(device_loop);
287 list = udev_list_get_next(list);
289 udev_enumerate_unref(enumerate);
293 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
295 struct udev_device *udev_device_parent = NULL;
296 char path[UTIL_PATH_SIZE];
299 if (udev_device == NULL)
302 util_strlcpy(path, udev_device->syspath, sizeof(path));
304 pos = strrchr(path, '/');
305 if (pos == path || pos == NULL)
308 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
309 if (udev_device_parent != NULL)
310 return udev_device_parent;
313 /* follow "device" link in deprecated sys /sys/class/ layout */
314 if (strncmp(udev_device->devpath, "/class/", 7) == 0) {
315 util_strlcpy(path, udev_device->syspath, sizeof(path));
316 util_strlcat(path, "/device", sizeof(path));
317 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
318 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
319 if (udev_device_parent != NULL)
320 return udev_device_parent;
326 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
328 if (udev_device->parent_device != NULL) {
329 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
330 return udev_device->parent_device;
332 udev_device->parent_device = device_new_from_parent(udev_device);
333 return udev_device->parent_device;
337 * udev_device_get_udev:
338 * @udev_device: udev device
340 * Retrieve the udev library context the device was created with.
342 * Returns: the udev library context
344 struct udev *udev_device_get_udev(struct udev_device *udev_device)
346 if (udev_device == NULL)
348 return udev_device->udev;
353 * @udev_device: udev device
355 * Take a reference of a udev device.
357 * Returns: the passed udev device
359 struct udev_device *udev_device_ref(struct udev_device *udev_device)
361 if (udev_device == NULL)
363 udev_device->refcount++;
369 * @udev_device: udev device
371 * Drop a reference of a udev device. If the refcount reaches zero,
372 * the ressources of the device will be released.
375 void udev_device_unref(struct udev_device *udev_device)
377 if (udev_device == NULL)
379 udev_device->refcount--;
380 if (udev_device->refcount > 0)
382 if (udev_device->parent_device != NULL)
383 udev_device_unref(udev_device->parent_device);
384 free(udev_device->syspath);
385 free(udev_device->devname);
386 free(udev_device->subsystem);
387 list_cleanup(udev_device->udev, &udev_device->link_list);
388 list_cleanup(udev_device->udev, &udev_device->properties_list);
389 free(udev_device->action);
390 free(udev_device->driver);
391 free(udev_device->devpath_old);
392 free(udev_device->physdevpath);
393 list_cleanup(udev_device->udev, &udev_device->attr_list);
394 info(udev_device->udev, "udev_device: %p released\n", udev_device);
399 * udev_device_get_devpath:
400 * @udev_device: udev device
402 * Retrieve the kernel devpath value of the udev device. The path
403 * does not contain the sys mount point, and starts with a '/'.
405 * Returns: the devpath of the udev device
407 const char *udev_device_get_devpath(struct udev_device *udev_device)
409 if (udev_device == NULL)
411 return udev_device->devpath;
415 * udev_device_get_syspath:
416 * @udev_device: udev device
418 * Retrieve the sys path of the udev device. The path is an
419 * absolute path and starts with the sys mount point.
421 * Returns: the sys path of the udev device
423 const char *udev_device_get_syspath(struct udev_device *udev_device)
425 if (udev_device == NULL)
427 return udev_device->syspath;
430 const char *udev_device_get_sysname(struct udev_device *udev_device)
432 if (udev_device == NULL)
434 return udev_device->sysname;
438 * udev_device_get_devnode:
439 * @udev_device: udev device
441 * Retrieve the device node file name belonging to the udev device.
442 * The path is an absolute path, and starts with the device directory.
444 * Returns: the device node file name of the udev device, or #NULL if no device node exists
446 const char *udev_device_get_devnode(struct udev_device *udev_device)
448 if (udev_device == NULL)
450 return udev_device->devname;
454 * udev_device_get_subsystem:
455 * @udev_device: udev device
457 * Retrieve the subsystem string of the udev device. The string does not
460 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
462 const char *udev_device_get_subsystem(struct udev_device *udev_device)
464 char subsystem[UTIL_NAME_SIZE];
466 if (udev_device == NULL)
468 if (udev_device->subsystem != NULL)
469 return udev_device->subsystem;
471 /* read "subsytem" link */
472 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
473 udev_device->subsystem = strdup(subsystem);
474 return udev_device->subsystem;
478 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
479 udev_device->subsystem = strdup("module");
480 return udev_device->subsystem;
482 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
483 udev_device->subsystem = strdup("drivers");
484 return udev_device->subsystem;
486 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
487 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
488 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
489 udev_device->subsystem = strdup("subsystem");
490 return udev_device->subsystem;
496 * udev_device_get_devlinks_list:
497 * @udev_device: udev device
499 * Retrieve the list of device links pointing to the device file of
500 * the udev device. The next list entry can be retrieved with
501 * udev_list_next(), which returns #NULL if no more entries exist.
502 * The devlink path can be retrieved from the list entry by
503 * udev_list_get_name(). The path is an absolute path, and starts with
504 * the device directory.
506 * Returns: the first entry of the device node link list
508 struct udev_list *udev_device_get_devlinks_list(struct udev_device *udev_device)
510 return list_get_entry(&udev_device->link_list);
514 * udev_device_get_properties_list:
515 * @udev_device: udev device
517 * Retrieve the list of key/value device properties of the udev
518 * device. The next list entry can be retrieved with udev_list_next(),
519 * which returns #NULL if no more entries exist. The property name
520 * can be retrieved from the list entry by udev_list_get_name(),
521 * the property value by udev_list_get_value().
523 * Returns: the first entry of the property list
525 struct udev_list *udev_device_get_properties_list(struct udev_device *udev_device)
527 return list_get_entry(&udev_device->properties_list);
530 const char *udev_device_get_driver(struct udev_device *udev_device)
532 char driver[UTIL_NAME_SIZE];
534 if (udev_device == NULL)
536 if (udev_device->driver != NULL)
537 return udev_device->driver;
538 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
540 udev_device->driver = strdup(driver);
541 return udev_device->driver;
544 dev_t udev_device_get_devnum(struct udev_device *udev_device)
546 if (udev_device == NULL)
547 return makedev(0, 0);
548 return udev_device->devnum;
551 const char *udev_device_get_action(struct udev_device *udev_device)
553 if (udev_device == NULL)
555 return udev_device->action;
558 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
560 if (udev_device == NULL)
562 return udev_device->seqnum;
565 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
567 struct udev_list *list;
568 char path[UTIL_PATH_SIZE];
569 char value[UTIL_NAME_SIZE];
573 const char *val = NULL;
575 /* look for possibly already cached result */
576 list = list_get_entry(&udev_device->attr_list);
577 while (list != NULL) {
578 if (strcmp(udev_list_get_name(list), attr) == 0) {
579 info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_get_value(list));
580 return udev_list_get_name(list);
582 list = udev_list_get_next(list);
585 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
586 util_strlcat(path, "/", sizeof(path));
587 util_strlcat(path, attr, sizeof(path));
589 if (lstat(path, &statbuf) != 0) {
590 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
594 if (S_ISLNK(statbuf.st_mode)) {
595 /* links return the last element of the target path */
596 char target[UTIL_NAME_SIZE];
600 len = readlink(path, target, sizeof(target));
603 pos = strrchr(target, '/');
606 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
607 list = list_insert(udev_device->udev, &udev_device->attr_list, attr, pos, 0);
608 val = udev_list_get_value(list);
614 /* skip directories */
615 if (S_ISDIR(statbuf.st_mode))
618 /* skip non-readable files */
619 if ((statbuf.st_mode & S_IRUSR) == 0)
622 /* read attribute value */
623 fd = open(path, O_RDONLY);
625 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
628 size = read(fd, value, sizeof(value));
632 if (size == sizeof(value))
635 /* got a valid value, store it in cache and return it */
637 util_remove_trailing_chars(value, '\n');
638 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
639 list = list_insert(udev_device->udev, &udev_device->attr_list, attr, value, 0);
640 val = udev_list_get_value(list);
644 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
648 udev_device->syspath = strdup(syspath);
649 if (udev_device->syspath == NULL)
651 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
652 pos = strrchr(udev_device->syspath, '/');
655 udev_device->sysname = &pos[1];
659 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
661 udev_device->subsystem = strdup(subsystem);
662 if (udev_device->subsystem == NULL)
667 int device_set_devname(struct udev_device *udev_device, const char *devname)
669 udev_device->devname = strdup(devname);
670 if (udev_device->devname == NULL)
675 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
677 if (list_insert(udev_device->udev, &udev_device->link_list, devlink, NULL, 0) == NULL)
682 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
684 if (list_insert(udev_device->udev, &udev_device->properties_list, key, value, 0) == NULL)
689 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
691 char name[UTIL_PATH_SIZE];
694 strncpy(name, property, sizeof(name));
695 val = strchr(name, '=');
702 device_add_property(udev_device, name, val);
706 int device_set_action(struct udev_device *udev_device, const char *action)
708 udev_device->action = strdup(action);
709 if (udev_device->action == NULL)
714 int device_set_driver(struct udev_device *udev_device, const char *driver)
716 udev_device->driver = strdup(driver);
717 if (udev_device->driver == NULL)
722 const char *device_get_devpath_old(struct udev_device *udev_device)
724 if (udev_device == NULL)
726 return udev_device->devpath_old;
729 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
731 udev_device->devpath_old = strdup(devpath_old);
732 if (udev_device->devpath_old == NULL)
737 const char *device_get_physdevpath(struct udev_device *udev_device)
739 if (udev_device == NULL)
741 return udev_device->physdevpath;
744 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
746 udev_device->physdevpath = strdup(physdevpath);
747 if (udev_device->physdevpath == NULL)
752 int device_get_timeout(struct udev_device *udev_device)
754 if (udev_device == NULL)
756 return udev_device->timeout;
759 int device_set_timeout(struct udev_device *udev_device, int timeout)
761 udev_device->timeout = timeout;
765 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
767 udev_device->seqnum = seqnum;
771 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
773 udev_device->devnum = devnum;
777 int device_get_num_fake_partitions(struct udev_device *udev_device)
779 if (udev_device == NULL)
781 return udev_device->num_fake_partitions;
784 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
786 udev_device->num_fake_partitions = num;
790 int device_get_devlink_priority(struct udev_device *udev_device)
792 if (udev_device == NULL)
794 return udev_device->devlink_priority;
797 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
799 udev_device->devlink_priority = prio;
803 int device_get_ignore_remove(struct udev_device *udev_device)
805 if (udev_device == NULL)
807 return udev_device->ignore_remove;
810 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
812 udev_device->ignore_remove = ignore;