From 55d33ddd5fb0b559eed33d8f47d9b07a2456979a Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 4 Sep 2018 08:42:25 +0200 Subject: [PATCH] Do not handle system-specific values in command.c The common command.c handled process errors from system-specific int values (errno). Rather, expose a new enum process_result to handle error cause in a generic way. --- app/src/command.c | 24 ++++++++---------------- app/src/command.h | 8 +++++++- app/src/sys/unix/command.c | 25 ++++++++++++++++--------- app/src/sys/win/command.c | 8 ++++---- 4 files changed, 35 insertions(+), 30 deletions(-) diff --git a/app/src/command.c b/app/src/command.c index 011a2b85..cc11a0de 100644 --- a/app/src/command.c +++ b/app/src/command.c @@ -1,8 +1,5 @@ #include "command.h" -#ifndef __WINDOWS__ -# include -#endif #include #include #include @@ -21,24 +18,19 @@ static inline const char *get_adb_command() { return adb_command; } -static void show_err_msg(int err) { -#ifdef __WINDOWS__ - (void) err; // unused - LOGE("Failed to execute adb"); -#else +static void show_adb_err_msg(enum process_result err) { switch (err) { - case -1: + case PROCESS_ERROR_GENERIC: LOGE("Failed to execute adb"); break; - case ENOENT: + case PROCESS_ERROR_MISSING_BINARY: LOGE("'adb' command not found (make it accessible from your PATH " "or define its full path in the ADB environment variable)"); break; - default: - LOGE("Failed to execute adb: %s", strerror(err)); + case PROCESS_SUCCESS: + /* do nothing */ break; } -#endif } process_t adb_execute(const char *serial, const char *const adb_cmd[], int len) { @@ -56,9 +48,9 @@ process_t adb_execute(const char *serial, const char *const adb_cmd[], int len) memcpy(&cmd[i], adb_cmd, len * sizeof(const char *)); cmd[len + i] = NULL; - int r = cmd_execute(cmd[0], cmd, &process); - if (r != 0) { - show_err_msg(r); + enum process_result r = cmd_execute(cmd[0], cmd, &process); + if (r != PROCESS_SUCCESS) { + show_adb_err_msg(r); return PROCESS_NONE; } return process; diff --git a/app/src/command.h b/app/src/command.h index dfc0d58a..3e0fcca6 100644 --- a/app/src/command.h +++ b/app/src/command.h @@ -32,7 +32,13 @@ #endif # define NO_EXIT_CODE -1 -int cmd_execute(const char *path, const char *const argv[], process_t *process); +enum process_result { + PROCESS_SUCCESS, + PROCESS_ERROR_GENERIC, + PROCESS_ERROR_MISSING_BINARY, +}; + +enum process_result cmd_execute(const char *path, const char *const argv[], process_t *process); SDL_bool cmd_terminate(process_t pid); SDL_bool cmd_simple_wait(process_t pid, exit_code_t *exit_code); diff --git a/app/src/sys/unix/command.c b/app/src/sys/unix/command.c index 20218cb9..ed9bbeac 100644 --- a/app/src/sys/unix/command.c +++ b/app/src/sys/unix/command.c @@ -9,19 +9,20 @@ #include #include "log.h" -int cmd_execute(const char *path, const char *const argv[], pid_t *pid) { +enum process_result cmd_execute(const char *path, const char *const argv[], pid_t *pid) { int fd[2]; - int ret = 0; if (pipe(fd) == -1) { perror("pipe"); - return -1; + return PROCESS_ERROR_GENERIC; } + enum process_result ret = PROCESS_SUCCESS; + *pid = fork(); if (*pid == -1) { perror("fork"); - ret = -1; + ret = PROCESS_ERROR_GENERIC; goto end; } @@ -30,9 +31,9 @@ int cmd_execute(const char *path, const char *const argv[], pid_t *pid) { close(fd[1]); fd[1] = -1; // wait for EOF or receive errno from child - if (read(fd[0], &ret, sizeof(int)) == -1) { + if (read(fd[0], &ret, sizeof(ret)) == -1) { perror("read"); - ret = -1; + ret = PROCESS_ERROR_GENERIC; goto end; } } else if (*pid == 0) { @@ -40,12 +41,18 @@ int cmd_execute(const char *path, const char *const argv[], pid_t *pid) { close(fd[0]); if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) == 0) { execvp(path, (char *const *)argv); + if (errno == ENOENT) { + ret = PROCESS_ERROR_MISSING_BINARY; + } else { + ret = PROCESS_ERROR_GENERIC; + } + perror("exec"); } else { perror("fcntl"); + ret = PROCESS_ERROR_GENERIC; } - // send errno to the parent - ret = errno; - if (write(fd[1], &ret, sizeof(int)) == -1) { + // send ret to the parent + if (write(fd[1], &ret, sizeof(ret)) == -1) { perror("write"); } // close write side before exiting diff --git a/app/src/sys/win/command.c b/app/src/sys/win/command.c index cea928b4..7a29442d 100644 --- a/app/src/sys/win/command.c +++ b/app/src/sys/win/command.c @@ -4,7 +4,7 @@ #include "log.h" #include "str_util.h" -int cmd_execute(const char *path, const char *const argv[], HANDLE *handle) { +enum process_result cmd_execute(const char *path, const char *const argv[], HANDLE *handle) { STARTUPINFO si; PROCESS_INFORMATION pi; memset(&si, 0, sizeof(si)); @@ -19,7 +19,7 @@ int cmd_execute(const char *path, const char *const argv[], HANDLE *handle) { if (ret >= sizeof(cmd)) { LOGE("Command too long (%" PRIsizet " chars)", sizeof(cmd) - 1); *handle = NULL; - return -1; + return PROCESS_ERROR_GENERIC; } #ifdef WINDOWS_NOCONSOLE @@ -29,11 +29,11 @@ int cmd_execute(const char *path, const char *const argv[], HANDLE *handle) { #endif if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi)) { *handle = NULL; - return -1; + return PROCESS_ERROR_GENERIC; } *handle = pi.hProcess; - return 0; + return PROCESS_SUCCESS; } SDL_bool cmd_terminate(HANDLE handle) {