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;
53 struct udev_device *device_init(struct udev *udev)
55 struct udev_device *udev_device;
60 udev_device = malloc(sizeof(struct udev_device));
61 if (udev_device == NULL)
63 memset(udev_device, 0x00, sizeof(struct udev_device));
64 udev_device->refcount = 1;
65 udev_device->udev = udev;
66 INIT_LIST_HEAD(&udev_device->link_list);
67 INIT_LIST_HEAD(&udev_device->env_list);
68 info(udev_device->udev, "udev_device: %p created\n", udev_device);
73 * udev_device_new_from_devpath:
74 * @udev: udev library context
75 * @devpath: sys device path
77 * Create new udev device, and fill in information from the sysfs
78 * device and the udev database entry. The devpath must not contain
79 * the sysfs mount path, and must contain a leading '/'.
81 * The initial refcount is 1, and needs to be decremented to
82 * release the ressources of the udev device.
84 * Returns: a new udev device, or #NULL, if it does not exist
86 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
90 struct udev_device *udev_device;
91 struct udevice *udevice;
92 struct name_entry *name_loop;
100 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
101 strlcat(path, devpath, sizeof(path));
102 if (stat(path, &statbuf) != 0)
104 if (!S_ISDIR(statbuf.st_mode))
107 udev_device = device_init(udev);
108 if (udev_device == NULL)
111 udevice = udev_device_init(udev);
112 if (udevice == NULL) {
117 /* resolve possible symlink to real path */
118 strlcpy(path, devpath, sizeof(path));
119 sysfs_resolve_link(udev, path, sizeof(path));
120 device_set_devpath(udev_device, devpath);
121 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
123 err = udev_db_get_device(udevice, path);
125 info(udev, "device %p filled with udev database data\n", udev_device);
127 if (udevice->name[0] != '\0')
128 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
130 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
131 char name[PATH_SIZE];
133 strlcpy(name, udev_get_dev_path(udev), sizeof(name));
134 strlcat(name, "/", sizeof(name));
135 strlcat(name, name_loop->name, sizeof(name));
136 name_list_add(udev, &udev_device->link_list, name, 0);
139 list_for_each_entry(name_loop, &udevice->env_list, node)
140 name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
142 udev_device_cleanup(udevice);
147 * udev_device_get_udev:
148 * @udev_device: udev device
150 * Retrieve the udev library context the device was created with.
152 * Returns: the udev library context
154 struct udev *udev_device_get_udev(struct udev_device *udev_device)
156 if (udev_device == NULL)
158 return udev_device->udev;
163 * @udev_device: udev device
165 * Take a reference of a udev device.
167 * Returns: the passed udev device
169 struct udev_device *udev_device_ref(struct udev_device *udev_device)
171 if (udev_device == NULL)
173 udev_device->refcount++;
179 * @udev_device: udev device
181 * Drop a reference of a udev device. If the refcount reaches zero,
182 * the ressources of the device will be released.
185 void udev_device_unref(struct udev_device *udev_device)
187 if (udev_device == NULL)
189 udev_device->refcount--;
190 if (udev_device->refcount > 0)
192 free(udev_device->syspath);
193 free(udev_device->devname);
194 free(udev_device->subsystem);
195 name_list_cleanup(udev_device->udev, &udev_device->link_list);
196 name_list_cleanup(udev_device->udev, &udev_device->env_list);
197 info(udev_device->udev, "udev_device: %p released\n", udev_device);
202 * udev_device_get_devpath:
203 * @udev_device: udev device
205 * Retrieve the kernel devpath value of the udev device. The path
206 * does not contain the sys mount point, and starts with a '/'.
208 * Returns: the devpath of the udev device
210 const char *udev_device_get_devpath(struct udev_device *udev_device)
212 if (udev_device == NULL)
214 return udev_device->devpath;
218 * udev_device_get_syspath:
219 * @udev_device: udev device
221 * Retrieve the sys path of the udev device. The path is an
222 * absolute path and starts with the sys mount point.
224 * Returns: the sys path of the udev device
226 const char *udev_device_get_syspath(struct udev_device *udev_device)
228 if (udev_device == NULL)
230 return udev_device->syspath;
234 * udev_device_get_devname:
235 * @udev_device: udev device
237 * Retrieve the device node file name belonging to the udev device.
238 * The path is an absolute path, and starts with the device directory.
240 * Returns: the device node file name of the udev device, or #NULL if no device node exists
242 const char *udev_device_get_devname(struct udev_device *udev_device)
244 if (udev_device == NULL)
246 return udev_device->devname;
250 * udev_device_get_subsystem:
251 * @udev_device: udev device
253 * Retrieve the subsystem string of the udev device. The string does not
256 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
258 const char *udev_device_get_subsystem(struct udev_device *udev_device)
260 char subsystem[NAME_SIZE];
262 if (udev_device == NULL)
264 if (udev_device->subsystem != NULL)
265 return udev_device->subsystem;
266 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
268 udev_device->subsystem = strdup(subsystem);
269 return udev_device->subsystem;
273 * udev_device_get_devlinks:
274 * @udev_device: udev device
275 * @cb: function to be called for every device link found
276 * @data: data to be passed to the function
278 * Retrieve the device links pointing to the device file of the
279 * udev device. For every device link, the passed function will be
280 * called with the device link string.
281 * The path is an absolute path, and starts with the device directory.
282 * If the function returns 1, remaning device links will be ignored.
284 * Returns: the number of device links passed to the caller, or a negative value on error
286 int udev_device_get_devlinks(struct udev_device *udev_device,
287 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
290 struct name_entry *name_loop;
293 if (udev_device == NULL)
295 list_for_each_entry(name_loop, &udev_device->link_list, node) {
297 if (cb(udev_device, name_loop->name, data) != 0)
304 * udev_device_get_properties:
305 * @udev_device: udev device
306 * @cb: function to be called for every property found
307 * @data: data to be passed to the function
309 * Retrieve the property key/value pairs belonging to the
310 * udev device. For every key/value pair, the passed function will be
311 * called. If the function returns 1, remaning properties will be
314 * Returns: the number of properties passed to the caller, or a negative value on error
316 int udev_device_get_properties(struct udev_device *udev_device,
317 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
320 struct name_entry *name_loop;
323 if (udev_device == NULL)
325 list_for_each_entry(name_loop, &udev_device->env_list, node) {
326 char name[PATH_SIZE];
329 strncpy(name, name_loop->name, PATH_SIZE);
330 name[PATH_SIZE-1] = '\0';
331 val = strchr(name, '=');
337 if (cb(udev_device, name, val, data) != 0)
343 const char *udev_device_get_driver(struct udev_device *udev_device)
345 if (udev_device == NULL)
347 return udev_device->driver;
350 dev_t udev_device_get_devnum(struct udev_device *udev_device)
352 if (udev_device == NULL)
353 return makedev(0, 0);
354 return udev_device->devnum;
357 const char *udev_device_get_action(struct udev_device *udev_device)
359 if (udev_device == NULL)
361 return udev_device->action;
364 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
366 if (udev_device == NULL)
368 return udev_device->seqnum;
371 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
373 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
375 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
379 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
381 udev_device->subsystem = strdup(subsystem);
382 if (udev_device->subsystem == NULL)
387 int device_set_devname(struct udev_device *udev_device, const char *devname)
389 udev_device->devname = strdup(devname);
390 if (udev_device->devname == NULL)
395 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
397 if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
402 int device_add_property(struct udev_device *udev_device, const char *property)
404 if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
409 int device_set_action(struct udev_device *udev_device, const char *action)
411 udev_device->action = strdup(action);
412 if (udev_device->action == NULL)
417 int device_set_driver(struct udev_device *udev_device, const char *driver)
419 udev_device->driver = strdup(driver);
420 if (udev_device->driver == NULL)
425 const char *device_get_devpath_old(struct udev_device *udev_device)
427 if (udev_device == NULL)
429 return udev_device->devpath_old;
432 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
434 udev_device->devpath_old = strdup(devpath_old);
435 if (udev_device->devpath_old == NULL)
440 const char *device_get_physdevpath(struct udev_device *udev_device)
442 if (udev_device == NULL)
444 return udev_device->physdevpath;
447 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
449 udev_device->physdevpath = strdup(physdevpath);
450 if (udev_device->physdevpath == NULL)
455 int device_get_timeout(struct udev_device *udev_device)
457 if (udev_device == NULL)
459 return udev_device->timeout;
462 int device_set_timeout(struct udev_device *udev_device, int timeout)
464 udev_device->timeout = timeout;
468 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
470 udev_device->seqnum = seqnum;
474 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
476 udev_device->devnum = devnum;