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"
35 void udev_log(struct udev *udev,
36 int priority, const char *file, int line, const char *fn,
37 const char *format, ...)
41 va_start(args, format);
42 udev->log_fn(udev, priority, file, line, fn, format, args);
46 static void log_stderr(struct udev *udev,
47 int priority, const char *file, int line, const char *fn,
48 const char *format, va_list args)
53 if (getenv("LIBUDEV_DEBUG") != NULL)
60 fprintf(stderr, "libudev: %s: ", fn);
61 vfprintf(stderr, format, args);
65 /* glue to udev logging, needed until udev logging code is "fixed" */
67 void log_message(int priority, const char *format, ...)
71 va_start(args, format);
72 log_stderr(NULL, priority, NULL, 0, "", format, args);
77 static ssize_t get_subsystem(struct udev *udev, const char *devpath, char *subsystem, size_t size)
83 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
84 strlcat(path, devpath, sizeof(path));
85 strlcat(path, "/subsystem", sizeof(path));
86 len = readlink(path, path, sizeof(path));
87 if (len < 0 || len >= (ssize_t) sizeof(path))
90 pos = strrchr(path, '/');
94 return strlcpy(subsystem, pos, size);
100 * Create udev library context.
102 * The initial refcount is 1, and needs to be decremented to
103 * release the ressources of the udev library context.
105 * Returns: a new udev library context
107 struct udev *udev_new(void)
111 udev = malloc(sizeof(struct udev));
114 memset(udev, 0x00, (sizeof(struct udev)));
116 udev->log_fn = log_stderr;
119 log_info(udev, "context %p created\n", udev);
125 * @udev: udev library context
127 * Take a reference of the udev library context.
129 * Returns: the passed udev library context
131 struct udev *udev_ref(struct udev *udev)
139 * @udev: udev library context
141 * Drop a reference of the udev library context. If the refcount
142 * reaches zero, the ressources of the context will be released.
145 void udev_unref(struct udev *udev)
148 if (udev->refcount > 0)
151 log_info(udev, "context %p released\n", udev);
157 * @udev: udev library context
158 * @log_fn: function to be called for logging messages
160 * The built-in logging, which writes to stderr if the
161 * LIBUDEV_DEBUG environment variable is set, can be
162 * overridden by a custom function, to plug log messages
163 * into the users logging functionality.
166 void udev_set_log_fn(struct udev *udev,
167 void (*log_fn)(struct udev *udev,
168 int priority, const char *file, int line, const char *fn,
169 const char *format, va_list args))
171 udev->log_fn = log_fn;
172 log_info(udev, "custom logging function %p registered\n", udev);
177 * @udev: udev library context
179 * Retrieve the sysfs mount point. The default is "/sys". For
180 * testing purposes, it can be overridden with the environment
181 * variable SYSFS_PATH.
183 * Returns: the sys mount point
185 const char *udev_get_sys_path(struct udev *udev)
192 * @udev: udev library context
194 * Retrieve the device directory path. The default value is "/dev",
195 * the actual value may be overridden in the udev configuration
198 * Returns: the device directory path
200 const char *udev_get_dev_path(struct udev *udev)
205 static struct udev_device *device_init(struct udev *udev)
207 struct udev_device *udev_device;
209 udev_device = malloc(sizeof(struct udev_device));
210 if (udev_device == NULL)
212 memset(udev_device, 0x00, sizeof(struct udev_device));
213 udev_device->refcount = 1;
214 udev_device->udev = udev;
219 * udev_device_new_from_devpath:
220 * @udev: udev library context
221 * @devpath: sys device path
223 * Create new udev device, and fill in information from the sysfs
224 * device and the udev database entry. The devpath must not contain
225 * the sysfs mount path, and must contain a leading '/'.
227 * The initial refcount is 1, and needs to be decremented to
228 * release the ressources of the udev device.
230 * Returns: a new udev device, or #NULL, if it does not exist
232 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
234 char path[PATH_SIZE];
236 struct udev_device *udev_device;
239 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
240 strlcat(path, devpath, sizeof(path));
241 if (stat(path, &statbuf) != 0)
243 if (!S_ISDIR(statbuf.st_mode))
246 udev_device = device_init(udev);
247 if (udev_device == NULL)
250 udev_device->udevice = udev_device_init(NULL);
251 if (udev_device->udevice == NULL) {
255 log_info(udev, "device %p created\n", udev_device);
257 strlcpy(path, devpath, sizeof(path));
258 sysfs_resolve_link(path, sizeof(path));
260 err = udev_db_get_device(udev_device->udevice, path);
262 log_info(udev, "device %p filled with udev database data\n", udev_device);
263 log_info(udev, "device %p filled with %s data\n", udev_device, udev_device_get_devpath(udev_device));
268 * udev_device_get_udev:
270 * Retrieve the udev library context the device was created with.
272 * Returns: the udev library context
274 struct udev *udev_device_get_udev(struct udev_device *udev_device)
276 return udev_device->udev;
281 * @udev_device: udev device
283 * Take a reference of a udev device.
285 * Returns: the passed udev device
287 struct udev_device *udev_device_ref(struct udev_device *udev_device)
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 udev_device->refcount--;
304 if (udev_device->refcount > 0)
306 udev_device_cleanup(udev_device->udevice);
311 * udev_device_get_devpath:
312 * @udev_device: udev device
314 * Retrieve the sys path of the udev device. The path does not contain
315 * the sys mount point.
317 * Returns: the sys path of the udev device
319 const char *udev_device_get_devpath(struct udev_device *udev_device)
321 return udev_device->udevice->dev->devpath;
325 * udev_device_get_devname:
326 * @udev_device: udev device
328 * Retrieve the device node file name belonging to the udev device.
329 * The path does not contain the device directory, and does not contain
332 * Returns: the device node file name of the udev device, or #NULL if no device node exists
334 const char *udev_device_get_devname(struct udev_device *udev_device)
336 if (udev_device->udevice->name[0] == '\0')
338 return udev_device->udevice->name;
342 * udev_device_get_subsystem:
343 * @udev_device: udev device
345 * Retrieve the subsystem string of the udev device. The string does not
348 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
350 const char *udev_device_get_subsystem(struct udev_device *udev_device)
352 struct sysfs_device *dev = udev_device->udevice->dev;
353 if (dev->subsystem[0] != '\0')
354 return dev->subsystem;
355 if (get_subsystem(udev_device->udev, dev->devpath,
356 dev->subsystem, sizeof(dev->subsystem)) < 2)
358 return dev->subsystem;
362 * udev_device_get_devlinks:
363 * @udev_device: udev device
364 * @cb: function to be called for every device link found
365 * @data: data to be passed to the function
367 * Retrieve the device links pointing to the device file of the
368 * udev device. For every device link, the passed function will be
369 * called with the device link string. If the function returns 1,
370 * remaning device links will be ignored. The device link path
371 * does not contain the device directory, and does not contain
374 * Returns: the number of device links passed to the caller, or a negative value on error
376 int udev_device_get_devlinks(struct udev_device *udev_device,
377 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
380 struct name_entry *name_loop;
383 list_for_each_entry(name_loop, &udev_device->udevice->symlink_list, node) {
385 if (cb(udev_device, name_loop->name, data) != 0)
392 * udev_device_get_properties:
393 * @udev_device: udev device
394 * @cb: function to be called for every property found
395 * @data: data to be passed to the function
397 * Retrieve the property key/value pairs belonging to the
398 * udev device. For every key/value pair, the passed function will be
399 * called. If the function returns 1, remaning properties will be
402 * Returns: the number of properties passed to the caller, or a negative value on error
404 int udev_device_get_properties(struct udev_device *udev_device,
405 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
408 struct name_entry *name_loop;
411 list_for_each_entry(name_loop, &udev_device->udevice->env_list, node) {
412 char name[PATH_SIZE];
415 strncpy(name, name_loop->name, PATH_SIZE);
416 name[PATH_SIZE-1] = '\0';
417 val = strchr(name, '=');
422 if (cb(udev_device, name, val, data) != 0)
428 static int devices_scan_subsystem(struct udev *udev,
429 const char *basedir, const char *subsystem, const char *subdir,
430 struct list_head *device_list)
432 char path[PATH_SIZE];
437 len = strlcpy(path, udev_get_sys_path(udev), sizeof(path));
438 strlcat(path, basedir, sizeof(path));
439 strlcat(path, "/", sizeof(path));
440 strlcat(path, subsystem, sizeof(path));
442 strlcat(path, subdir, sizeof(path));
446 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
447 char devpath[PATH_SIZE];
449 if (dent->d_name[0] == '.')
451 strlcpy(devpath, &path[len], sizeof(devpath));
452 strlcat(devpath, "/", sizeof(devpath));
453 strlcat(devpath, dent->d_name, sizeof(devpath));
454 sysfs_resolve_link(devpath, sizeof(devpath));
455 name_list_add(device_list, devpath, 1);
461 static int devices_scan_subsystems(struct udev *udev,
462 const char *basedir, const char *subsystem, const char *subdir,
463 struct list_head *device_list)
465 char path[PATH_SIZE];
469 if (subsystem != NULL)
470 return devices_scan_subsystem(udev, basedir, subsystem, subdir, device_list);
472 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
473 strlcat(path, basedir, sizeof(path));
477 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
478 if (dent->d_name[0] == '.')
480 devices_scan_subsystem(udev, basedir, dent->d_name, subdir, device_list);
486 static int devices_delay(struct udev *udev, const char *devpath)
488 static const char *delay_device_list[] = {
495 for (i = 0; delay_device_list[i] != NULL; i++) {
496 if (strstr(devpath, delay_device_list[i]) != NULL) {
497 log_info(udev, "delaying: %s\n", devpath);
504 static int devices_call(struct udev *udev, const char *devpath,
505 int (*cb)(struct udev *udev,
506 const char *devpath, const char *subsystem, const char *name,
511 char subsystem[NAME_SIZE];
514 name = strrchr(devpath, '/');
519 if (get_subsystem(udev, devpath, subsystem, sizeof(subsystem)) < 2)
521 *cb_rc = cb(udev, devpath, subsystem, name, data);
526 * udev_devices_enumerate:
527 * @udev_device: udev device
528 * @cb: function to be called for every property found
529 * @data: data to be passed to the function
531 * Retrieve the property key/value pairs belonging to the
532 * udev device. For every key/value pair, the passed function will be
533 * called. If the function returns 1, remaning properties will be
536 * Returns: the number of properties passed to the caller, or a negative value on error
538 int udev_devices_enumerate(struct udev *udev, const char *subsystem,
539 int (*cb)(struct udev *udev,
540 const char *devpath, const char *subsystem, const char *name, void *data),
543 char base[PATH_SIZE];
545 struct list_head device_list;
546 struct name_entry *loop_device;
547 struct name_entry *tmp_device;
551 INIT_LIST_HEAD(&device_list);
553 /* if we have /sys/subsystem/, forget all the old stuff */
554 strlcpy(base, sysfs_path, sizeof(base));
555 strlcat(base, "/subsystem", sizeof(base));
556 if (stat(base, &statbuf) == 0) {
557 devices_scan_subsystems(udev, "/subsystem", subsystem, "/devices", &device_list);
559 devices_scan_subsystems(udev, "/bus", subsystem, "/devices", &device_list);
560 devices_scan_subsystems(udev, "/class", subsystem, NULL, &device_list);
563 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
564 if (devices_delay(udev, loop_device->name))
567 if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
569 list_del(&loop_device->node);
573 /* handle remaining delayed devices */
574 list_for_each_entry_safe(loop_device, tmp_device, &device_list, node) {
576 if (devices_call(udev, loop_device->name, cb, data, &cb_rc) == 0)
578 list_del(&loop_device->node);