From 155159c545ee1a6d8e3cbea866ea66096f4db5f3 Mon Sep 17 00:00:00 2001 From: Spencer Low Date: Tue, 11 Aug 2015 16:08:43 -0700 Subject: [PATCH] adb: make stdin/stdout/stderr redirection errors fatal Make these fatal errors: - Win32 GetTempPathW() failures. - Errors opening /dev/null (and don't use LOG(FATAL) for this error since that will do a crash-dump on Windows which isn't appropriate for a transient runtime error). - Errors with dup2. - Errors opening adb.log. Change-Id: Ided76a5436d8c6f059d8f6799c49ba04c87181ae Signed-off-by: Spencer Low --- adb/client/main.cpp | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/adb/client/main.cpp b/adb/client/main.cpp index 73acbb0b8..7469744fa 100644 --- a/adb/client/main.cpp +++ b/adb/client/main.cpp @@ -88,13 +88,8 @@ static std::string GetLogFilePath() { DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path); if ((nchars >= arraysize(temp_path)) || (nchars == 0)) { // If string truncation or some other error. - // TODO(danalbert): Log the error message from - // FormatMessage(GetLastError()). Pure Windows APIs only touch - // GetLastError(), C Runtime APIs touch errno, so maybe there should be - // WPLOG or PLOGW (which would read GetLastError() instead of errno), - // in addition to PLOG, or maybe better to just ignore it and add a - // simplified version of FormatMessage() for use in log messages. - LOG(ERROR) << "Error creating log file"; + fatal("cannot retrieve temporary file path: %s\n", + SystemErrorCodeToString(GetLastError()).c_str()); } return narrow(temp_path) + log_name; @@ -109,19 +104,28 @@ static std::string GetLogFilePath() { static void close_stdin() { int fd = unix_open(kNullFileName, O_RDONLY); - CHECK_NE(fd, -1); - dup2(fd, STDIN_FILENO); + if (fd == -1) { + fatal("cannot open '%s': %s", kNullFileName, strerror(errno)); + } + if (dup2(fd, STDIN_FILENO) == -1) { + fatal("cannot redirect stdin: %s", strerror(errno)); + } unix_close(fd); } static void setup_daemon_logging(void) { - int fd = unix_open(GetLogFilePath().c_str(), O_WRONLY | O_CREAT | O_APPEND, + const std::string log_file_path(GetLogFilePath()); + int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND, 0640); if (fd == -1) { - fd = unix_open(kNullFileName, O_WRONLY); + fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno)); + } + if (dup2(fd, STDOUT_FILENO) == -1) { + fatal("cannot redirect stdout: %s", strerror(errno)); + } + if (dup2(fd, STDERR_FILENO) == -1) { + fatal("cannot redirect stderr: %s", strerror(errno)); } - dup2(fd, STDOUT_FILENO); - dup2(fd, STDERR_FILENO); unix_close(fd); #ifdef _WIN32