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];
74 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
75 util_strlcat(filename, "/uevent", sizeof(filename));
76 f = fopen(filename, "r");
80 while (fgets(line, sizeof(line), f)) {
83 pos = strchr(line, '\n');
87 device_add_property_from_string(udev_device, line);
94 static int device_read_db(struct udev_device *udev_device)
97 char filename[UTIL_PATH_SIZE];
98 char line[UTIL_LINE_SIZE];
102 syspath_to_db_path(udev_device, filename, sizeof(filename));
104 if (lstat(filename, &stats) != 0) {
105 info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno));
108 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
109 char target[UTIL_PATH_SIZE];
112 info(udev_device->udev, "found a symlink as db file\n");
113 target_len = readlink(filename, target, sizeof(target));
115 target[target_len] = '\0';
117 info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
120 dbg(udev_device->udev, "db link points to '%s'\n", target);
121 if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
126 f = fopen(filename, "r");
128 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
131 while (fgets(line, sizeof(line), f)) {
134 unsigned int maj, min;
144 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
147 sscanf(val, "%u:%u", &maj, &min);
148 device_set_devnum(udev_device, makedev(maj, min));
151 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
152 util_strlcat(filename, "/", sizeof(filename));
153 util_strlcat(filename, val, sizeof(filename));
154 device_add_devlink(udev_device, filename);
157 device_set_devlink_priority(udev_device, atoi(val));
160 device_set_timeout(udev_device, atoi(val));
163 device_set_num_fake_partitions(udev_device, atoi(val));
166 device_set_ignore_remove(udev_device, atoi(val));
169 device_add_property_from_string(udev_device, val);
178 struct udev_device *device_init(struct udev *udev)
180 struct udev_device *udev_device;
185 udev_device = malloc(sizeof(struct udev_device));
186 if (udev_device == NULL)
188 memset(udev_device, 0x00, sizeof(struct udev_device));
189 udev_device->refcount = 1;
190 udev_device->udev = udev;
191 INIT_LIST_HEAD(&udev_device->link_list);
192 INIT_LIST_HEAD(&udev_device->properties_list);
193 INIT_LIST_HEAD(&udev_device->attr_list);
194 info(udev_device->udev, "udev_device: %p created\n", udev_device);
199 * udev_device_new_from_syspath:
200 * @udev: udev library context
201 * @syspath: sys device path including sys directory
203 * Create new udev device, and fill in information from the sys
204 * device and the udev database entry. The sypath is the absolute
205 * path to the device, including the sys mount point.
207 * The initial refcount is 1, and needs to be decremented to
208 * release the ressources of the udev device.
210 * Returns: a new udev device, or #NULL, if it does not exist
212 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
214 char path[UTIL_PATH_SIZE];
216 struct udev_device *udev_device;
223 util_strlcpy(path, syspath, sizeof(path));
224 util_strlcat(path, "/uevent", sizeof(path));
225 if (stat(path, &statbuf) != 0) {
226 info(udev, "not a device :%s\n", syspath);
230 udev_device = device_init(udev);
231 if (udev_device == NULL)
234 /* resolve possible symlink to real path */
235 util_strlcpy(path, syspath, sizeof(path));
236 util_resolve_sys_link(udev, path, sizeof(path));
237 device_set_syspath(udev_device, path);
238 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
240 device_read_uevent_file(udev_device);
241 if (device_read_db(udev_device) >= 0)
242 info(udev, "device %p filled with udev database data\n", udev_device);
246 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
248 char path[UTIL_PATH_SIZE];
249 const char *type_str;
250 struct udev_enumerate *enumerate;
251 struct udev_list *list;
252 struct udev_device *device = NULL;
256 else if (type == 'c')
261 /* /sys/dev/{block,char}/<maj>:<min> links */
262 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
263 type_str, major(devnum), minor(devnum));
264 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
265 return udev_device_new_from_syspath(udev, path);
267 /* search all sys devices for the major/minor */
268 enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
269 if (enumerate == NULL)
271 list = udev_enumerate_get_devices_list(enumerate);
272 while (list != NULL) {
273 struct udev_device *device_loop;
275 device_loop = udev_device_new_from_syspath(udev, udev_list_get_name(list));
276 if (device_loop != NULL) {
277 if (udev_device_get_devnum(device_loop) == devnum) {
278 device = device_loop;
281 udev_device_unref(device_loop);
283 list = udev_list_get_next(list);
285 udev_enumerate_unref(enumerate);
289 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
291 struct udev_device *udev_device_parent = NULL;
292 char path[UTIL_PATH_SIZE];
295 if (udev_device == NULL)
298 util_strlcpy(path, udev_device->syspath, sizeof(path));
300 pos = strrchr(path, '/');
301 if (pos == path || pos == NULL)
304 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
305 if (udev_device_parent != NULL)
306 return udev_device_parent;
309 /* follow "device" link in deprecated sys /sys/class/ layout */
310 if (strncmp(udev_device->devpath, "/class/", 7) == 0) {
311 util_strlcpy(path, udev_device->syspath, sizeof(path));
312 util_strlcat(path, "/device", sizeof(path));
313 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
314 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
315 if (udev_device_parent != NULL)
316 return udev_device_parent;
322 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
324 if (udev_device->parent_device != NULL) {
325 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
326 return udev_device->parent_device;
328 udev_device->parent_device = device_new_from_parent(udev_device);
329 return udev_device->parent_device;
333 * udev_device_get_udev:
334 * @udev_device: udev device
336 * Retrieve the udev library context the device was created with.
338 * Returns: the udev library context
340 struct udev *udev_device_get_udev(struct udev_device *udev_device)
342 if (udev_device == NULL)
344 return udev_device->udev;
349 * @udev_device: udev device
351 * Take a reference of a udev device.
353 * Returns: the passed udev device
355 struct udev_device *udev_device_ref(struct udev_device *udev_device)
357 if (udev_device == NULL)
359 udev_device->refcount++;
365 * @udev_device: udev device
367 * Drop a reference of a udev device. If the refcount reaches zero,
368 * the ressources of the device will be released.
371 void udev_device_unref(struct udev_device *udev_device)
373 if (udev_device == NULL)
375 udev_device->refcount--;
376 if (udev_device->refcount > 0)
378 if (udev_device->parent_device != NULL)
379 udev_device_unref(udev_device->parent_device);
380 free(udev_device->syspath);
381 free(udev_device->devname);
382 free(udev_device->subsystem);
383 list_cleanup(udev_device->udev, &udev_device->link_list);
384 list_cleanup(udev_device->udev, &udev_device->properties_list);
385 free(udev_device->action);
386 free(udev_device->driver);
387 free(udev_device->devpath_old);
388 free(udev_device->physdevpath);
389 list_cleanup(udev_device->udev, &udev_device->attr_list);
390 info(udev_device->udev, "udev_device: %p released\n", udev_device);
395 * udev_device_get_devpath:
396 * @udev_device: udev device
398 * Retrieve the kernel devpath value of the udev device. The path
399 * does not contain the sys mount point, and starts with a '/'.
401 * Returns: the devpath of the udev device
403 const char *udev_device_get_devpath(struct udev_device *udev_device)
405 if (udev_device == NULL)
407 return udev_device->devpath;
411 * udev_device_get_syspath:
412 * @udev_device: udev device
414 * Retrieve the sys path of the udev device. The path is an
415 * absolute path and starts with the sys mount point.
417 * Returns: the sys path of the udev device
419 const char *udev_device_get_syspath(struct udev_device *udev_device)
421 if (udev_device == NULL)
423 return udev_device->syspath;
426 const char *udev_device_get_sysname(struct udev_device *udev_device)
428 if (udev_device == NULL)
430 return udev_device->sysname;
434 * udev_device_get_devnode:
435 * @udev_device: udev device
437 * Retrieve the device node file name belonging to the udev device.
438 * The path is an absolute path, and starts with the device directory.
440 * Returns: the device node file name of the udev device, or #NULL if no device node exists
442 const char *udev_device_get_devnode(struct udev_device *udev_device)
444 if (udev_device == NULL)
446 return udev_device->devname;
450 * udev_device_get_subsystem:
451 * @udev_device: udev device
453 * Retrieve the subsystem string of the udev device. The string does not
456 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
458 const char *udev_device_get_subsystem(struct udev_device *udev_device)
460 char subsystem[UTIL_NAME_SIZE];
462 if (udev_device == NULL)
464 if (udev_device->subsystem != NULL)
465 return udev_device->subsystem;
467 /* read "subsytem" link */
468 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
469 udev_device->subsystem = strdup(subsystem);
470 return udev_device->subsystem;
474 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
475 udev_device->subsystem = strdup("module");
476 return udev_device->subsystem;
478 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
479 udev_device->subsystem = strdup("drivers");
480 return udev_device->subsystem;
482 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
483 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
484 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
485 udev_device->subsystem = strdup("subsystem");
486 return udev_device->subsystem;
492 * udev_device_get_devlinks_list:
493 * @udev_device: udev device
495 * Retrieve the list of device links pointing to the device file of
496 * the udev device. The next list entry can be retrieved with
497 * udev_list_next(), which returns #NULL if no more entries exist.
498 * The devlink path can be retrieved from the list entry by
499 * udev_list_get_name(). The path is an absolute path, and starts with
500 * the device directory.
502 * Returns: the first entry of the device node link list
504 struct udev_list *udev_device_get_devlinks_list(struct udev_device *udev_device)
506 return list_get_entry(&udev_device->link_list);
510 * udev_device_get_properties_list:
511 * @udev_device: udev device
513 * Retrieve the list of key/value device properties of the udev
514 * device. The next list entry can be retrieved with udev_list_next(),
515 * which returns #NULL if no more entries exist. The property name
516 * can be retrieved from the list entry by udev_list_get_name(),
517 * the property value by udev_list_get_value().
519 * Returns: the first entry of the property list
521 struct udev_list *udev_device_get_properties_list(struct udev_device *udev_device)
523 return list_get_entry(&udev_device->properties_list);
526 const char *udev_device_get_driver(struct udev_device *udev_device)
528 char driver[UTIL_NAME_SIZE];
530 if (udev_device == NULL)
532 if (udev_device->driver != NULL)
533 return udev_device->driver;
534 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
536 udev_device->driver = strdup(driver);
537 return udev_device->driver;
540 dev_t udev_device_get_devnum(struct udev_device *udev_device)
542 if (udev_device == NULL)
543 return makedev(0, 0);
544 return udev_device->devnum;
547 const char *udev_device_get_action(struct udev_device *udev_device)
549 if (udev_device == NULL)
551 return udev_device->action;
554 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
556 if (udev_device == NULL)
558 return udev_device->seqnum;
561 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
563 struct udev_list *list;
564 char path[UTIL_PATH_SIZE];
565 char value[UTIL_NAME_SIZE];
569 const char *val = NULL;
571 /* look for possibly already cached result */
572 list = list_get_entry(&udev_device->attr_list);
573 while (list != NULL) {
574 if (strcmp(udev_list_get_name(list), attr) == 0) {
575 info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_get_value(list));
576 return udev_list_get_name(list);
578 list = udev_list_get_next(list);
581 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
582 util_strlcat(path, "/", sizeof(path));
583 util_strlcat(path, attr, sizeof(path));
585 if (lstat(path, &statbuf) != 0) {
586 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
590 if (S_ISLNK(statbuf.st_mode)) {
591 /* links return the last element of the target path */
592 char target[UTIL_NAME_SIZE];
596 len = readlink(path, target, sizeof(target));
599 pos = strrchr(target, '/');
602 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
603 list = list_insert(udev_device->udev, &udev_device->attr_list, attr, pos, 0);
604 val = udev_list_get_value(list);
610 /* skip directories */
611 if (S_ISDIR(statbuf.st_mode))
614 /* skip non-readable files */
615 if ((statbuf.st_mode & S_IRUSR) == 0)
618 /* read attribute value */
619 fd = open(path, O_RDONLY);
621 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
624 size = read(fd, value, sizeof(value));
628 if (size == sizeof(value))
631 /* got a valid value, store it in cache and return it */
633 util_remove_trailing_chars(value, '\n');
634 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
635 list = list_insert(udev_device->udev, &udev_device->attr_list, attr, value, 0);
636 val = udev_list_get_value(list);
640 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
644 udev_device->syspath = strdup(syspath);
645 if (udev_device->syspath == NULL)
647 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
648 pos = strrchr(udev_device->syspath, '/');
651 udev_device->sysname = &pos[1];
655 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
657 udev_device->subsystem = strdup(subsystem);
658 if (udev_device->subsystem == NULL)
663 int device_set_devname(struct udev_device *udev_device, const char *devname)
665 udev_device->devname = strdup(devname);
666 if (udev_device->devname == NULL)
671 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
673 if (list_insert(udev_device->udev, &udev_device->link_list, devlink, NULL, 0) == NULL)
678 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
680 if (list_insert(udev_device->udev, &udev_device->properties_list, key, value, 0) == NULL)
685 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
687 char name[UTIL_PATH_SIZE];
690 strncpy(name, property, sizeof(name));
691 val = strchr(name, '=');
698 device_add_property(udev_device, name, val);
702 int device_set_action(struct udev_device *udev_device, const char *action)
704 udev_device->action = strdup(action);
705 if (udev_device->action == NULL)
710 int device_set_driver(struct udev_device *udev_device, const char *driver)
712 udev_device->driver = strdup(driver);
713 if (udev_device->driver == NULL)
718 const char *device_get_devpath_old(struct udev_device *udev_device)
720 if (udev_device == NULL)
722 return udev_device->devpath_old;
725 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
727 udev_device->devpath_old = strdup(devpath_old);
728 if (udev_device->devpath_old == NULL)
733 const char *device_get_physdevpath(struct udev_device *udev_device)
735 if (udev_device == NULL)
737 return udev_device->physdevpath;
740 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
742 udev_device->physdevpath = strdup(physdevpath);
743 if (udev_device->physdevpath == NULL)
748 int device_get_timeout(struct udev_device *udev_device)
750 if (udev_device == NULL)
752 return udev_device->timeout;
755 int device_set_timeout(struct udev_device *udev_device, int timeout)
757 udev_device->timeout = timeout;
761 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
763 udev_device->seqnum = seqnum;
767 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
769 udev_device->devnum = devnum;
773 int device_get_num_fake_partitions(struct udev_device *udev_device)
775 if (udev_device == NULL)
777 return udev_device->num_fake_partitions;
780 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
782 udev_device->num_fake_partitions = num;
786 int device_get_devlink_priority(struct udev_device *udev_device)
788 if (udev_device == NULL)
790 return udev_device->devlink_priority;
793 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
795 udev_device->devlink_priority = prio;
799 int device_get_ignore_remove(struct udev_device *udev_device)
801 if (udev_device == NULL)
803 return udev_device->ignore_remove;
806 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
808 udev_device->ignore_remove = ignore;