chiark / gitweb /
libudev: device - add device lookup by subsystem:sysname
[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, lnk, 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 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
385 {
386         size_t sys_path_len;
387         char path_full[UTIL_PATH_SIZE];
388         char *path;
389         struct stat statbuf;
390
391         sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
392         path = &path_full[sys_path_len];
393
394         if (strcmp(subsystem, "subsystem") == 0) {
395                 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
396                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
397                 if (stat(path_full, &statbuf) == 0)
398                         goto found;
399
400                 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
401                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
402                 if (stat(path_full, &statbuf) == 0)
403                         goto found;
404
405                 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
406                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
407                 if (stat(path_full, &statbuf) == 0)
408                         goto found;
409                 goto out;
410         }
411
412         if (strcmp(subsystem, "module") == 0) {
413                 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
414                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
415                 if (stat(path_full, &statbuf) == 0)
416                         goto found;
417                 goto out;
418         }
419
420         if (strcmp(subsystem, "drivers") == 0) {
421                 char subsys[UTIL_NAME_SIZE];
422                 char *driver;
423
424                 util_strlcpy(subsys, sysname, sizeof(subsys));
425                 driver = strchr(subsys, ':');
426                 if (driver != NULL) {
427                         driver[0] = '\0';
428                         driver = &driver[1];
429                         util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
430                         util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
431                         util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
432                         util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
433                         if (stat(path_full, &statbuf) == 0)
434                                 goto found;
435
436                         util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
437                         util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
438                         util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
439                         util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
440                         if (stat(path_full, &statbuf) == 0)
441                                 goto found;
442                 }
443                 goto out;
444         }
445
446         util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
447         util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
448         util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
449         util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
450         if (stat(path_full, &statbuf) == 0)
451                 goto found;
452
453         util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
454         util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
455         util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
456         util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
457         if (stat(path_full, &statbuf) == 0)
458                 goto found;
459
460         util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
461         util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
462         util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
463         util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
464         if (stat(path_full, &statbuf) == 0)
465                 goto found;
466 out:
467         return NULL;
468 found:
469         return udev_device_new_from_syspath(udev, path_full);
470 }
471
472 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
473 {
474         struct udev_device *udev_device_parent = NULL;
475         char path[UTIL_PATH_SIZE];
476         const char *subdir;
477
478         /* follow "device" link in deprecated sys layout */
479         if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
480             strncmp(udev_device->devpath, "/block/", 7) == 0) {
481                 util_strlcpy(path, udev_device->syspath, sizeof(path));
482                 util_strlcat(path, "/device", sizeof(path));
483                 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
484                         udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
485                 return udev_device_parent;
486         }
487
488         util_strlcpy(path, udev_device->syspath, sizeof(path));
489         subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
490         while (1) {
491                 char *pos;
492
493                 pos = strrchr(subdir, '/');
494                 if (pos == NULL || pos < &subdir[2])
495                         break;
496                 pos[0] = '\0';
497                 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
498                 if (udev_device_parent != NULL)
499                         return udev_device_parent;
500         }
501         return NULL;
502 }
503
504 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
505 {
506         if (udev_device == NULL)
507                 return NULL;
508
509         if (udev_device->parent_device != NULL) {
510                 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
511                 return udev_device->parent_device;
512         }
513         udev_device->parent_device = device_new_from_parent(udev_device);
514         return udev_device->parent_device;
515 }
516
517 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
518 {
519         struct udev_device *parent;
520
521         parent = udev_device_get_parent(udev_device);
522         while (parent != NULL) {
523                 const char *parent_subsystem;
524
525                 parent_subsystem = udev_device_get_subsystem(parent);
526                 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
527                         break;
528                 parent = udev_device_get_parent(parent);
529         }
530         return parent;
531 }
532
533 /**
534  * udev_device_get_udev:
535  * @udev_device: udev device
536  *
537  * Retrieve the udev library context the device was created with.
538  *
539  * Returns: the udev library context
540  **/
541 struct udev *udev_device_get_udev(struct udev_device *udev_device)
542 {
543         if (udev_device == NULL)
544                 return NULL;
545         return udev_device->udev;
546 }
547
548 /**
549  * udev_device_ref:
550  * @udev_device: udev device
551  *
552  * Take a reference of a udev device.
553  *
554  * Returns: the passed udev device
555  **/
556 struct udev_device *udev_device_ref(struct udev_device *udev_device)
557 {
558         if (udev_device == NULL)
559                 return NULL;
560         udev_device->refcount++;
561         return udev_device;
562 }
563
564 /**
565  * udev_device_unref:
566  * @udev_device: udev device
567  *
568  * Drop a reference of a udev device. If the refcount reaches zero,
569  * the ressources of the device will be released.
570  *
571  **/
572 void udev_device_unref(struct udev_device *udev_device)
573 {
574         if (udev_device == NULL)
575                 return;
576         udev_device->refcount--;
577         if (udev_device->refcount > 0)
578                 return;
579         if (udev_device->parent_device != NULL)
580                 udev_device_unref(udev_device->parent_device);
581         free(udev_device->syspath);
582         free(udev_device->devnode);
583         free(udev_device->subsystem);
584         list_cleanup(udev_device->udev, &udev_device->devlink_list);
585         list_cleanup(udev_device->udev, &udev_device->properties_list);
586         free(udev_device->action);
587         free(udev_device->driver);
588         free(udev_device->devpath_old);
589         free(udev_device->physdevpath);
590         list_cleanup(udev_device->udev, &udev_device->attr_list);
591         info(udev_device->udev, "udev_device: %p released\n", udev_device);
592         free(udev_device);
593 }
594
595 /**
596  * udev_device_get_devpath:
597  * @udev_device: udev device
598  *
599  * Retrieve the kernel devpath value of the udev device. The path
600  * does not contain the sys mount point, and starts with a '/'.
601  *
602  * Returns: the devpath of the udev device
603  **/
604 const char *udev_device_get_devpath(struct udev_device *udev_device)
605 {
606         if (udev_device == NULL)
607                 return NULL;
608         return udev_device->devpath;
609 }
610
611 /**
612  * udev_device_get_syspath:
613  * @udev_device: udev device
614  *
615  * Retrieve the sys path of the udev device. The path is an
616  * absolute path and starts with the sys mount point.
617  *
618  * Returns: the sys path of the udev device
619  **/
620 const char *udev_device_get_syspath(struct udev_device *udev_device)
621 {
622         if (udev_device == NULL)
623                 return NULL;
624         return udev_device->syspath;
625 }
626
627 const char *udev_device_get_sysname(struct udev_device *udev_device)
628 {
629         if (udev_device == NULL)
630                 return NULL;
631         return udev_device->sysname;
632 }
633
634 /**
635  * udev_device_get_devnode:
636  * @udev_device: udev device
637  *
638  * Retrieve the device node file name belonging to the udev device.
639  * The path is an absolute path, and starts with the device directory.
640  *
641  * Returns: the device node file name of the udev device, or #NULL if no device node exists
642  **/
643 const char *udev_device_get_devnode(struct udev_device *udev_device)
644 {
645         if (udev_device == NULL)
646                 return NULL;
647         if (!udev_device->info_loaded)
648                 device_load_info(udev_device);
649         return udev_device->devnode;
650 }
651
652 /**
653  * udev_device_get_subsystem:
654  * @udev_device: udev device
655  *
656  * Retrieve the subsystem string of the udev device. The string does not
657  * contain any "/".
658  *
659  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
660  **/
661 const char *udev_device_get_subsystem(struct udev_device *udev_device)
662 {
663         char subsystem[UTIL_NAME_SIZE];
664
665         if (udev_device == NULL)
666                 return NULL;
667         if (udev_device->subsystem != NULL)
668                 return udev_device->subsystem;
669
670         /* read "subsytem" link */
671         if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
672                 udev_device->subsystem = strdup(subsystem);
673                 return udev_device->subsystem;
674         }
675
676         /* implicit names */
677         if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
678                 udev_device->subsystem = strdup("module");
679                 return udev_device->subsystem;
680         }
681         if (strstr(udev_device->devpath, "/drivers/") != NULL) {
682                 udev_device->subsystem = strdup("drivers");
683                 return udev_device->subsystem;
684         }
685         if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
686             strncmp(udev_device->devpath, "/class/", 7) == 0 ||
687             strncmp(udev_device->devpath, "/bus/", 5) == 0) {
688                 udev_device->subsystem = strdup("subsystem");
689                 return udev_device->subsystem;
690         }
691         return NULL;
692 }
693
694 /**
695  * udev_device_get_devlinks_list_entry:
696  * @udev_device: udev device
697  *
698  * Retrieve the list of device links pointing to the device file of
699  * the udev device. The next list entry can be retrieved with
700  * udev_list_entry_next(), which returns #NULL if no more entries exist.
701  * The devlink path can be retrieved from the list entry by
702  * udev_list_entry_get_name(). The path is an absolute path, and starts with
703  * the device directory.
704  *
705  * Returns: the first entry of the device node link list
706  **/
707 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
708 {
709         if (udev_device == NULL)
710                 return NULL;
711         if (!udev_device->info_loaded)
712                 device_load_info(udev_device);
713         return list_get_entry(&udev_device->devlink_list);
714 }
715
716 /**
717  * udev_device_get_properties_list_entry:
718  * @udev_device: udev device
719  *
720  * Retrieve the list of key/value device properties of the udev
721  * device. The next list entry can be retrieved with udev_list_entry_next(),
722  * which returns #NULL if no more entries exist. The property name
723  * can be retrieved from the list entry by udev_list_get_name(),
724  * the property value by udev_list_get_value().
725  *
726  * Returns: the first entry of the property list
727  **/
728 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
729 {
730         if (udev_device == NULL)
731                 return NULL;
732         if (!udev_device->info_loaded)
733                 device_load_info(udev_device);
734         return list_get_entry(&udev_device->properties_list);
735 }
736
737 const char *udev_device_get_driver(struct udev_device *udev_device)
738 {
739         char driver[UTIL_NAME_SIZE];
740
741         if (udev_device == NULL)
742                 return NULL;
743         if (udev_device->driver != NULL)
744                 return udev_device->driver;
745         if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
746                 return NULL;
747         udev_device->driver = strdup(driver);
748         return udev_device->driver;
749 }
750
751 dev_t udev_device_get_devnum(struct udev_device *udev_device)
752 {
753         if (udev_device == NULL)
754                 return makedev(0, 0);
755         if (!udev_device->info_loaded)
756                 device_load_info(udev_device);
757         return udev_device->devnum;
758 }
759
760 const char *udev_device_get_action(struct udev_device *udev_device)
761 {
762         if (udev_device == NULL)
763                 return NULL;
764         return udev_device->action;
765 }
766
767 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
768 {
769         if (udev_device == NULL)
770                 return 0;
771         return udev_device->seqnum;
772 }
773
774 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
775 {
776         struct udev_list_entry *list_entry;
777         char path[UTIL_PATH_SIZE];
778         char value[UTIL_NAME_SIZE];
779         struct stat statbuf;
780         int fd;
781         ssize_t size;
782         const char *val = NULL;
783
784         /* look for possibly already cached result */
785         udev_list_entry_foreach(list_entry, list_get_entry(&udev_device->attr_list)) {
786                 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
787                         info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list_entry));
788                         return udev_list_entry_get_value(list_entry);
789                 }
790         }
791
792         util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
793         util_strlcat(path, "/", sizeof(path));
794         util_strlcat(path, attr, sizeof(path));
795
796         if (lstat(path, &statbuf) != 0) {
797                 info(udev_device->udev, "stat '%s' failed: %m\n", path);
798                 goto out;
799         }
800
801         if (S_ISLNK(statbuf.st_mode)) {
802                 /* links return the last element of the target path */
803                 char target[UTIL_NAME_SIZE];
804                 int len;
805                 char *pos;
806
807                 len = readlink(path, target, sizeof(target));
808                 if (len > 0) {
809                         target[len] = '\0';
810                         pos = strrchr(target, '/');
811                         if (pos != NULL) {
812                                 pos = &pos[1];
813                                 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
814                                 list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
815                                 val = udev_list_entry_get_value(list_entry);
816                         }
817                 }
818                 goto out;
819         }
820
821         /* skip directories */
822         if (S_ISDIR(statbuf.st_mode))
823                 goto out;
824
825         /* skip non-readable files */
826         if ((statbuf.st_mode & S_IRUSR) == 0)
827                 goto out;
828
829         /* read attribute value */
830         fd = open(path, O_RDONLY);
831         if (fd < 0) {
832                 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
833                 goto out;
834         }
835         size = read(fd, value, sizeof(value));
836         close(fd);
837         if (size < 0)
838                 goto out;
839         if (size == sizeof(value))
840                 goto out;
841
842         /* got a valid value, store it in cache and return it */
843         value[size] = '\0';
844         util_remove_trailing_chars(value, '\n');
845         info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
846         list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
847         val = udev_list_entry_get_value(list_entry);
848 out:
849         return val;
850 }
851 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
852 {
853         const char *pos;
854
855         udev_device->syspath = strdup(syspath);
856         if (udev_device->syspath ==  NULL)
857                 return -ENOMEM;
858         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
859         pos = strrchr(udev_device->syspath, '/');
860         if (pos == NULL)
861                 return -EINVAL;
862         udev_device->sysname = &pos[1];
863         return 0;
864 }
865
866 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
867 {
868         udev_device->subsystem = strdup(subsystem);
869         if (udev_device->subsystem == NULL)
870                 return -1;
871         return 0;
872 }
873
874 int device_set_devnode(struct udev_device *udev_device, const char *devnode)
875 {
876         udev_device->devnode = strdup(devnode);
877         if (udev_device->devnode == NULL)
878                 return -ENOMEM;
879         return 0;
880 }
881
882 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
883 {
884         if (list_entry_add(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 1, 0) == NULL)
885                 return -ENOMEM;
886         return 0;
887 }
888
889 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
890 {
891         if (list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0) == NULL)
892                 return -ENOMEM;
893         return 0;
894 }
895
896 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
897 {
898         char name[UTIL_PATH_SIZE];
899         char *val;
900
901         strncpy(name, property, sizeof(name));
902         val = strchr(name, '=');
903         if (val == NULL)
904                 return -1;
905         val[0] = '\0';
906         val = &val[1];
907         if (val[0] == '\0')
908                 val = NULL;
909         device_add_property(udev_device, name, val);
910         return 0;
911 }
912
913 int device_set_action(struct udev_device *udev_device, const char *action)
914 {
915         udev_device->action = strdup(action);
916         if (udev_device->action == NULL)
917                 return -ENOMEM;
918         return 0;
919 }
920
921 int device_set_driver(struct udev_device *udev_device, const char *driver)
922 {
923         udev_device->driver = strdup(driver);
924         if (udev_device->driver == NULL)
925                 return -ENOMEM;
926         return 0;
927 }
928
929 const char *device_get_devpath_old(struct udev_device *udev_device)
930 {
931         return udev_device->devpath_old;
932 }
933
934 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
935 {
936         udev_device->devpath_old = strdup(devpath_old);
937         if (udev_device->devpath_old == NULL)
938                 return -ENOMEM;
939         return 0;
940 }
941
942 const char *device_get_physdevpath(struct udev_device *udev_device)
943 {
944         return udev_device->physdevpath;
945 }
946
947 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
948 {
949         udev_device->physdevpath = strdup(physdevpath);
950         if (udev_device->physdevpath == NULL)
951                 return -ENOMEM;
952         return 0;
953 }
954
955 int device_get_timeout(struct udev_device *udev_device)
956 {
957         return udev_device->timeout;
958 }
959
960 int device_set_timeout(struct udev_device *udev_device, int timeout)
961 {
962         udev_device->timeout = timeout;
963         return 0;
964 }
965 int device_get_event_timeout(struct udev_device *udev_device)
966 {
967         if (!udev_device->info_loaded)
968                 device_load_info(udev_device);
969         return udev_device->event_timeout;
970 }
971
972 int device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
973 {
974         udev_device->event_timeout = event_timeout;
975         return 0;
976 }
977
978 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
979 {
980         udev_device->seqnum = seqnum;
981         return 0;
982 }
983
984 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
985 {
986         udev_device->devnum = devnum;
987         return 0;
988 }
989
990 int device_get_num_fake_partitions(struct udev_device *udev_device)
991 {
992         if (!udev_device->info_loaded)
993                 device_load_info(udev_device);
994         return udev_device->num_fake_partitions;
995 }
996
997 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
998 {
999         udev_device->num_fake_partitions = num;
1000         return 0;
1001 }
1002
1003 int device_get_devlink_priority(struct udev_device *udev_device)
1004 {
1005         if (!udev_device->info_loaded)
1006                 device_load_info(udev_device);
1007         return udev_device->devlink_priority;
1008 }
1009
1010 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
1011 {
1012          udev_device->devlink_priority = prio;
1013         return 0;
1014 }
1015
1016 int device_get_ignore_remove(struct udev_device *udev_device)
1017 {
1018         if (!udev_device->info_loaded)
1019                 device_load_info(udev_device);
1020         return udev_device->ignore_remove;
1021 }
1022
1023 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1024 {
1025         udev_device->ignore_remove = ignore;
1026         return 0;
1027 }
1028