From 3bf6fd2894c283a976320b8f0862a63be0d78635 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 25 Nov 2021 21:33:50 +0100 Subject: [PATCH] Workaround "adb connect" error detection "adb connect" always returns successfully (with exit code 0), even in case of failure. As a workaround, capture its output and check if it starts with "connected". PR #2827 --- app/src/adb.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/app/src/adb.c b/app/src/adb.c index cb4f14da..819066c0 100644 --- a/app/src/adb.c +++ b/app/src/adb.c @@ -318,8 +318,37 @@ bool adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) { const char *const adb_cmd[] = {"connect", ip_port}; - sc_pid pid = adb_execute(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags); - return process_check_success_intr(intr, pid, "adb connect", flags); + sc_pipe pout; + sc_pid pid = adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout); + if (pid == SC_PROCESS_NONE) { + LOGE("Could not execute \"adb connect\""); + return false; + } + + // "adb connect" always returns successfully (with exit code 0), even in + // case of failure. As a workaround, check if its output starts with + // "connected". + char buf[128]; + ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf)); + sc_pipe_close(pout); + + bool ok = process_check_success_intr(intr, pid, "adb connect", flags); + if (!ok) { + return false; + } + + if (r == -1) { + return false; + } + + ok = !strncmp("connected", buf, sizeof("connected") - 1); + if (!ok && !(flags & SC_ADB_NO_STDERR)) { + // "adb connect" also prints errors to stdout. Since we capture it, + // re-print the error to stderr. + sc_str_truncate(buf, r, "\r\n"); + fprintf(stderr, "%s\n", buf); + } + return ok; } bool