chiark / gitweb /
libudev: device - lookup "subsystem" and "driver" only once
[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 <ctype.h>
29 #include <sys/stat.h>
30
31 #include "libudev.h"
32 #include "libudev-private.h"
33
34 struct udev_device {
35         int refcount;
36         struct udev *udev;
37         struct udev_device *parent_device;
38         char *syspath;
39         const char *devpath;
40         char *sysname;
41         const char *sysnum;
42         char *devnode;
43         char *subsystem;
44         int subsystem_set;
45         struct udev_list_node devlinks_list;
46         int devlinks_uptodate;
47         struct udev_list_node properties_list;
48         char *envp[128];
49         int envp_uptodate;
50         char *driver;
51         int driver_set;
52         dev_t devnum;
53         char *action;
54         int event_timeout;
55         char *devpath_old;
56         char *physdevpath;
57         int timeout;
58         unsigned long long int seqnum;
59         int num_fake_partitions;
60         int devlink_priority;
61         int ignore_remove;
62         struct udev_list_node attr_list;
63         int info_loaded;
64 };
65
66 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
67 {
68         size_t start;
69
70         /* translate to location of db file */
71         util_strlcpy(filename, udev_get_dev_path(udev), len);
72         start = util_strlcat(filename, "/.udev/db/", len);
73         util_strlcat(filename, devpath, len);
74         return util_path_encode(&filename[start], len - start);
75 }
76
77 static int device_read_db(struct udev_device *udev_device)
78 {
79         struct stat stats;
80         char filename[UTIL_PATH_SIZE];
81         char line[UTIL_LINE_SIZE];
82         FILE *f;
83
84         devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
85
86         if (lstat(filename, &stats) != 0) {
87                 info(udev_device->udev, "no db file to read %s: %m\n", filename);
88                 return -1;
89         }
90         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
91                 char target[UTIL_PATH_SIZE];
92                 char devnode[UTIL_PATH_SIZE];
93                 int target_len;
94                 char *next;
95
96                 target_len = readlink(filename, target, sizeof(target));
97                 if (target_len > 0)
98                         target[target_len] = '\0';
99                 else {
100                         info(udev_device->udev, "error reading db link %s: %m\n", filename);
101                         return -1;
102                 }
103
104                 next = strchr(target, ' ');
105                 if (next != NULL) {
106                         next[0] = '\0';
107                         next = &next[1];
108                 }
109                 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
110                 util_strlcat(devnode, "/", sizeof(devnode));
111                 util_strlcat(devnode, target, sizeof(devnode));
112                 udev_device_set_devnode(udev_device, devnode);
113                 while (next != NULL) {
114                         char devlink[UTIL_PATH_SIZE];
115                         const char *lnk;
116
117                         lnk = next;
118                         next = strchr(next, ' ');
119                         if (next != NULL) {
120                                 next[0] = '\0';
121                                 next = &next[1];
122                         }
123                         util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
124                         util_strlcat(devlink, "/", sizeof(devlink));
125                         util_strlcat(devlink, lnk, sizeof(devlink));
126                         udev_device_add_devlink(udev_device, devlink);
127                 }
128                 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
129                 return 0;
130         }
131
132         f = fopen(filename, "r");
133         if (f == NULL) {
134                 info(udev_device->udev, "error reading db file %s: %m\n", filename);
135                 return -1;
136         }
137         while (fgets(line, sizeof(line), f)) {
138                 ssize_t len;
139                 const char *val;
140
141                 len = strlen(line);
142                 if (len < 4)
143                         break;
144                 line[len-1] = '\0';
145                 val = &line[2];
146                 switch(line[0]) {
147                 case 'N':
148                         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
149                         util_strlcat(filename, "/", sizeof(filename));
150                         util_strlcat(filename, val, sizeof(filename));
151                         udev_device_set_devnode(udev_device, filename);
152                         break;
153                 case 'S':
154                         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
155                         util_strlcat(filename, "/", sizeof(filename));
156                         util_strlcat(filename, val, sizeof(filename));
157                         udev_device_add_devlink(udev_device, filename);
158                         break;
159                 case 'L':
160                         udev_device_set_devlink_priority(udev_device, atoi(val));
161                         break;
162                 case 'T':
163                         udev_device_set_event_timeout(udev_device, atoi(val));
164                         break;
165                 case 'A':
166                         udev_device_set_num_fake_partitions(udev_device, atoi(val));
167                         break;
168                 case 'R':
169                         udev_device_set_ignore_remove(udev_device, atoi(val));
170                         break;
171                 case 'E':
172                         udev_device_add_property_from_string(udev_device, val);
173                         break;
174                 }
175         }
176         fclose(f);
177
178         info(udev_device->udev, "device %p filled with db file data\n", udev_device);
179         return 0;
180 }
181
182 int udev_device_read_uevent_file(struct udev_device *udev_device)
183 {
184         char filename[UTIL_PATH_SIZE];
185         FILE *f;
186         char line[UTIL_LINE_SIZE];
187         int maj = 0;
188         int min = 0;
189
190         util_strlcpy(filename, udev_device->syspath, sizeof(filename));
191         util_strlcat(filename, "/uevent", sizeof(filename));
192         f = fopen(filename, "r");
193         if (f == NULL)
194                 return -1;
195
196         while (fgets(line, sizeof(line), f)) {
197                 char *pos;
198
199                 pos = strchr(line, '\n');
200                 if (pos == NULL)
201                         continue;
202                 pos[0] = '\0';
203
204                 if (strncmp(line, "MAJOR=", 6) == 0)
205                         maj = strtoull(&line[6], NULL, 10);
206                 else if (strncmp(line, "MINOR=", 6) == 0)
207                         min = strtoull(&line[6], NULL, 10);
208
209                 udev_device_add_property_from_string(udev_device, line);
210         }
211
212         udev_device->devnum = makedev(maj, min);
213
214         fclose(f);
215         return 0;
216 }
217
218 static void device_load_info(struct udev_device *device)
219 {
220         device->info_loaded = 1;
221         udev_device_read_uevent_file(device);
222         device_read_db(device);
223 }
224
225 void udev_device_set_info_loaded(struct udev_device *device)
226 {
227         device->info_loaded = 1;
228 }
229
230 struct udev_device *device_new(struct udev *udev)
231 {
232         struct udev_device *udev_device;
233
234         if (udev == NULL)
235                 return NULL;
236
237         udev_device = malloc(sizeof(struct udev_device));
238         if (udev_device == NULL)
239                 return NULL;
240         memset(udev_device, 0x00, sizeof(struct udev_device));
241         udev_device->refcount = 1;
242         udev_device->udev = udev;
243         udev_list_init(&udev_device->devlinks_list);
244         udev_list_init(&udev_device->properties_list);
245         udev_list_init(&udev_device->attr_list);
246         udev_device->event_timeout = -1;
247         info(udev_device->udev, "udev_device: %p created\n", udev_device);
248         return udev_device;
249 }
250
251 /**
252  * udev_device_new_from_syspath:
253  * @udev: udev library context
254  * @syspath: sys device path including sys directory
255  *
256  * Create new udev device, and fill in information from the sys
257  * device and the udev database entry. The sypath is the absolute
258  * path to the device, including the sys mount point.
259  *
260  * The initial refcount is 1, and needs to be decremented to
261  * release the ressources of the udev device.
262  *
263  * Returns: a new udev device, or #NULL, if it does not exist
264  **/
265 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
266 {
267         size_t len;
268         const char *subdir;
269         char path[UTIL_PATH_SIZE];
270         char *pos;
271         struct stat statbuf;
272         struct udev_device *udev_device;
273
274         if (udev == NULL)
275                 return NULL;
276         if (syspath == NULL)
277                 return NULL;
278
279         /* path starts in sys */
280         len = strlen(udev_get_sys_path(udev));
281         if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
282                 info(udev, "not in sys :%s\n", syspath);
283                 return NULL;
284         }
285
286         /* path is not a root directory */
287         subdir = &syspath[len+1];
288         pos = strrchr(subdir, '/');
289         if (pos == NULL || pos < &subdir[2]) {
290                 info(udev, "not a subdir :%s\n", syspath);
291                 return NULL;
292         }
293
294         /* resolve possible symlink to real path */
295         util_strlcpy(path, syspath, sizeof(path));
296         util_resolve_sys_link(udev, path, sizeof(path));
297
298         /* try to resolve the silly block layout if needed */
299         if (strncmp(&path[len], "/block/", 7) == 0) {
300                 char block[UTIL_PATH_SIZE];
301                 char part[UTIL_PATH_SIZE];
302
303                 util_strlcpy(block, path, sizeof(block));
304                 pos = strrchr(block, '/');
305                 if (pos == NULL)
306                         return NULL;
307                 util_strlcpy(part, pos, sizeof(part));
308                 pos[0] = '\0';
309                 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
310                         util_strlcpy(path, block, sizeof(path));
311                         util_strlcat(path, part, sizeof(path));
312                 }
313         }
314
315         /* path exists in sys */
316         if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
317             strncmp(&syspath[len], "/class/", 7) == 0 ||
318             strncmp(&syspath[len], "/block/", 7) == 0) {
319                 char file[UTIL_PATH_SIZE];
320
321                 /* all "devices" require a "uevent" file */
322                 util_strlcpy(file, path, sizeof(file));
323                 util_strlcat(file, "/uevent", sizeof(file));
324                 if (stat(file, &statbuf) != 0) {
325                         info(udev, "not a device: %s\n", syspath);
326                         return NULL;
327                 }
328         } else {
329                 /* everything else just needs to be a directory */
330                 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
331                         info(udev, "directory not found: %s\n", syspath);
332                         return NULL;
333                 }
334         }
335
336         udev_device = device_new(udev);
337         if (udev_device == NULL)
338                 return NULL;
339
340         udev_device_set_syspath(udev_device, path);
341         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
342
343         return udev_device;
344 }
345
346 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
347 {
348         char path[UTIL_PATH_SIZE];
349         const char *type_str;
350         struct udev_enumerate *udev_enumerate;
351         struct udev_list_entry *list_entry;
352         struct udev_device *device = NULL;
353
354         if (type == 'b')
355                 type_str = "block";
356         else if (type == 'c')
357                 type_str = "char";
358         else
359                 return NULL;
360
361         /* /sys/dev/{block,char}/<maj>:<min> link */
362         snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
363                  type_str, major(devnum), minor(devnum));
364         if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
365                 return udev_device_new_from_syspath(udev, path);
366
367         udev_enumerate = udev_enumerate_new(udev);
368         if (udev_enumerate == NULL)
369                 return NULL;
370
371         /* fallback to search sys devices for the major/minor */
372         if (type == 'b')
373                 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
374         else if (type == 'c')
375                 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
376         udev_enumerate_scan_devices(udev_enumerate);
377         udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
378                 struct udev_device *device_loop;
379
380                 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
381                 if (device_loop != NULL) {
382                         if (udev_device_get_devnum(device_loop) == devnum) {
383                                 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
384                                         continue;
385                                 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
386                                         continue;
387                                 device = device_loop;
388                                 break;
389                         }
390                         udev_device_unref(device_loop);
391                 }
392         }
393         udev_enumerate_unref(udev_enumerate);
394         return device;
395 }
396
397 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
398 {
399         size_t sys_path_len;
400         char path_full[UTIL_PATH_SIZE];
401         char *path;
402         struct stat statbuf;
403
404         sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
405         path = &path_full[sys_path_len];
406
407         if (strcmp(subsystem, "subsystem") == 0) {
408                 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
409                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
410                 if (stat(path_full, &statbuf) == 0)
411                         goto found;
412
413                 util_strlcpy(path, "/bus/", 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
418                 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
419                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
420                 if (stat(path_full, &statbuf) == 0)
421                         goto found;
422                 goto out;
423         }
424
425         if (strcmp(subsystem, "module") == 0) {
426                 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
427                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
428                 if (stat(path_full, &statbuf) == 0)
429                         goto found;
430                 goto out;
431         }
432
433         if (strcmp(subsystem, "drivers") == 0) {
434                 char subsys[UTIL_NAME_SIZE];
435                 char *driver;
436
437                 util_strlcpy(subsys, sysname, sizeof(subsys));
438                 driver = strchr(subsys, ':');
439                 if (driver != NULL) {
440                         driver[0] = '\0';
441                         driver = &driver[1];
442                         util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
443                         util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
444                         util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
445                         util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
446                         if (stat(path_full, &statbuf) == 0)
447                                 goto found;
448
449                         util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
450                         util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
451                         util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
452                         util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
453                         if (stat(path_full, &statbuf) == 0)
454                                 goto found;
455                 }
456                 goto out;
457         }
458
459         util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
460         util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
461         util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
462         util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
463         if (stat(path_full, &statbuf) == 0)
464                 goto found;
465
466         util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
467         util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
468         util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
469         util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
470         if (stat(path_full, &statbuf) == 0)
471                 goto found;
472
473         util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
474         util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
475         util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
476         util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
477         if (stat(path_full, &statbuf) == 0)
478                 goto found;
479 out:
480         return NULL;
481 found:
482         return udev_device_new_from_syspath(udev, path_full);
483 }
484
485 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
486 {
487         struct udev_device *udev_device_parent = NULL;
488         char path[UTIL_PATH_SIZE];
489         const char *subdir;
490
491         /* follow "device" link in deprecated sys layout */
492         if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
493             strncmp(udev_device->devpath, "/block/", 7) == 0) {
494                 util_strlcpy(path, udev_device->syspath, sizeof(path));
495                 util_strlcat(path, "/device", sizeof(path));
496                 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
497                         udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
498                 return udev_device_parent;
499         }
500
501         util_strlcpy(path, udev_device->syspath, sizeof(path));
502         subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
503         while (1) {
504                 char *pos;
505
506                 pos = strrchr(subdir, '/');
507                 if (pos == NULL || pos < &subdir[2])
508                         break;
509                 pos[0] = '\0';
510                 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
511                 if (udev_device_parent != NULL)
512                         return udev_device_parent;
513         }
514         return NULL;
515 }
516
517 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
518 {
519         if (udev_device == NULL)
520                 return NULL;
521
522         if (udev_device->parent_device != NULL) {
523                 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
524                 return udev_device->parent_device;
525         }
526         udev_device->parent_device = device_new_from_parent(udev_device);
527         return udev_device->parent_device;
528 }
529
530 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
531 {
532         struct udev_device *parent;
533
534         parent = udev_device_get_parent(udev_device);
535         while (parent != NULL) {
536                 const char *parent_subsystem;
537
538                 parent_subsystem = udev_device_get_subsystem(parent);
539                 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
540                         break;
541                 parent = udev_device_get_parent(parent);
542         }
543         return parent;
544 }
545
546 /**
547  * udev_device_get_udev:
548  * @udev_device: udev device
549  *
550  * Retrieve the udev library context the device was created with.
551  *
552  * Returns: the udev library context
553  **/
554 struct udev *udev_device_get_udev(struct udev_device *udev_device)
555 {
556         if (udev_device == NULL)
557                 return NULL;
558         return udev_device->udev;
559 }
560
561 /**
562  * udev_device_ref:
563  * @udev_device: udev device
564  *
565  * Take a reference of a udev device.
566  *
567  * Returns: the passed udev device
568  **/
569 struct udev_device *udev_device_ref(struct udev_device *udev_device)
570 {
571         if (udev_device == NULL)
572                 return NULL;
573         udev_device->refcount++;
574         return udev_device;
575 }
576
577 /**
578  * udev_device_unref:
579  * @udev_device: udev device
580  *
581  * Drop a reference of a udev device. If the refcount reaches zero,
582  * the ressources of the device will be released.
583  *
584  **/
585 void udev_device_unref(struct udev_device *udev_device)
586 {
587         unsigned int i;
588
589         if (udev_device == NULL)
590                 return;
591         udev_device->refcount--;
592         if (udev_device->refcount > 0)
593                 return;
594         if (udev_device->parent_device != NULL)
595                 udev_device_unref(udev_device->parent_device);
596         free(udev_device->syspath);
597         free(udev_device->sysname);
598         free(udev_device->devnode);
599         free(udev_device->subsystem);
600         udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list);
601         udev_list_cleanup(udev_device->udev, &udev_device->properties_list);
602         free(udev_device->action);
603         free(udev_device->driver);
604         free(udev_device->devpath_old);
605         free(udev_device->physdevpath);
606         udev_list_cleanup(udev_device->udev, &udev_device->attr_list);
607         for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
608                 free(udev_device->envp[i]);
609         info(udev_device->udev, "udev_device: %p released\n", udev_device);
610         free(udev_device);
611 }
612
613 /**
614  * udev_device_get_devpath:
615  * @udev_device: udev device
616  *
617  * Retrieve the kernel devpath value of the udev device. The path
618  * does not contain the sys mount point, and starts with a '/'.
619  *
620  * Returns: the devpath of the udev device
621  **/
622 const char *udev_device_get_devpath(struct udev_device *udev_device)
623 {
624         if (udev_device == NULL)
625                 return NULL;
626         return udev_device->devpath;
627 }
628
629 /**
630  * udev_device_get_syspath:
631  * @udev_device: udev device
632  *
633  * Retrieve the sys path of the udev device. The path is an
634  * absolute path and starts with the sys mount point.
635  *
636  * Returns: the sys path of the udev device
637  **/
638 const char *udev_device_get_syspath(struct udev_device *udev_device)
639 {
640         if (udev_device == NULL)
641                 return NULL;
642         return udev_device->syspath;
643 }
644
645 const char *udev_device_get_sysname(struct udev_device *udev_device)
646 {
647         if (udev_device == NULL)
648                 return NULL;
649         return udev_device->sysname;
650 }
651
652 const char *udev_device_get_sysnum(struct udev_device *udev_device)
653 {
654         if (udev_device == NULL)
655                 return NULL;
656         return udev_device->sysnum;
657 }
658
659 /**
660  * udev_device_get_devnode:
661  * @udev_device: udev device
662  *
663  * Retrieve the device node file name belonging to the udev device.
664  * The path is an absolute path, and starts with the device directory.
665  *
666  * Returns: the device node file name of the udev device, or #NULL if no device node exists
667  **/
668 const char *udev_device_get_devnode(struct udev_device *udev_device)
669 {
670         if (udev_device == NULL)
671                 return NULL;
672         if (!udev_device->info_loaded)
673                 device_load_info(udev_device);
674         return udev_device->devnode;
675 }
676
677 /**
678  * udev_device_get_subsystem:
679  * @udev_device: udev device
680  *
681  * Retrieve the subsystem string of the udev device. The string does not
682  * contain any "/".
683  *
684  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
685  **/
686 const char *udev_device_get_subsystem(struct udev_device *udev_device)
687 {
688         char subsystem[UTIL_NAME_SIZE];
689
690         if (udev_device == NULL)
691                 return NULL;
692         if (!udev_device->subsystem_set) {
693                 udev_device->subsystem_set = 1;
694                 /* read "subsytem" link */
695                 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
696                         udev_device_set_subsystem(udev_device, subsystem);
697                         return udev_device->subsystem;
698                 }
699                 /* implicit names */
700                 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
701                         udev_device_set_subsystem(udev_device, "module");
702                         return udev_device->subsystem;
703                 }
704                 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
705                         udev_device_set_subsystem(udev_device, "drivers");
706                         return udev_device->subsystem;
707                 }
708                 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
709                     strncmp(udev_device->devpath, "/class/", 7) == 0 ||
710                     strncmp(udev_device->devpath, "/bus/", 5) == 0) {
711                         udev_device_set_subsystem(udev_device, "subsystem");
712                         return udev_device->subsystem;
713                 }
714         }
715         return udev_device->subsystem;
716 }
717
718 /**
719  * udev_device_get_devlinks_list_entry:
720  * @udev_device: udev device
721  *
722  * Retrieve the list of device links pointing to the device file of
723  * the udev device. The next list entry can be retrieved with
724  * udev_list_entry_next(), which returns #NULL if no more entries exist.
725  * The devlink path can be retrieved from the list entry by
726  * udev_list_entry_get_name(). The path is an absolute path, and starts with
727  * the device directory.
728  *
729  * Returns: the first entry of the device node link list
730  **/
731 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
732 {
733         if (udev_device == NULL)
734                 return NULL;
735         if (!udev_device->info_loaded)
736                 device_load_info(udev_device);
737         return udev_list_get_entry(&udev_device->devlinks_list);
738 }
739
740 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
741 {
742         udev_device->devlinks_uptodate = 0;
743         udev_list_cleanup(udev_device->udev, &udev_device->devlinks_list);
744 }
745
746 /**
747  * udev_device_get_properties_list_entry:
748  * @udev_device: udev device
749  *
750  * Retrieve the list of key/value device properties of the udev
751  * device. The next list entry can be retrieved with udev_list_entry_next(),
752  * which returns #NULL if no more entries exist. The property name
753  * can be retrieved from the list entry by udev_list_get_name(),
754  * the property value by udev_list_get_value().
755  *
756  * Returns: the first entry of the property list
757  **/
758 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
759 {
760         if (udev_device == NULL)
761                 return NULL;
762         if (!udev_device->info_loaded)
763                 device_load_info(udev_device);
764         if (!udev_device->devlinks_uptodate) {
765                 char symlinks[UTIL_PATH_SIZE];
766                 struct udev_list_entry *list_entry;
767
768                 udev_device->devlinks_uptodate = 1;
769                 list_entry = udev_device_get_devlinks_list_entry(udev_device);
770                 if (list_entry != NULL) {
771                         util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
772                         udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
773                                 util_strlcat(symlinks, " ", sizeof(symlinks));
774                                 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
775                         }
776                         udev_device_add_property(udev_device, "DEVLINKS", symlinks);
777                 }
778         }
779         return udev_list_get_entry(&udev_device->properties_list);
780 }
781
782 const char *udev_device_get_driver(struct udev_device *udev_device)
783 {
784         char driver[UTIL_NAME_SIZE];
785
786         if (udev_device == NULL)
787                 return NULL;
788         if (!udev_device->driver_set) {
789                 udev_device->driver_set = 1;
790                 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
791                         udev_device->driver = strdup(driver);
792         }
793         return udev_device->driver;
794 }
795
796 dev_t udev_device_get_devnum(struct udev_device *udev_device)
797 {
798         if (udev_device == NULL)
799                 return makedev(0, 0);
800         if (!udev_device->info_loaded)
801                 device_load_info(udev_device);
802         return udev_device->devnum;
803 }
804
805 const char *udev_device_get_action(struct udev_device *udev_device)
806 {
807         if (udev_device == NULL)
808                 return NULL;
809         return udev_device->action;
810 }
811
812 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
813 {
814         if (udev_device == NULL)
815                 return 0;
816         return udev_device->seqnum;
817 }
818
819 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
820 {
821         struct udev_list_entry *list_entry;
822         char path[UTIL_PATH_SIZE];
823         char value[UTIL_NAME_SIZE];
824         struct stat statbuf;
825         int fd;
826         ssize_t size;
827         const char *val = NULL;
828
829         if (udev_device == NULL)
830                 return NULL;
831         if (attr == NULL)
832                 return NULL;
833
834         /* look for possibly already cached result */
835         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->attr_list)) {
836                 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
837                         info(udev_device->udev, "got '%s' (%s) from cache\n",
838                              attr, udev_list_entry_get_value(list_entry));
839                         return udev_list_entry_get_value(list_entry);
840                 }
841         }
842
843         util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
844         util_strlcat(path, "/", sizeof(path));
845         util_strlcat(path, attr, sizeof(path));
846
847         if (lstat(path, &statbuf) != 0) {
848                 info(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
849                 udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, NULL, 0, 0);
850                 goto out;
851         }
852
853         if (S_ISLNK(statbuf.st_mode)) {
854                 /* links return the last element of the target path */
855                 char target[UTIL_NAME_SIZE];
856                 int len;
857                 char *pos;
858
859                 len = readlink(path, target, sizeof(target));
860                 if (len > 0) {
861                         target[len] = '\0';
862                         pos = strrchr(target, '/');
863                         if (pos != NULL) {
864                                 pos = &pos[1];
865                                 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
866                                 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
867                                 val = udev_list_entry_get_value(list_entry);
868                         }
869                 }
870                 goto out;
871         }
872
873         /* skip directories */
874         if (S_ISDIR(statbuf.st_mode))
875                 goto out;
876
877         /* skip non-readable files */
878         if ((statbuf.st_mode & S_IRUSR) == 0)
879                 goto out;
880
881         /* read attribute value */
882         fd = open(path, O_RDONLY);
883         if (fd < 0) {
884                 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
885                 goto out;
886         }
887         size = read(fd, value, sizeof(value));
888         close(fd);
889         if (size < 0)
890                 goto out;
891         if (size == sizeof(value))
892                 goto out;
893
894         /* got a valid value, store it in cache and return it */
895         value[size] = '\0';
896         util_remove_trailing_chars(value, '\n');
897         info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
898         list_entry = udev_list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
899         val = udev_list_entry_get_value(list_entry);
900 out:
901         return val;
902 }
903
904 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
905 {
906         const char *pos;
907         size_t len;
908
909         free(udev_device->syspath);
910         udev_device->syspath = strdup(syspath);
911         if (udev_device->syspath ==  NULL)
912                 return -ENOMEM;
913         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
914         udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
915
916         pos = strrchr(udev_device->syspath, '/');
917         if (pos == NULL)
918                 return -EINVAL;
919         udev_device->sysname = strdup(&pos[1]);
920         if (udev_device->sysname == NULL)
921                 return -ENOMEM;
922
923         /* some devices have '!' in their name, change that to '/' */
924         len = 0;
925         while (udev_device->sysname[len] != '\0') {
926                 if (udev_device->sysname[len] == '!')
927                         udev_device->sysname[len] = '/';
928                 len++;
929         }
930
931         /* trailing number */
932         while (isdigit(udev_device->sysname[--len]))
933                 udev_device->sysnum = &udev_device->sysname[len];
934         return 0;
935 }
936
937 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
938 {
939         free(udev_device->subsystem);
940         udev_device->subsystem = strdup(subsystem);
941         if (udev_device->subsystem == NULL)
942                 return -ENOMEM;
943         udev_device->subsystem_set = 1;
944         udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
945         return 0;
946 }
947
948 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
949 {
950         free(udev_device->devnode);
951         udev_device->devnode = strdup(devnode);
952         if (devnode == NULL)
953                 return 0;
954         if (udev_device->devnode == NULL)
955                 return -ENOMEM;
956         udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
957         return 0;
958 }
959
960 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
961 {
962         udev_device->devlinks_uptodate = 0;
963         if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
964                 return -ENOMEM;
965         return 0;
966 }
967
968 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
969 {
970         udev_device->envp_uptodate = 0;
971         return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
972 }
973
974 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
975 {
976         char name[UTIL_PATH_SIZE];
977         char *val;
978
979         strncpy(name, property, sizeof(name));
980         val = strchr(name, '=');
981         if (val == NULL)
982                 return NULL;
983         val[0] = '\0';
984         val = &val[1];
985         if (val[0] == '\0')
986                 val = NULL;
987         return udev_device_add_property(udev_device, name, val);
988 }
989
990 char **udev_device_get_properties_envp(struct udev_device *udev_device)
991 {
992         if (!udev_device->envp_uptodate) {
993                 unsigned int i;
994                 struct udev_list_entry *list_entry;
995
996                 for (i = 0; i < ARRAY_SIZE(udev_device->envp) && udev_device->envp[i] != NULL; i++)
997                         free(udev_device->envp[i]);
998                 i = 0;
999                 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1000                         asprintf(&udev_device->envp[i++], "%s=%s",
1001                                  udev_list_entry_get_name(list_entry),
1002                                  udev_list_entry_get_value(list_entry));
1003                         if (i+1 >= ARRAY_SIZE(udev_device->envp))
1004                                 break;
1005                 }
1006                 udev_device->envp[i] = NULL;
1007                 info(udev_device->udev, "constructed envp from %u properties\n", i);
1008                 udev_device->envp_uptodate = 1;
1009         }
1010         return udev_device->envp;
1011 }
1012
1013 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1014 {
1015         free(udev_device->action);
1016         udev_device->action = strdup(action);
1017         if (udev_device->action == NULL)
1018                 return -ENOMEM;
1019         udev_device_add_property(udev_device, "ACTION", action);
1020         return 0;
1021 }
1022
1023 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1024 {
1025         free(udev_device->driver);
1026         udev_device->driver = strdup(driver);
1027         if (udev_device->driver == NULL)
1028                 return -ENOMEM;
1029         udev_device->driver_set = 1;
1030         return 0;
1031 }
1032
1033 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1034 {
1035         return udev_device->devpath_old;
1036 }
1037
1038 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1039 {
1040         udev_device->devpath_old = strdup(devpath_old);
1041         if (udev_device->devpath_old == NULL)
1042                 return -ENOMEM;
1043         return 0;
1044 }
1045
1046 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1047 {
1048         return udev_device->physdevpath;
1049 }
1050
1051 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1052 {
1053         udev_device->physdevpath = strdup(physdevpath);
1054         if (udev_device->physdevpath == NULL)
1055                 return -ENOMEM;
1056         return 0;
1057 }
1058
1059 int udev_device_get_timeout(struct udev_device *udev_device)
1060 {
1061         return udev_device->timeout;
1062 }
1063
1064 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1065 {
1066         udev_device->timeout = timeout;
1067         return 0;
1068 }
1069 int udev_device_get_event_timeout(struct udev_device *udev_device)
1070 {
1071         if (!udev_device->info_loaded)
1072                 device_load_info(udev_device);
1073         return udev_device->event_timeout;
1074 }
1075
1076 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1077 {
1078         udev_device->event_timeout = event_timeout;
1079         return 0;
1080 }
1081
1082 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1083 {
1084         udev_device->seqnum = seqnum;
1085         return 0;
1086 }
1087
1088 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1089 {
1090         udev_device->devnum = devnum;
1091         return 0;
1092 }
1093
1094 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1095 {
1096         if (!udev_device->info_loaded)
1097                 device_load_info(udev_device);
1098         return udev_device->num_fake_partitions;
1099 }
1100
1101 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1102 {
1103         udev_device->num_fake_partitions = num;
1104         return 0;
1105 }
1106
1107 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1108 {
1109         if (!udev_device->info_loaded)
1110                 device_load_info(udev_device);
1111         return udev_device->devlink_priority;
1112 }
1113
1114 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1115 {
1116          udev_device->devlink_priority = prio;
1117         return 0;
1118 }
1119
1120 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1121 {
1122         if (!udev_device->info_loaded)
1123                 device_load_info(udev_device);
1124         return udev_device->ignore_remove;
1125 }
1126
1127 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1128 {
1129         udev_device->ignore_remove = ignore;
1130         return 0;
1131 }