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