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