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;
61 unsigned int parent_set:1;
62 unsigned int subsystem_set:1;
63 unsigned int devtype_set:1;
64 unsigned int devlinks_uptodate:1;
65 unsigned int envp_uptodate:1;
66 unsigned int driver_set:1;
67 unsigned int info_loaded:1;
68 unsigned int ignore_remove:1;
71 static size_t devpath_to_db_path(struct udev *udev, const char *devpath, char *filename, size_t len)
75 /* translate to location of db file */
76 util_strlcpy(filename, udev_get_dev_path(udev), len);
77 start = util_strlcat(filename, "/.udev/db/", len);
78 util_strlcat(filename, devpath, len);
79 return util_path_encode(&filename[start], len - start);
82 int udev_device_read_db(struct udev_device *udev_device)
85 char filename[UTIL_PATH_SIZE];
86 char line[UTIL_LINE_SIZE];
89 devpath_to_db_path(udev_device->udev, udev_device->devpath, filename, sizeof(filename));
91 if (lstat(filename, &stats) != 0) {
92 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
95 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
96 char target[UTIL_PATH_SIZE];
97 char devnode[UTIL_PATH_SIZE];
101 target_len = readlink(filename, target, sizeof(target));
103 target[target_len] = '\0';
105 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
109 next = strchr(target, ' ');
114 util_strlcpy(devnode, udev_get_dev_path(udev_device->udev), sizeof(devnode));
115 util_strlcat(devnode, "/", sizeof(devnode));
116 util_strlcat(devnode, target, sizeof(devnode));
117 udev_device_set_devnode(udev_device, devnode);
118 while (next != NULL) {
119 char devlink[UTIL_PATH_SIZE];
123 next = strchr(next, ' ');
128 util_strlcpy(devlink, udev_get_dev_path(udev_device->udev), sizeof(devlink));
129 util_strlcat(devlink, "/", sizeof(devlink));
130 util_strlcat(devlink, lnk, sizeof(devlink));
131 udev_device_add_devlink(udev_device, devlink);
133 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
137 f = fopen(filename, "r");
139 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
142 while (fgets(line, sizeof(line), f)) {
153 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
154 util_strlcat(filename, "/", sizeof(filename));
155 util_strlcat(filename, val, sizeof(filename));
156 udev_device_set_devnode(udev_device, filename);
159 util_strlcpy(filename, udev_get_dev_path(udev_device->udev), sizeof(filename));
160 util_strlcat(filename, "/", sizeof(filename));
161 util_strlcat(filename, val, sizeof(filename));
162 udev_device_add_devlink(udev_device, filename);
165 udev_device_set_devlink_priority(udev_device, atoi(val));
168 udev_device_set_event_timeout(udev_device, atoi(val));
171 udev_device_set_num_fake_partitions(udev_device, atoi(val));
174 udev_device_set_ignore_remove(udev_device, atoi(val));
177 udev_device_add_property_from_string(udev_device, val);
183 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
187 int udev_device_read_uevent_file(struct udev_device *udev_device)
189 char filename[UTIL_PATH_SIZE];
191 char line[UTIL_LINE_SIZE];
195 util_strlcpy(filename, udev_device->syspath, sizeof(filename));
196 util_strlcat(filename, "/uevent", sizeof(filename));
197 f = fopen(filename, "r");
201 while (fgets(line, sizeof(line), f)) {
204 pos = strchr(line, '\n');
209 if (strncmp(line, "DEVTYPE=", 8) == 0)
210 udev_device_set_devtype(udev_device, &line[8]);
211 else if (strncmp(line, "MAJOR=", 6) == 0)
212 maj = strtoull(&line[6], NULL, 10);
213 else if (strncmp(line, "MINOR=", 6) == 0)
214 min = strtoull(&line[6], NULL, 10);
216 udev_device_add_property_from_string(udev_device, line);
219 udev_device->devnum = makedev(maj, min);
225 static void device_load_info(struct udev_device *device)
227 device->info_loaded = 1;
228 udev_device_read_uevent_file(device);
229 udev_device_read_db(device);
232 void udev_device_set_info_loaded(struct udev_device *device)
234 device->info_loaded = 1;
237 struct udev_device *device_new(struct udev *udev)
239 struct udev_device *udev_device;
240 struct udev_list_entry *list_entry;
245 udev_device = calloc(1, sizeof(struct udev_device));
246 if (udev_device == NULL)
248 udev_device->refcount = 1;
249 udev_device->udev = udev;
250 udev_list_init(&udev_device->devlinks_list);
251 udev_list_init(&udev_device->properties_list);
252 udev_list_init(&udev_device->sysattr_list);
253 udev_device->event_timeout = -1;
254 /* copy global properties */
255 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
256 udev_device_add_property(udev_device,
257 udev_list_entry_get_name(list_entry),
258 udev_list_entry_get_value(list_entry));
259 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
264 * udev_device_new_from_syspath:
265 * @udev: udev library context
266 * @syspath: sys device path including sys directory
268 * Create new udev device, and fill in information from the sys
269 * device and the udev database entry. The sypath is the absolute
270 * path to the device, including the sys mount point.
272 * The initial refcount is 1, and needs to be decremented to
273 * release the resources of the udev device.
275 * Returns: a new udev device, or #NULL, if it does not exist
277 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
281 char path[UTIL_PATH_SIZE];
284 struct udev_device *udev_device;
291 /* path starts in sys */
292 len = strlen(udev_get_sys_path(udev));
293 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
294 info(udev, "not in sys :%s\n", syspath);
298 /* path is not a root directory */
299 subdir = &syspath[len+1];
300 pos = strrchr(subdir, '/');
301 if (pos == NULL || pos < &subdir[2]) {
302 dbg(udev, "not a subdir :%s\n", syspath);
306 /* resolve possible symlink to real path */
307 util_strlcpy(path, syspath, sizeof(path));
308 util_resolve_sys_link(udev, path, sizeof(path));
310 /* try to resolve the silly block layout if needed */
311 if (strncmp(&path[len], "/block/", 7) == 0) {
312 char block[UTIL_PATH_SIZE];
313 char part[UTIL_PATH_SIZE];
315 util_strlcpy(block, path, sizeof(block));
316 pos = strrchr(block, '/');
319 util_strlcpy(part, pos, sizeof(part));
321 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0) {
322 util_strlcpy(path, block, sizeof(path));
323 util_strlcat(path, part, sizeof(path));
327 /* path exists in sys */
328 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
329 strncmp(&syspath[len], "/class/", 7) == 0 ||
330 strncmp(&syspath[len], "/block/", 7) == 0) {
331 char file[UTIL_PATH_SIZE];
333 /* all "devices" require a "uevent" file */
334 util_strlcpy(file, path, sizeof(file));
335 util_strlcat(file, "/uevent", sizeof(file));
336 if (stat(file, &statbuf) != 0) {
337 dbg(udev, "not a device: %s\n", syspath);
341 /* everything else just needs to be a directory */
342 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
343 dbg(udev, "directory not found: %s\n", syspath);
348 udev_device = device_new(udev);
349 if (udev_device == NULL)
352 udev_device_set_syspath(udev_device, path);
353 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
358 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
360 char path[UTIL_PATH_SIZE];
361 const char *type_str;
362 struct udev_enumerate *udev_enumerate;
363 struct udev_list_entry *list_entry;
364 struct udev_device *device = NULL;
368 else if (type == 'c')
373 /* /sys/dev/{block,char}/<maj>:<min> link */
374 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
375 type_str, major(devnum), minor(devnum));
376 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
377 return udev_device_new_from_syspath(udev, path);
379 udev_enumerate = udev_enumerate_new(udev);
380 if (udev_enumerate == NULL)
383 /* fallback to search sys devices for the major/minor */
385 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
386 else if (type == 'c')
387 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
388 udev_enumerate_scan_devices(udev_enumerate);
389 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
390 struct udev_device *device_loop;
392 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
393 if (device_loop != NULL) {
394 if (udev_device_get_devnum(device_loop) == devnum) {
395 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
397 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
399 device = device_loop;
402 udev_device_unref(device_loop);
405 udev_enumerate_unref(udev_enumerate);
409 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
412 char path_full[UTIL_PATH_SIZE];
416 sys_path_len = util_strlcpy(path_full, udev_get_sys_path(udev), sizeof(path_full));
417 path = &path_full[sys_path_len];
419 if (strcmp(subsystem, "subsystem") == 0) {
420 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
421 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
422 if (stat(path_full, &statbuf) == 0)
425 util_strlcpy(path, "/bus/", 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, "/class/", sizeof(path_full) - sys_path_len);
431 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
432 if (stat(path_full, &statbuf) == 0)
437 if (strcmp(subsystem, "module") == 0) {
438 util_strlcpy(path, "/module/", sizeof(path_full) - sys_path_len);
439 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
440 if (stat(path_full, &statbuf) == 0)
445 if (strcmp(subsystem, "drivers") == 0) {
446 char subsys[UTIL_NAME_SIZE];
449 util_strlcpy(subsys, sysname, sizeof(subsys));
450 driver = strchr(subsys, ':');
451 if (driver != NULL) {
454 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
455 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
456 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
457 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
458 if (stat(path_full, &statbuf) == 0)
461 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
462 util_strlcat(path, subsys, sizeof(path_full) - sys_path_len);
463 util_strlcat(path, "/drivers/", sizeof(path_full) - sys_path_len);
464 util_strlcat(path, driver, sizeof(path_full) - sys_path_len);
465 if (stat(path_full, &statbuf) == 0)
471 util_strlcpy(path, "/subsystem/", sizeof(path_full) - sys_path_len);
472 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
473 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
474 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
475 if (stat(path_full, &statbuf) == 0)
478 util_strlcpy(path, "/bus/", sizeof(path_full) - sys_path_len);
479 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
480 util_strlcat(path, "/devices/", sizeof(path_full) - sys_path_len);
481 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
482 if (stat(path_full, &statbuf) == 0)
485 util_strlcpy(path, "/class/", sizeof(path_full) - sys_path_len);
486 util_strlcat(path, subsystem, sizeof(path_full) - sys_path_len);
487 util_strlcat(path, "/", sizeof(path_full) - sys_path_len);
488 util_strlcat(path, sysname, sizeof(path_full) - sys_path_len);
489 if (stat(path_full, &statbuf) == 0)
494 return udev_device_new_from_syspath(udev, path_full);
497 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
499 struct udev_device *udev_device_parent = NULL;
500 char path[UTIL_PATH_SIZE];
503 /* follow "device" link in deprecated sys layout */
504 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
505 strncmp(udev_device->devpath, "/block/", 7) == 0) {
506 util_strlcpy(path, udev_device->syspath, sizeof(path));
507 util_strlcat(path, "/device", sizeof(path));
508 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
509 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
510 if (udev_device_parent != NULL)
511 return udev_device_parent;
515 util_strlcpy(path, udev_device->syspath, sizeof(path));
516 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
520 pos = strrchr(subdir, '/');
521 if (pos == NULL || pos < &subdir[2])
524 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
525 if (udev_device_parent != NULL)
526 return udev_device_parent;
531 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
533 if (udev_device == NULL)
535 if (!udev_device->parent_set) {
536 udev_device->parent_set = 1;
537 udev_device->parent_device = device_new_from_parent(udev_device);
539 if (udev_device->parent_device != NULL)
540 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
541 return udev_device->parent_device;
544 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
546 struct udev_device *parent;
548 if (subsystem == NULL)
551 parent = udev_device_get_parent(udev_device);
552 while (parent != NULL) {
553 const char *parent_subsystem;
554 const char *parent_devtype;
556 parent_subsystem = udev_device_get_subsystem(parent);
557 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
560 parent_devtype = udev_device_get_devtype(parent);
561 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
564 parent = udev_device_get_parent(parent);
570 * udev_device_get_udev:
571 * @udev_device: udev device
573 * Retrieve the udev library context the device was created with.
575 * Returns: the udev library context
577 struct udev *udev_device_get_udev(struct udev_device *udev_device)
579 if (udev_device == NULL)
581 return udev_device->udev;
586 * @udev_device: udev device
588 * Take a reference of a udev device.
590 * Returns: the passed udev device
592 struct udev_device *udev_device_ref(struct udev_device *udev_device)
594 if (udev_device == NULL)
596 udev_device->refcount++;
602 * @udev_device: udev device
604 * Drop a reference of a udev device. If the refcount reaches zero,
605 * the resources of the device will be released.
608 void udev_device_unref(struct udev_device *udev_device)
610 if (udev_device == NULL)
612 udev_device->refcount--;
613 if (udev_device->refcount > 0)
615 if (udev_device->parent_device != NULL)
616 udev_device_unref(udev_device->parent_device);
617 free(udev_device->syspath);
618 free(udev_device->sysname);
619 free(udev_device->devnode);
620 free(udev_device->subsystem);
621 free(udev_device->devtype);
622 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
623 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
624 free(udev_device->action);
625 free(udev_device->driver);
626 free(udev_device->devpath_old);
627 free(udev_device->physdevpath);
628 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
629 free(udev_device->envp);
630 free(udev_device->monitor_buf);
631 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
636 * udev_device_get_devpath:
637 * @udev_device: udev device
639 * Retrieve the kernel devpath value of the udev device. The path
640 * does not contain the sys mount point, and starts with a '/'.
642 * Returns: the devpath of the udev device
644 const char *udev_device_get_devpath(struct udev_device *udev_device)
646 if (udev_device == NULL)
648 return udev_device->devpath;
652 * udev_device_get_syspath:
653 * @udev_device: udev device
655 * Retrieve the sys path of the udev device. The path is an
656 * absolute path and starts with the sys mount point.
658 * Returns: the sys path of the udev device
660 const char *udev_device_get_syspath(struct udev_device *udev_device)
662 if (udev_device == NULL)
664 return udev_device->syspath;
667 const char *udev_device_get_sysname(struct udev_device *udev_device)
669 if (udev_device == NULL)
671 return udev_device->sysname;
674 const char *udev_device_get_sysnum(struct udev_device *udev_device)
676 if (udev_device == NULL)
678 return udev_device->sysnum;
682 * udev_device_get_devnode:
683 * @udev_device: udev device
685 * Retrieve the device node file name belonging to the udev device.
686 * The path is an absolute path, and starts with the device directory.
688 * Returns: the device node file name of the udev device, or #NULL if no device node exists
690 const char *udev_device_get_devnode(struct udev_device *udev_device)
692 if (udev_device == NULL)
694 if (!udev_device->info_loaded)
695 device_load_info(udev_device);
696 return udev_device->devnode;
700 * udev_device_get_subsystem:
701 * @udev_device: udev device
703 * Retrieve the subsystem string of the udev device. The string does not
706 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
708 const char *udev_device_get_subsystem(struct udev_device *udev_device)
710 char subsystem[UTIL_NAME_SIZE];
712 if (udev_device == NULL)
714 if (!udev_device->subsystem_set) {
715 udev_device->subsystem_set = 1;
716 /* read "subsytem" link */
717 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
718 udev_device_set_subsystem(udev_device, subsystem);
719 return udev_device->subsystem;
722 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
723 udev_device_set_subsystem(udev_device, "module");
724 return udev_device->subsystem;
726 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
727 udev_device_set_subsystem(udev_device, "drivers");
728 return udev_device->subsystem;
730 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
731 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
732 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
733 udev_device_set_subsystem(udev_device, "subsystem");
734 return udev_device->subsystem;
737 return udev_device->subsystem;
741 * udev_device_get_devtype:
742 * @udev_device: udev device
744 * Retrieve the devtype string of the udev device.
746 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
748 const char *udev_device_get_devtype(struct udev_device *udev_device)
750 if (udev_device == NULL)
752 if (!udev_device->devtype_set) {
753 udev_device->devtype_set = 1;
754 if (!udev_device->info_loaded)
755 udev_device_read_uevent_file(udev_device);
757 return udev_device->devtype;
761 * udev_device_get_devlinks_list_entry:
762 * @udev_device: udev device
764 * Retrieve the list of device links pointing to the device file of
765 * the udev device. The next list entry can be retrieved with
766 * udev_list_entry_next(), which returns #NULL if no more entries exist.
767 * The devlink path can be retrieved from the list entry by
768 * udev_list_entry_get_name(). The path is an absolute path, and starts with
769 * the device directory.
771 * Returns: the first entry of the device node link list
773 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
775 if (udev_device == NULL)
777 if (!udev_device->info_loaded)
778 device_load_info(udev_device);
779 return udev_list_get_entry(&udev_device->devlinks_list);
782 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
784 udev_device->devlinks_uptodate = 0;
785 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
789 * udev_device_get_properties_list_entry:
790 * @udev_device: udev device
792 * Retrieve the list of key/value device properties of the udev
793 * device. The next list entry can be retrieved with udev_list_entry_next(),
794 * which returns #NULL if no more entries exist. The property name
795 * can be retrieved from the list entry by udev_list_get_name(),
796 * the property value by udev_list_get_value().
798 * Returns: the first entry of the property list
800 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
802 if (udev_device == NULL)
804 if (!udev_device->info_loaded)
805 device_load_info(udev_device);
806 if (!udev_device->devlinks_uptodate) {
807 char symlinks[UTIL_PATH_SIZE];
808 struct udev_list_entry *list_entry;
810 udev_device->devlinks_uptodate = 1;
811 list_entry = udev_device_get_devlinks_list_entry(udev_device);
812 if (list_entry != NULL) {
813 util_strlcpy(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
814 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry)) {
815 util_strlcat(symlinks, " ", sizeof(symlinks));
816 util_strlcat(symlinks, udev_list_entry_get_name(list_entry), sizeof(symlinks));
818 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
821 return udev_list_get_entry(&udev_device->properties_list);
824 const char *udev_device_get_driver(struct udev_device *udev_device)
826 char driver[UTIL_NAME_SIZE];
828 if (udev_device == NULL)
830 if (!udev_device->driver_set) {
831 udev_device->driver_set = 1;
832 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
833 udev_device->driver = strdup(driver);
835 return udev_device->driver;
838 dev_t udev_device_get_devnum(struct udev_device *udev_device)
840 if (udev_device == NULL)
841 return makedev(0, 0);
842 if (!udev_device->info_loaded)
843 device_load_info(udev_device);
844 return udev_device->devnum;
847 const char *udev_device_get_action(struct udev_device *udev_device)
849 if (udev_device == NULL)
851 return udev_device->action;
854 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
856 if (udev_device == NULL)
858 return udev_device->seqnum;
861 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
863 struct udev_list_entry *list_entry;
864 char path[UTIL_PATH_SIZE];
869 const char *val = NULL;
871 if (udev_device == NULL)
876 /* look for possibly already cached result */
877 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
878 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
879 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
880 sysattr, udev_list_entry_get_value(list_entry));
881 return udev_list_entry_get_value(list_entry);
885 util_strlcpy(path, udev_device_get_syspath(udev_device), sizeof(path));
886 util_strlcat(path, "/", sizeof(path));
887 util_strlcat(path, sysattr, sizeof(path));
889 if (lstat(path, &statbuf) != 0) {
890 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
891 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
895 if (S_ISLNK(statbuf.st_mode)) {
896 char target[UTIL_NAME_SIZE];
900 /* some core links return the last element of the target path */
901 if (strcmp(sysattr, "driver") != 0 &&
902 strcmp(sysattr, "subsystem") != 0 &&
903 strcmp(sysattr, "module") != 0)
906 len = readlink(path, target, sizeof(target));
909 pos = strrchr(target, '/');
912 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
913 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
914 val = udev_list_entry_get_value(list_entry);
920 /* skip directories */
921 if (S_ISDIR(statbuf.st_mode))
924 /* skip non-readable files */
925 if ((statbuf.st_mode & S_IRUSR) == 0)
928 /* read attribute value */
929 fd = open(path, O_RDONLY);
931 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
934 size = read(fd, value, sizeof(value));
938 if (size == sizeof(value))
941 /* got a valid value, store it in cache and return it */
943 util_remove_trailing_chars(value, '\n');
944 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
945 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
946 val = udev_list_entry_get_value(list_entry);
951 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
956 free(udev_device->syspath);
957 udev_device->syspath = strdup(syspath);
958 if (udev_device->syspath == NULL)
960 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
961 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
963 pos = strrchr(udev_device->syspath, '/');
966 udev_device->sysname = strdup(&pos[1]);
967 if (udev_device->sysname == NULL)
970 /* some devices have '!' in their name, change that to '/' */
972 while (udev_device->sysname[len] != '\0') {
973 if (udev_device->sysname[len] == '!')
974 udev_device->sysname[len] = '/';
978 /* trailing number */
979 while (len > 0 && isdigit(udev_device->sysname[--len]))
980 udev_device->sysnum = &udev_device->sysname[len];
982 /* sysname is completely numeric */
984 udev_device->sysnum = NULL;
989 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
991 free(udev_device->subsystem);
992 udev_device->subsystem = strdup(subsystem);
993 if (udev_device->subsystem == NULL)
995 udev_device->subsystem_set = 1;
996 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1000 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1002 free(udev_device->devtype);
1003 udev_device->devtype = strdup(devtype);
1004 if (udev_device->devtype == NULL)
1006 udev_device->devtype_set = 1;
1007 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1011 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1013 free(udev_device->devnode);
1014 udev_device->devnode = strdup(devnode);
1015 if (devnode == NULL)
1017 if (udev_device->devnode == NULL)
1019 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1023 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink)
1025 udev_device->devlinks_uptodate = 0;
1026 if (udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0) == NULL)
1031 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1033 udev_device->envp_uptodate = 0;
1034 if (value == NULL) {
1035 struct udev_list_entry *list_entry;
1037 list_entry = udev_device_get_properties_list_entry(udev_device);
1038 list_entry = udev_list_entry_get_by_name(list_entry, key);
1039 if (list_entry != NULL)
1040 udev_list_entry_delete(list_entry);
1043 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1046 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1048 char name[UTIL_PATH_SIZE];
1051 util_strlcpy(name, property, sizeof(name));
1052 val = strchr(name, '=');
1059 return udev_device_add_property(udev_device, name, val);
1062 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1064 struct udev_list_entry *list_entry;
1066 if (udev_device == NULL)
1071 list_entry = udev_device_get_properties_list_entry(udev_device);
1072 list_entry = udev_list_entry_get_by_name(list_entry, key);
1073 return udev_list_entry_get_value(list_entry);
1076 #define ENVP_SIZE 128
1077 #define MONITOR_BUF_SIZE 4096
1078 static int update_envp_monitor_buf(struct udev_device *udev_device)
1081 struct udev_list_entry *list_entry;
1086 /* monitor buffer of property strings */
1087 free(udev_device->monitor_buf);
1088 udev_device->monitor_buf_len = 0;
1089 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1090 if (udev_device->monitor_buf == NULL)
1093 /* envp array, strings will point into monitor buffer */
1094 if (udev_device->envp == NULL)
1095 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1096 if (udev_device->envp == NULL)
1099 /* header <action>@<devpath> */
1100 action = udev_device_get_action(udev_device);
1103 bufpos = util_strlcpy(udev_device->monitor_buf, action, MONITOR_BUF_SIZE);
1104 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "@", MONITOR_BUF_SIZE-bufpos);
1105 if (len >= MONITOR_BUF_SIZE-bufpos)
1108 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1109 udev_device_get_devpath(udev_device),
1110 MONITOR_BUF_SIZE-bufpos);
1111 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1116 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1117 /* add string to envp array */
1118 udev_device->envp[i++] = &udev_device->monitor_buf[bufpos];
1119 if (i+1 >= ENVP_SIZE)
1122 /* add property string to monitor buffer */
1123 len = util_strlcpy(&udev_device->monitor_buf[bufpos],
1124 udev_list_entry_get_name(list_entry), MONITOR_BUF_SIZE-bufpos);
1125 if (len >= MONITOR_BUF_SIZE-bufpos)
1128 len = util_strlcpy(&udev_device->monitor_buf[bufpos], "=", MONITOR_BUF_SIZE-bufpos);
1129 if (len >= MONITOR_BUF_SIZE-bufpos)
1132 len = util_strlcpy(&udev_device->monitor_buf[bufpos], udev_list_entry_get_value(list_entry),
1133 MONITOR_BUF_SIZE-bufpos);
1134 if (len+1 >= MONITOR_BUF_SIZE-bufpos)
1138 udev_device->envp[i] = NULL;
1139 udev_device->monitor_buf_len = bufpos;
1140 udev_device->envp_uptodate = 1;
1141 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n", i, bufpos);
1145 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1147 if (!udev_device->envp_uptodate)
1148 if (update_envp_monitor_buf(udev_device) != 0)
1150 return udev_device->envp;
1153 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1155 if (!udev_device->envp_uptodate)
1156 if (update_envp_monitor_buf(udev_device) != 0)
1158 *buf = udev_device->monitor_buf;
1159 return udev_device->monitor_buf_len;
1162 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1164 free(udev_device->action);
1165 udev_device->action = strdup(action);
1166 if (udev_device->action == NULL)
1168 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1172 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1174 free(udev_device->driver);
1175 udev_device->driver = strdup(driver);
1176 if (udev_device->driver == NULL)
1178 udev_device->driver_set = 1;
1179 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1183 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1185 return udev_device->devpath_old;
1188 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1190 udev_device->devpath_old = strdup(devpath_old);
1191 if (udev_device->devpath_old == NULL)
1193 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1197 const char *udev_device_get_physdevpath(struct udev_device *udev_device)
1199 return udev_device->physdevpath;
1202 int udev_device_set_physdevpath(struct udev_device *udev_device, const char *physdevpath)
1204 udev_device->physdevpath = strdup(physdevpath);
1205 if (udev_device->physdevpath == NULL)
1210 int udev_device_get_timeout(struct udev_device *udev_device)
1212 return udev_device->timeout;
1215 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1217 udev_device->timeout = timeout;
1220 int udev_device_get_event_timeout(struct udev_device *udev_device)
1222 if (!udev_device->info_loaded)
1223 device_load_info(udev_device);
1224 return udev_device->event_timeout;
1227 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1229 udev_device->event_timeout = event_timeout;
1233 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1237 udev_device->seqnum = seqnum;
1238 snprintf(num, sizeof(num), "%llu", seqnum);
1239 udev_device_add_property(udev_device, "SEQNUM", num);
1243 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1247 udev_device->devnum = devnum;
1249 snprintf(num, sizeof(num), "%u", major(devnum));
1250 udev_device_add_property(udev_device, "MAJOR", num);
1251 snprintf(num, sizeof(num), "%u", minor(devnum));
1252 udev_device_add_property(udev_device, "MINOR", num);
1256 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1258 if (!udev_device->info_loaded)
1259 device_load_info(udev_device);
1260 return udev_device->num_fake_partitions;
1263 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1265 udev_device->num_fake_partitions = num;
1269 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1271 if (!udev_device->info_loaded)
1272 device_load_info(udev_device);
1273 return udev_device->devlink_priority;
1276 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1278 udev_device->devlink_priority = prio;
1282 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1284 if (!udev_device->info_loaded)
1285 device_load_info(udev_device);
1286 return udev_device->ignore_remove;
1289 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1291 udev_device->ignore_remove = ignore;