C++实现Linux下弹出U盘的方法
2016-07-13 18:08
483 查看
转载http://www.jb51.net/article/69806.htm
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <linux/fd.h>
#include <sys/mount.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <scsi/scsi_ioctl.h>
int main(int argc, char *argv[])
{
int fd = -1;
char *device;
if (argc != 2)
{
printf("usage: usb-s /dev/sde1");
return -1;
}
device = strdup(argv[1]);
if ((fd = open(device, O_RDONLY|O_NONBLOCK)) < 0)
{
printf("open device %s failed!\n", device);
free(device);
return -1;
}
int status, k;
sg_io_hdr_t io_hdr;
unsigned char allowRmBlk[6] = {ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0};
unsigned char startStop1Blk[6] = {START_STOP, 0, 0, 0, 1, 0};
unsigned char startStop2Blk[6] = {START_STOP, 0, 0, 0, 2, 0};
unsigned char inqBuff[2];
unsigned char sense_buffer[32];
if ((ioctl(fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
printf("not an sg device, or old sg driver\n");
goto out;
}
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = 6;
io_hdr.mx_sb_len = sizeof(sense_buffer);
io_hdr.dxfer_direction = SG_DXFER_NONE;
io_hdr.dxfer_len = 0;
io_hdr.dxferp = inqBuff;
io_hdr.sbp = sense_buffer;
io_hdr.timeout = 10000;
io_hdr.cmdp = allowRmBlk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
{
goto out;
}
io_hdr.cmdp = startStop1Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
{
goto out;
}
io_hdr.cmdp = startStop2Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
{
goto out;
}
/* force kernel to reread partition table when new disc inserted */
status = ioctl(fd, BLKRRPART);
out:
close(fd);
free(device);
return 0;
}
运行
g++ -g -Wall main.cpp -o usb-s
sudo usb-s /dev/sdb1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <string.h>
#include <linux/fd.h>
#include <sys/mount.h>
#include <scsi/scsi.h>
#include <scsi/sg.h>
#include <scsi/scsi_ioctl.h>
int main(int argc, char *argv[])
{
int fd = -1;
char *device;
if (argc != 2)
{
printf("usage: usb-s /dev/sde1");
return -1;
}
device = strdup(argv[1]);
if ((fd = open(device, O_RDONLY|O_NONBLOCK)) < 0)
{
printf("open device %s failed!\n", device);
free(device);
return -1;
}
int status, k;
sg_io_hdr_t io_hdr;
unsigned char allowRmBlk[6] = {ALLOW_MEDIUM_REMOVAL, 0, 0, 0, 0, 0};
unsigned char startStop1Blk[6] = {START_STOP, 0, 0, 0, 1, 0};
unsigned char startStop2Blk[6] = {START_STOP, 0, 0, 0, 2, 0};
unsigned char inqBuff[2];
unsigned char sense_buffer[32];
if ((ioctl(fd, SG_GET_VERSION_NUM, &k) < 0) || (k < 30000)) {
printf("not an sg device, or old sg driver\n");
goto out;
}
memset(&io_hdr, 0, sizeof(sg_io_hdr_t));
io_hdr.interface_id = 'S';
io_hdr.cmd_len = 6;
io_hdr.mx_sb_len = sizeof(sense_buffer);
io_hdr.dxfer_direction = SG_DXFER_NONE;
io_hdr.dxfer_len = 0;
io_hdr.dxferp = inqBuff;
io_hdr.sbp = sense_buffer;
io_hdr.timeout = 10000;
io_hdr.cmdp = allowRmBlk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
{
goto out;
}
io_hdr.cmdp = startStop1Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
{
goto out;
}
io_hdr.cmdp = startStop2Blk;
status = ioctl(fd, SG_IO, (void *)&io_hdr);
if (status < 0)
{
goto out;
}
/* force kernel to reread partition table when new disc inserted */
status = ioctl(fd, BLKRRPART);
out:
close(fd);
free(device);
return 0;
}
运行
g++ -g -Wall main.cpp -o usb-s
sudo usb-s /dev/sdb1
相关文章推荐
- Linux基础(5)Linux的用户,组管理以及Linux的下的权限浅谈
- CentOS7 yum install mysql-server 没有可用包
- 使程序在Linux下后台运行
- Linux命令(14)文件和文件夹权限管理:chmod
- Linux 技巧:让进程在后台可靠运行的几种方法
- CentOS基础命令使用技巧-1
- DSM -- 进入linux文件系统
- SELinux 基本操作命令
- 第7章 Linux档案与目录管理
- Linux QT环境的搭建
- Linux设备驱动之字符设备(三)
- Ubuntu 14.04 ssh服务的配置——亲测方便可用
- linux有用命令
- linux命令应用—crontab每秒执行一次命令
- Linux 常用命令记录--2016年7月13日
- grep设置查找颜色高亮
- [笔记]linux下和windows下的 创建线程函数
- Java——调用Linux和Windows下命令行执行程序
- epoll系列系统调用
- CentOS7 卡在开机界面进不去登录界面