Ensure that cached oat and image files are world readable like odex files
(cherry picked from commit 389ca5b86e1411c28b785408a7a4b8ac7837355c)
Change-Id: I619601e9549764a11b78d782725a1dab5c80afb3
diff --git a/src/class_linker.cc b/src/class_linker.cc
index 5bd214a..b73c2b0 100644
--- a/src/class_linker.cc
+++ b/src/class_linker.cc
@@ -684,52 +684,6 @@
return NULL;
}
-class LockedFd {
- public:
- static LockedFd* CreateAndLock(std::string& name, mode_t mode) {
- int fd = open(name.c_str(), O_CREAT | O_RDWR, mode);
- if (fd == -1) {
- PLOG(ERROR) << "Failed to open file '" << name << "'";
- return NULL;
- }
- fchmod(fd, mode);
-
- LOG(INFO) << "locking file " << name << " (fd=" << fd << ")";
- // try to lock non-blocking so we can log if we need may need to block
- int result = flock(fd, LOCK_EX | LOCK_NB);
- if (result == -1) {
- LOG(WARNING) << "sleeping while locking file " << name;
- // retry blocking
- result = flock(fd, LOCK_EX);
- }
- if (result == -1) {
- PLOG(ERROR) << "Failed to lock file '" << name << "'";
- close(fd);
- return NULL;
- }
- return new LockedFd(fd);
- }
-
- int GetFd() const {
- return fd_;
- }
-
- ~LockedFd() {
- if (fd_ != -1) {
- int result = flock(fd_, LOCK_UN);
- if (result == -1) {
- PLOG(WARNING) << "flock(" << fd_ << ", LOCK_UN) failed";
- }
- close(fd_);
- }
- }
-
- private:
- explicit LockedFd(int fd) : fd_(fd) {}
-
- int fd_;
-};
-
static const DexFile* FindDexFileInOatLocation(const std::string& dex_location,
uint32_t dex_location_checksum,
const std::string& oat_location) {
diff --git a/src/dex2oat.cc b/src/dex2oat.cc
index 47193e5..0d3fa58 100644
--- a/src/dex2oat.cc
+++ b/src/dex2oat.cc
@@ -16,6 +16,7 @@
#include <stdio.h>
#include <stdlib.h>
+#include <sys/stat.h>
#include <iostream>
#include <fstream>
@@ -639,7 +640,8 @@
// Check early that the result of compilation can be written
UniquePtr<File> oat_file;
- if (!oat_filename.empty()) {
+ bool create_file = !oat_filename.empty(); // as opposed to using open file descriptor
+ if (create_file) {
oat_file.reset(OS::OpenFile(oat_filename.c_str(), true));
if (oat_location.empty()) {
oat_location = oat_filename;
@@ -648,7 +650,11 @@
oat_file.reset(OS::FileFromFd(oat_location.c_str(), oat_fd));
}
if (oat_file.get() == NULL) {
- PLOG(ERROR) << "Unable to create oat file: " << oat_location;
+ PLOG(ERROR) << "Failed to create oat file: " << oat_location;
+ return EXIT_FAILURE;
+ }
+ if (create_file && fchmod(oat_file->Fd(), 0644) != 0) {
+ PLOG(ERROR) << "Failed to make oat file world readable: " << oat_location;
return EXIT_FAILURE;
}
diff --git a/src/image_writer.cc b/src/image_writer.cc
index 9e01d01..d1328c1 100644
--- a/src/image_writer.cc
+++ b/src/image_writer.cc
@@ -17,6 +17,7 @@
#include "image_writer.h"
#include <sys/mman.h>
+#include <sys/stat.h>
#include <vector>
@@ -92,6 +93,10 @@
LOG(ERROR) << "Failed to open image file " << image_filename;
return false;
}
+ if (fchmod(file->Fd(), 0644) != 0) {
+ PLOG(ERROR) << "Failed to make image file world readable: " << image_filename;
+ return EXIT_FAILURE;
+ }
bool success = file->WriteFully(image_->Begin(), image_end_);
if (!success) {
PLOG(ERROR) << "Failed to write image file " << image_filename;