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:
parent
6d2d803003
commit
55d33ddd5f
4 changed files with 35 additions and 30 deletions
|
@ -1,8 +1,5 @@
|
||||||
#include "command.h"
|
#include "command.h"
|
||||||
|
|
||||||
#ifndef __WINDOWS__
|
|
||||||
# include <errno.h>
|
|
||||||
#endif
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -21,24 +18,19 @@ static inline const char *get_adb_command() {
|
||||||
return adb_command;
|
return adb_command;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void show_err_msg(int err) {
|
static void show_adb_err_msg(enum process_result err) {
|
||||||
#ifdef __WINDOWS__
|
|
||||||
(void) err; // unused
|
|
||||||
LOGE("Failed to execute adb");
|
|
||||||
#else
|
|
||||||
switch (err) {
|
switch (err) {
|
||||||
case -1:
|
case PROCESS_ERROR_GENERIC:
|
||||||
LOGE("Failed to execute adb");
|
LOGE("Failed to execute adb");
|
||||||
break;
|
break;
|
||||||
case ENOENT:
|
case PROCESS_ERROR_MISSING_BINARY:
|
||||||
LOGE("'adb' command not found (make it accessible from your PATH "
|
LOGE("'adb' command not found (make it accessible from your PATH "
|
||||||
"or define its full path in the ADB environment variable)");
|
"or define its full path in the ADB environment variable)");
|
||||||
break;
|
break;
|
||||||
default:
|
case PROCESS_SUCCESS:
|
||||||
LOGE("Failed to execute adb: %s", strerror(err));
|
/* do nothing */
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
process_t adb_execute(const char *serial, const char *const adb_cmd[], int len) {
|
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 *));
|
memcpy(&cmd[i], adb_cmd, len * sizeof(const char *));
|
||||||
cmd[len + i] = NULL;
|
cmd[len + i] = NULL;
|
||||||
int r = cmd_execute(cmd[0], cmd, &process);
|
enum process_result r = cmd_execute(cmd[0], cmd, &process);
|
||||||
if (r != 0) {
|
if (r != PROCESS_SUCCESS) {
|
||||||
show_err_msg(r);
|
show_adb_err_msg(r);
|
||||||
return PROCESS_NONE;
|
return PROCESS_NONE;
|
||||||
}
|
}
|
||||||
return process;
|
return process;
|
||||||
|
|
|
@ -32,7 +32,13 @@
|
||||||
#endif
|
#endif
|
||||||
# define NO_EXIT_CODE -1
|
# 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_terminate(process_t pid);
|
||||||
SDL_bool cmd_simple_wait(process_t pid, exit_code_t *exit_code);
|
SDL_bool cmd_simple_wait(process_t pid, exit_code_t *exit_code);
|
||||||
|
|
||||||
|
|
|
@ -9,19 +9,20 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include "log.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 fd[2];
|
||||||
int ret = 0;
|
|
||||||
|
|
||||||
if (pipe(fd) == -1) {
|
if (pipe(fd) == -1) {
|
||||||
perror("pipe");
|
perror("pipe");
|
||||||
return -1;
|
return PROCESS_ERROR_GENERIC;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum process_result ret = PROCESS_SUCCESS;
|
||||||
|
|
||||||
*pid = fork();
|
*pid = fork();
|
||||||
if (*pid == -1) {
|
if (*pid == -1) {
|
||||||
perror("fork");
|
perror("fork");
|
||||||
ret = -1;
|
ret = PROCESS_ERROR_GENERIC;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,9 +31,9 @@ int cmd_execute(const char *path, const char *const argv[], pid_t *pid) {
|
||||||
close(fd[1]);
|
close(fd[1]);
|
||||||
fd[1] = -1;
|
fd[1] = -1;
|
||||||
// wait for EOF or receive errno from child
|
// 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");
|
perror("read");
|
||||||
ret = -1;
|
ret = PROCESS_ERROR_GENERIC;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
} else if (*pid == 0) {
|
} else if (*pid == 0) {
|
||||||
|
@ -40,12 +41,18 @@ int cmd_execute(const char *path, const char *const argv[], pid_t *pid) {
|
||||||
close(fd[0]);
|
close(fd[0]);
|
||||||
if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) == 0) {
|
if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) == 0) {
|
||||||
execvp(path, (char *const *)argv);
|
execvp(path, (char *const *)argv);
|
||||||
|
if (errno == ENOENT) {
|
||||||
|
ret = PROCESS_ERROR_MISSING_BINARY;
|
||||||
|
} else {
|
||||||
|
ret = PROCESS_ERROR_GENERIC;
|
||||||
|
}
|
||||||
|
perror("exec");
|
||||||
} else {
|
} else {
|
||||||
perror("fcntl");
|
perror("fcntl");
|
||||||
|
ret = PROCESS_ERROR_GENERIC;
|
||||||
}
|
}
|
||||||
// send errno to the parent
|
// send ret to the parent
|
||||||
ret = errno;
|
if (write(fd[1], &ret, sizeof(ret)) == -1) {
|
||||||
if (write(fd[1], &ret, sizeof(int)) == -1) {
|
|
||||||
perror("write");
|
perror("write");
|
||||||
}
|
}
|
||||||
// close write side before exiting
|
// close write side before exiting
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "str_util.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;
|
STARTUPINFO si;
|
||||||
PROCESS_INFORMATION pi;
|
PROCESS_INFORMATION pi;
|
||||||
memset(&si, 0, sizeof(si));
|
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)) {
|
if (ret >= sizeof(cmd)) {
|
||||||
LOGE("Command too long (%" PRIsizet " chars)", sizeof(cmd) - 1);
|
LOGE("Command too long (%" PRIsizet " chars)", sizeof(cmd) - 1);
|
||||||
*handle = NULL;
|
*handle = NULL;
|
||||||
return -1;
|
return PROCESS_ERROR_GENERIC;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WINDOWS_NOCONSOLE
|
#ifdef WINDOWS_NOCONSOLE
|
||||||
|
@ -29,11 +29,11 @@ int cmd_execute(const char *path, const char *const argv[], HANDLE *handle) {
|
||||||
#endif
|
#endif
|
||||||
if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi)) {
|
if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi)) {
|
||||||
*handle = NULL;
|
*handle = NULL;
|
||||||
return -1;
|
return PROCESS_ERROR_GENERIC;
|
||||||
}
|
}
|
||||||
|
|
||||||
*handle = pi.hProcess;
|
*handle = pi.hProcess;
|
||||||
return 0;
|
return PROCESS_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
SDL_bool cmd_terminate(HANDLE handle) {
|
SDL_bool cmd_terminate(HANDLE handle) {
|
||||||
|
|
Loading…
Reference in a new issue