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.
This commit is contained in:
Romain Vimont 2018-09-04 08:42:25 +02:00
parent 6d2d803003
commit 55d33ddd5f
4 changed files with 35 additions and 30 deletions

View file

@ -1,8 +1,5 @@
#include "command.h"
#ifndef __WINDOWS__
# include <errno.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -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;

View file

@ -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);

View file

@ -9,19 +9,20 @@
#include <unistd.h>
#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

View file

@ -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) {