tty.c revision 1.121 1 /* $NetBSD: tty.c,v 1.121 2000/11/01 23:51:38 eeh 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 !sigismember(&p->p_sigignore, SIGTTOU) &&
774 !sigismember(&p->p_sigmask, SIGTTOU)) {
775 pgsignal(p->p_pgrp, SIGTTOU, 1);
776 error = ttysleep(tp, &lbolt, TTOPRI | PCATCH, ttybg, 0);
777 if (error)
778 return (error);
779 }
780 break;
781 }
782
783 switch (cmd) { /* Process the ioctl. */
784 case FIOASYNC: /* set/clear async i/o */
785 s = spltty();
786 if (*(int *)data)
787 SET(tp->t_state, TS_ASYNC);
788 else
789 CLR(tp->t_state, TS_ASYNC);
790 splx(s);
791 break;
792 case FIONBIO: /* set/clear non-blocking i/o */
793 break; /* XXX: delete. */
794 case FIONREAD: /* get # bytes to read */
795 *(int *)data = ttnread(tp);
796 break;
797 case TIOCEXCL: /* set exclusive use of tty */
798 s = spltty();
799 SET(tp->t_state, TS_XCLUDE);
800 splx(s);
801 break;
802 case TIOCFLUSH: { /* flush buffers */
803 int flags = *(int *)data;
804
805 if (flags == 0)
806 flags = FREAD | FWRITE;
807 else
808 flags &= FREAD | FWRITE;
809 ttyflush(tp, flags);
810 break;
811 }
812 case TIOCCONS: /* become virtual console */
813 if (*(int *)data) {
814 if (constty && constty != tp &&
815 ISSET(constty->t_state, TS_CARR_ON | TS_ISOPEN) ==
816 (TS_CARR_ON | TS_ISOPEN))
817 return (EBUSY);
818 #ifndef UCONSOLE
819 if ((error = suser(p->p_ucred, &p->p_acflag)) != 0)
820 return (error);
821 #endif
822 constty = tp;
823 } else if (tp == constty)
824 constty = NULL;
825 break;
826 case TIOCDRAIN: /* wait till output drained */
827 if ((error = ttywait(tp)) != 0)
828 return (error);
829 break;
830 case TIOCGETA: { /* get termios struct */
831 struct termios *t = (struct termios *)data;
832
833 memcpy(t, &tp->t_termios, sizeof(struct termios));
834 break;
835 }
836 case TIOCGETD: /* get line discipline */
837 *(int *)data = (tp->t_linesw) ? tp->t_linesw->l_no : 0;
838 break;
839 case TIOCGLINED:
840 if (!tp->t_linesw)
841 return (EIO);
842 strncpy((char *)data, tp->t_linesw->l_name,
843 TTLINEDNAMELEN);
844 break;
845 case TIOCGWINSZ: /* get window size */
846 *(struct winsize *)data = tp->t_winsize;
847 break;
848 case TIOCGPGRP: /* get pgrp of tty */
849 if (!isctty(p, tp))
850 return (ENOTTY);
851 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
852 break;
853 case TIOCGSID: /* get sid of tty */
854 if (!isctty(p, tp))
855 return (ENOTTY);
856 *(int *)data = tp->t_session->s_sid;
857 break;
858 #ifdef TIOCHPCL
859 case TIOCHPCL: /* hang up on last close */
860 s = spltty();
861 SET(tp->t_cflag, HUPCL);
862 splx(s);
863 break;
864 #endif
865 case TIOCNXCL: /* reset exclusive use of tty */
866 s = spltty();
867 CLR(tp->t_state, TS_XCLUDE);
868 splx(s);
869 break;
870 case TIOCOUTQ: /* output queue size */
871 *(int *)data = tp->t_outq.c_cc;
872 break;
873 case TIOCSETA: /* set termios struct */
874 case TIOCSETAW: /* drain output, set */
875 case TIOCSETAF: { /* drn out, fls in, set */
876 struct termios *t = (struct termios *)data;
877
878 s = spltty();
879 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
880 if ((error = ttywait(tp)) != 0) {
881 splx(s);
882 return (error);
883 }
884 if (cmd == TIOCSETAF)
885 ttyflush(tp, FREAD);
886 }
887 if (!ISSET(t->c_cflag, CIGNORE)) {
888 /*
889 * Set device hardware.
890 */
891 if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
892 splx(s);
893 return (error);
894 } else {
895 tp->t_cflag = t->c_cflag;
896 tp->t_ispeed = t->c_ispeed;
897 tp->t_ospeed = t->c_ospeed;
898 if (t->c_ospeed == 0 && tp->t_session &&
899 tp->t_session->s_leader)
900 psignal(tp->t_session->s_leader,
901 SIGHUP);
902 }
903 ttsetwater(tp);
904 }
905 if (cmd != TIOCSETAF) {
906 if (ISSET(t->c_lflag, ICANON) !=
907 ISSET(tp->t_lflag, ICANON)) {
908 if (ISSET(t->c_lflag, ICANON)) {
909 SET(tp->t_lflag, PENDIN);
910 ttwakeup(tp);
911 } else {
912 struct clist tq;
913
914 catq(&tp->t_rawq, &tp->t_canq);
915 tq = tp->t_rawq;
916 tp->t_rawq = tp->t_canq;
917 tp->t_canq = tq;
918 CLR(tp->t_lflag, PENDIN);
919 }
920 }
921 }
922 tp->t_iflag = t->c_iflag;
923 tp->t_oflag = t->c_oflag;
924 /*
925 * Make the EXTPROC bit read only.
926 */
927 if (ISSET(tp->t_lflag, EXTPROC))
928 SET(t->c_lflag, EXTPROC);
929 else
930 CLR(t->c_lflag, EXTPROC);
931 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
932 memcpy(tp->t_cc, t->c_cc, sizeof(t->c_cc));
933 splx(s);
934 break;
935 }
936 case TIOCSETD: { /* set line discipline */
937 int t = *(int *)data;
938 struct linesw *lp;
939 dev_t device = tp->t_dev;
940
941 if ((u_int)t >= nlinesw)
942 return (ENXIO);
943 lp = linesw[t];
944 if (lp != tp->t_linesw) {
945 s = spltty();
946 if (tp->t_linesw)
947 (*tp->t_linesw->l_close)(tp, flag);
948 error = (*lp->l_open)(device, tp);
949 if (error) {
950 (void)(*tp->t_linesw->l_open)(device, tp);
951 splx(s);
952 return (error);
953 }
954 tp->t_linesw = lp;
955 splx(s);
956 }
957 break;
958 }
959 case TIOCSLINED: { /* set line discipline */
960 char *name = (char *)data;
961 struct linesw *lp;
962 dev_t device = tp->t_dev;
963
964 /* Null terminate to prevent buffer overflow */
965 data[TTLINEDNAMELEN] = 0;
966 lp = ttyldisc_lookup(name);
967
968 if (!lp)
969 return (ENXIO);
970
971 if (lp != tp->t_linesw) {
972 s = spltty();
973 if (tp->t_linesw)
974 (*tp->t_linesw->l_close)(tp, flag);
975 error = (*lp->l_open)(device, tp);
976 if (error) {
977 (void)(*tp->t_linesw->l_open)(device, tp);
978 splx(s);
979 return (error);
980 }
981 tp->t_linesw = lp;
982 splx(s);
983 }
984 break;
985 }
986 case TIOCSTART: /* start output, like ^Q */
987 s = spltty();
988 if (ISSET(tp->t_state, TS_TTSTOP) ||
989 ISSET(tp->t_lflag, FLUSHO)) {
990 CLR(tp->t_lflag, FLUSHO);
991 CLR(tp->t_state, TS_TTSTOP);
992 ttstart(tp);
993 }
994 splx(s);
995 break;
996 case TIOCSTI: /* simulate terminal input */
997 if (p->p_ucred->cr_uid && (flag & FREAD) == 0)
998 return (EPERM);
999 if (p->p_ucred->cr_uid && !isctty(p, tp))
1000 return (EACCES);
1001 (*tp->t_linesw->l_rint)(*(u_char *)data, tp);
1002 break;
1003 case TIOCSTOP: /* stop output, like ^S */
1004 s = spltty();
1005 if (!ISSET(tp->t_state, TS_TTSTOP)) {
1006 SET(tp->t_state, TS_TTSTOP);
1007 (*cdevsw[major(tp->t_dev)].d_stop)(tp, 0);
1008 }
1009 splx(s);
1010 break;
1011 case TIOCSCTTY: /* become controlling tty */
1012 /* Session ctty vnode pointer set in vnode layer. */
1013 if (!SESS_LEADER(p) ||
1014 ((p->p_session->s_ttyvp || tp->t_session) &&
1015 (tp->t_session != p->p_session)))
1016 return (EPERM);
1017 tp->t_session = p->p_session;
1018 tp->t_pgrp = p->p_pgrp;
1019 p->p_session->s_ttyp = tp;
1020 p->p_flag |= P_CONTROLT;
1021 break;
1022 case TIOCSPGRP: { /* set pgrp of tty */
1023 struct pgrp *pgrp = pgfind(*(int *)data);
1024
1025 if (!isctty(p, tp))
1026 return (ENOTTY);
1027 else if (pgrp == NULL)
1028 return (EINVAL);
1029 else if (pgrp->pg_session != p->p_session)
1030 return (EPERM);
1031 tp->t_pgrp = pgrp;
1032 break;
1033 }
1034 case TIOCSTAT: /* get load avg stats */
1035 ttyinfo(tp);
1036 break;
1037 case TIOCSWINSZ: /* set window size */
1038 if (memcmp((caddr_t)&tp->t_winsize, data,
1039 sizeof(struct winsize))) {
1040 tp->t_winsize = *(struct winsize *)data;
1041 pgsignal(tp->t_pgrp, SIGWINCH, 1);
1042 }
1043 break;
1044 default:
1045 #ifdef COMPAT_OLDTTY
1046 return (ttcompat(tp, cmd, data, flag, p));
1047 #else
1048 return (-1);
1049 #endif
1050 }
1051 return (0);
1052 }
1053
1054 int
1055 ttpoll(dev, events, p)
1056 dev_t dev;
1057 int events;
1058 struct proc *p;
1059 {
1060 struct tty *tp = (*cdevsw[major(dev)].d_tty)(dev);
1061 int revents = 0;
1062 int s = spltty();
1063
1064 if (events & (POLLIN | POLLRDNORM))
1065 if (ttnread(tp) > 0)
1066 revents |= events & (POLLIN | POLLRDNORM);
1067
1068 if (events & (POLLOUT | POLLWRNORM))
1069 if (tp->t_outq.c_cc <= tp->t_lowat)
1070 revents |= events & (POLLOUT | POLLWRNORM);
1071
1072 if (events & POLLHUP)
1073 if (!CONNECTED(tp))
1074 revents |= POLLHUP;
1075
1076 if (revents == 0) {
1077 if (events & (POLLIN | POLLHUP | POLLRDNORM))
1078 selrecord(p, &tp->t_rsel);
1079
1080 if (events & (POLLOUT | POLLWRNORM))
1081 selrecord(p, &tp->t_wsel);
1082 }
1083
1084 splx(s);
1085 return (revents);
1086 }
1087
1088 static int
1089 ttnread(tp)
1090 struct tty *tp;
1091 {
1092 int nread;
1093
1094 if (ISSET(tp->t_lflag, PENDIN))
1095 ttypend(tp);
1096 nread = tp->t_canq.c_cc;
1097 if (!ISSET(tp->t_lflag, ICANON)) {
1098 nread += tp->t_rawq.c_cc;
1099 if (nread < tp->t_cc[VMIN] && !tp->t_cc[VTIME])
1100 nread = 0;
1101 }
1102 return (nread);
1103 }
1104
1105 /*
1106 * Wait for output to drain.
1107 */
1108 int
1109 ttywait(tp)
1110 struct tty *tp;
1111 {
1112 int error, s;
1113
1114 error = 0;
1115 s = spltty();
1116 while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1117 CONNECTED(tp) && tp->t_oproc) {
1118 (*tp->t_oproc)(tp);
1119 SET(tp->t_state, TS_ASLEEP);
1120 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1121 if (error)
1122 break;
1123 }
1124 splx(s);
1125 return (error);
1126 }
1127
1128 /*
1129 * Flush if successfully wait.
1130 */
1131 int
1132 ttywflush(tp)
1133 struct tty *tp;
1134 {
1135 int error;
1136
1137 if ((error = ttywait(tp)) == 0)
1138 ttyflush(tp, FREAD);
1139 return (error);
1140 }
1141
1142 /*
1143 * Flush tty read and/or write queues, notifying anyone waiting.
1144 */
1145 void
1146 ttyflush(tp, rw)
1147 struct tty *tp;
1148 int rw;
1149 {
1150 int s;
1151
1152 s = spltty();
1153 if (rw & FREAD) {
1154 FLUSHQ(&tp->t_canq);
1155 FLUSHQ(&tp->t_rawq);
1156 tp->t_rocount = 0;
1157 tp->t_rocol = 0;
1158 CLR(tp->t_state, TS_LOCAL);
1159 ttwakeup(tp);
1160 }
1161 if (rw & FWRITE) {
1162 CLR(tp->t_state, TS_TTSTOP);
1163 (*cdevsw[major(tp->t_dev)].d_stop)(tp, rw);
1164 FLUSHQ(&tp->t_outq);
1165 wakeup((caddr_t)&tp->t_outq);
1166 selwakeup(&tp->t_wsel);
1167 }
1168 splx(s);
1169 }
1170
1171 /*
1172 * Copy in the default termios characters.
1173 */
1174 void
1175 ttychars(tp)
1176 struct tty *tp;
1177 {
1178
1179 memcpy(tp->t_cc, ttydefchars, sizeof(ttydefchars));
1180 }
1181
1182 /*
1183 * Send stop character on input overflow.
1184 */
1185 static void
1186 ttyblock(tp)
1187 struct tty *tp;
1188 {
1189 int total;
1190
1191 total = tp->t_rawq.c_cc + tp->t_canq.c_cc;
1192 if (tp->t_rawq.c_cc > TTYHOG) {
1193 ttyflush(tp, FREAD | FWRITE);
1194 CLR(tp->t_state, TS_TBLOCK);
1195 }
1196 /*
1197 * Block further input iff: current input > threshold
1198 * AND input is available to user program.
1199 */
1200 if (total >= TTYHOG / 2 &&
1201 !ISSET(tp->t_state, TS_TBLOCK) &&
1202 (!ISSET(tp->t_lflag, ICANON) || tp->t_canq.c_cc > 0)) {
1203 if (ISSET(tp->t_iflag, IXOFF) &&
1204 tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1205 putc(tp->t_cc[VSTOP], &tp->t_outq) == 0) {
1206 SET(tp->t_state, TS_TBLOCK);
1207 ttstart(tp);
1208 }
1209 /* Try to block remote output via hardware flow control. */
1210 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1211 (*tp->t_hwiflow)(tp, 1) != 0)
1212 SET(tp->t_state, TS_TBLOCK);
1213 }
1214 }
1215
1216 void
1217 ttrstrt(tp_arg)
1218 void *tp_arg;
1219 {
1220 struct tty *tp;
1221 int s;
1222
1223 #ifdef DIAGNOSTIC
1224 if (tp_arg == NULL)
1225 panic("ttrstrt");
1226 #endif
1227 tp = tp_arg;
1228 s = spltty();
1229
1230 CLR(tp->t_state, TS_TIMEOUT);
1231 ttstart(tp);
1232
1233 splx(s);
1234 }
1235
1236 int
1237 ttstart(tp)
1238 struct tty *tp;
1239 {
1240
1241 if (tp->t_oproc != NULL) /* XXX: Kludge for pty. */
1242 (*tp->t_oproc)(tp);
1243 return (0);
1244 }
1245
1246 /*
1247 * "close" a line discipline
1248 */
1249 int
1250 ttylclose(tp, flag)
1251 struct tty *tp;
1252 int flag;
1253 {
1254
1255 if (flag & FNONBLOCK)
1256 ttyflush(tp, FREAD | FWRITE);
1257 else
1258 ttywflush(tp);
1259 return (0);
1260 }
1261
1262 /*
1263 * Handle modem control transition on a tty.
1264 * Flag indicates new state of carrier.
1265 * Returns 0 if the line should be turned off, otherwise 1.
1266 */
1267 int
1268 ttymodem(tp, flag)
1269 struct tty *tp;
1270 int flag;
1271 {
1272
1273 if (flag == 0) {
1274 if (ISSET(tp->t_state, TS_CARR_ON)) {
1275 /*
1276 * Lost carrier.
1277 */
1278 CLR(tp->t_state, TS_CARR_ON);
1279 if (ISSET(tp->t_state, TS_ISOPEN) && !CONNECTED(tp)) {
1280 if (tp->t_session && tp->t_session->s_leader)
1281 psignal(tp->t_session->s_leader, SIGHUP);
1282 ttyflush(tp, FREAD | FWRITE);
1283 return (0);
1284 }
1285 }
1286 } else {
1287 if (!ISSET(tp->t_state, TS_CARR_ON)) {
1288 /*
1289 * Carrier now on.
1290 */
1291 SET(tp->t_state, TS_CARR_ON);
1292 ttwakeup(tp);
1293 }
1294 }
1295 return (1);
1296 }
1297
1298 /*
1299 * Default modem control routine (for other line disciplines).
1300 * Return argument flag, to turn off device on carrier drop.
1301 */
1302 int
1303 nullmodem(tp, flag)
1304 struct tty *tp;
1305 int flag;
1306 {
1307
1308 if (flag)
1309 SET(tp->t_state, TS_CARR_ON);
1310 else {
1311 CLR(tp->t_state, TS_CARR_ON);
1312 if (!CONNECTED(tp)) {
1313 if (tp->t_session && tp->t_session->s_leader)
1314 psignal(tp->t_session->s_leader, SIGHUP);
1315 return (0);
1316 }
1317 }
1318 return (1);
1319 }
1320
1321 /*
1322 * Reinput pending characters after state switch
1323 * call at spltty().
1324 */
1325 void
1326 ttypend(tp)
1327 struct tty *tp;
1328 {
1329 struct clist tq;
1330 int c;
1331
1332 CLR(tp->t_lflag, PENDIN);
1333 SET(tp->t_state, TS_TYPEN);
1334 tq = tp->t_rawq;
1335 tp->t_rawq.c_cc = 0;
1336 tp->t_rawq.c_cf = tp->t_rawq.c_cl = 0;
1337 while ((c = getc(&tq)) >= 0)
1338 ttyinput(c, tp);
1339 CLR(tp->t_state, TS_TYPEN);
1340 }
1341
1342 /*
1343 * Process a read call on a tty device.
1344 */
1345 int
1346 ttread(tp, uio, flag)
1347 struct tty *tp;
1348 struct uio *uio;
1349 int flag;
1350 {
1351 struct clist *qp;
1352 int c;
1353 long lflag;
1354 u_char *cc = tp->t_cc;
1355 struct proc *p = curproc;
1356 int s, first, error = 0;
1357 struct timeval stime;
1358 int has_stime = 0, last_cc = 0;
1359 long slp = 0;
1360
1361 loop: lflag = tp->t_lflag;
1362 s = spltty();
1363 /*
1364 * take pending input first
1365 */
1366 if (ISSET(lflag, PENDIN))
1367 ttypend(tp);
1368 splx(s);
1369
1370 /*
1371 * Hang process if it's in the background.
1372 */
1373 if (isbackground(p, tp)) {
1374 if (sigismember(&p->p_sigignore, SIGTTIN) ||
1375 sigismember(&p->p_sigmask, SIGTTIN) ||
1376 p->p_flag & P_PPWAIT || p->p_pgrp->pg_jobc == 0)
1377 return (EIO);
1378 pgsignal(p->p_pgrp, SIGTTIN, 1);
1379 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1380 if (error)
1381 return (error);
1382 goto loop;
1383 }
1384
1385 s = spltty();
1386 if (!ISSET(lflag, ICANON)) {
1387 int m = cc[VMIN];
1388 long t = cc[VTIME];
1389
1390 qp = &tp->t_rawq;
1391 /*
1392 * Check each of the four combinations.
1393 * (m > 0 && t == 0) is the normal read case.
1394 * It should be fairly efficient, so we check that and its
1395 * companion case (m == 0 && t == 0) first.
1396 * For the other two cases, we compute the target sleep time
1397 * into slp.
1398 */
1399 if (t == 0) {
1400 if (qp->c_cc < m)
1401 goto sleep;
1402 goto read;
1403 }
1404 t *= 100000; /* time in us */
1405 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1406 ((t1).tv_usec - (t2).tv_usec))
1407 if (m > 0) {
1408 if (qp->c_cc <= 0)
1409 goto sleep;
1410 if (qp->c_cc >= m)
1411 goto read;
1412 if (!has_stime) {
1413 /* first character, start timer */
1414 has_stime = 1;
1415 stime = time;
1416 slp = t;
1417 } else if (qp->c_cc > last_cc) {
1418 /* got a character, restart timer */
1419 stime = time;
1420 slp = t;
1421 } else {
1422 /* nothing, check expiration */
1423 slp = t - diff(time, stime);
1424 }
1425 } else { /* m == 0 */
1426 if (qp->c_cc > 0)
1427 goto read;
1428 if (!has_stime) {
1429 has_stime = 1;
1430 stime = time;
1431 slp = t;
1432 } else
1433 slp = t - diff(time, stime);
1434 }
1435 last_cc = qp->c_cc;
1436 #undef diff
1437 if (slp > 0) {
1438 /*
1439 * Rounding down may make us wake up just short
1440 * of the target, so we round up.
1441 * The formula is ceiling(slp * hz/1000000).
1442 * 32-bit arithmetic is enough for hz < 169.
1443 *
1444 * Also, use plain wakeup() not ttwakeup().
1445 */
1446 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1447 goto sleep;
1448 }
1449 } else if ((qp = &tp->t_canq)->c_cc <= 0) {
1450 int carrier;
1451
1452 sleep:
1453 /*
1454 * If there is no input, sleep on rawq
1455 * awaiting hardware receipt and notification.
1456 * If we have data, we don't need to check for carrier.
1457 */
1458 carrier = CONNECTED(tp);
1459 if (!carrier && ISSET(tp->t_state, TS_ISOPEN)) {
1460 splx(s);
1461 return (0); /* EOF */
1462 }
1463 if (flag & IO_NDELAY) {
1464 splx(s);
1465 return (EWOULDBLOCK);
1466 }
1467 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
1468 carrier ? ttyin : ttopen, slp);
1469 splx(s);
1470 /* VMIN == 0: any quantity read satisfies */
1471 if (cc[VMIN] == 0 && error == EWOULDBLOCK)
1472 return (0);
1473 if (error && error != EWOULDBLOCK)
1474 return (error);
1475 goto loop;
1476 }
1477 read:
1478 splx(s);
1479
1480 /*
1481 * Input present, check for input mapping and processing.
1482 */
1483 first = 1;
1484 while ((c = getc(qp)) >= 0) {
1485 /*
1486 * delayed suspend (^Y)
1487 */
1488 if (CCEQ(cc[VDSUSP], c) &&
1489 ISSET(lflag, IEXTEN|ISIG) == (IEXTEN|ISIG)) {
1490 pgsignal(tp->t_pgrp, SIGTSTP, 1);
1491 if (first) {
1492 error = ttysleep(tp, &lbolt,
1493 TTIPRI | PCATCH, ttybg, 0);
1494 if (error)
1495 break;
1496 goto loop;
1497 }
1498 break;
1499 }
1500 /*
1501 * Interpret EOF only in canonical mode.
1502 */
1503 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1504 break;
1505 /*
1506 * Give user character.
1507 */
1508 error = ureadc(c, uio);
1509 if (error)
1510 break;
1511 if (uio->uio_resid == 0)
1512 break;
1513 /*
1514 * In canonical mode check for a "break character"
1515 * marking the end of a "line of input".
1516 */
1517 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1518 break;
1519 first = 0;
1520 }
1521 /*
1522 * Look to unblock output now that (presumably)
1523 * the input queue has gone down.
1524 */
1525 s = spltty();
1526 if (ISSET(tp->t_state, TS_TBLOCK) && tp->t_rawq.c_cc < TTYHOG/5) {
1527 if (ISSET(tp->t_iflag, IXOFF) &&
1528 cc[VSTART] != _POSIX_VDISABLE &&
1529 putc(cc[VSTART], &tp->t_outq) == 0) {
1530 CLR(tp->t_state, TS_TBLOCK);
1531 ttstart(tp);
1532 }
1533 /* Try to unblock remote output via hardware flow control. */
1534 if (ISSET(tp->t_cflag, CHWFLOW) && tp->t_hwiflow &&
1535 (*tp->t_hwiflow)(tp, 0) != 0)
1536 CLR(tp->t_state, TS_TBLOCK);
1537 }
1538 splx(s);
1539 return (error);
1540 }
1541
1542 /*
1543 * Check the output queue on tp for space for a kernel message (from uprintf
1544 * or tprintf). Allow some space over the normal hiwater mark so we don't
1545 * lose messages due to normal flow control, but don't let the tty run amok.
1546 * Sleeps here are not interruptible, but we return prematurely if new signals
1547 * arrive.
1548 */
1549 int
1550 ttycheckoutq(tp, wait)
1551 struct tty *tp;
1552 int wait;
1553 {
1554 int hiwat, s;
1555 int error;
1556
1557 hiwat = tp->t_hiwat;
1558 s = spltty();
1559 if (tp->t_outq.c_cc > hiwat + 200)
1560 while (tp->t_outq.c_cc > hiwat) {
1561 ttstart(tp);
1562 if (wait == 0) {
1563 splx(s);
1564 return (0);
1565 }
1566 callout_reset(&tp->t_outq_ch, hz,
1567 (void (*)__P((void *)))wakeup, &tp->t_outq);
1568 SET(tp->t_state, TS_ASLEEP);
1569 error = tsleep(&tp->t_outq, (PZERO - 1) | PCATCH,
1570 "ttckoutq", 0);
1571 if (error == EINTR)
1572 wait = 0;
1573 }
1574 splx(s);
1575 return (1);
1576 }
1577
1578 /*
1579 * Process a write call on a tty device.
1580 */
1581 int
1582 ttwrite(tp, uio, flag)
1583 struct tty *tp;
1584 struct uio *uio;
1585 int flag;
1586 {
1587 u_char *cp = NULL;
1588 int cc, ce;
1589 struct proc *p;
1590 int i, hiwat, cnt, error, s;
1591 u_char obuf[OBUFSIZ];
1592
1593 hiwat = tp->t_hiwat;
1594 cnt = uio->uio_resid;
1595 error = 0;
1596 cc = 0;
1597 loop:
1598 s = spltty();
1599 if (!CONNECTED(tp)) {
1600 if (ISSET(tp->t_state, TS_ISOPEN)) {
1601 splx(s);
1602 return (EIO);
1603 } else if (flag & IO_NDELAY) {
1604 splx(s);
1605 error = EWOULDBLOCK;
1606 goto out;
1607 } else {
1608 /* Sleep awaiting carrier. */
1609 error = ttysleep(tp,
1610 &tp->t_rawq, TTIPRI | PCATCH, ttopen, 0);
1611 splx(s);
1612 if (error)
1613 goto out;
1614 goto loop;
1615 }
1616 }
1617 splx(s);
1618 /*
1619 * Hang the process if it's in the background.
1620 */
1621 p = curproc;
1622 if (isbackground(p, tp) &&
1623 ISSET(tp->t_lflag, TOSTOP) && (p->p_flag & P_PPWAIT) == 0 &&
1624 !sigismember(&p->p_sigignore, SIGTTOU) &&
1625 !sigismember(&p->p_sigmask, SIGTTOU)) {
1626 if (p->p_pgrp->pg_jobc == 0) {
1627 error = EIO;
1628 goto out;
1629 }
1630 pgsignal(p->p_pgrp, SIGTTOU, 1);
1631 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, ttybg, 0);
1632 if (error)
1633 goto out;
1634 goto loop;
1635 }
1636 /*
1637 * Process the user's data in at most OBUFSIZ chunks. Perform any
1638 * output translation. Keep track of high water mark, sleep on
1639 * overflow awaiting device aid in acquiring new space.
1640 */
1641 while (uio->uio_resid > 0 || cc > 0) {
1642 if (ISSET(tp->t_lflag, FLUSHO)) {
1643 uio->uio_resid = 0;
1644 return (0);
1645 }
1646 if (tp->t_outq.c_cc > hiwat)
1647 goto ovhiwat;
1648 /*
1649 * Grab a hunk of data from the user, unless we have some
1650 * leftover from last time.
1651 */
1652 if (cc == 0) {
1653 cc = min(uio->uio_resid, OBUFSIZ);
1654 cp = obuf;
1655 error = uiomove(cp, cc, uio);
1656 if (error) {
1657 cc = 0;
1658 break;
1659 }
1660 }
1661 /*
1662 * If nothing fancy need be done, grab those characters we
1663 * can handle without any of ttyoutput's processing and
1664 * just transfer them to the output q. For those chars
1665 * which require special processing (as indicated by the
1666 * bits in char_type), call ttyoutput. After processing
1667 * a hunk of data, look for FLUSHO so ^O's will take effect
1668 * immediately.
1669 */
1670 while (cc > 0) {
1671 if (!ISSET(tp->t_oflag, OPOST))
1672 ce = cc;
1673 else {
1674 ce = cc - scanc((u_int)cc, cp, char_type,
1675 CCLASSMASK);
1676 /*
1677 * If ce is zero, then we're processing
1678 * a special character through ttyoutput.
1679 */
1680 if (ce == 0) {
1681 tp->t_rocount = 0;
1682 if (ttyoutput(*cp, tp) >= 0) {
1683 /* out of space */
1684 goto overfull;
1685 }
1686 cp++;
1687 cc--;
1688 if (ISSET(tp->t_lflag, FLUSHO) ||
1689 tp->t_outq.c_cc > hiwat)
1690 goto ovhiwat;
1691 continue;
1692 }
1693 }
1694 /*
1695 * A bunch of normal characters have been found.
1696 * Transfer them en masse to the output queue and
1697 * continue processing at the top of the loop.
1698 * If there are any further characters in this
1699 * <= OBUFSIZ chunk, the first should be a character
1700 * requiring special handling by ttyoutput.
1701 */
1702 tp->t_rocount = 0;
1703 i = b_to_q(cp, ce, &tp->t_outq);
1704 ce -= i;
1705 tp->t_column += ce;
1706 cp += ce, cc -= ce, tk_nout += ce;
1707 tp->t_outcc += ce;
1708 if (i > 0) {
1709 /* out of space */
1710 goto overfull;
1711 }
1712 if (ISSET(tp->t_lflag, FLUSHO) ||
1713 tp->t_outq.c_cc > hiwat)
1714 break;
1715 }
1716 ttstart(tp);
1717 }
1718 out:
1719 /*
1720 * If cc is nonzero, we leave the uio structure inconsistent, as the
1721 * offset and iov pointers have moved forward, but it doesn't matter
1722 * (the call will either return short or restart with a new uio).
1723 */
1724 uio->uio_resid += cc;
1725 return (error);
1726
1727 overfull:
1728 /*
1729 * Since we are using ring buffers, if we can't insert any more into
1730 * the output queue, we can assume the ring is full and that someone
1731 * forgot to set the high water mark correctly. We set it and then
1732 * proceed as normal.
1733 */
1734 hiwat = tp->t_outq.c_cc - 1;
1735
1736 ovhiwat:
1737 ttstart(tp);
1738 s = spltty();
1739 /*
1740 * This can only occur if FLUSHO is set in t_lflag,
1741 * or if ttstart/oproc is synchronous (or very fast).
1742 */
1743 if (tp->t_outq.c_cc <= hiwat) {
1744 splx(s);
1745 goto loop;
1746 }
1747 if (flag & IO_NDELAY) {
1748 splx(s);
1749 uio->uio_resid += cc;
1750 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
1751 }
1752 SET(tp->t_state, TS_ASLEEP);
1753 error = ttysleep(tp, &tp->t_outq, TTOPRI | PCATCH, ttyout, 0);
1754 splx(s);
1755 if (error)
1756 goto out;
1757 goto loop;
1758 }
1759
1760 /*
1761 * Rubout one character from the rawq of tp
1762 * as cleanly as possible.
1763 */
1764 void
1765 ttyrub(c, tp)
1766 int c;
1767 struct tty *tp;
1768 {
1769 u_char *cp;
1770 int savecol;
1771 int tabc, s;
1772
1773 if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
1774 return;
1775 CLR(tp->t_lflag, FLUSHO);
1776 if (ISSET(tp->t_lflag, ECHOE)) {
1777 if (tp->t_rocount == 0) {
1778 /*
1779 * Screwed by ttwrite; retype
1780 */
1781 ttyretype(tp);
1782 return;
1783 }
1784 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
1785 ttyrubo(tp, 2);
1786 else {
1787 CLR(c, ~TTY_CHARMASK);
1788 switch (CCLASS(c)) {
1789 case ORDINARY:
1790 ttyrubo(tp, 1);
1791 break;
1792 case BACKSPACE:
1793 case CONTROL:
1794 case NEWLINE:
1795 case RETURN:
1796 case VTAB:
1797 if (ISSET(tp->t_lflag, ECHOCTL))
1798 ttyrubo(tp, 2);
1799 break;
1800 case TAB:
1801 if (tp->t_rocount < tp->t_rawq.c_cc) {
1802 ttyretype(tp);
1803 return;
1804 }
1805 s = spltty();
1806 savecol = tp->t_column;
1807 SET(tp->t_state, TS_CNTTB);
1808 SET(tp->t_lflag, FLUSHO);
1809 tp->t_column = tp->t_rocol;
1810 for (cp = firstc(&tp->t_rawq, &tabc); cp;
1811 cp = nextc(&tp->t_rawq, cp, &tabc))
1812 ttyecho(tabc, tp);
1813 CLR(tp->t_lflag, FLUSHO);
1814 CLR(tp->t_state, TS_CNTTB);
1815 splx(s);
1816
1817 /* savecol will now be length of the tab. */
1818 savecol -= tp->t_column;
1819 tp->t_column += savecol;
1820 if (savecol > 8)
1821 savecol = 8; /* overflow screw */
1822 while (--savecol >= 0)
1823 (void)ttyoutput('\b', tp);
1824 break;
1825 default: /* XXX */
1826 #define PANICSTR "ttyrub: would panic c = %d, val = %d\n"
1827 (void)printf(PANICSTR, c, CCLASS(c));
1828 #ifdef notdef
1829 panic(PANICSTR, c, CCLASS(c));
1830 #endif
1831 }
1832 }
1833 } else if (ISSET(tp->t_lflag, ECHOPRT)) {
1834 if (!ISSET(tp->t_state, TS_ERASE)) {
1835 SET(tp->t_state, TS_ERASE);
1836 (void)ttyoutput('\\', tp);
1837 }
1838 ttyecho(c, tp);
1839 } else
1840 ttyecho(tp->t_cc[VERASE], tp);
1841 --tp->t_rocount;
1842 }
1843
1844 /*
1845 * Back over cnt characters, erasing them.
1846 */
1847 static void
1848 ttyrubo(tp, cnt)
1849 struct tty *tp;
1850 int cnt;
1851 {
1852
1853 while (cnt-- > 0) {
1854 (void)ttyoutput('\b', tp);
1855 (void)ttyoutput(' ', tp);
1856 (void)ttyoutput('\b', tp);
1857 }
1858 }
1859
1860 /*
1861 * ttyretype --
1862 * Reprint the rawq line. Note, it is assumed that c_cc has already
1863 * been checked.
1864 */
1865 void
1866 ttyretype(tp)
1867 struct tty *tp;
1868 {
1869 u_char *cp;
1870 int s, c;
1871
1872 /* Echo the reprint character. */
1873 if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
1874 ttyecho(tp->t_cc[VREPRINT], tp);
1875
1876 (void)ttyoutput('\n', tp);
1877
1878 s = spltty();
1879 for (cp = firstc(&tp->t_canq, &c); cp; cp = nextc(&tp->t_canq, cp, &c))
1880 ttyecho(c, tp);
1881 for (cp = firstc(&tp->t_rawq, &c); cp; cp = nextc(&tp->t_rawq, cp, &c))
1882 ttyecho(c, tp);
1883 CLR(tp->t_state, TS_ERASE);
1884 splx(s);
1885
1886 tp->t_rocount = tp->t_rawq.c_cc;
1887 tp->t_rocol = 0;
1888 }
1889
1890 /*
1891 * Echo a typed character to the terminal.
1892 */
1893 static void
1894 ttyecho(c, tp)
1895 int c;
1896 struct tty *tp;
1897 {
1898
1899 if (!ISSET(tp->t_state, TS_CNTTB))
1900 CLR(tp->t_lflag, FLUSHO);
1901 if ((!ISSET(tp->t_lflag, ECHO) &&
1902 (!ISSET(tp->t_lflag, ECHONL) || c != '\n')) ||
1903 ISSET(tp->t_lflag, EXTPROC))
1904 return;
1905 if (((ISSET(tp->t_lflag, ECHOCTL) &&
1906 (ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n')) ||
1907 ISSET(c, TTY_CHARMASK) == 0177)) {
1908 (void)ttyoutput('^', tp);
1909 CLR(c, ~TTY_CHARMASK);
1910 if (c == 0177)
1911 c = '?';
1912 else
1913 c += 'A' - 1;
1914 }
1915 (void)ttyoutput(c, tp);
1916 }
1917
1918 /*
1919 * Wake up any readers on a tty.
1920 */
1921 void
1922 ttwakeup(tp)
1923 struct tty *tp;
1924 {
1925
1926 selwakeup(&tp->t_rsel);
1927 if (ISSET(tp->t_state, TS_ASYNC))
1928 pgsignal(tp->t_pgrp, SIGIO, 1);
1929 wakeup((caddr_t)&tp->t_rawq);
1930 }
1931
1932 /*
1933 * Look up a code for a specified speed in a conversion table;
1934 * used by drivers to map software speed values to hardware parameters.
1935 */
1936 int
1937 ttspeedtab(speed, table)
1938 int speed;
1939 struct speedtab *table;
1940 {
1941
1942 for ( ; table->sp_speed != -1; table++)
1943 if (table->sp_speed == speed)
1944 return (table->sp_code);
1945 return (-1);
1946 }
1947
1948 /*
1949 * Set tty hi and low water marks.
1950 *
1951 * Try to arrange the dynamics so there's about one second
1952 * from hi to low water.
1953 */
1954 void
1955 ttsetwater(tp)
1956 struct tty *tp;
1957 {
1958 int cps, x;
1959
1960 #define CLAMP(x, h, l) ((x) > h ? h : ((x) < l) ? l : (x))
1961
1962 cps = tp->t_ospeed / 10;
1963 tp->t_lowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
1964 x += cps;
1965 x = CLAMP(x, TTMAXHIWAT, TTMINHIWAT);
1966 tp->t_hiwat = roundup(x, CBSIZE);
1967 #undef CLAMP
1968 }
1969
1970 /*
1971 * Report on state of foreground process group.
1972 */
1973 void
1974 ttyinfo(tp)
1975 struct tty *tp;
1976 {
1977 struct proc *p, *pick;
1978 struct timeval utime, stime;
1979 int tmp;
1980
1981 if (ttycheckoutq(tp,0) == 0)
1982 return;
1983
1984 /* Print load average. */
1985 tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
1986 ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
1987
1988 if (tp->t_session == NULL)
1989 ttyprintf(tp, "not a controlling terminal\n");
1990 else if (tp->t_pgrp == NULL)
1991 ttyprintf(tp, "no foreground process group\n");
1992 else if ((p = tp->t_pgrp->pg_members.lh_first) == 0)
1993 ttyprintf(tp, "empty foreground process group\n");
1994 else {
1995 /* Pick interesting process. */
1996 for (pick = NULL; p != NULL; p = p->p_pglist.le_next)
1997 if (proc_compare(pick, p))
1998 pick = p;
1999
2000 ttyprintf(tp, " cmd: %s %d [%s] ", pick->p_comm, pick->p_pid,
2001 pick->p_stat == SONPROC ? "running" :
2002 pick->p_stat == SRUN ? "runnable" :
2003 pick->p_wmesg ? pick->p_wmesg : "iowait");
2004
2005 calcru(pick, &utime, &stime, NULL);
2006
2007 /* Round up and print user time. */
2008 utime.tv_usec += 5000;
2009 if (utime.tv_usec >= 1000000) {
2010 utime.tv_sec += 1;
2011 utime.tv_usec -= 1000000;
2012 }
2013 ttyprintf(tp, "%ld.%02ldu ", (long int)utime.tv_sec,
2014 (long int)utime.tv_usec / 10000);
2015
2016 /* Round up and print system time. */
2017 stime.tv_usec += 5000;
2018 if (stime.tv_usec >= 1000000) {
2019 stime.tv_sec += 1;
2020 stime.tv_usec -= 1000000;
2021 }
2022 ttyprintf(tp, "%ld.%02lds ", (long int)stime.tv_sec,
2023 (long int)stime.tv_usec / 10000);
2024
2025 #define pgtok(a) (((u_long) ((a) * NBPG) / 1024))
2026 /* Print percentage cpu. */
2027 tmp = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
2028 ttyprintf(tp, "%d%% ", tmp / 100);
2029
2030 /* Print resident set size. */
2031 if (pick->p_stat == SIDL || P_ZOMBIE(pick))
2032 tmp = 0;
2033 else {
2034 struct vmspace *vm = pick->p_vmspace;
2035 tmp = pgtok(vm_resident_count(vm));
2036 }
2037 ttyprintf(tp, "%dk\n", tmp);
2038 }
2039 tp->t_rocount = 0; /* so pending input will be retyped if BS */
2040 }
2041
2042 /*
2043 * Returns 1 if p2 is "better" than p1
2044 *
2045 * The algorithm for picking the "interesting" process is thus:
2046 *
2047 * 1) Only foreground processes are eligible - implied.
2048 * 2) Runnable processes are favored over anything else. The runner
2049 * with the highest cpu utilization is picked (p_estcpu). Ties are
2050 * broken by picking the highest pid.
2051 * 3) The sleeper with the shortest sleep time is next. With ties,
2052 * we pick out just "short-term" sleepers (P_SINTR == 0).
2053 * 4) Further ties are broken by picking the highest pid.
2054 */
2055 #define ISRUN(p) (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL) || \
2056 ((p)->p_stat == SONPROC))
2057 #define TESTAB(a, b) ((a)<<1 | (b))
2058 #define ONLYA 2
2059 #define ONLYB 1
2060 #define BOTH 3
2061
2062 static int
2063 proc_compare(p1, p2)
2064 struct proc *p1, *p2;
2065 {
2066
2067 if (p1 == NULL)
2068 return (1);
2069 /*
2070 * see if at least one of them is runnable
2071 */
2072 switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
2073 case ONLYA:
2074 return (0);
2075 case ONLYB:
2076 return (1);
2077 case BOTH:
2078 /*
2079 * tie - favor one with highest recent cpu utilization
2080 */
2081 if (p2->p_estcpu > p1->p_estcpu)
2082 return (1);
2083 if (p1->p_estcpu > p2->p_estcpu)
2084 return (0);
2085 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2086 }
2087 /*
2088 * weed out zombies
2089 */
2090 switch (TESTAB(P_ZOMBIE(p1), P_ZOMBIE(p2))) {
2091 case ONLYA:
2092 return (1);
2093 case ONLYB:
2094 return (0);
2095 case BOTH:
2096 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2097 }
2098 /*
2099 * pick the one with the smallest sleep time
2100 */
2101 if (p2->p_slptime > p1->p_slptime)
2102 return (0);
2103 if (p1->p_slptime > p2->p_slptime)
2104 return (1);
2105 /*
2106 * favor one sleeping in a non-interruptible sleep
2107 */
2108 if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
2109 return (1);
2110 if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
2111 return (0);
2112 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2113 }
2114
2115 /*
2116 * Output char to tty; console putchar style.
2117 */
2118 int
2119 tputchar(c, tp)
2120 int c;
2121 struct tty *tp;
2122 {
2123 int s;
2124
2125 s = spltty();
2126 if (ISSET(tp->t_state,
2127 TS_CARR_ON | TS_ISOPEN) != (TS_CARR_ON | TS_ISOPEN)) {
2128 splx(s);
2129 return (-1);
2130 }
2131 if (c == '\n')
2132 (void)ttyoutput('\r', tp);
2133 (void)ttyoutput(c, tp);
2134 ttstart(tp);
2135 splx(s);
2136 return (0);
2137 }
2138
2139 /*
2140 * Sleep on chan, returning ERESTART if tty changed while we napped and
2141 * returning any errors (e.g. EINTR/ETIMEDOUT) reported by tsleep. If
2142 * the tty is revoked, restarting a pending call will redo validation done
2143 * at the start of the call.
2144 */
2145 int
2146 ttysleep(tp, chan, pri, wmesg, timo)
2147 struct tty *tp;
2148 void *chan;
2149 int pri, timo;
2150 const char *wmesg;
2151 {
2152 int error;
2153 short gen;
2154
2155 gen = tp->t_gen;
2156 if ((error = tsleep(chan, pri, wmesg, timo)) != 0)
2157 return (error);
2158 return (tp->t_gen == gen ? 0 : ERESTART);
2159 }
2160
2161 /*
2162 * Initialise the global tty list.
2163 */
2164 void
2165 tty_init()
2166 {
2167 ttyldisc_init();
2168
2169 TAILQ_INIT(&ttylist);
2170 tty_count = 0;
2171
2172 pool_init(&tty_pool, sizeof(struct tty), 0, 0, 0, "ttypl",
2173 0, pool_page_alloc_nointr, pool_page_free_nointr, M_TTYS);
2174 }
2175
2176 /*
2177 * Attach a tty to the tty list.
2178 *
2179 * This should be called ONLY once per real tty (including pty's).
2180 * eg, on the sparc, the keyboard and mouse have struct tty's that are
2181 * distinctly NOT usable as tty's, and thus should not be attached to
2182 * the ttylist. This is why this call is not done from ttymalloc().
2183 *
2184 * Device drivers should attach tty's at a similar time that they are
2185 * ttymalloc()'ed, or, for the case of statically allocated struct tty's
2186 * either in the attach or (first) open routine.
2187 */
2188 void
2189 tty_attach(tp)
2190 struct tty *tp;
2191 {
2192
2193 TAILQ_INSERT_TAIL(&ttylist, tp, tty_link);
2194 ++tty_count;
2195 }
2196
2197 /*
2198 * Remove a tty from the tty list.
2199 */
2200 void
2201 tty_detach(tp)
2202 struct tty *tp;
2203 {
2204
2205 --tty_count;
2206 #ifdef DIAGNOSTIC
2207 if (tty_count < 0)
2208 panic("tty_detach: tty_count < 0");
2209 #endif
2210 TAILQ_REMOVE(&ttylist, tp, tty_link);
2211 }
2212
2213 /*
2214 * Allocate a tty structure and its associated buffers.
2215 */
2216 struct tty *
2217 ttymalloc()
2218 {
2219 struct tty *tp;
2220
2221 tp = pool_get(&tty_pool, PR_WAITOK);
2222 memset(tp, 0, sizeof(*tp));
2223 callout_init(&tp->t_outq_ch);
2224 callout_init(&tp->t_rstrt_ch);
2225 /* XXX: default to 1024 chars for now */
2226 clalloc(&tp->t_rawq, 1024, 1);
2227 clalloc(&tp->t_canq, 1024, 1);
2228 /* output queue doesn't need quoting */
2229 clalloc(&tp->t_outq, 1024, 0);
2230 /* Set default line discipline. */
2231 tp->t_linesw = linesw[0];
2232 return(tp);
2233 }
2234
2235 /*
2236 * Free a tty structure and its buffers.
2237 *
2238 * Be sure to call tty_detach() for any tty that has been
2239 * tty_attach()ed.
2240 */
2241 void
2242 ttyfree(tp)
2243 struct tty *tp;
2244 {
2245
2246 callout_stop(&tp->t_outq_ch);
2247 callout_stop(&tp->t_rstrt_ch);
2248 clfree(&tp->t_rawq);
2249 clfree(&tp->t_canq);
2250 clfree(&tp->t_outq);
2251 pool_put(&tty_pool, tp);
2252 }
2253