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 env_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->env_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;
253 else if (type == 'c')
258 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
259 type_str, major(devnum), minor(devnum));
260 if (util_resolve_sys_link(udev, path, sizeof(path)) < 0)
263 return udev_device_new_from_syspath(udev, path);
266 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
268 struct udev_device *udev_device_parent = NULL;
269 char path[UTIL_PATH_SIZE];
272 if (udev_device == NULL)
275 util_strlcpy(path, udev_device->syspath, sizeof(path));
277 pos = strrchr(path, '/');
278 if (pos == path || pos == NULL)
281 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
282 if (udev_device_parent != NULL)
283 return udev_device_parent;
286 /* follow "device" link in deprecated sys /sys/class/ layout */
287 if (strncmp(udev_device->devpath, "/class/", 7) == 0) {
288 util_strlcpy(path, udev_device->syspath, sizeof(path));
289 util_strlcat(path, "/device", sizeof(path));
290 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
291 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
292 if (udev_device_parent != NULL)
293 return udev_device_parent;
299 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
301 if (udev_device->parent_device != NULL) {
302 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
303 return udev_device->parent_device;
305 udev_device->parent_device = device_new_from_parent(udev_device);
306 return udev_device->parent_device;
310 * udev_device_get_udev:
311 * @udev_device: udev device
313 * Retrieve the udev library context the device was created with.
315 * Returns: the udev library context
317 struct udev *udev_device_get_udev(struct udev_device *udev_device)
319 if (udev_device == NULL)
321 return udev_device->udev;
326 * @udev_device: udev device
328 * Take a reference of a udev device.
330 * Returns: the passed udev device
332 struct udev_device *udev_device_ref(struct udev_device *udev_device)
334 if (udev_device == NULL)
336 udev_device->refcount++;
342 * @udev_device: udev device
344 * Drop a reference of a udev device. If the refcount reaches zero,
345 * the ressources of the device will be released.
348 void udev_device_unref(struct udev_device *udev_device)
350 if (udev_device == NULL)
352 udev_device->refcount--;
353 if (udev_device->refcount > 0)
355 if (udev_device->parent_device != NULL)
356 udev_device_unref(udev_device->parent_device);
357 free(udev_device->syspath);
358 free(udev_device->devname);
359 free(udev_device->subsystem);
360 util_name_list_cleanup(udev_device->udev, &udev_device->link_list);
361 util_name_list_cleanup(udev_device->udev, &udev_device->env_list);
362 free(udev_device->action);
363 free(udev_device->driver);
364 free(udev_device->devpath_old);
365 free(udev_device->physdevpath);
366 util_name_list_cleanup(udev_device->udev, &udev_device->attr_list);
367 info(udev_device->udev, "udev_device: %p released\n", udev_device);
372 * udev_device_get_devpath:
373 * @udev_device: udev device
375 * Retrieve the kernel devpath value of the udev device. The path
376 * does not contain the sys mount point, and starts with a '/'.
378 * Returns: the devpath of the udev device
380 const char *udev_device_get_devpath(struct udev_device *udev_device)
382 if (udev_device == NULL)
384 return udev_device->devpath;
388 * udev_device_get_syspath:
389 * @udev_device: udev device
391 * Retrieve the sys path of the udev device. The path is an
392 * absolute path and starts with the sys mount point.
394 * Returns: the sys path of the udev device
396 const char *udev_device_get_syspath(struct udev_device *udev_device)
398 if (udev_device == NULL)
400 return udev_device->syspath;
403 const char *udev_device_get_sysname(struct udev_device *udev_device)
405 if (udev_device == NULL)
407 return udev_device->sysname;
411 * udev_device_get_devnode:
412 * @udev_device: udev device
414 * Retrieve the device node file name belonging to the udev device.
415 * The path is an absolute path, and starts with the device directory.
417 * Returns: the device node file name of the udev device, or #NULL if no device node exists
419 const char *udev_device_get_devnode(struct udev_device *udev_device)
421 if (udev_device == NULL)
423 return udev_device->devname;
427 * udev_device_get_subsystem:
428 * @udev_device: udev device
430 * Retrieve the subsystem string of the udev device. The string does not
433 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
435 const char *udev_device_get_subsystem(struct udev_device *udev_device)
437 char subsystem[UTIL_NAME_SIZE];
439 if (udev_device == NULL)
441 if (udev_device->subsystem != NULL)
442 return udev_device->subsystem;
444 /* read "subsytem" link */
445 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
446 udev_device->subsystem = strdup(subsystem);
447 return udev_device->subsystem;
451 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
452 udev_device->subsystem = strdup("module");
453 return udev_device->subsystem;
455 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
456 udev_device->subsystem = strdup("drivers");
457 return udev_device->subsystem;
459 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
460 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
461 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
462 udev_device->subsystem = strdup("subsystem");
463 return udev_device->subsystem;
469 * udev_device_get_devlinks:
470 * @udev_device: udev device
471 * @cb: function to be called for every device link found
472 * @data: data to be passed to the function
474 * Retrieve the device links pointing to the device file of the
475 * udev device. For every device link, the passed function will be
476 * called with the device link string.
477 * The path is an absolute path, and starts with the device directory.
478 * If the function returns 1, remaning device links will be ignored.
480 * Returns: the number of device links passed to the caller, or a negative value on error
482 int udev_device_get_devlinks(struct udev_device *udev_device,
483 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
486 struct util_name_entry *name_loop;
489 if (udev_device == NULL)
491 list_for_each_entry(name_loop, &udev_device->link_list, node) {
493 if (cb(udev_device, name_loop->name, data) != 0)
500 * udev_device_get_properties:
501 * @udev_device: udev device
502 * @cb: function to be called for every property found
503 * @data: data to be passed to the function
505 * Retrieve the property key/value pairs belonging to the
506 * udev device. For every key/value pair, the passed function will be
507 * called. If the function returns 1, remaning properties will be
510 * Returns: the number of properties passed to the caller, or a negative value on error
512 int udev_device_get_properties(struct udev_device *udev_device,
513 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
516 struct util_name_entry *name_loop;
519 if (udev_device == NULL)
521 list_for_each_entry(name_loop, &udev_device->env_list, node) {
523 if (cb(udev_device, name_loop->name, name_loop->value, data) != 0)
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 util_name_entry *name_loop;
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_for_each_entry(name_loop, &udev_device->attr_list, node) {
576 if (strcmp(name_loop->name, attr) == 0) {
577 info(udev_device->udev, "'%s' in cache '%s'\n", attr, name_loop->value);
578 return name_loop->value;
582 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
583 util_strlcat(path, "/", sizeof(path));
584 util_strlcat(path, attr, sizeof(path));
586 if (lstat(path, &statbuf) != 0) {
587 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
591 if (S_ISLNK(statbuf.st_mode)) {
592 /* links return the last element of the target path */
593 char target[UTIL_NAME_SIZE];
597 len = readlink(path, target, sizeof(target));
600 pos = strrchr(target, '/');
603 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
604 val = util_name_list_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0)->value;
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 val = util_name_list_add(udev_device->udev, &udev_device->attr_list, attr, value, 0)->value;
639 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
643 udev_device->syspath = strdup(syspath);
644 if (udev_device->syspath == NULL)
646 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
647 pos = strrchr(udev_device->syspath, '/');
650 udev_device->sysname = &pos[1];
654 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
656 udev_device->subsystem = strdup(subsystem);
657 if (udev_device->subsystem == NULL)
662 int device_set_devname(struct udev_device *udev_device, const char *devname)
664 udev_device->devname = strdup(devname);
665 if (udev_device->devname == NULL)
670 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
672 if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, NULL, 0) == NULL)
677 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
679 if (util_name_list_add(udev_device->udev, &udev_device->env_list, key, value, 0) == NULL)
684 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
686 char name[UTIL_PATH_SIZE];
689 strncpy(name, property, sizeof(name));
690 val = strchr(name, '=');
697 device_add_property(udev_device, name, val);
701 int device_set_action(struct udev_device *udev_device, const char *action)
703 udev_device->action = strdup(action);
704 if (udev_device->action == NULL)
709 int device_set_driver(struct udev_device *udev_device, const char *driver)
711 udev_device->driver = strdup(driver);
712 if (udev_device->driver == NULL)
717 const char *device_get_devpath_old(struct udev_device *udev_device)
719 if (udev_device == NULL)
721 return udev_device->devpath_old;
724 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
726 udev_device->devpath_old = strdup(devpath_old);
727 if (udev_device->devpath_old == NULL)
732 const char *device_get_physdevpath(struct udev_device *udev_device)
734 if (udev_device == NULL)
736 return udev_device->physdevpath;
739 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
741 udev_device->physdevpath = strdup(physdevpath);
742 if (udev_device->physdevpath == NULL)
747 int device_get_timeout(struct udev_device *udev_device)
749 if (udev_device == NULL)
751 return udev_device->timeout;
754 int device_set_timeout(struct udev_device *udev_device, int timeout)
756 udev_device->timeout = timeout;
760 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
762 udev_device->seqnum = seqnum;
766 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
768 udev_device->devnum = devnum;
772 int device_get_num_fake_partitions(struct udev_device *udev_device)
774 if (udev_device == NULL)
776 return udev_device->num_fake_partitions;
779 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
781 udev_device->num_fake_partitions = num;
785 int device_get_devlink_priority(struct udev_device *udev_device)
787 if (udev_device == NULL)
789 return udev_device->devlink_priority;
792 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
794 udev_device->devlink_priority = prio;
798 int device_get_ignore_remove(struct udev_device *udev_device)
800 if (udev_device == NULL)
802 return udev_device->ignore_remove;
805 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
807 udev_device->ignore_remove = ignore;