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