-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathcmd.c
123 lines (104 loc) · 3.66 KB
/
cmd.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* cmd.c
Copyright (C) 2024 5ec1cff
This file is part of termux-tools.
termux-tools is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
termux-tools is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with termux-tools. If not, see
<https://www.gnu.org/licenses/>. */
#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/wait.h>
#ifndef TEMP_FAILURE_RETRY
#define TEMP_FAILURE_RETRY(expression) \
(__extension__({ \
long int __result; \
do \
__result = (long int)(expression); \
while (__result == -1L && errno == EINTR); \
__result; \
}))
#endif
void pump(int in_fd, int out_fd) {
char buf[4096];
ssize_t sz, t;
for (;;) {
sz = TEMP_FAILURE_RETRY(read(in_fd, buf, sizeof(buf)));
if (sz <= 0) return;
while (sz) {
t = TEMP_FAILURE_RETRY(write(out_fd, buf, sz));
if (t <= 0) return;
sz -= t;
}
}
}
int p_std_in[2], p_std_out[2], p_std_err[2];
void *pump_stdin(void *ignore) {
pump(STDIN_FILENO, p_std_in[1]);
close(p_std_in[1]);
return NULL;
}
void *pump_stdout(void *ignore) {
pump(p_std_out[0], STDOUT_FILENO);
close(p_std_out[0]);
return NULL;
}
void *pump_stderr(void *ignore) {
pump(p_std_err[0], STDERR_FILENO);
close(p_std_err[0]);
return NULL;
}
void replace_fd(int fd, int target_fd) {
if (dup2(fd, target_fd) == -1) err(EXIT_FAILURE, "dup");
close(fd);
if (fcntl(target_fd, F_SETFD, fcntl(target_fd, F_GETFD) & ~FD_CLOEXEC) == -1)
err(EXIT_FAILURE, "replace_fd");
}
int main(int argc, char **argv) {
if (pipe(p_std_in) == -1) err(EXIT_FAILURE, "pipe");
if (pipe(p_std_out) == -1) err(EXIT_FAILURE, "pipe");
if (pipe(p_std_err) == -1) err(EXIT_FAILURE, "pipe");
pid_t pid = fork();
if (pid < 0) {
err(EXIT_FAILURE, "fork");
} else if (pid > 0) {
close(p_std_in[0]);
close(p_std_out[1]);
close(p_std_err[1]);
signal(SIGPIPE, SIG_IGN);
pthread_t t_stdin;
pthread_create(&t_stdin, NULL, pump_stdin, NULL);
pthread_detach(t_stdin);
pthread_t t_stdout;
pthread_create(&t_stdout, NULL, pump_stdout, NULL);
pthread_t t_stderr;
pthread_create(&t_stderr, NULL, pump_stderr, NULL);
int status;
if (TEMP_FAILURE_RETRY(waitpid(pid, &status, 0)) < 0) err(EXIT_FAILURE, "wait");
pthread_join(t_stdout, NULL);
pthread_join(t_stderr, NULL);
if (WIFEXITED(status))
exit(WEXITSTATUS(status));
else
exit(EXIT_FAILURE);
} else {
replace_fd(p_std_in[0], STDIN_FILENO);
replace_fd(p_std_out[1], STDOUT_FILENO);
replace_fd(p_std_err[1], STDERR_FILENO);
execv("/system/bin/cmd", argv);
err(EXIT_FAILURE, "exec");
}
}