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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
// Some of the code came from pangoterm and libuv
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <uv.h>
// forkpty is not in POSIX, so headers are platform-specific
#if defined(__FreeBSD__) || defined(__DragonFly__)
# include <libutil.h>
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__)
# include <util.h>
#elif defined(__sun)
# include <fcntl.h>
# include <signal.h>
# include <sys/stream.h>
# include <sys/syscall.h>
# include <unistd.h>
#else
# include <pty.h>
#endif
#ifdef __APPLE__
# include <crt_externs.h>
#endif
#include "auto/config.h"
#include "klib/klist.h"
#include "nvim/eval/typval.h"
#include "nvim/event/loop.h"
#include "nvim/event/process.h"
#include "nvim/event/stream.h"
#include "nvim/log.h"
#include "nvim/os/fs.h"
#include "nvim/os/os_defs.h"
#include "nvim/os/pty_process.h"
#include "nvim/os/pty_process_unix.h"
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "os/pty_process_unix.c.generated.h"
#endif
#if defined(__sun) && !defined(HAVE_FORKPTY)
// this header defines STR, just as nvim.h, but it is defined as ('S'<<8),
// to avoid #undef STR, #undef STR, #define STR ('S'<<8) just delay the
// inclusion of the header even though it gets include out of order.
# include <sys/stropts.h>
static int openpty(int *amaster, int *aslave, char *name, struct termios *termp,
struct winsize *winp)
{
int slave = -1;
int master = open("/dev/ptmx", O_RDWR);
if (master == -1) {
goto error;
}
// grantpt will invoke a setuid program to change permissions
// and might fail if SIGCHLD handler is set, temporarily reset
// while running
void (*sig_saved)(int) = signal(SIGCHLD, SIG_DFL);
int res = grantpt(master);
signal(SIGCHLD, sig_saved);
if (res == -1 || unlockpt(master) == -1) {
goto error;
}
char *slave_name = ptsname(master);
if (slave_name == NULL) {
goto error;
}
slave = open(slave_name, O_RDWR|O_NOCTTY);
if (slave == -1) {
goto error;
}
// ptem emulates a terminal when used on a pseudo terminal driver,
// must be pushed before ldterm
ioctl(slave, I_PUSH, "ptem");
// ldterm provides most of the termio terminal interface
ioctl(slave, I_PUSH, "ldterm");
// ttcompat compatibility with older terminal ioctls
ioctl(slave, I_PUSH, "ttcompat");
if (termp) {
tcsetattr(slave, TCSAFLUSH, termp);
}
if (winp) {
ioctl(slave, TIOCSWINSZ, winp);
}
*amaster = master;
*aslave = slave;
// ignoring name, not passed and size is unknown in the API
return 0;
error:
if (slave != -1) {
close(slave);
}
if (master != -1) {
close(master);
}
return -1;
}
static int login_tty(int fd)
{
setsid();
if (ioctl(fd, TIOCSCTTY, NULL) == -1) {
return -1;
}
dup2(fd, STDIN_FILENO);
dup2(fd, STDOUT_FILENO);
dup2(fd, STDERR_FILENO);
if (fd > STDERR_FILENO) {
close(fd);
}
return 0;
}
static pid_t forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp)
{
int master, slave;
if (openpty(&master, &slave, name, termp, winp) == -1) {
return -1;
}
pid_t pid = fork();
switch (pid) {
case -1:
close(master);
close(slave);
return -1;
case 0:
close(master);
login_tty(slave);
return 0;
default:
close(slave);
*amaster = master;
return pid;
}
}
#endif
/// @returns zero on success, or negative error code
int pty_process_spawn(PtyProcess *ptyproc)
FUNC_ATTR_NONNULL_ALL
{
// termios initialized at first use
static struct termios termios_default;
if (!termios_default.c_cflag) {
init_termios(&termios_default);
}
int status = 0; // zero or negative error code (libuv convention)
Process *proc = (Process *)ptyproc;
assert(proc->err.closed);
uv_signal_start(&proc->loop->children_watcher, chld_handler, SIGCHLD);
ptyproc->winsize = (struct winsize){ ptyproc->height, ptyproc->width, 0, 0 };
uv_disable_stdio_inheritance();
int master;
int pid = forkpty(&master, NULL, &termios_default, &ptyproc->winsize);
if (pid < 0) {
status = -errno;
ELOG("forkpty failed: %s", strerror(errno));
return status;
} else if (pid == 0) {
init_child(ptyproc); // never returns
}
// make sure the master file descriptor is non blocking
int master_status_flags = fcntl(master, F_GETFL);
if (master_status_flags == -1) {
status = -errno;
ELOG("Failed to get master descriptor status flags: %s", strerror(errno));
goto error;
}
if (fcntl(master, F_SETFL, master_status_flags | O_NONBLOCK) == -1) {
status = -errno;
ELOG("Failed to make master descriptor non-blocking: %s", strerror(errno));
goto error;
}
// Other jobs and providers should not get a copy of this file descriptor.
if (os_set_cloexec(master) == -1) {
status = -errno;
ELOG("Failed to set CLOEXEC on ptmx file descriptor");
goto error;
}
if (!proc->in.closed
&& (status = set_duplicating_descriptor(master, &proc->in.uv.pipe))) {
goto error;
}
if (!proc->out.closed
&& (status = set_duplicating_descriptor(master, &proc->out.uv.pipe))) {
goto error;
}
ptyproc->tty_fd = master;
proc->pid = pid;
return 0;
error:
close(master);
kill(pid, SIGKILL);
waitpid(pid, NULL, 0);
return status;
}
const char *pty_process_tty_name(PtyProcess *ptyproc)
{
return ptsname(ptyproc->tty_fd);
}
void pty_process_resize(PtyProcess *ptyproc, uint16_t width, uint16_t height)
FUNC_ATTR_NONNULL_ALL
{
ptyproc->winsize = (struct winsize){ height, width, 0, 0 };
ioctl(ptyproc->tty_fd, TIOCSWINSZ, &ptyproc->winsize);
}
void pty_process_close(PtyProcess *ptyproc)
FUNC_ATTR_NONNULL_ALL
{
pty_process_close_master(ptyproc);
Process *proc = (Process *)ptyproc;
if (proc->internal_close_cb) {
proc->internal_close_cb(proc);
}
}
void pty_process_close_master(PtyProcess *ptyproc) FUNC_ATTR_NONNULL_ALL
{
if (ptyproc->tty_fd >= 0) {
close(ptyproc->tty_fd);
ptyproc->tty_fd = -1;
}
}
void pty_process_teardown(Loop *loop)
{
uv_signal_stop(&loop->children_watcher);
}
static void init_child(PtyProcess *ptyproc)
FUNC_ATTR_NONNULL_ALL
{
#if defined(HAVE__NSGETENVIRON)
# define environ (*_NSGetEnviron())
#else
extern char **environ;
#endif
// New session/process-group. #6530
setsid();
signal(SIGCHLD, SIG_DFL);
signal(SIGHUP, SIG_DFL);
signal(SIGINT, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGALRM, SIG_DFL);
Process *proc = (Process *)ptyproc;
if (proc->cwd && os_chdir(proc->cwd) != 0) {
ELOG("chdir(%s) failed: %s", proc->cwd, strerror(errno));
return;
}
const char *prog = process_get_exepath(proc);
assert(proc->env);
environ = tv_dict_to_env(proc->env);
execvp(prog, proc->argv);
ELOG("execvp(%s) failed: %s", prog, strerror(errno));
_exit(122); // 122 is EXEC_FAILED in the Vim source.
}
static void init_termios(struct termios *termios) FUNC_ATTR_NONNULL_ALL
{
// Taken from pangoterm
termios->c_iflag = ICRNL|IXON;
termios->c_oflag = OPOST|ONLCR;
#ifdef TAB0
termios->c_oflag |= TAB0;
#endif
termios->c_cflag = CS8|CREAD;
termios->c_lflag = ISIG|ICANON|IEXTEN|ECHO|ECHOE|ECHOK;
// not using cfsetspeed, not available on all platforms
cfsetispeed(termios, 38400);
cfsetospeed(termios, 38400);
#ifdef IUTF8
termios->c_iflag |= IUTF8;
#endif
#ifdef NL0
termios->c_oflag |= NL0;
#endif
#ifdef CR0
termios->c_oflag |= CR0;
#endif
#ifdef BS0
termios->c_oflag |= BS0;
#endif
#ifdef VT0
termios->c_oflag |= VT0;
#endif
#ifdef FF0
termios->c_oflag |= FF0;
#endif
#ifdef ECHOCTL
termios->c_lflag |= ECHOCTL;
#endif
#ifdef ECHOKE
termios->c_lflag |= ECHOKE;
#endif
termios->c_cc[VINTR] = 0x1f & 'C';
termios->c_cc[VQUIT] = 0x1f & '\\';
termios->c_cc[VERASE] = 0x7f;
termios->c_cc[VKILL] = 0x1f & 'U';
termios->c_cc[VEOF] = 0x1f & 'D';
termios->c_cc[VEOL] = _POSIX_VDISABLE;
termios->c_cc[VEOL2] = _POSIX_VDISABLE;
termios->c_cc[VSTART] = 0x1f & 'Q';
termios->c_cc[VSTOP] = 0x1f & 'S';
termios->c_cc[VSUSP] = 0x1f & 'Z';
termios->c_cc[VREPRINT] = 0x1f & 'R';
termios->c_cc[VWERASE] = 0x1f & 'W';
termios->c_cc[VLNEXT] = 0x1f & 'V';
termios->c_cc[VMIN] = 1;
termios->c_cc[VTIME] = 0;
}
static int set_duplicating_descriptor(int fd, uv_pipe_t *pipe)
FUNC_ATTR_NONNULL_ALL
{
int status = 0; // zero or negative error code (libuv convention)
int fd_dup = dup(fd);
if (fd_dup < 0) {
status = -errno;
ELOG("Failed to dup descriptor %d: %s", fd, strerror(errno));
return status;
}
if (os_set_cloexec(fd_dup) == -1) {
status = -errno;
ELOG("Failed to set CLOEXEC on duplicate fd");
goto error;
}
status = uv_pipe_open(pipe, fd_dup);
if (status) {
ELOG("Failed to set pipe to descriptor %d: %s",
fd_dup, uv_strerror(status));
goto error;
}
return status;
error:
close(fd_dup);
return status;
}
static void chld_handler(uv_signal_t *handle, int signum)
{
int stat = 0;
int pid;
Loop *loop = handle->loop->data;
kl_iter(WatcherPtr, loop->children, current) {
Process *proc = (*current)->data;
do {
pid = waitpid(proc->pid, &stat, WNOHANG);
} while (pid < 0 && errno == EINTR);
if (pid <= 0) {
continue;
}
if (WIFEXITED(stat)) {
proc->status = WEXITSTATUS(stat);
} else if (WIFSIGNALED(stat)) {
proc->status = 128 + WTERMSIG(stat);
}
proc->internal_exit_cb(proc);
}
}
|