-
Notifications
You must be signed in to change notification settings - Fork 16
/
pty.c
230 lines (198 loc) · 5.09 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
#if defined(__gnu_linux__) || defined(__CYGWIN__)
#define _GNU_SOURCE
#endif
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include "common.h"
#include "pty.h"
int
ptym_open(char *pts_name, int pts_namesz)
{
char *ptr;
int fdm;
snprintf(pts_name, pts_namesz, "/dev/ptmx");
#if 1
fdm = posix_openpt(O_RDWR);
#else
/* FeeBSD 11.1 does not have "/dev/ptmx" */
fdm = open("/dev/ptmx", O_RDWR);
#endif
if (fdm < 0)
return (-1);
if (grantpt(fdm) < 0) {
close(fdm);
return (-2);
}
if (unlockpt(fdm) < 0) {
close(fdm);
return (-3);
}
if ((ptr = ptsname(fdm)) == NULL) {
close(fdm);
return (-4);
}
snprintf(pts_name, pts_namesz, "%s", ptr);
return (fdm);
}
int
ptys_open(char *pts_name)
{
int fds;
if ((fds = open(pts_name, O_RDWR)) < 0)
return (-5);
return (fds);
}
pid_t
pty_fork(int *ptrfdm, char *slave_name, int slave_namesz,
const struct termios *slave_termios,
const struct winsize *slave_winsize)
{
int fdm, fds;
pid_t pid;
char pts_name[32];
if ((fdm = ptym_open(pts_name, sizeof(pts_name))) < 0)
fatal_sys("can't open master pty: %s, error %d", pts_name, fdm);
if (slave_name != NULL) {
/*
* Return name of slave. Null terminate to handle case
* where strlen(pts_name) > slave_namesz.
*/
snprintf(slave_name, slave_namesz, "%s", pts_name);
}
if ((pid = fork()) < 0) {
return (-1);
} else if (pid == 0) {
/*
* child
*/
if (setsid() < 0)
fatal_sys("setsid error");
/*
* System V acquires controlling terminal on open().
*/
if ((fds = ptys_open(pts_name)) < 0)
fatal_sys("can't open slave pty");
/* all done with master in child */
close(fdm);
#if defined(TIOCSCTTY)
/*
* TIOCSCTTY is the BSD way to acquire a controlling terminal.
*
* Don't check the return code. It would fail in Cygwin.
*/
ioctl(fds, TIOCSCTTY, (char *)0);
#endif
/*
* Set slave's termios and window size.
*/
if (slave_termios != NULL) {
if (tcsetattr(fds, TCSANOW, slave_termios) < 0)
fatal_sys("tcsetattr error on slave pty");
}
if (slave_winsize != NULL) {
if (ioctl(fds, TIOCSWINSZ, slave_winsize) < 0)
fatal_sys("TIOCSWINSZ error on slave pty");
}
/*
* Slave becomes stdin/stdout/stderr of child.
*/
if (dup2(fds, STDIN_FILENO) != STDIN_FILENO)
fatal_sys("dup2 error to stdin");
if (dup2(fds, STDOUT_FILENO) != STDOUT_FILENO)
fatal_sys("dup2 error to stdout");
if (dup2(fds, STDERR_FILENO) != STDERR_FILENO)
fatal_sys("dup2 error to stderr");
if (fds != STDIN_FILENO && fds != STDOUT_FILENO &&
fds != STDERR_FILENO) {
close(fds);
}
return (0);
} else {
/*
* parent
*/
*ptrfdm = fdm;
return (pid);
}
}
int
tty_raw(int fd, struct termios *save_termios)
{
int err;
struct termios buf;
if (tcgetattr(fd, &buf) < 0)
return (-1);
*save_termios = buf;
#if 1
/*
* Echo off, canonical mode off, extended input
* processing off, signal chars off.
*/
buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
/*
* No SIGINT on BREAK, CR-to-NL off, input parity
* check off, don't strip 8th bit on input, output
* flow control off.
*/
buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
/*
* Clear size bits, parity checking off.
*/
buf.c_cflag &= ~(CSIZE | PARENB);
/*
* Set 8 bits/char.
*/
buf.c_cflag |= CS8;
/*
* Output processing off.
*/
buf.c_oflag &= ~(OPOST);
/*
* Case B: 1 byte at a time, no timer.
*/
buf.c_cc[VMIN] = 1;
buf.c_cc[VTIME] = 0;
#else
cfmakeraw(&buf);
#endif
if (tcsetattr(fd, TCSAFLUSH, &buf) < 0)
return (-1);
/*
* Verify that the changes stuck. tcsetattr can return 0 on
* partial success.
*/
if (tcgetattr(fd, &buf) < 0) {
err = errno;
tcsetattr(fd, TCSAFLUSH, save_termios);
errno = err;
return (-1);
}
if ((buf.c_lflag & (ECHO | ICANON | IEXTEN | ISIG)) ||
(buf.c_iflag & (BRKINT | ICRNL | INPCK | ISTRIP | IXON)) ||
(buf.c_cflag & (CSIZE | PARENB | CS8)) != CS8 ||
(buf.c_oflag & OPOST) || buf.c_cc[VMIN] != 1 ||
buf.c_cc[VTIME] != 0) {
/*
* Only some of the changes were made. Restore the
* original settings.
*/
tcsetattr(fd, TCSAFLUSH, save_termios);
errno = EINVAL;
return (-1);
}
return (0);
}
int
tty_reset(int fd, struct termios *termio)
{
if (tcsetattr(fd, TCSAFLUSH, termio) < 0)
return (-1);
return (0);
}