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"
35 struct udev_device *device_init(struct udev *udev)
37 struct udev_device *udev_device;
42 udev_device = malloc(sizeof(struct udev_device));
43 if (udev_device == NULL)
45 memset(udev_device, 0x00, sizeof(struct udev_device));
46 udev_device->refcount = 1;
47 udev_device->udev = udev;
48 INIT_LIST_HEAD(&udev_device->link_list);
49 INIT_LIST_HEAD(&udev_device->env_list);
50 log_info(udev_device->udev, "udev_device: %p created\n", udev_device);
55 * udev_device_new_from_devpath:
56 * @udev: udev library context
57 * @devpath: sys device path
59 * Create new udev device, and fill in information from the sysfs
60 * device and the udev database entry. The devpath must not contain
61 * the sysfs mount path, and must contain a leading '/'.
63 * The initial refcount is 1, and needs to be decremented to
64 * release the ressources of the udev device.
66 * Returns: a new udev device, or #NULL, if it does not exist
68 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
72 struct udev_device *udev_device;
73 struct udevice *udevice;
74 struct name_entry *name_loop;
82 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
83 strlcat(path, devpath, sizeof(path));
84 if (stat(path, &statbuf) != 0)
86 if (!S_ISDIR(statbuf.st_mode))
89 udev_device = device_init(udev);
90 if (udev_device == NULL)
93 udevice = udev_device_init(NULL);
94 if (udevice == NULL) {
99 /* resolve possible symlink to real path */
100 strlcpy(path, devpath, sizeof(path));
101 sysfs_resolve_link(path, sizeof(path));
102 udev_device->devpath = strdup(path);
103 log_info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
105 err = udev_db_get_device(udevice, path);
107 log_info(udev, "device %p filled with udev database data\n", udev_device);
109 if (udevice->name[0] != '\0')
110 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
112 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
113 char name[PATH_SIZE];
115 strlcpy(name, udev_get_dev_path(udev), sizeof(name));
116 strlcat(name, "/", sizeof(name));
117 strlcat(name, name_loop->name, sizeof(name));
118 name_list_add(&udev_device->link_list, name, 0);
121 list_for_each_entry(name_loop, &udevice->env_list, node)
122 name_list_add(&udev_device->env_list, name_loop->name, 0);
124 udev_device_cleanup(udevice);
129 * udev_device_get_udev:
131 * Retrieve the udev library context the device was created with.
133 * Returns: the udev library context
135 struct udev *udev_device_get_udev(struct udev_device *udev_device)
137 if (udev_device == NULL)
139 return udev_device->udev;
144 * @udev_device: udev device
146 * Take a reference of a udev device.
148 * Returns: the passed udev device
150 struct udev_device *udev_device_ref(struct udev_device *udev_device)
152 if (udev_device == NULL)
154 udev_device->refcount++;
160 * @udev_device: udev device
162 * Drop a reference of a udev device. If the refcount reaches zero,
163 * the ressources of the device will be released.
166 void udev_device_unref(struct udev_device *udev_device)
168 if (udev_device == NULL)
170 udev_device->refcount--;
171 if (udev_device->refcount > 0)
173 free(udev_device->devpath);
174 free(udev_device->devname);
175 free(udev_device->subsystem);
176 name_list_cleanup(&udev_device->link_list);
177 name_list_cleanup(&udev_device->env_list);
178 log_info(udev_device->udev, "udev_device: %p released\n", udev_device);
183 * udev_device_get_devpath:
184 * @udev_device: udev device
186 * Retrieve the sys path of the udev device. The path does not contain
187 * the sys mount point.
189 * Returns: the sys path of the udev device
191 const char *udev_device_get_devpath(struct udev_device *udev_device)
193 if (udev_device == NULL)
195 return udev_device->devpath;
199 * udev_device_get_devname:
200 * @udev_device: udev device
202 * Retrieve the device node file name belonging to the udev device.
203 * The path is an absolute path, and starts with the device directory.
205 * Returns: the device node file name of the udev device, or #NULL if no device node exists
207 const char *udev_device_get_devname(struct udev_device *udev_device)
209 if (udev_device == NULL)
211 return udev_device->devname;
215 * udev_device_get_subsystem:
216 * @udev_device: udev device
218 * Retrieve the subsystem string of the udev device. The string does not
221 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
223 const char *udev_device_get_subsystem(struct udev_device *udev_device)
225 char subsystem[NAME_SIZE];
227 if (udev_device == NULL)
229 if (udev_device->subsystem != NULL)
230 return udev_device->subsystem;
231 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
233 udev_device->subsystem = strdup(subsystem);
234 return udev_device->subsystem;
238 * udev_device_get_devlinks:
239 * @udev_device: udev device
240 * @cb: function to be called for every device link found
241 * @data: data to be passed to the function
243 * Retrieve the device links pointing to the device file of the
244 * udev device. For every device link, the passed function will be
245 * called with the device link string.
246 * The path is an absolute path, and starts with the device directory.
247 * If the function returns 1, remaning device links will be ignored.
249 * Returns: the number of device links passed to the caller, or a negative value on error
251 int udev_device_get_devlinks(struct udev_device *udev_device,
252 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
255 struct name_entry *name_loop;
258 if (udev_device == NULL)
260 list_for_each_entry(name_loop, &udev_device->link_list, node) {
262 if (cb(udev_device, name_loop->name, data) != 0)
269 * udev_device_get_properties:
270 * @udev_device: udev device
271 * @cb: function to be called for every property found
272 * @data: data to be passed to the function
274 * Retrieve the property key/value pairs belonging to the
275 * udev device. For every key/value pair, the passed function will be
276 * called. If the function returns 1, remaning properties will be
279 * Returns: the number of properties passed to the caller, or a negative value on error
281 int udev_device_get_properties(struct udev_device *udev_device,
282 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
285 struct name_entry *name_loop;
288 if (udev_device == NULL)
290 list_for_each_entry(name_loop, &udev_device->env_list, node) {
291 char name[PATH_SIZE];
294 strncpy(name, name_loop->name, PATH_SIZE);
295 name[PATH_SIZE-1] = '\0';
296 val = strchr(name, '=');
302 if (cb(udev_device, name, val, data) != 0)