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/>.
33 #include "libudev-private.h"
38 struct udev_device *parent_device;
44 struct list_head link_list;
45 struct list_head env_list;
52 unsigned long long int seqnum;
53 int num_fake_partitions;
56 struct list_head attr_list;
59 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
63 /* translate to location of db file */
64 util_strlcpy(filename, udev_get_dev_path(udev), len);
65 start = util_strlcat(filename, "/.udev/db/", len);
66 util_strlcat(filename, devpath, len);
67 return util_path_encode(&filename[start], len - start);
70 static int device_read_db(struct udev_device *udev_device)
73 char filename[UTIL_PATH_SIZE];
74 char line[UTIL_LINE_SIZE];
78 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
80 if (lstat(filename, &stats) != 0) {
81 info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno));
84 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
85 char target[UTIL_PATH_SIZE];
88 info(udev_device->udev, "found a symlink as db file\n");
89 target_len = readlink(filename, target, sizeof(target));
91 target[target_len] = '\0';
93 info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
96 dbg(udev_device->udev, "db link points to '%s'\n", target);
97 if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
102 f = fopen(filename, "r");
104 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
107 while (fgets(line, sizeof(line), f)) {
110 unsigned int maj, min;
120 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
123 sscanf(val, "%u:%u", &maj, &min);
124 device_set_devnum(udev_device, makedev(maj, min));
127 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
128 util_strlcat(filename, "/", sizeof(filename));
129 util_strlcat(filename, val, sizeof(filename));
130 device_add_devlink(udev_device, filename);
133 device_set_devlink_priority(udev_device, atoi(val));
136 device_set_timeout(udev_device, atoi(val));
139 device_set_num_fake_partitions(udev_device, atoi(val));
142 device_set_ignore_remove(udev_device, atoi(val));
145 device_add_property(udev_device, val);
154 struct udev_device *device_init(struct udev *udev)
156 struct udev_device *udev_device;
161 udev_device = malloc(sizeof(struct udev_device));
162 if (udev_device == NULL)
164 memset(udev_device, 0x00, sizeof(struct udev_device));
165 udev_device->refcount = 1;
166 udev_device->udev = udev;
167 INIT_LIST_HEAD(&udev_device->link_list);
168 INIT_LIST_HEAD(&udev_device->env_list);
169 INIT_LIST_HEAD(&udev_device->attr_list);
170 info(udev_device->udev, "udev_device: %p created\n", udev_device);
175 * udev_device_new_from_devpath:
176 * @udev: udev library context
177 * @devpath: sys device path
179 * Create new udev device, and fill in information from the sysfs
180 * device and the udev database entry. The devpath must not contain
181 * the sysfs mount path, and must contain a leading '/'.
183 * The initial refcount is 1, and needs to be decremented to
184 * release the ressources of the udev device.
186 * Returns: a new udev device, or #NULL, if it does not exist
188 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
190 char path[UTIL_PATH_SIZE];
192 struct udev_device *udev_device;
199 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
200 util_strlcat(path, devpath, sizeof(path));
201 util_strlcat(path, "/uevent", sizeof(path));
202 if (stat(path, &statbuf) != 0) {
203 info(udev, "not a device :%s\n", devpath);
207 udev_device = device_init(udev);
208 if (udev_device == NULL)
211 /* resolve possible symlink to real path */
212 util_strlcpy(path, devpath, sizeof(path));
213 util_resolve_sys_link(udev, path, sizeof(path));
214 device_set_devpath(udev_device, path);
215 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
217 if (device_read_db(udev_device) >= 0)
218 info(udev, "device %p filled with udev database data\n", udev_device);
222 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
224 struct udev_device *udev_device_parent = NULL;
225 char path[UTIL_PATH_SIZE];
228 if (udev_device == NULL)
231 util_strlcpy(path, udev_device_get_devpath(udev_device), sizeof(path));
233 pos = strrchr(path, '/');
234 if (pos == path || pos == NULL)
237 udev_device_parent = udev_device_new_from_devpath(udev_device->udev, path);
238 if (udev_device_parent != NULL)
241 return udev_device_parent;
244 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
246 if (udev_device->parent_device == NULL)
247 udev_device->parent_device = device_new_from_parent(udev_device);
248 return udev_device->parent_device;
252 * udev_device_get_udev:
253 * @udev_device: udev device
255 * Retrieve the udev library context the device was created with.
257 * Returns: the udev library context
259 struct udev *udev_device_get_udev(struct udev_device *udev_device)
261 if (udev_device == NULL)
263 return udev_device->udev;
268 * @udev_device: udev device
270 * Take a reference of a udev device.
272 * Returns: the passed udev device
274 struct udev_device *udev_device_ref(struct udev_device *udev_device)
276 if (udev_device == NULL)
278 udev_device->refcount++;
284 * @udev_device: udev device
286 * Drop a reference of a udev device. If the refcount reaches zero,
287 * the ressources of the device will be released.
290 void udev_device_unref(struct udev_device *udev_device)
292 if (udev_device == NULL)
294 udev_device->refcount--;
295 if (udev_device->refcount > 0)
297 if (udev_device->parent_device != NULL)
298 udev_device_unref(udev_device->parent_device);
299 free(udev_device->syspath);
300 free(udev_device->devname);
301 free(udev_device->subsystem);
302 util_name_list_cleanup(udev_device->udev, &udev_device->link_list);
303 util_name_list_cleanup(udev_device->udev, &udev_device->env_list);
304 free(udev_device->action);
305 free(udev_device->driver);
306 free(udev_device->devpath_old);
307 free(udev_device->physdevpath);
308 util_name_list_cleanup(udev_device->udev, &udev_device->attr_list);
309 info(udev_device->udev, "udev_device: %p released\n", udev_device);
314 * udev_device_get_devpath:
315 * @udev_device: udev device
317 * Retrieve the kernel devpath value of the udev device. The path
318 * does not contain the sys mount point, and starts with a '/'.
320 * Returns: the devpath of the udev device
322 const char *udev_device_get_devpath(struct udev_device *udev_device)
324 if (udev_device == NULL)
326 return udev_device->devpath;
330 * udev_device_get_syspath:
331 * @udev_device: udev device
333 * Retrieve the sys path of the udev device. The path is an
334 * absolute path and starts with the sys mount point.
336 * Returns: the sys path of the udev device
338 const char *udev_device_get_syspath(struct udev_device *udev_device)
340 if (udev_device == NULL)
342 return udev_device->syspath;
345 const char *udev_device_get_sysname(struct udev_device *udev_device)
347 if (udev_device == NULL)
349 return udev_device->sysname;
353 * udev_device_get_devname:
354 * @udev_device: udev device
356 * Retrieve the device node file name belonging to the udev device.
357 * The path is an absolute path, and starts with the device directory.
359 * Returns: the device node file name of the udev device, or #NULL if no device node exists
361 const char *udev_device_get_devname(struct udev_device *udev_device)
363 if (udev_device == NULL)
365 return udev_device->devname;
369 * udev_device_get_subsystem:
370 * @udev_device: udev device
372 * Retrieve the subsystem string of the udev device. The string does not
375 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
377 const char *udev_device_get_subsystem(struct udev_device *udev_device)
379 char subsystem[UTIL_NAME_SIZE];
381 if (udev_device == NULL)
383 if (udev_device->subsystem != NULL)
384 return udev_device->subsystem;
385 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
387 udev_device->subsystem = strdup(subsystem);
388 return udev_device->subsystem;
392 * udev_device_get_devlinks:
393 * @udev_device: udev device
394 * @cb: function to be called for every device link found
395 * @data: data to be passed to the function
397 * Retrieve the device links pointing to the device file of the
398 * udev device. For every device link, the passed function will be
399 * called with the device link string.
400 * The path is an absolute path, and starts with the device directory.
401 * If the function returns 1, remaning device links will be ignored.
403 * Returns: the number of device links passed to the caller, or a negative value on error
405 int udev_device_get_devlinks(struct udev_device *udev_device,
406 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
409 struct util_name_entry *name_loop;
412 if (udev_device == NULL)
414 list_for_each_entry(name_loop, &udev_device->link_list, node) {
416 if (cb(udev_device, name_loop->name, data) != 0)
423 * udev_device_get_properties:
424 * @udev_device: udev device
425 * @cb: function to be called for every property found
426 * @data: data to be passed to the function
428 * Retrieve the property key/value pairs belonging to the
429 * udev device. For every key/value pair, the passed function will be
430 * called. If the function returns 1, remaning properties will be
433 * Returns: the number of properties passed to the caller, or a negative value on error
435 int udev_device_get_properties(struct udev_device *udev_device,
436 int (*cb)(struct udev_device *udev_device, const char *key, 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->env_list, node) {
445 char name[UTIL_NAME_SIZE];
448 strncpy(name, name_loop->name, sizeof(name));
449 val = strchr(name, '=');
455 if (cb(udev_device, name, val, data) != 0)
461 const char *udev_device_get_driver(struct udev_device *udev_device)
463 char driver[UTIL_NAME_SIZE];
465 if (udev_device == NULL)
467 if (udev_device->driver != NULL)
468 return udev_device->driver;
469 if (util_get_sys_driver(udev_device->udev, udev_device->devpath, driver, sizeof(driver)) < 2)
471 udev_device->driver = strdup(driver);
472 return udev_device->driver;
475 dev_t udev_device_get_devnum(struct udev_device *udev_device)
477 if (udev_device == NULL)
478 return makedev(0, 0);
479 return udev_device->devnum;
482 const char *udev_device_get_action(struct udev_device *udev_device)
484 if (udev_device == NULL)
486 return udev_device->action;
489 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
491 if (udev_device == NULL)
493 return udev_device->seqnum;
496 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
498 char path[UTIL_PATH_SIZE];
499 char value[UTIL_NAME_SIZE];
503 const char *val = NULL;
505 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
506 util_strlcat(path, "/", sizeof(path));
507 util_strlcat(path, attr, sizeof(path));
509 if (lstat(path, &statbuf) != 0) {
510 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
514 if (S_ISLNK(statbuf.st_mode)) {
515 /* links return the last element of the target path */
516 char target[UTIL_NAME_SIZE];
520 len = readlink(path, target, sizeof(target));
523 pos = strrchr(target, '/');
526 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
527 val = util_name_list_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0)->value;
533 /* skip directories */
534 if (S_ISDIR(statbuf.st_mode))
537 /* skip non-readable files */
538 if ((statbuf.st_mode & S_IRUSR) == 0)
541 /* read attribute value */
542 fd = open(path, O_RDONLY);
544 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
547 size = read(fd, value, sizeof(value));
551 if (size == sizeof(value))
554 /* got a valid value, store and return it */
556 util_remove_trailing_chars(value, '\n');
557 info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
558 val = util_name_list_add(udev_device->udev, &udev_device->attr_list, attr, value, 0)->value;
562 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
564 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
566 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
567 udev_device->sysname = strrchr(udev_device->syspath, '/');
568 if (udev_device->sysname != NULL)
569 udev_device->sysname = &udev_device->sysname[1];
573 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
575 udev_device->subsystem = strdup(subsystem);
576 if (udev_device->subsystem == NULL)
581 int device_set_devname(struct udev_device *udev_device, const char *devname)
583 udev_device->devname = strdup(devname);
584 if (udev_device->devname == NULL)
589 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
591 if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, NULL, 0) == NULL)
596 int device_add_property(struct udev_device *udev_device, const char *property)
598 if (util_name_list_add(udev_device->udev, &udev_device->env_list, property, NULL, 0) == NULL)
603 int device_set_action(struct udev_device *udev_device, const char *action)
605 udev_device->action = strdup(action);
606 if (udev_device->action == NULL)
611 int device_set_driver(struct udev_device *udev_device, const char *driver)
613 udev_device->driver = strdup(driver);
614 if (udev_device->driver == NULL)
619 const char *device_get_devpath_old(struct udev_device *udev_device)
621 if (udev_device == NULL)
623 return udev_device->devpath_old;
626 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
628 udev_device->devpath_old = strdup(devpath_old);
629 if (udev_device->devpath_old == NULL)
634 const char *device_get_physdevpath(struct udev_device *udev_device)
636 if (udev_device == NULL)
638 return udev_device->physdevpath;
641 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
643 udev_device->physdevpath = strdup(physdevpath);
644 if (udev_device->physdevpath == NULL)
649 int device_get_timeout(struct udev_device *udev_device)
651 if (udev_device == NULL)
653 return udev_device->timeout;
656 int device_set_timeout(struct udev_device *udev_device, int timeout)
658 udev_device->timeout = timeout;
662 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
664 udev_device->seqnum = seqnum;
668 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
670 udev_device->devnum = devnum;
674 int device_get_num_fake_partitions(struct udev_device *udev_device)
676 if (udev_device == NULL)
678 return udev_device->num_fake_partitions;
681 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
683 udev_device->num_fake_partitions = num;
687 int device_get_devlink_priority(struct udev_device *udev_device)
689 if (udev_device == NULL)
691 return udev_device->devlink_priority;
694 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
696 udev_device->devlink_priority = prio;
700 int device_get_ignore_remove(struct udev_device *udev_device)
702 if (udev_device == NULL)
704 return udev_device->ignore_remove;
707 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
709 udev_device->ignore_remove = ignore;