chiark / gitweb /
libudev: monitor - add event properties to udev_device
[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         info(udev_device->udev, "udev_device: %p released\n", udev_device);
198         free(udev_device);
199 }
200
201 /**
202  * udev_device_get_devpath:
203  * @udev_device: udev device
204  *
205  * Retrieve the kernel devpath value of the udev device. The path
206  * does not contain the sys mount point, and starts with a '/'.
207  *
208  * Returns: the devpath of the udev device
209  **/
210 const char *udev_device_get_devpath(struct udev_device *udev_device)
211 {
212         if (udev_device == NULL)
213                 return NULL;
214         return udev_device->devpath;
215 }
216
217 /**
218  * udev_device_get_syspath:
219  * @udev_device: udev device
220  *
221  * Retrieve the sys path of the udev device. The path is an
222  * absolute path and starts with the sys mount point.
223  *
224  * Returns: the sys path of the udev device
225  **/
226 const char *udev_device_get_syspath(struct udev_device *udev_device)
227 {
228         if (udev_device == NULL)
229                 return NULL;
230         return udev_device->syspath;
231 }
232
233 /**
234  * udev_device_get_devname:
235  * @udev_device: udev device
236  *
237  * Retrieve the device node file name belonging to the udev device.
238  * The path is an absolute path, and starts with the device directory.
239  *
240  * Returns: the device node file name of the udev device, or #NULL if no device node exists
241  **/
242 const char *udev_device_get_devname(struct udev_device *udev_device)
243 {
244         if (udev_device == NULL)
245                 return NULL;
246         return udev_device->devname;
247 }
248
249 /**
250  * udev_device_get_subsystem:
251  * @udev_device: udev device
252  *
253  * Retrieve the subsystem string of the udev device. The string does not
254  * contain any "/".
255  *
256  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
257  **/
258 const char *udev_device_get_subsystem(struct udev_device *udev_device)
259 {
260         char subsystem[NAME_SIZE];
261
262         if (udev_device == NULL)
263                 return NULL;
264         if (udev_device->subsystem != NULL)
265                 return udev_device->subsystem;
266         if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
267                 return NULL;
268         udev_device->subsystem = strdup(subsystem);
269         return udev_device->subsystem;
270 }
271
272 /**
273  * udev_device_get_devlinks:
274  * @udev_device: udev device
275  * @cb: function to be called for every device link found
276  * @data: data to be passed to the function
277  *
278  * Retrieve the device links pointing to the device file of the
279  * udev device. For every device link, the passed function will be
280  * called with the device link string.
281  * The path is an absolute path, and starts with the device directory.
282  * If the function returns 1, remaning device links will be ignored.
283  *
284  * Returns: the number of device links passed to the caller, or a negative value on error
285  **/
286 int udev_device_get_devlinks(struct udev_device *udev_device,
287                               int (*cb)(struct udev_device *udev_device, const char *value, void *data),
288                               void *data)
289 {
290         struct name_entry *name_loop;
291         int count = 0;
292
293         if (udev_device == NULL)
294                 return -1;
295         list_for_each_entry(name_loop, &udev_device->link_list, node) {
296                 count++;
297                 if (cb(udev_device, name_loop->name, data) != 0)
298                         break;
299         }
300         return count;
301 }
302
303 /**
304  * udev_device_get_properties:
305  * @udev_device: udev device
306  * @cb: function to be called for every property found
307  * @data: data to be passed to the function
308  *
309  * Retrieve the property key/value pairs belonging to the
310  * udev device. For every key/value pair, the passed function will be
311  * called. If the function returns 1, remaning properties will be
312  * ignored.
313  *
314  * Returns: the number of properties passed to the caller, or a negative value on error
315  **/
316 int udev_device_get_properties(struct udev_device *udev_device,
317                                 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
318                                 void *data)
319 {
320         struct name_entry *name_loop;
321         int count = 0;
322
323         if (udev_device == NULL)
324                 return -1;
325         list_for_each_entry(name_loop, &udev_device->env_list, node) {
326                 char name[PATH_SIZE];
327                 char *val;
328
329                 strncpy(name, name_loop->name, PATH_SIZE);
330                 name[PATH_SIZE-1] = '\0';
331                 val = strchr(name, '=');
332                 if (val == NULL)
333                         continue;
334                 val[0] = '\0';
335                 val = &val[1];
336                 count++;
337                 if (cb(udev_device, name, val, data) != 0)
338                         break;
339         }
340         return count;
341 }
342
343 const char *udev_device_get_driver(struct udev_device *udev_device)
344 {
345         if (udev_device == NULL)
346                 return NULL;
347         return udev_device->driver;
348 }
349
350 dev_t udev_device_get_devnum(struct udev_device *udev_device)
351 {
352         if (udev_device == NULL)
353                 return makedev(0, 0);
354         return udev_device->devnum;
355 }
356
357 const char *udev_device_get_action(struct udev_device *udev_device)
358 {
359         if (udev_device == NULL)
360                 return NULL;
361         return udev_device->action;
362 }
363
364 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
365 {
366         if (udev_device == NULL)
367                 return 0;
368         return udev_device->seqnum;
369 }
370
371 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
372 {
373         if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
374                 return -ENOMEM;
375         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
376         return 0;
377 }
378
379 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
380 {
381         udev_device->subsystem = strdup(subsystem);
382         if (udev_device->subsystem == NULL)
383                 return -1;
384         return 0;
385 }
386
387 int device_set_devname(struct udev_device *udev_device, const char *devname)
388 {
389         udev_device->devname = strdup(devname);
390         if (udev_device->devname == NULL)
391                 return -ENOMEM;
392         return 0;
393 }
394
395 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
396 {
397         if (name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
398                 return -ENOMEM;
399         return 0;
400 }
401
402 int device_add_property(struct udev_device *udev_device, const char *property)
403 {
404         if (name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
405                 return -ENOMEM;
406         return 0;
407 }
408
409 int device_set_action(struct udev_device *udev_device, const char *action)
410 {
411         udev_device->action = strdup(action);
412         if (udev_device->action == NULL)
413                 return -ENOMEM;
414         return 0;
415 }
416
417 int device_set_driver(struct udev_device *udev_device, const char *driver)
418 {
419         udev_device->driver = strdup(driver);
420         if (udev_device->driver == NULL)
421                 return -ENOMEM;
422         return 0;
423 }
424
425 const char *device_get_devpath_old(struct udev_device *udev_device)
426 {
427         if (udev_device == NULL)
428                 return NULL;
429         return udev_device->devpath_old;
430 }
431
432 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
433 {
434         udev_device->devpath_old = strdup(devpath_old);
435         if (udev_device->devpath_old == NULL)
436                 return -ENOMEM;
437         return 0;
438 }
439
440 const char *device_get_physdevpath(struct udev_device *udev_device)
441 {
442         if (udev_device == NULL)
443                 return NULL;
444         return udev_device->physdevpath;
445 }
446
447 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
448 {
449         udev_device->physdevpath = strdup(physdevpath);
450         if (udev_device->physdevpath == NULL)
451                 return -ENOMEM;
452         return 0;
453 }
454
455 int device_get_timeout(struct udev_device *udev_device)
456 {
457         if (udev_device == NULL)
458                 return -1;
459         return udev_device->timeout;
460 }
461
462 int device_set_timeout(struct udev_device *udev_device, int timeout)
463 {
464         udev_device->timeout = timeout;
465         return 0;
466 }
467
468 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
469 {
470         udev_device->seqnum = seqnum;
471         return 0;
472 }
473
474 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
475 {
476         udev_device->devnum = devnum;
477         return 0;
478 }