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