Skip to content

Commit b5cba56

Browse files
committed
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 <[email protected]>
1 parent 3b5e542 commit b5cba56

File tree

4 files changed

+68
-13
lines changed

4 files changed

+68
-13
lines changed

app/src/command.c

+26-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "command.h"
22

3+
#include <errno.h>
34
#include <stdio.h>
45
#include <stdlib.h>
56
#include <string.h>
@@ -18,9 +19,27 @@ static inline const char *get_adb_command() {
1819
return adb_command;
1920
}
2021

22+
static void show_err_msg(int err) {
23+
#ifdef __WINDOWS__
24+
LOGE("Failed to execute adb.\n");
25+
#else
26+
switch (err) {
27+
case 0:
28+
break;
29+
case ENOENT:
30+
LOGE("Missing adb. You need adb, accessible from your PATH to execute scrcpy.\n");
31+
break;
32+
default:
33+
LOGE("Failed to execute adb, errno: [%d].\n", err);
34+
break;
35+
}
36+
#endif
37+
}
38+
2139
process_t adb_execute(const char *serial, const char *const adb_cmd[], int len) {
2240
const char *cmd[len + 4];
23-
int i;
41+
int i, r;
42+
process_t process;
2443
cmd[0] = get_adb_command();
2544
if (serial) {
2645
cmd[1] = "-s";
@@ -32,7 +51,12 @@ process_t adb_execute(const char *serial, const char *const adb_cmd[], int len)
3251

3352
memcpy(&cmd[i], adb_cmd, len * sizeof(const char *));
3453
cmd[len + i] = NULL;
35-
return cmd_execute(cmd[0], cmd);
54+
r = cmd_execute(cmd[0], cmd, &process);
55+
if (r != 0) {
56+
show_err_msg(r);
57+
return -1;
58+
}
59+
return process;
3660
}
3761

3862
process_t adb_forward(const char *serial, uint16_t local_port, const char *device_socket_name) {

app/src/command.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#endif
3333
# define NO_EXIT_CODE -1
3434

35-
process_t cmd_execute(const char *path, const char *const argv[]);
35+
int cmd_execute(const char *path, const char *const argv[], process_t *process);
3636
SDL_bool cmd_terminate(process_t pid);
3737
SDL_bool cmd_simple_wait(process_t pid, exit_code_t *exit_code);
3838

app/src/sys/unix/command.c

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,52 @@
1+
#define _GNU_SOURCE
12
#include "command.h"
23

4+
#include <errno.h>
5+
#include <fcntl.h>
36
#include <signal.h>
47
#include <stdlib.h>
58
#include <sys/types.h>
69
#include <sys/wait.h>
710
#include <unistd.h>
811
#include "log.h"
912

10-
pid_t cmd_execute(const char *path, const char *const argv[]) {
11-
pid_t pid = fork();
12-
if (pid == -1) {
13+
int cmd_execute(const char *path, const char *const argv[], pid_t *pid) {
14+
int fd[2];
15+
int buf = 0;
16+
17+
if (0 > pipe2(fd, O_CLOEXEC)) {
18+
perror("pipe2");
19+
return -1;
20+
}
21+
22+
*pid = fork();
23+
if (*pid == -1) {
1324
perror("fork");
1425
return -1;
1526
}
16-
if (pid == 0) {
27+
28+
if (*pid > 0) {
29+
/* parent close write side. */
30+
close(fd[1]);
31+
/* and blocking read until child exec or write some fail
32+
* reason in fd. */
33+
if (0 > read(fd[0], &buf, sizeof(int))) {
34+
perror("read");
35+
return -1;
36+
}
37+
} else if (*pid == 0) {
38+
/* child close read side. */
39+
close(fd[0]);
1740
execvp(path, (char *const *)argv);
18-
perror("exec");
41+
/* if child execvp failed, before exit, we should write
42+
* reason into the pipe. */
43+
buf = errno;
44+
if (0 > write(fd[1], &buf, sizeof(int))) {
45+
perror("write");
46+
}
1947
_exit(1);
2048
}
21-
return pid;
49+
return buf;
2250
}
2351

2452
SDL_bool cmd_terminate(pid_t pid) {

app/src/sys/win/command.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "log.h"
55
#include "str_util.h"
66

7-
HANDLE cmd_execute(const char *path, const char *const argv[]) {
7+
int cmd_execute(const char *path, const char *const argv[], HANDLE *handle) {
88
STARTUPINFO si;
99
PROCESS_INFORMATION pi;
1010
memset(&si, 0, sizeof(si));
@@ -18,7 +18,8 @@ HANDLE cmd_execute(const char *path, const char *const argv[]) {
1818
size_t ret = xstrjoin(cmd, argv, ' ', sizeof(cmd));
1919
if (ret >= sizeof(cmd)) {
2020
LOGE("Command too long (%" PRIsizet " chars)", sizeof(cmd) - 1);
21-
return NULL;
21+
*handle = NULL;
22+
return -1;
2223
}
2324

2425
#ifdef WINDOWS_NOCONSOLE
@@ -27,10 +28,12 @@ HANDLE cmd_execute(const char *path, const char *const argv[]) {
2728
int flags = 0;
2829
#endif
2930
if (!CreateProcess(NULL, cmd, NULL, NULL, FALSE, flags, NULL, NULL, &si, &pi)) {
30-
return NULL;
31+
*handle = NULL;
32+
return -1;
3133
}
3234

33-
return pi.hProcess;
35+
*handle = pi.hProcess;
36+
return 0;
3437
}
3538

3639
SDL_bool cmd_terminate(HANDLE handle) {

0 commit comments

Comments
 (0)