65,201
社区成员




现在问题是插入大容量U盘会卡死,系统自动重启,系统版本是android automotive10,查看这个修改 :https://blog.csdn.net/kc58236582/article/details/50911494
目前插入小容量U盘读取时间小于10s就正常,但是U盘容量 越大,文件越多,这个时间就越长,请问还有什么解决办法? 谢谢
关键代码:
//add
class CheckThread:public Thread {
public:
CheckThread(std::vector<std::string> cmd, int fd):
mCmd(cmd),mFd(fd) {
}
virtual ~CheckThread() {
}
virtual bool threadLoop() {
//int rc = ForkExecvp(mCmd, sFsckUntrustedContext);//fork进程,check卡等待结果
int rc = ForkExecvp(mCmd, nullptr, sFsckUntrustedContext);
write(mFd, &rc, sizeof(int));//把结果写入管道
return false;//返回false,不循环了
}
private:
std::vector<std::string> mCmd;
int mFd;
}; //end
status_t Check(const std::string& source) {
int pass = 1;
int rc = 3; //wp add, source is 0
do {
std::vector<std::string> cmd;
cmd.push_back(kFsckPath);
cmd.push_back("-p");
cmd.push_back("-f");
cmd.push_back("-y");
cmd.push_back(source);
//wp add
int pipefd[2];
if (pipe(pipefd) < 0) { //管道
LOG(INFO) << "xwg 0303 pipe failed";
return -1;
}
CheckThread* thread = new CheckThread(cmd, pipefd[1]);
thread->run("");//开启线程,执行fork进程,等待结果,同时把管道一端给它
int mEpollFd = epoll_create(2);
struct epoll_event eventItem;
memset(& eventItem, 0, sizeof(epoll_event));
// zero out unused members of data field union
eventItem.events = EPOLLIN;
eventItem.data.fd = pipefd[0];
epoll_ctl(mEpollFd, EPOLL_CTL_ADD, pipefd[0], & eventItem);
struct epoll_event eventItems[2];
int eventCount = epoll_wait(mEpollFd, eventItems, 2, 10000);//使用epoll机制,设置timeout为10秒,这个10s设置不够灵活,u盘越大读取的会越慢
for (int i = 0; i < eventCount; i++) {
int fd = eventItems[i].data.fd;//有管道数据,说明check好了,返回结果就在管道中
uint32_t epollEvents = eventItems[i].events;
if (fd == pipefd[0]) {
if (epollEvents & EPOLLIN) {
read(fd, &rc, sizeof(int));
}
}
}//直接timeout了,说明10秒还没有check完
close(mEpollFd);
close(pipefd[0]);//关闭fd
close(pipefd[1]);
//end
// Fat devices are currently always untrusted
// rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext); //wp kill
if (rc < 0) {
LOG(ERROR) << "Filesystem check failed due to logwrap error";
errno = EIO;
return -1;
}
switch (rc) {
case 0:
LOG(INFO) << "Filesystem check completed OK";
return 0;
case 2:
LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
errno = ENODATA;
return -1;
case 3://3说明check超时了
//SLOGE("xwg 0303 Filesystem check outime");
LOG(INFO) << "xwg 0303 Filesystem check outime";
errno = ENODATA;
return -1;
case 4:
if (pass++ <= 3) {
LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")";
continue;
}
LOG(ERROR) << "Failing check after too many rechecks";
errno = EIO;
return -1;
case 8:
LOG(ERROR) << "Filesystem check failed (no filesystem)";
errno = ENODATA;
return -1;
default:
LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
errno = EIO;
return -1;
}
} while (0);
return 0;
}
class CheckThread:public Thread {
public:
CheckThread(std::vector<std::string> cmd, int fd):
mCmd(cmd),mFd(fd) {
}
virtual ~CheckThread() {
}
virtual bool threadLoop() {
//int rc = ForkExecvp(mCmd, sFsckUntrustedContext);//fork进程,check卡等待结果
int rc = ForkExecvp(mCmd, nullptr, sFsckUntrustedContext);
write(mFd, &rc, sizeof(int));//把结果写入管道
return false;//返回false,不循环了
}
private:
std::vector<std::string> mCmd;
int mFd;
}; //wp end
status_t Check(const std::string& source) {
int pass = 1;
int rc = 3; //wp add, source is 0
do {
std::vector<std::string> cmd;
cmd.push_back(kFsckPath);
cmd.push_back("-p");
cmd.push_back("-f");
cmd.push_back("-y");
cmd.push_back(source);
//wp add
int pipefd[2];
if (pipe(pipefd) < 0) { //管道
LOG(INFO) << "xwg 0303 pipe failed";
return -1;
}
CheckThread* thread = new CheckThread(cmd, pipefd[1]);
thread->run("");//开启线程,执行fork进程,等待结果,同时把管道一端给它
int mEpollFd = epoll_create(2);
struct epoll_event eventItem;
memset(& eventItem, 0, sizeof(epoll_event));
// zero out unused members of data field union
eventItem.events = EPOLLIN;
eventItem.data.fd = pipefd[0];
epoll_ctl(mEpollFd, EPOLL_CTL_ADD, pipefd[0], & eventItem);
struct epoll_event eventItems[2];
int eventCount = epoll_wait(mEpollFd, eventItems, 2, 10000);//使用epoll机制,设置timeout为10秒,这个10s设置不够灵活,u盘越大读取的会越慢
for (int i = 0; i < eventCount; i++) {
int fd = eventItems[i].data.fd;//有管道数据,说明check好了,返回结果就在管道中
uint32_t epollEvents = eventItems[i].events;
if (fd == pipefd[0]) {
if (epollEvents & EPOLLIN) {
read(fd, &rc, sizeof(int));
}
}
}//直接timeout了,说明10秒还没有check完
close(mEpollFd);
close(pipefd[0]);//关闭fd
close(pipefd[1]);
//wp end
// Fat devices are currently always untrusted
// rc = ForkExecvp(cmd, nullptr, sFsckUntrustedContext); //wp kill
if (rc < 0) {
LOG(ERROR) << "Filesystem check failed due to logwrap error";
errno = EIO;
return -1;
}
switch (rc) {
case 0:
LOG(INFO) << "Filesystem check completed OK";
return 0;
case 2:
LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
errno = ENODATA;
return -1;
case 3://3说明check超时了
//SLOGE("xwg 0303 Filesystem check outime");
LOG(INFO) << "xwg 0303 Filesystem check outime";
errno = ENODATA;
return -1;
case 4:
if (pass++ <= 3) {
LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")";
continue;
}
LOG(ERROR) << "Failing check after too many rechecks";
errno = EIO;
return -1;
case 8:
LOG(ERROR) << "Filesystem check failed (no filesystem)";
errno = ENODATA;
return -1;
default:
LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
errno = EIO;
return -1;
}
} while (0);
return 0;
}
研究了一下改了一下有效果了,现在有两个问题
1.如何kill线程
2.插入u盘是只读不可写,改如何解决?
//add
class CheckThread:public Thread {
public:
CheckThread(std::vector<std::string> cmd, int fd):
mCmd(cmd),mFd(fd) {
}
virtual ~CheckThread() {
}
virtual bool threadLoop() {
//int rc = ForkExecvp(mCmd, sFsckUntrustedContext);//fork进程,check卡等待结果
int rc = ForkExecvp(mCmd, nullptr, sFsckUntrustedContext);
write(mFd, &rc, sizeof(int));//把结果写入管道
LOG(WARNING) << "xwg:rc = " << rc;
return false;//返回false,不循环了
}
private:
std::vector<std::string> mCmd;
int mFd;
}; //end
status_t Check(const std::string& source) {
int pass = 1;
int rc = 3;
do {
std::vector<std::string> cmd;
cmd.push_back(kFsckPath);
cmd.push_back("-p");
cmd.push_back("-f");
cmd.push_back("-y");
cmd.push_back(source);
//add
int pipefd[2];
if (pipe(pipefd) < 0) { //管道
LOG(INFO) << "xwg 0303 pipe failed";
return -1;
}
CheckThread* thread = new CheckThread(cmd, pipefd[1]);
thread->run("");//开启线程,执行fork进程,等待结果,同时把管道一端给它
int mEpollFd = epoll_create(2);
struct epoll_event eventItem;
memset(& eventItem, 0, sizeof(epoll_event));
// zero out unused members of data field union
eventItem.events = EPOLLIN;
eventItem.data.fd = pipefd[0];
epoll_ctl(mEpollFd, EPOLL_CTL_ADD, pipefd[0], & eventItem);
struct epoll_event eventItems[2];
int eventCount = epoll_wait(mEpollFd, eventItems, 2, 10000);//使用epoll机制,设置timeout为5min
LOG(WARNING) << "xwg:eventConut = "<<eventCount;
for (int i = 0; i < eventCount; i++) {
LOG(WARNING) << "xwg:int i = "<<i;
int fd = eventItems[i].data.fd;//有管道数据,说明check好了,返回结果就在管道中
LOG(WARNING) << "xwg:fd = "<<fd;
uint32_t epollEvents = eventItems[i].events;
if (fd == pipefd[0]) {
if (epollEvents & EPOLLIN) {
read(fd, &rc, sizeof(int));
}
}
}//end for,直接timeout了,说明10秒还没有check完
close(mEpollFd);
close(pipefd[0]);//关闭fd
close(pipefd[1]);
//end
// Fat devices are currently always untrusted
rc = 0;
if (rc < 0) {
LOG(ERROR) << "Filesystem check failed due to logwrap error";
errno = EIO;
return -1;
}
switch (rc) {
case 0:
LOG(INFO) << "Filesystem check completed OK";
return 0;
case 2:
LOG(ERROR) << "Filesystem check failed (not a FAT filesystem)";
errno = ENODATA;
return -1;
case 3://3说明check超时了,add
//SLOGE("xwg 0303 Filesystem check outime");
LOG(INFO) << "xwg 0303 Filesystem check outime";
errno = ENODATA;
return -1;
case 4:
if (pass++ <= 3) {
LOG(WARNING) << "Filesystem modified - rechecking (pass " << pass << ")";
continue;
}
LOG(ERROR) << "Failing check after too many rechecks";
errno = EIO;
return -1;
case 8:
LOG(ERROR) << "Filesystem check failed (no filesystem)";
errno = ENODATA;
return -1;
default:
LOG(ERROR) << "Filesystem check failed (unknown exit code " << rc << ")";
errno = EIO;
return -1;
}
} while (0);
return 0;
}