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