]> git.ipfire.org Git - thirdparty/systemd.git/blame - udev/lib/libudev-device.c
fix off-by-one in pass_env_to_socket()
[thirdparty/systemd.git] / udev / lib / libudev-device.c
CommitLineData
eb1f0e66
KS
1/*
2 * libudev - interface to udev device information
3 *
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include "config.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <stddef.h>
25#include <unistd.h>
26#include <errno.h>
27#include <string.h>
28#include <dirent.h>
29#include <sys/stat.h>
30
31#include "libudev.h"
32#include "libudev-private.h"
33#include "../udev.h"
34
11d543c1
KS
35struct udev_device {
36 int refcount;
37 struct udev *udev;
38 char *devpath;
39 char *syspath;
40 char *devname;
41 char *subsystem;
42 struct list_head link_list;
43 struct list_head env_list;
44};
45
ba6929f6 46struct udev_device *device_init(struct udev *udev)
eb1f0e66
KS
47{
48 struct udev_device *udev_device;
49
ba6929f6
KS
50 if (udev == NULL)
51 return NULL;
52
eb1f0e66
KS
53 udev_device = malloc(sizeof(struct udev_device));
54 if (udev_device == NULL)
55 return NULL;
56 memset(udev_device, 0x00, sizeof(struct udev_device));
57 udev_device->refcount = 1;
58 udev_device->udev = udev;
ba6929f6
KS
59 INIT_LIST_HEAD(&udev_device->link_list);
60 INIT_LIST_HEAD(&udev_device->env_list);
7d563a17 61 info(udev_device->udev, "udev_device: %p created\n", udev_device);
eb1f0e66
KS
62 return udev_device;
63}
64
65/**
66 * udev_device_new_from_devpath:
67 * @udev: udev library context
68 * @devpath: sys device path
69 *
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 '/'.
73 *
74 * The initial refcount is 1, and needs to be decremented to
75 * release the ressources of the udev device.
76 *
77 * Returns: a new udev device, or #NULL, if it does not exist
78 **/
79struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
80{
81 char path[PATH_SIZE];
82 struct stat statbuf;
83 struct udev_device *udev_device;
ba6929f6
KS
84 struct udevice *udevice;
85 struct name_entry *name_loop;
eb1f0e66
KS
86 int err;
87
ba6929f6
KS
88 if (udev == NULL)
89 return NULL;
90 if (devpath == NULL)
91 return NULL;
92
eb1f0e66
KS
93 strlcpy(path, udev_get_sys_path(udev), sizeof(path));
94 strlcat(path, devpath, sizeof(path));
95 if (stat(path, &statbuf) != 0)
96 return NULL;
97 if (!S_ISDIR(statbuf.st_mode))
98 return NULL;
99
100 udev_device = device_init(udev);
101 if (udev_device == NULL)
102 return NULL;
103
7d563a17 104 udevice = udev_device_init(udev);
ba6929f6 105 if (udevice == NULL) {
eb1f0e66
KS
106 free(udev_device);
107 return NULL;
108 }
eb1f0e66 109
ba6929f6 110 /* resolve possible symlink to real path */
eb1f0e66 111 strlcpy(path, devpath, sizeof(path));
7d563a17 112 sysfs_resolve_link(udev, path, sizeof(path));
11d543c1 113 device_set_devpath(udev_device, devpath);
7d563a17 114 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
eb1f0e66 115
ba6929f6 116 err = udev_db_get_device(udevice, path);
eb1f0e66 117 if (err >= 0)
7d563a17 118 info(udev, "device %p filled with udev database data\n", udev_device);
ba6929f6
KS
119
120 if (udevice->name[0] != '\0')
121 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
122
123 list_for_each_entry(name_loop, &udevice->symlink_list, node) {
124 char name[PATH_SIZE];
125
126 strlcpy(name, udev_get_dev_path(udev), sizeof(name));
127 strlcat(name, "/", sizeof(name));
128 strlcat(name, name_loop->name, sizeof(name));
7d563a17 129 name_list_add(udev, &udev_device->link_list, name, 0);
ba6929f6
KS
130 }
131
132 list_for_each_entry(name_loop, &udevice->env_list, node)
7d563a17 133 name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
ba6929f6
KS
134
135 udev_device_cleanup(udevice);
eb1f0e66
KS
136 return udev_device;
137}
138
139/**
140 * udev_device_get_udev:
7d8787b3 141 * @udev_device: udev device
eb1f0e66
KS
142 *
143 * Retrieve the udev library context the device was created with.
144 *
145 * Returns: the udev library context
146 **/
147struct udev *udev_device_get_udev(struct udev_device *udev_device)
148{
ba6929f6
KS
149 if (udev_device == NULL)
150 return NULL;
eb1f0e66
KS
151 return udev_device->udev;
152}
153
154/**
155 * udev_device_ref:
156 * @udev_device: udev device
157 *
158 * Take a reference of a udev device.
159 *
160 * Returns: the passed udev device
161 **/
162struct udev_device *udev_device_ref(struct udev_device *udev_device)
163{
ba6929f6
KS
164 if (udev_device == NULL)
165 return NULL;
eb1f0e66
KS
166 udev_device->refcount++;
167 return udev_device;
168}
169
170/**
171 * udev_device_unref:
172 * @udev_device: udev device
173 *
174 * Drop a reference of a udev device. If the refcount reaches zero,
175 * the ressources of the device will be released.
176 *
177 **/
178void udev_device_unref(struct udev_device *udev_device)
179{
ba6929f6
KS
180 if (udev_device == NULL)
181 return;
eb1f0e66
KS
182 udev_device->refcount--;
183 if (udev_device->refcount > 0)
184 return;
11d543c1 185 free(udev_device->syspath);
ba6929f6
KS
186 free(udev_device->devname);
187 free(udev_device->subsystem);
7d563a17
KS
188 name_list_cleanup(udev_device->udev, &udev_device->link_list);
189 name_list_cleanup(udev_device->udev, &udev_device->env_list);
190 info(udev_device->udev, "udev_device: %p released\n", udev_device);
eb1f0e66
KS
191 free(udev_device);
192}
193
194/**
195 * udev_device_get_devpath:
196 * @udev_device: udev device
197 *
11d543c1
KS
198 * Retrieve the kernel devpath value of the udev device. The path
199 * does not contain the sys mount point, and starts with a '/'.
eb1f0e66 200 *
11d543c1 201 * Returns: the devpath of the udev device
eb1f0e66
KS
202 **/
203const char *udev_device_get_devpath(struct udev_device *udev_device)
204{
ba6929f6
KS
205 if (udev_device == NULL)
206 return NULL;
207 return udev_device->devpath;
eb1f0e66
KS
208}
209
11d543c1
KS
210/**
211 * udev_device_get_syspath:
212 * @udev_device: udev device
213 *
214 * Retrieve the sys path of the udev device. The path is an
215 * absolute path and starts with the sys mount point.
216 *
217 * Returns: the sys path of the udev device
218 **/
219const char *udev_device_get_syspath(struct udev_device *udev_device)
220{
221 if (udev_device == NULL)
222 return NULL;
223 return udev_device->syspath;
224}
225
eb1f0e66
KS
226/**
227 * udev_device_get_devname:
228 * @udev_device: udev device
229 *
230 * Retrieve the device node file name belonging to the udev device.
ba6929f6 231 * The path is an absolute path, and starts with the device directory.
eb1f0e66
KS
232 *
233 * Returns: the device node file name of the udev device, or #NULL if no device node exists
234 **/
235const char *udev_device_get_devname(struct udev_device *udev_device)
236{
ba6929f6 237 if (udev_device == NULL)
eb1f0e66 238 return NULL;
ba6929f6 239 return udev_device->devname;
eb1f0e66
KS
240}
241
242/**
243 * udev_device_get_subsystem:
244 * @udev_device: udev device
245 *
246 * Retrieve the subsystem string of the udev device. The string does not
247 * contain any "/".
248 *
249 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
250 **/
251const char *udev_device_get_subsystem(struct udev_device *udev_device)
252{
ba6929f6
KS
253 char subsystem[NAME_SIZE];
254
255 if (udev_device == NULL)
256 return 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)
eb1f0e66 260 return NULL;
ba6929f6
KS
261 udev_device->subsystem = strdup(subsystem);
262 return udev_device->subsystem;
eb1f0e66
KS
263}
264
265/**
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
270 *
271 * Retrieve the device links pointing to the device file of the
272 * udev device. For every device link, the passed function will be
ba6929f6
KS
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.
eb1f0e66
KS
276 *
277 * Returns: the number of device links passed to the caller, or a negative value on error
278 **/
279int udev_device_get_devlinks(struct udev_device *udev_device,
280 int (*cb)(struct udev_device *udev_device, const char *value, void *data),
281 void *data)
282{
283 struct name_entry *name_loop;
284 int count = 0;
285
ba6929f6
KS
286 if (udev_device == NULL)
287 return -1;
288 list_for_each_entry(name_loop, &udev_device->link_list, node) {
eb1f0e66
KS
289 count++;
290 if (cb(udev_device, name_loop->name, data) != 0)
291 break;
292 }
293 return count;
294}
295
296/**
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
301 *
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
305 * ignored.
306 *
307 * Returns: the number of properties passed to the caller, or a negative value on error
308 **/
309int 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),
311 void *data)
312{
313 struct name_entry *name_loop;
314 int count = 0;
315
ba6929f6
KS
316 if (udev_device == NULL)
317 return -1;
318 list_for_each_entry(name_loop, &udev_device->env_list, node) {
eb1f0e66
KS
319 char name[PATH_SIZE];
320 char *val;
321
322 strncpy(name, name_loop->name, PATH_SIZE);
323 name[PATH_SIZE-1] = '\0';
324 val = strchr(name, '=');
325 if (val == NULL)
326 continue;
327 val[0] = '\0';
328 val = &val[1];
ba6929f6 329 count++;
eb1f0e66
KS
330 if (cb(udev_device, name, val, data) != 0)
331 break;
332 }
333 return count;
334}
11d543c1
KS
335
336int device_set_devpath(struct udev_device *udev_device, const char *devpath)
337{
338 if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
339 return -ENOMEM;
340 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
341 return 0;
342}
343
344int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
345{
346 udev_device->subsystem = strdup(subsystem);
347 if (udev_device->subsystem == NULL)
348 return -1;
349 return 0;
350}
351
352int device_set_devname(struct udev_device *udev_device, const char *devname)
353{
354 udev_device->devname = strdup(devname);
355 if (udev_device->devname == NULL)
356 return -ENOMEM;
357 return 0;
358}
359
360int device_add_devlink(struct udev_device *udev_device, const char *devlink)
361{
7d563a17 362 if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
11d543c1
KS
363 return -ENOMEM;
364 return 0;
365}
366
367int device_add_property(struct udev_device *udev_device, const char *property)
368{
7d563a17 369 if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
11d543c1
KS
370 return -ENOMEM;
371 return 0;
372}