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;
60 size_t monitor_buf_len;
61 struct udev_list_node devlinks_list;
62 struct udev_list_node properties_list;
63 struct udev_list_node sysattr_list;
64 struct udev_list_node available_sysattr_list;
65 struct udev_list_node tags_list;
66 unsigned long long int seqnum;
67 unsigned long long int usec_initialized;
79 bool devlinks_uptodate;
90 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
92 udev_device->envp_uptodate = false;
94 struct udev_list_entry *list_entry;
96 list_entry = udev_device_get_properties_list_entry(udev_device);
97 list_entry = udev_list_entry_get_by_name(list_entry, key);
98 if (list_entry != NULL)
99 udev_list_entry_delete(list_entry);
102 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
105 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
107 char name[UTIL_LINE_SIZE];
110 util_strscpy(name, sizeof(name), property);
111 val = strchr(name, '=');
118 return udev_device_add_property(udev_device, name, val);
122 * parse property string, and if needed, update internal values accordingly
124 * udev_device_add_property_from_string_parse_finish() needs to be
125 * called after adding properties, and its return value checked
127 * udev_device_set_info_loaded() needs to be set, to avoid trying
128 * to use a device without a DEVPATH set
130 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
132 if (strncmp(property, "DEVPATH=", 8) == 0) {
133 char path[UTIL_PATH_SIZE];
135 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
136 udev_device_set_syspath(udev_device, path);
137 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
138 udev_device_set_subsystem(udev_device, &property[10]);
139 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
140 udev_device_set_devtype(udev_device, &property[8]);
141 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
142 if (property[8] == '/')
143 udev_device_set_devnode(udev_device, &property[8]);
145 udev_device_set_knodename(udev_device, &property[8]);
146 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
147 char devlinks[UTIL_PATH_SIZE];
151 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
153 next = strchr(slink, ' ');
154 while (next != NULL) {
156 udev_device_add_devlink(udev_device, slink, 0);
158 next = strchr(slink, ' ');
160 if (slink[0] != '\0')
161 udev_device_add_devlink(udev_device, slink, 0);
162 } else if (strncmp(property, "TAGS=", 5) == 0) {
163 char tags[UTIL_PATH_SIZE];
166 util_strscpy(tags, sizeof(tags), &property[5]);
167 next = strchr(tags, ':');
170 while (next[0] != '\0') {
174 next = strchr(tag, ':');
179 udev_device_add_tag(udev_device, tag);
182 } else if (strncmp(property, "DRIVER=", 7) == 0) {
183 udev_device_set_driver(udev_device, &property[7]);
184 } else if (strncmp(property, "ACTION=", 7) == 0) {
185 udev_device_set_action(udev_device, &property[7]);
186 } else if (strncmp(property, "MAJOR=", 6) == 0) {
187 udev_device->maj = strtoull(&property[6], NULL, 10);
188 } else if (strncmp(property, "MINOR=", 6) == 0) {
189 udev_device->min = strtoull(&property[6], NULL, 10);
190 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
191 udev_device_set_devpath_old(udev_device, &property[12]);
192 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
193 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
194 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
195 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
196 } else if (strncmp(property, "IFINDEX=", 8) == 0) {
197 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
199 udev_device_add_property_from_string(udev_device, property);
203 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
205 if (udev_device->maj > 0)
206 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
207 udev_device->maj = 0;
208 udev_device->min = 0;
210 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
216 * udev_device_get_property_value:
217 * @udev_device: udev device
218 * @key: property name
220 * Returns: the value of a device property, or #NULL if there is no such property.
222 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
224 struct udev_list_entry *list_entry;
226 if (udev_device == NULL)
231 list_entry = udev_device_get_properties_list_entry(udev_device);
232 list_entry = udev_list_entry_get_by_name(list_entry, key);
233 return udev_list_entry_get_value(list_entry);
236 int udev_device_read_db(struct udev_device *udev_device)
239 char filename[UTIL_PATH_SIZE];
240 char line[UTIL_LINE_SIZE];
243 if (udev_device->db_loaded)
245 udev_device->db_loaded = true;
247 id = udev_device_get_id_filename(udev_device);
250 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/", id, NULL);
251 f = fopen(filename, "re");
253 info(udev_device->udev, "no db file to read %s: %m\n", filename);
256 udev_device->is_initialized = true;
258 while (fgets(line, sizeof(line), f)) {
261 struct udev_list_entry *entry;
270 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
271 udev_device_set_devnode(udev_device, filename);
274 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
275 udev_device_add_devlink(udev_device, filename, 0);
278 udev_device_set_devlink_priority(udev_device, atoi(val));
281 entry = udev_device_add_property_from_string(udev_device, val);
282 udev_list_entry_set_flags(entry, 1);
285 udev_device_add_tag(udev_device, val);
288 udev_device_set_watch_handle(udev_device, atoi(val));
291 udev_device_set_usec_initialized(udev_device, strtoull(val, NULL, 10));
297 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
301 int udev_device_read_uevent_file(struct udev_device *udev_device)
303 char filename[UTIL_PATH_SIZE];
305 char line[UTIL_LINE_SIZE];
309 if (udev_device->uevent_loaded)
312 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
313 f = fopen(filename, "re");
316 udev_device->uevent_loaded = true;
318 while (fgets(line, sizeof(line), f)) {
321 pos = strchr(line, '\n');
326 if (strncmp(line, "DEVTYPE=", 8) == 0)
327 udev_device_set_devtype(udev_device, &line[8]);
328 else if (strncmp(line, "MAJOR=", 6) == 0)
329 maj = strtoull(&line[6], NULL, 10);
330 else if (strncmp(line, "MINOR=", 6) == 0)
331 min = strtoull(&line[6], NULL, 10);
332 else if (strncmp(line, "IFINDEX=", 8) == 0)
333 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
334 else if (strncmp(line, "DEVNAME=", 8) == 0)
335 udev_device_set_knodename(udev_device, &line[8]);
337 udev_device_add_property_from_string(udev_device, line);
340 udev_device->devnum = makedev(maj, min);
345 void udev_device_set_info_loaded(struct udev_device *device)
347 device->info_loaded = true;
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_list_init(&udev_device->available_sysattr_list);
367 udev_list_init(&udev_device->tags_list);
368 udev_device->event_timeout = -1;
369 udev_device->watch_handle = -1;
370 /* copy global properties */
371 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
372 udev_device_add_property(udev_device,
373 udev_list_entry_get_name(list_entry),
374 udev_list_entry_get_value(list_entry));
375 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
380 * udev_device_new_from_syspath:
381 * @udev: udev library context
382 * @syspath: sys device path including sys directory
384 * Create new udev device, and fill in information from the sys
385 * device and the udev database entry. The syspath is the absolute
386 * path to the device, including the sys mount point.
388 * The initial refcount is 1, and needs to be decremented to
389 * release the resources of the udev device.
391 * Returns: a new udev device, or #NULL, if it does not exist
393 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
397 char path[UTIL_PATH_SIZE];
400 struct udev_device *udev_device;
407 /* path starts in sys */
408 len = strlen(udev_get_sys_path(udev));
409 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
410 info(udev, "not in sys :%s\n", syspath);
414 /* path is not a root directory */
415 subdir = &syspath[len+1];
416 pos = strrchr(subdir, '/');
417 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
418 dbg(udev, "not a subdir :%s\n", syspath);
422 /* resolve possible symlink to real path */
423 util_strscpy(path, sizeof(path), syspath);
424 util_resolve_sys_link(udev, path, sizeof(path));
426 if (strncmp(&path[len], "/devices/", 9) == 0) {
427 char file[UTIL_PATH_SIZE];
429 /* all "devices" require a "uevent" file */
430 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
431 if (stat(file, &statbuf) != 0) {
432 dbg(udev, "not a device: %s\n", syspath);
436 /* everything else just needs to be a directory */
437 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
438 dbg(udev, "directory not found: %s\n", syspath);
443 udev_device = udev_device_new(udev);
444 if (udev_device == NULL)
447 udev_device_set_syspath(udev_device, path);
448 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
454 * udev_device_new_from_devnum:
455 * @udev: udev library context
456 * @type: char or block device
457 * @devnum: device major/minor number
459 * Create new udev device, and fill in information from the sys
460 * device and the udev database entry. The device is looked-up
461 * by its major/minor number and type. Character and block device
462 * numbers are not unique across the two types.
464 * The initial refcount is 1, and needs to be decremented to
465 * release the resources of the udev device.
467 * Returns: a new udev device, or #NULL, if it does not exist
469 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
471 char path[UTIL_PATH_SIZE];
472 const char *type_str;
476 else if (type == 'c')
481 /* use /sys/dev/{block,char}/<maj>:<min> link */
482 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
483 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
484 return udev_device_new_from_syspath(udev, path);
487 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
491 char subsys[UTIL_PATH_SIZE];
497 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
499 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
501 util_strscpy(subsys, sizeof(subsys), &id[1]);
502 sysname = strchr(subsys, ':');
506 sysname = &sysname[1];
507 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
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->available_sysattr_list);
792 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
793 free(udev_device->action);
794 free(udev_device->driver);
795 free(udev_device->devpath_old);
796 free(udev_device->sysname_old);
797 free(udev_device->knodename);
798 free(udev_device->id_filename);
799 free(udev_device->envp);
800 free(udev_device->monitor_buf);
801 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
806 * udev_device_get_devpath:
807 * @udev_device: udev device
809 * Retrieve the kernel devpath value of the udev device. The path
810 * does not contain the sys mount point, and starts with a '/'.
812 * Returns: the devpath of the udev device
814 const char *udev_device_get_devpath(struct udev_device *udev_device)
816 if (udev_device == NULL)
818 return udev_device->devpath;
822 * udev_device_get_syspath:
823 * @udev_device: udev device
825 * Retrieve the sys path of the udev device. The path is an
826 * absolute path and starts with the sys mount point.
828 * Returns: the sys path of the udev device
830 const char *udev_device_get_syspath(struct udev_device *udev_device)
832 if (udev_device == NULL)
834 return udev_device->syspath;
838 * udev_device_get_sysname:
839 * @udev_device: udev device
841 * Returns: the sys name of the device device
843 const char *udev_device_get_sysname(struct udev_device *udev_device)
845 if (udev_device == NULL)
847 return udev_device->sysname;
851 * udev_device_get_sysnum:
852 * @udev_device: udev device
854 * Returns: the trailing number of of the device name
856 const char *udev_device_get_sysnum(struct udev_device *udev_device)
858 if (udev_device == NULL)
860 return udev_device->sysnum;
864 * udev_device_get_devnode:
865 * @udev_device: udev device
867 * Retrieve the device node file name belonging to the udev device.
868 * The path is an absolute path, and starts with the device directory.
870 * Returns: the device node file name of the udev device, or #NULL if no device node exists
872 const char *udev_device_get_devnode(struct udev_device *udev_device)
874 if (udev_device == NULL)
876 if (!udev_device->info_loaded) {
877 udev_device_read_uevent_file(udev_device);
878 udev_device_read_db(udev_device);
881 /* we might get called before we handled an event and have a db, use the kernel-provided name */
882 if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
883 char filename[UTIL_NAME_SIZE];
885 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/",
886 udev_device_get_knodename(udev_device), NULL);
887 udev_device_set_devnode(udev_device, filename);
888 return udev_device->devnode;
891 return udev_device->devnode;
895 * udev_device_get_subsystem:
896 * @udev_device: udev device
898 * Retrieve the subsystem string of the udev device. The string does not
901 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
903 const char *udev_device_get_subsystem(struct udev_device *udev_device)
905 char subsystem[UTIL_NAME_SIZE];
907 if (udev_device == NULL)
909 if (!udev_device->subsystem_set) {
910 udev_device->subsystem_set = true;
911 /* read "subsystem" link */
912 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
913 udev_device_set_subsystem(udev_device, subsystem);
914 return udev_device->subsystem;
917 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
918 udev_device_set_subsystem(udev_device, "module");
919 return udev_device->subsystem;
921 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
922 udev_device_set_subsystem(udev_device, "drivers");
923 return udev_device->subsystem;
925 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
926 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
927 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
928 udev_device_set_subsystem(udev_device, "subsystem");
929 return udev_device->subsystem;
932 return udev_device->subsystem;
936 * udev_device_get_devtype:
937 * @udev_device: udev device
939 * Retrieve the devtype string of the udev device.
941 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
943 const char *udev_device_get_devtype(struct udev_device *udev_device)
945 if (udev_device == NULL)
947 if (!udev_device->devtype_set) {
948 udev_device->devtype_set = true;
949 udev_device_read_uevent_file(udev_device);
951 return udev_device->devtype;
955 * udev_device_get_devlinks_list_entry:
956 * @udev_device: udev device
958 * Retrieve the list of device links pointing to the device file of
959 * the udev device. The next list entry can be retrieved with
960 * udev_list_entry_next(), which returns #NULL if no more entries exist.
961 * The devlink path can be retrieved from the list entry by
962 * udev_list_entry_get_name(). The path is an absolute path, and starts with
963 * the device directory.
965 * Returns: the first entry of the device node link list
967 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
969 if (udev_device == NULL)
971 if (!udev_device->info_loaded)
972 udev_device_read_db(udev_device);
973 return udev_list_get_entry(&udev_device->devlinks_list);
976 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
978 udev_device->devlinks_uptodate = false;
979 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
983 * udev_device_get_properties_list_entry:
984 * @udev_device: udev device
986 * Retrieve the list of key/value device properties of the udev
987 * device. The next list entry can be retrieved with udev_list_entry_next(),
988 * which returns #NULL if no more entries exist. The property name
989 * can be retrieved from the list entry by udev_list_get_name(),
990 * the property value by udev_list_get_value().
992 * Returns: the first entry of the property list
994 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
996 if (udev_device == NULL)
998 if (!udev_device->info_loaded) {
999 udev_device_read_uevent_file(udev_device);
1000 udev_device_read_db(udev_device);
1002 if (!udev_device->devlinks_uptodate) {
1003 char symlinks[UTIL_PATH_SIZE];
1004 struct udev_list_entry *list_entry;
1006 udev_device->devlinks_uptodate = true;
1007 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1008 if (list_entry != NULL) {
1013 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1014 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1015 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1016 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1019 if (!udev_device->tags_uptodate) {
1020 udev_device->tags_uptodate = true;
1021 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1022 char tags[UTIL_PATH_SIZE];
1023 struct udev_list_entry *list_entry;
1028 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1029 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1030 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1031 udev_device_add_property(udev_device, "TAGS", tags);
1034 return udev_list_get_entry(&udev_device->properties_list);
1038 * udev_device_get_driver:
1039 * @udev_device: udev device
1041 * Returns: the driver string, or #NULL if there is no driver attached.
1043 const char *udev_device_get_driver(struct udev_device *udev_device)
1045 char driver[UTIL_NAME_SIZE];
1047 if (udev_device == NULL)
1049 if (!udev_device->driver_set) {
1050 udev_device->driver_set = true;
1051 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1052 udev_device->driver = strdup(driver);
1054 return udev_device->driver;
1058 * udev_device_get_devnum:
1059 * @udev_device: udev device
1061 * Returns: the device major/minor number.
1063 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1065 if (udev_device == NULL)
1066 return makedev(0, 0);
1067 if (!udev_device->info_loaded)
1068 udev_device_read_uevent_file(udev_device);
1069 return udev_device->devnum;
1073 * udev_device_get_action:
1074 * @udev_device: udev device
1076 * This is only valid if the device was received through a monitor. Devices read from
1077 * sys do not have an action string. Usual actions are: add, remove, change, online,
1080 * Returns: the kernel action value, or #NULL if there is no action value available.
1082 const char *udev_device_get_action(struct udev_device *udev_device)
1084 if (udev_device == NULL)
1086 return udev_device->action;
1090 * udev_device_get_devnum:
1091 * @udev_device: udev device
1093 * This is only valid if the device was received through a monitor. Devices read from
1094 * sys do not have a sequence number.
1096 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1098 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1100 if (udev_device == NULL)
1102 return udev_device->seqnum;
1106 * udev_device_get_usec_since_initialized:
1107 * @udev_device: udev device
1109 * Return the number of microseconds passed since udev set up the
1110 * device for the first time.
1112 * This is only implemented for devices with need to store properties
1113 * in the udev database. All other devices return 0 here.
1115 * Returns: the number of microseconds since the device was first seen.
1117 unsigned long long int udev_device_get_usec_since_initialized(struct udev_device *udev_device)
1119 unsigned long long now;
1121 if (udev_device == NULL)
1123 if (!udev_device->info_loaded)
1124 udev_device_read_db(udev_device);
1125 if (udev_device->usec_initialized == 0)
1127 now = usec_monotonic();
1130 return now - udev_device->usec_initialized;
1133 unsigned long long udev_device_get_usec_initialized(struct udev_device *udev_device)
1135 return udev_device->usec_initialized;
1138 void udev_device_set_usec_initialized(struct udev_device *udev_device, unsigned long long usec_initialized)
1140 udev_device->usec_initialized = usec_initialized;
1144 * udev_device_get_sysattr_value:
1145 * @udev_device: udev device
1146 * @sysattr: attribute name
1148 * The retrieved value is cached in the device. Repeated calls will return the same
1149 * value and not open the attribute again.
1151 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1153 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1155 struct udev_list_entry *list_entry;
1156 char path[UTIL_PATH_SIZE];
1158 struct stat statbuf;
1161 const char *val = NULL;
1163 if (udev_device == NULL)
1165 if (sysattr == NULL)
1168 /* look for possibly already cached result */
1169 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1170 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1171 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1172 sysattr, udev_list_entry_get_value(list_entry));
1173 return udev_list_entry_get_value(list_entry);
1177 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1178 if (lstat(path, &statbuf) != 0) {
1179 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1180 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1184 if (S_ISLNK(statbuf.st_mode)) {
1185 char target[UTIL_NAME_SIZE];
1189 /* some core links return the last element of the target path */
1190 if (strcmp(sysattr, "driver") != 0 &&
1191 strcmp(sysattr, "subsystem") != 0 &&
1192 strcmp(sysattr, "module") != 0)
1195 len = readlink(path, target, sizeof(target));
1196 if (len <= 0 || len == sizeof(target))
1200 pos = strrchr(target, '/');
1203 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1204 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1205 val = udev_list_entry_get_value(list_entry);
1211 /* skip directories */
1212 if (S_ISDIR(statbuf.st_mode))
1215 /* skip non-readable files */
1216 if ((statbuf.st_mode & S_IRUSR) == 0)
1219 /* read attribute value */
1220 fd = open(path, O_RDONLY|O_CLOEXEC);
1222 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1225 size = read(fd, value, sizeof(value));
1229 if (size == sizeof(value))
1232 /* got a valid value, store it in cache and return it */
1234 util_remove_trailing_chars(value, '\n');
1235 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1236 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1237 val = udev_list_entry_get_value(list_entry);
1242 static int udev_device_cache_sysattrs(struct udev_device *udev_device)
1244 struct dirent *entry;
1248 if (udev_device == NULL)
1250 /* caching already done? */
1251 if (udev_device->sysattrs_cached)
1254 dir = opendir(udev_device_get_syspath(udev_device));
1256 dbg(udev_device->udev, "sysfs dir '%s' can not be opened\n",
1257 udev_device_get_syspath(udev_device));
1261 while (NULL != (entry = readdir(dir))) {
1262 char path[UTIL_PATH_SIZE];
1263 struct stat statbuf;
1265 /* only handle symlinks and regular files */
1266 if (DT_LNK != entry->d_type && DT_REG != entry->d_type)
1269 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device),
1270 "/", entry->d_name, NULL);
1271 if (0 != lstat(path, &statbuf))
1274 if (0 == (statbuf.st_mode & S_IRUSR))
1277 if (DT_LNK == entry->d_type) {
1278 if (strcmp(entry->d_name, "driver") != 0 &&
1279 strcmp(entry->d_name, "subsystem") != 0 &&
1280 strcmp(entry->d_name, "module") != 0)
1283 udev_list_entry_add(udev_device->udev,
1284 &udev_device->available_sysattr_list, entry->d_name,
1285 DT_LNK == entry->d_type ? "s" : "r", 0, 0);
1289 dbg(udev_device->udev, "found %d sysattrs for '%s'\n", num,
1290 udev_device_get_syspath(udev_device));
1291 udev_device->sysattrs_cached = true;
1297 * udev_device_get_sysattr_list_entry:
1298 * @udev_device: udev device
1300 * Retrieve the list of available sysattrs, with value being empty;
1301 * This is to be able to read all available sysfs attributes for a particular
1302 * device without the necessity to access sysfs from outside libudev.
1304 * Returns: the first entry of the property list
1306 struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1308 /* perform initial caching of sysattr list */
1309 if (!udev_device->sysattrs_cached) {
1311 ret = udev_device_cache_sysattrs(udev_device);
1316 return udev_list_get_entry(&udev_device->available_sysattr_list);
1319 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1324 free(udev_device->syspath);
1325 udev_device->syspath = strdup(syspath);
1326 if (udev_device->syspath == NULL)
1328 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1329 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1331 pos = strrchr(udev_device->syspath, '/');
1334 udev_device->sysname = strdup(&pos[1]);
1335 if (udev_device->sysname == NULL)
1338 /* some devices have '!' in their name, change that to '/' */
1340 while (udev_device->sysname[len] != '\0') {
1341 if (udev_device->sysname[len] == '!')
1342 udev_device->sysname[len] = '/';
1346 /* trailing number */
1347 while (len > 0 && isdigit(udev_device->sysname[--len]))
1348 udev_device->sysnum = &udev_device->sysname[len];
1350 /* sysname is completely numeric */
1352 udev_device->sysnum = NULL;
1357 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1359 free(udev_device->subsystem);
1360 udev_device->subsystem = strdup(subsystem);
1361 if (udev_device->subsystem == NULL)
1363 udev_device->subsystem_set = true;
1364 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1368 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1370 free(udev_device->devtype);
1371 udev_device->devtype = strdup(devtype);
1372 if (udev_device->devtype == NULL)
1374 udev_device->devtype_set = true;
1375 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1379 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1381 free(udev_device->devnode);
1382 udev_device->devnode = strdup(devnode);
1383 if (udev_device->devnode == NULL)
1385 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1389 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1391 struct udev_list_entry *list_entry;
1393 udev_device->devlinks_uptodate = false;
1394 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1395 if (list_entry == NULL)
1398 udev_list_entry_set_flags(list_entry, 1);
1402 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1404 if (udev_device->id_filename == NULL) {
1405 if (udev_device_get_subsystem(udev_device) == NULL)
1408 if (major(udev_device_get_devnum(udev_device)) > 0) {
1409 /* use dev_t -- b259:131072, c254:0 */
1410 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1411 strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1412 major(udev_device_get_devnum(udev_device)),
1413 minor(udev_device_get_devnum(udev_device))) < 0)
1414 udev_device->id_filename = NULL;
1415 } else if (udev_device_get_ifindex(udev_device) > 0) {
1416 /* use netdev ifindex -- n3 */
1417 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1418 udev_device->id_filename = NULL;
1421 * use $subsys:$syname -- pci:0000:00:1f.2
1422 * sysname() has '!' translated, get it from devpath
1424 const char *sysname;
1425 sysname = strrchr(udev_device->devpath, '/');
1426 if (sysname == NULL)
1428 sysname = &sysname[1];
1429 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1430 udev_device->id_filename = NULL;
1433 return udev_device->id_filename;
1437 * udev_device_get_is_initialized:
1438 * @udev_device: udev device
1440 * Check if udev has already handled the device and has set up
1441 * device node permissions and context, or has renamed a network
1444 * This is only implemented for devices with a device node
1445 * or network interfaces. All other devices return 1 here.
1447 * Returns: 1 if the device is set up. 0 otherwise.
1449 int udev_device_get_is_initialized(struct udev_device *udev_device)
1451 if (!udev_device->info_loaded)
1452 udev_device_read_db(udev_device);
1453 return udev_device->is_initialized;
1456 void udev_device_set_is_initialized(struct udev_device *udev_device)
1458 udev_device->is_initialized = true;
1461 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1463 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1465 udev_device->tags_uptodate = false;
1466 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
1471 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1473 udev_device->tags_uptodate = false;
1474 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1478 * udev_device_get_tags_list_entry:
1479 * @udev_device: udev device
1481 * Retrieve the list of tags attached to the udev device. The next
1482 * list entry can be retrieved with udev_list_entry_next(),
1483 * which returns #NULL if no more entries exist. The tag string
1484 * can be retrieved from the list entry by udev_list_get_name().
1486 * Returns: the first entry of the tag list
1488 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1490 if (udev_device == NULL)
1492 return udev_list_get_entry(&udev_device->tags_list);
1495 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1497 struct udev_list_entry *list_entry;
1499 if (!udev_device->info_loaded)
1500 udev_device_read_db(udev_device);
1501 list_entry = udev_device_get_tags_list_entry(udev_device);
1502 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1503 if (list_entry != NULL)
1508 #define ENVP_SIZE 128
1509 #define MONITOR_BUF_SIZE 4096
1510 static int update_envp_monitor_buf(struct udev_device *udev_device)
1512 struct udev_list_entry *list_entry;
1517 /* monitor buffer of property strings */
1518 free(udev_device->monitor_buf);
1519 udev_device->monitor_buf_len = 0;
1520 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1521 if (udev_device->monitor_buf == NULL)
1524 /* envp array, strings will point into monitor buffer */
1525 if (udev_device->envp == NULL)
1526 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1527 if (udev_device->envp == NULL)
1531 s = udev_device->monitor_buf;
1532 l = MONITOR_BUF_SIZE;
1533 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1536 key = udev_list_entry_get_name(list_entry);
1537 /* skip private variables */
1541 /* add string to envp array */
1542 udev_device->envp[i++] = s;
1543 if (i+1 >= ENVP_SIZE)
1546 /* add property string to monitor buffer */
1547 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1550 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1554 udev_device->envp[i] = NULL;
1555 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1556 udev_device->envp_uptodate = true;
1557 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1558 i, udev_device->monitor_buf_len);
1562 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1564 if (!udev_device->envp_uptodate)
1565 if (update_envp_monitor_buf(udev_device) != 0)
1567 return udev_device->envp;
1570 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1572 if (!udev_device->envp_uptodate)
1573 if (update_envp_monitor_buf(udev_device) != 0)
1575 *buf = udev_device->monitor_buf;
1576 return udev_device->monitor_buf_len;
1579 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1581 free(udev_device->action);
1582 udev_device->action = strdup(action);
1583 if (udev_device->action == NULL)
1585 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1589 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1591 free(udev_device->driver);
1592 udev_device->driver = strdup(driver);
1593 if (udev_device->driver == NULL)
1595 udev_device->driver_set = true;
1596 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1600 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1602 return udev_device->devpath_old;
1605 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1610 free(udev_device->devpath_old);
1611 udev_device->devpath_old = strdup(devpath_old);
1612 if (udev_device->devpath_old == NULL)
1614 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1616 pos = strrchr(udev_device->devpath_old, '/');
1619 udev_device->sysname_old = strdup(&pos[1]);
1620 if (udev_device->sysname_old == NULL)
1623 /* some devices have '!' in their name, change that to '/' */
1625 while (udev_device->sysname_old[len] != '\0') {
1626 if (udev_device->sysname_old[len] == '!')
1627 udev_device->sysname_old[len] = '/';
1633 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1635 if (udev_device == NULL)
1637 return udev_device->sysname_old;
1640 const char *udev_device_get_knodename(struct udev_device *udev_device)
1642 return udev_device->knodename;
1645 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1647 free(udev_device->knodename);
1648 udev_device->knodename = strdup(knodename);
1649 if (udev_device->knodename == NULL)
1651 /* do not overwrite the udev property with the kernel property */
1652 if (udev_device->devnode == NULL)
1653 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1657 int udev_device_get_timeout(struct udev_device *udev_device)
1659 return udev_device->timeout;
1662 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1664 udev_device->timeout = timeout;
1667 int udev_device_get_event_timeout(struct udev_device *udev_device)
1669 if (!udev_device->info_loaded)
1670 udev_device_read_db(udev_device);
1671 return udev_device->event_timeout;
1674 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1678 udev_device->event_timeout = event_timeout;
1679 snprintf(num, sizeof(num), "%u", event_timeout);
1680 udev_device_add_property(udev_device, "TIMEOUT", num);
1684 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1688 udev_device->seqnum = seqnum;
1689 snprintf(num, sizeof(num), "%llu", seqnum);
1690 udev_device_add_property(udev_device, "SEQNUM", num);
1694 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1698 udev_device->devnum = devnum;
1700 snprintf(num, sizeof(num), "%u", major(devnum));
1701 udev_device_add_property(udev_device, "MAJOR", num);
1702 snprintf(num, sizeof(num), "%u", minor(devnum));
1703 udev_device_add_property(udev_device, "MINOR", num);
1707 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1709 if (!udev_device->info_loaded)
1710 udev_device_read_db(udev_device);
1711 return udev_device->devlink_priority;
1714 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1716 udev_device->devlink_priority = prio;
1720 int udev_device_get_watch_handle(struct udev_device *udev_device)
1722 if (!udev_device->info_loaded)
1723 udev_device_read_db(udev_device);
1724 return udev_device->watch_handle;
1727 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1729 udev_device->watch_handle = handle;
1733 int udev_device_get_ifindex(struct udev_device *udev_device)
1735 if (!udev_device->info_loaded)
1736 udev_device_read_uevent_file(udev_device);
1737 return udev_device->ifindex;
1740 int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
1744 udev_device->ifindex = ifindex;
1745 snprintf(num, sizeof(num), "%u", ifindex);
1746 udev_device_add_property(udev_device, "IFINDEX", num);