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_pty.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) 1982, 1986, 1989, 1993
    3  *      The Regents of the University of California.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)tty_pty.c   8.4 (Berkeley) 2/20/95
   34  * $FreeBSD: releng/5.0/sys/kern/tty_pty.c 94860 2002-04-16 17:09:22Z jhb $
   35  */
   36 
   37 /*
   38  * Pseudo-teletype Driver
   39  * (Actually two drivers, requiring two entries in 'cdevsw')
   40  */
   41 #include "opt_compat.h"
   42 #include <sys/param.h>
   43 #include <sys/systm.h>
   44 #include <sys/lock.h>
   45 #include <sys/mutex.h>
   46 #include <sys/sx.h>
   47 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
   48 #include <sys/ioctl_compat.h>
   49 #endif
   50 #include <sys/proc.h>
   51 #include <sys/tty.h>
   52 #include <sys/conf.h>
   53 #include <sys/fcntl.h>
   54 #include <sys/poll.h>
   55 #include <sys/kernel.h>
   56 #include <sys/vnode.h>
   57 #include <sys/signalvar.h>
   58 #include <sys/malloc.h>
   59 
   60 static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
   61 
   62 static void ptsstart(struct tty *tp);
   63 static void ptsstop(struct tty *tp, int rw);
   64 static void ptcwakeup(struct tty *tp, int flag);
   65 static dev_t ptyinit(dev_t cdev);
   66 
   67 static  d_open_t        ptsopen;
   68 static  d_close_t       ptsclose;
   69 static  d_read_t        ptsread;
   70 static  d_write_t       ptswrite;
   71 static  d_ioctl_t       ptyioctl;
   72 static  d_open_t        ptcopen;
   73 static  d_close_t       ptcclose;
   74 static  d_read_t        ptcread;
   75 static  d_write_t       ptcwrite;
   76 static  d_poll_t        ptcpoll;
   77 
   78 #define CDEV_MAJOR_S    5
   79 static struct cdevsw pts_cdevsw = {
   80         /* open */      ptsopen,
   81         /* close */     ptsclose,
   82         /* read */      ptsread,
   83         /* write */     ptswrite,
   84         /* ioctl */     ptyioctl,
   85         /* poll */      ttypoll,
   86         /* mmap */      nommap,
   87         /* strategy */  nostrategy,
   88         /* name */      "pts",
   89         /* maj */       CDEV_MAJOR_S,
   90         /* dump */      nodump,
   91         /* psize */     nopsize,
   92         /* flags */     D_TTY | D_KQFILTER,
   93         /* kqfilter */  ttykqfilter,
   94 };
   95 
   96 #define CDEV_MAJOR_C    6
   97 static struct cdevsw ptc_cdevsw = {
   98         /* open */      ptcopen,
   99         /* close */     ptcclose,
  100         /* read */      ptcread,
  101         /* write */     ptcwrite,
  102         /* ioctl */     ptyioctl,
  103         /* poll */      ptcpoll,
  104         /* mmap */      nommap,
  105         /* strategy */  nostrategy,
  106         /* name */      "ptc",
  107         /* maj */       CDEV_MAJOR_C,
  108         /* dump */      nodump,
  109         /* psize */     nopsize,
  110         /* flags */     D_TTY | D_KQFILTER,
  111         /* kqfilter */  ttykqfilter,
  112 };
  113 
  114 #define BUFSIZ 100              /* Chunk size iomoved to/from user */
  115 
  116 struct  pt_ioctl {
  117         int     pt_flags;
  118         struct  selinfo pt_selr, pt_selw;
  119         u_char  pt_send;
  120         u_char  pt_ucntl;
  121         struct tty pt_tty;
  122         dev_t   devs, devc;
  123         struct  prison *pt_prison;
  124 };
  125 
  126 #define PF_PKT          0x08            /* packet mode */
  127 #define PF_STOPPED      0x10            /* user told stopped */
  128 #define PF_REMOTE       0x20            /* remote and flow controlled input */
  129 #define PF_NOSTOP       0x40
  130 #define PF_UCNTL        0x80            /* user control mode */
  131 
  132 static char *names = "pqrsPQRS";
  133 /*
  134  * This function creates and initializes a pts/ptc pair
  135  *
  136  * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
  137  * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
  138  *
  139  * XXX: define and add mapping of upper minor bits to allow more 
  140  *      than 256 ptys.
  141  */
  142 static dev_t
  143 ptyinit(dev_t devc)
  144 {
  145         dev_t devs;
  146         struct pt_ioctl *pt;
  147         int n;
  148 
  149         n = minor(devc);
  150         /* For now we only map the lower 8 bits of the minor */
  151         if (n & ~0xff)
  152                 return (NODEV);
  153 
  154         devc->si_flags &= ~SI_CHEAPCLONE;
  155 
  156         pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
  157         pt->devs = devs = make_dev(&pts_cdevsw, n,
  158             UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
  159         pt->devc = devc;
  160 
  161         devs->si_drv1 = devc->si_drv1 = pt;
  162         devs->si_tty = devc->si_tty = &pt->pt_tty;
  163         pt->pt_tty.t_dev = devs;
  164         ttyregister(&pt->pt_tty);
  165         return (devc);
  166 }
  167 
  168 /*ARGSUSED*/
  169 static  int
  170 ptsopen(dev, flag, devtype, td)
  171         dev_t dev;
  172         int flag, devtype;
  173         struct thread *td;
  174 {
  175         register struct tty *tp;
  176         int error;
  177         struct pt_ioctl *pti;
  178 
  179         if (!dev->si_drv1)
  180                 return(ENXIO);  
  181         pti = dev->si_drv1;
  182         tp = dev->si_tty;
  183         if ((tp->t_state & TS_ISOPEN) == 0) {
  184                 ttychars(tp);           /* Set up default chars */
  185                 tp->t_iflag = TTYDEF_IFLAG;
  186                 tp->t_oflag = TTYDEF_OFLAG;
  187                 tp->t_lflag = TTYDEF_LFLAG;
  188                 tp->t_cflag = TTYDEF_CFLAG;
  189                 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
  190         } else if (tp->t_state & TS_XCLUDE && suser(td)) {
  191                 return (EBUSY);
  192         } else if (pti->pt_prison != td->td_ucred->cr_prison) {
  193                 return (EBUSY);
  194         }
  195         if (tp->t_oproc)                        /* Ctrlr still around. */
  196                 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
  197         while ((tp->t_state & TS_CARR_ON) == 0) {
  198                 if (flag&FNONBLOCK)
  199                         break;
  200                 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
  201                                  "ptsopn", 0);
  202                 if (error)
  203                         return (error);
  204         }
  205         error = (*linesw[tp->t_line].l_open)(dev, tp);
  206         if (error == 0)
  207                 ptcwakeup(tp, FREAD|FWRITE);
  208         return (error);
  209 }
  210 
  211 static  int
  212 ptsclose(dev, flag, mode, td)
  213         dev_t dev;
  214         int flag, mode;
  215         struct thread *td;
  216 {
  217         register struct tty *tp;
  218         int err;
  219 
  220         tp = dev->si_tty;
  221         err = (*linesw[tp->t_line].l_close)(tp, flag);
  222         ptsstop(tp, FREAD|FWRITE);
  223         (void) ttyclose(tp);
  224         return (err);
  225 }
  226 
  227 static  int
  228 ptsread(dev, uio, flag)
  229         dev_t dev;
  230         struct uio *uio;
  231         int flag;
  232 {
  233         struct thread *td = curthread;
  234         struct proc *p = td->td_proc;
  235         register struct tty *tp = dev->si_tty;
  236         register struct pt_ioctl *pti = dev->si_drv1;
  237         struct pgrp *pg;
  238         int error = 0;
  239 
  240 again:
  241         if (pti->pt_flags & PF_REMOTE) {
  242                 while (isbackground(p, tp)) {
  243                         sx_slock(&proctree_lock);
  244                         PROC_LOCK(p);
  245                         if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
  246                             SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
  247                             p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) {
  248                                 PROC_UNLOCK(p);
  249                                 sx_sunlock(&proctree_lock);
  250                                 return (EIO);
  251                         }
  252                         pg = p->p_pgrp;
  253                         PROC_UNLOCK(p);
  254                         PGRP_LOCK(pg);
  255                         sx_sunlock(&proctree_lock);
  256                         pgsignal(pg, SIGTTIN, 1);
  257                         PGRP_UNLOCK(pg);
  258                         error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
  259                                          0);
  260                         if (error)
  261                                 return (error);
  262                 }
  263                 if (tp->t_canq.c_cc == 0) {
  264                         if (flag & IO_NDELAY)
  265                                 return (EWOULDBLOCK);
  266                         error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
  267                                          "ptsin", 0);
  268                         if (error)
  269                                 return (error);
  270                         goto again;
  271                 }
  272                 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
  273                         if (ureadc(getc(&tp->t_canq), uio) < 0) {
  274                                 error = EFAULT;
  275                                 break;
  276                         }
  277                 if (tp->t_canq.c_cc == 1)
  278                         (void) getc(&tp->t_canq);
  279                 if (tp->t_canq.c_cc)
  280                         return (error);
  281         } else
  282                 if (tp->t_oproc)
  283                         error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
  284         ptcwakeup(tp, FWRITE);
  285         return (error);
  286 }
  287 
  288 /*
  289  * Write to pseudo-tty.
  290  * Wakeups of controlling tty will happen
  291  * indirectly, when tty driver calls ptsstart.
  292  */
  293 static  int
  294 ptswrite(dev, uio, flag)
  295         dev_t dev;
  296         struct uio *uio;
  297         int flag;
  298 {
  299         register struct tty *tp;
  300 
  301         tp = dev->si_tty;
  302         if (tp->t_oproc == 0)
  303                 return (EIO);
  304         return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
  305 }
  306 
  307 /*
  308  * Start output on pseudo-tty.
  309  * Wake up process selecting or sleeping for input from controlling tty.
  310  */
  311 static void
  312 ptsstart(tp)
  313         struct tty *tp;
  314 {
  315         register struct pt_ioctl *pti = tp->t_dev->si_drv1;
  316 
  317         if (tp->t_state & TS_TTSTOP)
  318                 return;
  319         if (pti->pt_flags & PF_STOPPED) {
  320                 pti->pt_flags &= ~PF_STOPPED;
  321                 pti->pt_send = TIOCPKT_START;
  322         }
  323         ptcwakeup(tp, FREAD);
  324 }
  325 
  326 static void
  327 ptcwakeup(tp, flag)
  328         struct tty *tp;
  329         int flag;
  330 {
  331         struct pt_ioctl *pti = tp->t_dev->si_drv1;
  332 
  333         if (flag & FREAD) {
  334                 selwakeup(&pti->pt_selr);
  335                 wakeup(TSA_PTC_READ(tp));
  336         }
  337         if (flag & FWRITE) {
  338                 selwakeup(&pti->pt_selw);
  339                 wakeup(TSA_PTC_WRITE(tp));
  340         }
  341 }
  342 
  343 static  int
  344 ptcopen(dev, flag, devtype, td)
  345         dev_t dev;
  346         int flag, devtype;
  347         struct thread *td;
  348 {
  349         register struct tty *tp;
  350         struct pt_ioctl *pti;
  351 
  352         if (!dev->si_drv1)
  353                 ptyinit(dev);
  354         if (!dev->si_drv1)
  355                 return(ENXIO);  
  356         tp = dev->si_tty;
  357         if (tp->t_oproc)
  358                 return (EIO);
  359         tp->t_timeout = -1;
  360         tp->t_oproc = ptsstart;
  361         tp->t_stop = ptsstop;
  362         (void)(*linesw[tp->t_line].l_modem)(tp, 1);
  363         tp->t_lflag &= ~EXTPROC;
  364         pti = dev->si_drv1;
  365         pti->pt_prison = td->td_ucred->cr_prison;
  366         pti->pt_flags = 0;
  367         pti->pt_send = 0;
  368         pti->pt_ucntl = 0;
  369         return (0);
  370 }
  371 
  372 static  int
  373 ptcclose(dev, flags, fmt, td)
  374         dev_t dev;
  375         int flags;
  376         int fmt;
  377         struct thread *td;
  378 {
  379         register struct tty *tp;
  380 
  381         tp = dev->si_tty;
  382         (void)(*linesw[tp->t_line].l_modem)(tp, 0);
  383 
  384         /*
  385          * XXX MDMBUF makes no sense for ptys but would inhibit the above
  386          * l_modem().  CLOCAL makes sense but isn't supported.   Special
  387          * l_modem()s that ignore carrier drop make no sense for ptys but
  388          * may be in use because other parts of the line discipline make
  389          * sense for ptys.  Recover by doing everything that a normal
  390          * ttymodem() would have done except for sending a SIGHUP.
  391          */
  392         if (tp->t_state & TS_ISOPEN) {
  393                 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
  394                 tp->t_state |= TS_ZOMBIE;
  395                 ttyflush(tp, FREAD | FWRITE);
  396         }
  397 
  398         tp->t_oproc = 0;                /* mark closed */
  399         return (0);
  400 }
  401 
  402 static  int
  403 ptcread(dev, uio, flag)
  404         dev_t dev;
  405         struct uio *uio;
  406         int flag;
  407 {
  408         register struct tty *tp = dev->si_tty;
  409         struct pt_ioctl *pti = dev->si_drv1;
  410         char buf[BUFSIZ];
  411         int error = 0, cc;
  412 
  413         /*
  414          * We want to block until the slave
  415          * is open, and there's something to read;
  416          * but if we lost the slave or we're NBIO,
  417          * then return the appropriate error instead.
  418          */
  419         for (;;) {
  420                 if (tp->t_state&TS_ISOPEN) {
  421                         if (pti->pt_flags&PF_PKT && pti->pt_send) {
  422                                 error = ureadc((int)pti->pt_send, uio);
  423                                 if (error)
  424                                         return (error);
  425                                 if (pti->pt_send & TIOCPKT_IOCTL) {
  426                                         cc = min(uio->uio_resid,
  427                                                 sizeof(tp->t_termios));
  428                                         uiomove((caddr_t)&tp->t_termios, cc,
  429                                                 uio);
  430                                 }
  431                                 pti->pt_send = 0;
  432                                 return (0);
  433                         }
  434                         if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
  435                                 error = ureadc((int)pti->pt_ucntl, uio);
  436                                 if (error)
  437                                         return (error);
  438                                 pti->pt_ucntl = 0;
  439                                 return (0);
  440                         }
  441                         if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
  442                                 break;
  443                 }
  444                 if ((tp->t_state & TS_CONNECTED) == 0)
  445                         return (0);     /* EOF */
  446                 if (flag & IO_NDELAY)
  447                         return (EWOULDBLOCK);
  448                 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
  449                 if (error)
  450                         return (error);
  451         }
  452         if (pti->pt_flags & (PF_PKT|PF_UCNTL))
  453                 error = ureadc(0, uio);
  454         while (uio->uio_resid > 0 && error == 0) {
  455                 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
  456                 if (cc <= 0)
  457                         break;
  458                 error = uiomove(buf, cc, uio);
  459         }
  460         ttwwakeup(tp);
  461         return (error);
  462 }
  463 
  464 static  void
  465 ptsstop(tp, flush)
  466         register struct tty *tp;
  467         int flush;
  468 {
  469         struct pt_ioctl *pti = tp->t_dev->si_drv1;
  470         int flag;
  471 
  472         /* note: FLUSHREAD and FLUSHWRITE already ok */
  473         if (flush == 0) {
  474                 flush = TIOCPKT_STOP;
  475                 pti->pt_flags |= PF_STOPPED;
  476         } else
  477                 pti->pt_flags &= ~PF_STOPPED;
  478         pti->pt_send |= flush;
  479         /* change of perspective */
  480         flag = 0;
  481         if (flush & FREAD)
  482                 flag |= FWRITE;
  483         if (flush & FWRITE)
  484                 flag |= FREAD;
  485         ptcwakeup(tp, flag);
  486 }
  487 
  488 static  int
  489 ptcpoll(dev, events, td)
  490         dev_t dev;
  491         int events;
  492         struct thread *td;
  493 {
  494         register struct tty *tp = dev->si_tty;
  495         struct pt_ioctl *pti = dev->si_drv1;
  496         int revents = 0;
  497         int s;
  498 
  499         if ((tp->t_state & TS_CONNECTED) == 0)
  500                 return (seltrue(dev, events, td) | POLLHUP);
  501 
  502         /*
  503          * Need to block timeouts (ttrstart).
  504          */
  505         s = spltty();
  506 
  507         if (events & (POLLIN | POLLRDNORM))
  508                 if ((tp->t_state & TS_ISOPEN) &&
  509                     ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
  510                      ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
  511                      ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
  512                         revents |= events & (POLLIN | POLLRDNORM);
  513 
  514         if (events & (POLLOUT | POLLWRNORM))
  515                 if (tp->t_state & TS_ISOPEN &&
  516                     ((pti->pt_flags & PF_REMOTE) ?
  517                      (tp->t_canq.c_cc == 0) : 
  518                      ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
  519                       (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
  520                         revents |= events & (POLLOUT | POLLWRNORM);
  521 
  522         if (events & POLLHUP)
  523                 if ((tp->t_state & TS_CARR_ON) == 0)
  524                         revents |= POLLHUP;
  525 
  526         if (revents == 0) {
  527                 if (events & (POLLIN | POLLRDNORM))
  528                         selrecord(td, &pti->pt_selr);
  529 
  530                 if (events & (POLLOUT | POLLWRNORM)) 
  531                         selrecord(td, &pti->pt_selw);
  532         }
  533         splx(s);
  534 
  535         return (revents);
  536 }
  537 
  538 static  int
  539 ptcwrite(dev, uio, flag)
  540         dev_t dev;
  541         register struct uio *uio;
  542         int flag;
  543 {
  544         register struct tty *tp = dev->si_tty;
  545         register u_char *cp = 0;
  546         register int cc = 0;
  547         u_char locbuf[BUFSIZ];
  548         int cnt = 0;
  549         struct pt_ioctl *pti = dev->si_drv1;
  550         int error = 0;
  551 
  552 again:
  553         if ((tp->t_state&TS_ISOPEN) == 0)
  554                 goto block;
  555         if (pti->pt_flags & PF_REMOTE) {
  556                 if (tp->t_canq.c_cc)
  557                         goto block;
  558                 while ((uio->uio_resid > 0 || cc > 0) &&
  559                        tp->t_canq.c_cc < TTYHOG - 1) {
  560                         if (cc == 0) {
  561                                 cc = min(uio->uio_resid, BUFSIZ);
  562                                 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
  563                                 cp = locbuf;
  564                                 error = uiomove((caddr_t)cp, cc, uio);
  565                                 if (error)
  566                                         return (error);
  567                                 /* check again for safety */
  568                                 if ((tp->t_state & TS_ISOPEN) == 0) {
  569                                         /* adjust as usual */
  570                                         uio->uio_resid += cc;
  571                                         return (EIO);
  572                                 }
  573                         }
  574                         if (cc > 0) {
  575                                 cc = b_to_q((char *)cp, cc, &tp->t_canq);
  576                                 /*
  577                                  * XXX we don't guarantee that the canq size
  578                                  * is >= TTYHOG, so the above b_to_q() may
  579                                  * leave some bytes uncopied.  However, space
  580                                  * is guaranteed for the null terminator if
  581                                  * we don't fail here since (TTYHOG - 1) is
  582                                  * not a multiple of CBSIZE.
  583                                  */
  584                                 if (cc > 0)
  585                                         break;
  586                         }
  587                 }
  588                 /* adjust for data copied in but not written */
  589                 uio->uio_resid += cc;
  590                 (void) putc(0, &tp->t_canq);
  591                 ttwakeup(tp);
  592                 wakeup(TSA_PTS_READ(tp));
  593                 return (0);
  594         }
  595         while (uio->uio_resid > 0 || cc > 0) {
  596                 if (cc == 0) {
  597                         cc = min(uio->uio_resid, BUFSIZ);
  598                         cp = locbuf;
  599                         error = uiomove((caddr_t)cp, cc, uio);
  600                         if (error)
  601                                 return (error);
  602                         /* check again for safety */
  603                         if ((tp->t_state & TS_ISOPEN) == 0) {
  604                                 /* adjust for data copied in but not written */
  605                                 uio->uio_resid += cc;
  606                                 return (EIO);
  607                         }
  608                 }
  609                 while (cc > 0) {
  610                         if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
  611                            (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
  612                                 wakeup(TSA_HUP_OR_INPUT(tp));
  613                                 goto block;
  614                         }
  615                         (*linesw[tp->t_line].l_rint)(*cp++, tp);
  616                         cnt++;
  617                         cc--;
  618                 }
  619                 cc = 0;
  620         }
  621         return (0);
  622 block:
  623         /*
  624          * Come here to wait for slave to open, for space
  625          * in outq, or space in rawq, or an empty canq.
  626          */
  627         if ((tp->t_state & TS_CONNECTED) == 0) {
  628                 /* adjust for data copied in but not written */
  629                 uio->uio_resid += cc;
  630                 return (EIO);
  631         }
  632         if (flag & IO_NDELAY) {
  633                 /* adjust for data copied in but not written */
  634                 uio->uio_resid += cc;
  635                 if (cnt == 0)
  636                         return (EWOULDBLOCK);
  637                 return (0);
  638         }
  639         error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
  640         if (error) {
  641                 /* adjust for data copied in but not written */
  642                 uio->uio_resid += cc;
  643                 return (error);
  644         }
  645         goto again;
  646 }
  647 
  648 /*ARGSUSED*/
  649 static  int
  650 ptyioctl(dev, cmd, data, flag, td)
  651         dev_t dev;
  652         u_long cmd;
  653         caddr_t data;
  654         int flag;
  655         struct thread *td;
  656 {
  657         register struct tty *tp = dev->si_tty;
  658         register struct pt_ioctl *pti = dev->si_drv1;
  659         register u_char *cc = tp->t_cc;
  660         int stop, error;
  661 
  662         if (devsw(dev)->d_open == ptcopen) {
  663                 switch (cmd) {
  664 
  665                 case TIOCGPGRP:
  666                         /*
  667                          * We avoid calling ttioctl on the controller since,
  668                          * in that case, tp must be the controlling terminal.
  669                          */
  670                         *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
  671                         return (0);
  672 
  673                 case TIOCPKT:
  674                         if (*(int *)data) {
  675                                 if (pti->pt_flags & PF_UCNTL)
  676                                         return (EINVAL);
  677                                 pti->pt_flags |= PF_PKT;
  678                         } else
  679                                 pti->pt_flags &= ~PF_PKT;
  680                         return (0);
  681 
  682                 case TIOCUCNTL:
  683                         if (*(int *)data) {
  684                                 if (pti->pt_flags & PF_PKT)
  685                                         return (EINVAL);
  686                                 pti->pt_flags |= PF_UCNTL;
  687                         } else
  688                                 pti->pt_flags &= ~PF_UCNTL;
  689                         return (0);
  690 
  691                 case TIOCREMOTE:
  692                         if (*(int *)data)
  693                                 pti->pt_flags |= PF_REMOTE;
  694                         else
  695                                 pti->pt_flags &= ~PF_REMOTE;
  696                         ttyflush(tp, FREAD|FWRITE);
  697                         return (0);
  698                 }
  699 
  700                 /*
  701                  * The rest of the ioctls shouldn't be called until 
  702                  * the slave is open.
  703                  */
  704                 if ((tp->t_state & TS_ISOPEN) == 0)
  705                         return (EAGAIN);
  706 
  707                 switch (cmd) {
  708 #ifdef COMPAT_43
  709                 case TIOCSETP:
  710                 case TIOCSETN:
  711 #endif
  712                 case TIOCSETD:
  713                 case TIOCSETA:
  714                 case TIOCSETAW:
  715                 case TIOCSETAF:
  716                         /*
  717                          * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
  718                          * ttywflush(tp) will hang if there are characters in
  719                          * the outq.
  720                          */
  721                         ndflush(&tp->t_outq, tp->t_outq.c_cc);
  722                         break;
  723 
  724                 case TIOCSIG:
  725                         if (*(unsigned int *)data >= NSIG ||
  726                             *(unsigned int *)data == 0)
  727                                 return(EINVAL);
  728                         if ((tp->t_lflag&NOFLSH) == 0)
  729                                 ttyflush(tp, FREAD|FWRITE);
  730                         if (tp->t_pgrp != NULL) {
  731                                 PGRP_LOCK(tp->t_pgrp);
  732                                 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
  733                                 PGRP_UNLOCK(tp->t_pgrp);
  734                         }
  735                         if ((*(unsigned int *)data == SIGINFO) &&
  736                             ((tp->t_lflag&NOKERNINFO) == 0))
  737                                 ttyinfo(tp);
  738                         return(0);
  739                 }
  740         }
  741         if (cmd == TIOCEXT) {
  742                 /*
  743                  * When the EXTPROC bit is being toggled, we need
  744                  * to send an TIOCPKT_IOCTL if the packet driver
  745                  * is turned on.
  746                  */
  747                 if (*(int *)data) {
  748                         if (pti->pt_flags & PF_PKT) {
  749                                 pti->pt_send |= TIOCPKT_IOCTL;
  750                                 ptcwakeup(tp, FREAD);
  751                         }
  752                         tp->t_lflag |= EXTPROC;
  753                 } else {
  754                         if ((tp->t_lflag & EXTPROC) &&
  755                             (pti->pt_flags & PF_PKT)) {
  756                                 pti->pt_send |= TIOCPKT_IOCTL;
  757                                 ptcwakeup(tp, FREAD);
  758                         }
  759                         tp->t_lflag &= ~EXTPROC;
  760                 }
  761                 return(0);
  762         }
  763         error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
  764         if (error == ENOIOCTL)
  765                  error = ttioctl(tp, cmd, data, flag);
  766         if (error == ENOIOCTL) {
  767                 if (pti->pt_flags & PF_UCNTL &&
  768                     (cmd & ~0xff) == UIOCCMD(0)) {
  769                         if (cmd & 0xff) {
  770                                 pti->pt_ucntl = (u_char)cmd;
  771                                 ptcwakeup(tp, FREAD);
  772                         }
  773                         return (0);
  774                 }
  775                 error = ENOTTY;
  776         }
  777         /*
  778          * If external processing and packet mode send ioctl packet.
  779          */
  780         if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
  781                 switch(cmd) {
  782                 case TIOCSETA:
  783                 case TIOCSETAW:
  784                 case TIOCSETAF:
  785 #ifdef COMPAT_43
  786                 case TIOCSETP:
  787                 case TIOCSETN:
  788 #endif
  789 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
  790                 case TIOCSETC:
  791                 case TIOCSLTC:
  792                 case TIOCLBIS:
  793                 case TIOCLBIC:
  794                 case TIOCLSET:
  795 #endif
  796                         pti->pt_send |= TIOCPKT_IOCTL;
  797                         ptcwakeup(tp, FREAD);
  798                 default:
  799                         break;
  800                 }
  801         }
  802         stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
  803                 && CCEQ(cc[VSTART], CTRL('q'));
  804         if (pti->pt_flags & PF_NOSTOP) {
  805                 if (stop) {
  806                         pti->pt_send &= ~TIOCPKT_NOSTOP;
  807                         pti->pt_send |= TIOCPKT_DOSTOP;
  808                         pti->pt_flags &= ~PF_NOSTOP;
  809                         ptcwakeup(tp, FREAD);
  810                 }
  811         } else {
  812                 if (!stop) {
  813                         pti->pt_send &= ~TIOCPKT_DOSTOP;
  814                         pti->pt_send |= TIOCPKT_NOSTOP;
  815                         pti->pt_flags |= PF_NOSTOP;
  816                         ptcwakeup(tp, FREAD);
  817                 }
  818         }
  819         return (error);
  820 }
  821 
  822 
  823 static void ptc_drvinit(void *unused);
  824 
  825 static void pty_clone(void *arg, char *name, int namelen, dev_t *dev);
  826 
  827 static void
  828 pty_clone(arg, name, namelen, dev)
  829         void *arg;
  830         char *name;
  831         int namelen;
  832         dev_t *dev;
  833 {
  834         int u;
  835 
  836         if (*dev != NODEV)
  837                 return;
  838         if (bcmp(name, "pty", 3) != 0)
  839                 return;
  840         if (name[5] != '\0')
  841                 return;
  842         switch (name[3]) {
  843         case 'p': u =   0; break;
  844         case 'q': u =  32; break;
  845         case 'r': u =  64; break;
  846         case 's': u =  96; break;
  847         case 'P': u = 128; break;
  848         case 'Q': u = 160; break;
  849         case 'R': u = 192; break;
  850         case 'S': u = 224; break;
  851         default: return;
  852         }
  853         if (name[4] >= '' && name[4] <= '9')
  854                 u += name[4] - '';
  855         else if (name[4] >= 'a' && name[4] <= 'v')
  856                 u += name[4] - 'a' + 10;
  857         else
  858                 return;
  859         *dev = make_dev(&ptc_cdevsw, u,
  860             UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
  861         (*dev)->si_flags |= SI_CHEAPCLONE;
  862         return;
  863 }
  864 
  865 static void
  866 ptc_drvinit(unused)
  867         void *unused;
  868 {
  869         EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
  870         cdevsw_add(&pts_cdevsw);
  871         cdevsw_add(&ptc_cdevsw);
  872 }
  873 
  874 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)

Cache object: 1c55f3f010885034d171de31c6f57621


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