android usb挂载分析---vold处理FrameWork层发出的消息

from : http://blog.csdn.net/column/details/android-usb.html


Framework层收到消息后,又向vold发送了volume mount的消息,所以vold层又继续着处理这个消息,先看下大概处理流程:


同FrameWork层阻塞在等待vold的消息一样,vold层也在等待着收到 FrameWork层的消息,不过是调用select函数百阻塞,因为这个还有内核可能会有其它的连接请求的到来等,所以不能阻塞。

我们看下代码:

  1. void SocketListener::runListener() {  
  2.     .  
  3.     .  
  4.     .  
  5.     if ((rc = select(max + 1, &read_fds, NULL, NULL, NULL)) < 0) {  
  6.             SLOGE("select failed (%s)", strerror(errno));  
  7.             sleep(1);  
  8.             continue;  
  9.         }   
  10.     .  
  11.     .  
  12.     .  
  13.         if (FD_ISSET(fd, &read_fds)) {  
  14.                     pthread_mutex_unlock(&mClientsLock);  
  15.                     if (!onDataAvailable(*it)) {  
  16.                         close(fd);  
  17.                         pthread_mutex_lock(&mClientsLock);  
  18.                         delete *it;  
  19.                         it = mClients->erase(it);  
  20.                         pthread_mutex_unlock(&mClientsLock);  
  21.                     }  
  22.                     FD_CLR(fd, &read_fds);  
  23.                     pthread_mutex_lock(&mClientsLock);  
  24.                     continue;  
  25.                 }  
  26. }  

收到消息后,调用onDataAvailable,这里这个函数的实现是在FrameworkListener类中,在onDataAvailable中接收数据,并调用dispatchCommand对分发命令:

  1. void FrameworkListener::dispatchCommand(SocketClient *cli, char *data) {  
  2.     .  
  3.     .  
  4.     .  
  5.     for (i = mCommands->begin(); i != mCommands->end(); ++i) {  
  6.         FrameworkCommand *c = *i;  
  7.   
  8.         if (!strcmp(argv[0], c->getCommand())) {  
  9.             if (c->runCommand(cli, argc, argv)) {  
  10.                 SLOGW("Handler '%s' error (%s)", c->getCommand(), strerror(errno));  
  11.             }  
  12.             goto out;  
  13.         }  
  14.     }  
  15. }  

mCommands中的命令是在什么时候加进去的?回顾下CommandListener的初始化,我们注册了很多的命令,对的,就是在注册这些命令的时候加进去的,这里传下来的命令是volume mount ,所以调用 VolumeCmd::runCommand

  1. int CommandListener::VolumeCmd::runCommand(SocketClient *cli,  
  2.                                                       int argc, char **argv) {  
  3.     .  
  4.     .  
  5.     .  
  6.  else if (!strcmp(argv[1], "mount")) {  
  7.         if (argc != 3) {  
  8.             cli->sendMsg(ResponseCode::CommandSyntaxError, "Usage: volume mount <path>"false);  
  9.             return 0;  
  10.         }  
  11.         rc = vm->mountVolume(argv[2]);  
  12. .  
  13. .  
  14. .  
  15. }  

针对mount命令,调用mountVolume,mountVolume中继续调用mountVol:

  1. int Volume::mountVol() {  
  2.     dev_t deviceNodes[4];  
  3.     int n, i, rc = 0;  
  4.     char errmsg[255];  
  5.   
  6.     if (getState() == Volume::State_NoMedia) {  
  7.         snprintf(errmsg, sizeof(errmsg),  
  8.                  "Volume %s %s mount failed - no media",  
  9.                  getLabel(), getMountpoint());  
  10.         mVm->getBroadcaster()->sendBroadcast(  
  11.                                          ResponseCode::VolumeMountFailedNoMedia,  
  12.                                          errmsg, false);  
  13.         errno = ENODEV;  
  14.         return -1;  
  15.     } else if (getState() != Volume::State_Idle) {  
  16.         errno = EBUSY;  
  17.         return -1;  
  18.     }  
  19.   
  20.     if (isMountpointMounted(getMountpoint())) {  
  21.         SLOGW("Volume is idle but appears to be mounted - fixing");  
  22.         setState(Volume::State_Mounted);  
  23.         // mCurrentlyMountedKdev = XXX  
  24.         return 0;  
  25.     }  
  26.   
  27.     n = getDeviceNodes((dev_t *) &deviceNodes, 4);  
  28.     if (!n) {  
  29.         SLOGE("Failed to get device nodes (%s)\n", strerror(errno));  
  30.         return -1;  
  31.     }  
  32.   
  33.     for (i = 0; i < n; i++) {  
  34.         char devicePath[255];  
  35.         int result = 0;  
  36.         const char *disktype = "fat";  
  37.   
  38.         sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]),  
  39.                 MINOR(deviceNodes[i]));  
  40.   
  41.         SLOGI("%s being considered for volume %s\n", devicePath, getLabel());  
  42.   
  43.         errno = 0;  
  44.         setState(Volume::State_Checking);  
  45.   
  46.         result = Fat::check(devicePath);  
  47.         if(result)  
  48.         {  
  49.             result = Ntfs::check(devicePath);  
  50.             if(!result)  
  51.             {  
  52.                  disktype = "ntfs";  
  53.             }  
  54.         }  
  55.                
  56.         if (result) {  
  57.             if (errno == ENODATA) {  
  58.                 SLOGW("%s does not contain a FAT(Ntfs) filesystem\n", devicePath);  
  59.                 continue;  
  60.             }  
  61.             errno = EIO;  
  62.             /* Badness - abort the mount */  
  63.             SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno));  
  64.             setState(Volume::State_Idle);  
  65.             return -1;  
  66.         }  
  67.   
  68.         /* 
  69.          * Mount the device on our internal staging mountpoint so we can 
  70.          * muck with it before exposing it to non priviledged users. 
  71.          */  
  72.         errno = 0;  
  73.         if(0 == strcmp(disktype, "fat"))  
  74.         {             
  75.             if (Fat::doMount(devicePath, "/mnt/secure/staging"falsefalse,false, 1000, 1015, 0702, true)) {  
  76.                 SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));  
  77.                 continue;  
  78.             }  
  79.         }  
  80.         else if(0 == strcmp(disktype, "ntfs"))  
  81.         {  
  82.             if (Ntfs::doMount(devicePath, "/mnt/secure/staging"falsefalse,false, 1000, 1015, 0702, true)) {  
  83.                 SLOGE("%s failed to mount via NTFS (%s)\n", devicePath, strerror(errno));  
  84.                 continue;  
  85.             }  
  86.         }  
  87.   
  88.         SLOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint());  
  89.   
  90.         protectFromAutorunStupidity();  
  91.   
  92.         if (createBindMounts()) {  
  93.             SLOGE("Failed to create bindmounts (%s)", strerror(errno));  
  94.             umount("/mnt/secure/staging");  
  95.             setState(Volume::State_Idle);  
  96.             return -1;  
  97.         }  
  98.   
  99.         /* 
  100.          * Now that the bindmount trickery is done, atomically move the 
  101.          * whole subtree to expose it to non priviledged users. 
  102.          */  
  103.         if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) {  
  104.             SLOGE("Failed to move mount (%s)", strerror(errno));  
  105.             umount("/mnt/secure/staging");  
  106.             setState(Volume::State_Idle);  
  107.             return -1;  
  108.         }  
  109.         setState(Volume::State_Mounted);  
  110.         mCurrentlyMountedKdev = deviceNodes[i];  
  111.         return 0;  
  112.     }  
  113.   
  114.     SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel());  
  115.     setState(Volume::State_Idle);  
  116.   
  117.     return -1;  
  118. }  

mountVol中首先检票Volume的状态,这里面必须为State_Idle状态才会进行后面的操作,这里有一点需要注意下,我们知道,在DirectVolume::handleDiskAdded的时候 向FrameWork层发送VolumeDiskInserted消息,这个时候 FrameWork层才下发volume mount消息,但是这个时候Voleme的State为State_Pending,要等到内核将这块设备的所有分区的add消息发出并调用完handlePartitionAdded才将Volume的状态设为State_Idle,这里会不会发生这种情况:FrameWork消息已经发下来了要进行mount了,但add分区的消息还没处理完,这个时候Volume的状态仍为State_Pending,所以在这里mountVol检查状态的时候不正确,直接返回失败,因为在我们的项目中发现有的时候存储设备会挂载不上,所以这里加了一个延时处理,状态不对时,睡眠一会再处理。状态检查之后调用getDeviceNodes获取有多少分区,然后对所有分区一一进行挂载,注意挂载的时候是先挂载到/mnt/secure/staging,然后现调用doMoveMount移动到挂载点。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值