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