2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2010 Kay Sievers <kay.sievers@vrfy.org>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
25 #include "libudev-private.h"
28 * SECTION:libudev-device
29 * @short_description: kernel sys devices
31 * Representation of kernel sys devices. Devices are uniquely identified
32 * by their syspath, every device has exactly one path in the kernel sys
33 * filesystem. Devices usually belong to a kernel subsystem, and and have
34 * a unique name inside that subsystem.
40 * Opaque object representing one kernel sys device.
44 struct udev_device *parent_device;
59 size_t monitor_buf_len;
60 struct udev_list_node devlinks_list;
61 struct udev_list_node properties_list;
62 struct udev_list_node sysattr_list;
63 unsigned long long int seqnum;
71 unsigned int parent_set:1;
72 unsigned int subsystem_set:1;
73 unsigned int devtype_set:1;
74 unsigned int devlinks_uptodate:1;
75 unsigned int envp_uptodate:1;
76 unsigned int driver_set:1;
77 unsigned int info_loaded:1;
78 unsigned int ignore_remove:1;
81 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
83 udev_device->envp_uptodate = 0;
85 struct udev_list_entry *list_entry;
87 list_entry = udev_device_get_properties_list_entry(udev_device);
88 list_entry = udev_list_entry_get_by_name(list_entry, key);
89 if (list_entry != NULL)
90 udev_list_entry_delete(list_entry);
93 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
96 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
98 char name[UTIL_LINE_SIZE];
101 util_strscpy(name, sizeof(name), property);
102 val = strchr(name, '=');
109 return udev_device_add_property(udev_device, name, val);
113 * parse property string, and if needed, update internal values accordingly
115 * udev_device_add_property_from_string_parse_finish() needs to be
116 * called after adding properties, and its return value checked
118 * udev_device_set_info_loaded() needs to be set, to avoid trying
119 * to use a device without a DEVPATH set
121 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
123 if (strncmp(property, "DEVPATH=", 8) == 0) {
124 char path[UTIL_PATH_SIZE];
126 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
127 udev_device_set_syspath(udev_device, path);
128 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
129 udev_device_set_subsystem(udev_device, &property[10]);
130 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
131 udev_device_set_devtype(udev_device, &property[8]);
132 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
133 if (property[8] == '/')
134 udev_device_set_devnode(udev_device, &property[8]);
136 udev_device_set_knodename(udev_device, &property[8]);
137 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
138 char devlinks[UTIL_PATH_SIZE];
142 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
144 next = strchr(slink, ' ');
145 while (next != NULL) {
147 udev_device_add_devlink(udev_device, slink, 0);
149 next = strchr(slink, ' ');
151 if (slink[0] != '\0')
152 udev_device_add_devlink(udev_device, slink, 0);
153 } else if (strncmp(property, "DRIVER=", 7) == 0) {
154 udev_device_set_driver(udev_device, &property[7]);
155 } else if (strncmp(property, "ACTION=", 7) == 0) {
156 udev_device_set_action(udev_device, &property[7]);
157 } else if (strncmp(property, "MAJOR=", 6) == 0) {
158 udev_device->maj = strtoull(&property[6], NULL, 10);
159 } else if (strncmp(property, "MINOR=", 6) == 0) {
160 udev_device->min = strtoull(&property[6], NULL, 10);
161 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
162 udev_device_set_devpath_old(udev_device, &property[12]);
163 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
164 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
165 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
166 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
168 udev_device_add_property_from_string(udev_device, property);
172 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
174 if (udev_device->maj > 0)
175 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
176 udev_device->maj = 0;
177 udev_device->min = 0;
179 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
185 * udev_device_get_property_value:
186 * @udev_device: udev device
187 * @key: property name
189 * Returns: the value of a device property, or #NULL if there is no such property.
191 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
193 struct udev_list_entry *list_entry;
195 if (udev_device == NULL)
200 list_entry = udev_device_get_properties_list_entry(udev_device);
201 list_entry = udev_list_entry_get_by_name(list_entry, key);
202 return udev_list_entry_get_value(list_entry);
205 int udev_device_read_db(struct udev_device *udev_device)
208 char filename[UTIL_PATH_SIZE];
209 char line[UTIL_LINE_SIZE];
212 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/",
213 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
215 if (lstat(filename, &stats) != 0) {
216 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
219 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
220 char target[UTIL_PATH_SIZE];
221 char devnode[UTIL_PATH_SIZE];
225 target_len = readlink(filename, target, sizeof(target));
227 target[target_len] = '\0';
229 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
233 next = strchr(target, ' ');
238 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
239 udev_device_set_devnode(udev_device, devnode);
240 while (next != NULL) {
241 char devlink[UTIL_PATH_SIZE];
245 next = strchr(next, ' ');
250 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
251 udev_device_add_devlink(udev_device, devlink, 0);
253 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
257 f = fopen(filename, "r");
259 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
262 while (fgets(line, sizeof(line), f)) {
273 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
274 udev_device_set_devnode(udev_device, filename);
277 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
278 udev_device_add_devlink(udev_device, filename, 0);
281 udev_device_set_devlink_priority(udev_device, atoi(val));
284 udev_device_set_event_timeout(udev_device, atoi(val));
287 udev_device_set_ignore_remove(udev_device, atoi(val));
290 udev_device_add_property_from_string(udev_device, val);
293 udev_device_set_watch_handle(udev_device, atoi(val));
299 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
303 int udev_device_read_uevent_file(struct udev_device *udev_device)
305 char filename[UTIL_PATH_SIZE];
307 char line[UTIL_LINE_SIZE];
311 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
312 f = fopen(filename, "r");
316 while (fgets(line, sizeof(line), f)) {
319 pos = strchr(line, '\n');
324 if (strncmp(line, "DEVTYPE=", 8) == 0)
325 udev_device_set_devtype(udev_device, &line[8]);
326 else if (strncmp(line, "MAJOR=", 6) == 0)
327 maj = strtoull(&line[6], NULL, 10);
328 else if (strncmp(line, "MINOR=", 6) == 0)
329 min = strtoull(&line[6], NULL, 10);
330 else if (strncmp(line, "DEVNAME=", 8) == 0)
331 udev_device_set_knodename(udev_device, &line[8]);
333 udev_device_add_property_from_string(udev_device, line);
336 udev_device->devnum = makedev(maj, min);
342 static void device_load_info(struct udev_device *device)
344 device->info_loaded = 1;
345 udev_device_read_uevent_file(device);
346 udev_device_read_db(device);
349 void udev_device_set_info_loaded(struct udev_device *device)
351 device->info_loaded = 1;
354 struct udev_device *udev_device_new(struct udev *udev)
356 struct udev_device *udev_device;
357 struct udev_list_entry *list_entry;
362 udev_device = calloc(1, sizeof(struct udev_device));
363 if (udev_device == NULL)
365 udev_device->refcount = 1;
366 udev_device->udev = udev;
367 udev_list_init(&udev_device->devlinks_list);
368 udev_list_init(&udev_device->properties_list);
369 udev_list_init(&udev_device->sysattr_list);
370 udev_device->event_timeout = -1;
371 udev_device->watch_handle = -1;
372 /* copy global properties */
373 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
374 udev_device_add_property(udev_device,
375 udev_list_entry_get_name(list_entry),
376 udev_list_entry_get_value(list_entry));
377 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
382 * udev_device_new_from_syspath:
383 * @udev: udev library context
384 * @syspath: sys device path including sys directory
386 * Create new udev device, and fill in information from the sys
387 * device and the udev database entry. The syspath is the absolute
388 * path to the device, including the sys mount point.
390 * The initial refcount is 1, and needs to be decremented to
391 * release the resources of the udev device.
393 * Returns: a new udev device, or #NULL, if it does not exist
395 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
399 char path[UTIL_PATH_SIZE];
402 struct udev_device *udev_device;
409 /* path starts in sys */
410 len = strlen(udev_get_sys_path(udev));
411 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
412 info(udev, "not in sys :%s\n", syspath);
416 /* path is not a root directory */
417 subdir = &syspath[len+1];
418 pos = strrchr(subdir, '/');
419 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
420 dbg(udev, "not a subdir :%s\n", syspath);
424 /* resolve possible symlink to real path */
425 util_strscpy(path, sizeof(path), syspath);
426 util_resolve_sys_link(udev, path, sizeof(path));
428 if (strncmp(&syspath[len], "/devices/", 9) == 0) {
429 char file[UTIL_PATH_SIZE];
431 /* all "devices" require a "uevent" file */
432 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
433 if (stat(file, &statbuf) != 0) {
434 dbg(udev, "not a device: %s\n", syspath);
438 /* everything else just needs to be a directory */
439 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
440 dbg(udev, "directory not found: %s\n", syspath);
445 udev_device = udev_device_new(udev);
446 if (udev_device == NULL)
449 udev_device_set_syspath(udev_device, path);
450 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
456 * udev_device_new_from_devnum:
457 * @udev: udev library context
458 * @type: char or block device
459 * @devnum: device major/minor number
461 * Create new udev device, and fill in information from the sys
462 * device and the udev database entry. The device is looked-up
463 * by its major/minor number and type. Character and block device
464 * numbers are not unique across the two types.
466 * The initial refcount is 1, and needs to be decremented to
467 * release the resources of the udev device.
469 * Returns: a new udev device, or #NULL, if it does not exist
471 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
473 char path[UTIL_PATH_SIZE];
474 const char *type_str;
478 else if (type == 'c')
483 /* use /sys/dev/{block,char}/<maj>:<min> link */
484 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
485 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
486 return udev_device_new_from_syspath(udev, path);
490 * udev_device_new_from_subsystem_sysname:
491 * @udev: udev library context
492 * @subsystem: the subsystem of the device
493 * @sysname: the name of the device
495 * Create new udev device, and fill in information from the sys device
496 * and the udev database entry. The device is looked up by the subsystem
497 * and name string of the device, like "mem" / "zero", or "block" / "sda".
499 * The initial refcount is 1, and needs to be decremented to
500 * release the resources of the udev device.
502 * Returns: a new udev device, or #NULL, if it does not exist
504 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
506 char path_full[UTIL_PATH_SIZE];
512 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
514 if (strcmp(subsystem, "subsystem") == 0) {
515 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
516 if (stat(path_full, &statbuf) == 0)
519 util_strscpyl(path, l, "/bus/", sysname, NULL);
520 if (stat(path_full, &statbuf) == 0)
523 util_strscpyl(path, l, "/class/", sysname, NULL);
524 if (stat(path_full, &statbuf) == 0)
529 if (strcmp(subsystem, "module") == 0) {
530 util_strscpyl(path, l, "/module/", sysname, NULL);
531 if (stat(path_full, &statbuf) == 0)
536 if (strcmp(subsystem, "drivers") == 0) {
537 char subsys[UTIL_NAME_SIZE];
540 util_strscpy(subsys, sizeof(subsys), sysname);
541 driver = strchr(subsys, ':');
542 if (driver != NULL) {
546 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
547 if (stat(path_full, &statbuf) == 0)
550 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
551 if (stat(path_full, &statbuf) == 0)
557 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
558 if (stat(path_full, &statbuf) == 0)
561 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
562 if (stat(path_full, &statbuf) == 0)
565 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
566 if (stat(path_full, &statbuf) == 0)
571 return udev_device_new_from_syspath(udev, path_full);
575 * udev_device_new_from_environment
576 * @udev: udev library context
578 * Create new udev device, and fill in information from the
579 * current process environment. This only works reliable if
580 * the process is called from a udev rule. It is usually used
581 * for tools executed from IMPORT= rules.
583 * The initial refcount is 1, and needs to be decremented to
584 * release the resources of the udev device.
586 * Returns: a new udev device, or #NULL, if it does not exist
588 struct udev_device *udev_device_new_from_environment(struct udev *udev)
591 struct udev_device *udev_device;
593 udev_device = udev_device_new(udev);
594 if (udev_device == NULL)
596 udev_device_set_info_loaded(udev_device);
598 for (i = 0; environ[i] != NULL; i++)
599 udev_device_add_property_from_string_parse(udev_device, environ[i]);
601 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
602 info(udev, "missing values, invalid device\n");
603 udev_device_unref(udev_device);
610 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
612 struct udev_device *udev_device_parent = NULL;
613 char path[UTIL_PATH_SIZE];
616 util_strscpy(path, sizeof(path), udev_device->syspath);
617 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
621 pos = strrchr(subdir, '/');
622 if (pos == NULL || pos < &subdir[2])
625 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
626 if (udev_device_parent != NULL)
627 return udev_device_parent;
633 * udev_device_get_parent:
634 * @udev_device: the device to start searching from
636 * Find the next parent device, and fill in information from the sys
637 * device and the udev database entry.
639 * The returned the device is not referenced. It is attached to the
640 * child device, and will be cleaned up when the child device
643 * It is not necessarily just the upper level directory, empty or not
644 * recognized sys directories are ignored.
646 * It can be called as many times as needed, without caring about
649 * Returns: a new udev device, or #NULL, if it no parent exist.
651 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
653 if (udev_device == NULL)
655 if (!udev_device->parent_set) {
656 udev_device->parent_set = 1;
657 udev_device->parent_device = device_new_from_parent(udev_device);
659 if (udev_device->parent_device != NULL)
660 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
661 return udev_device->parent_device;
665 * udev_device_get_parent_with_subsystem_devtype:
666 * @udev_device: udev device to start searching from
667 * @subsystem: the subsystem of the device
668 * @devtype: the type (DEVTYPE) of the device
670 * Find the next parent device, with a matching subsystem and devtype
671 * value, and fill in information from the sys device and the udev
674 * If devtype is #NULL, only subsystem is checked, and any devtype will
677 * The returned the device is not referenced. It is attached to the
678 * child device, and will be cleaned up when the child device
681 * It can be called as many times as needed, without caring about
684 * Returns: a new udev device, or #NULL if no matching parent exists.
686 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
688 struct udev_device *parent;
690 if (subsystem == NULL)
693 parent = udev_device_get_parent(udev_device);
694 while (parent != NULL) {
695 const char *parent_subsystem;
696 const char *parent_devtype;
698 parent_subsystem = udev_device_get_subsystem(parent);
699 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
702 parent_devtype = udev_device_get_devtype(parent);
703 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
706 parent = udev_device_get_parent(parent);
712 * udev_device_get_udev:
713 * @udev_device: udev device
715 * Retrieve the udev library context the device was created with.
717 * Returns: the udev library context
719 struct udev *udev_device_get_udev(struct udev_device *udev_device)
721 if (udev_device == NULL)
723 return udev_device->udev;
728 * @udev_device: udev device
730 * Take a reference of a udev device.
732 * Returns: the passed udev device
734 struct udev_device *udev_device_ref(struct udev_device *udev_device)
736 if (udev_device == NULL)
738 udev_device->refcount++;
744 * @udev_device: udev device
746 * Drop a reference of a udev device. If the refcount reaches zero,
747 * the resources of the device will be released.
750 void udev_device_unref(struct udev_device *udev_device)
752 if (udev_device == NULL)
754 udev_device->refcount--;
755 if (udev_device->refcount > 0)
757 if (udev_device->parent_device != NULL)
758 udev_device_unref(udev_device->parent_device);
759 free(udev_device->syspath);
760 free(udev_device->sysname);
761 free(udev_device->devnode);
762 free(udev_device->subsystem);
763 free(udev_device->devtype);
764 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
765 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
766 free(udev_device->action);
767 free(udev_device->driver);
768 free(udev_device->devpath_old);
769 free(udev_device->sysname_old);
770 free(udev_device->knodename);
771 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
772 free(udev_device->envp);
773 free(udev_device->monitor_buf);
774 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
779 * udev_device_get_devpath:
780 * @udev_device: udev device
782 * Retrieve the kernel devpath value of the udev device. The path
783 * does not contain the sys mount point, and starts with a '/'.
785 * Returns: the devpath of the udev device
787 const char *udev_device_get_devpath(struct udev_device *udev_device)
789 if (udev_device == NULL)
791 return udev_device->devpath;
795 * udev_device_get_syspath:
796 * @udev_device: udev device
798 * Retrieve the sys path of the udev device. The path is an
799 * absolute path and starts with the sys mount point.
801 * Returns: the sys path of the udev device
803 const char *udev_device_get_syspath(struct udev_device *udev_device)
805 if (udev_device == NULL)
807 return udev_device->syspath;
811 * udev_device_get_sysname:
812 * @udev_device: udev device
814 * Returns: the sys name of the device device
816 const char *udev_device_get_sysname(struct udev_device *udev_device)
818 if (udev_device == NULL)
820 return udev_device->sysname;
824 * udev_device_get_sysnum:
825 * @udev_device: udev device
827 * Returns: the trailing number of of the device name
829 const char *udev_device_get_sysnum(struct udev_device *udev_device)
831 if (udev_device == NULL)
833 return udev_device->sysnum;
837 * udev_device_get_devnode:
838 * @udev_device: udev device
840 * Retrieve the device node file name belonging to the udev device.
841 * The path is an absolute path, and starts with the device directory.
843 * Returns: the device node file name of the udev device, or #NULL if no device node exists
845 const char *udev_device_get_devnode(struct udev_device *udev_device)
847 if (udev_device == NULL)
849 if (!udev_device->info_loaded)
850 device_load_info(udev_device);
851 return udev_device->devnode;
855 * udev_device_get_subsystem:
856 * @udev_device: udev device
858 * Retrieve the subsystem string of the udev device. The string does not
861 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
863 const char *udev_device_get_subsystem(struct udev_device *udev_device)
865 char subsystem[UTIL_NAME_SIZE];
867 if (udev_device == NULL)
869 if (!udev_device->subsystem_set) {
870 udev_device->subsystem_set = 1;
871 /* read "subsystem" link */
872 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
873 udev_device_set_subsystem(udev_device, subsystem);
874 return udev_device->subsystem;
877 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
878 udev_device_set_subsystem(udev_device, "module");
879 return udev_device->subsystem;
881 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
882 udev_device_set_subsystem(udev_device, "drivers");
883 return udev_device->subsystem;
885 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
886 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
887 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
888 udev_device_set_subsystem(udev_device, "subsystem");
889 return udev_device->subsystem;
892 return udev_device->subsystem;
896 * udev_device_get_devtype:
897 * @udev_device: udev device
899 * Retrieve the devtype string of the udev device.
901 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
903 const char *udev_device_get_devtype(struct udev_device *udev_device)
905 if (udev_device == NULL)
907 if (!udev_device->devtype_set) {
908 udev_device->devtype_set = 1;
909 if (!udev_device->info_loaded)
910 udev_device_read_uevent_file(udev_device);
912 return udev_device->devtype;
916 * udev_device_get_devlinks_list_entry:
917 * @udev_device: udev device
919 * Retrieve the list of device links pointing to the device file of
920 * the udev device. The next list entry can be retrieved with
921 * udev_list_entry_next(), which returns #NULL if no more entries exist.
922 * The devlink path can be retrieved from the list entry by
923 * udev_list_entry_get_name(). The path is an absolute path, and starts with
924 * the device directory.
926 * Returns: the first entry of the device node link list
928 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
930 if (udev_device == NULL)
932 if (!udev_device->info_loaded)
933 device_load_info(udev_device);
934 return udev_list_get_entry(&udev_device->devlinks_list);
937 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
939 udev_device->devlinks_uptodate = 0;
940 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
944 * udev_device_get_properties_list_entry:
945 * @udev_device: udev device
947 * Retrieve the list of key/value device properties of the udev
948 * device. The next list entry can be retrieved with udev_list_entry_next(),
949 * which returns #NULL if no more entries exist. The property name
950 * can be retrieved from the list entry by udev_list_get_name(),
951 * the property value by udev_list_get_value().
953 * Returns: the first entry of the property list
955 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
957 if (udev_device == NULL)
959 if (!udev_device->info_loaded)
960 device_load_info(udev_device);
961 if (!udev_device->devlinks_uptodate) {
962 char symlinks[UTIL_PATH_SIZE];
963 struct udev_list_entry *list_entry;
965 udev_device->devlinks_uptodate = 1;
966 list_entry = udev_device_get_devlinks_list_entry(udev_device);
967 if (list_entry != NULL) {
972 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
973 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
974 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
975 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
978 return udev_list_get_entry(&udev_device->properties_list);
982 * udev_device_get_driver:
983 * @udev_device: udev device
985 * Returns: the driver string, or #NULL if there is no driver attached.
987 const char *udev_device_get_driver(struct udev_device *udev_device)
989 char driver[UTIL_NAME_SIZE];
991 if (udev_device == NULL)
993 if (!udev_device->driver_set) {
994 udev_device->driver_set = 1;
995 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
996 udev_device->driver = strdup(driver);
998 return udev_device->driver;
1002 * udev_device_get_devnum:
1003 * @udev_device: udev device
1005 * Returns: the device major/minor number.
1007 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1009 if (udev_device == NULL)
1010 return makedev(0, 0);
1011 if (!udev_device->info_loaded)
1012 device_load_info(udev_device);
1013 return udev_device->devnum;
1017 * udev_device_get_action:
1018 * @udev_device: udev device
1020 * This is only valid if the device was received through a monitor. Devices read from
1021 * sys do not have an action string. Usual actions are: add, remove, change, online,
1024 * Returns: the kernel action value, or #NULL if there is no action value available.
1026 const char *udev_device_get_action(struct udev_device *udev_device)
1028 if (udev_device == NULL)
1030 return udev_device->action;
1034 * udev_device_get_devnum:
1035 * @udev_device: udev device
1037 * This is only valid if the device was received through a monitor. Devices read from
1038 * sys do not have a sequence number.
1040 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1042 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1044 if (udev_device == NULL)
1046 return udev_device->seqnum;
1050 * udev_device_get_sysattr_value:
1051 * @udev_device: udev device
1052 * @sysattr: attribute name
1054 * The retrieved value is cached in the device. Repeated calls will return the same
1055 * value and not open the attribute again.
1057 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1059 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1061 struct udev_list_entry *list_entry;
1062 char path[UTIL_PATH_SIZE];
1064 struct stat statbuf;
1067 const char *val = NULL;
1069 if (udev_device == NULL)
1071 if (sysattr == NULL)
1074 /* look for possibly already cached result */
1075 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1076 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1077 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1078 sysattr, udev_list_entry_get_value(list_entry));
1079 return udev_list_entry_get_value(list_entry);
1083 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1084 if (lstat(path, &statbuf) != 0) {
1085 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1086 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1090 if (S_ISLNK(statbuf.st_mode)) {
1091 char target[UTIL_NAME_SIZE];
1095 /* some core links return the last element of the target path */
1096 if (strcmp(sysattr, "driver") != 0 &&
1097 strcmp(sysattr, "subsystem") != 0 &&
1098 strcmp(sysattr, "module") != 0)
1101 len = readlink(path, target, sizeof(target));
1104 pos = strrchr(target, '/');
1107 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1108 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1109 val = udev_list_entry_get_value(list_entry);
1115 /* skip directories */
1116 if (S_ISDIR(statbuf.st_mode))
1119 /* skip non-readable files */
1120 if ((statbuf.st_mode & S_IRUSR) == 0)
1123 /* read attribute value */
1124 fd = open(path, O_RDONLY);
1126 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1129 size = read(fd, value, sizeof(value));
1133 if (size == sizeof(value))
1136 /* got a valid value, store it in cache and return it */
1138 util_remove_trailing_chars(value, '\n');
1139 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1140 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1141 val = udev_list_entry_get_value(list_entry);
1146 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1151 free(udev_device->syspath);
1152 udev_device->syspath = strdup(syspath);
1153 if (udev_device->syspath == NULL)
1155 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1156 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1158 pos = strrchr(udev_device->syspath, '/');
1161 udev_device->sysname = strdup(&pos[1]);
1162 if (udev_device->sysname == NULL)
1165 /* some devices have '!' in their name, change that to '/' */
1167 while (udev_device->sysname[len] != '\0') {
1168 if (udev_device->sysname[len] == '!')
1169 udev_device->sysname[len] = '/';
1173 /* trailing number */
1174 while (len > 0 && isdigit(udev_device->sysname[--len]))
1175 udev_device->sysnum = &udev_device->sysname[len];
1177 /* sysname is completely numeric */
1179 udev_device->sysnum = NULL;
1184 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1186 free(udev_device->subsystem);
1187 udev_device->subsystem = strdup(subsystem);
1188 if (udev_device->subsystem == NULL)
1190 udev_device->subsystem_set = 1;
1191 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1195 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1197 free(udev_device->devtype);
1198 udev_device->devtype = strdup(devtype);
1199 if (udev_device->devtype == NULL)
1201 udev_device->devtype_set = 1;
1202 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1206 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1208 free(udev_device->devnode);
1209 udev_device->devnode = strdup(devnode);
1210 if (devnode == NULL)
1212 if (udev_device->devnode == NULL)
1214 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1218 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1220 struct udev_list_entry *list_entry;
1222 udev_device->devlinks_uptodate = 0;
1223 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1224 if (list_entry == NULL)
1227 udev_list_entry_set_flags(list_entry, 1);
1231 #define ENVP_SIZE 128
1232 #define MONITOR_BUF_SIZE 4096
1233 static int update_envp_monitor_buf(struct udev_device *udev_device)
1235 struct udev_list_entry *list_entry;
1240 /* monitor buffer of property strings */
1241 free(udev_device->monitor_buf);
1242 udev_device->monitor_buf_len = 0;
1243 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1244 if (udev_device->monitor_buf == NULL)
1247 /* envp array, strings will point into monitor buffer */
1248 if (udev_device->envp == NULL)
1249 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1250 if (udev_device->envp == NULL)
1254 s = udev_device->monitor_buf;
1255 l = MONITOR_BUF_SIZE;
1256 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1259 key = udev_list_entry_get_name(list_entry);
1260 /* skip private variables */
1264 /* add string to envp array */
1265 udev_device->envp[i++] = s;
1266 if (i+1 >= ENVP_SIZE)
1269 /* add property string to monitor buffer */
1270 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1273 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1277 udev_device->envp[i] = NULL;
1278 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1279 udev_device->envp_uptodate = 1;
1280 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1281 i, udev_device->monitor_buf_len);
1285 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1287 if (!udev_device->envp_uptodate)
1288 if (update_envp_monitor_buf(udev_device) != 0)
1290 return udev_device->envp;
1293 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1295 if (!udev_device->envp_uptodate)
1296 if (update_envp_monitor_buf(udev_device) != 0)
1298 *buf = udev_device->monitor_buf;
1299 return udev_device->monitor_buf_len;
1302 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1304 free(udev_device->action);
1305 udev_device->action = strdup(action);
1306 if (udev_device->action == NULL)
1308 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1312 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1314 free(udev_device->driver);
1315 udev_device->driver = strdup(driver);
1316 if (udev_device->driver == NULL)
1318 udev_device->driver_set = 1;
1319 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1323 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1325 return udev_device->devpath_old;
1328 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1333 free(udev_device->devpath_old);
1334 udev_device->devpath_old = strdup(devpath_old);
1335 if (udev_device->devpath_old == NULL)
1337 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1339 pos = strrchr(udev_device->devpath_old, '/');
1342 udev_device->sysname_old = strdup(&pos[1]);
1343 if (udev_device->sysname_old == NULL)
1346 /* some devices have '!' in their name, change that to '/' */
1348 while (udev_device->sysname_old[len] != '\0') {
1349 if (udev_device->sysname_old[len] == '!')
1350 udev_device->sysname_old[len] = '/';
1356 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1358 if (udev_device == NULL)
1360 return udev_device->sysname_old;
1363 const char *udev_device_get_knodename(struct udev_device *udev_device)
1365 return udev_device->knodename;
1368 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1370 free(udev_device->knodename);
1371 udev_device->knodename = strdup(knodename);
1372 if (udev_device->knodename == NULL)
1374 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1378 int udev_device_get_timeout(struct udev_device *udev_device)
1380 return udev_device->timeout;
1383 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1385 udev_device->timeout = timeout;
1388 int udev_device_get_event_timeout(struct udev_device *udev_device)
1390 if (!udev_device->info_loaded)
1391 device_load_info(udev_device);
1392 return udev_device->event_timeout;
1395 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1397 udev_device->event_timeout = event_timeout;
1401 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1405 udev_device->seqnum = seqnum;
1406 snprintf(num, sizeof(num), "%llu", seqnum);
1407 udev_device_add_property(udev_device, "SEQNUM", num);
1411 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1415 udev_device->devnum = devnum;
1417 snprintf(num, sizeof(num), "%u", major(devnum));
1418 udev_device_add_property(udev_device, "MAJOR", num);
1419 snprintf(num, sizeof(num), "%u", minor(devnum));
1420 udev_device_add_property(udev_device, "MINOR", num);
1424 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1426 if (!udev_device->info_loaded)
1427 device_load_info(udev_device);
1428 return udev_device->devlink_priority;
1431 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1433 udev_device->devlink_priority = prio;
1437 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1439 if (!udev_device->info_loaded)
1440 device_load_info(udev_device);
1441 return udev_device->ignore_remove;
1444 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1446 udev_device->ignore_remove = ignore;
1450 int udev_device_get_watch_handle(struct udev_device *udev_device)
1452 if (!udev_device->info_loaded)
1453 device_load_info(udev_device);
1454 return udev_device->watch_handle;
1457 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1459 udev_device->watch_handle = handle;