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;
49 size_t monitor_buf_len;
50 struct udev_list_node devlinks_list;
51 struct udev_list_node properties_list;
52 struct udev_list_node sysattr_list;
53 unsigned long long int seqnum;
56 int num_fake_partitions;
60 unsigned int parent_set:1;
61 unsigned int subsystem_set:1;
62 unsigned int devlinks_uptodate:1;
63 unsigned int envp_uptodate:1;
64 unsigned int driver_set:1;
65 unsigned int info_loaded:1;
66 unsigned int ignore_remove:1;
69 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
73 /* translate to location of db file */
74 util_strlcpy(filename, udev_get_dev_path(udev), len);
75 start = util_strlcat(filename, "/.udev/db/", len);
76 util_strlcat(filename, devpath, len);
77 return util_path_encode(&filename[start], len - start);
80 int udev_device_read_db(struct udev_device *udev_device)
83 char filename[UTIL_PATH_SIZE];
84 char line[UTIL_LINE_SIZE];
87 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
89 if (lstat(filename, &stats) != 0) {
90 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
93 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
94 char target[UTIL_PATH_SIZE];
95 char devnode[UTIL_PATH_SIZE];
99 target_len = readlink(filename, target, sizeof(target));
101 target[target_len] = '\0';
103 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
107 next = strchr(target, ' ');
112 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
113 util_strlcat(devnode, "/", sizeof(devnode));
114 util_strlcat(devnode, target, sizeof(devnode));
115 udev_device_set_devnode(udev_device, devnode);
116 while (next != NULL) {
117 char devlink[UTIL_PATH_SIZE];
121 next = strchr(next, ' ');
126 util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
127 util_strlcat(devlink, "/", sizeof(devlink));
128 util_strlcat(devlink, lnk, sizeof(devlink));
129 udev_device_add_devlink(udev_device, devlink);
131 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
135 f = fopen(filename, "r");
137 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
140 while (fgets(line, sizeof(line), f)) {
151 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
152 util_strlcat(filename, "/", sizeof(filename));
153 util_strlcat(filename, val, sizeof(filename));
154 udev_device_set_devnode(udev_device, filename);
157 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
158 util_strlcat(filename, "/", sizeof(filename));
159 util_strlcat(filename, val, sizeof(filename));
160 udev_device_add_devlink(udev_device, filename);
163 udev_device_set_devlink_priority(udev_device, atoi(val));
166 udev_device_set_event_timeout(udev_device, atoi(val));
169 udev_device_set_num_fake_partitions(udev_device, atoi(val));
172 udev_device_set_ignore_remove(udev_device, atoi(val));
175 udev_device_add_property_from_string(udev_device, val);
181 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
185 int udev_device_read_uevent_file(struct udev_device *udev_device)
187 char filename[UTIL_PATH_SIZE];
189 char line[UTIL_LINE_SIZE];
193 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
194 util_strlcat(filename, "/uevent", sizeof(filename));
195 f = fopen(filename, "r");
199 while (fgets(line, sizeof(line), f)) {
202 pos = strchr(line, '\n');
207 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);
212 udev_device_add_property_from_string(udev_device, line);
215 udev_device->devnum = makedev(maj, min);
221 static void device_load_info(struct udev_device *device)
223 device->info_loaded = 1;
224 udev_device_read_uevent_file(device);
225 udev_device_read_db(device);
228 void udev_device_set_info_loaded(struct udev_device *device)
230 device->info_loaded = 1;
233 struct udev_device *device_new(struct udev *udev)
235 struct udev_device *udev_device;
236 struct udev_list_entry *list_entry;
241 udev_device = calloc(1, sizeof(struct udev_device));
242 if (udev_device == NULL)
244 udev_device->refcount = 1;
245 udev_device->udev = udev;
246 udev_list_init(&udev_device->devlinks_list);
247 udev_list_init(&udev_device->properties_list);
248 udev_list_init(&udev_device->sysattr_list);
249 udev_device->event_timeout = -1;
250 /* copy global properties */
251 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
252 udev_device_add_property(udev_device,
253 udev_list_entry_get_name(list_entry),
254 udev_list_entry_get_value(list_entry));
255 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
260 * udev_device_new_from_syspath:
261 * @udev: udev library context
262 * @syspath: sys device path including sys directory
264 * Create new udev device, and fill in information from the sys
265 * device and the udev database entry. The sypath is the absolute
266 * path to the device, including the sys mount point.
268 * The initial refcount is 1, and needs to be decremented to
269 * release the resources of the udev device.
271 * Returns: a new udev device, or #NULL, if it does not exist
273 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
277 char path[UTIL_PATH_SIZE];
280 struct udev_device *udev_device;
287 /* path starts in sys */
288 len = strlen(udev_get_sys_path(udev));
289 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
290 info(udev, "not in sys :%s\n", syspath);
294 /* path is not a root directory */
295 subdir = &syspath[len+1];
296 pos = strrchr(subdir, '/');
297 if (pos == NULL || pos < &subdir[2]) {
298 dbg(udev, "not a subdir :%s\n", syspath);
302 /* resolve possible symlink to real path */
303 util_strlcpy(path, syspath, sizeof(path));
304 util_resolve_sys_link(udev, path, sizeof(path));
306 /* try to resolve the silly block layout if needed */
307 if (strncmp(&path[len], "/block/", 7) == 0) {
308 char block[UTIL_PATH_SIZE];
309 char part[UTIL_PATH_SIZE];
311 util_strlcpy(block, path, sizeof(block));
312 pos = strrchr(block, '/');
315 util_strlcpy(part, pos, sizeof(part));
317 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
318 util_strlcpy(path, block, sizeof(path));
319 util_strlcat(path, part, sizeof(path));
323 /* path exists in sys */
324 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
325 strncmp(&syspath[len], "/class/", 7) == 0 ||
326 strncmp(&syspath[len], "/block/", 7) == 0) {
327 char file[UTIL_PATH_SIZE];
329 /* all "devices" require a "uevent" file */
330 util_strlcpy(file, path, sizeof(file));
331 util_strlcat(file, "/uevent", sizeof(file));
332 if (stat(file, &statbuf) != 0) {
333 dbg(udev, "not a device: %s\n", syspath);
337 /* everything else just needs to be a directory */
338 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
339 dbg(udev, "directory not found: %s\n", syspath);
344 udev_device = device_new(udev);
345 if (udev_device == NULL)
348 udev_device_set_syspath(udev_device, path);
349 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
354 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
356 char path[UTIL_PATH_SIZE];
357 const char *type_str;
358 struct udev_enumerate *udev_enumerate;
359 struct udev_list_entry *list_entry;
360 struct udev_device *device = NULL;
364 else if (type == 'c')
369 /* /sys/dev/{block,char}/<maj>:<min> link */
370 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
371 type_str, major(devnum), minor(devnum));
372 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
373 return udev_device_new_from_syspath(udev, path);
375 udev_enumerate = udev_enumerate_new(udev);
376 if (udev_enumerate == NULL)
379 /* fallback to search sys devices for the major/minor */
381 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
382 else if (type == 'c')
383 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
384 udev_enumerate_scan_devices(udev_enumerate);
385 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
386 struct udev_device *device_loop;
388 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
389 if (device_loop != NULL) {
390 if (udev_device_get_devnum(device_loop) == devnum) {
391 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
393 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
395 device = device_loop;
398 udev_device_unref(device_loop);
401 udev_enumerate_unref(udev_enumerate);
405 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
408 char path_full[UTIL_PATH_SIZE];
412 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
413 path = &path_full[sys_path_len];
415 if (strcmp(subsystem, "subsystem") == 0) {
416 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
417 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
418 if (stat(path_full, &statbuf) == 0)
421 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
422 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
423 if (stat(path_full, &statbuf) == 0)
426 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
427 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
428 if (stat(path_full, &statbuf) == 0)
433 if (strcmp(subsystem, "module") == 0) {
434 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
435 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
436 if (stat(path_full, &statbuf) == 0)
441 if (strcmp(subsystem, "drivers") == 0) {
442 char subsys[UTIL_NAME_SIZE];
445 util_strlcpy(subsys, sysname, sizeof(subsys));
446 driver = strchr(subsys, ':');
447 if (driver != NULL) {
450 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
451 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
452 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
453 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
454 if (stat(path_full, &statbuf) == 0)
457 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
458 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
459 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
460 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
461 if (stat(path_full, &statbuf) == 0)
467 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
468 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
469 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
470 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
471 if (stat(path_full, &statbuf) == 0)
474 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
475 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
476 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
477 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
478 if (stat(path_full, &statbuf) == 0)
481 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
482 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
483 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
484 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
485 if (stat(path_full, &statbuf) == 0)
490 return udev_device_new_from_syspath(udev, path_full);
493 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
495 struct udev_device *udev_device_parent = NULL;
496 char path[UTIL_PATH_SIZE];
499 /* follow "device" link in deprecated sys layout */
500 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
501 strncmp(udev_device->devpath, "/block/", 7) == 0) {
502 util_strlcpy(path, udev_device->syspath, sizeof(path));
503 util_strlcat(path, "/device", sizeof(path));
504 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
505 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
506 if (udev_device_parent != NULL)
507 return udev_device_parent;
511 util_strlcpy(path, udev_device->syspath, sizeof(path));
512 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
516 pos = strrchr(subdir, '/');
517 if (pos == NULL || pos < &subdir[2])
520 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
521 if (udev_device_parent != NULL)
522 return udev_device_parent;
527 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
529 if (udev_device == NULL)
531 if (!udev_device->parent_set) {
532 udev_device->parent_set = 1;
533 udev_device->parent_device = device_new_from_parent(udev_device);
535 if (udev_device->parent_device != NULL)
536 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
537 return udev_device->parent_device;
540 struct udev_device *udev_device_get_parent_with_subsystem(struct udev_device *udev_device, const char *subsystem)
542 struct udev_device *parent;
544 parent = udev_device_get_parent(udev_device);
545 while (parent != NULL) {
546 const char *parent_subsystem;
548 parent_subsystem = udev_device_get_subsystem(parent);
549 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0)
551 parent = udev_device_get_parent(parent);
557 * udev_device_get_udev:
558 * @udev_device: udev device
560 * Retrieve the udev library context the device was created with.
562 * Returns: the udev library context
564 struct udev *udev_device_get_udev(struct udev_device *udev_device)
566 if (udev_device == NULL)
568 return udev_device->udev;
573 * @udev_device: udev device
575 * Take a reference of a udev device.
577 * Returns: the passed udev device
579 struct udev_device *udev_device_ref(struct udev_device *udev_device)
581 if (udev_device == NULL)
583 udev_device->refcount++;
589 * @udev_device: udev device
591 * Drop a reference of a udev device. If the refcount reaches zero,
592 * the resources of the device will be released.
595 void udev_device_unref(struct udev_device *udev_device)
597 if (udev_device == NULL)
599 udev_device->refcount--;
600 if (udev_device->refcount > 0)
602 if (udev_device->parent_device != NULL)
603 udev_device_unref(udev_device->parent_device);
604 free(udev_device->syspath);
605 free(udev_device->sysname);
606 free(udev_device->devnode);
607 free(udev_device->subsystem);
608 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
609 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
610 free(udev_device->action);
611 free(udev_device->driver);
612 free(udev_device->devpath_old);
613 free(udev_device->physdevpath);
614 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
615 free(udev_device->envp);
616 free(udev_device->monitor_buf);
617 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
622 * udev_device_get_devpath:
623 * @udev_device: udev device
625 * Retrieve the kernel devpath value of the udev device. The path
626 * does not contain the sys mount point, and starts with a '/'.
628 * Returns: the devpath of the udev device
630 const char *udev_device_get_devpath(struct udev_device *udev_device)
632 if (udev_device == NULL)
634 return udev_device->devpath;
638 * udev_device_get_syspath:
639 * @udev_device: udev device
641 * Retrieve the sys path of the udev device. The path is an
642 * absolute path and starts with the sys mount point.
644 * Returns: the sys path of the udev device
646 const char *udev_device_get_syspath(struct udev_device *udev_device)
648 if (udev_device == NULL)
650 return udev_device->syspath;
653 const char *udev_device_get_sysname(struct udev_device *udev_device)
655 if (udev_device == NULL)
657 return udev_device->sysname;
660 const char *udev_device_get_sysnum(struct udev_device *udev_device)
662 if (udev_device == NULL)
664 return udev_device->sysnum;
668 * udev_device_get_devnode:
669 * @udev_device: udev device
671 * Retrieve the device node file name belonging to the udev device.
672 * The path is an absolute path, and starts with the device directory.
674 * Returns: the device node file name of the udev device, or #NULL if no device node exists
676 const char *udev_device_get_devnode(struct udev_device *udev_device)
678 if (udev_device == NULL)
680 if (!udev_device->info_loaded)
681 device_load_info(udev_device);
682 return udev_device->devnode;
686 * udev_device_get_subsystem:
687 * @udev_device: udev device
689 * Retrieve the subsystem string of the udev device. The string does not
692 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
694 const char *udev_device_get_subsystem(struct udev_device *udev_device)
696 char subsystem[UTIL_NAME_SIZE];
698 if (udev_device == NULL)
700 if (!udev_device->subsystem_set) {
701 udev_device->subsystem_set = 1;
702 /* read "subsytem" link */
703 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
704 udev_device_set_subsystem(udev_device, subsystem);
705 return udev_device->subsystem;
708 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
709 udev_device_set_subsystem(udev_device, "module");
710 return udev_device->subsystem;
712 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
713 udev_device_set_subsystem(udev_device, "drivers");
714 return udev_device->subsystem;
716 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
717 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
718 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
719 udev_device_set_subsystem(udev_device, "subsystem");
720 return udev_device->subsystem;
723 return udev_device->subsystem;
727 * udev_device_get_devlinks_list_entry:
728 * @udev_device: udev device
730 * Retrieve the list of device links pointing to the device file of
731 * the udev device. The next list entry can be retrieved with
732 * udev_list_entry_next(), which returns #NULL if no more entries exist.
733 * The devlink path can be retrieved from the list entry by
734 * udev_list_entry_get_name(). The path is an absolute path, and starts with
735 * the device directory.
737 * Returns: the first entry of the device node link list
739 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
741 if (udev_device == NULL)
743 if (!udev_device->info_loaded)
744 device_load_info(udev_device);
745 return udev_list_get_entry(&udev_device->devlinks_list);
748 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
750 udev_device->devlinks_uptodate = 0;
751 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
755 * udev_device_get_properties_list_entry:
756 * @udev_device: udev device
758 * Retrieve the list of key/value device properties of the udev
759 * device. The next list entry can be retrieved with udev_list_entry_next(),
760 * which returns #NULL if no more entries exist. The property name
761 * can be retrieved from the list entry by udev_list_get_name(),
762 * the property value by udev_list_get_value().
764 * Returns: the first entry of the property list
766 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
768 if (udev_device == NULL)
770 if (!udev_device->info_loaded)
771 device_load_info(udev_device);
772 if (!udev_device->devlinks_uptodate) {
773 char symlinks[UTIL_PATH_SIZE];
774 struct udev_list_entry *list_entry;
776 udev_device->devlinks_uptodate = 1;
777 list_entry = udev_device_get_devlinks_list_entry(udev_device);
778 if (list_entry != NULL) {
779 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
780 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
781 util_strlcat(symlinks, " ", sizeof(symlinks));
782 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
784 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
787 return udev_list_get_entry(&udev_device->properties_list);
790 const char *udev_device_get_driver(struct udev_device *udev_device)
792 char driver[UTIL_NAME_SIZE];
794 if (udev_device == NULL)
796 if (!udev_device->driver_set) {
797 udev_device->driver_set = 1;
798 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
799 udev_device->driver = strdup(driver);
801 return udev_device->driver;
804 dev_t udev_device_get_devnum(struct udev_device *udev_device)
806 if (udev_device == NULL)
807 return makedev(0, 0);
808 if (!udev_device->info_loaded)
809 device_load_info(udev_device);
810 return udev_device->devnum;
813 const char *udev_device_get_action(struct udev_device *udev_device)
815 if (udev_device == NULL)
817 return udev_device->action;
820 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
822 if (udev_device == NULL)
824 return udev_device->seqnum;
827 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
829 struct udev_list_entry *list_entry;
830 char path[UTIL_PATH_SIZE];
831 char value[UTIL_NAME_SIZE];
835 const char *val = NULL;
837 if (udev_device == NULL)
842 /* look for possibly already cached result */
843 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
844 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
845 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
846 sysattr, udev_list_entry_get_value(list_entry));
847 return udev_list_entry_get_value(list_entry);
851 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
852 util_strlcat(path, "/", sizeof(path));
853 util_strlcat(path, sysattr, sizeof(path));
855 if (lstat(path, &statbuf) != 0) {
856 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
857 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
861 if (S_ISLNK(statbuf.st_mode)) {
862 char target[UTIL_NAME_SIZE];
866 /* some core links return the last element of the target path */
867 if (strcmp(sysattr, "driver") != 0 &&
868 strcmp(sysattr, "subsystem") != 0 &&
869 strcmp(sysattr, "module") != 0)
872 len = readlink(path, target, sizeof(target));
875 pos = strrchr(target, '/');
878 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
879 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
880 val = udev_list_entry_get_value(list_entry);
886 /* skip directories */
887 if (S_ISDIR(statbuf.st_mode))
890 /* skip non-readable files */
891 if ((statbuf.st_mode & S_IRUSR) == 0)
894 /* read attribute value */
895 fd = open(path, O_RDONLY);
897 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
900 size = read(fd, value, sizeof(value));
904 if (size == sizeof(value))
907 /* got a valid value, store it in cache and return it */
909 util_remove_trailing_chars(value, '\n');
910 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
911 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
912 val = udev_list_entry_get_value(list_entry);
917 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
922 free(udev_device->syspath);
923 udev_device->syspath = strdup(syspath);
924 if (udev_device->syspath == NULL)
926 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
927 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
929 pos = strrchr(udev_device->syspath, '/');
932 udev_device->sysname = strdup(&pos[1]);
933 if (udev_device->sysname == NULL)
936 /* some devices have '!' in their name, change that to '/' */
938 while (udev_device->sysname[len] != '\0') {
939 if (udev_device->sysname[len] == '!')
940 udev_device->sysname[len] = '/';
944 /* trailing number */
945 while (len > 0 && isdigit(udev_device->sysname[--len]))
946 udev_device->sysnum = &udev_device->sysname[len];
948 /* sysname is completely numeric */
950 udev_device->sysnum = NULL;
955 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
957 free(udev_device->subsystem);
958 udev_device->subsystem = strdup(subsystem);
959 if (udev_device->subsystem == NULL)
961 udev_device->subsystem_set = 1;
962 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
966 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
968 free(udev_device->devnode);
969 udev_device->devnode = strdup(devnode);
972 if (udev_device->devnode == NULL)
974 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
978 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
980 udev_device->devlinks_uptodate = 0;
981 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
986 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
988 udev_device->envp_uptodate = 0;
990 struct udev_list_entry *list_entry;
992 list_entry = udev_device_get_properties_list_entry(udev_device);
993 list_entry = udev_list_entry_get_by_name(list_entry, key);
994 if (list_entry != NULL)
995 udev_list_entry_delete(list_entry);
998 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1001 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1003 char name[UTIL_PATH_SIZE];
1006 util_strlcpy(name, property, sizeof(name));
1007 val = strchr(name, '=');
1014 return udev_device_add_property(udev_device, name, val);
1017 #define ENVP_SIZE 128
1018 #define MONITOR_BUF_SIZE 4096
1019 static int update_envp_monitor_buf(struct udev_device *udev_device)
1022 struct udev_list_entry *list_entry;
1027 /* monitor buffer of property strings */
1028 free(udev_device->monitor_buf);
1029 udev_device->monitor_buf_len = 0;
1030 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1031 if (udev_device->monitor_buf == NULL)
1034 /* envp array, strings will point into monitor buffer */
1035 if (udev_device->envp == NULL)
1036 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1037 if (udev_device->envp == NULL)
1040 /* header <action>@<devpath> */
1041 action = udev_device_get_action(udev_device);
1044 bufpos = util_strlcpy(udev_device->monitor_buf, action, MONITOR_BUF_SIZE);
1045 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "@", MONITOR_BUF_SIZE-bufpos);
1046 if (len >= MONITOR_BUF_SIZE-bufpos)
1049 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1050 udev_device_get_devpath(udev_device),
1051 MONITOR_BUF_SIZE-bufpos);
1052 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1057 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1058 /* add string to envp array */
1059 udev_device->envp[i++] = &udev_device->monitor_buf[bufpos];
1060 if (i+1 >= ENVP_SIZE)
1063 /* add property string to monitor buffer */
1064 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1065 udev_list_entry_get_name(list_entry), MONITOR_BUF_SIZE-bufpos);
1066 if (len >= MONITOR_BUF_SIZE-bufpos)
1069 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "=", MONITOR_BUF_SIZE-bufpos);
1070 if (len >= MONITOR_BUF_SIZE-bufpos)
1073 len = util_strlcpy(&udev_device->monitor_buf[bufpos], udev_list_entry_get_value(list_entry),
1074 MONITOR_BUF_SIZE-bufpos);
1075 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1079 udev_device->envp[i] = NULL;
1080 udev_device->monitor_buf_len = bufpos;
1081 udev_device->envp_uptodate = 1;
1082 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n", i, bufpos);
1086 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1088 if (!udev_device->envp_uptodate)
1089 if (update_envp_monitor_buf(udev_device) != 0)
1091 return udev_device->envp;
1094 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1096 if (!udev_device->envp_uptodate)
1097 if (update_envp_monitor_buf(udev_device) != 0)
1099 *buf = udev_device->monitor_buf;
1100 return udev_device->monitor_buf_len;
1103 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1105 free(udev_device->action);
1106 udev_device->action = strdup(action);
1107 if (udev_device->action == NULL)
1109 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1113 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1115 free(udev_device->driver);
1116 udev_device->driver = strdup(driver);
1117 if (udev_device->driver == NULL)
1119 udev_device->driver_set = 1;
1120 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1124 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1126 return udev_device->devpath_old;
1129 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1131 udev_device->devpath_old = strdup(devpath_old);
1132 if (udev_device->devpath_old == NULL)
1134 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1138 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1140 return udev_device->physdevpath;
1143 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1145 udev_device->physdevpath = strdup(physdevpath);
1146 if (udev_device->physdevpath == NULL)
1151 int udev_device_get_timeout(struct udev_device *udev_device)
1153 return udev_device->timeout;
1156 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1158 udev_device->timeout = timeout;
1161 int udev_device_get_event_timeout(struct udev_device *udev_device)
1163 if (!udev_device->info_loaded)
1164 device_load_info(udev_device);
1165 return udev_device->event_timeout;
1168 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1170 udev_device->event_timeout = event_timeout;
1174 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1178 udev_device->seqnum = seqnum;
1179 snprintf(num, sizeof(num), "%llu", seqnum);
1180 udev_device_add_property(udev_device, "SEQNUM", num);
1184 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1188 udev_device->devnum = devnum;
1190 snprintf(num, sizeof(num), "%u", major(devnum));
1191 udev_device_add_property(udev_device, "MAJOR", num);
1192 snprintf(num, sizeof(num), "%u", minor(devnum));
1193 udev_device_add_property(udev_device, "MINOR", num);
1197 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1199 if (!udev_device->info_loaded)
1200 device_load_info(udev_device);
1201 return udev_device->num_fake_partitions;
1204 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1206 udev_device->num_fake_partitions = num;
1210 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1212 if (!udev_device->info_loaded)
1213 device_load_info(udev_device);
1214 return udev_device->devlink_priority;
1217 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1219 udev_device->devlink_priority = prio;
1223 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1225 if (!udev_device->info_loaded)
1226 device_load_info(udev_device);
1227 return udev_device->ignore_remove;
1230 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1232 udev_device->ignore_remove = ignore;