/
pty.c
414 lines (343 loc) · 8.14 KB
/
pty.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
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
407
408
409
410
411
412
413
414
/*
* kmscon - Pseudo Terminal Handling
*
* Copyright (c) 2012 Ran Benita <ran234@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <pty.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include "conf.h"
#include "eloop.h"
#include "log.h"
#include "misc.h"
#include "pty.h"
#define LOG_SUBSYSTEM "pty"
/* Match N_TTY_BUF_SIZE from the kernel to read as much as we can. */
#define KMSCON_NREAD 4096
struct kmscon_pty {
unsigned long ref;
struct ev_eloop *eloop;
int fd;
struct ev_fd *efd;
struct kmscon_ring *msgbuf;
char io_buf[KMSCON_NREAD];
kmscon_pty_input_cb input_cb;
void *data;
};
int kmscon_pty_new(struct kmscon_pty **out, struct ev_eloop *loop,
kmscon_pty_input_cb input_cb, void *data)
{
struct kmscon_pty *pty;
int ret;
if (!out || !loop || !input_cb)
return -EINVAL;
pty = malloc(sizeof(*pty));
if (!pty)
return -ENOMEM;
memset(pty, 0, sizeof(*pty));
pty->fd = -1;
pty->ref = 1;
pty->eloop = loop;
pty->input_cb = input_cb;
pty->data = data;
ret = kmscon_ring_new(&pty->msgbuf);
if (ret)
goto err_free;
log_debug("new pty object");
ev_eloop_ref(pty->eloop);
*out = pty;
return 0;
err_free:
free(pty);
return ret;
}
void kmscon_pty_ref(struct kmscon_pty *pty)
{
if (!pty)
return;
pty->ref++;
}
void kmscon_pty_unref(struct kmscon_pty *pty)
{
if (!pty || !pty->ref || --pty->ref)
return;
log_debug("free pty object");
kmscon_pty_close(pty);
kmscon_ring_free(pty->msgbuf);
ev_eloop_unref(pty->eloop);
free(pty);
}
static void __attribute__((noreturn))
exec_child(int pty_master)
{
setenv("TERM", conf_global.term, 1);
execvp(conf_global.login, conf_global.argv);
log_err("failed to exec child: %m");
_exit(EXIT_FAILURE);
}
static int setup_child(int master, struct winsize *ws)
{
int ret;
sigset_t sigset;
pid_t pid;
char slave_name[128];
int slave = -1;
/* The child should not inherit our signal mask. */
sigemptyset(&sigset);
ret = pthread_sigmask(SIG_SETMASK, &sigset, NULL);
if (ret)
log_warn("cannot reset blocked signals: %m");
ret = grantpt(master);
if (ret < 0) {
log_err("grantpt failed: %m");
goto err_out;
}
ret = unlockpt(master);
if (ret < 0) {
log_err("cannot unlock pty: %m");
goto err_out;
}
ret = ptsname_r(master, slave_name, sizeof(slave_name));
if (ret) {
log_err("cannot find slave name: %m");
goto err_out;
}
/* This also loses our controlling tty. */
pid = setsid();
if (pid < 0) {
log_err("cannot start a new session: %m");
goto err_out;
}
/* And the slave pty becomes our controlling tty. */
slave = open(slave_name, O_RDWR | O_CLOEXEC);
if (slave < 0) {
log_err("cannot open slave: %m");
goto err_out;
}
if (ws) {
ret = ioctl(slave, TIOCSWINSZ, ws);
if (ret)
log_warn("cannot set slave window size: %m");
}
if (dup2(slave, STDIN_FILENO) != STDIN_FILENO ||
dup2(slave, STDOUT_FILENO) != STDOUT_FILENO ||
dup2(slave, STDERR_FILENO) != STDERR_FILENO) {
log_err("cannot duplicate slave: %m");
goto err_out;
}
close(master);
close(slave);
return 0;
err_out:
ret = -errno;
if (slave > 0)
close(slave);
close(master);
return ret;
}
/*
* This is functionally equivalent to forkpty(3). We do it manually to obtain
* a little bit more control of the process, and as a bonus avoid linking to
* the libutil library in glibc.
*/
static int pty_spawn(struct kmscon_pty *pty, unsigned short width,
unsigned short height)
{
int ret;
pid_t pid;
int master;
struct winsize ws;
if (pty->fd >= 0)
return -EALREADY;
memset(&ws, 0, sizeof(ws));
ws.ws_col = width;
ws.ws_row = height;
master = posix_openpt(O_RDWR | O_NOCTTY | O_CLOEXEC | O_NONBLOCK);
if (master < 0) {
log_err("cannot open master: %m");
return -errno;
}
log_debug("forking child");
pid = fork();
switch (pid) {
case -1:
log_err("cannot fork: %m");
ret = -errno;
goto err_master;
case 0:
ret = setup_child(master, &ws);
if (ret)
goto err_master;
exec_child(pty->fd);
abort();
default:
pty->fd = master;
break;
}
return 0;
err_master:
close(master);
return ret;
}
static int send_buf(struct kmscon_pty *pty)
{
const char *buf;
size_t len;
int ret;
while ((buf = kmscon_ring_peek(pty->msgbuf, &len))) {
ret = write(pty->fd, buf, len);
if (ret > 0) {
kmscon_ring_drop(pty->msgbuf, ret);
continue;
}
if (ret < 0 && errno != EWOULDBLOCK) {
log_warn("cannot write to child process");
return ret;
}
/* EWOULDBLOCK */
return 0;
}
ev_eloop_update_fd(pty->efd, EV_READABLE);
return 0;
}
static void pty_input(struct ev_fd *fd, int mask, void *data)
{
int ret;
ssize_t len;
struct kmscon_pty *pty = data;
if (!pty || pty->fd < 0)
return;
if (mask & (EV_ERR | EV_HUP)) {
if (mask & EV_ERR)
log_warn("error on child pty socket");
else
log_debug("child closed remote end");
goto err;
}
if (mask & EV_WRITEABLE) {
ret = send_buf(pty);
if (ret)
goto err;
}
if (mask & EV_READABLE) {
len = read(pty->fd, pty->io_buf, sizeof(pty->io_buf));
if (len > 0) {
if (pty->input_cb)
pty->input_cb(pty, pty->io_buf, len, pty->data);
} else if (len == 0) {
log_debug("child closed remote end");
goto err;
} else if (errno != EWOULDBLOCK) {
log_err("cannot read from pty: %m");
goto err;
}
}
return;
err:
ev_eloop_rm_fd(pty->efd);
pty->efd = NULL;
if (pty->input_cb)
pty->input_cb(pty, NULL, 0, pty->data);
}
int kmscon_pty_open(struct kmscon_pty *pty, unsigned short width,
unsigned short height)
{
int ret;
if (!pty)
return -EINVAL;
if (pty->fd >= 0)
return -EALREADY;
ret = pty_spawn(pty, width, height);
if (ret)
return ret;
ret = ev_eloop_new_fd(pty->eloop, &pty->efd, pty->fd,
EV_READABLE, pty_input, pty);
if (ret) {
close(pty->fd);
pty->fd = -1;
return ret;
}
return 0;
}
void kmscon_pty_close(struct kmscon_pty *pty)
{
if (!pty || pty->fd < 0)
return;
ev_eloop_rm_fd(pty->efd);
pty->efd = NULL;
close(pty->fd);
pty->fd = -1;
}
int kmscon_pty_write(struct kmscon_pty *pty, const char *u8, size_t len)
{
int ret;
if (!pty || pty->fd < 0 || !u8 || !len)
return -EINVAL;
if (!kmscon_ring_is_empty(pty->msgbuf))
goto buf;
ret = write(pty->fd, u8, len);
if (ret < 0) {
if (errno != EWOULDBLOCK) {
log_warn("cannot write to child process");
return ret;
}
} else if (ret == len) {
return 0;
} else if (ret > 0) {
len -= ret;
u8 = &u8[ret];
}
ev_eloop_update_fd(pty->efd, EV_READABLE | EV_WRITEABLE);
buf:
ret = kmscon_ring_write(pty->msgbuf, u8, len);
if (ret)
log_warn("cannot allocate buffer; dropping input");
return 0;
}
void kmscon_pty_resize(struct kmscon_pty *pty,
unsigned short width, unsigned short height)
{
int ret;
struct winsize ws;
if (!pty || pty->fd < 0)
return;
memset(&ws, 0, sizeof(ws));
ws.ws_col = width;
ws.ws_row = height;
/*
* This will send SIGWINCH to the pty slave foreground process group.
* We will also get one, but we don't need it.
*/
ret = ioctl(pty->fd, TIOCSWINSZ, &ws);
if (ret) {
log_warn("cannot set window size");
return;
}
log_debug("window size set to %hdx%hd", ws.ws_col, ws.ws_row);
}