Merge from vendor branch FILE:
[dragonfly.git] / sys / kern / tty.c
1 /*-
2  * Copyright (c) 1982, 1986, 1990, 1991, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  * (c) UNIX System Laboratories, Inc.
5  * All or some portions of this file are derived from material licensed
6  * to the University of California by American Telephone and Telegraph
7  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8  * the permission of UNIX System Laboratories, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *      This product includes software developed by the University of
21  *      California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  *
38  *      @(#)tty.c       8.8 (Berkeley) 1/21/94
39  * $FreeBSD: src/sys/kern/tty.c,v 1.129.2.5 2002/03/11 01:32:31 dd Exp $
40  * $DragonFly: src/sys/kern/tty.c,v 1.14 2004/10/12 19:20:46 dillon Exp $
41  */
42
43 /*-
44  * TODO:
45  *      o Fix races for sending the start char in ttyflush().
46  *      o Handle inter-byte timeout for "MIN > 0, TIME > 0" in ttyselect().
47  *        With luck, there will be MIN chars before select() returns().
48  *      o Handle CLOCAL consistently for ptys.  Perhaps disallow setting it.
49  *      o Don't allow input in TS_ZOMBIE case.  It would be visible through
50  *        FIONREAD.
51  *      o Do the new sio locking stuff here and use it to avoid special
52  *        case for EXTPROC?
53  *      o Lock PENDIN too?
54  *      o Move EXTPROC and/or PENDIN to t_state?
55  *      o Wrap most of ttioctl in spltty/splx.
56  *      o Implement TIOCNOTTY or remove it from <sys/ioctl.h>.
57  *      o Send STOP if IXOFF is toggled off while TS_TBLOCK is set.
58  *      o Don't allow certain termios flags to affect disciplines other
59  *        than TTYDISC.  Cancel their effects before switch disciplines
60  *        and ignore them if they are set while we are in another
61  *        discipline.
62  *      o Now that historical speed conversions are handled here, don't
63  *        do them in drivers.
64  *      o Check for TS_CARR_ON being set while everything is closed and not
65  *        waiting for carrier.  TS_CARR_ON isn't cleared if nothing is open,
66  *        so it would live until the next open even if carrier drops.
67  *      o Restore TS_WOPEN since it is useful in pstat.  It must be cleared
68  *        only when _all_ openers leave open().
69  */
70
71 #include "opt_compat.h"
72 #include "opt_uconsole.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/filio.h>
77 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
78 #include <sys/ioctl_compat.h>
79 #endif
80 #include <sys/proc.h>
81 #define TTYDEFCHARS
82 #include <sys/tty.h>
83 #include <sys/clist.h>
84 #undef  TTYDEFCHARS
85 #include <sys/fcntl.h>
86 #include <sys/conf.h>
87 #include <sys/dkstat.h>
88 #include <sys/poll.h>
89 #include <sys/kernel.h>
90 #include <sys/vnode.h>
91 #include <sys/signalvar.h>
92 #include <sys/resourcevar.h>
93 #include <sys/malloc.h>
94 #include <sys/filedesc.h>
95 #include <sys/sysctl.h>
96 #include <sys/thread2.h>
97
98 #include <vm/vm.h>
99 #include <sys/lock.h>
100 #include <vm/pmap.h>
101 #include <vm/vm_map.h>
102
103 MALLOC_DEFINE(M_TTYS, "ttys", "tty data structures");
104
105 static int      proc_compare (struct proc *p1, struct proc *p2);
106 static int      ttnread (struct tty *tp);
107 static void     ttyecho (int c, struct tty *tp);
108 static int      ttyoutput (int c, struct tty *tp);
109 static void     ttypend (struct tty *tp);
110 static void     ttyretype (struct tty *tp);
111 static void     ttyrub (int c, struct tty *tp);
112 static void     ttyrubo (struct tty *tp, int cnt);
113 static void     ttyunblock (struct tty *tp);
114 static int      ttywflush (struct tty *tp);
115 static int      filt_ttyread (struct knote *kn, long hint);
116 static void     filt_ttyrdetach (struct knote *kn);
117 static int      filt_ttywrite (struct knote *kn, long hint);
118 static void     filt_ttywdetach (struct knote *kn);
119
120 /*
121  * Table with character classes and parity. The 8th bit indicates parity,
122  * the 7th bit indicates the character is an alphameric or underscore (for
123  * ALTWERASE), and the low 6 bits indicate delay type.  If the low 6 bits
124  * are 0 then the character needs no special processing on output; classes
125  * other than 0 might be translated or (not currently) require delays.
126  */
127 #define E       0x00    /* Even parity. */
128 #define O       0x80    /* Odd parity. */
129 #define PARITY(c)       (char_type[c] & O)
130
131 #define ALPHA   0x40    /* Alpha or underscore. */
132 #define ISALPHA(c)      (char_type[(c) & TTY_CHARMASK] & ALPHA)
133
134 #define CCLASSMASK      0x3f
135 #define CCLASS(c)       (char_type[c] & CCLASSMASK)
136
137 #define BS      BACKSPACE
138 #define CC      CONTROL
139 #define CR      RETURN
140 #define NA      ORDINARY | ALPHA
141 #define NL      NEWLINE
142 #define NO      ORDINARY
143 #define TB      TAB
144 #define VT      VTAB
145
146 static u_char const char_type[] = {
147         E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* nul - bel */
148         O|BS, E|TB, E|NL, O|CC, E|VT, O|CR, O|CC, E|CC, /* bs - si */
149         O|CC, E|CC, E|CC, O|CC, E|CC, O|CC, O|CC, E|CC, /* dle - etb */
150         E|CC, O|CC, O|CC, E|CC, O|CC, E|CC, E|CC, O|CC, /* can - us */
151         O|NO, E|NO, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* sp - ' */
152         E|NO, O|NO, O|NO, E|NO, O|NO, E|NO, E|NO, O|NO, /* ( - / */
153         E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* 0 - 7 */
154         O|NA, E|NA, E|NO, O|NO, E|NO, O|NO, O|NO, E|NO, /* 8 - ? */
155         O|NO, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* @ - G */
156         E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* H - O */
157         E|NA, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* P - W */
158         O|NA, E|NA, E|NA, O|NO, E|NO, O|NO, O|NO, O|NA, /* X - _ */
159         E|NO, O|NA, O|NA, E|NA, O|NA, E|NA, E|NA, O|NA, /* ` - g */
160         O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* h - o */
161         O|NA, E|NA, E|NA, O|NA, E|NA, O|NA, O|NA, E|NA, /* p - w */
162         E|NA, O|NA, O|NA, E|NO, O|NO, E|NO, E|NO, O|CC, /* x - del */
163         /*
164          * Meta chars; should be settable per character set;
165          * for now, treat them all as normal characters.
166          */
167         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
168         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
169         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
170         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
171         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
172         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
173         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
174         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
175         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
176         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
177         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
178         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
179         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
180         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
181         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
182         NA,   NA,   NA,   NA,   NA,   NA,   NA,   NA,
183 };
184 #undef  BS
185 #undef  CC
186 #undef  CR
187 #undef  NA
188 #undef  NL
189 #undef  NO
190 #undef  TB
191 #undef  VT
192
193 /* Macros to clear/set/test flags. */
194 #define SET(t, f)       (t) |= (f)
195 #define CLR(t, f)       (t) &= ~(f)
196 #define ISSET(t, f)     ((t) & (f))
197
198 #undef MAX_INPUT                /* XXX wrong in <sys/syslimits.h> */
199 #define MAX_INPUT       TTYHOG  /* XXX limit is usually larger for !ICANON */
200
201 /*
202  * list of struct tty where pstat(8) can pick it up with sysctl
203  */
204 static SLIST_HEAD(, tty) tty_list;
205
206 /*
207  * Initial open of tty, or (re)entry to standard tty line discipline.
208  */
209 int
210 ttyopen(device, tp)
211         dev_t device;
212         struct tty *tp;
213 {
214         int s;
215
216         s = spltty();
217         tp->t_dev = device;
218         if (!ISSET(tp->t_state, TS_ISOPEN)) {
219                 SET(tp->t_state, TS_ISOPEN);
220                 if (ISSET(tp->t_cflag, CLOCAL))
221                         SET(tp->t_state, TS_CONNECTED);
222                 bzero(&tp->t_winsize, sizeof(tp->t_winsize));
223         }
224         ttsetwater(tp);
225         splx(s);
226         return (0);
227 }
228
229 /*
230  * Handle close() on a tty line: flush and set to initial state,
231  * bumping generation number so that pending read/write calls
232  * can detect recycling of the tty.
233  *
234  * XXX our caller should have done `spltty(); l_close(); ttyclose();'
235  * and l_close() should have flushed, but we repeat the spltty() and
236  * the flush in case there are buggy callers.
237  */
238 int
239 ttyclose(tp)
240         struct tty *tp;
241 {
242         int s;
243
244         funsetown(tp->t_sigio);
245         s = spltty();
246         if (constty == tp)
247                 constty = NULL;
248
249         ttyflush(tp, FREAD | FWRITE);
250         clist_free_cblocks(&tp->t_canq);
251         clist_free_cblocks(&tp->t_outq);
252         clist_free_cblocks(&tp->t_rawq);
253
254         tp->t_gen++;
255         tp->t_line = TTYDISC;
256         ttyclearsession(tp);
257         tp->t_state = 0;
258         splx(s);
259         return (0);
260 }
261
262 /*
263  * Disassociate the tty from its session.  Traditionally this has only been
264  * a half-close, meaning that the session was still allowed to point at the
265  * tty (resulting in the tty in the ps command showing something like 'p0-'),
266  * even though the tty is no longer pointing at the session.
267  *
268  * The half close seems to be useful only for 'ps' output but there is as
269  * yet no reason to remove the feature.  The full-close code is currently
270  * #if 0'd out.  See also sess_rele() in kern/kern_proc.c.
271  */
272 void
273 ttyclearsession(struct tty *tp)
274 {
275         struct session *sp;
276
277         tp->t_pgrp = NULL;
278         if ((sp = tp->t_session) != NULL) {
279                 tp->t_session = NULL;
280 #ifdef TTY_DO_FULL_CLOSE
281                 /* FULL CLOSE (not yet) */
282                 if (sp->s_ttyp == tp) {
283                         sp->s_ttyp = NULL;
284                 } else {
285                         printf("ttyclearsession: warning: sp->s_ttyp != tp "
286                                 "%p/%p\n", sp->s_ttyp, tp);
287                 }
288 #endif
289         }
290 }
291
292 /*
293  * Terminate the tty vnode association for a session.  This is the 
294  * 'other half' of the close.
295  */
296 void
297 ttyclosesession(struct session *sp, int dorele)
298 {
299         struct vnode *vp;
300         struct thread *td = curthread;
301
302         if ((vp = sp->s_ttyvp) == NULL)
303                 return;
304         sp->s_ttyvp = NULL;
305         if (vp->v_flag & VCTTYISOPEN) {
306                 if (vn_lock(vp, LK_EXCLUSIVE|LK_RETRY, td) == 0) {
307                         vclrflags(vp, VCTTYISOPEN);
308                         VOP_CLOSE(vp, 0, td);
309                         VOP_UNLOCK(vp, 0, td);
310                 }
311         }
312         if (dorele)
313                 vrele(vp);
314 }
315
316
317 #define FLUSHQ(q) {                                                     \
318         if ((q)->c_cc)                                                  \
319                 ndflush(q, (q)->c_cc);                                  \
320 }
321
322 /* Is 'c' a line delimiter ("break" character)? */
323 #define TTBREAKC(c, lflag)                                                      \
324         ((c) == '\n' || (((c) == cc[VEOF] ||                            \
325           (c) == cc[VEOL] || ((c) == cc[VEOL2] && lflag & IEXTEN)) &&   \
326          (c) != _POSIX_VDISABLE))
327
328 /*
329  * Process input of a single character received on a tty.
330  */
331 int
332 ttyinput(c, tp)
333         int c;
334         struct tty *tp;
335 {
336         tcflag_t iflag, lflag;
337         cc_t *cc;
338         int i, err;
339
340         /*
341          * If input is pending take it first.
342          */
343         lflag = tp->t_lflag;
344         if (ISSET(lflag, PENDIN))
345                 ttypend(tp);
346         /*
347          * Gather stats.
348          */
349         if (ISSET(lflag, ICANON)) {
350                 ++tk_cancc;
351                 ++tp->t_cancc;
352         } else {
353                 ++tk_rawcc;
354                 ++tp->t_rawcc;
355         }
356         ++tk_nin;
357
358         /*
359          * Block further input iff:
360          * current input > threshold AND input is available to user program
361          * AND input flow control is enabled and not yet invoked.
362          * The 3 is slop for PARMRK.
363          */
364         iflag = tp->t_iflag;
365         if (tp->t_rawq.c_cc + tp->t_canq.c_cc > tp->t_ihiwat - 3 &&
366             (!ISSET(lflag, ICANON) || tp->t_canq.c_cc != 0) &&
367             (ISSET(tp->t_cflag, CRTS_IFLOW) || ISSET(iflag, IXOFF)) &&
368             !ISSET(tp->t_state, TS_TBLOCK))
369                 ttyblock(tp);
370
371         /* Handle exceptional conditions (break, parity, framing). */
372         cc = tp->t_cc;
373         err = (ISSET(c, TTY_ERRORMASK));
374         if (err) {
375                 CLR(c, TTY_ERRORMASK);
376                 if (ISSET(err, TTY_BI)) {
377                         if (ISSET(iflag, IGNBRK))
378                                 return (0);
379                         if (ISSET(iflag, BRKINT)) {
380                                 ttyflush(tp, FREAD | FWRITE);
381                                 pgsignal(tp->t_pgrp, SIGINT, 1);
382                                 goto endcase;
383                         }
384                         if (ISSET(iflag, PARMRK))
385                                 goto parmrk;
386                 } else if ((ISSET(err, TTY_PE) && ISSET(iflag, INPCK))
387                         || ISSET(err, TTY_FE)) {
388                         if (ISSET(iflag, IGNPAR))
389                                 return (0);
390                         else if (ISSET(iflag, PARMRK)) {
391 parmrk:
392                                 if (tp->t_rawq.c_cc + tp->t_canq.c_cc >
393                                     MAX_INPUT - 3)
394                                         goto input_overflow;
395                                 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
396                                 (void)putc(0 | TTY_QUOTE, &tp->t_rawq);
397                                 (void)putc(c | TTY_QUOTE, &tp->t_rawq);
398                                 goto endcase;
399                         } else
400                                 c = 0;
401                 }
402         }
403
404         if (!ISSET(tp->t_state, TS_TYPEN) && ISSET(iflag, ISTRIP))
405                 CLR(c, 0x80);
406         if (!ISSET(lflag, EXTPROC)) {
407                 /*
408                  * Check for literal nexting very first
409                  */
410                 if (ISSET(tp->t_state, TS_LNCH)) {
411                         SET(c, TTY_QUOTE);
412                         CLR(tp->t_state, TS_LNCH);
413                 }
414                 /*
415                  * Scan for special characters.  This code
416                  * is really just a big case statement with
417                  * non-constant cases.  The bottom of the
418                  * case statement is labeled ``endcase'', so goto
419                  * it after a case match, or similar.
420                  */
421
422                 /*
423                  * Control chars which aren't controlled
424                  * by ICANON, ISIG, or IXON.
425                  */
426                 if (ISSET(lflag, IEXTEN)) {
427                         if (CCEQ(cc[VLNEXT], c)) {
428                                 if (ISSET(lflag, ECHO)) {
429                                         if (ISSET(lflag, ECHOE)) {
430                                                 (void)ttyoutput('^', tp);
431                                                 (void)ttyoutput('\b', tp);
432                                         } else
433                                                 ttyecho(c, tp);
434                                 }
435                                 SET(tp->t_state, TS_LNCH);
436                                 goto endcase;
437                         }
438                         if (CCEQ(cc[VDISCARD], c)) {
439                                 if (ISSET(lflag, FLUSHO))
440                                         CLR(tp->t_lflag, FLUSHO);
441                                 else {
442                                         ttyflush(tp, FWRITE);
443                                         ttyecho(c, tp);
444                                         if (tp->t_rawq.c_cc + tp->t_canq.c_cc)
445                                                 ttyretype(tp);
446                                         SET(tp->t_lflag, FLUSHO);
447                                 }
448                                 goto startoutput;
449                         }
450                 }
451                 /*
452                  * Signals.
453                  */
454                 if (ISSET(lflag, ISIG)) {
455                         if (CCEQ(cc[VINTR], c) || CCEQ(cc[VQUIT], c)) {
456                                 if (!ISSET(lflag, NOFLSH))
457                                         ttyflush(tp, FREAD | FWRITE);
458                                 ttyecho(c, tp);
459                                 pgsignal(tp->t_pgrp,
460                                     CCEQ(cc[VINTR], c) ? SIGINT : SIGQUIT, 1);
461                                 goto endcase;
462                         }
463                         if (CCEQ(cc[VSUSP], c)) {
464                                 if (!ISSET(lflag, NOFLSH))
465                                         ttyflush(tp, FREAD);
466                                 ttyecho(c, tp);
467                                 pgsignal(tp->t_pgrp, SIGTSTP, 1);
468                                 goto endcase;
469                         }
470                 }
471                 /*
472                  * Handle start/stop characters.
473                  */
474                 if (ISSET(iflag, IXON)) {
475                         if (CCEQ(cc[VSTOP], c)) {
476                                 if (!ISSET(tp->t_state, TS_TTSTOP)) {
477                                         SET(tp->t_state, TS_TTSTOP);
478                                         (*tp->t_stop)(tp, 0);
479                                         return (0);
480                                 }
481                                 if (!CCEQ(cc[VSTART], c))
482                                         return (0);
483                                 /*
484                                  * if VSTART == VSTOP then toggle
485                                  */
486                                 goto endcase;
487                         }
488                         if (CCEQ(cc[VSTART], c))
489                                 goto restartoutput;
490                 }
491                 /*
492                  * IGNCR, ICRNL, & INLCR
493                  */
494                 if (c == '\r') {
495                         if (ISSET(iflag, IGNCR))
496                                 return (0);
497                         else if (ISSET(iflag, ICRNL))
498                                 c = '\n';
499                 } else if (c == '\n' && ISSET(iflag, INLCR))
500                         c = '\r';
501         }
502         if (!ISSET(tp->t_lflag, EXTPROC) && ISSET(lflag, ICANON)) {
503                 /*
504                  * From here on down canonical mode character
505                  * processing takes place.
506                  */
507                 /*
508                  * erase or erase2 (^H / ^?)
509                  */
510                 if (CCEQ(cc[VERASE], c) || CCEQ(cc[VERASE2], c) ) {
511                         if (tp->t_rawq.c_cc)
512                                 ttyrub(unputc(&tp->t_rawq), tp);
513                         goto endcase;
514                 }
515                 /*
516                  * kill (^U)
517                  */
518                 if (CCEQ(cc[VKILL], c)) {
519                         if (ISSET(lflag, ECHOKE) &&
520                             tp->t_rawq.c_cc == tp->t_rocount &&
521                             !ISSET(lflag, ECHOPRT))
522                                 while (tp->t_rawq.c_cc)
523                                         ttyrub(unputc(&tp->t_rawq), tp);
524                         else {
525                                 ttyecho(c, tp);
526                                 if (ISSET(lflag, ECHOK) ||
527                                     ISSET(lflag, ECHOKE))
528                                         ttyecho('\n', tp);
529                                 FLUSHQ(&tp->t_rawq);
530                                 tp->t_rocount = 0;
531                         }
532                         CLR(tp->t_state, TS_LOCAL);
533                         goto endcase;
534                 }
535                 /*
536                  * word erase (^W)
537                  */
538                 if (CCEQ(cc[VWERASE], c) && ISSET(lflag, IEXTEN)) {
539                         int ctype;
540
541                         /*
542                          * erase whitespace
543                          */
544                         while ((c = unputc(&tp->t_rawq)) == ' ' || c == '\t')
545                                 ttyrub(c, tp);
546                         if (c == -1)
547                                 goto endcase;
548                         /*
549                          * erase last char of word and remember the
550                          * next chars type (for ALTWERASE)
551                          */
552                         ttyrub(c, tp);
553                         c = unputc(&tp->t_rawq);
554                         if (c == -1)
555                                 goto endcase;
556                         if (c == ' ' || c == '\t') {
557                                 (void)putc(c, &tp->t_rawq);
558                                 goto endcase;
559                         }
560                         ctype = ISALPHA(c);
561                         /*
562                          * erase rest of word
563                          */
564                         do {
565                                 ttyrub(c, tp);
566                                 c = unputc(&tp->t_rawq);
567                                 if (c == -1)
568                                         goto endcase;
569                         } while (c != ' ' && c != '\t' &&
570                             (!ISSET(lflag, ALTWERASE) || ISALPHA(c) == ctype));
571                         (void)putc(c, &tp->t_rawq);
572                         goto endcase;
573                 }
574                 /*
575                  * reprint line (^R)
576                  */
577                 if (CCEQ(cc[VREPRINT], c) && ISSET(lflag, IEXTEN)) {
578                         ttyretype(tp);
579                         goto endcase;
580                 }
581                 /*
582                  * ^T - kernel info and generate SIGINFO
583                  */
584                 if (CCEQ(cc[VSTATUS], c) && ISSET(lflag, IEXTEN)) {
585                         if (ISSET(lflag, ISIG))
586                                 pgsignal(tp->t_pgrp, SIGINFO, 1);
587                         if (!ISSET(lflag, NOKERNINFO))
588                                 ttyinfo(tp);
589                         goto endcase;
590                 }
591                 if (CCEQ(cc[VCHECKPT], c) && ISSET(lflag, IEXTEN)) {
592                         if (ISSET(lflag, ISIG))
593                                 pgsignal(tp->t_pgrp, SIGCKPT, 1);
594                         goto endcase;
595                 }
596         }
597         /*
598          * Check for input buffer overflow
599          */
600         if (tp->t_rawq.c_cc + tp->t_canq.c_cc >= MAX_INPUT) {
601 input_overflow:
602                 if (ISSET(iflag, IMAXBEL)) {
603                         if (tp->t_outq.c_cc < tp->t_ohiwat)
604                                 (void)ttyoutput(CTRL('g'), tp);
605                 }
606                 goto endcase;
607         }
608
609         if (   c == 0377 && ISSET(iflag, PARMRK) && !ISSET(iflag, ISTRIP)
610              && ISSET(iflag, IGNBRK|IGNPAR) != (IGNBRK|IGNPAR))
611                 (void)putc(0377 | TTY_QUOTE, &tp->t_rawq);
612
613         /*
614          * Put data char in q for user and
615          * wakeup on seeing a line delimiter.
616          */
617         if (putc(c, &tp->t_rawq) >= 0) {
618                 if (!ISSET(lflag, ICANON)) {
619                         ttwakeup(tp);
620                         ttyecho(c, tp);
621                         goto endcase;
622                 }
623                 if (TTBREAKC(c, lflag)) {
624                         tp->t_rocount = 0;
625                         catq(&tp->t_rawq, &tp->t_canq);
626                         ttwakeup(tp);
627                 } else if (tp->t_rocount++ == 0)
628                         tp->t_rocol = tp->t_column;
629                 if (ISSET(tp->t_state, TS_ERASE)) {
630                         /*
631                          * end of prterase \.../
632                          */
633                         CLR(tp->t_state, TS_ERASE);
634                         (void)ttyoutput('/', tp);
635                 }
636                 i = tp->t_column;
637                 ttyecho(c, tp);
638                 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ECHO)) {
639                         /*
640                          * Place the cursor over the '^' of the ^D.
641                          */
642                         i = imin(2, tp->t_column - i);
643                         while (i > 0) {
644                                 (void)ttyoutput('\b', tp);
645                                 i--;
646                         }
647                 }
648         }
649 endcase:
650         /*
651          * IXANY means allow any character to restart output.
652          */
653         if (ISSET(tp->t_state, TS_TTSTOP) &&
654             !ISSET(iflag, IXANY) && cc[VSTART] != cc[VSTOP])
655                 return (0);
656 restartoutput:
657         CLR(tp->t_lflag, FLUSHO);
658         CLR(tp->t_state, TS_TTSTOP);
659 startoutput:
660         return (ttstart(tp));
661 }
662
663 /*
664  * Output a single character on a tty, doing output processing
665  * as needed (expanding tabs, newline processing, etc.).
666  * Returns < 0 if succeeds, otherwise returns char to resend.
667  * Must be recursive.
668  */
669 static int
670 ttyoutput(c, tp)
671         int c;
672         struct tty *tp;
673 {
674         tcflag_t oflag;
675         int col, s;
676
677         oflag = tp->t_oflag;
678         if (!ISSET(oflag, OPOST)) {
679                 if (ISSET(tp->t_lflag, FLUSHO))
680                         return (-1);
681                 if (putc(c, &tp->t_outq))
682                         return (c);
683                 tk_nout++;
684                 tp->t_outcc++;
685                 return (-1);
686         }
687         /*
688          * Do tab expansion if OXTABS is set.  Special case if we external
689          * processing, we don't do the tab expansion because we'll probably
690          * get it wrong.  If tab expansion needs to be done, let it happen
691          * externally.
692          */
693         CLR(c, ~TTY_CHARMASK);
694         if (c == '\t' &&
695             ISSET(oflag, OXTABS) && !ISSET(tp->t_lflag, EXTPROC)) {
696                 c = 8 - (tp->t_column & 7);
697                 if (!ISSET(tp->t_lflag, FLUSHO)) {
698                         s = spltty();           /* Don't interrupt tabs. */
699                         c -= b_to_q("        ", c, &tp->t_outq);
700                         tk_nout += c;
701                         tp->t_outcc += c;
702                         splx(s);
703                 }
704                 tp->t_column += c;
705                 return (c ? -1 : '\t');
706         }
707         if (c == CEOT && ISSET(oflag, ONOEOT))
708                 return (-1);
709
710         /*
711          * Newline translation: if ONLCR is set,
712          * translate newline into "\r\n".
713          */
714         if (c == '\n' && ISSET(tp->t_oflag, ONLCR)) {
715                 tk_nout++;
716                 tp->t_outcc++;
717                 if (!ISSET(tp->t_lflag, FLUSHO) && putc('\r', &tp->t_outq))
718                         return (c);
719         }
720         /* If OCRNL is set, translate "\r" into "\n". */
721         else if (c == '\r' && ISSET(tp->t_oflag, OCRNL))
722                 c = '\n';
723         /* If ONOCR is set, don't transmit CRs when on column 0. */
724         else if (c == '\r' && ISSET(tp->t_oflag, ONOCR) && tp->t_column == 0)
725                 return (-1);
726
727         tk_nout++;
728         tp->t_outcc++;
729         if (!ISSET(tp->t_lflag, FLUSHO) && putc(c, &tp->t_outq))
730                 return (c);
731
732         col = tp->t_column;
733         switch (CCLASS(c)) {
734         case BACKSPACE:
735                 if (col > 0)
736                         --col;
737                 break;
738         case CONTROL:
739                 break;
740         case NEWLINE:
741                 if (ISSET(tp->t_oflag, ONLCR | ONLRET))
742                         col = 0;
743                 break;
744         case RETURN:
745                 col = 0;
746                 break;
747         case ORDINARY:
748                 ++col;
749                 break;
750         case TAB:
751                 col = (col + 8) & ~7;
752                 break;
753         }
754         tp->t_column = col;
755         return (-1);
756 }
757
758 /*
759  * Ioctls for all tty devices.  Called after line-discipline specific ioctl
760  * has been called to do discipline-specific functions and/or reject any
761  * of these ioctl commands.
762  */
763 /* ARGSUSED */
764 int
765 ttioctl(struct tty *tp, u_long cmd, void *data, int flag)
766 {
767         struct thread *td = curthread;
768         struct proc *p = td->td_proc;
769         int s, error;
770
771         KKASSERT(p);
772
773         /* If the ioctl involves modification, hang if in the background. */
774         switch (cmd) {
775         case  TIOCCBRK:
776         case  TIOCCONS:
777         case  TIOCDRAIN:
778         case  TIOCEXCL:
779         case  TIOCFLUSH:
780 #ifdef TIOCHPCL
781         case  TIOCHPCL:
782 #endif
783         case  TIOCNXCL:
784         case  TIOCSBRK:
785         case  TIOCSCTTY:
786         case  TIOCSDRAINWAIT:
787         case  TIOCSETA:
788         case  TIOCSETAF:
789         case  TIOCSETAW:
790         case  TIOCSETD:
791         case  TIOCSPGRP:
792         case  TIOCSTART:
793         case  TIOCSTAT:
794         case  TIOCSTI:
795         case  TIOCSTOP:
796         case  TIOCSWINSZ:
797 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
798         case  TIOCLBIC:
799         case  TIOCLBIS:
800         case  TIOCLSET:
801         case  TIOCSETC:
802         case OTIOCSETD:
803         case  TIOCSETN:
804         case  TIOCSETP:
805         case  TIOCSLTC:
806 #endif
807                 while (isbackground(p, tp) && !(p->p_flag & P_PPWAIT) &&
808                     !SIGISMEMBER(p->p_sigignore, SIGTTOU) &&
809                     !SIGISMEMBER(p->p_sigmask, SIGTTOU)) {
810                         if (p->p_pgrp->pg_jobc == 0)
811                                 return (EIO);
812                         pgsignal(p->p_pgrp, SIGTTOU, 1);
813                         error = ttysleep(tp, &lbolt, PCATCH, "ttybg1",
814                                          0);
815                         if (error)
816                                 return (error);
817                 }
818                 break;
819         }
820
821         switch (cmd) {                  /* Process the ioctl. */
822         case FIOASYNC:                  /* set/clear async i/o */
823                 s = spltty();
824                 if (*(int *)data)
825                         SET(tp->t_state, TS_ASYNC);
826                 else
827                         CLR(tp->t_state, TS_ASYNC);
828                 splx(s);
829                 break;
830         case FIONBIO:                   /* set/clear non-blocking i/o */
831                 break;                  /* XXX: delete. */
832         case FIONREAD:                  /* get # bytes to read */
833                 s = spltty();
834                 *(int *)data = ttnread(tp);
835                 splx(s);
836                 break;
837
838         case FIOSETOWN:
839                 /*
840                  * Policy -- Don't allow FIOSETOWN on someone else's 
841                  *           controlling tty
842                  */
843                 if (tp->t_session != NULL && !isctty(p, tp))
844                         return (ENOTTY);
845
846                 error = fsetown(*(int *)data, &tp->t_sigio);
847                 if (error)
848                         return (error);
849                 break;
850         case FIOGETOWN:
851                 if (tp->t_session != NULL && !isctty(p, tp))
852                         return (ENOTTY);
853                 *(int *)data = fgetown(tp->t_sigio);
854                 break;
855
856         case TIOCEXCL:                  /* set exclusive use of tty */
857                 s = spltty();
858                 SET(tp->t_state, TS_XCLUDE);
859                 splx(s);
860                 break;
861         case TIOCFLUSH: {               /* flush buffers */
862                 int flags = *(int *)data;
863
864                 if (flags == 0)
865                         flags = FREAD | FWRITE;
866                 else
867                         flags &= FREAD | FWRITE;
868                 ttyflush(tp, flags);
869                 break;
870         }
871         case TIOCCONS:                  /* become virtual console */
872                 if (*(int *)data) {
873                         if (constty && constty != tp &&
874                             ISSET(constty->t_state, TS_CONNECTED))
875                                 return (EBUSY);
876 #ifndef UCONSOLE
877                         if ((error = suser(td)) != 0)
878                                 return (error);
879 #endif
880                         constty = tp;
881                 } else if (tp == constty)
882                         constty = NULL;
883                 break;
884         case TIOCDRAIN:                 /* wait till output drained */
885                 error = ttywait(tp);
886                 if (error)
887                         return (error);
888                 break;
889         case TIOCGETA: {                /* get termios struct */
890                 struct termios *t = (struct termios *)data;
891
892                 bcopy(&tp->t_termios, t, sizeof(struct termios));
893                 break;
894         }
895         case TIOCGETD:                  /* get line discipline */
896                 *(int *)data = tp->t_line;
897                 break;
898         case TIOCGWINSZ:                /* get window size */
899                 *(struct winsize *)data = tp->t_winsize;
900                 break;
901         case TIOCGPGRP:                 /* get pgrp of tty */
902                 if (!isctty(p, tp))
903                         return (ENOTTY);
904                 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : NO_PID;
905                 break;
906 #ifdef TIOCHPCL
907         case TIOCHPCL:                  /* hang up on last close */
908                 s = spltty();
909                 SET(tp->t_cflag, HUPCL);
910                 splx(s);
911                 break;
912 #endif
913         case TIOCNXCL:                  /* reset exclusive use of tty */
914                 s = spltty();
915                 CLR(tp->t_state, TS_XCLUDE);
916                 splx(s);
917                 break;
918         case TIOCOUTQ:                  /* output queue size */
919                 *(int *)data = tp->t_outq.c_cc;
920                 break;
921         case TIOCSETA:                  /* set termios struct */
922         case TIOCSETAW:                 /* drain output, set */
923         case TIOCSETAF: {               /* drn out, fls in, set */
924                 struct termios *t = (struct termios *)data;
925
926                 if (t->c_ispeed == 0)
927                         t->c_ispeed = t->c_ospeed;
928                 if (t->c_ispeed == 0)
929                         t->c_ispeed = tp->t_ospeed;
930                 if (t->c_ispeed == 0)
931                         return (EINVAL);
932                 s = spltty();
933                 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
934                         error = ttywait(tp);
935                         if (error) {
936                                 splx(s);
937                                 return (error);
938                         }
939                         if (cmd == TIOCSETAF)
940                                 ttyflush(tp, FREAD);
941                 }
942                 if (!ISSET(t->c_cflag, CIGNORE)) {
943                         /*
944                          * Set device hardware.
945                          */
946                         if (tp->t_param && (error = (*tp->t_param)(tp, t))) {
947                                 splx(s);
948                                 return (error);
949                         }
950                         if (ISSET(t->c_cflag, CLOCAL) &&
951                             !ISSET(tp->t_cflag, CLOCAL)) {
952                                 /*
953                                  * XXX disconnections would be too hard to
954                                  * get rid of without this kludge.  The only
955                                  * way to get rid of controlling terminals
956                                  * is to exit from the session leader.
957                                  */
958                                 CLR(tp->t_state, TS_ZOMBIE);
959
960                                 wakeup(TSA_CARR_ON(tp));
961                                 ttwakeup(tp);
962                                 ttwwakeup(tp);
963                         }
964                         if ((ISSET(tp->t_state, TS_CARR_ON) ||
965                              ISSET(t->c_cflag, CLOCAL)) &&
966                             !ISSET(tp->t_state, TS_ZOMBIE))
967                                 SET(tp->t_state, TS_CONNECTED);
968                         else
969                                 CLR(tp->t_state, TS_CONNECTED);
970                         tp->t_cflag = t->c_cflag;
971                         tp->t_ispeed = t->c_ispeed;
972                         if (t->c_ospeed != 0)
973                                 tp->t_ospeed = t->c_ospeed;
974                         ttsetwater(tp);
975                 }
976                 if (ISSET(t->c_lflag, ICANON) != ISSET(tp->t_lflag, ICANON) &&
977                     cmd != TIOCSETAF) {
978                         if (ISSET(t->c_lflag, ICANON))
979                                 SET(tp->t_lflag, PENDIN);
980                         else {
981                                 /*
982                                  * XXX we really shouldn't allow toggling
983                                  * ICANON while we're in a non-termios line
984                                  * discipline.  Now we have to worry about
985                                  * panicing for a null queue.
986                                  */
987                                 if (tp->t_canq.c_cbreserved > 0 &&
988                                     tp->t_rawq.c_cbreserved > 0) {
989                                         catq(&tp->t_rawq, &tp->t_canq);
990                                         /*
991                                          * XXX the queue limits may be
992                                          * different, so the old queue
993                                          * swapping method no longer works.
994                                          */
995                                         catq(&tp->t_canq, &tp->t_rawq);
996                                 }
997                                 CLR(tp->t_lflag, PENDIN);
998                         }
999                         ttwakeup(tp);
1000                 }
1001                 tp->t_iflag = t->c_iflag;
1002                 tp->t_oflag = t->c_oflag;
1003                 /*
1004                  * Make the EXTPROC bit read only.
1005                  */
1006                 if (ISSET(tp->t_lflag, EXTPROC))
1007                         SET(t->c_lflag, EXTPROC);
1008                 else
1009                         CLR(t->c_lflag, EXTPROC);
1010                 tp->t_lflag = t->c_lflag | ISSET(tp->t_lflag, PENDIN);
1011                 if (t->c_cc[VMIN] != tp->t_cc[VMIN] ||
1012                     t->c_cc[VTIME] != tp->t_cc[VTIME])
1013                         ttwakeup(tp);
1014                 bcopy(t->c_cc, tp->t_cc, sizeof(t->c_cc));
1015                 splx(s);
1016                 break;
1017         }
1018         case TIOCSETD: {                /* set line discipline */
1019                 int t = *(int *)data;
1020                 dev_t device = tp->t_dev;
1021
1022                 if ((u_int)t >= nlinesw)
1023                         return (ENXIO);
1024                 if (t != tp->t_line) {
1025                         s = spltty();
1026                         (*linesw[tp->t_line].l_close)(tp, flag);
1027                         error = (*linesw[t].l_open)(device, tp);
1028                         if (error) {
1029                                 (void)(*linesw[tp->t_line].l_open)(device, tp);
1030                                 splx(s);
1031                                 return (error);
1032                         }
1033                         tp->t_line = t;
1034                         splx(s);
1035                 }
1036                 break;
1037         }
1038         case TIOCSTART:                 /* start output, like ^Q */
1039                 s = spltty();
1040                 if (ISSET(tp->t_state, TS_TTSTOP) ||
1041                     ISSET(tp->t_lflag, FLUSHO)) {
1042                         CLR(tp->t_lflag, FLUSHO);
1043                         CLR(tp->t_state, TS_TTSTOP);
1044                         ttstart(tp);
1045                 }
1046                 splx(s);
1047                 break;
1048         case TIOCSTI:                   /* simulate terminal input */
1049                 if ((flag & FREAD) == 0 && suser(td))
1050                         return (EPERM);
1051                 if (!isctty(p, tp) && suser(td))
1052                         return (EACCES);
1053                 s = spltty();
1054                 (*linesw[tp->t_line].l_rint)(*(u_char *)data, tp);
1055                 splx(s);
1056                 break;
1057         case TIOCSTOP:                  /* stop output, like ^S */
1058                 s = spltty();
1059                 if (!ISSET(tp->t_state, TS_TTSTOP)) {
1060                         SET(tp->t_state, TS_TTSTOP);
1061                         (*tp->t_stop)(tp, 0);
1062                 }
1063                 splx(s);
1064                 break;
1065         case TIOCSCTTY:                 /* become controlling tty */
1066                 /* Session ctty vnode pointer set in vnode layer. */
1067                 if (!SESS_LEADER(p) ||
1068                     ((p->p_session->s_ttyvp || tp->t_session) &&
1069                     (tp->t_session != p->p_session)))
1070                         return (EPERM);
1071                 tp->t_session = p->p_session;
1072                 tp->t_pgrp = p->p_pgrp;
1073                 p->p_session->s_ttyp = tp;
1074                 p->p_flag |= P_CONTROLT;
1075                 break;
1076         case TIOCSPGRP: {               /* set pgrp of tty */
1077                 struct pgrp *pgrp = pgfind(*(int *)data);
1078
1079                 if (!isctty(p, tp))
1080                         return (ENOTTY);
1081                 else if (pgrp == NULL || pgrp->pg_session != p->p_session)
1082                         return (EPERM);
1083                 tp->t_pgrp = pgrp;
1084                 break;
1085         }
1086         case TIOCSTAT:                  /* simulate control-T */
1087                 s = spltty();
1088                 ttyinfo(tp);
1089                 splx(s);
1090                 break;
1091         case TIOCSWINSZ:                /* set window size */
1092                 if (bcmp((caddr_t)&tp->t_winsize, data,
1093                     sizeof (struct winsize))) {
1094                         tp->t_winsize = *(struct winsize *)data;
1095                         pgsignal(tp->t_pgrp, SIGWINCH, 1);
1096                 }
1097                 break;
1098         case TIOCSDRAINWAIT:
1099                 error = suser(td);
1100                 if (error)
1101                         return (error);
1102                 tp->t_timeout = *(int *)data * hz;
1103                 wakeup(TSA_OCOMPLETE(tp));
1104                 wakeup(TSA_OLOWAT(tp));
1105                 break;
1106         case TIOCGDRAINWAIT:
1107                 *(int *)data = tp->t_timeout / hz;
1108                 break;
1109         default:
1110 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1111                 return (ttcompat(tp, cmd, data, flag));
1112 #else
1113                 return (ENOIOCTL);
1114 #endif
1115         }
1116         return (0);
1117 }
1118
1119 int
1120 ttypoll(dev, events, td)
1121         dev_t dev;
1122         int events;
1123         struct thread *td;
1124 {
1125         int s;
1126         int revents = 0;
1127         struct tty *tp;
1128
1129         tp = dev->si_tty;
1130         if (tp == NULL) /* XXX used to return ENXIO, but that means true! */
1131                 return ((events & (POLLIN | POLLOUT | POLLRDNORM | POLLWRNORM))
1132                         | POLLHUP);
1133
1134         s = spltty();
1135         if (events & (POLLIN | POLLRDNORM)) {
1136                 if (ttnread(tp) > 0 || ISSET(tp->t_state, TS_ZOMBIE))
1137                         revents |= events & (POLLIN | POLLRDNORM);
1138                 else
1139                         selrecord(td, &tp->t_rsel);
1140         }
1141         if (events & (POLLOUT | POLLWRNORM)) {
1142                 if ((tp->t_outq.c_cc <= tp->t_olowat &&
1143                      ISSET(tp->t_state, TS_CONNECTED))
1144                     || ISSET(tp->t_state, TS_ZOMBIE))
1145                         revents |= events & (POLLOUT | POLLWRNORM);
1146                 else
1147                         selrecord(td, &tp->t_wsel);
1148         }
1149         splx(s);
1150         return (revents);
1151 }
1152
1153 static struct filterops ttyread_filtops =
1154         { 1, NULL, filt_ttyrdetach, filt_ttyread };
1155 static struct filterops ttywrite_filtops =
1156         { 1, NULL, filt_ttywdetach, filt_ttywrite };
1157
1158 int
1159 ttykqfilter(dev, kn)
1160         dev_t dev;
1161         struct knote *kn;
1162 {
1163         struct tty *tp = dev->si_tty;
1164         struct klist *klist;
1165         int s;
1166
1167         switch (kn->kn_filter) {
1168         case EVFILT_READ:
1169                 klist = &tp->t_rsel.si_note;
1170                 kn->kn_fop = &ttyread_filtops;
1171                 break;
1172         case EVFILT_WRITE:
1173                 klist = &tp->t_wsel.si_note;
1174                 kn->kn_fop = &ttywrite_filtops;
1175                 break;
1176         default:
1177                 return (1);
1178         }
1179
1180         kn->kn_hook = (caddr_t)dev;
1181
1182         s = spltty();
1183         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
1184         splx(s);
1185
1186         return (0);
1187 }
1188
1189 static void
1190 filt_ttyrdetach(struct knote *kn)
1191 {
1192         struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1193         int s = spltty();
1194
1195         SLIST_REMOVE(&tp->t_rsel.si_note, kn, knote, kn_selnext);
1196         splx(s);
1197 }
1198
1199 static int
1200 filt_ttyread(struct knote *kn, long hint)
1201 {
1202         struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1203
1204         kn->kn_data = ttnread(tp);
1205         if (ISSET(tp->t_state, TS_ZOMBIE)) {
1206                 kn->kn_flags |= EV_EOF;
1207                 return (1);
1208         }
1209         return (kn->kn_data > 0);
1210 }
1211
1212 static void
1213 filt_ttywdetach(struct knote *kn)
1214 {
1215         struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1216         int s = spltty();
1217
1218         SLIST_REMOVE(&tp->t_wsel.si_note, kn, knote, kn_selnext);
1219         splx(s);
1220 }
1221
1222 static int
1223 filt_ttywrite(kn, hint)
1224         struct knote *kn;
1225         long hint;
1226 {
1227         struct tty *tp = ((dev_t)kn->kn_hook)->si_tty;
1228
1229         kn->kn_data = tp->t_outq.c_cc;
1230         if (ISSET(tp->t_state, TS_ZOMBIE))
1231                 return (1);
1232         return (kn->kn_data <= tp->t_olowat &&
1233             ISSET(tp->t_state, TS_CONNECTED));
1234 }
1235
1236 /*
1237  * Must be called at spltty().
1238  */
1239 static int
1240 ttnread(tp)
1241         struct tty *tp;
1242 {
1243         int nread;
1244
1245         if (ISSET(tp->t_lflag, PENDIN))
1246                 ttypend(tp);
1247         nread = tp->t_canq.c_cc;
1248         if (!ISSET(tp->t_lflag, ICANON)) {
1249                 nread += tp->t_rawq.c_cc;
1250                 if (nread < tp->t_cc[VMIN] && tp->t_cc[VTIME] == 0)
1251                         nread = 0;
1252         }
1253         return (nread);
1254 }
1255
1256 /*
1257  * Wait for output to drain.
1258  */
1259 int
1260 ttywait(tp)
1261         struct tty *tp;
1262 {
1263         int error, s;
1264
1265         error = 0;
1266         s = spltty();
1267         while ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1268                ISSET(tp->t_state, TS_CONNECTED) && tp->t_oproc) {
1269                 (*tp->t_oproc)(tp);
1270                 if ((tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)) &&
1271                     ISSET(tp->t_state, TS_CONNECTED)) {
1272                         SET(tp->t_state, TS_SO_OCOMPLETE);
1273                         error = ttysleep(tp, TSA_OCOMPLETE(tp),
1274                                          PCATCH, "ttywai",
1275                                          tp->t_timeout);
1276                         if (error) {
1277                                 if (error == EWOULDBLOCK)
1278                                         error = EIO;
1279                                 break;
1280                         }
1281                 } else
1282                         break;
1283         }
1284         if (!error && (tp->t_outq.c_cc || ISSET(tp->t_state, TS_BUSY)))
1285                 error = EIO;
1286         splx(s);
1287         return (error);
1288 }
1289
1290 /*
1291  * Flush if successfully wait.
1292  */
1293 static int
1294 ttywflush(tp)
1295         struct tty *tp;
1296 {
1297         int error;
1298
1299         if ((error = ttywait(tp)) == 0)
1300                 ttyflush(tp, FREAD);
1301         return (error);
1302 }
1303
1304 /*
1305  * Flush tty read and/or write queues, notifying anyone waiting.
1306  */
1307 void
1308 ttyflush(tp, rw)
1309         struct tty *tp;
1310         int rw;
1311 {
1312         int s;
1313
1314         s = spltty();
1315 #if 0
1316 again:
1317 #endif
1318         if (rw & FWRITE) {
1319                 FLUSHQ(&tp->t_outq);
1320                 CLR(tp->t_state, TS_TTSTOP);
1321         }
1322         (*tp->t_stop)(tp, rw);
1323         if (rw & FREAD) {
1324                 FLUSHQ(&tp->t_canq);
1325                 FLUSHQ(&tp->t_rawq);
1326                 CLR(tp->t_lflag, PENDIN);
1327                 tp->t_rocount = 0;
1328                 tp->t_rocol = 0;
1329                 CLR(tp->t_state, TS_LOCAL);
1330                 ttwakeup(tp);
1331                 if (ISSET(tp->t_state, TS_TBLOCK)) {
1332                         if (rw & FWRITE)
1333                                 FLUSHQ(&tp->t_outq);
1334                         ttyunblock(tp);
1335
1336                         /*
1337                          * Don't let leave any state that might clobber the
1338                          * next line discipline (although we should do more
1339                          * to send the START char).  Not clearing the state
1340                          * may have caused the "putc to a clist with no
1341                          * reserved cblocks" panic/printf.
1342                          */
1343                         CLR(tp->t_state, TS_TBLOCK);
1344
1345 #if 0 /* forget it, sleeping isn't always safe and we don't know when it is */
1346                         if (ISSET(tp->t_iflag, IXOFF)) {
1347                                 /*
1348                                  * XXX wait a bit in the hope that the stop
1349                                  * character (if any) will go out.  Waiting
1350                                  * isn't good since it allows races.  This
1351                                  * will be fixed when the stop character is
1352                                  * put in a special queue.  Don't bother with
1353                                  * the checks in ttywait() since the timeout
1354                                  * will save us.
1355                                  */
1356                                 SET(tp->t_state, TS_SO_OCOMPLETE);
1357                                 ttysleep(tp, TSA_OCOMPLETE(tp), 0,
1358                                          "ttyfls", hz / 10);
1359                                 /*
1360                                  * Don't try sending the stop character again.
1361                                  */
1362                                 CLR(tp->t_state, TS_TBLOCK);
1363                                 goto again;
1364                         }
1365 #endif
1366                 }
1367         }
1368         if (rw & FWRITE) {
1369                 FLUSHQ(&tp->t_outq);
1370                 ttwwakeup(tp);
1371         }
1372         splx(s);
1373 }
1374
1375 /*
1376  * Copy in the default termios characters.
1377  */
1378 void
1379 termioschars(t)
1380         struct termios *t;
1381 {
1382
1383         bcopy(ttydefchars, t->c_cc, sizeof t->c_cc);
1384 }
1385
1386 /*
1387  * Old interface.
1388  */
1389 void
1390 ttychars(tp)
1391         struct tty *tp;
1392 {
1393
1394         termioschars(&tp->t_termios);
1395 }
1396
1397 /*
1398  * Handle input high water.  Send stop character for the IXOFF case.  Turn
1399  * on our input flow control bit and propagate the changes to the driver.
1400  * XXX the stop character should be put in a special high priority queue.
1401  */
1402 void
1403 ttyblock(tp)
1404         struct tty *tp;
1405 {
1406
1407         SET(tp->t_state, TS_TBLOCK);
1408         if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTOP] != _POSIX_VDISABLE &&
1409             putc(tp->t_cc[VSTOP], &tp->t_outq) != 0)
1410                 CLR(tp->t_state, TS_TBLOCK);    /* try again later */
1411         ttstart(tp);
1412 }
1413
1414 /*
1415  * Handle input low water.  Send start character for the IXOFF case.  Turn
1416  * off our input flow control bit and propagate the changes to the driver.
1417  * XXX the start character should be put in a special high priority queue.
1418  */
1419 static void
1420 ttyunblock(tp)
1421         struct tty *tp;
1422 {
1423
1424         CLR(tp->t_state, TS_TBLOCK);
1425         if (ISSET(tp->t_iflag, IXOFF) && tp->t_cc[VSTART] != _POSIX_VDISABLE &&
1426             putc(tp->t_cc[VSTART], &tp->t_outq) != 0)
1427                 SET(tp->t_state, TS_TBLOCK);    /* try again later */
1428         ttstart(tp);
1429 }
1430
1431 #ifdef notyet
1432 /* Not used by any current (i386) drivers. */
1433 /*
1434  * Restart after an inter-char delay.
1435  */
1436 void
1437 ttrstrt(tp_arg)
1438         void *tp_arg;
1439 {
1440         struct tty *tp;
1441         int s;
1442
1443         KASSERT(tp_arg != NULL, ("ttrstrt"));
1444
1445         tp = tp_arg;
1446         s = spltty();
1447
1448         CLR(tp->t_state, TS_TIMEOUT);
1449         ttstart(tp);
1450
1451         splx(s);
1452 }
1453 #endif
1454
1455 int
1456 ttstart(tp)
1457         struct tty *tp;
1458 {
1459
1460         if (tp->t_oproc != NULL)        /* XXX: Kludge for pty. */
1461                 (*tp->t_oproc)(tp);
1462         return (0);
1463 }
1464
1465 /*
1466  * "close" a line discipline
1467  */
1468 int
1469 ttylclose(tp, flag)
1470         struct tty *tp;
1471         int flag;
1472 {
1473
1474         if (flag & FNONBLOCK || ttywflush(tp))
1475                 ttyflush(tp, FREAD | FWRITE);
1476         return (0);
1477 }
1478
1479 /*
1480  * Handle modem control transition on a tty.
1481  * Flag indicates new state of carrier.
1482  * Returns 0 if the line should be turned off, otherwise 1.
1483  */
1484 int
1485 ttymodem(tp, flag)
1486         struct tty *tp;
1487         int flag;
1488 {
1489
1490         if (ISSET(tp->t_state, TS_CARR_ON) && ISSET(tp->t_cflag, MDMBUF)) {
1491                 /*
1492                  * MDMBUF: do flow control according to carrier flag
1493                  * XXX TS_CAR_OFLOW doesn't do anything yet.  TS_TTSTOP
1494                  * works if IXON and IXANY are clear.
1495                  */
1496                 if (flag) {
1497                         CLR(tp->t_state, TS_CAR_OFLOW);
1498                         CLR(tp->t_state, TS_TTSTOP);
1499                         ttstart(tp);
1500                 } else if (!ISSET(tp->t_state, TS_CAR_OFLOW)) {
1501                         SET(tp->t_state, TS_CAR_OFLOW);
1502                         SET(tp->t_state, TS_TTSTOP);
1503                         (*tp->t_stop)(tp, 0);
1504                 }
1505         } else if (flag == 0) {
1506                 /*
1507                  * Lost carrier.
1508                  */
1509                 CLR(tp->t_state, TS_CARR_ON);
1510                 if (ISSET(tp->t_state, TS_ISOPEN) &&
1511                     !ISSET(tp->t_cflag, CLOCAL)) {
1512                         SET(tp->t_state, TS_ZOMBIE);
1513                         CLR(tp->t_state, TS_CONNECTED);
1514                         if (tp->t_session && tp->t_session->s_leader)
1515                                 psignal(tp->t_session->s_leader, SIGHUP);
1516                         ttyflush(tp, FREAD | FWRITE);
1517                         return (0);
1518                 }
1519         } else {
1520                 /*
1521                  * Carrier now on.
1522                  */
1523                 SET(tp->t_state, TS_CARR_ON);
1524                 if (!ISSET(tp->t_state, TS_ZOMBIE))
1525                         SET(tp->t_state, TS_CONNECTED);
1526                 wakeup(TSA_CARR_ON(tp));
1527                 ttwakeup(tp);
1528                 ttwwakeup(tp);
1529         }
1530         return (1);
1531 }
1532
1533 /*
1534  * Reinput pending characters after state switch
1535  * call at spltty().
1536  */
1537 static void
1538 ttypend(tp)
1539         struct tty *tp;
1540 {
1541         struct clist tq;
1542         int c;
1543
1544         CLR(tp->t_lflag, PENDIN);
1545         SET(tp->t_state, TS_TYPEN);
1546         /*
1547          * XXX this assumes too much about clist internals.  It may even
1548          * fail if the cblock slush pool is empty.  We can't allocate more
1549          * cblocks here because we are called from an interrupt handler
1550          * and clist_alloc_cblocks() can wait.
1551          */
1552         tq = tp->t_rawq;
1553         bzero(&tp->t_rawq, sizeof tp->t_rawq);
1554         tp->t_rawq.c_cbmax = tq.c_cbmax;
1555         tp->t_rawq.c_cbreserved = tq.c_cbreserved;
1556         while ((c = getc(&tq)) >= 0)
1557                 ttyinput(c, tp);
1558         CLR(tp->t_state, TS_TYPEN);
1559 }
1560
1561 /*
1562  * Process a read call on a tty device.
1563  */
1564 int
1565 ttread(tp, uio, flag)
1566         struct tty *tp;
1567         struct uio *uio;
1568         int flag;
1569 {
1570         struct clist *qp;
1571         int c;
1572         tcflag_t lflag;
1573         cc_t *cc = tp->t_cc;
1574         struct proc *p = curproc;
1575         int s, first, error = 0;
1576         int has_stime = 0, last_cc = 0;
1577         long slp = 0;           /* XXX this should be renamed `timo'. */
1578         struct timeval stime;
1579
1580 loop:
1581         s = spltty();
1582         lflag = tp->t_lflag;
1583         /*
1584          * take pending input first
1585          */
1586         if (ISSET(lflag, PENDIN)) {
1587                 ttypend(tp);
1588                 splx(s);        /* reduce latency */
1589                 s = spltty();
1590                 lflag = tp->t_lflag;    /* XXX ttypend() clobbers it */
1591         }
1592
1593         /*
1594          * Hang process if it's in the background.
1595          */
1596         if (isbackground(p, tp)) {
1597                 splx(s);
1598                 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
1599                     SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
1600                     (p->p_flag & P_PPWAIT) || p->p_pgrp->pg_jobc == 0)
1601                         return (EIO);
1602                 pgsignal(p->p_pgrp, SIGTTIN, 1);
1603                 error = ttysleep(tp, &lbolt, PCATCH, "ttybg2", 0);
1604                 if (error)
1605                         return (error);
1606                 goto loop;
1607         }
1608
1609         if (ISSET(tp->t_state, TS_ZOMBIE)) {
1610                 splx(s);
1611                 return (0);     /* EOF */
1612         }
1613
1614         /*
1615          * If canonical, use the canonical queue,
1616          * else use the raw queue.
1617          *
1618          * (should get rid of clists...)
1619          */
1620         qp = ISSET(lflag, ICANON) ? &tp->t_canq : &tp->t_rawq;
1621
1622         if (flag & IO_NDELAY) {
1623                 if (qp->c_cc > 0)
1624                         goto read;
1625                 if (!ISSET(lflag, ICANON) && cc[VMIN] == 0) {
1626                         splx(s);
1627                         return (0);
1628                 }
1629                 splx(s);
1630                 return (EWOULDBLOCK);
1631         }
1632         if (!ISSET(lflag, ICANON)) {
1633                 int m = cc[VMIN];
1634                 long t = cc[VTIME];
1635                 struct timeval timecopy;
1636
1637                 /*
1638                  * Check each of the four combinations.
1639                  * (m > 0 && t == 0) is the normal read case.
1640                  * It should be fairly efficient, so we check that and its
1641                  * companion case (m == 0 && t == 0) first.
1642                  * For the other two cases, we compute the target sleep time
1643                  * into slp.
1644                  */
1645                 if (t == 0) {
1646                         if (qp->c_cc < m)
1647                                 goto sleep;
1648                         if (qp->c_cc > 0)
1649                                 goto read;
1650
1651                         /* m, t and qp->c_cc are all 0.  0 is enough input. */
1652                         splx(s);
1653                         return (0);
1654                 }
1655                 t *= 100000;            /* time in us */
1656 #define diff(t1, t2) (((t1).tv_sec - (t2).tv_sec) * 1000000 + \
1657                          ((t1).tv_usec - (t2).tv_usec))
1658                 if (m > 0) {
1659                         if (qp->c_cc <= 0)
1660                                 goto sleep;
1661                         if (qp->c_cc >= m)
1662                                 goto read;
1663                         getmicrotime(&timecopy);
1664                         if (!has_stime) {
1665                                 /* first character, start timer */
1666                                 has_stime = 1;
1667                                 stime = timecopy;
1668                                 slp = t;
1669                         } else if (qp->c_cc > last_cc) {
1670                                 /* got a character, restart timer */
1671                                 stime = timecopy;
1672                                 slp = t;
1673                         } else {
1674                                 /* nothing, check expiration */
1675                                 slp = t - diff(timecopy, stime);
1676                                 if (slp <= 0)
1677                                         goto read;
1678                         }
1679                         last_cc = qp->c_cc;
1680                 } else {        /* m == 0 */
1681                         if (qp->c_cc > 0)
1682                                 goto read;
1683                         getmicrotime(&timecopy);
1684                         if (!has_stime) {
1685                                 has_stime = 1;
1686                                 stime = timecopy;
1687                                 slp = t;
1688                         } else {
1689                                 slp = t - diff(timecopy, stime);
1690                                 if (slp <= 0) {
1691                                         /* Timed out, but 0 is enough input. */
1692                                         splx(s);
1693                                         return (0);
1694                                 }
1695                         }
1696                 }
1697 #undef diff
1698                 /*
1699                  * Rounding down may make us wake up just short
1700                  * of the target, so we round up.
1701                  * The formula is ceiling(slp * hz/1000000).
1702                  * 32-bit arithmetic is enough for hz < 169.
1703                  * XXX see tvtohz() for how to avoid overflow if hz
1704                  * is large (divide by `tick' and/or arrange to
1705                  * use tvtohz() if hz is large).
1706                  */
1707                 slp = (long) (((u_long)slp * hz) + 999999) / 1000000;
1708                 goto sleep;
1709         }
1710         if (qp->c_cc <= 0) {
1711 sleep:
1712                 /*
1713                  * There is no input, or not enough input and we can block.
1714                  */
1715                 error = ttysleep(tp, TSA_HUP_OR_INPUT(tp), PCATCH,
1716                                  ISSET(tp->t_state, TS_CONNECTED) ?
1717                                  "ttyin" : "ttyhup", (int)slp);
1718                 splx(s);
1719                 if (error == EWOULDBLOCK)
1720                         error = 0;
1721                 else if (error)
1722                         return (error);
1723                 /*
1724                  * XXX what happens if another process eats some input
1725                  * while we are asleep (not just here)?  It would be
1726                  * safest to detect changes and reset our state variables
1727                  * (has_stime and last_cc).
1728                  */
1729                 slp = 0;
1730                 goto loop;
1731         }
1732 read:
1733         splx(s);
1734         /*
1735          * Input present, check for input mapping and processing.
1736          */
1737         first = 1;
1738         if (ISSET(lflag, ICANON | ISIG))
1739                 goto slowcase;
1740         for (;;) {
1741                 char ibuf[IBUFSIZ];
1742                 int icc;
1743
1744                 icc = imin(uio->uio_resid, IBUFSIZ);
1745                 icc = q_to_b(qp, ibuf, icc);
1746                 if (icc <= 0) {
1747                         if (first)
1748                                 goto loop;
1749                         break;
1750                 }
1751                 error = uiomove(ibuf, icc, uio);
1752                 /*
1753                  * XXX if there was an error then we should ungetc() the
1754                  * unmoved chars and reduce icc here.
1755                  */
1756                 if (error)
1757                         break;
1758                 if (uio->uio_resid == 0)
1759                         break;
1760                 first = 0;
1761         }
1762         goto out;
1763 slowcase:
1764         for (;;) {
1765                 c = getc(qp);
1766                 if (c < 0) {
1767                         if (first)
1768                                 goto loop;
1769                         break;
1770                 }
1771                 /*
1772                  * delayed suspend (^Y)
1773                  */
1774                 if (CCEQ(cc[VDSUSP], c) &&
1775                     ISSET(lflag, IEXTEN | ISIG) == (IEXTEN | ISIG)) {
1776                         pgsignal(tp->t_pgrp, SIGTSTP, 1);
1777                         if (first) {
1778                                 error = ttysleep(tp, &lbolt, PCATCH,
1779                                                  "ttybg3", 0);
1780                                 if (error)
1781                                         break;
1782                                 goto loop;
1783                         }
1784                         break;
1785                 }
1786                 /*
1787                  * Interpret EOF only in canonical mode.
1788                  */
1789                 if (CCEQ(cc[VEOF], c) && ISSET(lflag, ICANON))
1790                         break;
1791                 /*
1792                  * Give user character.
1793                  */
1794                 error = ureadc(c, uio);
1795                 if (error)
1796                         /* XXX should ungetc(c, qp). */
1797                         break;
1798                 if (uio->uio_resid == 0)
1799                         break;
1800                 /*
1801                  * In canonical mode check for a "break character"
1802                  * marking the end of a "line of input".
1803                  */
1804                 if (ISSET(lflag, ICANON) && TTBREAKC(c, lflag))
1805                         break;
1806                 first = 0;
1807         }
1808
1809 out:
1810         /*
1811          * Look to unblock input now that (presumably)
1812          * the input queue has gone down.
1813          */
1814         s = spltty();
1815         if (ISSET(tp->t_state, TS_TBLOCK) &&
1816             tp->t_rawq.c_cc + tp->t_canq.c_cc <= tp->t_ilowat)
1817                 ttyunblock(tp);
1818         splx(s);
1819
1820         return (error);
1821 }
1822
1823 /*
1824  * Check the output queue on tp for space for a kernel message (from uprintf
1825  * or tprintf).  Allow some space over the normal hiwater mark so we don't
1826  * lose messages due to normal flow control, but don't let the tty run amok.
1827  * Sleeps here are not interruptible, but we return prematurely if new signals
1828  * arrive.
1829  */
1830 int
1831 ttycheckoutq(tp, wait)
1832         struct tty *tp;
1833         int wait;
1834 {
1835         int hiwat, s;
1836         sigset_t oldmask;
1837
1838         hiwat = tp->t_ohiwat;
1839         SIGEMPTYSET(oldmask);
1840         s = spltty();
1841         if (wait)
1842                 oldmask = curproc->p_siglist;
1843         if (tp->t_outq.c_cc > hiwat + OBUFSIZ + 100)
1844                 while (tp->t_outq.c_cc > hiwat) {
1845                         ttstart(tp);
1846                         if (tp->t_outq.c_cc <= hiwat)
1847                                 break;
1848                         if (!(wait && SIGSETEQ(curproc->p_siglist, oldmask))) {
1849                                 splx(s);
1850                                 return (0);
1851                         }
1852                         SET(tp->t_state, TS_SO_OLOWAT);
1853                         tsleep(TSA_OLOWAT(tp), 0, "ttoutq", hz);
1854                 }
1855         splx(s);
1856         return (1);
1857 }
1858
1859 /*
1860  * Process a write call on a tty device.
1861  */
1862 int
1863 ttwrite(tp, uio, flag)
1864         struct tty *tp;
1865         struct uio *uio;
1866         int flag;
1867 {
1868         char *cp = NULL;
1869         int cc, ce;
1870         struct proc *p;
1871         int i, hiwat, cnt, error, s;
1872         char obuf[OBUFSIZ];
1873
1874         hiwat = tp->t_ohiwat;
1875         cnt = uio->uio_resid;
1876         error = 0;
1877         cc = 0;
1878 loop:
1879         s = spltty();
1880         if (ISSET(tp->t_state, TS_ZOMBIE)) {
1881                 splx(s);
1882                 if (uio->uio_resid == cnt)
1883                         error = EIO;
1884                 goto out;
1885         }
1886         if (!ISSET(tp->t_state, TS_CONNECTED)) {
1887                 if (flag & IO_NDELAY) {
1888                         splx(s);
1889                         error = EWOULDBLOCK;
1890                         goto out;
1891                 }
1892                 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ttydcd", 0);
1893                 splx(s);
1894                 if (error)
1895                         goto out;
1896                 goto loop;
1897         }
1898         splx(s);
1899         /*
1900          * Hang the process if it's in the background.
1901          */
1902         p = curproc;
1903         if (isbackground(p, tp) &&
1904             ISSET(tp->t_lflag, TOSTOP) && !(p->p_flag & P_PPWAIT) &&
1905             !SIGISMEMBER(p->p_sigignore, SIGTTOU) &&
1906             !SIGISMEMBER(p->p_sigmask, SIGTTOU)) {
1907                 if (p->p_pgrp->pg_jobc == 0) {
1908                         error = EIO;
1909                         goto out;
1910                 }
1911                 pgsignal(p->p_pgrp, SIGTTOU, 1);
1912                 error = ttysleep(tp, &lbolt, PCATCH, "ttybg4", 0);
1913                 if (error)
1914                         goto out;
1915                 goto loop;
1916         }
1917         /*
1918          * Process the user's data in at most OBUFSIZ chunks.  Perform any
1919          * output translation.  Keep track of high water mark, sleep on
1920          * overflow awaiting device aid in acquiring new space.
1921          */
1922         while (uio->uio_resid > 0 || cc > 0) {
1923                 if (ISSET(tp->t_lflag, FLUSHO)) {
1924                         uio->uio_resid = 0;
1925                         return (0);
1926                 }
1927                 if (tp->t_outq.c_cc > hiwat)
1928                         goto ovhiwat;
1929                 /*
1930                  * Grab a hunk of data from the user, unless we have some
1931                  * leftover from last time.
1932                  */
1933                 if (cc == 0) {
1934                         cc = imin(uio->uio_resid, OBUFSIZ);
1935                         cp = obuf;
1936                         error = uiomove(cp, cc, uio);
1937                         if (error) {
1938                                 cc = 0;
1939                                 break;
1940                         }
1941                 }
1942                 /*
1943                  * If nothing fancy need be done, grab those characters we
1944                  * can handle without any of ttyoutput's processing and
1945                  * just transfer them to the output q.  For those chars
1946                  * which require special processing (as indicated by the
1947                  * bits in char_type), call ttyoutput.  After processing
1948                  * a hunk of data, look for FLUSHO so ^O's will take effect
1949                  * immediately.
1950                  */
1951                 while (cc > 0) {
1952                         if (!ISSET(tp->t_oflag, OPOST))
1953                                 ce = cc;
1954                         else {
1955                                 ce = cc - scanc((u_int)cc, (u_char *)cp,
1956                                                 char_type, CCLASSMASK);
1957                                 /*
1958                                  * If ce is zero, then we're processing
1959                                  * a special character through ttyoutput.
1960                                  */
1961                                 if (ce == 0) {
1962                                         tp->t_rocount = 0;
1963                                         if (ttyoutput(*cp, tp) >= 0) {
1964                                                 /* No Clists, wait a bit. */
1965                                                 ttstart(tp);
1966                                                 if (flag & IO_NDELAY) {
1967                                                         error = EWOULDBLOCK;
1968                                                         goto out;
1969                                                 }
1970                                                 error = ttysleep(tp, &lbolt,
1971                                                                  PCATCH,
1972                                                                  "ttybf1", 0);
1973                                                 if (error)
1974                                                         goto out;
1975                                                 goto loop;
1976                                         }
1977                                         cp++;
1978                                         cc--;
1979                                         if (ISSET(tp->t_lflag, FLUSHO) ||
1980                                             tp->t_outq.c_cc > hiwat)
1981                                                 goto ovhiwat;
1982                                         continue;
1983                                 }
1984                         }
1985                         /*
1986                          * A bunch of normal characters have been found.
1987                          * Transfer them en masse to the output queue and
1988                          * continue processing at the top of the loop.
1989                          * If there are any further characters in this
1990                          * <= OBUFSIZ chunk, the first should be a character
1991                          * requiring special handling by ttyoutput.
1992                          */
1993                         tp->t_rocount = 0;
1994                         i = b_to_q(cp, ce, &tp->t_outq);
1995                         ce -= i;
1996                         tp->t_column += ce;
1997                         cp += ce, cc -= ce, tk_nout += ce;
1998                         tp->t_outcc += ce;
1999                         if (i > 0) {
2000                                 /* No Clists, wait a bit. */
2001                                 ttstart(tp);
2002                                 if (flag & IO_NDELAY) {
2003                                         error = EWOULDBLOCK;
2004                                         goto out;
2005                                 }
2006                                 error = ttysleep(tp, &lbolt, PCATCH,
2007                                                  "ttybf2", 0);
2008                                 if (error)
2009                                         goto out;
2010                                 goto loop;
2011                         }
2012                         if (ISSET(tp->t_lflag, FLUSHO) ||
2013                             tp->t_outq.c_cc > hiwat)
2014                                 break;
2015                 }
2016                 ttstart(tp);
2017         }
2018 out:
2019         /*
2020          * If cc is nonzero, we leave the uio structure inconsistent, as the
2021          * offset and iov pointers have moved forward, but it doesn't matter
2022          * (the call will either return short or restart with a new uio).
2023          */
2024         uio->uio_resid += cc;
2025         return (error);
2026
2027 ovhiwat:
2028         ttstart(tp);
2029         s = spltty();
2030         /*
2031          * This can only occur if FLUSHO is set in t_lflag,
2032          * or if ttstart/oproc is synchronous (or very fast).
2033          */
2034         if (tp->t_outq.c_cc <= hiwat) {
2035                 splx(s);
2036                 goto loop;
2037         }
2038         if (flag & IO_NDELAY) {
2039                 splx(s);
2040                 uio->uio_resid += cc;
2041                 return (uio->uio_resid == cnt ? EWOULDBLOCK : 0);
2042         }
2043         SET(tp->t_state, TS_SO_OLOWAT);
2044         error = ttysleep(tp, TSA_OLOWAT(tp), PCATCH, "ttywri", tp->t_timeout);
2045         splx(s);
2046         if (error == EWOULDBLOCK)
2047                 error = EIO;
2048         if (error)
2049                 goto out;
2050         goto loop;
2051 }
2052
2053 /*
2054  * Rubout one character from the rawq of tp
2055  * as cleanly as possible.
2056  */
2057 static void
2058 ttyrub(c, tp)
2059         int c;
2060         struct tty *tp;
2061 {
2062         char *cp;
2063         int savecol;
2064         int tabc, s;
2065
2066         if (!ISSET(tp->t_lflag, ECHO) || ISSET(tp->t_lflag, EXTPROC))
2067                 return;
2068         CLR(tp->t_lflag, FLUSHO);
2069         if (ISSET(tp->t_lflag, ECHOE)) {
2070                 if (tp->t_rocount == 0) {
2071                         /*
2072                          * Screwed by ttwrite; retype
2073                          */
2074                         ttyretype(tp);
2075                         return;
2076                 }
2077                 if (c == ('\t' | TTY_QUOTE) || c == ('\n' | TTY_QUOTE))
2078                         ttyrubo(tp, 2);
2079                 else {
2080                         CLR(c, ~TTY_CHARMASK);
2081                         switch (CCLASS(c)) {
2082                         case ORDINARY:
2083                                 ttyrubo(tp, 1);
2084                                 break;
2085                         case BACKSPACE:
2086                         case CONTROL:
2087                         case NEWLINE:
2088                         case RETURN:
2089                         case VTAB:
2090                                 if (ISSET(tp->t_lflag, ECHOCTL))
2091                                         ttyrubo(tp, 2);
2092                                 break;
2093                         case TAB:
2094                                 if (tp->t_rocount < tp->t_rawq.c_cc) {
2095                                         ttyretype(tp);
2096                                         return;
2097                                 }
2098                                 s = spltty();
2099                                 savecol = tp->t_column;
2100                                 SET(tp->t_state, TS_CNTTB);
2101                                 SET(tp->t_lflag, FLUSHO);
2102                                 tp->t_column = tp->t_rocol;
2103                                 cp = tp->t_rawq.c_cf;
2104                                 if (cp)
2105                                         tabc = *cp;     /* XXX FIX NEXTC */
2106                                 for (; cp; cp = nextc(&tp->t_rawq, cp, &tabc))
2107                                         ttyecho(tabc, tp);
2108                                 CLR(tp->t_lflag, FLUSHO);
2109                                 CLR(tp->t_state, TS_CNTTB);
2110                                 splx(s);
2111
2112                                 /* savecol will now be length of the tab. */
2113                                 savecol -= tp->t_column;
2114                                 tp->t_column += savecol;
2115                                 if (savecol > 8)
2116                                         savecol = 8;    /* overflow screw */
2117                                 while (--savecol >= 0)
2118                                         (void)ttyoutput('\b', tp);
2119                                 break;
2120                         default:                        /* XXX */
2121 #define PANICSTR        "ttyrub: would panic c = %d, val = %d\n"
2122                                 (void)printf(PANICSTR, c, CCLASS(c));
2123 #ifdef notdef
2124                                 panic(PANICSTR, c, CCLASS(c));
2125 #endif
2126                         }
2127                 }
2128         } else if (ISSET(tp->t_lflag, ECHOPRT)) {
2129                 if (!ISSET(tp->t_state, TS_ERASE)) {
2130                         SET(tp->t_state, TS_ERASE);
2131                         (void)ttyoutput('\\', tp);
2132                 }
2133                 ttyecho(c, tp);
2134         } else {
2135                 ttyecho(tp->t_cc[VERASE], tp);
2136                 /*
2137                  * This code may be executed not only when an ERASE key
2138                  * is pressed, but also when ^U (KILL) or ^W (WERASE) are.
2139                  * So, I didn't think it was worthwhile to pass the extra
2140                  * information (which would need an extra parameter,
2141                  * changing every call) needed to distinguish the ERASE2
2142                  * case from the ERASE.
2143                  */
2144         }
2145         --tp->t_rocount;
2146 }
2147
2148 /*
2149  * Back over cnt characters, erasing them.
2150  */
2151 static void
2152 ttyrubo(tp, cnt)
2153         struct tty *tp;
2154         int cnt;
2155 {
2156
2157         while (cnt-- > 0) {
2158                 (void)ttyoutput('\b', tp);
2159                 (void)ttyoutput(' ', tp);
2160                 (void)ttyoutput('\b', tp);
2161         }
2162 }
2163
2164 /*
2165  * ttyretype --
2166  *      Reprint the rawq line.  Note, it is assumed that c_cc has already
2167  *      been checked.
2168  */
2169 static void
2170 ttyretype(tp)
2171         struct tty *tp;
2172 {
2173         char *cp;
2174         int s, c;
2175
2176         /* Echo the reprint character. */
2177         if (tp->t_cc[VREPRINT] != _POSIX_VDISABLE)
2178                 ttyecho(tp->t_cc[VREPRINT], tp);
2179
2180         (void)ttyoutput('\n', tp);
2181
2182         /*
2183          * XXX
2184          * FIX: NEXTC IS BROKEN - DOESN'T CHECK QUOTE
2185          * BIT OF FIRST CHAR.
2186          */
2187         s = spltty();
2188         for (cp = tp->t_canq.c_cf, c = (cp != NULL ? *cp : 0);
2189             cp != NULL; cp = nextc(&tp->t_canq, cp, &c))
2190                 ttyecho(c, tp);
2191         for (cp = tp->t_rawq.c_cf, c = (cp != NULL ? *cp : 0);
2192             cp != NULL; cp = nextc(&tp->t_rawq, cp, &c))
2193                 ttyecho(c, tp);
2194         CLR(tp->t_state, TS_ERASE);
2195         splx(s);
2196
2197         tp->t_rocount = tp->t_rawq.c_cc;
2198         tp->t_rocol = 0;
2199 }
2200
2201 /*
2202  * Echo a typed character to the terminal.
2203  */
2204 static void
2205 ttyecho(c, tp)
2206         int c;
2207         struct tty *tp;
2208 {
2209
2210         if (!ISSET(tp->t_state, TS_CNTTB))
2211                 CLR(tp->t_lflag, FLUSHO);
2212         if ((!ISSET(tp->t_lflag, ECHO) &&
2213              (c != '\n' || !ISSET(tp->t_lflag, ECHONL))) ||
2214             ISSET(tp->t_lflag, EXTPROC))
2215                 return;
2216         if (ISSET(tp->t_lflag, ECHOCTL) &&
2217             ((ISSET(c, TTY_CHARMASK) <= 037 && c != '\t' && c != '\n') ||
2218             ISSET(c, TTY_CHARMASK) == 0177)) {
2219                 (void)ttyoutput('^', tp);
2220                 CLR(c, ~TTY_CHARMASK);
2221                 if (c == 0177)
2222                         c = '?';
2223                 else
2224                         c += 'A' - 1;
2225         }
2226         (void)ttyoutput(c, tp);
2227 }
2228
2229 /*
2230  * Wake up any readers on a tty.
2231  */
2232 void
2233 ttwakeup(tp)
2234         struct tty *tp;
2235 {
2236
2237         if (tp->t_rsel.si_pid != 0)
2238                 selwakeup(&tp->t_rsel);
2239         if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2240                 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL));
2241         wakeup(TSA_HUP_OR_INPUT(tp));
2242         KNOTE(&tp->t_rsel.si_note, 0);
2243 }
2244
2245 /*
2246  * Wake up any writers on a tty.
2247  */
2248 void
2249 ttwwakeup(tp)
2250         struct tty *tp;
2251 {
2252
2253         if (tp->t_wsel.si_pid != 0 && tp->t_outq.c_cc <= tp->t_olowat)
2254                 selwakeup(&tp->t_wsel);
2255         if (ISSET(tp->t_state, TS_ASYNC) && tp->t_sigio != NULL)
2256                 pgsigio(tp->t_sigio, SIGIO, (tp->t_session != NULL));
2257         if (ISSET(tp->t_state, TS_BUSY | TS_SO_OCOMPLETE) ==
2258             TS_SO_OCOMPLETE && tp->t_outq.c_cc == 0) {
2259                 CLR(tp->t_state, TS_SO_OCOMPLETE);
2260                 wakeup(TSA_OCOMPLETE(tp));
2261         }
2262         if (ISSET(tp->t_state, TS_SO_OLOWAT) &&
2263             tp->t_outq.c_cc <= tp->t_olowat) {
2264                 CLR(tp->t_state, TS_SO_OLOWAT);
2265                 wakeup(TSA_OLOWAT(tp));
2266         }
2267         KNOTE(&tp->t_wsel.si_note, 0);
2268 }
2269
2270 /*
2271  * Look up a code for a specified speed in a conversion table;
2272  * used by drivers to map software speed values to hardware parameters.
2273  */
2274 int
2275 ttspeedtab(speed, table)
2276         int speed;
2277         struct speedtab *table;
2278 {
2279
2280         for ( ; table->sp_speed != -1; table++)
2281                 if (table->sp_speed == speed)
2282                         return (table->sp_code);
2283         return (-1);
2284 }
2285
2286 /*
2287  * Set input and output watermarks and buffer sizes.  For input, the
2288  * high watermark is about one second's worth of input above empty, the
2289  * low watermark is slightly below high water, and the buffer size is a
2290  * driver-dependent amount above high water.  For output, the watermarks
2291  * are near the ends of the buffer, with about 1 second's worth of input
2292  * between them.  All this only applies to the standard line discipline.
2293  */
2294 void
2295 ttsetwater(tp)
2296         struct tty *tp;
2297 {
2298         int cps, ttmaxhiwat, x;
2299
2300         /* Input. */
2301         clist_alloc_cblocks(&tp->t_canq, TTYHOG, 512);
2302         switch (tp->t_ispeedwat) {
2303         case (speed_t)-1:
2304                 cps = tp->t_ispeed / 10;
2305                 break;
2306         case 0:
2307                 /*
2308                  * This case is for old drivers that don't know about
2309                  * t_ispeedwat.  Arrange for them to get the old buffer
2310                  * sizes and watermarks.
2311                  */
2312                 cps = TTYHOG - 2 * 256;
2313                 tp->t_ififosize = 2 * 256;
2314                 break;
2315         default:
2316                 cps = tp->t_ispeedwat / 10;
2317                 break;
2318         }
2319         tp->t_ihiwat = cps;
2320         tp->t_ilowat = 7 * cps / 8;
2321         x = cps + tp->t_ififosize;
2322         clist_alloc_cblocks(&tp->t_rawq, x, x);
2323
2324         /* Output. */
2325         switch (tp->t_ospeedwat) {
2326         case (speed_t)-1:
2327                 cps = tp->t_ospeed / 10;
2328                 ttmaxhiwat = 2 * TTMAXHIWAT;
2329                 break;
2330         case 0:
2331                 cps = tp->t_ospeed / 10;
2332                 ttmaxhiwat = TTMAXHIWAT;
2333                 break;
2334         default:
2335                 cps = tp->t_ospeedwat / 10;
2336                 ttmaxhiwat = 8 * TTMAXHIWAT;
2337                 break;
2338         }
2339 #define CLAMP(x, h, l)  ((x) > h ? h : ((x) < l) ? l : (x))
2340         tp->t_olowat = x = CLAMP(cps / 2, TTMAXLOWAT, TTMINLOWAT);
2341         x += cps;
2342         x = CLAMP(x, ttmaxhiwat, TTMINHIWAT);   /* XXX clamps are too magic */
2343         tp->t_ohiwat = roundup(x, CBSIZE);      /* XXX for compat */
2344         x = imax(tp->t_ohiwat, TTMAXHIWAT);     /* XXX for compat/safety */
2345         x += OBUFSIZ + 100;
2346         clist_alloc_cblocks(&tp->t_outq, x, x);
2347 #undef  CLAMP
2348 }
2349
2350 /*
2351  * Report on state of foreground process group.
2352  */
2353 void
2354 ttyinfo(tp)
2355         struct tty *tp;
2356 {
2357         struct proc *p, *pick;
2358         struct timeval utime, stime;
2359         int tmp;
2360
2361         if (ttycheckoutq(tp,0) == 0)
2362                 return;
2363
2364         /*
2365          * We always print the load average, then figure out what else to
2366          * print based on the state of the current process group.
2367          */
2368         tmp = (averunnable.ldavg[0] * 100 + FSCALE / 2) >> FSHIFT;
2369         ttyprintf(tp, "load: %d.%02d ", tmp / 100, tmp % 100);
2370
2371         if (tp->t_session == NULL) {
2372                 ttyprintf(tp, "not a controlling terminal\n");
2373         } else if (tp->t_pgrp == NULL) {
2374                 ttyprintf(tp, "no foreground process group\n");
2375         } else if ((p = LIST_FIRST(&tp->t_pgrp->pg_members)) == 0) {
2376                 ttyprintf(tp, "empty foreground process group\n");
2377         } else {
2378                 /*
2379                  * Pick an interesting process.  Note that certain elements,
2380                  * in particular the wmesg, require a critical section for
2381                  * safe access (YYY and we are still not MP safe).
2382                  *
2383                  * NOTE: p_wmesg is p_thread->td_wmesg, and p_comm is
2384                  * p_thread->td_comm.
2385                  */
2386                 char buf[64];
2387                 const char *str;
2388                 int isinmem;
2389                 long vmsz;
2390                 int pctcpu;
2391
2392                 crit_enter();
2393
2394                 for (pick = NULL; p != 0; p = LIST_NEXT(p, p_pglist)) {
2395                         if (proc_compare(pick, p))
2396                                 pick = p;
2397                 }
2398
2399                 /*
2400                  * Figure out what wait/process-state message, and command
2401                  * buffer to present
2402                  */
2403                 if (pick->p_thread == NULL)
2404                         str = "exiting";
2405                 else if (pick->p_stat == SRUN)
2406                         str = "running";
2407                 else if (pick->p_wmesg) /* p_thread must not be NULL */
2408                         str = pick->p_wmesg;
2409                 else
2410                         str = "iowait";
2411
2412                 snprintf(buf, sizeof(buf), "cmd: %s %d [%s]",
2413                         (pick->p_thread ? pick->p_comm : "?"),
2414                         pick->p_pid, str);
2415
2416                 /*
2417                  * Calculate cpu usage, percent cpu, and cmsz.  Note that
2418                  * 'pick' becomes invalid the moment we exit the critical
2419                  * section.
2420                  */
2421                 if (pick->p_thread && (pick->p_flag & P_INMEM)) {
2422                         calcru(pick, &utime, &stime, NULL);
2423                         isinmem = 1;
2424                 } else {
2425                         isinmem = 0;
2426                 }
2427
2428                 pctcpu = (pick->p_pctcpu * 10000 + FSCALE / 2) >> FSHIFT;
2429
2430                 if (pick->p_stat == SIDL || pick->p_stat == SZOMB)
2431                     vmsz = 0;
2432                 else
2433                     vmsz = pgtok(vmspace_resident_count(pick->p_vmspace));
2434
2435                 crit_exit();
2436
2437                 /*
2438                  * Dump the output
2439                  */
2440                 ttyprintf(tp, " %s ", buf);
2441                 if (isinmem) {
2442                         ttyprintf(tp, "%ld.%02ldu ",
2443                                 utime.tv_sec, utime.tv_usec / 10000);
2444                         ttyprintf(tp, "%ld.%02lds ",
2445                                 stime.tv_sec, stime.tv_usec / 10000);
2446                 } else {
2447                         ttyprintf(tp, "?.??u ?.??s ");
2448                 }
2449                 ttyprintf(tp, "%d%% %ldk\n", pctcpu / 100, vmsz);
2450         }
2451         tp->t_rocount = 0;      /* so pending input will be retyped if BS */
2452 }
2453
2454 /*
2455  * Returns 1 if p2 is "better" than p1
2456  *
2457  * The algorithm for picking the "interesting" process is thus:
2458  *
2459  *      1) Only foreground processes are eligible - implied.
2460  *      2) Runnable processes are favored over anything else.  The runner
2461  *         with the highest cpu utilization is picked (p_estcpu).  Ties are
2462  *         broken by picking the highest pid.
2463  *      3) The sleeper with the shortest sleep time is next.  With ties,
2464  *         we pick out just "short-term" sleepers (P_SINTR == 0).
2465  *      4) Further ties are broken by picking the highest pid.
2466  */
2467 #define ISRUN(p)        (((p)->p_stat == SRUN) || ((p)->p_stat == SIDL))
2468 #define TESTAB(a, b)    ((a)<<1 | (b))
2469 #define ONLYA   2
2470 #define ONLYB   1
2471 #define BOTH    3
2472
2473 static int
2474 proc_compare(p1, p2)
2475         struct proc *p1, *p2;
2476 {
2477
2478         if (p1 == NULL)
2479                 return (1);
2480         /*
2481          * see if at least one of them is runnable
2482          */
2483         switch (TESTAB(ISRUN(p1), ISRUN(p2))) {
2484         case ONLYA:
2485                 return (0);
2486         case ONLYB:
2487                 return (1);
2488         case BOTH:
2489                 /*
2490                  * tie - favor one with highest recent cpu utilization
2491                  */
2492                 if (p2->p_estcpu > p1->p_estcpu)
2493                         return (1);
2494                 if (p1->p_estcpu > p2->p_estcpu)
2495                         return (0);
2496                 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2497         }
2498         /*
2499          * weed out zombies
2500          */
2501         switch (TESTAB(p1->p_stat == SZOMB, p2->p_stat == SZOMB)) {
2502         case ONLYA:
2503                 return (1);
2504         case ONLYB:
2505                 return (0);
2506         case BOTH:
2507                 return (p2->p_pid > p1->p_pid); /* tie - return highest pid */
2508         }
2509         /*
2510          * pick the one with the smallest sleep time
2511          */
2512         if (p2->p_slptime > p1->p_slptime)
2513                 return (0);
2514         if (p1->p_slptime > p2->p_slptime)
2515                 return (1);
2516         /*
2517          * favor one sleeping in a non-interruptible sleep
2518          */
2519         if (p1->p_flag & P_SINTR && (p2->p_flag & P_SINTR) == 0)
2520                 return (1);
2521         if (p2->p_flag & P_SINTR && (p1->p_flag & P_SINTR) == 0)
2522                 return (0);
2523         return (p2->p_pid > p1->p_pid);         /* tie - return highest pid */
2524 }
2525
2526 /*
2527  * Output char to tty; console putchar style.
2528  */
2529 int
2530 tputchar(c, tp)
2531         int c;
2532         struct tty *tp;
2533 {
2534         int s;
2535
2536         s = spltty();
2537         if (!ISSET(tp->t_state, TS_CONNECTED)) {
2538                 splx(s);
2539                 return (-1);
2540         }
2541         if (c == '\n')
2542                 (void)ttyoutput('\r', tp);
2543         (void)ttyoutput(c, tp);
2544         ttstart(tp);
2545         splx(s);
2546         return (0);
2547 }
2548
2549 /*
2550  * Sleep on chan, returning ERESTART if tty changed while we napped and
2551  * returning any errors (e.g. EINTR/EWOULDBLOCK) reported by tsleep.  If
2552  * the tty is revoked, restarting a pending call will redo validation done
2553  * at the start of the call.
2554  */
2555 int
2556 ttysleep(tp, chan, slpflags, wmesg, timo)
2557         struct tty *tp;
2558         void *chan;
2559         int slpflags, timo;
2560         char *wmesg;
2561 {
2562         int error;
2563         int gen;
2564
2565         gen = tp->t_gen;
2566         error = tsleep(chan, slpflags, wmesg, timo);
2567         if (error)
2568                 return (error);
2569         return (tp->t_gen == gen ? 0 : ERESTART);
2570 }
2571
2572 /*
2573  * Allocate a tty struct.  Clists in the struct will be allocated by
2574  * ttyopen().
2575  */
2576 struct tty *
2577 ttymalloc(tp)
2578         struct tty *tp;
2579 {
2580
2581         if (tp)
2582                 return(tp);
2583         tp = malloc(sizeof *tp, M_TTYS, M_WAITOK|M_ZERO);
2584         ttyregister(tp);
2585         return (tp);
2586 }
2587
2588 #if 0
2589 /*
2590  * Free a tty struct.  Clists in the struct should have been freed by
2591  * ttyclose().
2592  *
2593  * XXX not yet usable: since we support a half-closed state and do not
2594  * ref count the tty reference from the session, it is possible for a 
2595  * session to hold a ref on the tty.  See TTY_DO_FULL_CLOSE.
2596  */
2597 void
2598 ttyfree(tp)
2599         struct tty *tp;
2600 {
2601         free(tp, M_TTYS);
2602 }
2603 #endif /* 0 */
2604
2605 void
2606 ttyregister(tp)
2607         struct tty *tp;
2608 {
2609         SLIST_INSERT_HEAD(&tty_list, tp, t_list);
2610 }
2611
2612 static int
2613 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
2614 {
2615         int error;
2616         struct tty *tp, t;
2617         SLIST_FOREACH(tp, &tty_list, t_list) {
2618                 t = *tp;
2619                 if (t.t_dev)
2620                         t.t_dev = (dev_t)dev2udev(t.t_dev);
2621                 error = SYSCTL_OUT(req, (caddr_t)&t, sizeof(t));
2622                 if (error)
2623                         return (error);
2624         }
2625         return (0);
2626 }
2627
2628 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD,
2629         0, 0, sysctl_kern_ttys, "S,tty", "All struct ttys");
2630
2631 void
2632 nottystop(tp, rw)
2633         struct tty *tp;
2634         int rw;
2635 {
2636
2637         return;
2638 }
2639
2640 int
2641 ttyread(dev, uio, flag)
2642         dev_t dev;
2643         struct uio *uio;
2644         int flag;
2645 {
2646         struct tty *tp;
2647
2648         tp = dev->si_tty;
2649         if (tp == NULL)
2650                 return (ENODEV);
2651         return ((*linesw[tp->t_line].l_read)(tp, uio, flag));
2652 }
2653
2654 int
2655 ttywrite(dev, uio, flag)
2656         dev_t dev;
2657         struct uio *uio;
2658         int flag;
2659 {
2660         struct tty *tp;
2661
2662         tp = dev->si_tty;
2663         if (tp == NULL)
2664                 return (ENODEV);
2665         return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
2666 }