From 0426ae885c13e5901a4994f01086839a3bc13728 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 17 Nov 2021 18:47:20 +0100 Subject: [PATCH] Make "adb get-serialno" interruptible All process executions must be interruptible, so that Ctrl+c reacts immediately. --- app/src/adb.c | 35 +++-------------------------------- app/src/adb.h | 10 +++++++--- app/src/server.c | 25 ++++++++++++++++++++++++- 3 files changed, 34 insertions(+), 36 deletions(-) diff --git a/app/src/adb.c b/app/src/adb.c index a8712bf4..d98efe6f 100644 --- a/app/src/adb.c +++ b/app/src/adb.c @@ -233,37 +233,8 @@ adb_install(const char *serial, const char *local) { return pid; } -static ssize_t -adb_execute_for_output(const char *serial, const char *const adb_cmd[], - size_t adb_cmd_len, char *buf, size_t buf_len, - const char *name) { - sc_pipe pout; - sc_pid pid = adb_execute_p(serial, adb_cmd, adb_cmd_len, NULL, &pout, NULL); - if (pid == SC_PROCESS_NONE) { - return -1; - } - - ssize_t r = sc_pipe_read_all(pout, buf, buf_len); - sc_pipe_close(pout); - - if (!sc_process_check_success(pid, name, true)) { - return -1; - } - - return r; -} - -char * -adb_get_serialno(void) { - char buf[128]; - +sc_pid +adb_get_serialno(sc_pipe *pout) { const char *const adb_cmd[] = {"get-serialno"}; - ssize_t r = adb_execute_for_output(NULL, adb_cmd, ARRAY_LEN(adb_cmd), - buf, sizeof(buf), "get-serialno"); - if (r <= 0) { - return NULL; - } - - sc_str_truncate(buf, r, "\r\n"); - return strdup(buf); + return adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), NULL, pout, NULL); } diff --git a/app/src/adb.h b/app/src/adb.h index 085b3e6b..d3b3faae 100644 --- a/app/src/adb.h +++ b/app/src/adb.h @@ -35,8 +35,12 @@ adb_push(const char *serial, const char *local, const char *remote); sc_pid adb_install(const char *serial, const char *local); -// Return the result of "adb get-serialno". -char * -adb_get_serialno(void); +/** + * Execute `adb get-serialno` + * + * The result can be read from the output parameter `pout`. + */ +sc_pid +adb_get_serialno(sc_pipe *pout); #endif diff --git a/app/src/server.c b/app/src/server.c index 56ba6b68..5ad8d70f 100644 --- a/app/src/server.c +++ b/app/src/server.c @@ -422,6 +422,29 @@ sc_server_on_terminated(void *userdata) { LOGD("Server terminated"); } +static char * +sc_server_get_serialno(struct sc_intr *intr) { + sc_pipe pout; + sc_pid pid = adb_get_serialno(&pout); + if (pid == SC_PROCESS_NONE) { + return false; + } + + char buf[128]; + ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf)); + sc_pipe_close(pout); + + bool ok = + sc_process_check_success_intr(intr, pid, "adb get-serialno", true); + if (!ok) { + return NULL; + } + + sc_str_truncate(buf, r, " \r\n"); + + return strdup(buf); +} + static bool sc_server_fill_serial(struct sc_server *server) { // Retrieve the actual device immediately if not provided, so that all @@ -430,7 +453,7 @@ sc_server_fill_serial(struct sc_server *server) { // device/emulator" error) if (!server->params.serial) { // The serial is owned by sc_server_params, and will be freed on destroy - server->params.serial = adb_get_serialno(); + server->params.serial = sc_server_get_serialno(&server->intr); if (!server->params.serial) { LOGE("Could not get device serial"); return false;