chiark / gitweb /
5d1ad9f223acb307adc65c9dec6f0af681807a1d
[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  * The returned the device is not referenced. It is attached to the
581  * child device, and will be cleaned up when the child device
582  * is cleaned up.
583  *
584  * It can be called as many times as needed, without caring about
585  * references.
586  *
587  * Returns: a new udev device, or #NULL if no matching parent exists.
588  **/
589 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
590 {
591         struct udev_device *parent;
592
593         if (subsystem == NULL)
594                 return NULL;
595
596         parent = udev_device_get_parent(udev_device);
597         while (parent != NULL) {
598                 const char *parent_subsystem;
599                 const char *parent_devtype;
600
601                 parent_subsystem = udev_device_get_subsystem(parent);
602                 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
603                         if (devtype == NULL)
604                                 break;
605                         parent_devtype = udev_device_get_devtype(parent);
606                         if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
607                                 break;
608                 }
609                 parent = udev_device_get_parent(parent);
610         }
611         return parent;
612 }
613
614 /**
615  * udev_device_get_udev:
616  * @udev_device: udev device
617  *
618  * Retrieve the udev library context the device was created with.
619  *
620  * Returns: the udev library context
621  **/
622 struct udev *udev_device_get_udev(struct udev_device *udev_device)
623 {
624         if (udev_device == NULL)
625                 return NULL;
626         return udev_device->udev;
627 }
628
629 /**
630  * udev_device_ref:
631  * @udev_device: udev device
632  *
633  * Take a reference of a udev device.
634  *
635  * Returns: the passed udev device
636  **/
637 struct udev_device *udev_device_ref(struct udev_device *udev_device)
638 {
639         if (udev_device == NULL)
640                 return NULL;
641         udev_device->refcount++;
642         return udev_device;
643 }
644
645 /**
646  * udev_device_unref:
647  * @udev_device: udev device
648  *
649  * Drop a reference of a udev device. If the refcount reaches zero,
650  * the resources of the device will be released.
651  *
652  **/
653 void udev_device_unref(struct udev_device *udev_device)
654 {
655         if (udev_device == NULL)
656                 return;
657         udev_device->refcount--;
658         if (udev_device->refcount > 0)
659                 return;
660         if (udev_device->parent_device != NULL)
661                 udev_device_unref(udev_device->parent_device);
662         free(udev_device->syspath);
663         free(udev_device->sysname);
664         free(udev_device->devnode);
665         free(udev_device->subsystem);
666         free(udev_device->devtype);
667         udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
668         udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
669         free(udev_device->action);
670         free(udev_device->driver);
671         free(udev_device->devpath_old);
672         free(udev_device->sysname_old);
673         free(udev_device->knodename);
674         udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
675         free(udev_device->envp);
676         free(udev_device->monitor_buf);
677         dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
678         free(udev_device);
679 }
680
681 /**
682  * udev_device_get_devpath:
683  * @udev_device: udev device
684  *
685  * Retrieve the kernel devpath value of the udev device. The path
686  * does not contain the sys mount point, and starts with a '/'.
687  *
688  * Returns: the devpath of the udev device
689  **/
690 const char *udev_device_get_devpath(struct udev_device *udev_device)
691 {
692         if (udev_device == NULL)
693                 return NULL;
694         return udev_device->devpath;
695 }
696
697 /**
698  * udev_device_get_syspath:
699  * @udev_device: udev device
700  *
701  * Retrieve the sys path of the udev device. The path is an
702  * absolute path and starts with the sys mount point.
703  *
704  * Returns: the sys path of the udev device
705  **/
706 const char *udev_device_get_syspath(struct udev_device *udev_device)
707 {
708         if (udev_device == NULL)
709                 return NULL;
710         return udev_device->syspath;
711 }
712
713 /**
714  * udev_device_get_sysname:
715  * @udev_device: udev device
716  *
717  * Returns: the sys name of the device device
718  **/
719 const char *udev_device_get_sysname(struct udev_device *udev_device)
720 {
721         if (udev_device == NULL)
722                 return NULL;
723         return udev_device->sysname;
724 }
725
726 /**
727  * udev_device_get_sysnum:
728  * @udev_device: udev device
729  *
730  * Returns: the trailing number of of the device name
731  **/
732 const char *udev_device_get_sysnum(struct udev_device *udev_device)
733 {
734         if (udev_device == NULL)
735                 return NULL;
736         return udev_device->sysnum;
737 }
738
739 /**
740  * udev_device_get_devnode:
741  * @udev_device: udev device
742  *
743  * Retrieve the device node file name belonging to the udev device.
744  * The path is an absolute path, and starts with the device directory.
745  *
746  * Returns: the device node file name of the udev device, or #NULL if no device node exists
747  **/
748 const char *udev_device_get_devnode(struct udev_device *udev_device)
749 {
750         if (udev_device == NULL)
751                 return NULL;
752         if (!udev_device->info_loaded)
753                 device_load_info(udev_device);
754         return udev_device->devnode;
755 }
756
757 /**
758  * udev_device_get_subsystem:
759  * @udev_device: udev device
760  *
761  * Retrieve the subsystem string of the udev device. The string does not
762  * contain any "/".
763  *
764  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
765  **/
766 const char *udev_device_get_subsystem(struct udev_device *udev_device)
767 {
768         char subsystem[UTIL_NAME_SIZE];
769
770         if (udev_device == NULL)
771                 return NULL;
772         if (!udev_device->subsystem_set) {
773                 udev_device->subsystem_set = 1;
774                 /* read "subsystem" link */
775                 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
776                         udev_device_set_subsystem(udev_device, subsystem);
777                         return udev_device->subsystem;
778                 }
779                 /* implicit names */
780                 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
781                         udev_device_set_subsystem(udev_device, "module");
782                         return udev_device->subsystem;
783                 }
784                 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
785                         udev_device_set_subsystem(udev_device, "drivers");
786                         return udev_device->subsystem;
787                 }
788                 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
789                     strncmp(udev_device->devpath, "/class/", 7) == 0 ||
790                     strncmp(udev_device->devpath, "/bus/", 5) == 0) {
791                         udev_device_set_subsystem(udev_device, "subsystem");
792                         return udev_device->subsystem;
793                 }
794         }
795         return udev_device->subsystem;
796 }
797
798 /**
799  * udev_device_get_devtype:
800  * @udev_device: udev device
801  *
802  * Retrieve the devtype string of the udev device.
803  *
804  * Returns: the devtype name of the udev device, or #NULL if it can not be determined
805  **/
806 const char *udev_device_get_devtype(struct udev_device *udev_device)
807 {
808         if (udev_device == NULL)
809                 return NULL;
810         if (!udev_device->devtype_set) {
811                 udev_device->devtype_set = 1;
812                 if (!udev_device->info_loaded)
813                         udev_device_read_uevent_file(udev_device);
814         }
815         return udev_device->devtype;
816 }
817
818 /**
819  * udev_device_get_devlinks_list_entry:
820  * @udev_device: udev device
821  *
822  * Retrieve the list of device links pointing to the device file of
823  * the udev device. The next list entry can be retrieved with
824  * udev_list_entry_next(), which returns #NULL if no more entries exist.
825  * The devlink path can be retrieved from the list entry by
826  * udev_list_entry_get_name(). The path is an absolute path, and starts with
827  * the device directory.
828  *
829  * Returns: the first entry of the device node link list
830  **/
831 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
832 {
833         if (udev_device == NULL)
834                 return NULL;
835         if (!udev_device->info_loaded)
836                 device_load_info(udev_device);
837         return udev_list_get_entry(&udev_device->devlinks_list);
838 }
839
840 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
841 {
842         udev_device->devlinks_uptodate = 0;
843         udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
844 }
845
846 /**
847  * udev_device_get_properties_list_entry:
848  * @udev_device: udev device
849  *
850  * Retrieve the list of key/value device properties of the udev
851  * device. The next list entry can be retrieved with udev_list_entry_next(),
852  * which returns #NULL if no more entries exist. The property name
853  * can be retrieved from the list entry by udev_list_get_name(),
854  * the property value by udev_list_get_value().
855  *
856  * Returns: the first entry of the property list
857  **/
858 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
859 {
860         if (udev_device == NULL)
861                 return NULL;
862         if (!udev_device->info_loaded)
863                 device_load_info(udev_device);
864         if (!udev_device->devlinks_uptodate) {
865                 char symlinks[UTIL_PATH_SIZE];
866                 struct udev_list_entry *list_entry;
867
868                 udev_device->devlinks_uptodate = 1;
869                 list_entry = udev_device_get_devlinks_list_entry(udev_device);
870                 if (list_entry != NULL) {
871                         char *s;
872                         size_t l;
873
874                         s = symlinks;
875                         l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
876                         udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
877                                 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
878                         udev_device_add_property(udev_device, "DEVLINKS", symlinks);
879                 }
880         }
881         return udev_list_get_entry(&udev_device->properties_list);
882 }
883
884 /**
885  * udev_device_get_driver:
886  * @udev_device: udev device
887  *
888  * Returns: the driver string, or #NULL if there is no driver attached.
889  **/
890 const char *udev_device_get_driver(struct udev_device *udev_device)
891 {
892         char driver[UTIL_NAME_SIZE];
893
894         if (udev_device == NULL)
895                 return NULL;
896         if (!udev_device->driver_set) {
897                 udev_device->driver_set = 1;
898                 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
899                         udev_device->driver = strdup(driver);
900         }
901         return udev_device->driver;
902 }
903
904 /**
905  * udev_device_get_devnum:
906  * @udev_device: udev device
907  *
908  * Returns: the device major/minor number.
909  **/
910 dev_t udev_device_get_devnum(struct udev_device *udev_device)
911 {
912         if (udev_device == NULL)
913                 return makedev(0, 0);
914         if (!udev_device->info_loaded)
915                 device_load_info(udev_device);
916         return udev_device->devnum;
917 }
918
919 /**
920  * udev_device_get_action:
921  * @udev_device: udev device
922  *
923  * This is only valid if the device was received through a monitor. Devices read from
924  * sys do not have an action string. Usual actions are: add, remove, change, online,
925  * offline.
926  *
927  * Returns: the kernel action value, or #NULL if there is no action value available.
928  **/
929 const char *udev_device_get_action(struct udev_device *udev_device)
930 {
931         if (udev_device == NULL)
932                 return NULL;
933         return udev_device->action;
934 }
935
936 /**
937  * udev_device_get_devnum:
938  * @udev_device: udev device
939  *
940  * This is only valid if the device was received through a monitor. Devices read from
941  * sys do not have a sequence number.
942  *
943  * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
944  **/
945 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
946 {
947         if (udev_device == NULL)
948                 return 0;
949         return udev_device->seqnum;
950 }
951
952 /**
953  * udev_device_get_sysattr_value:
954  * @udev_device: udev device
955  * @sysattr: attribute name
956  *
957  * The retrieved value is cached in the device. Repeated calls will return the same
958  * value and not open the attribute again.
959  *
960  * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
961  **/
962 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
963 {
964         struct udev_list_entry *list_entry;
965         char path[UTIL_PATH_SIZE];
966         char value[4096];
967         struct stat statbuf;
968         int fd;
969         ssize_t size;
970         const char *val = NULL;
971
972         if (udev_device == NULL)
973                 return NULL;
974         if (sysattr == NULL)
975                 return NULL;
976
977         /* look for possibly already cached result */
978         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
979                 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
980                         dbg(udev_device->udev, "got '%s' (%s) from cache\n",
981                             sysattr, udev_list_entry_get_value(list_entry));
982                         return udev_list_entry_get_value(list_entry);
983                 }
984         }
985
986         util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
987         if (lstat(path, &statbuf) != 0) {
988                 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
989                 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
990                 goto out;
991         }
992
993         if (S_ISLNK(statbuf.st_mode)) {
994                 char target[UTIL_NAME_SIZE];
995                 int len;
996                 char *pos;
997
998                 /* some core links return the last element of the target path */
999                 if (strcmp(sysattr, "driver") != 0 &&
1000                     strcmp(sysattr, "subsystem") != 0 &&
1001                     strcmp(sysattr, "module") != 0)
1002                         goto out;
1003
1004                 len = readlink(path, target, sizeof(target));
1005                 if (len > 0) {
1006                         target[len] = '\0';
1007                         pos = strrchr(target, '/');
1008                         if (pos != NULL) {
1009                                 pos = &pos[1];
1010                                 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1011                                 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1012                                 val = udev_list_entry_get_value(list_entry);
1013                         }
1014                 }
1015                 goto out;
1016         }
1017
1018         /* skip directories */
1019         if (S_ISDIR(statbuf.st_mode))
1020                 goto out;
1021
1022         /* skip non-readable files */
1023         if ((statbuf.st_mode & S_IRUSR) == 0)
1024                 goto out;
1025
1026         /* read attribute value */
1027         fd = open(path, O_RDONLY);
1028         if (fd < 0) {
1029                 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1030                 goto out;
1031         }
1032         size = read(fd, value, sizeof(value));
1033         close(fd);
1034         if (size < 0)
1035                 goto out;
1036         if (size == sizeof(value))
1037                 goto out;
1038
1039         /* got a valid value, store it in cache and return it */
1040         value[size] = '\0';
1041         util_remove_trailing_chars(value, '\n');
1042         dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1043         list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1044         val = udev_list_entry_get_value(list_entry);
1045 out:
1046         return val;
1047 }
1048
1049 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1050 {
1051         const char *pos;
1052         size_t len;
1053
1054         free(udev_device->syspath);
1055         udev_device->syspath = strdup(syspath);
1056         if (udev_device->syspath ==  NULL)
1057                 return -ENOMEM;
1058         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1059         udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1060
1061         pos = strrchr(udev_device->syspath, '/');
1062         if (pos == NULL)
1063                 return -EINVAL;
1064         udev_device->sysname = strdup(&pos[1]);
1065         if (udev_device->sysname == NULL)
1066                 return -ENOMEM;
1067
1068         /* some devices have '!' in their name, change that to '/' */
1069         len = 0;
1070         while (udev_device->sysname[len] != '\0') {
1071                 if (udev_device->sysname[len] == '!')
1072                         udev_device->sysname[len] = '/';
1073                 len++;
1074         }
1075
1076         /* trailing number */
1077         while (len > 0 && isdigit(udev_device->sysname[--len]))
1078                 udev_device->sysnum = &udev_device->sysname[len];
1079
1080         /* sysname is completely numeric */
1081         if (len == 0)
1082                 udev_device->sysnum = NULL;
1083
1084         return 0;
1085 }
1086
1087 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1088 {
1089         free(udev_device->subsystem);
1090         udev_device->subsystem = strdup(subsystem);
1091         if (udev_device->subsystem == NULL)
1092                 return -ENOMEM;
1093         udev_device->subsystem_set = 1;
1094         udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1095         return 0;
1096 }
1097
1098 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1099 {
1100         free(udev_device->devtype);
1101         udev_device->devtype = strdup(devtype);
1102         if (udev_device->devtype == NULL)
1103                 return -ENOMEM;
1104         udev_device->devtype_set = 1;
1105         udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1106         return 0;
1107 }
1108
1109 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1110 {
1111         free(udev_device->devnode);
1112         udev_device->devnode = strdup(devnode);
1113         if (devnode == NULL)
1114                 return 0;
1115         if (udev_device->devnode == NULL)
1116                 return -ENOMEM;
1117         udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1118         return 0;
1119 }
1120
1121 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1122 {
1123         struct udev_list_entry *list_entry;
1124
1125         udev_device->devlinks_uptodate = 0;
1126         list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1127         if (list_entry == NULL)
1128                 return -ENOMEM;
1129         if (unique)
1130                 udev_list_entry_set_flag(list_entry, 1);
1131         return 0;
1132 }
1133
1134 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1135 {
1136         udev_device->envp_uptodate = 0;
1137         if (value == NULL) {
1138                 struct udev_list_entry *list_entry;
1139
1140                 list_entry = udev_device_get_properties_list_entry(udev_device);
1141                 list_entry = udev_list_entry_get_by_name(list_entry, key);
1142                 if (list_entry != NULL)
1143                         udev_list_entry_delete(list_entry);
1144                 return NULL;
1145         }
1146         return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1147 }
1148
1149 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1150 {
1151         char name[UTIL_PATH_SIZE];
1152         char *val;
1153
1154         util_strscpy(name, sizeof(name), property);
1155         val = strchr(name, '=');
1156         if (val == NULL)
1157                 return NULL;
1158         val[0] = '\0';
1159         val = &val[1];
1160         if (val[0] == '\0')
1161                 val = NULL;
1162         return udev_device_add_property(udev_device, name, val);
1163 }
1164
1165 /**
1166  * udev_device_get_property_value:
1167  * @udev_device: udev device
1168  * @key: property name
1169  *
1170  * Returns: the value of a device property, or #NULL if there is no such property.
1171  **/
1172 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1173 {
1174         struct udev_list_entry *list_entry;
1175
1176         if (udev_device == NULL)
1177                 return NULL;
1178         if (key == NULL)
1179                 return NULL;
1180
1181         list_entry = udev_device_get_properties_list_entry(udev_device);
1182         list_entry =  udev_list_entry_get_by_name(list_entry, key);
1183         return udev_list_entry_get_value(list_entry);
1184 }
1185
1186 #define ENVP_SIZE                       128
1187 #define MONITOR_BUF_SIZE                4096
1188 static int update_envp_monitor_buf(struct udev_device *udev_device)
1189 {
1190         struct udev_list_entry *list_entry;
1191         char *s;
1192         size_t l;
1193         unsigned int i;
1194
1195         /* monitor buffer of property strings */
1196         free(udev_device->monitor_buf);
1197         udev_device->monitor_buf_len = 0;
1198         udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1199         if (udev_device->monitor_buf == NULL)
1200                 return -ENOMEM;
1201
1202         /* envp array, strings will point into monitor buffer */
1203         if (udev_device->envp == NULL)
1204                 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1205         if (udev_device->envp == NULL)
1206                 return -ENOMEM;
1207
1208         i = 0;
1209         s = udev_device->monitor_buf;
1210         l = MONITOR_BUF_SIZE;
1211         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1212                 const char *key;
1213
1214                 key = udev_list_entry_get_name(list_entry);
1215                 /* skip private variables */
1216                 if (key[0] == '.')
1217                         continue;
1218
1219                 /* add string to envp array */
1220                 udev_device->envp[i++] = s;
1221                 if (i+1 >= ENVP_SIZE)
1222                         return -EINVAL;
1223
1224                 /* add property string to monitor buffer */
1225                 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1226                 if (l == 0)
1227                         return -EINVAL;
1228                 s++;
1229         }
1230         udev_device->envp[i] = NULL;
1231         udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1232         udev_device->envp_uptodate = 1;
1233         dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1234             i, udev_device->monitor_buf_len);
1235         return 0;
1236 }
1237
1238 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1239 {
1240         if (!udev_device->envp_uptodate)
1241                 if (update_envp_monitor_buf(udev_device) != 0)
1242                         return NULL;
1243         return udev_device->envp;
1244 }
1245
1246 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1247 {
1248         if (!udev_device->envp_uptodate)
1249                 if (update_envp_monitor_buf(udev_device) != 0)
1250                         return -EINVAL;
1251         *buf = udev_device->monitor_buf;
1252         return udev_device->monitor_buf_len;
1253 }
1254
1255 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1256 {
1257         free(udev_device->action);
1258         udev_device->action = strdup(action);
1259         if (udev_device->action == NULL)
1260                 return -ENOMEM;
1261         udev_device_add_property(udev_device, "ACTION", udev_device->action);
1262         return 0;
1263 }
1264
1265 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1266 {
1267         free(udev_device->driver);
1268         udev_device->driver = strdup(driver);
1269         if (udev_device->driver == NULL)
1270                 return -ENOMEM;
1271         udev_device->driver_set = 1;
1272         udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1273         return 0;
1274 }
1275
1276 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1277 {
1278         return udev_device->devpath_old;
1279 }
1280
1281 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1282 {
1283         const char *pos;
1284         size_t len;
1285
1286         free(udev_device->devpath_old);
1287         udev_device->devpath_old = strdup(devpath_old);
1288         if (udev_device->devpath_old == NULL)
1289                 return -ENOMEM;
1290         udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1291
1292         pos = strrchr(udev_device->devpath_old, '/');
1293         if (pos == NULL)
1294                 return -EINVAL;
1295         udev_device->sysname_old = strdup(&pos[1]);
1296         if (udev_device->sysname_old == NULL)
1297                 return -ENOMEM;
1298
1299         /* some devices have '!' in their name, change that to '/' */
1300         len = 0;
1301         while (udev_device->sysname_old[len] != '\0') {
1302                 if (udev_device->sysname_old[len] == '!')
1303                         udev_device->sysname_old[len] = '/';
1304                 len++;
1305         }
1306         return 0;
1307 }
1308
1309 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1310 {
1311         if (udev_device == NULL)
1312                 return NULL;
1313         return udev_device->sysname_old;
1314 }
1315
1316 const char *udev_device_get_knodename(struct udev_device *udev_device)
1317 {
1318         return udev_device->knodename;
1319 }
1320
1321 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1322 {
1323         free(udev_device->knodename);
1324         udev_device->knodename = strdup(knodename);
1325         if (udev_device->knodename == NULL)
1326                 return -ENOMEM;
1327         udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1328         return 0;
1329 }
1330
1331 int udev_device_get_timeout(struct udev_device *udev_device)
1332 {
1333         return udev_device->timeout;
1334 }
1335
1336 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1337 {
1338         udev_device->timeout = timeout;
1339         return 0;
1340 }
1341 int udev_device_get_event_timeout(struct udev_device *udev_device)
1342 {
1343         if (!udev_device->info_loaded)
1344                 device_load_info(udev_device);
1345         return udev_device->event_timeout;
1346 }
1347
1348 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1349 {
1350         udev_device->event_timeout = event_timeout;
1351         return 0;
1352 }
1353
1354 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1355 {
1356         char num[32];
1357
1358         udev_device->seqnum = seqnum;
1359         snprintf(num, sizeof(num), "%llu", seqnum);
1360         udev_device_add_property(udev_device, "SEQNUM", num);
1361         return 0;
1362 }
1363
1364 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1365 {
1366         char num[32];
1367
1368         udev_device->devnum = devnum;
1369
1370         snprintf(num, sizeof(num), "%u", major(devnum));
1371         udev_device_add_property(udev_device, "MAJOR", num);
1372         snprintf(num, sizeof(num), "%u", minor(devnum));
1373         udev_device_add_property(udev_device, "MINOR", num);
1374         return 0;
1375 }
1376
1377 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1378 {
1379         if (!udev_device->info_loaded)
1380                 device_load_info(udev_device);
1381         return udev_device->num_fake_partitions;
1382 }
1383
1384 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1385 {
1386         udev_device->num_fake_partitions = num;
1387         return 0;
1388 }
1389
1390 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1391 {
1392         if (!udev_device->info_loaded)
1393                 device_load_info(udev_device);
1394         return udev_device->devlink_priority;
1395 }
1396
1397 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1398 {
1399          udev_device->devlink_priority = prio;
1400         return 0;
1401 }
1402
1403 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1404 {
1405         if (!udev_device->info_loaded)
1406                 device_load_info(udev_device);
1407         return udev_device->ignore_remove;
1408 }
1409
1410 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1411 {
1412         udev_device->ignore_remove = ignore;
1413         return 0;
1414 }
1415
1416 int udev_device_get_watch_handle(struct udev_device *udev_device)
1417 {
1418         if (!udev_device->info_loaded)
1419                 device_load_info(udev_device);
1420         return udev_device->watch_handle;
1421 }
1422
1423 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1424 {
1425         udev_device->watch_handle = handle;
1426         return 0;
1427 }