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:
130 * @udev_device: udev device
132 * Retrieve the udev library context the device was created with.
134 * Returns: the udev library context
136 struct udev *udev_device_get_udev(struct udev_device *udev_device)
138 if (udev_device == NULL)
140 return udev_device->udev;
145 * @udev_device: udev device
147 * Take a reference of a udev device.
149 * Returns: the passed udev device
151 struct udev_device *udev_device_ref(struct udev_device *udev_device)
153 if (udev_device == NULL)
155 udev_device->refcount++;
161 * @udev_device: udev device
163 * Drop a reference of a udev device. If the refcount reaches zero,
164 * the ressources of the device will be released.
167 void udev_device_unref(struct udev_device *udev_device)
169 if (udev_device == NULL)
171 udev_device->refcount--;
172 if (udev_device->refcount > 0)
174 free(udev_device->devpath);
175 free(udev_device->devname);
176 free(udev_device->subsystem);
177 name_list_cleanup(&udev_device->link_list);
178 name_list_cleanup(&udev_device->env_list);
179 log_info(udev_device->udev, "udev_device: %p released\n", udev_device);
184 * udev_device_get_devpath:
185 * @udev_device: udev device
187 * Retrieve the sys path of the udev device. The path does not contain
188 * the sys mount point.
190 * Returns: the sys path of the udev device
192 const char *udev_device_get_devpath(struct udev_device *udev_device)
194 if (udev_device == NULL)
196 return udev_device->devpath;
200 * udev_device_get_devname:
201 * @udev_device: udev device
203 * Retrieve the device node file name belonging to the udev device.
204 * The path is an absolute path, and starts with the device directory.
206 * Returns: the device node file name of the udev device, or #NULL if no device node exists
208 const char *udev_device_get_devname(struct udev_device *udev_device)
210 if (udev_device == NULL)
212 return udev_device->devname;
216 * udev_device_get_subsystem:
217 * @udev_device: udev device
219 * Retrieve the subsystem string of the udev device. The string does not
222 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
224 const char *udev_device_get_subsystem(struct udev_device *udev_device)
226 char subsystem[NAME_SIZE];
228 if (udev_device == NULL)
230 if (udev_device->subsystem != NULL)
231 return udev_device->subsystem;
232 if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
234 udev_device->subsystem = strdup(subsystem);
235 return udev_device->subsystem;
239 * udev_device_get_devlinks:
240 * @udev_device: udev device
241 * @cb: function to be called for every device link found
242 * @data: data to be passed to the function
244 * Retrieve the device links pointing to the device file of the
245 * udev device. For every device link, the passed function will be
246 * called with the device link string.
247 * The path is an absolute path, and starts with the device directory.
248 * If the function returns 1, remaning device links will be ignored.
250 * Returns: the number of device links passed to the caller, or a negative value on error
252 int udev_device_get_devlinks(struct udev_device *udev_device,
253 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
256 struct name_entry *name_loop;
259 if (udev_device == NULL)
261 list_for_each_entry(name_loop, &udev_device->link_list, node) {
263 if (cb(udev_device, name_loop->name, data) != 0)
270 * udev_device_get_properties:
271 * @udev_device: udev device
272 * @cb: function to be called for every property found
273 * @data: data to be passed to the function
275 * Retrieve the property key/value pairs belonging to the
276 * udev device. For every key/value pair, the passed function will be
277 * called. If the function returns 1, remaning properties will be
280 * Returns: the number of properties passed to the caller, or a negative value on error
282 int udev_device_get_properties(struct udev_device *udev_device,
283 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
286 struct name_entry *name_loop;
289 if (udev_device == NULL)
291 list_for_each_entry(name_loop, &udev_device->env_list, node) {
292 char name[PATH_SIZE];
295 strncpy(name, name_loop->name, PATH_SIZE);
296 name[PATH_SIZE-1] = '\0';
297 val = strchr(name, '=');
303 if (cb(udev_device, name, val, data) != 0)