summaryrefslogtreecommitdiff
path: root/init/util.cpp
diff options
context:
space:
mode:
authorTom Cherry <tomcherry@google.com>2017-05-04 18:17:33 -0700
committerTom Cherry <tomcherry@google.com>2017-05-05 14:37:12 -0700
commit2cbbe9f7a35efdc94e8e34ef92eb6f70a85887fe (patch)
tree215ecac00aa47fbac652e5e7bc99761176884b84 /init/util.cpp
parent517e1f17cfec2143d4d10a64b1496a550acf3ea2 (diff)
init: do not log directly from read_file() and write_file()
Their callers may be able to add more context, so use an error string to record the error. Bug: 38038887 Test: boot bullhead Test: Init unit tests Change-Id: I46690d1c66e00a4b15cadc6fd0d6b50e990388c3
Diffstat (limited to 'init/util.cpp')
-rw-r--r--init/util.cpp28
1 files changed, 18 insertions, 10 deletions
diff --git a/init/util.cpp b/init/util.cpp
index 4df0c25c9..e7f724b8f 100644
--- a/init/util.cpp
+++ b/init/util.cpp
@@ -151,12 +151,14 @@ out_unlink:
return -1;
}
-bool read_file(const std::string& path, std::string* content) {
+bool ReadFile(const std::string& path, std::string* content, std::string* err) {
content->clear();
+ *err = "";
android::base::unique_fd fd(
TEMP_FAILURE_RETRY(open(path.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
if (fd == -1) {
+ *err = "Unable to open '" + path + "': " + strerror(errno);
return false;
}
@@ -164,29 +166,35 @@ bool read_file(const std::string& path, std::string* content) {
// or group-writable files.
struct stat sb;
if (fstat(fd, &sb) == -1) {
- PLOG(ERROR) << "fstat failed for '" << path << "'";
+ *err = "fstat failed for '" + path + "': " + strerror(errno);
return false;
}
if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) {
- LOG(ERROR) << "skipping insecure file '" << path << "'";
+ *err = "Skipping insecure file '" + path + "'";
return false;
}
- return android::base::ReadFdToString(fd, content);
+ if (!android::base::ReadFdToString(fd, content)) {
+ *err = "Unable to read '" + path + "': " + strerror(errno);
+ return false;
+ }
+ return true;
}
-bool write_file(const std::string& path, const std::string& content) {
+bool WriteFile(const std::string& path, const std::string& content, std::string* err) {
+ *err = "";
+
android::base::unique_fd fd(TEMP_FAILURE_RETRY(
open(path.c_str(), O_WRONLY | O_CREAT | O_NOFOLLOW | O_TRUNC | O_CLOEXEC, 0600)));
if (fd == -1) {
- PLOG(ERROR) << "write_file: Unable to open '" << path << "'";
+ *err = "Unable to open '" + path + "': " + strerror(errno);
return false;
}
- bool success = android::base::WriteStringToFd(content, fd);
- if (!success) {
- PLOG(ERROR) << "write_file: Unable to write to '" << path << "'";
+ if (!android::base::WriteStringToFd(content, fd)) {
+ *err = "Unable to write to '" + path + "': " + strerror(errno);
+ return false;
}
- return success;
+ return true;
}
int mkdir_recursive(const std::string& path, mode_t mode, selabel_handle* sehandle) {