圖 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";
}