gpt4 book ai didi

c - 如何将/proc/bus/usb/devices 条目映射到/dev/sdX 设备?

转载 作者:IT王子 更新时间:2023-10-29 01:24:20 26 4
gpt4 key购买 nike

我需要知道如何找出/proc/bus/usb/devices 中/dev/sdX 设备映射到的条目。基本上,我需要知道给定 U 盘的供应商 ID 和产品 ID(可能没有序列号)。

在我的例子中,我的闪存驱动器在/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

我碰巧知道在我的例子中它是/dev/sda,但我不确定如何在代码中解决这个问题。我的第一种方法是遍历所有/dev/sdXX 设备并发出 SCSI_IOCTL_GET_BUS_NUMBER 和/或 SCSI_IOCTL_GET_IDLUN 请求,但返回的信息无法帮助我匹配它:

/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 、主机将其映射到/proc/bus/usb/devices 中的条目。或者我如何从/proc/bus/usb/001/006 设备获取 SCSI 总线号,这是一个 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;
}

有人知道吗?

最佳答案

我相信您可以使用 libudev 库收集此类信息。

以下是有关它的一些详细信息:http://www.signal11.us/oss/udev/

我在上面的网站上发现了这样的东西:

.. Using libudev, we'll be able to inspect the devices, including their Vendor ID (VID), Product ID (PID), serial number, and device strings, without ever opening the device. Further, libudev will tell us exactly where inside /dev the device's node is located, giving the application a robust and distribution-independent way of accessing the device. ...

关于c - 如何将/proc/bus/usb/devices 条目映射到/dev/sdX 设备?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10653794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com