chiark / gitweb /
a772dbbcf8841456d477ae1e6e632d5dd587fabb
[elogind.git] / udev / lib / libudev-device.c
1 /*
2  * libudev - interface to udev device information
3  *
4  * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <stddef.h>
23 #include <unistd.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <dirent.h>
27 #include <fcntl.h>
28 #include <sys/stat.h>
29
30 #include "libudev.h"
31 #include "libudev-private.h"
32
33 struct udev_device {
34         int refcount;
35         struct udev *udev;
36         struct udev_device *parent_device;
37         char *syspath;
38         const char *devpath;
39         const char *sysname;
40         char *devname;
41         char *subsystem;
42         struct list_head link_list;
43         struct list_head properties_list;
44         char *action;
45         char *driver;
46         char *devpath_old;
47         char *physdevpath;
48         int timeout;
49         dev_t devnum;
50         unsigned long long int seqnum;
51         int num_fake_partitions;
52         int devlink_priority;
53         int ignore_remove;
54         struct list_head attr_list;
55 };
56
57 static size_t syspath_to_db_path(struct udev_device *udev_device, char *filename, size_t len)
58 {
59         size_t start;
60
61         /* translate to location of db file */
62         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), len);
63         start = util_strlcat(filename, "/.udev/db/", len);
64         util_strlcat(filename, udev_device->devpath, len);
65         return util_path_encode(&filename[start], len - start);
66 }
67
68 static int device_read_uevent_file(struct udev_device *udev_device)
69 {
70         char filename[UTIL_PATH_SIZE];
71         FILE *f;
72         char line[UTIL_LINE_SIZE];
73
74         util_strlcpy(filename, udev_device->syspath, sizeof(filename));
75         util_strlcat(filename, "/uevent", sizeof(filename));
76         f = fopen(filename, "r");
77         if (f == NULL)
78                 return -1;
79
80         while (fgets(line, sizeof(line), f)) {
81                 char *pos;
82
83                 pos = strchr(line, '\n');
84                 if (pos == NULL)
85                         continue;
86                 pos[0] = '\0';
87                 device_add_property_from_string(udev_device, line);
88         }
89
90         fclose(f);
91         return 0;
92 }
93
94 static int device_read_db(struct udev_device *udev_device)
95 {
96         struct stat stats;
97         char filename[UTIL_PATH_SIZE];
98         char line[UTIL_LINE_SIZE];
99         FILE *f;
100         int rc = 0;
101
102         syspath_to_db_path(udev_device, filename, sizeof(filename));
103
104         if (lstat(filename, &stats) != 0) {
105                 info(udev_device->udev, "no db file to read %s: %s\n", filename, strerror(errno));
106                 return -1;
107         }
108         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
109                 char target[UTIL_PATH_SIZE];
110                 int target_len;
111
112                 info(udev_device->udev, "found a symlink as db file\n");
113                 target_len = readlink(filename, target, sizeof(target));
114                 if (target_len > 0)
115                         target[target_len] = '\0';
116                 else {
117                         info(udev_device->udev, "error reading db link %s: %s\n", filename, strerror(errno));
118                         return -1;
119                 }
120                 dbg(udev_device->udev, "db link points to '%s'\n", target);
121                 if (asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), target) < 0)
122                         return -ENOMEM;
123                 return 0;
124         }
125
126         f = fopen(filename, "r");
127         if (f == NULL) {
128                 info(udev_device->udev, "error reading db file %s: %s\n", filename, strerror(errno));
129                 return -1;
130         }
131         while (fgets(line, sizeof(line), f)) {
132                 ssize_t len;
133                 const char *val;
134                 unsigned int maj, min;
135
136                 len = strlen(line);
137                 if (len < 4)
138                         break;
139                 line[len-1] = '\0';
140                 val = &line[2];
141
142                 switch(line[0]) {
143                 case 'N':
144                         asprintf(&udev_device->devname, "%s/%s", udev_get_dev_path(udev_device->udev), val);
145                         break;
146                 case 'M':
147                         sscanf(val, "%u:%u", &maj, &min);
148                         device_set_devnum(udev_device, makedev(maj, min));
149                         break;
150                 case 'S':
151                         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
152                         util_strlcat(filename, "/", sizeof(filename));
153                         util_strlcat(filename, val, sizeof(filename));
154                         device_add_devlink(udev_device, filename);
155                         break;
156                 case 'L':
157                         device_set_devlink_priority(udev_device, atoi(val));
158                         break;
159                 case 'T':
160                         device_set_timeout(udev_device,  atoi(val));
161                         break;
162                 case 'A':
163                         device_set_num_fake_partitions(udev_device, atoi(val));
164                         break;
165                 case 'R':
166                         device_set_ignore_remove(udev_device, atoi(val));
167                         break;
168                 case 'E':
169                         device_add_property_from_string(udev_device, val);
170                         break;
171                 }
172         }
173         fclose(f);
174
175         return rc;
176 }
177
178 struct udev_device *device_init(struct udev *udev)
179 {
180         struct udev_device *udev_device;
181
182         if (udev == NULL)
183                 return NULL;
184
185         udev_device = malloc(sizeof(struct udev_device));
186         if (udev_device == NULL)
187                 return NULL;
188         memset(udev_device, 0x00, sizeof(struct udev_device));
189         udev_device->refcount = 1;
190         udev_device->udev = udev;
191         INIT_LIST_HEAD(&udev_device->link_list);
192         INIT_LIST_HEAD(&udev_device->properties_list);
193         INIT_LIST_HEAD(&udev_device->attr_list);
194         info(udev_device->udev, "udev_device: %p created\n", udev_device);
195         return udev_device;
196 }
197
198 /**
199  * udev_device_new_from_syspath:
200  * @udev: udev library context
201  * @syspath: sys device path including sys directory
202  *
203  * Create new udev device, and fill in information from the sys
204  * device and the udev database entry. The sypath is the absolute
205  * path to the device, including the sys mount point.
206  *
207  * The initial refcount is 1, and needs to be decremented to
208  * release the ressources of the udev device.
209  *
210  * Returns: a new udev device, or #NULL, if it does not exist
211  **/
212 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
213 {
214         char path[UTIL_PATH_SIZE];
215         struct stat statbuf;
216         struct udev_device *udev_device;
217
218         if (udev == NULL)
219                 return NULL;
220         if (syspath == NULL)
221                 return NULL;
222
223         util_strlcpy(path, syspath, sizeof(path));
224         util_strlcat(path, "/uevent", sizeof(path));
225         if (stat(path, &statbuf) != 0) {
226                 info(udev, "not a device :%s\n", syspath);
227                 return NULL;
228         }
229
230         udev_device = device_init(udev);
231         if (udev_device == NULL)
232                 return NULL;
233
234         /* resolve possible symlink to real path */
235         util_strlcpy(path, syspath, sizeof(path));
236         util_resolve_sys_link(udev, path, sizeof(path));
237         device_set_syspath(udev_device, path);
238         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
239
240         device_read_uevent_file(udev_device);
241         if (device_read_db(udev_device) >= 0)
242                 info(udev, "device %p filled with udev database data\n", udev_device);
243         return udev_device;
244 }
245
246 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
247 {
248         char path[UTIL_PATH_SIZE];
249         const char *type_str;
250         struct udev_enumerate *enumerate;
251         struct udev_list *list;
252         struct udev_device *device = NULL;
253
254         if (type == 'b')
255                 type_str = "block";
256         else if (type == 'c')
257                 type_str = "char";
258         else
259                 return NULL;
260
261         /* /sys/dev/{block,char}/<maj>:<min> links */
262         snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
263                  type_str, major(devnum), minor(devnum));
264         if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
265                 return udev_device_new_from_syspath(udev, path);
266
267         /* search all sys devices for the major/minor */
268         enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
269         if (enumerate == NULL)
270                 return NULL;
271         list = udev_enumerate_get_devices_list(enumerate);
272         while (list != NULL) {
273                 struct udev_device *device_loop;
274
275                 device_loop = udev_device_new_from_syspath(udev, udev_list_get_name(list));
276                 if (device_loop != NULL) {
277                         if (udev_device_get_devnum(device_loop) == devnum) {
278                                 device = device_loop;
279                                 break;
280                         }
281                         udev_device_unref(device_loop);
282                 }
283                 list = udev_list_get_next(list);
284         }
285         udev_enumerate_unref(enumerate);
286         return device;
287 }
288
289 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
290 {
291         struct udev_device *udev_device_parent = NULL;
292         char path[UTIL_PATH_SIZE];
293         char *pos;
294
295         if (udev_device == NULL)
296                 return NULL;
297
298         util_strlcpy(path, udev_device->syspath, sizeof(path));
299         while (1) {
300                 pos = strrchr(path, '/');
301                 if (pos == path || pos == NULL)
302                         break;
303                 pos[0] = '\0';
304                 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
305                 if (udev_device_parent != NULL)
306                         return udev_device_parent;
307         }
308
309         /* follow "device" link in deprecated sys /sys/class/ layout */
310         if (strncmp(udev_device->devpath, "/class/", 7) == 0) {
311                 util_strlcpy(path, udev_device->syspath, sizeof(path));
312                 util_strlcat(path, "/device", sizeof(path));
313                 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
314                         udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
315                         if (udev_device_parent != NULL)
316                                 return udev_device_parent;
317                 }
318         }
319         return NULL;
320 }
321
322 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
323 {
324         if (udev_device->parent_device != NULL) {
325                 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
326                 return udev_device->parent_device;
327         }
328         udev_device->parent_device = device_new_from_parent(udev_device);
329         return udev_device->parent_device;
330 }
331
332 /**
333  * udev_device_get_udev:
334  * @udev_device: udev device
335  *
336  * Retrieve the udev library context the device was created with.
337  *
338  * Returns: the udev library context
339  **/
340 struct udev *udev_device_get_udev(struct udev_device *udev_device)
341 {
342         if (udev_device == NULL)
343                 return NULL;
344         return udev_device->udev;
345 }
346
347 /**
348  * udev_device_ref:
349  * @udev_device: udev device
350  *
351  * Take a reference of a udev device.
352  *
353  * Returns: the passed udev device
354  **/
355 struct udev_device *udev_device_ref(struct udev_device *udev_device)
356 {
357         if (udev_device == NULL)
358                 return NULL;
359         udev_device->refcount++;
360         return udev_device;
361 }
362
363 /**
364  * udev_device_unref:
365  * @udev_device: udev device
366  *
367  * Drop a reference of a udev device. If the refcount reaches zero,
368  * the ressources of the device will be released.
369  *
370  **/
371 void udev_device_unref(struct udev_device *udev_device)
372 {
373         if (udev_device == NULL)
374                 return;
375         udev_device->refcount--;
376         if (udev_device->refcount > 0)
377                 return;
378         if (udev_device->parent_device != NULL)
379                 udev_device_unref(udev_device->parent_device);
380         free(udev_device->syspath);
381         free(udev_device->devname);
382         free(udev_device->subsystem);
383         list_cleanup(udev_device->udev, &udev_device->link_list);
384         list_cleanup(udev_device->udev, &udev_device->properties_list);
385         free(udev_device->action);
386         free(udev_device->driver);
387         free(udev_device->devpath_old);
388         free(udev_device->physdevpath);
389         list_cleanup(udev_device->udev, &udev_device->attr_list);
390         info(udev_device->udev, "udev_device: %p released\n", udev_device);
391         free(udev_device);
392 }
393
394 /**
395  * udev_device_get_devpath:
396  * @udev_device: udev device
397  *
398  * Retrieve the kernel devpath value of the udev device. The path
399  * does not contain the sys mount point, and starts with a '/'.
400  *
401  * Returns: the devpath of the udev device
402  **/
403 const char *udev_device_get_devpath(struct udev_device *udev_device)
404 {
405         if (udev_device == NULL)
406                 return NULL;
407         return udev_device->devpath;
408 }
409
410 /**
411  * udev_device_get_syspath:
412  * @udev_device: udev device
413  *
414  * Retrieve the sys path of the udev device. The path is an
415  * absolute path and starts with the sys mount point.
416  *
417  * Returns: the sys path of the udev device
418  **/
419 const char *udev_device_get_syspath(struct udev_device *udev_device)
420 {
421         if (udev_device == NULL)
422                 return NULL;
423         return udev_device->syspath;
424 }
425
426 const char *udev_device_get_sysname(struct udev_device *udev_device)
427 {
428         if (udev_device == NULL)
429                 return NULL;
430         return udev_device->sysname;
431 }
432
433 /**
434  * udev_device_get_devnode:
435  * @udev_device: udev device
436  *
437  * Retrieve the device node file name belonging to the udev device.
438  * The path is an absolute path, and starts with the device directory.
439  *
440  * Returns: the device node file name of the udev device, or #NULL if no device node exists
441  **/
442 const char *udev_device_get_devnode(struct udev_device *udev_device)
443 {
444         if (udev_device == NULL)
445                 return NULL;
446         return udev_device->devname;
447 }
448
449 /**
450  * udev_device_get_subsystem:
451  * @udev_device: udev device
452  *
453  * Retrieve the subsystem string of the udev device. The string does not
454  * contain any "/".
455  *
456  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
457  **/
458 const char *udev_device_get_subsystem(struct udev_device *udev_device)
459 {
460         char subsystem[UTIL_NAME_SIZE];
461
462         if (udev_device == NULL)
463                 return NULL;
464         if (udev_device->subsystem != NULL)
465                 return udev_device->subsystem;
466
467         /* read "subsytem" link */
468         if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
469                 udev_device->subsystem = strdup(subsystem);
470                 return udev_device->subsystem;
471         }
472
473         /* implicit names */
474         if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
475                 udev_device->subsystem = strdup("module");
476                 return udev_device->subsystem;
477         }
478         if (strstr(udev_device->devpath, "/drivers/") != NULL) {
479                 udev_device->subsystem = strdup("drivers");
480                 return udev_device->subsystem;
481         }
482         if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
483             strncmp(udev_device->devpath, "/class/", 7) == 0 ||
484             strncmp(udev_device->devpath, "/bus/", 5) == 0) {
485                 udev_device->subsystem = strdup("subsystem");
486                 return udev_device->subsystem;
487         }
488         return NULL;
489 }
490
491 /**
492  * udev_device_get_devlinks_list:
493  * @udev_device: udev device
494  *
495  * Retrieve the list of device links pointing to the device file of
496  * the udev device. The next list entry can be retrieved with
497  * udev_list_next(), which returns #NULL if no more entries exist.
498  * The devlink path can be retrieved from the list entry by
499  * udev_list_get_name(). The path is an absolute path, and starts with
500  * the device directory.
501  *
502  * Returns: the first entry of the device node link list
503  **/
504 struct udev_list *udev_device_get_devlinks_list(struct udev_device *udev_device)
505 {
506         return list_get_entry(&udev_device->link_list);
507 }
508
509 /**
510  * udev_device_get_properties_list:
511  * @udev_device: udev device
512  *
513  * Retrieve the list of key/value device properties of the udev
514  * device. The next list entry can be retrieved with udev_list_next(),
515  * which returns #NULL if no more entries exist. The property name
516  * can be retrieved from the list entry by udev_list_get_name(),
517  * the property value by udev_list_get_value().
518  *
519  * Returns: the first entry of the property list
520  **/
521 struct udev_list *udev_device_get_properties_list(struct udev_device *udev_device)
522 {
523         return list_get_entry(&udev_device->properties_list);
524 }
525
526 const char *udev_device_get_driver(struct udev_device *udev_device)
527 {
528         char driver[UTIL_NAME_SIZE];
529
530         if (udev_device == NULL)
531                 return NULL;
532         if (udev_device->driver != NULL)
533                 return udev_device->driver;
534         if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
535                 return NULL;
536         udev_device->driver = strdup(driver);
537         return udev_device->driver;
538 }
539
540 dev_t udev_device_get_devnum(struct udev_device *udev_device)
541 {
542         if (udev_device == NULL)
543                 return makedev(0, 0);
544         return udev_device->devnum;
545 }
546
547 const char *udev_device_get_action(struct udev_device *udev_device)
548 {
549         if (udev_device == NULL)
550                 return NULL;
551         return udev_device->action;
552 }
553
554 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
555 {
556         if (udev_device == NULL)
557                 return 0;
558         return udev_device->seqnum;
559 }
560
561 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
562 {
563         struct udev_list *list;
564         char path[UTIL_PATH_SIZE];
565         char value[UTIL_NAME_SIZE];
566         struct stat statbuf;
567         int fd;
568         ssize_t size;
569         const char *val = NULL;
570
571         /* look for possibly already cached result */
572         list = list_get_entry(&udev_device->attr_list);
573         while (list != NULL) {
574                 if (strcmp(udev_list_get_name(list), attr) == 0) {
575                         info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_get_value(list));
576                         return udev_list_get_name(list);
577                 }
578                 list = udev_list_get_next(list);
579         }
580
581         util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
582         util_strlcat(path, "/", sizeof(path));
583         util_strlcat(path, attr, sizeof(path));
584
585         if (lstat(path, &statbuf) != 0) {
586                 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
587                 goto out;
588         }
589
590         if (S_ISLNK(statbuf.st_mode)) {
591                 /* links return the last element of the target path */
592                 char target[UTIL_NAME_SIZE];
593                 int len;
594                 char *pos;
595
596                 len = readlink(path, target, sizeof(target));
597                 if (len > 0) {
598                         target[len] = '\0';
599                         pos = strrchr(target, '/');
600                         if (pos != NULL) {
601                                 pos = &pos[1];
602                                 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
603                                 list = list_insert(udev_device->udev, &udev_device->attr_list, attr, pos, 0);
604                                 val = udev_list_get_value(list);
605                         }
606                 }
607                 goto out;
608         }
609
610         /* skip directories */
611         if (S_ISDIR(statbuf.st_mode))
612                 goto out;
613
614         /* skip non-readable files */
615         if ((statbuf.st_mode & S_IRUSR) == 0)
616                 goto out;
617
618         /* read attribute value */
619         fd = open(path, O_RDONLY);
620         if (fd < 0) {
621                 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
622                 goto out;
623         }
624         size = read(fd, value, sizeof(value));
625         close(fd);
626         if (size < 0)
627                 goto out;
628         if (size == sizeof(value))
629                 goto out;
630
631         /* got a valid value, store it in cache and return it */
632         value[size] = '\0';
633         util_remove_trailing_chars(value, '\n');
634         info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
635         list = list_insert(udev_device->udev, &udev_device->attr_list, attr, value, 0);
636         val = udev_list_get_value(list);
637 out:
638         return val;
639 }
640 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
641 {
642         const char *pos;
643
644         udev_device->syspath = strdup(syspath);
645         if (udev_device->syspath ==  NULL)
646                 return -ENOMEM;
647         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
648         pos = strrchr(udev_device->syspath, '/');
649         if (pos == NULL)
650                 return -EINVAL;
651         udev_device->sysname = &pos[1];
652         return 0;
653 }
654
655 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
656 {
657         udev_device->subsystem = strdup(subsystem);
658         if (udev_device->subsystem == NULL)
659                 return -1;
660         return 0;
661 }
662
663 int device_set_devname(struct udev_device *udev_device, const char *devname)
664 {
665         udev_device->devname = strdup(devname);
666         if (udev_device->devname == NULL)
667                 return -ENOMEM;
668         return 0;
669 }
670
671 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
672 {
673         if (list_insert(udev_device->udev, &udev_device->link_list, devlink, NULL, 0) == NULL)
674                 return -ENOMEM;
675         return 0;
676 }
677
678 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
679 {
680         if (list_insert(udev_device->udev, &udev_device->properties_list, key, value, 0) == NULL)
681                 return -ENOMEM;
682         return 0;
683 }
684
685 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
686 {
687         char name[UTIL_PATH_SIZE];
688         char *val;
689
690         strncpy(name, property, sizeof(name));
691         val = strchr(name, '=');
692         if (val == NULL)
693                 return -1;
694         val[0] = '\0';
695         val = &val[1];
696         if (val[0] == '\0')
697                 val = NULL;
698         device_add_property(udev_device, name, val);
699         return 0;
700 }
701
702 int device_set_action(struct udev_device *udev_device, const char *action)
703 {
704         udev_device->action = strdup(action);
705         if (udev_device->action == NULL)
706                 return -ENOMEM;
707         return 0;
708 }
709
710 int device_set_driver(struct udev_device *udev_device, const char *driver)
711 {
712         udev_device->driver = strdup(driver);
713         if (udev_device->driver == NULL)
714                 return -ENOMEM;
715         return 0;
716 }
717
718 const char *device_get_devpath_old(struct udev_device *udev_device)
719 {
720         if (udev_device == NULL)
721                 return NULL;
722         return udev_device->devpath_old;
723 }
724
725 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
726 {
727         udev_device->devpath_old = strdup(devpath_old);
728         if (udev_device->devpath_old == NULL)
729                 return -ENOMEM;
730         return 0;
731 }
732
733 const char *device_get_physdevpath(struct udev_device *udev_device)
734 {
735         if (udev_device == NULL)
736                 return NULL;
737         return udev_device->physdevpath;
738 }
739
740 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
741 {
742         udev_device->physdevpath = strdup(physdevpath);
743         if (udev_device->physdevpath == NULL)
744                 return -ENOMEM;
745         return 0;
746 }
747
748 int device_get_timeout(struct udev_device *udev_device)
749 {
750         if (udev_device == NULL)
751                 return -1;
752         return udev_device->timeout;
753 }
754
755 int device_set_timeout(struct udev_device *udev_device, int timeout)
756 {
757         udev_device->timeout = timeout;
758         return 0;
759 }
760
761 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
762 {
763         udev_device->seqnum = seqnum;
764         return 0;
765 }
766
767 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
768 {
769         udev_device->devnum = devnum;
770         return 0;
771 }
772
773 int device_get_num_fake_partitions(struct udev_device *udev_device)
774 {
775         if (udev_device == NULL)
776                 return -1;
777         return udev_device->num_fake_partitions;
778 }
779
780 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
781 {
782         udev_device->num_fake_partitions = num;
783         return 0;
784 }
785
786 int device_get_devlink_priority(struct udev_device *udev_device)
787 {
788         if (udev_device == NULL)
789                 return -1;
790         return udev_device->devlink_priority;
791 }
792
793 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
794 {
795          udev_device->devlink_priority = prio;
796         return 0;
797 }
798
799 int device_get_ignore_remove(struct udev_device *udev_device)
800 {
801         if (udev_device == NULL)
802                 return -1;
803         return udev_device->ignore_remove;
804 }
805
806 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
807 {
808         udev_device->ignore_remove = ignore;
809         return 0;
810 }
811