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_db(struct udev_device *udev_device)
71 char filename[UTIL_PATH_SIZE];
72 char line[UTIL_LINE_SIZE];
76 syspath_to_db_path(udev_device, filename, sizeof(filename));
78 if (lstat(filename, &stats) != 0) {
79 info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno));
82 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
83 char target[UTIL_PATH_SIZE];
86 info(udev_device->udev, "found a symlink as db file\n");
87 target_len = readlink(filename, target, sizeof(target));
89 target[target_len] = '\0';
91 info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
94 dbg(udev_device->udev, "db link points to '%s'\n", target);
95 if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
100 f = fopen(filename, "r");
102 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
105 while (fgets(line, sizeof(line), f)) {
108 unsigned int maj, min;
118 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
121 sscanf(val, "%u:%u", &maj, &min);
122 device_set_devnum(udev_device, makedev(maj, min));
125 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
126 util_strlcat(filename, "/", sizeof(filename));
127 util_strlcat(filename, val, sizeof(filename));
128 device_add_devlink(udev_device, filename);
131 device_set_devlink_priority(udev_device, atoi(val));
134 device_set_timeout(udev_device, atoi(val));
137 device_set_num_fake_partitions(udev_device, atoi(val));
140 device_set_ignore_remove(udev_device, atoi(val));
143 device_add_property_from_string(udev_device, val);
152 struct udev_device *device_init(struct udev *udev)
154 struct udev_device *udev_device;
159 udev_device = malloc(sizeof(struct udev_device));
160 if (udev_device == NULL)
162 memset(udev_device, 0x00, sizeof(struct udev_device));
163 udev_device->refcount = 1;
164 udev_device->udev = udev;
165 INIT_LIST_HEAD(&udev_device->link_list);
166 INIT_LIST_HEAD(&udev_device->env_list);
167 INIT_LIST_HEAD(&udev_device->attr_list);
168 info(udev_device->udev, "udev_device: %p created\n", udev_device);
173 * udev_device_new_from_syspath:
174 * @udev: udev library context
175 * @syspath: sys device path including sys directory
177 * Create new udev device, and fill in information from the sys
178 * device and the udev database entry. The sypath is the absolute
179 * path to the device, including the sys mount point.
181 * The initial refcount is 1, and needs to be decremented to
182 * release the ressources of the udev device.
184 * Returns: a new udev device, or #NULL, if it does not exist
186 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
188 char path[UTIL_PATH_SIZE];
190 struct udev_device *udev_device;
197 util_strlcpy(path, syspath, sizeof(path));
198 util_strlcat(path, "/uevent", sizeof(path));
199 if (stat(path, &statbuf) != 0) {
200 info(udev, "not a device :%s\n", syspath);
204 udev_device = device_init(udev);
205 if (udev_device == NULL)
208 /* resolve possible symlink to real path */
209 util_strlcpy(path, syspath, sizeof(path));
210 util_resolve_sys_link(udev, path, sizeof(path));
211 device_set_syspath(udev_device, path);
212 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
214 if (device_read_db(udev_device) >= 0)
215 info(udev, "device %p filled with udev database data\n", udev_device);
219 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
221 char path[UTIL_PATH_SIZE];
223 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
224 udev_get_sys_path(udev),
225 type == 'b' ? "block" : "char",
226 major(devnum), minor(devnum));
227 if (util_resolve_sys_link(udev, path, sizeof(path)) < 0)
230 return udev_device_new_from_syspath(udev, path);
233 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
235 struct udev_device *udev_device_parent = NULL;
236 char path[UTIL_PATH_SIZE];
239 if (udev_device == NULL)
242 util_strlcpy(path, udev_device->syspath, sizeof(path));
244 pos = strrchr(path, '/');
245 if (pos == path || pos == NULL)
248 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
249 if (udev_device_parent != NULL)
250 return udev_device_parent;
253 /* follow "device" link in deprecated sys /sys/class/ layout */
254 if (strncmp(udev_device->devpath, "/class/", 7) == 0) {
255 util_strlcpy(path, udev_device->syspath, sizeof(path));
256 util_strlcat(path, "/device", sizeof(path));
257 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
258 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
259 if (udev_device_parent != NULL)
260 return udev_device_parent;
266 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
268 if (udev_device->parent_device != NULL) {
269 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
270 return udev_device->parent_device;
272 udev_device->parent_device = device_new_from_parent(udev_device);
273 return udev_device->parent_device;
277 * udev_device_get_udev:
278 * @udev_device: udev device
280 * Retrieve the udev library context the device was created with.
282 * Returns: the udev library context
284 struct udev *udev_device_get_udev(struct udev_device *udev_device)
286 if (udev_device == NULL)
288 return udev_device->udev;
293 * @udev_device: udev device
295 * Take a reference of a udev device.
297 * Returns: the passed udev device
299 struct udev_device *udev_device_ref(struct udev_device *udev_device)
301 if (udev_device == NULL)
303 udev_device->refcount++;
309 * @udev_device: udev device
311 * Drop a reference of a udev device. If the refcount reaches zero,
312 * the ressources of the device will be released.
315 void udev_device_unref(struct udev_device *udev_device)
317 if (udev_device == NULL)
319 udev_device->refcount--;
320 if (udev_device->refcount > 0)
322 if (udev_device->parent_device != NULL)
323 udev_device_unref(udev_device->parent_device);
324 free(udev_device->syspath);
325 free(udev_device->devname);
326 free(udev_device->subsystem);
327 util_name_list_cleanup(udev_device->udev, &udev_device->link_list);
328 util_name_list_cleanup(udev_device->udev, &udev_device->env_list);
329 free(udev_device->action);
330 free(udev_device->driver);
331 free(udev_device->devpath_old);
332 free(udev_device->physdevpath);
333 util_name_list_cleanup(udev_device->udev, &udev_device->attr_list);
334 info(udev_device->udev, "udev_device: %p released\n", udev_device);
339 * udev_device_get_devpath:
340 * @udev_device: udev device
342 * Retrieve the kernel devpath value of the udev device. The path
343 * does not contain the sys mount point, and starts with a '/'.
345 * Returns: the devpath of the udev device
347 const char *udev_device_get_devpath(struct udev_device *udev_device)
349 if (udev_device == NULL)
351 return udev_device->devpath;
355 * udev_device_get_syspath:
356 * @udev_device: udev device
358 * Retrieve the sys path of the udev device. The path is an
359 * absolute path and starts with the sys mount point.
361 * Returns: the sys path of the udev device
363 const char *udev_device_get_syspath(struct udev_device *udev_device)
365 if (udev_device == NULL)
367 return udev_device->syspath;
370 const char *udev_device_get_sysname(struct udev_device *udev_device)
372 if (udev_device == NULL)
374 return udev_device->sysname;
378 * udev_device_get_devnode:
379 * @udev_device: udev device
381 * Retrieve the device node file name belonging to the udev device.
382 * The path is an absolute path, and starts with the device directory.
384 * Returns: the device node file name of the udev device, or #NULL if no device node exists
386 const char *udev_device_get_devnode(struct udev_device *udev_device)
388 if (udev_device == NULL)
390 return udev_device->devname;
394 * udev_device_get_subsystem:
395 * @udev_device: udev device
397 * Retrieve the subsystem string of the udev device. The string does not
400 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
402 const char *udev_device_get_subsystem(struct udev_device *udev_device)
404 char subsystem[UTIL_NAME_SIZE];
406 if (udev_device == NULL)
408 if (udev_device->subsystem != NULL)
409 return udev_device->subsystem;
411 /* read "subsytem" link */
412 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
413 udev_device->subsystem = strdup(subsystem);
414 return udev_device->subsystem;
418 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
419 udev_device->subsystem = strdup("module");
420 return udev_device->subsystem;
422 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
423 udev_device->subsystem = strdup("drivers");
424 return udev_device->subsystem;
426 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
427 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
428 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
429 udev_device->subsystem = strdup("subsystem");
430 return udev_device->subsystem;
436 * udev_device_get_devlinks:
437 * @udev_device: udev device
438 * @cb: function to be called for every device link found
439 * @data: data to be passed to the function
441 * Retrieve the device links pointing to the device file of the
442 * udev device. For every device link, the passed function will be
443 * called with the device link string.
444 * The path is an absolute path, and starts with the device directory.
445 * If the function returns 1, remaning device links will be ignored.
447 * Returns: the number of device links passed to the caller, or a negative value on error
449 int udev_device_get_devlinks(struct udev_device *udev_device,
450 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
453 struct util_name_entry *name_loop;
456 if (udev_device == NULL)
458 list_for_each_entry(name_loop, &udev_device->link_list, node) {
460 if (cb(udev_device, name_loop->name, data) != 0)
467 * udev_device_get_properties:
468 * @udev_device: udev device
469 * @cb: function to be called for every property found
470 * @data: data to be passed to the function
472 * Retrieve the property key/value pairs belonging to the
473 * udev device. For every key/value pair, the passed function will be
474 * called. If the function returns 1, remaning properties will be
477 * Returns: the number of properties passed to the caller, or a negative value on error
479 int udev_device_get_properties(struct udev_device *udev_device,
480 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
483 struct util_name_entry *name_loop;
486 if (udev_device == NULL)
488 list_for_each_entry(name_loop, &udev_device->env_list, node) {
490 if (cb(udev_device, name_loop->name, name_loop->value, data) != 0)
496 const char *udev_device_get_driver(struct udev_device *udev_device)
498 char driver[UTIL_NAME_SIZE];
500 if (udev_device == NULL)
502 if (udev_device->driver != NULL)
503 return udev_device->driver;
504 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
506 udev_device->driver = strdup(driver);
507 return udev_device->driver;
510 dev_t udev_device_get_devnum(struct udev_device *udev_device)
512 if (udev_device == NULL)
513 return makedev(0, 0);
514 return udev_device->devnum;
517 const char *udev_device_get_action(struct udev_device *udev_device)
519 if (udev_device == NULL)
521 return udev_device->action;
524 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
526 if (udev_device == NULL)
528 return udev_device->seqnum;
531 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
533 struct util_name_entry *name_loop;
534 char path[UTIL_PATH_SIZE];
535 char value[UTIL_NAME_SIZE];
539 const char *val = NULL;
541 /* look for possibly already cached result */
542 list_for_each_entry(name_loop, &udev_device->attr_list, node) {
543 if (strcmp(name_loop->name, attr) == 0) {
544 info(udev_device->udev, "'%s' in cache '%s'\n", attr, name_loop->value);
545 return name_loop->value;
549 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
550 util_strlcat(path, "/", sizeof(path));
551 util_strlcat(path, attr, sizeof(path));
553 if (lstat(path, &statbuf) != 0) {
554 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
558 if (S_ISLNK(statbuf.st_mode)) {
559 /* links return the last element of the target path */
560 char target[UTIL_NAME_SIZE];
564 len = readlink(path, target, sizeof(target));
567 pos = strrchr(target, '/');
570 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
571 val = util_name_list_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0)->value;
577 /* skip directories */
578 if (S_ISDIR(statbuf.st_mode))
581 /* skip non-readable files */
582 if ((statbuf.st_mode & S_IRUSR) == 0)
585 /* read attribute value */
586 fd = open(path, O_RDONLY);
588 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
591 size = read(fd, value, sizeof(value));
595 if (size == sizeof(value))
598 /* got a valid value, store it in cache and return it */
600 util_remove_trailing_chars(value, '\n');
601 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
602 val = util_name_list_add(udev_device->udev, &udev_device->attr_list, attr, value, 0)->value;
606 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
610 udev_device->syspath = strdup(syspath);
611 if (udev_device->syspath == NULL)
613 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
614 pos = strrchr(udev_device->syspath, '/');
617 udev_device->sysname = &pos[1];
621 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
623 udev_device->subsystem = strdup(subsystem);
624 if (udev_device->subsystem == NULL)
629 int device_set_devname(struct udev_device *udev_device, const char *devname)
631 udev_device->devname = strdup(devname);
632 if (udev_device->devname == NULL)
637 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
639 if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, NULL, 0) == NULL)
644 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
646 if (util_name_list_add(udev_device->udev, &udev_device->env_list, key, value, 0) == NULL)
651 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
653 char name[UTIL_PATH_SIZE];
656 strncpy(name, property, sizeof(name));
657 val = strchr(name, '=');
664 device_add_property(udev_device, name, val);
668 int device_set_action(struct udev_device *udev_device, const char *action)
670 udev_device->action = strdup(action);
671 if (udev_device->action == NULL)
676 int device_set_driver(struct udev_device *udev_device, const char *driver)
678 udev_device->driver = strdup(driver);
679 if (udev_device->driver == NULL)
684 const char *device_get_devpath_old(struct udev_device *udev_device)
686 if (udev_device == NULL)
688 return udev_device->devpath_old;
691 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
693 udev_device->devpath_old = strdup(devpath_old);
694 if (udev_device->devpath_old == NULL)
699 const char *device_get_physdevpath(struct udev_device *udev_device)
701 if (udev_device == NULL)
703 return udev_device->physdevpath;
706 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
708 udev_device->physdevpath = strdup(physdevpath);
709 if (udev_device->physdevpath == NULL)
714 int device_get_timeout(struct udev_device *udev_device)
716 if (udev_device == NULL)
718 return udev_device->timeout;
721 int device_set_timeout(struct udev_device *udev_device, int timeout)
723 udev_device->timeout = timeout;
727 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
729 udev_device->seqnum = seqnum;
733 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
735 udev_device->devnum = devnum;
739 int device_get_num_fake_partitions(struct udev_device *udev_device)
741 if (udev_device == NULL)
743 return udev_device->num_fake_partitions;
746 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
748 udev_device->num_fake_partitions = num;
752 int device_get_devlink_priority(struct udev_device *udev_device)
754 if (udev_device == NULL)
756 return udev_device->devlink_priority;
759 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
761 udev_device->devlink_priority = prio;
765 int device_get_ignore_remove(struct udev_device *udev_device)
767 if (udev_device == NULL)
769 return udev_device->ignore_remove;
772 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
774 udev_device->ignore_remove = ignore;