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