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-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
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  * 4. Neither the name of the University nor the names of its contributors
   14  *    may be used to endorse or promote products derived from this software
   15  *    without specific prior written permission.
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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  *      @(#)tty_pty.c   8.4 (Berkeley) 2/20/95
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD$");
   34 
   35 /*
   36  * Pseudo-teletype Driver
   37  * (Actually two drivers, requiring two entries in 'cdevsw')
   38  */
   39 #include "opt_compat.h"
   40 #include "opt_tty.h"
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/libkern.h>
   44 #include <sys/lock.h>
   45 #include <sys/mutex.h>
   46 #include <sys/sx.h>
   47 #ifndef BURN_BRIDGES
   48 #if defined(COMPAT_43)
   49 #include <sys/ioctl_compat.h>
   50 #endif
   51 #endif
   52 #include <sys/proc.h>
   53 #include <sys/tty.h>
   54 #include <sys/conf.h>
   55 #include <sys/fcntl.h>
   56 #include <sys/poll.h>
   57 #include <sys/kernel.h>
   58 #include <sys/uio.h>
   59 #include <sys/signalvar.h>
   60 #include <sys/malloc.h>
   61 
   62 static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
   63 
   64 static void ptsstart(struct tty *tp);
   65 static void ptsstop(struct tty *tp, int rw);
   66 static void ptcwakeup(struct tty *tp, int flag);
   67 static struct cdev *ptyinit(struct cdev *cdev, struct thread *td);
   68 
   69 static  d_open_t        ptsopen;
   70 static  d_close_t       ptsclose;
   71 static  d_read_t        ptsread;
   72 static  d_write_t       ptswrite;
   73 static  d_ioctl_t       ptsioctl;
   74 static  d_open_t        ptcopen;
   75 static  d_close_t       ptcclose;
   76 static  d_read_t        ptcread;
   77 static  d_ioctl_t       ptcioctl;
   78 static  d_write_t       ptcwrite;
   79 static  d_poll_t        ptcpoll;
   80 
   81 static struct cdevsw pts_cdevsw = {
   82         .d_version =    D_VERSION,
   83         .d_open =       ptsopen,
   84         .d_close =      ptsclose,
   85         .d_read =       ptsread,
   86         .d_write =      ptswrite,
   87         .d_ioctl =      ptsioctl,
   88         .d_name =       "pts",
   89         .d_flags =      D_TTY | D_NEEDGIANT,
   90 };
   91 
   92 static struct cdevsw ptc_cdevsw = {
   93         .d_version =    D_VERSION,
   94         .d_open =       ptcopen,
   95         .d_close =      ptcclose,
   96         .d_read =       ptcread,
   97         .d_write =      ptcwrite,
   98         .d_ioctl =      ptcioctl,
   99         .d_poll =       ptcpoll,
  100         .d_name =       "ptc",
  101         .d_flags =      D_TTY | D_NEEDGIANT,
  102 };
  103 
  104 #define BUFSIZ 100              /* Chunk size iomoved to/from user */
  105 
  106 struct  ptsc {
  107         int     pt_flags;
  108         struct  selinfo pt_selr, pt_selw;
  109         u_char  pt_send;
  110         u_char  pt_ucntl;
  111         struct tty *pt_tty;
  112         struct cdev *devs, *devc;
  113         struct  prison *pt_prison;
  114 };
  115 
  116 #define PF_PKT          0x08            /* packet mode */
  117 #define PF_STOPPED      0x10            /* user told stopped */
  118 #define PF_NOSTOP       0x40
  119 #define PF_UCNTL        0x80            /* user control mode */
  120 
  121 #define TSA_PTC_READ(tp)        ((void *)&(tp)->t_outq.c_cf)
  122 #define TSA_PTC_WRITE(tp)       ((void *)&(tp)->t_rawq.c_cl)
  123 #define TSA_PTS_READ(tp)        ((void *)&(tp)->t_canq)
  124 
  125 static const char names[] = "pqrsPQRSlmnoLMNO";
  126 /*
  127  * This function creates and initializes a pts/ptc pair
  128  *
  129  * pts == /dev/tty[pqrsPQRSlmnoLMNO][0123456789abcdefghijklmnopqrstuv]
  130  * ptc == /dev/pty[pqrsPQRSlmnoLMNO][0123456789abcdefghijklmnopqrstuv]
  131  */
  132 static struct cdev *
  133 ptyinit(struct cdev *devc, struct thread *td)
  134 {
  135         struct cdev *devs;
  136         struct ptsc *pt;
  137         int n;
  138 
  139         n = minor2unit(minor(devc));
  140 
  141         /* We only allow for up to 32 ptys per char in "names". */
  142         if (n >= 32 * (sizeof(names) - 1))
  143                 return (NULL);
  144 
  145         devc->si_flags &= ~SI_CHEAPCLONE;
  146 
  147         pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
  148         pt->devs = devs = make_dev_cred(&pts_cdevsw, n, td->td_ucred,
  149             UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
  150         pt->devc = devc;
  151 
  152         pt->pt_tty = ttymalloc(pt->pt_tty);
  153         pt->pt_tty->t_sc = pt;
  154         devs->si_drv1 = devc->si_drv1 = pt;
  155         devs->si_tty = devc->si_tty = pt->pt_tty;
  156         pt->pt_tty->t_dev = devs;
  157         return (devc);
  158 }
  159 
  160 /*ARGSUSED*/
  161 static  int
  162 ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
  163 {
  164         struct tty *tp;
  165         int error;
  166         struct ptsc *pt;
  167 
  168         if (!dev->si_drv1)
  169                 return(ENXIO);
  170         pt = dev->si_drv1;
  171         tp = dev->si_tty;
  172         if ((tp->t_state & TS_ISOPEN) == 0) {
  173                 ttyinitmode(tp, 1, 0);
  174         } else if (tp->t_state & TS_XCLUDE && suser(td))
  175                 return (EBUSY);
  176         else if (pt->pt_prison != td->td_ucred->cr_prison && suser(td))
  177                 return (EBUSY);
  178         if (tp->t_oproc)                        /* Ctrlr still around. */
  179                 (void)ttyld_modem(tp, 1);
  180         while ((tp->t_state & TS_CARR_ON) == 0) {
  181                 if (flag&FNONBLOCK)
  182                         break;
  183                 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
  184                                  "ptsopn", 0);
  185                 if (error)
  186                         return (error);
  187         }
  188         error = ttyld_open(tp, dev);
  189         if (error == 0)
  190                 ptcwakeup(tp, FREAD|FWRITE);
  191         return (error);
  192 }
  193 
  194 static  int
  195 ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
  196 {
  197         struct tty *tp;
  198         int err;
  199 
  200         tp = dev->si_tty;
  201         err = ttyld_close(tp, flag);
  202         (void) tty_close(tp);
  203         return (err);
  204 }
  205 
  206 static  int
  207 ptsread(struct cdev *dev, struct uio *uio, int flag)
  208 {
  209         struct tty *tp = dev->si_tty;
  210         int error = 0;
  211 
  212         if (tp->t_oproc)
  213                 error = ttyld_read(tp, uio, flag);
  214         ptcwakeup(tp, FWRITE);
  215         return (error);
  216 }
  217 
  218 /*
  219  * Write to pseudo-tty.
  220  * Wakeups of controlling tty will happen
  221  * indirectly, when tty driver calls ptsstart.
  222  */
  223 static  int
  224 ptswrite(struct cdev *dev, struct uio *uio, int flag)
  225 {
  226         struct tty *tp;
  227 
  228         tp = dev->si_tty;
  229         if (tp->t_oproc == 0)
  230                 return (EIO);
  231         return (ttyld_write(tp, uio, flag));
  232 }
  233 
  234 /*
  235  * Start output on pseudo-tty.
  236  * Wake up process selecting or sleeping for input from controlling tty.
  237  */
  238 static void
  239 ptsstart(struct tty *tp)
  240 {
  241         struct ptsc *pt = tp->t_sc;
  242 
  243         if (tp->t_state & TS_TTSTOP)
  244                 return;
  245         if (pt->pt_flags & PF_STOPPED) {
  246                 pt->pt_flags &= ~PF_STOPPED;
  247                 pt->pt_send = TIOCPKT_START;
  248         }
  249         ptcwakeup(tp, FREAD);
  250 }
  251 
  252 static void
  253 ptcwakeup(struct tty *tp, int flag)
  254 {
  255         struct ptsc *pt = tp->t_sc;
  256 
  257         if (flag & FREAD) {
  258                 selwakeuppri(&pt->pt_selr, TTIPRI);
  259                 wakeup(TSA_PTC_READ(tp));
  260         }
  261         if (flag & FWRITE) {
  262                 selwakeuppri(&pt->pt_selw, TTOPRI);
  263                 wakeup(TSA_PTC_WRITE(tp));
  264         }
  265 }
  266 
  267 static  int
  268 ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
  269 {
  270         struct tty *tp;
  271         struct ptsc *pt;
  272 
  273         if (!dev->si_drv1)
  274                 ptyinit(dev, td);
  275         if (!dev->si_drv1)
  276                 return(ENXIO);
  277         tp = dev->si_tty;
  278 
  279         if (tp->t_oproc)
  280                 return (EIO);
  281         tp->t_timeout = -1;
  282         tp->t_oproc = ptsstart;
  283         tp->t_stop = ptsstop;
  284         (void)ttyld_modem(tp, 1);
  285         tp->t_lflag &= ~EXTPROC;
  286         pt = dev->si_drv1;
  287         pt->pt_prison = td->td_ucred->cr_prison;
  288         pt->pt_flags = 0;
  289         pt->pt_send = 0;
  290         pt->pt_ucntl = 0;
  291         return (0);
  292 }
  293 
  294 static  int
  295 ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
  296 {
  297         struct tty *tp;
  298 
  299         tp = dev->si_tty;
  300         (void)ttyld_modem(tp, 0);
  301 
  302         /*
  303          * XXX MDMBUF makes no sense for ptys but would inhibit the above
  304          * l_modem().  CLOCAL makes sense but isn't supported.   Special
  305          * l_modem()s that ignore carrier drop make no sense for ptys but
  306          * may be in use because other parts of the line discipline make
  307          * sense for ptys.  Recover by doing everything that a normal
  308          * ttymodem() would have done except for sending a SIGHUP.
  309          */
  310         if (tp->t_state & TS_ISOPEN) {
  311                 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
  312                 tp->t_state |= TS_ZOMBIE;
  313                 ttyflush(tp, FREAD | FWRITE);
  314         }
  315 
  316         tp->t_oproc = 0;                /* mark closed */
  317         return (0);
  318 }
  319 
  320 static  int
  321 ptcread(struct cdev *dev, struct uio *uio, int flag)
  322 {
  323         struct tty *tp = dev->si_tty;
  324         struct ptsc *pt = dev->si_drv1;
  325         char buf[BUFSIZ];
  326         int error = 0, cc;
  327 
  328         /*
  329          * We want to block until the slave
  330          * is open, and there's something to read;
  331          * but if we lost the slave or we're NBIO,
  332          * then return the appropriate error instead.
  333          */
  334         for (;;) {
  335                 if (tp->t_state&TS_ISOPEN) {
  336                         if (pt->pt_flags&PF_PKT && pt->pt_send) {
  337                                 error = ureadc((int)pt->pt_send, uio);
  338                                 if (error)
  339                                         return (error);
  340                                 if (pt->pt_send & TIOCPKT_IOCTL) {
  341                                         cc = min(uio->uio_resid,
  342                                                 sizeof(tp->t_termios));
  343                                         uiomove(&tp->t_termios, cc, uio);
  344                                 }
  345                                 pt->pt_send = 0;
  346                                 return (0);
  347                         }
  348                         if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
  349                                 error = ureadc((int)pt->pt_ucntl, uio);
  350                                 if (error)
  351                                         return (error);
  352                                 pt->pt_ucntl = 0;
  353                                 return (0);
  354                         }
  355                         if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
  356                                 break;
  357                 }
  358                 if ((tp->t_state & TS_CONNECTED) == 0)
  359                         return (0);     /* EOF */
  360                 if (flag & O_NONBLOCK)
  361                         return (EWOULDBLOCK);
  362                 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
  363                 if (error)
  364                         return (error);
  365         }
  366         if (pt->pt_flags & (PF_PKT|PF_UCNTL))
  367                 error = ureadc(0, uio);
  368         while (uio->uio_resid > 0 && error == 0) {
  369                 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
  370                 if (cc <= 0)
  371                         break;
  372                 error = uiomove(buf, cc, uio);
  373         }
  374         ttwwakeup(tp);
  375         return (error);
  376 }
  377 
  378 static  void
  379 ptsstop(struct tty *tp, int flush)
  380 {
  381         struct ptsc *pt = tp->t_sc;
  382         int flag;
  383 
  384         /* note: FLUSHREAD and FLUSHWRITE already ok */
  385         if (flush == 0) {
  386                 flush = TIOCPKT_STOP;
  387                 pt->pt_flags |= PF_STOPPED;
  388         } else
  389                 pt->pt_flags &= ~PF_STOPPED;
  390         pt->pt_send |= flush;
  391         /* change of perspective */
  392         flag = 0;
  393         if (flush & FREAD)
  394                 flag |= FWRITE;
  395         if (flush & FWRITE)
  396                 flag |= FREAD;
  397         ptcwakeup(tp, flag);
  398 }
  399 
  400 static  int
  401 ptcpoll(struct cdev *dev, int events, struct thread *td)
  402 {
  403         struct tty *tp = dev->si_tty;
  404         struct ptsc *pt = dev->si_drv1;
  405         int revents = 0;
  406         int s;
  407 
  408         if ((tp->t_state & TS_CONNECTED) == 0)
  409                 return (events & 
  410                    (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
  411 
  412         /*
  413          * Need to block timeouts (ttrstart).
  414          */
  415         s = spltty();
  416 
  417         if (events & (POLLIN | POLLRDNORM))
  418                 if ((tp->t_state & TS_ISOPEN) &&
  419                     ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
  420                      ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
  421                      ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
  422                         revents |= events & (POLLIN | POLLRDNORM);
  423 
  424         if (events & (POLLOUT | POLLWRNORM))
  425                 if (tp->t_state & TS_ISOPEN &&
  426                     (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
  427                       (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
  428                         revents |= events & (POLLOUT | POLLWRNORM);
  429 
  430         if (events & POLLHUP)
  431                 if ((tp->t_state & TS_CARR_ON) == 0)
  432                         revents |= POLLHUP;
  433 
  434         if (revents == 0) {
  435                 if (events & (POLLIN | POLLRDNORM))
  436                         selrecord(td, &pt->pt_selr);
  437 
  438                 if (events & (POLLOUT | POLLWRNORM))
  439                         selrecord(td, &pt->pt_selw);
  440         }
  441         splx(s);
  442 
  443         return (revents);
  444 }
  445 
  446 static  int
  447 ptcwrite(struct cdev *dev, struct uio *uio, int flag)
  448 {
  449         struct tty *tp = dev->si_tty;
  450         u_char *cp = 0;
  451         int cc = 0;
  452         u_char locbuf[BUFSIZ];
  453         int cnt = 0;
  454         int error = 0;
  455 
  456 again:
  457         if ((tp->t_state&TS_ISOPEN) == 0)
  458                 goto block;
  459         while (uio->uio_resid > 0 || cc > 0) {
  460                 if (cc == 0) {
  461                         cc = min(uio->uio_resid, BUFSIZ);
  462                         cp = locbuf;
  463                         error = uiomove(cp, cc, uio);
  464                         if (error)
  465                                 return (error);
  466                         /* check again for safety */
  467                         if ((tp->t_state & TS_ISOPEN) == 0) {
  468                                 /* adjust for data copied in but not written */
  469                                 uio->uio_resid += cc;
  470                                 return (EIO);
  471                         }
  472                 }
  473                 while (cc > 0) {
  474                         if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
  475                            (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
  476                                 wakeup(TSA_HUP_OR_INPUT(tp));
  477                                 goto block;
  478                         }
  479                         ttyld_rint(tp, *cp++);
  480                         cnt++;
  481                         cc--;
  482                 }
  483                 cc = 0;
  484         }
  485         return (0);
  486 block:
  487         /*
  488          * Come here to wait for slave to open, for space
  489          * in outq, or space in rawq, or an empty canq.
  490          */
  491         if ((tp->t_state & TS_CONNECTED) == 0) {
  492                 /* adjust for data copied in but not written */
  493                 uio->uio_resid += cc;
  494                 return (EIO);
  495         }
  496         if (flag & O_NONBLOCK) {
  497                 /* adjust for data copied in but not written */
  498                 uio->uio_resid += cc;
  499                 if (cnt == 0)
  500                         return (EWOULDBLOCK);
  501                 return (0);
  502         }
  503         error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
  504         if (error) {
  505                 /* adjust for data copied in but not written */
  506                 uio->uio_resid += cc;
  507                 return (error);
  508         }
  509         goto again;
  510 }
  511 
  512 /*ARGSUSED*/
  513 static  int
  514 ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  515 {
  516         struct tty *tp = dev->si_tty;
  517         struct ptsc *pt = dev->si_drv1;
  518         int ival;
  519 
  520         switch (cmd) {
  521 
  522         case TIOCGPGRP:
  523                 /*
  524                  * We avoid calling ttioctl on the controller since,
  525                  * in that case, tp must be the controlling terminal.
  526                  */
  527                 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
  528                 return (0);
  529 
  530         case TIOCPKT:
  531                 if (*(int *)data) {
  532                         if (pt->pt_flags & PF_UCNTL)
  533                                 return (EINVAL);
  534                         pt->pt_flags |= PF_PKT;
  535                 } else
  536                         pt->pt_flags &= ~PF_PKT;
  537                 return (0);
  538 
  539         case TIOCUCNTL:
  540                 if (*(int *)data) {
  541                         if (pt->pt_flags & PF_PKT)
  542                                 return (EINVAL);
  543                         pt->pt_flags |= PF_UCNTL;
  544                 } else
  545                         pt->pt_flags &= ~PF_UCNTL;
  546                 return (0);
  547         }
  548 
  549         /*
  550          * The rest of the ioctls shouldn't be called until
  551          * the slave is open.
  552          */
  553         if ((tp->t_state & TS_ISOPEN) == 0)
  554                 return (EAGAIN);
  555 
  556         switch (cmd) {
  557 #ifndef BURN_BRIDGES
  558 #ifdef COMPAT_43
  559         case TIOCSETP:
  560         case TIOCSETN:
  561 #endif
  562 #endif
  563         case TIOCSETD:
  564         case TIOCSETA:
  565         case TIOCSETAW:
  566         case TIOCSETAF:
  567                 /*
  568                  * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
  569                  * ttywflush(tp) will hang if there are characters in
  570                  * the outq.
  571                  */
  572                 ndflush(&tp->t_outq, tp->t_outq.c_cc);
  573                 break;
  574 
  575         case _IO('t', 95):
  576                 ival = IOCPARM_IVAL(data);
  577                 data = (caddr_t)&ival;
  578                 /* FALLTHROUGH */
  579         case TIOCSIG:
  580                 if (*(unsigned int *)data >= NSIG ||
  581                     *(unsigned int *)data == 0)
  582                         return(EINVAL);
  583                 if ((tp->t_lflag&NOFLSH) == 0)
  584                         ttyflush(tp, FREAD|FWRITE);
  585                 if (tp->t_pgrp != NULL) {
  586                         PGRP_LOCK(tp->t_pgrp);
  587                         pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
  588                         PGRP_UNLOCK(tp->t_pgrp);
  589                 }
  590                 if ((*(unsigned int *)data == SIGINFO) &&
  591                     ((tp->t_lflag&NOKERNINFO) == 0))
  592                         ttyinfo(tp);
  593                 return(0);
  594         }
  595 
  596         return (ptsioctl(dev, cmd, data, flag, td));
  597 }
  598 
  599 /*ARGSUSED*/
  600 static  int
  601 ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
  602 {
  603         struct tty *tp = dev->si_tty;
  604         struct ptsc *pt = dev->si_drv1;
  605         u_char *cc = tp->t_cc;
  606         int stop, error;
  607 
  608         if (cmd == TIOCEXT) {
  609                 /*
  610                  * When the EXTPROC bit is being toggled, we need
  611                  * to send an TIOCPKT_IOCTL if the packet driver
  612                  * is turned on.
  613                  */
  614                 if (*(int *)data) {
  615                         if (pt->pt_flags & PF_PKT) {
  616                                 pt->pt_send |= TIOCPKT_IOCTL;
  617                                 ptcwakeup(tp, FREAD);
  618                         }
  619                         tp->t_lflag |= EXTPROC;
  620                 } else {
  621                         if ((tp->t_lflag & EXTPROC) &&
  622                             (pt->pt_flags & PF_PKT)) {
  623                                 pt->pt_send |= TIOCPKT_IOCTL;
  624                                 ptcwakeup(tp, FREAD);
  625                         }
  626                         tp->t_lflag &= ~EXTPROC;
  627                 }
  628                 return(0);
  629         }
  630         error = ttyioctl(dev, cmd, data, flag, td);
  631         if (error == ENOTTY) {
  632                 if (pt->pt_flags & PF_UCNTL &&
  633                     (cmd & ~0xff) == UIOCCMD(0)) {
  634                         if (cmd & 0xff) {
  635                                 pt->pt_ucntl = (u_char)cmd;
  636                                 ptcwakeup(tp, FREAD);
  637                         }
  638                         return (0);
  639                 }
  640                 error = ENOTTY;
  641         }
  642         /*
  643          * If external processing and packet mode send ioctl packet.
  644          */
  645         if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
  646                 switch(cmd) {
  647                 case TIOCSETA:
  648                 case TIOCSETAW:
  649                 case TIOCSETAF:
  650 #ifndef BURN_BRIDGES
  651 #ifdef COMPAT_43
  652                 case TIOCSETP:
  653                 case TIOCSETN:
  654                 case TIOCSETC:
  655                 case TIOCSLTC:
  656                 case TIOCLBIS:
  657                 case TIOCLBIC:
  658                 case TIOCLSET:
  659 #endif
  660 #endif
  661                         pt->pt_send |= TIOCPKT_IOCTL;
  662                         ptcwakeup(tp, FREAD);
  663                         break;
  664                 default:
  665                         break;
  666                 }
  667         }
  668         stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
  669                 && CCEQ(cc[VSTART], CTRL('q'));
  670         if (pt->pt_flags & PF_NOSTOP) {
  671                 if (stop) {
  672                         pt->pt_send &= ~TIOCPKT_NOSTOP;
  673                         pt->pt_send |= TIOCPKT_DOSTOP;
  674                         pt->pt_flags &= ~PF_NOSTOP;
  675                         ptcwakeup(tp, FREAD);
  676                 }
  677         } else {
  678                 if (!stop) {
  679                         pt->pt_send &= ~TIOCPKT_DOSTOP;
  680                         pt->pt_send |= TIOCPKT_NOSTOP;
  681                         pt->pt_flags |= PF_NOSTOP;
  682                         ptcwakeup(tp, FREAD);
  683                 }
  684         }
  685         return (error);
  686 }
  687 
  688 static void
  689 pty_clone(void *arg, struct ucred *cr, char *name, int namelen,
  690     struct cdev **dev)
  691 {
  692         char *cp;
  693         int u;
  694 
  695         if (*dev != NULL)
  696                 return;
  697         if (bcmp(name, "pty", 3) != 0)
  698                 return;
  699         if (name[5] != '\0' || name[3] == '\0')
  700                 return;
  701         cp = index(names, name[3]);
  702         if (cp == NULL)
  703                 return;
  704         u = (cp - names) * 32;
  705         if (name[4] >= '' && name[4] <= '9')
  706                 u += name[4] - '';
  707         else if (name[4] >= 'a' && name[4] <= 'v')
  708                 u += name[4] - 'a' + 10;
  709         else
  710                 return;
  711         *dev = make_dev_cred(&ptc_cdevsw, unit2minor(u), cr,
  712             UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
  713         dev_ref(*dev);
  714         (*dev)->si_flags |= SI_CHEAPCLONE;
  715         return;
  716 }
  717 
  718 static void
  719 ptc_drvinit(void *unused)
  720 {
  721 
  722         EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
  723 }
  724 
  725 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,ptc_drvinit,NULL)

Cache object: 16b3238ffd5db685e238cd73c87700ce


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