2 * libudev - interface to udev device information
4 * Copyright (C) 2008-2009 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.
24 #include "libudev-private.h"
27 * SECTION:libudev-device
28 * @short_description: kernel sys devices
30 * Representation of kernel sys devices. Devices are uniquely identified
31 * by their syspath, every device has exactly one path in the kernel sys
32 * filesystem. Devices usually belong to a kernel subsystem, and and have
33 * a unique name inside that subsystem.
39 * Opaque object representing one kernel sys device.
43 struct udev_device *parent_device;
58 size_t monitor_buf_len;
59 struct udev_list_node devlinks_list;
60 struct udev_list_node properties_list;
61 struct udev_list_node sysattr_list;
62 unsigned long long int seqnum;
65 int num_fake_partitions;
70 unsigned int parent_set:1;
71 unsigned int subsystem_set:1;
72 unsigned int devtype_set:1;
73 unsigned int devlinks_uptodate:1;
74 unsigned int envp_uptodate:1;
75 unsigned int driver_set:1;
76 unsigned int info_loaded:1;
77 unsigned int ignore_remove:1;
80 int udev_device_read_db(struct udev_device *udev_device)
83 char filename[UTIL_PATH_SIZE];
84 char line[UTIL_LINE_SIZE];
87 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/.udev/db/",
88 udev_device_get_subsystem(udev_device), ":", udev_device_get_sysname(udev_device), NULL);
90 if (lstat(filename, &stats) != 0) {
91 dbg(udev_device->udev, "no db file to read %s: %m\n", filename);
94 if ((stats.st_mode & S_IFMT) == S_IFLNK) {
95 char target[UTIL_PATH_SIZE];
96 char devnode[UTIL_PATH_SIZE];
100 target_len = readlink(filename, target, sizeof(target));
102 target[target_len] = '\0';
104 dbg(udev_device->udev, "error reading db link %s: %m\n", filename);
108 next = strchr(target, ' ');
113 util_strscpyl(devnode, sizeof(devnode), udev_get_dev_path(udev_device->udev), "/", target, NULL);
114 udev_device_set_devnode(udev_device, devnode);
115 while (next != NULL) {
116 char devlink[UTIL_PATH_SIZE];
120 next = strchr(next, ' ');
125 util_strscpyl(devlink, sizeof(devlink), udev_get_dev_path(udev_device->udev), "/", lnk, NULL);
126 udev_device_add_devlink(udev_device, devlink, 0);
128 info(udev_device->udev, "device %p filled with db symlink data '%s'\n", udev_device, udev_device->devnode);
132 f = fopen(filename, "r");
134 dbg(udev_device->udev, "error reading db file %s: %m\n", filename);
137 while (fgets(line, sizeof(line), f)) {
148 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
149 udev_device_set_devnode(udev_device, filename);
152 util_strscpyl(filename, sizeof(filename), udev_get_dev_path(udev_device->udev), "/", val, NULL);
153 udev_device_add_devlink(udev_device, filename, 0);
156 udev_device_set_devlink_priority(udev_device, atoi(val));
159 udev_device_set_event_timeout(udev_device, atoi(val));
162 udev_device_set_num_fake_partitions(udev_device, atoi(val));
165 udev_device_set_ignore_remove(udev_device, atoi(val));
168 udev_device_add_property_from_string(udev_device, val);
171 udev_device_set_watch_handle(udev_device, atoi(val));
177 info(udev_device->udev, "device %p filled with db file data\n", udev_device);
181 int udev_device_read_uevent_file(struct udev_device *udev_device)
183 char filename[UTIL_PATH_SIZE];
185 char line[UTIL_LINE_SIZE];
189 util_strscpyl(filename, sizeof(filename), udev_device->syspath, "/uevent", NULL);
190 f = fopen(filename, "r");
194 while (fgets(line, sizeof(line), f)) {
197 pos = strchr(line, '\n');
202 if (strncmp(line, "DEVTYPE=", 8) == 0)
203 udev_device_set_devtype(udev_device, &line[8]);
204 else if (strncmp(line, "MAJOR=", 6) == 0)
205 maj = strtoull(&line[6], NULL, 10);
206 else if (strncmp(line, "MINOR=", 6) == 0)
207 min = strtoull(&line[6], NULL, 10);
208 else if (strncmp(line, "DEVNAME=", 8) == 0)
209 udev_device_set_knodename(udev_device, &line[8]);
211 udev_device_add_property_from_string(udev_device, line);
214 udev_device->devnum = makedev(maj, min);
220 static void device_load_info(struct udev_device *device)
222 device->info_loaded = 1;
223 udev_device_read_uevent_file(device);
224 udev_device_read_db(device);
227 void udev_device_set_info_loaded(struct udev_device *device)
229 device->info_loaded = 1;
232 struct udev_device *udev_device_new(struct udev *udev)
234 struct udev_device *udev_device;
235 struct udev_list_entry *list_entry;
240 udev_device = calloc(1, sizeof(struct udev_device));
241 if (udev_device == NULL)
243 udev_device->refcount = 1;
244 udev_device->udev = udev;
245 udev_list_init(&udev_device->devlinks_list);
246 udev_list_init(&udev_device->properties_list);
247 udev_list_init(&udev_device->sysattr_list);
248 udev_device->event_timeout = -1;
249 udev_device->watch_handle = -1;
250 /* copy global properties */
251 udev_list_entry_foreach(list_entry, udev_get_properties_list_entry(udev))
252 udev_device_add_property(udev_device,
253 udev_list_entry_get_name(list_entry),
254 udev_list_entry_get_value(list_entry));
255 dbg(udev_device->udev, "udev_device: %p created\n", udev_device);
260 * udev_device_new_from_syspath:
261 * @udev: udev library context
262 * @syspath: sys device path including sys directory
264 * Create new udev device, and fill in information from the sys
265 * device and the udev database entry. The syspath is the absolute
266 * path to the device, including the sys mount point.
268 * The initial refcount is 1, and needs to be decremented to
269 * release the resources of the udev device.
271 * Returns: a new udev device, or #NULL, if it does not exist
273 struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
277 char path[UTIL_PATH_SIZE];
280 struct udev_device *udev_device;
287 /* path starts in sys */
288 len = strlen(udev_get_sys_path(udev));
289 if (strncmp(syspath, udev_get_sys_path(udev), len) != 0) {
290 info(udev, "not in sys :%s\n", syspath);
294 /* path is not a root directory */
295 subdir = &syspath[len+1];
296 pos = strrchr(subdir, '/');
297 if (pos == NULL || pos[1] == '\0' || pos < &subdir[2]) {
298 dbg(udev, "not a subdir :%s\n", syspath);
302 /* resolve possible symlink to real path */
303 util_strscpy(path, sizeof(path), syspath);
304 util_resolve_sys_link(udev, path, sizeof(path));
306 /* try to resolve the silly block layout if needed */
307 if (strncmp(&path[len], "/block/", 7) == 0) {
308 char block[UTIL_PATH_SIZE];
309 char part[UTIL_PATH_SIZE];
311 util_strscpy(block, sizeof(block), path);
312 pos = strrchr(block, '/');
315 util_strscpy(part, sizeof(part), pos);
317 if (util_resolve_sys_link(udev, block, sizeof(block)) == 0)
318 util_strscpyl(path, sizeof(path), block, part, NULL);
321 /* path exists in sys */
322 if (strncmp(&syspath[len], "/devices/", 9) == 0 ||
323 strncmp(&syspath[len], "/class/", 7) == 0 ||
324 strncmp(&syspath[len], "/block/", 7) == 0) {
325 char file[UTIL_PATH_SIZE];
327 /* all "devices" require a "uevent" file */
328 util_strscpyl(file, sizeof(file), path, "/uevent", NULL);
329 if (stat(file, &statbuf) != 0) {
330 dbg(udev, "not a device: %s\n", syspath);
334 /* everything else just needs to be a directory */
335 if (stat(path, &statbuf) != 0 || !S_ISDIR(statbuf.st_mode)) {
336 dbg(udev, "directory not found: %s\n", syspath);
341 udev_device = udev_device_new(udev);
342 if (udev_device == NULL)
345 udev_device_set_syspath(udev_device, path);
346 info(udev, "device %p has devpath '%s'\n", udev_device, udev_device_get_devpath(udev_device));
352 * udev_device_new_from_devnum:
353 * @udev: udev library context
354 * @type: char or block device
355 * @devnum: device major/minor number
357 * Create new udev device, and fill in information from the sys
358 * device and the udev database entry. The device is looked up
359 * by its major/minor number. Character and block device numbers
360 * are not unique across the two types, they do not share the same
363 * The initial refcount is 1, and needs to be decremented to
364 * release the resources of the udev device.
366 * Returns: a new udev device, or #NULL, if it does not exist
368 struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, dev_t devnum)
370 char path[UTIL_PATH_SIZE];
371 const char *type_str;
372 struct udev_enumerate *udev_enumerate;
373 struct udev_list_entry *list_entry;
374 struct udev_device *device = NULL;
378 else if (type == 'c')
383 /* /sys/dev/{block,char}/<maj>:<min> link */
384 snprintf(path, sizeof(path), "%s/dev/%s/%u:%u", udev_get_sys_path(udev),
385 type_str, major(devnum), minor(devnum));
386 if (util_resolve_sys_link(udev, path, sizeof(path)) == 0)
387 return udev_device_new_from_syspath(udev, path);
389 udev_enumerate = udev_enumerate_new(udev);
390 if (udev_enumerate == NULL)
393 /* fallback to search sys devices for the major/minor */
395 udev_enumerate_add_match_subsystem(udev_enumerate, "block");
396 else if (type == 'c')
397 udev_enumerate_add_nomatch_subsystem(udev_enumerate, "block");
398 udev_enumerate_scan_devices(udev_enumerate);
399 udev_list_entry_foreach(list_entry, udev_enumerate_get_list_entry(udev_enumerate)) {
400 struct udev_device *device_loop;
402 device_loop = udev_device_new_from_syspath(udev, udev_list_entry_get_name(list_entry));
403 if (device_loop != NULL) {
404 if (udev_device_get_devnum(device_loop) == devnum) {
405 if (type == 'b' && strcmp(udev_device_get_subsystem(device_loop), "block") != 0)
407 if (type == 'c' && strcmp(udev_device_get_subsystem(device_loop), "block") == 0)
409 device = device_loop;
412 udev_device_unref(device_loop);
415 udev_enumerate_unref(udev_enumerate);
420 * udev_device_new_from_subsystem_sysname:
421 * @udev: udev library context
422 * @subsystem: the subsystem of the device
423 * @sysname: the name of the device
425 * Create new udev device, and fill in information from the sys
426 * device and the udev database entry. The device is looked up
427 * by the subsystem and name string of the device, like "mem",
428 * "zero", or "block", "sda".
430 * The initial refcount is 1, and needs to be decremented to
431 * release the resources of the udev device.
433 * Returns: a new udev device, or #NULL, if it does not exist
435 struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *udev, const char *subsystem, const char *sysname)
437 char path_full[UTIL_PATH_SIZE];
443 l = util_strpcpyl(&path, sizeof(path_full), udev_get_sys_path(udev), NULL);
445 if (strcmp(subsystem, "subsystem") == 0) {
446 util_strscpyl(path, l, "/subsystem/", sysname, NULL);
447 if (stat(path_full, &statbuf) == 0)
450 util_strscpyl(path, l, "/bus/", sysname, NULL);
451 if (stat(path_full, &statbuf) == 0)
454 util_strscpyl(path, l, "/class/", sysname, NULL);
455 if (stat(path_full, &statbuf) == 0)
460 if (strcmp(subsystem, "module") == 0) {
461 util_strscpyl(path, l, "/module/", sysname, NULL);
462 if (stat(path_full, &statbuf) == 0)
467 if (strcmp(subsystem, "drivers") == 0) {
468 char subsys[UTIL_NAME_SIZE];
471 util_strscpy(subsys, sizeof(subsys), sysname);
472 driver = strchr(subsys, ':');
473 if (driver != NULL) {
477 util_strscpyl(path, l, "/subsystem/", subsys, "/drivers/", driver, NULL);
478 if (stat(path_full, &statbuf) == 0)
481 util_strscpyl(path, l, "/bus/", subsys, "/drivers/", driver, NULL);
482 if (stat(path_full, &statbuf) == 0)
488 util_strscpyl(path, l, "/subsystem/", subsystem, "/devices/", sysname, NULL);
489 if (stat(path_full, &statbuf) == 0)
492 util_strscpyl(path, l, "/bus/", subsystem, "/devices/", sysname, NULL);
493 if (stat(path_full, &statbuf) == 0)
496 util_strscpyl(path, l, "/class/", subsystem, "/", sysname, NULL);
497 if (stat(path_full, &statbuf) == 0)
502 return udev_device_new_from_syspath(udev, path_full);
505 static struct udev_device *device_new_from_parent(struct udev_device *udev_device)
507 struct udev_device *udev_device_parent = NULL;
508 char path[UTIL_PATH_SIZE];
511 /* follow "device" link in deprecated sys layout */
512 if (strncmp(udev_device->devpath, "/class/", 7) == 0 ||
513 strncmp(udev_device->devpath, "/block/", 7) == 0) {
514 util_strscpyl(path, sizeof(path), udev_device->syspath, "/device", NULL);
515 if (util_resolve_sys_link(udev_device->udev, path, sizeof(path)) == 0) {
516 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
517 if (udev_device_parent != NULL)
518 return udev_device_parent;
522 util_strscpy(path, sizeof(path), udev_device->syspath);
523 subdir = &path[strlen(udev_get_sys_path(udev_device->udev))+1];
527 pos = strrchr(subdir, '/');
528 if (pos == NULL || pos < &subdir[2])
531 udev_device_parent = udev_device_new_from_syspath(udev_device->udev, path);
532 if (udev_device_parent != NULL)
533 return udev_device_parent;
539 * udev_device_get_parent:
540 * @udev_device: the device to start searching from
542 * Find the next parent device, and fill in information from the sys
543 * device and the udev database entry.
545 * The returned the device is not referenced. It is attached to the
546 * child device, and will be cleaned up when the child device
549 * It is not necessarily just the upper level directory, empty or not
550 * recognized sys directories are ignored.
552 * It can be called as many times as needed, without caring about
555 * Returns: a new udev device, or #NULL, if it no parent exist.
557 struct udev_device *udev_device_get_parent(struct udev_device *udev_device)
559 if (udev_device == NULL)
561 if (!udev_device->parent_set) {
562 udev_device->parent_set = 1;
563 udev_device->parent_device = device_new_from_parent(udev_device);
565 if (udev_device->parent_device != NULL)
566 dbg(udev_device->udev, "returning existing parent %p\n", udev_device->parent_device);
567 return udev_device->parent_device;
571 * udev_device_get_parent_with_subsystem_devtype:
572 * @udev_device: udev device to start searching from
573 * @subsystem: the subsystem of the device
574 * @devtype: the type (DEVTYPE) of the device
576 * Find the next parent device, with a matching subsystem and devtype
577 * value, and fill in information from the sys device and the udev
580 * If devtype is NULL, only subsystem is checked, and any devtype will
583 * The returned the device is not referenced. It is attached to the
584 * child device, and will be cleaned up when the child device
587 * It can be called as many times as needed, without caring about
590 * Returns: a new udev device, or #NULL if no matching parent exists.
592 struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_device *udev_device, const char *subsystem, const char *devtype)
594 struct udev_device *parent;
596 if (subsystem == NULL)
599 parent = udev_device_get_parent(udev_device);
600 while (parent != NULL) {
601 const char *parent_subsystem;
602 const char *parent_devtype;
604 parent_subsystem = udev_device_get_subsystem(parent);
605 if (parent_subsystem != NULL && strcmp(parent_subsystem, subsystem) == 0) {
608 parent_devtype = udev_device_get_devtype(parent);
609 if (parent_devtype != NULL && strcmp(parent_devtype, devtype) == 0)
612 parent = udev_device_get_parent(parent);
618 * udev_device_get_udev:
619 * @udev_device: udev device
621 * Retrieve the udev library context the device was created with.
623 * Returns: the udev library context
625 struct udev *udev_device_get_udev(struct udev_device *udev_device)
627 if (udev_device == NULL)
629 return udev_device->udev;
634 * @udev_device: udev device
636 * Take a reference of a udev device.
638 * Returns: the passed udev device
640 struct udev_device *udev_device_ref(struct udev_device *udev_device)
642 if (udev_device == NULL)
644 udev_device->refcount++;
650 * @udev_device: udev device
652 * Drop a reference of a udev device. If the refcount reaches zero,
653 * the resources of the device will be released.
656 void udev_device_unref(struct udev_device *udev_device)
658 if (udev_device == NULL)
660 udev_device->refcount--;
661 if (udev_device->refcount > 0)
663 if (udev_device->parent_device != NULL)
664 udev_device_unref(udev_device->parent_device);
665 free(udev_device->syspath);
666 free(udev_device->sysname);
667 free(udev_device->devnode);
668 free(udev_device->subsystem);
669 free(udev_device->devtype);
670 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
671 udev_list_cleanup_entries(udev_device->udev, &udev_device->properties_list);
672 free(udev_device->action);
673 free(udev_device->driver);
674 free(udev_device->devpath_old);
675 free(udev_device->sysname_old);
676 free(udev_device->knodename);
677 udev_list_cleanup_entries(udev_device->udev, &udev_device->sysattr_list);
678 free(udev_device->envp);
679 free(udev_device->monitor_buf);
680 dbg(udev_device->udev, "udev_device: %p released\n", udev_device);
685 * udev_device_get_devpath:
686 * @udev_device: udev device
688 * Retrieve the kernel devpath value of the udev device. The path
689 * does not contain the sys mount point, and starts with a '/'.
691 * Returns: the devpath of the udev device
693 const char *udev_device_get_devpath(struct udev_device *udev_device)
695 if (udev_device == NULL)
697 return udev_device->devpath;
701 * udev_device_get_syspath:
702 * @udev_device: udev device
704 * Retrieve the sys path of the udev device. The path is an
705 * absolute path and starts with the sys mount point.
707 * Returns: the sys path of the udev device
709 const char *udev_device_get_syspath(struct udev_device *udev_device)
711 if (udev_device == NULL)
713 return udev_device->syspath;
717 * udev_device_get_sysname:
718 * @udev_device: udev device
720 * Returns: the sys name of the device device
722 const char *udev_device_get_sysname(struct udev_device *udev_device)
724 if (udev_device == NULL)
726 return udev_device->sysname;
730 * udev_device_get_sysnum:
731 * @udev_device: udev device
733 * Returns: the trailing number of of the device name
735 const char *udev_device_get_sysnum(struct udev_device *udev_device)
737 if (udev_device == NULL)
739 return udev_device->sysnum;
743 * udev_device_get_devnode:
744 * @udev_device: udev device
746 * Retrieve the device node file name belonging to the udev device.
747 * The path is an absolute path, and starts with the device directory.
749 * Returns: the device node file name of the udev device, or #NULL if no device node exists
751 const char *udev_device_get_devnode(struct udev_device *udev_device)
753 if (udev_device == NULL)
755 if (!udev_device->info_loaded)
756 device_load_info(udev_device);
757 return udev_device->devnode;
761 * udev_device_get_subsystem:
762 * @udev_device: udev device
764 * Retrieve the subsystem string of the udev device. The string does not
767 * Returns: the subsystem name of the udev device, or #NULL if it can not be determined
769 const char *udev_device_get_subsystem(struct udev_device *udev_device)
771 char subsystem[UTIL_NAME_SIZE];
773 if (udev_device == NULL)
775 if (!udev_device->subsystem_set) {
776 udev_device->subsystem_set = 1;
777 /* read "subsystem" link */
778 if (util_get_sys_subsystem(udev_device->udev, udev_device->syspath, subsystem, sizeof(subsystem)) > 0) {
779 udev_device_set_subsystem(udev_device, subsystem);
780 return udev_device->subsystem;
783 if (strncmp(udev_device->devpath, "/module/", 8) == 0) {
784 udev_device_set_subsystem(udev_device, "module");
785 return udev_device->subsystem;
787 if (strstr(udev_device->devpath, "/drivers/") != NULL) {
788 udev_device_set_subsystem(udev_device, "drivers");
789 return udev_device->subsystem;
791 if (strncmp(udev_device->devpath, "/subsystem/", 11) == 0 ||
792 strncmp(udev_device->devpath, "/class/", 7) == 0 ||
793 strncmp(udev_device->devpath, "/bus/", 5) == 0) {
794 udev_device_set_subsystem(udev_device, "subsystem");
795 return udev_device->subsystem;
798 return udev_device->subsystem;
802 * udev_device_get_devtype:
803 * @udev_device: udev device
805 * Retrieve the devtype string of the udev device.
807 * Returns: the devtype name of the udev device, or #NULL if it can not be determined
809 const char *udev_device_get_devtype(struct udev_device *udev_device)
811 if (udev_device == NULL)
813 if (!udev_device->devtype_set) {
814 udev_device->devtype_set = 1;
815 if (!udev_device->info_loaded)
816 udev_device_read_uevent_file(udev_device);
818 return udev_device->devtype;
822 * udev_device_get_devlinks_list_entry:
823 * @udev_device: udev device
825 * Retrieve the list of device links pointing to the device file of
826 * the udev device. The next list entry can be retrieved with
827 * udev_list_entry_next(), which returns #NULL if no more entries exist.
828 * The devlink path can be retrieved from the list entry by
829 * udev_list_entry_get_name(). The path is an absolute path, and starts with
830 * the device directory.
832 * Returns: the first entry of the device node link list
834 struct udev_list_entry *udev_device_get_devlinks_list_entry(struct udev_device *udev_device)
836 if (udev_device == NULL)
838 if (!udev_device->info_loaded)
839 device_load_info(udev_device);
840 return udev_list_get_entry(&udev_device->devlinks_list);
843 void udev_device_cleanup_devlinks_list(struct udev_device *udev_device)
845 udev_device->devlinks_uptodate = 0;
846 udev_list_cleanup_entries(udev_device->udev, &udev_device->devlinks_list);
850 * udev_device_get_properties_list_entry:
851 * @udev_device: udev device
853 * Retrieve the list of key/value device properties of the udev
854 * device. The next list entry can be retrieved with udev_list_entry_next(),
855 * which returns #NULL if no more entries exist. The property name
856 * can be retrieved from the list entry by udev_list_get_name(),
857 * the property value by udev_list_get_value().
859 * Returns: the first entry of the property list
861 struct udev_list_entry *udev_device_get_properties_list_entry(struct udev_device *udev_device)
863 if (udev_device == NULL)
865 if (!udev_device->info_loaded)
866 device_load_info(udev_device);
867 if (!udev_device->devlinks_uptodate) {
868 char symlinks[UTIL_PATH_SIZE];
869 struct udev_list_entry *list_entry;
871 udev_device->devlinks_uptodate = 1;
872 list_entry = udev_device_get_devlinks_list_entry(udev_device);
873 if (list_entry != NULL) {
878 l = util_strpcpyl(&s, sizeof(symlinks), udev_list_entry_get_name(list_entry), NULL);
879 udev_list_entry_foreach(list_entry, udev_list_entry_get_next(list_entry))
880 l = util_strpcpyl(&s, l, " ", udev_list_entry_get_name(list_entry), NULL);
881 udev_device_add_property(udev_device, "DEVLINKS", symlinks);
884 return udev_list_get_entry(&udev_device->properties_list);
888 * udev_device_get_driver:
889 * @udev_device: udev device
891 * Returns: the driver string, or #NULL if there is no driver attached.
893 const char *udev_device_get_driver(struct udev_device *udev_device)
895 char driver[UTIL_NAME_SIZE];
897 if (udev_device == NULL)
899 if (!udev_device->driver_set) {
900 udev_device->driver_set = 1;
901 if (util_get_sys_driver(udev_device->udev, udev_device->syspath, driver, sizeof(driver)) > 0)
902 udev_device->driver = strdup(driver);
904 return udev_device->driver;
908 * udev_device_get_devnum:
909 * @udev_device: udev device
911 * Returns: the device major/minor number.
913 dev_t udev_device_get_devnum(struct udev_device *udev_device)
915 if (udev_device == NULL)
916 return makedev(0, 0);
917 if (!udev_device->info_loaded)
918 device_load_info(udev_device);
919 return udev_device->devnum;
923 * udev_device_get_action:
924 * @udev_device: udev device
926 * This is only valid if the device was received through a monitor. Devices read from
927 * sys do not have an action string. Usual actions are: add, remove, change, online,
930 * Returns: the kernel action value, or #NULL if there is no action value available.
932 const char *udev_device_get_action(struct udev_device *udev_device)
934 if (udev_device == NULL)
936 return udev_device->action;
940 * udev_device_get_devnum:
941 * @udev_device: udev device
943 * This is only valid if the device was received through a monitor. Devices read from
944 * sys do not have a sequence number.
946 * Returns: the kernel event sequence number, or 0 if there is no sequence number available.
948 unsigned long long int udev_device_get_seqnum(struct udev_device *udev_device)
950 if (udev_device == NULL)
952 return udev_device->seqnum;
956 * udev_device_get_sysattr_value:
957 * @udev_device: udev device
958 * @sysattr: attribute name
960 * The retrieved value is cached in the device. Repeated calls will return the same
961 * value and not open the attribute again.
963 * Returns: the content of a sys attribute file, or #NULL if there is no sys attribute value.
965 const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
967 struct udev_list_entry *list_entry;
968 char path[UTIL_PATH_SIZE];
973 const char *val = NULL;
975 if (udev_device == NULL)
980 /* look for possibly already cached result */
981 udev_list_entry_foreach(list_entry, udev_list_get_entry(&udev_device->sysattr_list)) {
982 if (strcmp(udev_list_entry_get_name(list_entry), sysattr) == 0) {
983 dbg(udev_device->udev, "got '%s' (%s) from cache\n",
984 sysattr, udev_list_entry_get_value(list_entry));
985 return udev_list_entry_get_value(list_entry);
989 util_strscpyl(path, sizeof(path), udev_device_get_syspath(udev_device), "/", sysattr, NULL);
990 if (lstat(path, &statbuf) != 0) {
991 dbg(udev_device->udev, "no attribute '%s', keep negative entry\n", path);
992 udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, NULL, 0, 0);
996 if (S_ISLNK(statbuf.st_mode)) {
997 char target[UTIL_NAME_SIZE];
1001 /* some core links return the last element of the target path */
1002 if (strcmp(sysattr, "driver") != 0 &&
1003 strcmp(sysattr, "subsystem") != 0 &&
1004 strcmp(sysattr, "module") != 0)
1007 len = readlink(path, target, sizeof(target));
1010 pos = strrchr(target, '/');
1013 dbg(udev_device->udev, "cache '%s' with link value '%s'\n", sysattr, pos);
1014 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, pos, 0, 0);
1015 val = udev_list_entry_get_value(list_entry);
1021 /* skip directories */
1022 if (S_ISDIR(statbuf.st_mode))
1025 /* skip non-readable files */
1026 if ((statbuf.st_mode & S_IRUSR) == 0)
1029 /* read attribute value */
1030 fd = open(path, O_RDONLY);
1032 dbg(udev_device->udev, "attribute '%s' can not be opened\n", path);
1035 size = read(fd, value, sizeof(value));
1039 if (size == sizeof(value))
1042 /* got a valid value, store it in cache and return it */
1044 util_remove_trailing_chars(value, '\n');
1045 dbg(udev_device->udev, "'%s' has attribute value '%s'\n", path, value);
1046 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->sysattr_list, sysattr, value, 0, 0);
1047 val = udev_list_entry_get_value(list_entry);
1052 int udev_device_set_syspath(struct udev_device *udev_device, const char *syspath)
1057 free(udev_device->syspath);
1058 udev_device->syspath = strdup(syspath);
1059 if (udev_device->syspath == NULL)
1061 udev_device->devpath = &udev_device->syspath[strlen(udev_get_sys_path(udev_device->udev))];
1062 udev_device_add_property(udev_device, "DEVPATH", udev_device->devpath);
1064 pos = strrchr(udev_device->syspath, '/');
1067 udev_device->sysname = strdup(&pos[1]);
1068 if (udev_device->sysname == NULL)
1071 /* some devices have '!' in their name, change that to '/' */
1073 while (udev_device->sysname[len] != '\0') {
1074 if (udev_device->sysname[len] == '!')
1075 udev_device->sysname[len] = '/';
1079 /* trailing number */
1080 while (len > 0 && isdigit(udev_device->sysname[--len]))
1081 udev_device->sysnum = &udev_device->sysname[len];
1083 /* sysname is completely numeric */
1085 udev_device->sysnum = NULL;
1090 int udev_device_set_subsystem(struct udev_device *udev_device, const char *subsystem)
1092 free(udev_device->subsystem);
1093 udev_device->subsystem = strdup(subsystem);
1094 if (udev_device->subsystem == NULL)
1096 udev_device->subsystem_set = 1;
1097 udev_device_add_property(udev_device, "SUBSYSTEM", udev_device->subsystem);
1101 int udev_device_set_devtype(struct udev_device *udev_device, const char *devtype)
1103 free(udev_device->devtype);
1104 udev_device->devtype = strdup(devtype);
1105 if (udev_device->devtype == NULL)
1107 udev_device->devtype_set = 1;
1108 udev_device_add_property(udev_device, "DEVTYPE", udev_device->devtype);
1112 int udev_device_set_devnode(struct udev_device *udev_device, const char *devnode)
1114 free(udev_device->devnode);
1115 udev_device->devnode = strdup(devnode);
1116 if (devnode == NULL)
1118 if (udev_device->devnode == NULL)
1120 udev_device_add_property(udev_device, "DEVNAME", udev_device->devnode);
1124 int udev_device_add_devlink(struct udev_device *udev_device, const char *devlink, int unique)
1126 struct udev_list_entry *list_entry;
1128 udev_device->devlinks_uptodate = 0;
1129 list_entry = udev_list_entry_add(udev_device->udev, &udev_device->devlinks_list, devlink, NULL, 1, 0);
1130 if (list_entry == NULL)
1133 udev_list_entry_set_flag(list_entry, 1);
1137 struct udev_list_entry *udev_device_add_property(struct udev_device *udev_device, const char *key, const char *value)
1139 udev_device->envp_uptodate = 0;
1140 if (value == NULL) {
1141 struct udev_list_entry *list_entry;
1143 list_entry = udev_device_get_properties_list_entry(udev_device);
1144 list_entry = udev_list_entry_get_by_name(list_entry, key);
1145 if (list_entry != NULL)
1146 udev_list_entry_delete(list_entry);
1149 return udev_list_entry_add(udev_device->udev, &udev_device->properties_list, key, value, 1, 0);
1152 struct udev_list_entry *udev_device_add_property_from_string(struct udev_device *udev_device, const char *property)
1154 char name[UTIL_PATH_SIZE];
1157 util_strscpy(name, sizeof(name), property);
1158 val = strchr(name, '=');
1165 return udev_device_add_property(udev_device, name, val);
1169 * udev_device_get_property_value:
1170 * @udev_device: udev device
1171 * @key: property name
1173 * Returns: the value of a device property, or #NULL if there is no such property.
1175 const char *udev_device_get_property_value(struct udev_device *udev_device, const char *key)
1177 struct udev_list_entry *list_entry;
1179 if (udev_device == NULL)
1184 list_entry = udev_device_get_properties_list_entry(udev_device);
1185 list_entry = udev_list_entry_get_by_name(list_entry, key);
1186 return udev_list_entry_get_value(list_entry);
1189 #define ENVP_SIZE 128
1190 #define MONITOR_BUF_SIZE 4096
1191 static int update_envp_monitor_buf(struct udev_device *udev_device)
1193 struct udev_list_entry *list_entry;
1198 /* monitor buffer of property strings */
1199 free(udev_device->monitor_buf);
1200 udev_device->monitor_buf_len = 0;
1201 udev_device->monitor_buf = malloc(MONITOR_BUF_SIZE);
1202 if (udev_device->monitor_buf == NULL)
1205 /* envp array, strings will point into monitor buffer */
1206 if (udev_device->envp == NULL)
1207 udev_device->envp = malloc(sizeof(char *) * ENVP_SIZE);
1208 if (udev_device->envp == NULL)
1212 s = udev_device->monitor_buf;
1213 l = MONITOR_BUF_SIZE;
1214 udev_list_entry_foreach(list_entry, udev_device_get_properties_list_entry(udev_device)) {
1217 key = udev_list_entry_get_name(list_entry);
1218 /* skip private variables */
1222 /* add string to envp array */
1223 udev_device->envp[i++] = s;
1224 if (i+1 >= ENVP_SIZE)
1227 /* add property string to monitor buffer */
1228 l = util_strpcpyl(&s, l, key, "=", udev_list_entry_get_value(list_entry), NULL);
1233 udev_device->envp[i] = NULL;
1234 udev_device->monitor_buf_len = s - udev_device->monitor_buf;
1235 udev_device->envp_uptodate = 1;
1236 dbg(udev_device->udev, "filled envp/monitor buffer, %u properties, %zu bytes\n",
1237 i, udev_device->monitor_buf_len);
1241 char **udev_device_get_properties_envp(struct udev_device *udev_device)
1243 if (!udev_device->envp_uptodate)
1244 if (update_envp_monitor_buf(udev_device) != 0)
1246 return udev_device->envp;
1249 ssize_t udev_device_get_properties_monitor_buf(struct udev_device *udev_device, const char **buf)
1251 if (!udev_device->envp_uptodate)
1252 if (update_envp_monitor_buf(udev_device) != 0)
1254 *buf = udev_device->monitor_buf;
1255 return udev_device->monitor_buf_len;
1258 int udev_device_set_action(struct udev_device *udev_device, const char *action)
1260 free(udev_device->action);
1261 udev_device->action = strdup(action);
1262 if (udev_device->action == NULL)
1264 udev_device_add_property(udev_device, "ACTION", udev_device->action);
1268 int udev_device_set_driver(struct udev_device *udev_device, const char *driver)
1270 free(udev_device->driver);
1271 udev_device->driver = strdup(driver);
1272 if (udev_device->driver == NULL)
1274 udev_device->driver_set = 1;
1275 udev_device_add_property(udev_device, "DRIVER", udev_device->driver);
1279 const char *udev_device_get_devpath_old(struct udev_device *udev_device)
1281 return udev_device->devpath_old;
1284 int udev_device_set_devpath_old(struct udev_device *udev_device, const char *devpath_old)
1289 free(udev_device->devpath_old);
1290 udev_device->devpath_old = strdup(devpath_old);
1291 if (udev_device->devpath_old == NULL)
1293 udev_device_add_property(udev_device, "DEVPATH_OLD", udev_device->devpath_old);
1295 pos = strrchr(udev_device->devpath_old, '/');
1298 udev_device->sysname_old = strdup(&pos[1]);
1299 if (udev_device->sysname_old == NULL)
1302 /* some devices have '!' in their name, change that to '/' */
1304 while (udev_device->sysname_old[len] != '\0') {
1305 if (udev_device->sysname_old[len] == '!')
1306 udev_device->sysname_old[len] = '/';
1312 const char *udev_device_get_sysname_old(struct udev_device *udev_device)
1314 if (udev_device == NULL)
1316 return udev_device->sysname_old;
1319 const char *udev_device_get_knodename(struct udev_device *udev_device)
1321 return udev_device->knodename;
1324 int udev_device_set_knodename(struct udev_device *udev_device, const char *knodename)
1326 free(udev_device->knodename);
1327 udev_device->knodename = strdup(knodename);
1328 if (udev_device->knodename == NULL)
1330 udev_device_add_property(udev_device, "DEVNAME", udev_device->knodename);
1334 int udev_device_get_timeout(struct udev_device *udev_device)
1336 return udev_device->timeout;
1339 int udev_device_set_timeout(struct udev_device *udev_device, int timeout)
1341 udev_device->timeout = timeout;
1344 int udev_device_get_event_timeout(struct udev_device *udev_device)
1346 if (!udev_device->info_loaded)
1347 device_load_info(udev_device);
1348 return udev_device->event_timeout;
1351 int udev_device_set_event_timeout(struct udev_device *udev_device, int event_timeout)
1353 udev_device->event_timeout = event_timeout;
1357 int udev_device_set_seqnum(struct udev_device *udev_device, unsigned long long int seqnum)
1361 udev_device->seqnum = seqnum;
1362 snprintf(num, sizeof(num), "%llu", seqnum);
1363 udev_device_add_property(udev_device, "SEQNUM", num);
1367 int udev_device_set_devnum(struct udev_device *udev_device, dev_t devnum)
1371 udev_device->devnum = devnum;
1373 snprintf(num, sizeof(num), "%u", major(devnum));
1374 udev_device_add_property(udev_device, "MAJOR", num);
1375 snprintf(num, sizeof(num), "%u", minor(devnum));
1376 udev_device_add_property(udev_device, "MINOR", num);
1380 int udev_device_get_num_fake_partitions(struct udev_device *udev_device)
1382 if (!udev_device->info_loaded)
1383 device_load_info(udev_device);
1384 return udev_device->num_fake_partitions;
1387 int udev_device_set_num_fake_partitions(struct udev_device *udev_device, int num)
1389 udev_device->num_fake_partitions = num;
1393 int udev_device_get_devlink_priority(struct udev_device *udev_device)
1395 if (!udev_device->info_loaded)
1396 device_load_info(udev_device);
1397 return udev_device->devlink_priority;
1400 int udev_device_set_devlink_priority(struct udev_device *udev_device, int prio)
1402 udev_device->devlink_priority = prio;
1406 int udev_device_get_ignore_remove(struct udev_device *udev_device)
1408 if (!udev_device->info_loaded)
1409 device_load_info(udev_device);
1410 return udev_device->ignore_remove;
1413 int udev_device_set_ignore_remove(struct udev_device *udev_device, int ignore)
1415 udev_device->ignore_remove = ignore;
1419 int udev_device_get_watch_handle(struct udev_device *udev_device)
1421 if (!udev_device->info_loaded)
1422 device_load_info(udev_device);
1423 return udev_device->watch_handle;
1426 int udev_device_set_watch_handle(struct udev_device *udev_device, int handle)
1428 udev_device->watch_handle = handle;