diff --git a/init/util.cpp b/init/util.cpp index 888a36652..c98718101 100644 --- a/init/util.cpp +++ b/init/util.cpp @@ -163,7 +163,7 @@ out_unlink: bool read_file(const char* path, std::string* content) { content->clear(); - int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY|O_NOFOLLOW|O_CLOEXEC)); + android::base::unique_fd fd(TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NOFOLLOW | O_CLOEXEC))); if (fd == -1) { return false; } @@ -176,17 +176,16 @@ bool read_file(const char* path, std::string* content) { return false; } if ((sb.st_mode & (S_IWGRP | S_IWOTH)) != 0) { - PLOG(ERROR) << "skipping insecure file '" << path << "'"; + LOG(ERROR) << "skipping insecure file '" << path << "'"; return false; } - bool okay = android::base::ReadFdToString(fd, content); - close(fd); - return okay; + return android::base::ReadFdToString(fd, content); } bool write_file(const char* path, const char* content) { - int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY|O_CREAT|O_NOFOLLOW|O_CLOEXEC, 0600)); + android::base::unique_fd fd( + TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0600))); if (fd == -1) { PLOG(ERROR) << "write_file: Unable to open '" << path << "'"; return false; @@ -195,7 +194,6 @@ bool write_file(const char* path, const char* content) { if (!success) { PLOG(ERROR) << "write_file: Unable to write to '" << path << "'"; } - close(fd); return success; }