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"
42 struct list_head link_list;
43 struct list_head env_list;
50 unsigned long long int seqnum;
51 int num_fake_partitions;
56 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
60 /* translate to location of db file */
61 util_strlcpy(filename, udev_get_dev_path(udev), len);
62 start = util_strlcat(filename, "/.udev/db/", len);
63 util_strlcat(filename, devpath, len);
64 return util_path_encode(&filename[start], len - start);
67 static int device_read_db(struct udev_device *udev_device)
70 char filename[UTIL_PATH_SIZE];
71 char line[UTIL_LINE_SIZE];
75 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
77 if (lstat(filename, &stats) != 0) {
78 info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno));
81 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
82 char target[UTIL_PATH_SIZE];
85 info(udev_device->udev, "found a symlink as db file\n");
86 target_len = readlink(filename, target, sizeof(target));
88 target[target_len] = '\0';
90 info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
93 dbg(udev_device->udev, "db link points to '%s'\n", target);
94 if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
99 f = fopen(filename, "r");
101 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
104 while (fgets(line, sizeof(line), f)) {
107 unsigned int maj, min;
117 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
120 sscanf(val, "%u:%u", &maj, &min);
121 device_set_devnum(udev_device, makedev(maj, min));
124 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
125 util_strlcat(filename, "/", sizeof(filename));
126 util_strlcat(filename, val, sizeof(filename));
127 device_add_devlink(udev_device, filename);
130 device_set_devlink_priority(udev_device, atoi(val));
133 device_set_timeout(udev_device, atoi(val));
136 device_set_num_fake_partitions(udev_device, atoi(val));
139 device_set_ignore_remove(udev_device, atoi(val));
142 device_add_property(udev_device, val);
151 struct udev_device *device_init(struct udev *udev)
153 struct udev_device *udev_device;
158 udev_device = malloc(sizeof(struct udev_device));
159 if (udev_device == NULL)
161 memset(udev_device, 0x00, sizeof(struct udev_device));
162 udev_device->refcount = 1;
163 udev_device->udev = udev;
164 INIT_LIST_HEAD(&udev_device->link_list);
165 INIT_LIST_HEAD(&udev_device->env_list);
166 info(udev_device->udev, "udev_device: %p created\n", udev_device);
171 * udev_device_new_from_devpath:
172 * @udev: udev library context
173 * @devpath: sys device path
175 * Create new udev device, and fill in information from the sysfs
176 * device and the udev database entry. The devpath must not contain
177 * the sysfs mount path, and must contain a leading '/'.
179 * The initial refcount is 1, and needs to be decremented to
180 * release the ressources of the udev device.
182 * Returns: a new udev device, or #NULL, if it does not exist
184 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
186 char path[UTIL_PATH_SIZE];
188 struct udev_device *udev_device;
195 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
196 util_strlcat(path, devpath, sizeof(path));
197 util_strlcat(path, "/uevent", sizeof(path));
198 if (stat(path, &statbuf) != 0) {
199 info(udev, "not a device :%s\n", path);
203 udev_device = device_init(udev);
204 if (udev_device == NULL)
207 /* resolve possible symlink to real path */
208 util_strlcpy(path, devpath, sizeof(path));
209 util_resolve_sys_link(udev, path, sizeof(path));
210 device_set_devpath(udev_device, path);
211 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
213 if (device_read_db(udev_device) >= 0)
214 info(udev, "device %p filled with udev database data\n", udev_device);
219 struct udev_device *udev_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, '/');
234 udev_device_parent = udev_device_new_from_devpath(udev_device->udev, path);
235 if (udev_device_parent != NULL)
238 return udev_device_parent;
242 * udev_device_get_udev:
243 * @udev_device: udev device
245 * Retrieve the udev library context the device was created with.
247 * Returns: the udev library context
249 struct udev *udev_device_get_udev(struct udev_device *udev_device)
251 if (udev_device == NULL)
253 return udev_device->udev;
258 * @udev_device: udev device
260 * Take a reference of a udev device.
262 * Returns: the passed udev device
264 struct udev_device *udev_device_ref(struct udev_device *udev_device)
266 if (udev_device == NULL)
268 udev_device->refcount++;
274 * @udev_device: udev device
276 * Drop a reference of a udev device. If the refcount reaches zero,
277 * the ressources of the device will be released.
280 void udev_device_unref(struct udev_device *udev_device)
282 if (udev_device == NULL)
284 udev_device->refcount--;
285 if (udev_device->refcount > 0)
287 free(udev_device->syspath);
288 free(udev_device->devname);
289 free(udev_device->subsystem);
290 util_name_list_cleanup(udev_device->udev, &udev_device->link_list);
291 util_name_list_cleanup(udev_device->udev, &udev_device->env_list);
292 free(udev_device->action);
293 free(udev_device->driver);
294 free(udev_device->devpath_old);
295 free(udev_device->physdevpath);
296 info(udev_device->udev, "udev_device: %p released\n", udev_device);
301 * udev_device_get_devpath:
302 * @udev_device: udev device
304 * Retrieve the kernel devpath value of the udev device. The path
305 * does not contain the sys mount point, and starts with a '/'.
307 * Returns: the devpath of the udev device
309 const char *udev_device_get_devpath(struct udev_device *udev_device)
311 if (udev_device == NULL)
313 return udev_device->devpath;
317 * udev_device_get_syspath:
318 * @udev_device: udev device
320 * Retrieve the sys path of the udev device. The path is an
321 * absolute path and starts with the sys mount point.
323 * Returns: the sys path of the udev device
325 const char *udev_device_get_syspath(struct udev_device *udev_device)
327 if (udev_device == NULL)
329 return udev_device->syspath;
332 const char *udev_device_get_sysname(struct udev_device *udev_device)
334 if (udev_device == NULL)
336 return udev_device->sysname;
340 * udev_device_get_devname:
341 * @udev_device: udev device
343 * Retrieve the device node file name belonging to the udev device.
344 * The path is an absolute path, and starts with the device directory.
346 * Returns: the device node file name of the udev device, or #NULL if no device node exists
348 const char *udev_device_get_devname(struct udev_device *udev_device)
350 if (udev_device == NULL)
352 return udev_device->devname;
356 * udev_device_get_subsystem:
357 * @udev_device: udev device
359 * Retrieve the subsystem string of the udev device. The string does not
362 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
364 const char *udev_device_get_subsystem(struct udev_device *udev_device)
366 char subsystem[UTIL_NAME_SIZE];
368 if (udev_device == NULL)
370 if (udev_device->subsystem != NULL)
371 return udev_device->subsystem;
372 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
374 udev_device->subsystem = strdup(subsystem);
375 return udev_device->subsystem;
379 * udev_device_get_devlinks:
380 * @udev_device: udev device
381 * @cb: function to be called for every device link found
382 * @data: data to be passed to the function
384 * Retrieve the device links pointing to the device file of the
385 * udev device. For every device link, the passed function will be
386 * called with the device link string.
387 * The path is an absolute path, and starts with the device directory.
388 * If the function returns 1, remaning device links will be ignored.
390 * Returns: the number of device links passed to the caller, or a negative value on error
392 int udev_device_get_devlinks(struct udev_device *udev_device,
393 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
396 struct util_name_entry *name_loop;
399 if (udev_device == NULL)
401 list_for_each_entry(name_loop, &udev_device->link_list, node) {
403 if (cb(udev_device, name_loop->name, data) != 0)
410 * udev_device_get_properties:
411 * @udev_device: udev device
412 * @cb: function to be called for every property found
413 * @data: data to be passed to the function
415 * Retrieve the property key/value pairs belonging to the
416 * udev device. For every key/value pair, the passed function will be
417 * called. If the function returns 1, remaning properties will be
420 * Returns: the number of properties passed to the caller, or a negative value on error
422 int udev_device_get_properties(struct udev_device *udev_device,
423 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
426 struct util_name_entry *name_loop;
429 if (udev_device == NULL)
431 list_for_each_entry(name_loop, &udev_device->env_list, node) {
432 char name[UTIL_NAME_SIZE];
435 strncpy(name, name_loop->name, sizeof(name));
436 val = strchr(name, '=');
442 if (cb(udev_device, name, val, data) != 0)
448 const char *udev_device_get_driver(struct udev_device *udev_device)
450 char driver[UTIL_NAME_SIZE];
452 if (udev_device == NULL)
454 if (udev_device->driver != NULL)
455 return udev_device->driver;
456 if (util_get_sys_driver(udev_device->udev, udev_device->devpath, driver, sizeof(driver)) < 2)
458 udev_device->driver = strdup(driver);
459 return udev_device->driver;
462 dev_t udev_device_get_devnum(struct udev_device *udev_device)
464 if (udev_device == NULL)
465 return makedev(0, 0);
466 return udev_device->devnum;
469 const char *udev_device_get_action(struct udev_device *udev_device)
471 if (udev_device == NULL)
473 return udev_device->action;
476 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
478 if (udev_device == NULL)
480 return udev_device->seqnum;
483 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
485 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
487 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
488 udev_device->sysname = strrchr(udev_device->syspath, '/');
489 if (udev_device->sysname != NULL)
490 udev_device->sysname = &udev_device->sysname[1];
494 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
496 udev_device->subsystem = strdup(subsystem);
497 if (udev_device->subsystem == NULL)
502 int device_set_devname(struct udev_device *udev_device, const char *devname)
504 udev_device->devname = strdup(devname);
505 if (udev_device->devname == NULL)
510 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
512 if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
517 int device_add_property(struct udev_device *udev_device, const char *property)
519 if (util_name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
524 int device_set_action(struct udev_device *udev_device, const char *action)
526 udev_device->action = strdup(action);
527 if (udev_device->action == NULL)
532 int device_set_driver(struct udev_device *udev_device, const char *driver)
534 udev_device->driver = strdup(driver);
535 if (udev_device->driver == NULL)
540 const char *device_get_devpath_old(struct udev_device *udev_device)
542 if (udev_device == NULL)
544 return udev_device->devpath_old;
547 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
549 udev_device->devpath_old = strdup(devpath_old);
550 if (udev_device->devpath_old == NULL)
555 const char *device_get_physdevpath(struct udev_device *udev_device)
557 if (udev_device == NULL)
559 return udev_device->physdevpath;
562 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
564 udev_device->physdevpath = strdup(physdevpath);
565 if (udev_device->physdevpath == NULL)
570 int device_get_timeout(struct udev_device *udev_device)
572 if (udev_device == NULL)
574 return udev_device->timeout;
577 int device_set_timeout(struct udev_device *udev_device, int timeout)
579 udev_device->timeout = timeout;
583 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
585 udev_device->seqnum = seqnum;
589 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
591 udev_device->devnum = devnum;
595 int device_get_num_fake_partitions(struct udev_device *udev_device)
597 if (udev_device == NULL)
599 return udev_device->num_fake_partitions;
602 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
604 udev_device->num_fake_partitions = num;
608 int device_get_devlink_priority(struct udev_device *udev_device)
610 if (udev_device == NULL)
612 return udev_device->devlink_priority;
615 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
617 udev_device->devlink_priority = prio;
621 int device_get_ignore_remove(struct udev_device *udev_device)
623 if (udev_device == NULL)
625 return udev_device->ignore_remove;
628 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
630 udev_device->ignore_remove = ignore;