How to connect / proc / bus / usb / devices to device / dev / sdX?

I need to know how I can understand which entry in / proc / bus / usb / devices is connected to / dev / sdX. Basically, I need to know the vendor ID and product ID of this USB drive (which may not have a serial number).

In my case, I have this entry for my flash drive in / proc / bus / usb / devices:

T:  Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  6 Spd=480 MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=0781 ProdID=5530 Rev= 2.00
S:  Manufacturer=SanDisk
S:  Product=Cruzer
S:  SerialNumber=0765400A1BD05BEE
C:* #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=200mA
I:* If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage
E:  Ad=81(I) Atr=02(Bulk) MxPS= 512 Ivl=0ms
E:  Ad=02(O) Atr=02(Bulk) MxPS= 512 Ivl=0ms

I know that in my case it is / dev / sda, but I'm not sure how I can figure it out in the code. My first approach was to iterate over all / dev / sdXX devices and issue a SCSI_IOCTL_GET_BUS_NUMBER and / or SCSI_IOCTL_GET_IDLUN request, but the information returned does not help me match:

/tmp # ./getscsiinfo /dev/sda
SCSI bus number: 8
ID: 00
LUN: 00
Channel: 00
Host#: 08
four_in_one: 08000000
host_unique_id: 0

, SCSI ID, LUN, Channel, Host, /proc/bus/usb/devices. SCSI /proc/bus/usb/ 001/006, usbfs ioctl:

/tmp # ./getscsiinfo /proc/bus/usb/001/006
Could not get bus number: Inappropriate ioctl for device

getscsiinfo:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <sys/ioctl.h>

struct scsi_idlun
{
    int four_in_one;
    int host_unique_id;
};

int main(int argc, char** argv) {
    if (argc != 2)
        return 1;

    int fd = open(argv[1], O_RDONLY | O_NONBLOCK);
    if (fd < 0)
    {
        printf("Error opening device: %m\n");
        return 1;
    }

    int busNumber = -1;
    if (ioctl(fd, SCSI_IOCTL_GET_BUS_NUMBER, &busNumber) < 0)
    {
        printf("Could not get bus number: %m\n");
        close(fd);
        return 1;
    }

    printf("SCSI bus number: %d\n", busNumber);

    struct scsi_idlun argid;
    if (ioctl(fd, SCSI_IOCTL_GET_IDLUN, &argid) < 0)
    {
        printf("Could not get id: %m\n");
        close(fd);
        return 1;
    }

    printf("ID: %02x\n", argid.four_in_one & 0xFF);
    printf("LUN: %02x\n", (argid.four_in_one >> 8) & 0xFF);
    printf("Channel: %02x\n", (argid.four_in_one >> 16) & 0xFF);
    printf("Host#: %02x\n", (argid.four_in_one >> 24) & 0xFF);
    printf("four_in_one: %08x\n", (unsigned int)argid.four_in_one);
    printf("host_unique_id: %d\n", argid.host_unique_id);

    close(fd);
    return 0;
}

- ?

+3
5

, ​​ libudev.

: http://www.signal11.us/oss/udev/

- :

.. libudev, , (VID), (PID), , . , libudev , /dev node, ....

+2

udevadm , .

udevadm info -a -p $(udevadm info -q path -n /dev/sda)
, .
+2

proc/bus/usb, usbfs, /proc/scsi/scsi. LUN.

0

( , ). 3.1 ( ).

(, ) - , , USB.

, /sys/block, sdb, :

# cd /sys/block
# cd `readlink sdb`; cd ../../../../../..
# ls -l
total 0
drwxr-xr-x 6 root root     0 Aug 14 10:47 1-1:1.0
-rw-r--r-- 1 root root  4096 Aug 14 10:52 authorized
-rw-r--r-- 1 root root  4096 Aug 14 10:52 avoid_reset_quirk
-r--r--r-- 1 root root  4096 Aug 14 10:47 bcdDevice
-rw-r--r-- 1 root root  4096 Aug 14 10:49 bConfigurationValue
-r--r--r-- 1 root root  4096 Aug 14 10:47 bDeviceClass
-r--r--r-- 1 root root  4096 Aug 14 10:49 bDeviceProtocol
-r--r--r-- 1 root root  4096 Aug 14 10:49 bDeviceSubClass
-r--r--r-- 1 root root  4096 Aug 14 10:49 bmAttributes
-r--r--r-- 1 root root  4096 Aug 14 10:49 bMaxPacketSize0
-r--r--r-- 1 root root  4096 Aug 14 10:49 bMaxPower
-r--r--r-- 1 root root  4096 Aug 14 10:49 bNumConfigurations
-r--r--r-- 1 root root  4096 Aug 14 10:49 bNumInterfaces
-r--r--r-- 1 root root  4096 Aug 14 10:49 busnum
-r--r--r-- 1 root root  4096 Aug 14 10:52 configuration
-r--r--r-- 1 root root 65553 Aug 14 10:47 descriptors
-r--r--r-- 1 root root  4096 Aug 14 10:52 dev
-r--r--r-- 1 root root  4096 Aug 14 10:49 devnum
-r--r--r-- 1 root root  4096 Aug 14 10:52 devpath
lrwxrwxrwx 1 root root     0 Aug 14 10:47 driver -> ../../../../../../bus/usb/drivers/usb
drwxr-xr-x 3 root root     0 Aug 14 10:52 ep_00
-r--r--r-- 1 root root  4096 Aug 14 10:47 idProduct
-r--r--r-- 1 root root  4096 Aug 14 10:47 idVendor
-r--r--r-- 1 root root  4096 Aug 14 10:52 ltm_capable
-r--r--r-- 1 root root  4096 Aug 14 10:47 manufacturer
-r--r--r-- 1 root root  4096 Aug 14 10:49 maxchild
lrwxrwxrwx 1 root root     0 Aug 14 10:52 port -> ../1-0:1.0/port1
drwxr-xr-x 2 root root     0 Aug 14 10:52 power
-r--r--r-- 1 root root  4096 Aug 14 10:47 product
-r--r--r-- 1 root root  4096 Aug 14 10:52 quirks
-r--r--r-- 1 root root  4096 Aug 14 10:47 removable
--w------- 1 root root  4096 Aug 14 10:52 remove
-r--r--r-- 1 root root  4096 Aug 14 10:47 serial
-r--r--r-- 1 root root  4096 Aug 14 10:49 speed
lrwxrwxrwx 1 root root     0 Aug 14 10:47 subsystem -> ../../../../../../bus/usb
-rw-r--r-- 1 root root  4096 Aug 14 10:47 uevent
-r--r--r-- 1 root root  4096 Aug 14 10:52 urbnum
-r--r--r-- 1 root root  4096 Aug 14 10:49 version

( USB , BeyondLogic.)

, USB /proc/bus/usb/devices. , , cat serial , , :

T:  Bus=01 Lev=01 Prnt=01 Port=00 Cnt=01 Dev#=  2 Spd=480 MxCh= 0
D:  Ver= 2.00 Cls=00(>ifc ) Sub=00 Prot=00 MxPS=64 #Cfgs=  1
P:  Vendor=0781 ProdID=5575 Rev=01.26
S:  Manufacturer=SanDisk
S:  Product=Cruzer Glide
S:  SerialNumber=4C530100801115115112
C:  #Ifs= 1 Cfg#= 1 Atr=80 MxPwr=200mA
I:  If#= 0 Alt= 0 #EPs= 2 Cls=08(stor.) Sub=06 Prot=50 Driver=usb-storage

/sys/block, - sysfs . , , , , :

# ls -l sd*
lrwxrwxrwx 1 root root 0 Aug 14 10:45 sda -> ../devices/pci0000:00/0000:00:10.0/host32/target32:0:0/32:0:0:0/block/sda
lrwxrwxrwx 1 root root 0 Aug 14 10:47 sdb -> ../devices/pci0000:00/0000:00:11.0/0000:02:03.0/usb1/1-1/1-1:1.0/host33/target33:0:0/33:0:0:0/block/sdb

, , . . , Pi :

# ls -l sd*
lrwxrwxrwx 1 root root 0 Aug 13 23:54 sda -> ../devices/platform/soc/3f980000.usb/usb1/1-1/1-1.4/1-1.4:1.0/host3/target3:0:0/3:0:0:0/block/sda
lrwxrwxrwx 1 root root 0 Aug 13 23:54 sdb -> ../devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/host4/target4:0:0/4:0:0:0/block/sdb

, , , , USB.

. , .

0
source