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