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