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