summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorElliott Hughes2015-08-12 10:18:34 -0500
committerGerrit Code Review2015-08-12 10:18:34 -0500
commit9508dcb9aa543511af90fd61c5f6663ff2a1c4cb (patch)
tree4da27153fe0ed08887e9b87c62cf49bdb89625ea
parent95e7cb5b8e8fa67eb32377325fa6fa8472500baa (diff)
parent155159c545ee1a6d8e3cbea866ea66096f4db5f3 (diff)
downloadplatform-system-core-9508dcb9aa543511af90fd61c5f6663ff2a1c4cb.tar.gz
platform-system-core-9508dcb9aa543511af90fd61c5f6663ff2a1c4cb.tar.xz
platform-system-core-9508dcb9aa543511af90fd61c5f6663ff2a1c4cb.zip
Merge "adb: make stdin/stdout/stderr redirection errors fatal"
-rw-r--r--adb/client/main.cpp30
1 files 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() {
88 DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path); 88 DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
89 if ((nchars >= arraysize(temp_path)) || (nchars == 0)) { 89 if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
90 // If string truncation or some other error. 90 // If string truncation or some other error.
91 // TODO(danalbert): Log the error message from 91 fatal("cannot retrieve temporary file path: %s\n",
92 // FormatMessage(GetLastError()). Pure Windows APIs only touch 92 SystemErrorCodeToString(GetLastError()).c_str());
93 // GetLastError(), C Runtime APIs touch errno, so maybe there should be
94 // WPLOG or PLOGW (which would read GetLastError() instead of errno),
95 // in addition to PLOG, or maybe better to just ignore it and add a
96 // simplified version of FormatMessage() for use in log messages.
97 LOG(ERROR) << "Error creating log file";
98 } 93 }
99 94
100 return narrow(temp_path) + log_name; 95 return narrow(temp_path) + log_name;
@@ -109,19 +104,28 @@ static std::string GetLogFilePath() {
109 104
110static void close_stdin() { 105static void close_stdin() {
111 int fd = unix_open(kNullFileName, O_RDONLY); 106 int fd = unix_open(kNullFileName, O_RDONLY);
112 CHECK_NE(fd, -1); 107 if (fd == -1) {
113 dup2(fd, STDIN_FILENO); 108 fatal("cannot open '%s': %s", kNullFileName, strerror(errno));
109 }
110 if (dup2(fd, STDIN_FILENO) == -1) {
111 fatal("cannot redirect stdin: %s", strerror(errno));
112 }
114 unix_close(fd); 113 unix_close(fd);
115} 114}
116 115
117static void setup_daemon_logging(void) { 116static void setup_daemon_logging(void) {
118 int fd = unix_open(GetLogFilePath().c_str(), O_WRONLY | O_CREAT | O_APPEND, 117 const std::string log_file_path(GetLogFilePath());
118 int fd = unix_open(log_file_path.c_str(), O_WRONLY | O_CREAT | O_APPEND,
119 0640); 119 0640);
120 if (fd == -1) { 120 if (fd == -1) {
121 fd = unix_open(kNullFileName, O_WRONLY); 121 fatal("cannot open '%s': %s", log_file_path.c_str(), strerror(errno));
122 }
123 if (dup2(fd, STDOUT_FILENO) == -1) {
124 fatal("cannot redirect stdout: %s", strerror(errno));
125 }
126 if (dup2(fd, STDERR_FILENO) == -1) {
127 fatal("cannot redirect stderr: %s", strerror(errno));
122 } 128 }
123 dup2(fd, STDOUT_FILENO);
124 dup2(fd, STDERR_FILENO);
125 unix_close(fd); 129 unix_close(fd);
126 130
127#ifdef _WIN32 131#ifdef _WIN32