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