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_value_list;
64 struct udev_list_node 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;
87 bool sysattr_list_read;
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_value_list);
366 udev_list_init(&udev_device->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_value_list);
791 udev_list_cleanup_entries(udev_device->udev, &udev_device->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_value_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_value_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_value_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_value_list, sysattr, value, 0, 0);
1237 val = udev_list_entry_get_value(list_entry);
1242 static int udev_device_sysattr_list_read(struct udev_device *udev_device)
1244 struct dirent *dent;
1248 if (udev_device == NULL)
1250 if (udev_device->sysattr_list_read)
1253 dir = opendir(udev_device_get_syspath(udev_device));
1255 dbg(udev_device->udev, "sysfs dir '%s' can not be opened\n",
1256 udev_device_get_syspath(udev_device));
1260 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
1261 char path[UTIL_PATH_SIZE];
1262 struct stat statbuf;
1264 /* only handle symlinks and regular files */
1265 if (dent->d_type != DT_LNK && dent->d_type != DT_REG)
1268 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", dent->d_name, NULL);
1269 if (lstat(path, &statbuf) != 0)
1271 if ((statbuf.st_mode & S_IRUSR) == 0)
1274 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list,
1275 dent->d_name, NULL, 0, 0);
1280 dbg(udev_device->udev, "found %d sysattrs for '%s'\n", num, udev_device_get_syspath(udev_device));
1281 udev_device->sysattr_list_read = true;
1287 * udev_device_get_sysattr_list_entry:
1288 * @udev_device: udev device
1290 * Retrieve the list of available sysattrs, with value being empty;
1291 * This just return all available sysfs attributes for a particular
1292 * device without reading their values.
1294 * Returns: the first entry of the property list
1296 struct udev_list_entry *udev_device_get_sysattr_list_entry(struct udev_device *udev_device)
1298 if (!udev_device->sysattr_list_read) {
1300 ret = udev_device_sysattr_list_read(udev_device);
1305 return udev_list_get_entry(&udev_device->sysattr_list);
1308 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1313 free(udev_device->syspath);
1314 udev_device->syspath = strdup(syspath);
1315 if (udev_device->syspath == NULL)
1317 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1318 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1320 pos = strrchr(udev_device->syspath, '/');
1323 udev_device->sysname = strdup(&pos[1]);
1324 if (udev_device->sysname == NULL)
1327 /* some devices have '!' in their name, change that to '/' */
1329 while (udev_device->sysname[len] != '\0') {
1330 if (udev_device->sysname[len] == '!')
1331 udev_device->sysname[len] = '/';
1335 /* trailing number */
1336 while (len > 0 && isdigit(udev_device->sysname[--len]))
1337 udev_device->sysnum = &udev_device->sysname[len];
1339 /* sysname is completely numeric */
1341 udev_device->sysnum = NULL;
1346 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1348 free(udev_device->subsystem);
1349 udev_device->subsystem = strdup(subsystem);
1350 if (udev_device->subsystem == NULL)
1352 udev_device->subsystem_set = true;
1353 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1357 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1359 free(udev_device->devtype);
1360 udev_device->devtype = strdup(devtype);
1361 if (udev_device->devtype == NULL)
1363 udev_device->devtype_set = true;
1364 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1368 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1370 free(udev_device->devnode);
1371 udev_device->devnode = strdup(devnode);
1372 if (udev_device->devnode == NULL)
1374 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1378 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1380 struct udev_list_entry *list_entry;
1382 udev_device->devlinks_uptodate = false;
1383 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1384 if (list_entry == NULL)
1387 udev_list_entry_set_flags(list_entry, 1);
1391 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1393 if (udev_device->id_filename == NULL) {
1394 if (udev_device_get_subsystem(udev_device) == NULL)
1397 if (major(udev_device_get_devnum(udev_device)) > 0) {
1398 /* use dev_t -- b259:131072, c254:0 */
1399 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1400 strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1401 major(udev_device_get_devnum(udev_device)),
1402 minor(udev_device_get_devnum(udev_device))) < 0)
1403 udev_device->id_filename = NULL;
1404 } else if (udev_device_get_ifindex(udev_device) > 0) {
1405 /* use netdev ifindex -- n3 */
1406 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1407 udev_device->id_filename = NULL;
1410 * use $subsys:$syname -- pci:0000:00:1f.2
1411 * sysname() has '!' translated, get it from devpath
1413 const char *sysname;
1414 sysname = strrchr(udev_device->devpath, '/');
1415 if (sysname == NULL)
1417 sysname = &sysname[1];
1418 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1419 udev_device->id_filename = NULL;
1422 return udev_device->id_filename;
1426 * udev_device_get_is_initialized:
1427 * @udev_device: udev device
1429 * Check if udev has already handled the device and has set up
1430 * device node permissions and context, or has renamed a network
1433 * This is only implemented for devices with a device node
1434 * or network interfaces. All other devices return 1 here.
1436 * Returns: 1 if the device is set up. 0 otherwise.
1438 int udev_device_get_is_initialized(struct udev_device *udev_device)
1440 if (!udev_device->info_loaded)
1441 udev_device_read_db(udev_device);
1442 return udev_device->is_initialized;
1445 void udev_device_set_is_initialized(struct udev_device *udev_device)
1447 udev_device->is_initialized = true;
1450 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1452 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1454 udev_device->tags_uptodate = false;
1455 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
1460 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1462 udev_device->tags_uptodate = false;
1463 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1467 * udev_device_get_tags_list_entry:
1468 * @udev_device: udev device
1470 * Retrieve the list of tags attached to the udev device. The next
1471 * list entry can be retrieved with udev_list_entry_next(),
1472 * which returns #NULL if no more entries exist. The tag string
1473 * can be retrieved from the list entry by udev_list_get_name().
1475 * Returns: the first entry of the tag list
1477 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1479 if (udev_device == NULL)
1481 return udev_list_get_entry(&udev_device->tags_list);
1484 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1486 struct udev_list_entry *list_entry;
1488 if (!udev_device->info_loaded)
1489 udev_device_read_db(udev_device);
1490 list_entry = udev_device_get_tags_list_entry(udev_device);
1491 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1492 if (list_entry != NULL)
1497 #define ENVP_SIZE 128
1498 #define MONITOR_BUF_SIZE 4096
1499 static int update_envp_monitor_buf(struct udev_device *udev_device)
1501 struct udev_list_entry *list_entry;
1506 /* monitor buffer of property strings */
1507 free(udev_device->monitor_buf);
1508 udev_device->monitor_buf_len = 0;
1509 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1510 if (udev_device->monitor_buf == NULL)
1513 /* envp array, strings will point into monitor buffer */
1514 if (udev_device->envp == NULL)
1515 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1516 if (udev_device->envp == NULL)
1520 s = udev_device->monitor_buf;
1521 l = MONITOR_BUF_SIZE;
1522 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1525 key = udev_list_entry_get_name(list_entry);
1526 /* skip private variables */
1530 /* add string to envp array */
1531 udev_device->envp[i++] = s;
1532 if (i+1 >= ENVP_SIZE)
1535 /* add property string to monitor buffer */
1536 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1539 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1543 udev_device->envp[i] = NULL;
1544 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1545 udev_device->envp_uptodate = true;
1546 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1547 i, udev_device->monitor_buf_len);
1551 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1553 if (!udev_device->envp_uptodate)
1554 if (update_envp_monitor_buf(udev_device) != 0)
1556 return udev_device->envp;
1559 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1561 if (!udev_device->envp_uptodate)
1562 if (update_envp_monitor_buf(udev_device) != 0)
1564 *buf = udev_device->monitor_buf;
1565 return udev_device->monitor_buf_len;
1568 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1570 free(udev_device->action);
1571 udev_device->action = strdup(action);
1572 if (udev_device->action == NULL)
1574 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1578 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1580 free(udev_device->driver);
1581 udev_device->driver = strdup(driver);
1582 if (udev_device->driver == NULL)
1584 udev_device->driver_set = true;
1585 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1589 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1591 return udev_device->devpath_old;
1594 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1599 free(udev_device->devpath_old);
1600 udev_device->devpath_old = strdup(devpath_old);
1601 if (udev_device->devpath_old == NULL)
1603 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1605 pos = strrchr(udev_device->devpath_old, '/');
1608 udev_device->sysname_old = strdup(&pos[1]);
1609 if (udev_device->sysname_old == NULL)
1612 /* some devices have '!' in their name, change that to '/' */
1614 while (udev_device->sysname_old[len] != '\0') {
1615 if (udev_device->sysname_old[len] == '!')
1616 udev_device->sysname_old[len] = '/';
1622 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1624 if (udev_device == NULL)
1626 return udev_device->sysname_old;
1629 const char *udev_device_get_knodename(struct udev_device *udev_device)
1631 return udev_device->knodename;
1634 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1636 free(udev_device->knodename);
1637 udev_device->knodename = strdup(knodename);
1638 if (udev_device->knodename == NULL)
1640 /* do not overwrite the udev property with the kernel property */
1641 if (udev_device->devnode == NULL)
1642 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1646 int udev_device_get_timeout(struct udev_device *udev_device)
1648 return udev_device->timeout;
1651 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1653 udev_device->timeout = timeout;
1656 int udev_device_get_event_timeout(struct udev_device *udev_device)
1658 if (!udev_device->info_loaded)
1659 udev_device_read_db(udev_device);
1660 return udev_device->event_timeout;
1663 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1667 udev_device->event_timeout = event_timeout;
1668 snprintf(num, sizeof(num), "%u", event_timeout);
1669 udev_device_add_property(udev_device, "TIMEOUT", num);
1673 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1677 udev_device->seqnum = seqnum;
1678 snprintf(num, sizeof(num), "%llu", seqnum);
1679 udev_device_add_property(udev_device, "SEQNUM", num);
1683 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1687 udev_device->devnum = devnum;
1689 snprintf(num, sizeof(num), "%u", major(devnum));
1690 udev_device_add_property(udev_device, "MAJOR", num);
1691 snprintf(num, sizeof(num), "%u", minor(devnum));
1692 udev_device_add_property(udev_device, "MINOR", num);
1696 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1698 if (!udev_device->info_loaded)
1699 udev_device_read_db(udev_device);
1700 return udev_device->devlink_priority;
1703 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1705 udev_device->devlink_priority = prio;
1709 int udev_device_get_watch_handle(struct udev_device *udev_device)
1711 if (!udev_device->info_loaded)
1712 udev_device_read_db(udev_device);
1713 return udev_device->watch_handle;
1716 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1718 udev_device->watch_handle = handle;
1722 int udev_device_get_ifindex(struct udev_device *udev_device)
1724 if (!udev_device->info_loaded)
1725 udev_device_read_uevent_file(udev_device);
1726 return udev_device->ifindex;
1729 int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
1733 udev_device->ifindex = ifindex;
1734 snprintf(num, sizeof(num), "%u", ifindex);
1735 udev_device_add_property(udev_device, "IFINDEX", num);