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