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/>.
32 #include "libudev-private.h"
37 struct udev_device *parent_device;
43 struct list_head link_list;
44 struct list_head env_list;
51 unsigned long long int seqnum;
52 int num_fake_partitions;
57 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
61 /* translate to location of db file */
62 util_strlcpy(filename, udev_get_dev_path(udev), len);
63 start = util_strlcat(filename, "/.udev/db/", len);
64 util_strlcat(filename, 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 devpath_to_db_path(udev_device->udev, udev_device->devpath, 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(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 info(udev_device->udev, "udev_device: %p created\n", udev_device);
172 * udev_device_new_from_devpath:
173 * @udev: udev library context
174 * @devpath: sys device path
176 * Create new udev device, and fill in information from the sysfs
177 * device and the udev database entry. The devpath must not contain
178 * the sysfs mount path, and must contain a leading '/'.
180 * The initial refcount is 1, and needs to be decremented to
181 * release the ressources of the udev device.
183 * Returns: a new udev device, or #NULL, if it does not exist
185 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
187 char path[UTIL_PATH_SIZE];
189 struct udev_device *udev_device;
196 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
197 util_strlcat(path, devpath, sizeof(path));
198 util_strlcat(path, "/uevent", sizeof(path));
199 if (stat(path, &statbuf) != 0) {
200 info(udev, "not a device :%s\n", devpath);
204 udev_device = device_init(udev);
205 if (udev_device == NULL)
208 /* resolve possible symlink to real path */
209 util_strlcpy(path, devpath, sizeof(path));
210 util_resolve_sys_link(udev, path, sizeof(path));
211 device_set_devpath(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_get_devpath(udev_device), sizeof(path));
230 pos = strrchr(path, '/');
231 if (pos == path || pos == NULL)
234 udev_device_parent = udev_device_new_from_devpath(udev_device->udev, path);
235 if (udev_device_parent != NULL)
238 return udev_device_parent;
241 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
243 if (udev_device->parent_device == NULL)
244 udev_device->parent_device = device_new_from_parent(udev_device);
245 return udev_device->parent_device;
249 * udev_device_get_udev:
250 * @udev_device: udev device
252 * Retrieve the udev library context the device was created with.
254 * Returns: the udev library context
256 struct udev *udev_device_get_udev(struct udev_device *udev_device)
258 if (udev_device == NULL)
260 return udev_device->udev;
265 * @udev_device: udev device
267 * Take a reference of a udev device.
269 * Returns: the passed udev device
271 struct udev_device *udev_device_ref(struct udev_device *udev_device)
273 if (udev_device == NULL)
275 udev_device->refcount++;
281 * @udev_device: udev device
283 * Drop a reference of a udev device. If the refcount reaches zero,
284 * the ressources of the device will be released.
287 void udev_device_unref(struct udev_device *udev_device)
289 if (udev_device == NULL)
291 udev_device->refcount--;
292 if (udev_device->refcount > 0)
294 if (udev_device->parent_device != NULL)
295 udev_device_unref(udev_device->parent_device);
296 free(udev_device->syspath);
297 free(udev_device->devname);
298 free(udev_device->subsystem);
299 util_name_list_cleanup(udev_device->udev, &udev_device->link_list);
300 util_name_list_cleanup(udev_device->udev, &udev_device->env_list);
301 free(udev_device->action);
302 free(udev_device->driver);
303 free(udev_device->devpath_old);
304 free(udev_device->physdevpath);
305 info(udev_device->udev, "udev_device: %p released\n", udev_device);
310 * udev_device_get_devpath:
311 * @udev_device: udev device
313 * Retrieve the kernel devpath value of the udev device. The path
314 * does not contain the sys mount point, and starts with a '/'.
316 * Returns: the devpath of the udev device
318 const char *udev_device_get_devpath(struct udev_device *udev_device)
320 if (udev_device == NULL)
322 return udev_device->devpath;
326 * udev_device_get_syspath:
327 * @udev_device: udev device
329 * Retrieve the sys path of the udev device. The path is an
330 * absolute path and starts with the sys mount point.
332 * Returns: the sys path of the udev device
334 const char *udev_device_get_syspath(struct udev_device *udev_device)
336 if (udev_device == NULL)
338 return udev_device->syspath;
341 const char *udev_device_get_sysname(struct udev_device *udev_device)
343 if (udev_device == NULL)
345 return udev_device->sysname;
349 * udev_device_get_devname:
350 * @udev_device: udev device
352 * Retrieve the device node file name belonging to the udev device.
353 * The path is an absolute path, and starts with the device directory.
355 * Returns: the device node file name of the udev device, or #NULL if no device node exists
357 const char *udev_device_get_devname(struct udev_device *udev_device)
359 if (udev_device == NULL)
361 return udev_device->devname;
365 * udev_device_get_subsystem:
366 * @udev_device: udev device
368 * Retrieve the subsystem string of the udev device. The string does not
371 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
373 const char *udev_device_get_subsystem(struct udev_device *udev_device)
375 char subsystem[UTIL_NAME_SIZE];
377 if (udev_device == NULL)
379 if (udev_device->subsystem != NULL)
380 return udev_device->subsystem;
381 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
383 udev_device->subsystem = strdup(subsystem);
384 return udev_device->subsystem;
388 * udev_device_get_devlinks:
389 * @udev_device: udev device
390 * @cb: function to be called for every device link found
391 * @data: data to be passed to the function
393 * Retrieve the device links pointing to the device file of the
394 * udev device. For every device link, the passed function will be
395 * called with the device link string.
396 * The path is an absolute path, and starts with the device directory.
397 * If the function returns 1, remaning device links will be ignored.
399 * Returns: the number of device links passed to the caller, or a negative value on error
401 int udev_device_get_devlinks(struct udev_device *udev_device,
402 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
405 struct util_name_entry *name_loop;
408 if (udev_device == NULL)
410 list_for_each_entry(name_loop, &udev_device->link_list, node) {
412 if (cb(udev_device, name_loop->name, data) != 0)
419 * udev_device_get_properties:
420 * @udev_device: udev device
421 * @cb: function to be called for every property found
422 * @data: data to be passed to the function
424 * Retrieve the property key/value pairs belonging to the
425 * udev device. For every key/value pair, the passed function will be
426 * called. If the function returns 1, remaning properties will be
429 * Returns: the number of properties passed to the caller, or a negative value on error
431 int udev_device_get_properties(struct udev_device *udev_device,
432 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
435 struct util_name_entry *name_loop;
438 if (udev_device == NULL)
440 list_for_each_entry(name_loop, &udev_device->env_list, node) {
441 char name[UTIL_NAME_SIZE];
444 strncpy(name, name_loop->name, sizeof(name));
445 val = strchr(name, '=');
451 if (cb(udev_device, name, val, data) != 0)
457 const char *udev_device_get_driver(struct udev_device *udev_device)
459 char driver[UTIL_NAME_SIZE];
461 if (udev_device == NULL)
463 if (udev_device->driver != NULL)
464 return udev_device->driver;
465 if (util_get_sys_driver(udev_device->udev, udev_device->devpath, driver, sizeof(driver)) < 2)
467 udev_device->driver = strdup(driver);
468 return udev_device->driver;
471 dev_t udev_device_get_devnum(struct udev_device *udev_device)
473 if (udev_device == NULL)
474 return makedev(0, 0);
475 return udev_device->devnum;
478 const char *udev_device_get_action(struct udev_device *udev_device)
480 if (udev_device == NULL)
482 return udev_device->action;
485 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
487 if (udev_device == NULL)
489 return udev_device->seqnum;
492 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
494 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
496 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
497 udev_device->sysname = strrchr(udev_device->syspath, '/');
498 if (udev_device->sysname != NULL)
499 udev_device->sysname = &udev_device->sysname[1];
503 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
505 udev_device->subsystem = strdup(subsystem);
506 if (udev_device->subsystem == NULL)
511 int device_set_devname(struct udev_device *udev_device, const char *devname)
513 udev_device->devname = strdup(devname);
514 if (udev_device->devname == NULL)
519 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
521 if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
526 int device_add_property(struct udev_device *udev_device, const char *property)
528 if (util_name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
533 int device_set_action(struct udev_device *udev_device, const char *action)
535 udev_device->action = strdup(action);
536 if (udev_device->action == NULL)
541 int device_set_driver(struct udev_device *udev_device, const char *driver)
543 udev_device->driver = strdup(driver);
544 if (udev_device->driver == NULL)
549 const char *device_get_devpath_old(struct udev_device *udev_device)
551 if (udev_device == NULL)
553 return udev_device->devpath_old;
556 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
558 udev_device->devpath_old = strdup(devpath_old);
559 if (udev_device->devpath_old == NULL)
564 const char *device_get_physdevpath(struct udev_device *udev_device)
566 if (udev_device == NULL)
568 return udev_device->physdevpath;
571 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
573 udev_device->physdevpath = strdup(physdevpath);
574 if (udev_device->physdevpath == NULL)
579 int device_get_timeout(struct udev_device *udev_device)
581 if (udev_device == NULL)
583 return udev_device->timeout;
586 int device_set_timeout(struct udev_device *udev_device, int timeout)
588 udev_device->timeout = timeout;
592 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
594 udev_device->seqnum = seqnum;
598 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
600 udev_device->devnum = devnum;
604 int device_get_num_fake_partitions(struct udev_device *udev_device)
606 if (udev_device == NULL)
608 return udev_device->num_fake_partitions;
611 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
613 udev_device->num_fake_partitions = num;
617 int device_get_devlink_priority(struct udev_device *udev_device)
619 if (udev_device == NULL)
621 return udev_device->devlink_priority;
624 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
626 udev_device->devlink_priority = prio;
630 int device_get_ignore_remove(struct udev_device *udev_device)
632 if (udev_device == NULL)
634 return udev_device->ignore_remove;
637 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
639 udev_device->ignore_remove = ignore;