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, "r");
284 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
287 while (fgets(line, sizeof(line), f)) {
298 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
299 udev_device_set_devnode(udev_device, filename);
302 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
303 udev_device_add_devlink(udev_device, filename, 0);
306 udev_device_set_devlink_priority(udev_device, atoi(val));
309 udev_device_set_event_timeout(udev_device, atoi(val));
312 udev_device_add_property_from_string(udev_device, val);
315 udev_device_add_tag(udev_device, val);
318 udev_device_set_watch_handle(udev_device, atoi(val));
324 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
325 udev_device->db_loaded = true;
329 int udev_device_read_uevent_file(struct udev_device *udev_device)
331 char filename[UTIL_PATH_SIZE];
333 char line[UTIL_LINE_SIZE];
337 if (udev_device->uevent_loaded)
340 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
341 f = fopen(filename, "r");
345 while (fgets(line, sizeof(line), f)) {
348 pos = strchr(line, '\n');
353 if (strncmp(line, "DEVTYPE=", 8) == 0)
354 udev_device_set_devtype(udev_device, &line[8]);
355 else if (strncmp(line, "MAJOR=", 6) == 0)
356 maj = strtoull(&line[6], NULL, 10);
357 else if (strncmp(line, "MINOR=", 6) == 0)
358 min = strtoull(&line[6], NULL, 10);
359 else if (strncmp(line, "DEVNAME=", 8) == 0)
360 udev_device_set_knodename(udev_device, &line[8]);
362 udev_device_add_property_from_string(udev_device, line);
365 udev_device->devnum = makedev(maj, min);
367 udev_device->uevent_loaded = true;
371 void udev_device_set_info_loaded(struct udev_device *device)
373 device->info_loaded = true;
376 struct udev_device *udev_device_new(struct udev *udev)
378 struct udev_device *udev_device;
379 struct udev_list_entry *list_entry;
384 udev_device = calloc(1, sizeof(struct udev_device));
385 if (udev_device == NULL)
387 udev_device->refcount = 1;
388 udev_device->udev = udev;
389 udev_list_init(&udev_device->devlinks_list);
390 udev_list_init(&udev_device->properties_list);
391 udev_list_init(&udev_device->sysattr_list);
392 udev_list_init(&udev_device->tags_list);
393 udev_device->event_timeout = -1;
394 udev_device->watch_handle = -1;
395 /* copy global properties */
396 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
397 udev_device_add_property(udev_device,
398 udev_list_entry_get_name(list_entry),
399 udev_list_entry_get_value(list_entry));
400 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
405 * udev_device_new_from_syspath:
406 * @udev: udev library context
407 * @syspath: sys device path including sys directory
409 * Create new udev device, and fill in information from the sys
410 * device and the udev database entry. The syspath is the absolute
411 * path to the device, including the sys mount point.
413 * The initial refcount is 1, and needs to be decremented to
414 * release the resources of the udev device.
416 * Returns: a new udev device, or #NULL, if it does not exist
418 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
422 char path[UTIL_PATH_SIZE];
425 struct udev_device *udev_device;
432 /* path starts in sys */
433 len = strlen(udev_get_sys_path(udev));
434 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
435 info(udev, "not in sys :%s\n", syspath);
439 /* path is not a root directory */
440 subdir = &syspath[len+1];
441 pos = strrchr(subdir, '/');
442 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
443 dbg(udev, "not a subdir :%s\n", syspath);
447 /* resolve possible symlink to real path */
448 util_strscpy(path, sizeof(path), syspath);
449 util_resolve_sys_link(udev, path, sizeof(path));
451 if (strncmp(&path[len], "/devices/", 9) == 0) {
452 char file[UTIL_PATH_SIZE];
454 /* all "devices" require a "uevent" file */
455 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
456 if (stat(file, &statbuf) != 0) {
457 dbg(udev, "not a device: %s\n", syspath);
461 /* everything else just needs to be a directory */
462 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
463 dbg(udev, "directory not found: %s\n", syspath);
468 udev_device = udev_device_new(udev);
469 if (udev_device == NULL)
472 udev_device_set_syspath(udev_device, path);
473 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
479 * udev_device_new_from_devnum:
480 * @udev: udev library context
481 * @type: char or block device
482 * @devnum: device major/minor number
484 * Create new udev device, and fill in information from the sys
485 * device and the udev database entry. The device is looked-up
486 * by its major/minor number and type. Character and block device
487 * numbers are not unique across the two types.
489 * The initial refcount is 1, and needs to be decremented to
490 * release the resources of the udev device.
492 * Returns: a new udev device, or #NULL, if it does not exist
494 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
496 char path[UTIL_PATH_SIZE];
497 const char *type_str;
501 else if (type == 'c')
506 /* use /sys/dev/{block,char}/<maj>:<min> link */
507 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
508 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
509 return udev_device_new_from_syspath(udev, path);
513 * udev_device_new_from_subsystem_sysname:
514 * @udev: udev library context
515 * @subsystem: the subsystem of the device
516 * @sysname: the name of the device
518 * Create new udev device, and fill in information from the sys device
519 * and the udev database entry. The device is looked up by the subsystem
520 * and name string of the device, like "mem" / "zero", or "block" / "sda".
522 * The initial refcount is 1, and needs to be decremented to
523 * release the resources of the udev device.
525 * Returns: a new udev device, or #NULL, if it does not exist
527 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
529 char path_full[UTIL_PATH_SIZE];
535 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
537 if (strcmp(subsystem, "subsystem") == 0) {
538 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
539 if (stat(path_full, &statbuf) == 0)
542 util_strscpyl(path, l, "/bus/", sysname, NULL);
543 if (stat(path_full, &statbuf) == 0)
546 util_strscpyl(path, l, "/class/", sysname, NULL);
547 if (stat(path_full, &statbuf) == 0)
552 if (strcmp(subsystem, "module") == 0) {
553 util_strscpyl(path, l, "/module/", sysname, NULL);
554 if (stat(path_full, &statbuf) == 0)
559 if (strcmp(subsystem, "drivers") == 0) {
560 char subsys[UTIL_NAME_SIZE];
563 util_strscpy(subsys, sizeof(subsys), sysname);
564 driver = strchr(subsys, ':');
565 if (driver != NULL) {
569 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
570 if (stat(path_full, &statbuf) == 0)
573 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
574 if (stat(path_full, &statbuf) == 0)
580 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
581 if (stat(path_full, &statbuf) == 0)
584 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
585 if (stat(path_full, &statbuf) == 0)
588 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
589 if (stat(path_full, &statbuf) == 0)
594 return udev_device_new_from_syspath(udev, path_full);
598 * udev_device_new_from_environment
599 * @udev: udev library context
601 * Create new udev device, and fill in information from the
602 * current process environment. This only works reliable if
603 * the process is called from a udev rule. It is usually used
604 * for tools executed from IMPORT= rules.
606 * The initial refcount is 1, and needs to be decremented to
607 * release the resources of the udev device.
609 * Returns: a new udev device, or #NULL, if it does not exist
611 struct udev_device *udev_device_new_from_environment(struct udev *udev)
614 struct udev_device *udev_device;
616 udev_device = udev_device_new(udev);
617 if (udev_device == NULL)
619 udev_device_set_info_loaded(udev_device);
621 for (i = 0; environ[i] != NULL; i++)
622 udev_device_add_property_from_string_parse(udev_device, environ[i]);
624 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
625 info(udev, "missing values, invalid device\n");
626 udev_device_unref(udev_device);
633 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
635 struct udev_device *udev_device_parent = NULL;
636 char path[UTIL_PATH_SIZE];
639 util_strscpy(path, sizeof(path), udev_device->syspath);
640 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
644 pos = strrchr(subdir, '/');
645 if (pos == NULL || pos < &subdir[2])
648 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
649 if (udev_device_parent != NULL)
650 return udev_device_parent;
656 * udev_device_get_parent:
657 * @udev_device: the device to start searching from
659 * Find the next parent device, and fill in information from the sys
660 * device and the udev database entry.
662 * The returned the device is not referenced. It is attached to the
663 * child device, and will be cleaned up when the child device
666 * It is not necessarily just the upper level directory, empty or not
667 * recognized sys directories are ignored.
669 * It can be called as many times as needed, without caring about
672 * Returns: a new udev device, or #NULL, if it no parent exist.
674 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
676 if (udev_device == NULL)
678 if (!udev_device->parent_set) {
679 udev_device->parent_set = true;
680 udev_device->parent_device = device_new_from_parent(udev_device);
682 if (udev_device->parent_device != NULL)
683 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
684 return udev_device->parent_device;
688 * udev_device_get_parent_with_subsystem_devtype:
689 * @udev_device: udev device to start searching from
690 * @subsystem: the subsystem of the device
691 * @devtype: the type (DEVTYPE) of the device
693 * Find the next parent device, with a matching subsystem and devtype
694 * value, and fill in information from the sys device and the udev
697 * If devtype is #NULL, only subsystem is checked, and any devtype will
700 * The returned the device is not referenced. It is attached to the
701 * child device, and will be cleaned up when the child device
704 * It can be called as many times as needed, without caring about
707 * Returns: a new udev device, or #NULL if no matching parent exists.
709 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
711 struct udev_device *parent;
713 if (subsystem == NULL)
716 parent = udev_device_get_parent(udev_device);
717 while (parent != NULL) {
718 const char *parent_subsystem;
719 const char *parent_devtype;
721 parent_subsystem = udev_device_get_subsystem(parent);
722 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
725 parent_devtype = udev_device_get_devtype(parent);
726 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
729 parent = udev_device_get_parent(parent);
735 * udev_device_get_udev:
736 * @udev_device: udev device
738 * Retrieve the udev library context the device was created with.
740 * Returns: the udev library context
742 struct udev *udev_device_get_udev(struct udev_device *udev_device)
744 if (udev_device == NULL)
746 return udev_device->udev;
751 * @udev_device: udev device
753 * Take a reference of a udev device.
755 * Returns: the passed udev device
757 struct udev_device *udev_device_ref(struct udev_device *udev_device)
759 if (udev_device == NULL)
761 udev_device->refcount++;
767 * @udev_device: udev device
769 * Drop a reference of a udev device. If the refcount reaches zero,
770 * the resources of the device will be released.
773 void udev_device_unref(struct udev_device *udev_device)
775 if (udev_device == NULL)
777 udev_device->refcount--;
778 if (udev_device->refcount > 0)
780 if (udev_device->parent_device != NULL)
781 udev_device_unref(udev_device->parent_device);
782 free(udev_device->syspath);
783 free(udev_device->sysname);
784 free(udev_device->devnode);
785 free(udev_device->subsystem);
786 free(udev_device->devtype);
787 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
788 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
789 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
790 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
791 free(udev_device->action);
792 free(udev_device->driver);
793 free(udev_device->devpath_old);
794 free(udev_device->sysname_old);
795 free(udev_device->knodename);
796 free(udev_device->envp);
797 free(udev_device->monitor_buf);
798 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
803 * udev_device_get_devpath:
804 * @udev_device: udev device
806 * Retrieve the kernel devpath value of the udev device. The path
807 * does not contain the sys mount point, and starts with a '/'.
809 * Returns: the devpath of the udev device
811 const char *udev_device_get_devpath(struct udev_device *udev_device)
813 if (udev_device == NULL)
815 return udev_device->devpath;
819 * udev_device_get_syspath:
820 * @udev_device: udev device
822 * Retrieve the sys path of the udev device. The path is an
823 * absolute path and starts with the sys mount point.
825 * Returns: the sys path of the udev device
827 const char *udev_device_get_syspath(struct udev_device *udev_device)
829 if (udev_device == NULL)
831 return udev_device->syspath;
835 * udev_device_get_sysname:
836 * @udev_device: udev device
838 * Returns: the sys name of the device device
840 const char *udev_device_get_sysname(struct udev_device *udev_device)
842 if (udev_device == NULL)
844 return udev_device->sysname;
848 * udev_device_get_sysnum:
849 * @udev_device: udev device
851 * Returns: the trailing number of of the device name
853 const char *udev_device_get_sysnum(struct udev_device *udev_device)
855 if (udev_device == NULL)
857 return udev_device->sysnum;
861 * udev_device_get_devnode:
862 * @udev_device: udev device
864 * Retrieve the device node file name belonging to the udev device.
865 * The path is an absolute path, and starts with the device directory.
867 * Returns: the device node file name of the udev device, or #NULL if no device node exists
869 const char *udev_device_get_devnode(struct udev_device *udev_device)
871 if (udev_device == NULL)
873 if (!udev_device->info_loaded)
874 udev_device_read_db(udev_device);
875 return udev_device->devnode;
879 * udev_device_get_subsystem:
880 * @udev_device: udev device
882 * Retrieve the subsystem string of the udev device. The string does not
885 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
887 const char *udev_device_get_subsystem(struct udev_device *udev_device)
889 char subsystem[UTIL_NAME_SIZE];
891 if (udev_device == NULL)
893 if (!udev_device->subsystem_set) {
894 udev_device->subsystem_set = true;
895 /* read "subsystem" link */
896 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
897 udev_device_set_subsystem(udev_device, subsystem);
898 return udev_device->subsystem;
901 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
902 udev_device_set_subsystem(udev_device, "module");
903 return udev_device->subsystem;
905 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
906 udev_device_set_subsystem(udev_device, "drivers");
907 return udev_device->subsystem;
909 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
910 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
911 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
912 udev_device_set_subsystem(udev_device, "subsystem");
913 return udev_device->subsystem;
916 return udev_device->subsystem;
920 * udev_device_get_devtype:
921 * @udev_device: udev device
923 * Retrieve the devtype string of the udev device.
925 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
927 const char *udev_device_get_devtype(struct udev_device *udev_device)
929 if (udev_device == NULL)
931 if (!udev_device->devtype_set) {
932 udev_device->devtype_set = true;
933 udev_device_read_uevent_file(udev_device);
935 return udev_device->devtype;
939 * udev_device_get_devlinks_list_entry:
940 * @udev_device: udev device
942 * Retrieve the list of device links pointing to the device file of
943 * the udev device. The next list entry can be retrieved with
944 * udev_list_entry_next(), which returns #NULL if no more entries exist.
945 * The devlink path can be retrieved from the list entry by
946 * udev_list_entry_get_name(). The path is an absolute path, and starts with
947 * the device directory.
949 * Returns: the first entry of the device node link list
951 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
953 if (udev_device == NULL)
955 if (!udev_device->info_loaded)
956 udev_device_read_db(udev_device);
957 return udev_list_get_entry(&udev_device->devlinks_list);
960 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
962 udev_device->devlinks_uptodate = false;
963 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
967 * udev_device_get_properties_list_entry:
968 * @udev_device: udev device
970 * Retrieve the list of key/value device properties of the udev
971 * device. The next list entry can be retrieved with udev_list_entry_next(),
972 * which returns #NULL if no more entries exist. The property name
973 * can be retrieved from the list entry by udev_list_get_name(),
974 * the property value by udev_list_get_value().
976 * Returns: the first entry of the property list
978 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
980 if (udev_device == NULL)
982 if (!udev_device->info_loaded) {
983 udev_device_read_uevent_file(udev_device);
984 udev_device_read_db(udev_device);
986 if (!udev_device->devlinks_uptodate) {
987 char symlinks[UTIL_PATH_SIZE];
988 struct udev_list_entry *list_entry;
990 udev_device->devlinks_uptodate = true;
991 list_entry = udev_device_get_devlinks_list_entry(udev_device);
992 if (list_entry != NULL) {
997 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
998 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
999 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1000 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1003 if (!udev_device->tags_uptodate) {
1004 udev_device->tags_uptodate = true;
1005 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1006 char tags[UTIL_PATH_SIZE];
1007 struct udev_list_entry *list_entry;
1012 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1013 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1014 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1015 udev_device_add_property(udev_device, "TAGS", tags);
1018 return udev_list_get_entry(&udev_device->properties_list);
1022 * udev_device_get_driver:
1023 * @udev_device: udev device
1025 * Returns: the driver string, or #NULL if there is no driver attached.
1027 const char *udev_device_get_driver(struct udev_device *udev_device)
1029 char driver[UTIL_NAME_SIZE];
1031 if (udev_device == NULL)
1033 if (!udev_device->driver_set) {
1034 udev_device->driver_set = true;
1035 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1036 udev_device->driver = strdup(driver);
1038 return udev_device->driver;
1042 * udev_device_get_devnum:
1043 * @udev_device: udev device
1045 * Returns: the device major/minor number.
1047 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1049 if (udev_device == NULL)
1050 return makedev(0, 0);
1051 if (!udev_device->info_loaded)
1052 udev_device_read_uevent_file(udev_device);
1053 return udev_device->devnum;
1057 * udev_device_get_action:
1058 * @udev_device: udev device
1060 * This is only valid if the device was received through a monitor. Devices read from
1061 * sys do not have an action string. Usual actions are: add, remove, change, online,
1064 * Returns: the kernel action value, or #NULL if there is no action value available.
1066 const char *udev_device_get_action(struct udev_device *udev_device)
1068 if (udev_device == NULL)
1070 return udev_device->action;
1074 * udev_device_get_devnum:
1075 * @udev_device: udev device
1077 * This is only valid if the device was received through a monitor. Devices read from
1078 * sys do not have a sequence number.
1080 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1082 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1084 if (udev_device == NULL)
1086 return udev_device->seqnum;
1090 * udev_device_get_sysattr_value:
1091 * @udev_device: udev device
1092 * @sysattr: attribute name
1094 * The retrieved value is cached in the device. Repeated calls will return the same
1095 * value and not open the attribute again.
1097 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1099 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1101 struct udev_list_entry *list_entry;
1102 char path[UTIL_PATH_SIZE];
1104 struct stat statbuf;
1107 const char *val = NULL;
1109 if (udev_device == NULL)
1111 if (sysattr == NULL)
1114 /* look for possibly already cached result */
1115 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1116 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1117 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1118 sysattr, udev_list_entry_get_value(list_entry));
1119 return udev_list_entry_get_value(list_entry);
1123 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1124 if (lstat(path, &statbuf) != 0) {
1125 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1126 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1130 if (S_ISLNK(statbuf.st_mode)) {
1131 char target[UTIL_NAME_SIZE];
1135 /* some core links return the last element of the target path */
1136 if (strcmp(sysattr, "driver") != 0 &&
1137 strcmp(sysattr, "subsystem") != 0 &&
1138 strcmp(sysattr, "module") != 0)
1141 len = readlink(path, target, sizeof(target));
1142 if (len <= 0 || len == sizeof(target))
1146 pos = strrchr(target, '/');
1149 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1150 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1151 val = udev_list_entry_get_value(list_entry);
1157 /* skip directories */
1158 if (S_ISDIR(statbuf.st_mode))
1161 /* skip non-readable files */
1162 if ((statbuf.st_mode & S_IRUSR) == 0)
1165 /* read attribute value */
1166 fd = open(path, O_RDONLY);
1168 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1171 size = read(fd, value, sizeof(value));
1175 if (size == sizeof(value))
1178 /* got a valid value, store it in cache and return it */
1180 util_remove_trailing_chars(value, '\n');
1181 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1182 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1183 val = udev_list_entry_get_value(list_entry);
1188 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1193 free(udev_device->syspath);
1194 udev_device->syspath = strdup(syspath);
1195 if (udev_device->syspath == NULL)
1197 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1198 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1200 pos = strrchr(udev_device->syspath, '/');
1203 udev_device->sysname = strdup(&pos[1]);
1204 if (udev_device->sysname == NULL)
1207 /* some devices have '!' in their name, change that to '/' */
1209 while (udev_device->sysname[len] != '\0') {
1210 if (udev_device->sysname[len] == '!')
1211 udev_device->sysname[len] = '/';
1215 /* trailing number */
1216 while (len > 0 && isdigit(udev_device->sysname[--len]))
1217 udev_device->sysnum = &udev_device->sysname[len];
1219 /* sysname is completely numeric */
1221 udev_device->sysnum = NULL;
1226 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1228 free(udev_device->subsystem);
1229 udev_device->subsystem = strdup(subsystem);
1230 if (udev_device->subsystem == NULL)
1232 udev_device->subsystem_set = true;
1233 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1237 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1239 free(udev_device->devtype);
1240 udev_device->devtype = strdup(devtype);
1241 if (udev_device->devtype == NULL)
1243 udev_device->devtype_set = true;
1244 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1248 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1250 free(udev_device->devnode);
1251 udev_device->devnode = strdup(devnode);
1252 if (devnode == NULL)
1254 if (udev_device->devnode == NULL)
1256 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1260 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1262 struct udev_list_entry *list_entry;
1264 udev_device->devlinks_uptodate = false;
1265 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1266 if (list_entry == NULL)
1269 udev_list_entry_set_flags(list_entry, 1);
1273 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1275 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1277 udev_device->tags_uptodate = false;
1278 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
1283 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1285 udev_device->tags_uptodate = false;
1286 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1289 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1291 return udev_list_get_entry(&udev_device->tags_list);
1294 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1296 struct udev_list_entry *list_entry;
1298 if (!udev_device->info_loaded)
1299 udev_device_read_db(udev_device);
1300 list_entry = udev_device_get_tags_list_entry(udev_device);
1301 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1302 if (list_entry != NULL)
1307 #define ENVP_SIZE 128
1308 #define MONITOR_BUF_SIZE 4096
1309 static int update_envp_monitor_buf(struct udev_device *udev_device)
1311 struct udev_list_entry *list_entry;
1316 /* monitor buffer of property strings */
1317 free(udev_device->monitor_buf);
1318 udev_device->monitor_buf_len = 0;
1319 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1320 if (udev_device->monitor_buf == NULL)
1323 /* envp array, strings will point into monitor buffer */
1324 if (udev_device->envp == NULL)
1325 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1326 if (udev_device->envp == NULL)
1330 s = udev_device->monitor_buf;
1331 l = MONITOR_BUF_SIZE;
1332 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1335 key = udev_list_entry_get_name(list_entry);
1336 /* skip private variables */
1340 /* add string to envp array */
1341 udev_device->envp[i++] = s;
1342 if (i+1 >= ENVP_SIZE)
1345 /* add property string to monitor buffer */
1346 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1349 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1353 udev_device->envp[i] = NULL;
1354 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1355 udev_device->envp_uptodate = true;
1356 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1357 i, udev_device->monitor_buf_len);
1361 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1363 if (!udev_device->envp_uptodate)
1364 if (update_envp_monitor_buf(udev_device) != 0)
1366 return udev_device->envp;
1369 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1371 if (!udev_device->envp_uptodate)
1372 if (update_envp_monitor_buf(udev_device) != 0)
1374 *buf = udev_device->monitor_buf;
1375 return udev_device->monitor_buf_len;
1378 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1380 free(udev_device->action);
1381 udev_device->action = strdup(action);
1382 if (udev_device->action == NULL)
1384 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1388 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1390 free(udev_device->driver);
1391 udev_device->driver = strdup(driver);
1392 if (udev_device->driver == NULL)
1394 udev_device->driver_set = true;
1395 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1399 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1401 return udev_device->devpath_old;
1404 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1409 free(udev_device->devpath_old);
1410 udev_device->devpath_old = strdup(devpath_old);
1411 if (udev_device->devpath_old == NULL)
1413 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1415 pos = strrchr(udev_device->devpath_old, '/');
1418 udev_device->sysname_old = strdup(&pos[1]);
1419 if (udev_device->sysname_old == NULL)
1422 /* some devices have '!' in their name, change that to '/' */
1424 while (udev_device->sysname_old[len] != '\0') {
1425 if (udev_device->sysname_old[len] == '!')
1426 udev_device->sysname_old[len] = '/';
1432 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1434 if (udev_device == NULL)
1436 return udev_device->sysname_old;
1439 const char *udev_device_get_knodename(struct udev_device *udev_device)
1441 return udev_device->knodename;
1444 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1446 free(udev_device->knodename);
1447 udev_device->knodename = strdup(knodename);
1448 if (udev_device->knodename == NULL)
1450 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1454 int udev_device_get_timeout(struct udev_device *udev_device)
1456 return udev_device->timeout;
1459 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1461 udev_device->timeout = timeout;
1464 int udev_device_get_event_timeout(struct udev_device *udev_device)
1466 if (!udev_device->info_loaded)
1467 udev_device_read_db(udev_device);
1468 return udev_device->event_timeout;
1471 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1473 udev_device->event_timeout = event_timeout;
1477 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1481 udev_device->seqnum = seqnum;
1482 snprintf(num, sizeof(num), "%llu", seqnum);
1483 udev_device_add_property(udev_device, "SEQNUM", num);
1487 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1491 udev_device->devnum = devnum;
1493 snprintf(num, sizeof(num), "%u", major(devnum));
1494 udev_device_add_property(udev_device, "MAJOR", num);
1495 snprintf(num, sizeof(num), "%u", minor(devnum));
1496 udev_device_add_property(udev_device, "MINOR", num);
1500 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1502 if (!udev_device->info_loaded)
1503 udev_device_read_db(udev_device);
1504 return udev_device->devlink_priority;
1507 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1509 udev_device->devlink_priority = prio;
1513 int udev_device_get_watch_handle(struct udev_device *udev_device)
1515 if (!udev_device->info_loaded)
1516 udev_device_read_db(udev_device);
1517 return udev_device->watch_handle;
1520 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1522 udev_device->watch_handle = handle;