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 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
221 struct udev_device *udev_device_parent = NULL;
222 char path[UTIL_PATH_SIZE];
225 if (udev_device == NULL)
228 util_strlcpy(path, udev_device->syspath, sizeof(path));
230 pos = strrchr(path, '/');
231 if (pos == path || pos == NULL)
234 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
235 if (udev_device_parent != NULL)
236 return udev_device_parent;
239 /* follow "device" link in deprecated sys /sys/class/ layout */
240 if (strncmp(udev_device->devpath, "/class/", 7) == 0) {
241 util_strlcpy(path, udev_device->syspath, sizeof(path));
242 util_strlcat(path, "/device", sizeof(path));
243 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
244 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
245 if (udev_device_parent != NULL)
246 return udev_device_parent;
252 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
254 if (udev_device->parent_device != NULL) {
255 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
256 return udev_device->parent_device;
258 udev_device->parent_device = device_new_from_parent(udev_device);
259 return udev_device->parent_device;
263 * udev_device_get_udev:
264 * @udev_device: udev device
266 * Retrieve the udev library context the device was created with.
268 * Returns: the udev library context
270 struct udev *udev_device_get_udev(struct udev_device *udev_device)
272 if (udev_device == NULL)
274 return udev_device->udev;
279 * @udev_device: udev device
281 * Take a reference of a udev device.
283 * Returns: the passed udev device
285 struct udev_device *udev_device_ref(struct udev_device *udev_device)
287 if (udev_device == NULL)
289 udev_device->refcount++;
295 * @udev_device: udev device
297 * Drop a reference of a udev device. If the refcount reaches zero,
298 * the ressources of the device will be released.
301 void udev_device_unref(struct udev_device *udev_device)
303 if (udev_device == NULL)
305 udev_device->refcount--;
306 if (udev_device->refcount > 0)
308 if (udev_device->parent_device != NULL)
309 udev_device_unref(udev_device->parent_device);
310 free(udev_device->syspath);
311 free(udev_device->devname);
312 free(udev_device->subsystem);
313 util_name_list_cleanup(udev_device->udev, &udev_device->link_list);
314 util_name_list_cleanup(udev_device->udev, &udev_device->env_list);
315 free(udev_device->action);
316 free(udev_device->driver);
317 free(udev_device->devpath_old);
318 free(udev_device->physdevpath);
319 util_name_list_cleanup(udev_device->udev, &udev_device->attr_list);
320 info(udev_device->udev, "udev_device: %p released\n", udev_device);
325 * udev_device_get_devpath:
326 * @udev_device: udev device
328 * Retrieve the kernel devpath value of the udev device. The path
329 * does not contain the sys mount point, and starts with a '/'.
331 * Returns: the devpath of the udev device
333 const char *udev_device_get_devpath(struct udev_device *udev_device)
335 if (udev_device == NULL)
337 return udev_device->devpath;
341 * udev_device_get_syspath:
342 * @udev_device: udev device
344 * Retrieve the sys path of the udev device. The path is an
345 * absolute path and starts with the sys mount point.
347 * Returns: the sys path of the udev device
349 const char *udev_device_get_syspath(struct udev_device *udev_device)
351 if (udev_device == NULL)
353 return udev_device->syspath;
356 const char *udev_device_get_sysname(struct udev_device *udev_device)
358 if (udev_device == NULL)
360 return udev_device->sysname;
364 * udev_device_get_devname:
365 * @udev_device: udev device
367 * Retrieve the device node file name belonging to the udev device.
368 * The path is an absolute path, and starts with the device directory.
370 * Returns: the device node file name of the udev device, or #NULL if no device node exists
372 const char *udev_device_get_devname(struct udev_device *udev_device)
374 if (udev_device == NULL)
376 return udev_device->devname;
380 * udev_device_get_subsystem:
381 * @udev_device: udev device
383 * Retrieve the subsystem string of the udev device. The string does not
386 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
388 const char *udev_device_get_subsystem(struct udev_device *udev_device)
390 char subsystem[UTIL_NAME_SIZE];
392 if (udev_device == NULL)
394 if (udev_device->subsystem != NULL)
395 return udev_device->subsystem;
397 /* read "subsytem" link */
398 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
399 udev_device->subsystem = strdup(subsystem);
400 return udev_device->subsystem;
404 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
405 udev_device->subsystem = strdup("module");
406 return udev_device->subsystem;
408 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
409 udev_device->subsystem = strdup("drivers");
410 return udev_device->subsystem;
412 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
413 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
414 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
415 udev_device->subsystem = strdup("subsystem");
416 return udev_device->subsystem;
422 * udev_device_get_devlinks:
423 * @udev_device: udev device
424 * @cb: function to be called for every device link found
425 * @data: data to be passed to the function
427 * Retrieve the device links pointing to the device file of the
428 * udev device. For every device link, the passed function will be
429 * called with the device link string.
430 * The path is an absolute path, and starts with the device directory.
431 * If the function returns 1, remaning device links will be ignored.
433 * Returns: the number of device links passed to the caller, or a negative value on error
435 int udev_device_get_devlinks(struct udev_device *udev_device,
436 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
439 struct util_name_entry *name_loop;
442 if (udev_device == NULL)
444 list_for_each_entry(name_loop, &udev_device->link_list, node) {
446 if (cb(udev_device, name_loop->name, data) != 0)
453 * udev_device_get_properties:
454 * @udev_device: udev device
455 * @cb: function to be called for every property found
456 * @data: data to be passed to the function
458 * Retrieve the property key/value pairs belonging to the
459 * udev device. For every key/value pair, the passed function will be
460 * called. If the function returns 1, remaning properties will be
463 * Returns: the number of properties passed to the caller, or a negative value on error
465 int udev_device_get_properties(struct udev_device *udev_device,
466 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
469 struct util_name_entry *name_loop;
472 if (udev_device == NULL)
474 list_for_each_entry(name_loop, &udev_device->env_list, node) {
476 if (cb(udev_device, name_loop->name, name_loop->value, data) != 0)
482 const char *udev_device_get_driver(struct udev_device *udev_device)
484 char driver[UTIL_NAME_SIZE];
486 if (udev_device == NULL)
488 if (udev_device->driver != NULL)
489 return udev_device->driver;
490 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
492 udev_device->driver = strdup(driver);
493 return udev_device->driver;
496 dev_t udev_device_get_devnum(struct udev_device *udev_device)
498 if (udev_device == NULL)
499 return makedev(0, 0);
500 return udev_device->devnum;
503 const char *udev_device_get_action(struct udev_device *udev_device)
505 if (udev_device == NULL)
507 return udev_device->action;
510 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
512 if (udev_device == NULL)
514 return udev_device->seqnum;
517 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
519 struct util_name_entry *name_loop;
520 char path[UTIL_PATH_SIZE];
521 char value[UTIL_NAME_SIZE];
525 const char *val = NULL;
527 /* look for possibly already cached result */
528 list_for_each_entry(name_loop, &udev_device->attr_list, node) {
529 if (strcmp(name_loop->name, attr) == 0) {
530 info(udev_device->udev, "'%s' in cache '%s'\n", attr, name_loop->value);
531 return name_loop->value;
535 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
536 util_strlcat(path, "/", sizeof(path));
537 util_strlcat(path, attr, sizeof(path));
539 if (lstat(path, &statbuf) != 0) {
540 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
544 if (S_ISLNK(statbuf.st_mode)) {
545 /* links return the last element of the target path */
546 char target[UTIL_NAME_SIZE];
550 len = readlink(path, target, sizeof(target));
553 pos = strrchr(target, '/');
556 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
557 val = util_name_list_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0)->value;
563 /* skip directories */
564 if (S_ISDIR(statbuf.st_mode))
567 /* skip non-readable files */
568 if ((statbuf.st_mode & S_IRUSR) == 0)
571 /* read attribute value */
572 fd = open(path, O_RDONLY);
574 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
577 size = read(fd, value, sizeof(value));
581 if (size == sizeof(value))
584 /* got a valid value, store it in cache and return it */
586 util_remove_trailing_chars(value, '\n');
587 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
588 val = util_name_list_add(udev_device->udev, &udev_device->attr_list, attr, value, 0)->value;
592 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
596 udev_device->syspath = strdup(syspath);
597 if (udev_device->syspath == NULL)
599 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
600 pos = strrchr(udev_device->syspath, '/');
603 udev_device->sysname = &pos[1];
607 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
609 udev_device->subsystem = strdup(subsystem);
610 if (udev_device->subsystem == NULL)
615 int device_set_devname(struct udev_device *udev_device, const char *devname)
617 udev_device->devname = strdup(devname);
618 if (udev_device->devname == NULL)
623 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
625 if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, NULL, 0) == NULL)
630 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
632 if (util_name_list_add(udev_device->udev, &udev_device->env_list, key, value, 0) == NULL)
637 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
639 char name[UTIL_PATH_SIZE];
642 strncpy(name, property, sizeof(name));
643 val = strchr(name, '=');
650 device_add_property(udev_device, name, val);
654 int device_set_action(struct udev_device *udev_device, const char *action)
656 udev_device->action = strdup(action);
657 if (udev_device->action == NULL)
662 int device_set_driver(struct udev_device *udev_device, const char *driver)
664 udev_device->driver = strdup(driver);
665 if (udev_device->driver == NULL)
670 const char *device_get_devpath_old(struct udev_device *udev_device)
672 if (udev_device == NULL)
674 return udev_device->devpath_old;
677 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
679 udev_device->devpath_old = strdup(devpath_old);
680 if (udev_device->devpath_old == NULL)
685 const char *device_get_physdevpath(struct udev_device *udev_device)
687 if (udev_device == NULL)
689 return udev_device->physdevpath;
692 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
694 udev_device->physdevpath = strdup(physdevpath);
695 if (udev_device->physdevpath == NULL)
700 int device_get_timeout(struct udev_device *udev_device)
702 if (udev_device == NULL)
704 return udev_device->timeout;
707 int device_set_timeout(struct udev_device *udev_device, int timeout)
709 udev_device->timeout = timeout;
713 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
715 udev_device->seqnum = seqnum;
719 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
721 udev_device->devnum = devnum;
725 int device_get_num_fake_partitions(struct udev_device *udev_device)
727 if (udev_device == NULL)
729 return udev_device->num_fake_partitions;
732 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
734 udev_device->num_fake_partitions = num;
738 int device_get_devlink_priority(struct udev_device *udev_device)
740 if (udev_device == NULL)
742 return udev_device->devlink_priority;
745 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
747 udev_device->devlink_priority = prio;
751 int device_get_ignore_remove(struct udev_device *udev_device)
753 if (udev_device == NULL)
755 return udev_device->ignore_remove;
758 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
760 udev_device->ignore_remove = ignore;