Notify adb missing

There are many user who encounters missing adb.
To stop things happens again, we check it and show
sexy response to user.

Signed-off-by: yuchenlin <npes87184@gmail.com>
This commit is contained in:
yuchenlin 2018-09-01 09:18:06 +08:00 committed by Romain Vimont
parent 3b5e54278e
commit 6d2d803003
4 changed files with 88 additions and 14 deletions

View file

@ -1,5 +1,8 @@
#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>
@ -18,9 +21,30 @@ static inline const char *get_adb_command() {
return adb_command; return adb_command;
} }
static void show_err_msg(int err) {
#ifdef __WINDOWS__
(void) err; // unused
LOGE("Failed to execute adb");
#else
switch (err) {
case -1:
LOGE("Failed to execute adb");
break;
case ENOENT:
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));
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) {
const char *cmd[len + 4]; const char *cmd[len + 4];
int i; int i;
process_t process;
cmd[0] = get_adb_command(); cmd[0] = get_adb_command();
if (serial) { if (serial) {
cmd[1] = "-s"; cmd[1] = "-s";
@ -32,7 +56,12 @@ 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;
return cmd_execute(cmd[0], cmd); int r = cmd_execute(cmd[0], cmd, &process);
if (r != 0) {
show_err_msg(r);
return PROCESS_NONE;
}
return process;
} }
process_t adb_forward(const char *serial, uint16_t local_port, const char *device_socket_name) { process_t adb_forward(const char *serial, uint16_t local_port, const char *device_socket_name) {

View file

@ -32,7 +32,7 @@
#endif #endif
# define NO_EXIT_CODE -1 # define NO_EXIT_CODE -1
process_t cmd_execute(const char *path, const char *const argv[]); int 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);

View file

@ -1,5 +1,7 @@
#include "command.h" #include "command.h"
#include <errno.h>
#include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h> #include <sys/types.h>
@ -7,18 +9,58 @@
#include <unistd.h> #include <unistd.h>
#include "log.h" #include "log.h"
pid_t cmd_execute(const char *path, const char *const argv[]) { int cmd_execute(const char *path, const char *const argv[], pid_t *pid) {
pid_t pid = fork(); int fd[2];
if (pid == -1) { int ret = 0;
perror("fork");
if (pipe(fd) == -1) {
perror("pipe");
return -1; return -1;
} }
if (pid == 0) {
execvp(path, (char *const *)argv); *pid = fork();
perror("exec"); if (*pid == -1) {
perror("fork");
ret = -1;
goto end;
}
if (*pid > 0) {
// parent close write side
close(fd[1]);
fd[1] = -1;
// wait for EOF or receive errno from child
if (read(fd[0], &ret, sizeof(int)) == -1) {
perror("read");
ret = -1;
goto end;
}
} else if (*pid == 0) {
// child close read side
close(fd[0]);
if (fcntl(fd[1], F_SETFD, FD_CLOEXEC) == 0) {
execvp(path, (char *const *)argv);
} else {
perror("fcntl");
}
// send errno to the parent
ret = errno;
if (write(fd[1], &ret, sizeof(int)) == -1) {
perror("write");
}
// close write side before exiting
close(fd[1]);
_exit(1); _exit(1);
} }
return pid;
end:
if (fd[0] != -1) {
close(fd[0]);
}
if (fd[1] != -1) {
close(fd[1]);
}
return ret;
} }
SDL_bool cmd_terminate(pid_t pid) { SDL_bool cmd_terminate(pid_t pid) {

View file

@ -4,7 +4,7 @@
#include "log.h" #include "log.h"
#include "str_util.h" #include "str_util.h"
HANDLE cmd_execute(const char *path, const char *const argv[]) { int 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));
@ -18,7 +18,8 @@ HANDLE cmd_execute(const char *path, const char *const argv[]) {
size_t ret = xstrjoin(cmd, argv, ' ', sizeof(cmd)); size_t ret = xstrjoin(cmd, argv, ' ', sizeof(cmd));
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);
return NULL; *handle = NULL;
return -1;
} }
#ifdef WINDOWS_NOCONSOLE #ifdef WINDOWS_NOCONSOLE
@ -27,10 +28,12 @@ HANDLE cmd_execute(const char *path, const char *const argv[]) {
int flags = 0; int flags = 0;
#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)) {
return NULL; *handle = NULL;
return -1;
} }
return pi.hProcess; *handle = pi.hProcess;
return 0;
} }
SDL_bool cmd_terminate(HANDLE handle) { SDL_bool cmd_terminate(HANDLE handle) {