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 struct udev_list_node tags_list;
64 unsigned long long int seqnum;
75 bool devlinks_uptodate;
84 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
86 udev_device->envp_uptodate = false;
88 struct udev_list_entry *list_entry;
90 list_entry = udev_device_get_properties_list_entry(udev_device);
91 list_entry = udev_list_entry_get_by_name(list_entry, key);
92 if (list_entry != NULL)
93 udev_list_entry_delete(list_entry);
96 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
99 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
101 char name[UTIL_LINE_SIZE];
104 util_strscpy(name, sizeof(name), property);
105 val = strchr(name, '=');
112 return udev_device_add_property(udev_device, name, val);
116 * parse property string, and if needed, update internal values accordingly
118 * udev_device_add_property_from_string_parse_finish() needs to be
119 * called after adding properties, and its return value checked
121 * udev_device_set_info_loaded() needs to be set, to avoid trying
122 * to use a device without a DEVPATH set
124 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
126 if (strncmp(property, "DEVPATH=", 8) == 0) {
127 char path[UTIL_PATH_SIZE];
129 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
130 udev_device_set_syspath(udev_device, path);
131 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
132 udev_device_set_subsystem(udev_device, &property[10]);
133 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
134 udev_device_set_devtype(udev_device, &property[8]);
135 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
136 if (property[8] == '/')
137 udev_device_set_devnode(udev_device, &property[8]);
139 udev_device_set_knodename(udev_device, &property[8]);
140 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
141 char devlinks[UTIL_PATH_SIZE];
145 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
147 next = strchr(slink, ' ');
148 while (next != NULL) {
150 udev_device_add_devlink(udev_device, slink, 0);
152 next = strchr(slink, ' ');
154 if (slink[0] != '\0')
155 udev_device_add_devlink(udev_device, slink, 0);
156 } else if (strncmp(property, "TAGS=", 5) == 0) {
157 char tags[UTIL_PATH_SIZE];
160 util_strscpy(tags, sizeof(tags), &property[5]);
161 next = strchr(tags, ':');
164 while (next[0] != '\0') {
168 next = strchr(tag, ':');
173 udev_device_add_tag(udev_device, tag);
176 } else if (strncmp(property, "DRIVER=", 7) == 0) {
177 udev_device_set_driver(udev_device, &property[7]);
178 } else if (strncmp(property, "ACTION=", 7) == 0) {
179 udev_device_set_action(udev_device, &property[7]);
180 } else if (strncmp(property, "MAJOR=", 6) == 0) {
181 udev_device->maj = strtoull(&property[6], NULL, 10);
182 } else if (strncmp(property, "MINOR=", 6) == 0) {
183 udev_device->min = strtoull(&property[6], NULL, 10);
184 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
185 udev_device_set_devpath_old(udev_device, &property[12]);
186 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
187 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
188 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
189 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
191 udev_device_add_property_from_string(udev_device, property);
195 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
197 if (udev_device->maj > 0)
198 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
199 udev_device->maj = 0;
200 udev_device->min = 0;
202 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
208 * udev_device_get_property_value:
209 * @udev_device: udev device
210 * @key: property name
212 * Returns: the value of a device property, or #NULL if there is no such property.
214 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
216 struct udev_list_entry *list_entry;
218 if (udev_device == NULL)
223 list_entry = udev_device_get_properties_list_entry(udev_device);
224 list_entry = udev_list_entry_get_by_name(list_entry, key);
225 return udev_list_entry_get_value(list_entry);
228 int udev_device_read_db(struct udev_device *udev_device)
231 char filename[UTIL_PATH_SIZE];
232 char line[UTIL_LINE_SIZE];
235 if (udev_device->db_loaded)
238 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/",
239 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
241 if (lstat(filename, &stats) != 0) {
242 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
245 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
246 char target[UTIL_PATH_SIZE];
247 char devnode[UTIL_PATH_SIZE];
251 target_len = readlink(filename, target, sizeof(target));
252 if (target_len <= 0 || target_len == sizeof(target)) {
253 info(udev_device->udev, "error reading db link %s: %m\n", filename);
256 target[target_len] = '\0';
258 next = strchr(target, ' ');
263 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
264 udev_device_set_devnode(udev_device, devnode);
265 while (next != NULL) {
266 char devlink[UTIL_PATH_SIZE];
270 next = strchr(next, ' ');
275 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
276 udev_device_add_devlink(udev_device, devlink, 0);
278 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
282 f = fopen(filename, "re");
284 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
287 udev_device->db_loaded = true;
289 while (fgets(line, sizeof(line), f)) {
300 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
301 udev_device_set_devnode(udev_device, filename);
304 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
305 udev_device_add_devlink(udev_device, filename, 0);
308 udev_device_set_devlink_priority(udev_device, atoi(val));
311 udev_device_set_event_timeout(udev_device, atoi(val));
314 udev_device_add_property_from_string(udev_device, val);
317 udev_device_add_tag(udev_device, val);
320 udev_device_set_watch_handle(udev_device, atoi(val));
326 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
330 int udev_device_read_uevent_file(struct udev_device *udev_device)
332 char filename[UTIL_PATH_SIZE];
334 char line[UTIL_LINE_SIZE];
338 if (udev_device->uevent_loaded)
341 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
342 f = fopen(filename, "re");
345 udev_device->uevent_loaded = true;
347 while (fgets(line, sizeof(line), f)) {
350 pos = strchr(line, '\n');
355 if (strncmp(line, "DEVTYPE=", 8) == 0)
356 udev_device_set_devtype(udev_device, &line[8]);
357 else if (strncmp(line, "MAJOR=", 6) == 0)
358 maj = strtoull(&line[6], NULL, 10);
359 else if (strncmp(line, "MINOR=", 6) == 0)
360 min = strtoull(&line[6], NULL, 10);
361 else if (strncmp(line, "DEVNAME=", 8) == 0)
362 udev_device_set_knodename(udev_device, &line[8]);
364 udev_device_add_property_from_string(udev_device, line);
367 udev_device->devnum = makedev(maj, min);
372 void udev_device_set_info_loaded(struct udev_device *device)
374 device->info_loaded = true;
377 struct udev_device *udev_device_new(struct udev *udev)
379 struct udev_device *udev_device;
380 struct udev_list_entry *list_entry;
385 udev_device = calloc(1, sizeof(struct udev_device));
386 if (udev_device == NULL)
388 udev_device->refcount = 1;
389 udev_device->udev = udev;
390 udev_list_init(&udev_device->devlinks_list);
391 udev_list_init(&udev_device->properties_list);
392 udev_list_init(&udev_device->sysattr_list);
393 udev_list_init(&udev_device->tags_list);
394 udev_device->event_timeout = -1;
395 udev_device->watch_handle = -1;
396 /* copy global properties */
397 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
398 udev_device_add_property(udev_device,
399 udev_list_entry_get_name(list_entry),
400 udev_list_entry_get_value(list_entry));
401 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
406 * udev_device_new_from_syspath:
407 * @udev: udev library context
408 * @syspath: sys device path including sys directory
410 * Create new udev device, and fill in information from the sys
411 * device and the udev database entry. The syspath is the absolute
412 * path to the device, including the sys mount point.
414 * The initial refcount is 1, and needs to be decremented to
415 * release the resources of the udev device.
417 * Returns: a new udev device, or #NULL, if it does not exist
419 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
423 char path[UTIL_PATH_SIZE];
426 struct udev_device *udev_device;
433 /* path starts in sys */
434 len = strlen(udev_get_sys_path(udev));
435 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
436 info(udev, "not in sys :%s\n", syspath);
440 /* path is not a root directory */
441 subdir = &syspath[len+1];
442 pos = strrchr(subdir, '/');
443 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
444 dbg(udev, "not a subdir :%s\n", syspath);
448 /* resolve possible symlink to real path */
449 util_strscpy(path, sizeof(path), syspath);
450 util_resolve_sys_link(udev, path, sizeof(path));
452 if (strncmp(&path[len], "/devices/", 9) == 0) {
453 char file[UTIL_PATH_SIZE];
455 /* all "devices" require a "uevent" file */
456 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
457 if (stat(file, &statbuf) != 0) {
458 dbg(udev, "not a device: %s\n", syspath);
462 /* everything else just needs to be a directory */
463 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
464 dbg(udev, "directory not found: %s\n", syspath);
469 udev_device = udev_device_new(udev);
470 if (udev_device == NULL)
473 udev_device_set_syspath(udev_device, path);
474 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
480 * udev_device_new_from_devnum:
481 * @udev: udev library context
482 * @type: char or block device
483 * @devnum: device major/minor number
485 * Create new udev device, and fill in information from the sys
486 * device and the udev database entry. The device is looked-up
487 * by its major/minor number and type. Character and block device
488 * numbers are not unique across the two types.
490 * The initial refcount is 1, and needs to be decremented to
491 * release the resources of the udev device.
493 * Returns: a new udev device, or #NULL, if it does not exist
495 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
497 char path[UTIL_PATH_SIZE];
498 const char *type_str;
502 else if (type == 'c')
507 /* use /sys/dev/{block,char}/<maj>:<min> link */
508 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
509 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
510 return udev_device_new_from_syspath(udev, path);
514 * udev_device_new_from_subsystem_sysname:
515 * @udev: udev library context
516 * @subsystem: the subsystem of the device
517 * @sysname: the name of the device
519 * Create new udev device, and fill in information from the sys device
520 * and the udev database entry. The device is looked up by the subsystem
521 * and name string of the device, like "mem" / "zero", or "block" / "sda".
523 * The initial refcount is 1, and needs to be decremented to
524 * release the resources of the udev device.
526 * Returns: a new udev device, or #NULL, if it does not exist
528 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
530 char path_full[UTIL_PATH_SIZE];
536 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
538 if (strcmp(subsystem, "subsystem") == 0) {
539 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
540 if (stat(path_full, &statbuf) == 0)
543 util_strscpyl(path, l, "/bus/", sysname, NULL);
544 if (stat(path_full, &statbuf) == 0)
547 util_strscpyl(path, l, "/class/", sysname, NULL);
548 if (stat(path_full, &statbuf) == 0)
553 if (strcmp(subsystem, "module") == 0) {
554 util_strscpyl(path, l, "/module/", sysname, NULL);
555 if (stat(path_full, &statbuf) == 0)
560 if (strcmp(subsystem, "drivers") == 0) {
561 char subsys[UTIL_NAME_SIZE];
564 util_strscpy(subsys, sizeof(subsys), sysname);
565 driver = strchr(subsys, ':');
566 if (driver != NULL) {
570 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
571 if (stat(path_full, &statbuf) == 0)
574 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
575 if (stat(path_full, &statbuf) == 0)
581 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
582 if (stat(path_full, &statbuf) == 0)
585 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
586 if (stat(path_full, &statbuf) == 0)
589 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
590 if (stat(path_full, &statbuf) == 0)
595 return udev_device_new_from_syspath(udev, path_full);
599 * udev_device_new_from_environment
600 * @udev: udev library context
602 * Create new udev device, and fill in information from the
603 * current process environment. This only works reliable if
604 * the process is called from a udev rule. It is usually used
605 * for tools executed from IMPORT= rules.
607 * The initial refcount is 1, and needs to be decremented to
608 * release the resources of the udev device.
610 * Returns: a new udev device, or #NULL, if it does not exist
612 struct udev_device *udev_device_new_from_environment(struct udev *udev)
615 struct udev_device *udev_device;
617 udev_device = udev_device_new(udev);
618 if (udev_device == NULL)
620 udev_device_set_info_loaded(udev_device);
622 for (i = 0; environ[i] != NULL; i++)
623 udev_device_add_property_from_string_parse(udev_device, environ[i]);
625 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
626 info(udev, "missing values, invalid device\n");
627 udev_device_unref(udev_device);
634 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
636 struct udev_device *udev_device_parent = NULL;
637 char path[UTIL_PATH_SIZE];
640 util_strscpy(path, sizeof(path), udev_device->syspath);
641 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
645 pos = strrchr(subdir, '/');
646 if (pos == NULL || pos < &subdir[2])
649 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
650 if (udev_device_parent != NULL)
651 return udev_device_parent;
657 * udev_device_get_parent:
658 * @udev_device: the device to start searching from
660 * Find the next parent device, and fill in information from the sys
661 * device and the udev database entry.
663 * The returned the device is not referenced. It is attached to the
664 * child device, and will be cleaned up when the child device
667 * It is not necessarily just the upper level directory, empty or not
668 * recognized sys directories are ignored.
670 * It can be called as many times as needed, without caring about
673 * Returns: a new udev device, or #NULL, if it no parent exist.
675 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
677 if (udev_device == NULL)
679 if (!udev_device->parent_set) {
680 udev_device->parent_set = true;
681 udev_device->parent_device = device_new_from_parent(udev_device);
683 if (udev_device->parent_device != NULL)
684 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
685 return udev_device->parent_device;
689 * udev_device_get_parent_with_subsystem_devtype:
690 * @udev_device: udev device to start searching from
691 * @subsystem: the subsystem of the device
692 * @devtype: the type (DEVTYPE) of the device
694 * Find the next parent device, with a matching subsystem and devtype
695 * value, and fill in information from the sys device and the udev
698 * If devtype is #NULL, only subsystem is checked, and any devtype will
701 * The returned the device is not referenced. It is attached to the
702 * child device, and will be cleaned up when the child device
705 * It can be called as many times as needed, without caring about
708 * Returns: a new udev device, or #NULL if no matching parent exists.
710 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
712 struct udev_device *parent;
714 if (subsystem == NULL)
717 parent = udev_device_get_parent(udev_device);
718 while (parent != NULL) {
719 const char *parent_subsystem;
720 const char *parent_devtype;
722 parent_subsystem = udev_device_get_subsystem(parent);
723 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
726 parent_devtype = udev_device_get_devtype(parent);
727 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
730 parent = udev_device_get_parent(parent);
736 * udev_device_get_udev:
737 * @udev_device: udev device
739 * Retrieve the udev library context the device was created with.
741 * Returns: the udev library context
743 struct udev *udev_device_get_udev(struct udev_device *udev_device)
745 if (udev_device == NULL)
747 return udev_device->udev;
752 * @udev_device: udev device
754 * Take a reference of a udev device.
756 * Returns: the passed udev device
758 struct udev_device *udev_device_ref(struct udev_device *udev_device)
760 if (udev_device == NULL)
762 udev_device->refcount++;
768 * @udev_device: udev device
770 * Drop a reference of a udev device. If the refcount reaches zero,
771 * the resources of the device will be released.
774 void udev_device_unref(struct udev_device *udev_device)
776 if (udev_device == NULL)
778 udev_device->refcount--;
779 if (udev_device->refcount > 0)
781 if (udev_device->parent_device != NULL)
782 udev_device_unref(udev_device->parent_device);
783 free(udev_device->syspath);
784 free(udev_device->sysname);
785 free(udev_device->devnode);
786 free(udev_device->subsystem);
787 free(udev_device->devtype);
788 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
789 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
790 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
791 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
792 free(udev_device->action);
793 free(udev_device->driver);
794 free(udev_device->devpath_old);
795 free(udev_device->sysname_old);
796 free(udev_device->knodename);
797 free(udev_device->envp);
798 free(udev_device->monitor_buf);
799 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
804 * udev_device_get_devpath:
805 * @udev_device: udev device
807 * Retrieve the kernel devpath value of the udev device. The path
808 * does not contain the sys mount point, and starts with a '/'.
810 * Returns: the devpath of the udev device
812 const char *udev_device_get_devpath(struct udev_device *udev_device)
814 if (udev_device == NULL)
816 return udev_device->devpath;
820 * udev_device_get_syspath:
821 * @udev_device: udev device
823 * Retrieve the sys path of the udev device. The path is an
824 * absolute path and starts with the sys mount point.
826 * Returns: the sys path of the udev device
828 const char *udev_device_get_syspath(struct udev_device *udev_device)
830 if (udev_device == NULL)
832 return udev_device->syspath;
836 * udev_device_get_sysname:
837 * @udev_device: udev device
839 * Returns: the sys name of the device device
841 const char *udev_device_get_sysname(struct udev_device *udev_device)
843 if (udev_device == NULL)
845 return udev_device->sysname;
849 * udev_device_get_sysnum:
850 * @udev_device: udev device
852 * Returns: the trailing number of of the device name
854 const char *udev_device_get_sysnum(struct udev_device *udev_device)
856 if (udev_device == NULL)
858 return udev_device->sysnum;
862 * udev_device_get_devnode:
863 * @udev_device: udev device
865 * Retrieve the device node file name belonging to the udev device.
866 * The path is an absolute path, and starts with the device directory.
868 * Returns: the device node file name of the udev device, or #NULL if no device node exists
870 const char *udev_device_get_devnode(struct udev_device *udev_device)
872 if (udev_device == NULL)
874 if (!udev_device->info_loaded)
875 udev_device_read_db(udev_device);
876 return udev_device->devnode;
880 * udev_device_get_subsystem:
881 * @udev_device: udev device
883 * Retrieve the subsystem string of the udev device. The string does not
886 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
888 const char *udev_device_get_subsystem(struct udev_device *udev_device)
890 char subsystem[UTIL_NAME_SIZE];
892 if (udev_device == NULL)
894 if (!udev_device->subsystem_set) {
895 udev_device->subsystem_set = true;
896 /* read "subsystem" link */
897 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
898 udev_device_set_subsystem(udev_device, subsystem);
899 return udev_device->subsystem;
902 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
903 udev_device_set_subsystem(udev_device, "module");
904 return udev_device->subsystem;
906 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
907 udev_device_set_subsystem(udev_device, "drivers");
908 return udev_device->subsystem;
910 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
911 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
912 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
913 udev_device_set_subsystem(udev_device, "subsystem");
914 return udev_device->subsystem;
917 return udev_device->subsystem;
921 * udev_device_get_devtype:
922 * @udev_device: udev device
924 * Retrieve the devtype string of the udev device.
926 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
928 const char *udev_device_get_devtype(struct udev_device *udev_device)
930 if (udev_device == NULL)
932 if (!udev_device->devtype_set) {
933 udev_device->devtype_set = true;
934 udev_device_read_uevent_file(udev_device);
936 return udev_device->devtype;
940 * udev_device_get_devlinks_list_entry:
941 * @udev_device: udev device
943 * Retrieve the list of device links pointing to the device file of
944 * the udev device. The next list entry can be retrieved with
945 * udev_list_entry_next(), which returns #NULL if no more entries exist.
946 * The devlink path can be retrieved from the list entry by
947 * udev_list_entry_get_name(). The path is an absolute path, and starts with
948 * the device directory.
950 * Returns: the first entry of the device node link list
952 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
954 if (udev_device == NULL)
956 if (!udev_device->info_loaded)
957 udev_device_read_db(udev_device);
958 return udev_list_get_entry(&udev_device->devlinks_list);
961 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
963 udev_device->devlinks_uptodate = false;
964 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
968 * udev_device_get_properties_list_entry:
969 * @udev_device: udev device
971 * Retrieve the list of key/value device properties of the udev
972 * device. The next list entry can be retrieved with udev_list_entry_next(),
973 * which returns #NULL if no more entries exist. The property name
974 * can be retrieved from the list entry by udev_list_get_name(),
975 * the property value by udev_list_get_value().
977 * Returns: the first entry of the property list
979 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
981 if (udev_device == NULL)
983 if (!udev_device->info_loaded) {
984 udev_device_read_uevent_file(udev_device);
985 udev_device_read_db(udev_device);
987 if (!udev_device->devlinks_uptodate) {
988 char symlinks[UTIL_PATH_SIZE];
989 struct udev_list_entry *list_entry;
991 udev_device->devlinks_uptodate = true;
992 list_entry = udev_device_get_devlinks_list_entry(udev_device);
993 if (list_entry != NULL) {
998 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
999 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1000 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1001 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1004 if (!udev_device->tags_uptodate) {
1005 udev_device->tags_uptodate = true;
1006 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1007 char tags[UTIL_PATH_SIZE];
1008 struct udev_list_entry *list_entry;
1013 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1014 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1015 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1016 udev_device_add_property(udev_device, "TAGS", tags);
1019 return udev_list_get_entry(&udev_device->properties_list);
1023 * udev_device_get_driver:
1024 * @udev_device: udev device
1026 * Returns: the driver string, or #NULL if there is no driver attached.
1028 const char *udev_device_get_driver(struct udev_device *udev_device)
1030 char driver[UTIL_NAME_SIZE];
1032 if (udev_device == NULL)
1034 if (!udev_device->driver_set) {
1035 udev_device->driver_set = true;
1036 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1037 udev_device->driver = strdup(driver);
1039 return udev_device->driver;
1043 * udev_device_get_devnum:
1044 * @udev_device: udev device
1046 * Returns: the device major/minor number.
1048 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1050 if (udev_device == NULL)
1051 return makedev(0, 0);
1052 if (!udev_device->info_loaded)
1053 udev_device_read_uevent_file(udev_device);
1054 return udev_device->devnum;
1058 * udev_device_get_action:
1059 * @udev_device: udev device
1061 * This is only valid if the device was received through a monitor. Devices read from
1062 * sys do not have an action string. Usual actions are: add, remove, change, online,
1065 * Returns: the kernel action value, or #NULL if there is no action value available.
1067 const char *udev_device_get_action(struct udev_device *udev_device)
1069 if (udev_device == NULL)
1071 return udev_device->action;
1075 * udev_device_get_devnum:
1076 * @udev_device: udev device
1078 * This is only valid if the device was received through a monitor. Devices read from
1079 * sys do not have a sequence number.
1081 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1083 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1085 if (udev_device == NULL)
1087 return udev_device->seqnum;
1091 * udev_device_get_sysattr_value:
1092 * @udev_device: udev device
1093 * @sysattr: attribute name
1095 * The retrieved value is cached in the device. Repeated calls will return the same
1096 * value and not open the attribute again.
1098 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1100 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1102 struct udev_list_entry *list_entry;
1103 char path[UTIL_PATH_SIZE];
1105 struct stat statbuf;
1108 const char *val = NULL;
1110 if (udev_device == NULL)
1112 if (sysattr == NULL)
1115 /* look for possibly already cached result */
1116 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1117 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1118 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1119 sysattr, udev_list_entry_get_value(list_entry));
1120 return udev_list_entry_get_value(list_entry);
1124 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1125 if (lstat(path, &statbuf) != 0) {
1126 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1127 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1131 if (S_ISLNK(statbuf.st_mode)) {
1132 char target[UTIL_NAME_SIZE];
1136 /* some core links return the last element of the target path */
1137 if (strcmp(sysattr, "driver") != 0 &&
1138 strcmp(sysattr, "subsystem") != 0 &&
1139 strcmp(sysattr, "module") != 0)
1142 len = readlink(path, target, sizeof(target));
1143 if (len <= 0 || len == sizeof(target))
1147 pos = strrchr(target, '/');
1150 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1151 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1152 val = udev_list_entry_get_value(list_entry);
1158 /* skip directories */
1159 if (S_ISDIR(statbuf.st_mode))
1162 /* skip non-readable files */
1163 if ((statbuf.st_mode & S_IRUSR) == 0)
1166 /* read attribute value */
1167 fd = open(path, O_RDONLY|O_CLOEXEC);
1169 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1172 size = read(fd, value, sizeof(value));
1176 if (size == sizeof(value))
1179 /* got a valid value, store it in cache and return it */
1181 util_remove_trailing_chars(value, '\n');
1182 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1183 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1184 val = udev_list_entry_get_value(list_entry);
1189 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1194 free(udev_device->syspath);
1195 udev_device->syspath = strdup(syspath);
1196 if (udev_device->syspath == NULL)
1198 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1199 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1201 pos = strrchr(udev_device->syspath, '/');
1204 udev_device->sysname = strdup(&pos[1]);
1205 if (udev_device->sysname == NULL)
1208 /* some devices have '!' in their name, change that to '/' */
1210 while (udev_device->sysname[len] != '\0') {
1211 if (udev_device->sysname[len] == '!')
1212 udev_device->sysname[len] = '/';
1216 /* trailing number */
1217 while (len > 0 && isdigit(udev_device->sysname[--len]))
1218 udev_device->sysnum = &udev_device->sysname[len];
1220 /* sysname is completely numeric */
1222 udev_device->sysnum = NULL;
1227 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1229 free(udev_device->subsystem);
1230 udev_device->subsystem = strdup(subsystem);
1231 if (udev_device->subsystem == NULL)
1233 udev_device->subsystem_set = true;
1234 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1238 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1240 free(udev_device->devtype);
1241 udev_device->devtype = strdup(devtype);
1242 if (udev_device->devtype == NULL)
1244 udev_device->devtype_set = true;
1245 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1249 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1251 free(udev_device->devnode);
1252 udev_device->devnode = strdup(devnode);
1253 if (devnode == NULL)
1255 if (udev_device->devnode == NULL)
1257 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1261 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1263 struct udev_list_entry *list_entry;
1265 udev_device->devlinks_uptodate = false;
1266 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1267 if (list_entry == NULL)
1270 udev_list_entry_set_flags(list_entry, 1);
1274 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1276 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1278 udev_device->tags_uptodate = false;
1279 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
1284 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1286 udev_device->tags_uptodate = false;
1287 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1291 * udev_device_get_tags_list_entry:
1292 * @udev_device: udev device
1294 * Retrieve the list of tags attached to the udev device. The next
1295 * list entry can be retrieved with udev_list_entry_next(),
1296 * which returns #NULL if no more entries exist. The tag string
1297 * can be retrieved from the list entry by udev_list_get_name().
1299 * Returns: the first entry of the tag list
1301 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1303 if (udev_device == NULL)
1305 return udev_list_get_entry(&udev_device->tags_list);
1308 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1310 struct udev_list_entry *list_entry;
1312 if (!udev_device->info_loaded)
1313 udev_device_read_db(udev_device);
1314 list_entry = udev_device_get_tags_list_entry(udev_device);
1315 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1316 if (list_entry != NULL)
1321 #define ENVP_SIZE 128
1322 #define MONITOR_BUF_SIZE 4096
1323 static int update_envp_monitor_buf(struct udev_device *udev_device)
1325 struct udev_list_entry *list_entry;
1330 /* monitor buffer of property strings */
1331 free(udev_device->monitor_buf);
1332 udev_device->monitor_buf_len = 0;
1333 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1334 if (udev_device->monitor_buf == NULL)
1337 /* envp array, strings will point into monitor buffer */
1338 if (udev_device->envp == NULL)
1339 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1340 if (udev_device->envp == NULL)
1344 s = udev_device->monitor_buf;
1345 l = MONITOR_BUF_SIZE;
1346 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1349 key = udev_list_entry_get_name(list_entry);
1350 /* skip private variables */
1354 /* add string to envp array */
1355 udev_device->envp[i++] = s;
1356 if (i+1 >= ENVP_SIZE)
1359 /* add property string to monitor buffer */
1360 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1363 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1367 udev_device->envp[i] = NULL;
1368 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1369 udev_device->envp_uptodate = true;
1370 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1371 i, udev_device->monitor_buf_len);
1375 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1377 if (!udev_device->envp_uptodate)
1378 if (update_envp_monitor_buf(udev_device) != 0)
1380 return udev_device->envp;
1383 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1385 if (!udev_device->envp_uptodate)
1386 if (update_envp_monitor_buf(udev_device) != 0)
1388 *buf = udev_device->monitor_buf;
1389 return udev_device->monitor_buf_len;
1392 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1394 free(udev_device->action);
1395 udev_device->action = strdup(action);
1396 if (udev_device->action == NULL)
1398 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1402 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1404 free(udev_device->driver);
1405 udev_device->driver = strdup(driver);
1406 if (udev_device->driver == NULL)
1408 udev_device->driver_set = true;
1409 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1413 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1415 return udev_device->devpath_old;
1418 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1423 free(udev_device->devpath_old);
1424 udev_device->devpath_old = strdup(devpath_old);
1425 if (udev_device->devpath_old == NULL)
1427 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1429 pos = strrchr(udev_device->devpath_old, '/');
1432 udev_device->sysname_old = strdup(&pos[1]);
1433 if (udev_device->sysname_old == NULL)
1436 /* some devices have '!' in their name, change that to '/' */
1438 while (udev_device->sysname_old[len] != '\0') {
1439 if (udev_device->sysname_old[len] == '!')
1440 udev_device->sysname_old[len] = '/';
1446 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1448 if (udev_device == NULL)
1450 return udev_device->sysname_old;
1453 const char *udev_device_get_knodename(struct udev_device *udev_device)
1455 return udev_device->knodename;
1458 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1460 free(udev_device->knodename);
1461 udev_device->knodename = strdup(knodename);
1462 if (udev_device->knodename == NULL)
1464 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1468 int udev_device_get_timeout(struct udev_device *udev_device)
1470 return udev_device->timeout;
1473 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1475 udev_device->timeout = timeout;
1478 int udev_device_get_event_timeout(struct udev_device *udev_device)
1480 if (!udev_device->info_loaded)
1481 udev_device_read_db(udev_device);
1482 return udev_device->event_timeout;
1485 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1487 udev_device->event_timeout = event_timeout;
1491 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1495 udev_device->seqnum = seqnum;
1496 snprintf(num, sizeof(num), "%llu", seqnum);
1497 udev_device_add_property(udev_device, "SEQNUM", num);
1501 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1505 udev_device->devnum = devnum;
1507 snprintf(num, sizeof(num), "%u", major(devnum));
1508 udev_device_add_property(udev_device, "MAJOR", num);
1509 snprintf(num, sizeof(num), "%u", minor(devnum));
1510 udev_device_add_property(udev_device, "MINOR", num);
1514 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1516 if (!udev_device->info_loaded)
1517 udev_device_read_db(udev_device);
1518 return udev_device->devlink_priority;
1521 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1523 udev_device->devlink_priority = prio;
1527 int udev_device_get_watch_handle(struct udev_device *udev_device)
1529 if (!udev_device->info_loaded)
1530 udev_device_read_db(udev_device);
1531 return udev_device->watch_handle;
1534 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1536 udev_device->watch_handle = handle;