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