chiark / gitweb /
libudev: list - use binary search for list lookup
[elogind.git] / libudev / libudev-device.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008-2010 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 <stdbool.h>
17 #include <errno.h>
18 #include <string.h>
19 #include <dirent.h>
20 #include <fcntl.h>
21 #include <ctype.h>
22 #include <net/if.h>
23 #include <sys/stat.h>
24 #include <sys/ioctl.h>
25 #include <sys/socket.h>
26 #include <linux/sockios.h>
27
28 #include "libudev.h"
29 #include "libudev-private.h"
30
31 /**
32  * SECTION:libudev-device
33  * @short_description: kernel sys devices
34  *
35  * Representation of kernel sys devices. Devices are uniquely identified
36  * by their syspath, every device has exactly one path in the kernel sys
37  * filesystem. Devices usually belong to a kernel subsystem, and and have
38  * a unique name inside that subsystem.
39  */
40
41 /**
42  * udev_device:
43  *
44  * Opaque object representing one kernel sys device.
45  */
46 struct udev_device {
47         struct udev *udev;
48         struct udev_device *parent_device;
49         char *syspath;
50         const char *devpath;
51         char *sysname;
52         const char *sysnum;
53         char *devnode;
54         mode_t devnode_mode;
55         char *subsystem;
56         char *devtype;
57         char *driver;
58         char *action;
59         char *devpath_old;
60         char *knodename;
61         char *id_filename;
62         char **envp;
63         char *monitor_buf;
64         size_t monitor_buf_len;
65         struct udev_list devlinks_list;
66         struct udev_list properties_list;
67         struct udev_list sysattr_value_list;
68         struct udev_list sysattr_list;
69         struct udev_list tags_list;
70         unsigned long long int seqnum;
71         unsigned long long int usec_initialized;
72         int timeout;
73         int devlink_priority;
74         int refcount;
75         dev_t devnum;
76         int ifindex;
77         int watch_handle;
78         int maj, min;
79         bool parent_set;
80         bool subsystem_set;
81         bool devtype_set;
82         bool devlinks_uptodate;
83         bool envp_uptodate;
84         bool tags_uptodate;
85         bool driver_set;
86         bool info_loaded;
87         bool db_loaded;
88         bool uevent_loaded;
89         bool is_initialized;
90         bool sysattr_list_read;
91         bool db_persist;
92 };
93
94 /**
95  * udev_device_get_devnum:
96  * @udev_device: udev device
97  *
98  * This is only valid if the device was received through a monitor. Devices read from
99  * sys do not have a sequence number.
100  *
101  * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
102  **/
103 UDEV_EXPORT unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
104 {
105         if (udev_device == NULL)
106                 return 0;
107         return udev_device->seqnum;
108 }
109
110 static int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
111 {
112         char num[32];
113
114         udev_device->seqnum = seqnum;
115         snprintf(num, sizeof(num), "%llu", seqnum);
116         udev_device_add_property(udev_device, "SEQNUM", num);
117         return 0;
118 }
119
120 int udev_device_get_ifindex(struct udev_device *udev_device)
121 {
122         if (!udev_device->info_loaded)
123                 udev_device_read_uevent_file(udev_device);
124         return udev_device->ifindex;
125 }
126
127 static int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
128 {
129         char num[32];
130
131         udev_device->ifindex = ifindex;
132         snprintf(num, sizeof(num), "%u", ifindex);
133         udev_device_add_property(udev_device, "IFINDEX", num);
134         return 0;
135 }
136
137 /**
138  * udev_device_get_devnum:
139  * @udev_device: udev device
140  *
141  * Returns: the device major/minor number.
142  **/
143 UDEV_EXPORT dev_t udev_device_get_devnum(struct udev_device *udev_device)
144 {
145         if (udev_device == NULL)
146                 return makedev(0, 0);
147         if (!udev_device->info_loaded)
148                 udev_device_read_uevent_file(udev_device);
149         return udev_device->devnum;
150 }
151
152 static int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
153 {
154         char num[32];
155
156         udev_device->devnum = devnum;
157
158         snprintf(num, sizeof(num), "%u", major(devnum));
159         udev_device_add_property(udev_device, "MAJOR", num);
160         snprintf(num, sizeof(num), "%u", minor(devnum));
161         udev_device_add_property(udev_device, "MINOR", num);
162         return 0;
163 }
164
165 int udev_device_get_timeout(struct udev_device *udev_device)
166 {
167         return udev_device->timeout;
168 }
169
170 static int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
171 {
172         char num[32];
173
174         udev_device->timeout = timeout;
175         snprintf(num, sizeof(num), "%u", timeout);
176         udev_device_add_property(udev_device, "TIMEOUT", num);
177         return 0;
178 }
179
180 const char *udev_device_get_knodename(struct udev_device *udev_device)
181 {
182         return udev_device->knodename;
183 }
184
185 static int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
186 {
187         free(udev_device->knodename);
188         udev_device->knodename = strdup(knodename);
189         if (udev_device->knodename == NULL)
190                 return -ENOMEM;
191         /* do not overwrite the udev property with the kernel property */
192         if (udev_device->devnode == NULL)
193                 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
194         return 0;
195 }
196
197 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
198 {
199         return udev_device->devpath_old;
200 }
201
202 static int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
203 {
204         const char *pos;
205
206         free(udev_device->devpath_old);
207         udev_device->devpath_old = strdup(devpath_old);
208         if (udev_device->devpath_old == NULL)
209                 return -ENOMEM;
210         udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
211
212         pos = strrchr(udev_device->devpath_old, '/');
213         if (pos == NULL)
214                 return -EINVAL;
215         return 0;
216 }
217
218 /**
219  * udev_device_get_driver:
220  * @udev_device: udev device
221  *
222  * Returns: the driver string, or #NULL if there is no driver attached.
223  **/
224 UDEV_EXPORT const char *udev_device_get_driver(struct udev_device *udev_device)
225 {
226         char driver[UTIL_NAME_SIZE];
227
228         if (udev_device == NULL)
229                 return NULL;
230         if (!udev_device->driver_set) {
231                 udev_device->driver_set = true;
232                 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
233                         udev_device->driver = strdup(driver);
234         }
235         return udev_device->driver;
236 }
237
238 static int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
239 {
240         free(udev_device->driver);
241         udev_device->driver = strdup(driver);
242         if (udev_device->driver == NULL)
243                 return -ENOMEM;
244         udev_device->driver_set = true;
245         udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
246         return 0;
247 }
248
249 /**
250  * udev_device_get_devtype:
251  * @udev_device: udev device
252  *
253  * Retrieve the devtype string of the udev device.
254  *
255  * Returns: the devtype name of the udev device, or #NULL if it can not be determined
256  **/
257 UDEV_EXPORT const char *udev_device_get_devtype(struct udev_device *udev_device)
258 {
259         if (udev_device == NULL)
260                 return NULL;
261         if (!udev_device->devtype_set) {
262                 udev_device->devtype_set = true;
263                 udev_device_read_uevent_file(udev_device);
264         }
265         return udev_device->devtype;
266 }
267
268 static int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
269 {
270         free(udev_device->devtype);
271         udev_device->devtype = strdup(devtype);
272         if (udev_device->devtype == NULL)
273                 return -ENOMEM;
274         udev_device->devtype_set = true;
275         udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
276         return 0;
277 }
278
279 static int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
280 {
281         free(udev_device->subsystem);
282         udev_device->subsystem = strdup(subsystem);
283         if (udev_device->subsystem == NULL)
284                 return -ENOMEM;
285         udev_device->subsystem_set = true;
286         udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
287         return 0;
288 }
289
290 /**
291  * udev_device_get_subsystem:
292  * @udev_device: udev device
293  *
294  * Retrieve the subsystem string of the udev device. The string does not
295  * contain any "/".
296  *
297  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
298  **/
299 UDEV_EXPORT const char *udev_device_get_subsystem(struct udev_device *udev_device)
300 {
301         char subsystem[UTIL_NAME_SIZE];
302
303         if (udev_device == NULL)
304                 return NULL;
305         if (!udev_device->subsystem_set) {
306                 udev_device->subsystem_set = true;
307                 /* read "subsystem" link */
308                 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
309                         udev_device_set_subsystem(udev_device, subsystem);
310                         return udev_device->subsystem;
311                 }
312                 /* implicit names */
313                 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
314                         udev_device_set_subsystem(udev_device, "module");
315                         return udev_device->subsystem;
316                 }
317                 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
318                         udev_device_set_subsystem(udev_device, "drivers");
319                         return udev_device->subsystem;
320                 }
321                 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
322                     strncmp(udev_device->devpath, "/class/", 7) == 0 ||
323                     strncmp(udev_device->devpath, "/bus/", 5) == 0) {
324                         udev_device_set_subsystem(udev_device, "subsystem");
325                         return udev_device->subsystem;
326                 }
327         }
328         return udev_device->subsystem;
329 }
330
331 mode_t udev_device_get_devnode_mode(struct udev_device *udev_device)
332 {
333         if (!udev_device->info_loaded)
334                 udev_device_read_uevent_file(udev_device);
335         return udev_device->devnode_mode;
336 }
337
338 static int udev_device_set_devnode_mode(struct udev_device *udev_device, mode_t mode)
339 {
340         char num[32];
341
342         udev_device->devnode_mode = mode;
343         snprintf(num, sizeof(num), "%#o", mode);
344         udev_device_add_property(udev_device, "DEVMODE", num);
345         return 0;
346 }
347
348 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
349 {
350         udev_device->envp_uptodate = false;
351         if (value == NULL) {
352                 struct udev_list_entry *list_entry;
353
354                 list_entry = udev_device_get_properties_list_entry(udev_device);
355                 list_entry = udev_list_entry_get_by_name(list_entry, key);
356                 if (list_entry != NULL)
357                         udev_list_entry_delete(list_entry);
358                 return NULL;
359         }
360         return udev_list_entry_add(&udev_device->properties_list, key, value);
361 }
362
363 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
364 {
365         char name[UTIL_LINE_SIZE];
366         char *val;
367
368         util_strscpy(name, sizeof(name), property);
369         val = strchr(name, '=');
370         if (val == NULL)
371                 return NULL;
372         val[0] = '\0';
373         val = &val[1];
374         if (val[0] == '\0')
375                 val = NULL;
376         return udev_device_add_property(udev_device, name, val);
377 }
378
379 /*
380  * parse property string, and if needed, update internal values accordingly
381  *
382  * udev_device_add_property_from_string_parse_finish() needs to be
383  * called after adding properties, and its return value checked
384  *
385  * udev_device_set_info_loaded() needs to be set, to avoid trying
386  * to use a device without a DEVPATH set
387  */
388 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
389 {
390         if (strncmp(property, "DEVPATH=", 8) == 0) {
391                 char path[UTIL_PATH_SIZE];
392
393                 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
394                 udev_device_set_syspath(udev_device, path);
395         } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
396                 udev_device_set_subsystem(udev_device, &property[10]);
397         } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
398                 udev_device_set_devtype(udev_device, &property[8]);
399         } else if (strncmp(property, "DEVNAME=", 8) == 0) {
400                 if (property[8] == '/')
401                         udev_device_set_devnode(udev_device, &property[8]);
402                 else
403                         udev_device_set_knodename(udev_device, &property[8]);
404         } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
405                 char devlinks[UTIL_PATH_SIZE];
406                 char *slink;
407                 char *next;
408
409                 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
410                 slink = devlinks;
411                 next = strchr(slink, ' ');
412                 while (next != NULL) {
413                         next[0] = '\0';
414                         udev_device_add_devlink(udev_device, slink, 0);
415                         slink = &next[1];
416                         next = strchr(slink, ' ');
417                 }
418                 if (slink[0] != '\0')
419                         udev_device_add_devlink(udev_device, slink, 0);
420         } else if (strncmp(property, "TAGS=", 5) == 0) {
421                 char tags[UTIL_PATH_SIZE];
422                 char *next;
423
424                 util_strscpy(tags, sizeof(tags), &property[5]);
425                 next = strchr(tags, ':');
426                 if (next != NULL) {
427                         next++;
428                         while (next[0] != '\0') {
429                                 char *tag;
430
431                                 tag = next;
432                                 next = strchr(tag, ':');
433                                 if (next == NULL)
434                                         break;
435                                 next[0] = '\0';
436                                 next++;
437                                 udev_device_add_tag(udev_device, tag);
438                         }
439                 }
440         } else if (strncmp(property, "DRIVER=", 7) == 0) {
441                 udev_device_set_driver(udev_device, &property[7]);
442         } else if (strncmp(property, "ACTION=", 7) == 0) {
443                 udev_device_set_action(udev_device, &property[7]);
444         } else if (strncmp(property, "MAJOR=", 6) == 0) {
445                 udev_device->maj = strtoull(&property[6], NULL, 10);
446         } else if (strncmp(property, "MINOR=", 6) == 0) {
447                 udev_device->min = strtoull(&property[6], NULL, 10);
448         } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
449                 udev_device_set_devpath_old(udev_device, &property[12]);
450         } else if (strncmp(property, "SEQNUM=", 7) == 0) {
451                 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
452         } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
453                 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
454         } else if (strncmp(property, "IFINDEX=", 8) == 0) {
455                 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
456         } else if (strncmp(property, "DEVMODE=", 8) == 0) {
457                 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
458         } else {
459                 udev_device_add_property_from_string(udev_device, property);
460         }
461 }
462
463 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
464 {
465         if (udev_device->maj > 0)
466                 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
467         udev_device->maj = 0;
468         udev_device->min = 0;
469
470         if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
471                 return -EINVAL;
472         return 0;
473 }
474
475 /**
476  * udev_device_get_property_value:
477  * @udev_device: udev device
478  * @key: property name
479  *
480  * Returns: the value of a device property, or #NULL if there is no such property.
481  **/
482 UDEV_EXPORT const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
483 {
484         struct udev_list_entry *list_entry;
485
486         if (udev_device == NULL)
487                 return NULL;
488         if (key == NULL)
489                 return NULL;
490
491         list_entry = udev_device_get_properties_list_entry(udev_device);
492         list_entry = udev_list_entry_get_by_name(list_entry, key);
493         return udev_list_entry_get_value(list_entry);
494 }
495
496 int udev_device_read_db(struct udev_device *udev_device, const char *dbfile)
497 {
498         char filename[UTIL_PATH_SIZE];
499         char line[UTIL_LINE_SIZE];
500         FILE *f;
501
502         /* providing a database file will always force-load it */
503         if (dbfile == NULL) {
504                 const char *id;
505
506                 if (udev_device->db_loaded)
507                         return 0;
508                 udev_device->db_loaded = true;
509
510                 id = udev_device_get_id_filename(udev_device);
511                 if (id == NULL)
512                         return -1;
513                 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev_device->udev), "/data/", id, NULL);
514                 dbfile = filename;
515         }
516
517         f = fopen(dbfile, "re");
518         if (f == NULL) {
519                 info(udev_device->udev, "no db file to read %s: %m\n", dbfile);
520                 return -1;
521         }
522         udev_device->is_initialized = true;
523
524         while (fgets(line, sizeof(line), f)) {
525                 ssize_t len;
526                 const char *val;
527                 struct udev_list_entry *entry;
528
529                 len = strlen(line);
530                 if (len < 4)
531                         break;
532                 line[len-1] = '\0';
533                 val = &line[2];
534                 switch(line[0]) {
535                 case 'N':
536                         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
537                         udev_device_set_devnode(udev_device, filename);
538                         break;
539                 case 'S':
540                         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
541                         udev_device_add_devlink(udev_device, filename, 0);
542                         break;
543                 case 'L':
544                         udev_device_set_devlink_priority(udev_device, atoi(val));
545                         break;
546                 case 'E':
547                         entry = udev_device_add_property_from_string(udev_device, val);
548                         udev_list_entry_set_num(entry, true);
549                         break;
550                 case 'G':
551                         udev_device_add_tag(udev_device, val);
552                         break;
553                 case 'W':
554                         udev_device_set_watch_handle(udev_device, atoi(val));
555                         break;
556                 case 'I':
557                         udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
558                         break;
559                 }
560         }
561         fclose(f);
562
563         info(udev_device->udev, "device %p filled with db file data\n", udev_device);
564         return 0;
565 }
566
567 int udev_device_read_uevent_file(struct udev_device *udev_device)
568 {
569         char filename[UTIL_PATH_SIZE];
570         FILE *f;
571         char line[UTIL_LINE_SIZE];
572         int maj = 0;
573         int min = 0;
574
575         if (udev_device->uevent_loaded)
576                 return 0;
577
578         util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
579         f = fopen(filename, "re");
580         if (f == NULL)
581                 return -1;
582         udev_device->uevent_loaded = true;
583
584         while (fgets(line, sizeof(line), f)) {
585                 char *pos;
586
587                 pos = strchr(line, '\n');
588                 if (pos == NULL)
589                         continue;
590                 pos[0] = '\0';
591
592                 if (strncmp(line, "DEVTYPE=", 8) == 0)
593                         udev_device_set_devtype(udev_device, &line[8]);
594                 else if (strncmp(line, "MAJOR=", 6) == 0)
595                         maj = strtoull(&line[6], NULL, 10);
596                 else if (strncmp(line, "MINOR=", 6) == 0)
597                         min = strtoull(&line[6], NULL, 10);
598                 else if (strncmp(line, "IFINDEX=", 8) == 0)
599                         udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
600                 else if (strncmp(line, "DEVNAME=", 8) == 0)
601                         udev_device_set_knodename(udev_device, &line[8]);
602                 else if (strncmp(line, "DEVMODE=", 8) == 0)
603                         udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
604
605                 udev_device_add_property_from_string(udev_device, line);
606         }
607
608         udev_device->devnum = makedev(maj, min);
609         fclose(f);
610         return 0;
611 }
612
613 void udev_device_set_info_loaded(struct udev_device *device)
614 {
615         device->info_loaded = true;
616 }
617
618 struct udev_device *udev_device_new(struct udev *udev)
619 {
620         struct udev_device *udev_device;
621         struct udev_list_entry *list_entry;
622
623         if (udev == NULL)
624                 return NULL;
625
626         udev_device = calloc(1, sizeof(struct udev_device));
627         if (udev_device == NULL)
628                 return NULL;
629         udev_device->refcount = 1;
630         udev_device->udev = udev;
631         udev_list_init(udev, &udev_device->devlinks_list, true);
632         udev_list_init(udev, &udev_device->properties_list, true);
633         udev_list_init(udev, &udev_device->sysattr_value_list, true);
634         udev_list_init(udev, &udev_device->sysattr_list, false);
635         udev_list_init(udev, &udev_device->tags_list, true);
636         udev_device->timeout = -1;
637         udev_device->watch_handle = -1;
638         /* copy global properties */
639         udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
640                 udev_device_add_property(udev_device,
641                                          udev_list_entry_get_name(list_entry),
642                                          udev_list_entry_get_value(list_entry));
643         dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
644         return udev_device;
645 }
646
647 /**
648  * udev_device_new_from_syspath:
649  * @udev: udev library context
650  * @syspath: sys device path including sys directory
651  *
652  * Create new udev device, and fill in information from the sys
653  * device and the udev database entry. The syspath is the absolute
654  * path to the device, including the sys mount point.
655  *
656  * The initial refcount is 1, and needs to be decremented to
657  * release the resources of the udev device.
658  *
659  * Returns: a new udev device, or #NULL, if it does not exist
660  **/
661 UDEV_EXPORT struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
662 {
663         size_t len;
664         const char *subdir;
665         char path[UTIL_PATH_SIZE];
666         char *pos;
667         struct stat statbuf;
668         struct udev_device *udev_device;
669
670         if (udev == NULL)
671                 return NULL;
672         if (syspath == NULL)
673                 return NULL;
674
675         /* path starts in sys */
676         len = strlen(udev_get_sys_path(udev));
677         if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
678                 info(udev, "not in sys :%s\n", syspath);
679                 return NULL;
680         }
681
682         /* path is not a root directory */
683         subdir = &syspath[len+1];
684         pos = strrchr(subdir, '/');
685         if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
686                 dbg(udev, "not a subdir :%s\n", syspath);
687                 return NULL;
688         }
689
690         /* resolve possible symlink to real path */
691         util_strscpy(path, sizeof(path), syspath);
692         util_resolve_sys_link(udev, path, sizeof(path));
693
694         if (strncmp(&path[len], "/devices/", 9) == 0) {
695                 char file[UTIL_PATH_SIZE];
696
697                 /* all "devices" require a "uevent" file */
698                 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
699                 if (stat(file, &statbuf) != 0) {
700                         dbg(udev, "not a device: %s\n", syspath);
701                         return NULL;
702                 }
703         } else {
704                 /* everything else just needs to be a directory */
705                 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
706                         dbg(udev, "directory not found: %s\n", syspath);
707                         return NULL;
708                 }
709         }
710
711         udev_device = udev_device_new(udev);
712         if (udev_device == NULL)
713                 return NULL;
714
715         udev_device_set_syspath(udev_device, path);
716         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
717
718         return udev_device;
719 }
720
721 /**
722  * udev_device_new_from_devnum:
723  * @udev: udev library context
724  * @type: char or block device
725  * @devnum: device major/minor number
726  *
727  * Create new udev device, and fill in information from the sys
728  * device and the udev database entry. The device is looked-up
729  * by its major/minor number and type. Character and block device
730  * numbers are not unique across the two types.
731  *
732  * The initial refcount is 1, and needs to be decremented to
733  * release the resources of the udev device.
734  *
735  * Returns: a new udev device, or #NULL, if it does not exist
736  **/
737 UDEV_EXPORT struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
738 {
739         char path[UTIL_PATH_SIZE];
740         const char *type_str;
741
742         if (type == 'b')
743                 type_str = "block";
744         else if (type == 'c')
745                 type_str = "char";
746         else
747                 return NULL;
748
749         /* use /sys/dev/{block,char}/<maj>:<min> link */
750         snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
751                  udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
752         return udev_device_new_from_syspath(udev, path);
753 }
754
755 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
756 {
757         char type;
758         int maj, min;
759         char subsys[UTIL_PATH_SIZE];
760         char *sysname;
761
762         switch(id[0]) {
763         case 'b':
764         case 'c':
765                 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
766                         return NULL;
767                 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
768         case 'n': {
769                 int sk;
770                 struct ifreq ifr;
771                 struct udev_device *dev;
772                 int ifindex;
773
774                 ifindex = strtoul(&id[1], NULL, 10);
775                 if (ifindex <= 0)
776                         return NULL;
777
778                 sk = socket(PF_INET, SOCK_DGRAM, 0);
779                 if (sk < 0)
780                         return NULL;
781                 memset(&ifr, 0x00, sizeof(struct ifreq));
782                 ifr.ifr_ifindex = ifindex;
783                 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
784                         close(sk);
785                         return NULL;
786                 }
787                 close(sk);
788
789                 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
790                 if (dev == NULL)
791                         return NULL;
792                 if (udev_device_get_ifindex(dev) == ifindex)
793                         return dev;
794                 udev_device_unref(dev);
795                 return NULL;
796         }
797         case '+':
798                 util_strscpy(subsys, sizeof(subsys), &id[1]);
799                 sysname = strchr(subsys, ':');
800                 if (sysname == NULL)
801                         return NULL;
802                 sysname[0] = '\0';
803                 sysname = &sysname[1];
804                 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
805         default:
806                 return NULL;
807         }
808 }
809
810 /**
811  * udev_device_new_from_subsystem_sysname:
812  * @udev: udev library context
813  * @subsystem: the subsystem of the device
814  * @sysname: the name of the device
815  *
816  * Create new udev device, and fill in information from the sys device
817  * and the udev database entry. The device is looked up by the subsystem
818  * and name string of the device, like "mem" / "zero", or "block" / "sda".
819  *
820  * The initial refcount is 1, and needs to be decremented to
821  * release the resources of the udev device.
822  *
823  * Returns: a new udev device, or #NULL, if it does not exist
824  **/
825 UDEV_EXPORT struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
826 {
827         char path_full[UTIL_PATH_SIZE];
828         char *path;
829         size_t l;
830         struct stat statbuf;
831
832         path = path_full;
833         l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
834
835         if (strcmp(subsystem, "subsystem") == 0) {
836                 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
837                 if (stat(path_full, &statbuf) == 0)
838                         goto found;
839
840                 util_strscpyl(path, l, "/bus/", sysname, NULL);
841                 if (stat(path_full, &statbuf) == 0)
842                         goto found;
843
844                 util_strscpyl(path, l, "/class/", sysname, NULL);
845                 if (stat(path_full, &statbuf) == 0)
846                         goto found;
847                 goto out;
848         }
849
850         if (strcmp(subsystem, "module") == 0) {
851                 util_strscpyl(path, l, "/module/", sysname, NULL);
852                 if (stat(path_full, &statbuf) == 0)
853                         goto found;
854                 goto out;
855         }
856
857         if (strcmp(subsystem, "drivers") == 0) {
858                 char subsys[UTIL_NAME_SIZE];
859                 char *driver;
860
861                 util_strscpy(subsys, sizeof(subsys), sysname);
862                 driver = strchr(subsys, ':');
863                 if (driver != NULL) {
864                         driver[0] = '\0';
865                         driver = &driver[1];
866
867                         util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
868                         if (stat(path_full, &statbuf) == 0)
869                                 goto found;
870
871                         util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
872                         if (stat(path_full, &statbuf) == 0)
873                                 goto found;
874                 }
875                 goto out;
876         }
877
878         util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
879         if (stat(path_full, &statbuf) == 0)
880                 goto found;
881
882         util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
883         if (stat(path_full, &statbuf) == 0)
884                 goto found;
885
886         util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
887         if (stat(path_full, &statbuf) == 0)
888                 goto found;
889 out:
890         return NULL;
891 found:
892         return udev_device_new_from_syspath(udev, path_full);
893 }
894
895 /**
896  * udev_device_new_from_environment
897  * @udev: udev library context
898  *
899  * Create new udev device, and fill in information from the
900  * current process environment. This only works reliable if
901  * the process is called from a udev rule. It is usually used
902  * for tools executed from IMPORT= rules.
903  *
904  * The initial refcount is 1, and needs to be decremented to
905  * release the resources of the udev device.
906  *
907  * Returns: a new udev device, or #NULL, if it does not exist
908  **/
909 UDEV_EXPORT struct udev_device *udev_device_new_from_environment(struct udev *udev)
910 {
911         int i;
912         struct udev_device *udev_device;
913
914         udev_device = udev_device_new(udev);
915         if (udev_device == NULL)
916                 return NULL;
917         udev_device_set_info_loaded(udev_device);
918
919         for (i = 0; environ[i] != NULL; i++)
920                 udev_device_add_property_from_string_parse(udev_device, environ[i]);
921
922         if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
923                 info(udev, "missing values, invalid device\n");
924                 udev_device_unref(udev_device);
925                 udev_device = NULL;
926         }
927
928         return udev_device;
929 }
930
931 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
932 {
933         struct udev_device *udev_device_parent = NULL;
934         char path[UTIL_PATH_SIZE];
935         const char *subdir;
936
937         util_strscpy(path, sizeof(path), udev_device->syspath);
938         subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
939         for (;;) {
940                 char *pos;
941
942                 pos = strrchr(subdir, '/');
943                 if (pos == NULL || pos < &subdir[2])
944                         break;
945                 pos[0] = '\0';
946                 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
947                 if (udev_device_parent != NULL)
948                         return udev_device_parent;
949         }
950         return NULL;
951 }
952
953 /**
954  * udev_device_get_parent:
955  * @udev_device: the device to start searching from
956  *
957  * Find the next parent device, and fill in information from the sys
958  * device and the udev database entry.
959  *
960  * The returned the device is not referenced. It is attached to the
961  * child device, and will be cleaned up when the child device
962  * is cleaned up.
963  *
964  * It is not necessarily just the upper level directory, empty or not
965  * recognized sys directories are ignored.
966  *
967  * It can be called as many times as needed, without caring about
968  * references.
969  *
970  * Returns: a new udev device, or #NULL, if it no parent exist.
971  **/
972 UDEV_EXPORT struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
973 {
974         if (udev_device == NULL)
975                 return NULL;
976         if (!udev_device->parent_set) {
977                 udev_device->parent_set = true;
978                 udev_device->parent_device = device_new_from_parent(udev_device);
979         }
980         if (udev_device->parent_device != NULL)
981                 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
982         return udev_device->parent_device;
983 }
984
985 /**
986  * udev_device_get_parent_with_subsystem_devtype:
987  * @udev_device: udev device to start searching from
988  * @subsystem: the subsystem of the device
989  * @devtype: the type (DEVTYPE) of the device
990  *
991  * Find the next parent device, with a matching subsystem and devtype
992  * value, and fill in information from the sys device and the udev
993  * database entry.
994  *
995  * If devtype is #NULL, only subsystem is checked, and any devtype will
996  * match.
997  *
998  * The returned the device is not referenced. It is attached to the
999  * child device, and will be cleaned up when the child device
1000  * is cleaned up.
1001  *
1002  * It can be called as many times as needed, without caring about
1003  * references.
1004  *
1005  * Returns: a new udev device, or #NULL if no matching parent exists.
1006  **/
1007 UDEV_EXPORT struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
1008 {
1009         struct udev_device *parent;
1010
1011         if (subsystem == NULL)
1012                 return NULL;
1013
1014         parent = udev_device_get_parent(udev_device);
1015         while (parent != NULL) {
1016                 const char *parent_subsystem;
1017                 const char *parent_devtype;
1018
1019                 parent_subsystem = udev_device_get_subsystem(parent);
1020                 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
1021                         if (devtype == NULL)
1022                                 break;
1023                         parent_devtype = udev_device_get_devtype(parent);
1024                         if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
1025                                 break;
1026                 }
1027                 parent = udev_device_get_parent(parent);
1028         }
1029         return parent;
1030 }
1031
1032 /**
1033  * udev_device_get_udev:
1034  * @udev_device: udev device
1035  *
1036  * Retrieve the udev library context the device was created with.
1037  *
1038  * Returns: the udev library context
1039  **/
1040 UDEV_EXPORT struct udev *udev_device_get_udev(struct udev_device *udev_device)
1041 {
1042         if (udev_device == NULL)
1043                 return NULL;
1044         return udev_device->udev;
1045 }
1046
1047 /**
1048  * udev_device_ref:
1049  * @udev_device: udev device
1050  *
1051  * Take a reference of a udev device.
1052  *
1053  * Returns: the passed udev device
1054  **/
1055 UDEV_EXPORT struct udev_device *udev_device_ref(struct udev_device *udev_device)
1056 {
1057         if (udev_device == NULL)
1058                 return NULL;
1059         udev_device->refcount++;
1060         return udev_device;
1061 }
1062
1063 /**
1064  * udev_device_unref:
1065  * @udev_device: udev device
1066  *
1067  * Drop a reference of a udev device. If the refcount reaches zero,
1068  * the resources of the device will be released.
1069  *
1070  **/
1071 UDEV_EXPORT void udev_device_unref(struct udev_device *udev_device)
1072 {
1073         if (udev_device == NULL)
1074                 return;
1075         udev_device->refcount--;
1076         if (udev_device->refcount > 0)
1077                 return;
1078         if (udev_device->parent_device != NULL)
1079                 udev_device_unref(udev_device->parent_device);
1080         free(udev_device->syspath);
1081         free(udev_device->sysname);
1082         free(udev_device->devnode);
1083         free(udev_device->subsystem);
1084         free(udev_device->devtype);
1085         udev_list_cleanup(&udev_device->devlinks_list);
1086         udev_list_cleanup(&udev_device->properties_list);
1087         udev_list_cleanup(&udev_device->sysattr_value_list);
1088         udev_list_cleanup(&udev_device->sysattr_list);
1089         udev_list_cleanup(&udev_device->tags_list);
1090         free(udev_device->action);
1091         free(udev_device->driver);
1092         free(udev_device->devpath_old);
1093         free(udev_device->knodename);
1094         free(udev_device->id_filename);
1095         free(udev_device->envp);
1096         free(udev_device->monitor_buf);
1097         dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
1098         free(udev_device);
1099 }
1100
1101 /**
1102  * udev_device_get_devpath:
1103  * @udev_device: udev device
1104  *
1105  * Retrieve the kernel devpath value of the udev device. The path
1106  * does not contain the sys mount point, and starts with a '/'.
1107  *
1108  * Returns: the devpath of the udev device
1109  **/
1110 UDEV_EXPORT const char *udev_device_get_devpath(struct udev_device *udev_device)
1111 {
1112         if (udev_device == NULL)
1113                 return NULL;
1114         return udev_device->devpath;
1115 }
1116
1117 /**
1118  * udev_device_get_syspath:
1119  * @udev_device: udev device
1120  *
1121  * Retrieve the sys path of the udev device. The path is an
1122  * absolute path and starts with the sys mount point.
1123  *
1124  * Returns: the sys path of the udev device
1125  **/
1126 UDEV_EXPORT const char *udev_device_get_syspath(struct udev_device *udev_device)
1127 {
1128         if (udev_device == NULL)
1129                 return NULL;
1130         return udev_device->syspath;
1131 }
1132
1133 /**
1134  * udev_device_get_sysname:
1135  * @udev_device: udev device
1136  *
1137  * Returns: the sys name of the device device
1138  **/
1139 UDEV_EXPORT const char *udev_device_get_sysname(struct udev_device *udev_device)
1140 {
1141         if (udev_device == NULL)
1142                 return NULL;
1143         return udev_device->sysname;
1144 }
1145
1146 /**
1147  * udev_device_get_sysnum:
1148  * @udev_device: udev device
1149  *
1150  * Returns: the trailing number of of the device name
1151  **/
1152 UDEV_EXPORT const char *udev_device_get_sysnum(struct udev_device *udev_device)
1153 {
1154         if (udev_device == NULL)
1155                 return NULL;
1156         return udev_device->sysnum;
1157 }
1158
1159 /**
1160  * udev_device_get_devnode:
1161  * @udev_device: udev device
1162  *
1163  * Retrieve the device node file name belonging to the udev device.
1164  * The path is an absolute path, and starts with the device directory.
1165  *
1166  * Returns: the device node file name of the udev device, or #NULL if no device node exists
1167  **/
1168 UDEV_EXPORT const char *udev_device_get_devnode(struct udev_device *udev_device)
1169 {
1170         if (udev_device == NULL)
1171                 return NULL;
1172         if (!udev_device->info_loaded) {
1173                 udev_device_read_uevent_file(udev_device);
1174                 udev_device_read_db(udev_device, NULL);
1175         }
1176
1177         /* we might get called before we handled an event and have a db, use the kernel-provided name */
1178         if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
1179                 char filename[UTIL_NAME_SIZE];
1180
1181                 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/",
1182                               udev_device_get_knodename(udev_device), NULL);
1183                 udev_device_set_devnode(udev_device, filename);
1184                 return udev_device->devnode;
1185         }
1186
1187         return udev_device->devnode;
1188 }
1189
1190 /**
1191  * udev_device_get_devlinks_list_entry:
1192  * @udev_device: udev device
1193  *
1194  * Retrieve the list of device links pointing to the device file of
1195  * the udev device. The next list entry can be retrieved with
1196  * udev_list_entry_next(), which returns #NULL if no more entries exist.
1197  * The devlink path can be retrieved from the list entry by
1198  * udev_list_entry_get_name(). The path is an absolute path, and starts with
1199  * the device directory.
1200  *
1201  * Returns: the first entry of the device node link list
1202  **/
1203 UDEV_EXPORT struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1204 {
1205         if (udev_device == NULL)
1206                 return NULL;
1207         if (!udev_device->info_loaded)
1208                 udev_device_read_db(udev_device, NULL);
1209         return udev_list_get_entry(&udev_device->devlinks_list);
1210 }
1211
1212 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1213 {
1214         udev_device->devlinks_uptodate = false;
1215         udev_list_cleanup(&udev_device->devlinks_list);
1216 }
1217
1218 /**
1219  * udev_device_get_properties_list_entry:
1220  * @udev_device: udev device
1221  *
1222  * Retrieve the list of key/value device properties of the udev
1223  * device. The next list entry can be retrieved with udev_list_entry_next(),
1224  * which returns #NULL if no more entries exist. The property name
1225  * can be retrieved from the list entry by udev_list_get_name(),
1226  * the property value by udev_list_get_value().
1227  *
1228  * Returns: the first entry of the property list
1229  **/
1230 UDEV_EXPORT struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1231 {
1232         if (udev_device == NULL)
1233                 return NULL;
1234         if (!udev_device->info_loaded) {
1235                 udev_device_read_uevent_file(udev_device);
1236                 udev_device_read_db(udev_device, NULL);
1237         }
1238         if (!udev_device->devlinks_uptodate) {
1239                 char symlinks[UTIL_PATH_SIZE];
1240                 struct udev_list_entry *list_entry;
1241
1242                 udev_device->devlinks_uptodate = true;
1243                 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1244                 if (list_entry != NULL) {
1245                         char *s;
1246                         size_t l;
1247
1248                         s = symlinks;
1249                         l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1250                         udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1251                                 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1252                         udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1253                 }
1254         }
1255         if (!udev_device->tags_uptodate) {
1256                 udev_device->tags_uptodate = true;
1257                 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1258                         char tags[UTIL_PATH_SIZE];
1259                         struct udev_list_entry *list_entry;
1260                         char *s;
1261                         size_t l;
1262
1263                         s = tags;
1264                         l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1265                         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1266                                 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1267                         udev_device_add_property(udev_device, "TAGS", tags);
1268                 }
1269         }
1270         return udev_list_get_entry(&udev_device->properties_list);
1271 }
1272
1273 /**
1274  * udev_device_get_action:
1275  * @udev_device: udev device
1276  *
1277  * This is only valid if the device was received through a monitor. Devices read from
1278  * sys do not have an action string. Usual actions are: add, remove, change, online,
1279  * offline.
1280  *
1281  * Returns: the kernel action value, or #NULL if there is no action value available.
1282  **/
1283 UDEV_EXPORT const char *udev_device_get_action(struct udev_device *udev_device)
1284 {
1285         if (udev_device == NULL)
1286                 return NULL;
1287         return udev_device->action;
1288 }
1289
1290 /**
1291  * udev_device_get_usec_since_initialized:
1292  * @udev_device: udev device
1293  *
1294  * Return the number of microseconds passed since udev set up the
1295  * device for the first time.
1296  *
1297  * This is only implemented for devices with need to store properties
1298  * in the udev database. All other devices return 0 here.
1299  *
1300  * Returns: the number of microseconds since the device was first seen.
1301  **/
1302 UDEV_EXPORT unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1303 {
1304         unsigned long long now;
1305
1306         if (udev_device == NULL)
1307                 return 0;
1308         if (!udev_device->info_loaded)
1309                 udev_device_read_db(udev_device, NULL);
1310         if (udev_device->usec_initialized == 0)
1311                 return 0;
1312         now = now_usec();
1313         if (now == 0)
1314                 return 0;
1315         return now - udev_device->usec_initialized;
1316 }
1317
1318 unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device)
1319 {
1320         return udev_device->usec_initialized;
1321 }
1322
1323 void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized)
1324 {
1325         udev_device->usec_initialized = usec_initialized;
1326 }
1327
1328 /**
1329  * udev_device_get_sysattr_value:
1330  * @udev_device: udev device
1331  * @sysattr: attribute name
1332  *
1333  * The retrieved value is cached in the device. Repeated calls will return the same
1334  * value and not open the attribute again.
1335  *
1336  * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1337  **/
1338 UDEV_EXPORT const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1339 {
1340         struct udev_list_entry *list_entry;
1341         char path[UTIL_PATH_SIZE];
1342         char value[4096];
1343         struct stat statbuf;
1344         int fd;
1345         ssize_t size;
1346         const char *val = NULL;
1347
1348         if (udev_device == NULL)
1349                 return NULL;
1350         if (sysattr == NULL)
1351                 return NULL;
1352
1353         /* look for possibly already cached result */
1354         list_entry = udev_list_get_entry(&udev_device->sysattr_value_list);
1355         list_entry = udev_list_entry_get_by_name(list_entry, sysattr);
1356         if (list_entry != NULL) {
1357                 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1358                     sysattr, udev_list_entry_get_value(list_entry));
1359                 return udev_list_entry_get_value(list_entry);
1360         }
1361
1362         util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1363         if (lstat(path, &statbuf) != 0) {
1364                 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1365                 udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL);
1366                 goto out;
1367         }
1368
1369         if (S_ISLNK(statbuf.st_mode)) {
1370                 char target[UTIL_NAME_SIZE];
1371                 int len;
1372                 char *pos;
1373
1374                 /* some core links return the last element of the target path */
1375                 if (strcmp(sysattr, "driver") != 0 &&
1376                     strcmp(sysattr, "subsystem") != 0 &&
1377                     strcmp(sysattr, "module") != 0)
1378                         goto out;
1379
1380                 len = readlink(path, target, sizeof(target));
1381                 if (len <= 0 || len == sizeof(target))
1382                         goto out;
1383                 target[len] = '\0';
1384
1385                 pos = strrchr(target, '/');
1386                 if (pos != NULL) {
1387                         pos = &pos[1];
1388                         dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1389                         list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, pos);
1390                         val = udev_list_entry_get_value(list_entry);
1391                 }
1392
1393                 goto out;
1394         }
1395
1396         /* skip directories */
1397         if (S_ISDIR(statbuf.st_mode))
1398                 goto out;
1399
1400         /* skip non-readable files */
1401         if ((statbuf.st_mode & S_IRUSR) == 0)
1402                 goto out;
1403
1404         /* read attribute value */
1405         fd = open(path, O_RDONLY|O_CLOEXEC);
1406         if (fd < 0) {
1407                 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1408                 goto out;
1409         }
1410         size = read(fd, value, sizeof(value));
1411         close(fd);
1412         if (size < 0)
1413                 goto out;
1414         if (size == sizeof(value))
1415                 goto out;
1416
1417         /* got a valid value, store it in cache and return it */
1418         value[size] = '\0';
1419         util_remove_trailing_chars(value, '\n');
1420         dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1421         list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1422         val = udev_list_entry_get_value(list_entry);
1423 out:
1424         return val;
1425 }
1426
1427 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1428 {
1429         struct dirent *dent;
1430         DIR *dir;
1431         int num = 0;
1432
1433         if (udev_device == NULL)
1434                 return -1;
1435         if (udev_device->sysattr_list_read)
1436                 return 0;
1437
1438         dir = opendir(udev_device_get_syspath(udev_device));
1439         if (!dir) {
1440                 dbg(udev_device->udev, "sysfs dir '%s' can not be opened\n",
1441                                 udev_device_get_syspath(udev_device));
1442                 return -1;
1443         }
1444
1445         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1446                 char path[UTIL_PATH_SIZE];
1447                 struct stat statbuf;
1448
1449                 /* only handle symlinks and regular files */
1450                 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1451                         continue;
1452
1453                 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1454                 if (lstat(path, &statbuf) != 0)
1455                         continue;
1456                 if ((statbuf.st_mode & S_IRUSR) == 0)
1457                         continue;
1458
1459                 udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL);
1460                 num++;
1461         }
1462
1463         closedir(dir);
1464         dbg(udev_device->udev, "found %d sysattrs for '%s'\n", num, udev_device_get_syspath(udev_device));
1465         udev_device->sysattr_list_read = true;
1466
1467         return num;
1468 }
1469
1470 /**
1471  * udev_device_get_sysattr_list_entry:
1472  * @udev_device: udev device
1473  *
1474  * Retrieve the list of available sysattrs, with value being empty;
1475  * This just return all available sysfs attributes for a particular
1476  * device without reading their values.
1477  *
1478  * Returns: the first entry of the property list
1479  **/
1480 UDEV_EXPORT struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1481 {
1482         if (!udev_device->sysattr_list_read) {
1483                 int ret;
1484                 ret = udev_device_sysattr_list_read(udev_device);
1485                 if (0 > ret)
1486                         return NULL;
1487         }
1488
1489         return udev_list_get_entry(&udev_device->sysattr_list);
1490 }
1491
1492 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1493 {
1494         const char *pos;
1495         size_t len;
1496
1497         free(udev_device->syspath);
1498         udev_device->syspath = strdup(syspath);
1499         if (udev_device->syspath ==  NULL)
1500                 return -ENOMEM;
1501         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1502         udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1503
1504         pos = strrchr(udev_device->syspath, '/');
1505         if (pos == NULL)
1506                 return -EINVAL;
1507         udev_device->sysname = strdup(&pos[1]);
1508         if (udev_device->sysname == NULL)
1509                 return -ENOMEM;
1510
1511         /* some devices have '!' in their name, change that to '/' */
1512         len = 0;
1513         while (udev_device->sysname[len] != '\0') {
1514                 if (udev_device->sysname[len] == '!')
1515                         udev_device->sysname[len] = '/';
1516                 len++;
1517         }
1518
1519         /* trailing number */
1520         while (len > 0 && isdigit(udev_device->sysname[--len]))
1521                 udev_device->sysnum = &udev_device->sysname[len];
1522
1523         /* sysname is completely numeric */
1524         if (len == 0)
1525                 udev_device->sysnum = NULL;
1526
1527         return 0;
1528 }
1529
1530 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1531 {
1532         free(udev_device->devnode);
1533         udev_device->devnode = strdup(devnode);
1534         if (udev_device->devnode == NULL)
1535                 return -ENOMEM;
1536         udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1537         return 0;
1538 }
1539
1540 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1541 {
1542         struct udev_list_entry *list_entry;
1543
1544         udev_device->devlinks_uptodate = false;
1545         list_entry = udev_list_entry_add(&udev_device->devlinks_list, devlink, NULL);
1546         if (list_entry == NULL)
1547                 return -ENOMEM;
1548         if (unique)
1549                 udev_list_entry_set_num(list_entry, true);
1550         return 0;
1551 }
1552
1553 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1554 {
1555         if (udev_device->id_filename == NULL) {
1556                 if (udev_device_get_subsystem(udev_device) == NULL)
1557                         return NULL;
1558
1559                 if (major(udev_device_get_devnum(udev_device)) > 0) {
1560                         /* use dev_t -- b259:131072, c254:0 */
1561                         if (asprintf(&udev_device->id_filename, "%c%u:%u",
1562                                      strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1563                                      major(udev_device_get_devnum(udev_device)),
1564                                      minor(udev_device_get_devnum(udev_device))) < 0)
1565                                 udev_device->id_filename = NULL;
1566                 } else if (udev_device_get_ifindex(udev_device) > 0) {
1567                         /* use netdev ifindex -- n3 */
1568                         if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1569                                 udev_device->id_filename = NULL;
1570                 } else {
1571                         /*
1572                          * use $subsys:$syname -- pci:0000:00:1f.2
1573                          * sysname() has '!' translated, get it from devpath
1574                          */
1575                         const char *sysname;
1576                         sysname = strrchr(udev_device->devpath, '/');
1577                         if (sysname == NULL)
1578                                 return NULL;
1579                         sysname = &sysname[1];
1580                         if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1581                                 udev_device->id_filename = NULL;
1582                 }
1583         }
1584         return udev_device->id_filename;
1585 }
1586
1587 /**
1588  * udev_device_get_is_initialized:
1589  * @udev_device: udev device
1590  *
1591  * Check if udev has already handled the device and has set up
1592  * device node permissions and context, or has renamed a network
1593  * device.
1594  *
1595  * This is only implemented for devices with a device node
1596  * or network interfaces. All other devices return 1 here.
1597  *
1598  * Returns: 1 if the device is set up. 0 otherwise.
1599  **/
1600 UDEV_EXPORT int udev_device_get_is_initialized(struct udev_device *udev_device)
1601 {
1602         if (!udev_device->info_loaded)
1603                 udev_device_read_db(udev_device, NULL);
1604         return udev_device->is_initialized;
1605 }
1606
1607 void udev_device_set_is_initialized(struct udev_device *udev_device)
1608 {
1609         udev_device->is_initialized = true;
1610 }
1611
1612 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1613 {
1614         if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1615                 return -EINVAL;
1616         udev_device->tags_uptodate = false;
1617         if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL)
1618                 return 0;
1619         return -ENOMEM;
1620 }
1621
1622 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1623 {
1624         udev_device->tags_uptodate = false;
1625         udev_list_cleanup(&udev_device->tags_list);
1626 }
1627
1628 /**
1629  * udev_device_get_tags_list_entry:
1630  * @udev_device: udev device
1631  *
1632  * Retrieve the list of tags attached to the udev device. The next
1633  * list entry can be retrieved with udev_list_entry_next(),
1634  * which returns #NULL if no more entries exist. The tag string
1635  * can be retrieved from the list entry by udev_list_get_name().
1636  *
1637  * Returns: the first entry of the tag list
1638  **/
1639 UDEV_EXPORT struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1640 {
1641         if (udev_device == NULL)
1642                 return NULL;
1643         if (!udev_device->info_loaded)
1644                 udev_device_read_db(udev_device, NULL);
1645         return udev_list_get_entry(&udev_device->tags_list);
1646 }
1647
1648 UDEV_EXPORT int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1649 {
1650         struct udev_list_entry *list_entry;
1651
1652         if (udev_device == NULL)
1653                 return false;
1654         if (!udev_device->info_loaded)
1655                 udev_device_read_db(udev_device, NULL);
1656         list_entry = udev_device_get_tags_list_entry(udev_device);
1657         if (udev_list_entry_get_by_name(list_entry, tag) != NULL)
1658                 return true;
1659         return false;
1660 }
1661
1662 #define ENVP_SIZE                       128
1663 #define MONITOR_BUF_SIZE                4096
1664 static int update_envp_monitor_buf(struct udev_device *udev_device)
1665 {
1666         struct udev_list_entry *list_entry;
1667         char *s;
1668         size_t l;
1669         unsigned int i;
1670
1671         /* monitor buffer of property strings */
1672         free(udev_device->monitor_buf);
1673         udev_device->monitor_buf_len = 0;
1674         udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1675         if (udev_device->monitor_buf == NULL)
1676                 return -ENOMEM;
1677
1678         /* envp array, strings will point into monitor buffer */
1679         if (udev_device->envp == NULL)
1680                 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1681         if (udev_device->envp == NULL)
1682                 return -ENOMEM;
1683
1684         i = 0;
1685         s = udev_device->monitor_buf;
1686         l = MONITOR_BUF_SIZE;
1687         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1688                 const char *key;
1689
1690                 key = udev_list_entry_get_name(list_entry);
1691                 /* skip private variables */
1692                 if (key[0] == '.')
1693                         continue;
1694
1695                 /* add string to envp array */
1696                 udev_device->envp[i++] = s;
1697                 if (i+1 >= ENVP_SIZE)
1698                         return -EINVAL;
1699
1700                 /* add property string to monitor buffer */
1701                 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1702                 if (l == 0)
1703                         return -EINVAL;
1704                 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1705                 s++;
1706                 l--;
1707         }
1708         udev_device->envp[i] = NULL;
1709         udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1710         udev_device->envp_uptodate = true;
1711         dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1712             i, udev_device->monitor_buf_len);
1713         return 0;
1714 }
1715
1716 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1717 {
1718         if (!udev_device->envp_uptodate)
1719                 if (update_envp_monitor_buf(udev_device) != 0)
1720                         return NULL;
1721         return udev_device->envp;
1722 }
1723
1724 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1725 {
1726         if (!udev_device->envp_uptodate)
1727                 if (update_envp_monitor_buf(udev_device) != 0)
1728                         return -EINVAL;
1729         *buf = udev_device->monitor_buf;
1730         return udev_device->monitor_buf_len;
1731 }
1732
1733 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1734 {
1735         free(udev_device->action);
1736         udev_device->action = strdup(action);
1737         if (udev_device->action == NULL)
1738                 return -ENOMEM;
1739         udev_device_add_property(udev_device, "ACTION", udev_device->action);
1740         return 0;
1741 }
1742
1743 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1744 {
1745         if (!udev_device->info_loaded)
1746                 udev_device_read_db(udev_device, NULL);
1747         return udev_device->devlink_priority;
1748 }
1749
1750 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1751 {
1752          udev_device->devlink_priority = prio;
1753         return 0;
1754 }
1755
1756 int udev_device_get_watch_handle(struct udev_device *udev_device)
1757 {
1758         if (!udev_device->info_loaded)
1759                 udev_device_read_db(udev_device, NULL);
1760         return udev_device->watch_handle;
1761 }
1762
1763 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1764 {
1765         udev_device->watch_handle = handle;
1766         return 0;
1767 }
1768
1769 bool udev_device_get_db_persist(struct udev_device *udev_device)
1770 {
1771         return udev_device->db_persist;
1772 }
1773
1774 void udev_device_set_db_persist(struct udev_device *udev_device)
1775 {
1776         udev_device->db_persist = true;
1777 }