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;
46 struct udev_device *device_init(struct udev *udev)
48 struct udev_device *udev_device;
53 udev_device = malloc(sizeof(struct udev_device));
54 if (udev_device == NULL)
56 memset(udev_device, 0x00, sizeof(struct udev_device));
57 udev_device->refcount = 1;
58 udev_device->udev = udev;
59 INIT_LIST_HEAD(&udev_device->link_list);
60 INIT_LIST_HEAD(&udev_device->env_list);
61 log_info(udev_device->udev, "udev_device: %p created\n", udev_device);
66 * udev_device_new_from_devpath:
67 * @udev: udev library context
68 * @devpath: sys device path
70 * Create new udev device, and fill in information from the sysfs
71 * device and the udev database entry. The devpath must not contain
72 * the sysfs mount path, and must contain a leading '/'.
74 * The initial refcount is 1, and needs to be decremented to
75 * release the ressources of the udev device.
77 * Returns: a new udev device, or #NULL, if it does not exist
79 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
83 struct udev_device *udev_device;
84 struct udevice *udevice;
85 struct name_entry *name_loop;
93 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
94 strlcat(path, devpath, sizeof(path));
95 if (stat(path, &statbuf) != 0)
97 if (!S_ISDIR(statbuf.st_mode))
100 udev_device = device_init(udev);
101 if (udev_device == NULL)
104 udevice = udev_device_init(NULL);
105 if (udevice == NULL) {
110 /* resolve possible symlink to real path */
111 strlcpy(path, devpath, sizeof(path));
112 sysfs_resolve_link(path, sizeof(path));
113 device_set_devpath(udev_device, devpath);
114 log_info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
116 err = udev_db_get_device(udevice, path);
118 log_info(udev, "device %p filled with udev database data\n", udev_device);
120 if (udevice->name[0] != '\0')
121 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
123 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
124 char name[PATH_SIZE];
126 strlcpy(name, udev_get_dev_path(udev), sizeof(name));
127 strlcat(name, "/", sizeof(name));
128 strlcat(name, name_loop->name, sizeof(name));
129 name_list_add(&udev_device->link_list, name, 0);
132 list_for_each_entry(name_loop, &udevice->env_list, node)
133 name_list_add(&udev_device->env_list, name_loop->name, 0);
135 udev_device_cleanup(udevice);
140 * udev_device_get_udev:
141 * @udev_device: udev device
143 * Retrieve the udev library context the device was created with.
145 * Returns: the udev library context
147 struct udev *udev_device_get_udev(struct udev_device *udev_device)
149 if (udev_device == NULL)
151 return udev_device->udev;
156 * @udev_device: udev device
158 * Take a reference of a udev device.
160 * Returns: the passed udev device
162 struct udev_device *udev_device_ref(struct udev_device *udev_device)
164 if (udev_device == NULL)
166 udev_device->refcount++;
172 * @udev_device: udev device
174 * Drop a reference of a udev device. If the refcount reaches zero,
175 * the ressources of the device will be released.
178 void udev_device_unref(struct udev_device *udev_device)
180 if (udev_device == NULL)
182 udev_device->refcount--;
183 if (udev_device->refcount > 0)
185 free(udev_device->syspath);
186 free(udev_device->devname);
187 free(udev_device->subsystem);
188 name_list_cleanup(&udev_device->link_list);
189 name_list_cleanup(&udev_device->env_list);
190 log_info(udev_device->udev, "udev_device: %p released\n", udev_device);
195 * udev_device_get_devpath:
196 * @udev_device: udev device
198 * Retrieve the kernel devpath value of the udev device. The path
199 * does not contain the sys mount point, and starts with a '/'.
201 * Returns: the devpath of the udev device
203 const char *udev_device_get_devpath(struct udev_device *udev_device)
205 if (udev_device == NULL)
207 return udev_device->devpath;
211 * udev_device_get_syspath:
212 * @udev_device: udev device
214 * Retrieve the sys path of the udev device. The path is an
215 * absolute path and starts with the sys mount point.
217 * Returns: the sys path of the udev device
219 const char *udev_device_get_syspath(struct udev_device *udev_device)
221 if (udev_device == NULL)
223 return udev_device->syspath;
227 * udev_device_get_devname:
228 * @udev_device: udev device
230 * Retrieve the device node file name belonging to the udev device.
231 * The path is an absolute path, and starts with the device directory.
233 * Returns: the device node file name of the udev device, or #NULL if no device node exists
235 const char *udev_device_get_devname(struct udev_device *udev_device)
237 if (udev_device == NULL)
239 return udev_device->devname;
243 * udev_device_get_subsystem:
244 * @udev_device: udev device
246 * Retrieve the subsystem string of the udev device. The string does not
249 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
251 const char *udev_device_get_subsystem(struct udev_device *udev_device)
253 char subsystem[NAME_SIZE];
255 if (udev_device == NULL)
257 if (udev_device->subsystem != NULL)
258 return udev_device->subsystem;
259 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
261 udev_device->subsystem = strdup(subsystem);
262 return udev_device->subsystem;
266 * udev_device_get_devlinks:
267 * @udev_device: udev device
268 * @cb: function to be called for every device link found
269 * @data: data to be passed to the function
271 * Retrieve the device links pointing to the device file of the
272 * udev device. For every device link, the passed function will be
273 * called with the device link string.
274 * The path is an absolute path, and starts with the device directory.
275 * If the function returns 1, remaning device links will be ignored.
277 * Returns: the number of device links passed to the caller, or a negative value on error
279 int udev_device_get_devlinks(struct udev_device *udev_device,
280 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
283 struct name_entry *name_loop;
286 if (udev_device == NULL)
288 list_for_each_entry(name_loop, &udev_device->link_list, node) {
290 if (cb(udev_device, name_loop->name, data) != 0)
297 * udev_device_get_properties:
298 * @udev_device: udev device
299 * @cb: function to be called for every property found
300 * @data: data to be passed to the function
302 * Retrieve the property key/value pairs belonging to the
303 * udev device. For every key/value pair, the passed function will be
304 * called. If the function returns 1, remaning properties will be
307 * Returns: the number of properties passed to the caller, or a negative value on error
309 int udev_device_get_properties(struct udev_device *udev_device,
310 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
313 struct name_entry *name_loop;
316 if (udev_device == NULL)
318 list_for_each_entry(name_loop, &udev_device->env_list, node) {
319 char name[PATH_SIZE];
322 strncpy(name, name_loop->name, PATH_SIZE);
323 name[PATH_SIZE-1] = '\0';
324 val = strchr(name, '=');
330 if (cb(udev_device, name, val, data) != 0)
336 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
338 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
340 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
344 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
346 udev_device->subsystem = strdup(subsystem);
347 if (udev_device->subsystem == NULL)
352 int device_set_devname(struct udev_device *udev_device, const char *devname)
354 udev_device->devname = strdup(devname);
355 if (udev_device->devname == NULL)
360 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
362 if (name_list_add(&udev_device->link_list, devlink, 0) == NULL)
367 int device_add_property(struct udev_device *udev_device, const char *property)
369 if (name_list_add(&udev_device->env_list, property, 0) == NULL)