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 struct udev_device *device_init(struct udev *udev)
58 struct udev_device *udev_device;
63 udev_device = malloc(sizeof(struct udev_device));
64 if (udev_device == NULL)
66 memset(udev_device, 0x00, sizeof(struct udev_device));
67 udev_device->refcount = 1;
68 udev_device->udev = udev;
69 INIT_LIST_HEAD(&udev_device->link_list);
70 INIT_LIST_HEAD(&udev_device->env_list);
71 info(udev_device->udev, "udev_device: %p created\n", udev_device);
76 * udev_device_new_from_devpath:
77 * @udev: udev library context
78 * @devpath: sys device path
80 * Create new udev device, and fill in information from the sysfs
81 * device and the udev database entry. The devpath must not contain
82 * the sysfs mount path, and must contain a leading '/'.
84 * The initial refcount is 1, and needs to be decremented to
85 * release the ressources of the udev device.
87 * Returns: a new udev device, or #NULL, if it does not exist
89 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
93 struct udev_device *udev_device;
94 struct udevice *udevice;
95 struct name_entry *name_loop;
103 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
104 strlcat(path, devpath, sizeof(path));
105 if (stat(path, &statbuf) != 0)
107 if (!S_ISDIR(statbuf.st_mode))
110 udev_device = device_init(udev);
111 if (udev_device == NULL)
114 udevice = udev_device_init(udev);
115 if (udevice == NULL) {
120 /* resolve possible symlink to real path */
121 strlcpy(path, devpath, sizeof(path));
122 sysfs_resolve_link(udev, path, sizeof(path));
123 device_set_devpath(udev_device, devpath);
124 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
126 err = udev_db_get_device(udevice, path);
128 info(udev, "device %p filled with udev database data\n", udev_device);
130 if (udevice->name[0] != '\0')
131 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
133 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
134 char name[PATH_SIZE];
136 strlcpy(name, udev_get_dev_path(udev), sizeof(name));
137 strlcat(name, "/", sizeof(name));
138 strlcat(name, name_loop->name, sizeof(name));
139 name_list_add(udev, &udev_device->link_list, name, 0);
142 list_for_each_entry(name_loop, &udevice->env_list, node)
143 name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
145 udev_device_cleanup(udevice);
150 * udev_device_get_udev:
151 * @udev_device: udev device
153 * Retrieve the udev library context the device was created with.
155 * Returns: the udev library context
157 struct udev *udev_device_get_udev(struct udev_device *udev_device)
159 if (udev_device == NULL)
161 return udev_device->udev;
166 * @udev_device: udev device
168 * Take a reference of a udev device.
170 * Returns: the passed udev device
172 struct udev_device *udev_device_ref(struct udev_device *udev_device)
174 if (udev_device == NULL)
176 udev_device->refcount++;
182 * @udev_device: udev device
184 * Drop a reference of a udev device. If the refcount reaches zero,
185 * the ressources of the device will be released.
188 void udev_device_unref(struct udev_device *udev_device)
190 if (udev_device == NULL)
192 udev_device->refcount--;
193 if (udev_device->refcount > 0)
195 free(udev_device->syspath);
196 free(udev_device->devname);
197 free(udev_device->subsystem);
198 name_list_cleanup(udev_device->udev, &udev_device->link_list);
199 name_list_cleanup(udev_device->udev, &udev_device->env_list);
200 free(udev_device->action);
201 free(udev_device->driver);
202 free(udev_device->devpath_old);
203 free(udev_device->physdevpath);
204 info(udev_device->udev, "udev_device: %p released\n", udev_device);
209 * udev_device_get_devpath:
210 * @udev_device: udev device
212 * Retrieve the kernel devpath value of the udev device. The path
213 * does not contain the sys mount point, and starts with a '/'.
215 * Returns: the devpath of the udev device
217 const char *udev_device_get_devpath(struct udev_device *udev_device)
219 if (udev_device == NULL)
221 return udev_device->devpath;
225 * udev_device_get_syspath:
226 * @udev_device: udev device
228 * Retrieve the sys path of the udev device. The path is an
229 * absolute path and starts with the sys mount point.
231 * Returns: the sys path of the udev device
233 const char *udev_device_get_syspath(struct udev_device *udev_device)
235 if (udev_device == NULL)
237 return udev_device->syspath;
241 * udev_device_get_devname:
242 * @udev_device: udev device
244 * Retrieve the device node file name belonging to the udev device.
245 * The path is an absolute path, and starts with the device directory.
247 * Returns: the device node file name of the udev device, or #NULL if no device node exists
249 const char *udev_device_get_devname(struct udev_device *udev_device)
251 if (udev_device == NULL)
253 return udev_device->devname;
257 * udev_device_get_subsystem:
258 * @udev_device: udev device
260 * Retrieve the subsystem string of the udev device. The string does not
263 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
265 const char *udev_device_get_subsystem(struct udev_device *udev_device)
267 char subsystem[NAME_SIZE];
269 if (udev_device == NULL)
271 if (udev_device->subsystem != NULL)
272 return udev_device->subsystem;
273 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
275 udev_device->subsystem = strdup(subsystem);
276 return udev_device->subsystem;
280 * udev_device_get_devlinks:
281 * @udev_device: udev device
282 * @cb: function to be called for every device link found
283 * @data: data to be passed to the function
285 * Retrieve the device links pointing to the device file of the
286 * udev device. For every device link, the passed function will be
287 * called with the device link string.
288 * The path is an absolute path, and starts with the device directory.
289 * If the function returns 1, remaning device links will be ignored.
291 * Returns: the number of device links passed to the caller, or a negative value on error
293 int udev_device_get_devlinks(struct udev_device *udev_device,
294 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
297 struct name_entry *name_loop;
300 if (udev_device == NULL)
302 list_for_each_entry(name_loop, &udev_device->link_list, node) {
304 if (cb(udev_device, name_loop->name, data) != 0)
311 * udev_device_get_properties:
312 * @udev_device: udev device
313 * @cb: function to be called for every property found
314 * @data: data to be passed to the function
316 * Retrieve the property key/value pairs belonging to the
317 * udev device. For every key/value pair, the passed function will be
318 * called. If the function returns 1, remaning properties will be
321 * Returns: the number of properties passed to the caller, or a negative value on error
323 int udev_device_get_properties(struct udev_device *udev_device,
324 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
327 struct name_entry *name_loop;
330 if (udev_device == NULL)
332 list_for_each_entry(name_loop, &udev_device->env_list, node) {
333 char name[PATH_SIZE];
336 strncpy(name, name_loop->name, PATH_SIZE);
337 name[PATH_SIZE-1] = '\0';
338 val = strchr(name, '=');
344 if (cb(udev_device, name, val, data) != 0)
350 const char *udev_device_get_driver(struct udev_device *udev_device)
352 char driver[NAME_SIZE];
354 if (udev_device == NULL)
356 if (udev_device->driver != NULL)
357 return udev_device->driver;
358 if (util_get_sys_driver(udev_device->udev, udev_device->devpath, driver, sizeof(driver)) < 2)
360 udev_device->driver = strdup(driver);
361 return udev_device->driver;
364 dev_t udev_device_get_devnum(struct udev_device *udev_device)
366 if (udev_device == NULL)
367 return makedev(0, 0);
368 return udev_device->devnum;
371 const char *udev_device_get_action(struct udev_device *udev_device)
373 if (udev_device == NULL)
375 return udev_device->action;
378 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
380 if (udev_device == NULL)
382 return udev_device->seqnum;
385 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
387 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
389 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
393 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
395 udev_device->subsystem = strdup(subsystem);
396 if (udev_device->subsystem == NULL)
401 int device_set_devname(struct udev_device *udev_device, const char *devname)
403 udev_device->devname = strdup(devname);
404 if (udev_device->devname == NULL)
409 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
411 if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
416 int device_add_property(struct udev_device *udev_device, const char *property)
418 if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
423 int device_set_action(struct udev_device *udev_device, const char *action)
425 udev_device->action = strdup(action);
426 if (udev_device->action == NULL)
431 int device_set_driver(struct udev_device *udev_device, const char *driver)
433 udev_device->driver = strdup(driver);
434 if (udev_device->driver == NULL)
439 const char *device_get_devpath_old(struct udev_device *udev_device)
441 if (udev_device == NULL)
443 return udev_device->devpath_old;
446 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
448 udev_device->devpath_old = strdup(devpath_old);
449 if (udev_device->devpath_old == NULL)
454 const char *device_get_physdevpath(struct udev_device *udev_device)
456 if (udev_device == NULL)
458 return udev_device->physdevpath;
461 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
463 udev_device->physdevpath = strdup(physdevpath);
464 if (udev_device->physdevpath == NULL)
469 int device_get_timeout(struct udev_device *udev_device)
471 if (udev_device == NULL)
473 return udev_device->timeout;
476 int device_set_timeout(struct udev_device *udev_device, int timeout)
478 udev_device->timeout = timeout;
482 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
484 udev_device->seqnum = seqnum;
488 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
490 udev_device->devnum = devnum;
494 int device_get_num_fake_partitions(struct udev_device *udev_device)
496 if (udev_device == NULL)
498 return udev_device->num_fake_partitions;
501 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
503 udev_device->num_fake_partitions = num;
506 int device_get_link_priority(struct udev_device *udev_device)
508 if (udev_device == NULL)
510 return udev_device->link_priority;
513 int device_set_link_priority(struct udev_device *udev_device, int prio)
515 udev_device->link_priority = prio;
518 int device_get_ignore_remove(struct udev_device *udev_device)
520 if (udev_device == NULL)
522 return udev_device->ignore_remove;
525 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
527 udev_device->ignore_remove = ignore;