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 tags_list;
65 unsigned long long int seqnum;
77 bool devlinks_uptodate;
87 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
89 udev_device->envp_uptodate = false;
91 struct udev_list_entry *list_entry;
93 list_entry = udev_device_get_properties_list_entry(udev_device);
94 list_entry = udev_list_entry_get_by_name(list_entry, key);
95 if (list_entry != NULL)
96 udev_list_entry_delete(list_entry);
99 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
102 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
104 char name[UTIL_LINE_SIZE];
107 util_strscpy(name, sizeof(name), property);
108 val = strchr(name, '=');
115 return udev_device_add_property(udev_device, name, val);
119 * parse property string, and if needed, update internal values accordingly
121 * udev_device_add_property_from_string_parse_finish() needs to be
122 * called after adding properties, and its return value checked
124 * udev_device_set_info_loaded() needs to be set, to avoid trying
125 * to use a device without a DEVPATH set
127 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
129 if (strncmp(property, "DEVPATH=", 8) == 0) {
130 char path[UTIL_PATH_SIZE];
132 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
133 udev_device_set_syspath(udev_device, path);
134 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
135 udev_device_set_subsystem(udev_device, &property[10]);
136 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
137 udev_device_set_devtype(udev_device, &property[8]);
138 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
139 if (property[8] == '/')
140 udev_device_set_devnode(udev_device, &property[8]);
142 udev_device_set_knodename(udev_device, &property[8]);
143 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
144 char devlinks[UTIL_PATH_SIZE];
148 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
150 next = strchr(slink, ' ');
151 while (next != NULL) {
153 udev_device_add_devlink(udev_device, slink, 0);
155 next = strchr(slink, ' ');
157 if (slink[0] != '\0')
158 udev_device_add_devlink(udev_device, slink, 0);
159 } else if (strncmp(property, "TAGS=", 5) == 0) {
160 char tags[UTIL_PATH_SIZE];
163 util_strscpy(tags, sizeof(tags), &property[5]);
164 next = strchr(tags, ':');
167 while (next[0] != '\0') {
171 next = strchr(tag, ':');
176 udev_device_add_tag(udev_device, tag);
179 } else if (strncmp(property, "DRIVER=", 7) == 0) {
180 udev_device_set_driver(udev_device, &property[7]);
181 } else if (strncmp(property, "ACTION=", 7) == 0) {
182 udev_device_set_action(udev_device, &property[7]);
183 } else if (strncmp(property, "MAJOR=", 6) == 0) {
184 udev_device->maj = strtoull(&property[6], NULL, 10);
185 } else if (strncmp(property, "MINOR=", 6) == 0) {
186 udev_device->min = strtoull(&property[6], NULL, 10);
187 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
188 udev_device_set_devpath_old(udev_device, &property[12]);
189 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
190 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
191 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
192 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
193 } else if (strncmp(property, "IFINDEX=", 8) == 0) {
194 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
196 udev_device_add_property_from_string(udev_device, property);
200 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
202 if (udev_device->maj > 0)
203 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
204 udev_device->maj = 0;
205 udev_device->min = 0;
207 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
213 * udev_device_get_property_value:
214 * @udev_device: udev device
215 * @key: property name
217 * Returns: the value of a device property, or #NULL if there is no such property.
219 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
221 struct udev_list_entry *list_entry;
223 if (udev_device == NULL)
228 list_entry = udev_device_get_properties_list_entry(udev_device);
229 list_entry = udev_list_entry_get_by_name(list_entry, key);
230 return udev_list_entry_get_value(list_entry);
233 int udev_device_read_db(struct udev_device *udev_device)
236 char filename[UTIL_PATH_SIZE];
237 char line[UTIL_LINE_SIZE];
240 if (udev_device->db_loaded)
242 udev_device->db_loaded = true;
244 id = udev_device_get_id_filename(udev_device);
247 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/", id, NULL);
248 f = fopen(filename, "re");
250 info(udev_device->udev, "no db file to read %s: %m\n", filename);
253 udev_device->is_initialized = true;
255 while (fgets(line, sizeof(line), f)) {
258 struct udev_list_entry *entry;
267 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
268 udev_device_set_devnode(udev_device, filename);
271 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
272 udev_device_add_devlink(udev_device, filename, 0);
275 udev_device_set_devlink_priority(udev_device, atoi(val));
278 entry = udev_device_add_property_from_string(udev_device, val);
279 udev_list_entry_set_flags(entry, 1);
282 udev_device_add_tag(udev_device, val);
285 udev_device_set_watch_handle(udev_device, atoi(val));
291 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
295 int udev_device_read_uevent_file(struct udev_device *udev_device)
297 char filename[UTIL_PATH_SIZE];
299 char line[UTIL_LINE_SIZE];
303 if (udev_device->uevent_loaded)
306 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
307 f = fopen(filename, "re");
310 udev_device->uevent_loaded = true;
312 while (fgets(line, sizeof(line), f)) {
315 pos = strchr(line, '\n');
320 if (strncmp(line, "DEVTYPE=", 8) == 0)
321 udev_device_set_devtype(udev_device, &line[8]);
322 else if (strncmp(line, "MAJOR=", 6) == 0)
323 maj = strtoull(&line[6], NULL, 10);
324 else if (strncmp(line, "MINOR=", 6) == 0)
325 min = strtoull(&line[6], NULL, 10);
326 else if (strncmp(line, "IFINDEX=", 8) == 0)
327 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
328 else if (strncmp(line, "DEVNAME=", 8) == 0)
329 udev_device_set_knodename(udev_device, &line[8]);
331 udev_device_add_property_from_string(udev_device, line);
334 udev_device->devnum = makedev(maj, min);
339 void udev_device_set_info_loaded(struct udev_device *device)
341 device->info_loaded = true;
344 struct udev_device *udev_device_new(struct udev *udev)
346 struct udev_device *udev_device;
347 struct udev_list_entry *list_entry;
352 udev_device = calloc(1, sizeof(struct udev_device));
353 if (udev_device == NULL)
355 udev_device->refcount = 1;
356 udev_device->udev = udev;
357 udev_list_init(&udev_device->devlinks_list);
358 udev_list_init(&udev_device->properties_list);
359 udev_list_init(&udev_device->sysattr_list);
360 udev_list_init(&udev_device->tags_list);
361 udev_device->event_timeout = -1;
362 udev_device->watch_handle = -1;
363 /* copy global properties */
364 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
365 udev_device_add_property(udev_device,
366 udev_list_entry_get_name(list_entry),
367 udev_list_entry_get_value(list_entry));
368 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
373 * udev_device_new_from_syspath:
374 * @udev: udev library context
375 * @syspath: sys device path including sys directory
377 * Create new udev device, and fill in information from the sys
378 * device and the udev database entry. The syspath is the absolute
379 * path to the device, including the sys mount point.
381 * The initial refcount is 1, and needs to be decremented to
382 * release the resources of the udev device.
384 * Returns: a new udev device, or #NULL, if it does not exist
386 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
390 char path[UTIL_PATH_SIZE];
393 struct udev_device *udev_device;
400 /* path starts in sys */
401 len = strlen(udev_get_sys_path(udev));
402 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
403 info(udev, "not in sys :%s\n", syspath);
407 /* path is not a root directory */
408 subdir = &syspath[len+1];
409 pos = strrchr(subdir, '/');
410 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
411 dbg(udev, "not a subdir :%s\n", syspath);
415 /* resolve possible symlink to real path */
416 util_strscpy(path, sizeof(path), syspath);
417 util_resolve_sys_link(udev, path, sizeof(path));
419 if (strncmp(&path[len], "/devices/", 9) == 0) {
420 char file[UTIL_PATH_SIZE];
422 /* all "devices" require a "uevent" file */
423 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
424 if (stat(file, &statbuf) != 0) {
425 dbg(udev, "not a device: %s\n", syspath);
429 /* everything else just needs to be a directory */
430 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
431 dbg(udev, "directory not found: %s\n", syspath);
436 udev_device = udev_device_new(udev);
437 if (udev_device == NULL)
440 udev_device_set_syspath(udev_device, path);
441 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
447 * udev_device_new_from_devnum:
448 * @udev: udev library context
449 * @type: char or block device
450 * @devnum: device major/minor number
452 * Create new udev device, and fill in information from the sys
453 * device and the udev database entry. The device is looked-up
454 * by its major/minor number and type. Character and block device
455 * numbers are not unique across the two types.
457 * The initial refcount is 1, and needs to be decremented to
458 * release the resources of the udev device.
460 * Returns: a new udev device, or #NULL, if it does not exist
462 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
464 char path[UTIL_PATH_SIZE];
465 const char *type_str;
469 else if (type == 'c')
474 /* use /sys/dev/{block,char}/<maj>:<min> link */
475 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
476 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
477 return udev_device_new_from_syspath(udev, path);
480 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
484 char subsys[UTIL_PATH_SIZE];
490 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
492 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
494 util_strscpy(subsys, sizeof(subsys), &id[1]);
495 sysname = strchr(subsys, ':');
499 sysname = &sysname[1];
500 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
507 * udev_device_new_from_subsystem_sysname:
508 * @udev: udev library context
509 * @subsystem: the subsystem of the device
510 * @sysname: the name of the device
512 * Create new udev device, and fill in information from the sys device
513 * and the udev database entry. The device is looked up by the subsystem
514 * and name string of the device, like "mem" / "zero", or "block" / "sda".
516 * The initial refcount is 1, and needs to be decremented to
517 * release the resources of the udev device.
519 * Returns: a new udev device, or #NULL, if it does not exist
521 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
523 char path_full[UTIL_PATH_SIZE];
529 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
531 if (strcmp(subsystem, "subsystem") == 0) {
532 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
533 if (stat(path_full, &statbuf) == 0)
536 util_strscpyl(path, l, "/bus/", sysname, NULL);
537 if (stat(path_full, &statbuf) == 0)
540 util_strscpyl(path, l, "/class/", sysname, NULL);
541 if (stat(path_full, &statbuf) == 0)
546 if (strcmp(subsystem, "module") == 0) {
547 util_strscpyl(path, l, "/module/", sysname, NULL);
548 if (stat(path_full, &statbuf) == 0)
553 if (strcmp(subsystem, "drivers") == 0) {
554 char subsys[UTIL_NAME_SIZE];
557 util_strscpy(subsys, sizeof(subsys), sysname);
558 driver = strchr(subsys, ':');
559 if (driver != NULL) {
563 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
564 if (stat(path_full, &statbuf) == 0)
567 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
568 if (stat(path_full, &statbuf) == 0)
574 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
575 if (stat(path_full, &statbuf) == 0)
578 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
579 if (stat(path_full, &statbuf) == 0)
582 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
583 if (stat(path_full, &statbuf) == 0)
588 return udev_device_new_from_syspath(udev, path_full);
592 * udev_device_new_from_environment
593 * @udev: udev library context
595 * Create new udev device, and fill in information from the
596 * current process environment. This only works reliable if
597 * the process is called from a udev rule. It is usually used
598 * for tools executed from IMPORT= rules.
600 * The initial refcount is 1, and needs to be decremented to
601 * release the resources of the udev device.
603 * Returns: a new udev device, or #NULL, if it does not exist
605 struct udev_device *udev_device_new_from_environment(struct udev *udev)
608 struct udev_device *udev_device;
610 udev_device = udev_device_new(udev);
611 if (udev_device == NULL)
613 udev_device_set_info_loaded(udev_device);
615 for (i = 0; environ[i] != NULL; i++)
616 udev_device_add_property_from_string_parse(udev_device, environ[i]);
618 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
619 info(udev, "missing values, invalid device\n");
620 udev_device_unref(udev_device);
627 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
629 struct udev_device *udev_device_parent = NULL;
630 char path[UTIL_PATH_SIZE];
633 util_strscpy(path, sizeof(path), udev_device->syspath);
634 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
638 pos = strrchr(subdir, '/');
639 if (pos == NULL || pos < &subdir[2])
642 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
643 if (udev_device_parent != NULL)
644 return udev_device_parent;
650 * udev_device_get_parent:
651 * @udev_device: the device to start searching from
653 * Find the next parent device, and fill in information from the sys
654 * device and the udev database entry.
656 * The returned the device is not referenced. It is attached to the
657 * child device, and will be cleaned up when the child device
660 * It is not necessarily just the upper level directory, empty or not
661 * recognized sys directories are ignored.
663 * It can be called as many times as needed, without caring about
666 * Returns: a new udev device, or #NULL, if it no parent exist.
668 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
670 if (udev_device == NULL)
672 if (!udev_device->parent_set) {
673 udev_device->parent_set = true;
674 udev_device->parent_device = device_new_from_parent(udev_device);
676 if (udev_device->parent_device != NULL)
677 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
678 return udev_device->parent_device;
682 * udev_device_get_parent_with_subsystem_devtype:
683 * @udev_device: udev device to start searching from
684 * @subsystem: the subsystem of the device
685 * @devtype: the type (DEVTYPE) of the device
687 * Find the next parent device, with a matching subsystem and devtype
688 * value, and fill in information from the sys device and the udev
691 * If devtype is #NULL, only subsystem is checked, and any devtype will
694 * The returned the device is not referenced. It is attached to the
695 * child device, and will be cleaned up when the child device
698 * It can be called as many times as needed, without caring about
701 * Returns: a new udev device, or #NULL if no matching parent exists.
703 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
705 struct udev_device *parent;
707 if (subsystem == NULL)
710 parent = udev_device_get_parent(udev_device);
711 while (parent != NULL) {
712 const char *parent_subsystem;
713 const char *parent_devtype;
715 parent_subsystem = udev_device_get_subsystem(parent);
716 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
719 parent_devtype = udev_device_get_devtype(parent);
720 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
723 parent = udev_device_get_parent(parent);
729 * udev_device_get_udev:
730 * @udev_device: udev device
732 * Retrieve the udev library context the device was created with.
734 * Returns: the udev library context
736 struct udev *udev_device_get_udev(struct udev_device *udev_device)
738 if (udev_device == NULL)
740 return udev_device->udev;
745 * @udev_device: udev device
747 * Take a reference of a udev device.
749 * Returns: the passed udev device
751 struct udev_device *udev_device_ref(struct udev_device *udev_device)
753 if (udev_device == NULL)
755 udev_device->refcount++;
761 * @udev_device: udev device
763 * Drop a reference of a udev device. If the refcount reaches zero,
764 * the resources of the device will be released.
767 void udev_device_unref(struct udev_device *udev_device)
769 if (udev_device == NULL)
771 udev_device->refcount--;
772 if (udev_device->refcount > 0)
774 if (udev_device->parent_device != NULL)
775 udev_device_unref(udev_device->parent_device);
776 free(udev_device->syspath);
777 free(udev_device->sysname);
778 free(udev_device->devnode);
779 free(udev_device->subsystem);
780 free(udev_device->devtype);
781 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
782 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
783 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
784 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
785 free(udev_device->action);
786 free(udev_device->driver);
787 free(udev_device->devpath_old);
788 free(udev_device->sysname_old);
789 free(udev_device->knodename);
790 free(udev_device->id_filename);
791 free(udev_device->envp);
792 free(udev_device->monitor_buf);
793 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
798 * udev_device_get_devpath:
799 * @udev_device: udev device
801 * Retrieve the kernel devpath value of the udev device. The path
802 * does not contain the sys mount point, and starts with a '/'.
804 * Returns: the devpath of the udev device
806 const char *udev_device_get_devpath(struct udev_device *udev_device)
808 if (udev_device == NULL)
810 return udev_device->devpath;
814 * udev_device_get_syspath:
815 * @udev_device: udev device
817 * Retrieve the sys path of the udev device. The path is an
818 * absolute path and starts with the sys mount point.
820 * Returns: the sys path of the udev device
822 const char *udev_device_get_syspath(struct udev_device *udev_device)
824 if (udev_device == NULL)
826 return udev_device->syspath;
830 * udev_device_get_sysname:
831 * @udev_device: udev device
833 * Returns: the sys name of the device device
835 const char *udev_device_get_sysname(struct udev_device *udev_device)
837 if (udev_device == NULL)
839 return udev_device->sysname;
843 * udev_device_get_sysnum:
844 * @udev_device: udev device
846 * Returns: the trailing number of of the device name
848 const char *udev_device_get_sysnum(struct udev_device *udev_device)
850 if (udev_device == NULL)
852 return udev_device->sysnum;
856 * udev_device_get_devnode:
857 * @udev_device: udev device
859 * Retrieve the device node file name belonging to the udev device.
860 * The path is an absolute path, and starts with the device directory.
862 * Returns: the device node file name of the udev device, or #NULL if no device node exists
864 const char *udev_device_get_devnode(struct udev_device *udev_device)
866 if (udev_device == NULL)
868 if (!udev_device->info_loaded) {
869 udev_device_read_uevent_file(udev_device);
870 udev_device_read_db(udev_device);
873 /* we might get called before we handled an event and have a db, use the kernel-provided name */
874 if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
875 char filename[UTIL_NAME_SIZE];
877 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/",
878 udev_device_get_knodename(udev_device), NULL);
879 udev_device_set_devnode(udev_device, filename);
880 return udev_device->devnode;
883 return udev_device->devnode;
887 * udev_device_get_subsystem:
888 * @udev_device: udev device
890 * Retrieve the subsystem string of the udev device. The string does not
893 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
895 const char *udev_device_get_subsystem(struct udev_device *udev_device)
897 char subsystem[UTIL_NAME_SIZE];
899 if (udev_device == NULL)
901 if (!udev_device->subsystem_set) {
902 udev_device->subsystem_set = true;
903 /* read "subsystem" link */
904 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
905 udev_device_set_subsystem(udev_device, subsystem);
906 return udev_device->subsystem;
909 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
910 udev_device_set_subsystem(udev_device, "module");
911 return udev_device->subsystem;
913 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
914 udev_device_set_subsystem(udev_device, "drivers");
915 return udev_device->subsystem;
917 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
918 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
919 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
920 udev_device_set_subsystem(udev_device, "subsystem");
921 return udev_device->subsystem;
924 return udev_device->subsystem;
928 * udev_device_get_devtype:
929 * @udev_device: udev device
931 * Retrieve the devtype string of the udev device.
933 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
935 const char *udev_device_get_devtype(struct udev_device *udev_device)
937 if (udev_device == NULL)
939 if (!udev_device->devtype_set) {
940 udev_device->devtype_set = true;
941 udev_device_read_uevent_file(udev_device);
943 return udev_device->devtype;
947 * udev_device_get_devlinks_list_entry:
948 * @udev_device: udev device
950 * Retrieve the list of device links pointing to the device file of
951 * the udev device. The next list entry can be retrieved with
952 * udev_list_entry_next(), which returns #NULL if no more entries exist.
953 * The devlink path can be retrieved from the list entry by
954 * udev_list_entry_get_name(). The path is an absolute path, and starts with
955 * the device directory.
957 * Returns: the first entry of the device node link list
959 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
961 if (udev_device == NULL)
963 if (!udev_device->info_loaded)
964 udev_device_read_db(udev_device);
965 return udev_list_get_entry(&udev_device->devlinks_list);
968 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
970 udev_device->devlinks_uptodate = false;
971 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
975 * udev_device_get_properties_list_entry:
976 * @udev_device: udev device
978 * Retrieve the list of key/value device properties of the udev
979 * device. The next list entry can be retrieved with udev_list_entry_next(),
980 * which returns #NULL if no more entries exist. The property name
981 * can be retrieved from the list entry by udev_list_get_name(),
982 * the property value by udev_list_get_value().
984 * Returns: the first entry of the property list
986 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
988 if (udev_device == NULL)
990 if (!udev_device->info_loaded) {
991 udev_device_read_uevent_file(udev_device);
992 udev_device_read_db(udev_device);
994 if (!udev_device->devlinks_uptodate) {
995 char symlinks[UTIL_PATH_SIZE];
996 struct udev_list_entry *list_entry;
998 udev_device->devlinks_uptodate = true;
999 list_entry = udev_device_get_devlinks_list_entry(udev_device);
1000 if (list_entry != NULL) {
1005 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1006 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1007 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1008 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1011 if (!udev_device->tags_uptodate) {
1012 udev_device->tags_uptodate = true;
1013 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1014 char tags[UTIL_PATH_SIZE];
1015 struct udev_list_entry *list_entry;
1020 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1021 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1022 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1023 udev_device_add_property(udev_device, "TAGS", tags);
1026 return udev_list_get_entry(&udev_device->properties_list);
1030 * udev_device_get_driver:
1031 * @udev_device: udev device
1033 * Returns: the driver string, or #NULL if there is no driver attached.
1035 const char *udev_device_get_driver(struct udev_device *udev_device)
1037 char driver[UTIL_NAME_SIZE];
1039 if (udev_device == NULL)
1041 if (!udev_device->driver_set) {
1042 udev_device->driver_set = true;
1043 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1044 udev_device->driver = strdup(driver);
1046 return udev_device->driver;
1050 * udev_device_get_devnum:
1051 * @udev_device: udev device
1053 * Returns: the device major/minor number.
1055 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1057 if (udev_device == NULL)
1058 return makedev(0, 0);
1059 if (!udev_device->info_loaded)
1060 udev_device_read_uevent_file(udev_device);
1061 return udev_device->devnum;
1065 * udev_device_get_action:
1066 * @udev_device: udev device
1068 * This is only valid if the device was received through a monitor. Devices read from
1069 * sys do not have an action string. Usual actions are: add, remove, change, online,
1072 * Returns: the kernel action value, or #NULL if there is no action value available.
1074 const char *udev_device_get_action(struct udev_device *udev_device)
1076 if (udev_device == NULL)
1078 return udev_device->action;
1082 * udev_device_get_devnum:
1083 * @udev_device: udev device
1085 * This is only valid if the device was received through a monitor. Devices read from
1086 * sys do not have a sequence number.
1088 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1090 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1092 if (udev_device == NULL)
1094 return udev_device->seqnum;
1098 * udev_device_get_sysattr_value:
1099 * @udev_device: udev device
1100 * @sysattr: attribute name
1102 * The retrieved value is cached in the device. Repeated calls will return the same
1103 * value and not open the attribute again.
1105 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1107 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1109 struct udev_list_entry *list_entry;
1110 char path[UTIL_PATH_SIZE];
1112 struct stat statbuf;
1115 const char *val = NULL;
1117 if (udev_device == NULL)
1119 if (sysattr == NULL)
1122 /* look for possibly already cached result */
1123 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1124 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1125 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1126 sysattr, udev_list_entry_get_value(list_entry));
1127 return udev_list_entry_get_value(list_entry);
1131 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1132 if (lstat(path, &statbuf) != 0) {
1133 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1134 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1138 if (S_ISLNK(statbuf.st_mode)) {
1139 char target[UTIL_NAME_SIZE];
1143 /* some core links return the last element of the target path */
1144 if (strcmp(sysattr, "driver") != 0 &&
1145 strcmp(sysattr, "subsystem") != 0 &&
1146 strcmp(sysattr, "module") != 0)
1149 len = readlink(path, target, sizeof(target));
1150 if (len <= 0 || len == sizeof(target))
1154 pos = strrchr(target, '/');
1157 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1158 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1159 val = udev_list_entry_get_value(list_entry);
1165 /* skip directories */
1166 if (S_ISDIR(statbuf.st_mode))
1169 /* skip non-readable files */
1170 if ((statbuf.st_mode & S_IRUSR) == 0)
1173 /* read attribute value */
1174 fd = open(path, O_RDONLY|O_CLOEXEC);
1176 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1179 size = read(fd, value, sizeof(value));
1183 if (size == sizeof(value))
1186 /* got a valid value, store it in cache and return it */
1188 util_remove_trailing_chars(value, '\n');
1189 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1190 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1191 val = udev_list_entry_get_value(list_entry);
1196 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1201 free(udev_device->syspath);
1202 udev_device->syspath = strdup(syspath);
1203 if (udev_device->syspath == NULL)
1205 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1206 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1208 pos = strrchr(udev_device->syspath, '/');
1211 udev_device->sysname = strdup(&pos[1]);
1212 if (udev_device->sysname == NULL)
1215 /* some devices have '!' in their name, change that to '/' */
1217 while (udev_device->sysname[len] != '\0') {
1218 if (udev_device->sysname[len] == '!')
1219 udev_device->sysname[len] = '/';
1223 /* trailing number */
1224 while (len > 0 && isdigit(udev_device->sysname[--len]))
1225 udev_device->sysnum = &udev_device->sysname[len];
1227 /* sysname is completely numeric */
1229 udev_device->sysnum = NULL;
1234 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1236 free(udev_device->subsystem);
1237 udev_device->subsystem = strdup(subsystem);
1238 if (udev_device->subsystem == NULL)
1240 udev_device->subsystem_set = true;
1241 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1245 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1247 free(udev_device->devtype);
1248 udev_device->devtype = strdup(devtype);
1249 if (udev_device->devtype == NULL)
1251 udev_device->devtype_set = true;
1252 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1256 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1258 free(udev_device->devnode);
1259 udev_device->devnode = strdup(devnode);
1260 if (udev_device->devnode == NULL)
1262 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1266 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1268 struct udev_list_entry *list_entry;
1270 udev_device->devlinks_uptodate = false;
1271 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1272 if (list_entry == NULL)
1275 udev_list_entry_set_flags(list_entry, 1);
1279 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1281 if (udev_device->id_filename == NULL) {
1282 if (udev_device_get_subsystem(udev_device) == NULL)
1285 if (major(udev_device_get_devnum(udev_device)) > 0) {
1286 /* use dev_t -- b259:131072, c254:0 */
1287 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1288 strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1289 major(udev_device_get_devnum(udev_device)),
1290 minor(udev_device_get_devnum(udev_device))) < 0)
1291 udev_device->id_filename = NULL;
1292 } else if (udev_device_get_ifindex(udev_device) > 0) {
1293 /* use netdev ifindex -- n3 */
1294 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1295 udev_device->id_filename = NULL;
1298 * use $subsys:$syname -- pci:0000:00:1f.2
1299 * sysname() has '!' translated, get it from devpath
1301 const char *sysname;
1302 sysname = strrchr(udev_device->devpath, '/');
1303 if (sysname == NULL)
1305 sysname = &sysname[1];
1306 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1307 udev_device->id_filename = NULL;
1310 return udev_device->id_filename;
1314 * udev_device_get_is_initialized:
1315 * @udev_device: udev device
1317 * Check if udev has already handled the device and has set up
1318 * device node permissions and context, or has renamed a network
1321 * For now, this is only implemented for devices with a device node
1322 * or network interfaces. All other devices return 1 here.
1324 * Returns: 1 if the device is set up. 0 otherwise.
1326 int udev_device_get_is_initialized(struct udev_device *udev_device)
1328 if (!udev_device->info_loaded)
1329 udev_device_read_db(udev_device);
1330 return udev_device->is_initialized;
1333 void udev_device_set_is_initialized(struct udev_device *udev_device)
1335 udev_device->is_initialized = true;
1338 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1340 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1342 udev_device->tags_uptodate = false;
1343 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
1348 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1350 udev_device->tags_uptodate = false;
1351 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1355 * udev_device_get_tags_list_entry:
1356 * @udev_device: udev device
1358 * Retrieve the list of tags attached to the udev device. The next
1359 * list entry can be retrieved with udev_list_entry_next(),
1360 * which returns #NULL if no more entries exist. The tag string
1361 * can be retrieved from the list entry by udev_list_get_name().
1363 * Returns: the first entry of the tag list
1365 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1367 if (udev_device == NULL)
1369 return udev_list_get_entry(&udev_device->tags_list);
1372 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1374 struct udev_list_entry *list_entry;
1376 if (!udev_device->info_loaded)
1377 udev_device_read_db(udev_device);
1378 list_entry = udev_device_get_tags_list_entry(udev_device);
1379 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1380 if (list_entry != NULL)
1385 #define ENVP_SIZE 128
1386 #define MONITOR_BUF_SIZE 4096
1387 static int update_envp_monitor_buf(struct udev_device *udev_device)
1389 struct udev_list_entry *list_entry;
1394 /* monitor buffer of property strings */
1395 free(udev_device->monitor_buf);
1396 udev_device->monitor_buf_len = 0;
1397 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1398 if (udev_device->monitor_buf == NULL)
1401 /* envp array, strings will point into monitor buffer */
1402 if (udev_device->envp == NULL)
1403 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1404 if (udev_device->envp == NULL)
1408 s = udev_device->monitor_buf;
1409 l = MONITOR_BUF_SIZE;
1410 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1413 key = udev_list_entry_get_name(list_entry);
1414 /* skip private variables */
1418 /* add string to envp array */
1419 udev_device->envp[i++] = s;
1420 if (i+1 >= ENVP_SIZE)
1423 /* add property string to monitor buffer */
1424 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1427 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1431 udev_device->envp[i] = NULL;
1432 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1433 udev_device->envp_uptodate = true;
1434 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1435 i, udev_device->monitor_buf_len);
1439 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1441 if (!udev_device->envp_uptodate)
1442 if (update_envp_monitor_buf(udev_device) != 0)
1444 return udev_device->envp;
1447 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1449 if (!udev_device->envp_uptodate)
1450 if (update_envp_monitor_buf(udev_device) != 0)
1452 *buf = udev_device->monitor_buf;
1453 return udev_device->monitor_buf_len;
1456 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1458 free(udev_device->action);
1459 udev_device->action = strdup(action);
1460 if (udev_device->action == NULL)
1462 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1466 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1468 free(udev_device->driver);
1469 udev_device->driver = strdup(driver);
1470 if (udev_device->driver == NULL)
1472 udev_device->driver_set = true;
1473 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1477 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1479 return udev_device->devpath_old;
1482 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1487 free(udev_device->devpath_old);
1488 udev_device->devpath_old = strdup(devpath_old);
1489 if (udev_device->devpath_old == NULL)
1491 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1493 pos = strrchr(udev_device->devpath_old, '/');
1496 udev_device->sysname_old = strdup(&pos[1]);
1497 if (udev_device->sysname_old == NULL)
1500 /* some devices have '!' in their name, change that to '/' */
1502 while (udev_device->sysname_old[len] != '\0') {
1503 if (udev_device->sysname_old[len] == '!')
1504 udev_device->sysname_old[len] = '/';
1510 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1512 if (udev_device == NULL)
1514 return udev_device->sysname_old;
1517 const char *udev_device_get_knodename(struct udev_device *udev_device)
1519 return udev_device->knodename;
1522 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1524 free(udev_device->knodename);
1525 udev_device->knodename = strdup(knodename);
1526 if (udev_device->knodename == NULL)
1528 /* do not overwrite the udev property with the kernel property */
1529 if (udev_device->devnode == NULL)
1530 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1534 int udev_device_get_timeout(struct udev_device *udev_device)
1536 return udev_device->timeout;
1539 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1541 udev_device->timeout = timeout;
1544 int udev_device_get_event_timeout(struct udev_device *udev_device)
1546 if (!udev_device->info_loaded)
1547 udev_device_read_db(udev_device);
1548 return udev_device->event_timeout;
1551 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1555 udev_device->event_timeout = event_timeout;
1556 snprintf(num, sizeof(num), "%u", event_timeout);
1557 udev_device_add_property(udev_device, "TIMEOUT", num);
1561 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1565 udev_device->seqnum = seqnum;
1566 snprintf(num, sizeof(num), "%llu", seqnum);
1567 udev_device_add_property(udev_device, "SEQNUM", num);
1571 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1575 udev_device->devnum = devnum;
1577 snprintf(num, sizeof(num), "%u", major(devnum));
1578 udev_device_add_property(udev_device, "MAJOR", num);
1579 snprintf(num, sizeof(num), "%u", minor(devnum));
1580 udev_device_add_property(udev_device, "MINOR", num);
1584 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1586 if (!udev_device->info_loaded)
1587 udev_device_read_db(udev_device);
1588 return udev_device->devlink_priority;
1591 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1593 udev_device->devlink_priority = prio;
1597 int udev_device_get_watch_handle(struct udev_device *udev_device)
1599 if (!udev_device->info_loaded)
1600 udev_device_read_db(udev_device);
1601 return udev_device->watch_handle;
1604 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1606 udev_device->watch_handle = handle;
1610 int udev_device_get_ifindex(struct udev_device *udev_device)
1612 if (!udev_device->info_loaded)
1613 udev_device_read_uevent_file(udev_device);
1614 return udev_device->ifindex;
1617 int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
1621 udev_device->ifindex = ifindex;
1622 snprintf(num, sizeof(num), "%u", ifindex);
1623 udev_device_add_property(udev_device, "IFINDEX", num);