chiark / gitweb /
c4714e4088d1a8dc6f1b5b0cc774fab307bf6e12
[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 <ctype.h>
29 #include <sys/stat.h>
30
31 #include "libudev.h"
32 #include "libudev-private.h"
33
34 struct udev_device {
35         struct udev *udev;
36         struct udev_device *parent_device;
37         char *syspath;
38         const char *devpath;
39         char *sysname;
40         const char *sysnum;
41         char *devnode;
42         char *subsystem;
43         char *driver;
44         char *action;
45         char *devpath_old;
46         char *physdevpath;
47         char **envp;
48         char *monitor_buf;
49         size_t monitor_buf_len;
50         struct udev_list_node devlinks_list;
51         struct udev_list_node properties_list;
52         struct udev_list_node sysattr_list;
53         unsigned long long int seqnum;
54         int event_timeout;
55         int timeout;
56         int num_fake_partitions;
57         int devlink_priority;
58         int refcount;
59         dev_t devnum;
60         unsigned int parent_set:1;
61         unsigned int subsystem_set:1;
62         unsigned int devlinks_uptodate:1;
63         unsigned int envp_uptodate:1;
64         unsigned int driver_set:1;
65         unsigned int info_loaded:1;
66         unsigned int ignore_remove:1;
67 };
68
69 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
70 {
71         size_t start;
72
73         /* translate to location of db file */
74         util_strlcpy(filename, udev_get_dev_path(udev), len);
75         start = util_strlcat(filename, "/.udev/db/", len);
76         util_strlcat(filename, devpath, len);
77         return util_path_encode(&filename[start], len - start);
78 }
79
80 int udev_device_read_db(struct udev_device *udev_device)
81 {
82         struct stat stats;
83         char filename[UTIL_PATH_SIZE];
84         char line[UTIL_LINE_SIZE];
85         FILE *f;
86
87         devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
88
89         if (lstat(filename, &stats) != 0) {
90                 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
91                 return -1;
92         }
93         if ((stats.st_mode & S_IFMT) == S_IFLNK) {
94                 char target[UTIL_PATH_SIZE];
95                 char devnode[UTIL_PATH_SIZE];
96                 int target_len;
97                 char *next;
98
99                 target_len = readlink(filename, target, sizeof(target));
100                 if (target_len > 0)
101                         target[target_len] = '\0';
102                 else {
103                         dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
104                         return -1;
105                 }
106
107                 next = strchr(target, ' ');
108                 if (next != NULL) {
109                         next[0] = '\0';
110                         next = &next[1];
111                 }
112                 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
113                 util_strlcat(devnode, "/", sizeof(devnode));
114                 util_strlcat(devnode, target, sizeof(devnode));
115                 udev_device_set_devnode(udev_device, devnode);
116                 while (next != NULL) {
117                         char devlink[UTIL_PATH_SIZE];
118                         const char *lnk;
119
120                         lnk = next;
121                         next = strchr(next, ' ');
122                         if (next != NULL) {
123                                 next[0] = '\0';
124                                 next = &next[1];
125                         }
126                         util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
127                         util_strlcat(devlink, "/", sizeof(devlink));
128                         util_strlcat(devlink, lnk, sizeof(devlink));
129                         udev_device_add_devlink(udev_device, devlink);
130                 }
131                 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
132                 return 0;
133         }
134
135         f = fopen(filename, "r");
136         if (f == NULL) {
137                 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
138                 return -1;
139         }
140         while (fgets(line, sizeof(line), f)) {
141                 ssize_t len;
142                 const char *val;
143
144                 len = strlen(line);
145                 if (len < 4)
146                         break;
147                 line[len-1] = '\0';
148                 val = &line[2];
149                 switch(line[0]) {
150                 case 'N':
151                         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
152                         util_strlcat(filename, "/", sizeof(filename));
153                         util_strlcat(filename, val, sizeof(filename));
154                         udev_device_set_devnode(udev_device, filename);
155                         break;
156                 case 'S':
157                         util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
158                         util_strlcat(filename, "/", sizeof(filename));
159                         util_strlcat(filename, val, sizeof(filename));
160                         udev_device_add_devlink(udev_device, filename);
161                         break;
162                 case 'L':
163                         udev_device_set_devlink_priority(udev_device, atoi(val));
164                         break;
165                 case 'T':
166                         udev_device_set_event_timeout(udev_device, atoi(val));
167                         break;
168                 case 'A':
169                         udev_device_set_num_fake_partitions(udev_device, atoi(val));
170                         break;
171                 case 'R':
172                         udev_device_set_ignore_remove(udev_device, atoi(val));
173                         break;
174                 case 'E':
175                         udev_device_add_property_from_string(udev_device, val);
176                         break;
177                 }
178         }
179         fclose(f);
180
181         info(udev_device->udev, "device %p filled with db file data\n", udev_device);
182         return 0;
183 }
184
185 int udev_device_read_uevent_file(struct udev_device *udev_device)
186 {
187         char filename[UTIL_PATH_SIZE];
188         FILE *f;
189         char line[UTIL_LINE_SIZE];
190         int maj = 0;
191         int min = 0;
192
193         util_strlcpy(filename, udev_device->syspath, sizeof(filename));
194         util_strlcat(filename, "/uevent", sizeof(filename));
195         f = fopen(filename, "r");
196         if (f == NULL)
197                 return -1;
198
199         while (fgets(line, sizeof(line), f)) {
200                 char *pos;
201
202                 pos = strchr(line, '\n');
203                 if (pos == NULL)
204                         continue;
205                 pos[0] = '\0';
206
207                 if (strncmp(line, "MAJOR=", 6) == 0)
208                         maj = strtoull(&line[6], NULL, 10);
209                 else if (strncmp(line, "MINOR=", 6) == 0)
210                         min = strtoull(&line[6], NULL, 10);
211
212                 udev_device_add_property_from_string(udev_device, line);
213         }
214
215         udev_device->devnum = makedev(maj, min);
216
217         fclose(f);
218         return 0;
219 }
220
221 static void device_load_info(struct udev_device *device)
222 {
223         device->info_loaded = 1;
224         udev_device_read_uevent_file(device);
225         udev_device_read_db(device);
226 }
227
228 void udev_device_set_info_loaded(struct udev_device *device)
229 {
230         device->info_loaded = 1;
231 }
232
233 struct udev_device *device_new(struct udev *udev)
234 {
235         struct udev_device *udev_device;
236         struct udev_list_entry *list_entry;
237
238         if (udev == NULL)
239                 return NULL;
240
241         udev_device = calloc(1, sizeof(struct udev_device));
242         if (udev_device == NULL)
243                 return NULL;
244         udev_device->refcount = 1;
245         udev_device->udev = udev;
246         udev_list_init(&udev_device->devlinks_list);
247         udev_list_init(&udev_device->properties_list);
248         udev_list_init(&udev_device->sysattr_list);
249         udev_device->event_timeout = -1;
250         /* copy global properties */
251         udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
252                 udev_device_add_property(udev_device,
253                                          udev_list_entry_get_name(list_entry),
254                                          udev_list_entry_get_value(list_entry));
255         dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
256         return udev_device;
257 }
258
259 /**
260  * udev_device_new_from_syspath:
261  * @udev: udev library context
262  * @syspath: sys device path including sys directory
263  *
264  * Create new udev device, and fill in information from the sys
265  * device and the udev database entry. The sypath is the absolute
266  * path to the device, including the sys mount point.
267  *
268  * The initial refcount is 1, and needs to be decremented to
269  * release the resources of the udev device.
270  *
271  * Returns: a new udev device, or #NULL, if it does not exist
272  **/
273 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
274 {
275         size_t len;
276         const char *subdir;
277         char path[UTIL_PATH_SIZE];
278         char *pos;
279         struct stat statbuf;
280         struct udev_device *udev_device;
281
282         if (udev == NULL)
283                 return NULL;
284         if (syspath == NULL)
285                 return NULL;
286
287         /* path starts in sys */
288         len = strlen(udev_get_sys_path(udev));
289         if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
290                 info(udev, "not in sys :%s\n", syspath);
291                 return NULL;
292         }
293
294         /* path is not a root directory */
295         subdir = &syspath[len+1];
296         pos = strrchr(subdir, '/');
297         if (pos == NULL || pos < &subdir[2]) {
298                 dbg(udev, "not a subdir :%s\n", syspath);
299                 return NULL;
300         }
301
302         /* resolve possible symlink to real path */
303         util_strlcpy(path, syspath, sizeof(path));
304         util_resolve_sys_link(udev, path, sizeof(path));
305
306         /* try to resolve the silly block layout if needed */
307         if (strncmp(&path[len], "/block/", 7) == 0) {
308                 char block[UTIL_PATH_SIZE];
309                 char part[UTIL_PATH_SIZE];
310
311                 util_strlcpy(block, path, sizeof(block));
312                 pos = strrchr(block, '/');
313                 if (pos == NULL)
314                         return NULL;
315                 util_strlcpy(part, pos, sizeof(part));
316                 pos[0] = '\0';
317                 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
318                         util_strlcpy(path, block, sizeof(path));
319                         util_strlcat(path, part, sizeof(path));
320                 }
321         }
322
323         /* path exists in sys */
324         if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
325             strncmp(&syspath[len], "/class/", 7) == 0 ||
326             strncmp(&syspath[len], "/block/", 7) == 0) {
327                 char file[UTIL_PATH_SIZE];
328
329                 /* all "devices" require a "uevent" file */
330                 util_strlcpy(file, path, sizeof(file));
331                 util_strlcat(file, "/uevent", sizeof(file));
332                 if (stat(file, &statbuf) != 0) {
333                         dbg(udev, "not a device: %s\n", syspath);
334                         return NULL;
335                 }
336         } else {
337                 /* everything else just needs to be a directory */
338                 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
339                         dbg(udev, "directory not found: %s\n", syspath);
340                         return NULL;
341                 }
342         }
343
344         udev_device = device_new(udev);
345         if (udev_device == NULL)
346                 return NULL;
347
348         udev_device_set_syspath(udev_device, path);
349         info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
350
351         return udev_device;
352 }
353
354 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
355 {
356         char path[UTIL_PATH_SIZE];
357         const char *type_str;
358         struct udev_enumerate *udev_enumerate;
359         struct udev_list_entry *list_entry;
360         struct udev_device *device = NULL;
361
362         if (type == 'b')
363                 type_str = "block";
364         else if (type == 'c')
365                 type_str = "char";
366         else
367                 return NULL;
368
369         /* /sys/dev/{block,char}/<maj>:<min> link */
370         snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
371                  type_str, major(devnum), minor(devnum));
372         if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
373                 return udev_device_new_from_syspath(udev, path);
374
375         udev_enumerate = udev_enumerate_new(udev);
376         if (udev_enumerate == NULL)
377                 return NULL;
378
379         /* fallback to search sys devices for the major/minor */
380         if (type == 'b')
381                 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
382         else if (type == 'c')
383                 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
384         udev_enumerate_scan_devices(udev_enumerate);
385         udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
386                 struct udev_device *device_loop;
387
388                 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
389                 if (device_loop != NULL) {
390                         if (udev_device_get_devnum(device_loop) == devnum) {
391                                 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
392                                         continue;
393                                 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
394                                         continue;
395                                 device = device_loop;
396                                 break;
397                         }
398                         udev_device_unref(device_loop);
399                 }
400         }
401         udev_enumerate_unref(udev_enumerate);
402         return device;
403 }
404
405 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
406 {
407         size_t sys_path_len;
408         char path_full[UTIL_PATH_SIZE];
409         char *path;
410         struct stat statbuf;
411
412         sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
413         path = &path_full[sys_path_len];
414
415         if (strcmp(subsystem, "subsystem") == 0) {
416                 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
417                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
418                 if (stat(path_full, &statbuf) == 0)
419                         goto found;
420
421                 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
422                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
423                 if (stat(path_full, &statbuf) == 0)
424                         goto found;
425
426                 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
427                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
428                 if (stat(path_full, &statbuf) == 0)
429                         goto found;
430                 goto out;
431         }
432
433         if (strcmp(subsystem, "module") == 0) {
434                 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
435                 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
436                 if (stat(path_full, &statbuf) == 0)
437                         goto found;
438                 goto out;
439         }
440
441         if (strcmp(subsystem, "drivers") == 0) {
442                 char subsys[UTIL_NAME_SIZE];
443                 char *driver;
444
445                 util_strlcpy(subsys, sysname, sizeof(subsys));
446                 driver = strchr(subsys, ':');
447                 if (driver != NULL) {
448                         driver[0] = '\0';
449                         driver = &driver[1];
450                         util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
451                         util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
452                         util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
453                         util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
454                         if (stat(path_full, &statbuf) == 0)
455                                 goto found;
456
457                         util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
458                         util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
459                         util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
460                         util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
461                         if (stat(path_full, &statbuf) == 0)
462                                 goto found;
463                 }
464                 goto out;
465         }
466
467         util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
468         util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
469         util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
470         util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
471         if (stat(path_full, &statbuf) == 0)
472                 goto found;
473
474         util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
475         util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
476         util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
477         util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
478         if (stat(path_full, &statbuf) == 0)
479                 goto found;
480
481         util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
482         util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
483         util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
484         util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
485         if (stat(path_full, &statbuf) == 0)
486                 goto found;
487 out:
488         return NULL;
489 found:
490         return udev_device_new_from_syspath(udev, path_full);
491 }
492
493 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
494 {
495         struct udev_device *udev_device_parent = NULL;
496         char path[UTIL_PATH_SIZE];
497         const char *subdir;
498
499         /* follow "device" link in deprecated sys layout */
500         if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
501             strncmp(udev_device->devpath, "/block/", 7) == 0) {
502                 util_strlcpy(path, udev_device->syspath, sizeof(path));
503                 util_strlcat(path, "/device", sizeof(path));
504                 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
505                         udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
506                         if (udev_device_parent != NULL)
507                                 return udev_device_parent;
508                 }
509         }
510
511         util_strlcpy(path, udev_device->syspath, sizeof(path));
512         subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
513         while (1) {
514                 char *pos;
515
516                 pos = strrchr(subdir, '/');
517                 if (pos == NULL || pos < &subdir[2])
518                         break;
519                 pos[0] = '\0';
520                 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
521                 if (udev_device_parent != NULL)
522                         return udev_device_parent;
523         }
524         return NULL;
525 }
526
527 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
528 {
529         if (udev_device == NULL)
530                 return NULL;
531         if (!udev_device->parent_set) {
532                 udev_device->parent_set = 1;
533                 udev_device->parent_device = device_new_from_parent(udev_device);
534         }
535         if (udev_device->parent_device != NULL)
536                 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
537         return udev_device->parent_device;
538 }
539
540 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
541 {
542         struct udev_device *parent;
543
544         parent = udev_device_get_parent(udev_device);
545         while (parent != NULL) {
546                 const char *parent_subsystem;
547
548                 parent_subsystem = udev_device_get_subsystem(parent);
549                 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
550                         break;
551                 parent = udev_device_get_parent(parent);
552         }
553         return parent;
554 }
555
556 /**
557  * udev_device_get_udev:
558  * @udev_device: udev device
559  *
560  * Retrieve the udev library context the device was created with.
561  *
562  * Returns: the udev library context
563  **/
564 struct udev *udev_device_get_udev(struct udev_device *udev_device)
565 {
566         if (udev_device == NULL)
567                 return NULL;
568         return udev_device->udev;
569 }
570
571 /**
572  * udev_device_ref:
573  * @udev_device: udev device
574  *
575  * Take a reference of a udev device.
576  *
577  * Returns: the passed udev device
578  **/
579 struct udev_device *udev_device_ref(struct udev_device *udev_device)
580 {
581         if (udev_device == NULL)
582                 return NULL;
583         udev_device->refcount++;
584         return udev_device;
585 }
586
587 /**
588  * udev_device_unref:
589  * @udev_device: udev device
590  *
591  * Drop a reference of a udev device. If the refcount reaches zero,
592  * the resources of the device will be released.
593  *
594  **/
595 void udev_device_unref(struct udev_device *udev_device)
596 {
597         if (udev_device == NULL)
598                 return;
599         udev_device->refcount--;
600         if (udev_device->refcount > 0)
601                 return;
602         if (udev_device->parent_device != NULL)
603                 udev_device_unref(udev_device->parent_device);
604         free(udev_device->syspath);
605         free(udev_device->sysname);
606         free(udev_device->devnode);
607         free(udev_device->subsystem);
608         udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
609         udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
610         free(udev_device->action);
611         free(udev_device->driver);
612         free(udev_device->devpath_old);
613         free(udev_device->physdevpath);
614         udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
615         free(udev_device->envp);
616         free(udev_device->monitor_buf);
617         dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
618         free(udev_device);
619 }
620
621 /**
622  * udev_device_get_devpath:
623  * @udev_device: udev device
624  *
625  * Retrieve the kernel devpath value of the udev device. The path
626  * does not contain the sys mount point, and starts with a '/'.
627  *
628  * Returns: the devpath of the udev device
629  **/
630 const char *udev_device_get_devpath(struct udev_device *udev_device)
631 {
632         if (udev_device == NULL)
633                 return NULL;
634         return udev_device->devpath;
635 }
636
637 /**
638  * udev_device_get_syspath:
639  * @udev_device: udev device
640  *
641  * Retrieve the sys path of the udev device. The path is an
642  * absolute path and starts with the sys mount point.
643  *
644  * Returns: the sys path of the udev device
645  **/
646 const char *udev_device_get_syspath(struct udev_device *udev_device)
647 {
648         if (udev_device == NULL)
649                 return NULL;
650         return udev_device->syspath;
651 }
652
653 const char *udev_device_get_sysname(struct udev_device *udev_device)
654 {
655         if (udev_device == NULL)
656                 return NULL;
657         return udev_device->sysname;
658 }
659
660 const char *udev_device_get_sysnum(struct udev_device *udev_device)
661 {
662         if (udev_device == NULL)
663                 return NULL;
664         return udev_device->sysnum;
665 }
666
667 /**
668  * udev_device_get_devnode:
669  * @udev_device: udev device
670  *
671  * Retrieve the device node file name belonging to the udev device.
672  * The path is an absolute path, and starts with the device directory.
673  *
674  * Returns: the device node file name of the udev device, or #NULL if no device node exists
675  **/
676 const char *udev_device_get_devnode(struct udev_device *udev_device)
677 {
678         if (udev_device == NULL)
679                 return NULL;
680         if (!udev_device->info_loaded)
681                 device_load_info(udev_device);
682         return udev_device->devnode;
683 }
684
685 /**
686  * udev_device_get_subsystem:
687  * @udev_device: udev device
688  *
689  * Retrieve the subsystem string of the udev device. The string does not
690  * contain any "/".
691  *
692  * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
693  **/
694 const char *udev_device_get_subsystem(struct udev_device *udev_device)
695 {
696         char subsystem[UTIL_NAME_SIZE];
697
698         if (udev_device == NULL)
699                 return NULL;
700         if (!udev_device->subsystem_set) {
701                 udev_device->subsystem_set = 1;
702                 /* read "subsytem" link */
703                 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
704                         udev_device_set_subsystem(udev_device, subsystem);
705                         return udev_device->subsystem;
706                 }
707                 /* implicit names */
708                 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
709                         udev_device_set_subsystem(udev_device, "module");
710                         return udev_device->subsystem;
711                 }
712                 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
713                         udev_device_set_subsystem(udev_device, "drivers");
714                         return udev_device->subsystem;
715                 }
716                 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
717                     strncmp(udev_device->devpath, "/class/", 7) == 0 ||
718                     strncmp(udev_device->devpath, "/bus/", 5) == 0) {
719                         udev_device_set_subsystem(udev_device, "subsystem");
720                         return udev_device->subsystem;
721                 }
722         }
723         return udev_device->subsystem;
724 }
725
726 /**
727  * udev_device_get_devlinks_list_entry:
728  * @udev_device: udev device
729  *
730  * Retrieve the list of device links pointing to the device file of
731  * the udev device. The next list entry can be retrieved with
732  * udev_list_entry_next(), which returns #NULL if no more entries exist.
733  * The devlink path can be retrieved from the list entry by
734  * udev_list_entry_get_name(). The path is an absolute path, and starts with
735  * the device directory.
736  *
737  * Returns: the first entry of the device node link list
738  **/
739 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
740 {
741         if (udev_device == NULL)
742                 return NULL;
743         if (!udev_device->info_loaded)
744                 device_load_info(udev_device);
745         return udev_list_get_entry(&udev_device->devlinks_list);
746 }
747
748 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
749 {
750         udev_device->devlinks_uptodate = 0;
751         udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
752 }
753
754 /**
755  * udev_device_get_properties_list_entry:
756  * @udev_device: udev device
757  *
758  * Retrieve the list of key/value device properties of the udev
759  * device. The next list entry can be retrieved with udev_list_entry_next(),
760  * which returns #NULL if no more entries exist. The property name
761  * can be retrieved from the list entry by udev_list_get_name(),
762  * the property value by udev_list_get_value().
763  *
764  * Returns: the first entry of the property list
765  **/
766 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
767 {
768         if (udev_device == NULL)
769                 return NULL;
770         if (!udev_device->info_loaded)
771                 device_load_info(udev_device);
772         if (!udev_device->devlinks_uptodate) {
773                 char symlinks[UTIL_PATH_SIZE];
774                 struct udev_list_entry *list_entry;
775
776                 udev_device->devlinks_uptodate = 1;
777                 list_entry = udev_device_get_devlinks_list_entry(udev_device);
778                 if (list_entry != NULL) {
779                         util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
780                         udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
781                                 util_strlcat(symlinks, " ", sizeof(symlinks));
782                                 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
783                         }
784                         udev_device_add_property(udev_device, "DEVLINKS", symlinks);
785                 }
786         }
787         return udev_list_get_entry(&udev_device->properties_list);
788 }
789
790 const char *udev_device_get_driver(struct udev_device *udev_device)
791 {
792         char driver[UTIL_NAME_SIZE];
793
794         if (udev_device == NULL)
795                 return NULL;
796         if (!udev_device->driver_set) {
797                 udev_device->driver_set = 1;
798                 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
799                         udev_device->driver = strdup(driver);
800         }
801         return udev_device->driver;
802 }
803
804 dev_t udev_device_get_devnum(struct udev_device *udev_device)
805 {
806         if (udev_device == NULL)
807                 return makedev(0, 0);
808         if (!udev_device->info_loaded)
809                 device_load_info(udev_device);
810         return udev_device->devnum;
811 }
812
813 const char *udev_device_get_action(struct udev_device *udev_device)
814 {
815         if (udev_device == NULL)
816                 return NULL;
817         return udev_device->action;
818 }
819
820 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
821 {
822         if (udev_device == NULL)
823                 return 0;
824         return udev_device->seqnum;
825 }
826
827 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
828 {
829         struct udev_list_entry *list_entry;
830         char path[UTIL_PATH_SIZE];
831         char value[UTIL_NAME_SIZE];
832         struct stat statbuf;
833         int fd;
834         ssize_t size;
835         const char *val = NULL;
836
837         if (udev_device == NULL)
838                 return NULL;
839         if (sysattr == NULL)
840                 return NULL;
841
842         /* look for possibly already cached result */
843         udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
844                 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
845                         dbg(udev_device->udev, "got '%s' (%s) from cache\n",
846                             sysattr, udev_list_entry_get_value(list_entry));
847                         return udev_list_entry_get_value(list_entry);
848                 }
849         }
850
851         util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
852         util_strlcat(path, "/", sizeof(path));
853         util_strlcat(path, sysattr, sizeof(path));
854
855         if (lstat(path, &statbuf) != 0) {
856                 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
857                 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
858                 goto out;
859         }
860
861         if (S_ISLNK(statbuf.st_mode)) {
862                 /* links return the last element of the target path */
863                 char target[UTIL_NAME_SIZE];
864                 int len;
865                 char *pos;
866
867                 len = readlink(path, target, sizeof(target));
868                 if (len > 0) {
869                         target[len] = '\0';
870                         pos = strrchr(target, '/');
871                         if (pos != NULL) {
872                                 pos = &pos[1];
873                                 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
874                                 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
875                                 val = udev_list_entry_get_value(list_entry);
876                         }
877                 }
878                 goto out;
879         }
880
881         /* skip directories */
882         if (S_ISDIR(statbuf.st_mode))
883                 goto out;
884
885         /* skip non-readable files */
886         if ((statbuf.st_mode & S_IRUSR) == 0)
887                 goto out;
888
889         /* read attribute value */
890         fd = open(path, O_RDONLY);
891         if (fd < 0) {
892                 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
893                 goto out;
894         }
895         size = read(fd, value, sizeof(value));
896         close(fd);
897         if (size < 0)
898                 goto out;
899         if (size == sizeof(value))
900                 goto out;
901
902         /* got a valid value, store it in cache and return it */
903         value[size] = '\0';
904         util_remove_trailing_chars(value, '\n');
905         dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
906         list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
907         val = udev_list_entry_get_value(list_entry);
908 out:
909         return val;
910 }
911
912 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
913 {
914         const char *pos;
915         size_t len;
916
917         free(udev_device->syspath);
918         udev_device->syspath = strdup(syspath);
919         if (udev_device->syspath ==  NULL)
920                 return -ENOMEM;
921         udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
922         udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
923
924         pos = strrchr(udev_device->syspath, '/');
925         if (pos == NULL)
926                 return -EINVAL;
927         udev_device->sysname = strdup(&pos[1]);
928         if (udev_device->sysname == NULL)
929                 return -ENOMEM;
930
931         /* some devices have '!' in their name, change that to '/' */
932         len = 0;
933         while (udev_device->sysname[len] != '\0') {
934                 if (udev_device->sysname[len] == '!')
935                         udev_device->sysname[len] = '/';
936                 len++;
937         }
938
939         /* trailing number */
940         while (len > 0 && isdigit(udev_device->sysname[--len]))
941                 udev_device->sysnum = &udev_device->sysname[len];
942
943         /* sysname is completely numeric */
944         if (len == 0)
945                 udev_device->sysnum = NULL;
946
947         return 0;
948 }
949
950 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
951 {
952         free(udev_device->subsystem);
953         udev_device->subsystem = strdup(subsystem);
954         if (udev_device->subsystem == NULL)
955                 return -ENOMEM;
956         udev_device->subsystem_set = 1;
957         udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
958         return 0;
959 }
960
961 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
962 {
963         free(udev_device->devnode);
964         udev_device->devnode = strdup(devnode);
965         if (devnode == NULL)
966                 return 0;
967         if (udev_device->devnode == NULL)
968                 return -ENOMEM;
969         udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
970         return 0;
971 }
972
973 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
974 {
975         udev_device->devlinks_uptodate = 0;
976         if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
977                 return -ENOMEM;
978         return 0;
979 }
980
981 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
982 {
983         udev_device->envp_uptodate = 0;
984         if (value == NULL) {
985                 struct udev_list_entry *list_entry;
986
987                 list_entry = udev_device_get_properties_list_entry(udev_device);
988                 list_entry = udev_list_entry_get_by_name(list_entry, key);
989                 if (list_entry != NULL)
990                         udev_list_entry_delete(list_entry);
991                 return NULL;
992         }
993         return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
994 }
995
996 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
997 {
998         char name[UTIL_PATH_SIZE];
999         char *val;
1000
1001         util_strlcpy(name, property, sizeof(name));
1002         val = strchr(name, '=');
1003         if (val == NULL)
1004                 return NULL;
1005         val[0] = '\0';
1006         val = &val[1];
1007         if (val[0] == '\0')
1008                 val = NULL;
1009         return udev_device_add_property(udev_device, name, val);
1010 }
1011
1012 #define ENVP_SIZE                       128
1013 #define MONITOR_BUF_SIZE                4096
1014 static int update_envp_monitor_buf(struct udev_device *udev_device)
1015 {
1016         const char *action;
1017         struct udev_list_entry *list_entry;
1018         size_t bufpos;
1019         size_t len;
1020         unsigned int i;
1021
1022         /* monitor buffer of property strings */
1023         free(udev_device->monitor_buf);
1024         udev_device->monitor_buf_len = 0;
1025         udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1026         if (udev_device->monitor_buf == NULL)
1027                 return -ENOMEM;
1028
1029         /* envp array, strings will point into monitor buffer */
1030         if (udev_device->envp == NULL)
1031                 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1032         if (udev_device->envp == NULL)
1033                 return -ENOMEM;
1034
1035         /* header <action>@<devpath> */
1036         action = udev_device_get_action(udev_device);
1037         if (action == NULL)
1038                 return -EINVAL;
1039         bufpos = util_strlcpy(udev_device->monitor_buf, action, MONITOR_BUF_SIZE);
1040         len = util_strlcpy(&udev_device->monitor_buf[bufpos], "@", MONITOR_BUF_SIZE-bufpos);
1041         if (len >= MONITOR_BUF_SIZE-bufpos)
1042                 return -EINVAL;
1043         bufpos += len;
1044         len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1045                            udev_device_get_devpath(udev_device),
1046                            MONITOR_BUF_SIZE-bufpos);
1047         if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1048                 return -EINVAL;
1049         bufpos += len+1;
1050
1051         i = 0;
1052         udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1053                 /* add string to envp array */
1054                 udev_device->envp[i++] = &udev_device->monitor_buf[bufpos];
1055                 if (i+1 >= ENVP_SIZE)
1056                         return -EINVAL;
1057
1058                 /* add property string to monitor buffer */
1059                 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1060                                    udev_list_entry_get_name(list_entry), MONITOR_BUF_SIZE-bufpos);
1061                 if (len >= MONITOR_BUF_SIZE-bufpos)
1062                         return -EINVAL;
1063                 bufpos += len;
1064                 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "=", MONITOR_BUF_SIZE-bufpos);
1065                 if (len >= MONITOR_BUF_SIZE-bufpos)
1066                         return -EINVAL;
1067                 bufpos += len;
1068                 len = util_strlcpy(&udev_device->monitor_buf[bufpos], udev_list_entry_get_value(list_entry),
1069                                    MONITOR_BUF_SIZE-bufpos);
1070                 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1071                         return -EINVAL;
1072                 bufpos += len+1;
1073         }
1074         udev_device->envp[i] = NULL;
1075         udev_device->monitor_buf_len = bufpos;
1076         udev_device->envp_uptodate = 1;
1077         dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n", i, bufpos);
1078         return 0;
1079 }
1080
1081 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1082 {
1083         if (!udev_device->envp_uptodate)
1084                 if (update_envp_monitor_buf(udev_device) != 0)
1085                         return NULL;
1086         return udev_device->envp;
1087 }
1088
1089 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1090 {
1091         if (!udev_device->envp_uptodate)
1092                 if (update_envp_monitor_buf(udev_device) != 0)
1093                         return -EINVAL;
1094         *buf = udev_device->monitor_buf;
1095         return udev_device->monitor_buf_len;
1096 }
1097
1098 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1099 {
1100         free(udev_device->action);
1101         udev_device->action = strdup(action);
1102         if (udev_device->action == NULL)
1103                 return -ENOMEM;
1104         udev_device_add_property(udev_device, "ACTION", udev_device->action);
1105         return 0;
1106 }
1107
1108 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1109 {
1110         free(udev_device->driver);
1111         udev_device->driver = strdup(driver);
1112         if (udev_device->driver == NULL)
1113                 return -ENOMEM;
1114         udev_device->driver_set = 1;
1115         udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1116         return 0;
1117 }
1118
1119 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1120 {
1121         return udev_device->devpath_old;
1122 }
1123
1124 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1125 {
1126         udev_device->devpath_old = strdup(devpath_old);
1127         if (udev_device->devpath_old == NULL)
1128                 return -ENOMEM;
1129         udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1130         return 0;
1131 }
1132
1133 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1134 {
1135         return udev_device->physdevpath;
1136 }
1137
1138 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1139 {
1140         udev_device->physdevpath = strdup(physdevpath);
1141         if (udev_device->physdevpath == NULL)
1142                 return -ENOMEM;
1143         return 0;
1144 }
1145
1146 int udev_device_get_timeout(struct udev_device *udev_device)
1147 {
1148         return udev_device->timeout;
1149 }
1150
1151 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1152 {
1153         udev_device->timeout = timeout;
1154         return 0;
1155 }
1156 int udev_device_get_event_timeout(struct udev_device *udev_device)
1157 {
1158         if (!udev_device->info_loaded)
1159                 device_load_info(udev_device);
1160         return udev_device->event_timeout;
1161 }
1162
1163 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1164 {
1165         udev_device->event_timeout = event_timeout;
1166         return 0;
1167 }
1168
1169 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1170 {
1171         char num[32];
1172
1173         udev_device->seqnum = seqnum;
1174         snprintf(num, sizeof(num), "%llu", seqnum);
1175         udev_device_add_property(udev_device, "SEQNUM", num);
1176         return 0;
1177 }
1178
1179 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1180 {
1181         char num[32];
1182
1183         udev_device->devnum = devnum;
1184
1185         snprintf(num, sizeof(num), "%u", major(devnum));
1186         udev_device_add_property(udev_device, "MAJOR", num);
1187         snprintf(num, sizeof(num), "%u", minor(devnum));
1188         udev_device_add_property(udev_device, "MINOR", num);
1189         return 0;
1190 }
1191
1192 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1193 {
1194         if (!udev_device->info_loaded)
1195                 device_load_info(udev_device);
1196         return udev_device->num_fake_partitions;
1197 }
1198
1199 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1200 {
1201         udev_device->num_fake_partitions = num;
1202         return 0;
1203 }
1204
1205 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1206 {
1207         if (!udev_device->info_loaded)
1208                 device_load_info(udev_device);
1209         return udev_device->devlink_priority;
1210 }
1211
1212 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1213 {
1214          udev_device->devlink_priority = prio;
1215         return 0;
1216 }
1217
1218 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1219 {
1220         if (!udev_device->info_loaded)
1221                 device_load_info(udev_device);
1222         return udev_device->ignore_remove;
1223 }
1224
1225 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1226 {
1227         udev_device->ignore_remove = ignore;
1228         return 0;
1229 }