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;
66 int num_fake_partitions;
72 unsigned int parent_set:1;
73 unsigned int subsystem_set:1;
74 unsigned int devtype_set:1;
75 unsigned int devlinks_uptodate:1;
76 unsigned int envp_uptodate:1;
77 unsigned int driver_set:1;
78 unsigned int info_loaded:1;
79 unsigned int ignore_remove:1;
82 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
84 udev_device->envp_uptodate = 0;
86 struct udev_list_entry *list_entry;
88 list_entry = udev_device_get_properties_list_entry(udev_device);
89 list_entry = udev_list_entry_get_by_name(list_entry, key);
90 if (list_entry != NULL)
91 udev_list_entry_delete(list_entry);
94 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
97 static struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
99 char name[UTIL_LINE_SIZE];
102 util_strscpy(name, sizeof(name), property);
103 val = strchr(name, '=');
110 return udev_device_add_property(udev_device, name, val);
114 * parse property string, and if needed, update internal values accordingly
116 * udev_device_add_property_from_string_parse_finish() needs to be
117 * called after adding properties, and its return value checked
119 * udev_device_set_info_loaded() needs to be set, to avoid trying
120 * to use a device without a DEVPATH set
122 void udev_device_add_property_from_string_parse(struct udev_device *udev_device, const char *property)
124 if (strncmp(property, "DEVPATH=", 8) == 0) {
125 char path[UTIL_PATH_SIZE];
127 util_strscpyl(path, sizeof(path), udev_get_sys_path(udev_device->udev), &property[8], NULL);
128 udev_device_set_syspath(udev_device, path);
129 } else if (strncmp(property, "SUBSYSTEM=", 10) == 0) {
130 udev_device_set_subsystem(udev_device, &property[10]);
131 } else if (strncmp(property, "DEVTYPE=", 8) == 0) {
132 udev_device_set_devtype(udev_device, &property[8]);
133 } else if (strncmp(property, "DEVNAME=", 8) == 0) {
134 if (property[8] == '/')
135 udev_device_set_devnode(udev_device, &property[8]);
137 udev_device_set_knodename(udev_device, &property[8]);
138 } else if (strncmp(property, "DEVLINKS=", 9) == 0) {
139 char devlinks[UTIL_PATH_SIZE];
143 util_strscpy(devlinks, sizeof(devlinks), &property[9]);
145 next = strchr(slink, ' ');
146 while (next != NULL) {
148 udev_device_add_devlink(udev_device, slink, 0);
150 next = strchr(slink, ' ');
152 if (slink[0] != '\0')
153 udev_device_add_devlink(udev_device, slink, 0);
154 } else if (strncmp(property, "DRIVER=", 7) == 0) {
155 udev_device_set_driver(udev_device, &property[7]);
156 } else if (strncmp(property, "ACTION=", 7) == 0) {
157 udev_device_set_action(udev_device, &property[7]);
158 } else if (strncmp(property, "MAJOR=", 6) == 0) {
159 udev_device->maj = strtoull(&property[6], NULL, 10);
160 } else if (strncmp(property, "MINOR=", 6) == 0) {
161 udev_device->min = strtoull(&property[6], NULL, 10);
162 } else if (strncmp(property, "DEVPATH_OLD=", 12) == 0) {
163 udev_device_set_devpath_old(udev_device, &property[12]);
164 } else if (strncmp(property, "SEQNUM=", 7) == 0) {
165 udev_device_set_seqnum(udev_device, strtoull(&property[7], NULL, 10));
166 } else if (strncmp(property, "TIMEOUT=", 8) == 0) {
167 udev_device_set_timeout(udev_device, strtoull(&property[8], NULL, 10));
169 udev_device_add_property_from_string(udev_device, property);
173 int udev_device_add_property_from_string_parse_finish(struct udev_device *udev_device)
175 if (udev_device->maj > 0)
176 udev_device_set_devnum(udev_device, makedev(udev_device->maj, udev_device->min));
177 udev_device->maj = 0;
178 udev_device->min = 0;
180 if (udev_device->devpath == NULL || udev_device->subsystem == NULL)
186 * udev_device_get_property_value:
187 * @udev_device: udev device
188 * @key: property name
190 * Returns: the value of a device property, or #NULL if there is no such property.
192 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
194 struct udev_list_entry *list_entry;
196 if (udev_device == NULL)
201 list_entry = udev_device_get_properties_list_entry(udev_device);
202 list_entry = udev_list_entry_get_by_name(list_entry, key);
203 return udev_list_entry_get_value(list_entry);
206 int udev_device_read_db(struct udev_device *udev_device)
209 char filename[UTIL_PATH_SIZE];
210 char line[UTIL_LINE_SIZE];
213 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/",
214 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
216 if (lstat(filename, &stats) != 0) {
217 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
220 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
221 char target[UTIL_PATH_SIZE];
222 char devnode[UTIL_PATH_SIZE];
226 target_len = readlink(filename, target, sizeof(target));
228 target[target_len] = '\0';
230 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
234 next = strchr(target, ' ');
239 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
240 udev_device_set_devnode(udev_device, devnode);
241 while (next != NULL) {
242 char devlink[UTIL_PATH_SIZE];
246 next = strchr(next, ' ');
251 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
252 udev_device_add_devlink(udev_device, devlink, 0);
254 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
258 f = fopen(filename, "r");
260 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
263 while (fgets(line, sizeof(line), f)) {
274 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
275 udev_device_set_devnode(udev_device, filename);
278 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
279 udev_device_add_devlink(udev_device, filename, 0);
282 udev_device_set_devlink_priority(udev_device, atoi(val));
285 udev_device_set_event_timeout(udev_device, atoi(val));
288 udev_device_set_num_fake_partitions(udev_device, atoi(val));
291 udev_device_set_ignore_remove(udev_device, atoi(val));
294 udev_device_add_property_from_string(udev_device, val);
297 udev_device_set_watch_handle(udev_device, atoi(val));
303 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
307 int udev_device_read_uevent_file(struct udev_device *udev_device)
309 char filename[UTIL_PATH_SIZE];
311 char line[UTIL_LINE_SIZE];
315 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
316 f = fopen(filename, "r");
320 while (fgets(line, sizeof(line), f)) {
323 pos = strchr(line, '\n');
328 if (strncmp(line, "DEVTYPE=", 8) == 0)
329 udev_device_set_devtype(udev_device, &line[8]);
330 else if (strncmp(line, "MAJOR=", 6) == 0)
331 maj = strtoull(&line[6], NULL, 10);
332 else if (strncmp(line, "MINOR=", 6) == 0)
333 min = strtoull(&line[6], NULL, 10);
334 else if (strncmp(line, "DEVNAME=", 8) == 0)
335 udev_device_set_knodename(udev_device, &line[8]);
337 udev_device_add_property_from_string(udev_device, line);
340 udev_device->devnum = makedev(maj, min);
346 static void device_load_info(struct udev_device *device)
348 device->info_loaded = 1;
349 udev_device_read_uevent_file(device);
350 udev_device_read_db(device);
353 void udev_device_set_info_loaded(struct udev_device *device)
355 device->info_loaded = 1;
358 struct udev_device *udev_device_new(struct udev *udev)
360 struct udev_device *udev_device;
361 struct udev_list_entry *list_entry;
366 udev_device = calloc(1, sizeof(struct udev_device));
367 if (udev_device == NULL)
369 udev_device->refcount = 1;
370 udev_device->udev = udev;
371 udev_list_init(&udev_device->devlinks_list);
372 udev_list_init(&udev_device->properties_list);
373 udev_list_init(&udev_device->sysattr_list);
374 udev_device->event_timeout = -1;
375 udev_device->watch_handle = -1;
376 /* copy global properties */
377 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
378 udev_device_add_property(udev_device,
379 udev_list_entry_get_name(list_entry),
380 udev_list_entry_get_value(list_entry));
381 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
386 * udev_device_new_from_syspath:
387 * @udev: udev library context
388 * @syspath: sys device path including sys directory
390 * Create new udev device, and fill in information from the sys
391 * device and the udev database entry. The syspath is the absolute
392 * path to the device, including the sys mount point.
394 * The initial refcount is 1, and needs to be decremented to
395 * release the resources of the udev device.
397 * Returns: a new udev device, or #NULL, if it does not exist
399 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
403 char path[UTIL_PATH_SIZE];
406 struct udev_device *udev_device;
413 /* path starts in sys */
414 len = strlen(udev_get_sys_path(udev));
415 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
416 info(udev, "not in sys :%s\n", syspath);
420 /* path is not a root directory */
421 subdir = &syspath[len+1];
422 pos = strrchr(subdir, '/');
423 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
424 dbg(udev, "not a subdir :%s\n", syspath);
428 /* resolve possible symlink to real path */
429 util_strscpy(path, sizeof(path), syspath);
430 util_resolve_sys_link(udev, path, sizeof(path));
432 if (strncmp(&syspath[len], "/devices/", 9) == 0) {
433 char file[UTIL_PATH_SIZE];
435 /* all "devices" require a "uevent" file */
436 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
437 if (stat(file, &statbuf) != 0) {
438 dbg(udev, "not a device: %s\n", syspath);
442 /* everything else just needs to be a directory */
443 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
444 dbg(udev, "directory not found: %s\n", syspath);
449 udev_device = udev_device_new(udev);
450 if (udev_device == NULL)
453 udev_device_set_syspath(udev_device, path);
454 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
460 * udev_device_new_from_devnum:
461 * @udev: udev library context
462 * @type: char or block device
463 * @devnum: device major/minor number
465 * Create new udev device, and fill in information from the sys
466 * device and the udev database entry. The device is looked-up
467 * by its major/minor number and type. Character and block device
468 * numbers are not unique across the two types.
470 * The initial refcount is 1, and needs to be decremented to
471 * release the resources of the udev device.
473 * Returns: a new udev device, or #NULL, if it does not exist
475 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
477 char path[UTIL_PATH_SIZE];
478 const char *type_str;
482 else if (type == 'c')
487 /* use /sys/dev/{block,char}/<maj>:<min> link */
488 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u",
489 udev_get_sys_path(udev), type_str, major(devnum), minor(devnum));
490 return udev_device_new_from_syspath(udev, path);
494 * udev_device_new_from_subsystem_sysname:
495 * @udev: udev library context
496 * @subsystem: the subsystem of the device
497 * @sysname: the name of the device
499 * Create new udev device, and fill in information from the sys device
500 * and the udev database entry. The device is looked up by the subsystem
501 * and name string of the device, like "mem" / "zero", or "block" / "sda".
503 * The initial refcount is 1, and needs to be decremented to
504 * release the resources of the udev device.
506 * Returns: a new udev device, or #NULL, if it does not exist
508 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
510 char path_full[UTIL_PATH_SIZE];
516 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
518 if (strcmp(subsystem, "subsystem") == 0) {
519 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
520 if (stat(path_full, &statbuf) == 0)
523 util_strscpyl(path, l, "/bus/", sysname, NULL);
524 if (stat(path_full, &statbuf) == 0)
527 util_strscpyl(path, l, "/class/", sysname, NULL);
528 if (stat(path_full, &statbuf) == 0)
533 if (strcmp(subsystem, "module") == 0) {
534 util_strscpyl(path, l, "/module/", sysname, NULL);
535 if (stat(path_full, &statbuf) == 0)
540 if (strcmp(subsystem, "drivers") == 0) {
541 char subsys[UTIL_NAME_SIZE];
544 util_strscpy(subsys, sizeof(subsys), sysname);
545 driver = strchr(subsys, ':');
546 if (driver != NULL) {
550 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
551 if (stat(path_full, &statbuf) == 0)
554 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
555 if (stat(path_full, &statbuf) == 0)
561 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
562 if (stat(path_full, &statbuf) == 0)
565 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
566 if (stat(path_full, &statbuf) == 0)
569 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
570 if (stat(path_full, &statbuf) == 0)
575 return udev_device_new_from_syspath(udev, path_full);
579 * udev_device_new_from_environment
580 * @udev: udev library context
582 * Create new udev device, and fill in information from the
583 * current process environment. This only works reliable if
584 * the process is called from a udev rule. It is usually used
585 * for tools executed from IMPORT= rules.
587 * The initial refcount is 1, and needs to be decremented to
588 * release the resources of the udev device.
590 * Returns: a new udev device, or #NULL, if it does not exist
592 struct udev_device *udev_device_new_from_environment(struct udev *udev)
595 struct udev_device *udev_device;
597 udev_device = udev_device_new(udev);
598 if (udev_device == NULL)
600 udev_device_set_info_loaded(udev_device);
602 for (i = 0; environ[i] != NULL; i++)
603 udev_device_add_property_from_string_parse(udev_device, environ[i]);
605 if (udev_device_add_property_from_string_parse_finish(udev_device) < 0) {
606 info(udev, "missing values, invalid device\n");
607 udev_device_unref(udev_device);
614 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
616 struct udev_device *udev_device_parent = NULL;
617 char path[UTIL_PATH_SIZE];
620 util_strscpy(path, sizeof(path), udev_device->syspath);
621 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
625 pos = strrchr(subdir, '/');
626 if (pos == NULL || pos < &subdir[2])
629 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
630 if (udev_device_parent != NULL)
631 return udev_device_parent;
637 * udev_device_get_parent:
638 * @udev_device: the device to start searching from
640 * Find the next parent device, and fill in information from the sys
641 * device and the udev database entry.
643 * The returned the device is not referenced. It is attached to the
644 * child device, and will be cleaned up when the child device
647 * It is not necessarily just the upper level directory, empty or not
648 * recognized sys directories are ignored.
650 * It can be called as many times as needed, without caring about
653 * Returns: a new udev device, or #NULL, if it no parent exist.
655 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
657 if (udev_device == NULL)
659 if (!udev_device->parent_set) {
660 udev_device->parent_set = 1;
661 udev_device->parent_device = device_new_from_parent(udev_device);
663 if (udev_device->parent_device != NULL)
664 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
665 return udev_device->parent_device;
669 * udev_device_get_parent_with_subsystem_devtype:
670 * @udev_device: udev device to start searching from
671 * @subsystem: the subsystem of the device
672 * @devtype: the type (DEVTYPE) of the device
674 * Find the next parent device, with a matching subsystem and devtype
675 * value, and fill in information from the sys device and the udev
678 * If devtype is #NULL, only subsystem is checked, and any devtype will
681 * The returned the device is not referenced. It is attached to the
682 * child device, and will be cleaned up when the child device
685 * It can be called as many times as needed, without caring about
688 * Returns: a new udev device, or #NULL if no matching parent exists.
690 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
692 struct udev_device *parent;
694 if (subsystem == NULL)
697 parent = udev_device_get_parent(udev_device);
698 while (parent != NULL) {
699 const char *parent_subsystem;
700 const char *parent_devtype;
702 parent_subsystem = udev_device_get_subsystem(parent);
703 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
706 parent_devtype = udev_device_get_devtype(parent);
707 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
710 parent = udev_device_get_parent(parent);
716 * udev_device_get_udev:
717 * @udev_device: udev device
719 * Retrieve the udev library context the device was created with.
721 * Returns: the udev library context
723 struct udev *udev_device_get_udev(struct udev_device *udev_device)
725 if (udev_device == NULL)
727 return udev_device->udev;
732 * @udev_device: udev device
734 * Take a reference of a udev device.
736 * Returns: the passed udev device
738 struct udev_device *udev_device_ref(struct udev_device *udev_device)
740 if (udev_device == NULL)
742 udev_device->refcount++;
748 * @udev_device: udev device
750 * Drop a reference of a udev device. If the refcount reaches zero,
751 * the resources of the device will be released.
754 void udev_device_unref(struct udev_device *udev_device)
756 if (udev_device == NULL)
758 udev_device->refcount--;
759 if (udev_device->refcount > 0)
761 if (udev_device->parent_device != NULL)
762 udev_device_unref(udev_device->parent_device);
763 free(udev_device->syspath);
764 free(udev_device->sysname);
765 free(udev_device->devnode);
766 free(udev_device->subsystem);
767 free(udev_device->devtype);
768 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
769 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
770 free(udev_device->action);
771 free(udev_device->driver);
772 free(udev_device->devpath_old);
773 free(udev_device->sysname_old);
774 free(udev_device->knodename);
775 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
776 free(udev_device->envp);
777 free(udev_device->monitor_buf);
778 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
783 * udev_device_get_devpath:
784 * @udev_device: udev device
786 * Retrieve the kernel devpath value of the udev device. The path
787 * does not contain the sys mount point, and starts with a '/'.
789 * Returns: the devpath of the udev device
791 const char *udev_device_get_devpath(struct udev_device *udev_device)
793 if (udev_device == NULL)
795 return udev_device->devpath;
799 * udev_device_get_syspath:
800 * @udev_device: udev device
802 * Retrieve the sys path of the udev device. The path is an
803 * absolute path and starts with the sys mount point.
805 * Returns: the sys path of the udev device
807 const char *udev_device_get_syspath(struct udev_device *udev_device)
809 if (udev_device == NULL)
811 return udev_device->syspath;
815 * udev_device_get_sysname:
816 * @udev_device: udev device
818 * Returns: the sys name of the device device
820 const char *udev_device_get_sysname(struct udev_device *udev_device)
822 if (udev_device == NULL)
824 return udev_device->sysname;
828 * udev_device_get_sysnum:
829 * @udev_device: udev device
831 * Returns: the trailing number of of the device name
833 const char *udev_device_get_sysnum(struct udev_device *udev_device)
835 if (udev_device == NULL)
837 return udev_device->sysnum;
841 * udev_device_get_devnode:
842 * @udev_device: udev device
844 * Retrieve the device node file name belonging to the udev device.
845 * The path is an absolute path, and starts with the device directory.
847 * Returns: the device node file name of the udev device, or #NULL if no device node exists
849 const char *udev_device_get_devnode(struct udev_device *udev_device)
851 if (udev_device == NULL)
853 if (!udev_device->info_loaded)
854 device_load_info(udev_device);
855 return udev_device->devnode;
859 * udev_device_get_subsystem:
860 * @udev_device: udev device
862 * Retrieve the subsystem string of the udev device. The string does not
865 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
867 const char *udev_device_get_subsystem(struct udev_device *udev_device)
869 char subsystem[UTIL_NAME_SIZE];
871 if (udev_device == NULL)
873 if (!udev_device->subsystem_set) {
874 udev_device->subsystem_set = 1;
875 /* read "subsystem" link */
876 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
877 udev_device_set_subsystem(udev_device, subsystem);
878 return udev_device->subsystem;
881 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
882 udev_device_set_subsystem(udev_device, "module");
883 return udev_device->subsystem;
885 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
886 udev_device_set_subsystem(udev_device, "drivers");
887 return udev_device->subsystem;
889 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
890 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
891 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
892 udev_device_set_subsystem(udev_device, "subsystem");
893 return udev_device->subsystem;
896 return udev_device->subsystem;
900 * udev_device_get_devtype:
901 * @udev_device: udev device
903 * Retrieve the devtype string of the udev device.
905 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
907 const char *udev_device_get_devtype(struct udev_device *udev_device)
909 if (udev_device == NULL)
911 if (!udev_device->devtype_set) {
912 udev_device->devtype_set = 1;
913 if (!udev_device->info_loaded)
914 udev_device_read_uevent_file(udev_device);
916 return udev_device->devtype;
920 * udev_device_get_devlinks_list_entry:
921 * @udev_device: udev device
923 * Retrieve the list of device links pointing to the device file of
924 * the udev device. The next list entry can be retrieved with
925 * udev_list_entry_next(), which returns #NULL if no more entries exist.
926 * The devlink path can be retrieved from the list entry by
927 * udev_list_entry_get_name(). The path is an absolute path, and starts with
928 * the device directory.
930 * Returns: the first entry of the device node link list
932 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
934 if (udev_device == NULL)
936 if (!udev_device->info_loaded)
937 device_load_info(udev_device);
938 return udev_list_get_entry(&udev_device->devlinks_list);
941 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
943 udev_device->devlinks_uptodate = 0;
944 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
948 * udev_device_get_properties_list_entry:
949 * @udev_device: udev device
951 * Retrieve the list of key/value device properties of the udev
952 * device. The next list entry can be retrieved with udev_list_entry_next(),
953 * which returns #NULL if no more entries exist. The property name
954 * can be retrieved from the list entry by udev_list_get_name(),
955 * the property value by udev_list_get_value().
957 * Returns: the first entry of the property list
959 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
961 if (udev_device == NULL)
963 if (!udev_device->info_loaded)
964 device_load_info(udev_device);
965 if (!udev_device->devlinks_uptodate) {
966 char symlinks[UTIL_PATH_SIZE];
967 struct udev_list_entry *list_entry;
969 udev_device->devlinks_uptodate = 1;
970 list_entry = udev_device_get_devlinks_list_entry(udev_device);
971 if (list_entry != NULL) {
976 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
977 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
978 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
979 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
982 return udev_list_get_entry(&udev_device->properties_list);
986 * udev_device_get_driver:
987 * @udev_device: udev device
989 * Returns: the driver string, or #NULL if there is no driver attached.
991 const char *udev_device_get_driver(struct udev_device *udev_device)
993 char driver[UTIL_NAME_SIZE];
995 if (udev_device == NULL)
997 if (!udev_device->driver_set) {
998 udev_device->driver_set = 1;
999 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
1000 udev_device->driver = strdup(driver);
1002 return udev_device->driver;
1006 * udev_device_get_devnum:
1007 * @udev_device: udev device
1009 * Returns: the device major/minor number.
1011 dev_t udev_device_get_devnum(struct udev_device *udev_device)
1013 if (udev_device == NULL)
1014 return makedev(0, 0);
1015 if (!udev_device->info_loaded)
1016 device_load_info(udev_device);
1017 return udev_device->devnum;
1021 * udev_device_get_action:
1022 * @udev_device: udev device
1024 * This is only valid if the device was received through a monitor. Devices read from
1025 * sys do not have an action string. Usual actions are: add, remove, change, online,
1028 * Returns: the kernel action value, or #NULL if there is no action value available.
1030 const char *udev_device_get_action(struct udev_device *udev_device)
1032 if (udev_device == NULL)
1034 return udev_device->action;
1038 * udev_device_get_devnum:
1039 * @udev_device: udev device
1041 * This is only valid if the device was received through a monitor. Devices read from
1042 * sys do not have a sequence number.
1044 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
1046 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
1048 if (udev_device == NULL)
1050 return udev_device->seqnum;
1054 * udev_device_get_sysattr_value:
1055 * @udev_device: udev device
1056 * @sysattr: attribute name
1058 * The retrieved value is cached in the device. Repeated calls will return the same
1059 * value and not open the attribute again.
1061 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
1063 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
1065 struct udev_list_entry *list_entry;
1066 char path[UTIL_PATH_SIZE];
1068 struct stat statbuf;
1071 const char *val = NULL;
1073 if (udev_device == NULL)
1075 if (sysattr == NULL)
1078 /* look for possibly already cached result */
1079 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
1080 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
1081 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
1082 sysattr, udev_list_entry_get_value(list_entry));
1083 return udev_list_entry_get_value(list_entry);
1087 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
1088 if (lstat(path, &statbuf) != 0) {
1089 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
1090 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
1094 if (S_ISLNK(statbuf.st_mode)) {
1095 char target[UTIL_NAME_SIZE];
1099 /* some core links return the last element of the target path */
1100 if (strcmp(sysattr, "driver") != 0 &&
1101 strcmp(sysattr, "subsystem") != 0 &&
1102 strcmp(sysattr, "module") != 0)
1105 len = readlink(path, target, sizeof(target));
1108 pos = strrchr(target, '/');
1111 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1112 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1113 val = udev_list_entry_get_value(list_entry);
1119 /* skip directories */
1120 if (S_ISDIR(statbuf.st_mode))
1123 /* skip non-readable files */
1124 if ((statbuf.st_mode & S_IRUSR) == 0)
1127 /* read attribute value */
1128 fd = open(path, O_RDONLY);
1130 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1133 size = read(fd, value, sizeof(value));
1137 if (size == sizeof(value))
1140 /* got a valid value, store it in cache and return it */
1142 util_remove_trailing_chars(value, '\n');
1143 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1144 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1145 val = udev_list_entry_get_value(list_entry);
1150 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1155 free(udev_device->syspath);
1156 udev_device->syspath = strdup(syspath);
1157 if (udev_device->syspath == NULL)
1159 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1160 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1162 pos = strrchr(udev_device->syspath, '/');
1165 udev_device->sysname = strdup(&pos[1]);
1166 if (udev_device->sysname == NULL)
1169 /* some devices have '!' in their name, change that to '/' */
1171 while (udev_device->sysname[len] != '\0') {
1172 if (udev_device->sysname[len] == '!')
1173 udev_device->sysname[len] = '/';
1177 /* trailing number */
1178 while (len > 0 && isdigit(udev_device->sysname[--len]))
1179 udev_device->sysnum = &udev_device->sysname[len];
1181 /* sysname is completely numeric */
1183 udev_device->sysnum = NULL;
1188 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1190 free(udev_device->subsystem);
1191 udev_device->subsystem = strdup(subsystem);
1192 if (udev_device->subsystem == NULL)
1194 udev_device->subsystem_set = 1;
1195 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1199 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1201 free(udev_device->devtype);
1202 udev_device->devtype = strdup(devtype);
1203 if (udev_device->devtype == NULL)
1205 udev_device->devtype_set = 1;
1206 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1210 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1212 free(udev_device->devnode);
1213 udev_device->devnode = strdup(devnode);
1214 if (devnode == NULL)
1216 if (udev_device->devnode == NULL)
1218 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1222 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1224 struct udev_list_entry *list_entry;
1226 udev_device->devlinks_uptodate = 0;
1227 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1228 if (list_entry == NULL)
1231 udev_list_entry_set_flags(list_entry, 1);
1235 #define ENVP_SIZE 128
1236 #define MONITOR_BUF_SIZE 4096
1237 static int update_envp_monitor_buf(struct udev_device *udev_device)
1239 struct udev_list_entry *list_entry;
1244 /* monitor buffer of property strings */
1245 free(udev_device->monitor_buf);
1246 udev_device->monitor_buf_len = 0;
1247 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1248 if (udev_device->monitor_buf == NULL)
1251 /* envp array, strings will point into monitor buffer */
1252 if (udev_device->envp == NULL)
1253 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1254 if (udev_device->envp == NULL)
1258 s = udev_device->monitor_buf;
1259 l = MONITOR_BUF_SIZE;
1260 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1263 key = udev_list_entry_get_name(list_entry);
1264 /* skip private variables */
1268 /* add string to envp array */
1269 udev_device->envp[i++] = s;
1270 if (i+1 >= ENVP_SIZE)
1273 /* add property string to monitor buffer */
1274 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1277 /* advance past the trailing '\0' that util_strpcpyl() guarantees */
1281 udev_device->envp[i] = NULL;
1282 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1283 udev_device->envp_uptodate = 1;
1284 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1285 i, udev_device->monitor_buf_len);
1289 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1291 if (!udev_device->envp_uptodate)
1292 if (update_envp_monitor_buf(udev_device) != 0)
1294 return udev_device->envp;
1297 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1299 if (!udev_device->envp_uptodate)
1300 if (update_envp_monitor_buf(udev_device) != 0)
1302 *buf = udev_device->monitor_buf;
1303 return udev_device->monitor_buf_len;
1306 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1308 free(udev_device->action);
1309 udev_device->action = strdup(action);
1310 if (udev_device->action == NULL)
1312 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1316 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1318 free(udev_device->driver);
1319 udev_device->driver = strdup(driver);
1320 if (udev_device->driver == NULL)
1322 udev_device->driver_set = 1;
1323 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1327 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1329 return udev_device->devpath_old;
1332 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1337 free(udev_device->devpath_old);
1338 udev_device->devpath_old = strdup(devpath_old);
1339 if (udev_device->devpath_old == NULL)
1341 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1343 pos = strrchr(udev_device->devpath_old, '/');
1346 udev_device->sysname_old = strdup(&pos[1]);
1347 if (udev_device->sysname_old == NULL)
1350 /* some devices have '!' in their name, change that to '/' */
1352 while (udev_device->sysname_old[len] != '\0') {
1353 if (udev_device->sysname_old[len] == '!')
1354 udev_device->sysname_old[len] = '/';
1360 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1362 if (udev_device == NULL)
1364 return udev_device->sysname_old;
1367 const char *udev_device_get_knodename(struct udev_device *udev_device)
1369 return udev_device->knodename;
1372 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1374 free(udev_device->knodename);
1375 udev_device->knodename = strdup(knodename);
1376 if (udev_device->knodename == NULL)
1378 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1382 int udev_device_get_timeout(struct udev_device *udev_device)
1384 return udev_device->timeout;
1387 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1389 udev_device->timeout = timeout;
1392 int udev_device_get_event_timeout(struct udev_device *udev_device)
1394 if (!udev_device->info_loaded)
1395 device_load_info(udev_device);
1396 return udev_device->event_timeout;
1399 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1401 udev_device->event_timeout = event_timeout;
1405 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1409 udev_device->seqnum = seqnum;
1410 snprintf(num, sizeof(num), "%llu", seqnum);
1411 udev_device_add_property(udev_device, "SEQNUM", num);
1415 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1419 udev_device->devnum = devnum;
1421 snprintf(num, sizeof(num), "%u", major(devnum));
1422 udev_device_add_property(udev_device, "MAJOR", num);
1423 snprintf(num, sizeof(num), "%u", minor(devnum));
1424 udev_device_add_property(udev_device, "MINOR", num);
1428 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1430 if (!udev_device->info_loaded)
1431 device_load_info(udev_device);
1432 return udev_device->num_fake_partitions;
1435 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1437 udev_device->num_fake_partitions = num;
1441 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1443 if (!udev_device->info_loaded)
1444 device_load_info(udev_device);
1445 return udev_device->devlink_priority;
1448 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1450 udev_device->devlink_priority = prio;
1454 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1456 if (!udev_device->info_loaded)
1457 device_load_info(udev_device);
1458 return udev_device->ignore_remove;
1461 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1463 udev_device->ignore_remove = ignore;
1467 int udev_device_get_watch_handle(struct udev_device *udev_device)
1469 if (!udev_device->info_loaded)
1470 device_load_info(udev_device);
1471 return udev_device->watch_handle;
1474 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1476 udev_device->watch_handle = handle;