c++ - how to list USB mass storage devices programatically using libudev in Linux? -
i doing project mass storage devices in linux. trying write application list connected usb mass storage devices , give notification when new mass storage device plugged in. using libudev purpose. have used code found in "http://www.signal11.us/oss/udev/" . have done modification here
/* create list of devices in 'block' subsystem. */ enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "block"); udev_enumerate_scan_devices(enumerate); devices = udev_enumerate_get_list_entry(enumerate);
the problem list block devices. want usb mass storage devices. how list. 1 more problem how label of usb storage devices using libudev.
one solution match devices following criteria:
- subsystem == "scsi", devtype == "scsi_device"
- child device exists subsystem == "block"
- child device exists subsystem == "scsi_disk"
- parent device exists subsystem == "usb", devtype == "usb_device"
here example program (also available on github):
#include <libudev.h> #include <stdio.h> static struct udev_device* get_child( struct udev* udev, struct udev_device* parent, const char* subsystem) { struct udev_device* child = null; struct udev_enumerate *enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_parent(enumerate, parent); udev_enumerate_add_match_subsystem(enumerate, subsystem); udev_enumerate_scan_devices(enumerate); struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate); struct udev_list_entry *entry; udev_list_entry_foreach(entry, devices) { const char *path = udev_list_entry_get_name(entry); child = udev_device_new_from_syspath(udev, path); break; } udev_enumerate_unref(enumerate); return child; } static void enumerate_usb_mass_storage(struct udev* udev) { struct udev_enumerate* enumerate = udev_enumerate_new(udev); udev_enumerate_add_match_subsystem(enumerate, "scsi"); udev_enumerate_add_match_property(enumerate, "devtype", "scsi_device"); udev_enumerate_scan_devices(enumerate); struct udev_list_entry *devices = udev_enumerate_get_list_entry(enumerate); struct udev_list_entry *entry; udev_list_entry_foreach(entry, devices) { const char* path = udev_list_entry_get_name(entry); struct udev_device* scsi = udev_device_new_from_syspath(udev, path); struct udev_device* block = get_child(udev, scsi, "block"); struct udev_device* scsi_disk = get_child(udev, scsi, "scsi_disk"); struct udev_device* usb = udev_device_get_parent_with_subsystem_devtype( scsi, "usb", "usb_device"); if (block && scsi_disk && usb) { printf("block = %s, usb = %s:%s, scsi = %s\n", udev_device_get_devnode(block), udev_device_get_sysattr_value(usb, "idvendor"), udev_device_get_sysattr_value(usb, "idproduct"), udev_device_get_sysattr_value(scsi, "vendor")); } if (block) udev_device_unref(block); if (scsi_disk) udev_device_unref(scsi_disk); udev_device_unref(scsi); } udev_enumerate_unref(enumerate); } int main() { struct udev* udev = udev_new(); enumerate_usb_mass_storage(udev); udev_unref(udev); return 0; }
and it's output external disk:
block = /dev/sdb, usb = 0bc2:ab20, scsi = seagate
Comments
Post a Comment