2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
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.
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.
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/>.
32 #include "libudev-private.h"
36 struct udev_device *parent_device;
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;
57 int num_fake_partitions;
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;
72 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
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);
83 int udev_device_read_db(struct udev_device *udev_device)
86 char filename[UTIL_PATH_SIZE];
87 char line[UTIL_LINE_SIZE];
90 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
92 if (lstat(filename, &stats) != 0) {
93 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
96 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
97 char target[UTIL_PATH_SIZE];
98 char devnode[UTIL_PATH_SIZE];
102 target_len = readlink(filename, target, sizeof(target));
104 target[target_len] = '\0';
106 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
110 next = strchr(target, ' ');
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];
124 next = strchr(next, ' ');
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);
134 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
138 f = fopen(filename, "r");
140 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
143 while (fgets(line, sizeof(line), f)) {
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);
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);
166 udev_device_set_devlink_priority(udev_device, atoi(val));
169 udev_device_set_event_timeout(udev_device, atoi(val));
172 udev_device_set_num_fake_partitions(udev_device, atoi(val));
175 udev_device_set_ignore_remove(udev_device, atoi(val));
178 udev_device_add_property_from_string(udev_device, val);
181 udev_device_set_watch_handle(udev_device, atoi(val));
187 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
191 int udev_device_read_uevent_file(struct udev_device *udev_device)
193 char filename[UTIL_PATH_SIZE];
195 char line[UTIL_LINE_SIZE];
199 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
200 util_strlcat(filename, "/uevent", sizeof(filename));
201 f = fopen(filename, "r");
205 while (fgets(line, sizeof(line), f)) {
208 pos = strchr(line, '\n');
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);
220 udev_device_add_property_from_string(udev_device, line);
223 udev_device->devnum = makedev(maj, min);
229 static void device_load_info(struct udev_device *device)
231 device->info_loaded = 1;
232 udev_device_read_uevent_file(device);
233 udev_device_read_db(device);
236 void udev_device_set_info_loaded(struct udev_device *device)
238 device->info_loaded = 1;
241 struct udev_device *device_new(struct udev *udev)
243 struct udev_device *udev_device;
244 struct udev_list_entry *list_entry;
249 udev_device = calloc(1, sizeof(struct udev_device));
250 if (udev_device == 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);
269 * udev_device_new_from_syspath:
270 * @udev: udev library context
271 * @syspath: sys device path including sys directory
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.
277 * The initial refcount is 1, and needs to be decremented to
278 * release the resources of the udev device.
280 * Returns: a new udev device, or #NULL, if it does not exist
282 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
286 char path[UTIL_PATH_SIZE];
289 struct udev_device *udev_device;
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);
303 /* path is not a root directory */
304 subdir = &syspath[len+1];
305 pos = strrchr(subdir, '/');
306 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
307 dbg(udev, "not a subdir :%s\n", syspath);
311 /* resolve possible symlink to real path */
312 util_strlcpy(path, syspath, sizeof(path));
313 util_resolve_sys_link(udev, path, sizeof(path));
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];
320 util_strlcpy(block, path, sizeof(block));
321 pos = strrchr(block, '/');
324 util_strlcpy(part, pos, sizeof(part));
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));
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];
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);
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);
353 udev_device = device_new(udev);
354 if (udev_device == NULL)
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));
363 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
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;
373 else if (type == 'c')
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);
384 udev_enumerate = udev_enumerate_new(udev);
385 if (udev_enumerate == NULL)
388 /* fallback to search sys devices for the major/minor */
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;
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)
402 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
404 device = device_loop;
407 udev_device_unref(device_loop);
410 udev_enumerate_unref(udev_enumerate);
414 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
417 char path_full[UTIL_PATH_SIZE];
421 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
422 path = &path_full[sys_path_len];
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)
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)
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)
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)
450 if (strcmp(subsystem, "drivers") == 0) {
451 char subsys[UTIL_NAME_SIZE];
454 util_strlcpy(subsys, sysname, sizeof(subsys));
455 driver = strchr(subsys, ':');
456 if (driver != NULL) {
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)
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)
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)
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)
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)
499 return udev_device_new_from_syspath(udev, path_full);
502 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
504 struct udev_device *udev_device_parent = NULL;
505 char path[UTIL_PATH_SIZE];
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;
520 util_strlcpy(path, udev_device->syspath, sizeof(path));
521 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
525 pos = strrchr(subdir, '/');
526 if (pos == NULL || pos < &subdir[2])
529 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
530 if (udev_device_parent != NULL)
531 return udev_device_parent;
536 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
538 if (udev_device == NULL)
540 if (!udev_device->parent_set) {
541 udev_device->parent_set = 1;
542 udev_device->parent_device = device_new_from_parent(udev_device);
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;
549 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
551 struct udev_device *parent;
553 if (subsystem == NULL)
556 parent = udev_device_get_parent(udev_device);
557 while (parent != NULL) {
558 const char *parent_subsystem;
559 const char *parent_devtype;
561 parent_subsystem = udev_device_get_subsystem(parent);
562 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
565 parent_devtype = udev_device_get_devtype(parent);
566 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
569 parent = udev_device_get_parent(parent);
575 * udev_device_get_udev:
576 * @udev_device: udev device
578 * Retrieve the udev library context the device was created with.
580 * Returns: the udev library context
582 struct udev *udev_device_get_udev(struct udev_device *udev_device)
584 if (udev_device == NULL)
586 return udev_device->udev;
591 * @udev_device: udev device
593 * Take a reference of a udev device.
595 * Returns: the passed udev device
597 struct udev_device *udev_device_ref(struct udev_device *udev_device)
599 if (udev_device == NULL)
601 udev_device->refcount++;
607 * @udev_device: udev device
609 * Drop a reference of a udev device. If the refcount reaches zero,
610 * the resources of the device will be released.
613 void udev_device_unref(struct udev_device *udev_device)
615 if (udev_device == NULL)
617 udev_device->refcount--;
618 if (udev_device->refcount > 0)
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);
641 * udev_device_get_devpath:
642 * @udev_device: udev device
644 * Retrieve the kernel devpath value of the udev device. The path
645 * does not contain the sys mount point, and starts with a '/'.
647 * Returns: the devpath of the udev device
649 const char *udev_device_get_devpath(struct udev_device *udev_device)
651 if (udev_device == NULL)
653 return udev_device->devpath;
657 * udev_device_get_syspath:
658 * @udev_device: udev device
660 * Retrieve the sys path of the udev device. The path is an
661 * absolute path and starts with the sys mount point.
663 * Returns: the sys path of the udev device
665 const char *udev_device_get_syspath(struct udev_device *udev_device)
667 if (udev_device == NULL)
669 return udev_device->syspath;
672 const char *udev_device_get_sysname(struct udev_device *udev_device)
674 if (udev_device == NULL)
676 return udev_device->sysname;
679 const char *udev_device_get_sysnum(struct udev_device *udev_device)
681 if (udev_device == NULL)
683 return udev_device->sysnum;
687 * udev_device_get_devnode:
688 * @udev_device: udev device
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.
693 * Returns: the device node file name of the udev device, or #NULL if no device node exists
695 const char *udev_device_get_devnode(struct udev_device *udev_device)
697 if (udev_device == NULL)
699 if (!udev_device->info_loaded)
700 device_load_info(udev_device);
701 return udev_device->devnode;
705 * udev_device_get_subsystem:
706 * @udev_device: udev device
708 * Retrieve the subsystem string of the udev device. The string does not
711 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
713 const char *udev_device_get_subsystem(struct udev_device *udev_device)
715 char subsystem[UTIL_NAME_SIZE];
717 if (udev_device == 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;
727 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
728 udev_device_set_subsystem(udev_device, "module");
729 return udev_device->subsystem;
731 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
732 udev_device_set_subsystem(udev_device, "drivers");
733 return udev_device->subsystem;
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;
742 return udev_device->subsystem;
746 * udev_device_get_devtype:
747 * @udev_device: udev device
749 * Retrieve the devtype string of the udev device.
751 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
753 const char *udev_device_get_devtype(struct udev_device *udev_device)
755 if (udev_device == 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);
762 return udev_device->devtype;
766 * udev_device_get_devlinks_list_entry:
767 * @udev_device: udev device
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.
776 * Returns: the first entry of the device node link list
778 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
780 if (udev_device == NULL)
782 if (!udev_device->info_loaded)
783 device_load_info(udev_device);
784 return udev_list_get_entry(&udev_device->devlinks_list);
787 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
789 udev_device->devlinks_uptodate = 0;
790 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
794 * udev_device_get_properties_list_entry:
795 * @udev_device: udev device
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().
803 * Returns: the first entry of the property list
805 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
807 if (udev_device == 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;
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));
823 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
826 return udev_list_get_entry(&udev_device->properties_list);
829 const char *udev_device_get_driver(struct udev_device *udev_device)
831 char driver[UTIL_NAME_SIZE];
833 if (udev_device == 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);
840 return udev_device->driver;
843 dev_t udev_device_get_devnum(struct udev_device *udev_device)
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;
852 const char *udev_device_get_action(struct udev_device *udev_device)
854 if (udev_device == NULL)
856 return udev_device->action;
859 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
861 if (udev_device == NULL)
863 return udev_device->seqnum;
866 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
868 struct udev_list_entry *list_entry;
869 char path[UTIL_PATH_SIZE];
874 const char *val = NULL;
876 if (udev_device == NULL)
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);
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));
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);
900 if (S_ISLNK(statbuf.st_mode)) {
901 char target[UTIL_NAME_SIZE];
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)
911 len = readlink(path, target, sizeof(target));
914 pos = strrchr(target, '/');
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);
925 /* skip directories */
926 if (S_ISDIR(statbuf.st_mode))
929 /* skip non-readable files */
930 if ((statbuf.st_mode & S_IRUSR) == 0)
933 /* read attribute value */
934 fd = open(path, O_RDONLY);
936 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
939 size = read(fd, value, sizeof(value));
943 if (size == sizeof(value))
946 /* got a valid value, store it in cache and return it */
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);
956 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
961 free(udev_device->syspath);
962 udev_device->syspath = strdup(syspath);
963 if (udev_device->syspath == NULL)
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);
968 pos = strrchr(udev_device->syspath, '/');
971 udev_device->sysname = strdup(&pos[1]);
972 if (udev_device->sysname == NULL)
975 /* some devices have '!' in their name, change that to '/' */
977 while (udev_device->sysname[len] != '\0') {
978 if (udev_device->sysname[len] == '!')
979 udev_device->sysname[len] = '/';
983 /* trailing number */
984 while (len > 0 && isdigit(udev_device->sysname[--len]))
985 udev_device->sysnum = &udev_device->sysname[len];
987 /* sysname is completely numeric */
989 udev_device->sysnum = NULL;
994 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
996 free(udev_device->subsystem);
997 udev_device->subsystem = strdup(subsystem);
998 if (udev_device->subsystem == NULL)
1000 udev_device->subsystem_set = 1;
1001 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1005 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1007 free(udev_device->devtype);
1008 udev_device->devtype = strdup(devtype);
1009 if (udev_device->devtype == NULL)
1011 udev_device->devtype_set = 1;
1012 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1016 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1018 free(udev_device->devnode);
1019 udev_device->devnode = strdup(devnode);
1020 if (devnode == NULL)
1022 if (udev_device->devnode == NULL)
1024 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1028 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
1030 udev_device->devlinks_uptodate = 0;
1031 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
1036 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1038 udev_device->envp_uptodate = 0;
1039 if (value == NULL) {
1040 struct udev_list_entry *list_entry;
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);
1048 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1051 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1053 char name[UTIL_PATH_SIZE];
1056 util_strlcpy(name, property, sizeof(name));
1057 val = strchr(name, '=');
1064 return udev_device_add_property(udev_device, name, val);
1067 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1069 struct udev_list_entry *list_entry;
1071 if (udev_device == NULL)
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);
1081 #define ENVP_SIZE 128
1082 #define MONITOR_BUF_SIZE 4096
1083 static int update_envp_monitor_buf(struct udev_device *udev_device)
1086 struct udev_list_entry *list_entry;
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)
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)
1104 /* header <action>@<devpath> */
1105 action = udev_device_get_action(udev_device);
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)
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)
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)
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)
1133 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "=", MONITOR_BUF_SIZE-bufpos);
1134 if (len >= MONITOR_BUF_SIZE-bufpos)
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)
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);
1150 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1152 if (!udev_device->envp_uptodate)
1153 if (update_envp_monitor_buf(udev_device) != 0)
1155 return udev_device->envp;
1158 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1160 if (!udev_device->envp_uptodate)
1161 if (update_envp_monitor_buf(udev_device) != 0)
1163 *buf = udev_device->monitor_buf;
1164 return udev_device->monitor_buf_len;
1167 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1169 free(udev_device->action);
1170 udev_device->action = strdup(action);
1171 if (udev_device->action == NULL)
1173 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1177 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1179 free(udev_device->driver);
1180 udev_device->driver = strdup(driver);
1181 if (udev_device->driver == NULL)
1183 udev_device->driver_set = 1;
1184 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1188 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1190 return udev_device->devpath_old;
1193 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1195 udev_device->devpath_old = strdup(devpath_old);
1196 if (udev_device->devpath_old == NULL)
1198 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1202 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1204 return udev_device->physdevpath;
1207 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1209 udev_device->physdevpath = strdup(physdevpath);
1210 if (udev_device->physdevpath == NULL)
1215 int udev_device_get_timeout(struct udev_device *udev_device)
1217 return udev_device->timeout;
1220 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1222 udev_device->timeout = timeout;
1225 int udev_device_get_event_timeout(struct udev_device *udev_device)
1227 if (!udev_device->info_loaded)
1228 device_load_info(udev_device);
1229 return udev_device->event_timeout;
1232 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1234 udev_device->event_timeout = event_timeout;
1238 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1242 udev_device->seqnum = seqnum;
1243 snprintf(num, sizeof(num), "%llu", seqnum);
1244 udev_device_add_property(udev_device, "SEQNUM", num);
1248 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1252 udev_device->devnum = devnum;
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);
1261 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1263 if (!udev_device->info_loaded)
1264 device_load_info(udev_device);
1265 return udev_device->num_fake_partitions;
1268 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1270 udev_device->num_fake_partitions = num;
1274 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1276 if (!udev_device->info_loaded)
1277 device_load_info(udev_device);
1278 return udev_device->devlink_priority;
1281 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1283 udev_device->devlink_priority = prio;
1287 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1289 if (!udev_device->info_loaded)
1290 device_load_info(udev_device);
1291 return udev_device->ignore_remove;
1294 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1296 udev_device->ignore_remove = ignore;
1300 int udev_device_get_watch_handle(struct udev_device *udev_device)
1302 if (!udev_device->info_loaded)
1303 device_load_info(udev_device);
1304 return udev_device->watch_handle;
1307 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1309 udev_device->watch_handle = handle;