chiark / gitweb /
libudev: udev_device_get_sysattr_value() return syspath of custom links
[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_core_link_value(udev_device->udev, "driver", 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_core_link_value(udev_device->udev, "subsystem", 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, "USEC_INITIALIZED=", 19) == 0) {
441                 udev_device_set_usec_initialized(udev_device, strtoull(&property[19], NULL, 10));
442         } else if (strncmp(property, "DRIVER=", 7) == 0) {
443                 udev_device_set_driver(udev_device, &property[7]);
444         } else if (strncmp(property, "ACTION=", 7) == 0) {
445                 udev_device_set_action(udev_device, &property[7]);
446         } else if (strncmp(property, "MAJOR=", 6) == 0) {
447                 udev_device->maj = strtoull(&property[6], NULL, 10);
448         } else if (strncmp(property, "MINOR=", 6) == 0) {
449                 udev_device->min = strtoull(&property[6], NULL, 10);
450         } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
451                 udev_device_set_devpath_old(udev_device, &property[12]);
452         } else if (strncmp(property, "SEQNUM=", 7) == 0) {
453                 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
454         } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
455                 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
456         } else if (strncmp(property, "IFINDEX=", 8) == 0) {
457                 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
458         } else if (strncmp(property, "DEVMODE=", 8) == 0) {
459                 udev_device_set_devnode_mode(udev_device, strtoul(&property[8], NULL, 8));
460         } else {
461                 udev_device_add_property_from_string(udev_device, property);
462         }
463 }
464
465 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
466 {
467         if (udev_device->maj > 0)
468                 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
469         udev_device->maj = 0;
470         udev_device->min = 0;
471
472         if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
473                 return -EINVAL;
474         return 0;
475 }
476
477 /**
478  * udev_device_get_property_value:
479  * @udev_device: udev device
480  * @key: property name
481  *
482  * Returns: the value of a device property, or #NULL if there is no such property.
483  **/
484 UDEV_EXPORT const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
485 {
486         struct udev_list_entry *list_entry;
487
488         if (udev_device == NULL)
489                 return NULL;
490         if (key == NULL)
491                 return NULL;
492
493         list_entry = udev_device_get_properties_list_entry(udev_device);
494         list_entry = udev_list_entry_get_by_name(list_entry, key);
495         return udev_list_entry_get_value(list_entry);
496 }
497
498 int udev_device_read_db(struct udev_device *udev_device, const char *dbfile)
499 {
500         char filename[UTIL_PATH_SIZE];
501         char line[UTIL_LINE_SIZE];
502         FILE *f;
503
504         /* providing a database file will always force-load it */
505         if (dbfile == NULL) {
506                 const char *id;
507
508                 if (udev_device->db_loaded)
509                         return 0;
510                 udev_device->db_loaded = true;
511
512                 id = udev_device_get_id_filename(udev_device);
513                 if (id == NULL)
514                         return -1;
515                 util_strscpyl(filename, sizeof(filename), udev_get_run_path(udev_device->udev), "/data/", id, NULL);
516                 dbfile = filename;
517         }
518
519         f = fopen(dbfile, "re");
520         if (f == NULL) {
521                 info(udev_device->udev, "no db file to read %s: %m\n", dbfile);
522                 return -1;
523         }
524         udev_device->is_initialized = true;
525
526         while (fgets(line, sizeof(line), f)) {
527                 ssize_t len;
528                 const char *val;
529                 struct udev_list_entry *entry;
530
531                 len = strlen(line);
532                 if (len < 4)
533                         break;
534                 line[len-1] = '\0';
535                 val = &line[2];
536                 switch(line[0]) {
537                 case 'N':
538                         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
539                         udev_device_set_devnode(udev_device, filename);
540                         break;
541                 case 'S':
542                         util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
543                         udev_device_add_devlink(udev_device, filename, 0);
544                         break;
545                 case 'L':
546                         udev_device_set_devlink_priority(udev_device, atoi(val));
547                         break;
548                 case 'E':
549                         entry = udev_device_add_property_from_string(udev_device, val);
550                         udev_list_entry_set_num(entry, true);
551                         break;
552                 case 'G':
553                         udev_device_add_tag(udev_device, val);
554                         break;
555                 case 'W':
556                         udev_device_set_watch_handle(udev_device, atoi(val));
557                         break;
558                 case 'I':
559                         udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
560                         break;
561                 }
562         }
563         fclose(f);
564
565         info(udev_device->udev, "device %p filled with db file data\n", udev_device);
566         return 0;
567 }
568
569 int udev_device_read_uevent_file(struct udev_device *udev_device)
570 {
571         char filename[UTIL_PATH_SIZE];
572         FILE *f;
573         char line[UTIL_LINE_SIZE];
574         int maj = 0;
575         int min = 0;
576
577         if (udev_device->uevent_loaded)
578                 return 0;
579
580         util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
581         f = fopen(filename, "re");
582         if (f == NULL)
583                 return -1;
584         udev_device->uevent_loaded = true;
585
586         while (fgets(line, sizeof(line), f)) {
587                 char *pos;
588
589                 pos = strchr(line, '\n');
590                 if (pos == NULL)
591                         continue;
592                 pos[0] = '\0';
593
594                 if (strncmp(line, "DEVTYPE=", 8) == 0)
595                         udev_device_set_devtype(udev_device, &line[8]);
596                 else if (strncmp(line, "MAJOR=", 6) == 0)
597                         maj = strtoull(&line[6], NULL, 10);
598                 else if (strncmp(line, "MINOR=", 6) == 0)
599                         min = strtoull(&line[6], NULL, 10);
600                 else if (strncmp(line, "IFINDEX=", 8) == 0)
601                         udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
602                 else if (strncmp(line, "DEVNAME=", 8) == 0)
603                         udev_device_set_knodename(udev_device, &line[8]);
604                 else if (strncmp(line, "DEVMODE=", 8) == 0)
605                         udev_device->devnode_mode = strtoul(&line[8], NULL, 8);
606
607                 udev_device_add_property_from_string(udev_device, line);
608         }
609
610         udev_device->devnum = makedev(maj, min);
611         fclose(f);
612         return 0;
613 }
614
615 void udev_device_set_info_loaded(struct udev_device *device)
616 {
617         device->info_loaded = true;
618 }
619
620 struct udev_device *udev_device_new(struct udev *udev)
621 {
622         struct udev_device *udev_device;
623         struct udev_list_entry *list_entry;
624
625         if (udev == NULL)
626                 return NULL;
627
628         udev_device = calloc(1, sizeof(struct udev_device));
629         if (udev_device == NULL)
630                 return NULL;
631         udev_device->refcount = 1;
632         udev_device->udev = udev;
633         udev_list_init(udev, &udev_device->devlinks_list, true);
634         udev_list_init(udev, &udev_device->properties_list, true);
635         udev_list_init(udev, &udev_device->sysattr_value_list, true);
636         udev_list_init(udev, &udev_device->sysattr_list, false);
637         udev_list_init(udev, &udev_device->tags_list, true);
638         udev_device->timeout = -1;
639         udev_device->watch_handle = -1;
640         /* copy global properties */
641         udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
642                 udev_device_add_property(udev_device,
643                                          udev_list_entry_get_name(list_entry),
644                                          udev_list_entry_get_value(list_entry));
645         dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
646         return udev_device;
647 }
648
649 /**
650  * udev_device_new_from_syspath:
651  * @udev: udev library context
652  * @syspath: sys device path including sys directory
653  *
654  * Create new udev device, and fill in information from the sys
655  * device and the udev database entry. The syspath is the absolute
656  * path to the device, including the sys mount point.
657  *
658  * The initial refcount is 1, and needs to be decremented to
659  * release the resources of the udev device.
660  *
661  * Returns: a new udev device, or #NULL, if it does not exist
662  **/
663 UDEV_EXPORT struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
664 {
665         size_t len;
666         const char *subdir;
667         char path[UTIL_PATH_SIZE];
668         char *pos;
669         struct stat statbuf;
670         struct udev_device *udev_device;
671
672         if (udev == NULL)
673                 return NULL;
674         if (syspath == NULL)
675                 return NULL;
676
677         /* path starts in sys */
678         len = strlen(udev_get_sys_path(udev));
679         if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
680                 info(udev, "not in sys :%s\n", syspath);
681                 return NULL;
682         }
683
684         /* path is not a root directory */
685         subdir = &syspath[len+1];
686         pos = strrchr(subdir, '/');
687         if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
688                 dbg(udev, "not a subdir :%s\n", syspath);
689                 return NULL;
690         }
691
692         /* resolve possible symlink to real path */
693         util_strscpy(path, sizeof(path), syspath);
694         util_resolve_sys_link(udev, path, sizeof(path));
695
696         if (strncmp(&path[len], "/devices/", 9) == 0) {
697                 char file[UTIL_PATH_SIZE];
698
699                 /* all "devices" require a "uevent" file */
700                 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
701                 if (stat(file, &statbuf) != 0) {
702                         dbg(udev, "not a device: %s\n", syspath);
703                         return NULL;
704                 }
705         } else {
706                 /* everything else just needs to be a directory */
707                 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
708                         dbg(udev, "directory not found: %s\n", syspath);
709                         return NULL;
710                 }
711         }
712
713         udev_device = udev_device_new(udev);
714         if (udev_device == NULL)
715                 return NULL;
716
717         udev_device_set_syspath(udev_device, path);
718         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
719
720         return udev_device;
721 }
722
723 /**
724  * udev_device_new_from_devnum:
725  * @udev: udev library context
726  * @type: char or block device
727  * @devnum: device major/minor number
728  *
729  * Create new udev device, and fill in information from the sys
730  * device and the udev database entry. The device is looked-up
731  * by its major/minor number and type. Character and block device
732  * numbers are not unique across the two types.
733  *
734  * The initial refcount is 1, and needs to be decremented to
735  * release the resources of the udev device.
736  *
737  * Returns: a new udev device, or #NULL, if it does not exist
738  **/
739 UDEV_EXPORT struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
740 {
741         char path[UTIL_PATH_SIZE];
742         const char *type_str;
743
744         if (type == 'b')
745                 type_str = "block";
746         else if (type == 'c')
747                 type_str = "char";
748         else
749                 return NULL;
750
751         /* use /sys/dev/{block,char}/<maj>:<min> link */
752         snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
753                  udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
754         return udev_device_new_from_syspath(udev, path);
755 }
756
757 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
758 {
759         char type;
760         int maj, min;
761         char subsys[UTIL_PATH_SIZE];
762         char *sysname;
763
764         switch(id[0]) {
765         case 'b':
766         case 'c':
767                 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
768                         return NULL;
769                 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
770         case 'n': {
771                 int sk;
772                 struct ifreq ifr;
773                 struct udev_device *dev;
774                 int ifindex;
775
776                 ifindex = strtoul(&id[1], NULL, 10);
777                 if (ifindex <= 0)
778                         return NULL;
779
780                 sk = socket(PF_INET, SOCK_DGRAM, 0);
781                 if (sk < 0)
782                         return NULL;
783                 memset(&ifr, 0x00, sizeof(struct ifreq));
784                 ifr.ifr_ifindex = ifindex;
785                 if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
786                         close(sk);
787                         return NULL;
788                 }
789                 close(sk);
790
791                 dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
792                 if (dev == NULL)
793                         return NULL;
794                 if (udev_device_get_ifindex(dev) == ifindex)
795                         return dev;
796                 udev_device_unref(dev);
797                 return NULL;
798         }
799         case '+':
800                 util_strscpy(subsys, sizeof(subsys), &id[1]);
801                 sysname = strchr(subsys, ':');
802                 if (sysname == NULL)
803                         return NULL;
804                 sysname[0] = '\0';
805                 sysname = &sysname[1];
806                 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
807         default:
808                 return NULL;
809         }
810 }
811
812 /**
813  * udev_device_new_from_subsystem_sysname:
814  * @udev: udev library context
815  * @subsystem: the subsystem of the device
816  * @sysname: the name of the device
817  *
818  * Create new udev device, and fill in information from the sys device
819  * and the udev database entry. The device is looked up by the subsystem
820  * and name string of the device, like "mem" / "zero", or "block" / "sda".
821  *
822  * The initial refcount is 1, and needs to be decremented to
823  * release the resources of the udev device.
824  *
825  * Returns: a new udev device, or #NULL, if it does not exist
826  **/
827 UDEV_EXPORT struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
828 {
829         char path_full[UTIL_PATH_SIZE];
830         char *path;
831         size_t l;
832         struct stat statbuf;
833
834         path = path_full;
835         l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
836
837         if (strcmp(subsystem, "subsystem") == 0) {
838                 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
839                 if (stat(path_full, &statbuf) == 0)
840                         goto found;
841
842                 util_strscpyl(path, l, "/bus/", sysname, NULL);
843                 if (stat(path_full, &statbuf) == 0)
844                         goto found;
845
846                 util_strscpyl(path, l, "/class/", sysname, NULL);
847                 if (stat(path_full, &statbuf) == 0)
848                         goto found;
849                 goto out;
850         }
851
852         if (strcmp(subsystem, "module") == 0) {
853                 util_strscpyl(path, l, "/module/", sysname, NULL);
854                 if (stat(path_full, &statbuf) == 0)
855                         goto found;
856                 goto out;
857         }
858
859         if (strcmp(subsystem, "drivers") == 0) {
860                 char subsys[UTIL_NAME_SIZE];
861                 char *driver;
862
863                 util_strscpy(subsys, sizeof(subsys), sysname);
864                 driver = strchr(subsys, ':');
865                 if (driver != NULL) {
866                         driver[0] = '\0';
867                         driver = &driver[1];
868
869                         util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
870                         if (stat(path_full, &statbuf) == 0)
871                                 goto found;
872
873                         util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
874                         if (stat(path_full, &statbuf) == 0)
875                                 goto found;
876                 }
877                 goto out;
878         }
879
880         util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
881         if (stat(path_full, &statbuf) == 0)
882                 goto found;
883
884         util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
885         if (stat(path_full, &statbuf) == 0)
886                 goto found;
887
888         util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
889         if (stat(path_full, &statbuf) == 0)
890                 goto found;
891 out:
892         return NULL;
893 found:
894         return udev_device_new_from_syspath(udev, path_full);
895 }
896
897 /**
898  * udev_device_new_from_environment
899  * @udev: udev library context
900  *
901  * Create new udev device, and fill in information from the
902  * current process environment. This only works reliable if
903  * the process is called from a udev rule. It is usually used
904  * for tools executed from IMPORT= rules.
905  *
906  * The initial refcount is 1, and needs to be decremented to
907  * release the resources of the udev device.
908  *
909  * Returns: a new udev device, or #NULL, if it does not exist
910  **/
911 UDEV_EXPORT struct udev_device *udev_device_new_from_environment(struct udev *udev)
912 {
913         int i;
914         struct udev_device *udev_device;
915
916         udev_device = udev_device_new(udev);
917         if (udev_device == NULL)
918                 return NULL;
919         udev_device_set_info_loaded(udev_device);
920
921         for (i = 0; environ[i] != NULL; i++)
922                 udev_device_add_property_from_string_parse(udev_device, environ[i]);
923
924         if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
925                 info(udev, "missing values, invalid device\n");
926                 udev_device_unref(udev_device);
927                 udev_device = NULL;
928         }
929
930         return udev_device;
931 }
932
933 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
934 {
935         struct udev_device *udev_device_parent = NULL;
936         char path[UTIL_PATH_SIZE];
937         const char *subdir;
938
939         util_strscpy(path, sizeof(path), udev_device->syspath);
940         subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
941         for (;;) {
942                 char *pos;
943
944                 pos = strrchr(subdir, '/');
945                 if (pos == NULL || pos < &subdir[2])
946                         break;
947                 pos[0] = '\0';
948                 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
949                 if (udev_device_parent != NULL)
950                         return udev_device_parent;
951         }
952         return NULL;
953 }
954
955 /**
956  * udev_device_get_parent:
957  * @udev_device: the device to start searching from
958  *
959  * Find the next parent device, and fill in information from the sys
960  * device and the udev database entry.
961  *
962  * The returned the device is not referenced. It is attached to the
963  * child device, and will be cleaned up when the child device
964  * is cleaned up.
965  *
966  * It is not necessarily just the upper level directory, empty or not
967  * recognized sys directories are ignored.
968  *
969  * It can be called as many times as needed, without caring about
970  * references.
971  *
972  * Returns: a new udev device, or #NULL, if it no parent exist.
973  **/
974 UDEV_EXPORT struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
975 {
976         if (udev_device == NULL)
977                 return NULL;
978         if (!udev_device->parent_set) {
979                 udev_device->parent_set = true;
980                 udev_device->parent_device = device_new_from_parent(udev_device);
981         }
982         if (udev_device->parent_device != NULL)
983                 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
984         return udev_device->parent_device;
985 }
986
987 /**
988  * udev_device_get_parent_with_subsystem_devtype:
989  * @udev_device: udev device to start searching from
990  * @subsystem: the subsystem of the device
991  * @devtype: the type (DEVTYPE) of the device
992  *
993  * Find the next parent device, with a matching subsystem and devtype
994  * value, and fill in information from the sys device and the udev
995  * database entry.
996  *
997  * If devtype is #NULL, only subsystem is checked, and any devtype will
998  * match.
999  *
1000  * The returned the device is not referenced. It is attached to the
1001  * child device, and will be cleaned up when the child device
1002  * is cleaned up.
1003  *
1004  * It can be called as many times as needed, without caring about
1005  * references.
1006  *
1007  * Returns: a new udev device, or #NULL if no matching parent exists.
1008  **/
1009 UDEV_EXPORT struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
1010 {
1011         struct udev_device *parent;
1012
1013         if (subsystem == NULL)
1014                 return NULL;
1015
1016         parent = udev_device_get_parent(udev_device);
1017         while (parent != NULL) {
1018                 const char *parent_subsystem;
1019                 const char *parent_devtype;
1020
1021                 parent_subsystem = udev_device_get_subsystem(parent);
1022                 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
1023                         if (devtype == NULL)
1024                                 break;
1025                         parent_devtype = udev_device_get_devtype(parent);
1026                         if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
1027                                 break;
1028                 }
1029                 parent = udev_device_get_parent(parent);
1030         }
1031         return parent;
1032 }
1033
1034 /**
1035  * udev_device_get_udev:
1036  * @udev_device: udev device
1037  *
1038  * Retrieve the udev library context the device was created with.
1039  *
1040  * Returns: the udev library context
1041  **/
1042 UDEV_EXPORT struct udev *udev_device_get_udev(struct udev_device *udev_device)
1043 {
1044         if (udev_device == NULL)
1045                 return NULL;
1046         return udev_device->udev;
1047 }
1048
1049 /**
1050  * udev_device_ref:
1051  * @udev_device: udev device
1052  *
1053  * Take a reference of a udev device.
1054  *
1055  * Returns: the passed udev device
1056  **/
1057 UDEV_EXPORT struct udev_device *udev_device_ref(struct udev_device *udev_device)
1058 {
1059         if (udev_device == NULL)
1060                 return NULL;
1061         udev_device->refcount++;
1062         return udev_device;
1063 }
1064
1065 /**
1066  * udev_device_unref:
1067  * @udev_device: udev device
1068  *
1069  * Drop a reference of a udev device. If the refcount reaches zero,
1070  * the resources of the device will be released.
1071  *
1072  **/
1073 UDEV_EXPORT void udev_device_unref(struct udev_device *udev_device)
1074 {
1075         if (udev_device == NULL)
1076                 return;
1077         udev_device->refcount--;
1078         if (udev_device->refcount > 0)
1079                 return;
1080         if (udev_device->parent_device != NULL)
1081                 udev_device_unref(udev_device->parent_device);
1082         free(udev_device->syspath);
1083         free(udev_device->sysname);
1084         free(udev_device->devnode);
1085         free(udev_device->subsystem);
1086         free(udev_device->devtype);
1087         udev_list_cleanup(&udev_device->devlinks_list);
1088         udev_list_cleanup(&udev_device->properties_list);
1089         udev_list_cleanup(&udev_device->sysattr_value_list);
1090         udev_list_cleanup(&udev_device->sysattr_list);
1091         udev_list_cleanup(&udev_device->tags_list);
1092         free(udev_device->action);
1093         free(udev_device->driver);
1094         free(udev_device->devpath_old);
1095         free(udev_device->knodename);
1096         free(udev_device->id_filename);
1097         free(udev_device->envp);
1098         free(udev_device->monitor_buf);
1099         dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
1100         free(udev_device);
1101 }
1102
1103 /**
1104  * udev_device_get_devpath:
1105  * @udev_device: udev device
1106  *
1107  * Retrieve the kernel devpath value of the udev device. The path
1108  * does not contain the sys mount point, and starts with a '/'.
1109  *
1110  * Returns: the devpath of the udev device
1111  **/
1112 UDEV_EXPORT const char *udev_device_get_devpath(struct udev_device *udev_device)
1113 {
1114         if (udev_device == NULL)
1115                 return NULL;
1116         return udev_device->devpath;
1117 }
1118
1119 /**
1120  * udev_device_get_syspath:
1121  * @udev_device: udev device
1122  *
1123  * Retrieve the sys path of the udev device. The path is an
1124  * absolute path and starts with the sys mount point.
1125  *
1126  * Returns: the sys path of the udev device
1127  **/
1128 UDEV_EXPORT const char *udev_device_get_syspath(struct udev_device *udev_device)
1129 {
1130         if (udev_device == NULL)
1131                 return NULL;
1132         return udev_device->syspath;
1133 }
1134
1135 /**
1136  * udev_device_get_sysname:
1137  * @udev_device: udev device
1138  *
1139  * Returns: the sys name of the device device
1140  **/
1141 UDEV_EXPORT const char *udev_device_get_sysname(struct udev_device *udev_device)
1142 {
1143         if (udev_device == NULL)
1144                 return NULL;
1145         return udev_device->sysname;
1146 }
1147
1148 /**
1149  * udev_device_get_sysnum:
1150  * @udev_device: udev device
1151  *
1152  * Returns: the trailing number of of the device name
1153  **/
1154 UDEV_EXPORT const char *udev_device_get_sysnum(struct udev_device *udev_device)
1155 {
1156         if (udev_device == NULL)
1157                 return NULL;
1158         return udev_device->sysnum;
1159 }
1160
1161 /**
1162  * udev_device_get_devnode:
1163  * @udev_device: udev device
1164  *
1165  * Retrieve the device node file name belonging to the udev device.
1166  * The path is an absolute path, and starts with the device directory.
1167  *
1168  * Returns: the device node file name of the udev device, or #NULL if no device node exists
1169  **/
1170 UDEV_EXPORT const char *udev_device_get_devnode(struct udev_device *udev_device)
1171 {
1172         if (udev_device == NULL)
1173                 return NULL;
1174         if (!udev_device->info_loaded) {
1175                 udev_device_read_uevent_file(udev_device);
1176                 udev_device_read_db(udev_device, NULL);
1177         }
1178
1179         /* we might get called before we handled an event and have a db, use the kernel-provided name */
1180         if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
1181                 char filename[UTIL_NAME_SIZE];
1182
1183                 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/",
1184                               udev_device_get_knodename(udev_device), NULL);
1185                 udev_device_set_devnode(udev_device, filename);
1186                 return udev_device->devnode;
1187         }
1188
1189         return udev_device->devnode;
1190 }
1191
1192 /**
1193  * udev_device_get_devlinks_list_entry:
1194  * @udev_device: udev device
1195  *
1196  * Retrieve the list of device links pointing to the device file of
1197  * the udev device. The next list entry can be retrieved with
1198  * udev_list_entry_next(), which returns #NULL if no more entries exist.
1199  * The devlink path can be retrieved from the list entry by
1200  * udev_list_entry_get_name(). The path is an absolute path, and starts with
1201  * the device directory.
1202  *
1203  * Returns: the first entry of the device node link list
1204  **/
1205 UDEV_EXPORT struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
1206 {
1207         if (udev_device == NULL)
1208                 return NULL;
1209         if (!udev_device->info_loaded)
1210                 udev_device_read_db(udev_device, NULL);
1211         return udev_list_get_entry(&udev_device->devlinks_list);
1212 }
1213
1214 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
1215 {
1216         udev_device->devlinks_uptodate = false;
1217         udev_list_cleanup(&udev_device->devlinks_list);
1218 }
1219
1220 /**
1221  * udev_device_get_properties_list_entry:
1222  * @udev_device: udev device
1223  *
1224  * Retrieve the list of key/value device properties of the udev
1225  * device. The next list entry can be retrieved with udev_list_entry_next(),
1226  * which returns #NULL if no more entries exist. The property name
1227  * can be retrieved from the list entry by udev_list_get_name(),
1228  * the property value by udev_list_get_value().
1229  *
1230  * Returns: the first entry of the property list
1231  **/
1232 UDEV_EXPORT struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
1233 {
1234         if (udev_device == NULL)
1235                 return NULL;
1236         if (!udev_device->info_loaded) {
1237                 udev_device_read_uevent_file(udev_device);
1238                 udev_device_read_db(udev_device, NULL);
1239         }
1240         if (!udev_device->devlinks_uptodate) {
1241                 char symlinks[UTIL_PATH_SIZE];
1242                 struct udev_list_entry *list_entry;
1243
1244                 udev_device->devlinks_uptodate = true;
1245                 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1246                 if (list_entry != NULL) {
1247                         char *s;
1248                         size_t l;
1249
1250                         s = symlinks;
1251                         l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1252                         udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1253                                 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1254                         udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1255                 }
1256         }
1257         if (!udev_device->tags_uptodate) {
1258                 udev_device->tags_uptodate = true;
1259                 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1260                         char tags[UTIL_PATH_SIZE];
1261                         struct udev_list_entry *list_entry;
1262                         char *s;
1263                         size_t l;
1264
1265                         s = tags;
1266                         l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1267                         udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1268                                 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1269                         udev_device_add_property(udev_device, "TAGS", tags);
1270                 }
1271         }
1272         return udev_list_get_entry(&udev_device->properties_list);
1273 }
1274
1275 /**
1276  * udev_device_get_action:
1277  * @udev_device: udev device
1278  *
1279  * This is only valid if the device was received through a monitor. Devices read from
1280  * sys do not have an action string. Usual actions are: add, remove, change, online,
1281  * offline.
1282  *
1283  * Returns: the kernel action value, or #NULL if there is no action value available.
1284  **/
1285 UDEV_EXPORT const char *udev_device_get_action(struct udev_device *udev_device)
1286 {
1287         if (udev_device == NULL)
1288                 return NULL;
1289         return udev_device->action;
1290 }
1291
1292 /**
1293  * udev_device_get_usec_since_initialized:
1294  * @udev_device: udev device
1295  *
1296  * Return the number of microseconds passed since udev set up the
1297  * device for the first time.
1298  *
1299  * This is only implemented for devices with need to store properties
1300  * in the udev database. All other devices return 0 here.
1301  *
1302  * Returns: the number of microseconds since the device was first seen.
1303  **/
1304 UDEV_EXPORT unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1305 {
1306         unsigned long long now;
1307
1308         if (udev_device == NULL)
1309                 return 0;
1310         if (!udev_device->info_loaded)
1311                 udev_device_read_db(udev_device, NULL);
1312         if (udev_device->usec_initialized == 0)
1313                 return 0;
1314         now = now_usec();
1315         if (now == 0)
1316                 return 0;
1317         return now - udev_device->usec_initialized;
1318 }
1319
1320 unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device)
1321 {
1322         return udev_device->usec_initialized;
1323 }
1324
1325 void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized)
1326 {
1327         char num[32];
1328
1329         udev_device->usec_initialized = usec_initialized;
1330         snprintf(num, sizeof(num), "%llu", usec_initialized);
1331         udev_device_add_property(udev_device, "USEC_INITIALIZED", num);
1332 }
1333
1334 /**
1335  * udev_device_get_sysattr_value:
1336  * @udev_device: udev device
1337  * @sysattr: attribute name
1338  *
1339  * The retrieved value is cached in the device. Repeated calls will return the same
1340  * value and not open the attribute again.
1341  *
1342  * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1343  **/
1344 UDEV_EXPORT const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1345 {
1346         struct udev_list_entry *list_entry;
1347         char path[UTIL_PATH_SIZE];
1348         char value[4096];
1349         struct stat statbuf;
1350         int fd;
1351         ssize_t size;
1352         const char *val = NULL;
1353
1354         if (udev_device == NULL)
1355                 return NULL;
1356         if (sysattr == NULL)
1357                 return NULL;
1358
1359         /* look for possibly already cached result */
1360         list_entry = udev_list_get_entry(&udev_device->sysattr_value_list);
1361         list_entry = udev_list_entry_get_by_name(list_entry, sysattr);
1362         if (list_entry != NULL) {
1363                 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1364                     sysattr, udev_list_entry_get_value(list_entry));
1365                 return udev_list_entry_get_value(list_entry);
1366         }
1367
1368         util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1369         if (lstat(path, &statbuf) != 0) {
1370                 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1371                 udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, NULL);
1372                 goto out;
1373         }
1374
1375         if (S_ISLNK(statbuf.st_mode)) {
1376                 struct udev_device *dev;
1377
1378                 /*
1379                  * Some core links return only the last element of the target path,
1380                  * these are just values, the paths should not be exposed.
1381                  */
1382                 if (strcmp(sysattr, "driver") == 0 ||
1383                     strcmp(sysattr, "subsystem") == 0 ||
1384                     strcmp(sysattr, "module") == 0) {
1385                         if (util_get_sys_core_link_value(udev_device->udev, sysattr,
1386                                                          udev_device->syspath, value, sizeof(value)) < 0)
1387                                 return NULL;
1388                         dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, value);
1389                         list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1390                         val = udev_list_entry_get_value(list_entry);
1391                         goto out;
1392                 }
1393
1394                 /* resolve link to a device and return its syspath */
1395                 util_strscpyl(path, sizeof(path), udev_device->syspath, "/", sysattr, NULL);
1396                 dev = udev_device_new_from_syspath(udev_device->udev, path);
1397                 if (dev != NULL) {
1398                         list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr,
1399                                                          udev_device_get_syspath(dev));
1400                         val = udev_list_entry_get_value(list_entry);
1401                         udev_device_unref(dev);
1402                 }
1403
1404                 goto out;
1405         }
1406
1407         /* skip directories */
1408         if (S_ISDIR(statbuf.st_mode))
1409                 goto out;
1410
1411         /* skip non-readable files */
1412         if ((statbuf.st_mode & S_IRUSR) == 0)
1413                 goto out;
1414
1415         /* read attribute value */
1416         fd = open(path, O_RDONLY|O_CLOEXEC);
1417         if (fd < 0) {
1418                 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1419                 goto out;
1420         }
1421         size = read(fd, value, sizeof(value));
1422         close(fd);
1423         if (size < 0)
1424                 goto out;
1425         if (size == sizeof(value))
1426                 goto out;
1427
1428         /* got a valid value, store it in cache and return it */
1429         value[size] = '\0';
1430         util_remove_trailing_chars(value, '\n');
1431         dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1432         list_entry = udev_list_entry_add(&udev_device->sysattr_value_list, sysattr, value);
1433         val = udev_list_entry_get_value(list_entry);
1434 out:
1435         return val;
1436 }
1437
1438 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1439 {
1440         struct dirent *dent;
1441         DIR *dir;
1442         int num = 0;
1443
1444         if (udev_device == NULL)
1445                 return -1;
1446         if (udev_device->sysattr_list_read)
1447                 return 0;
1448
1449         dir = opendir(udev_device_get_syspath(udev_device));
1450         if (!dir) {
1451                 dbg(udev_device->udev, "sysfs dir '%s' can not be opened\n",
1452                                 udev_device_get_syspath(udev_device));
1453                 return -1;
1454         }
1455
1456         for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1457                 char path[UTIL_PATH_SIZE];
1458                 struct stat statbuf;
1459
1460                 /* only handle symlinks and regular files */
1461                 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1462                         continue;
1463
1464                 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1465                 if (lstat(path, &statbuf) != 0)
1466                         continue;
1467                 if ((statbuf.st_mode & S_IRUSR) == 0)
1468                         continue;
1469
1470                 udev_list_entry_add(&udev_device->sysattr_list, dent->d_name, NULL);
1471                 num++;
1472         }
1473
1474         closedir(dir);
1475         dbg(udev_device->udev, "found %d sysattrs for '%s'\n", num, udev_device_get_syspath(udev_device));
1476         udev_device->sysattr_list_read = true;
1477
1478         return num;
1479 }
1480
1481 /**
1482  * udev_device_get_sysattr_list_entry:
1483  * @udev_device: udev device
1484  *
1485  * Retrieve the list of available sysattrs, with value being empty;
1486  * This just return all available sysfs attributes for a particular
1487  * device without reading their values.
1488  *
1489  * Returns: the first entry of the property list
1490  **/
1491 UDEV_EXPORT struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1492 {
1493         if (!udev_device->sysattr_list_read) {
1494                 int ret;
1495                 ret = udev_device_sysattr_list_read(udev_device);
1496                 if (0 > ret)
1497                         return NULL;
1498         }
1499
1500         return udev_list_get_entry(&udev_device->sysattr_list);
1501 }
1502
1503 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1504 {
1505         const char *pos;
1506         size_t len;
1507
1508         free(udev_device->syspath);
1509         udev_device->syspath = strdup(syspath);
1510         if (udev_device->syspath ==  NULL)
1511                 return -ENOMEM;
1512         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1513         udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1514
1515         pos = strrchr(udev_device->syspath, '/');
1516         if (pos == NULL)
1517                 return -EINVAL;
1518         udev_device->sysname = strdup(&pos[1]);
1519         if (udev_device->sysname == NULL)
1520                 return -ENOMEM;
1521
1522         /* some devices have '!' in their name, change that to '/' */
1523         len = 0;
1524         while (udev_device->sysname[len] != '\0') {
1525                 if (udev_device->sysname[len] == '!')
1526                         udev_device->sysname[len] = '/';
1527                 len++;
1528         }
1529
1530         /* trailing number */
1531         while (len > 0 && isdigit(udev_device->sysname[--len]))
1532                 udev_device->sysnum = &udev_device->sysname[len];
1533
1534         /* sysname is completely numeric */
1535         if (len == 0)
1536                 udev_device->sysnum = NULL;
1537
1538         return 0;
1539 }
1540
1541 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1542 {
1543         free(udev_device->devnode);
1544         udev_device->devnode = strdup(devnode);
1545         if (udev_device->devnode == NULL)
1546                 return -ENOMEM;
1547         udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1548         return 0;
1549 }
1550
1551 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1552 {
1553         struct udev_list_entry *list_entry;
1554
1555         udev_device->devlinks_uptodate = false;
1556         list_entry = udev_list_entry_add(&udev_device->devlinks_list, devlink, NULL);
1557         if (list_entry == NULL)
1558                 return -ENOMEM;
1559         if (unique)
1560                 udev_list_entry_set_num(list_entry, true);
1561         return 0;
1562 }
1563
1564 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1565 {
1566         if (udev_device->id_filename == NULL) {
1567                 if (udev_device_get_subsystem(udev_device) == NULL)
1568                         return NULL;
1569
1570                 if (major(udev_device_get_devnum(udev_device)) > 0) {
1571                         /* use dev_t -- b259:131072, c254:0 */
1572                         if (asprintf(&udev_device->id_filename, "%c%u:%u",
1573                                      strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1574                                      major(udev_device_get_devnum(udev_device)),
1575                                      minor(udev_device_get_devnum(udev_device))) < 0)
1576                                 udev_device->id_filename = NULL;
1577                 } else if (udev_device_get_ifindex(udev_device) > 0) {
1578                         /* use netdev ifindex -- n3 */
1579                         if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1580                                 udev_device->id_filename = NULL;
1581                 } else {
1582                         /*
1583                          * use $subsys:$syname -- pci:0000:00:1f.2
1584                          * sysname() has '!' translated, get it from devpath
1585                          */
1586                         const char *sysname;
1587                         sysname = strrchr(udev_device->devpath, '/');
1588                         if (sysname == NULL)
1589                                 return NULL;
1590                         sysname = &sysname[1];
1591                         if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1592                                 udev_device->id_filename = NULL;
1593                 }
1594         }
1595         return udev_device->id_filename;
1596 }
1597
1598 /**
1599  * udev_device_get_is_initialized:
1600  * @udev_device: udev device
1601  *
1602  * Check if udev has already handled the device and has set up
1603  * device node permissions and context, or has renamed a network
1604  * device.
1605  *
1606  * This is only implemented for devices with a device node
1607  * or network interfaces. All other devices return 1 here.
1608  *
1609  * Returns: 1 if the device is set up. 0 otherwise.
1610  **/
1611 UDEV_EXPORT int udev_device_get_is_initialized(struct udev_device *udev_device)
1612 {
1613         if (!udev_device->info_loaded)
1614                 udev_device_read_db(udev_device, NULL);
1615         return udev_device->is_initialized;
1616 }
1617
1618 void udev_device_set_is_initialized(struct udev_device *udev_device)
1619 {
1620         udev_device->is_initialized = true;
1621 }
1622
1623 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1624 {
1625         if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1626                 return -EINVAL;
1627         udev_device->tags_uptodate = false;
1628         if (udev_list_entry_add(&udev_device->tags_list, tag, NULL) != NULL)
1629                 return 0;
1630         return -ENOMEM;
1631 }
1632
1633 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1634 {
1635         udev_device->tags_uptodate = false;
1636         udev_list_cleanup(&udev_device->tags_list);
1637 }
1638
1639 /**
1640  * udev_device_get_tags_list_entry:
1641  * @udev_device: udev device
1642  *
1643  * Retrieve the list of tags attached to the udev device. The next
1644  * list entry can be retrieved with udev_list_entry_next(),
1645  * which returns #NULL if no more entries exist. The tag string
1646  * can be retrieved from the list entry by udev_list_get_name().
1647  *
1648  * Returns: the first entry of the tag list
1649  **/
1650 UDEV_EXPORT struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1651 {
1652         if (udev_device == NULL)
1653                 return NULL;
1654         if (!udev_device->info_loaded)
1655                 udev_device_read_db(udev_device, NULL);
1656         return udev_list_get_entry(&udev_device->tags_list);
1657 }
1658
1659 UDEV_EXPORT int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1660 {
1661         struct udev_list_entry *list_entry;
1662
1663         if (udev_device == NULL)
1664                 return false;
1665         if (!udev_device->info_loaded)
1666                 udev_device_read_db(udev_device, NULL);
1667         list_entry = udev_device_get_tags_list_entry(udev_device);
1668         if (udev_list_entry_get_by_name(list_entry, tag) != NULL)
1669                 return true;
1670         return false;
1671 }
1672
1673 #define ENVP_SIZE                       128
1674 #define MONITOR_BUF_SIZE                4096
1675 static int update_envp_monitor_buf(struct udev_device *udev_device)
1676 {
1677         struct udev_list_entry *list_entry;
1678         char *s;
1679         size_t l;
1680         unsigned int i;
1681
1682         /* monitor buffer of property strings */
1683         free(udev_device->monitor_buf);
1684         udev_device->monitor_buf_len = 0;
1685         udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1686         if (udev_device->monitor_buf == NULL)
1687                 return -ENOMEM;
1688
1689         /* envp array, strings will point into monitor buffer */
1690         if (udev_device->envp == NULL)
1691                 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1692         if (udev_device->envp == NULL)
1693                 return -ENOMEM;
1694
1695         i = 0;
1696         s = udev_device->monitor_buf;
1697         l = MONITOR_BUF_SIZE;
1698         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1699                 const char *key;
1700
1701                 key = udev_list_entry_get_name(list_entry);
1702                 /* skip private variables */
1703                 if (key[0] == '.')
1704                         continue;
1705
1706                 /* add string to envp array */
1707                 udev_device->envp[i++] = s;
1708                 if (i+1 >= ENVP_SIZE)
1709                         return -EINVAL;
1710
1711                 /* add property string to monitor buffer */
1712                 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1713                 if (l == 0)
1714                         return -EINVAL;
1715                 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1716                 s++;
1717                 l--;
1718         }
1719         udev_device->envp[i] = NULL;
1720         udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1721         udev_device->envp_uptodate = true;
1722         dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1723             i, udev_device->monitor_buf_len);
1724         return 0;
1725 }
1726
1727 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1728 {
1729         if (!udev_device->envp_uptodate)
1730                 if (update_envp_monitor_buf(udev_device) != 0)
1731                         return NULL;
1732         return udev_device->envp;
1733 }
1734
1735 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1736 {
1737         if (!udev_device->envp_uptodate)
1738                 if (update_envp_monitor_buf(udev_device) != 0)
1739                         return -EINVAL;
1740         *buf = udev_device->monitor_buf;
1741         return udev_device->monitor_buf_len;
1742 }
1743
1744 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1745 {
1746         free(udev_device->action);
1747         udev_device->action = strdup(action);
1748         if (udev_device->action == NULL)
1749                 return -ENOMEM;
1750         udev_device_add_property(udev_device, "ACTION", udev_device->action);
1751         return 0;
1752 }
1753
1754 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1755 {
1756         if (!udev_device->info_loaded)
1757                 udev_device_read_db(udev_device, NULL);
1758         return udev_device->devlink_priority;
1759 }
1760
1761 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1762 {
1763          udev_device->devlink_priority = prio;
1764         return 0;
1765 }
1766
1767 int udev_device_get_watch_handle(struct udev_device *udev_device)
1768 {
1769         if (!udev_device->info_loaded)
1770                 udev_device_read_db(udev_device, NULL);
1771         return udev_device->watch_handle;
1772 }
1773
1774 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1775 {
1776         udev_device->watch_handle = handle;
1777         return 0;
1778 }
1779
1780 bool udev_device_get_db_persist(struct udev_device *udev_device)
1781 {
1782         return udev_device->db_persist;
1783 }
1784
1785 void udev_device_set_db_persist(struct udev_device *udev_device)
1786 {
1787         udev_device->db_persist = true;
1788 }