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