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