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

Cache object: 0ff4ba77ade6df143806f8b4d28a63ff


[ 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.