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 free(udev_device->action);
198 free(udev_device->driver);
199 free(udev_device->devpath_old);
200 free(udev_device->physdevpath);
201 info(udev_device->udev, "udev_device: %p released\n", udev_device);
206 * udev_device_get_devpath:
207 * @udev_device: udev device
209 * Retrieve the kernel devpath value of the udev device. The path
210 * does not contain the sys mount point, and starts with a '/'.
212 * Returns: the devpath of the udev device
214 const char *udev_device_get_devpath(struct udev_device *udev_device)
216 if (udev_device == NULL)
218 return udev_device->devpath;
222 * udev_device_get_syspath:
223 * @udev_device: udev device
225 * Retrieve the sys path of the udev device. The path is an
226 * absolute path and starts with the sys mount point.
228 * Returns: the sys path of the udev device
230 const char *udev_device_get_syspath(struct udev_device *udev_device)
232 if (udev_device == NULL)
234 return udev_device->syspath;
238 * udev_device_get_devname:
239 * @udev_device: udev device
241 * Retrieve the device node file name belonging to the udev device.
242 * The path is an absolute path, and starts with the device directory.
244 * Returns: the device node file name of the udev device, or #NULL if no device node exists
246 const char *udev_device_get_devname(struct udev_device *udev_device)
248 if (udev_device == NULL)
250 return udev_device->devname;
254 * udev_device_get_subsystem:
255 * @udev_device: udev device
257 * Retrieve the subsystem string of the udev device. The string does not
260 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
262 const char *udev_device_get_subsystem(struct udev_device *udev_device)
264 char subsystem[NAME_SIZE];
266 if (udev_device == NULL)
268 if (udev_device->subsystem != NULL)
269 return udev_device->subsystem;
270 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
272 udev_device->subsystem = strdup(subsystem);
273 return udev_device->subsystem;
277 * udev_device_get_devlinks:
278 * @udev_device: udev device
279 * @cb: function to be called for every device link found
280 * @data: data to be passed to the function
282 * Retrieve the device links pointing to the device file of the
283 * udev device. For every device link, the passed function will be
284 * called with the device link string.
285 * The path is an absolute path, and starts with the device directory.
286 * If the function returns 1, remaning device links will be ignored.
288 * Returns: the number of device links passed to the caller, or a negative value on error
290 int udev_device_get_devlinks(struct udev_device *udev_device,
291 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
294 struct name_entry *name_loop;
297 if (udev_device == NULL)
299 list_for_each_entry(name_loop, &udev_device->link_list, node) {
301 if (cb(udev_device, name_loop->name, data) != 0)
308 * udev_device_get_properties:
309 * @udev_device: udev device
310 * @cb: function to be called for every property found
311 * @data: data to be passed to the function
313 * Retrieve the property key/value pairs belonging to the
314 * udev device. For every key/value pair, the passed function will be
315 * called. If the function returns 1, remaning properties will be
318 * Returns: the number of properties passed to the caller, or a negative value on error
320 int udev_device_get_properties(struct udev_device *udev_device,
321 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
324 struct name_entry *name_loop;
327 if (udev_device == NULL)
329 list_for_each_entry(name_loop, &udev_device->env_list, node) {
330 char name[PATH_SIZE];
333 strncpy(name, name_loop->name, PATH_SIZE);
334 name[PATH_SIZE-1] = '\0';
335 val = strchr(name, '=');
341 if (cb(udev_device, name, val, data) != 0)
347 const char *udev_device_get_driver(struct udev_device *udev_device)
349 if (udev_device == NULL)
351 return udev_device->driver;
354 dev_t udev_device_get_devnum(struct udev_device *udev_device)
356 if (udev_device == NULL)
357 return makedev(0, 0);
358 return udev_device->devnum;
361 const char *udev_device_get_action(struct udev_device *udev_device)
363 if (udev_device == NULL)
365 return udev_device->action;
368 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
370 if (udev_device == NULL)
372 return udev_device->seqnum;
375 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
377 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
379 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
383 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
385 udev_device->subsystem = strdup(subsystem);
386 if (udev_device->subsystem == NULL)
391 int device_set_devname(struct udev_device *udev_device, const char *devname)
393 udev_device->devname = strdup(devname);
394 if (udev_device->devname == NULL)
399 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
401 if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
406 int device_add_property(struct udev_device *udev_device, const char *property)
408 if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
413 int device_set_action(struct udev_device *udev_device, const char *action)
415 udev_device->action = strdup(action);
416 if (udev_device->action == NULL)
421 int device_set_driver(struct udev_device *udev_device, const char *driver)
423 udev_device->driver = strdup(driver);
424 if (udev_device->driver == NULL)
429 const char *device_get_devpath_old(struct udev_device *udev_device)
431 if (udev_device == NULL)
433 return udev_device->devpath_old;
436 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
438 udev_device->devpath_old = strdup(devpath_old);
439 if (udev_device->devpath_old == NULL)
444 const char *device_get_physdevpath(struct udev_device *udev_device)
446 if (udev_device == NULL)
448 return udev_device->physdevpath;
451 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
453 udev_device->physdevpath = strdup(physdevpath);
454 if (udev_device->physdevpath == NULL)
459 int device_get_timeout(struct udev_device *udev_device)
461 if (udev_device == NULL)
463 return udev_device->timeout;
466 int device_set_timeout(struct udev_device *udev_device, int timeout)
468 udev_device->timeout = timeout;
472 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
474 udev_device->seqnum = seqnum;
478 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
480 udev_device->devnum = devnum;