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"
41 struct list_head link_list;
42 struct list_head env_list;
49 unsigned long long int seqnum;
50 int num_fake_partitions;
55 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
59 /* translate to location of db file */
60 util_strlcpy(filename, udev_get_dev_path(udev), len);
61 start = util_strlcat(filename, "/.udev/db/", len);
62 util_strlcat(filename, devpath, len);
63 return util_path_encode(&filename[start], len - start);
66 static int device_read_db(struct udev_device *udev_device)
69 char filename[UTIL_PATH_SIZE];
70 char line[UTIL_LINE_SIZE];
74 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
76 if (lstat(filename, &stats) != 0) {
77 info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno));
80 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
81 char target[UTIL_PATH_SIZE];
84 info(udev_device->udev, "found a symlink as db file\n");
85 target_len = readlink(filename, target, sizeof(target));
87 target[target_len] = '\0';
89 info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
92 dbg(udev_device->udev, "db link points to '%s'\n", target);
93 if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
98 f = fopen(filename, "r");
100 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
103 while (fgets(line, sizeof(line), f)) {
106 unsigned int maj, min;
116 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
119 sscanf(val, "%u:%u", &maj, &min);
120 device_set_devnum(udev_device, makedev(maj, min));
123 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
124 util_strlcat(filename, "/", sizeof(filename));
125 util_strlcat(filename, val, sizeof(filename));
126 device_add_devlink(udev_device, filename);
129 device_set_devlink_priority(udev_device, atoi(val));
132 device_set_timeout(udev_device, atoi(val));
135 device_set_num_fake_partitions(udev_device, atoi(val));
138 device_set_ignore_remove(udev_device, atoi(val));
141 device_add_property(udev_device, val);
150 struct udev_device *device_init(struct udev *udev)
152 struct udev_device *udev_device;
157 udev_device = malloc(sizeof(struct udev_device));
158 if (udev_device == NULL)
160 memset(udev_device, 0x00, sizeof(struct udev_device));
161 udev_device->refcount = 1;
162 udev_device->udev = udev;
163 INIT_LIST_HEAD(&udev_device->link_list);
164 INIT_LIST_HEAD(&udev_device->env_list);
165 info(udev_device->udev, "udev_device: %p created\n", udev_device);
170 * udev_device_new_from_devpath:
171 * @udev: udev library context
172 * @devpath: sys device path
174 * Create new udev device, and fill in information from the sysfs
175 * device and the udev database entry. The devpath must not contain
176 * the sysfs mount path, and must contain a leading '/'.
178 * The initial refcount is 1, and needs to be decremented to
179 * release the ressources of the udev device.
181 * Returns: a new udev device, or #NULL, if it does not exist
183 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
185 char path[UTIL_PATH_SIZE];
187 struct udev_device *udev_device;
194 util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
195 util_strlcat(path, devpath, sizeof(path));
196 if (stat(path, &statbuf) != 0)
198 if (!S_ISDIR(statbuf.st_mode))
201 udev_device = device_init(udev);
202 if (udev_device == NULL)
205 /* resolve possible symlink to real path */
206 util_strlcpy(path, devpath, sizeof(path));
207 util_resolve_sys_link(udev, path, sizeof(path));
208 device_set_devpath(udev_device, path);
209 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
211 if (device_read_db(udev_device) >= 0)
212 info(udev, "device %p filled with udev database data\n", udev_device);
218 * udev_device_get_udev:
219 * @udev_device: udev device
221 * Retrieve the udev library context the device was created with.
223 * Returns: the udev library context
225 struct udev *udev_device_get_udev(struct udev_device *udev_device)
227 if (udev_device == NULL)
229 return udev_device->udev;
234 * @udev_device: udev device
236 * Take a reference of a udev device.
238 * Returns: the passed udev device
240 struct udev_device *udev_device_ref(struct udev_device *udev_device)
242 if (udev_device == NULL)
244 udev_device->refcount++;
250 * @udev_device: udev device
252 * Drop a reference of a udev device. If the refcount reaches zero,
253 * the ressources of the device will be released.
256 void udev_device_unref(struct udev_device *udev_device)
258 if (udev_device == NULL)
260 udev_device->refcount--;
261 if (udev_device->refcount > 0)
263 free(udev_device->syspath);
264 free(udev_device->devname);
265 free(udev_device->subsystem);
266 util_name_list_cleanup(udev_device->udev, &udev_device->link_list);
267 util_name_list_cleanup(udev_device->udev, &udev_device->env_list);
268 free(udev_device->action);
269 free(udev_device->driver);
270 free(udev_device->devpath_old);
271 free(udev_device->physdevpath);
272 info(udev_device->udev, "udev_device: %p released\n", udev_device);
277 * udev_device_get_devpath:
278 * @udev_device: udev device
280 * Retrieve the kernel devpath value of the udev device. The path
281 * does not contain the sys mount point, and starts with a '/'.
283 * Returns: the devpath of the udev device
285 const char *udev_device_get_devpath(struct udev_device *udev_device)
287 if (udev_device == NULL)
289 return udev_device->devpath;
293 * udev_device_get_syspath:
294 * @udev_device: udev device
296 * Retrieve the sys path of the udev device. The path is an
297 * absolute path and starts with the sys mount point.
299 * Returns: the sys path of the udev device
301 const char *udev_device_get_syspath(struct udev_device *udev_device)
303 if (udev_device == NULL)
305 return udev_device->syspath;
309 * udev_device_get_devname:
310 * @udev_device: udev device
312 * Retrieve the device node file name belonging to the udev device.
313 * The path is an absolute path, and starts with the device directory.
315 * Returns: the device node file name of the udev device, or #NULL if no device node exists
317 const char *udev_device_get_devname(struct udev_device *udev_device)
319 if (udev_device == NULL)
321 return udev_device->devname;
325 * udev_device_get_subsystem:
326 * @udev_device: udev device
328 * Retrieve the subsystem string of the udev device. The string does not
331 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
333 const char *udev_device_get_subsystem(struct udev_device *udev_device)
335 char subsystem[UTIL_PATH_SIZE];
337 if (udev_device == NULL)
339 if (udev_device->subsystem != NULL)
340 return udev_device->subsystem;
341 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
343 udev_device->subsystem = strdup(subsystem);
344 return udev_device->subsystem;
348 * udev_device_get_devlinks:
349 * @udev_device: udev device
350 * @cb: function to be called for every device link found
351 * @data: data to be passed to the function
353 * Retrieve the device links pointing to the device file of the
354 * udev device. For every device link, the passed function will be
355 * called with the device link string.
356 * The path is an absolute path, and starts with the device directory.
357 * If the function returns 1, remaning device links will be ignored.
359 * Returns: the number of device links passed to the caller, or a negative value on error
361 int udev_device_get_devlinks(struct udev_device *udev_device,
362 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
365 struct util_name_entry *name_loop;
368 if (udev_device == NULL)
370 list_for_each_entry(name_loop, &udev_device->link_list, node) {
372 if (cb(udev_device, name_loop->name, data) != 0)
379 * udev_device_get_properties:
380 * @udev_device: udev device
381 * @cb: function to be called for every property found
382 * @data: data to be passed to the function
384 * Retrieve the property key/value pairs belonging to the
385 * udev device. For every key/value pair, the passed function will be
386 * called. If the function returns 1, remaning properties will be
389 * Returns: the number of properties passed to the caller, or a negative value on error
391 int udev_device_get_properties(struct udev_device *udev_device,
392 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
395 struct util_name_entry *name_loop;
398 if (udev_device == NULL)
400 list_for_each_entry(name_loop, &udev_device->env_list, node) {
401 char name[UTIL_PATH_SIZE];
404 strncpy(name, name_loop->name, sizeof(name));
405 val = strchr(name, '=');
411 if (cb(udev_device, name, val, data) != 0)
417 const char *udev_device_get_driver(struct udev_device *udev_device)
419 char driver[UTIL_PATH_SIZE];
421 if (udev_device == NULL)
423 if (udev_device->driver != NULL)
424 return udev_device->driver;
425 if (util_get_sys_driver(udev_device->udev, udev_device->devpath, driver, sizeof(driver)) < 2)
427 udev_device->driver = strdup(driver);
428 return udev_device->driver;
431 dev_t udev_device_get_devnum(struct udev_device *udev_device)
433 if (udev_device == NULL)
434 return makedev(0, 0);
435 return udev_device->devnum;
438 const char *udev_device_get_action(struct udev_device *udev_device)
440 if (udev_device == NULL)
442 return udev_device->action;
445 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
447 if (udev_device == NULL)
449 return udev_device->seqnum;
452 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
454 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
456 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
460 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
462 udev_device->subsystem = strdup(subsystem);
463 if (udev_device->subsystem == NULL)
468 int device_set_devname(struct udev_device *udev_device, const char *devname)
470 udev_device->devname = strdup(devname);
471 if (udev_device->devname == NULL)
476 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
478 if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
483 int device_add_property(struct udev_device *udev_device, const char *property)
485 if (util_name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
490 int device_set_action(struct udev_device *udev_device, const char *action)
492 udev_device->action = strdup(action);
493 if (udev_device->action == NULL)
498 int device_set_driver(struct udev_device *udev_device, const char *driver)
500 udev_device->driver = strdup(driver);
501 if (udev_device->driver == NULL)
506 const char *device_get_devpath_old(struct udev_device *udev_device)
508 if (udev_device == NULL)
510 return udev_device->devpath_old;
513 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
515 udev_device->devpath_old = strdup(devpath_old);
516 if (udev_device->devpath_old == NULL)
521 const char *device_get_physdevpath(struct udev_device *udev_device)
523 if (udev_device == NULL)
525 return udev_device->physdevpath;
528 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
530 udev_device->physdevpath = strdup(physdevpath);
531 if (udev_device->physdevpath == NULL)
536 int device_get_timeout(struct udev_device *udev_device)
538 if (udev_device == NULL)
540 return udev_device->timeout;
543 int device_set_timeout(struct udev_device *udev_device, int timeout)
545 udev_device->timeout = timeout;
549 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
551 udev_device->seqnum = seqnum;
555 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
557 udev_device->devnum = devnum;
561 int device_get_num_fake_partitions(struct udev_device *udev_device)
563 if (udev_device == NULL)
565 return udev_device->num_fake_partitions;
568 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
570 udev_device->num_fake_partitions = num;
574 int device_get_devlink_priority(struct udev_device *udev_device)
576 if (udev_device == NULL)
578 return udev_device->devlink_priority;
581 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
583 udev_device->devlink_priority = prio;
587 int device_get_ignore_remove(struct udev_device *udev_device)
589 if (udev_device == NULL)
591 return udev_device->ignore_remove;
594 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
596 udev_device->ignore_remove = ignore;