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;
86 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
88 udev_device->envp_uptodate = false;
90 struct udev_list_entry *list_entry;
92 list_entry = udev_device_get_properties_list_entry(udev_device);
93 list_entry = udev_list_entry_get_by_name(list_entry, key);
94 if (list_entry != NULL)
95 udev_list_entry_delete(list_entry);
98 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
101 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
103 char name[UTIL_LINE_SIZE];
106 util_strscpy(name, sizeof(name), property);
107 val = strchr(name, '=');
114 return udev_device_add_property(udev_device, name, val);
118 * parse property string, and if needed, update internal values accordingly
120 * udev_device_add_property_from_string_parse_finish() needs to be
121 * called after adding properties, and its return value checked
123 * udev_device_set_info_loaded() needs to be set, to avoid trying
124 * to use a device without a DEVPATH set
126 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
128 if (strncmp(property, "DEVPATH=", 8) == 0) {
129 char path[UTIL_PATH_SIZE];
131 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
132 udev_device_set_syspath(udev_device, path);
133 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
134 udev_device_set_subsystem(udev_device, &property[10]);
135 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
136 udev_device_set_devtype(udev_device, &property[8]);
137 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
138 if (property[8] == '/')
139 udev_device_set_devnode(udev_device, &property[8]);
141 udev_device_set_knodename(udev_device, &property[8]);
142 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
143 char devlinks[UTIL_PATH_SIZE];
147 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
149 next = strchr(slink, ' ');
150 while (next != NULL) {
152 udev_device_add_devlink(udev_device, slink, 0);
154 next = strchr(slink, ' ');
156 if (slink[0] != '\0')
157 udev_device_add_devlink(udev_device, slink, 0);
158 } else if (strncmp(property, "TAGS=", 5) == 0) {
159 char tags[UTIL_PATH_SIZE];
162 util_strscpy(tags, sizeof(tags), &property[5]);
163 next = strchr(tags, ':');
166 while (next[0] != '\0') {
170 next = strchr(tag, ':');
175 udev_device_add_tag(udev_device, tag);
178 } else if (strncmp(property, "DRIVER=", 7) == 0) {
179 udev_device_set_driver(udev_device, &property[7]);
180 } else if (strncmp(property, "ACTION=", 7) == 0) {
181 udev_device_set_action(udev_device, &property[7]);
182 } else if (strncmp(property, "MAJOR=", 6) == 0) {
183 udev_device->maj = strtoull(&property[6], NULL, 10);
184 } else if (strncmp(property, "MINOR=", 6) == 0) {
185 udev_device->min = strtoull(&property[6], NULL, 10);
186 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
187 udev_device_set_devpath_old(udev_device, &property[12]);
188 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
189 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
190 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
191 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
192 } else if (strncmp(property, "IFINDEX=", 8) == 0) {
193 udev_device_set_ifindex(udev_device, strtoull(&property[8], NULL, 10));
195 udev_device_add_property_from_string(udev_device, property);
199 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
201 if (udev_device->maj > 0)
202 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
203 udev_device->maj = 0;
204 udev_device->min = 0;
206 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
212 * udev_device_get_property_value:
213 * @udev_device: udev device
214 * @key: property name
216 * Returns: the value of a device property, or #NULL if there is no such property.
218 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
220 struct udev_list_entry *list_entry;
222 if (udev_device == NULL)
227 list_entry = udev_device_get_properties_list_entry(udev_device);
228 list_entry = udev_list_entry_get_by_name(list_entry, key);
229 return udev_list_entry_get_value(list_entry);
232 int udev_device_read_db(struct udev_device *udev_device)
235 char filename[UTIL_PATH_SIZE];
236 char line[UTIL_LINE_SIZE];
239 if (udev_device->db_loaded)
241 udev_device->db_loaded = true;
243 id = udev_device_get_id_filename(udev_device);
246 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/", id, NULL);
247 f = fopen(filename, "re");
249 info(udev_device->udev, "no db file to read %s: %m\n", filename);
253 while (fgets(line, sizeof(line), f)) {
256 struct udev_list_entry *entry;
265 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
266 udev_device_set_devnode(udev_device, filename);
269 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
270 udev_device_add_devlink(udev_device, filename, 0);
273 udev_device_set_devlink_priority(udev_device, atoi(val));
276 entry = udev_device_add_property_from_string(udev_device, val);
277 udev_list_entry_set_flags(entry, 1);
280 udev_device_add_tag(udev_device, val);
283 udev_device_set_watch_handle(udev_device, atoi(val));
289 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
293 int udev_device_read_uevent_file(struct udev_device *udev_device)
295 char filename[UTIL_PATH_SIZE];
297 char line[UTIL_LINE_SIZE];
301 if (udev_device->uevent_loaded)
304 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
305 f = fopen(filename, "re");
308 udev_device->uevent_loaded = true;
310 while (fgets(line, sizeof(line), f)) {
313 pos = strchr(line, '\n');
318 if (strncmp(line, "DEVTYPE=", 8) == 0)
319 udev_device_set_devtype(udev_device, &line[8]);
320 else if (strncmp(line, "MAJOR=", 6) == 0)
321 maj = strtoull(&line[6], NULL, 10);
322 else if (strncmp(line, "MINOR=", 6) == 0)
323 min = strtoull(&line[6], NULL, 10);
324 else if (strncmp(line, "IFINDEX=", 8) == 0)
325 udev_device_set_ifindex(udev_device, strtoull(&line[8], NULL, 10));
326 else if (strncmp(line, "DEVNAME=", 8) == 0)
327 udev_device_set_knodename(udev_device, &line[8]);
329 udev_device_add_property_from_string(udev_device, line);
332 udev_device->devnum = makedev(maj, min);
337 void udev_device_set_info_loaded(struct udev_device *device)
339 device->info_loaded = true;
342 struct udev_device *udev_device_new(struct udev *udev)
344 struct udev_device *udev_device;
345 struct udev_list_entry *list_entry;
350 udev_device = calloc(1, sizeof(struct udev_device));
351 if (udev_device == NULL)
353 udev_device->refcount = 1;
354 udev_device->udev = udev;
355 udev_list_init(&udev_device->devlinks_list);
356 udev_list_init(&udev_device->properties_list);
357 udev_list_init(&udev_device->sysattr_list);
358 udev_list_init(&udev_device->tags_list);
359 udev_device->event_timeout = -1;
360 udev_device->watch_handle = -1;
361 /* copy global properties */
362 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
363 udev_device_add_property(udev_device,
364 udev_list_entry_get_name(list_entry),
365 udev_list_entry_get_value(list_entry));
366 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
371 * udev_device_new_from_syspath:
372 * @udev: udev library context
373 * @syspath: sys device path including sys directory
375 * Create new udev device, and fill in information from the sys
376 * device and the udev database entry. The syspath is the absolute
377 * path to the device, including the sys mount point.
379 * The initial refcount is 1, and needs to be decremented to
380 * release the resources of the udev device.
382 * Returns: a new udev device, or #NULL, if it does not exist
384 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
388 char path[UTIL_PATH_SIZE];
391 struct udev_device *udev_device;
398 /* path starts in sys */
399 len = strlen(udev_get_sys_path(udev));
400 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
401 info(udev, "not in sys :%s\n", syspath);
405 /* path is not a root directory */
406 subdir = &syspath[len+1];
407 pos = strrchr(subdir, '/');
408 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
409 dbg(udev, "not a subdir :%s\n", syspath);
413 /* resolve possible symlink to real path */
414 util_strscpy(path, sizeof(path), syspath);
415 util_resolve_sys_link(udev, path, sizeof(path));
417 if (strncmp(&path[len], "/devices/", 9) == 0) {
418 char file[UTIL_PATH_SIZE];
420 /* all "devices" require a "uevent" file */
421 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
422 if (stat(file, &statbuf) != 0) {
423 dbg(udev, "not a device: %s\n", syspath);
427 /* everything else just needs to be a directory */
428 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
429 dbg(udev, "directory not found: %s\n", syspath);
434 udev_device = udev_device_new(udev);
435 if (udev_device == NULL)
438 udev_device_set_syspath(udev_device, path);
439 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
445 * udev_device_new_from_devnum:
446 * @udev: udev library context
447 * @type: char or block device
448 * @devnum: device major/minor number
450 * Create new udev device, and fill in information from the sys
451 * device and the udev database entry. The device is looked-up
452 * by its major/minor number and type. Character and block device
453 * numbers are not unique across the two types.
455 * The initial refcount is 1, and needs to be decremented to
456 * release the resources of the udev device.
458 * Returns: a new udev device, or #NULL, if it does not exist
460 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
462 char path[UTIL_PATH_SIZE];
463 const char *type_str;
467 else if (type == 'c')
472 /* use /sys/dev/{block,char}/<maj>:<min> link */
473 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
474 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
475 return udev_device_new_from_syspath(udev, path);
478 struct udev_device *udev_device_new_from_id_filename(struct udev *udev, char *id)
482 char subsys[UTIL_PATH_SIZE];
488 if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
490 return udev_device_new_from_devnum(udev, type, makedev(maj, min));
492 util_strscpy(subsys, sizeof(subsys), &id[1]);
493 sysname = strchr(subsys, ':');
497 sysname = &sysname[1];
498 return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
505 * udev_device_new_from_subsystem_sysname:
506 * @udev: udev library context
507 * @subsystem: the subsystem of the device
508 * @sysname: the name of the device
510 * Create new udev device, and fill in information from the sys device
511 * and the udev database entry. The device is looked up by the subsystem
512 * and name string of the device, like "mem" / "zero", or "block" / "sda".
514 * The initial refcount is 1, and needs to be decremented to
515 * release the resources of the udev device.
517 * Returns: a new udev device, or #NULL, if it does not exist
519 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
521 char path_full[UTIL_PATH_SIZE];
527 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
529 if (strcmp(subsystem, "subsystem") == 0) {
530 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
531 if (stat(path_full, &statbuf) == 0)
534 util_strscpyl(path, l, "/bus/", sysname, NULL);
535 if (stat(path_full, &statbuf) == 0)
538 util_strscpyl(path, l, "/class/", sysname, NULL);
539 if (stat(path_full, &statbuf) == 0)
544 if (strcmp(subsystem, "module") == 0) {
545 util_strscpyl(path, l, "/module/", sysname, NULL);
546 if (stat(path_full, &statbuf) == 0)
551 if (strcmp(subsystem, "drivers") == 0) {
552 char subsys[UTIL_NAME_SIZE];
555 util_strscpy(subsys, sizeof(subsys), sysname);
556 driver = strchr(subsys, ':');
557 if (driver != NULL) {
561 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
562 if (stat(path_full, &statbuf) == 0)
565 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
566 if (stat(path_full, &statbuf) == 0)
572 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
573 if (stat(path_full, &statbuf) == 0)
576 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
577 if (stat(path_full, &statbuf) == 0)
580 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
581 if (stat(path_full, &statbuf) == 0)
586 return udev_device_new_from_syspath(udev, path_full);
590 * udev_device_new_from_environment
591 * @udev: udev library context
593 * Create new udev device, and fill in information from the
594 * current process environment. This only works reliable if
595 * the process is called from a udev rule. It is usually used
596 * for tools executed from IMPORT= rules.
598 * The initial refcount is 1, and needs to be decremented to
599 * release the resources of the udev device.
601 * Returns: a new udev device, or #NULL, if it does not exist
603 struct udev_device *udev_device_new_from_environment(struct udev *udev)
606 struct udev_device *udev_device;
608 udev_device = udev_device_new(udev);
609 if (udev_device == NULL)
611 udev_device_set_info_loaded(udev_device);
613 for (i = 0; environ[i] != NULL; i++)
614 udev_device_add_property_from_string_parse(udev_device, environ[i]);
616 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
617 info(udev, "missing values, invalid device\n");
618 udev_device_unref(udev_device);
625 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
627 struct udev_device *udev_device_parent = NULL;
628 char path[UTIL_PATH_SIZE];
631 util_strscpy(path, sizeof(path), udev_device->syspath);
632 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
636 pos = strrchr(subdir, '/');
637 if (pos == NULL || pos < &subdir[2])
640 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
641 if (udev_device_parent != NULL)
642 return udev_device_parent;
648 * udev_device_get_parent:
649 * @udev_device: the device to start searching from
651 * Find the next parent device, and fill in information from the sys
652 * device and the udev database entry.
654 * The returned the device is not referenced. It is attached to the
655 * child device, and will be cleaned up when the child device
658 * It is not necessarily just the upper level directory, empty or not
659 * recognized sys directories are ignored.
661 * It can be called as many times as needed, without caring about
664 * Returns: a new udev device, or #NULL, if it no parent exist.
666 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
668 if (udev_device == NULL)
670 if (!udev_device->parent_set) {
671 udev_device->parent_set = true;
672 udev_device->parent_device = device_new_from_parent(udev_device);
674 if (udev_device->parent_device != NULL)
675 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
676 return udev_device->parent_device;
680 * udev_device_get_parent_with_subsystem_devtype:
681 * @udev_device: udev device to start searching from
682 * @subsystem: the subsystem of the device
683 * @devtype: the type (DEVTYPE) of the device
685 * Find the next parent device, with a matching subsystem and devtype
686 * value, and fill in information from the sys device and the udev
689 * If devtype is #NULL, only subsystem is checked, and any devtype will
692 * The returned the device is not referenced. It is attached to the
693 * child device, and will be cleaned up when the child device
696 * It can be called as many times as needed, without caring about
699 * Returns: a new udev device, or #NULL if no matching parent exists.
701 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
703 struct udev_device *parent;
705 if (subsystem == NULL)
708 parent = udev_device_get_parent(udev_device);
709 while (parent != NULL) {
710 const char *parent_subsystem;
711 const char *parent_devtype;
713 parent_subsystem = udev_device_get_subsystem(parent);
714 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
717 parent_devtype = udev_device_get_devtype(parent);
718 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
721 parent = udev_device_get_parent(parent);
727 * udev_device_get_udev:
728 * @udev_device: udev device
730 * Retrieve the udev library context the device was created with.
732 * Returns: the udev library context
734 struct udev *udev_device_get_udev(struct udev_device *udev_device)
736 if (udev_device == NULL)
738 return udev_device->udev;
743 * @udev_device: udev device
745 * Take a reference of a udev device.
747 * Returns: the passed udev device
749 struct udev_device *udev_device_ref(struct udev_device *udev_device)
751 if (udev_device == NULL)
753 udev_device->refcount++;
759 * @udev_device: udev device
761 * Drop a reference of a udev device. If the refcount reaches zero,
762 * the resources of the device will be released.
765 void udev_device_unref(struct udev_device *udev_device)
767 if (udev_device == NULL)
769 udev_device->refcount--;
770 if (udev_device->refcount > 0)
772 if (udev_device->parent_device != NULL)
773 udev_device_unref(udev_device->parent_device);
774 free(udev_device->syspath);
775 free(udev_device->sysname);
776 free(udev_device->devnode);
777 free(udev_device->subsystem);
778 free(udev_device->devtype);
779 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
780 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
781 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
782 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
783 free(udev_device->action);
784 free(udev_device->driver);
785 free(udev_device->devpath_old);
786 free(udev_device->sysname_old);
787 free(udev_device->knodename);
788 free(udev_device->id_filename);
789 free(udev_device->envp);
790 free(udev_device->monitor_buf);
791 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
796 * udev_device_get_devpath:
797 * @udev_device: udev device
799 * Retrieve the kernel devpath value of the udev device. The path
800 * does not contain the sys mount point, and starts with a '/'.
802 * Returns: the devpath of the udev device
804 const char *udev_device_get_devpath(struct udev_device *udev_device)
806 if (udev_device == NULL)
808 return udev_device->devpath;
812 * udev_device_get_syspath:
813 * @udev_device: udev device
815 * Retrieve the sys path of the udev device. The path is an
816 * absolute path and starts with the sys mount point.
818 * Returns: the sys path of the udev device
820 const char *udev_device_get_syspath(struct udev_device *udev_device)
822 if (udev_device == NULL)
824 return udev_device->syspath;
828 * udev_device_get_sysname:
829 * @udev_device: udev device
831 * Returns: the sys name of the device device
833 const char *udev_device_get_sysname(struct udev_device *udev_device)
835 if (udev_device == NULL)
837 return udev_device->sysname;
841 * udev_device_get_sysnum:
842 * @udev_device: udev device
844 * Returns: the trailing number of of the device name
846 const char *udev_device_get_sysnum(struct udev_device *udev_device)
848 if (udev_device == NULL)
850 return udev_device->sysnum;
854 * udev_device_get_devnode:
855 * @udev_device: udev device
857 * Retrieve the device node file name belonging to the udev device.
858 * The path is an absolute path, and starts with the device directory.
860 * Returns: the device node file name of the udev device, or #NULL if no device node exists
862 const char *udev_device_get_devnode(struct udev_device *udev_device)
864 if (udev_device == NULL)
866 if (!udev_device->info_loaded) {
867 udev_device_read_uevent_file(udev_device);
868 udev_device_read_db(udev_device);
871 /* we might get called before we handled an event and have a db, use the kernel-provided name */
872 if (udev_device->devnode == NULL && udev_device_get_knodename(udev_device) != NULL) {
873 char filename[UTIL_NAME_SIZE];
875 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/",
876 udev_device_get_knodename(udev_device), NULL);
877 udev_device_set_devnode(udev_device, filename);
878 return udev_device->devnode;
881 return udev_device->devnode;
885 * udev_device_get_subsystem:
886 * @udev_device: udev device
888 * Retrieve the subsystem string of the udev device. The string does not
891 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
893 const char *udev_device_get_subsystem(struct udev_device *udev_device)
895 char subsystem[UTIL_NAME_SIZE];
897 if (udev_device == NULL)
899 if (!udev_device->subsystem_set) {
900 udev_device->subsystem_set = true;
901 /* read "subsystem" link */
902 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
903 udev_device_set_subsystem(udev_device, subsystem);
904 return udev_device->subsystem;
907 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
908 udev_device_set_subsystem(udev_device, "module");
909 return udev_device->subsystem;
911 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
912 udev_device_set_subsystem(udev_device, "drivers");
913 return udev_device->subsystem;
915 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
916 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
917 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
918 udev_device_set_subsystem(udev_device, "subsystem");
919 return udev_device->subsystem;
922 return udev_device->subsystem;
926 * udev_device_get_devtype:
927 * @udev_device: udev device
929 * Retrieve the devtype string of the udev device.
931 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
933 const char *udev_device_get_devtype(struct udev_device *udev_device)
935 if (udev_device == NULL)
937 if (!udev_device->devtype_set) {
938 udev_device->devtype_set = true;
939 udev_device_read_uevent_file(udev_device);
941 return udev_device->devtype;
945 * udev_device_get_devlinks_list_entry:
946 * @udev_device: udev device
948 * Retrieve the list of device links pointing to the device file of
949 * the udev device. The next list entry can be retrieved with
950 * udev_list_entry_next(), which returns #NULL if no more entries exist.
951 * The devlink path can be retrieved from the list entry by
952 * udev_list_entry_get_name(). The path is an absolute path, and starts with
953 * the device directory.
955 * Returns: the first entry of the device node link list
957 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
959 if (udev_device == NULL)
961 if (!udev_device->info_loaded)
962 udev_device_read_db(udev_device);
963 return udev_list_get_entry(&udev_device->devlinks_list);
966 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
968 udev_device->devlinks_uptodate = false;
969 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
973 * udev_device_get_properties_list_entry:
974 * @udev_device: udev device
976 * Retrieve the list of key/value device properties of the udev
977 * device. The next list entry can be retrieved with udev_list_entry_next(),
978 * which returns #NULL if no more entries exist. The property name
979 * can be retrieved from the list entry by udev_list_get_name(),
980 * the property value by udev_list_get_value().
982 * Returns: the first entry of the property list
984 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
986 if (udev_device == NULL)
988 if (!udev_device->info_loaded) {
989 udev_device_read_uevent_file(udev_device);
990 udev_device_read_db(udev_device);
992 if (!udev_device->devlinks_uptodate) {
993 char symlinks[UTIL_PATH_SIZE];
994 struct udev_list_entry *list_entry;
996 udev_device->devlinks_uptodate = true;
997 list_entry = udev_device_get_devlinks_list_entry(udev_device);
998 if (list_entry != NULL) {
1003 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
1004 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
1005 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
1006 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
1009 if (!udev_device->tags_uptodate) {
1010 udev_device->tags_uptodate = true;
1011 if (udev_device_get_tags_list_entry(udev_device) != NULL) {
1012 char tags[UTIL_PATH_SIZE];
1013 struct udev_list_entry *list_entry;
1018 l = util_strpcpyl(&s, sizeof(tags), ":", NULL);
1019 udev_list_entry_foreach(list_entry, udev_device_get_tags_list_entry(udev_device))
1020 l = util_strpcpyl(&s, l, udev_list_entry_get_name(list_entry), ":", NULL);
1021 udev_device_add_property(udev_device, "TAGS", tags);
1024 return udev_list_get_entry(&udev_device->properties_list);
1028 * udev_device_get_driver:
1029 * @udev_device: udev device
1031 * Returns: the driver string, or #NULL if there is no driver attached.
1033 const char *udev_device_get_driver(struct udev_device *udev_device)
1035 char driver[UTIL_NAME_SIZE];
1037 if (udev_device == NULL)
1039 if (!udev_device->driver_set) {
1040 udev_device->driver_set = true;
1041 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1042 udev_device->driver = strdup(driver);
1044 return udev_device->driver;
1048 * udev_device_get_devnum:
1049 * @udev_device: udev device
1051 * Returns: the device major/minor number.
1053 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1055 if (udev_device == NULL)
1056 return makedev(0, 0);
1057 if (!udev_device->info_loaded)
1058 udev_device_read_uevent_file(udev_device);
1059 return udev_device->devnum;
1063 * udev_device_get_action:
1064 * @udev_device: udev device
1066 * This is only valid if the device was received through a monitor. Devices read from
1067 * sys do not have an action string. Usual actions are: add, remove, change, online,
1070 * Returns: the kernel action value, or #NULL if there is no action value available.
1072 const char *udev_device_get_action(struct udev_device *udev_device)
1074 if (udev_device == NULL)
1076 return udev_device->action;
1080 * udev_device_get_devnum:
1081 * @udev_device: udev device
1083 * This is only valid if the device was received through a monitor. Devices read from
1084 * sys do not have a sequence number.
1086 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1088 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1090 if (udev_device == NULL)
1092 return udev_device->seqnum;
1096 * udev_device_get_sysattr_value:
1097 * @udev_device: udev device
1098 * @sysattr: attribute name
1100 * The retrieved value is cached in the device. Repeated calls will return the same
1101 * value and not open the attribute again.
1103 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1105 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1107 struct udev_list_entry *list_entry;
1108 char path[UTIL_PATH_SIZE];
1110 struct stat statbuf;
1113 const char *val = NULL;
1115 if (udev_device == NULL)
1117 if (sysattr == NULL)
1120 /* look for possibly already cached result */
1121 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1122 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1123 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1124 sysattr, udev_list_entry_get_value(list_entry));
1125 return udev_list_entry_get_value(list_entry);
1129 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1130 if (lstat(path, &statbuf) != 0) {
1131 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1132 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1136 if (S_ISLNK(statbuf.st_mode)) {
1137 char target[UTIL_NAME_SIZE];
1141 /* some core links return the last element of the target path */
1142 if (strcmp(sysattr, "driver") != 0 &&
1143 strcmp(sysattr, "subsystem") != 0 &&
1144 strcmp(sysattr, "module") != 0)
1147 len = readlink(path, target, sizeof(target));
1148 if (len <= 0 || len == sizeof(target))
1152 pos = strrchr(target, '/');
1155 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1156 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1157 val = udev_list_entry_get_value(list_entry);
1163 /* skip directories */
1164 if (S_ISDIR(statbuf.st_mode))
1167 /* skip non-readable files */
1168 if ((statbuf.st_mode & S_IRUSR) == 0)
1171 /* read attribute value */
1172 fd = open(path, O_RDONLY|O_CLOEXEC);
1174 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1177 size = read(fd, value, sizeof(value));
1181 if (size == sizeof(value))
1184 /* got a valid value, store it in cache and return it */
1186 util_remove_trailing_chars(value, '\n');
1187 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1188 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1189 val = udev_list_entry_get_value(list_entry);
1194 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1199 free(udev_device->syspath);
1200 udev_device->syspath = strdup(syspath);
1201 if (udev_device->syspath == NULL)
1203 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1204 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1206 pos = strrchr(udev_device->syspath, '/');
1209 udev_device->sysname = strdup(&pos[1]);
1210 if (udev_device->sysname == NULL)
1213 /* some devices have '!' in their name, change that to '/' */
1215 while (udev_device->sysname[len] != '\0') {
1216 if (udev_device->sysname[len] == '!')
1217 udev_device->sysname[len] = '/';
1221 /* trailing number */
1222 while (len > 0 && isdigit(udev_device->sysname[--len]))
1223 udev_device->sysnum = &udev_device->sysname[len];
1225 /* sysname is completely numeric */
1227 udev_device->sysnum = NULL;
1232 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1234 free(udev_device->subsystem);
1235 udev_device->subsystem = strdup(subsystem);
1236 if (udev_device->subsystem == NULL)
1238 udev_device->subsystem_set = true;
1239 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1243 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1245 free(udev_device->devtype);
1246 udev_device->devtype = strdup(devtype);
1247 if (udev_device->devtype == NULL)
1249 udev_device->devtype_set = true;
1250 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1254 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1256 free(udev_device->devnode);
1257 udev_device->devnode = strdup(devnode);
1258 if (udev_device->devnode == NULL)
1260 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1264 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1266 struct udev_list_entry *list_entry;
1268 udev_device->devlinks_uptodate = false;
1269 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1270 if (list_entry == NULL)
1273 udev_list_entry_set_flags(list_entry, 1);
1277 const char *udev_device_get_id_filename(struct udev_device *udev_device)
1279 if (udev_device->id_filename == NULL) {
1280 if (udev_device_get_subsystem(udev_device) == NULL)
1283 if (major(udev_device_get_devnum(udev_device)) > 0) {
1284 /* use dev_t -- b259:131072, c254:0 */
1285 if (asprintf(&udev_device->id_filename, "%c%u:%u",
1286 strcmp(udev_device_get_subsystem(udev_device), "block") == 0 ? 'b' : 'c',
1287 major(udev_device_get_devnum(udev_device)),
1288 minor(udev_device_get_devnum(udev_device))) < 0)
1289 udev_device->id_filename = NULL;
1290 } else if (strcmp(udev_device_get_subsystem(udev_device), "net") == 0) {
1291 /* use netdev ifindex -- n3 */
1292 if (asprintf(&udev_device->id_filename, "n%u", udev_device_get_ifindex(udev_device)) < 0)
1293 udev_device->id_filename = NULL;
1296 * use $subsys:$syname -- pci:0000:00:1f.2
1297 * sysname() has '!' translated, get it from devpath
1299 const char *sysname;
1300 sysname = strrchr(udev_device->devpath, '/');
1301 if (sysname == NULL)
1303 sysname = &sysname[1];
1304 if (asprintf(&udev_device->id_filename, "+%s:%s", udev_device_get_subsystem(udev_device), sysname) < 0)
1305 udev_device->id_filename = NULL;
1308 return udev_device->id_filename;
1311 int udev_device_add_tag(struct udev_device *udev_device, const char *tag)
1313 if (strchr(tag, ':') != NULL || strchr(tag, ' ') != NULL)
1315 udev_device->tags_uptodate = false;
1316 if (udev_list_entry_add(udev_device->udev, &udev_device->tags_list, tag, NULL, 1, 0) != NULL)
1321 void udev_device_cleanup_tags_list(struct udev_device *udev_device)
1323 udev_device->tags_uptodate = false;
1324 udev_list_cleanup_entries(udev_device->udev, &udev_device->tags_list);
1328 * udev_device_get_tags_list_entry:
1329 * @udev_device: udev device
1331 * Retrieve the list of tags attached to the udev device. The next
1332 * list entry can be retrieved with udev_list_entry_next(),
1333 * which returns #NULL if no more entries exist. The tag string
1334 * can be retrieved from the list entry by udev_list_get_name().
1336 * Returns: the first entry of the tag list
1338 struct udev_list_entry *udev_device_get_tags_list_entry(struct udev_device *udev_device)
1340 if (udev_device == NULL)
1342 return udev_list_get_entry(&udev_device->tags_list);
1345 int udev_device_has_tag(struct udev_device *udev_device, const char *tag)
1347 struct udev_list_entry *list_entry;
1349 if (!udev_device->info_loaded)
1350 udev_device_read_db(udev_device);
1351 list_entry = udev_device_get_tags_list_entry(udev_device);
1352 list_entry = udev_list_entry_get_by_name(list_entry, tag);
1353 if (list_entry != NULL)
1358 #define ENVP_SIZE 128
1359 #define MONITOR_BUF_SIZE 4096
1360 static int update_envp_monitor_buf(struct udev_device *udev_device)
1362 struct udev_list_entry *list_entry;
1367 /* monitor buffer of property strings */
1368 free(udev_device->monitor_buf);
1369 udev_device->monitor_buf_len = 0;
1370 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1371 if (udev_device->monitor_buf == NULL)
1374 /* envp array, strings will point into monitor buffer */
1375 if (udev_device->envp == NULL)
1376 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1377 if (udev_device->envp == NULL)
1381 s = udev_device->monitor_buf;
1382 l = MONITOR_BUF_SIZE;
1383 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1386 key = udev_list_entry_get_name(list_entry);
1387 /* skip private variables */
1391 /* add string to envp array */
1392 udev_device->envp[i++] = s;
1393 if (i+1 >= ENVP_SIZE)
1396 /* add property string to monitor buffer */
1397 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1400 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1404 udev_device->envp[i] = NULL;
1405 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1406 udev_device->envp_uptodate = true;
1407 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1408 i, udev_device->monitor_buf_len);
1412 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1414 if (!udev_device->envp_uptodate)
1415 if (update_envp_monitor_buf(udev_device) != 0)
1417 return udev_device->envp;
1420 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1422 if (!udev_device->envp_uptodate)
1423 if (update_envp_monitor_buf(udev_device) != 0)
1425 *buf = udev_device->monitor_buf;
1426 return udev_device->monitor_buf_len;
1429 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1431 free(udev_device->action);
1432 udev_device->action = strdup(action);
1433 if (udev_device->action == NULL)
1435 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1439 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1441 free(udev_device->driver);
1442 udev_device->driver = strdup(driver);
1443 if (udev_device->driver == NULL)
1445 udev_device->driver_set = true;
1446 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1450 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1452 return udev_device->devpath_old;
1455 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1460 free(udev_device->devpath_old);
1461 udev_device->devpath_old = strdup(devpath_old);
1462 if (udev_device->devpath_old == NULL)
1464 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1466 pos = strrchr(udev_device->devpath_old, '/');
1469 udev_device->sysname_old = strdup(&pos[1]);
1470 if (udev_device->sysname_old == NULL)
1473 /* some devices have '!' in their name, change that to '/' */
1475 while (udev_device->sysname_old[len] != '\0') {
1476 if (udev_device->sysname_old[len] == '!')
1477 udev_device->sysname_old[len] = '/';
1483 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1485 if (udev_device == NULL)
1487 return udev_device->sysname_old;
1490 const char *udev_device_get_knodename(struct udev_device *udev_device)
1492 return udev_device->knodename;
1495 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1497 free(udev_device->knodename);
1498 udev_device->knodename = strdup(knodename);
1499 if (udev_device->knodename == NULL)
1501 /* do not overwrite the udev property with the kernel property */
1502 if (udev_device->devnode == NULL)
1503 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1507 int udev_device_get_timeout(struct udev_device *udev_device)
1509 return udev_device->timeout;
1512 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1514 udev_device->timeout = timeout;
1517 int udev_device_get_event_timeout(struct udev_device *udev_device)
1519 if (!udev_device->info_loaded)
1520 udev_device_read_db(udev_device);
1521 return udev_device->event_timeout;
1524 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1528 udev_device->event_timeout = event_timeout;
1529 snprintf(num, sizeof(num), "%u", event_timeout);
1530 udev_device_add_property(udev_device, "TIMEOUT", num);
1534 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1538 udev_device->seqnum = seqnum;
1539 snprintf(num, sizeof(num), "%llu", seqnum);
1540 udev_device_add_property(udev_device, "SEQNUM", num);
1544 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1548 udev_device->devnum = devnum;
1550 snprintf(num, sizeof(num), "%u", major(devnum));
1551 udev_device_add_property(udev_device, "MAJOR", num);
1552 snprintf(num, sizeof(num), "%u", minor(devnum));
1553 udev_device_add_property(udev_device, "MINOR", num);
1557 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1559 if (!udev_device->info_loaded)
1560 udev_device_read_db(udev_device);
1561 return udev_device->devlink_priority;
1564 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1566 udev_device->devlink_priority = prio;
1570 int udev_device_get_watch_handle(struct udev_device *udev_device)
1572 if (!udev_device->info_loaded)
1573 udev_device_read_db(udev_device);
1574 return udev_device->watch_handle;
1577 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1579 udev_device->watch_handle = handle;
1583 int udev_device_get_ifindex(struct udev_device *udev_device)
1585 return udev_device->ifindex;
1588 int udev_device_set_ifindex(struct udev_device *udev_device, int ifindex)
1592 udev_device->ifindex = ifindex;
1593 snprintf(num, sizeof(num), "%u", ifindex);
1594 udev_device_add_property(udev_device, "IFINDEX", num);