chiark / gitweb /
libudev: add monitor documentation
[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 *device_init(struct udev *udev)
36 {
37         struct udev_device *udev_device;
38
39         if (udev == NULL)
40                 return NULL;
41
42         udev_device = malloc(sizeof(struct udev_device));
43         if (udev_device == NULL)
44                 return 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);
51         return udev_device;
52 }
53
54 /**
55  * udev_device_new_from_devpath:
56  * @udev: udev library context
57  * @devpath: sys device path
58  *
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 '/'.
62  *
63  * The initial refcount is 1, and needs to be decremented to
64  * release the ressources of the udev device.
65  *
66  * Returns: a new udev device, or #NULL, if it does not exist
67  **/
68 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
69 {
70         char path[PATH_SIZE];
71         struct stat statbuf;
72         struct udev_device *udev_device;
73         struct udevice *udevice;
74         struct name_entry *name_loop;
75         int err;
76
77         if (udev == NULL)
78                 return NULL;
79         if (devpath == NULL)
80                 return NULL;
81
82         strlcpy(path, udev_get_sys_path(udev), sizeof(path));
83         strlcat(path, devpath, sizeof(path));
84         if (stat(path, &statbuf) != 0)
85                 return NULL;
86         if (!S_ISDIR(statbuf.st_mode))
87                 return NULL;
88
89         udev_device = device_init(udev);
90         if (udev_device == NULL)
91                 return NULL;
92
93         udevice = udev_device_init(NULL);
94         if (udevice == NULL) {
95                 free(udev_device);
96                 return NULL;
97         }
98
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));
104
105         err = udev_db_get_device(udevice, path);
106         if (err >= 0)
107                 log_info(udev, "device %p filled with udev database data\n", udev_device);
108
109         if (udevice->name[0] != '\0')
110                 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
111
112         list_for_each_entry(name_loop, &udevice->symlink_list, node) {
113                 char name[PATH_SIZE];
114
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);
119         }
120
121         list_for_each_entry(name_loop, &udevice->env_list, node)
122                 name_list_add(&udev_device->env_list, name_loop->name, 0);
123
124         udev_device_cleanup(udevice);
125         return udev_device;
126 }
127
128 /**
129  * udev_device_get_udev:
130  * @udev_device: udev device
131  *
132  * Retrieve the udev library context the device was created with.
133  *
134  * Returns: the udev library context
135  **/
136 struct udev *udev_device_get_udev(struct udev_device *udev_device)
137 {
138         if (udev_device == NULL)
139                 return NULL;
140         return udev_device->udev;
141 }
142
143 /**
144  * udev_device_ref:
145  * @udev_device: udev device
146  *
147  * Take a reference of a udev device.
148  *
149  * Returns: the passed udev device
150  **/
151 struct udev_device *udev_device_ref(struct udev_device *udev_device)
152 {
153         if (udev_device == NULL)
154                 return NULL;
155         udev_device->refcount++;
156         return udev_device;
157 }
158
159 /**
160  * udev_device_unref:
161  * @udev_device: udev device
162  *
163  * Drop a reference of a udev device. If the refcount reaches zero,
164  * the ressources of the device will be released.
165  *
166  **/
167 void udev_device_unref(struct udev_device *udev_device)
168 {
169         if (udev_device == NULL)
170                 return;
171         udev_device->refcount--;
172         if (udev_device->refcount > 0)
173                 return;
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);
180         free(udev_device);
181 }
182
183 /**
184  * udev_device_get_devpath:
185  * @udev_device: udev device
186  *
187  * Retrieve the sys path of the udev device. The path does not contain
188  * the sys mount point.
189  *
190  * Returns: the sys path of the udev device
191  **/
192 const char *udev_device_get_devpath(struct udev_device *udev_device)
193 {
194         if (udev_device == NULL)
195                 return NULL;
196         return udev_device->devpath;
197 }
198
199 /**
200  * udev_device_get_devname:
201  * @udev_device: udev device
202  *
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.
205  *
206  * Returns: the device node file name of the udev device, or #NULL if no device node exists
207  **/
208 const char *udev_device_get_devname(struct udev_device *udev_device)
209 {
210         if (udev_device == NULL)
211                 return NULL;
212         return udev_device->devname;
213 }
214
215 /**
216  * udev_device_get_subsystem:
217  * @udev_device: udev device
218  *
219  * Retrieve the subsystem string of the udev device. The string does not
220  * contain any "/".
221  *
222  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
223  **/
224 const char *udev_device_get_subsystem(struct udev_device *udev_device)
225 {
226         char subsystem[NAME_SIZE];
227
228         if (udev_device == NULL)
229                 return 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)
233                 return NULL;
234         udev_device->subsystem = strdup(subsystem);
235         return udev_device->subsystem;
236 }
237
238 /**
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
243  *
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.
249  *
250  * Returns: the number of device links passed to the caller, or a negative value on error
251  **/
252 int udev_device_get_devlinks(struct udev_device *udev_device,
253                               int (*cb)(struct udev_device *udev_device, const char *value, void *data),
254                               void *data)
255 {
256         struct name_entry *name_loop;
257         int count = 0;
258
259         if (udev_device == NULL)
260                 return -1;
261         list_for_each_entry(name_loop, &udev_device->link_list, node) {
262                 count++;
263                 if (cb(udev_device, name_loop->name, data) != 0)
264                         break;
265         }
266         return count;
267 }
268
269 /**
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
274  *
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
278  * ignored.
279  *
280  * Returns: the number of properties passed to the caller, or a negative value on error
281  **/
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),
284                                 void *data)
285 {
286         struct name_entry *name_loop;
287         int count = 0;
288
289         if (udev_device == NULL)
290                 return -1;
291         list_for_each_entry(name_loop, &udev_device->env_list, node) {
292                 char name[PATH_SIZE];
293                 char *val;
294
295                 strncpy(name, name_loop->name, PATH_SIZE);
296                 name[PATH_SIZE-1] = '\0';
297                 val = strchr(name, '=');
298                 if (val == NULL)
299                         continue;
300                 val[0] = '\0';
301                 val = &val[1];
302                 count++;
303                 if (cb(udev_device, name, val, data) != 0)
304                         break;
305         }
306         return count;
307 }