tty.c revision 1.111 1 /* $NetBSD: tty.c,v 1.111 1998/09/01 00:23:28 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1982, 1986, 1990, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * (c) UNIX System Laboratories, Inc.
7 * All or some portions of this file are derived from material licensed
8 * to the University of California by American Telephone and Telegraph
9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 * the permission of UNIX System Laboratories, Inc.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 *
40 * @(#)tty.c 8.13 (Berkeley) 1/9/95
41 */
42
43 #include "opt_uconsole.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/ioctl.h>
48 #include <sys/proc.h>
49 #define TTYDEFCHARS
50 #include <sys/tty.h>
51 #undef TTYDEFCHARS
52 #include <sys/file.h>
53 #include <sys/conf.h>
54 #include <sys/dkstat.h>
55 #include <sys/uio.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/syslog.h>
59 #include <sys/malloc.h>
60 #include <sys/pool.h>
61 #include <sys/signalvar.h>
62 #include <sys/resourcevar.h>
63 #include <sys/poll.h>
64
65 #include <vm/vm.h>
66
67 static int ttnread __P((struct tty *));
68 static void ttyblock __P((struct tty *));
69 static void ttyecho __P((int, struct tty *));
70 static void ttyrubo __P((struct tty *, int));
71 static int proc_compare __P((struct proc *, struct proc *));
72
73 /* Symbolic sleep message strings. */
74 const char ttclos[] = "ttycls";
75 const char ttopen[] = "ttyopn";
76 const char ttybg[] = "ttybg";
77 #ifdef REAL_CLISTS
78 const char ttybuf[] = "ttybuf";
79 #endif
80 const char ttyin[] = "ttyin";
81 const char ttyout[] = "ttyout";
82
83 /*
84 * Used to determine whether we still have a connection. This is true in
85 * one of 3 cases:
86 * 1) We have carrier.
87 * 2) It's a locally attached terminal, and we are therefore ignoring carrier.
88 * 3) We're using a flow control mechanism that overloads the carrier signal.
89 */
90 #define CONNECTED(tp) (ISSET(tp->t_state, TS_CARR_ON) || \
91 ISSET(tp->t_cflag, CLOCAL | MDMBUF))
92
93 /*
94 * Table with character classes and parity. The 8th bit indicates parity,
95 * the 7th bit indicates the character is an alphameric or underscore (for
96 * ALTWERASE), and the low 6 bits indicate delay type. If the low 6 bits
97 * are 0 then the character needs no special processing on output; classes
98 * other than 0 might be translated or (not currently) require delays.
99 */
100 #define E 0x00 /* Even parity. */
101 #define O 0x80 /* Odd parity. */
102 #define PARITY(c) (char_type[c] & O)
103
104 #define ALPHA 0x40 /* Alpha or underscore. */
105 #define ISALPHA(c) (char_type[(c) & TTY_CHARMASK] & ALPHA)
106
107 #define CCLASSMASK 0x3f
108 #define CCLASS(c) (char_type[c] & CCLASSMASK)
109
110 #define BS BACKSPACE
111 #define CC CONTROL
112 #define CR RETURN
113 #define NA ORDINARY | ALPHA
114 #define NL NEWLINE
115 #define NO ORDINARY
116 #define TB TAB
117 #define VT VTAB
118
119 char const char_type[] = {
120 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
121 O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
122 O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
123 E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
124 O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
125 E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
126 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
127 O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
128 O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
129 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
130 E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
131 O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
132 E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
133 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
134 O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
135 E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
136 /*
137 * Meta chars; should be settable per character set;
138 * for now, treat them all as normal characters.
139 */
140 NA, NA, NA, NA, NA, NA, NA, NA,
141 NA, NA, NA, NA, NA, NA, NA, NA,
142 NA, NA, NA, NA, NA, NA, NA, NA,
143 NA, NA, NA, NA, NA, NA, NA, NA,
144 NA, NA, NA, NA, NA, NA, NA, NA,
145 NA, NA, NA, NA, NA, NA, NA, NA,
146 NA, NA, NA, NA, NA, NA, NA, NA,
147 NA, NA, NA, NA, NA, NA, NA, NA,
148 NA, NA, NA, NA, NA, NA, NA, NA,
149 NA, NA, NA, NA, NA, NA, NA, NA,
150 NA, NA, NA, NA, NA, NA, NA, NA,
151 NA, NA, NA, NA, NA, NA, NA, NA,
152 NA, NA, NA, NA, NA, NA, NA, NA,
153 NA, NA, NA, NA, NA, NA, NA, NA,
154 NA, NA, NA, NA, NA, NA, NA, NA,
155 NA, NA, NA, NA, NA, NA, NA, NA,
156 };
157 #undef BS
158 #undef CC
159 #undef CR
160 #undef NA
161 #undef NL
162 #undef NO
163 #undef TB
164 #undef VT
165
166 /* Macros to clear/set/test flags. */
167 #define SET(t, f) (t) |= (f)
168 #define CLR(t, f) (t) &= ~((unsigned)(f))
169 #define ISSET(t, f) ((t) & (f))
170
171 struct ttylist_head ttylist; /* TAILQ_HEAD */
172 int tty_count;
173
174 struct pool tty_pool;
175
176 int
177 ttyopen(tp, dialout, nonblock)
178 struct tty *tp;
179 int dialout, nonblock;
180 {
181 int s;
182 int error;
183
184 s = spltty();
185
186 if (dialout) {
187 /*
188 * If the device is already open for non-dialout, fail.
189 * Otherwise, set TS_DIALOUT to block any pending non-dialout
190 * opens.
191 */
192 if (ISSET(tp->t_state, TS_ISOPEN) &&
193 !ISSET(tp->t_state, TS_DIALOUT)) {
194 splx(s);
195 return (EBUSY);
196 }
197 SET(tp->t_state, TS_DIALOUT);
198 } else {
199 if (!nonblock) {
200 /*
201 * Wait for carrier. Also wait for any dialout
202 * processes to close the tty first.
203 */
204 while (ISSET(tp->t_state, TS_DIALOUT) ||
205 (!ISSET(tp->t_state, TS_CARR_ON) &&
206 !ISSET(tp->t_cflag, CLOCAL | MDMBUF))) {
207 tp->t_wopen++;
208 error = ttysleep(tp, &tp->t_rawq,
209 TTIPRI | PCATCH, ttopen, 0);
210 tp->t_wopen--;
211 if (error) {
212 splx(s);
213 return (error);
214 }
215 }
216 } else {
217 /*
218 * Don't allow a non-blocking non-dialout open if the
219 * device is already open for dialout.
220 */
221 if (ISSET(tp->t_state, TS_DIALOUT)) {
222 splx(s);
223 return (EBUSY);
224 }
225 }
226 }
227
228 splx(s);
229 return (0);
230 }
231
232 /*
233 * Initial open of tty, or (re)entry to standard tty line discipline.
234 */
235 int
236 ttylopen(device, tp)
237 dev_t device;
238 register struct tty *tp;
239 {
240 int s;
241
242 s = spltty();
243 tp->t_dev = device;
244 if (!ISSET(tp->t_state, TS_ISOPEN)) {
245 SET(tp->t_state, TS_ISOPEN);
246 memset(&tp->t_winsize, 0, sizeof(tp->t_winsize));
247 #ifdef COMPAT_OLDTTY
248 tp->t_flags = 0;
249 #endif
250 }
251 splx(s);
252 return (0);
253 }
254
255 /*
256 * Handle close() on a tty line: flush and set to initial state,
257 * bumping generation number so that pending read/write calls
258 * can detect recycling of the tty.
259 */
260 int
261 ttyclose(tp)
262 register struct tty *tp;
263 {
264 extern struct tty *constty; /* Temporary virtual console. */
265
266 if (constty == tp)
267 constty = NULL;
268
269 ttyflush(tp, FREAD | FWRITE);
270
271 tp->t_gen++;
272 tp->t_pgrp = NULL;
273 tp->t_session = NULL;
274 tp->t_state = 0;
275 return (0);
276 }
277
278 #define FLUSHQ(q) { \
279 if ((q)->c_cc) \
280 ndflush(q, (q)->c_cc); \
281 }
282
283 /*
284 * This macro is used in canonical mode input processing, where a read
285 * request shall not return unless a 'line delimiter' ('\n') or 'break'
286 * (EOF, EOL, EOL2) character (or a signal) has been received. As EOL2
287 * is an extension to the POSIX.1 defined set of special characters,
288 * recognize it only if IEXTEN is set in the set of local flags.
289 */
290 #define TTBREAKC(c, lflg) \
291 ((c) == '\n' || (((c) == cc[VEOF] || (c) == cc[VEOL] || \
292 ((c) == cc[VEOL2] && ISSET(lflg, IEXTEN))) && (c) != _POSIX_VDISABLE))
293
294
295 /*
296 * Process input of a single character received on a tty.
297 */
298 int
299 ttyinput(c, tp)
300 register int c;
301 register struct tty *tp;
302 {
303 register int iflag, lflag;
304 register u_char *cc;
305 int i, error;
306
307 /*
308 * Unless the receiver is enabled, drop incoming data.
309 */
310 if (!ISSET(tp->t_cflag, CREAD))
311 return (0);
312
313 /*
314 * If input is pending take it first.
315 */
316 lflag = tp->t_lflag;
317 if (ISSET(lflag, PENDIN))
318 ttypend(tp);
319 /*
320 * Gather stats.
321 */
322 if (ISSET(lflag, ICANON)) {
323 ++tk_cancc;
324 ++tp->t_cancc;
325 } else {
326 ++tk_rawcc;
327 ++tp->t_rawcc;
328 }
329 ++tk_nin;
330
331 cc = tp->t_cc;
332
333 /*
334 * Handle exceptional conditions (break, parity, framing).
335 */
336 iflag = tp->t_iflag;
337 if ((error = (ISSET(c, TTY_ERRORMASK))) != 0) {
338 CLR(c, TTY_ERRORMASK);
339 if (ISSET(error, TTY_FE) && c == 0) { /* Break. */
340 if (ISSET(iflag, IGNBRK))
341 return (0);
342 else if (ISSET(iflag, BRKINT)) {
343 ttyflush(tp, FREAD | FWRITE);
344 pgsignal(tp->t_pgrp, SIGINT, 1);
345 return (0);
346 }
347 else if (ISSET(iflag, PARMRK))
348 goto parmrk;
349 }
350 else if ((ISSET(error, TTY_PE) && ISSET(iflag, INPCK)) ||
351 ISSET(error, TTY_FE)) {
352 if (ISSET(iflag, IGNPAR))
353 return (0);
354 else if (ISSET(iflag, PARMRK)) {
355 parmrk: (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
356 (void)putc(0 | TTY_QUOTE, &tp->t_rawq);
357 (void)putc(c | TTY_QUOTE, &tp->t_rawq);
358 return (0);
359 }
360 else
361 c = 0;
362 }
363 }
364 else if (c == 0377 &&
365 ISSET(iflag, ISTRIP|IGNPAR|INPCK|PARMRK) == (INPCK|PARMRK)) {
366 /* "Escape" a valid character of '\377'. */
367 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
368 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
369 goto endcase;
370 }
371
372 /*
373 * In tandem mode, check high water mark.
374 */
375 if (ISSET(iflag, IXOFF) || ISSET(tp->t_cflag, CHWFLOW))
376 ttyblock(tp);
377 if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
378 CLR(c, 0x80);
379 if (!ISSET(lflag, EXTPROC)) {
380 /*
381 * Check for literal nexting very first
382 */
383 if (ISSET(tp->t_state, TS_LNCH)) {
384 SET(c, TTY_QUOTE);
385 CLR(tp->t_state, TS_LNCH);
386 }
387 /*
388 * Scan for special characters. This code
389 * is really just a big case statement with
390 * non-constant cases. The bottom of the
391 * case statement is labeled ``endcase'', so goto
392 * it after a case match, or similar.
393 */
394
395 /*
396 * Control chars which aren't controlled
397 * by ICANON, ISIG, or IXON.
398 */
399 if (ISSET(lflag, IEXTEN)) {
400 if (CCEQ(cc[VLNEXT], c)) {
401 if (ISSET(lflag, ECHO)) {
402 if (ISSET(lflag, ECHOE)) {
403 (void)ttyoutput('^', tp);
404 (void)ttyoutput('\b', tp);
405 } else
406 ttyecho(c, tp);
407 }
408 SET(tp->t_state, TS_LNCH);
409 goto endcase;
410 }
411 if (CCEQ(cc[VDISCARD], c)) {
412 if (ISSET(lflag, FLUSHO))
413 CLR(tp->t_lflag, FLUSHO);
414 else {
415 ttyflush(tp, FWRITE);
416 ttyecho(c, tp);
417 if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
418 ttyretype(tp);
419 SET(tp->t_lflag, FLUSHO);
420 }
421 goto startoutput;
422 }
423 }
424 /*
425 * Signals.
426 */
427 if (ISSET(lflag, ISIG)) {
428 if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
429 if (!ISSET(lflag, NOFLSH))
430 ttyflush(tp, FREAD | FWRITE);
431 ttyecho(c, tp);
432 pgsignal(tp->t_pgrp,
433 CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
434 goto endcase;
435 }
436 if (CCEQ(cc[VSUSP], c)) {
437 if (!ISSET(lflag, NOFLSH))
438 ttyflush(tp, FREAD);
439 ttyecho(c, tp);
440 pgsignal(tp->t_pgrp, SIGTSTP, 1);
441 goto endcase;
442 }
443 }
444 /*
445 * Handle start/stop characters.
446 */
447 if (ISSET(iflag, IXON)) {
448 if (CCEQ(cc[VSTOP], c)) {
449 if (!ISSET(tp->t_state, TS_TTSTOP)) {
450 SET(tp->t_state, TS_TTSTOP);
451 (*cdevsw[major(tp->t_dev)].d_stop)(tp,
452 0);
453 return (0);
454 }
455 if (!CCEQ(cc[VSTART], c))
456 return (0);
457 /*
458 * if VSTART == VSTOP then toggle
459 */
460 goto endcase;
461 }
462 if (CCEQ(cc[VSTART], c))
463 goto restartoutput;
464 }
465 /*
466 * IGNCR, ICRNL, & INLCR
467 */
468 if (c == '\r') {
469 if (ISSET(iflag, IGNCR))
470 goto endcase;
471 else if (ISSET(iflag, ICRNL))
472 c = '\n';
473 } else if (c == '\n' && ISSET(iflag, INLCR))
474 c = '\r';
475 }
476 if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) {
477 /*
478 * From here on down canonical mode character
479 * processing takes place.
480 */
481 /*
482 * erase (^H / ^?)
483 */
484 if (CCEQ(cc[VERASE], c)) {
485 if (tp->t_rawq.c_cc)
486 ttyrub(unputc(&tp->t_rawq), tp);
487 goto endcase;
488 }
489 /*
490 * kill (^U)
491 */
492 if (CCEQ(cc[VKILL], c)) {
493 if (ISSET(lflag, ECHOKE) &&
494 tp->t_rawq.c_cc == tp->t_rocount &&
495 !ISSET(lflag, ECHOPRT))
496 while (tp->t_rawq.c_cc)
497 ttyrub(unputc(&tp->t_rawq), tp);
498 else {
499 ttyecho(c, tp);
500 if (ISSET(lflag, ECHOK) ||
501 ISSET(lflag, ECHOKE))
502 ttyecho('\n', tp);
503 FLUSHQ(&tp->t_rawq);
504 tp->t_rocount = 0;
505 }
506 CLR(tp->t_state, TS_LOCAL);
507 goto endcase;
508 }
509 /*
510 * Extensions to the POSIX.1 GTI set of functions.
511 */
512 if (ISSET(lflag, IEXTEN)) {
513 /*
514 * word erase (^W)
515 */
516 if (CCEQ(cc[VWERASE], c)) {
517 int alt = ISSET(lflag, ALTWERASE);
518 int ctype;
519
520 /*
521 * erase whitespace
522 */
523 while ((c = unputc(&tp->t_rawq)) == ' ' ||
524 c == '\t')
525 ttyrub(c, tp);
526 if (c == -1)
527 goto endcase;
528 /*
529 * erase last char of word and remember the
530 * next chars type (for ALTWERASE)
531 */
532 ttyrub(c, tp);
533 c = unputc(&tp->t_rawq);
534 if (c == -1)
535 goto endcase;
536 if (c == ' ' || c == '\t') {
537 (void)putc(c, &tp->t_rawq);
538 goto endcase;
539 }
540 ctype = ISALPHA(c);
541 /*
542 * erase rest of word
543 */
544 do {
545 ttyrub(c, tp);
546 c = unputc(&tp->t_rawq);
547 if (c == -1)
548 goto endcase;
549 } while (c != ' ' && c != '\t' &&
550 (alt == 0 || ISALPHA(c) == ctype));
551 (void)putc(c, &tp->t_rawq);
552 goto endcase;
553 }
554 /*
555 * reprint line (^R)
556 */
557 if (CCEQ(cc[VREPRINT], c)) {
558 ttyretype(tp);
559 goto endcase;
560 }
561 /*
562 * ^T - kernel info and generate SIGINFO
563 */
564 if (CCEQ(cc[VSTATUS], c)) {
565 if (ISSET(lflag, ISIG))
566 pgsignal(tp->t_pgrp, SIGINFO, 1);
567 if (!ISSET(lflag, NOKERNINFO))
568 ttyinfo(tp);
569 goto endcase;
570 }
571 }
572 }
573 /*
574 * Check for input buffer overflow
575 */
576 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= TTYHOG) {
577 if (ISSET(iflag, IMAXBEL)) {
578 if (tp->t_outq.c_cc < tp->t_hiwat)
579 (void)ttyoutput(CTRL('g'), tp);
580 } else
581 ttyflush(tp, FREAD | FWRITE);
582 goto endcase;
583 }
584 /*
585 * Put data char in q for user and
586 * wakeup on seeing a line delimiter.
587 */
588 if (putc(c, &tp->t_rawq) >= 0) {
589 if (!ISSET(lflag, ICANON)) {
590 ttwakeup(tp);
591 ttyecho(c, tp);
592 goto endcase;
593 }
594 if (TTBREAKC(c, lflag)) {
595 tp->t_rocount = 0;
596 catq(&tp->t_rawq, &tp->t_canq);
597 ttwakeup(tp);
598 } else if (tp->t_rocount++ == 0)
599 tp->t_rocol = tp->t_column;
600 if (ISSET(tp->t_state, TS_ERASE)) {
601 /*
602 * end of prterase \.../
603 */
604 CLR(tp->t_state, TS_ERASE);
605 (void)ttyoutput('/', tp);
606 }
607 i = tp->t_column;
608 ttyecho(c, tp);
609 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
610 /*
611 * Place the cursor over the '^' of the ^D.
612 */
613 i = min(2, tp->t_column - i);
614 while (i > 0) {
615 (void)ttyoutput('\b', tp);
616 i--;
617 }
618 }
619 }
620 endcase:
621 /*
622 * IXANY means allow any character to restart output.
623 */
624 if (ISSET(tp->t_state, TS_TTSTOP) &&
625 !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP])
626 return (0);
627 restartoutput:
628 CLR(tp->t_lflag, FLUSHO);
629 CLR(tp->t_state, TS_TTSTOP);
630 startoutput:
631 return (ttstart(tp));
632 }
633
634 /*
635 * Output a single character on a tty, doing output processing
636 * as needed (expanding tabs, newline processing, etc.).
637 * Returns < 0 if succeeds, otherwise returns char to resend.
638 * Must be recursive.
639 */
640 int
641 ttyoutput(c, tp)
642 register int c;
643 register struct tty *tp;
644 {
645 register long oflag;
646 register int col, notout, s;
647
648 oflag = tp->t_oflag;
649 if (!ISSET(oflag, OPOST)) {
650 tk_nout++;
651 tp->t_outcc++;
652 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
653 return (c);
654 return (-1);
655 }
656 /*
657 * Do tab expansion if OXTABS is set. Special case if we do external
658 * processing, we don't do the tab expansion because we'll probably
659 * get it wrong. If tab expansion needs to be done, let it happen
660 * externally.
661 */
662 CLR(c, ~TTY_CHARMASK);
663 if (c == '\t' &&
664 ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
665 c = 8 - (tp->t_column & 7);
666 if (ISSET(tp->t_lflag, FLUSHO)) {
667 notout = 0;
668 } else {
669 s = spltty(); /* Don't interrupt tabs. */
670 notout = b_to_q(" ", c, &tp->t_outq);
671 c -= notout;
672 tk_nout += c;
673 tp->t_outcc += c;
674 splx(s);
675 }
676 tp->t_column += c;
677 return (notout ? '\t' : -1);
678 }
679 if (c == CEOT && ISSET(oflag, ONOEOT))
680 return (-1);
681
682 /*
683 * Newline translation: if ONLCR is set,
684 * translate newline into "\r\n".
685 */
686 if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
687 tk_nout++;
688 tp->t_outcc++;
689 if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq))
690 return (c);
691 }
692 /* If OCRNL is set, translate "\r" into "\n". */
693 else if (c == '\r' && ISSET(tp->t_oflag, OCRNL))
694 c = '\n';
695 /* If ONOCR is set, don't transmit CRs when on column 0. */
696 else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0)
697 return (-1);
698
699 tk_nout++;
700 tp->t_outcc++;
701 if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
702 return (c);
703
704 col = tp->t_column;
705 switch (CCLASS(c)) {
706 case BACKSPACE:
707 if (col > 0)
708 --col;
709 break;
710 case CONTROL:
711 break;
712 case NEWLINE:
713 if (ISSET(tp->t_oflag, ONLCR | ONLRET))
714 col = 0;
715 break;
716 case RETURN:
717 col = 0;
718 break;
719 case ORDINARY:
720 ++col;
721 break;
722 case TAB:
723 col = (col + 8) & ~7;
724 break;
725 }
726 tp->t_column = col;
727 return (-1);
728 }
729
730 /*
731 * Ioctls for all tty devices. Called after line-discipline specific ioctl
732 * has been called to do discipline-specific functions and/or reject any
733 * of these ioctl commands.
734 */
735 /* ARGSUSED */
736 int
737 ttioctl(tp, cmd, data, flag, p)
738 register struct tty *tp;
739 u_long cmd;
740 caddr_t data;
741 int flag;
742 struct proc *p;
743 {
744 extern struct tty *constty; /* Temporary virtual console. */
745 extern int nlinesw;
746 int s, error;
747
748 /* If the ioctl involves modification, hang if in the background. */
749 switch (cmd) {
750 case TIOCFLUSH:
751 case TIOCDRAIN:
752 case TIOCSBRK:
753 case TIOCCBRK:
754 case TIOCSTART:
755 case TIOCSETA:
756 case TIOCSETD:
757 case TIOCSETAF:
758 case TIOCSETAW:
759 #ifdef notdef
760 case TIOCSPGRP:
761 #endif
762 case TIOCSTAT:
763 case TIOCSTI:
764 case TIOCSWINSZ:
765 #ifdef COMPAT_OLDTTY
766 case TIOCLBIC:
767 case TIOCLBIS:
768 case TIOCLSET:
769 case TIOCSETC:
770 case OTIOCSETD:
771 case TIOCSETN:
772 case TIOCSETP:
773 case TIOCSLTC:
774 #endif
775 while (isbackground(curproc, tp) &&
776 p->p_pgrp->pg_jobc && (p->p_flag & P_PPWAIT) == 0 &&
777 (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
778 (p->p_sigmask & sigmask(SIGTTOU)) == 0) {
779 pgsignal(p->p_pgrp, SIGTTOU, 1);
780 error = ttysleep(tp,
781 &lbolt, TTOPRI | PCATCH, ttybg, 0);
782 if (error)
783 return (error);
784 }
785 break;
786 }
787
788 switch (cmd) { /* Process the ioctl. */
789 case FIOASYNC: /* set/clear async i/o */
790 s = spltty();
791 if (*(int *)data)
792 SET(tp->t_state, TS_ASYNC);
793 else
794 CLR(tp->t_state, TS_ASYNC);
795 splx(s);
796 break;
797 case FIONBIO: /* set/clear non-blocking i/o */
798 break; /* XXX: delete. */
799 case FIONREAD: /* get # bytes to read */
800 *(int *)data = ttnread(tp);
801 break;
802 case TIOCEXCL: /* set exclusive use of tty */
803 s = spltty();
804 SET(tp->t_state, TS_XCLUDE);
805 splx(s);
806 break;
807 case TIOCFLUSH: { /* flush buffers */
808 register int flags = *(int *)data;
809
810 if (flags == 0)
811 flags = FREAD | FWRITE;
812 else
813 flags &= FREAD | FWRITE;
814 ttyflush(tp, flags);
815 break;
816 }
817 case TIOCCONS: /* become virtual console */
818 if (*(int *)data) {
819 if (constty && constty != tp &&
820 ISSET(constty->t_state, TS_CARR_ON | TS_ISOPEN) ==
821 (TS_CARR_ON | TS_ISOPEN))
822 return (EBUSY);
823 #ifndef UCONSOLE
824 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
825 return (error);
826 #endif
827 constty = tp;
828 } else if (tp == constty)
829 constty = NULL;
830 break;
831 case TIOCDRAIN: /* wait till output drained */
832 if ((error = ttywait(tp)) != 0)
833 return (error);
834 break;
835 case TIOCGETA: { /* get termios struct */
836 struct termios *t = (struct termios *)data;
837
838 memcpy(t, &tp->t_termios, sizeof(struct termios));
839 break;
840 }
841 case TIOCGETD: /* get line discipline */
842 *(int *)data = tp->t_line;
843 break;
844 case TIOCGWINSZ: /* get window size */
845 *(struct winsize *)data = tp->t_winsize;
846 break;
847 case TIOCGPGRP: /* get pgrp of tty */
848 if (!isctty(p, tp))
849 return (ENOTTY);
850 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
851 break;
852 case TIOCGSID: /* get sid of tty */
853 if (!isctty(p, tp))
854 return (ENOTTY);
855 *(int *)data = tp->t_session->s_sid;
856 break;
857 #ifdef TIOCHPCL
858 case TIOCHPCL: /* hang up on last close */
859 s = spltty();
860 SET(tp->t_cflag, HUPCL);
861 splx(s);
862 break;
863 #endif
864 case TIOCNXCL: /* reset exclusive use of tty */
865 s = spltty();
866 CLR(tp->t_state, TS_XCLUDE);
867 splx(s);
868 break;
869 case TIOCOUTQ: /* output queue size */
870 *(int *)data = tp->t_outq.c_cc;
871 break;
872 case TIOCSETA: /* set termios struct */
873 case TIOCSETAW: /* drain output, set */
874 case TIOCSETAF: { /* drn out, fls in, set */
875 register struct termios *t = (struct termios *)data;
876
877 s = spltty();
878 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
879 if ((error = ttywait(tp)) != 0) {
880 splx(s);
881 return (error);
882 }
883 if (cmd == TIOCSETAF)
884 ttyflush(tp, FREAD);
885 }
886 if (!ISSET(t->c_cflag, CIGNORE)) {
887 /*
888 * Set device hardware.
889 */
890 if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
891 splx(s);
892 return (error);
893 } else {
894 tp->t_cflag = t->c_cflag;
895 tp->t_ispeed = t->c_ispeed;
896 tp->t_ospeed = t->c_ospeed;
897 if (t->c_ospeed == 0 && tp->t_session &&
898 tp->t_session->s_leader)
899 psignal(tp->t_session->s_leader,
900 SIGHUP);
901 }
902 ttsetwater(tp);
903 }
904 if (cmd != TIOCSETAF) {
905 if (ISSET(t->c_lflag, ICANON) !=
906 ISSET(tp->t_lflag, ICANON)) {
907 if (ISSET(t->c_lflag, ICANON)) {
908 SET(tp->t_lflag, PENDIN);
909 ttwakeup(tp);
910 } else {
911 struct clist tq;
912
913 catq(&tp->t_rawq, &tp->t_canq);
914 tq = tp->t_rawq;
915 tp->t_rawq = tp->t_canq;
916 tp->t_canq = tq;
917 CLR(tp->t_lflag, PENDIN);
918 }
919 }
920 }
921 tp->t_iflag = t->c_iflag;
922 tp->t_oflag = t->c_oflag;
923 /*
924 * Make the EXTPROC bit read only.
925 */
926 if (ISSET(tp->t_lflag, EXTPROC))
927 SET(t->c_lflag, EXTPROC);
928 else
929 CLR(t->c_lflag, EXTPROC);
930 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
931 memcpy(tp->t_cc, t->c_cc, sizeof(t->c_cc));
932 splx(s);
933 break;
934 }
935 case TIOCSETD: { /* set line discipline */
936 register int t = *(int *)data;
937 dev_t device = tp->t_dev;
938
939 if ((u_int)t >= nlinesw)
940 return (ENXIO);
941 if (t != tp->t_line) {
942 s = spltty();
943 (*linesw[tp->t_line].l_close)(tp, flag);
944 error = (*linesw[t].l_open)(device, tp);
945 if (error) {
946 (void)(*linesw[tp->t_line].l_open)(device, tp);
947 splx(s);
948 return (error);
949 }
950 tp->t_line = t;
951 splx(s);
952 }
953 break;
954 }
955 case TIOCSTART: /* start output, like ^Q */
956 s = spltty();
957 if (ISSET(tp->t_state, TS_TTSTOP) ||
958 ISSET(tp->t_lflag, FLUSHO)) {
959 CLR(tp->t_lflag, FLUSHO);
960 CLR(tp->t_state, TS_TTSTOP);
961 ttstart(tp);
962 }
963 splx(s);
964 break;
965 case TIOCSTI: /* simulate terminal input */
966 if (p->p_ucred->cr_uid && (flag & FREAD) == 0)
967 return (EPERM);
968 if (p->p_ucred->cr_uid && !isctty(p, tp))
969 return (EACCES);
970 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp);
971 break;
972 case TIOCSTOP: /* stop output, like ^S */
973 s = spltty();
974 if (!ISSET(tp->t_state, TS_TTSTOP)) {
975 SET(tp->t_state, TS_TTSTOP);
976 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
977 }
978 splx(s);
979 break;
980 case TIOCSCTTY: /* become controlling tty */
981 /* Session ctty vnode pointer set in vnode layer. */
982 if (!SESS_LEADER(p) ||
983 ((p->p_session->s_ttyvp || tp->t_session) &&
984 (tp->t_session != p->p_session)))
985 return (EPERM);
986 tp->t_session = p->p_session;
987 tp->t_pgrp = p->p_pgrp;
988 p->p_session->s_ttyp = tp;
989 p->p_flag |= P_CONTROLT;
990 break;
991 case TIOCSPGRP: { /* set pgrp of tty */
992 register struct pgrp *pgrp = pgfind(*(int *)data);
993
994 if (!isctty(p, tp))
995 return (ENOTTY);
996 else if (pgrp == NULL)
997 return (EINVAL);
998 else if (pgrp->pg_session != p->p_session)
999 return (EPERM);
1000 tp->t_pgrp = pgrp;
1001 break;
1002 }
1003 case TIOCSTAT: /* get load avg stats */
1004 ttyinfo(tp);
1005 break;
1006 case TIOCSWINSZ: /* set window size */
1007 if (memcmp((caddr_t)&tp->t_winsize, data,
1008 sizeof(struct winsize))) {
1009 tp->t_winsize = *(struct winsize *)data;
1010 pgsignal(tp->t_pgrp, SIGWINCH, 1);
1011 }
1012 break;
1013 default:
1014 #ifdef COMPAT_OLDTTY
1015 return (ttcompat(tp, cmd, data, flag, p));
1016 #else
1017 return (-1);
1018 #endif
1019 }
1020 return (0);
1021 }
1022
1023 int
1024 ttpoll(dev, events, p)
1025 dev_t dev;
1026 int events;
1027 struct proc *p;
1028 {
1029 register struct tty *tp = (*cdevsw[major(dev)].d_tty)(dev);
1030 int revents = 0;
1031 int s = spltty();
1032
1033 if (events & (POLLIN | POLLRDNORM))
1034 if (ttnread(tp) > 0)
1035 revents |= events & (POLLIN | POLLRDNORM);
1036
1037 if (events & (POLLOUT | POLLWRNORM))
1038 if (tp->t_outq.c_cc <= tp->t_lowat)
1039 revents |= events & (POLLOUT | POLLWRNORM);
1040
1041 if (events & POLLHUP)
1042 if (!CONNECTED(tp))
1043 revents |= POLLHUP;
1044
1045 if (revents == 0) {
1046 if (events & (POLLIN | POLLHUP | POLLRDNORM))
1047 selrecord(p, &tp->t_rsel);
1048
1049 if (events & (POLLOUT | POLLWRNORM))
1050 selrecord(p, &tp->t_wsel);
1051 }
1052
1053 splx(s);
1054 return (revents);
1055 }
1056
1057 static int
1058 ttnread(tp)
1059 struct tty *tp;
1060 {
1061 int nread;
1062
1063 if (ISSET(tp->t_lflag, PENDIN))
1064 ttypend(tp);
1065 nread = tp->t_canq.c_cc;
1066 if (!ISSET(tp->t_lflag, ICANON)) {
1067 nread += tp->t_rawq.c_cc;
1068 if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME])
1069 nread = 0;
1070 }
1071 return (nread);
1072 }
1073
1074 /*
1075 * Wait for output to drain.
1076 */
1077 int
1078 ttywait(tp)
1079 register struct tty *tp;
1080 {
1081 int error, s;
1082
1083 error = 0;
1084 s = spltty();
1085 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1086 CONNECTED(tp) && tp->t_oproc) {
1087 (*tp->t_oproc)(tp);
1088 SET(tp->t_state, TS_ASLEEP);
1089 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1090 if (error)
1091 break;
1092 }
1093 splx(s);
1094 return (error);
1095 }
1096
1097 /*
1098 * Flush if successfully wait.
1099 */
1100 int
1101 ttywflush(tp)
1102 struct tty *tp;
1103 {
1104 int error;
1105
1106 if ((error = ttywait(tp)) == 0)
1107 ttyflush(tp, FREAD);
1108 return (error);
1109 }
1110
1111 /*
1112 * Flush tty read and/or write queues, notifying anyone waiting.
1113 */
1114 void
1115 ttyflush(tp, rw)
1116 register struct tty *tp;
1117 int rw;
1118 {
1119 register int s;
1120
1121 s = spltty();
1122 if (rw & FREAD) {
1123 FLUSHQ(&tp->t_canq);
1124 FLUSHQ(&tp->t_rawq);
1125 tp->t_rocount = 0;
1126 tp->t_rocol = 0;
1127 CLR(tp->t_state, TS_LOCAL);
1128 ttwakeup(tp);
1129 }
1130 if (rw & FWRITE) {
1131 CLR(tp->t_state, TS_TTSTOP);
1132 (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
1133 FLUSHQ(&tp->t_outq);
1134 wakeup((caddr_t)&tp->t_outq);
1135 selwakeup(&tp->t_wsel);
1136 }
1137 splx(s);
1138 }
1139
1140 /*
1141 * Copy in the default termios characters.
1142 */
1143 void
1144 ttychars(tp)
1145 struct tty *tp;
1146 {
1147
1148 memcpy(tp->t_cc, ttydefchars, sizeof(ttydefchars));
1149 }
1150
1151 /*
1152 * Send stop character on input overflow.
1153 */
1154 static void
1155 ttyblock(tp)
1156 register struct tty *tp;
1157 {
1158 register int total;
1159
1160 total = tp->t_rawq.c_cc + tp->t_canq.c_cc;
1161 if (tp->t_rawq.c_cc > TTYHOG) {
1162 ttyflush(tp, FREAD | FWRITE);
1163 CLR(tp->t_state, TS_TBLOCK);
1164 }
1165 /*
1166 * Block further input iff: current input > threshold
1167 * AND input is available to user program.
1168 */
1169 if (total >= TTYHOG / 2 &&
1170 !ISSET(tp->t_state, TS_TBLOCK) &&
1171 (!ISSET(tp->t_lflag, ICANON) || tp->t_canq.c_cc > 0)) {
1172 if (ISSET(tp->t_iflag, IXOFF) &&
1173 tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1174 putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
1175 SET(tp->t_state, TS_TBLOCK);
1176 ttstart(tp);
1177 }
1178 /* Try to block remote output via hardware flow control. */
1179 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1180 (*tp->t_hwiflow)(tp, 1) != 0)
1181 SET(tp->t_state, TS_TBLOCK);
1182 }
1183 }
1184
1185 void
1186 ttrstrt(tp_arg)
1187 void *tp_arg;
1188 {
1189 struct tty *tp;
1190 int s;
1191
1192 #ifdef DIAGNOSTIC
1193 if (tp_arg == NULL)
1194 panic("ttrstrt");
1195 #endif
1196 tp = tp_arg;
1197 s = spltty();
1198
1199 CLR(tp->t_state, TS_TIMEOUT);
1200 ttstart(tp);
1201
1202 splx(s);
1203 }
1204
1205 int
1206 ttstart(tp)
1207 struct tty *tp;
1208 {
1209
1210 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1211 (*tp->t_oproc)(tp);
1212 return (0);
1213 }
1214
1215 /*
1216 * "close" a line discipline
1217 */
1218 int
1219 ttylclose(tp, flag)
1220 struct tty *tp;
1221 int flag;
1222 {
1223
1224 if (flag & FNONBLOCK)
1225 ttyflush(tp, FREAD | FWRITE);
1226 else
1227 ttywflush(tp);
1228 return (0);
1229 }
1230
1231 /*
1232 * Handle modem control transition on a tty.
1233 * Flag indicates new state of carrier.
1234 * Returns 0 if the line should be turned off, otherwise 1.
1235 */
1236 int
1237 ttymodem(tp, flag)
1238 register struct tty *tp;
1239 int flag;
1240 {
1241
1242 if (flag == 0) {
1243 if (ISSET(tp->t_state, TS_CARR_ON)) {
1244 /*
1245 * Lost carrier.
1246 */
1247 CLR(tp->t_state, TS_CARR_ON);
1248 if (ISSET(tp->t_state, TS_ISOPEN) && !CONNECTED(tp)) {
1249 if (tp->t_session && tp->t_session->s_leader)
1250 psignal(tp->t_session->s_leader, SIGHUP);
1251 ttyflush(tp, FREAD | FWRITE);
1252 return (0);
1253 }
1254 }
1255 } else {
1256 if (!ISSET(tp->t_state, TS_CARR_ON)) {
1257 /*
1258 * Carrier now on.
1259 */
1260 SET(tp->t_state, TS_CARR_ON);
1261 ttwakeup(tp);
1262 }
1263 }
1264 return (1);
1265 }
1266
1267 /*
1268 * Default modem control routine (for other line disciplines).
1269 * Return argument flag, to turn off device on carrier drop.
1270 */
1271 int
1272 nullmodem(tp, flag)
1273 register struct tty *tp;
1274 int flag;
1275 {
1276
1277 if (flag)
1278 SET(tp->t_state, TS_CARR_ON);
1279 else {
1280 CLR(tp->t_state, TS_CARR_ON);
1281 if (!CONNECTED(tp)) {
1282 if (tp->t_session && tp->t_session->s_leader)
1283 psignal(tp->t_session->s_leader, SIGHUP);
1284 return (0);
1285 }
1286 }
1287 return (1);
1288 }
1289
1290 /*
1291 * Reinput pending characters after state switch
1292 * call at spltty().
1293 */
1294 void
1295 ttypend(tp)
1296 register struct tty *tp;
1297 {
1298 struct clist tq;
1299 register int c;
1300
1301 CLR(tp->t_lflag, PENDIN);
1302 SET(tp->t_state, TS_TYPEN);
1303 tq = tp->t_rawq;
1304 tp->t_rawq.c_cc = 0;
1305 tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
1306 while ((c = getc(&tq)) >= 0)
1307 ttyinput(c, tp);
1308 CLR(tp->t_state, TS_TYPEN);
1309 }
1310
1311 /*
1312 * Process a read call on a tty device.
1313 */
1314 int
1315 ttread(tp, uio, flag)
1316 register struct tty *tp;
1317 struct uio *uio;
1318 int flag;
1319 {
1320 register struct clist *qp;
1321 register int c;
1322 register long lflag;
1323 register u_char *cc = tp->t_cc;
1324 register struct proc *p = curproc;
1325 int s, first, error = 0;
1326 struct timeval stime;
1327 int has_stime = 0, last_cc = 0;
1328 long slp = 0;
1329
1330 loop: lflag = tp->t_lflag;
1331 s = spltty();
1332 /*
1333 * take pending input first
1334 */
1335 if (ISSET(lflag, PENDIN))
1336 ttypend(tp);
1337 splx(s);
1338
1339 /*
1340 * Hang process if it's in the background.
1341 */
1342 if (isbackground(p, tp)) {
1343 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
1344 (p->p_sigmask & sigmask(SIGTTIN)) ||
1345 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0)
1346 return (EIO);
1347 pgsignal(p->p_pgrp, SIGTTIN, 1);
1348 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1349 if (error)
1350 return (error);
1351 goto loop;
1352 }
1353
1354 s = spltty();
1355 if (!ISSET(lflag, ICANON)) {
1356 int m = cc[VMIN];
1357 long t = cc[VTIME];
1358
1359 qp = &tp->t_rawq;
1360 /*
1361 * Check each of the four combinations.
1362 * (m > 0 && t == 0) is the normal read case.
1363 * It should be fairly efficient, so we check that and its
1364 * companion case (m == 0 && t == 0) first.
1365 * For the other two cases, we compute the target sleep time
1366 * into slp.
1367 */
1368 if (t == 0) {
1369 if (qp->c_cc < m)
1370 goto sleep;
1371 goto read;
1372 }
1373 t *= 100000; /* time in us */
1374 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1375 ((t1).tv_usec - (t2).tv_usec))
1376 if (m > 0) {
1377 if (qp->c_cc <= 0)
1378 goto sleep;
1379 if (qp->c_cc >= m)
1380 goto read;
1381 if (!has_stime) {
1382 /* first character, start timer */
1383 has_stime = 1;
1384 stime = time;
1385 slp = t;
1386 } else if (qp->c_cc > last_cc) {
1387 /* got a character, restart timer */
1388 stime = time;
1389 slp = t;
1390 } else {
1391 /* nothing, check expiration */
1392 slp = t - diff(time, stime);
1393 }
1394 } else { /* m == 0 */
1395 if (qp->c_cc > 0)
1396 goto read;
1397 if (!has_stime) {
1398 has_stime = 1;
1399 stime = time;
1400 slp = t;
1401 } else
1402 slp = t - diff(time, stime);
1403 }
1404 last_cc = qp->c_cc;
1405 #undef diff
1406 if (slp > 0) {
1407 /*
1408 * Rounding down may make us wake up just short
1409 * of the target, so we round up.
1410 * The formula is ceiling(slp * hz/1000000).
1411 * 32-bit arithmetic is enough for hz < 169.
1412 *
1413 * Also, use plain wakeup() not ttwakeup().
1414 */
1415 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1416 goto sleep;
1417 }
1418 } else if ((qp = &tp->t_canq)->c_cc <= 0) {
1419 int carrier;
1420
1421 sleep:
1422 /*
1423 * If there is no input, sleep on rawq
1424 * awaiting hardware receipt and notification.
1425 * If we have data, we don't need to check for carrier.
1426 */
1427 carrier = CONNECTED(tp);
1428 if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) {
1429 splx(s);
1430 return (0); /* EOF */
1431 }
1432 if (flag & IO_NDELAY) {
1433 splx(s);
1434 return (EWOULDBLOCK);
1435 }
1436 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
1437 carrier ? ttyin : ttopen, slp);
1438 splx(s);
1439 /* VMIN == 0: any quantity read satisfies */
1440 if (cc[VMIN] == 0 && error == EWOULDBLOCK)
1441 return (0);
1442 if (error && error != EWOULDBLOCK)
1443 return (error);
1444 goto loop;
1445 }
1446 read:
1447 splx(s);
1448
1449 /*
1450 * Input present, check for input mapping and processing.
1451 */
1452 first = 1;
1453 while ((c = getc(qp)) >= 0) {
1454 /*
1455 * delayed suspend (^Y)
1456 */
1457 if (CCEQ(cc[VDSUSP], c) &&
1458 ISSET(lflag, IEXTEN|ISIG) == (IEXTEN|ISIG)) {
1459 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1460 if (first) {
1461 error = ttysleep(tp, &lbolt,
1462 TTIPRI | PCATCH, ttybg, 0);
1463 if (error)
1464 break;
1465 goto loop;
1466 }
1467 break;
1468 }
1469 /*
1470 * Interpret EOF only in canonical mode.
1471 */
1472 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1473 break;
1474 /*
1475 * Give user character.
1476 */
1477 error = ureadc(c, uio);
1478 if (error)
1479 break;
1480 if (uio->uio_resid == 0)
1481 break;
1482 /*
1483 * In canonical mode check for a "break character"
1484 * marking the end of a "line of input".
1485 */
1486 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1487 break;
1488 first = 0;
1489 }
1490 /*
1491 * Look to unblock output now that (presumably)
1492 * the input queue has gone down.
1493 */
1494 s = spltty();
1495 if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG/5) {
1496 if (ISSET(tp->t_iflag, IXOFF) &&
1497 cc[VSTART] != _POSIX_VDISABLE &&
1498 putc(cc[VSTART], &tp->t_outq) == 0) {
1499 CLR(tp->t_state, TS_TBLOCK);
1500 ttstart(tp);
1501 }
1502 /* Try to unblock remote output via hardware flow control. */
1503 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1504 (*tp->t_hwiflow)(tp, 0) != 0)
1505 CLR(tp->t_state, TS_TBLOCK);
1506 }
1507 splx(s);
1508 return (error);
1509 }
1510
1511 /*
1512 * Check the output queue on tp for space for a kernel message (from uprintf
1513 * or tprintf). Allow some space over the normal hiwater mark so we don't
1514 * lose messages due to normal flow control, but don't let the tty run amok.
1515 * Sleeps here are not interruptible, but we return prematurely if new signals
1516 * arrive.
1517 */
1518 int
1519 ttycheckoutq(tp, wait)
1520 register struct tty *tp;
1521 int wait;
1522 {
1523 int hiwat, s, oldsig;
1524
1525 hiwat = tp->t_hiwat;
1526 s = spltty();
1527 oldsig = wait ? curproc->p_siglist : 0;
1528 if (tp->t_outq.c_cc > hiwat + 200)
1529 while (tp->t_outq.c_cc > hiwat) {
1530 ttstart(tp);
1531 if (wait == 0 || curproc->p_siglist != oldsig) {
1532 splx(s);
1533 return (0);
1534 }
1535 timeout((void (*)__P((void *)))wakeup,
1536 (void *)&tp->t_outq, hz);
1537 SET(tp->t_state, TS_ASLEEP);
1538 tsleep(&tp->t_outq, PZERO - 1, "ttckoutq", 0);
1539 }
1540 splx(s);
1541 return (1);
1542 }
1543
1544 /*
1545 * Process a write call on a tty device.
1546 */
1547 int
1548 ttwrite(tp, uio, flag)
1549 register struct tty *tp;
1550 register struct uio *uio;
1551 int flag;
1552 {
1553 register u_char *cp = NULL;
1554 register int cc, ce;
1555 register struct proc *p;
1556 int i, hiwat, cnt, error, s;
1557 u_char obuf[OBUFSIZ];
1558
1559 hiwat = tp->t_hiwat;
1560 cnt = uio->uio_resid;
1561 error = 0;
1562 cc = 0;
1563 loop:
1564 s = spltty();
1565 if (!CONNECTED(tp)) {
1566 if (ISSET(tp->t_state, TS_ISOPEN)) {
1567 splx(s);
1568 return (EIO);
1569 } else if (flag & IO_NDELAY) {
1570 splx(s);
1571 error = EWOULDBLOCK;
1572 goto out;
1573 } else {
1574 /* Sleep awaiting carrier. */
1575 error = ttysleep(tp,
1576 &tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
1577 splx(s);
1578 if (error)
1579 goto out;
1580 goto loop;
1581 }
1582 }
1583 splx(s);
1584 /*
1585 * Hang the process if it's in the background.
1586 */
1587 p = curproc;
1588 if (isbackground(p, tp) &&
1589 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 &&
1590 (p->p_sigignore & sigmask(SIGTTOU)) == 0 &&
1591 (p->p_sigmask & sigmask(SIGTTOU)) == 0) {
1592 if (p->p_pgrp->pg_jobc == 0) {
1593 error = EIO;
1594 goto out;
1595 }
1596 pgsignal(p->p_pgrp, SIGTTOU, 1);
1597 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1598 if (error)
1599 goto out;
1600 goto loop;
1601 }
1602 /*
1603 * Process the user's data in at most OBUFSIZ chunks. Perform any
1604 * output translation. Keep track of high water mark, sleep on
1605 * overflow awaiting device aid in acquiring new space.
1606 */
1607 while (uio->uio_resid > 0 || cc > 0) {
1608 if (ISSET(tp->t_lflag, FLUSHO)) {
1609 uio->uio_resid = 0;
1610 return (0);
1611 }
1612 if (tp->t_outq.c_cc > hiwat)
1613 goto ovhiwat;
1614 /*
1615 * Grab a hunk of data from the user, unless we have some
1616 * leftover from last time.
1617 */
1618 if (cc == 0) {
1619 cc = min(uio->uio_resid, OBUFSIZ);
1620 cp = obuf;
1621 error = uiomove(cp, cc, uio);
1622 if (error) {
1623 cc = 0;
1624 break;
1625 }
1626 }
1627 /*
1628 * If nothing fancy need be done, grab those characters we
1629 * can handle without any of ttyoutput's processing and
1630 * just transfer them to the output q. For those chars
1631 * which require special processing (as indicated by the
1632 * bits in char_type), call ttyoutput. After processing
1633 * a hunk of data, look for FLUSHO so ^O's will take effect
1634 * immediately.
1635 */
1636 while (cc > 0) {
1637 if (!ISSET(tp->t_oflag, OPOST))
1638 ce = cc;
1639 else {
1640 ce = cc - scanc((u_int)cc, cp, char_type,
1641 CCLASSMASK);
1642 /*
1643 * If ce is zero, then we're processing
1644 * a special character through ttyoutput.
1645 */
1646 if (ce == 0) {
1647 tp->t_rocount = 0;
1648 if (ttyoutput(*cp, tp) >= 0) {
1649 #ifdef REAL_CLISTS
1650 /* No Clists, wait a bit. */
1651 ttstart(tp);
1652 if (error = ttysleep(tp, &lbolt,
1653 TTOPRI | PCATCH, ttybuf, 0))
1654 break;
1655 goto loop;
1656 #else
1657 /* out of space */
1658 goto overfull;
1659 #endif
1660 }
1661 cp++;
1662 cc--;
1663 if (ISSET(tp->t_lflag, FLUSHO) ||
1664 tp->t_outq.c_cc > hiwat)
1665 goto ovhiwat;
1666 continue;
1667 }
1668 }
1669 /*
1670 * A bunch of normal characters have been found.
1671 * Transfer them en masse to the output queue and
1672 * continue processing at the top of the loop.
1673 * If there are any further characters in this
1674 * <= OBUFSIZ chunk, the first should be a character
1675 * requiring special handling by ttyoutput.
1676 */
1677 tp->t_rocount = 0;
1678 i = b_to_q(cp, ce, &tp->t_outq);
1679 ce -= i;
1680 tp->t_column += ce;
1681 cp += ce, cc -= ce, tk_nout += ce;
1682 tp->t_outcc += ce;
1683 if (i > 0) {
1684 #ifdef REAL_CLISTS
1685 /* No Clists, wait a bit. */
1686 ttstart(tp);
1687 if (error = ttysleep(tp,
1688 &lbolt, TTOPRI | PCATCH, ttybuf, 0))
1689 break;
1690 goto loop;
1691 #else
1692 /* out of space */
1693 goto overfull;
1694 #endif
1695 }
1696 if (ISSET(tp->t_lflag, FLUSHO) ||
1697 tp->t_outq.c_cc > hiwat)
1698 break;
1699 }
1700 ttstart(tp);
1701 }
1702 out:
1703 /*
1704 * If cc is nonzero, we leave the uio structure inconsistent, as the
1705 * offset and iov pointers have moved forward, but it doesn't matter
1706 * (the call will either return short or restart with a new uio).
1707 */
1708 uio->uio_resid += cc;
1709 return (error);
1710
1711 #ifndef REAL_CLISTS
1712 overfull:
1713 /*
1714 * Since we are using ring buffers, if we can't insert any more into
1715 * the output queue, we can assume the ring is full and that someone
1716 * forgot to set the high water mark correctly. We set it and then
1717 * proceed as normal.
1718 */
1719 hiwat = tp->t_outq.c_cc - 1;
1720 #endif
1721
1722 ovhiwat:
1723 ttstart(tp);
1724 s = spltty();
1725 /*
1726 * This can only occur if FLUSHO is set in t_lflag,
1727 * or if ttstart/oproc is synchronous (or very fast).
1728 */
1729 if (tp->t_outq.c_cc <= hiwat) {
1730 splx(s);
1731 goto loop;
1732 }
1733 if (flag & IO_NDELAY) {
1734 splx(s);
1735 uio->uio_resid += cc;
1736 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
1737 }
1738 SET(tp->t_state, TS_ASLEEP);
1739 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1740 splx(s);
1741 if (error)
1742 goto out;
1743 goto loop;
1744 }
1745
1746 /*
1747 * Rubout one character from the rawq of tp
1748 * as cleanly as possible.
1749 */
1750 void
1751 ttyrub(c, tp)
1752 int c;
1753 register struct tty *tp;
1754 {
1755 register u_char *cp;
1756 register int savecol;
1757 int tabc, s;
1758
1759 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
1760 return;
1761 CLR(tp->t_lflag, FLUSHO);
1762 if (ISSET(tp->t_lflag, ECHOE)) {
1763 if (tp->t_rocount == 0) {
1764 /*
1765 * Screwed by ttwrite; retype
1766 */
1767 ttyretype(tp);
1768 return;
1769 }
1770 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
1771 ttyrubo(tp, 2);
1772 else {
1773 CLR(c, ~TTY_CHARMASK);
1774 switch (CCLASS(c)) {
1775 case ORDINARY:
1776 ttyrubo(tp, 1);
1777 break;
1778 case BACKSPACE:
1779 case CONTROL:
1780 case NEWLINE:
1781 case RETURN:
1782 case VTAB:
1783 if (ISSET(tp->t_lflag, ECHOCTL))
1784 ttyrubo(tp, 2);
1785 break;
1786 case TAB:
1787 if (tp->t_rocount < tp->t_rawq.c_cc) {
1788 ttyretype(tp);
1789 return;
1790 }
1791 s = spltty();
1792 savecol = tp->t_column;
1793 SET(tp->t_state, TS_CNTTB);
1794 SET(tp->t_lflag, FLUSHO);
1795 tp->t_column = tp->t_rocol;
1796 for (cp = firstc(&tp->t_rawq, &tabc); cp;
1797 cp = nextc(&tp->t_rawq, cp, &tabc))
1798 ttyecho(tabc, tp);
1799 CLR(tp->t_lflag, FLUSHO);
1800 CLR(tp->t_state, TS_CNTTB);
1801 splx(s);
1802
1803 /* savecol will now be length of the tab. */
1804 savecol -= tp->t_column;
1805 tp->t_column += savecol;
1806 if (savecol > 8)
1807 savecol = 8; /* overflow screw */
1808 while (--savecol >= 0)
1809 (void)ttyoutput('\b', tp);
1810 break;
1811 default: /* XXX */
1812 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
1813 (void)printf(PANICSTR, c, CCLASS(c));
1814 #ifdef notdef
1815 panic(PANICSTR, c, CCLASS(c));
1816 #endif
1817 }
1818 }
1819 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
1820 if (!ISSET(tp->t_state, TS_ERASE)) {
1821 SET(tp->t_state, TS_ERASE);
1822 (void)ttyoutput('\\', tp);
1823 }
1824 ttyecho(c, tp);
1825 } else
1826 ttyecho(tp->t_cc[VERASE], tp);
1827 --tp->t_rocount;
1828 }
1829
1830 /*
1831 * Back over cnt characters, erasing them.
1832 */
1833 static void
1834 ttyrubo(tp, cnt)
1835 register struct tty *tp;
1836 int cnt;
1837 {
1838
1839 while (cnt-- > 0) {
1840 (void)ttyoutput('\b', tp);
1841 (void)ttyoutput(' ', tp);
1842 (void)ttyoutput('\b', tp);
1843 }
1844 }
1845
1846 /*
1847 * ttyretype --
1848 * Reprint the rawq line. Note, it is assumed that c_cc has already
1849 * been checked.
1850 */
1851 void
1852 ttyretype(tp)
1853 register struct tty *tp;
1854 {
1855 register u_char *cp;
1856 int s, c;
1857
1858 /* Echo the reprint character. */
1859 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
1860 ttyecho(tp->t_cc[VREPRINT], tp);
1861
1862 (void)ttyoutput('\n', tp);
1863
1864 s = spltty();
1865 for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c))
1866 ttyecho(c, tp);
1867 for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c))
1868 ttyecho(c, tp);
1869 CLR(tp->t_state, TS_ERASE);
1870 splx(s);
1871
1872 tp->t_rocount = tp->t_rawq.c_cc;
1873 tp->t_rocol = 0;
1874 }
1875
1876 /*
1877 * Echo a typed character to the terminal.
1878 */
1879 static void
1880 ttyecho(c, tp)
1881 register int c;
1882 register struct tty *tp;
1883 {
1884
1885 if (!ISSET(tp->t_state, TS_CNTTB))
1886 CLR(tp->t_lflag, FLUSHO);
1887 if ((!ISSET(tp->t_lflag, ECHO) &&
1888 (!ISSET(tp->t_lflag, ECHONL) || c != '\n')) ||
1889 ISSET(tp->t_lflag, EXTPROC))
1890 return;
1891 if (((ISSET(tp->t_lflag, ECHOCTL) &&
1892 (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n')) ||
1893 ISSET(c, TTY_CHARMASK) == 0177)) {
1894 (void)ttyoutput('^', tp);
1895 CLR(c, ~TTY_CHARMASK);
1896 if (c == 0177)
1897 c = '?';
1898 else
1899 c += 'A' - 1;
1900 }
1901 (void)ttyoutput(c, tp);
1902 }
1903
1904 /*
1905 * Wake up any readers on a tty.
1906 */
1907 void
1908 ttwakeup(tp)
1909 register struct tty *tp;
1910 {
1911
1912 selwakeup(&tp->t_rsel);
1913 if (ISSET(tp->t_state, TS_ASYNC))
1914 pgsignal(tp->t_pgrp, SIGIO, 1);
1915 wakeup((caddr_t)&tp->t_rawq);
1916 }
1917
1918 /*
1919 * Look up a code for a specified speed in a conversion table;
1920 * used by drivers to map software speed values to hardware parameters.
1921 */
1922 int
1923 ttspeedtab(speed, table)
1924 int speed;
1925 register struct speedtab *table;
1926 {
1927
1928 for ( ; table->sp_speed != -1; table++)
1929 if (table->sp_speed == speed)
1930 return (table->sp_code);
1931 return (-1);
1932 }
1933
1934 /*
1935 * Set tty hi and low water marks.
1936 *
1937 * Try to arrange the dynamics so there's about one second
1938 * from hi to low water.
1939 */
1940 void
1941 ttsetwater(tp)
1942 struct tty *tp;
1943 {
1944 register int cps, x;
1945
1946 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
1947
1948 cps = tp->t_ospeed / 10;
1949 tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
1950 x += cps;
1951 x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT);
1952 tp->t_hiwat = roundup(x, CBSIZE);
1953 #undef CLAMP
1954 }
1955
1956 /*
1957 * Report on state of foreground process group.
1958 */
1959 void
1960 ttyinfo(tp)
1961 register struct tty *tp;
1962 {
1963 register struct proc *p, *pick;
1964 struct timeval utime, stime;
1965 int tmp;
1966
1967 if (ttycheckoutq(tp,0) == 0)
1968 return;
1969
1970 /* Print load average. */
1971 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
1972 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
1973
1974 if (tp->t_session == NULL)
1975 ttyprintf(tp, "not a controlling terminal\n");
1976 else if (tp->t_pgrp == NULL)
1977 ttyprintf(tp, "no foreground process group\n");
1978 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0)
1979 ttyprintf(tp, "empty foreground process group\n");
1980 else {
1981 /* Pick interesting process. */
1982 for (pick = NULL; p != 0; p = p->p_pglist.le_next)
1983 if (proc_compare(pick, p))
1984 pick = p;
1985
1986 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
1987 pick->p_stat == SRUN ? "running" :
1988 pick->p_wmesg ? pick->p_wmesg : "iowait");
1989
1990 calcru(pick, &utime, &stime, NULL);
1991
1992 /* Round up and print user time. */
1993 utime.tv_usec += 5000;
1994 if (utime.tv_usec >= 1000000) {
1995 utime.tv_sec += 1;
1996 utime.tv_usec -= 1000000;
1997 }
1998 ttyprintf(tp, "%ld.%02ldu ", utime.tv_sec,
1999 utime.tv_usec / 10000);
2000
2001 /* Round up and print system time. */
2002 stime.tv_usec += 5000;
2003 if (stime.tv_usec >= 1000000) {
2004 stime.tv_sec += 1;
2005 stime.tv_usec -= 1000000;
2006 }
2007 ttyprintf(tp, "%ld.%02lds ", stime.tv_sec,
2008 stime.tv_usec / 10000);
2009
2010 #define pgtok(a) (((u_long) ((a) * NBPG) / 1024))
2011 /* Print percentage cpu. */
2012 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
2013 ttyprintf(tp, "%d%% ", tmp / 100);
2014
2015 /* Print resident set size. */
2016 if (pick->p_stat == SIDL || pick->p_stat == SZOMB)
2017 tmp = 0;
2018 else {
2019 register struct vmspace *vm = pick->p_vmspace;
2020 tmp = pgtok(vm_resident_count(vm));
2021 }
2022 ttyprintf(tp, "%dk\n", tmp);
2023 }
2024 tp->t_rocount = 0; /* so pending input will be retyped if BS */
2025 }
2026
2027 /*
2028 * Returns 1 if p2 is "better" than p1
2029 *
2030 * The algorithm for picking the "interesting" process is thus:
2031 *
2032 * 1) Only foreground processes are eligible - implied.
2033 * 2) Runnable processes are favored over anything else. The runner
2034 * with the highest cpu utilization is picked (p_estcpu). Ties are
2035 * broken by picking the highest pid.
2036 * 3) The sleeper with the shortest sleep time is next. With ties,
2037 * we pick out just "short-term" sleepers (P_SINTR == 0).
2038 * 4) Further ties are broken by picking the highest pid.
2039 */
2040 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2041 #define TESTAB(a, b) ((a)<<1 | (b))
2042 #define ONLYA 2
2043 #define ONLYB 1
2044 #define BOTH 3
2045
2046 static int
2047 proc_compare(p1, p2)
2048 register struct proc *p1, *p2;
2049 {
2050
2051 if (p1 == NULL)
2052 return (1);
2053 /*
2054 * see if at least one of them is runnable
2055 */
2056 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
2057 case ONLYA:
2058 return (0);
2059 case ONLYB:
2060 return (1);
2061 case BOTH:
2062 /*
2063 * tie - favor one with highest recent cpu utilization
2064 */
2065 if (p2->p_estcpu > p1->p_estcpu)
2066 return (1);
2067 if (p1->p_estcpu > p2->p_estcpu)
2068 return (0);
2069 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2070 }
2071 /*
2072 * weed out zombies
2073 */
2074 switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
2075 case ONLYA:
2076 return (1);
2077 case ONLYB:
2078 return (0);
2079 case BOTH:
2080 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2081 }
2082 /*
2083 * pick the one with the smallest sleep time
2084 */
2085 if (p2->p_slptime > p1->p_slptime)
2086 return (0);
2087 if (p1->p_slptime > p2->p_slptime)
2088 return (1);
2089 /*
2090 * favor one sleeping in a non-interruptible sleep
2091 */
2092 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
2093 return (1);
2094 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
2095 return (0);
2096 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2097 }
2098
2099 /*
2100 * Output char to tty; console putchar style.
2101 */
2102 int
2103 tputchar(c, tp)
2104 int c;
2105 struct tty *tp;
2106 {
2107 register int s;
2108
2109 s = spltty();
2110 if (ISSET(tp->t_state,
2111 TS_CARR_ON | TS_ISOPEN) != (TS_CARR_ON | TS_ISOPEN)) {
2112 splx(s);
2113 return (-1);
2114 }
2115 if (c == '\n')
2116 (void)ttyoutput('\r', tp);
2117 (void)ttyoutput(c, tp);
2118 ttstart(tp);
2119 splx(s);
2120 return (0);
2121 }
2122
2123 /*
2124 * Sleep on chan, returning ERESTART if tty changed while we napped and
2125 * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If
2126 * the tty is revoked, restarting a pending call will redo validation done
2127 * at the start of the call.
2128 */
2129 int
2130 ttysleep(tp, chan, pri, wmesg, timo)
2131 struct tty *tp;
2132 void *chan;
2133 int pri, timo;
2134 const char *wmesg;
2135 {
2136 int error;
2137 short gen;
2138
2139 gen = tp->t_gen;
2140 if ((error = tsleep(chan, pri, wmesg, timo)) != 0)
2141 return (error);
2142 return (tp->t_gen == gen ? 0 : ERESTART);
2143 }
2144
2145 /*
2146 * Initialise the global tty list.
2147 */
2148 void
2149 tty_init()
2150 {
2151
2152 TAILQ_INIT(&ttylist);
2153 tty_count = 0;
2154
2155 pool_init(&tty_pool, sizeof(struct tty), 0, 0, 0, "ttypl",
2156 0, pool_page_alloc_nointr, pool_page_free_nointr, M_TTYS);
2157 }
2158
2159 /*
2160 * Attach a tty to the tty list.
2161 *
2162 * This should be called ONLY once per real tty (including pty's).
2163 * eg, on the sparc, the keyboard and mouse have struct tty's that are
2164 * distinctly NOT usable as tty's, and thus should not be attached to
2165 * the ttylist. This is why this call is not done from ttymalloc().
2166 *
2167 * Device drivers should attach tty's at a similar time that they are
2168 * ttymalloc()'ed, or, for the case of statically allocated struct tty's
2169 * either in the attach or (first) open routine.
2170 */
2171 void
2172 tty_attach(tp)
2173 struct tty *tp;
2174 {
2175
2176 TAILQ_INSERT_TAIL(&ttylist, tp, tty_link);
2177 ++tty_count;
2178 }
2179
2180 /*
2181 * Remove a tty from the tty list.
2182 */
2183 void
2184 tty_detach(tp)
2185 struct tty *tp;
2186 {
2187
2188 --tty_count;
2189 #ifdef DIAGNOSTIC
2190 if (tty_count < 0)
2191 panic("tty_detach: tty_count < 0");
2192 #endif
2193 TAILQ_REMOVE(&ttylist, tp, tty_link);
2194 }
2195
2196 /*
2197 * Allocate a tty structure and its associated buffers.
2198 */
2199 struct tty *
2200 ttymalloc()
2201 {
2202 struct tty *tp;
2203
2204 tp = pool_get(&tty_pool, PR_WAITOK);
2205 memset(tp, 0, sizeof(*tp));
2206 /* XXX: default to 1024 chars for now */
2207 clalloc(&tp->t_rawq, 1024, 1);
2208 clalloc(&tp->t_canq, 1024, 1);
2209 /* output queue doesn't need quoting */
2210 clalloc(&tp->t_outq, 1024, 0);
2211 return(tp);
2212 }
2213
2214 /*
2215 * Free a tty structure and its buffers.
2216 *
2217 * Be sure to call tty_detach() for any tty that has been
2218 * tty_attach()ed.
2219 */
2220 void
2221 ttyfree(tp)
2222 struct tty *tp;
2223 {
2224
2225 clfree(&tp->t_rawq);
2226 clfree(&tp->t_canq);
2227 clfree(&tp->t_outq);
2228 pool_put(&tty_pool, tp);
2229 }
2230