chiark / gitweb /
libudev: switch to "udev_device_get_parent"
[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
34 struct udev_device {
35         int refcount;
36         struct udev *udev;
37         struct udev_device *parent_device;
38         char *syspath;
39         const char *devpath;
40         const char *sysname;
41         char *devname;
42         char *subsystem;
43         struct list_head link_list;
44         struct list_head env_list;
45         char *action;
46         char *driver;
47         char *devpath_old;
48         char *physdevpath;
49         int timeout;
50         dev_t devnum;
51         unsigned long long int seqnum;
52         int num_fake_partitions;
53         int devlink_priority;
54         int ignore_remove;
55 };
56
57 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
58 {
59         size_t start;
60
61         /* translate to location of db file */
62         util_strlcpy(filename, udev_get_dev_path(udev), len);
63         start = util_strlcat(filename, "/.udev/db/", len);
64         util_strlcat(filename, devpath, len);
65         return util_path_encode(&filename[start], len - start);
66 }
67
68 static int device_read_db(struct udev_device *udev_device)
69 {
70         struct stat stats;
71         char filename[UTIL_PATH_SIZE];
72         char line[UTIL_LINE_SIZE];
73         FILE *f;
74         int rc = 0;
75
76         devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
77
78         if (lstat(filename, &stats) != 0) {
79                 info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno));
80                 return -1;
81         }
82         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
83                 char target[UTIL_PATH_SIZE];
84                 int target_len;
85
86                 info(udev_device->udev, "found a symlink as db file\n");
87                 target_len = readlink(filename, target, sizeof(target));
88                 if (target_len > 0)
89                         target[target_len] = '\0';
90                 else {
91                         info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
92                         return -1;
93                 }
94                 dbg(udev_device->udev, "db link points to '%s'\n", target);
95                 if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
96                         return -ENOMEM;
97                 return 0;
98         }
99
100         f = fopen(filename, "r");
101         if (f == NULL) {
102                 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
103                 return -1;
104         }
105         while (fgets(line, sizeof(line), f)) {
106                 ssize_t len;
107                 const char *val;
108                 unsigned int maj, min;
109
110                 len = strlen(line);
111                 if (len < 4)
112                         break;
113                 line[len-1] = '\0';
114                 val = &line[2];
115
116                 switch(line[0]) {
117                 case 'N':
118                         asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
119                         break;
120                 case 'M':
121                         sscanf(val, "%u:%u", &maj, &min);
122                         device_set_devnum(udev_device, makedev(maj, min));
123                         break;
124                 case 'S':
125                         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
126                         util_strlcat(filename, "/", sizeof(filename));
127                         util_strlcat(filename, val, sizeof(filename));
128                         device_add_devlink(udev_device, filename);
129                         break;
130                 case 'L':
131                         device_set_devlink_priority(udev_device, atoi(val));
132                         break;
133                 case 'T':
134                         device_set_timeout(udev_device,  atoi(val));
135                         break;
136                 case 'A':
137                         device_set_num_fake_partitions(udev_device, atoi(val));
138                         break;
139                 case 'R':
140                         device_set_ignore_remove(udev_device, atoi(val));
141                         break;
142                 case 'E':
143                         device_add_property(udev_device, val);
144                         break;
145                 }
146         }
147         fclose(f);
148
149         return rc;
150 }
151
152 struct udev_device *device_init(struct udev *udev)
153 {
154         struct udev_device *udev_device;
155
156         if (udev == NULL)
157                 return NULL;
158
159         udev_device = malloc(sizeof(struct udev_device));
160         if (udev_device == NULL)
161                 return NULL;
162         memset(udev_device, 0x00, sizeof(struct udev_device));
163         udev_device->refcount = 1;
164         udev_device->udev = udev;
165         INIT_LIST_HEAD(&udev_device->link_list);
166         INIT_LIST_HEAD(&udev_device->env_list);
167         info(udev_device->udev, "udev_device: %p created\n", udev_device);
168         return udev_device;
169 }
170
171 /**
172  * udev_device_new_from_devpath:
173  * @udev: udev library context
174  * @devpath: sys device path
175  *
176  * Create new udev device, and fill in information from the sysfs
177  * device and the udev database entry. The devpath must not contain
178  * the sysfs mount path, and must contain a leading '/'.
179  *
180  * The initial refcount is 1, and needs to be decremented to
181  * release the ressources of the udev device.
182  *
183  * Returns: a new udev device, or #NULL, if it does not exist
184  **/
185 struct udev_device *udev_device_new_from_devpath(struct udev *udev, const char *devpath)
186 {
187         char path[UTIL_PATH_SIZE];
188         struct stat statbuf;
189         struct udev_device *udev_device;
190
191         if (udev == NULL)
192                 return NULL;
193         if (devpath == NULL)
194                 return NULL;
195
196         util_strlcpy(path, udev_get_sys_path(udev), sizeof(path));
197         util_strlcat(path, devpath, sizeof(path));
198         util_strlcat(path, "/uevent", sizeof(path));
199         if (stat(path, &statbuf) != 0) {
200                 info(udev, "not a device :%s\n", devpath);
201                 return NULL;
202         }
203
204         udev_device = device_init(udev);
205         if (udev_device == NULL)
206                 return NULL;
207
208         /* resolve possible symlink to real path */
209         util_strlcpy(path, devpath, sizeof(path));
210         util_resolve_sys_link(udev, path, sizeof(path));
211         device_set_devpath(udev_device, path);
212         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
213
214         if (device_read_db(udev_device) >= 0)
215                 info(udev, "device %p filled with udev database data\n", udev_device);
216         return udev_device;
217 }
218
219 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
220 {
221         struct udev_device *udev_device_parent = NULL;
222         char path[UTIL_PATH_SIZE];
223         char *pos;
224
225         if (udev_device == NULL)
226                 return NULL;
227
228         util_strlcpy(path, udev_device_get_devpath(udev_device), sizeof(path));
229         while (1) {
230                 pos = strrchr(path, '/');
231                 if (pos == path || pos == NULL)
232                         break;
233                 pos[0] = '\0';
234                 udev_device_parent = udev_device_new_from_devpath(udev_device->udev, path);
235                 if (udev_device_parent != NULL)
236                         break;
237         }
238         return udev_device_parent;
239 }
240
241 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
242 {
243         if (udev_device->parent_device == NULL)
244                 udev_device->parent_device = device_new_from_parent(udev_device);
245         return udev_device->parent_device;
246 }
247
248 /**
249  * udev_device_get_udev:
250  * @udev_device: udev device
251  *
252  * Retrieve the udev library context the device was created with.
253  *
254  * Returns: the udev library context
255  **/
256 struct udev *udev_device_get_udev(struct udev_device *udev_device)
257 {
258         if (udev_device == NULL)
259                 return NULL;
260         return udev_device->udev;
261 }
262
263 /**
264  * udev_device_ref:
265  * @udev_device: udev device
266  *
267  * Take a reference of a udev device.
268  *
269  * Returns: the passed udev device
270  **/
271 struct udev_device *udev_device_ref(struct udev_device *udev_device)
272 {
273         if (udev_device == NULL)
274                 return NULL;
275         udev_device->refcount++;
276         return udev_device;
277 }
278
279 /**
280  * udev_device_unref:
281  * @udev_device: udev device
282  *
283  * Drop a reference of a udev device. If the refcount reaches zero,
284  * the ressources of the device will be released.
285  *
286  **/
287 void udev_device_unref(struct udev_device *udev_device)
288 {
289         if (udev_device == NULL)
290                 return;
291         udev_device->refcount--;
292         if (udev_device->refcount > 0)
293                 return;
294         if (udev_device->parent_device != NULL)
295                 udev_device_unref(udev_device->parent_device);
296         free(udev_device->syspath);
297         free(udev_device->devname);
298         free(udev_device->subsystem);
299         util_name_list_cleanup(udev_device->udev, &udev_device->link_list);
300         util_name_list_cleanup(udev_device->udev, &udev_device->env_list);
301         free(udev_device->action);
302         free(udev_device->driver);
303         free(udev_device->devpath_old);
304         free(udev_device->physdevpath);
305         info(udev_device->udev, "udev_device: %p released\n", udev_device);
306         free(udev_device);
307 }
308
309 /**
310  * udev_device_get_devpath:
311  * @udev_device: udev device
312  *
313  * Retrieve the kernel devpath value of the udev device. The path
314  * does not contain the sys mount point, and starts with a '/'.
315  *
316  * Returns: the devpath of the udev device
317  **/
318 const char *udev_device_get_devpath(struct udev_device *udev_device)
319 {
320         if (udev_device == NULL)
321                 return NULL;
322         return udev_device->devpath;
323 }
324
325 /**
326  * udev_device_get_syspath:
327  * @udev_device: udev device
328  *
329  * Retrieve the sys path of the udev device. The path is an
330  * absolute path and starts with the sys mount point.
331  *
332  * Returns: the sys path of the udev device
333  **/
334 const char *udev_device_get_syspath(struct udev_device *udev_device)
335 {
336         if (udev_device == NULL)
337                 return NULL;
338         return udev_device->syspath;
339 }
340
341 const char *udev_device_get_sysname(struct udev_device *udev_device)
342 {
343         if (udev_device == NULL)
344                 return NULL;
345         return udev_device->sysname;
346 }
347
348 /**
349  * udev_device_get_devname:
350  * @udev_device: udev device
351  *
352  * Retrieve the device node file name belonging to the udev device.
353  * The path is an absolute path, and starts with the device directory.
354  *
355  * Returns: the device node file name of the udev device, or #NULL if no device node exists
356  **/
357 const char *udev_device_get_devname(struct udev_device *udev_device)
358 {
359         if (udev_device == NULL)
360                 return NULL;
361         return udev_device->devname;
362 }
363
364 /**
365  * udev_device_get_subsystem:
366  * @udev_device: udev device
367  *
368  * Retrieve the subsystem string of the udev device. The string does not
369  * contain any "/".
370  *
371  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
372  **/
373 const char *udev_device_get_subsystem(struct udev_device *udev_device)
374 {
375         char subsystem[UTIL_NAME_SIZE];
376
377         if (udev_device == NULL)
378                 return NULL;
379         if (udev_device->subsystem != NULL)
380                 return udev_device->subsystem;
381         if (util_get_sys_subsystem(udev_device->udev, udev_device->devpath, subsystem, sizeof(subsystem)) < 2)
382                 return NULL;
383         udev_device->subsystem = strdup(subsystem);
384         return udev_device->subsystem;
385 }
386
387 /**
388  * udev_device_get_devlinks:
389  * @udev_device: udev device
390  * @cb: function to be called for every device link found
391  * @data: data to be passed to the function
392  *
393  * Retrieve the device links pointing to the device file of the
394  * udev device. For every device link, the passed function will be
395  * called with the device link string.
396  * The path is an absolute path, and starts with the device directory.
397  * If the function returns 1, remaning device links will be ignored.
398  *
399  * Returns: the number of device links passed to the caller, or a negative value on error
400  **/
401 int udev_device_get_devlinks(struct udev_device *udev_device,
402                               int (*cb)(struct udev_device *udev_device, const char *value, void *data),
403                               void *data)
404 {
405         struct util_name_entry *name_loop;
406         int count = 0;
407
408         if (udev_device == NULL)
409                 return -1;
410         list_for_each_entry(name_loop, &udev_device->link_list, node) {
411                 count++;
412                 if (cb(udev_device, name_loop->name, data) != 0)
413                         break;
414         }
415         return count;
416 }
417
418 /**
419  * udev_device_get_properties:
420  * @udev_device: udev device
421  * @cb: function to be called for every property found
422  * @data: data to be passed to the function
423  *
424  * Retrieve the property key/value pairs belonging to the
425  * udev device. For every key/value pair, the passed function will be
426  * called. If the function returns 1, remaning properties will be
427  * ignored.
428  *
429  * Returns: the number of properties passed to the caller, or a negative value on error
430  **/
431 int udev_device_get_properties(struct udev_device *udev_device,
432                                 int (*cb)(struct udev_device *udev_device, const char *key, const char *value, void *data),
433                                 void *data)
434 {
435         struct util_name_entry *name_loop;
436         int count = 0;
437
438         if (udev_device == NULL)
439                 return -1;
440         list_for_each_entry(name_loop, &udev_device->env_list, node) {
441                 char name[UTIL_NAME_SIZE];
442                 char *val;
443
444                 strncpy(name, name_loop->name, sizeof(name));
445                 val = strchr(name, '=');
446                 if (val == NULL)
447                         continue;
448                 val[0] = '\0';
449                 val = &val[1];
450                 count++;
451                 if (cb(udev_device, name, val, data) != 0)
452                         break;
453         }
454         return count;
455 }
456
457 const char *udev_device_get_driver(struct udev_device *udev_device)
458 {
459         char driver[UTIL_NAME_SIZE];
460
461         if (udev_device == NULL)
462                 return NULL;
463         if (udev_device->driver != NULL)
464                 return udev_device->driver;
465         if (util_get_sys_driver(udev_device->udev, udev_device->devpath, driver, sizeof(driver)) < 2)
466                 return NULL;
467         udev_device->driver = strdup(driver);
468         return udev_device->driver;
469 }
470
471 dev_t udev_device_get_devnum(struct udev_device *udev_device)
472 {
473         if (udev_device == NULL)
474                 return makedev(0, 0);
475         return udev_device->devnum;
476 }
477
478 const char *udev_device_get_action(struct udev_device *udev_device)
479 {
480         if (udev_device == NULL)
481                 return NULL;
482         return udev_device->action;
483 }
484
485 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
486 {
487         if (udev_device == NULL)
488                 return 0;
489         return udev_device->seqnum;
490 }
491
492 int device_set_devpath(struct udev_device *udev_device, const char *devpath)
493 {
494         if (asprintf(&udev_device->syspath, "%s%s", udev_get_sys_path(udev_device->udev), devpath) < 0)
495                 return -ENOMEM;
496         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
497         udev_device->sysname = strrchr(udev_device->syspath, '/');
498         if (udev_device->sysname != NULL)
499                 udev_device->sysname = &udev_device->sysname[1];
500         return 0;
501 }
502
503 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
504 {
505         udev_device->subsystem = strdup(subsystem);
506         if (udev_device->subsystem == NULL)
507                 return -1;
508         return 0;
509 }
510
511 int device_set_devname(struct udev_device *udev_device, const char *devname)
512 {
513         udev_device->devname = strdup(devname);
514         if (udev_device->devname == NULL)
515                 return -ENOMEM;
516         return 0;
517 }
518
519 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
520 {
521         if (util_name_list_add(udev_device->udev, &udev_device->link_list, devlink, 0) == NULL)
522                 return -ENOMEM;
523         return 0;
524 }
525
526 int device_add_property(struct udev_device *udev_device, const char *property)
527 {
528         if (util_name_list_add(udev_device->udev, &udev_device->env_list, property, 0) == NULL)
529                 return -ENOMEM;
530         return 0;
531 }
532
533 int device_set_action(struct udev_device *udev_device, const char *action)
534 {
535         udev_device->action = strdup(action);
536         if (udev_device->action == NULL)
537                 return -ENOMEM;
538         return 0;
539 }
540
541 int device_set_driver(struct udev_device *udev_device, const char *driver)
542 {
543         udev_device->driver = strdup(driver);
544         if (udev_device->driver == NULL)
545                 return -ENOMEM;
546         return 0;
547 }
548
549 const char *device_get_devpath_old(struct udev_device *udev_device)
550 {
551         if (udev_device == NULL)
552                 return NULL;
553         return udev_device->devpath_old;
554 }
555
556 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
557 {
558         udev_device->devpath_old = strdup(devpath_old);
559         if (udev_device->devpath_old == NULL)
560                 return -ENOMEM;
561         return 0;
562 }
563
564 const char *device_get_physdevpath(struct udev_device *udev_device)
565 {
566         if (udev_device == NULL)
567                 return NULL;
568         return udev_device->physdevpath;
569 }
570
571 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
572 {
573         udev_device->physdevpath = strdup(physdevpath);
574         if (udev_device->physdevpath == NULL)
575                 return -ENOMEM;
576         return 0;
577 }
578
579 int device_get_timeout(struct udev_device *udev_device)
580 {
581         if (udev_device == NULL)
582                 return -1;
583         return udev_device->timeout;
584 }
585
586 int device_set_timeout(struct udev_device *udev_device, int timeout)
587 {
588         udev_device->timeout = timeout;
589         return 0;
590 }
591
592 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
593 {
594         udev_device->seqnum = seqnum;
595         return 0;
596 }
597
598 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
599 {
600         udev_device->devnum = devnum;
601         return 0;
602 }
603
604 int device_get_num_fake_partitions(struct udev_device *udev_device)
605 {
606         if (udev_device == NULL)
607                 return -1;
608         return udev_device->num_fake_partitions;
609 }
610
611 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
612 {
613         udev_device->num_fake_partitions = num;
614         return 0;
615 }
616
617 int device_get_devlink_priority(struct udev_device *udev_device)
618 {
619         if (udev_device == NULL)
620                 return -1;
621         return udev_device->devlink_priority;
622 }
623
624 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
625 {
626          udev_device->devlink_priority = prio;
627         return 0;
628 }
629
630 int device_get_ignore_remove(struct udev_device *udev_device)
631 {
632         if (udev_device == NULL)
633                 return -1;
634         return udev_device->ignore_remove;
635 }
636
637 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
638 {
639         udev_device->ignore_remove = ignore;
640         return 0;
641 }
642