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