图 vold模块简要的层次结构图
图 U盘挂载事件的函数调用时序图
其中,在PublicVolume接收到StorageManagerService中的mount方法执行sdcard命令时,会传入相应的参数,如下代码所示:
if (execl(kFusePath, kFusePath,
"-u", "1023", // AID_MEDIA_RW
"-g", "1023", // AID_MEDIA_RW
"-U", std::to_string(getMountUserId()).c_str(),
mRawPath.c_str(),
stableName.c_str(),
NULL)) {
PLOG(ERROR) << "Failed to exec";
}
其中u, g, U等最终会传递给系统调用mount,因此在这条调用中加入w参数,最终挂载的U盘便对上层应用开放了完整的权限,如下:
if (execl(kFusePath, kFusePath,
"-u", "1023", // AID_MEDIA_RW
"-g", "1023", // AID_MEDIA_RW
"-U", std::to_string(getMountUserId()).c_str(),
"-w", // WRITE permission of USB storage is needed
mRawPath.c_str(),
stableName.c_str(),
NULL)) {
PLOG(ERROR) << "Failed to exec";
}