chiark / gitweb /
bec0d69af3edf286baa794eb3ce5b15aecdb7de9
[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         char *action;
45         char *driver;
46         char *devpath_old;
47         char *physdevpath;
48         int timeout;
49         dev_t devnum;
50 };
51
52 struct udev_device *device_init(struct udev *udev)
53 {
54         struct udev_device *udev_device;
55
56         if (udev == NULL)
57                 return NULL;
58
59         udev_device = malloc(sizeof(struct udev_device));
60         if (udev_device == NULL)
61                 return NULL;
62         memset(udev_device, 0x00, sizeof(struct udev_device));
63         udev_device->refcount = 1;
64         udev_device->udev = udev;
65         INIT_LIST_HEAD(&udev_device->link_list);
66         INIT_LIST_HEAD(&udev_device->env_list);
67         info(udev_device->udev, "udev_device: %p created\n", udev_device);
68         return udev_device;
69 }
70
71 /**
72  * udev_device_new_from_devpath:
73  * @udev: udev library context
74  * @devpath: sys device path
75  *
76  * Create new udev device, and fill in information from the sysfs
77  * device and the udev database entry. The devpath must not contain
78  * the sysfs mount path, and must contain a leading '/'.
79  *
80  * The initial refcount is 1, and needs to be decremented to
81  * release the ressources of the udev device.
82  *
83  * Returns: a new udev device, or #NULL, if it does not exist
84  **/
85 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
86 {
87         char path[PATH_SIZE];
88         struct stat statbuf;
89         struct udev_device *udev_device;
90         struct udevice *udevice;
91         struct name_entry *name_loop;
92         int err;
93
94         if (udev == NULL)
95                 return NULL;
96         if (devpath == NULL)
97                 return NULL;
98
99         strlcpy(path, udev_get_sys_path(udev), sizeof(path));
100         strlcat(path, devpath, sizeof(path));
101         if (stat(path, &statbuf) != 0)
102                 return NULL;
103         if (!S_ISDIR(statbuf.st_mode))
104                 return NULL;
105
106         udev_device = device_init(udev);
107         if (udev_device == NULL)
108                 return NULL;
109
110         udevice = udev_device_init(udev);
111         if (udevice == NULL) {
112                 free(udev_device);
113                 return NULL;
114         }
115
116         /* resolve possible symlink to real path */
117         strlcpy(path, devpath, sizeof(path));
118         sysfs_resolve_link(udev, path, sizeof(path));
119         device_set_devpath(udev_device, devpath);
120         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
121
122         err = udev_db_get_device(udevice, path);
123         if (err >= 0)
124                 info(udev, "device %p filled with udev database data\n", udev_device);
125
126         if (udevice->name[0] != '\0')
127                 asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev), udevice->name);
128
129         list_for_each_entry(name_loop, &udevice->symlink_list, node) {
130                 char name[PATH_SIZE];
131
132                 strlcpy(name, udev_get_dev_path(udev), sizeof(name));
133                 strlcat(name, "/", sizeof(name));
134                 strlcat(name, name_loop->name, sizeof(name));
135                 name_list_add(udev, &udev_device->link_list, name, 0);
136         }
137
138         list_for_each_entry(name_loop, &udevice->env_list, node)
139                 name_list_add(udev_device->udev, &udev_device->env_list, name_loop->name, 0);
140
141         udev_device_cleanup(udevice);
142         return udev_device;
143 }
144
145 /**
146  * udev_device_get_udev:
147  * @udev_device: udev device
148  *
149  * Retrieve the udev library context the device was created with.
150  *
151  * Returns: the udev library context
152  **/
153 struct udev *udev_device_get_udev(struct udev_device *udev_device)
154 {
155         if (udev_device == NULL)
156                 return NULL;
157         return udev_device->udev;
158 }
159
160 /**
161  * udev_device_ref:
162  * @udev_device: udev device
163  *
164  * Take a reference of a udev device.
165  *
166  * Returns: the passed udev device
167  **/
168 struct udev_device *udev_device_ref(struct udev_device *udev_device)
169 {
170         if (udev_device == NULL)
171                 return NULL;
172         udev_device->refcount++;
173         return udev_device;
174 }
175
176 /**
177  * udev_device_unref:
178  * @udev_device: udev device
179  *
180  * Drop a reference of a udev device. If the refcount reaches zero,
181  * the ressources of the device will be released.
182  *
183  **/
184 void udev_device_unref(struct udev_device *udev_device)
185 {
186         if (udev_device == NULL)
187                 return;
188         udev_device->refcount--;
189         if (udev_device->refcount > 0)
190                 return;
191         free(udev_device->syspath);
192         free(udev_device->devname);
193         free(udev_device->subsystem);
194         name_list_cleanup(udev_device->udev, &udev_device->link_list);
195         name_list_cleanup(udev_device->udev, &udev_device->env_list);
196         info(udev_device->udev, "udev_device: %p released\n", udev_device);
197         free(udev_device);
198 }
199
200 /**
201  * udev_device_get_devpath:
202  * @udev_device: udev device
203  *
204  * Retrieve the kernel devpath value of the udev device. The path
205  * does not contain the sys mount point, and starts with a '/'.
206  *
207  * Returns: the devpath of the udev device
208  **/
209 const char *udev_device_get_devpath(struct udev_device *udev_device)
210 {
211         if (udev_device == NULL)
212                 return NULL;
213         return udev_device->devpath;
214 }
215
216 /**
217  * udev_device_get_syspath:
218  * @udev_device: udev device
219  *
220  * Retrieve the sys path of the udev device. The path is an
221  * absolute path and starts with the sys mount point.
222  *
223  * Returns: the sys path of the udev device
224  **/
225 const char *udev_device_get_syspath(struct udev_device *udev_device)
226 {
227         if (udev_device == NULL)
228                 return NULL;
229         return udev_device->syspath;
230 }
231
232 /**
233  * udev_device_get_devname:
234  * @udev_device: udev device
235  *
236  * Retrieve the device node file name belonging to the udev device.
237  * The path is an absolute path, and starts with the device directory.
238  *
239  * Returns: the device node file name of the udev device, or #NULL if no device node exists
240  **/
241 const char *udev_device_get_devname(struct udev_device *udev_device)
242 {
243         if (udev_device == NULL)
244                 return NULL;
245         return udev_device->devname;
246 }
247
248 /**
249  * udev_device_get_subsystem:
250  * @udev_device: udev device
251  *
252  * Retrieve the subsystem string of the udev device. The string does not
253  * contain any "/".
254  *
255  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
256  **/
257 const char *udev_device_get_subsystem(struct udev_device *udev_device)
258 {
259         char subsystem[NAME_SIZE];
260
261         if (udev_device == NULL)
262                 return NULL;
263         if (udev_device->subsystem != NULL)
264                 return udev_device->subsystem;
265         if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
266                 return NULL;
267         udev_device->subsystem = strdup(subsystem);
268         return udev_device->subsystem;
269 }
270
271 /**
272  * udev_device_get_devlinks:
273  * @udev_device: udev device
274  * @cb: function to be called for every device link found
275  * @data: data to be passed to the function
276  *
277  * Retrieve the device links pointing to the device file of the
278  * udev device. For every device link, the passed function will be
279  * called with the device link string.
280  * The path is an absolute path, and starts with the device directory.
281  * If the function returns 1, remaning device links will be ignored.
282  *
283  * Returns: the number of device links passed to the caller, or a negative value on error
284  **/
285 int udev_device_get_devlinks(struct udev_device *udev_device,
286                               int (*cb)(struct udev_device *udev_device, const char *value, void *data),
287                               void *data)
288 {
289         struct name_entry *name_loop;
290         int count = 0;
291
292         if (udev_device == NULL)
293                 return -1;
294         list_for_each_entry(name_loop, &udev_device->link_list, node) {
295                 count++;
296                 if (cb(udev_device, name_loop->name, data) != 0)
297                         break;
298         }
299         return count;
300 }
301
302 /**
303  * udev_device_get_properties:
304  * @udev_device: udev device
305  * @cb: function to be called for every property found
306  * @data: data to be passed to the function
307  *
308  * Retrieve the property key/value pairs belonging to the
309  * udev device. For every key/value pair, the passed function will be
310  * called. If the function returns 1, remaning properties will be
311  * ignored.
312  *
313  * Returns: the number of properties passed to the caller, or a negative value on error
314  **/
315 int udev_device_get_properties(struct udev_device *udev_device,
316                                 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
317                                 void *data)
318 {
319         struct name_entry *name_loop;
320         int count = 0;
321
322         if (udev_device == NULL)
323                 return -1;
324         list_for_each_entry(name_loop, &udev_device->env_list, node) {
325                 char name[PATH_SIZE];
326                 char *val;
327
328                 strncpy(name, name_loop->name, PATH_SIZE);
329                 name[PATH_SIZE-1] = '\0';
330                 val = strchr(name, '=');
331                 if (val == NULL)
332                         continue;
333                 val[0] = '\0';
334                 val = &val[1];
335                 count++;
336                 if (cb(udev_device, name, val, data) != 0)
337                         break;
338         }
339         return count;
340 }
341
342 const char *udev_device_get_driver(struct udev_device *udev_device)
343 {
344         if (udev_device == NULL)
345                 return NULL;
346         return udev_device->driver;
347 }
348
349 dev_t udev_device_get_devnum(struct udev_device *udev_device)
350 {
351         if (udev_device == NULL)
352                 return makedev(0, 0);
353         return udev_device->devnum;
354 }
355
356 const char *udev_device_get_action(struct udev_device *udev_device)
357 {
358         if (udev_device == NULL)
359                 return NULL;
360         return udev_device->action;
361 }
362
363 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
364 {
365         if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
366                 return -ENOMEM;
367         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
368         return 0;
369 }
370
371 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
372 {
373         udev_device->subsystem = strdup(subsystem);
374         if (udev_device->subsystem == NULL)
375                 return -1;
376         return 0;
377 }
378
379 int device_set_devname(struct udev_device *udev_device, const char *devname)
380 {
381         udev_device->devname = strdup(devname);
382         if (udev_device->devname == NULL)
383                 return -ENOMEM;
384         return 0;
385 }
386
387 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
388 {
389         if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
390                 return -ENOMEM;
391         return 0;
392 }
393
394 int device_add_property(struct udev_device *udev_device, const char *property)
395 {
396         if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
397                 return -ENOMEM;
398         return 0;
399 }
400
401 int device_set_action(struct udev_device *udev_device, const char *action)
402 {
403         udev_device->action = strdup(action);
404         if (udev_device->action == NULL)
405                 return -ENOMEM;
406         return 0;
407 }
408
409 int device_set_driver(struct udev_device *udev_device, const char *driver)
410 {
411         udev_device->driver = strdup(driver);
412         if (udev_device->driver == NULL)
413                 return -ENOMEM;
414         return 0;
415 }
416
417 const char *device_get_devpath_old(struct udev_device *udev_device)
418 {
419         if (udev_device == NULL)
420                 return NULL;
421         return udev_device->devpath_old;
422 }
423
424 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
425 {
426         udev_device->devpath_old = strdup(devpath_old);
427         if (udev_device->devpath_old == NULL)
428                 return -ENOMEM;
429         return 0;
430 }
431
432 const char *device_get_physdevpath(struct udev_device *udev_device)
433 {
434         if (udev_device == NULL)
435                 return NULL;
436         return udev_device->physdevpath;
437 }
438
439 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
440 {
441         udev_device->physdevpath = strdup(physdevpath);
442         if (udev_device->physdevpath == NULL)
443                 return -ENOMEM;
444         return 0;
445 }
446
447 int device_get_timeout(struct udev_device *udev_device)
448 {
449         if (udev_device == NULL)
450                 return -1;
451         return udev_device->timeout;
452 }
453
454 int device_set_timeout(struct udev_device *udev_device, int timeout)
455 {
456         udev_device->timeout = timeout;
457         return 0;
458 }
459
460 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
461 {
462         udev_device->devnum = devnum;
463         return 0;
464 }