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