2 * libudev - interface to udev device information
4 * Copyright (C) 2008 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
24 #include "libudev-private.h"
28 struct udev_device *parent_device;
42 size_t monitor_buf_len;
43 struct udev_list_node devlinks_list;
44 struct udev_list_node properties_list;
45 struct udev_list_node sysattr_list;
46 unsigned long long int seqnum;
49 int num_fake_partitions;
54 unsigned int parent_set:1;
55 unsigned int subsystem_set:1;
56 unsigned int devtype_set:1;
57 unsigned int devlinks_uptodate:1;
58 unsigned int envp_uptodate:1;
59 unsigned int driver_set:1;
60 unsigned int info_loaded:1;
61 unsigned int ignore_remove:1;
64 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
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);
75 int udev_device_read_db(struct udev_device *udev_device)
78 char filename[UTIL_PATH_SIZE];
79 char line[UTIL_LINE_SIZE];
82 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
84 if (lstat(filename, &stats) != 0) {
85 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
88 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
89 char target[UTIL_PATH_SIZE];
90 char devnode[UTIL_PATH_SIZE];
94 target_len = readlink(filename, target, sizeof(target));
96 target[target_len] = '\0';
98 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
102 next = strchr(target, ' ');
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];
116 next = strchr(next, ' ');
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);
126 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
130 f = fopen(filename, "r");
132 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
135 while (fgets(line, sizeof(line), f)) {
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);
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);
158 udev_device_set_devlink_priority(udev_device, atoi(val));
161 udev_device_set_event_timeout(udev_device, atoi(val));
164 udev_device_set_num_fake_partitions(udev_device, atoi(val));
167 udev_device_set_ignore_remove(udev_device, atoi(val));
170 udev_device_add_property_from_string(udev_device, val);
173 udev_device_set_watch_handle(udev_device, atoi(val));
179 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
183 int udev_device_read_uevent_file(struct udev_device *udev_device)
185 char filename[UTIL_PATH_SIZE];
187 char line[UTIL_LINE_SIZE];
191 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
192 util_strlcat(filename, "/uevent", sizeof(filename));
193 f = fopen(filename, "r");
197 while (fgets(line, sizeof(line), f)) {
200 pos = strchr(line, '\n');
205 if (strncmp(line, "DEVTYPE=", 8) == 0)
206 udev_device_set_devtype(udev_device, &line[8]);
207 else if (strncmp(line, "MAJOR=", 6) == 0)
208 maj = strtoull(&line[6], NULL, 10);
209 else if (strncmp(line, "MINOR=", 6) == 0)
210 min = strtoull(&line[6], NULL, 10);
211 else if (strncmp(line, "DEVNAME=", 8) == 0)
212 udev_device_set_knodename(udev_device, &line[8]);
214 udev_device_add_property_from_string(udev_device, line);
217 udev_device->devnum = makedev(maj, min);
223 static void device_load_info(struct udev_device *device)
225 device->info_loaded = 1;
226 udev_device_read_uevent_file(device);
227 udev_device_read_db(device);
230 void udev_device_set_info_loaded(struct udev_device *device)
232 device->info_loaded = 1;
235 struct udev_device *device_new(struct udev *udev)
237 struct udev_device *udev_device;
238 struct udev_list_entry *list_entry;
243 udev_device = calloc(1, sizeof(struct udev_device));
244 if (udev_device == NULL)
246 udev_device->refcount = 1;
247 udev_device->udev = udev;
248 udev_list_init(&udev_device->devlinks_list);
249 udev_list_init(&udev_device->properties_list);
250 udev_list_init(&udev_device->sysattr_list);
251 udev_device->event_timeout = -1;
252 udev_device->watch_handle = -1;
253 /* copy global properties */
254 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
255 udev_device_add_property(udev_device,
256 udev_list_entry_get_name(list_entry),
257 udev_list_entry_get_value(list_entry));
258 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
263 * udev_device_new_from_syspath:
264 * @udev: udev library context
265 * @syspath: sys device path including sys directory
267 * Create new udev device, and fill in information from the sys
268 * device and the udev database entry. The sypath is the absolute
269 * path to the device, including the sys mount point.
271 * The initial refcount is 1, and needs to be decremented to
272 * release the resources of the udev device.
274 * Returns: a new udev device, or #NULL, if it does not exist
276 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
280 char path[UTIL_PATH_SIZE];
283 struct udev_device *udev_device;
290 /* path starts in sys */
291 len = strlen(udev_get_sys_path(udev));
292 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
293 info(udev, "not in sys :%s\n", syspath);
297 /* path is not a root directory */
298 subdir = &syspath[len+1];
299 pos = strrchr(subdir, '/');
300 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
301 dbg(udev, "not a subdir :%s\n", syspath);
305 /* resolve possible symlink to real path */
306 util_strlcpy(path, syspath, sizeof(path));
307 util_resolve_sys_link(udev, path, sizeof(path));
309 /* try to resolve the silly block layout if needed */
310 if (strncmp(&path[len], "/block/", 7) == 0) {
311 char block[UTIL_PATH_SIZE];
312 char part[UTIL_PATH_SIZE];
314 util_strlcpy(block, path, sizeof(block));
315 pos = strrchr(block, '/');
318 util_strlcpy(part, pos, sizeof(part));
320 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
321 util_strlcpy(path, block, sizeof(path));
322 util_strlcat(path, part, sizeof(path));
326 /* path exists in sys */
327 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
328 strncmp(&syspath[len], "/class/", 7) == 0 ||
329 strncmp(&syspath[len], "/block/", 7) == 0) {
330 char file[UTIL_PATH_SIZE];
332 /* all "devices" require a "uevent" file */
333 util_strlcpy(file, path, sizeof(file));
334 util_strlcat(file, "/uevent", sizeof(file));
335 if (stat(file, &statbuf) != 0) {
336 dbg(udev, "not a device: %s\n", syspath);
340 /* everything else just needs to be a directory */
341 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
342 dbg(udev, "directory not found: %s\n", syspath);
347 udev_device = device_new(udev);
348 if (udev_device == NULL)
351 udev_device_set_syspath(udev_device, path);
352 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
357 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
359 char path[UTIL_PATH_SIZE];
360 const char *type_str;
361 struct udev_enumerate *udev_enumerate;
362 struct udev_list_entry *list_entry;
363 struct udev_device *device = NULL;
367 else if (type == 'c')
372 /* /sys/dev/{block,char}/<maj>:<min> link */
373 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
374 type_str, major(devnum), minor(devnum));
375 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
376 return udev_device_new_from_syspath(udev, path);
378 udev_enumerate = udev_enumerate_new(udev);
379 if (udev_enumerate == NULL)
382 /* fallback to search sys devices for the major/minor */
384 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
385 else if (type == 'c')
386 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
387 udev_enumerate_scan_devices(udev_enumerate);
388 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
389 struct udev_device *device_loop;
391 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
392 if (device_loop != NULL) {
393 if (udev_device_get_devnum(device_loop) == devnum) {
394 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
396 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
398 device = device_loop;
401 udev_device_unref(device_loop);
404 udev_enumerate_unref(udev_enumerate);
408 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
411 char path_full[UTIL_PATH_SIZE];
415 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
416 path = &path_full[sys_path_len];
418 if (strcmp(subsystem, "subsystem") == 0) {
419 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
420 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
421 if (stat(path_full, &statbuf) == 0)
424 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
425 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
426 if (stat(path_full, &statbuf) == 0)
429 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
430 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
431 if (stat(path_full, &statbuf) == 0)
436 if (strcmp(subsystem, "module") == 0) {
437 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
438 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
439 if (stat(path_full, &statbuf) == 0)
444 if (strcmp(subsystem, "drivers") == 0) {
445 char subsys[UTIL_NAME_SIZE];
448 util_strlcpy(subsys, sysname, sizeof(subsys));
449 driver = strchr(subsys, ':');
450 if (driver != NULL) {
453 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
454 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
455 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
456 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
457 if (stat(path_full, &statbuf) == 0)
460 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
461 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
462 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
463 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
464 if (stat(path_full, &statbuf) == 0)
470 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
471 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
472 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
473 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
474 if (stat(path_full, &statbuf) == 0)
477 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
478 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
479 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
480 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
481 if (stat(path_full, &statbuf) == 0)
484 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
485 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
486 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
487 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
488 if (stat(path_full, &statbuf) == 0)
493 return udev_device_new_from_syspath(udev, path_full);
496 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
498 struct udev_device *udev_device_parent = NULL;
499 char path[UTIL_PATH_SIZE];
502 /* follow "device" link in deprecated sys layout */
503 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
504 strncmp(udev_device->devpath, "/block/", 7) == 0) {
505 util_strlcpy(path, udev_device->syspath, sizeof(path));
506 util_strlcat(path, "/device", sizeof(path));
507 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 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;
514 util_strlcpy(path, udev_device->syspath, sizeof(path));
515 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
519 pos = strrchr(subdir, '/');
520 if (pos == NULL || pos < &subdir[2])
523 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
524 if (udev_device_parent != NULL)
525 return udev_device_parent;
530 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
532 if (udev_device == NULL)
534 if (!udev_device->parent_set) {
535 udev_device->parent_set = 1;
536 udev_device->parent_device = device_new_from_parent(udev_device);
538 if (udev_device->parent_device != NULL)
539 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
540 return udev_device->parent_device;
543 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
545 struct udev_device *parent;
547 if (subsystem == NULL)
550 parent = udev_device_get_parent(udev_device);
551 while (parent != NULL) {
552 const char *parent_subsystem;
553 const char *parent_devtype;
555 parent_subsystem = udev_device_get_subsystem(parent);
556 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
559 parent_devtype = udev_device_get_devtype(parent);
560 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
563 parent = udev_device_get_parent(parent);
569 * udev_device_get_udev:
570 * @udev_device: udev device
572 * Retrieve the udev library context the device was created with.
574 * Returns: the udev library context
576 struct udev *udev_device_get_udev(struct udev_device *udev_device)
578 if (udev_device == NULL)
580 return udev_device->udev;
585 * @udev_device: udev device
587 * Take a reference of a udev device.
589 * Returns: the passed udev device
591 struct udev_device *udev_device_ref(struct udev_device *udev_device)
593 if (udev_device == NULL)
595 udev_device->refcount++;
601 * @udev_device: udev device
603 * Drop a reference of a udev device. If the refcount reaches zero,
604 * the resources of the device will be released.
607 void udev_device_unref(struct udev_device *udev_device)
609 if (udev_device == NULL)
611 udev_device->refcount--;
612 if (udev_device->refcount > 0)
614 if (udev_device->parent_device != NULL)
615 udev_device_unref(udev_device->parent_device);
616 free(udev_device->syspath);
617 free(udev_device->sysname);
618 free(udev_device->devnode);
619 free(udev_device->subsystem);
620 free(udev_device->devtype);
621 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
622 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
623 free(udev_device->action);
624 free(udev_device->driver);
625 free(udev_device->devpath_old);
626 free(udev_device->knodename);
627 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
628 free(udev_device->envp);
629 free(udev_device->monitor_buf);
630 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
635 * udev_device_get_devpath:
636 * @udev_device: udev device
638 * Retrieve the kernel devpath value of the udev device. The path
639 * does not contain the sys mount point, and starts with a '/'.
641 * Returns: the devpath of the udev device
643 const char *udev_device_get_devpath(struct udev_device *udev_device)
645 if (udev_device == NULL)
647 return udev_device->devpath;
651 * udev_device_get_syspath:
652 * @udev_device: udev device
654 * Retrieve the sys path of the udev device. The path is an
655 * absolute path and starts with the sys mount point.
657 * Returns: the sys path of the udev device
659 const char *udev_device_get_syspath(struct udev_device *udev_device)
661 if (udev_device == NULL)
663 return udev_device->syspath;
666 const char *udev_device_get_sysname(struct udev_device *udev_device)
668 if (udev_device == NULL)
670 return udev_device->sysname;
673 const char *udev_device_get_sysnum(struct udev_device *udev_device)
675 if (udev_device == NULL)
677 return udev_device->sysnum;
681 * udev_device_get_devnode:
682 * @udev_device: udev device
684 * Retrieve the device node file name belonging to the udev device.
685 * The path is an absolute path, and starts with the device directory.
687 * Returns: the device node file name of the udev device, or #NULL if no device node exists
689 const char *udev_device_get_devnode(struct udev_device *udev_device)
691 if (udev_device == NULL)
693 if (!udev_device->info_loaded)
694 device_load_info(udev_device);
695 return udev_device->devnode;
699 * udev_device_get_subsystem:
700 * @udev_device: udev device
702 * Retrieve the subsystem string of the udev device. The string does not
705 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
707 const char *udev_device_get_subsystem(struct udev_device *udev_device)
709 char subsystem[UTIL_NAME_SIZE];
711 if (udev_device == NULL)
713 if (!udev_device->subsystem_set) {
714 udev_device->subsystem_set = 1;
715 /* read "subsytem" link */
716 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
717 udev_device_set_subsystem(udev_device, subsystem);
718 return udev_device->subsystem;
721 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
722 udev_device_set_subsystem(udev_device, "module");
723 return udev_device->subsystem;
725 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
726 udev_device_set_subsystem(udev_device, "drivers");
727 return udev_device->subsystem;
729 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
730 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
731 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
732 udev_device_set_subsystem(udev_device, "subsystem");
733 return udev_device->subsystem;
736 return udev_device->subsystem;
740 * udev_device_get_devtype:
741 * @udev_device: udev device
743 * Retrieve the devtype string of the udev device.
745 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
747 const char *udev_device_get_devtype(struct udev_device *udev_device)
749 if (udev_device == NULL)
751 if (!udev_device->devtype_set) {
752 udev_device->devtype_set = 1;
753 if (!udev_device->info_loaded)
754 udev_device_read_uevent_file(udev_device);
756 return udev_device->devtype;
760 * udev_device_get_devlinks_list_entry:
761 * @udev_device: udev device
763 * Retrieve the list of device links pointing to the device file of
764 * the udev device. The next list entry can be retrieved with
765 * udev_list_entry_next(), which returns #NULL if no more entries exist.
766 * The devlink path can be retrieved from the list entry by
767 * udev_list_entry_get_name(). The path is an absolute path, and starts with
768 * the device directory.
770 * Returns: the first entry of the device node link list
772 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
774 if (udev_device == NULL)
776 if (!udev_device->info_loaded)
777 device_load_info(udev_device);
778 return udev_list_get_entry(&udev_device->devlinks_list);
781 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
783 udev_device->devlinks_uptodate = 0;
784 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
788 * udev_device_get_properties_list_entry:
789 * @udev_device: udev device
791 * Retrieve the list of key/value device properties of the udev
792 * device. The next list entry can be retrieved with udev_list_entry_next(),
793 * which returns #NULL if no more entries exist. The property name
794 * can be retrieved from the list entry by udev_list_get_name(),
795 * the property value by udev_list_get_value().
797 * Returns: the first entry of the property list
799 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
801 if (udev_device == NULL)
803 if (!udev_device->info_loaded)
804 device_load_info(udev_device);
805 if (!udev_device->devlinks_uptodate) {
806 char symlinks[UTIL_PATH_SIZE];
807 struct udev_list_entry *list_entry;
809 udev_device->devlinks_uptodate = 1;
810 list_entry = udev_device_get_devlinks_list_entry(udev_device);
811 if (list_entry != NULL) {
812 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
813 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
814 util_strlcat(symlinks, " ", sizeof(symlinks));
815 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
817 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
820 return udev_list_get_entry(&udev_device->properties_list);
823 const char *udev_device_get_driver(struct udev_device *udev_device)
825 char driver[UTIL_NAME_SIZE];
827 if (udev_device == NULL)
829 if (!udev_device->driver_set) {
830 udev_device->driver_set = 1;
831 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
832 udev_device->driver = strdup(driver);
834 return udev_device->driver;
837 dev_t udev_device_get_devnum(struct udev_device *udev_device)
839 if (udev_device == NULL)
840 return makedev(0, 0);
841 if (!udev_device->info_loaded)
842 device_load_info(udev_device);
843 return udev_device->devnum;
846 const char *udev_device_get_action(struct udev_device *udev_device)
848 if (udev_device == NULL)
850 return udev_device->action;
853 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
855 if (udev_device == NULL)
857 return udev_device->seqnum;
860 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
862 struct udev_list_entry *list_entry;
863 char path[UTIL_PATH_SIZE];
868 const char *val = NULL;
870 if (udev_device == NULL)
875 /* look for possibly already cached result */
876 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
877 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
878 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
879 sysattr, udev_list_entry_get_value(list_entry));
880 return udev_list_entry_get_value(list_entry);
884 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
885 util_strlcat(path, "/", sizeof(path));
886 util_strlcat(path, sysattr, sizeof(path));
888 if (lstat(path, &statbuf) != 0) {
889 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
890 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
894 if (S_ISLNK(statbuf.st_mode)) {
895 char target[UTIL_NAME_SIZE];
899 /* some core links return the last element of the target path */
900 if (strcmp(sysattr, "driver") != 0 &&
901 strcmp(sysattr, "subsystem") != 0 &&
902 strcmp(sysattr, "module") != 0)
905 len = readlink(path, target, sizeof(target));
908 pos = strrchr(target, '/');
911 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
912 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
913 val = udev_list_entry_get_value(list_entry);
919 /* skip directories */
920 if (S_ISDIR(statbuf.st_mode))
923 /* skip non-readable files */
924 if ((statbuf.st_mode & S_IRUSR) == 0)
927 /* read attribute value */
928 fd = open(path, O_RDONLY);
930 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
933 size = read(fd, value, sizeof(value));
937 if (size == sizeof(value))
940 /* got a valid value, store it in cache and return it */
942 util_remove_trailing_chars(value, '\n');
943 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
944 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
945 val = udev_list_entry_get_value(list_entry);
950 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
955 free(udev_device->syspath);
956 udev_device->syspath = strdup(syspath);
957 if (udev_device->syspath == NULL)
959 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
960 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
962 pos = strrchr(udev_device->syspath, '/');
965 udev_device->sysname = strdup(&pos[1]);
966 if (udev_device->sysname == NULL)
969 /* some devices have '!' in their name, change that to '/' */
971 while (udev_device->sysname[len] != '\0') {
972 if (udev_device->sysname[len] == '!')
973 udev_device->sysname[len] = '/';
977 /* trailing number */
978 while (len > 0 && isdigit(udev_device->sysname[--len]))
979 udev_device->sysnum = &udev_device->sysname[len];
981 /* sysname is completely numeric */
983 udev_device->sysnum = NULL;
988 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
990 free(udev_device->subsystem);
991 udev_device->subsystem = strdup(subsystem);
992 if (udev_device->subsystem == NULL)
994 udev_device->subsystem_set = 1;
995 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
999 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1001 free(udev_device->devtype);
1002 udev_device->devtype = strdup(devtype);
1003 if (udev_device->devtype == NULL)
1005 udev_device->devtype_set = 1;
1006 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1010 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1012 free(udev_device->devnode);
1013 udev_device->devnode = strdup(devnode);
1014 if (devnode == NULL)
1016 if (udev_device->devnode == NULL)
1018 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1022 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
1024 udev_device->devlinks_uptodate = 0;
1025 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
1030 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1032 udev_device->envp_uptodate = 0;
1033 if (value == NULL) {
1034 struct udev_list_entry *list_entry;
1036 list_entry = udev_device_get_properties_list_entry(udev_device);
1037 list_entry = udev_list_entry_get_by_name(list_entry, key);
1038 if (list_entry != NULL)
1039 udev_list_entry_delete(list_entry);
1042 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1045 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1047 char name[UTIL_PATH_SIZE];
1050 util_strlcpy(name, property, sizeof(name));
1051 val = strchr(name, '=');
1058 return udev_device_add_property(udev_device, name, val);
1061 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1063 struct udev_list_entry *list_entry;
1065 if (udev_device == NULL)
1070 list_entry = udev_device_get_properties_list_entry(udev_device);
1071 list_entry = udev_list_entry_get_by_name(list_entry, key);
1072 return udev_list_entry_get_value(list_entry);
1075 #define ENVP_SIZE 128
1076 #define MONITOR_BUF_SIZE 4096
1077 static int update_envp_monitor_buf(struct udev_device *udev_device)
1079 struct udev_list_entry *list_entry;
1084 /* monitor buffer of property strings */
1085 free(udev_device->monitor_buf);
1086 udev_device->monitor_buf_len = 0;
1087 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1088 if (udev_device->monitor_buf == NULL)
1091 /* envp array, strings will point into monitor buffer */
1092 if (udev_device->envp == NULL)
1093 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1094 if (udev_device->envp == NULL)
1099 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1100 /* add string to envp array */
1101 udev_device->envp[i++] = &udev_device->monitor_buf[bufpos];
1102 if (i+1 >= ENVP_SIZE)
1105 /* add property string to monitor buffer */
1106 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1107 udev_list_entry_get_name(list_entry), MONITOR_BUF_SIZE-bufpos);
1108 if (len >= MONITOR_BUF_SIZE-bufpos)
1111 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "=", MONITOR_BUF_SIZE-bufpos);
1112 if (len >= MONITOR_BUF_SIZE-bufpos)
1115 len = util_strlcpy(&udev_device->monitor_buf[bufpos], udev_list_entry_get_value(list_entry),
1116 MONITOR_BUF_SIZE-bufpos);
1117 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1121 udev_device->envp[i] = NULL;
1122 udev_device->monitor_buf_len = bufpos;
1123 udev_device->envp_uptodate = 1;
1124 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n", i, bufpos);
1128 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1130 if (!udev_device->envp_uptodate)
1131 if (update_envp_monitor_buf(udev_device) != 0)
1133 return udev_device->envp;
1136 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1138 if (!udev_device->envp_uptodate)
1139 if (update_envp_monitor_buf(udev_device) != 0)
1141 *buf = udev_device->monitor_buf;
1142 return udev_device->monitor_buf_len;
1145 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1147 free(udev_device->action);
1148 udev_device->action = strdup(action);
1149 if (udev_device->action == NULL)
1151 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1155 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1157 free(udev_device->driver);
1158 udev_device->driver = strdup(driver);
1159 if (udev_device->driver == NULL)
1161 udev_device->driver_set = 1;
1162 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1166 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1168 return udev_device->devpath_old;
1171 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1173 udev_device->devpath_old = strdup(devpath_old);
1174 if (udev_device->devpath_old == NULL)
1176 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1180 const char *udev_device_get_knodename(struct udev_device *udev_device)
1182 return udev_device->knodename;
1185 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1187 udev_device->knodename = strdup(knodename);
1188 if (udev_device->knodename == NULL)
1193 int udev_device_get_timeout(struct udev_device *udev_device)
1195 return udev_device->timeout;
1198 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1200 udev_device->timeout = timeout;
1203 int udev_device_get_event_timeout(struct udev_device *udev_device)
1205 if (!udev_device->info_loaded)
1206 device_load_info(udev_device);
1207 return udev_device->event_timeout;
1210 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1212 udev_device->event_timeout = event_timeout;
1216 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1220 udev_device->seqnum = seqnum;
1221 snprintf(num, sizeof(num), "%llu", seqnum);
1222 udev_device_add_property(udev_device, "SEQNUM", num);
1226 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1230 udev_device->devnum = devnum;
1232 snprintf(num, sizeof(num), "%u", major(devnum));
1233 udev_device_add_property(udev_device, "MAJOR", num);
1234 snprintf(num, sizeof(num), "%u", minor(devnum));
1235 udev_device_add_property(udev_device, "MINOR", num);
1239 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1241 if (!udev_device->info_loaded)
1242 device_load_info(udev_device);
1243 return udev_device->num_fake_partitions;
1246 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1248 udev_device->num_fake_partitions = num;
1252 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1254 if (!udev_device->info_loaded)
1255 device_load_info(udev_device);
1256 return udev_device->devlink_priority;
1259 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1261 udev_device->devlink_priority = prio;
1265 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1267 if (!udev_device->info_loaded)
1268 device_load_info(udev_device);
1269 return udev_device->ignore_remove;
1272 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1274 udev_device->ignore_remove = ignore;
1278 int udev_device_get_watch_handle(struct udev_device *udev_device)
1280 if (!udev_device->info_loaded)
1281 device_load_info(udev_device);
1282 return udev_device->watch_handle;
1285 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1287 udev_device->watch_handle = handle;