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