2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2010 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.
25 #include "libudev-private.h"
28 * SECTION:libudev-device
29 * @short_description: kernel sys devices
31 * Representation of kernel sys devices. Devices are uniquely identified
32 * by their syspath, every device has exactly one path in the kernel sys
33 * filesystem. Devices usually belong to a kernel subsystem, and and have
34 * a unique name inside that subsystem.
40 * Opaque object representing one kernel sys device.
44 struct udev_device *parent_device;
59 size_t monitor_buf_len;
60 struct udev_list_node devlinks_list;
61 struct udev_list_node properties_list;
62 struct udev_list_node sysattr_list;
63 unsigned long long int seqnum;
71 unsigned int parent_set:1;
72 unsigned int subsystem_set:1;
73 unsigned int devtype_set:1;
74 unsigned int devlinks_uptodate:1;
75 unsigned int envp_uptodate:1;
76 unsigned int driver_set:1;
77 unsigned int info_loaded:1;
80 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
82 udev_device->envp_uptodate = 0;
84 struct udev_list_entry *list_entry;
86 list_entry = udev_device_get_properties_list_entry(udev_device);
87 list_entry = udev_list_entry_get_by_name(list_entry, key);
88 if (list_entry != NULL)
89 udev_list_entry_delete(list_entry);
92 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
95 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
97 char name[UTIL_LINE_SIZE];
100 util_strscpy(name, sizeof(name), property);
101 val = strchr(name, '=');
108 return udev_device_add_property(udev_device, name, val);
112 * parse property string, and if needed, update internal values accordingly
114 * udev_device_add_property_from_string_parse_finish() needs to be
115 * called after adding properties, and its return value checked
117 * udev_device_set_info_loaded() needs to be set, to avoid trying
118 * to use a device without a DEVPATH set
120 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
122 if (strncmp(property, "DEVPATH=", 8) == 0) {
123 char path[UTIL_PATH_SIZE];
125 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
126 udev_device_set_syspath(udev_device, path);
127 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
128 udev_device_set_subsystem(udev_device, &property[10]);
129 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
130 udev_device_set_devtype(udev_device, &property[8]);
131 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
132 if (property[8] == '/')
133 udev_device_set_devnode(udev_device, &property[8]);
135 udev_device_set_knodename(udev_device, &property[8]);
136 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
137 char devlinks[UTIL_PATH_SIZE];
141 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
143 next = strchr(slink, ' ');
144 while (next != NULL) {
146 udev_device_add_devlink(udev_device, slink, 0);
148 next = strchr(slink, ' ');
150 if (slink[0] != '\0')
151 udev_device_add_devlink(udev_device, slink, 0);
152 } else if (strncmp(property, "DRIVER=", 7) == 0) {
153 udev_device_set_driver(udev_device, &property[7]);
154 } else if (strncmp(property, "ACTION=", 7) == 0) {
155 udev_device_set_action(udev_device, &property[7]);
156 } else if (strncmp(property, "MAJOR=", 6) == 0) {
157 udev_device->maj = strtoull(&property[6], NULL, 10);
158 } else if (strncmp(property, "MINOR=", 6) == 0) {
159 udev_device->min = strtoull(&property[6], NULL, 10);
160 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
161 udev_device_set_devpath_old(udev_device, &property[12]);
162 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
163 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
164 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
165 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
167 udev_device_add_property_from_string(udev_device, property);
171 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
173 if (udev_device->maj > 0)
174 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
175 udev_device->maj = 0;
176 udev_device->min = 0;
178 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
184 * udev_device_get_property_value:
185 * @udev_device: udev device
186 * @key: property name
188 * Returns: the value of a device property, or #NULL if there is no such property.
190 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
192 struct udev_list_entry *list_entry;
194 if (udev_device == NULL)
199 list_entry = udev_device_get_properties_list_entry(udev_device);
200 list_entry = udev_list_entry_get_by_name(list_entry, key);
201 return udev_list_entry_get_value(list_entry);
204 int udev_device_read_db(struct udev_device *udev_device)
207 char filename[UTIL_PATH_SIZE];
208 char line[UTIL_LINE_SIZE];
211 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/",
212 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
214 if (lstat(filename, &stats) != 0) {
215 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
218 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
219 char target[UTIL_PATH_SIZE];
220 char devnode[UTIL_PATH_SIZE];
224 target_len = readlink(filename, target, sizeof(target));
226 target[target_len] = '\0';
228 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
232 next = strchr(target, ' ');
237 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
238 udev_device_set_devnode(udev_device, devnode);
239 while (next != NULL) {
240 char devlink[UTIL_PATH_SIZE];
244 next = strchr(next, ' ');
249 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
250 udev_device_add_devlink(udev_device, devlink, 0);
252 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
256 f = fopen(filename, "r");
258 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
261 while (fgets(line, sizeof(line), f)) {
272 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
273 udev_device_set_devnode(udev_device, filename);
276 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
277 udev_device_add_devlink(udev_device, filename, 0);
280 udev_device_set_devlink_priority(udev_device, atoi(val));
283 udev_device_set_event_timeout(udev_device, atoi(val));
286 udev_device_add_property_from_string(udev_device, val);
289 udev_device_set_watch_handle(udev_device, atoi(val));
295 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
299 int udev_device_read_uevent_file(struct udev_device *udev_device)
301 char filename[UTIL_PATH_SIZE];
303 char line[UTIL_LINE_SIZE];
307 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
308 f = fopen(filename, "r");
312 while (fgets(line, sizeof(line), f)) {
315 pos = strchr(line, '\n');
320 if (strncmp(line, "DEVTYPE=", 8) == 0)
321 udev_device_set_devtype(udev_device, &line[8]);
322 else if (strncmp(line, "MAJOR=", 6) == 0)
323 maj = strtoull(&line[6], NULL, 10);
324 else if (strncmp(line, "MINOR=", 6) == 0)
325 min = strtoull(&line[6], NULL, 10);
326 else if (strncmp(line, "DEVNAME=", 8) == 0)
327 udev_device_set_knodename(udev_device, &line[8]);
329 udev_device_add_property_from_string(udev_device, line);
332 udev_device->devnum = makedev(maj, min);
338 static void device_load_info(struct udev_device *device)
340 device->info_loaded = 1;
341 udev_device_read_uevent_file(device);
342 udev_device_read_db(device);
345 void udev_device_set_info_loaded(struct udev_device *device)
347 device->info_loaded = 1;
350 struct udev_device *udev_device_new(struct udev *udev)
352 struct udev_device *udev_device;
353 struct udev_list_entry *list_entry;
358 udev_device = calloc(1, sizeof(struct udev_device));
359 if (udev_device == NULL)
361 udev_device->refcount = 1;
362 udev_device->udev = udev;
363 udev_list_init(&udev_device->devlinks_list);
364 udev_list_init(&udev_device->properties_list);
365 udev_list_init(&udev_device->sysattr_list);
366 udev_device->event_timeout = -1;
367 udev_device->watch_handle = -1;
368 /* copy global properties */
369 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
370 udev_device_add_property(udev_device,
371 udev_list_entry_get_name(list_entry),
372 udev_list_entry_get_value(list_entry));
373 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
378 * udev_device_new_from_syspath:
379 * @udev: udev library context
380 * @syspath: sys device path including sys directory
382 * Create new udev device, and fill in information from the sys
383 * device and the udev database entry. The syspath is the absolute
384 * path to the device, including the sys mount point.
386 * The initial refcount is 1, and needs to be decremented to
387 * release the resources of the udev device.
389 * Returns: a new udev device, or #NULL, if it does not exist
391 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
395 char path[UTIL_PATH_SIZE];
398 struct udev_device *udev_device;
405 /* path starts in sys */
406 len = strlen(udev_get_sys_path(udev));
407 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
408 info(udev, "not in sys :%s\n", syspath);
412 /* path is not a root directory */
413 subdir = &syspath[len+1];
414 pos = strrchr(subdir, '/');
415 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
416 dbg(udev, "not a subdir :%s\n", syspath);
420 /* resolve possible symlink to real path */
421 util_strscpy(path, sizeof(path), syspath);
422 util_resolve_sys_link(udev, path, sizeof(path));
424 if (strncmp(&syspath[len], "/devices/", 9) == 0) {
425 char file[UTIL_PATH_SIZE];
427 /* all "devices" require a "uevent" file */
428 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
429 if (stat(file, &statbuf) != 0) {
430 dbg(udev, "not a device: %s\n", syspath);
434 /* everything else just needs to be a directory */
435 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
436 dbg(udev, "directory not found: %s\n", syspath);
441 udev_device = udev_device_new(udev);
442 if (udev_device == NULL)
445 udev_device_set_syspath(udev_device, path);
446 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
452 * udev_device_new_from_devnum:
453 * @udev: udev library context
454 * @type: char or block device
455 * @devnum: device major/minor number
457 * Create new udev device, and fill in information from the sys
458 * device and the udev database entry. The device is looked-up
459 * by its major/minor number and type. Character and block device
460 * numbers are not unique across the two types.
462 * The initial refcount is 1, and needs to be decremented to
463 * release the resources of the udev device.
465 * Returns: a new udev device, or #NULL, if it does not exist
467 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
469 char path[UTIL_PATH_SIZE];
470 const char *type_str;
474 else if (type == 'c')
479 /* use /sys/dev/{block,char}/<maj>:<min> link */
480 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
481 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
482 return udev_device_new_from_syspath(udev, path);
486 * udev_device_new_from_subsystem_sysname:
487 * @udev: udev library context
488 * @subsystem: the subsystem of the device
489 * @sysname: the name of the device
491 * Create new udev device, and fill in information from the sys device
492 * and the udev database entry. The device is looked up by the subsystem
493 * and name string of the device, like "mem" / "zero", or "block" / "sda".
495 * The initial refcount is 1, and needs to be decremented to
496 * release the resources of the udev device.
498 * Returns: a new udev device, or #NULL, if it does not exist
500 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
502 char path_full[UTIL_PATH_SIZE];
508 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
510 if (strcmp(subsystem, "subsystem") == 0) {
511 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
512 if (stat(path_full, &statbuf) == 0)
515 util_strscpyl(path, l, "/bus/", sysname, NULL);
516 if (stat(path_full, &statbuf) == 0)
519 util_strscpyl(path, l, "/class/", sysname, NULL);
520 if (stat(path_full, &statbuf) == 0)
525 if (strcmp(subsystem, "module") == 0) {
526 util_strscpyl(path, l, "/module/", sysname, NULL);
527 if (stat(path_full, &statbuf) == 0)
532 if (strcmp(subsystem, "drivers") == 0) {
533 char subsys[UTIL_NAME_SIZE];
536 util_strscpy(subsys, sizeof(subsys), sysname);
537 driver = strchr(subsys, ':');
538 if (driver != NULL) {
542 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
543 if (stat(path_full, &statbuf) == 0)
546 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
547 if (stat(path_full, &statbuf) == 0)
553 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
554 if (stat(path_full, &statbuf) == 0)
557 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
558 if (stat(path_full, &statbuf) == 0)
561 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
562 if (stat(path_full, &statbuf) == 0)
567 return udev_device_new_from_syspath(udev, path_full);
571 * udev_device_new_from_environment
572 * @udev: udev library context
574 * Create new udev device, and fill in information from the
575 * current process environment. This only works reliable if
576 * the process is called from a udev rule. It is usually used
577 * for tools executed from IMPORT= rules.
579 * The initial refcount is 1, and needs to be decremented to
580 * release the resources of the udev device.
582 * Returns: a new udev device, or #NULL, if it does not exist
584 struct udev_device *udev_device_new_from_environment(struct udev *udev)
587 struct udev_device *udev_device;
589 udev_device = udev_device_new(udev);
590 if (udev_device == NULL)
592 udev_device_set_info_loaded(udev_device);
594 for (i = 0; environ[i] != NULL; i++)
595 udev_device_add_property_from_string_parse(udev_device, environ[i]);
597 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
598 info(udev, "missing values, invalid device\n");
599 udev_device_unref(udev_device);
606 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
608 struct udev_device *udev_device_parent = NULL;
609 char path[UTIL_PATH_SIZE];
612 util_strscpy(path, sizeof(path), udev_device->syspath);
613 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
617 pos = strrchr(subdir, '/');
618 if (pos == NULL || pos < &subdir[2])
621 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
622 if (udev_device_parent != NULL)
623 return udev_device_parent;
629 * udev_device_get_parent:
630 * @udev_device: the device to start searching from
632 * Find the next parent device, and fill in information from the sys
633 * device and the udev database entry.
635 * The returned the device is not referenced. It is attached to the
636 * child device, and will be cleaned up when the child device
639 * It is not necessarily just the upper level directory, empty or not
640 * recognized sys directories are ignored.
642 * It can be called as many times as needed, without caring about
645 * Returns: a new udev device, or #NULL, if it no parent exist.
647 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
649 if (udev_device == NULL)
651 if (!udev_device->parent_set) {
652 udev_device->parent_set = 1;
653 udev_device->parent_device = device_new_from_parent(udev_device);
655 if (udev_device->parent_device != NULL)
656 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
657 return udev_device->parent_device;
661 * udev_device_get_parent_with_subsystem_devtype:
662 * @udev_device: udev device to start searching from
663 * @subsystem: the subsystem of the device
664 * @devtype: the type (DEVTYPE) of the device
666 * Find the next parent device, with a matching subsystem and devtype
667 * value, and fill in information from the sys device and the udev
670 * If devtype is #NULL, only subsystem is checked, and any devtype will
673 * The returned the device is not referenced. It is attached to the
674 * child device, and will be cleaned up when the child device
677 * It can be called as many times as needed, without caring about
680 * Returns: a new udev device, or #NULL if no matching parent exists.
682 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
684 struct udev_device *parent;
686 if (subsystem == NULL)
689 parent = udev_device_get_parent(udev_device);
690 while (parent != NULL) {
691 const char *parent_subsystem;
692 const char *parent_devtype;
694 parent_subsystem = udev_device_get_subsystem(parent);
695 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
698 parent_devtype = udev_device_get_devtype(parent);
699 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
702 parent = udev_device_get_parent(parent);
708 * udev_device_get_udev:
709 * @udev_device: udev device
711 * Retrieve the udev library context the device was created with.
713 * Returns: the udev library context
715 struct udev *udev_device_get_udev(struct udev_device *udev_device)
717 if (udev_device == NULL)
719 return udev_device->udev;
724 * @udev_device: udev device
726 * Take a reference of a udev device.
728 * Returns: the passed udev device
730 struct udev_device *udev_device_ref(struct udev_device *udev_device)
732 if (udev_device == NULL)
734 udev_device->refcount++;
740 * @udev_device: udev device
742 * Drop a reference of a udev device. If the refcount reaches zero,
743 * the resources of the device will be released.
746 void udev_device_unref(struct udev_device *udev_device)
748 if (udev_device == NULL)
750 udev_device->refcount--;
751 if (udev_device->refcount > 0)
753 if (udev_device->parent_device != NULL)
754 udev_device_unref(udev_device->parent_device);
755 free(udev_device->syspath);
756 free(udev_device->sysname);
757 free(udev_device->devnode);
758 free(udev_device->subsystem);
759 free(udev_device->devtype);
760 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
761 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
762 free(udev_device->action);
763 free(udev_device->driver);
764 free(udev_device->devpath_old);
765 free(udev_device->sysname_old);
766 free(udev_device->knodename);
767 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
768 free(udev_device->envp);
769 free(udev_device->monitor_buf);
770 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
775 * udev_device_get_devpath:
776 * @udev_device: udev device
778 * Retrieve the kernel devpath value of the udev device. The path
779 * does not contain the sys mount point, and starts with a '/'.
781 * Returns: the devpath of the udev device
783 const char *udev_device_get_devpath(struct udev_device *udev_device)
785 if (udev_device == NULL)
787 return udev_device->devpath;
791 * udev_device_get_syspath:
792 * @udev_device: udev device
794 * Retrieve the sys path of the udev device. The path is an
795 * absolute path and starts with the sys mount point.
797 * Returns: the sys path of the udev device
799 const char *udev_device_get_syspath(struct udev_device *udev_device)
801 if (udev_device == NULL)
803 return udev_device->syspath;
807 * udev_device_get_sysname:
808 * @udev_device: udev device
810 * Returns: the sys name of the device device
812 const char *udev_device_get_sysname(struct udev_device *udev_device)
814 if (udev_device == NULL)
816 return udev_device->sysname;
820 * udev_device_get_sysnum:
821 * @udev_device: udev device
823 * Returns: the trailing number of of the device name
825 const char *udev_device_get_sysnum(struct udev_device *udev_device)
827 if (udev_device == NULL)
829 return udev_device->sysnum;
833 * udev_device_get_devnode:
834 * @udev_device: udev device
836 * Retrieve the device node file name belonging to the udev device.
837 * The path is an absolute path, and starts with the device directory.
839 * Returns: the device node file name of the udev device, or #NULL if no device node exists
841 const char *udev_device_get_devnode(struct udev_device *udev_device)
843 if (udev_device == NULL)
845 if (!udev_device->info_loaded)
846 device_load_info(udev_device);
847 return udev_device->devnode;
851 * udev_device_get_subsystem:
852 * @udev_device: udev device
854 * Retrieve the subsystem string of the udev device. The string does not
857 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
859 const char *udev_device_get_subsystem(struct udev_device *udev_device)
861 char subsystem[UTIL_NAME_SIZE];
863 if (udev_device == NULL)
865 if (!udev_device->subsystem_set) {
866 udev_device->subsystem_set = 1;
867 /* read "subsystem" link */
868 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
869 udev_device_set_subsystem(udev_device, subsystem);
870 return udev_device->subsystem;
873 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
874 udev_device_set_subsystem(udev_device, "module");
875 return udev_device->subsystem;
877 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
878 udev_device_set_subsystem(udev_device, "drivers");
879 return udev_device->subsystem;
881 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
882 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
883 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
884 udev_device_set_subsystem(udev_device, "subsystem");
885 return udev_device->subsystem;
888 return udev_device->subsystem;
892 * udev_device_get_devtype:
893 * @udev_device: udev device
895 * Retrieve the devtype string of the udev device.
897 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
899 const char *udev_device_get_devtype(struct udev_device *udev_device)
901 if (udev_device == NULL)
903 if (!udev_device->devtype_set) {
904 udev_device->devtype_set = 1;
905 if (!udev_device->info_loaded)
906 udev_device_read_uevent_file(udev_device);
908 return udev_device->devtype;
912 * udev_device_get_devlinks_list_entry:
913 * @udev_device: udev device
915 * Retrieve the list of device links pointing to the device file of
916 * the udev device. The next list entry can be retrieved with
917 * udev_list_entry_next(), which returns #NULL if no more entries exist.
918 * The devlink path can be retrieved from the list entry by
919 * udev_list_entry_get_name(). The path is an absolute path, and starts with
920 * the device directory.
922 * Returns: the first entry of the device node link list
924 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
926 if (udev_device == NULL)
928 if (!udev_device->info_loaded)
929 device_load_info(udev_device);
930 return udev_list_get_entry(&udev_device->devlinks_list);
933 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
935 udev_device->devlinks_uptodate = 0;
936 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
940 * udev_device_get_properties_list_entry:
941 * @udev_device: udev device
943 * Retrieve the list of key/value device properties of the udev
944 * device. The next list entry can be retrieved with udev_list_entry_next(),
945 * which returns #NULL if no more entries exist. The property name
946 * can be retrieved from the list entry by udev_list_get_name(),
947 * the property value by udev_list_get_value().
949 * Returns: the first entry of the property list
951 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
953 if (udev_device == NULL)
955 if (!udev_device->info_loaded)
956 device_load_info(udev_device);
957 if (!udev_device->devlinks_uptodate) {
958 char symlinks[UTIL_PATH_SIZE];
959 struct udev_list_entry *list_entry;
961 udev_device->devlinks_uptodate = 1;
962 list_entry = udev_device_get_devlinks_list_entry(udev_device);
963 if (list_entry != NULL) {
968 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
969 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
970 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
971 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
974 return udev_list_get_entry(&udev_device->properties_list);
978 * udev_device_get_driver:
979 * @udev_device: udev device
981 * Returns: the driver string, or #NULL if there is no driver attached.
983 const char *udev_device_get_driver(struct udev_device *udev_device)
985 char driver[UTIL_NAME_SIZE];
987 if (udev_device == NULL)
989 if (!udev_device->driver_set) {
990 udev_device->driver_set = 1;
991 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
992 udev_device->driver = strdup(driver);
994 return udev_device->driver;
998 * udev_device_get_devnum:
999 * @udev_device: udev device
1001 * Returns: the device major/minor number.
1003 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1005 if (udev_device == NULL)
1006 return makedev(0, 0);
1007 if (!udev_device->info_loaded)
1008 device_load_info(udev_device);
1009 return udev_device->devnum;
1013 * udev_device_get_action:
1014 * @udev_device: udev device
1016 * This is only valid if the device was received through a monitor. Devices read from
1017 * sys do not have an action string. Usual actions are: add, remove, change, online,
1020 * Returns: the kernel action value, or #NULL if there is no action value available.
1022 const char *udev_device_get_action(struct udev_device *udev_device)
1024 if (udev_device == NULL)
1026 return udev_device->action;
1030 * udev_device_get_devnum:
1031 * @udev_device: udev device
1033 * This is only valid if the device was received through a monitor. Devices read from
1034 * sys do not have a sequence number.
1036 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1038 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1040 if (udev_device == NULL)
1042 return udev_device->seqnum;
1046 * udev_device_get_sysattr_value:
1047 * @udev_device: udev device
1048 * @sysattr: attribute name
1050 * The retrieved value is cached in the device. Repeated calls will return the same
1051 * value and not open the attribute again.
1053 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1055 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1057 struct udev_list_entry *list_entry;
1058 char path[UTIL_PATH_SIZE];
1060 struct stat statbuf;
1063 const char *val = NULL;
1065 if (udev_device == NULL)
1067 if (sysattr == NULL)
1070 /* look for possibly already cached result */
1071 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1072 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1073 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1074 sysattr, udev_list_entry_get_value(list_entry));
1075 return udev_list_entry_get_value(list_entry);
1079 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1080 if (lstat(path, &statbuf) != 0) {
1081 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1082 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1086 if (S_ISLNK(statbuf.st_mode)) {
1087 char target[UTIL_NAME_SIZE];
1091 /* some core links return the last element of the target path */
1092 if (strcmp(sysattr, "driver") != 0 &&
1093 strcmp(sysattr, "subsystem") != 0 &&
1094 strcmp(sysattr, "module") != 0)
1097 len = readlink(path, target, sizeof(target));
1100 pos = strrchr(target, '/');
1103 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1104 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1105 val = udev_list_entry_get_value(list_entry);
1111 /* skip directories */
1112 if (S_ISDIR(statbuf.st_mode))
1115 /* skip non-readable files */
1116 if ((statbuf.st_mode & S_IRUSR) == 0)
1119 /* read attribute value */
1120 fd = open(path, O_RDONLY);
1122 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1125 size = read(fd, value, sizeof(value));
1129 if (size == sizeof(value))
1132 /* got a valid value, store it in cache and return it */
1134 util_remove_trailing_chars(value, '\n');
1135 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1136 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1137 val = udev_list_entry_get_value(list_entry);
1142 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1147 free(udev_device->syspath);
1148 udev_device->syspath = strdup(syspath);
1149 if (udev_device->syspath == NULL)
1151 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1152 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1154 pos = strrchr(udev_device->syspath, '/');
1157 udev_device->sysname = strdup(&pos[1]);
1158 if (udev_device->sysname == NULL)
1161 /* some devices have '!' in their name, change that to '/' */
1163 while (udev_device->sysname[len] != '\0') {
1164 if (udev_device->sysname[len] == '!')
1165 udev_device->sysname[len] = '/';
1169 /* trailing number */
1170 while (len > 0 && isdigit(udev_device->sysname[--len]))
1171 udev_device->sysnum = &udev_device->sysname[len];
1173 /* sysname is completely numeric */
1175 udev_device->sysnum = NULL;
1180 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1182 free(udev_device->subsystem);
1183 udev_device->subsystem = strdup(subsystem);
1184 if (udev_device->subsystem == NULL)
1186 udev_device->subsystem_set = 1;
1187 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1191 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1193 free(udev_device->devtype);
1194 udev_device->devtype = strdup(devtype);
1195 if (udev_device->devtype == NULL)
1197 udev_device->devtype_set = 1;
1198 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1202 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1204 free(udev_device->devnode);
1205 udev_device->devnode = strdup(devnode);
1206 if (devnode == NULL)
1208 if (udev_device->devnode == NULL)
1210 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1214 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1216 struct udev_list_entry *list_entry;
1218 udev_device->devlinks_uptodate = 0;
1219 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1220 if (list_entry == NULL)
1223 udev_list_entry_set_flags(list_entry, 1);
1227 #define ENVP_SIZE 128
1228 #define MONITOR_BUF_SIZE 4096
1229 static int update_envp_monitor_buf(struct udev_device *udev_device)
1231 struct udev_list_entry *list_entry;
1236 /* monitor buffer of property strings */
1237 free(udev_device->monitor_buf);
1238 udev_device->monitor_buf_len = 0;
1239 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1240 if (udev_device->monitor_buf == NULL)
1243 /* envp array, strings will point into monitor buffer */
1244 if (udev_device->envp == NULL)
1245 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1246 if (udev_device->envp == NULL)
1250 s = udev_device->monitor_buf;
1251 l = MONITOR_BUF_SIZE;
1252 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1255 key = udev_list_entry_get_name(list_entry);
1256 /* skip private variables */
1260 /* add string to envp array */
1261 udev_device->envp[i++] = s;
1262 if (i+1 >= ENVP_SIZE)
1265 /* add property string to monitor buffer */
1266 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1269 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1273 udev_device->envp[i] = NULL;
1274 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1275 udev_device->envp_uptodate = 1;
1276 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1277 i, udev_device->monitor_buf_len);
1281 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1283 if (!udev_device->envp_uptodate)
1284 if (update_envp_monitor_buf(udev_device) != 0)
1286 return udev_device->envp;
1289 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1291 if (!udev_device->envp_uptodate)
1292 if (update_envp_monitor_buf(udev_device) != 0)
1294 *buf = udev_device->monitor_buf;
1295 return udev_device->monitor_buf_len;
1298 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1300 free(udev_device->action);
1301 udev_device->action = strdup(action);
1302 if (udev_device->action == NULL)
1304 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1308 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1310 free(udev_device->driver);
1311 udev_device->driver = strdup(driver);
1312 if (udev_device->driver == NULL)
1314 udev_device->driver_set = 1;
1315 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1319 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1321 return udev_device->devpath_old;
1324 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1329 free(udev_device->devpath_old);
1330 udev_device->devpath_old = strdup(devpath_old);
1331 if (udev_device->devpath_old == NULL)
1333 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1335 pos = strrchr(udev_device->devpath_old, '/');
1338 udev_device->sysname_old = strdup(&pos[1]);
1339 if (udev_device->sysname_old == NULL)
1342 /* some devices have '!' in their name, change that to '/' */
1344 while (udev_device->sysname_old[len] != '\0') {
1345 if (udev_device->sysname_old[len] == '!')
1346 udev_device->sysname_old[len] = '/';
1352 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1354 if (udev_device == NULL)
1356 return udev_device->sysname_old;
1359 const char *udev_device_get_knodename(struct udev_device *udev_device)
1361 return udev_device->knodename;
1364 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1366 free(udev_device->knodename);
1367 udev_device->knodename = strdup(knodename);
1368 if (udev_device->knodename == NULL)
1370 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1374 int udev_device_get_timeout(struct udev_device *udev_device)
1376 return udev_device->timeout;
1379 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1381 udev_device->timeout = timeout;
1384 int udev_device_get_event_timeout(struct udev_device *udev_device)
1386 if (!udev_device->info_loaded)
1387 device_load_info(udev_device);
1388 return udev_device->event_timeout;
1391 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1393 udev_device->event_timeout = event_timeout;
1397 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1401 udev_device->seqnum = seqnum;
1402 snprintf(num, sizeof(num), "%llu", seqnum);
1403 udev_device_add_property(udev_device, "SEQNUM", num);
1407 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1411 udev_device->devnum = devnum;
1413 snprintf(num, sizeof(num), "%u", major(devnum));
1414 udev_device_add_property(udev_device, "MAJOR", num);
1415 snprintf(num, sizeof(num), "%u", minor(devnum));
1416 udev_device_add_property(udev_device, "MINOR", num);
1420 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1422 if (!udev_device->info_loaded)
1423 device_load_info(udev_device);
1424 return udev_device->devlink_priority;
1427 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1429 udev_device->devlink_priority = prio;
1433 int udev_device_get_watch_handle(struct udev_device *udev_device)
1435 if (!udev_device->info_loaded)
1436 device_load_info(udev_device);
1437 return udev_device->watch_handle;
1440 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1442 udev_device->watch_handle = handle;