Merge "adb: make stdin/stdout/stderr redirection errors fatal"

This commit is contained in:
Elliott Hughes 2015-08-12 15:18:34 +00:00 committed by Gerrit Code Review
commit 9508dcb9aa

View file

@ -88,13 +88,8 @@ static std::string GetLogFilePath() {
DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path); DWORD nchars = GetTempPathW(arraysize(temp_path), temp_path);
if ((nchars >= arraysize(temp_path)) || (nchars == 0)) { if ((nchars >= arraysize(temp_path)) || (nchars == 0)) {
// If string truncation or some other error. // If string truncation or some other error.
// TODO(danalbert): Log the error message from fatal("cannot retrieve temporary file path: %s\n",
// FormatMessage(GetLastError()). Pure Windows APIs only touch SystemErrorCodeToString(GetLastError()).c_str());
// 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";
} }
return narrow(temp_path) + log_name; return narrow(temp_path) + log_name;
@ -109,19 +104,28 @@ static std::string GetLogFilePath() {
static void close_stdin() { static void close_stdin() {
int fd = unix_open(kNullFileName, O_RDONLY); int fd = unix_open(kNullFileName, O_RDONLY);
CHECK_NE(fd, -1); if (fd == -1) {
dup2(fd, STDIN_FILENO); fatal("cannot open '%s': %s", kNullFileName, strerror(errno));
}
if (dup2(fd, STDIN_FILENO) == -1) {
fatal("cannot redirect stdin: %s", strerror(errno));
}
unix_close(fd); unix_close(fd);
} }
static void setup_daemon_logging(void) { 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); 0640);
if (fd == -1) { 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); unix_close(fd);
#ifdef _WIN32 #ifdef _WIN32