chiark / gitweb /
volume_id: more fat debugging
[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 <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29
30 #include "libudev.h"
31 #include "libudev-private.h"
32
33 struct udev_device {
34         int refcount;
35         struct udev *udev;
36         struct udev_device *parent_device;
37         char *syspath;
38         const char *devpath;
39         const char *sysname;
40         char *devnode;
41         char *subsystem;
42         struct list_node devlink_list;
43         struct list_node properties_list;
44         char *action;
45         int event_timeout;
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         struct list_node attr_list;
56         int info_loaded;
57 };
58
59 static size_t syspath_to_db_path(struct udev_device *udev_device, char *filename, size_t len)
60 {
61         size_t start;
62
63         /* translate to location of db file */
64         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), len);
65         start = util_strlcat(filename, "/.udev/db/", len);
66         util_strlcat(filename, udev_device->devpath, len);
67         return util_path_encode(&filename[start], len - start);
68 }
69
70 static int device_read_db(struct udev_device *udev_device)
71 {
72         struct stat stats;
73         char filename[UTIL_PATH_SIZE];
74         char line[UTIL_LINE_SIZE];
75         FILE *f;
76
77         syspath_to_db_path(udev_device, filename, sizeof(filename));
78
79         if (lstat(filename, &stats) != 0) {
80                 info(udev_device->udev, "no db file to read %s: %m\n", filename);
81                 return -1;
82         }
83         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
84                 char target[UTIL_PATH_SIZE];
85                 int target_len;
86                 char *next;
87
88                 target_len = readlink(filename, target, sizeof(target));
89                 if (target_len > 0)
90                         target[target_len] = '\0';
91                 else {
92                         info(udev_device->udev, "error reading db link %s: %m\n", filename);
93                         return -1;
94                 }
95
96                 next = strchr(target, ' ');
97                 if (next != NULL) {
98                         next[0] = '\0';
99                         next = &next[1];
100                 }
101                 if (asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
102                         return -ENOMEM;
103                 while (next != NULL) {
104                         char linkname[UTIL_PATH_SIZE];
105                         const char *lnk;
106
107                         lnk = next;
108                         next = strchr(next, ' ');
109                         if (next != NULL) {
110                                 next[0] = '\0';
111                                 next = &next[1];
112                         }
113                         util_strlcpy(linkname, udev_get_dev_path(udev_device->udev), sizeof(linkname));
114                         util_strlcat(linkname, "/", sizeof(linkname));
115                         util_strlcat(linkname, next, sizeof(linkname));
116                         device_add_devlink(udev_device, linkname);
117                 }
118                 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
119                 return 0;
120         }
121
122         f = fopen(filename, "r");
123         if (f == NULL) {
124                 info(udev_device->udev, "error reading db file %s: %m\n", filename);
125                 return -1;
126         }
127         while (fgets(line, sizeof(line), f)) {
128                 ssize_t len;
129                 const char *val;
130
131                 len = strlen(line);
132                 if (len < 4)
133                         break;
134                 line[len-1] = '\0';
135                 val = &line[2];
136
137                 switch(line[0]) {
138                 case 'N':
139                         asprintf(&udev_device->devnode, "%s/%s", udev_get_dev_path(udev_device->udev), val);
140                         break;
141                 case 'S':
142                         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
143                         util_strlcat(filename, "/", sizeof(filename));
144                         util_strlcat(filename, val, sizeof(filename));
145                         device_add_devlink(udev_device, filename);
146                         break;
147                 case 'L':
148                         device_set_devlink_priority(udev_device, atoi(val));
149                         break;
150                 case 'T':
151                         device_set_event_timeout(udev_device, atoi(val));
152                         break;
153                 case 'A':
154                         device_set_num_fake_partitions(udev_device, atoi(val));
155                         break;
156                 case 'R':
157                         device_set_ignore_remove(udev_device, atoi(val));
158                         break;
159                 case 'E':
160                         device_add_property_from_string(udev_device, val);
161                         break;
162                 }
163         }
164         fclose(f);
165
166         info(udev_device->udev, "device %p filled with db file data\n", udev_device);
167         return 0;
168 }
169
170 static int device_read_uevent_file(struct udev_device *udev_device)
171 {
172         char filename[UTIL_PATH_SIZE];
173         FILE *f;
174         char line[UTIL_LINE_SIZE];
175         int maj = 0;
176         int min = 0;
177
178         util_strlcpy(filename, udev_device->syspath, sizeof(filename));
179         util_strlcat(filename, "/uevent", sizeof(filename));
180         f = fopen(filename, "r");
181         if (f == NULL)
182                 return -1;
183
184         while (fgets(line, sizeof(line), f)) {
185                 char *pos;
186
187                 pos = strchr(line, '\n');
188                 if (pos == NULL)
189                         continue;
190                 pos[0] = '\0';
191
192                 if (strncmp(line, "MAJOR=", 6) == 0)
193                         maj = strtoull(&line[6], NULL, 10);
194                 else if (strncmp(line, "MINOR=", 6) == 0)
195                         min = strtoull(&line[6], NULL, 10);
196
197                 device_add_property_from_string(udev_device, line);
198         }
199
200         udev_device->devnum = makedev(maj, min);
201
202         fclose(f);
203         return 0;
204 }
205
206 static void device_load_info(struct udev_device *device)
207 {
208         device_read_uevent_file(device);
209         device_read_db(device);
210         device->info_loaded = 1;
211 }
212
213 void device_set_info_loaded(struct udev_device *device)
214 {
215         device->info_loaded = 1;
216 }
217
218 struct udev_device *device_new(struct udev *udev)
219 {
220         struct udev_device *udev_device;
221
222         if (udev == NULL)
223                 return NULL;
224
225         udev_device = malloc(sizeof(struct udev_device));
226         if (udev_device == NULL)
227                 return NULL;
228         memset(udev_device, 0x00, sizeof(struct udev_device));
229         udev_device->refcount = 1;
230         udev_device->udev = udev;
231         list_init(&udev_device->devlink_list);
232         list_init(&udev_device->properties_list);
233         list_init(&udev_device->attr_list);
234         info(udev_device->udev, "udev_device: %p created\n", udev_device);
235         return udev_device;
236 }
237
238 /**
239  * udev_device_new_from_syspath:
240  * @udev: udev library context
241  * @syspath: sys device path including sys directory
242  *
243  * Create new udev device, and fill in information from the sys
244  * device and the udev database entry. The sypath is the absolute
245  * path to the device, including the sys mount point.
246  *
247  * The initial refcount is 1, and needs to be decremented to
248  * release the ressources of the udev device.
249  *
250  * Returns: a new udev device, or #NULL, if it does not exist
251  **/
252 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
253 {
254         size_t len;
255         const char *subdir;
256         char path[UTIL_PATH_SIZE];
257         char *pos;
258         struct stat statbuf;
259         struct udev_device *udev_device;
260
261         if (udev == NULL)
262                 return NULL;
263         if (syspath == NULL)
264                 return NULL;
265
266         /* path starts in sys */
267         len = strlen(udev_get_sys_path(udev));
268         if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
269                 info(udev, "not in sys :%s\n", syspath);
270                 return NULL;
271         }
272
273         /* path is not a root directory */
274         subdir = &syspath[len+1];
275         pos = strrchr(subdir, '/');
276         if (pos == NULL || pos < &subdir[2]) {
277                 info(udev, "not a subdir :%s\n", syspath);
278                 return NULL;
279         }
280
281         /* resolve possible symlink to real path */
282         util_strlcpy(path, syspath, sizeof(path));
283         util_resolve_sys_link(udev, path, sizeof(path));
284
285         /* try to resolve the silly block layout if needed */
286         if (strncmp(&path[len], "/block/", 7) == 0) {
287                 char block[UTIL_PATH_SIZE];
288                 char part[UTIL_PATH_SIZE];
289
290                 util_strlcpy(block, path, sizeof(block));
291                 pos = strrchr(block, '/');
292                 if (pos == NULL)
293                         return NULL;
294                 util_strlcpy(part, pos, sizeof(part));
295                 pos[0] = '\0';
296                 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
297                         util_strlcpy(path, block, sizeof(path));
298                         util_strlcat(path, part, sizeof(path));
299                 }
300         }
301
302         /* path exists in sys */
303         if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
304             strncmp(&syspath[len], "/class/", 7) == 0 ||
305             strncmp(&syspath[len], "/block/", 7) == 0) {
306                 char file[UTIL_PATH_SIZE];
307
308                 /* all "devices" require a "uevent" file */
309                 util_strlcpy(file, path, sizeof(file));
310                 util_strlcat(file, "/uevent", sizeof(file));
311                 if (stat(file, &statbuf) != 0) {
312                         info(udev, "not a device: %s\n", syspath);
313                         return NULL;
314                 }
315         } else {
316                 /* everything else just needs to be a directory */
317                 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
318                         info(udev, "directory not found: %s\n", syspath);
319                         return NULL;
320                 }
321         }
322
323         udev_device = device_new(udev);
324         if (udev_device == NULL)
325                 return NULL;
326
327         device_set_syspath(udev_device, path);
328         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
329
330         return udev_device;
331 }
332
333 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
334 {
335         char path[UTIL_PATH_SIZE];
336         const char *type_str;
337         struct udev_enumerate *udev_enumerate;
338         struct udev_list_entry *list_entry;
339         struct udev_device *device = NULL;
340
341         if (type == 'b')
342                 type_str = "block";
343         else if (type == 'c')
344                 type_str = "char";
345         else
346                 return NULL;
347
348         /* /sys/dev/{block,char}/<maj>:<min> link */
349         snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
350                  type_str, major(devnum), minor(devnum));
351         if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
352                 return udev_device_new_from_syspath(udev, path);
353
354         udev_enumerate = udev_enumerate_new(udev);
355         if (udev_enumerate == NULL)
356                 return NULL;
357
358         /* fallback to search sys devices for the major/minor */
359         if (type == 'b')
360                 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
361         else if (type == 'c')
362                 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
363         udev_enumerate_scan_devices(udev_enumerate);
364         udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
365                 struct udev_device *device_loop;
366
367                 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
368                 if (device_loop != NULL) {
369                         if (udev_device_get_devnum(device_loop) == devnum) {
370                                 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
371                                         continue;
372                                 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
373                                         continue;
374                                 device = device_loop;
375                                 break;
376                         }
377                         udev_device_unref(device_loop);
378                 }
379         }
380         udev_enumerate_unref(udev_enumerate);
381         return device;
382 }
383
384 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
385 {
386         struct udev_device *udev_device_parent = NULL;
387         char path[UTIL_PATH_SIZE];
388         const char *subdir;
389
390         /* follow "device" link in deprecated sys layout */
391         if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
392             strncmp(udev_device->devpath, "/block/", 7) == 0) {
393                 util_strlcpy(path, udev_device->syspath, sizeof(path));
394                 util_strlcat(path, "/device", sizeof(path));
395                 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
396                         udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
397                 return udev_device_parent;
398         }
399
400         util_strlcpy(path, udev_device->syspath, sizeof(path));
401         subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
402         while (1) {
403                 char *pos;
404
405                 pos = strrchr(subdir, '/');
406                 if (pos == NULL || pos < &subdir[2])
407                         break;
408                 pos[0] = '\0';
409                 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
410                 if (udev_device_parent != NULL)
411                         return udev_device_parent;
412         }
413         return NULL;
414 }
415
416 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
417 {
418         if (udev_device == NULL)
419                 return NULL;
420
421         if (udev_device->parent_device != NULL) {
422                 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
423                 return udev_device->parent_device;
424         }
425         udev_device->parent_device = device_new_from_parent(udev_device);
426         return udev_device->parent_device;
427 }
428
429 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
430 {
431         struct udev_device *parent;
432
433         parent = udev_device_get_parent(udev_device);
434         while (parent != NULL) {
435                 const char *parent_subsystem;
436
437                 parent_subsystem = udev_device_get_subsystem(parent);
438                 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
439                         break;
440                 parent = udev_device_get_parent(parent);
441         }
442         return parent;
443 }
444
445 /**
446  * udev_device_get_udev:
447  * @udev_device: udev device
448  *
449  * Retrieve the udev library context the device was created with.
450  *
451  * Returns: the udev library context
452  **/
453 struct udev *udev_device_get_udev(struct udev_device *udev_device)
454 {
455         if (udev_device == NULL)
456                 return NULL;
457         return udev_device->udev;
458 }
459
460 /**
461  * udev_device_ref:
462  * @udev_device: udev device
463  *
464  * Take a reference of a udev device.
465  *
466  * Returns: the passed udev device
467  **/
468 struct udev_device *udev_device_ref(struct udev_device *udev_device)
469 {
470         if (udev_device == NULL)
471                 return NULL;
472         udev_device->refcount++;
473         return udev_device;
474 }
475
476 /**
477  * udev_device_unref:
478  * @udev_device: udev device
479  *
480  * Drop a reference of a udev device. If the refcount reaches zero,
481  * the ressources of the device will be released.
482  *
483  **/
484 void udev_device_unref(struct udev_device *udev_device)
485 {
486         if (udev_device == NULL)
487                 return;
488         udev_device->refcount--;
489         if (udev_device->refcount > 0)
490                 return;
491         if (udev_device->parent_device != NULL)
492                 udev_device_unref(udev_device->parent_device);
493         free(udev_device->syspath);
494         free(udev_device->devnode);
495         free(udev_device->subsystem);
496         list_cleanup(udev_device->udev, &udev_device->devlink_list);
497         list_cleanup(udev_device->udev, &udev_device->properties_list);
498         free(udev_device->action);
499         free(udev_device->driver);
500         free(udev_device->devpath_old);
501         free(udev_device->physdevpath);
502         list_cleanup(udev_device->udev, &udev_device->attr_list);
503         info(udev_device->udev, "udev_device: %p released\n", udev_device);
504         free(udev_device);
505 }
506
507 /**
508  * udev_device_get_devpath:
509  * @udev_device: udev device
510  *
511  * Retrieve the kernel devpath value of the udev device. The path
512  * does not contain the sys mount point, and starts with a '/'.
513  *
514  * Returns: the devpath of the udev device
515  **/
516 const char *udev_device_get_devpath(struct udev_device *udev_device)
517 {
518         if (udev_device == NULL)
519                 return NULL;
520         return udev_device->devpath;
521 }
522
523 /**
524  * udev_device_get_syspath:
525  * @udev_device: udev device
526  *
527  * Retrieve the sys path of the udev device. The path is an
528  * absolute path and starts with the sys mount point.
529  *
530  * Returns: the sys path of the udev device
531  **/
532 const char *udev_device_get_syspath(struct udev_device *udev_device)
533 {
534         if (udev_device == NULL)
535                 return NULL;
536         return udev_device->syspath;
537 }
538
539 const char *udev_device_get_sysname(struct udev_device *udev_device)
540 {
541         if (udev_device == NULL)
542                 return NULL;
543         return udev_device->sysname;
544 }
545
546 /**
547  * udev_device_get_devnode:
548  * @udev_device: udev device
549  *
550  * Retrieve the device node file name belonging to the udev device.
551  * The path is an absolute path, and starts with the device directory.
552  *
553  * Returns: the device node file name of the udev device, or #NULL if no device node exists
554  **/
555 const char *udev_device_get_devnode(struct udev_device *udev_device)
556 {
557         if (udev_device == NULL)
558                 return NULL;
559         if (!udev_device->info_loaded)
560                 device_load_info(udev_device);
561         return udev_device->devnode;
562 }
563
564 /**
565  * udev_device_get_subsystem:
566  * @udev_device: udev device
567  *
568  * Retrieve the subsystem string of the udev device. The string does not
569  * contain any "/".
570  *
571  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
572  **/
573 const char *udev_device_get_subsystem(struct udev_device *udev_device)
574 {
575         char subsystem[UTIL_NAME_SIZE];
576
577         if (udev_device == NULL)
578                 return NULL;
579         if (udev_device->subsystem != NULL)
580                 return udev_device->subsystem;
581
582         /* read "subsytem" link */
583         if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
584                 udev_device->subsystem = strdup(subsystem);
585                 return udev_device->subsystem;
586         }
587
588         /* implicit names */
589         if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
590                 udev_device->subsystem = strdup("module");
591                 return udev_device->subsystem;
592         }
593         if (strstr(udev_device->devpath, "/drivers/") != NULL) {
594                 udev_device->subsystem = strdup("drivers");
595                 return udev_device->subsystem;
596         }
597         if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
598             strncmp(udev_device->devpath, "/class/", 7) == 0 ||
599             strncmp(udev_device->devpath, "/bus/", 5) == 0) {
600                 udev_device->subsystem = strdup("subsystem");
601                 return udev_device->subsystem;
602         }
603         return NULL;
604 }
605
606 /**
607  * udev_device_get_devlinks_list_entry:
608  * @udev_device: udev device
609  *
610  * Retrieve the list of device links pointing to the device file of
611  * the udev device. The next list entry can be retrieved with
612  * udev_list_entry_next(), which returns #NULL if no more entries exist.
613  * The devlink path can be retrieved from the list entry by
614  * udev_list_entry_get_name(). The path is an absolute path, and starts with
615  * the device directory.
616  *
617  * Returns: the first entry of the device node link list
618  **/
619 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
620 {
621         if (udev_device == NULL)
622                 return NULL;
623         if (!udev_device->info_loaded)
624                 device_load_info(udev_device);
625         return list_get_entry(&udev_device->devlink_list);
626 }
627
628 /**
629  * udev_device_get_properties_list_entry:
630  * @udev_device: udev device
631  *
632  * Retrieve the list of key/value device properties of the udev
633  * device. The next list entry can be retrieved with udev_list_entry_next(),
634  * which returns #NULL if no more entries exist. The property name
635  * can be retrieved from the list entry by udev_list_get_name(),
636  * the property value by udev_list_get_value().
637  *
638  * Returns: the first entry of the property list
639  **/
640 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
641 {
642         if (udev_device == NULL)
643                 return NULL;
644         if (!udev_device->info_loaded)
645                 device_load_info(udev_device);
646         return list_get_entry(&udev_device->properties_list);
647 }
648
649 const char *udev_device_get_driver(struct udev_device *udev_device)
650 {
651         char driver[UTIL_NAME_SIZE];
652
653         if (udev_device == NULL)
654                 return NULL;
655         if (udev_device->driver != NULL)
656                 return udev_device->driver;
657         if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
658                 return NULL;
659         udev_device->driver = strdup(driver);
660         return udev_device->driver;
661 }
662
663 dev_t udev_device_get_devnum(struct udev_device *udev_device)
664 {
665         if (udev_device == NULL)
666                 return makedev(0, 0);
667         if (!udev_device->info_loaded)
668                 device_load_info(udev_device);
669         return udev_device->devnum;
670 }
671
672 const char *udev_device_get_action(struct udev_device *udev_device)
673 {
674         if (udev_device == NULL)
675                 return NULL;
676         return udev_device->action;
677 }
678
679 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
680 {
681         if (udev_device == NULL)
682                 return 0;
683         return udev_device->seqnum;
684 }
685
686 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
687 {
688         struct udev_list_entry *list_entry;
689         char path[UTIL_PATH_SIZE];
690         char value[UTIL_NAME_SIZE];
691         struct stat statbuf;
692         int fd;
693         ssize_t size;
694         const char *val = NULL;
695
696         /* look for possibly already cached result */
697         udev_list_entry_foreach(list_entry, list_get_entry(&udev_device->attr_list)) {
698                 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
699                         info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list_entry));
700                         return udev_list_entry_get_value(list_entry);
701                 }
702         }
703
704         util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
705         util_strlcat(path, "/", sizeof(path));
706         util_strlcat(path, attr, sizeof(path));
707
708         if (lstat(path, &statbuf) != 0) {
709                 info(udev_device->udev, "stat '%s' failed: %m\n", path);
710                 goto out;
711         }
712
713         if (S_ISLNK(statbuf.st_mode)) {
714                 /* links return the last element of the target path */
715                 char target[UTIL_NAME_SIZE];
716                 int len;
717                 char *pos;
718
719                 len = readlink(path, target, sizeof(target));
720                 if (len > 0) {
721                         target[len] = '\0';
722                         pos = strrchr(target, '/');
723                         if (pos != NULL) {
724                                 pos = &pos[1];
725                                 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
726                                 list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
727                                 val = udev_list_entry_get_value(list_entry);
728                         }
729                 }
730                 goto out;
731         }
732
733         /* skip directories */
734         if (S_ISDIR(statbuf.st_mode))
735                 goto out;
736
737         /* skip non-readable files */
738         if ((statbuf.st_mode & S_IRUSR) == 0)
739                 goto out;
740
741         /* read attribute value */
742         fd = open(path, O_RDONLY);
743         if (fd < 0) {
744                 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
745                 goto out;
746         }
747         size = read(fd, value, sizeof(value));
748         close(fd);
749         if (size < 0)
750                 goto out;
751         if (size == sizeof(value))
752                 goto out;
753
754         /* got a valid value, store it in cache and return it */
755         value[size] = '\0';
756         util_remove_trailing_chars(value, '\n');
757         info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
758         list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
759         val = udev_list_entry_get_value(list_entry);
760 out:
761         return val;
762 }
763 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
764 {
765         const char *pos;
766
767         udev_device->syspath = strdup(syspath);
768         if (udev_device->syspath ==  NULL)
769                 return -ENOMEM;
770         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
771         pos = strrchr(udev_device->syspath, '/');
772         if (pos == NULL)
773                 return -EINVAL;
774         udev_device->sysname = &pos[1];
775         return 0;
776 }
777
778 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
779 {
780         udev_device->subsystem = strdup(subsystem);
781         if (udev_device->subsystem == NULL)
782                 return -1;
783         return 0;
784 }
785
786 int device_set_devnode(struct udev_device *udev_device, const char *devnode)
787 {
788         udev_device->devnode = strdup(devnode);
789         if (udev_device->devnode == NULL)
790                 return -ENOMEM;
791         return 0;
792 }
793
794 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
795 {
796         if (list_entry_add(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 1, 0) == NULL)
797                 return -ENOMEM;
798         return 0;
799 }
800
801 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
802 {
803         if (list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0) == NULL)
804                 return -ENOMEM;
805         return 0;
806 }
807
808 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
809 {
810         char name[UTIL_PATH_SIZE];
811         char *val;
812
813         strncpy(name, property, sizeof(name));
814         val = strchr(name, '=');
815         if (val == NULL)
816                 return -1;
817         val[0] = '\0';
818         val = &val[1];
819         if (val[0] == '\0')
820                 val = NULL;
821         device_add_property(udev_device, name, val);
822         return 0;
823 }
824
825 int device_set_action(struct udev_device *udev_device, const char *action)
826 {
827         udev_device->action = strdup(action);
828         if (udev_device->action == NULL)
829                 return -ENOMEM;
830         return 0;
831 }
832
833 int device_set_driver(struct udev_device *udev_device, const char *driver)
834 {
835         udev_device->driver = strdup(driver);
836         if (udev_device->driver == NULL)
837                 return -ENOMEM;
838         return 0;
839 }
840
841 const char *device_get_devpath_old(struct udev_device *udev_device)
842 {
843         return udev_device->devpath_old;
844 }
845
846 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
847 {
848         udev_device->devpath_old = strdup(devpath_old);
849         if (udev_device->devpath_old == NULL)
850                 return -ENOMEM;
851         return 0;
852 }
853
854 const char *device_get_physdevpath(struct udev_device *udev_device)
855 {
856         return udev_device->physdevpath;
857 }
858
859 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
860 {
861         udev_device->physdevpath = strdup(physdevpath);
862         if (udev_device->physdevpath == NULL)
863                 return -ENOMEM;
864         return 0;
865 }
866
867 int device_get_timeout(struct udev_device *udev_device)
868 {
869         return udev_device->timeout;
870 }
871
872 int device_set_timeout(struct udev_device *udev_device, int timeout)
873 {
874         udev_device->timeout = timeout;
875         return 0;
876 }
877 int device_get_event_timeout(struct udev_device *udev_device)
878 {
879         if (!udev_device->info_loaded)
880                 device_load_info(udev_device);
881         return udev_device->event_timeout;
882 }
883
884 int device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
885 {
886         udev_device->event_timeout = event_timeout;
887         return 0;
888 }
889
890 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
891 {
892         udev_device->seqnum = seqnum;
893         return 0;
894 }
895
896 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
897 {
898         udev_device->devnum = devnum;
899         return 0;
900 }
901
902 int device_get_num_fake_partitions(struct udev_device *udev_device)
903 {
904         if (!udev_device->info_loaded)
905                 device_load_info(udev_device);
906         return udev_device->num_fake_partitions;
907 }
908
909 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
910 {
911         udev_device->num_fake_partitions = num;
912         return 0;
913 }
914
915 int device_get_devlink_priority(struct udev_device *udev_device)
916 {
917         if (!udev_device->info_loaded)
918                 device_load_info(udev_device);
919         return udev_device->devlink_priority;
920 }
921
922 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
923 {
924          udev_device->devlink_priority = prio;
925         return 0;
926 }
927
928 int device_get_ignore_remove(struct udev_device *udev_device)
929 {
930         if (!udev_device->info_loaded)
931                 device_load_info(udev_device);
932         return udev_device->ignore_remove;
933 }
934
935 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
936 {
937         udev_device->ignore_remove = ignore;
938         return 0;
939 }
940