summaryrefslogtreecommitdiff
path: root/client/main.cpp
diff options
context:
space:
mode:
authorSpencer Low <CompareAndSwap@gmail.com>2015-05-20 23:17:26 -0700
committerSpencer Low <compareandswap@gmail.com>2015-06-02 21:04:56 +0000
commit9e0c6616ea866de4e1ec7fa701839a34466f7118 (patch)
treeaed3c1cd78f581def1238b0d1c54356350396ba8 /client/main.cpp
parent59ab92cdd28a9872b5cfe382ab2b47636958f775 (diff)
downloadadb-9e0c6616ea866de4e1ec7fa701839a34466f7118.tar.gz
adb: win32: fix logging to adb.log
In the adb client, redirect stdin and stderr of the adb server to `nul', so that when the adb server starts up, it avoids issues in the C Runtime where it closes stderr, making it hard to properly reopen. There are probably other ways to avoid this issue, but I think this is the cleanest that will keep working over the years and will exercise the most commonly used code-paths in the C Runtime. Fix some adb_close() calls to be unix_close() (only really matters on Windows). Make stderr non-buffered on Windows, to match the (sensible) Linux behavior. Change-Id: I1b15c64240e50dbeb56788b0d0d901f4536ad788 Signed-off-by: Spencer Low <CompareAndSwap@gmail.com>
Diffstat (limited to 'client/main.cpp')
-rw-r--r--client/main.cpp20
1 files changed, 17 insertions, 3 deletions
diff --git a/client/main.cpp b/client/main.cpp
index 0cd6670..c018b8a 100644
--- a/client/main.cpp
+++ b/client/main.cpp
@@ -110,7 +110,7 @@ static void close_stdin() {
int fd = unix_open(kNullFileName, O_RDONLY);
CHECK_NE(fd, -1);
dup2(fd, STDIN_FILENO);
- adb_close(fd);
+ unix_close(fd);
}
static void setup_daemon_logging(void) {
@@ -121,7 +121,13 @@ static void setup_daemon_logging(void) {
}
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
- adb_close(fd);
+ unix_close(fd);
+
+#ifdef _WIN32
+ // On Windows, stderr is buffered by default, so switch to non-buffered
+ // to match Linux.
+ setvbuf(stderr, NULL, _IONBF, 0);
+#endif
fprintf(stderr, "--- adb starting (pid %d) ---\n", getpid());
}
@@ -153,7 +159,15 @@ int adb_main(int is_daemon, int server_port) {
// Inform our parent that we are up and running.
// TODO(danalbert): Can't use SendOkay because we're sending "OK\n", not
// "OKAY".
- // TODO(danalbert): Why do we use stdout for Windows?
+ // TODO(danalbert): Why do we use stdout for Windows? There is a
+ // comment in launch_server() that suggests that non-Windows uses
+ // stderr because it is non-buffered. So perhaps the history is that
+ // stdout was preferred for all platforms, but it was discovered that
+ // non-Windows needed a non-buffered fd, so stderr was used there.
+ // Note that using stderr on unix means that if you do
+ // `ADB_TRACE=all adb start-server`, it will say "ADB server didn't ACK"
+ // and "* failed to start daemon *" because the adb server will write
+ // logging to stderr, obscuring the OK\n output that is sent to stderr.
#if defined(_WIN32)
int reply_fd = STDOUT_FILENO;
// Change stdout mode to binary so \n => \r\n translation does not