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
|
/* fhandler_termios.cc
Copyright 1999, 2000, 2001, 2002 Red Hat, Inc.
This file is part of Cygwin.
This software is a copyrighted work licensed under the terms of the
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
details. */
#include "winsup.h"
#include <sys/termios.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <ctype.h>
#include "cygerrno.h"
#include "security.h"
#include "fhandler.h"
#include "sigproc.h"
#include "pinfo.h"
#include "tty.h"
/* Common functions shared by tty/console */
void
fhandler_termios::tcinit (tty_min *this_tc, int force)
{
/* Initial termios values */
tc = this_tc;
if (force || !TTYISSETF (INITIALIZED))
{
tc->ti.c_iflag = BRKINT | ICRNL | IXON;
tc->ti.c_oflag = OPOST | ONLCR;
tc->ti.c_cflag = B38400 | CS8 | CREAD;
tc->ti.c_lflag = ISIG | ICANON | ECHO | IEXTEN;
tc->ti.c_cc[VDISCARD] = CFLUSH;
tc->ti.c_cc[VEOL] = CEOL;
tc->ti.c_cc[VEOL2] = CEOL2;
tc->ti.c_cc[VEOF] = CEOF;
tc->ti.c_cc[VERASE] = CERASE;
tc->ti.c_cc[VINTR] = CINTR;
tc->ti.c_cc[VKILL] = CKILL;
tc->ti.c_cc[VLNEXT] = CLNEXT;
tc->ti.c_cc[VMIN] = 1;
tc->ti.c_cc[VQUIT] = CQUIT;
tc->ti.c_cc[VREPRINT] = CRPRNT;
tc->ti.c_cc[VSTART] = CSTART;
tc->ti.c_cc[VSTOP] = CSTOP;
tc->ti.c_cc[VSUSP] = CSUSP;
tc->ti.c_cc[VSWTC] = CSWTCH;
tc->ti.c_cc[VTIME] = 0;
tc->ti.c_cc[VWERASE] = CWERASE;
tc->ti.c_ispeed = tc->ti.c_ospeed = B38400;
tc->pgid = myself->pgid;
TTYSETF (INITIALIZED);
}
}
int
fhandler_termios::tcsetpgrp (const pid_t pgid)
{
termios_printf ("tty %d pgid %d, sid %d, tsid %d", tc->ntty, pgid,
myself->sid, tc->getsid ());
if (myself->sid != tc->getsid ())
{
set_errno (EPERM);
return -1;
}
tc->setpgid (pgid);
return 0;
}
int
fhandler_termios::tcgetpgrp ()
{
return tc->pgid;
}
void
tty_min::kill_pgrp (int sig)
{
int killself = 0;
winpids pids;
for (unsigned i = 0; i < pids.npids; i++)
{
_pinfo *p = pids[i];
if (!proc_exists (p) || p->ctty != ntty || p->pgid != pgid)
continue;
if (p == myself)
killself++;
else
(void) sig_send (p, sig);
}
if (killself)
sig_send (myself, sig);
}
void
tty_min::set_ctty (int ttynum, int flags)
{
if ((myself->ctty < 0 || myself->ctty == ttynum) && !(flags & O_NOCTTY))
{
myself->ctty = ttynum;
syscall_printf ("attached tty%d sid %d, pid %d, tty->pgid %d, tty->sid %d",
ttynum, myself->sid, myself->pid, pgid, getsid ());
pinfo p (getsid ());
if (myself->sid == myself->pid &&
(p == myself || !proc_exists (p)))
{
paranoid_printf ("resetting tty%d sid. Was %d, now %d. pgid was %d, now %d.",
ttynum, getsid (), myself->sid, getpgid (), myself->pgid);
/* We are the session leader */
setsid (myself->sid);
setpgid (myself->pgid);
}
else
myself->sid = getsid ();
if (getpgid () == 0)
setpgid (myself->pgid);
}
}
bg_check_types
fhandler_termios::bg_check (int sig)
{
if (!myself->pgid || tc->getpgid () == myself->pgid ||
myself->ctty != tc->ntty ||
((sig == SIGTTOU) && !(tc->ti.c_lflag & TOSTOP)))
return bg_ok;
if (sig < 0)
sig = -sig;
termios_printf ("bg I/O pgid %d, tpgid %d, ctty %d",
myself->pgid, tc->getpgid (), myself->ctty);
if (tc->getsid () == 0)
{
/* The pty has been closed by the master. Return an EOF
indication. FIXME: There is nothing to stop somebody
from reallocating this pty. I think this is the case
which is handled by unlockpt on a Unix system. */
termios_printf ("closed by master");
return bg_eof;
}
/* If the process group is no more or if process is ignoring or blocks 'sig',
return with error */
int pgid_gone = !pid_exists (myself->pgid);
int sigs_ignored =
((void *) myself->getsig (sig).sa_handler == (void *) SIG_IGN) ||
(myself->getsigmask () & SIGTOMASK (sig));
if (pgid_gone)
goto setEIO;
else if (!sigs_ignored)
/* nothing */;
else if (sig == SIGTTOU)
return bg_ok; /* Just allow the output */
else
goto setEIO; /* This is an output error */
/* Don't raise a SIGTT* signal if we have already been interrupted
by another signal. */
if (WaitForSingleObject (signal_arrived, 0) != WAIT_OBJECT_0)
kill_pgrp (myself->pgid, sig);
return bg_signalled;
setEIO:
set_errno (EIO);
return bg_error;
}
#define set_input_done(x) input_done = input_done || (x)
inline void
fhandler_termios::echo_erase (int force)
{
if (force || tc->ti.c_lflag & ECHO)
doecho ("\b \b", 3);
}
line_edit_status
fhandler_termios::line_edit (const char *rptr, int nread, int always_accept)
{
line_edit_status ret = line_edit_ok;
char c;
int input_done = 0;
bool sawsig = FALSE;
int iscanon = tc->ti.c_lflag & ICANON;
while (nread-- > 0)
{
c = *rptr++;
termios_printf ("char %c", c);
/* Check for special chars */
if (c == '\r')
{
if (tc->ti.c_iflag & IGNCR)
continue;
if (tc->ti.c_iflag & ICRNL)
{
c = '\n';
set_input_done (iscanon);
}
}
else if (c == '\n')
{
if (tc->ti.c_iflag & INLCR)
c = '\r';
else
set_input_done (iscanon);
}
if (tc->ti.c_iflag & ISTRIP)
c &= 0x7f;
if (tc->ti.c_lflag & ISIG)
{
int sig;
if (CCEQ (tc->ti.c_cc[VINTR], c))
sig = SIGINT;
else if (CCEQ (tc->ti.c_cc[VQUIT], c))
sig = SIGQUIT;
else if (CCEQ (tc->ti.c_cc[VSUSP], c))
sig = SIGTSTP;
else
goto not_a_sig;
termios_printf ("got interrupt %d, sending signal %d", c, sig);
eat_readahead (-1);
tc->kill_pgrp (sig);
tc->ti.c_lflag &= ~FLUSHO;
sawsig = 1;
goto restart_output;
}
not_a_sig:
if (tc->ti.c_iflag & IXON)
{
if (CCEQ (tc->ti.c_cc[VSTOP], c))
{
if (!tc->output_stopped)
{
tc->output_stopped = 1;
acquire_output_mutex (INFINITE);
}
continue;
}
else if (CCEQ (tc->ti.c_cc[VSTART], c))
{
restart_output:
tc->output_stopped = 0;
release_output_mutex ();
continue;
}
else if ((tc->ti.c_iflag & IXANY) && tc->output_stopped)
goto restart_output;
}
if (iscanon && tc->ti.c_lflag & IEXTEN && CCEQ (tc->ti.c_cc[VDISCARD], c))
{
tc->ti.c_lflag ^= FLUSHO;
continue;
}
if (!iscanon)
/* nothing */;
else if (CCEQ (tc->ti.c_cc[VERASE], c))
{
if (eat_readahead (1))
echo_erase ();
continue;
}
else if (CCEQ (tc->ti.c_cc[VWERASE], c))
{
int ch;
do
if (!eat_readahead (1))
break;
else
echo_erase ();
while ((ch = peek_readahead (1)) >= 0 && !isspace (ch));
continue;
}
else if (CCEQ (tc->ti.c_cc[VKILL], c))
{
int nchars = eat_readahead (-1);
if (tc->ti.c_lflag & ECHO)
while (nchars--)
echo_erase (1);
continue;
}
else if (CCEQ (tc->ti.c_cc[VREPRINT], c))
{
if (tc->ti.c_lflag & ECHO)
{
doecho ("\n\r", 2);
doecho (rabuf, ralen);
}
continue;
}
else if (CCEQ (tc->ti.c_cc[VEOF], c))
{
termios_printf ("EOF");
(void) accept_input();
ret = line_edit_input_done;
continue;
}
else if (CCEQ (tc->ti.c_cc[VEOL], c) ||
CCEQ (tc->ti.c_cc[VEOL2], c) ||
c == '\n')
{
set_input_done (1);
termios_printf ("EOL");
}
if (tc->ti.c_iflag & IUCLC && isupper (c))
c = cyg_tolower (c);
put_readahead (c);
if (!iscanon || always_accept || input_done)
{
(void) accept_input();
ret = line_edit_input_done;
input_done = 0;
}
if (tc->ti.c_lflag & ECHO)
doecho (&c, 1);
}
if ((!iscanon || always_accept) && ralen > 0)
ret = line_edit_input_done;
if (sawsig)
ret = line_edit_signalled;
return ret;
}
void
fhandler_termios::fixup_after_fork (HANDLE parent)
{
this->fhandler_base::fixup_after_fork (parent);
fork_fixup (parent, get_output_handle (), "output_handle");
}
__off64_t
fhandler_termios::lseek (__off64_t, int)
{
set_errno (ESPIPE);
return -1;
}
|