chiark / gitweb /
libudev: device - lookup subsystem and devtype together
[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 *devtype;
44         char *driver;
45         char *action;
46         char *devpath_old;
47         char *physdevpath;
48         char **envp;
49         char *monitor_buf;
50         size_t monitor_buf_len;
51         struct udev_list_node devlinks_list;
52         struct udev_list_node properties_list;
53         struct udev_list_node sysattr_list;
54         unsigned long long int seqnum;
55         int event_timeout;
56         int timeout;
57         int num_fake_partitions;
58         int devlink_priority;
59         int refcount;
60         dev_t devnum;
61         unsigned int parent_set:1;
62         unsigned int subsystem_set:1;
63         unsigned int devtype_set:1;
64         unsigned int devlinks_uptodate:1;
65         unsigned int envp_uptodate:1;
66         unsigned int driver_set:1;
67         unsigned int info_loaded:1;
68         unsigned int ignore_remove:1;
69 };
70
71 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
72 {
73         size_t start;
74
75         /* translate to location of db file */
76         util_strlcpy(filename, udev_get_dev_path(udev), len);
77         start = util_strlcat(filename, "/.udev/db/", len);
78         util_strlcat(filename, devpath, len);
79         return util_path_encode(&filename[start], len - start);
80 }
81
82 int udev_device_read_db(struct udev_device *udev_device)
83 {
84         struct stat stats;
85         char filename[UTIL_PATH_SIZE];
86         char line[UTIL_LINE_SIZE];
87         FILE *f;
88
89         devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
90
91         if (lstat(filename, &stats) != 0) {
92                 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
93                 return -1;
94         }
95         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
96                 char target[UTIL_PATH_SIZE];
97                 char devnode[UTIL_PATH_SIZE];
98                 int target_len;
99                 char *next;
100
101                 target_len = readlink(filename, target, sizeof(target));
102                 if (target_len > 0)
103                         target[target_len] = '\0';
104                 else {
105                         dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
106                         return -1;
107                 }
108
109                 next = strchr(target, ' ');
110                 if (next != NULL) {
111                         next[0] = '\0';
112                         next = &next[1];
113                 }
114                 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
115                 util_strlcat(devnode, "/", sizeof(devnode));
116                 util_strlcat(devnode, target, sizeof(devnode));
117                 udev_device_set_devnode(udev_device, devnode);
118                 while (next != NULL) {
119                         char devlink[UTIL_PATH_SIZE];
120                         const char *lnk;
121
122                         lnk = next;
123                         next = strchr(next, ' ');
124                         if (next != NULL) {
125                                 next[0] = '\0';
126                                 next = &next[1];
127                         }
128                         util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
129                         util_strlcat(devlink, "/", sizeof(devlink));
130                         util_strlcat(devlink, lnk, sizeof(devlink));
131                         udev_device_add_devlink(udev_device, devlink);
132                 }
133                 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
134                 return 0;
135         }
136
137         f = fopen(filename, "r");
138         if (f == NULL) {
139                 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
140                 return -1;
141         }
142         while (fgets(line, sizeof(line), f)) {
143                 ssize_t len;
144                 const char *val;
145
146                 len = strlen(line);
147                 if (len < 4)
148                         break;
149                 line[len-1] = '\0';
150                 val = &line[2];
151                 switch(line[0]) {
152                 case 'N':
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_set_devnode(udev_device, filename);
157                         break;
158                 case 'S':
159                         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
160                         util_strlcat(filename, "/", sizeof(filename));
161                         util_strlcat(filename, val, sizeof(filename));
162                         udev_device_add_devlink(udev_device, filename);
163                         break;
164                 case 'L':
165                         udev_device_set_devlink_priority(udev_device, atoi(val));
166                         break;
167                 case 'T':
168                         udev_device_set_event_timeout(udev_device, atoi(val));
169                         break;
170                 case 'A':
171                         udev_device_set_num_fake_partitions(udev_device, atoi(val));
172                         break;
173                 case 'R':
174                         udev_device_set_ignore_remove(udev_device, atoi(val));
175                         break;
176                 case 'E':
177                         udev_device_add_property_from_string(udev_device, val);
178                         break;
179                 }
180         }
181         fclose(f);
182
183         info(udev_device->udev, "device %p filled with db file data\n", udev_device);
184         return 0;
185 }
186
187 int udev_device_read_uevent_file(struct udev_device *udev_device)
188 {
189         char filename[UTIL_PATH_SIZE];
190         FILE *f;
191         char line[UTIL_LINE_SIZE];
192         int maj = 0;
193         int min = 0;
194
195         util_strlcpy(filename, udev_device->syspath, sizeof(filename));
196         util_strlcat(filename, "/uevent", sizeof(filename));
197         f = fopen(filename, "r");
198         if (f == NULL)
199                 return -1;
200
201         while (fgets(line, sizeof(line), f)) {
202                 char *pos;
203
204                 pos = strchr(line, '\n');
205                 if (pos == NULL)
206                         continue;
207                 pos[0] = '\0';
208
209                 if (strncmp(line, "DEVTYPE=", 8) == 0)
210                         udev_device_set_devtype(udev_device, &line[8]);
211                 else if (strncmp(line, "MAJOR=", 6) == 0)
212                         maj = strtoull(&line[6], NULL, 10);
213                 else if (strncmp(line, "MINOR=", 6) == 0)
214                         min = strtoull(&line[6], NULL, 10);
215
216                 udev_device_add_property_from_string(udev_device, line);
217         }
218
219         udev_device->devnum = makedev(maj, min);
220
221         fclose(f);
222         return 0;
223 }
224
225 static void device_load_info(struct udev_device *device)
226 {
227         device->info_loaded = 1;
228         udev_device_read_uevent_file(device);
229         udev_device_read_db(device);
230 }
231
232 void udev_device_set_info_loaded(struct udev_device *device)
233 {
234         device->info_loaded = 1;
235 }
236
237 struct udev_device *device_new(struct udev *udev)
238 {
239         struct udev_device *udev_device;
240         struct udev_list_entry *list_entry;
241
242         if (udev == NULL)
243                 return NULL;
244
245         udev_device = calloc(1, sizeof(struct udev_device));
246         if (udev_device == NULL)
247                 return NULL;
248         udev_device->refcount = 1;
249         udev_device->udev = udev;
250         udev_list_init(&udev_device->devlinks_list);
251         udev_list_init(&udev_device->properties_list);
252         udev_list_init(&udev_device->sysattr_list);
253         udev_device->event_timeout = -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 < &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(struct udev_device *udev_device, const char *subsystem)
545 {
546         struct udev_device *parent;
547
548         parent = udev_device_get_parent(udev_device);
549         while (parent != NULL) {
550                 const char *parent_subsystem;
551
552                 parent_subsystem = udev_device_get_subsystem(parent);
553                 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
554                         break;
555                 parent = udev_device_get_parent(parent);
556         }
557         return parent;
558 }
559
560 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
561 {
562         struct udev_device *parent;
563
564         if (subsystem == NULL)
565                 return NULL;
566
567         parent = udev_device_get_parent(udev_device);
568         while (parent != NULL) {
569                 const char *parent_subsystem;
570                 const char *parent_devtype;
571
572                 parent_subsystem = udev_device_get_subsystem(parent);
573                 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
574                         if (devtype == NULL)
575                                 break;
576                         parent_devtype = udev_device_get_devtype(parent);
577                         if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
578                                 break;
579                 }
580                 parent = udev_device_get_parent(parent);
581         }
582         return parent;
583 }
584
585 /**
586  * udev_device_get_udev:
587  * @udev_device: udev device
588  *
589  * Retrieve the udev library context the device was created with.
590  *
591  * Returns: the udev library context
592  **/
593 struct udev *udev_device_get_udev(struct udev_device *udev_device)
594 {
595         if (udev_device == NULL)
596                 return NULL;
597         return udev_device->udev;
598 }
599
600 /**
601  * udev_device_ref:
602  * @udev_device: udev device
603  *
604  * Take a reference of a udev device.
605  *
606  * Returns: the passed udev device
607  **/
608 struct udev_device *udev_device_ref(struct udev_device *udev_device)
609 {
610         if (udev_device == NULL)
611                 return NULL;
612         udev_device->refcount++;
613         return udev_device;
614 }
615
616 /**
617  * udev_device_unref:
618  * @udev_device: udev device
619  *
620  * Drop a reference of a udev device. If the refcount reaches zero,
621  * the resources of the device will be released.
622  *
623  **/
624 void udev_device_unref(struct udev_device *udev_device)
625 {
626         if (udev_device == NULL)
627                 return;
628         udev_device->refcount--;
629         if (udev_device->refcount > 0)
630                 return;
631         if (udev_device->parent_device != NULL)
632                 udev_device_unref(udev_device->parent_device);
633         free(udev_device->syspath);
634         free(udev_device->sysname);
635         free(udev_device->devnode);
636         free(udev_device->subsystem);
637         free(udev_device->devtype);
638         udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
639         udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
640         free(udev_device->action);
641         free(udev_device->driver);
642         free(udev_device->devpath_old);
643         free(udev_device->physdevpath);
644         udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
645         free(udev_device->envp);
646         free(udev_device->monitor_buf);
647         dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
648         free(udev_device);
649 }
650
651 /**
652  * udev_device_get_devpath:
653  * @udev_device: udev device
654  *
655  * Retrieve the kernel devpath value of the udev device. The path
656  * does not contain the sys mount point, and starts with a '/'.
657  *
658  * Returns: the devpath of the udev device
659  **/
660 const char *udev_device_get_devpath(struct udev_device *udev_device)
661 {
662         if (udev_device == NULL)
663                 return NULL;
664         return udev_device->devpath;
665 }
666
667 /**
668  * udev_device_get_syspath:
669  * @udev_device: udev device
670  *
671  * Retrieve the sys path of the udev device. The path is an
672  * absolute path and starts with the sys mount point.
673  *
674  * Returns: the sys path of the udev device
675  **/
676 const char *udev_device_get_syspath(struct udev_device *udev_device)
677 {
678         if (udev_device == NULL)
679                 return NULL;
680         return udev_device->syspath;
681 }
682
683 const char *udev_device_get_sysname(struct udev_device *udev_device)
684 {
685         if (udev_device == NULL)
686                 return NULL;
687         return udev_device->sysname;
688 }
689
690 const char *udev_device_get_sysnum(struct udev_device *udev_device)
691 {
692         if (udev_device == NULL)
693                 return NULL;
694         return udev_device->sysnum;
695 }
696
697 /**
698  * udev_device_get_devnode:
699  * @udev_device: udev device
700  *
701  * Retrieve the device node file name belonging to the udev device.
702  * The path is an absolute path, and starts with the device directory.
703  *
704  * Returns: the device node file name of the udev device, or #NULL if no device node exists
705  **/
706 const char *udev_device_get_devnode(struct udev_device *udev_device)
707 {
708         if (udev_device == NULL)
709                 return NULL;
710         if (!udev_device->info_loaded)
711                 device_load_info(udev_device);
712         return udev_device->devnode;
713 }
714
715 /**
716  * udev_device_get_subsystem:
717  * @udev_device: udev device
718  *
719  * Retrieve the subsystem string of the udev device. The string does not
720  * contain any "/".
721  *
722  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
723  **/
724 const char *udev_device_get_subsystem(struct udev_device *udev_device)
725 {
726         char subsystem[UTIL_NAME_SIZE];
727
728         if (udev_device == NULL)
729                 return NULL;
730         if (!udev_device->subsystem_set) {
731                 udev_device->subsystem_set = 1;
732                 /* read "subsytem" link */
733                 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
734                         udev_device_set_subsystem(udev_device, subsystem);
735                         return udev_device->subsystem;
736                 }
737                 /* implicit names */
738                 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
739                         udev_device_set_subsystem(udev_device, "module");
740                         return udev_device->subsystem;
741                 }
742                 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
743                         udev_device_set_subsystem(udev_device, "drivers");
744                         return udev_device->subsystem;
745                 }
746                 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
747                     strncmp(udev_device->devpath, "/class/", 7) == 0 ||
748                     strncmp(udev_device->devpath, "/bus/", 5) == 0) {
749                         udev_device_set_subsystem(udev_device, "subsystem");
750                         return udev_device->subsystem;
751                 }
752         }
753         return udev_device->subsystem;
754 }
755
756 /**
757  * udev_device_get_devtype:
758  * @udev_device: udev device
759  *
760  * Retrieve the devtype string of the udev device.
761  *
762  * Returns: the devtype name of the udev device, or #NULL if it can not be determined
763  **/
764 const char *udev_device_get_devtype(struct udev_device *udev_device)
765 {
766         if (udev_device == NULL)
767                 return NULL;
768         if (!udev_device->devtype_set) {
769                 udev_device->devtype_set = 1;
770                 if (!udev_device->info_loaded)
771                         udev_device_read_uevent_file(udev_device);
772         }
773         return udev_device->devtype;
774 }
775
776 /**
777  * udev_device_get_devlinks_list_entry:
778  * @udev_device: udev device
779  *
780  * Retrieve the list of device links pointing to the device file of
781  * the udev device. The next list entry can be retrieved with
782  * udev_list_entry_next(), which returns #NULL if no more entries exist.
783  * The devlink path can be retrieved from the list entry by
784  * udev_list_entry_get_name(). The path is an absolute path, and starts with
785  * the device directory.
786  *
787  * Returns: the first entry of the device node link list
788  **/
789 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
790 {
791         if (udev_device == NULL)
792                 return NULL;
793         if (!udev_device->info_loaded)
794                 device_load_info(udev_device);
795         return udev_list_get_entry(&udev_device->devlinks_list);
796 }
797
798 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
799 {
800         udev_device->devlinks_uptodate = 0;
801         udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
802 }
803
804 /**
805  * udev_device_get_properties_list_entry:
806  * @udev_device: udev device
807  *
808  * Retrieve the list of key/value device properties of the udev
809  * device. The next list entry can be retrieved with udev_list_entry_next(),
810  * which returns #NULL if no more entries exist. The property name
811  * can be retrieved from the list entry by udev_list_get_name(),
812  * the property value by udev_list_get_value().
813  *
814  * Returns: the first entry of the property list
815  **/
816 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
817 {
818         if (udev_device == NULL)
819                 return NULL;
820         if (!udev_device->info_loaded)
821                 device_load_info(udev_device);
822         if (!udev_device->devlinks_uptodate) {
823                 char symlinks[UTIL_PATH_SIZE];
824                 struct udev_list_entry *list_entry;
825
826                 udev_device->devlinks_uptodate = 1;
827                 list_entry = udev_device_get_devlinks_list_entry(udev_device);
828                 if (list_entry != NULL) {
829                         util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
830                         udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
831                                 util_strlcat(symlinks, " ", sizeof(symlinks));
832                                 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
833                         }
834                         udev_device_add_property(udev_device, "DEVLINKS", symlinks);
835                 }
836         }
837         return udev_list_get_entry(&udev_device->properties_list);
838 }
839
840 const char *udev_device_get_driver(struct udev_device *udev_device)
841 {
842         char driver[UTIL_NAME_SIZE];
843
844         if (udev_device == NULL)
845                 return NULL;
846         if (!udev_device->driver_set) {
847                 udev_device->driver_set = 1;
848                 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
849                         udev_device->driver = strdup(driver);
850         }
851         return udev_device->driver;
852 }
853
854 dev_t udev_device_get_devnum(struct udev_device *udev_device)
855 {
856         if (udev_device == NULL)
857                 return makedev(0, 0);
858         if (!udev_device->info_loaded)
859                 device_load_info(udev_device);
860         return udev_device->devnum;
861 }
862
863 const char *udev_device_get_action(struct udev_device *udev_device)
864 {
865         if (udev_device == NULL)
866                 return NULL;
867         return udev_device->action;
868 }
869
870 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
871 {
872         if (udev_device == NULL)
873                 return 0;
874         return udev_device->seqnum;
875 }
876
877 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
878 {
879         struct udev_list_entry *list_entry;
880         char path[UTIL_PATH_SIZE];
881         char value[UTIL_NAME_SIZE];
882         struct stat statbuf;
883         int fd;
884         ssize_t size;
885         const char *val = NULL;
886
887         if (udev_device == NULL)
888                 return NULL;
889         if (sysattr == NULL)
890                 return NULL;
891
892         /* look for possibly already cached result */
893         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
894                 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
895                         dbg(udev_device->udev, "got '%s' (%s) from cache\n",
896                             sysattr, udev_list_entry_get_value(list_entry));
897                         return udev_list_entry_get_value(list_entry);
898                 }
899         }
900
901         util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
902         util_strlcat(path, "/", sizeof(path));
903         util_strlcat(path, sysattr, sizeof(path));
904
905         if (lstat(path, &statbuf) != 0) {
906                 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
907                 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
908                 goto out;
909         }
910
911         if (S_ISLNK(statbuf.st_mode)) {
912                 char target[UTIL_NAME_SIZE];
913                 int len;
914                 char *pos;
915
916                 /* some core links return the last element of the target path */
917                 if (strcmp(sysattr, "driver") != 0 &&
918                     strcmp(sysattr, "subsystem") != 0 &&
919                     strcmp(sysattr, "module") != 0)
920                         goto out;
921
922                 len = readlink(path, target, sizeof(target));
923                 if (len > 0) {
924                         target[len] = '\0';
925                         pos = strrchr(target, '/');
926                         if (pos != NULL) {
927                                 pos = &pos[1];
928                                 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
929                                 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
930                                 val = udev_list_entry_get_value(list_entry);
931                         }
932                 }
933                 goto out;
934         }
935
936         /* skip directories */
937         if (S_ISDIR(statbuf.st_mode))
938                 goto out;
939
940         /* skip non-readable files */
941         if ((statbuf.st_mode & S_IRUSR) == 0)
942                 goto out;
943
944         /* read attribute value */
945         fd = open(path, O_RDONLY);
946         if (fd < 0) {
947                 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
948                 goto out;
949         }
950         size = read(fd, value, sizeof(value));
951         close(fd);
952         if (size < 0)
953                 goto out;
954         if (size == sizeof(value))
955                 goto out;
956
957         /* got a valid value, store it in cache and return it */
958         value[size] = '\0';
959         util_remove_trailing_chars(value, '\n');
960         dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
961         list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
962         val = udev_list_entry_get_value(list_entry);
963 out:
964         return val;
965 }
966
967 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
968 {
969         const char *pos;
970         size_t len;
971
972         free(udev_device->syspath);
973         udev_device->syspath = strdup(syspath);
974         if (udev_device->syspath ==  NULL)
975                 return -ENOMEM;
976         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
977         udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
978
979         pos = strrchr(udev_device->syspath, '/');
980         if (pos == NULL)
981                 return -EINVAL;
982         udev_device->sysname = strdup(&pos[1]);
983         if (udev_device->sysname == NULL)
984                 return -ENOMEM;
985
986         /* some devices have '!' in their name, change that to '/' */
987         len = 0;
988         while (udev_device->sysname[len] != '\0') {
989                 if (udev_device->sysname[len] == '!')
990                         udev_device->sysname[len] = '/';
991                 len++;
992         }
993
994         /* trailing number */
995         while (len > 0 && isdigit(udev_device->sysname[--len]))
996                 udev_device->sysnum = &udev_device->sysname[len];
997
998         /* sysname is completely numeric */
999         if (len == 0)
1000                 udev_device->sysnum = NULL;
1001
1002         return 0;
1003 }
1004
1005 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1006 {
1007         free(udev_device->subsystem);
1008         udev_device->subsystem = strdup(subsystem);
1009         if (udev_device->subsystem == NULL)
1010                 return -ENOMEM;
1011         udev_device->subsystem_set = 1;
1012         udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1013         return 0;
1014 }
1015
1016 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1017 {
1018         free(udev_device->devtype);
1019         udev_device->devtype = strdup(devtype);
1020         if (udev_device->devtype == NULL)
1021                 return -ENOMEM;
1022         udev_device->devtype_set = 1;
1023         udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1024         return 0;
1025 }
1026
1027 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1028 {
1029         free(udev_device->devnode);
1030         udev_device->devnode = strdup(devnode);
1031         if (devnode == NULL)
1032                 return 0;
1033         if (udev_device->devnode == NULL)
1034                 return -ENOMEM;
1035         udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1036         return 0;
1037 }
1038
1039 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
1040 {
1041         udev_device->devlinks_uptodate = 0;
1042         if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
1043                 return -ENOMEM;
1044         return 0;
1045 }
1046
1047 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1048 {
1049         udev_device->envp_uptodate = 0;
1050         if (value == NULL) {
1051                 struct udev_list_entry *list_entry;
1052
1053                 list_entry = udev_device_get_properties_list_entry(udev_device);
1054                 list_entry = udev_list_entry_get_by_name(list_entry, key);
1055                 if (list_entry != NULL)
1056                         udev_list_entry_delete(list_entry);
1057                 return NULL;
1058         }
1059         return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1060 }
1061
1062 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1063 {
1064         char name[UTIL_PATH_SIZE];
1065         char *val;
1066
1067         util_strlcpy(name, property, sizeof(name));
1068         val = strchr(name, '=');
1069         if (val == NULL)
1070                 return NULL;
1071         val[0] = '\0';
1072         val = &val[1];
1073         if (val[0] == '\0')
1074                 val = NULL;
1075         return udev_device_add_property(udev_device, name, val);
1076 }
1077
1078 #define ENVP_SIZE                       128
1079 #define MONITOR_BUF_SIZE                4096
1080 static int update_envp_monitor_buf(struct udev_device *udev_device)
1081 {
1082         const char *action;
1083         struct udev_list_entry *list_entry;
1084         size_t bufpos;
1085         size_t len;
1086         unsigned int i;
1087
1088         /* monitor buffer of property strings */
1089         free(udev_device->monitor_buf);
1090         udev_device->monitor_buf_len = 0;
1091         udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1092         if (udev_device->monitor_buf == NULL)
1093                 return -ENOMEM;
1094
1095         /* envp array, strings will point into monitor buffer */
1096         if (udev_device->envp == NULL)
1097                 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1098         if (udev_device->envp == NULL)
1099                 return -ENOMEM;
1100
1101         /* header <action>@<devpath> */
1102         action = udev_device_get_action(udev_device);
1103         if (action == NULL)
1104                 return -EINVAL;
1105         bufpos = util_strlcpy(udev_device->monitor_buf, action, MONITOR_BUF_SIZE);
1106         len = util_strlcpy(&udev_device->monitor_buf[bufpos], "@", MONITOR_BUF_SIZE-bufpos);
1107         if (len >= MONITOR_BUF_SIZE-bufpos)
1108                 return -EINVAL;
1109         bufpos += len;
1110         len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1111                            udev_device_get_devpath(udev_device),
1112                            MONITOR_BUF_SIZE-bufpos);
1113         if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1114                 return -EINVAL;
1115         bufpos += len+1;
1116
1117         i = 0;
1118         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1119                 /* add string to envp array */
1120                 udev_device->envp[i++] = &udev_device->monitor_buf[bufpos];
1121                 if (i+1 >= ENVP_SIZE)
1122                         return -EINVAL;
1123
1124                 /* add property string to monitor buffer */
1125                 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1126                                    udev_list_entry_get_name(list_entry), MONITOR_BUF_SIZE-bufpos);
1127                 if (len >= MONITOR_BUF_SIZE-bufpos)
1128                         return -EINVAL;
1129                 bufpos += len;
1130                 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "=", MONITOR_BUF_SIZE-bufpos);
1131                 if (len >= MONITOR_BUF_SIZE-bufpos)
1132                         return -EINVAL;
1133                 bufpos += len;
1134                 len = util_strlcpy(&udev_device->monitor_buf[bufpos], udev_list_entry_get_value(list_entry),
1135                                    MONITOR_BUF_SIZE-bufpos);
1136                 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1137                         return -EINVAL;
1138                 bufpos += len+1;
1139         }
1140         udev_device->envp[i] = NULL;
1141         udev_device->monitor_buf_len = bufpos;
1142         udev_device->envp_uptodate = 1;
1143         dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n", i, bufpos);
1144         return 0;
1145 }
1146
1147 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1148 {
1149         if (!udev_device->envp_uptodate)
1150                 if (update_envp_monitor_buf(udev_device) != 0)
1151                         return NULL;
1152         return udev_device->envp;
1153 }
1154
1155 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1156 {
1157         if (!udev_device->envp_uptodate)
1158                 if (update_envp_monitor_buf(udev_device) != 0)
1159                         return -EINVAL;
1160         *buf = udev_device->monitor_buf;
1161         return udev_device->monitor_buf_len;
1162 }
1163
1164 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1165 {
1166         free(udev_device->action);
1167         udev_device->action = strdup(action);
1168         if (udev_device->action == NULL)
1169                 return -ENOMEM;
1170         udev_device_add_property(udev_device, "ACTION", udev_device->action);
1171         return 0;
1172 }
1173
1174 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1175 {
1176         free(udev_device->driver);
1177         udev_device->driver = strdup(driver);
1178         if (udev_device->driver == NULL)
1179                 return -ENOMEM;
1180         udev_device->driver_set = 1;
1181         udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1182         return 0;
1183 }
1184
1185 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1186 {
1187         return udev_device->devpath_old;
1188 }
1189
1190 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1191 {
1192         udev_device->devpath_old = strdup(devpath_old);
1193         if (udev_device->devpath_old == NULL)
1194                 return -ENOMEM;
1195         udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1196         return 0;
1197 }
1198
1199 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1200 {
1201         return udev_device->physdevpath;
1202 }
1203
1204 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1205 {
1206         udev_device->physdevpath = strdup(physdevpath);
1207         if (udev_device->physdevpath == NULL)
1208                 return -ENOMEM;
1209         return 0;
1210 }
1211
1212 int udev_device_get_timeout(struct udev_device *udev_device)
1213 {
1214         return udev_device->timeout;
1215 }
1216
1217 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1218 {
1219         udev_device->timeout = timeout;
1220         return 0;
1221 }
1222 int udev_device_get_event_timeout(struct udev_device *udev_device)
1223 {
1224         if (!udev_device->info_loaded)
1225                 device_load_info(udev_device);
1226         return udev_device->event_timeout;
1227 }
1228
1229 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1230 {
1231         udev_device->event_timeout = event_timeout;
1232         return 0;
1233 }
1234
1235 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1236 {
1237         char num[32];
1238
1239         udev_device->seqnum = seqnum;
1240         snprintf(num, sizeof(num), "%llu", seqnum);
1241         udev_device_add_property(udev_device, "SEQNUM", num);
1242         return 0;
1243 }
1244
1245 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1246 {
1247         char num[32];
1248
1249         udev_device->devnum = devnum;
1250
1251         snprintf(num, sizeof(num), "%u", major(devnum));
1252         udev_device_add_property(udev_device, "MAJOR", num);
1253         snprintf(num, sizeof(num), "%u", minor(devnum));
1254         udev_device_add_property(udev_device, "MINOR", num);
1255         return 0;
1256 }
1257
1258 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1259 {
1260         if (!udev_device->info_loaded)
1261                 device_load_info(udev_device);
1262         return udev_device->num_fake_partitions;
1263 }
1264
1265 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1266 {
1267         udev_device->num_fake_partitions = num;
1268         return 0;
1269 }
1270
1271 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1272 {
1273         if (!udev_device->info_loaded)
1274                 device_load_info(udev_device);
1275         return udev_device->devlink_priority;
1276 }
1277
1278 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1279 {
1280          udev_device->devlink_priority = prio;
1281         return 0;
1282 }
1283
1284 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1285 {
1286         if (!udev_device->info_loaded)
1287                 device_load_info(udev_device);
1288         return udev_device->ignore_remove;
1289 }
1290
1291 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1292 {
1293         udev_device->ignore_remove = ignore;
1294         return 0;
1295 }