forked from Yasushi/putty
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathuxsocks.c
178 lines (147 loc) · 4.02 KB
/
uxsocks.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
* Main program for Unix psocks.
*/
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include "putty.h"
#include "ssh.h"
#include "psocks.h"
const bool buildinfo_gtk_relevant = false;
typedef struct PsocksDataSinkPopen {
stdio_sink sink[2];
PsocksDataSink pds;
} PsocksDataSinkPopen;
static void popen_free(PsocksDataSink *pds)
{
PsocksDataSinkPopen *pdsp = container_of(pds, PsocksDataSinkPopen, pds);
for (size_t i = 0; i < 2; i++)
pclose(pdsp->sink[i].fp);
sfree(pdsp);
}
static PsocksDataSink *open_pipes(
const char *cmd, const char *const *direction_args,
const char *index_arg, char **err)
{
FILE *fp[2];
char *errmsg = NULL;
for (size_t i = 0; i < 2; i++) {
/* No escaping needed: the provided command is already
* shell-quoted, and our extra arguments are simple */
char *command = dupprintf("%s %s %s", cmd,
direction_args[i], index_arg);
fp[i] = popen(command, "w");
sfree(command);
if (!fp[i]) {
if (!errmsg)
errmsg = dupprintf("%s", strerror(errno));
}
}
if (errmsg) {
for (size_t i = 0; i < 2; i++)
if (fp[i])
pclose(fp[i]);
*err = errmsg;
return NULL;
}
PsocksDataSinkPopen *pdsp = snew(PsocksDataSinkPopen);
for (size_t i = 0; i < 2; i++) {
setvbuf(fp[i], NULL, _IONBF, 0);
stdio_sink_init(&pdsp->sink[i], fp[i]);
pdsp->pds.s[i] = BinarySink_UPCAST(&pdsp->sink[i]);
}
pdsp->pds.free = popen_free;
return &pdsp->pds;
}
static int signalpipe[2] = { -1, -1 };
static void sigchld(int signum)
{
if (write(signalpipe[1], "x", 1) <= 0)
/* not much we can do about it */;
}
static pid_t subcommand_pid = -1;
static bool still_running = true;
static void start_subcommand(strbuf *args)
{
pid_t pid;
/*
* Set up the pipe we'll use to tell us about SIGCHLD.
*/
if (pipe(signalpipe) < 0) {
perror("pipe");
exit(1);
}
putty_signal(SIGCHLD, sigchld);
/*
* Make an array of argument pointers that execvp will like.
*/
size_t nargs = 0;
for (size_t i = 0; i < args->len; i++)
if (args->s[i] == '\0')
nargs++;
char **exec_args = snewn(nargs + 1, char *);
char *p = args->s;
for (size_t a = 0; a < nargs; a++) {
exec_args[a] = p;
size_t len = strlen(p);
assert(len < args->len - (p - args->s));
p += 1 + len;
}
exec_args[nargs] = NULL;
pid = fork();
if (pid < 0) {
perror("fork");
exit(1);
} else if (pid == 0) {
execvp(exec_args[0], exec_args);
perror("exec");
_exit(127);
} else {
subcommand_pid = pid;
sfree(exec_args);
}
}
static const PsocksPlatform platform = {
open_pipes,
start_subcommand,
};
static bool psocks_pw_setup(void *ctx, pollwrapper *pw)
{
if (signalpipe[0] >= 0)
pollwrap_add_fd_rwx(pw, signalpipe[0], SELECT_R);
return true;
}
static void psocks_pw_check(void *ctx, pollwrapper *pw)
{
if (signalpipe[0] >= 0 &&
pollwrap_check_fd_rwx(pw, signalpipe[0], SELECT_R)) {
while (true) {
int status;
pid_t pid = waitpid(-1, &status, WNOHANG);
if (pid <= 0)
break;
if (pid == subcommand_pid)
still_running = false;
}
}
}
static bool psocks_continue(void *ctx, bool found_any_fd,
bool ran_any_callback)
{
return still_running;
}
typedef bool (*cliloop_continue_t)(void *ctx, bool found_any_fd,
bool ran_any_callback);
int main(int argc, char **argv)
{
psocks_state *ps = psocks_new(&platform);
psocks_cmdline(ps, argc, argv);
sk_init();
uxsel_init();
psocks_start(ps);
cli_main_loop(psocks_pw_setup, psocks_pw_check, psocks_continue, NULL);
}