chiark / gitweb /
bad6e94de58f462a0e6fac888442f3e2b68eca69
[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         struct udev *udev;
36         struct udev_device *parent_device;
37         char *syspath;
38         const char *devpath;
39         char *sysname;
40         const char *sysnum;
41         char *devnode;
42         char *subsystem;
43         char *driver;
44         char *action;
45         char *devpath_old;
46         char *physdevpath;
47         char **envp;
48         char *monitor_buf;
49         size_t monitor_buf_len;
50         struct udev_list_node devlinks_list;
51         struct udev_list_node properties_list;
52         struct udev_list_node sysattr_list;
53         unsigned long long int seqnum;
54         int event_timeout;
55         int timeout;
56         int num_fake_partitions;
57         int devlink_priority;
58         int refcount;
59         dev_t devnum;
60         unsigned int parent_set:1;
61         unsigned int subsystem_set:1;
62         unsigned int devlinks_uptodate:1;
63         unsigned int envp_uptodate:1;
64         unsigned int driver_set:1;
65         unsigned int info_loaded:1;
66         unsigned int ignore_remove:1;
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         if (udev_device == NULL)
596                 return;
597         udev_device->refcount--;
598         if (udev_device->refcount > 0)
599                 return;
600         if (udev_device->parent_device != NULL)
601                 udev_device_unref(udev_device->parent_device);
602         free(udev_device->syspath);
603         free(udev_device->sysname);
604         free(udev_device->devnode);
605         free(udev_device->subsystem);
606         udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
607         udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
608         free(udev_device->action);
609         free(udev_device->driver);
610         free(udev_device->devpath_old);
611         free(udev_device->physdevpath);
612         udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
613         free(udev_device->envp);
614         free(udev_device->monitor_buf);
615         info(udev_device->udev, "udev_device: %p released\n", udev_device);
616         free(udev_device);
617 }
618
619 /**
620  * udev_device_get_devpath:
621  * @udev_device: udev device
622  *
623  * Retrieve the kernel devpath value of the udev device. The path
624  * does not contain the sys mount point, and starts with a '/'.
625  *
626  * Returns: the devpath of the udev device
627  **/
628 const char *udev_device_get_devpath(struct udev_device *udev_device)
629 {
630         if (udev_device == NULL)
631                 return NULL;
632         return udev_device->devpath;
633 }
634
635 /**
636  * udev_device_get_syspath:
637  * @udev_device: udev device
638  *
639  * Retrieve the sys path of the udev device. The path is an
640  * absolute path and starts with the sys mount point.
641  *
642  * Returns: the sys path of the udev device
643  **/
644 const char *udev_device_get_syspath(struct udev_device *udev_device)
645 {
646         if (udev_device == NULL)
647                 return NULL;
648         return udev_device->syspath;
649 }
650
651 const char *udev_device_get_sysname(struct udev_device *udev_device)
652 {
653         if (udev_device == NULL)
654                 return NULL;
655         return udev_device->sysname;
656 }
657
658 const char *udev_device_get_sysnum(struct udev_device *udev_device)
659 {
660         if (udev_device == NULL)
661                 return NULL;
662         return udev_device->sysnum;
663 }
664
665 /**
666  * udev_device_get_devnode:
667  * @udev_device: udev device
668  *
669  * Retrieve the device node file name belonging to the udev device.
670  * The path is an absolute path, and starts with the device directory.
671  *
672  * Returns: the device node file name of the udev device, or #NULL if no device node exists
673  **/
674 const char *udev_device_get_devnode(struct udev_device *udev_device)
675 {
676         if (udev_device == NULL)
677                 return NULL;
678         if (!udev_device->info_loaded)
679                 device_load_info(udev_device);
680         return udev_device->devnode;
681 }
682
683 /**
684  * udev_device_get_subsystem:
685  * @udev_device: udev device
686  *
687  * Retrieve the subsystem string of the udev device. The string does not
688  * contain any "/".
689  *
690  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
691  **/
692 const char *udev_device_get_subsystem(struct udev_device *udev_device)
693 {
694         char subsystem[UTIL_NAME_SIZE];
695
696         if (udev_device == NULL)
697                 return NULL;
698         if (!udev_device->subsystem_set) {
699                 udev_device->subsystem_set = 1;
700                 /* read "subsytem" link */
701                 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
702                         udev_device_set_subsystem(udev_device, subsystem);
703                         return udev_device->subsystem;
704                 }
705                 /* implicit names */
706                 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
707                         udev_device_set_subsystem(udev_device, "module");
708                         return udev_device->subsystem;
709                 }
710                 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
711                         udev_device_set_subsystem(udev_device, "drivers");
712                         return udev_device->subsystem;
713                 }
714                 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
715                     strncmp(udev_device->devpath, "/class/", 7) == 0 ||
716                     strncmp(udev_device->devpath, "/bus/", 5) == 0) {
717                         udev_device_set_subsystem(udev_device, "subsystem");
718                         return udev_device->subsystem;
719                 }
720         }
721         return udev_device->subsystem;
722 }
723
724 /**
725  * udev_device_get_devlinks_list_entry:
726  * @udev_device: udev device
727  *
728  * Retrieve the list of device links pointing to the device file of
729  * the udev device. The next list entry can be retrieved with
730  * udev_list_entry_next(), which returns #NULL if no more entries exist.
731  * The devlink path can be retrieved from the list entry by
732  * udev_list_entry_get_name(). The path is an absolute path, and starts with
733  * the device directory.
734  *
735  * Returns: the first entry of the device node link list
736  **/
737 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
738 {
739         if (udev_device == NULL)
740                 return NULL;
741         if (!udev_device->info_loaded)
742                 device_load_info(udev_device);
743         return udev_list_get_entry(&udev_device->devlinks_list);
744 }
745
746 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
747 {
748         udev_device->devlinks_uptodate = 0;
749         udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
750 }
751
752 /**
753  * udev_device_get_properties_list_entry:
754  * @udev_device: udev device
755  *
756  * Retrieve the list of key/value device properties of the udev
757  * device. The next list entry can be retrieved with udev_list_entry_next(),
758  * which returns #NULL if no more entries exist. The property name
759  * can be retrieved from the list entry by udev_list_get_name(),
760  * the property value by udev_list_get_value().
761  *
762  * Returns: the first entry of the property list
763  **/
764 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
765 {
766         if (udev_device == NULL)
767                 return NULL;
768         if (!udev_device->info_loaded)
769                 device_load_info(udev_device);
770         if (!udev_device->devlinks_uptodate) {
771                 char symlinks[UTIL_PATH_SIZE];
772                 struct udev_list_entry *list_entry;
773
774                 udev_device->devlinks_uptodate = 1;
775                 list_entry = udev_device_get_devlinks_list_entry(udev_device);
776                 if (list_entry != NULL) {
777                         util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
778                         udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
779                                 util_strlcat(symlinks, " ", sizeof(symlinks));
780                                 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
781                         }
782                         udev_device_add_property(udev_device, "DEVLINKS", symlinks);
783                 }
784         }
785         return udev_list_get_entry(&udev_device->properties_list);
786 }
787
788 const char *udev_device_get_driver(struct udev_device *udev_device)
789 {
790         char driver[UTIL_NAME_SIZE];
791
792         if (udev_device == NULL)
793                 return NULL;
794         if (!udev_device->driver_set) {
795                 udev_device->driver_set = 1;
796                 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
797                         udev_device->driver = strdup(driver);
798         }
799         return udev_device->driver;
800 }
801
802 dev_t udev_device_get_devnum(struct udev_device *udev_device)
803 {
804         if (udev_device == NULL)
805                 return makedev(0, 0);
806         if (!udev_device->info_loaded)
807                 device_load_info(udev_device);
808         return udev_device->devnum;
809 }
810
811 const char *udev_device_get_action(struct udev_device *udev_device)
812 {
813         if (udev_device == NULL)
814                 return NULL;
815         return udev_device->action;
816 }
817
818 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
819 {
820         if (udev_device == NULL)
821                 return 0;
822         return udev_device->seqnum;
823 }
824
825 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
826 {
827         struct udev_list_entry *list_entry;
828         char path[UTIL_PATH_SIZE];
829         char value[UTIL_NAME_SIZE];
830         struct stat statbuf;
831         int fd;
832         ssize_t size;
833         const char *val = NULL;
834
835         if (udev_device == NULL)
836                 return NULL;
837         if (sysattr == NULL)
838                 return NULL;
839
840         /* look for possibly already cached result */
841         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
842                 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
843                         info(udev_device->udev, "got '%s' (%s) from cache\n",
844                              sysattr, udev_list_entry_get_value(list_entry));
845                         return udev_list_entry_get_value(list_entry);
846                 }
847         }
848
849         util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
850         util_strlcat(path, "/", sizeof(path));
851         util_strlcat(path, sysattr, sizeof(path));
852
853         if (lstat(path, &statbuf) != 0) {
854                 info(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
855                 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
856                 goto out;
857         }
858
859         if (S_ISLNK(statbuf.st_mode)) {
860                 /* links return the last element of the target path */
861                 char target[UTIL_NAME_SIZE];
862                 int len;
863                 char *pos;
864
865                 len = readlink(path, target, sizeof(target));
866                 if (len > 0) {
867                         target[len] = '\0';
868                         pos = strrchr(target, '/');
869                         if (pos != NULL) {
870                                 pos = &pos[1];
871                                 info(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
872                                 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
873                                 val = udev_list_entry_get_value(list_entry);
874                         }
875                 }
876                 goto out;
877         }
878
879         /* skip directories */
880         if (S_ISDIR(statbuf.st_mode))
881                 goto out;
882
883         /* skip non-readable files */
884         if ((statbuf.st_mode & S_IRUSR) == 0)
885                 goto out;
886
887         /* read attribute value */
888         fd = open(path, O_RDONLY);
889         if (fd < 0) {
890                 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
891                 goto out;
892         }
893         size = read(fd, value, sizeof(value));
894         close(fd);
895         if (size < 0)
896                 goto out;
897         if (size == sizeof(value))
898                 goto out;
899
900         /* got a valid value, store it in cache and return it */
901         value[size] = '\0';
902         util_remove_trailing_chars(value, '\n');
903         info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
904         list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
905         val = udev_list_entry_get_value(list_entry);
906 out:
907         return val;
908 }
909
910 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
911 {
912         const char *pos;
913         size_t len;
914
915         free(udev_device->syspath);
916         udev_device->syspath = strdup(syspath);
917         if (udev_device->syspath ==  NULL)
918                 return -ENOMEM;
919         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
920         udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
921
922         pos = strrchr(udev_device->syspath, '/');
923         if (pos == NULL)
924                 return -EINVAL;
925         udev_device->sysname = strdup(&pos[1]);
926         if (udev_device->sysname == NULL)
927                 return -ENOMEM;
928
929         /* some devices have '!' in their name, change that to '/' */
930         len = 0;
931         while (udev_device->sysname[len] != '\0') {
932                 if (udev_device->sysname[len] == '!')
933                         udev_device->sysname[len] = '/';
934                 len++;
935         }
936
937         /* trailing number */
938         while (len > 0 && isdigit(udev_device->sysname[--len]))
939                 udev_device->sysnum = &udev_device->sysname[len];
940
941         /* sysname is completely numeric */
942         if (len == 0)
943                 udev_device->sysnum = NULL;
944
945         return 0;
946 }
947
948 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
949 {
950         free(udev_device->subsystem);
951         udev_device->subsystem = strdup(subsystem);
952         if (udev_device->subsystem == NULL)
953                 return -ENOMEM;
954         udev_device->subsystem_set = 1;
955         udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
956         return 0;
957 }
958
959 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
960 {
961         free(udev_device->devnode);
962         udev_device->devnode = strdup(devnode);
963         if (devnode == NULL)
964                 return 0;
965         if (udev_device->devnode == NULL)
966                 return -ENOMEM;
967         udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
968         return 0;
969 }
970
971 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
972 {
973         udev_device->devlinks_uptodate = 0;
974         if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
975                 return -ENOMEM;
976         return 0;
977 }
978
979 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
980 {
981         udev_device->envp_uptodate = 0;
982         if (value == NULL) {
983                 struct udev_list_entry *list_entry;
984
985                 list_entry = udev_device_get_properties_list_entry(udev_device);
986                 list_entry = udev_list_entry_get_by_name(list_entry, key);
987                 if (list_entry != NULL)
988                         udev_list_entry_remove(list_entry);
989                 return NULL;
990         }
991         return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
992 }
993
994 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
995 {
996         char name[UTIL_PATH_SIZE];
997         char *val;
998
999         util_strlcpy(name, property, sizeof(name));
1000         val = strchr(name, '=');
1001         if (val == NULL)
1002                 return NULL;
1003         val[0] = '\0';
1004         val = &val[1];
1005         if (val[0] == '\0')
1006                 val = NULL;
1007         return udev_device_add_property(udev_device, name, val);
1008 }
1009
1010 #define ENVP_SIZE                       128
1011 #define MONITOR_BUF_SIZE                4096
1012 static int update_envp_monitor_buf(struct udev_device *udev_device)
1013 {
1014         const char *action;
1015         struct udev_list_entry *list_entry;
1016         size_t bufpos;
1017         size_t len;
1018         unsigned int i;
1019
1020         /* monitor buffer of property strings */
1021         free(udev_device->monitor_buf);
1022         udev_device->monitor_buf_len = 0;
1023         udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1024         if (udev_device->monitor_buf == NULL)
1025                 return -ENOMEM;
1026
1027         /* envp array, strings will point into monitor buffer */
1028         free(udev_device->envp);
1029         udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1030         if (udev_device->envp == NULL)
1031                 return -ENOMEM;
1032
1033         /* header <action>@<devpath> */
1034         action = udev_device_get_action(udev_device);
1035         if (action == NULL)
1036                 return -EINVAL;
1037         bufpos = util_strlcpy(udev_device->monitor_buf, action, MONITOR_BUF_SIZE);
1038         len = util_strlcpy(&udev_device->monitor_buf[bufpos], "@", MONITOR_BUF_SIZE-bufpos);
1039         if (len >= MONITOR_BUF_SIZE-bufpos)
1040                 return -EINVAL;
1041         bufpos += len;
1042         len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1043                            udev_device_get_devpath(udev_device),
1044                            MONITOR_BUF_SIZE-bufpos);
1045         if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1046                 return -EINVAL;
1047         bufpos += len+1;
1048
1049         i = 0;
1050         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1051                 /* add string to envp array */
1052                 udev_device->envp[i++] = &udev_device->monitor_buf[bufpos];
1053                 if (i+1 >= ENVP_SIZE)
1054                         return -EINVAL;
1055
1056                 /* add property string to monitor buffer */
1057                 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1058                                    udev_list_entry_get_name(list_entry), MONITOR_BUF_SIZE-bufpos);
1059                 if (len >= MONITOR_BUF_SIZE-bufpos)
1060                         return -EINVAL;
1061                 bufpos += len;
1062                 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "=", MONITOR_BUF_SIZE-bufpos);
1063                 if (len >= MONITOR_BUF_SIZE-bufpos)
1064                         return -EINVAL;
1065                 bufpos += len;
1066                 len = util_strlcpy(&udev_device->monitor_buf[bufpos], udev_list_entry_get_value(list_entry),
1067                                    MONITOR_BUF_SIZE-bufpos);
1068                 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1069                         return -EINVAL;
1070                 bufpos += len+1;
1071         }
1072         udev_device->envp[i] = NULL;
1073         udev_device->monitor_buf_len = bufpos;
1074         udev_device->envp_uptodate = 1;
1075         info(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n", i, bufpos);
1076         return 0;
1077 }
1078
1079 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1080 {
1081         if (!udev_device->envp_uptodate)
1082                 if (update_envp_monitor_buf(udev_device) != 0)
1083                         return NULL;
1084         return udev_device->envp;
1085 }
1086
1087 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1088 {
1089         if (!udev_device->envp_uptodate)
1090                 if (update_envp_monitor_buf(udev_device) != 0)
1091                         return -EINVAL;
1092         *buf = udev_device->monitor_buf;
1093         return udev_device->monitor_buf_len;
1094 }
1095
1096 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1097 {
1098         free(udev_device->action);
1099         udev_device->action = strdup(action);
1100         if (udev_device->action == NULL)
1101                 return -ENOMEM;
1102         udev_device_add_property(udev_device, "ACTION", udev_device->action);
1103         return 0;
1104 }
1105
1106 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1107 {
1108         free(udev_device->driver);
1109         udev_device->driver = strdup(driver);
1110         if (udev_device->driver == NULL)
1111                 return -ENOMEM;
1112         udev_device->driver_set = 1;
1113         udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1114         return 0;
1115 }
1116
1117 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1118 {
1119         return udev_device->devpath_old;
1120 }
1121
1122 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1123 {
1124         udev_device->devpath_old = strdup(devpath_old);
1125         if (udev_device->devpath_old == NULL)
1126                 return -ENOMEM;
1127         udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1128         return 0;
1129 }
1130
1131 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1132 {
1133         return udev_device->physdevpath;
1134 }
1135
1136 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1137 {
1138         udev_device->physdevpath = strdup(physdevpath);
1139         if (udev_device->physdevpath == NULL)
1140                 return -ENOMEM;
1141         return 0;
1142 }
1143
1144 int udev_device_get_timeout(struct udev_device *udev_device)
1145 {
1146         return udev_device->timeout;
1147 }
1148
1149 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1150 {
1151         udev_device->timeout = timeout;
1152         return 0;
1153 }
1154 int udev_device_get_event_timeout(struct udev_device *udev_device)
1155 {
1156         if (!udev_device->info_loaded)
1157                 device_load_info(udev_device);
1158         return udev_device->event_timeout;
1159 }
1160
1161 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1162 {
1163         udev_device->event_timeout = event_timeout;
1164         return 0;
1165 }
1166
1167 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1168 {
1169         char num[32];
1170
1171         udev_device->seqnum = seqnum;
1172         snprintf(num, sizeof(num), "%llu", seqnum);
1173         udev_device_add_property(udev_device, "SEQNUM", num);
1174         return 0;
1175 }
1176
1177 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1178 {
1179         char num[32];
1180
1181         udev_device->devnum = devnum;
1182
1183         snprintf(num, sizeof(num), "%u", major(devnum));
1184         udev_device_add_property(udev_device, "MAJOR", num);
1185         snprintf(num, sizeof(num), "%u", minor(devnum));
1186         udev_device_add_property(udev_device, "MINOR", num);
1187         return 0;
1188 }
1189
1190 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1191 {
1192         if (!udev_device->info_loaded)
1193                 device_load_info(udev_device);
1194         return udev_device->num_fake_partitions;
1195 }
1196
1197 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1198 {
1199         udev_device->num_fake_partitions = num;
1200         return 0;
1201 }
1202
1203 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1204 {
1205         if (!udev_device->info_loaded)
1206                 device_load_info(udev_device);
1207         return udev_device->devlink_priority;
1208 }
1209
1210 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1211 {
1212          udev_device->devlink_priority = prio;
1213         return 0;
1214 }
1215
1216 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1217 {
1218         if (!udev_device->info_loaded)
1219                 device_load_info(udev_device);
1220         return udev_device->ignore_remove;
1221 }
1222
1223 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1224 {
1225         udev_device->ignore_remove = ignore;
1226         return 0;
1227 }