]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/tty.c
MFC r366500:
[FreeBSD/FreeBSD.git] / sys / kern / tty.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Portions of this software were developed under sponsorship from Snow
8  * B.V., the Netherlands.
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  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 #include "opt_capsicum.h"
36
37 #include <sys/param.h>
38 #include <sys/capsicum.h>
39 #include <sys/conf.h>
40 #include <sys/cons.h>
41 #include <sys/fcntl.h>
42 #include <sys/file.h>
43 #include <sys/filedesc.h>
44 #include <sys/filio.h>
45 #ifdef COMPAT_43TTY
46 #include <sys/ioctl_compat.h>
47 #endif /* COMPAT_43TTY */
48 #include <sys/kernel.h>
49 #include <sys/limits.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/poll.h>
53 #include <sys/priv.h>
54 #include <sys/proc.h>
55 #include <sys/serial.h>
56 #include <sys/signal.h>
57 #include <sys/stat.h>
58 #include <sys/sx.h>
59 #include <sys/sysctl.h>
60 #include <sys/systm.h>
61 #include <sys/tty.h>
62 #include <sys/ttycom.h>
63 #define TTYDEFCHARS
64 #include <sys/ttydefaults.h>
65 #undef TTYDEFCHARS
66 #include <sys/ucred.h>
67 #include <sys/vnode.h>
68
69 #include <machine/stdarg.h>
70
71 static MALLOC_DEFINE(M_TTY, "tty", "tty device");
72
73 static void tty_rel_free(struct tty *tp);
74
75 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
76 static struct sx tty_list_sx;
77 SX_SYSINIT(tty_list, &tty_list_sx, "tty list");
78 static unsigned int tty_list_count = 0;
79
80 /* Character device of /dev/console. */
81 static struct cdev      *dev_console;
82 static const char       *dev_console_filename;
83
84 /*
85  * Flags that are supported and stored by this implementation.
86  */
87 #define TTYSUP_IFLAG    (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|\
88                         INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL)
89 #define TTYSUP_OFLAG    (OPOST|ONLCR|TAB3|ONOEOT|OCRNL|ONOCR|ONLRET)
90 #define TTYSUP_LFLAG    (ECHOKE|ECHOE|ECHOK|ECHO|ECHONL|ECHOPRT|\
91                         ECHOCTL|ISIG|ICANON|ALTWERASE|IEXTEN|TOSTOP|\
92                         FLUSHO|NOKERNINFO|NOFLSH)
93 #define TTYSUP_CFLAG    (CIGNORE|CSIZE|CSTOPB|CREAD|PARENB|PARODD|\
94                         HUPCL|CLOCAL|CCTS_OFLOW|CRTS_IFLOW|CDTR_IFLOW|\
95                         CDSR_OFLOW|CCAR_OFLOW)
96
97 #define TTY_CALLOUT(tp,d) (dev2unit(d) & TTYUNIT_CALLOUT)
98
99 static int  tty_drainwait = 5 * 60;
100 SYSCTL_INT(_kern, OID_AUTO, tty_drainwait, CTLFLAG_RWTUN,
101     &tty_drainwait, 0, "Default output drain timeout in seconds");
102
103 /*
104  * Set TTY buffer sizes.
105  */
106
107 #define TTYBUF_MAX      65536
108
109 /*
110  * Allocate buffer space if necessary, and set low watermarks, based on speed.
111  * Note that the ttyxxxq_setsize() functions may drop and then reacquire the tty
112  * lock during memory allocation.  They will return ENXIO if the tty disappears
113  * while unlocked.
114  */
115 static int
116 tty_watermarks(struct tty *tp)
117 {
118         size_t bs = 0;
119         int error;
120
121         /* Provide an input buffer for 2 seconds of data. */
122         if (tp->t_termios.c_cflag & CREAD)
123                 bs = MIN(tp->t_termios.c_ispeed / 5, TTYBUF_MAX);
124         error = ttyinq_setsize(&tp->t_inq, tp, bs);
125         if (error != 0)
126                 return (error);
127
128         /* Set low watermark at 10% (when 90% is available). */
129         tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
130
131         /* Provide an output buffer for 2 seconds of data. */
132         bs = MIN(tp->t_termios.c_ospeed / 5, TTYBUF_MAX);
133         error = ttyoutq_setsize(&tp->t_outq, tp, bs);
134         if (error != 0)
135                 return (error);
136
137         /* Set low watermark at 10% (when 90% is available). */
138         tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
139
140         return (0);
141 }
142
143 static int
144 tty_drain(struct tty *tp, int leaving)
145 {
146         sbintime_t timeout_at;
147         size_t bytes;
148         int error;
149
150         if (ttyhook_hashook(tp, getc_inject))
151                 /* buffer is inaccessible */
152                 return (0);
153
154         /*
155          * For close(), use the recent historic timeout of "1 second without
156          * making progress".  For tcdrain(), use t_drainwait as the timeout,
157          * with zero meaning "no timeout" which gives POSIX behavior.
158          */
159         if (leaving)
160                 timeout_at = getsbinuptime() + SBT_1S;
161         else if (tp->t_drainwait != 0)
162                 timeout_at = getsbinuptime() + SBT_1S * tp->t_drainwait;
163         else
164                 timeout_at = 0;
165
166         /*
167          * Poll the output buffer and the hardware for completion, at 10 Hz.
168          * Polling is required for devices which are not able to signal an
169          * interrupt when the transmitter becomes idle (most USB serial devs).
170          * The unusual structure of this loop ensures we check for busy one more
171          * time after tty_timedwait() returns EWOULDBLOCK, so that success has
172          * higher priority than timeout if the IO completed in the last 100mS.
173          */
174         error = 0;
175         bytes = ttyoutq_bytesused(&tp->t_outq);
176         for (;;) {
177                 if (ttyoutq_bytesused(&tp->t_outq) == 0 && !ttydevsw_busy(tp))
178                         return (0);
179                 if (error != 0)
180                         return (error);
181                 ttydevsw_outwakeup(tp);
182                 error = tty_timedwait(tp, &tp->t_outwait, hz / 10);
183                 if (error != 0 && error != EWOULDBLOCK)
184                         return (error);
185                 else if (timeout_at == 0 || getsbinuptime() < timeout_at)
186                         error = 0;
187                 else if (leaving && ttyoutq_bytesused(&tp->t_outq) < bytes) {
188                         /* In close, making progress, grant an extra second. */
189                         error = 0;
190                         timeout_at += SBT_1S;
191                         bytes = ttyoutq_bytesused(&tp->t_outq);
192                 }
193         }
194 }
195
196 /*
197  * Though ttydev_enter() and ttydev_leave() seem to be related, they
198  * don't have to be used together. ttydev_enter() is used by the cdev
199  * operations to prevent an actual operation from being processed when
200  * the TTY has been abandoned. ttydev_leave() is used by ttydev_open()
201  * and ttydev_close() to determine whether per-TTY data should be
202  * deallocated.
203  */
204
205 static __inline int
206 ttydev_enter(struct tty *tp)
207 {
208
209         tty_lock(tp);
210
211         if (tty_gone(tp) || !tty_opened(tp)) {
212                 /* Device is already gone. */
213                 tty_unlock(tp);
214                 return (ENXIO);
215         }
216
217         return (0);
218 }
219
220 static void
221 ttydev_leave(struct tty *tp)
222 {
223
224         tty_assert_locked(tp);
225
226         if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
227                 /* Device is still opened somewhere. */
228                 tty_unlock(tp);
229                 return;
230         }
231
232         tp->t_flags |= TF_OPENCLOSE;
233
234         /* Remove console TTY. */
235         if (constty == tp)
236                 constty_clear();
237
238         /* Drain any output. */
239         if (!tty_gone(tp))
240                 tty_drain(tp, 1);
241
242         ttydisc_close(tp);
243
244         /* Free i/o queues now since they might be large. */
245         ttyinq_free(&tp->t_inq);
246         tp->t_inlow = 0;
247         ttyoutq_free(&tp->t_outq);
248         tp->t_outlow = 0;
249
250         knlist_clear(&tp->t_inpoll.si_note, 1);
251         knlist_clear(&tp->t_outpoll.si_note, 1);
252
253         if (!tty_gone(tp))
254                 ttydevsw_close(tp);
255
256         tp->t_flags &= ~TF_OPENCLOSE;
257         cv_broadcast(&tp->t_dcdwait);
258         tty_rel_free(tp);
259 }
260
261 /*
262  * Operations that are exposed through the character device in /dev.
263  */
264 static int
265 ttydev_open(struct cdev *dev, int oflags, int devtype __unused,
266     struct thread *td)
267 {
268         struct tty *tp;
269         int error;
270
271         tp = dev->si_drv1;
272         error = 0;
273         tty_lock(tp);
274         if (tty_gone(tp)) {
275                 /* Device is already gone. */
276                 tty_unlock(tp);
277                 return (ENXIO);
278         }
279
280         /*
281          * Block when other processes are currently opening or closing
282          * the TTY.
283          */
284         while (tp->t_flags & TF_OPENCLOSE) {
285                 error = tty_wait(tp, &tp->t_dcdwait);
286                 if (error != 0) {
287                         tty_unlock(tp);
288                         return (error);
289                 }
290         }
291         tp->t_flags |= TF_OPENCLOSE;
292
293         /*
294          * Make sure the "tty" and "cua" device cannot be opened at the
295          * same time.  The console is a "tty" device.
296          */
297         if (TTY_CALLOUT(tp, dev)) {
298                 if (tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) {
299                         error = EBUSY;
300                         goto done;
301                 }
302         } else {
303                 if (tp->t_flags & TF_OPENED_OUT) {
304                         error = EBUSY;
305                         goto done;
306                 }
307         }
308
309         if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
310                 error = EBUSY;
311                 goto done;
312         }
313
314         if (!tty_opened(tp)) {
315                 /* Set proper termios flags. */
316                 if (TTY_CALLOUT(tp, dev))
317                         tp->t_termios = tp->t_termios_init_out;
318                 else
319                         tp->t_termios = tp->t_termios_init_in;
320                 ttydevsw_param(tp, &tp->t_termios);
321                 /* Prevent modem control on callout devices and /dev/console. */
322                 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
323                         tp->t_termios.c_cflag |= CLOCAL;
324
325                 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
326
327                 error = ttydevsw_open(tp);
328                 if (error != 0)
329                         goto done;
330
331                 ttydisc_open(tp);
332                 error = tty_watermarks(tp);
333                 if (error != 0)
334                         goto done;
335         }
336
337         /* Wait for Carrier Detect. */
338         if ((oflags & O_NONBLOCK) == 0 &&
339             (tp->t_termios.c_cflag & CLOCAL) == 0) {
340                 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
341                         error = tty_wait(tp, &tp->t_dcdwait);
342                         if (error != 0)
343                                 goto done;
344                 }
345         }
346
347         if (dev == dev_console)
348                 tp->t_flags |= TF_OPENED_CONS;
349         else if (TTY_CALLOUT(tp, dev))
350                 tp->t_flags |= TF_OPENED_OUT;
351         else
352                 tp->t_flags |= TF_OPENED_IN;
353         MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
354             (tp->t_flags & TF_OPENED_OUT) == 0);
355
356 done:   tp->t_flags &= ~TF_OPENCLOSE;
357         cv_broadcast(&tp->t_dcdwait);
358         ttydev_leave(tp);
359
360         return (error);
361 }
362
363 static int
364 ttydev_close(struct cdev *dev, int fflag, int devtype __unused,
365     struct thread *td __unused)
366 {
367         struct tty *tp = dev->si_drv1;
368
369         tty_lock(tp);
370
371         /*
372          * Don't actually close the device if it is being used as the
373          * console.
374          */
375         MPASS((tp->t_flags & (TF_OPENED_CONS | TF_OPENED_IN)) == 0 ||
376             (tp->t_flags & TF_OPENED_OUT) == 0);
377         if (dev == dev_console)
378                 tp->t_flags &= ~TF_OPENED_CONS;
379         else
380                 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
381
382         if (tp->t_flags & TF_OPENED) {
383                 tty_unlock(tp);
384                 return (0);
385         }
386
387         /* If revoking, flush output now to avoid draining it later. */
388         if (fflag & FREVOKE)
389                 tty_flush(tp, FWRITE);
390
391         tp->t_flags &= ~TF_EXCLUDE;
392
393         /* Properly wake up threads that are stuck - revoke(). */
394         tp->t_revokecnt++;
395         tty_wakeup(tp, FREAD|FWRITE);
396         cv_broadcast(&tp->t_bgwait);
397         cv_broadcast(&tp->t_dcdwait);
398
399         ttydev_leave(tp);
400
401         return (0);
402 }
403
404 static __inline int
405 tty_is_ctty(struct tty *tp, struct proc *p)
406 {
407
408         tty_assert_locked(tp);
409
410         return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
411 }
412
413 int
414 tty_wait_background(struct tty *tp, struct thread *td, int sig)
415 {
416         struct proc *p = td->td_proc;
417         struct pgrp *pg;
418         ksiginfo_t ksi;
419         int error;
420
421         MPASS(sig == SIGTTIN || sig == SIGTTOU);
422         tty_assert_locked(tp);
423
424         for (;;) {
425                 PROC_LOCK(p);
426                 /*
427                  * The process should only sleep, when:
428                  * - This terminal is the controlling terminal
429                  * - Its process group is not the foreground process
430                  *   group
431                  * - The parent process isn't waiting for the child to
432                  *   exit
433                  * - the signal to send to the process isn't masked
434                  */
435                 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
436                         /* Allow the action to happen. */
437                         PROC_UNLOCK(p);
438                         return (0);
439                 }
440
441                 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
442                     SIGISMEMBER(td->td_sigmask, sig)) {
443                         /* Only allow them in write()/ioctl(). */
444                         PROC_UNLOCK(p);
445                         return (sig == SIGTTOU ? 0 : EIO);
446                 }
447
448                 pg = p->p_pgrp;
449                 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
450                         /* Don't allow the action to happen. */
451                         PROC_UNLOCK(p);
452                         return (EIO);
453                 }
454                 PROC_UNLOCK(p);
455
456                 /*
457                  * Send the signal and sleep until we're the new
458                  * foreground process group.
459                  */
460                 if (sig != 0) {
461                         ksiginfo_init(&ksi);
462                         ksi.ksi_code = SI_KERNEL;
463                         ksi.ksi_signo = sig;
464                         sig = 0;
465                 }
466                 PGRP_LOCK(pg);
467
468                 /*
469                  * pg may no longer be our process group.
470                  * Re-check after locking process group.
471                  */
472                 PROC_LOCK(p);
473                 if (p->p_pgrp != pg) {
474                         PROC_UNLOCK(p);
475                         PGRP_UNLOCK(pg);
476                         continue;
477                 }
478
479                 PROC_UNLOCK(p);
480                 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
481                 PGRP_UNLOCK(pg);
482
483                 error = tty_wait(tp, &tp->t_bgwait);
484                 if (error)
485                         return (error);
486         }
487 }
488
489 static int
490 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
491 {
492         struct tty *tp = dev->si_drv1;
493         int error;
494
495         error = ttydev_enter(tp);
496         if (error)
497                 goto done;
498         error = ttydisc_read(tp, uio, ioflag);
499         tty_unlock(tp);
500
501         /*
502          * The read() call should not throw an error when the device is
503          * being destroyed. Silently convert it to an EOF.
504          */
505 done:   if (error == ENXIO)
506                 error = 0;
507         return (error);
508 }
509
510 static int
511 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
512 {
513         struct tty *tp = dev->si_drv1;
514         int error;
515
516         error = ttydev_enter(tp);
517         if (error)
518                 return (error);
519
520         if (tp->t_termios.c_lflag & TOSTOP) {
521                 error = tty_wait_background(tp, curthread, SIGTTOU);
522                 if (error)
523                         goto done;
524         }
525
526         if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
527                 /* Allow non-blocking writes to bypass serialization. */
528                 error = ttydisc_write(tp, uio, ioflag);
529         } else {
530                 /* Serialize write() calls. */
531                 while (tp->t_flags & TF_BUSY_OUT) {
532                         error = tty_wait(tp, &tp->t_outserwait);
533                         if (error)
534                                 goto done;
535                 }
536
537                 tp->t_flags |= TF_BUSY_OUT;
538                 error = ttydisc_write(tp, uio, ioflag);
539                 tp->t_flags &= ~TF_BUSY_OUT;
540                 cv_signal(&tp->t_outserwait);
541         }
542
543 done:   tty_unlock(tp);
544         return (error);
545 }
546
547 static int
548 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
549     struct thread *td)
550 {
551         struct tty *tp = dev->si_drv1;
552         int error;
553
554         error = ttydev_enter(tp);
555         if (error)
556                 return (error);
557
558         switch (cmd) {
559         case TIOCCBRK:
560         case TIOCCONS:
561         case TIOCDRAIN:
562         case TIOCEXCL:
563         case TIOCFLUSH:
564         case TIOCNXCL:
565         case TIOCSBRK:
566         case TIOCSCTTY:
567         case TIOCSETA:
568         case TIOCSETAF:
569         case TIOCSETAW:
570         case TIOCSPGRP:
571         case TIOCSTART:
572         case TIOCSTAT:
573         case TIOCSTI:
574         case TIOCSTOP:
575         case TIOCSWINSZ:
576 #if 0
577         case TIOCSDRAINWAIT:
578         case TIOCSETD:
579 #endif
580 #ifdef COMPAT_43TTY
581         case  TIOCLBIC:
582         case  TIOCLBIS:
583         case  TIOCLSET:
584         case  TIOCSETC:
585         case OTIOCSETD:
586         case  TIOCSETN:
587         case  TIOCSETP:
588         case  TIOCSLTC:
589 #endif /* COMPAT_43TTY */
590                 /*
591                  * If the ioctl() causes the TTY to be modified, let it
592                  * wait in the background.
593                  */
594                 error = tty_wait_background(tp, curthread, SIGTTOU);
595                 if (error)
596                         goto done;
597         }
598
599         if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
600                 struct termios *old = &tp->t_termios;
601                 struct termios *new = (struct termios *)data;
602                 struct termios *lock = TTY_CALLOUT(tp, dev) ?
603                     &tp->t_termios_lock_out : &tp->t_termios_lock_in;
604                 int cc;
605
606                 /*
607                  * Lock state devices.  Just overwrite the values of the
608                  * commands that are currently in use.
609                  */
610                 new->c_iflag = (old->c_iflag & lock->c_iflag) |
611                     (new->c_iflag & ~lock->c_iflag);
612                 new->c_oflag = (old->c_oflag & lock->c_oflag) |
613                     (new->c_oflag & ~lock->c_oflag);
614                 new->c_cflag = (old->c_cflag & lock->c_cflag) |
615                     (new->c_cflag & ~lock->c_cflag);
616                 new->c_lflag = (old->c_lflag & lock->c_lflag) |
617                     (new->c_lflag & ~lock->c_lflag);
618                 for (cc = 0; cc < NCCS; ++cc)
619                         if (lock->c_cc[cc])
620                                 new->c_cc[cc] = old->c_cc[cc];
621                 if (lock->c_ispeed)
622                         new->c_ispeed = old->c_ispeed;
623                 if (lock->c_ospeed)
624                         new->c_ospeed = old->c_ospeed;
625         }
626
627         error = tty_ioctl(tp, cmd, data, fflag, td);
628 done:   tty_unlock(tp);
629
630         return (error);
631 }
632
633 static int
634 ttydev_poll(struct cdev *dev, int events, struct thread *td)
635 {
636         struct tty *tp = dev->si_drv1;
637         int error, revents = 0;
638
639         error = ttydev_enter(tp);
640         if (error)
641                 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
642
643         if (events & (POLLIN|POLLRDNORM)) {
644                 /* See if we can read something. */
645                 if (ttydisc_read_poll(tp) > 0)
646                         revents |= events & (POLLIN|POLLRDNORM);
647         }
648
649         if (tp->t_flags & TF_ZOMBIE) {
650                 /* Hangup flag on zombie state. */
651                 revents |= POLLHUP;
652         } else if (events & (POLLOUT|POLLWRNORM)) {
653                 /* See if we can write something. */
654                 if (ttydisc_write_poll(tp) > 0)
655                         revents |= events & (POLLOUT|POLLWRNORM);
656         }
657
658         if (revents == 0) {
659                 if (events & (POLLIN|POLLRDNORM))
660                         selrecord(td, &tp->t_inpoll);
661                 if (events & (POLLOUT|POLLWRNORM))
662                         selrecord(td, &tp->t_outpoll);
663         }
664
665         tty_unlock(tp);
666
667         return (revents);
668 }
669
670 static int
671 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
672     int nprot, vm_memattr_t *memattr)
673 {
674         struct tty *tp = dev->si_drv1;
675         int error;
676
677         /* Handle mmap() through the driver. */
678
679         error = ttydev_enter(tp);
680         if (error)
681                 return (-1);
682         error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
683         tty_unlock(tp);
684
685         return (error);
686 }
687
688 /*
689  * kqueue support.
690  */
691
692 static void
693 tty_kqops_read_detach(struct knote *kn)
694 {
695         struct tty *tp = kn->kn_hook;
696
697         knlist_remove(&tp->t_inpoll.si_note, kn, 0);
698 }
699
700 static int
701 tty_kqops_read_event(struct knote *kn, long hint __unused)
702 {
703         struct tty *tp = kn->kn_hook;
704
705         tty_assert_locked(tp);
706
707         if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
708                 kn->kn_flags |= EV_EOF;
709                 return (1);
710         } else {
711                 kn->kn_data = ttydisc_read_poll(tp);
712                 return (kn->kn_data > 0);
713         }
714 }
715
716 static void
717 tty_kqops_write_detach(struct knote *kn)
718 {
719         struct tty *tp = kn->kn_hook;
720
721         knlist_remove(&tp->t_outpoll.si_note, kn, 0);
722 }
723
724 static int
725 tty_kqops_write_event(struct knote *kn, long hint __unused)
726 {
727         struct tty *tp = kn->kn_hook;
728
729         tty_assert_locked(tp);
730
731         if (tty_gone(tp)) {
732                 kn->kn_flags |= EV_EOF;
733                 return (1);
734         } else {
735                 kn->kn_data = ttydisc_write_poll(tp);
736                 return (kn->kn_data > 0);
737         }
738 }
739
740 static struct filterops tty_kqops_read = {
741         .f_isfd = 1,
742         .f_detach = tty_kqops_read_detach,
743         .f_event = tty_kqops_read_event,
744 };
745
746 static struct filterops tty_kqops_write = {
747         .f_isfd = 1,
748         .f_detach = tty_kqops_write_detach,
749         .f_event = tty_kqops_write_event,
750 };
751
752 static int
753 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
754 {
755         struct tty *tp = dev->si_drv1;
756         int error;
757
758         error = ttydev_enter(tp);
759         if (error)
760                 return (error);
761
762         switch (kn->kn_filter) {
763         case EVFILT_READ:
764                 kn->kn_hook = tp;
765                 kn->kn_fop = &tty_kqops_read;
766                 knlist_add(&tp->t_inpoll.si_note, kn, 1);
767                 break;
768         case EVFILT_WRITE:
769                 kn->kn_hook = tp;
770                 kn->kn_fop = &tty_kqops_write;
771                 knlist_add(&tp->t_outpoll.si_note, kn, 1);
772                 break;
773         default:
774                 error = EINVAL;
775                 break;
776         }
777
778         tty_unlock(tp);
779         return (error);
780 }
781
782 static struct cdevsw ttydev_cdevsw = {
783         .d_version      = D_VERSION,
784         .d_open         = ttydev_open,
785         .d_close        = ttydev_close,
786         .d_read         = ttydev_read,
787         .d_write        = ttydev_write,
788         .d_ioctl        = ttydev_ioctl,
789         .d_kqfilter     = ttydev_kqfilter,
790         .d_poll         = ttydev_poll,
791         .d_mmap         = ttydev_mmap,
792         .d_name         = "ttydev",
793         .d_flags        = D_TTY,
794 };
795
796 /*
797  * Init/lock-state devices
798  */
799
800 static int
801 ttyil_open(struct cdev *dev, int oflags __unused, int devtype __unused,
802     struct thread *td)
803 {
804         struct tty *tp;
805         int error;
806
807         tp = dev->si_drv1;
808         error = 0;
809         tty_lock(tp);
810         if (tty_gone(tp))
811                 error = ENODEV;
812         tty_unlock(tp);
813
814         return (error);
815 }
816
817 static int
818 ttyil_close(struct cdev *dev __unused, int flag __unused, int mode __unused,
819     struct thread *td __unused)
820 {
821
822         return (0);
823 }
824
825 static int
826 ttyil_rdwr(struct cdev *dev __unused, struct uio *uio __unused,
827     int ioflag __unused)
828 {
829
830         return (ENODEV);
831 }
832
833 static int
834 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
835     struct thread *td)
836 {
837         struct tty *tp = dev->si_drv1;
838         int error;
839
840         tty_lock(tp);
841         if (tty_gone(tp)) {
842                 error = ENODEV;
843                 goto done;
844         }
845
846         error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
847         if (error != ENOIOCTL)
848                 goto done;
849         error = 0;
850
851         switch (cmd) {
852         case TIOCGETA:
853                 /* Obtain terminal flags through tcgetattr(). */
854                 *(struct termios*)data = *(struct termios*)dev->si_drv2;
855                 break;
856         case TIOCSETA:
857                 /* Set terminal flags through tcsetattr(). */
858                 error = priv_check(td, PRIV_TTY_SETA);
859                 if (error)
860                         break;
861                 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
862                 break;
863         case TIOCGETD:
864                 *(int *)data = TTYDISC;
865                 break;
866         case TIOCGWINSZ:
867                 bzero(data, sizeof(struct winsize));
868                 break;
869         default:
870                 error = ENOTTY;
871         }
872
873 done:   tty_unlock(tp);
874         return (error);
875 }
876
877 static struct cdevsw ttyil_cdevsw = {
878         .d_version      = D_VERSION,
879         .d_open         = ttyil_open,
880         .d_close        = ttyil_close,
881         .d_read         = ttyil_rdwr,
882         .d_write        = ttyil_rdwr,
883         .d_ioctl        = ttyil_ioctl,
884         .d_name         = "ttyil",
885         .d_flags        = D_TTY,
886 };
887
888 static void
889 tty_init_termios(struct tty *tp)
890 {
891         struct termios *t = &tp->t_termios_init_in;
892
893         t->c_cflag = TTYDEF_CFLAG;
894         t->c_iflag = TTYDEF_IFLAG;
895         t->c_lflag = TTYDEF_LFLAG;
896         t->c_oflag = TTYDEF_OFLAG;
897         t->c_ispeed = TTYDEF_SPEED;
898         t->c_ospeed = TTYDEF_SPEED;
899         memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
900
901         tp->t_termios_init_out = *t;
902 }
903
904 void
905 tty_init_console(struct tty *tp, speed_t s)
906 {
907         struct termios *ti = &tp->t_termios_init_in;
908         struct termios *to = &tp->t_termios_init_out;
909
910         if (s != 0) {
911                 ti->c_ispeed = ti->c_ospeed = s;
912                 to->c_ispeed = to->c_ospeed = s;
913         }
914
915         ti->c_cflag |= CLOCAL;
916         to->c_cflag |= CLOCAL;
917 }
918
919 /*
920  * Standard device routine implementations, mostly meant for
921  * pseudo-terminal device drivers. When a driver creates a new terminal
922  * device class, missing routines are patched.
923  */
924
925 static int
926 ttydevsw_defopen(struct tty *tp __unused)
927 {
928
929         return (0);
930 }
931
932 static void
933 ttydevsw_defclose(struct tty *tp __unused)
934 {
935
936 }
937
938 static void
939 ttydevsw_defoutwakeup(struct tty *tp __unused)
940 {
941
942         panic("Terminal device has output, while not implemented");
943 }
944
945 static void
946 ttydevsw_definwakeup(struct tty *tp __unused)
947 {
948
949 }
950
951 static int
952 ttydevsw_defioctl(struct tty *tp __unused, u_long cmd __unused,
953     caddr_t data __unused, struct thread *td __unused)
954 {
955
956         return (ENOIOCTL);
957 }
958
959 static int
960 ttydevsw_defcioctl(struct tty *tp __unused, int unit __unused,
961     u_long cmd __unused, caddr_t data __unused, struct thread *td __unused)
962 {
963
964         return (ENOIOCTL);
965 }
966
967 static int
968 ttydevsw_defparam(struct tty *tp __unused, struct termios *t)
969 {
970
971         /*
972          * Allow the baud rate to be adjusted for pseudo-devices, but at
973          * least restrict it to 115200 to prevent excessive buffer
974          * usage.  Also disallow 0, to prevent foot shooting.
975          */
976         if (t->c_ispeed < B50)
977                 t->c_ispeed = B50;
978         else if (t->c_ispeed > B115200)
979                 t->c_ispeed = B115200;
980         if (t->c_ospeed < B50)
981                 t->c_ospeed = B50;
982         else if (t->c_ospeed > B115200)
983                 t->c_ospeed = B115200;
984         t->c_cflag |= CREAD;
985
986         return (0);
987 }
988
989 static int
990 ttydevsw_defmodem(struct tty *tp __unused, int sigon __unused,
991     int sigoff __unused)
992 {
993
994         /* Simulate a carrier to make the TTY layer happy. */
995         return (SER_DCD);
996 }
997
998 static int
999 ttydevsw_defmmap(struct tty *tp __unused, vm_ooffset_t offset __unused,
1000     vm_paddr_t *paddr __unused, int nprot __unused,
1001     vm_memattr_t *memattr __unused)
1002 {
1003
1004         return (-1);
1005 }
1006
1007 static void
1008 ttydevsw_defpktnotify(struct tty *tp __unused, char event __unused)
1009 {
1010
1011 }
1012
1013 static void
1014 ttydevsw_deffree(void *softc __unused)
1015 {
1016
1017         panic("Terminal device freed without a free-handler");
1018 }
1019
1020 static bool
1021 ttydevsw_defbusy(struct tty *tp __unused)
1022 {
1023
1024         return (FALSE);
1025 }
1026
1027 /*
1028  * TTY allocation and deallocation. TTY devices can be deallocated when
1029  * the driver doesn't use it anymore, when the TTY isn't a session's
1030  * controlling TTY and when the device node isn't opened through devfs.
1031  */
1032
1033 struct tty *
1034 tty_alloc(struct ttydevsw *tsw, void *sc)
1035 {
1036
1037         return (tty_alloc_mutex(tsw, sc, NULL));
1038 }
1039
1040 struct tty *
1041 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
1042 {
1043         struct tty *tp;
1044
1045         /* Make sure the driver defines all routines. */
1046 #define PATCH_FUNC(x) do {                              \
1047         if (tsw->tsw_ ## x == NULL)                     \
1048                 tsw->tsw_ ## x = ttydevsw_def ## x;     \
1049 } while (0)
1050         PATCH_FUNC(open);
1051         PATCH_FUNC(close);
1052         PATCH_FUNC(outwakeup);
1053         PATCH_FUNC(inwakeup);
1054         PATCH_FUNC(ioctl);
1055         PATCH_FUNC(cioctl);
1056         PATCH_FUNC(param);
1057         PATCH_FUNC(modem);
1058         PATCH_FUNC(mmap);
1059         PATCH_FUNC(pktnotify);
1060         PATCH_FUNC(free);
1061         PATCH_FUNC(busy);
1062 #undef PATCH_FUNC
1063
1064         tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
1065         tp->t_devsw = tsw;
1066         tp->t_devswsoftc = sc;
1067         tp->t_flags = tsw->tsw_flags;
1068         tp->t_drainwait = tty_drainwait;
1069
1070         tty_init_termios(tp);
1071
1072         cv_init(&tp->t_inwait, "ttyin");
1073         cv_init(&tp->t_outwait, "ttyout");
1074         cv_init(&tp->t_outserwait, "ttyosr");
1075         cv_init(&tp->t_bgwait, "ttybg");
1076         cv_init(&tp->t_dcdwait, "ttydcd");
1077
1078         /* Allow drivers to use a custom mutex to lock the TTY. */
1079         if (mutex != NULL) {
1080                 tp->t_mtx = mutex;
1081         } else {
1082                 tp->t_mtx = &tp->t_mtxobj;
1083                 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1084         }
1085
1086         knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1087         knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1088
1089         return (tp);
1090 }
1091
1092 static void
1093 tty_dealloc(void *arg)
1094 {
1095         struct tty *tp = arg;
1096
1097         /*
1098          * ttyydev_leave() usually frees the i/o queues earlier, but it is
1099          * not always called between queue allocation and here.  The queues
1100          * may be allocated by ioctls on a pty control device without the
1101          * corresponding pty slave device ever being open, or after it is
1102          * closed.
1103          */
1104         ttyinq_free(&tp->t_inq);
1105         ttyoutq_free(&tp->t_outq);
1106         seldrain(&tp->t_inpoll);
1107         seldrain(&tp->t_outpoll);
1108         knlist_destroy(&tp->t_inpoll.si_note);
1109         knlist_destroy(&tp->t_outpoll.si_note);
1110
1111         cv_destroy(&tp->t_inwait);
1112         cv_destroy(&tp->t_outwait);
1113         cv_destroy(&tp->t_bgwait);
1114         cv_destroy(&tp->t_dcdwait);
1115         cv_destroy(&tp->t_outserwait);
1116
1117         if (tp->t_mtx == &tp->t_mtxobj)
1118                 mtx_destroy(&tp->t_mtxobj);
1119         ttydevsw_free(tp);
1120         free(tp, M_TTY);
1121 }
1122
1123 static void
1124 tty_rel_free(struct tty *tp)
1125 {
1126         struct cdev *dev;
1127
1128         tty_assert_locked(tp);
1129
1130 #define TF_ACTIVITY     (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1131         if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1132                 /* TTY is still in use. */
1133                 tty_unlock(tp);
1134                 return;
1135         }
1136
1137         /* Stop asynchronous I/O. */
1138         funsetown(&tp->t_sigio);
1139
1140         /* TTY can be deallocated. */
1141         dev = tp->t_dev;
1142         tp->t_dev = NULL;
1143         tty_unlock(tp);
1144
1145         if (dev != NULL) {
1146                 sx_xlock(&tty_list_sx);
1147                 TAILQ_REMOVE(&tty_list, tp, t_list);
1148                 tty_list_count--;
1149                 sx_xunlock(&tty_list_sx);
1150                 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1151         }
1152 }
1153
1154 void
1155 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1156 {
1157
1158         MPASS(tp->t_sessioncnt > 0);
1159         tty_assert_locked(tp);
1160
1161         if (tp->t_pgrp == pg)
1162                 tp->t_pgrp = NULL;
1163
1164         tty_unlock(tp);
1165 }
1166
1167 void
1168 tty_rel_sess(struct tty *tp, struct session *sess)
1169 {
1170
1171         MPASS(tp->t_sessioncnt > 0);
1172
1173         /* Current session has left. */
1174         if (tp->t_session == sess) {
1175                 tp->t_session = NULL;
1176                 MPASS(tp->t_pgrp == NULL);
1177         }
1178         tp->t_sessioncnt--;
1179         tty_rel_free(tp);
1180 }
1181
1182 void
1183 tty_rel_gone(struct tty *tp)
1184 {
1185
1186         tty_assert_locked(tp);
1187         MPASS(!tty_gone(tp));
1188
1189         /* Simulate carrier removal. */
1190         ttydisc_modem(tp, 0);
1191
1192         /* Wake up all blocked threads. */
1193         tty_wakeup(tp, FREAD|FWRITE);
1194         cv_broadcast(&tp->t_bgwait);
1195         cv_broadcast(&tp->t_dcdwait);
1196
1197         tp->t_flags |= TF_GONE;
1198         tty_rel_free(tp);
1199 }
1200
1201 static int
1202 tty_drop_ctty(struct tty *tp, struct proc *p)
1203 {
1204         struct session *session;
1205         struct vnode *vp;
1206
1207         /*
1208          * This looks terrible, but it's generally safe as long as the tty
1209          * hasn't gone away while we had the lock dropped.  All of our sanity
1210          * checking that this operation is OK happens after we've picked it back
1211          * up, so other state changes are generally not fatal and the potential
1212          * for this particular operation to happen out-of-order in a
1213          * multithreaded scenario is likely a non-issue.
1214          */
1215         tty_unlock(tp);
1216         sx_xlock(&proctree_lock);
1217         tty_lock(tp);
1218         if (tty_gone(tp)) {
1219                 sx_xunlock(&proctree_lock);
1220                 return (ENODEV);
1221         }
1222
1223         /*
1224          * If the session doesn't have a controlling TTY, or if we weren't
1225          * invoked on the controlling TTY, we'll return ENOIOCTL as we've
1226          * historically done.
1227          */
1228         session = p->p_session;
1229         if (session->s_ttyp == NULL || session->s_ttyp != tp) {
1230                 sx_xunlock(&proctree_lock);
1231                 return (ENOTTY);
1232         }
1233
1234         if (!SESS_LEADER(p)) {
1235                 sx_xunlock(&proctree_lock);
1236                 return (EPERM);
1237         }
1238
1239         PROC_LOCK(p);
1240         SESS_LOCK(session);
1241         vp = session->s_ttyvp;
1242         session->s_ttyp = NULL;
1243         session->s_ttyvp = NULL;
1244         session->s_ttydp = NULL;
1245         SESS_UNLOCK(session);
1246
1247         tp->t_sessioncnt--;
1248         p->p_flag &= ~P_CONTROLT;
1249         PROC_UNLOCK(p);
1250         sx_xunlock(&proctree_lock);
1251
1252         /*
1253          * If we did have a vnode, release our reference.  Ordinarily we manage
1254          * these at the devfs layer, but we can't necessarily know that we were
1255          * invoked on the vnode referenced in the session (i.e. the vnode we
1256          * hold a reference to).  We explicitly don't check VBAD/VI_DOOMED here
1257          * to avoid a vnode leak -- in circumstances elsewhere where we'd hit a
1258          * VI_DOOMED vnode, release has been deferred until the controlling TTY
1259          * is either changed or released.
1260          */
1261         if (vp != NULL)
1262                 vrele(vp);
1263         return (0);
1264 }
1265
1266 /*
1267  * Exposing information about current TTY's through sysctl
1268  */
1269
1270 static void
1271 tty_to_xtty(struct tty *tp, struct xtty *xt)
1272 {
1273
1274         tty_assert_locked(tp);
1275
1276         xt->xt_size = sizeof(struct xtty);
1277         xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1278         xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1279         xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1280         xt->xt_inlow = tp->t_inlow;
1281         xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1282         xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1283         xt->xt_outlow = tp->t_outlow;
1284         xt->xt_column = tp->t_column;
1285         xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1286         xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1287         xt->xt_flags = tp->t_flags;
1288         xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : (uint32_t)NODEV;
1289 }
1290
1291 static int
1292 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1293 {
1294         unsigned long lsize;
1295         struct xtty *xtlist, *xt;
1296         struct tty *tp;
1297         int error;
1298
1299         sx_slock(&tty_list_sx);
1300         lsize = tty_list_count * sizeof(struct xtty);
1301         if (lsize == 0) {
1302                 sx_sunlock(&tty_list_sx);
1303                 return (0);
1304         }
1305
1306         xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1307
1308         TAILQ_FOREACH(tp, &tty_list, t_list) {
1309                 tty_lock(tp);
1310                 tty_to_xtty(tp, xt);
1311                 tty_unlock(tp);
1312                 xt++;
1313         }
1314         sx_sunlock(&tty_list_sx);
1315
1316         error = SYSCTL_OUT(req, xtlist, lsize);
1317         free(xtlist, M_TTY);
1318         return (error);
1319 }
1320
1321 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1322         0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1323
1324 /*
1325  * Device node creation. Device has been set up, now we can expose it to
1326  * the user.
1327  */
1328
1329 int
1330 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1331     const char *fmt, ...)
1332 {
1333         va_list ap;
1334         struct make_dev_args args;
1335         struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1336         const char *prefix = "tty";
1337         char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1338         uid_t uid;
1339         gid_t gid;
1340         mode_t mode;
1341         int error;
1342
1343         /* Remove "tty" prefix from devices like PTY's. */
1344         if (tp->t_flags & TF_NOPREFIX)
1345                 prefix = "";
1346
1347         va_start(ap, fmt);
1348         vsnrprintf(name, sizeof name, 32, fmt, ap);
1349         va_end(ap);
1350
1351         if (cred == NULL) {
1352                 /* System device. */
1353                 uid = UID_ROOT;
1354                 gid = GID_WHEEL;
1355                 mode = S_IRUSR|S_IWUSR;
1356         } else {
1357                 /* User device. */
1358                 uid = cred->cr_ruid;
1359                 gid = GID_TTY;
1360                 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1361         }
1362
1363         flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1364         flags |= MAKEDEV_CHECKNAME;
1365
1366         /* Master call-in device. */
1367         make_dev_args_init(&args);
1368         args.mda_flags = flags;
1369         args.mda_devsw = &ttydev_cdevsw;
1370         args.mda_cr = cred;
1371         args.mda_uid = uid;
1372         args.mda_gid = gid;
1373         args.mda_mode = mode;
1374         args.mda_si_drv1 = tp;
1375         error = make_dev_s(&args, &dev, "%s%s", prefix, name);
1376         if (error != 0)
1377                 return (error);
1378         tp->t_dev = dev;
1379
1380         init = lock = cua = cinit = clock = NULL;
1381
1382         /* Slave call-in devices. */
1383         if (tp->t_flags & TF_INITLOCK) {
1384                 args.mda_devsw = &ttyil_cdevsw;
1385                 args.mda_unit = TTYUNIT_INIT;
1386                 args.mda_si_drv1 = tp;
1387                 args.mda_si_drv2 = &tp->t_termios_init_in;
1388                 error = make_dev_s(&args, &init, "%s%s.init", prefix, name);
1389                 if (error != 0)
1390                         goto fail;
1391                 dev_depends(dev, init);
1392
1393                 args.mda_unit = TTYUNIT_LOCK;
1394                 args.mda_si_drv2 = &tp->t_termios_lock_in;
1395                 error = make_dev_s(&args, &lock, "%s%s.lock", prefix, name);
1396                 if (error != 0)
1397                         goto fail;
1398                 dev_depends(dev, lock);
1399         }
1400
1401         /* Call-out devices. */
1402         if (tp->t_flags & TF_CALLOUT) {
1403                 make_dev_args_init(&args);
1404                 args.mda_flags = flags;
1405                 args.mda_devsw = &ttydev_cdevsw;
1406                 args.mda_cr = cred;
1407                 args.mda_uid = UID_UUCP;
1408                 args.mda_gid = GID_DIALER;
1409                 args.mda_mode = 0660;
1410                 args.mda_unit = TTYUNIT_CALLOUT;
1411                 args.mda_si_drv1 = tp;
1412                 error = make_dev_s(&args, &cua, "cua%s", name);
1413                 if (error != 0)
1414                         goto fail;
1415                 dev_depends(dev, cua);
1416
1417                 /* Slave call-out devices. */
1418                 if (tp->t_flags & TF_INITLOCK) {
1419                         args.mda_devsw = &ttyil_cdevsw;
1420                         args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1421                         args.mda_si_drv2 = &tp->t_termios_init_out;
1422                         error = make_dev_s(&args, &cinit, "cua%s.init", name);
1423                         if (error != 0)
1424                                 goto fail;
1425                         dev_depends(dev, cinit);
1426
1427                         args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1428                         args.mda_si_drv2 = &tp->t_termios_lock_out;
1429                         error = make_dev_s(&args, &clock, "cua%s.lock", name);
1430                         if (error != 0)
1431                                 goto fail;
1432                         dev_depends(dev, clock);
1433                 }
1434         }
1435
1436         sx_xlock(&tty_list_sx);
1437         TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1438         tty_list_count++;
1439         sx_xunlock(&tty_list_sx);
1440
1441         return (0);
1442
1443 fail:
1444         destroy_dev(dev);
1445         if (init)
1446                 destroy_dev(init);
1447         if (lock)
1448                 destroy_dev(lock);
1449         if (cinit)
1450                 destroy_dev(cinit);
1451         if (clock)
1452                 destroy_dev(clock);
1453
1454         return (error);
1455 }
1456
1457 /*
1458  * Signalling processes.
1459  */
1460
1461 void
1462 tty_signal_sessleader(struct tty *tp, int sig)
1463 {
1464         struct proc *p;
1465
1466         tty_assert_locked(tp);
1467         MPASS(sig >= 1 && sig < NSIG);
1468
1469         /* Make signals start output again. */
1470         tp->t_flags &= ~TF_STOPPED;
1471
1472         if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1473                 p = tp->t_session->s_leader;
1474                 PROC_LOCK(p);
1475                 kern_psignal(p, sig);
1476                 PROC_UNLOCK(p);
1477         }
1478 }
1479
1480 void
1481 tty_signal_pgrp(struct tty *tp, int sig)
1482 {
1483         ksiginfo_t ksi;
1484
1485         tty_assert_locked(tp);
1486         MPASS(sig >= 1 && sig < NSIG);
1487
1488         /* Make signals start output again. */
1489         tp->t_flags &= ~TF_STOPPED;
1490
1491         if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1492                 tty_info(tp);
1493         if (tp->t_pgrp != NULL) {
1494                 ksiginfo_init(&ksi);
1495                 ksi.ksi_signo = sig;
1496                 ksi.ksi_code = SI_KERNEL;
1497                 PGRP_LOCK(tp->t_pgrp);
1498                 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1499                 PGRP_UNLOCK(tp->t_pgrp);
1500         }
1501 }
1502
1503 void
1504 tty_wakeup(struct tty *tp, int flags)
1505 {
1506
1507         if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1508                 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1509
1510         if (flags & FWRITE) {
1511                 cv_broadcast(&tp->t_outwait);
1512                 selwakeup(&tp->t_outpoll);
1513                 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1514         }
1515         if (flags & FREAD) {
1516                 cv_broadcast(&tp->t_inwait);
1517                 selwakeup(&tp->t_inpoll);
1518                 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1519         }
1520 }
1521
1522 int
1523 tty_wait(struct tty *tp, struct cv *cv)
1524 {
1525         int error;
1526         int revokecnt = tp->t_revokecnt;
1527
1528         tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1529         MPASS(!tty_gone(tp));
1530
1531         error = cv_wait_sig(cv, tp->t_mtx);
1532
1533         /* Bail out when the device slipped away. */
1534         if (tty_gone(tp))
1535                 return (ENXIO);
1536
1537         /* Restart the system call when we may have been revoked. */
1538         if (tp->t_revokecnt != revokecnt)
1539                 return (ERESTART);
1540
1541         return (error);
1542 }
1543
1544 int
1545 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1546 {
1547         int error;
1548         int revokecnt = tp->t_revokecnt;
1549
1550         tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1551         MPASS(!tty_gone(tp));
1552
1553         error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1554
1555         /* Bail out when the device slipped away. */
1556         if (tty_gone(tp))
1557                 return (ENXIO);
1558
1559         /* Restart the system call when we may have been revoked. */
1560         if (tp->t_revokecnt != revokecnt)
1561                 return (ERESTART);
1562
1563         return (error);
1564 }
1565
1566 void
1567 tty_flush(struct tty *tp, int flags)
1568 {
1569
1570         if (flags & FWRITE) {
1571                 tp->t_flags &= ~TF_HIWAT_OUT;
1572                 ttyoutq_flush(&tp->t_outq);
1573                 tty_wakeup(tp, FWRITE);
1574                 if (!tty_gone(tp)) {
1575                         ttydevsw_outwakeup(tp);
1576                         ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1577                 }
1578         }
1579         if (flags & FREAD) {
1580                 tty_hiwat_in_unblock(tp);
1581                 ttyinq_flush(&tp->t_inq);
1582                 tty_wakeup(tp, FREAD);
1583                 if (!tty_gone(tp)) {
1584                         ttydevsw_inwakeup(tp);
1585                         ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1586                 }
1587         }
1588 }
1589
1590 void
1591 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1592 {
1593
1594         if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1595                 return;
1596         tp->t_winsize = *wsz;
1597         tty_signal_pgrp(tp, SIGWINCH);
1598 }
1599
1600 static int
1601 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1602     struct thread *td)
1603 {
1604         int error;
1605
1606         switch (cmd) {
1607         /*
1608          * Modem commands.
1609          * The SER_* and TIOCM_* flags are the same, but one bit
1610          * shifted. I don't know why.
1611          */
1612         case TIOCSDTR:
1613                 ttydevsw_modem(tp, SER_DTR, 0);
1614                 return (0);
1615         case TIOCCDTR:
1616                 ttydevsw_modem(tp, 0, SER_DTR);
1617                 return (0);
1618         case TIOCMSET: {
1619                 int bits = *(int *)data;
1620                 ttydevsw_modem(tp,
1621                     (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1622                     ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1623                 return (0);
1624         }
1625         case TIOCMBIS: {
1626                 int bits = *(int *)data;
1627                 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1628                 return (0);
1629         }
1630         case TIOCMBIC: {
1631                 int bits = *(int *)data;
1632                 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1633                 return (0);
1634         }
1635         case TIOCMGET:
1636                 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1637                 return (0);
1638
1639         case FIOASYNC:
1640                 if (*(int *)data)
1641                         tp->t_flags |= TF_ASYNC;
1642                 else
1643                         tp->t_flags &= ~TF_ASYNC;
1644                 return (0);
1645         case FIONBIO:
1646                 /* This device supports non-blocking operation. */
1647                 return (0);
1648         case FIONREAD:
1649                 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1650                 return (0);
1651         case FIONWRITE:
1652         case TIOCOUTQ:
1653                 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1654                 return (0);
1655         case FIOSETOWN:
1656                 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1657                         /* Not allowed to set ownership. */
1658                         return (ENOTTY);
1659
1660                 /* Temporarily unlock the TTY to set ownership. */
1661                 tty_unlock(tp);
1662                 error = fsetown(*(int *)data, &tp->t_sigio);
1663                 tty_lock(tp);
1664                 return (error);
1665         case FIOGETOWN:
1666                 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1667                         /* Not allowed to set ownership. */
1668                         return (ENOTTY);
1669
1670                 /* Get ownership. */
1671                 *(int *)data = fgetown(&tp->t_sigio);
1672                 return (0);
1673         case TIOCGETA:
1674                 /* Obtain terminal flags through tcgetattr(). */
1675                 *(struct termios*)data = tp->t_termios;
1676                 return (0);
1677         case TIOCSETA:
1678         case TIOCSETAW:
1679         case TIOCSETAF: {
1680                 struct termios *t = data;
1681
1682                 /*
1683                  * Who makes up these funny rules? According to POSIX,
1684                  * input baud rate is set equal to the output baud rate
1685                  * when zero.
1686                  */
1687                 if (t->c_ispeed == 0)
1688                         t->c_ispeed = t->c_ospeed;
1689
1690                 /* Discard any unsupported bits. */
1691                 t->c_iflag &= TTYSUP_IFLAG;
1692                 t->c_oflag &= TTYSUP_OFLAG;
1693                 t->c_lflag &= TTYSUP_LFLAG;
1694                 t->c_cflag &= TTYSUP_CFLAG;
1695
1696                 /* Set terminal flags through tcsetattr(). */
1697                 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1698                         error = tty_drain(tp, 0);
1699                         if (error)
1700                                 return (error);
1701                         if (cmd == TIOCSETAF)
1702                                 tty_flush(tp, FREAD);
1703                 }
1704
1705                 /*
1706                  * Only call param() when the flags really change.
1707                  */
1708                 if ((t->c_cflag & CIGNORE) == 0 &&
1709                     (tp->t_termios.c_cflag != t->c_cflag ||
1710                     ((tp->t_termios.c_iflag ^ t->c_iflag) &
1711                     (IXON|IXOFF|IXANY)) ||
1712                     tp->t_termios.c_ispeed != t->c_ispeed ||
1713                     tp->t_termios.c_ospeed != t->c_ospeed)) {
1714                         error = ttydevsw_param(tp, t);
1715                         if (error)
1716                                 return (error);
1717
1718                         /* XXX: CLOCAL? */
1719
1720                         tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1721                         tp->t_termios.c_ispeed = t->c_ispeed;
1722                         tp->t_termios.c_ospeed = t->c_ospeed;
1723
1724                         /* Baud rate has changed - update watermarks. */
1725                         error = tty_watermarks(tp);
1726                         if (error)
1727                                 return (error);
1728                 }
1729
1730                 /* Copy new non-device driver parameters. */
1731                 tp->t_termios.c_iflag = t->c_iflag;
1732                 tp->t_termios.c_oflag = t->c_oflag;
1733                 tp->t_termios.c_lflag = t->c_lflag;
1734                 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1735
1736                 ttydisc_optimize(tp);
1737
1738                 if ((t->c_lflag & ICANON) == 0) {
1739                         /*
1740                          * When in non-canonical mode, wake up all
1741                          * readers. Canonicalize any partial input. VMIN
1742                          * and VTIME could also be adjusted.
1743                          */
1744                         ttyinq_canonicalize(&tp->t_inq);
1745                         tty_wakeup(tp, FREAD);
1746                 }
1747
1748                 /*
1749                  * For packet mode: notify the PTY consumer that VSTOP
1750                  * and VSTART may have been changed.
1751                  */
1752                 if (tp->t_termios.c_iflag & IXON &&
1753                     tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1754                     tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1755                         ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1756                 else
1757                         ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1758                 return (0);
1759         }
1760         case TIOCGETD:
1761                 /* For compatibility - we only support TTYDISC. */
1762                 *(int *)data = TTYDISC;
1763                 return (0);
1764         case TIOCGPGRP:
1765                 if (!tty_is_ctty(tp, td->td_proc))
1766                         return (ENOTTY);
1767
1768                 if (tp->t_pgrp != NULL)
1769                         *(int *)data = tp->t_pgrp->pg_id;
1770                 else
1771                         *(int *)data = NO_PID;
1772                 return (0);
1773         case TIOCGSID:
1774                 if (!tty_is_ctty(tp, td->td_proc))
1775                         return (ENOTTY);
1776
1777                 MPASS(tp->t_session);
1778                 *(int *)data = tp->t_session->s_sid;
1779                 return (0);
1780         case TIOCNOTTY:
1781                 return (tty_drop_ctty(tp, td->td_proc));
1782         case TIOCSCTTY: {
1783                 struct proc *p = td->td_proc;
1784
1785                 /* XXX: This looks awful. */
1786                 tty_unlock(tp);
1787                 sx_xlock(&proctree_lock);
1788                 tty_lock(tp);
1789
1790                 if (!SESS_LEADER(p)) {
1791                         /* Only the session leader may do this. */
1792                         sx_xunlock(&proctree_lock);
1793                         return (EPERM);
1794                 }
1795
1796                 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1797                         /* This is already our controlling TTY. */
1798                         sx_xunlock(&proctree_lock);
1799                         return (0);
1800                 }
1801
1802                 if (p->p_session->s_ttyp != NULL ||
1803                     (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1804                     tp->t_session->s_ttyvp->v_type != VBAD)) {
1805                         /*
1806                          * There is already a relation between a TTY and
1807                          * a session, or the caller is not the session
1808                          * leader.
1809                          *
1810                          * Allow the TTY to be stolen when the vnode is
1811                          * invalid, but the reference to the TTY is
1812                          * still active.  This allows immediate reuse of
1813                          * TTYs of which the session leader has been
1814                          * killed or the TTY revoked.
1815                          */
1816                         sx_xunlock(&proctree_lock);
1817                         return (EPERM);
1818                 }
1819
1820                 /* Connect the session to the TTY. */
1821                 tp->t_session = p->p_session;
1822                 tp->t_session->s_ttyp = tp;
1823                 tp->t_sessioncnt++;
1824
1825                 /* Assign foreground process group. */
1826                 tp->t_pgrp = p->p_pgrp;
1827                 PROC_LOCK(p);
1828                 p->p_flag |= P_CONTROLT;
1829                 PROC_UNLOCK(p);
1830
1831                 sx_xunlock(&proctree_lock);
1832                 return (0);
1833         }
1834         case TIOCSPGRP: {
1835                 struct pgrp *pg;
1836
1837                 /*
1838                  * XXX: Temporarily unlock the TTY to locate the process
1839                  * group. This code would be lot nicer if we would ever
1840                  * decompose proctree_lock.
1841                  */
1842                 tty_unlock(tp);
1843                 sx_slock(&proctree_lock);
1844                 pg = pgfind(*(int *)data);
1845                 if (pg != NULL)
1846                         PGRP_UNLOCK(pg);
1847                 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1848                         sx_sunlock(&proctree_lock);
1849                         tty_lock(tp);
1850                         return (EPERM);
1851                 }
1852                 tty_lock(tp);
1853
1854                 /*
1855                  * Determine if this TTY is the controlling TTY after
1856                  * relocking the TTY.
1857                  */
1858                 if (!tty_is_ctty(tp, td->td_proc)) {
1859                         sx_sunlock(&proctree_lock);
1860                         return (ENOTTY);
1861                 }
1862                 tp->t_pgrp = pg;
1863                 sx_sunlock(&proctree_lock);
1864
1865                 /* Wake up the background process groups. */
1866                 cv_broadcast(&tp->t_bgwait);
1867                 return (0);
1868         }
1869         case TIOCFLUSH: {
1870                 int flags = *(int *)data;
1871
1872                 if (flags == 0)
1873                         flags = (FREAD|FWRITE);
1874                 else
1875                         flags &= (FREAD|FWRITE);
1876                 tty_flush(tp, flags);
1877                 return (0);
1878         }
1879         case TIOCDRAIN:
1880                 /* Drain TTY output. */
1881                 return tty_drain(tp, 0);
1882         case TIOCGDRAINWAIT:
1883                 *(int *)data = tp->t_drainwait;
1884                 return (0);
1885         case TIOCSDRAINWAIT:
1886                 error = priv_check(td, PRIV_TTY_DRAINWAIT);
1887                 if (error == 0)
1888                         tp->t_drainwait = *(int *)data;
1889                 return (error);
1890         case TIOCCONS:
1891                 /* Set terminal as console TTY. */
1892                 if (*(int *)data) {
1893                         error = priv_check(td, PRIV_TTY_CONSOLE);
1894                         if (error)
1895                                 return (error);
1896
1897                         /*
1898                          * XXX: constty should really need to be locked!
1899                          * XXX: allow disconnected constty's to be stolen!
1900                          */
1901
1902                         if (constty == tp)
1903                                 return (0);
1904                         if (constty != NULL)
1905                                 return (EBUSY);
1906
1907                         tty_unlock(tp);
1908                         constty_set(tp);
1909                         tty_lock(tp);
1910                 } else if (constty == tp) {
1911                         constty_clear();
1912                 }
1913                 return (0);
1914         case TIOCGWINSZ:
1915                 /* Obtain window size. */
1916                 *(struct winsize*)data = tp->t_winsize;
1917                 return (0);
1918         case TIOCSWINSZ:
1919                 /* Set window size. */
1920                 tty_set_winsize(tp, data);
1921                 return (0);
1922         case TIOCEXCL:
1923                 tp->t_flags |= TF_EXCLUDE;
1924                 return (0);
1925         case TIOCNXCL:
1926                 tp->t_flags &= ~TF_EXCLUDE;
1927                 return (0);
1928         case TIOCSTOP:
1929                 tp->t_flags |= TF_STOPPED;
1930                 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1931                 return (0);
1932         case TIOCSTART:
1933                 tp->t_flags &= ~TF_STOPPED;
1934                 ttydevsw_outwakeup(tp);
1935                 ttydevsw_pktnotify(tp, TIOCPKT_START);
1936                 return (0);
1937         case TIOCSTAT:
1938                 tty_info(tp);
1939                 return (0);
1940         case TIOCSTI:
1941                 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1942                         return (EPERM);
1943                 if (!tty_is_ctty(tp, td->td_proc) &&
1944                     priv_check(td, PRIV_TTY_STI))
1945                         return (EACCES);
1946                 ttydisc_rint(tp, *(char *)data, 0);
1947                 ttydisc_rint_done(tp);
1948                 return (0);
1949         }
1950
1951 #ifdef COMPAT_43TTY
1952         return tty_ioctl_compat(tp, cmd, data, fflag, td);
1953 #else /* !COMPAT_43TTY */
1954         return (ENOIOCTL);
1955 #endif /* COMPAT_43TTY */
1956 }
1957
1958 int
1959 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1960 {
1961         int error;
1962
1963         tty_assert_locked(tp);
1964
1965         if (tty_gone(tp))
1966                 return (ENXIO);
1967
1968         error = ttydevsw_ioctl(tp, cmd, data, td);
1969         if (error == ENOIOCTL)
1970                 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1971
1972         return (error);
1973 }
1974
1975 dev_t
1976 tty_udev(struct tty *tp)
1977 {
1978
1979         if (tp->t_dev)
1980                 return (dev2udev(tp->t_dev));
1981         else
1982                 return (NODEV);
1983 }
1984
1985 int
1986 tty_checkoutq(struct tty *tp)
1987 {
1988
1989         /* 256 bytes should be enough to print a log message. */
1990         return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1991 }
1992
1993 void
1994 tty_hiwat_in_block(struct tty *tp)
1995 {
1996
1997         if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1998             tp->t_termios.c_iflag & IXOFF &&
1999             tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
2000                 /*
2001                  * Input flow control. Only enter the high watermark when we
2002                  * can successfully store the VSTOP character.
2003                  */
2004                 if (ttyoutq_write_nofrag(&tp->t_outq,
2005                     &tp->t_termios.c_cc[VSTOP], 1) == 0)
2006                         tp->t_flags |= TF_HIWAT_IN;
2007         } else {
2008                 /* No input flow control. */
2009                 tp->t_flags |= TF_HIWAT_IN;
2010         }
2011 }
2012
2013 void
2014 tty_hiwat_in_unblock(struct tty *tp)
2015 {
2016
2017         if (tp->t_flags & TF_HIWAT_IN &&
2018             tp->t_termios.c_iflag & IXOFF &&
2019             tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
2020                 /*
2021                  * Input flow control. Only leave the high watermark when we
2022                  * can successfully store the VSTART character.
2023                  */
2024                 if (ttyoutq_write_nofrag(&tp->t_outq,
2025                     &tp->t_termios.c_cc[VSTART], 1) == 0)
2026                         tp->t_flags &= ~TF_HIWAT_IN;
2027         } else {
2028                 /* No input flow control. */
2029                 tp->t_flags &= ~TF_HIWAT_IN;
2030         }
2031
2032         if (!tty_gone(tp))
2033                 ttydevsw_inwakeup(tp);
2034 }
2035
2036 /*
2037  * TTY hooks interface.
2038  */
2039
2040 static int
2041 ttyhook_defrint(struct tty *tp, char c, int flags)
2042 {
2043
2044         if (ttyhook_rint_bypass(tp, &c, 1) != 1)
2045                 return (-1);
2046
2047         return (0);
2048 }
2049
2050 int
2051 ttyhook_register(struct tty **rtp, struct proc *p, int fd, struct ttyhook *th,
2052     void *softc)
2053 {
2054         struct tty *tp;
2055         struct file *fp;
2056         struct cdev *dev;
2057         struct cdevsw *cdp;
2058         struct filedesc *fdp;
2059         cap_rights_t rights;
2060         int error, ref;
2061
2062         /* Validate the file descriptor. */
2063         fdp = p->p_fd;
2064         error = fget_unlocked(fdp, fd, cap_rights_init(&rights, CAP_TTYHOOK),
2065             &fp, NULL);
2066         if (error != 0)
2067                 return (error);
2068         if (fp->f_ops == &badfileops) {
2069                 error = EBADF;
2070                 goto done1;
2071         }
2072
2073         /*
2074          * Make sure the vnode is bound to a character device.
2075          * Unlocked check for the vnode type is ok there, because we
2076          * only shall prevent calling devvn_refthread on the file that
2077          * never has been opened over a character device.
2078          */
2079         if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
2080                 error = EINVAL;
2081                 goto done1;
2082         }
2083
2084         /* Make sure it is a TTY. */
2085         cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
2086         if (cdp == NULL) {
2087                 error = ENXIO;
2088                 goto done1;
2089         }
2090         if (dev != fp->f_data) {
2091                 error = ENXIO;
2092                 goto done2;
2093         }
2094         if (cdp != &ttydev_cdevsw) {
2095                 error = ENOTTY;
2096                 goto done2;
2097         }
2098         tp = dev->si_drv1;
2099
2100         /* Try to attach the hook to the TTY. */
2101         error = EBUSY;
2102         tty_lock(tp);
2103         MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
2104         if (tp->t_flags & TF_HOOK)
2105                 goto done3;
2106
2107         tp->t_flags |= TF_HOOK;
2108         tp->t_hook = th;
2109         tp->t_hooksoftc = softc;
2110         *rtp = tp;
2111         error = 0;
2112
2113         /* Maybe we can switch into bypass mode now. */
2114         ttydisc_optimize(tp);
2115
2116         /* Silently convert rint() calls to rint_bypass() when possible. */
2117         if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
2118                 th->th_rint = ttyhook_defrint;
2119
2120 done3:  tty_unlock(tp);
2121 done2:  dev_relthread(dev, ref);
2122 done1:  fdrop(fp, curthread);
2123         return (error);
2124 }
2125
2126 void
2127 ttyhook_unregister(struct tty *tp)
2128 {
2129
2130         tty_assert_locked(tp);
2131         MPASS(tp->t_flags & TF_HOOK);
2132
2133         /* Disconnect the hook. */
2134         tp->t_flags &= ~TF_HOOK;
2135         tp->t_hook = NULL;
2136
2137         /* Maybe we need to leave bypass mode. */
2138         ttydisc_optimize(tp);
2139
2140         /* Maybe deallocate the TTY as well. */
2141         tty_rel_free(tp);
2142 }
2143
2144 /*
2145  * /dev/console handling.
2146  */
2147
2148 static int
2149 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
2150 {
2151         struct tty *tp;
2152
2153         /* System has no console device. */
2154         if (dev_console_filename == NULL)
2155                 return (ENXIO);
2156
2157         /* Look up corresponding TTY by device name. */
2158         sx_slock(&tty_list_sx);
2159         TAILQ_FOREACH(tp, &tty_list, t_list) {
2160                 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
2161                         dev_console->si_drv1 = tp;
2162                         break;
2163                 }
2164         }
2165         sx_sunlock(&tty_list_sx);
2166
2167         /* System console has no TTY associated. */
2168         if (dev_console->si_drv1 == NULL)
2169                 return (ENXIO);
2170
2171         return (ttydev_open(dev, oflags, devtype, td));
2172 }
2173
2174 static int
2175 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2176 {
2177
2178         log_console(uio);
2179
2180         return (ttydev_write(dev, uio, ioflag));
2181 }
2182
2183 /*
2184  * /dev/console is a little different than normal TTY's.  When opened,
2185  * it determines which TTY to use.  When data gets written to it, it
2186  * will be logged in the kernel message buffer.
2187  */
2188 static struct cdevsw ttyconsdev_cdevsw = {
2189         .d_version      = D_VERSION,
2190         .d_open         = ttyconsdev_open,
2191         .d_close        = ttydev_close,
2192         .d_read         = ttydev_read,
2193         .d_write        = ttyconsdev_write,
2194         .d_ioctl        = ttydev_ioctl,
2195         .d_kqfilter     = ttydev_kqfilter,
2196         .d_poll         = ttydev_poll,
2197         .d_mmap         = ttydev_mmap,
2198         .d_name         = "ttyconsdev",
2199         .d_flags        = D_TTY,
2200 };
2201
2202 static void
2203 ttyconsdev_init(void *unused __unused)
2204 {
2205
2206         dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2207             NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2208 }
2209
2210 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2211
2212 void
2213 ttyconsdev_select(const char *name)
2214 {
2215
2216         dev_console_filename = name;
2217 }
2218
2219 /*
2220  * Debugging routines.
2221  */
2222
2223 #include "opt_ddb.h"
2224 #ifdef DDB
2225 #include <ddb/ddb.h>
2226 #include <ddb/db_sym.h>
2227
2228 static const struct {
2229         int flag;
2230         char val;
2231 } ttystates[] = {
2232 #if 0
2233         { TF_NOPREFIX,          'N' },
2234 #endif
2235         { TF_INITLOCK,          'I' },
2236         { TF_CALLOUT,           'C' },
2237
2238         /* Keep these together -> 'Oi' and 'Oo'. */
2239         { TF_OPENED,            'O' },
2240         { TF_OPENED_IN,         'i' },
2241         { TF_OPENED_OUT,        'o' },
2242         { TF_OPENED_CONS,       'c' },
2243
2244         { TF_GONE,              'G' },
2245         { TF_OPENCLOSE,         'B' },
2246         { TF_ASYNC,             'Y' },
2247         { TF_LITERAL,           'L' },
2248
2249         /* Keep these together -> 'Hi' and 'Ho'. */
2250         { TF_HIWAT,             'H' },
2251         { TF_HIWAT_IN,          'i' },
2252         { TF_HIWAT_OUT,         'o' },
2253
2254         { TF_STOPPED,           'S' },
2255         { TF_EXCLUDE,           'X' },
2256         { TF_BYPASS,            'l' },
2257         { TF_ZOMBIE,            'Z' },
2258         { TF_HOOK,              's' },
2259
2260         /* Keep these together -> 'bi' and 'bo'. */
2261         { TF_BUSY,              'b' },
2262         { TF_BUSY_IN,           'i' },
2263         { TF_BUSY_OUT,          'o' },
2264
2265         { 0,                    '\0'},
2266 };
2267
2268 #define TTY_FLAG_BITS \
2269         "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN" \
2270         "\5OPENED_OUT\6OPENED_CONS\7GONE\10OPENCLOSE" \
2271         "\11ASYNC\12LITERAL\13HIWAT_IN\14HIWAT_OUT" \
2272         "\15STOPPED\16EXCLUDE\17BYPASS\20ZOMBIE" \
2273         "\21HOOK\22BUSY_IN\23BUSY_OUT"
2274
2275 #define DB_PRINTSYM(name, addr) \
2276         db_printf("%s  " #name ": ", sep); \
2277         db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2278         db_printf("\n");
2279
2280 static void
2281 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2282 {
2283
2284         db_printf("%sdevsw: ", sep);
2285         db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2286         db_printf(" (%p)\n", tsw);
2287         DB_PRINTSYM(open, tsw->tsw_open);
2288         DB_PRINTSYM(close, tsw->tsw_close);
2289         DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2290         DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2291         DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2292         DB_PRINTSYM(param, tsw->tsw_param);
2293         DB_PRINTSYM(modem, tsw->tsw_modem);
2294         DB_PRINTSYM(mmap, tsw->tsw_mmap);
2295         DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2296         DB_PRINTSYM(free, tsw->tsw_free);
2297 }
2298
2299 static void
2300 _db_show_hooks(const char *sep, const struct ttyhook *th)
2301 {
2302
2303         db_printf("%shook: ", sep);
2304         db_printsym((db_addr_t)th, DB_STGY_ANY);
2305         db_printf(" (%p)\n", th);
2306         if (th == NULL)
2307                 return;
2308         DB_PRINTSYM(rint, th->th_rint);
2309         DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2310         DB_PRINTSYM(rint_done, th->th_rint_done);
2311         DB_PRINTSYM(rint_poll, th->th_rint_poll);
2312         DB_PRINTSYM(getc_inject, th->th_getc_inject);
2313         DB_PRINTSYM(getc_capture, th->th_getc_capture);
2314         DB_PRINTSYM(getc_poll, th->th_getc_poll);
2315         DB_PRINTSYM(close, th->th_close);
2316 }
2317
2318 static void
2319 _db_show_termios(const char *name, const struct termios *t)
2320 {
2321
2322         db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2323             "lflag 0x%x ispeed %u ospeed %u\n", name,
2324             t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2325             t->c_ispeed, t->c_ospeed);
2326 }
2327
2328 /* DDB command to show TTY statistics. */
2329 DB_SHOW_COMMAND(tty, db_show_tty)
2330 {
2331         struct tty *tp;
2332
2333         if (!have_addr) {
2334                 db_printf("usage: show tty <addr>\n");
2335                 return;
2336         }
2337         tp = (struct tty *)addr;
2338
2339         db_printf("%p: %s\n", tp, tty_devname(tp));
2340         db_printf("\tmtx: %p\n", tp->t_mtx);
2341         db_printf("\tflags: 0x%b\n", tp->t_flags, TTY_FLAG_BITS);
2342         db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2343
2344         /* Buffering mechanisms. */
2345         db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2346             "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2347             tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2348             tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2349         db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2350             &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2351             tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2352         db_printf("\tinlow: %zu\n", tp->t_inlow);
2353         db_printf("\toutlow: %zu\n", tp->t_outlow);
2354         _db_show_termios("\ttermios", &tp->t_termios);
2355         db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2356             tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2357             tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2358         db_printf("\tcolumn: %u\n", tp->t_column);
2359         db_printf("\twritepos: %u\n", tp->t_writepos);
2360         db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2361
2362         /* Init/lock-state devices. */
2363         _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2364         _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2365         _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2366         _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2367
2368         /* Hooks */
2369         _db_show_devsw("\t", tp->t_devsw);
2370         _db_show_hooks("\t", tp->t_hook);
2371
2372         /* Process info. */
2373         db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2374             tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2375             tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2376         db_printf("\tsession: %p", tp->t_session);
2377         if (tp->t_session != NULL)
2378             db_printf(" count %u leader %p tty %p sid %d login %s",
2379                 tp->t_session->s_count, tp->t_session->s_leader,
2380                 tp->t_session->s_ttyp, tp->t_session->s_sid,
2381                 tp->t_session->s_login);
2382         db_printf("\n");
2383         db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2384         db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2385         db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2386         db_printf("\tdev: %p\n", tp->t_dev);
2387 }
2388
2389 /* DDB command to list TTYs. */
2390 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2391 {
2392         struct tty *tp;
2393         size_t isiz, osiz;
2394         int i, j;
2395
2396         /* Make the output look like `pstat -t'. */
2397         db_printf("PTR        ");
2398 #if defined(__LP64__)
2399         db_printf("        ");
2400 #endif
2401         db_printf("      LINE   INQ  CAN  LIN  LOW  OUTQ  USE  LOW   "
2402             "COL  SESS  PGID STATE\n");
2403
2404         TAILQ_FOREACH(tp, &tty_list, t_list) {
2405                 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2406                 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2407
2408                 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d "
2409                     "%5d ", tp, tty_devname(tp), isiz,
2410                     tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2411                     tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2412                     isiz - tp->t_inlow, osiz,
2413                     tp->t_outq.to_end - tp->t_outq.to_begin,
2414                     osiz - tp->t_outlow, MIN(tp->t_column, 99999),
2415                     tp->t_session ? tp->t_session->s_sid : 0,
2416                     tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2417
2418                 /* Flag bits. */
2419                 for (i = j = 0; ttystates[i].flag; i++)
2420                         if (tp->t_flags & ttystates[i].flag) {
2421                                 db_printf("%c", ttystates[i].val);
2422                                 j++;
2423                         }
2424                 if (j == 0)
2425                         db_printf("-");
2426                 db_printf("\n");
2427         }
2428 }
2429 #endif /* DDB */