-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.c
305 lines (279 loc) · 7.6 KB
/
run.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#define _XOPEN_SOURCE 500
#define __EXTENSIONS__
#include "tintin.h"
#include <fcntl.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#ifdef HAVE_TERMIOS_H
# include <termios.h>
#endif
#include "protos/print.h"
#include "protos/pty.h"
#include "protos/utils.h"
extern char **environ;
static void print_stty(struct session *ses)
{
int fd = ses->socket;
struct termios ta;
struct winsize ws;
char buf[BUFFER_SIZE], *bptr;
#define battr(c, a, b) bptr+=sprintf(bptr, " %s%s~7~", (ta.c_##c&a)?"~9~":"~2~-~4~", b);
ZERO(ta);
ZERO(ws);
tintin_printf(ses, "~7~pty attributes (fd=%d):", fd);
if (tcgetattr(fd, &ta))
tintin_printf(ses, " attrs: unknown");
else
{
tintin_printf(ses, " attrs: cflag=~3~%x~7~, iflag=~3~%x~7~, oflag=~3~%x~7~, lflag=~3~%x~7~",
ta.c_cflag, ta.c_iflag, ta.c_oflag, ta.c_lflag);
bptr=buf+sprintf(buf, " ~3~[%x]~7~:", ta.c_cflag);
battr(cflag, PARENB, "parenb");
battr(cflag, PARODD, "parodd");
battr(cflag, CS8, "cs8");
battr(cflag, HUPCL, "hupcl");
battr(cflag, CSTOPB, "cstopb");
battr(cflag, CREAD, "cread");
battr(cflag, CLOCAL, "clocal");
#ifdef CRTSCTS
battr(cflag, CRTSCTS, "crtscts");
#endif
tintin_printf(ses, "%s", buf);
bptr=buf+sprintf(buf, " ~3~[%x]~7~:", ta.c_iflag);
battr(iflag, IGNBRK, "ignbrk");
battr(iflag, BRKINT, "brkint");
battr(iflag, IGNPAR, "ignpar");
battr(iflag, PARMRK, "parmrk");
battr(iflag, INPCK, "inpck");
battr(iflag, ISTRIP, "istrip");
battr(iflag, INLCR, "inlcr");
battr(iflag, IGNCR, "igncr");
battr(iflag, ICRNL, "icrnl");
battr(iflag, IXON, "ixon");
battr(iflag, IXOFF, "ixoff");
#ifdef IUCLC
battr(iflag, IUCLC, "iuclc");
#endif
#ifdef IXANY
battr(iflag, IXANY, "ixany");
#endif
#ifdef IMAXBEL
battr(iflag, IMAXBEL, "imaxbel");
#endif
tintin_printf(ses, "%s", buf);
bptr=buf+sprintf(buf, " ~3~[%x]~7~:", ta.c_oflag);
battr(oflag, OPOST, "opost");
#ifdef OLCUC
battr(oflag, OLCUC, "olcuc");
#endif
battr(oflag, OCRNL, "ocrnl");
battr(oflag, ONLCR, "onlcr");
battr(oflag, ONOCR, "onocr");
battr(oflag, ONLRET, "onlret");
#ifdef OFILL
battr(oflag, OFILL, "ofill");
#endif
#ifdef OFDEL
battr(oflag, OFDEL, "ofdel");
#endif
/*
battr(oflag, NL0, "nl0");
battr(oflag, CR0, "cr0");
battr(oflag, TAB0, "tab0");
battr(oflag, BS0, "bs0");
battr(oflag, VT0, "vt0");
battr(oflag, FF0, "ff0");
*/
tintin_printf(ses, "%s", buf);
bptr=buf+sprintf(buf, " ~3~[%x]~7~:", ta.c_lflag);
battr(lflag, ISIG, "isig");
battr(lflag, ICANON, "icanon");
battr(lflag, IEXTEN, "iexten");
battr(lflag, ECHO, "echo");
battr(lflag, ECHOE, "echoe");
battr(lflag, ECHOK, "echok");
battr(lflag, ECHONL, "echonl");
battr(lflag, NOFLSH, "noflsh");
#ifdef XCASE
battr(lflag, XCASE, "xcase");
#endif
battr(lflag, TOSTOP, "tostop");
#ifdef ECHOPRT
battr(lflag, ECHOPRT, "echoprt");
#endif
#ifdef ECHOCTL
battr(lflag, ECHOCTL, "echoctl");
#endif
#ifdef ECHOKE
battr(lflag, ECHOKE, "echoke");
#endif
tintin_printf(ses, "%s", buf);
}
if (ioctl(fd, TIOCGWINSZ, &ws))
tintin_printf(ses, " window size: unknown");
else
tintin_printf(ses, " window size: %dx%d",
ws.ws_col, ws.ws_row);
}
void stty_command(const char *arg, struct session *ses)
{
print_stty(ses);
}
void pty_resize(int fd, int sx, int sy)
{
struct winsize ws;
if (sx>0 && sy>0)
{
ws.ws_row=sy;
ws.ws_col=sx;
ws.ws_xpixel=0;
ws.ws_ypixel=0;
ioctl(fd, TIOCSWINSZ, &ws);
}
}
int run(const char *command, int sx, int sy, const char *term)
{
int fd, res;
#if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
/* Work around a kfreebsd 9.x bug. If the handler for SIGCHLD is anything
but SIG_DFL, grantpt() and thus forkpty() doesn't work. */
struct sigaction act, oldact;
sigemptyset(&act.sa_mask);
act.sa_flags=SA_RESTART;
act.sa_handler=SIG_DFL;
sigaction(SIGCHLD, &act, &oldact);
#endif
#ifndef PTY_ECHO_HACK
struct winsize ws;
ws.ws_row=sy;
ws.ws_col=sx;
ws.ws_xpixel=0;
ws.ws_ypixel=0;
res = forkpty(&fd, 0, 0, (sy>0 && sx>0)?&ws:0);
#else
res = forkpty(&fd, 0, 0, 0);
#endif
#if defined(__FreeBSD_kernel__) && defined(__GLIBC__)
int err = errno;
sigaction(SIGCHLD, &oldact, 0);
while (waitpid(-1, 0, WNOHANG)>0);
errno = err;
#endif
switch (res)
{
case -1:
return -1;
case 0:
{
struct termios ta;
const char *argv[4];
char cmd[BUFFER_SIZE+5];
tcgetattr(1, &ta);
cfmakeraw(&ta);
tcsetattr(1, TCSANOW, &ta);
sprintf(cmd, "exec %s", command);
argv[0]="sh";
argv[1]="-c";
argv[2]=cmd;
argv[3]=0;
if (term)
setenv("TERM", term, 1); /* TERM=KBtin. Or should we lie? */
execve("/bin/sh", (char*const*)argv, environ);
fprintf(stderr, "#ERROR: Couldn't exec `%s'\n", command);
exit(127);
}
default:
return fd;
}
}
FILE* mypopen(const char *command, bool wr, int ofd)
{
int p[2];
if (pipe(p))
{
close(ofd);
return 0;
}
switch (fork())
{
case -1:
close(p[0]);
close(p[1]);
close(ofd);
return 0;
case 0:
{
const char *argv[4];
char cmd[BUFFER_SIZE+5];
if (!wr)
{
close(p[0]);
if (ofd==-1)
ofd=open("/dev/null", O_RDONLY);
if (ofd!=-1)
dup2(ofd, 0);
dup2(p[1], 1);
dup2(p[1], 2);
close(p[1]);
}
else
{
close(p[1]);
if (ofd==-1)
ofd=open("/dev/null", O_WRONLY);
if (ofd!=-1)
dup2(ofd, 1), dup2(ofd, 2);
dup2(p[0], 0);
close(p[0]);
signal(SIGINT, SIG_IGN);
signal(SIGHUP, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
}
if (ofd>2)
close(ofd);
sprintf(cmd, "exec %s", command);
argv[0]="sh";
argv[1]="-c";
argv[2]=cmd;
argv[3]=0;
execve("/bin/sh", (char*const*)argv, environ);
fprintf(stderr, "#ERROR: Couldn't exec `%s'\n", command);
exit(127);
}
default:
close(ofd);
close(p[!wr]);
return fdopen(p[wr], wr?"w":"r");
}
}
void pty_write_line(const char *line, int pty)
{
char out[4*BUFFER_SIZE+1];
int len;
#ifdef PTY_ECHO_HACK
struct termios ta, oldta;
tcgetattr(pty, &oldta);
memcpy(&ta, &oldta, sizeof(ta));
cfmakeraw(&ta);
ta.c_cc[VMIN]=MAX_INPUT;
tcsetattr(pty, TCSANOW, &ta);
#else
# ifdef RESET_RAW
struct termios ta;
tcgetattr(pty, &ta);
cfmakeraw(&ta);
tcsetattr(pty, TCSANOW, &ta);
# endif
#endif
len=sprintf(out, "%s\n", line);
if (write(pty, out, len) == -1)
syserr("write in pty_write_line()");
#ifdef PTY_ECHO_HACK
/* FIXME: if write() blocks, they'll act in raw mode */
tcsetattr(pty, TCSANOW, &oldta);
sleep(0); /* let'em act */
#endif
}