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