Print this page
14249 pseudo-terminal nomenclature should reflect POSIX
Change-Id: Ib4a3cef899ff4c71b09cb0dc6878863c5e8357bc

Split Close
Expand all
Collapse all
          --- old/usr/src/uts/common/io/tty_pty.c
          +++ new/usr/src/uts/common/io/tty_pty.c
↓ open down ↓ 3 lines elided ↑ open up ↑
   4    4   * Copyright 2015, Joyent, Inc.
   5    5   */
   6    6  
   7    7  /*
   8    8   * Copyright (c) 1983 Regents of the University of California.
   9    9   * All rights reserved. The Berkeley software License Agreement
  10   10   * specifies the terms and conditions for redistribution.
  11   11   */
  12   12  
  13   13  /*
  14      - * PTY - Stream "pseudo-tty" device.  For each "controller" side
  15      - * it connects to a "slave" side.
       14 + * PTY - Stream "pseudo-terminal" device.  For each "manager" side it connects
       15 + * to a "subsidiary" side.
  16   16   */
  17   17  
  18   18  
  19   19  #include <sys/param.h>
  20   20  #include <sys/systm.h>
  21   21  #include <sys/filio.h>
  22   22  #include <sys/ioccom.h>
  23   23  #include <sys/termios.h>
  24   24  #include <sys/termio.h>
  25   25  #include <sys/ttold.h>
  26   26  #include <sys/stropts.h>
  27   27  #include <sys/stream.h>
  28   28  #include <sys/tty.h>
  29   29  #include <sys/user.h>
  30   30  #include <sys/conf.h>
  31   31  #include <sys/file.h>
  32      -#include <sys/vnode.h>  /* 1/0 on the vomit meter */
       32 +#include <sys/vnode.h>
  33   33  #include <sys/proc.h>
  34   34  #include <sys/uio.h>
  35   35  #include <sys/errno.h>
  36   36  #include <sys/strsubr.h>
  37   37  #include <sys/poll.h>
  38   38  #include <sys/sysmacros.h>
  39   39  #include <sys/debug.h>
  40   40  #include <sys/procset.h>
  41   41  #include <sys/cred.h>
  42   42  #include <sys/ptyvar.h>
↓ open down ↓ 66 lines elided ↑ open up ↑
 109  109  
 110  110  extern int dseekneg_flag;
 111  111  extern struct mod_ops mod_driverops;
 112  112  extern struct dev_ops ptc_ops;
 113  113  
 114  114  /*
 115  115   * Module linkage information for the kernel.
 116  116   */
 117  117  
 118  118  static struct modldrv modldrv = {
 119      -        &mod_driverops, /* Type of module.  This one is a pseudo driver */
      119 +        &mod_driverops,
 120  120          "tty pseudo driver control 'ptc'",
 121      -        &ptc_ops,       /* driver ops */
      121 +        &ptc_ops,
 122  122  };
 123  123  
 124  124  static struct modlinkage modlinkage = {
 125  125          MODREV_1,
 126  126          &modldrv,
 127  127          NULL
 128  128  };
 129  129  
 130  130  int
 131  131  _init()
↓ open down ↓ 97 lines elided ↑ open up ↑
 229  229  
 230  230          for (dev = 0; dev < npty; dev++) {
 231  231                  cv_destroy(&pty_softc[dev].pt_cv_flags);
 232  232                  cv_destroy(&pty_softc[dev].pt_cv_readq);
 233  233                  cv_destroy(&pty_softc[dev].pt_cv_writeq);
 234  234                  mutex_destroy(&pty_softc[dev].ptc_lock);
 235  235          }
 236  236  }
 237  237  
 238  238  /*
 239      - * Controller side.  This is not, alas, a streams device; there are too
      239 + * Manager side.  This is not, alas, a streams device; there are too
 240  240   * many old features that we must support and that don't work well
 241  241   * with streams.
 242  242   */
 243  243  
 244      -/*ARGSUSED*/
 245  244  int
 246  245  ptcopen(dev_t *devp, int flag, int otyp, struct cred *cred)
 247  246  {
 248  247          dev_t dev = *devp;
 249  248          struct pty *pty;
 250  249          queue_t *q;
 251  250  
 252  251          if (getminor(dev) >= npty) {
 253  252                  return (ENXIO);
 254  253          }
 255  254          pty = &pty_softc[getminor(dev)];
 256  255          mutex_enter(&pty->ptc_lock);
 257  256          if (pty->pt_flags & PF_CARR_ON) {
 258  257                  mutex_exit(&pty->ptc_lock);
 259      -                return (EIO);   /* controller is exclusive use */
      258 +                return (EIO);   /* manager is exclusive use */
 260  259                                  /* XXX - should be EBUSY! */
 261  260          }
 262  261          if (pty->pt_flags & PF_WOPEN) {
 263  262                  pty->pt_flags &= ~PF_WOPEN;
 264  263                  cv_broadcast(&pty->pt_cv_flags);
 265  264          }
 266  265  
 267  266          if ((q = pty->pt_ttycommon.t_readq) != NULL) {
 268  267                  /*
 269      -                 * Send an un-hangup to the slave, since "carrier" is
      268 +                 * Send an un-hangup to the subsidiary, since "carrier" is
 270  269                   * coming back up.  Make sure we're doing canonicalization.
 271  270                   */
 272  271                  (void) putctl(q, M_UNHANGUP);
 273  272                  (void) putctl1(q, M_CTL, MC_DOCANON);
 274  273          }
 275  274          pty->pt_flags |= PF_CARR_ON;
 276  275          pty->pt_send = 0;
 277  276          pty->pt_ucntl = 0;
 278  277  
 279  278          mutex_exit(&pty->ptc_lock);
 280  279          return (0);
 281  280  }
 282  281  
 283      -/*ARGSUSED1*/
 284  282  int
 285  283  ptcclose(dev_t dev, int flag, int otyp, struct cred *cred)
 286  284  {
 287  285          struct pty *pty;
 288  286          mblk_t *bp;
 289  287          queue_t *q;
 290  288  
 291  289          pty = &pty_softc[getminor(dev)];
 292  290  
 293  291          mutex_enter(&pty->ptc_lock);
 294  292          if ((q = pty->pt_ttycommon.t_readq) != NULL) {
 295  293                  /*
 296      -                 * Send a hangup to the slave, since "carrier" is dropping.
      294 +                 * Send a hangup to the subsidiary, since "carrier" is dropping.
 297  295                   */
 298  296                  (void) putctl(q, M_HANGUP);
 299  297          }
 300  298  
 301  299          /*
 302      -         * Clear out all the controller-side state.  This also
      300 +         * Clear out all the manager-side state.  This also
 303  301           * clears PF_CARR_ON, which is correct because the
 304      -         * "carrier" is dropping since the controller process
      302 +         * "carrier" is dropping since the manager process
 305  303           * is going away.
 306  304           */
 307  305          pty->pt_flags &= (PF_WOPEN|PF_STOPPED|PF_NOSTOP);
 308  306          while ((bp = pty->pt_stuffqfirst) != NULL) {
 309  307                  if ((pty->pt_stuffqfirst = bp->b_next) == NULL)
 310  308                          pty->pt_stuffqlast = NULL;
 311  309                  else
 312  310                          pty->pt_stuffqfirst->b_prev = NULL;
 313  311                  pty->pt_stuffqlen--;
 314  312                  bp->b_next = bp->b_prev = NULL;
↓ open down ↓ 7 lines elided ↑ open up ↑
 322  320  ptcread(dev_t dev, struct uio *uio, struct cred *cred)
 323  321  {
 324  322          struct pty *pty = &pty_softc[getminor(dev)];
 325  323          mblk_t *bp, *nbp;
 326  324          queue_t *q;
 327  325          unsigned char tmp;
 328  326          ssize_t cc;
 329  327          int error;
 330  328          off_t off;
 331  329  
 332      -#ifdef lint
 333      -        cred = cred;
 334      -#endif
 335      -
 336  330          off = uio->uio_offset;
 337  331  
 338  332          mutex_enter(&pty->ptc_lock);
 339  333  
 340  334          for (;;) {
 341  335                  while (pty->pt_flags & PF_READ) {
 342  336                          pty->pt_flags |= PF_WREAD;
 343  337                          cv_wait(&pty->pt_cv_flags, &pty->ptc_lock);
 344  338                  }
 345  339                  pty->pt_flags |= PF_READ;
↓ open down ↓ 122 lines elided ↑ open up ↑
 468  462                                  bp->b_next = bp->b_prev = NULL;
 469  463                                  freemsg(bp);
 470  464                                  mutex_enter(&pty->ptc_lock);
 471  465                          }
 472  466                          uio->uio_offset = off;
 473  467                          goto out;
 474  468                  }
 475  469  
 476  470                  /*
 477  471                   * There's no data available.
 478      -                 * We want to block until the slave is open, and there's
 479      -                 * something to read; but if we lost the slave or we're NBIO,
 480      -                 * then return the appropriate error instead.  POSIX-style
 481      -                 * non-block has top billing and gives -1 with errno = EAGAIN,
 482      -                 * BSD-style comes next and gives -1 with errno = EWOULDBLOCK,
 483      -                 * SVID-style comes last and gives 0.
      472 +                 * We want to block until the subsidiary is open, and there's
      473 +                 * something to read; but if we lost the subsidiary or we're
      474 +                 * NBIO, then return the appropriate error instead.
      475 +                 * POSIX-style non-block has top billing and gives -1 with
      476 +                 * errno = EAGAIN, BSD-style comes next and gives -1 with
      477 +                 * errno = EWOULDBLOCK, SVID-style comes last and gives 0.
 484  478                   */
 485      -                if (pty->pt_flags & PF_SLAVEGONE) {
      479 +                if (pty->pt_flags & PF_SUBSIDGONE) {
 486  480                          error = EIO;
 487  481                          goto out;
 488  482                  }
 489  483                  if (uio->uio_fmode & FNONBLOCK) {
 490  484                          error = EAGAIN;
 491  485                          goto out;
 492  486                  }
 493  487                  if (pty->pt_flags & PF_NBIO) {
 494  488                          error = EWOULDBLOCK;
 495  489                          goto out;
↓ open down ↓ 29 lines elided ↑ open up ↑
 525  519          struct pty *pty = &pty_softc[getminor(dev)];
 526  520          queue_t *q;
 527  521          int written;
 528  522          mblk_t *mp;
 529  523          int fmode = 0;
 530  524          int error = 0;
 531  525  
 532  526          off_t off;
 533  527          off = uio->uio_offset;
 534  528  
 535      -#ifdef lint
 536      -        cred = cred;
 537      -#endif
 538      -
 539      -
 540  529          mutex_enter(&pty->ptc_lock);
 541  530  
 542  531  again:
 543  532          while (pty->pt_flags & PF_WRITE) {
 544  533                  pty->pt_flags |= PF_WWRITE;
 545  534                  cv_wait(&pty->pt_cv_flags, &pty->ptc_lock);
 546  535          }
 547  536  
 548  537          pty->pt_flags |= PF_WRITE;
 549  538  
 550  539          if ((q = pty->pt_ttycommon.t_readq) == NULL) {
 551  540  
 552  541                  /*
 553      -                 * Wait for slave to open.
      542 +                 * Wait for subsidiary to open.
 554  543                   */
 555      -                if (pty->pt_flags & PF_SLAVEGONE) {
      544 +                if (pty->pt_flags & PF_SUBSIDGONE) {
 556  545                          error = EIO;
 557  546                          goto out;
 558  547                  }
 559  548                  if (uio->uio_fmode & FNONBLOCK) {
 560  549                          error = EAGAIN;
 561  550                          goto out;
 562  551                  }
 563  552                  if (pty->pt_flags & PF_NBIO) {
 564  553                          error = EWOULDBLOCK;
 565  554                          goto out;
↓ open down ↓ 15 lines elided ↑ open up ↑
 581  570          }
 582  571  
 583  572          /*
 584  573           * If in remote mode, even zero-length writes generate messages.
 585  574           */
 586  575          written = 0;
 587  576          if ((pty->pt_flags & PF_REMOTE) || uio->uio_resid > 0) {
 588  577                  do {
 589  578                          while (!canput(q)) {
 590  579                                  /*
 591      -                                 * Wait for slave's read queue to unclog.
      580 +                                 * Wait for subsidiary's read queue to unclog.
 592  581                                   */
 593      -                                if (pty->pt_flags & PF_SLAVEGONE) {
      582 +                                if (pty->pt_flags & PF_SUBSIDGONE) {
 594  583                                          error = EIO;
 595  584                                          goto out;
 596  585                                  }
 597  586                                  if (uio->uio_fmode & FNONBLOCK) {
 598  587                                          if (!written)
 599  588                                                  error = EAGAIN;
 600  589                                          goto out;
 601  590                                  }
 602  591                                  if (pty->pt_flags & PF_NBIO) {
 603  592                                          if (!written)
↓ open down ↓ 152 lines elided ↑ open up ↑
 756  745                  } else {
 757  746                          if ((q = pty->pt_ttycommon.t_readq) != NULL)
 758  747                                  (void) putctl1(q, M_CTL, MC_DOCANON);
 759  748                          pty->pt_flags &= ~PF_REMOTE;
 760  749                  }
 761  750                  mutex_exit(&pty->ptc_lock);
 762  751                  break;
 763  752  
 764  753          case TIOCSIGNAL:
 765  754                  /*
 766      -                 * Blast a M_PCSIG message up the slave stream; the
      755 +                 * Blast a M_PCSIG message up the subsidiary stream; the
 767  756                   * signal number is the argument to the "ioctl".
 768  757                   */
 769  758                  copy_in(data, d_arg);
 770  759                  mutex_enter(&pty->ptc_lock);
 771  760                  if ((q = pty->pt_ttycommon.t_readq) != NULL)
 772  761                          (void) putctl1(q, M_PCSIG, (int)d_arg);
 773  762                  mutex_exit(&pty->ptc_lock);
 774  763                  break;
 775  764  
 776  765          case FIONBIO:
↓ open down ↓ 10 lines elided ↑ open up ↑
 787  776                  copy_in(data, d_arg);
 788  777                  mutex_enter(&pty->ptc_lock);
 789  778                  if (d_arg)
 790  779                          pty->pt_flags |= PF_ASYNC;
 791  780                  else
 792  781                          pty->pt_flags &= ~PF_ASYNC;
 793  782                  mutex_exit(&pty->ptc_lock);
 794  783                  break;
 795  784  
 796  785          /*
 797      -         * These, at least, can work on the controller-side process
      786 +         * These, at least, can work on the manager-side process
 798  787           * group.
 799  788           */
 800  789          case FIOGETOWN:
 801  790                  mutex_enter(&pty->ptc_lock);
 802  791                  d_arg = -pty->pt_pgrp;
 803  792                  mutex_exit(&pty->ptc_lock);
 804  793                  copy_out(d_arg, data);
 805  794                  break;
 806  795  
 807  796          case FIOSETOWN:
 808  797                  copy_in(data, d_arg);
 809  798                  mutex_enter(&pty->ptc_lock);
 810  799                  pty->pt_pgrp = (short)(-d_arg);
 811  800                  mutex_exit(&pty->ptc_lock);
 812  801                  break;
 813  802  
 814  803          case FIONREAD: {
 815  804                  /*
 816      -                 * Return the total number of bytes of data in all messages
 817      -                 * in slave write queue, which is master read queue, unless a
 818      -                 * special message would be read.
      805 +                 * Return the total number of bytes of data in all messages in
      806 +                 * subsidiary write queue, which is manager read queue, unless
      807 +                 * a special message would be read.
 819  808                   */
 820  809                  mblk_t *mp;
 821  810                  size_t count = 0;
 822  811  
 823  812                  mutex_enter(&pty->ptc_lock);
 824  813                  if (pty->pt_flags&(PF_PKT|PF_UCNTL) && pty->pt_send)
 825  814                          count = 1;      /* will return 1 byte */
 826  815                  else if ((pty->pt_flags & (PF_UCNTL|PF_43UCNTL)) &&
 827  816                      pty->pt_ucntl)
 828  817                          count = 1;      /* will return 1 byte */
↓ open down ↓ 80 lines elided ↑ open up ↑
 909  898                  tty_arg.ts_lines = pty->pt_ttycommon.t_size.ws_row;
 910  899                  tty_arg.ts_cols = pty->pt_ttycommon.t_size.ws_col;
 911  900                  mutex_exit(&pty->ptc_lock);
 912  901                  if (copyout(&tty_arg, (caddr_t)data,
 913  902                      sizeof (struct ttysize)) != 0)
 914  903                          return (EFAULT);
 915  904                  break;
 916  905  
 917  906          /*
 918  907           * XXX These should not be here.  The only reason why an
 919      -         * "ioctl" on the controller side should get the
 920      -         * slave side's process group is so that the process on
 921      -         * the controller side can send a signal to the slave
      908 +         * "ioctl" on the manager side should get the
      909 +         * subsidiary side's process group is so that the process on
      910 +         * the manager side can send a signal to the subsidiary
 922  911           * side's process group; however, this is better done
 923  912           * with TIOCSIGNAL, both because it doesn't require us
 924      -         * to know about the slave side's process group and because
 925      -         * the controller side process may not have permission to
      913 +         * to know about the subsidiary side's process group and because
      914 +         * the manager side process may not have permission to
 926  915           * send that signal to the entire process group.
 927  916           *
 928  917           * However, since vanilla 4BSD doesn't provide TIOCSIGNAL,
 929  918           * we can't just get rid of them.
 930  919           */
 931  920          case TIOCGPGRP:
 932  921          case TIOCSPGRP:
 933  922          /*
 934  923           * This is amazingly disgusting, but the stupid semantics of
 935  924           * 4BSD pseudo-ttys makes us do it.  If we do one of these guys
 936      -         * on the controller side, it really applies to the slave-side
      925 +         * on the manager side, it really applies to the subsidiary-side
 937  926           * stream.  It should NEVER have been possible to do ANY sort
 938      -         * of tty operations on the controller side, but it's too late
      927 +         * of tty operations on the manager side, but it's too late
 939  928           * to fix that now.  However, we won't waste our time implementing
 940  929           * anything that the original pseudo-tty driver didn't handle.
 941  930           */
 942  931          case TIOCGETP:
 943  932          case TIOCSETP:
 944  933          case TIOCSETN:
 945  934          case TIOCGETC:
 946  935          case TIOCSETC:
 947  936          case TIOCGLTC:
 948  937          case TIOCSLTC:
↓ open down ↓ 27 lines elided ↑ open up ↑
 976  965  
 977  966  int
 978  967  ptcpoll(dev_t dev, short events, int anyyet, short *reventsp,
 979  968      struct pollhead **phpp)
 980  969  {
 981  970          struct pty *pty = &pty_softc[getminor(dev)];
 982  971          pollhead_t *php = &ptcph;
 983  972          queue_t *q;
 984  973          int pos = 0;
 985  974  
 986      -#ifdef lint
 987      -        anyyet = anyyet;
 988      -#endif
 989  975          if (polllock(php, &pty->ptc_lock) != 0) {
 990  976                  *reventsp = POLLNVAL;
 991  977                  return (0);
 992  978          }
 993  979  
 994  980          ASSERT(MUTEX_HELD(&pty->ptc_lock));
 995  981  
 996  982          *reventsp = 0;
 997      -        if (pty->pt_flags & PF_SLAVEGONE) {
      983 +        if (pty->pt_flags & PF_SUBSIDGONE) {
 998  984                  if (events & (POLLIN|POLLRDNORM))
 999  985                          *reventsp |= (events & (POLLIN|POLLRDNORM));
1000  986                  if (events & (POLLOUT|POLLWRNORM))
1001  987                          *reventsp |= (events & (POLLOUT|POLLWRNORM));
1002  988                  mutex_exit(&pty->ptc_lock);
1003  989                  /*
1004  990                   * A non NULL pollhead pointer should be returned in case
1005  991                   * user polls for 0 events.
1006  992                   */
1007  993                  *phpp = !anyyet && !*reventsp ? php : (struct pollhead *)NULL;
↓ open down ↓ 114 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX