Merge changes I40575081,I1ca8f8cf,I38bfd273

am: 1c6a56b27f

Change-Id: I8410e8cb691eb0b5e3e721b6b715eb30f28eef51
This commit is contained in:
Paul Crowley 2019-04-05 12:15:24 -07:00 committed by android-build-merger
commit e6c7dffaa8
4 changed files with 35 additions and 37 deletions

View file

@ -60,10 +60,10 @@
#include <android-base/unique_fd.h> #include <android-base/unique_fd.h>
using android::base::StringPrintf; using android::base::StringPrintf;
using android::base::WriteStringToFile;
using android::fs_mgr::GetEntryForMountPoint; using android::fs_mgr::GetEntryForMountPoint;
using android::vold::kEmptyAuthentication; using android::vold::kEmptyAuthentication;
using android::vold::KeyBuffer; using android::vold::KeyBuffer;
using android::vold::writeStringToFile;
namespace { namespace {
@ -351,18 +351,14 @@ bool fscrypt_initialize_global_de() {
std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode; std::string modestring = device_ref.contents_mode + ":" + device_ref.filenames_mode;
std::string mode_filename = std::string("/data") + fscrypt_key_mode; std::string mode_filename = std::string("/data") + fscrypt_key_mode;
if (!android::base::WriteStringToFile(modestring, mode_filename)) { if (!android::vold::writeStringToFile(modestring, mode_filename)) return false;
PLOG(ERROR) << "Cannot save type";
return false;
}
std::string ref_filename = std::string("/data") + fscrypt_key_ref; std::string ref_filename = std::string("/data") + fscrypt_key_ref;
if (!android::base::WriteStringToFile(device_ref.key_raw_ref, ref_filename)) { if (!android::vold::writeStringToFile(device_ref.key_raw_ref, ref_filename)) return false;
PLOG(ERROR) << "Cannot save key reference to:" << ref_filename;
return false;
}
LOG(INFO) << "Wrote system DE key reference to:" << ref_filename; LOG(INFO) << "Wrote system DE key reference to:" << ref_filename;
if (!android::vold::FsyncDirectory(device_key_dir)) return false;
s_global_de_initialized = true; s_global_de_initialized = true;
return true; return true;
} }
@ -419,7 +415,7 @@ static void drop_caches() {
// Clean any dirty pages (otherwise they won't be dropped). // Clean any dirty pages (otherwise they won't be dropped).
sync(); sync();
// Drop inode and page caches. // Drop inode and page caches.
if (!WriteStringToFile("3", "/proc/sys/vm/drop_caches")) { if (!writeStringToFile("3", "/proc/sys/vm/drop_caches")) {
PLOG(ERROR) << "Failed to drop caches during key eviction"; PLOG(ERROR) << "Failed to drop caches during key eviction";
} }
} }

View file

@ -147,33 +147,6 @@ static bool readFileToString(const std::string& filename, std::string* result) {
return true; return true;
} }
static bool writeStringToFile(const std::string& payload, const std::string& filename) {
android::base::unique_fd fd(TEMP_FAILURE_RETRY(
open(filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0666)));
if (fd == -1) {
PLOG(ERROR) << "Failed to open " << filename;
return false;
}
if (!android::base::WriteStringToFd(payload, fd)) {
PLOG(ERROR) << "Failed to write to " << filename;
unlink(filename.c_str());
return false;
}
// fsync as close won't guarantee flush data
// see close(2), fsync(2) and b/68901441
if (fsync(fd) == -1) {
if (errno == EROFS || errno == EINVAL) {
PLOG(WARNING) << "Skip fsync " << filename
<< " on a file system does not support synchronization";
} else {
PLOG(ERROR) << "Failed to fsync " << filename;
unlink(filename.c_str());
return false;
}
}
return true;
}
static bool readRandomBytesOrLog(size_t count, std::string* out) { static bool readRandomBytesOrLog(size_t count, std::string* out) {
auto status = ReadRandomBytes(count, *out); auto status = ReadRandomBytes(count, *out);
if (status != OK) { if (status != OK) {

View file

@ -42,6 +42,7 @@
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h>
#include <list> #include <list>
#include <mutex> #include <mutex>
@ -840,5 +841,32 @@ bool FsyncDirectory(const std::string& dirname) {
return true; return true;
} }
bool writeStringToFile(const std::string& payload, const std::string& filename) {
android::base::unique_fd fd(TEMP_FAILURE_RETRY(
open(filename.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0666)));
if (fd == -1) {
PLOG(ERROR) << "Failed to open " << filename;
return false;
}
if (!android::base::WriteStringToFd(payload, fd)) {
PLOG(ERROR) << "Failed to write to " << filename;
unlink(filename.c_str());
return false;
}
// fsync as close won't guarantee flush data
// see close(2), fsync(2) and b/68901441
if (fsync(fd) == -1) {
if (errno == EROFS || errno == EINVAL) {
PLOG(WARNING) << "Skip fsync " << filename
<< " on a file system does not support synchronization";
} else {
PLOG(ERROR) << "Failed to fsync " << filename;
unlink(filename.c_str());
return false;
}
}
return true;
}
} // namespace vold } // namespace vold
} // namespace android } // namespace android

View file

@ -134,6 +134,7 @@ status_t WaitForFile(const char* filename, std::chrono::nanoseconds timeout);
bool FsyncDirectory(const std::string& dirname); bool FsyncDirectory(const std::string& dirname);
bool writeStringToFile(const std::string& payload, const std::string& filename);
} // namespace vold } // namespace vold
} // namespace android } // namespace android