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;
52 struct udev_device *device_init(struct udev *udev)
54 struct udev_device *udev_device;
59 udev_device = malloc(sizeof(struct udev_device));
60 if (udev_device == NULL)
62 memset(udev_device, 0x00, sizeof(struct udev_device));
63 udev_device->refcount = 1;
64 udev_device->udev = udev;
65 INIT_LIST_HEAD(&udev_device->link_list);
66 INIT_LIST_HEAD(&udev_device->env_list);
67 info(udev_device->udev, "udev_device: %p created\n", udev_device);
72 * udev_device_new_from_devpath:
73 * @udev: udev library context
74 * @devpath: sys device path
76 * Create new udev device, and fill in information from the sysfs
77 * device and the udev database entry. The devpath must not contain
78 * the sysfs mount path, and must contain a leading '/'.
80 * The initial refcount is 1, and needs to be decremented to
81 * release the ressources of the udev device.
83 * Returns: a new udev device, or #NULL, if it does not exist
85 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
89 struct udev_device *udev_device;
90 struct udevice *udevice;
91 struct name_entry *name_loop;
99 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
100 strlcat(path, devpath, sizeof(path));
101 if (stat(path, &statbuf) != 0)
103 if (!S_ISDIR(statbuf.st_mode))
106 udev_device = device_init(udev);
107 if (udev_device == NULL)
110 udevice = udev_device_init(udev);
111 if (udevice == NULL) {
116 /* resolve possible symlink to real path */
117 strlcpy(path, devpath, sizeof(path));
118 sysfs_resolve_link(udev, path, sizeof(path));
119 device_set_devpath(udev_device, devpath);
120 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
122 err = udev_db_get_device(udevice, path);
124 info(udev, "device %p filled with udev database data\n", udev_device);
126 if (udevice->name[0] != '\0')
127 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
129 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
130 char name[PATH_SIZE];
132 strlcpy(name, udev_get_dev_path(udev), sizeof(name));
133 strlcat(name, "/", sizeof(name));
134 strlcat(name, name_loop->name, sizeof(name));
135 name_list_add(udev, &udev_device->link_list, name, 0);
138 list_for_each_entry(name_loop, &udevice->env_list, node)
139 name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
141 udev_device_cleanup(udevice);
146 * udev_device_get_udev:
147 * @udev_device: udev device
149 * Retrieve the udev library context the device was created with.
151 * Returns: the udev library context
153 struct udev *udev_device_get_udev(struct udev_device *udev_device)
155 if (udev_device == NULL)
157 return udev_device->udev;
162 * @udev_device: udev device
164 * Take a reference of a udev device.
166 * Returns: the passed udev device
168 struct udev_device *udev_device_ref(struct udev_device *udev_device)
170 if (udev_device == NULL)
172 udev_device->refcount++;
178 * @udev_device: udev device
180 * Drop a reference of a udev device. If the refcount reaches zero,
181 * the ressources of the device will be released.
184 void udev_device_unref(struct udev_device *udev_device)
186 if (udev_device == NULL)
188 udev_device->refcount--;
189 if (udev_device->refcount > 0)
191 free(udev_device->syspath);
192 free(udev_device->devname);
193 free(udev_device->subsystem);
194 name_list_cleanup(udev_device->udev, &udev_device->link_list);
195 name_list_cleanup(udev_device->udev, &udev_device->env_list);
196 info(udev_device->udev, "udev_device: %p released\n", udev_device);
201 * udev_device_get_devpath:
202 * @udev_device: udev device
204 * Retrieve the kernel devpath value of the udev device. The path
205 * does not contain the sys mount point, and starts with a '/'.
207 * Returns: the devpath of the udev device
209 const char *udev_device_get_devpath(struct udev_device *udev_device)
211 if (udev_device == NULL)
213 return udev_device->devpath;
217 * udev_device_get_syspath:
218 * @udev_device: udev device
220 * Retrieve the sys path of the udev device. The path is an
221 * absolute path and starts with the sys mount point.
223 * Returns: the sys path of the udev device
225 const char *udev_device_get_syspath(struct udev_device *udev_device)
227 if (udev_device == NULL)
229 return udev_device->syspath;
233 * udev_device_get_devname:
234 * @udev_device: udev device
236 * Retrieve the device node file name belonging to the udev device.
237 * The path is an absolute path, and starts with the device directory.
239 * Returns: the device node file name of the udev device, or #NULL if no device node exists
241 const char *udev_device_get_devname(struct udev_device *udev_device)
243 if (udev_device == NULL)
245 return udev_device->devname;
249 * udev_device_get_subsystem:
250 * @udev_device: udev device
252 * Retrieve the subsystem string of the udev device. The string does not
255 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
257 const char *udev_device_get_subsystem(struct udev_device *udev_device)
259 char subsystem[NAME_SIZE];
261 if (udev_device == NULL)
263 if (udev_device->subsystem != NULL)
264 return udev_device->subsystem;
265 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
267 udev_device->subsystem = strdup(subsystem);
268 return udev_device->subsystem;
272 * udev_device_get_devlinks:
273 * @udev_device: udev device
274 * @cb: function to be called for every device link found
275 * @data: data to be passed to the function
277 * Retrieve the device links pointing to the device file of the
278 * udev device. For every device link, the passed function will be
279 * called with the device link string.
280 * The path is an absolute path, and starts with the device directory.
281 * If the function returns 1, remaning device links will be ignored.
283 * Returns: the number of device links passed to the caller, or a negative value on error
285 int udev_device_get_devlinks(struct udev_device *udev_device,
286 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
289 struct name_entry *name_loop;
292 if (udev_device == NULL)
294 list_for_each_entry(name_loop, &udev_device->link_list, node) {
296 if (cb(udev_device, name_loop->name, data) != 0)
303 * udev_device_get_properties:
304 * @udev_device: udev device
305 * @cb: function to be called for every property found
306 * @data: data to be passed to the function
308 * Retrieve the property key/value pairs belonging to the
309 * udev device. For every key/value pair, the passed function will be
310 * called. If the function returns 1, remaning properties will be
313 * Returns: the number of properties passed to the caller, or a negative value on error
315 int udev_device_get_properties(struct udev_device *udev_device,
316 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
319 struct name_entry *name_loop;
322 if (udev_device == NULL)
324 list_for_each_entry(name_loop, &udev_device->env_list, node) {
325 char name[PATH_SIZE];
328 strncpy(name, name_loop->name, PATH_SIZE);
329 name[PATH_SIZE-1] = '\0';
330 val = strchr(name, '=');
336 if (cb(udev_device, name, val, data) != 0)
342 const char *udev_device_get_driver(struct udev_device *udev_device)
344 if (udev_device == NULL)
346 return udev_device->driver;
349 dev_t udev_device_get_devnum(struct udev_device *udev_device)
351 if (udev_device == NULL)
352 return makedev(0, 0);
353 return udev_device->devnum;
356 const char *udev_device_get_action(struct udev_device *udev_device)
358 if (udev_device == NULL)
360 return udev_device->action;
363 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
365 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
367 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
371 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
373 udev_device->subsystem = strdup(subsystem);
374 if (udev_device->subsystem == NULL)
379 int device_set_devname(struct udev_device *udev_device, const char *devname)
381 udev_device->devname = strdup(devname);
382 if (udev_device->devname == NULL)
387 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
389 if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
394 int device_add_property(struct udev_device *udev_device, const char *property)
396 if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
401 int device_set_action(struct udev_device *udev_device, const char *action)
403 udev_device->action = strdup(action);
404 if (udev_device->action == NULL)
409 int device_set_driver(struct udev_device *udev_device, const char *driver)
411 udev_device->driver = strdup(driver);
412 if (udev_device->driver == NULL)
417 const char *device_get_devpath_old(struct udev_device *udev_device)
419 if (udev_device == NULL)
421 return udev_device->devpath_old;
424 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
426 udev_device->devpath_old = strdup(devpath_old);
427 if (udev_device->devpath_old == NULL)
432 const char *device_get_physdevpath(struct udev_device *udev_device)
434 if (udev_device == NULL)
436 return udev_device->physdevpath;
439 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
441 udev_device->physdevpath = strdup(physdevpath);
442 if (udev_device->physdevpath == NULL)
447 int device_get_timeout(struct udev_device *udev_device)
449 if (udev_device == NULL)
451 return udev_device->timeout;
454 int device_set_timeout(struct udev_device *udev_device, int timeout)
456 udev_device->timeout = timeout;
460 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
462 udev_device->devnum = devnum;