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