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

Cache object: c3b56312cb3233034a7c83d5ef8448eb


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