chiark / gitweb /
lubudev: accept more sys directories as devices, and parent devices
[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         size_t len;
233         const char *subdir;
234         const char *pos;
235         char path[UTIL_PATH_SIZE];
236         struct stat statbuf;
237         struct udev_device *udev_device;
238
239         if (udev == NULL)
240                 return NULL;
241         if (syspath == NULL)
242                 return NULL;
243
244         /* path starts in sys */
245         len = strlen(udev_get_sys_path(udev));
246         if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
247                 info(udev, "not in sys :%s\n", syspath);
248                 return NULL;
249         }
250
251         /* path is not a root directory */
252         subdir = &syspath[len+1];
253         pos = strrchr(subdir, '/');
254         if (pos == NULL || pos < &subdir[2]) {
255                 info(udev, "not in subdir :%s\n", syspath);
256                 return NULL;
257         }
258
259         /* resolve possible symlink to real path */
260         util_strlcpy(path, syspath, sizeof(path));
261         util_resolve_sys_link(udev, path, sizeof(path));
262
263         /* path exists in sys */
264         if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
265             strncmp(&syspath[len], "/class/", 7) == 0 ||
266             strncmp(&syspath[len], "/block/", 7) == 0) {
267                 char file[UTIL_PATH_SIZE];
268
269                 /* all "devices" require a "uevent" file */
270                 util_strlcpy(file, path, sizeof(file));
271                 util_strlcat(file, "/uevent", sizeof(file));
272                 if (stat(file, &statbuf) != 0) {
273                         info(udev, "not a device: %s\n", syspath);
274                         return NULL;
275                 }
276         } else {
277                 /* everything else just needs to be a directory */
278                 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
279                         info(udev, "directory not found: %s\n", syspath);
280                         return NULL;
281                 }
282         }
283
284         udev_device = device_init(udev);
285         if (udev_device == NULL)
286                 return NULL;
287
288         device_set_syspath(udev_device, path);
289         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
290
291         return udev_device;
292 }
293
294 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
295 {
296         char path[UTIL_PATH_SIZE];
297         const char *type_str;
298         struct udev_enumerate *enumerate;
299         struct udev_list_entry *list_entry;
300         struct udev_device *device = NULL;
301
302         if (type == 'b')
303                 type_str = "block";
304         else if (type == 'c')
305                 type_str = "char";
306         else
307                 return NULL;
308
309         /* /sys/dev/{block,char}/<maj>:<min> link */
310         snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
311                  type_str, major(devnum), minor(devnum));
312         if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
313                 return udev_device_new_from_syspath(udev, path);
314
315         /* fallback to search all sys devices for the major/minor */
316         enumerate = udev_enumerate_new_from_subsystems(udev, NULL);
317         if (enumerate == NULL)
318                 return NULL;
319         udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(enumerate)) {
320                 struct udev_device *device_loop;
321
322                 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
323                 if (device_loop != NULL) {
324                         if (udev_device_get_devnum(device_loop) == devnum) {
325                                 device = device_loop;
326                                 break;
327                         }
328                         udev_device_unref(device_loop);
329                 }
330         }
331         udev_enumerate_unref(enumerate);
332         return device;
333 }
334
335 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
336 {
337         struct udev_device *udev_device_parent = NULL;
338         char path[UTIL_PATH_SIZE];
339         const char *subdir;
340
341         /* follow "device" link in deprecated sys layout */
342         if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
343             strncmp(udev_device->devpath, "/block/", 7) == 0) {
344                 util_strlcpy(path, udev_device->syspath, sizeof(path));
345                 util_strlcat(path, "/device", sizeof(path));
346                 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0)
347                         udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
348                 return udev_device_parent;
349         }
350
351         util_strlcpy(path, udev_device->syspath, sizeof(path));
352         subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
353         while (1) {
354                 char *pos;
355
356                 pos = strrchr(subdir, '/');
357                 if (pos == NULL || pos < &subdir[2])
358                         break;
359                 pos[0] = '\0';
360                 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
361                 if (udev_device_parent != NULL)
362                         return udev_device_parent;
363         }
364         return NULL;
365 }
366
367 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
368 {
369         if (udev_device == NULL)
370                 return NULL;
371
372         if (udev_device->parent_device != NULL) {
373                 info(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
374                 return udev_device->parent_device;
375         }
376         udev_device->parent_device = device_new_from_parent(udev_device);
377         return udev_device->parent_device;
378 }
379
380 /**
381  * udev_device_get_udev:
382  * @udev_device: udev device
383  *
384  * Retrieve the udev library context the device was created with.
385  *
386  * Returns: the udev library context
387  **/
388 struct udev *udev_device_get_udev(struct udev_device *udev_device)
389 {
390         if (udev_device == NULL)
391                 return NULL;
392         return udev_device->udev;
393 }
394
395 /**
396  * udev_device_ref:
397  * @udev_device: udev device
398  *
399  * Take a reference of a udev device.
400  *
401  * Returns: the passed udev device
402  **/
403 struct udev_device *udev_device_ref(struct udev_device *udev_device)
404 {
405         if (udev_device == NULL)
406                 return NULL;
407         udev_device->refcount++;
408         return udev_device;
409 }
410
411 /**
412  * udev_device_unref:
413  * @udev_device: udev device
414  *
415  * Drop a reference of a udev device. If the refcount reaches zero,
416  * the ressources of the device will be released.
417  *
418  **/
419 void udev_device_unref(struct udev_device *udev_device)
420 {
421         if (udev_device == NULL)
422                 return;
423         udev_device->refcount--;
424         if (udev_device->refcount > 0)
425                 return;
426         if (udev_device->parent_device != NULL)
427                 udev_device_unref(udev_device->parent_device);
428         free(udev_device->syspath);
429         free(udev_device->devnode);
430         free(udev_device->subsystem);
431         list_cleanup(udev_device->udev, &udev_device->devlink_list);
432         list_cleanup(udev_device->udev, &udev_device->properties_list);
433         free(udev_device->action);
434         free(udev_device->driver);
435         free(udev_device->devpath_old);
436         free(udev_device->physdevpath);
437         list_cleanup(udev_device->udev, &udev_device->attr_list);
438         info(udev_device->udev, "udev_device: %p released\n", udev_device);
439         free(udev_device);
440 }
441
442 /**
443  * udev_device_get_devpath:
444  * @udev_device: udev device
445  *
446  * Retrieve the kernel devpath value of the udev device. The path
447  * does not contain the sys mount point, and starts with a '/'.
448  *
449  * Returns: the devpath of the udev device
450  **/
451 const char *udev_device_get_devpath(struct udev_device *udev_device)
452 {
453         if (udev_device == NULL)
454                 return NULL;
455         return udev_device->devpath;
456 }
457
458 /**
459  * udev_device_get_syspath:
460  * @udev_device: udev device
461  *
462  * Retrieve the sys path of the udev device. The path is an
463  * absolute path and starts with the sys mount point.
464  *
465  * Returns: the sys path of the udev device
466  **/
467 const char *udev_device_get_syspath(struct udev_device *udev_device)
468 {
469         if (udev_device == NULL)
470                 return NULL;
471         return udev_device->syspath;
472 }
473
474 const char *udev_device_get_sysname(struct udev_device *udev_device)
475 {
476         if (udev_device == NULL)
477                 return NULL;
478         return udev_device->sysname;
479 }
480
481 /**
482  * udev_device_get_devnode:
483  * @udev_device: udev device
484  *
485  * Retrieve the device node file name belonging to the udev device.
486  * The path is an absolute path, and starts with the device directory.
487  *
488  * Returns: the device node file name of the udev device, or #NULL if no device node exists
489  **/
490 const char *udev_device_get_devnode(struct udev_device *udev_device)
491 {
492         if (udev_device == NULL)
493                 return NULL;
494         if (!udev_device->info_loaded)
495                 device_load_info(udev_device);
496         return udev_device->devnode;
497 }
498
499 /**
500  * udev_device_get_subsystem:
501  * @udev_device: udev device
502  *
503  * Retrieve the subsystem string of the udev device. The string does not
504  * contain any "/".
505  *
506  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
507  **/
508 const char *udev_device_get_subsystem(struct udev_device *udev_device)
509 {
510         char subsystem[UTIL_NAME_SIZE];
511
512         if (udev_device == NULL)
513                 return NULL;
514         if (udev_device->subsystem != NULL)
515                 return udev_device->subsystem;
516
517         /* read "subsytem" link */
518         if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
519                 udev_device->subsystem = strdup(subsystem);
520                 return udev_device->subsystem;
521         }
522
523         /* implicit names */
524         if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
525                 udev_device->subsystem = strdup("module");
526                 return udev_device->subsystem;
527         }
528         if (strstr(udev_device->devpath, "/drivers/") != NULL) {
529                 udev_device->subsystem = strdup("drivers");
530                 return udev_device->subsystem;
531         }
532         if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
533             strncmp(udev_device->devpath, "/class/", 7) == 0 ||
534             strncmp(udev_device->devpath, "/bus/", 5) == 0) {
535                 udev_device->subsystem = strdup("subsystem");
536                 return udev_device->subsystem;
537         }
538         return NULL;
539 }
540
541 /**
542  * udev_device_get_devlinks_list_entry:
543  * @udev_device: udev device
544  *
545  * Retrieve the list of device links pointing to the device file of
546  * the udev device. The next list entry can be retrieved with
547  * udev_list_entry_next(), which returns #NULL if no more entries exist.
548  * The devlink path can be retrieved from the list entry by
549  * udev_list_entry_get_name(). The path is an absolute path, and starts with
550  * the device directory.
551  *
552  * Returns: the first entry of the device node link list
553  **/
554 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
555 {
556         if (udev_device == NULL)
557                 return NULL;
558         if (!udev_device->info_loaded)
559                 device_load_info(udev_device);
560         return list_get_entry(&udev_device->devlink_list);
561 }
562
563 /**
564  * udev_device_get_properties_list_entry:
565  * @udev_device: udev device
566  *
567  * Retrieve the list of key/value device properties of the udev
568  * device. The next list entry can be retrieved with udev_list_entry_next(),
569  * which returns #NULL if no more entries exist. The property name
570  * can be retrieved from the list entry by udev_list_get_name(),
571  * the property value by udev_list_get_value().
572  *
573  * Returns: the first entry of the property list
574  **/
575 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
576 {
577         if (udev_device == NULL)
578                 return NULL;
579         if (!udev_device->info_loaded)
580                 device_load_info(udev_device);
581         return list_get_entry(&udev_device->properties_list);
582 }
583
584 const char *udev_device_get_driver(struct udev_device *udev_device)
585 {
586         char driver[UTIL_NAME_SIZE];
587
588         if (udev_device == NULL)
589                 return NULL;
590         if (udev_device->driver != NULL)
591                 return udev_device->driver;
592         if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) < 2)
593                 return NULL;
594         udev_device->driver = strdup(driver);
595         return udev_device->driver;
596 }
597
598 dev_t udev_device_get_devnum(struct udev_device *udev_device)
599 {
600         if (udev_device == NULL)
601                 return makedev(0, 0);
602         if (!udev_device->info_loaded)
603                 device_load_info(udev_device);
604         return udev_device->devnum;
605 }
606
607 const char *udev_device_get_action(struct udev_device *udev_device)
608 {
609         if (udev_device == NULL)
610                 return NULL;
611         return udev_device->action;
612 }
613
614 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
615 {
616         if (udev_device == NULL)
617                 return 0;
618         return udev_device->seqnum;
619 }
620
621 const char *udev_device_get_attr_value(struct udev_device *udev_device, const char *attr)
622 {
623         struct udev_list_entry *list_entry;
624         char path[UTIL_PATH_SIZE];
625         char value[UTIL_NAME_SIZE];
626         struct stat statbuf;
627         int fd;
628         ssize_t size;
629         const char *val = NULL;
630
631         /* look for possibly already cached result */
632         udev_list_entry_foreach(list_entry, list_get_entry(&udev_device->attr_list)) {
633                 if (strcmp(udev_list_entry_get_name(list_entry), attr) == 0) {
634                         info(udev_device->udev, "got '%s' (%s) from cache\n", attr, udev_list_entry_get_value(list_entry));
635                         return udev_list_entry_get_value(list_entry);
636                 }
637         }
638
639         util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
640         util_strlcat(path, "/", sizeof(path));
641         util_strlcat(path, attr, sizeof(path));
642
643         if (lstat(path, &statbuf) != 0) {
644                 info(udev_device->udev, "stat '%s' failed: %s\n", path, strerror(errno));
645                 goto out;
646         }
647
648         if (S_ISLNK(statbuf.st_mode)) {
649                 /* links return the last element of the target path */
650                 char target[UTIL_NAME_SIZE];
651                 int len;
652                 char *pos;
653
654                 len = readlink(path, target, sizeof(target));
655                 if (len > 0) {
656                         target[len] = '\0';
657                         pos = strrchr(target, '/');
658                         if (pos != NULL) {
659                                 pos = &pos[1];
660                                 info(udev_device->udev, "cache '%s' with link value '%s'\n", attr, pos);
661                                 list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, pos, 0, 0);
662                                 val = udev_list_entry_get_value(list_entry);
663                         }
664                 }
665                 goto out;
666         }
667
668         /* skip directories */
669         if (S_ISDIR(statbuf.st_mode))
670                 goto out;
671
672         /* skip non-readable files */
673         if ((statbuf.st_mode & S_IRUSR) == 0)
674                 goto out;
675
676         /* read attribute value */
677         fd = open(path, O_RDONLY);
678         if (fd < 0) {
679                 info(udev_device->udev, "attribute '%s' can not be opened\n", path);
680                 goto out;
681         }
682         size = read(fd, value, sizeof(value));
683         close(fd);
684         if (size < 0)
685                 goto out;
686         if (size == sizeof(value))
687                 goto out;
688
689         /* got a valid value, store it in cache and return it */
690         value[size] = '\0';
691         util_remove_trailing_chars(value, '\n');
692         info(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
693         list_entry = list_entry_add(udev_device->udev, &udev_device->attr_list, attr, value, 0, 0);
694         val = udev_list_entry_get_value(list_entry);
695 out:
696         return val;
697 }
698 int device_set_syspath(struct udev_device *udev_device, const char *syspath)
699 {
700         const char *pos;
701
702         udev_device->syspath = strdup(syspath);
703         if (udev_device->syspath ==  NULL)
704                 return -ENOMEM;
705         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
706         pos = strrchr(udev_device->syspath, '/');
707         if (pos == NULL)
708                 return -EINVAL;
709         udev_device->sysname = &pos[1];
710         return 0;
711 }
712
713 int device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
714 {
715         udev_device->subsystem = strdup(subsystem);
716         if (udev_device->subsystem == NULL)
717                 return -1;
718         return 0;
719 }
720
721 int device_set_devnode(struct udev_device *udev_device, const char *devnode)
722 {
723         udev_device->devnode = strdup(devnode);
724         if (udev_device->devnode == NULL)
725                 return -ENOMEM;
726         return 0;
727 }
728
729 int device_add_devlink(struct udev_device *udev_device, const char *devlink)
730 {
731         if (list_entry_add(udev_device->udev, &udev_device->devlink_list, devlink, NULL, 1, 0) == NULL)
732                 return -ENOMEM;
733         return 0;
734 }
735
736 int device_add_property(struct udev_device *udev_device, const char *key, const char *value)
737 {
738         if (list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0) == NULL)
739                 return -ENOMEM;
740         return 0;
741 }
742
743 int device_add_property_from_string(struct udev_device *udev_device, const char *property)
744 {
745         char name[UTIL_PATH_SIZE];
746         char *val;
747
748         strncpy(name, property, sizeof(name));
749         val = strchr(name, '=');
750         if (val == NULL)
751                 return -1;
752         val[0] = '\0';
753         val = &val[1];
754         if (val[0] == '\0')
755                 val = NULL;
756         device_add_property(udev_device, name, val);
757         return 0;
758 }
759
760 int device_set_action(struct udev_device *udev_device, const char *action)
761 {
762         udev_device->action = strdup(action);
763         if (udev_device->action == NULL)
764                 return -ENOMEM;
765         return 0;
766 }
767
768 int device_set_driver(struct udev_device *udev_device, const char *driver)
769 {
770         udev_device->driver = strdup(driver);
771         if (udev_device->driver == NULL)
772                 return -ENOMEM;
773         return 0;
774 }
775
776 const char *device_get_devpath_old(struct udev_device *udev_device)
777 {
778         return udev_device->devpath_old;
779 }
780
781 int device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
782 {
783         udev_device->devpath_old = strdup(devpath_old);
784         if (udev_device->devpath_old == NULL)
785                 return -ENOMEM;
786         return 0;
787 }
788
789 const char *device_get_physdevpath(struct udev_device *udev_device)
790 {
791         return udev_device->physdevpath;
792 }
793
794 int device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
795 {
796         udev_device->physdevpath = strdup(physdevpath);
797         if (udev_device->physdevpath == NULL)
798                 return -ENOMEM;
799         return 0;
800 }
801
802 int device_get_timeout(struct udev_device *udev_device)
803 {
804         return udev_device->timeout;
805 }
806
807 int device_set_timeout(struct udev_device *udev_device, int timeout)
808 {
809         udev_device->timeout = timeout;
810         return 0;
811 }
812 int device_get_event_timeout(struct udev_device *udev_device)
813 {
814         if (!udev_device->info_loaded)
815                 device_load_info(udev_device);
816         return udev_device->event_timeout;
817 }
818
819 int device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
820 {
821         udev_device->event_timeout = event_timeout;
822         return 0;
823 }
824
825 int device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
826 {
827         udev_device->seqnum = seqnum;
828         return 0;
829 }
830
831 int device_set_devnum(struct udev_device *udev_device, dev_t devnum)
832 {
833         udev_device->devnum = devnum;
834         return 0;
835 }
836
837 int device_get_num_fake_partitions(struct udev_device *udev_device)
838 {
839         if (!udev_device->info_loaded)
840                 device_load_info(udev_device);
841         return udev_device->num_fake_partitions;
842 }
843
844 int device_set_num_fake_partitions(struct udev_device *udev_device, int num)
845 {
846         udev_device->num_fake_partitions = num;
847         return 0;
848 }
849
850 int device_get_devlink_priority(struct udev_device *udev_device)
851 {
852         if (!udev_device->info_loaded)
853                 device_load_info(udev_device);
854         return udev_device->devlink_priority;
855 }
856
857 int device_set_devlink_priority(struct udev_device *udev_device, int prio)
858 {
859          udev_device->devlink_priority = prio;
860         return 0;
861 }
862
863 int device_get_ignore_remove(struct udev_device *udev_device)
864 {
865         if (!udev_device->info_loaded)
866                 device_load_info(udev_device);
867         return udev_device->ignore_remove;
868 }
869
870 int device_set_ignore_remove(struct udev_device *udev_device, int ignore)
871 {
872         udev_device->ignore_remove = ignore;
873         return 0;
874 }
875