The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/tty.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: ca84ea4559144eadc12c49eb0d8fa78a


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.