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