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