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