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