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