Skip to content

Commit

Permalink
Use inherit flags for adb commands
Browse files Browse the repository at this point in the history
Explicitly indicate, for each adb call, if stdout and stderr must be
inherited.
  • Loading branch information
rom1v committed Nov 24, 2021
1 parent 922733c commit 8a096d2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 53 deletions.
74 changes: 39 additions & 35 deletions app/src/adb.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {

static sc_pid
adb_execute_p(const char *serial, const char *const adb_cmd[],
size_t len, sc_pipe *pout) {
size_t len, unsigned inherit, sc_pipe *pout) {
int i;
sc_pid pid;

Expand All @@ -134,10 +134,6 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],

memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
argv[len + i] = NULL;
unsigned inherit = SC_STDERR;
if (!pout) {
inherit |= SC_STDOUT;
}
enum sc_process_result r =
sc_process_execute_p(argv, &pid, inherit, NULL, pout, NULL);
if (r != SC_PROCESS_SUCCESS) {
Expand All @@ -150,50 +146,54 @@ adb_execute_p(const char *serial, const char *const adb_cmd[],
}

sc_pid
adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
return adb_execute_p(serial, adb_cmd, len, NULL);
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
unsigned inherit) {
return adb_execute_p(serial, adb_cmd, len, inherit, NULL);
}

static sc_pid
adb_exec_forward(const char *serial, uint16_t local_port,
const char *device_socket_name) {
const char *device_socket_name, unsigned inherit) {
char local[4 + 5 + 1]; // tcp:PORT
char remote[108 + 14 + 1]; // localabstract:NAME
sprintf(local, "tcp:%" PRIu16, local_port);
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
const char *const adb_cmd[] = {"forward", local, remote};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit);
}

static sc_pid
adb_exec_forward_remove(const char *serial, uint16_t local_port) {
adb_exec_forward_remove(const char *serial, uint16_t local_port,
unsigned inherit) {
char local[4 + 5 + 1]; // tcp:PORT
sprintf(local, "tcp:%" PRIu16, local_port);
const char *const adb_cmd[] = {"forward", "--remove", local};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit);
}

static sc_pid
adb_exec_reverse(const char *serial, const char *device_socket_name,
uint16_t local_port) {
uint16_t local_port, unsigned inherit) {
char local[4 + 5 + 1]; // tcp:PORT
char remote[108 + 14 + 1]; // localabstract:NAME
sprintf(local, "tcp:%" PRIu16, local_port);
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
const char *const adb_cmd[] = {"reverse", remote, local};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit);
}

static sc_pid
adb_exec_reverse_remove(const char *serial, const char *device_socket_name) {
adb_exec_reverse_remove(const char *serial, const char *device_socket_name,
unsigned inherit) {
char remote[108 + 14 + 1]; // localabstract:NAME
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
const char *const adb_cmd[] = {"reverse", "--remove", remote};
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit);
}

static sc_pid
adb_exec_push(const char *serial, const char *local, const char *remote) {
adb_exec_push(const char *serial, const char *local, const char *remote,
unsigned inherit) {
#ifdef __WINDOWS__
// Windows will parse the string, so the paths must be quoted
// (see sys/win/command.c)
Expand All @@ -209,7 +209,7 @@ adb_exec_push(const char *serial, const char *local, const char *remote) {
#endif

const char *const adb_cmd[] = {"push", local, remote};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit);

#ifdef __WINDOWS__
free((void *) remote);
Expand All @@ -220,7 +220,7 @@ adb_exec_push(const char *serial, const char *local, const char *remote) {
}

static sc_pid
adb_exec_install(const char *serial, const char *local) {
adb_exec_install(const char *serial, const char *local, unsigned inherit) {
#ifdef __WINDOWS__
// Windows will parse the string, so the local name must be quoted
// (see sys/win/command.c)
Expand All @@ -231,7 +231,7 @@ adb_exec_install(const char *serial, const char *local) {
#endif

const char *const adb_cmd[] = {"install", "-r", local};
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), inherit);

#ifdef __WINDOWS__
free((void *) local);
Expand All @@ -241,58 +241,62 @@ adb_exec_install(const char *serial, const char *local) {
}

static sc_pid
adb_exec_get_serialno(sc_pipe *pout) {
adb_exec_get_serialno(unsigned inherit, sc_pipe *pout) {
const char *const adb_cmd[] = {"get-serialno"};
return adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), pout);
return adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), inherit, pout);
}

bool
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
const char *device_socket_name) {
sc_pid pid = adb_exec_forward(serial, local_port, device_socket_name);
const char *device_socket_name, unsigned inherit) {
sc_pid pid =
adb_exec_forward(serial, local_port, device_socket_name, inherit);
return sc_process_check_success_intr(intr, pid, "adb forward", true);
}

bool
adb_forward_remove(struct sc_intr *intr, const char *serial,
uint16_t local_port) {
sc_pid pid = adb_exec_forward_remove(serial, local_port);
uint16_t local_port, unsigned inherit) {
sc_pid pid = adb_exec_forward_remove(serial, local_port, inherit);
return sc_process_check_success_intr(intr, pid, "adb forward --remove",
true);
}

bool
adb_reverse(struct sc_intr *intr, const char *serial,
const char *device_socket_name, uint16_t local_port) {
sc_pid pid = adb_exec_reverse(serial, device_socket_name, local_port);
const char *device_socket_name, uint16_t local_port,
unsigned inherit) {
sc_pid pid =
adb_exec_reverse(serial, device_socket_name, local_port, inherit);
return sc_process_check_success_intr(intr, pid, "adb reverse", true);
}

bool
adb_reverse_remove(struct sc_intr *intr, const char *serial,
const char *device_socket_name) {
sc_pid pid = adb_exec_reverse_remove(serial, device_socket_name);
const char *device_socket_name, unsigned inherit) {
sc_pid pid = adb_exec_reverse_remove(serial, device_socket_name, inherit);
return sc_process_check_success_intr(intr, pid, "adb reverse --remove",
true);
}

bool
adb_push(struct sc_intr *intr, const char *serial, const char *local,
const char *remote) {
sc_pid pid = adb_exec_push(serial, local, remote);
const char *remote, unsigned inherit) {
sc_pid pid = adb_exec_push(serial, local, remote, inherit);
return sc_process_check_success_intr(intr, pid, "adb push", true);
}

bool
adb_install(struct sc_intr *intr, const char *serial, const char *local) {
sc_pid pid = adb_exec_install(serial, local);
adb_install(struct sc_intr *intr, const char *serial, const char *local,
unsigned inherit) {
sc_pid pid = adb_exec_install(serial, local, inherit);
return sc_process_check_success_intr(intr, pid, "adb install", true);
}

char *
adb_get_serialno(struct sc_intr *intr) {
adb_get_serialno(struct sc_intr *intr, unsigned inherit) {
sc_pipe pout;
sc_pid pid = adb_exec_get_serialno(&pout);
sc_pid pid = adb_exec_get_serialno(inherit, &pout);
if (pid == SC_PROCESS_NONE) {
LOGE("Could not execute \"adb get-serialno\"");
return NULL;
Expand Down
19 changes: 11 additions & 8 deletions app/src/adb.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,40 @@
#include "util/intr.h"

sc_pid
adb_execute(const char *serial, const char *const adb_cmd[], size_t len);
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
unsigned inherit);

bool
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
const char *device_socket_name);
const char *device_socket_name, unsigned inherit);

bool
adb_forward_remove(struct sc_intr *intr, const char *serial,
uint16_t local_port);
uint16_t local_port, unsigned inherit);

bool
adb_reverse(struct sc_intr *intr, const char *serial,
const char *device_socket_name, uint16_t local_port);
const char *device_socket_name, uint16_t local_port,
unsigned inherit);

bool
adb_reverse_remove(struct sc_intr *intr, const char *serial,
const char *device_socket_name);
const char *device_socket_name, unsigned inherit);

bool
adb_push(struct sc_intr *intr, const char *serial, const char *local,
const char *remote);
const char *remote, unsigned inherit);

bool
adb_install(struct sc_intr *intr, const char *serial, const char *local);
adb_install(struct sc_intr *intr, const char *serial, const char *local,
unsigned inherit);

/**
* Execute `adb get-serialno`
*
* Return the result, to be freed by the caller, or NULL on error.
*/
char *
adb_get_serialno(struct sc_intr *intr);
adb_get_serialno(struct sc_intr *intr, unsigned inherit);

#endif
10 changes: 5 additions & 5 deletions app/src/adb_tunnel.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
struct sc_port_range port_range) {
uint16_t port = port_range.first;
for (;;) {
if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port)) {
if (!adb_reverse(intr, serial, SC_SOCKET_NAME, port, SC_STDERR)) {
// the command itself failed, it will fail on any port
return false;
}
Expand Down Expand Up @@ -51,7 +51,7 @@ enable_tunnel_reverse_any_port(struct sc_adb_tunnel *tunnel,
}

// failure, disable tunnel and try another port
if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME)) {
if (!adb_reverse_remove(intr, serial, SC_SOCKET_NAME, SC_STDERR)) {
LOGW("Could not remove reverse tunnel on port %" PRIu16, port);
}

Expand Down Expand Up @@ -81,7 +81,7 @@ enable_tunnel_forward_any_port(struct sc_adb_tunnel *tunnel,

uint16_t port = port_range.first;
for (;;) {
if (adb_forward(intr, serial, port, SC_SOCKET_NAME)) {
if (adb_forward(intr, serial, port, SC_SOCKET_NAME, SC_STDERR)) {
// success
tunnel->local_port = port;
tunnel->enabled = true;
Expand Down Expand Up @@ -146,9 +146,9 @@ sc_adb_tunnel_close(struct sc_adb_tunnel *tunnel, struct sc_intr *intr,

bool ret;
if (tunnel->forward) {
ret = adb_forward_remove(intr, serial, tunnel->local_port);
ret = adb_forward_remove(intr, serial, tunnel->local_port, SC_STDERR);
} else {
ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME);
ret = adb_reverse_remove(intr, serial, SC_SOCKET_NAME, SC_STDERR);

assert(tunnel->server_socket != SC_SOCKET_NONE);
if (!net_close(tunnel->server_socket)) {
Expand Down
6 changes: 4 additions & 2 deletions app/src/file_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,17 @@ run_file_handler(void *data) {

if (req.action == ACTION_INSTALL_APK) {
LOGI("Installing %s...", req.file);
bool ok = adb_install(intr, serial, req.file);
bool ok =
adb_install(intr, serial, req.file, SC_STDOUT | SC_STDERR);
if (ok) {
LOGI("%s successfully installed", req.file);
} else {
LOGE("Failed to install %s", req.file);
}
} else {
LOGI("Pushing %s...", req.file);
bool ok = adb_push(intr, serial, req.file, push_target);
bool ok = adb_push(intr, serial, req.file, push_target,
SC_STDOUT | SC_STDERR);
if (ok) {
LOGI("%s successfully pushed to %s", req.file, push_target);
} else {
Expand Down
8 changes: 5 additions & 3 deletions app/src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ push_server(struct sc_intr *intr, const char *serial) {
free(server_path);
return false;
}
bool ok = adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH);
bool ok = adb_push(intr, serial, server_path, SC_DEVICE_SERVER_PATH,
SC_STDOUT | SC_STDERR);
free(server_path);
return ok;
}
Expand Down Expand Up @@ -234,7 +235,8 @@ execute_server(struct sc_server *server,
// Port: 5005
// Then click on "Debug"
#endif
pid = adb_execute(params->serial, cmd, count);
// Inherit both stdout and stderr (all server logs are printed to stdout)
pid = adb_execute(params->serial, cmd, count, SC_STDOUT | SC_STDERR);

end:
for (unsigned i = dyn_idx; i < count; ++i) {
Expand Down Expand Up @@ -482,7 +484,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->intr);
server->params.serial = adb_get_serialno(&server->intr, SC_STDERR);
if (!server->params.serial) {
LOGE("Could not get device serial");
return false;
Expand Down

0 comments on commit 8a096d2

Please sign in to comment.