android10 修改vold check sd卡同步改成异步机制问题请教

yhm2046 2022-03-04 10:07:36

现在问题是插入大容量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;
}

 

...全文
535 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yhm2046 2022-03-05
  • 打赏
  • 举报
回复

研究了一下改了一下有效果了,现在有两个问题
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;
}

65,201

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧