#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <scsi/sg.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <memory.h>
void show_vendor(struct sg_io_hdr * hdr) {
unsigned char * buffer = hdr->dxferp;
int i;
printf("vendor id:");
for (i=8; i<16; ++i) {
putchar(buffer[i]);
}
putchar('\n');
}
void show_product(struct sg_io_hdr * hdr) {
unsigned char * buffer = hdr->dxferp;
int i;
printf("product id:");
for (i=16; i<32; ++i) {
putchar(buffer[i]);
}
putchar('\n');
}
void show_product_rev(struct sg_io_hdr * hdr) {
unsigned char * buffer = hdr->dxferp;
int i;
printf("product ver:");
for (i=32; i<36; ++i) {
putchar(buffer[i]);
}
putchar('\n');
}
int main()
{
struct sg_io_hdr io_hdr;
unsigned char cdb[6];
unsigned char sense[32];
unsigned char buff[65535];
int dev_fd;
int errno;
char pathname[20]="/dev/sda";
dev_fd= open(pathname, O_RDWR | O_NONBLOCK);
if (dev_fd < 0 && errno == EROFS)
dev_fd = open(pathname, O_RDONLY | O_NONBLOCK);
if (dev_fd<0)
{
printf("open device error");
return 0;
}
cdb[0]=0x12;
cdb[4]=36;
memset(&io_hdr,0,sizeof(struct sg_io_hdr));
io_hdr.interface_id='S';
io_hdr.cmd_len=sizeof(cdb);
io_hdr.mx_sb_len=sizeof(sense);
io_hdr.dxfer_len=64;
io_hdr.dxferp=buff;
io_hdr.cmdp=cdb;
io_hdr.sbp=sense;
io_hdr.timeout=60;
io_hdr.dxfer_direction=SG_DXFER_FROM_DEV;
if(ioctl(dev_fd,SG_IO,&io_hdr)<0){
printf("ioctl error!\n");
return 0;
}
char* p=(char*)io_hdr.dxferp;
int i;
for(i=0;i<io_hdr.dxfer_len;i++){
putchar(*(p+i));
}
show_vendor(&io_hdr);
show_product(&io_hdr);
show_product_rev(&io_hdr);
}
linux下读取磁盘设备号以其序列号信息
5星 · 超过95%的资源 需积分: 48 119 浏览量
2012-09-20
12:54:46
上传
评论
收藏 1KB RAR 举报
克劳思倜√
- 粉丝: 0
- 资源: 7
- 1
- 2
- 3
前往页