diff --git a/Utils.cpp b/Utils.cpp index cdca85e..04c3956 100644 --- a/Utils.cpp +++ b/Utils.cpp @@ -19,6 +19,7 @@ #include "Process.h" #include "sehandle.h" +#include #include #include #include @@ -40,13 +41,16 @@ #include #include #include + #include #include +#include #ifndef UMOUNT_NOFOLLOW #define UMOUNT_NOFOLLOW 0x00000008 /* Don't follow symlink on umount */ #endif +using namespace std::chrono_literals; using android::base::ReadFileToString; using android::base::StringPrintf; @@ -788,5 +792,20 @@ status_t UnmountTree(const std::string& prefix) { return OK; } +// TODO(118708649): fix duplication with init/util.h +status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout) { + android::base::Timer t; + while (t.duration() < timeout) { + struct stat sb; + if (stat(filename, &sb) != -1) { + LOG(INFO) << "wait for '" << filename << "' took " << t; + return 0; + } + std::this_thread::sleep_for(10ms); + } + LOG(WARNING) << "wait for '" << filename << "' timed out and took " << t; + return -1; +} + } // namespace vold } // namespace android diff --git a/Utils.h b/Utils.h index 48d605a..4d3522a 100644 --- a/Utils.h +++ b/Utils.h @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -132,6 +133,8 @@ bool IsRunningInEmulator(); status_t UnmountTree(const std::string& prefix); +status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout); + } // namespace vold } // namespace android diff --git a/cryptfs.cpp b/cryptfs.cpp index 6e449ac..5be29be 100644 --- a/cryptfs.cpp +++ b/cryptfs.cpp @@ -30,6 +30,7 @@ #include "Keymaster.h" #include "Process.h" #include "ScryptParameters.h" +#include "Utils.h" #include "VoldUtil.h" #include "VolumeManager.h" #include "secontext.h" @@ -73,6 +74,8 @@ extern "C" { #include } +using namespace std::chrono_literals; + #define UNUSED __attribute__((unused)) #define DM_CRYPT_BUF_SIZE 4096 @@ -1099,6 +1102,12 @@ static int create_crypto_blk_dev(struct crypt_mnt_ftr* crypt_ftr, const unsigned goto errout; } + /* Ensure the dm device has been created before returning. */ + if (android::vold::WaitForFile(crypto_blk_name, 1s) < 0) { + // WaitForFile generates a suitable log message + goto errout; + } + /* We made it here with no errors. Woot! */ retval = 0;