chiark / gitweb /
43ed2f3dbfd215d6b1923d1b55022a317356bcce
[elogind.git] / udev / lib / libudev-device.c
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
35 struct 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
46 struct udev_device *device_init(struct udev *udev)
47 {
48         struct udev_device *udev_device;
49
50         if (udev == NULL)
51                 return NULL;
52
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;
59         INIT_LIST_HEAD(&udev_device->link_list);
60         INIT_LIST_HEAD(&udev_device->env_list);
61         info(udev_device->udev, "udev_device: %p created\n", udev_device);
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  **/
79 struct 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;
84         struct udevice *udevice;
85         struct name_entry *name_loop;
86         int err;
87
88         if (udev == NULL)
89                 return NULL;
90         if (devpath == NULL)
91                 return NULL;
92
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
104         udevice = udev_device_init(udev);
105         if (udevice == NULL) {
106                 free(udev_device);
107                 return NULL;
108         }
109
110         /* resolve possible symlink to real path */
111         strlcpy(path, devpath, sizeof(path));
112         sysfs_resolve_link(udev, path, sizeof(path));
113         device_set_devpath(udev_device, devpath);
114         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
115
116         err = udev_db_get_device(udevice, path);
117         if (err >= 0)
118                 info(udev, "device %p filled with udev database data\n", udev_device);
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));
129                 name_list_add(udev, &udev_device->link_list, name, 0);
130         }
131
132         list_for_each_entry(name_loop, &udevice->env_list, node)
133                 name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
134
135         udev_device_cleanup(udevice);
136         return udev_device;
137 }
138
139 /**
140  * udev_device_get_udev:
141  * @udev_device: udev device
142  *
143  * Retrieve the udev library context the device was created with.
144  *
145  * Returns: the udev library context
146  **/
147 struct udev *udev_device_get_udev(struct udev_device *udev_device)
148 {
149         if (udev_device == NULL)
150                 return NULL;
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  **/
162 struct udev_device *udev_device_ref(struct udev_device *udev_device)
163 {
164         if (udev_device == NULL)
165                 return NULL;
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  **/
178 void udev_device_unref(struct udev_device *udev_device)
179 {
180         if (udev_device == NULL)
181                 return;
182         udev_device->refcount--;
183         if (udev_device->refcount > 0)
184                 return;
185         free(udev_device->syspath);
186         free(udev_device->devname);
187         free(udev_device->subsystem);
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);
191         free(udev_device);
192 }
193
194 /**
195  * udev_device_get_devpath:
196  * @udev_device: udev device
197  *
198  * Retrieve the kernel devpath value of the udev device. The path
199  * does not contain the sys mount point, and starts with a '/'.
200  *
201  * Returns: the devpath of the udev device
202  **/
203 const char *udev_device_get_devpath(struct udev_device *udev_device)
204 {
205         if (udev_device == NULL)
206                 return NULL;
207         return udev_device->devpath;
208 }
209
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  **/
219 const 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
226 /**
227  * udev_device_get_devname:
228  * @udev_device: udev device
229  *
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.
232  *
233  * Returns: the device node file name of the udev device, or #NULL if no device node exists
234  **/
235 const char *udev_device_get_devname(struct udev_device *udev_device)
236 {
237         if (udev_device == NULL)
238                 return NULL;
239         return udev_device->devname;
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  **/
251 const char *udev_device_get_subsystem(struct udev_device *udev_device)
252 {
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)
260                 return NULL;
261         udev_device->subsystem = strdup(subsystem);
262         return udev_device->subsystem;
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
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.
276  *
277  * Returns: the number of device links passed to the caller, or a negative value on error
278  **/
279 int 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
286         if (udev_device == NULL)
287                 return -1;
288         list_for_each_entry(name_loop, &udev_device->link_list, node) {
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  **/
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),
311                                 void *data)
312 {
313         struct name_entry *name_loop;
314         int count = 0;
315
316         if (udev_device == NULL)
317                 return -1;
318         list_for_each_entry(name_loop, &udev_device->env_list, node) {
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];
329                 count++;
330                 if (cb(udev_device, name, val, data) != 0)
331                         break;
332         }
333         return count;
334 }
335
336 int 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
344 int 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
352 int 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
360 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
361 {
362         if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
363                 return -ENOMEM;
364         return 0;
365 }
366
367 int device_add_property(struct udev_device *udev_device, const char *property)
368 {
369         if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
370                 return -ENOMEM;
371         return 0;
372 }