-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathpty.c
61 lines (51 loc) · 1.28 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
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <pty.h>
#include "pty.h"
/* Instead of using openpty from glibc, the following custom version is used
* because we need to bypass dynamically loading the nsswitch libraries.
* Executing glibc's openpty calls grantpt, which in turn depends on nsswitch
* being loaded. The version of glibc inside a container may be different than
* the version that wshd is compiled for, leading to undefined behavior. */
int openpty(int *master, int *slave, char *slave_name) {
int rv;
int lock;
int pty;
char buf[32];
/* Open master */
rv = open("/dev/ptmx", O_RDWR | O_NOCTTY);
if (rv == -1) {
return -1;
}
*master = rv;
/* Figure out PTY number */
pty = 0;
rv = ioctl(*master, TIOCGPTN, &pty);
if (rv == -1) {
return -1;
}
rv = snprintf(buf, sizeof(buf), "/dev/pts/%d", pty);
if (rv >= sizeof(buf)) {
return -1;
}
/* Unlock slave before opening it */
lock = 0;
rv = ioctl(*master, TIOCSPTLCK, &lock);
if (rv == -1) {
return -1;
}
/* Open slave */
rv = open(buf, O_RDWR | O_NOCTTY);
if (rv == -1) {
return -1;
}
*slave = rv;
if (slave_name != NULL) {
strcpy(slave_name, buf);
}
return 0;
}