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