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