Print this page
    
OS-3752 Increase IOV_MAX to at least 1024
OS-2834 ship lx brand
    
      
        | Split | 
	Close | 
      
      | Expand all | 
      | Collapse all | 
    
    
          --- old/usr/src/uts/common/os/streamio.c
          +++ new/usr/src/uts/common/os/streamio.c
   1    1  /*
   2    2   * CDDL HEADER START
   3    3   *
   4    4   * The contents of this file are subject to the terms of the
   5    5   * Common Development and Distribution License (the "License").
   6    6   * You may not use this file except in compliance with the License.
   7    7   *
   8    8   * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9    9   * or http://www.opensolaris.org/os/licensing.
  10   10   * See the License for the specific language governing permissions
  11   11   * and limitations under the License.
  12   12   *
  13   13   * When distributing Covered Code, include this CDDL HEADER in each
  14   14   * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  
    | 
      ↓ open down ↓ | 
    16 lines elided | 
    
      ↑ open up ↑ | 
  
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  /*      Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
  22   22  /*        All Rights Reserved   */
  23   23  
  24   24  
  25   25  /*
  26   26   * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
  27      - * Copyright (c) 2014, Joyent, Inc. All rights reserved.
       27 + * Copyright 2015, Joyent, Inc. All rights reserved.
  28   28   */
  29   29  
  30   30  #include <sys/types.h>
  31   31  #include <sys/sysmacros.h>
  32   32  #include <sys/param.h>
  33   33  #include <sys/errno.h>
  34   34  #include <sys/signal.h>
  35   35  #include <sys/stat.h>
  36   36  #include <sys/proc.h>
  37   37  #include <sys/cred.h>
  38   38  #include <sys/user.h>
  39   39  #include <sys/vnode.h>
  40   40  #include <sys/file.h>
  41   41  #include <sys/stream.h>
  42   42  #include <sys/strsubr.h>
  43   43  #include <sys/stropts.h>
  44   44  #include <sys/tihdr.h>
  45   45  #include <sys/var.h>
  46   46  #include <sys/poll.h>
  47   47  #include <sys/termio.h>
  48   48  #include <sys/ttold.h>
  49   49  #include <sys/systm.h>
  50   50  #include <sys/uio.h>
  51   51  #include <sys/cmn_err.h>
  52   52  #include <sys/sad.h>
  53   53  #include <sys/netstack.h>
  54   54  #include <sys/priocntl.h>
  55   55  #include <sys/jioctl.h>
  56   56  #include <sys/procset.h>
  57   57  #include <sys/session.h>
  58   58  #include <sys/kmem.h>
  59   59  #include <sys/filio.h>
  60   60  #include <sys/vtrace.h>
  61   61  #include <sys/debug.h>
  62   62  #include <sys/strredir.h>
  63   63  #include <sys/fs/fifonode.h>
  64   64  #include <sys/fs/snode.h>
  65   65  #include <sys/strlog.h>
  66   66  #include <sys/strsun.h>
  67   67  #include <sys/project.h>
  68   68  #include <sys/kbio.h>
  69   69  #include <sys/msio.h>
  
    | 
      ↓ open down ↓ | 
    32 lines elided | 
    
      ↑ open up ↑ | 
  
  70   70  #include <sys/tty.h>
  71   71  #include <sys/ptyvar.h>
  72   72  #include <sys/vuid_event.h>
  73   73  #include <sys/modctl.h>
  74   74  #include <sys/sunddi.h>
  75   75  #include <sys/sunldi_impl.h>
  76   76  #include <sys/autoconf.h>
  77   77  #include <sys/policy.h>
  78   78  #include <sys/dld.h>
  79   79  #include <sys/zone.h>
       80 +#include <sys/limits.h>
  80   81  #include <c2/audit.h>
  81   82  
  82   83  /*
  83   84   * This define helps improve the readability of streams code while
  84   85   * still maintaining a very old streams performance enhancement.  The
  85   86   * performance enhancement basically involved having all callers
  86   87   * of straccess() perform the first check that straccess() will do
  87   88   * locally before actually calling straccess().  (There by reducing
  88   89   * the number of unnecessary calls to straccess().)
  89   90   */
  90   91  #define i_straccess(x, y)       ((stp->sd_sidp == NULL) ? 0 : \
  91   92                                      (stp->sd_vnode->v_type == VFIFO) ? 0 : \
  92   93                                      straccess((x), (y)))
  93   94  
  94   95  /*
  95   96   * what is mblk_pull_len?
  96   97   *
  97   98   * If a streams message consists of many short messages,
  98   99   * a performance degradation occurs from copyout overhead.
  99  100   * To decrease the per mblk overhead, messages that are
 100  101   * likely to consist of many small mblks are pulled up into
 101  102   * one continuous chunk of memory.
 102  103   *
 103  104   * To avoid the processing overhead of examining every
 104  105   * mblk, a quick heuristic is used. If the first mblk in
 105  106   * the message is shorter than mblk_pull_len, it is likely
 106  107   * that the rest of the mblk will be short.
 107  108   *
 108  109   * This heuristic was decided upon after performance tests
 109  110   * indicated that anything more complex slowed down the main
 110  111   * code path.
 111  112   */
 112  113  #define MBLK_PULL_LEN 64
 113  114  uint32_t mblk_pull_len = MBLK_PULL_LEN;
 114  115  
 115  116  /*
 116  117   * The sgttyb_handling flag controls the handling of the old BSD
 117  118   * TIOCGETP, TIOCSETP, and TIOCSETN ioctls as follows:
 118  119   *
 119  120   * 0 - Emit no warnings at all and retain old, broken behavior.
 120  121   * 1 - Emit no warnings and silently handle new semantics.
 121  122   * 2 - Send cmn_err(CE_NOTE) when either TIOCSETP or TIOCSETN is used
 122  123   *     (once per system invocation).  Handle with new semantics.
 123  124   * 3 - Send SIGSYS when any TIOCGETP, TIOCSETP, or TIOCSETN call is
 124  125   *     made (so that offenders drop core and are easy to debug).
 125  126   *
 126  127   * The "new semantics" are that TIOCGETP returns B38400 for
 127  128   * sg_[io]speed if the corresponding value is over B38400, and that
 128  129   * TIOCSET[PN] accept B38400 in these cases to mean "retain current
 129  130   * bit rate."
 130  131   */
 131  132  int sgttyb_handling = 1;
 132  133  static boolean_t sgttyb_complaint;
 133  134  
 134  135  /* don't push drcompat module by default on Style-2 streams */
 135  136  static int push_drcompat = 0;
 136  137  
 137  138  /*
 138  139   * id value used to distinguish between different ioctl messages
 139  140   */
 140  141  static uint32_t ioc_id;
 141  142  
 142  143  static void putback(struct stdata *, queue_t *, mblk_t *, int);
 143  144  static void strcleanall(struct vnode *);
 144  145  static int strwsrv(queue_t *);
 145  146  static int strdocmd(struct stdata *, struct strcmd *, cred_t *);
 146  147  
 147  148  /*
 148  149   * qinit and module_info structures for stream head read and write queues
 149  150   */
 150  151  struct module_info strm_info = { 0, "strrhead", 0, INFPSZ, STRHIGH, STRLOW };
 151  152  struct module_info stwm_info = { 0, "strwhead", 0, 0, 0, 0 };
 152  153  struct qinit strdata = { strrput, NULL, NULL, NULL, NULL, &strm_info };
 153  154  struct qinit stwdata = { NULL, strwsrv, NULL, NULL, NULL, &stwm_info };
 154  155  struct module_info fiform_info = { 0, "fifostrrhead", 0, PIPE_BUF, FIFOHIWAT,
 155  156      FIFOLOWAT };
 156  157  struct module_info fifowm_info = { 0, "fifostrwhead", 0, 0, 0, 0 };
 157  158  struct qinit fifo_strdata = { strrput, NULL, NULL, NULL, NULL, &fiform_info };
 158  159  struct qinit fifo_stwdata = { NULL, strwsrv, NULL, NULL, NULL, &fifowm_info };
 159  160  
 160  161  extern kmutex_t strresources;   /* protects global resources */
 161  162  extern kmutex_t muxifier;       /* single-threads multiplexor creation */
 162  163  
 163  164  static boolean_t msghasdata(mblk_t *bp);
 164  165  #define msgnodata(bp) (!msghasdata(bp))
 165  166  
 166  167  /*
 167  168   * Stream head locking notes:
 168  169   *      There are four monitors associated with the stream head:
 169  170   *      1. v_stream monitor: in stropen() and strclose() v_lock
 170  171   *              is held while the association of vnode and stream
 171  172   *              head is established or tested for.
 172  173   *      2. open/close/push/pop monitor: sd_lock is held while each
 173  174   *              thread bids for exclusive access to this monitor
 174  175   *              for opening or closing a stream.  In addition, this
 175  176   *              monitor is entered during pushes and pops.  This
 176  177   *              guarantees that during plumbing operations there
 177  178   *              is only one thread trying to change the plumbing.
 178  179   *              Any other threads present in the stream are only
 179  180   *              using the plumbing.
 180  181   *      3. read/write monitor: in the case of read, a thread holds
 181  182   *              sd_lock while trying to get data from the stream
 182  183   *              head queue.  if there is none to fulfill a read
 183  184   *              request, it sets RSLEEP and calls cv_wait_sig() down
 184  185   *              in strwaitq() to await the arrival of new data.
 185  186   *              when new data arrives in strrput(), sd_lock is acquired
 186  187   *              before testing for RSLEEP and calling cv_broadcast().
 187  188   *              the behavior of strwrite(), strwsrv(), and WSLEEP
 188  189   *              mirror this.
 189  190   *      4. ioctl monitor: sd_lock is gotten to ensure that only one
 190  191   *              thread is doing an ioctl at a time.
 191  192   */
 192  193  
 193  194  static int
 194  195  push_mod(queue_t *qp, dev_t *devp, struct stdata *stp, const char *name,
 195  196      int anchor, cred_t *crp, uint_t anchor_zoneid)
 196  197  {
 197  198          int error;
 198  199          fmodsw_impl_t *fp;
 199  200  
 200  201          if (stp->sd_flag & (STRHUP|STRDERR|STWRERR)) {
 201  202                  error = (stp->sd_flag & STRHUP) ? ENXIO : EIO;
 202  203                  return (error);
 203  204          }
 204  205          if (stp->sd_pushcnt >= nstrpush) {
 205  206                  return (EINVAL);
 206  207          }
 207  208  
 208  209          if ((fp = fmodsw_find(name, FMODSW_HOLD | FMODSW_LOAD)) == NULL) {
 209  210                  stp->sd_flag |= STREOPENFAIL;
 210  211                  return (EINVAL);
 211  212          }
 212  213  
 213  214          /*
 214  215           * push new module and call its open routine via qattach
 215  216           */
 216  217          if ((error = qattach(qp, devp, 0, crp, fp, B_FALSE)) != 0)
 217  218                  return (error);
 218  219  
 219  220          /*
 220  221           * Check to see if caller wants a STREAMS anchor
 221  222           * put at this place in the stream, and add if so.
 222  223           */
 223  224          mutex_enter(&stp->sd_lock);
 224  225          if (anchor == stp->sd_pushcnt) {
 225  226                  stp->sd_anchor = stp->sd_pushcnt;
 226  227                  stp->sd_anchorzone = anchor_zoneid;
 227  228          }
 228  229          mutex_exit(&stp->sd_lock);
 229  230  
 230  231          return (0);
 231  232  }
 232  233  
 233  234  /*
 234  235   * Open a stream device.
 235  236   */
 236  237  int
 237  238  stropen(vnode_t *vp, dev_t *devp, int flag, cred_t *crp)
 238  239  {
 239  240          struct stdata *stp;
 240  241          queue_t *qp;
 241  242          int s;
 242  243          dev_t dummydev, savedev;
 243  244          struct autopush *ap;
 244  245          struct dlautopush dlap;
 245  246          int error = 0;
 246  247          ssize_t rmin, rmax;
 247  248          int cloneopen;
 248  249          queue_t *brq;
 249  250          major_t major;
 250  251          str_stack_t *ss;
 251  252          zoneid_t zoneid;
 252  253          uint_t anchor;
 253  254  
 254  255          /*
 255  256           * If the stream already exists, wait for any open in progress
 256  257           * to complete, then call the open function of each module and
 257  258           * driver in the stream.  Otherwise create the stream.
 258  259           */
 259  260          TRACE_1(TR_FAC_STREAMS_FR, TR_STROPEN, "stropen:%p", vp);
 260  261  retry:
 261  262          mutex_enter(&vp->v_lock);
 262  263          if ((stp = vp->v_stream) != NULL) {
 263  264  
 264  265                  /*
 265  266                   * Waiting for stream to be created to device
 266  267                   * due to another open.
 267  268                   */
 268  269                  mutex_exit(&vp->v_lock);
 269  270  
 270  271                  if (STRMATED(stp)) {
 271  272                          struct stdata *strmatep = stp->sd_mate;
 272  273  
 273  274                          STRLOCKMATES(stp);
 274  275                          if (strmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
 275  276                                  if (flag & (FNDELAY|FNONBLOCK)) {
 276  277                                          error = EAGAIN;
 277  278                                          mutex_exit(&strmatep->sd_lock);
 278  279                                          goto ckreturn;
 279  280                                  }
 280  281                                  mutex_exit(&stp->sd_lock);
 281  282                                  if (!cv_wait_sig(&strmatep->sd_monitor,
 282  283                                      &strmatep->sd_lock)) {
 283  284                                          error = EINTR;
 284  285                                          mutex_exit(&strmatep->sd_lock);
 285  286                                          mutex_enter(&stp->sd_lock);
 286  287                                          goto ckreturn;
 287  288                                  }
 288  289                                  mutex_exit(&strmatep->sd_lock);
 289  290                                  goto retry;
 290  291                          }
 291  292                          if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
 292  293                                  if (flag & (FNDELAY|FNONBLOCK)) {
 293  294                                          error = EAGAIN;
 294  295                                          mutex_exit(&strmatep->sd_lock);
 295  296                                          goto ckreturn;
 296  297                                  }
 297  298                                  mutex_exit(&strmatep->sd_lock);
 298  299                                  if (!cv_wait_sig(&stp->sd_monitor,
 299  300                                      &stp->sd_lock)) {
 300  301                                          error = EINTR;
 301  302                                          goto ckreturn;
 302  303                                  }
 303  304                                  mutex_exit(&stp->sd_lock);
 304  305                                  goto retry;
 305  306                          }
 306  307  
 307  308                          if (stp->sd_flag & (STRDERR|STWRERR)) {
 308  309                                  error = EIO;
 309  310                                  mutex_exit(&strmatep->sd_lock);
 310  311                                  goto ckreturn;
 311  312                          }
 312  313  
 313  314                          stp->sd_flag |= STWOPEN;
 314  315                          STRUNLOCKMATES(stp);
 315  316                  } else {
 316  317                          mutex_enter(&stp->sd_lock);
 317  318                          if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
 318  319                                  if (flag & (FNDELAY|FNONBLOCK)) {
 319  320                                          error = EAGAIN;
 320  321                                          goto ckreturn;
 321  322                                  }
 322  323                                  if (!cv_wait_sig(&stp->sd_monitor,
 323  324                                      &stp->sd_lock)) {
 324  325                                          error = EINTR;
 325  326                                          goto ckreturn;
 326  327                                  }
 327  328                                  mutex_exit(&stp->sd_lock);
 328  329                                  goto retry;  /* could be clone! */
 329  330                          }
 330  331  
 331  332                          if (stp->sd_flag & (STRDERR|STWRERR)) {
 332  333                                  error = EIO;
 333  334                                  goto ckreturn;
 334  335                          }
 335  336  
 336  337                          stp->sd_flag |= STWOPEN;
 337  338                          mutex_exit(&stp->sd_lock);
 338  339                  }
 339  340  
 340  341                  /*
 341  342                   * Open all modules and devices down stream to notify
 342  343                   * that another user is streaming.  For modules, set the
 343  344                   * last argument to MODOPEN and do not pass any open flags.
 344  345                   * Ignore dummydev since this is not the first open.
 345  346                   */
 346  347                  claimstr(stp->sd_wrq);
 347  348                  qp = stp->sd_wrq;
 348  349                  while (_SAMESTR(qp)) {
 349  350                          qp = qp->q_next;
 350  351                          if ((error = qreopen(_RD(qp), devp, flag, crp)) != 0)
 351  352                                  break;
 352  353                  }
 353  354                  releasestr(stp->sd_wrq);
 354  355                  mutex_enter(&stp->sd_lock);
 355  356                  stp->sd_flag &= ~(STRHUP|STWOPEN|STRDERR|STWRERR);
 356  357                  stp->sd_rerror = 0;
 357  358                  stp->sd_werror = 0;
 358  359  ckreturn:
 359  360                  cv_broadcast(&stp->sd_monitor);
 360  361                  mutex_exit(&stp->sd_lock);
 361  362                  return (error);
 362  363          }
 363  364  
 364  365          /*
 365  366           * This vnode isn't streaming.  SPECFS already
 366  367           * checked for multiple vnodes pointing to the
 367  368           * same stream, so create a stream to the driver.
 368  369           */
 369  370          qp = allocq();
 370  371          stp = shalloc(qp);
 371  372  
 372  373          /*
 373  374           * Initialize stream head.  shalloc() has given us
 374  375           * exclusive access, and we have the vnode locked;
 375  376           * we can do whatever we want with stp.
 376  377           */
 377  378          stp->sd_flag = STWOPEN;
 378  379          stp->sd_siglist = NULL;
 379  380          stp->sd_pollist.ph_list = NULL;
 380  381          stp->sd_sigflags = 0;
 381  382          stp->sd_mark = NULL;
 382  383          stp->sd_closetime = STRTIMOUT;
 383  384          stp->sd_sidp = NULL;
 384  385          stp->sd_pgidp = NULL;
 385  386          stp->sd_vnode = vp;
 386  387          stp->sd_rerror = 0;
 387  388          stp->sd_werror = 0;
 388  389          stp->sd_wroff = 0;
 389  390          stp->sd_tail = 0;
 390  391          stp->sd_iocblk = NULL;
 391  392          stp->sd_cmdblk = NULL;
 392  393          stp->sd_pushcnt = 0;
 393  394          stp->sd_qn_minpsz = 0;
 394  395          stp->sd_qn_maxpsz = INFPSZ - 1; /* used to check for initialization */
 395  396          stp->sd_maxblk = INFPSZ;
 396  397          qp->q_ptr = _WR(qp)->q_ptr = stp;
 397  398          STREAM(qp) = STREAM(_WR(qp)) = stp;
 398  399          vp->v_stream = stp;
 399  400          mutex_exit(&vp->v_lock);
 400  401          if (vp->v_type == VFIFO) {
 401  402                  stp->sd_flag |= OLDNDELAY;
 402  403                  /*
 403  404                   * This means, both for pipes and fifos
 404  405                   * strwrite will send SIGPIPE if the other
 405  406                   * end is closed. For putmsg it depends
 406  407                   * on whether it is a XPG4_2 application
 407  408                   * or not
 408  409                   */
 409  410                  stp->sd_wput_opt = SW_SIGPIPE;
 410  411  
 411  412                  /* setq might sleep in kmem_alloc - avoid holding locks. */
 412  413                  setq(qp, &fifo_strdata, &fifo_stwdata, NULL, QMTSAFE,
 413  414                      SQ_CI|SQ_CO, B_FALSE);
 414  415  
 415  416                  set_qend(qp);
 416  417                  stp->sd_strtab = fifo_getinfo();
 417  418                  _WR(qp)->q_nfsrv = _WR(qp);
 418  419                  qp->q_nfsrv = qp;
 419  420                  /*
 420  421                   * Wake up others that are waiting for stream to be created.
 421  422                   */
 422  423                  mutex_enter(&stp->sd_lock);
 423  424                  /*
 424  425                   * nothing is be pushed on stream yet, so
 425  426                   * optimized stream head packetsizes are just that
 426  427                   * of the read queue
 427  428                   */
 428  429                  stp->sd_qn_minpsz = qp->q_minpsz;
 429  430                  stp->sd_qn_maxpsz = qp->q_maxpsz;
 430  431                  stp->sd_flag &= ~STWOPEN;
 431  432                  goto fifo_opendone;
 432  433          }
 433  434          /* setq might sleep in kmem_alloc - avoid holding locks. */
 434  435          setq(qp, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_FALSE);
 435  436  
 436  437          set_qend(qp);
 437  438  
 438  439          /*
 439  440           * Open driver and create stream to it (via qattach).
 440  441           */
 441  442          savedev = *devp;
 442  443          cloneopen = (getmajor(*devp) == clone_major);
 443  444          if ((error = qattach(qp, devp, flag, crp, NULL, B_FALSE)) != 0) {
 444  445                  mutex_enter(&vp->v_lock);
 445  446                  vp->v_stream = NULL;
 446  447                  mutex_exit(&vp->v_lock);
 447  448                  mutex_enter(&stp->sd_lock);
 448  449                  cv_broadcast(&stp->sd_monitor);
 449  450                  mutex_exit(&stp->sd_lock);
 450  451                  freeq(_RD(qp));
 451  452                  shfree(stp);
 452  453                  return (error);
 453  454          }
 454  455          /*
 455  456           * Set sd_strtab after open in order to handle clonable drivers
 456  457           */
 457  458          stp->sd_strtab = STREAMSTAB(getmajor(*devp));
 458  459  
 459  460          /*
 460  461           * Historical note: dummydev used to be be prior to the initial
 461  462           * open (via qattach above), which made the value seen
 462  463           * inconsistent between an I_PUSH and an autopush of a module.
 463  464           */
 464  465          dummydev = *devp;
 465  466  
 466  467          /*
 467  468           * For clone open of old style (Q not associated) network driver,
 468  469           * push DRMODNAME module to handle DL_ATTACH/DL_DETACH
 469  470           */
 470  471          brq = _RD(_WR(qp)->q_next);
 471  472          major = getmajor(*devp);
 472  473          if (push_drcompat && cloneopen && NETWORK_DRV(major) &&
 473  474              ((brq->q_flag & _QASSOCIATED) == 0)) {
 474  475                  if (push_mod(qp, &dummydev, stp, DRMODNAME, 0, crp, 0) != 0)
 475  476                          cmn_err(CE_WARN, "cannot push " DRMODNAME
 476  477                              " streams module");
 477  478          }
 478  479  
 479  480          if (!NETWORK_DRV(major)) {
 480  481                  savedev = *devp;
 481  482          } else {
 482  483                  /*
 483  484                   * For network devices, process differently based on the
 484  485                   * return value from dld_autopush():
 485  486                   *
 486  487                   *   0: the passed-in device points to a GLDv3 datalink with
 487  488                   *   per-link autopush configuration; use that configuration
 488  489                   *   and ignore any per-driver autopush configuration.
 489  490                   *
 490  491                   *   1: the passed-in device points to a physical GLDv3
 491  492                   *   datalink without per-link autopush configuration.  The
 492  493                   *   passed in device was changed to refer to the actual
 493  494                   *   physical device (if it's not already); we use that new
 494  495                   *   device to look up any per-driver autopush configuration.
 495  496                   *
 496  497                   *   -1: neither of the above cases applied; use the initial
 497  498                   *   device to look up any per-driver autopush configuration.
 498  499                   */
 499  500                  switch (dld_autopush(&savedev, &dlap)) {
 500  501                  case 0:
 501  502                          zoneid = crgetzoneid(crp);
 502  503                          for (s = 0; s < dlap.dap_npush; s++) {
 503  504                                  error = push_mod(qp, &dummydev, stp,
 504  505                                      dlap.dap_aplist[s], dlap.dap_anchor, crp,
 505  506                                      zoneid);
 506  507                                  if (error != 0)
 507  508                                          break;
 508  509                          }
 509  510                          goto opendone;
 510  511                  case 1:
 511  512                          break;
 512  513                  case -1:
 513  514                          savedev = *devp;
 514  515                          break;
 515  516                  }
 516  517          }
 517  518          /*
 518  519           * Find the autopush configuration based on "savedev". Start with the
 519  520           * global zone. If not found check in the local zone.
 520  521           */
 521  522          zoneid = GLOBAL_ZONEID;
 522  523  retryap:
 523  524          ss = netstack_find_by_stackid(zoneid_to_netstackid(zoneid))->
 524  525              netstack_str;
 525  526          if ((ap = sad_ap_find_by_dev(savedev, ss)) == NULL) {
 526  527                  netstack_rele(ss->ss_netstack);
 527  528                  if (zoneid == GLOBAL_ZONEID) {
 528  529                          /*
 529  530                           * None found. Also look in the zone's autopush table.
 530  531                           */
 531  532                          zoneid = crgetzoneid(crp);
 532  533                          if (zoneid != GLOBAL_ZONEID)
 533  534                                  goto retryap;
 534  535                  }
 535  536                  goto opendone;
 536  537          }
 537  538          anchor = ap->ap_anchor;
 538  539          zoneid = crgetzoneid(crp);
 539  540          for (s = 0; s < ap->ap_npush; s++) {
 540  541                  error = push_mod(qp, &dummydev, stp, ap->ap_list[s],
 541  542                      anchor, crp, zoneid);
 542  543                  if (error != 0)
 543  544                          break;
 544  545          }
 545  546          sad_ap_rele(ap, ss);
 546  547          netstack_rele(ss->ss_netstack);
 547  548  
 548  549  opendone:
 549  550  
 550  551          /*
 551  552           * let specfs know that open failed part way through
 552  553           */
 553  554          if (error) {
 554  555                  mutex_enter(&stp->sd_lock);
 555  556                  stp->sd_flag |= STREOPENFAIL;
 556  557                  mutex_exit(&stp->sd_lock);
 557  558          }
 558  559  
 559  560          /*
 560  561           * Wake up others that are waiting for stream to be created.
 561  562           */
 562  563          mutex_enter(&stp->sd_lock);
 563  564          stp->sd_flag &= ~STWOPEN;
 564  565  
 565  566          /*
 566  567           * As a performance concern we are caching the values of
 567  568           * q_minpsz and q_maxpsz of the module below the stream
 568  569           * head in the stream head.
 569  570           */
 570  571          mutex_enter(QLOCK(stp->sd_wrq->q_next));
 571  572          rmin = stp->sd_wrq->q_next->q_minpsz;
 572  573          rmax = stp->sd_wrq->q_next->q_maxpsz;
 573  574          mutex_exit(QLOCK(stp->sd_wrq->q_next));
 574  575  
 575  576          /* do this processing here as a performance concern */
 576  577          if (strmsgsz != 0) {
 577  578                  if (rmax == INFPSZ)
 578  579                          rmax = strmsgsz;
 579  580                  else
 580  581                          rmax = MIN(strmsgsz, rmax);
 581  582          }
 582  583  
 583  584          mutex_enter(QLOCK(stp->sd_wrq));
 584  585          stp->sd_qn_minpsz = rmin;
 585  586          stp->sd_qn_maxpsz = rmax;
 586  587          mutex_exit(QLOCK(stp->sd_wrq));
 587  588  
 588  589  fifo_opendone:
 589  590          cv_broadcast(&stp->sd_monitor);
 590  591          mutex_exit(&stp->sd_lock);
 591  592          return (error);
 592  593  }
 593  594  
 594  595  static int strsink(queue_t *, mblk_t *);
 595  596  static struct qinit deadrend = {
 596  597          strsink, NULL, NULL, NULL, NULL, &strm_info, NULL
 597  598  };
 598  599  static struct qinit deadwend = {
 599  600          NULL, NULL, NULL, NULL, NULL, &stwm_info, NULL
 600  601  };
 601  602  
 602  603  /*
 603  604   * Close a stream.
 604  605   * This is called from closef() on the last close of an open stream.
 605  606   * Strclean() will already have removed the siglist and pollist
 606  607   * information, so all that remains is to remove all multiplexor links
 607  608   * for the stream, pop all the modules (and the driver), and free the
 608  609   * stream structure.
 609  610   */
 610  611  
 611  612  int
 612  613  strclose(struct vnode *vp, int flag, cred_t *crp)
 613  614  {
 614  615          struct stdata *stp;
 615  616          queue_t *qp;
 616  617          int rval;
 617  618          int freestp = 1;
 618  619          queue_t *rmq;
 619  620  
 620  621          TRACE_1(TR_FAC_STREAMS_FR,
 621  622              TR_STRCLOSE, "strclose:%p", vp);
 622  623          ASSERT(vp->v_stream);
 623  624  
 624  625          stp = vp->v_stream;
 625  626          ASSERT(!(stp->sd_flag & STPLEX));
 626  627          qp = stp->sd_wrq;
 627  628  
 628  629          /*
 629  630           * Needed so that strpoll will return non-zero for this fd.
 630  631           * Note that with POLLNOERR STRHUP does still cause POLLHUP.
 631  632           */
 632  633          mutex_enter(&stp->sd_lock);
 633  634          stp->sd_flag |= STRHUP;
 634  635          mutex_exit(&stp->sd_lock);
 635  636  
 636  637          /*
 637  638           * If the registered process or process group did not have an
 638  639           * open instance of this stream then strclean would not be
 639  640           * called. Thus at the time of closing all remaining siglist entries
 640  641           * are removed.
 641  642           */
 642  643          if (stp->sd_siglist != NULL)
 643  644                  strcleanall(vp);
 644  645  
 645  646          ASSERT(stp->sd_siglist == NULL);
 646  647          ASSERT(stp->sd_sigflags == 0);
 647  648  
 648  649          if (STRMATED(stp)) {
 649  650                  struct stdata *strmatep = stp->sd_mate;
 650  651                  int waited = 1;
 651  652  
 652  653                  STRLOCKMATES(stp);
 653  654                  while (waited) {
 654  655                          waited = 0;
 655  656                          while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
 656  657                                  mutex_exit(&strmatep->sd_lock);
 657  658                                  cv_wait(&stp->sd_monitor, &stp->sd_lock);
 658  659                                  mutex_exit(&stp->sd_lock);
 659  660                                  STRLOCKMATES(stp);
 660  661                                  waited = 1;
 661  662                          }
 662  663                          while (strmatep->sd_flag &
 663  664                              (STWOPEN|STRCLOSE|STRPLUMB)) {
 664  665                                  mutex_exit(&stp->sd_lock);
 665  666                                  cv_wait(&strmatep->sd_monitor,
 666  667                                      &strmatep->sd_lock);
 667  668                                  mutex_exit(&strmatep->sd_lock);
 668  669                                  STRLOCKMATES(stp);
 669  670                                  waited = 1;
 670  671                          }
 671  672                  }
 672  673                  stp->sd_flag |= STRCLOSE;
 673  674                  STRUNLOCKMATES(stp);
 674  675          } else {
 675  676                  mutex_enter(&stp->sd_lock);
 676  677                  stp->sd_flag |= STRCLOSE;
 677  678                  mutex_exit(&stp->sd_lock);
 678  679          }
 679  680  
 680  681          ASSERT(qp->q_first == NULL);    /* No more delayed write */
 681  682  
 682  683          /* Check if an I_LINK was ever done on this stream */
 683  684          if (stp->sd_flag & STRHASLINKS) {
 684  685                  netstack_t *ns;
 685  686                  str_stack_t *ss;
 686  687  
 687  688                  ns = netstack_find_by_cred(crp);
 688  689                  ASSERT(ns != NULL);
 689  690                  ss = ns->netstack_str;
 690  691                  ASSERT(ss != NULL);
 691  692  
 692  693                  (void) munlinkall(stp, LINKCLOSE|LINKNORMAL, crp, &rval, ss);
 693  694                  netstack_rele(ss->ss_netstack);
 694  695          }
 695  696  
 696  697          while (_SAMESTR(qp)) {
 697  698                  /*
 698  699                   * Holding sd_lock prevents q_next from changing in
 699  700                   * this stream.
 700  701                   */
 701  702                  mutex_enter(&stp->sd_lock);
 702  703                  if (!(flag & (FNDELAY|FNONBLOCK)) && (stp->sd_closetime > 0)) {
 703  704  
 704  705                          /*
 705  706                           * sleep until awakened by strwsrv() or timeout
 706  707                           */
 707  708                          for (;;) {
 708  709                                  mutex_enter(QLOCK(qp->q_next));
 709  710                                  if (!(qp->q_next->q_mblkcnt)) {
 710  711                                          mutex_exit(QLOCK(qp->q_next));
 711  712                                          break;
 712  713                                  }
 713  714                                  stp->sd_flag |= WSLEEP;
 714  715  
 715  716                                  /* ensure strwsrv gets enabled */
 716  717                                  qp->q_next->q_flag |= QWANTW;
 717  718                                  mutex_exit(QLOCK(qp->q_next));
 718  719                                  /* get out if we timed out or recv'd a signal */
 719  720                                  if (str_cv_wait(&qp->q_wait, &stp->sd_lock,
 720  721                                      stp->sd_closetime, 0) <= 0) {
 721  722                                          break;
 722  723                                  }
 723  724                          }
 724  725                          stp->sd_flag &= ~WSLEEP;
 725  726                  }
 726  727                  mutex_exit(&stp->sd_lock);
 727  728  
 728  729                  rmq = qp->q_next;
 729  730                  if (rmq->q_flag & QISDRV) {
 730  731                          ASSERT(!_SAMESTR(rmq));
 731  732                          wait_sq_svc(_RD(qp)->q_syncq);
 732  733                  }
 733  734  
 734  735                  qdetach(_RD(rmq), 1, flag, crp, B_FALSE);
 735  736          }
 736  737  
 737  738          /*
 738  739           * Since we call pollwakeup in close() now, the poll list should
 739  740           * be empty in most cases. The only exception is the layered devices
 740  741           * (e.g. the console drivers with redirection modules pushed on top
 741  742           * of it).  We have to do this after calling qdetach() because
 742  743           * the redirection module won't have torn down the console
 743  744           * redirection until after qdetach() has been invoked.
 744  745           */
 745  746          if (stp->sd_pollist.ph_list != NULL) {
 746  747                  pollwakeup(&stp->sd_pollist, POLLERR);
 747  748                  pollhead_clean(&stp->sd_pollist);
 748  749          }
 749  750          ASSERT(stp->sd_pollist.ph_list == NULL);
 750  751          ASSERT(stp->sd_sidp == NULL);
 751  752          ASSERT(stp->sd_pgidp == NULL);
 752  753  
 753  754          /* Prevent qenable from re-enabling the stream head queue */
 754  755          disable_svc(_RD(qp));
 755  756  
 756  757          /*
 757  758           * Wait until service procedure of each queue is
 758  759           * run, if QINSERVICE is set.
 759  760           */
 760  761          wait_svc(_RD(qp));
 761  762  
 762  763          /*
 763  764           * Now, flush both queues.
 764  765           */
 765  766          flushq(_RD(qp), FLUSHALL);
 766  767          flushq(qp, FLUSHALL);
 767  768  
 768  769          /*
 769  770           * If the write queue of the stream head is pointing to a
 770  771           * read queue, we have a twisted stream.  If the read queue
 771  772           * is alive, convert the stream head queues into a dead end.
 772  773           * If the read queue is dead, free the dead pair.
 773  774           */
 774  775          if (qp->q_next && !_SAMESTR(qp)) {
 775  776                  if (qp->q_next->q_qinfo == &deadrend) { /* half-closed pipe */
 776  777                          flushq(qp->q_next, FLUSHALL); /* ensure no message */
 777  778                          shfree(qp->q_next->q_stream);
 778  779                          freeq(qp->q_next);
 779  780                          freeq(_RD(qp));
 780  781                  } else if (qp->q_next == _RD(qp)) {     /* fifo */
 781  782                          freeq(_RD(qp));
 782  783                  } else {                                /* pipe */
 783  784                          freestp = 0;
 784  785                          /*
 785  786                           * The q_info pointers are never accessed when
 786  787                           * SQLOCK is held.
 787  788                           */
 788  789                          ASSERT(qp->q_syncq == _RD(qp)->q_syncq);
 789  790                          mutex_enter(SQLOCK(qp->q_syncq));
 790  791                          qp->q_qinfo = &deadwend;
 791  792                          _RD(qp)->q_qinfo = &deadrend;
 792  793                          mutex_exit(SQLOCK(qp->q_syncq));
 793  794                  }
 794  795          } else {
 795  796                  freeq(_RD(qp)); /* free stream head queue pair */
 796  797          }
 797  798  
 798  799          mutex_enter(&vp->v_lock);
 799  800          if (stp->sd_iocblk) {
 800  801                  if (stp->sd_iocblk != (mblk_t *)-1) {
 801  802                          freemsg(stp->sd_iocblk);
 802  803                  }
 803  804                  stp->sd_iocblk = NULL;
 804  805          }
 805  806          stp->sd_vnode = NULL;
 806  807          vp->v_stream = NULL;
 807  808          mutex_exit(&vp->v_lock);
 808  809          mutex_enter(&stp->sd_lock);
 809  810          freemsg(stp->sd_cmdblk);
 810  811          stp->sd_cmdblk = NULL;
 811  812          stp->sd_flag &= ~STRCLOSE;
 812  813          cv_broadcast(&stp->sd_monitor);
 813  814          mutex_exit(&stp->sd_lock);
 814  815  
 815  816          if (freestp)
 816  817                  shfree(stp);
 817  818          return (0);
 818  819  }
 819  820  
 820  821  static int
 821  822  strsink(queue_t *q, mblk_t *bp)
 822  823  {
 823  824          struct copyresp *resp;
 824  825  
 825  826          switch (bp->b_datap->db_type) {
 826  827          case M_FLUSH:
 827  828                  if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
 828  829                          *bp->b_rptr &= ~FLUSHR;
 829  830                          bp->b_flag |= MSGNOLOOP;
 830  831                          /*
 831  832                           * Protect against the driver passing up
 832  833                           * messages after it has done a qprocsoff.
 833  834                           */
 834  835                          if (_OTHERQ(q)->q_next == NULL)
 835  836                                  freemsg(bp);
 836  837                          else
 837  838                                  qreply(q, bp);
 838  839                  } else {
 839  840                          freemsg(bp);
 840  841                  }
 841  842                  break;
 842  843  
 843  844          case M_COPYIN:
 844  845          case M_COPYOUT:
 845  846                  if (bp->b_cont) {
 846  847                          freemsg(bp->b_cont);
 847  848                          bp->b_cont = NULL;
 848  849                  }
 849  850                  bp->b_datap->db_type = M_IOCDATA;
 850  851                  bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
 851  852                  resp = (struct copyresp *)bp->b_rptr;
 852  853                  resp->cp_rval = (caddr_t)1;     /* failure */
 853  854                  /*
 854  855                   * Protect against the driver passing up
 855  856                   * messages after it has done a qprocsoff.
 856  857                   */
 857  858                  if (_OTHERQ(q)->q_next == NULL)
 858  859                          freemsg(bp);
 859  860                  else
 860  861                          qreply(q, bp);
 861  862                  break;
 862  863  
 863  864          case M_IOCTL:
 864  865                  if (bp->b_cont) {
 865  866                          freemsg(bp->b_cont);
 866  867                          bp->b_cont = NULL;
 867  868                  }
 868  869                  bp->b_datap->db_type = M_IOCNAK;
 869  870                  /*
 870  871                   * Protect against the driver passing up
 871  872                   * messages after it has done a qprocsoff.
 872  873                   */
 873  874                  if (_OTHERQ(q)->q_next == NULL)
 874  875                          freemsg(bp);
 875  876                  else
 876  877                          qreply(q, bp);
 877  878                  break;
 878  879  
 879  880          default:
 880  881                  freemsg(bp);
 881  882                  break;
 882  883          }
 883  884  
 884  885          return (0);
 885  886  }
 886  887  
 887  888  /*
 888  889   * Clean up after a process when it closes a stream.  This is called
 889  890   * from closef for all closes, whereas strclose is called only for the
 890  891   * last close on a stream.  The siglist is scanned for entries for the
 891  892   * current process, and these are removed.
 892  893   */
 893  894  void
 894  895  strclean(struct vnode *vp)
 895  896  {
 896  897          strsig_t *ssp, *pssp, *tssp;
 897  898          stdata_t *stp;
 898  899          int update = 0;
 899  900  
 900  901          TRACE_1(TR_FAC_STREAMS_FR,
 901  902              TR_STRCLEAN, "strclean:%p", vp);
 902  903          stp = vp->v_stream;
 903  904          pssp = NULL;
 904  905          mutex_enter(&stp->sd_lock);
 905  906          ssp = stp->sd_siglist;
 906  907          while (ssp) {
 907  908                  if (ssp->ss_pidp == curproc->p_pidp) {
 908  909                          tssp = ssp->ss_next;
 909  910                          if (pssp)
 910  911                                  pssp->ss_next = tssp;
 911  912                          else
 912  913                                  stp->sd_siglist = tssp;
 913  914                          mutex_enter(&pidlock);
 914  915                          PID_RELE(ssp->ss_pidp);
 915  916                          mutex_exit(&pidlock);
 916  917                          kmem_free(ssp, sizeof (strsig_t));
 917  918                          update = 1;
 918  919                          ssp = tssp;
 919  920                  } else {
 920  921                          pssp = ssp;
 921  922                          ssp = ssp->ss_next;
 922  923                  }
 923  924          }
 924  925          if (update) {
 925  926                  stp->sd_sigflags = 0;
 926  927                  for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
 927  928                          stp->sd_sigflags |= ssp->ss_events;
 928  929          }
 929  930          mutex_exit(&stp->sd_lock);
 930  931  }
 931  932  
 932  933  /*
 933  934   * Used on the last close to remove any remaining items on the siglist.
 934  935   * These could be present on the siglist due to I_ESETSIG calls that
 935  936   * use process groups or processed that do not have an open file descriptor
 936  937   * for this stream (Such entries would not be removed by strclean).
 937  938   */
 938  939  static void
 939  940  strcleanall(struct vnode *vp)
 940  941  {
 941  942          strsig_t *ssp, *nssp;
 942  943          stdata_t *stp;
 943  944  
 944  945          stp = vp->v_stream;
 945  946          mutex_enter(&stp->sd_lock);
 946  947          ssp = stp->sd_siglist;
 947  948          stp->sd_siglist = NULL;
 948  949          while (ssp) {
 949  950                  nssp = ssp->ss_next;
 950  951                  mutex_enter(&pidlock);
 951  952                  PID_RELE(ssp->ss_pidp);
 952  953                  mutex_exit(&pidlock);
 953  954                  kmem_free(ssp, sizeof (strsig_t));
 954  955                  ssp = nssp;
 955  956          }
 956  957          stp->sd_sigflags = 0;
 957  958          mutex_exit(&stp->sd_lock);
 958  959  }
 959  960  
 960  961  /*
 961  962   * Retrieve the next message from the logical stream head read queue
 962  963   * using either rwnext (if sync stream) or getq_noenab.
 963  964   * It is the callers responsibility to call qbackenable after
 964  965   * it is finished with the message. The caller should not call
 965  966   * qbackenable until after any putback calls to avoid spurious backenabling.
 966  967   */
 967  968  mblk_t *
 968  969  strget(struct stdata *stp, queue_t *q, struct uio *uiop, int first,
 969  970      int *errorp)
 970  971  {
 971  972          mblk_t *bp;
 972  973          int error;
 973  974          ssize_t rbytes = 0;
 974  975  
 975  976          /* Holding sd_lock prevents the read queue from changing  */
 976  977          ASSERT(MUTEX_HELD(&stp->sd_lock));
 977  978  
  
    | 
      ↓ open down ↓ | 
    888 lines elided | 
    
      ↑ open up ↑ | 
  
 978  979          if (uiop != NULL && stp->sd_struiordq != NULL &&
 979  980              q->q_first == NULL &&
 980  981              (!first || (stp->sd_wakeq & RSLEEP))) {
 981  982                  /*
 982  983                   * Stream supports rwnext() for the read side.
 983  984                   * If this is the first time we're called by e.g. strread
 984  985                   * only do the downcall if there is a deferred wakeup
 985  986                   * (registered in sd_wakeq).
 986  987                   */
 987  988                  struiod_t uiod;
      989 +                struct iovec buf[IOV_MAX_STACK];
      990 +                int iovlen = 0;
 988  991  
 989  992                  if (first)
 990  993                          stp->sd_wakeq &= ~RSLEEP;
 991  994  
 992      -                (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
 993      -                    sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
      995 +                if (uiop->uio_iovcnt > IOV_MAX_STACK) {
      996 +                        iovlen = uiop->uio_iovcnt * sizeof (iovec_t);
      997 +                        uiod.d_iov = kmem_alloc(iovlen, KM_SLEEP);
      998 +                } else {
      999 +                        uiod.d_iov = buf;
     1000 +                }
     1001 +
     1002 +                (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov, uiop->uio_iovcnt);
 994 1003                  uiod.d_mp = 0;
 995 1004                  /*
 996 1005                   * Mark that a thread is in rwnext on the read side
 997 1006                   * to prevent strrput from nacking ioctls immediately.
 998 1007                   * When the last concurrent rwnext returns
 999 1008                   * the ioctls are nack'ed.
1000 1009                   */
1001 1010                  ASSERT(MUTEX_HELD(&stp->sd_lock));
1002 1011                  stp->sd_struiodnak++;
1003 1012                  /*
1004 1013                   * Note: rwnext will drop sd_lock.
1005 1014                   */
1006 1015                  error = rwnext(q, &uiod);
1007 1016                  ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
1008 1017                  mutex_enter(&stp->sd_lock);
1009 1018                  stp->sd_struiodnak--;
1010 1019                  while (stp->sd_struiodnak == 0 &&
1011 1020                      ((bp = stp->sd_struionak) != NULL)) {
1012 1021                          stp->sd_struionak = bp->b_next;
1013 1022                          bp->b_next = NULL;
1014 1023                          bp->b_datap->db_type = M_IOCNAK;
1015 1024                          /*
1016 1025                           * Protect against the driver passing up
1017 1026                           * messages after it has done a qprocsoff.
1018 1027                           */
1019 1028                          if (_OTHERQ(q)->q_next == NULL)
1020 1029                                  freemsg(bp);
1021 1030                          else {
  
    | 
      ↓ open down ↓ | 
    18 lines elided | 
    
      ↑ open up ↑ | 
  
1022 1031                                  mutex_exit(&stp->sd_lock);
1023 1032                                  qreply(q, bp);
1024 1033                                  mutex_enter(&stp->sd_lock);
1025 1034                          }
1026 1035                  }
1027 1036                  ASSERT(MUTEX_HELD(&stp->sd_lock));
1028 1037                  if (error == 0 || error == EWOULDBLOCK) {
1029 1038                          if ((bp = uiod.d_mp) != NULL) {
1030 1039                                  *errorp = 0;
1031 1040                                  ASSERT(MUTEX_HELD(&stp->sd_lock));
     1041 +                                if (iovlen != 0)
     1042 +                                        kmem_free(uiod.d_iov, iovlen);
1032 1043                                  return (bp);
1033 1044                          }
1034 1045                          error = 0;
1035 1046                  } else if (error == EINVAL) {
1036 1047                          /*
1037 1048                           * The stream plumbing must have
1038 1049                           * changed while we were away, so
1039 1050                           * just turn off rwnext()s.
1040 1051                           */
1041 1052                          error = 0;
1042 1053                  } else if (error == EBUSY) {
1043 1054                          /*
1044 1055                           * The module might have data in transit using putnext
1045 1056                           * Fall back on waiting + getq.
1046 1057                           */
1047 1058                          error = 0;
1048 1059                  } else {
1049 1060                          *errorp = error;
1050 1061                          ASSERT(MUTEX_HELD(&stp->sd_lock));
     1062 +                        if (iovlen != 0)
     1063 +                                kmem_free(uiod.d_iov, iovlen);
1051 1064                          return (NULL);
1052 1065                  }
     1066 +
     1067 +                if (iovlen != 0)
     1068 +                        kmem_free(uiod.d_iov, iovlen);
     1069 +
1053 1070                  /*
1054 1071                   * Try a getq in case a rwnext() generated mblk
1055 1072                   * has bubbled up via strrput().
1056 1073                   */
1057 1074          }
1058 1075          *errorp = 0;
1059 1076          ASSERT(MUTEX_HELD(&stp->sd_lock));
1060 1077  
1061 1078          /*
1062 1079           * If we have a valid uio, try and use this as a guide for how
1063 1080           * many bytes to retrieve from the queue via getq_noenab().
1064 1081           * Doing this can avoid unneccesary counting of overlong
1065 1082           * messages in putback(). We currently only do this for sockets
1066 1083           * and only if there is no sd_rputdatafunc hook.
1067 1084           *
1068 1085           * The sd_rputdatafunc hook transforms the entire message
1069 1086           * before any bytes in it can be given to a client. So, rbytes
1070 1087           * must be 0 if there is a hook.
1071 1088           */
1072 1089          if ((uiop != NULL) && (stp->sd_vnode->v_type == VSOCK) &&
1073 1090              (stp->sd_rputdatafunc == NULL))
1074 1091                  rbytes = uiop->uio_resid;
1075 1092  
1076 1093          return (getq_noenab(q, rbytes));
1077 1094  }
1078 1095  
1079 1096  /*
1080 1097   * Copy out the message pointed to by `bp' into the uio pointed to by `uiop'.
1081 1098   * If the message does not fit in the uio the remainder of it is returned;
1082 1099   * otherwise NULL is returned.  Any embedded zero-length mblk_t's are
1083 1100   * consumed, even if uio_resid reaches zero.  On error, `*errorp' is set to
1084 1101   * the error code, the message is consumed, and NULL is returned.
1085 1102   */
1086 1103  static mblk_t *
1087 1104  struiocopyout(mblk_t *bp, struct uio *uiop, int *errorp)
1088 1105  {
1089 1106          int error;
1090 1107          ptrdiff_t n;
1091 1108          mblk_t *nbp;
1092 1109  
1093 1110          ASSERT(bp->b_wptr >= bp->b_rptr);
1094 1111  
1095 1112          do {
1096 1113                  if ((n = MIN(uiop->uio_resid, MBLKL(bp))) != 0) {
1097 1114                          ASSERT(n > 0);
1098 1115  
1099 1116                          error = uiomove(bp->b_rptr, n, UIO_READ, uiop);
1100 1117                          if (error != 0) {
1101 1118                                  freemsg(bp);
1102 1119                                  *errorp = error;
1103 1120                                  return (NULL);
1104 1121                          }
1105 1122                  }
1106 1123  
1107 1124                  bp->b_rptr += n;
1108 1125                  while (bp != NULL && (bp->b_rptr >= bp->b_wptr)) {
1109 1126                          nbp = bp;
1110 1127                          bp = bp->b_cont;
1111 1128                          freeb(nbp);
1112 1129                  }
1113 1130          } while (bp != NULL && uiop->uio_resid > 0);
1114 1131  
1115 1132          *errorp = 0;
1116 1133          return (bp);
1117 1134  }
1118 1135  
1119 1136  /*
1120 1137   * Read a stream according to the mode flags in sd_flag:
1121 1138   *
1122 1139   * (default mode)               - Byte stream, msg boundaries are ignored
1123 1140   * RD_MSGDIS (msg discard)      - Read on msg boundaries and throw away
1124 1141   *                              any data remaining in msg
1125 1142   * RD_MSGNODIS (msg non-discard) - Read on msg boundaries and put back
1126 1143   *                              any remaining data on head of read queue
1127 1144   *
1128 1145   * Consume readable messages on the front of the queue until
1129 1146   * ttolwp(curthread)->lwp_count
1130 1147   * is satisfied, the readable messages are exhausted, or a message
1131 1148   * boundary is reached in a message mode.  If no data was read and
1132 1149   * the stream was not opened with the NDELAY flag, block until data arrives.
1133 1150   * Otherwise return the data read and update the count.
1134 1151   *
1135 1152   * In default mode a 0 length message signifies end-of-file and terminates
1136 1153   * a read in progress.  The 0 length message is removed from the queue
1137 1154   * only if it is the only message read (no data is read).
1138 1155   *
1139 1156   * An attempt to read an M_PROTO or M_PCPROTO message results in an
1140 1157   * EBADMSG error return, unless either RD_PROTDAT or RD_PROTDIS are set.
1141 1158   * If RD_PROTDAT is set, M_PROTO and M_PCPROTO messages are read as data.
1142 1159   * If RD_PROTDIS is set, the M_PROTO and M_PCPROTO parts of the message
1143 1160   * are unlinked from and M_DATA blocks in the message, the protos are
1144 1161   * thrown away, and the data is read.
1145 1162   */
1146 1163  /* ARGSUSED */
1147 1164  int
1148 1165  strread(struct vnode *vp, struct uio *uiop, cred_t *crp)
1149 1166  {
1150 1167          struct stdata *stp;
1151 1168          mblk_t *bp, *nbp;
1152 1169          queue_t *q;
1153 1170          int error = 0;
1154 1171          uint_t old_sd_flag;
1155 1172          int first;
1156 1173          char rflg;
1157 1174          uint_t mark;            /* Contains MSG*MARK and _LASTMARK */
1158 1175  #define _LASTMARK       0x8000  /* Distinct from MSG*MARK */
1159 1176          short delim;
1160 1177          unsigned char pri = 0;
1161 1178          char waitflag;
1162 1179          unsigned char type;
1163 1180  
1164 1181          TRACE_1(TR_FAC_STREAMS_FR,
1165 1182              TR_STRREAD_ENTER, "strread:%p", vp);
1166 1183          ASSERT(vp->v_stream);
1167 1184          stp = vp->v_stream;
1168 1185  
1169 1186          mutex_enter(&stp->sd_lock);
1170 1187  
1171 1188          if ((error = i_straccess(stp, JCREAD)) != 0) {
1172 1189                  mutex_exit(&stp->sd_lock);
1173 1190                  return (error);
1174 1191          }
1175 1192  
1176 1193          if (stp->sd_flag & (STRDERR|STPLEX)) {
1177 1194                  error = strgeterr(stp, STRDERR|STPLEX, 0);
1178 1195                  if (error != 0) {
1179 1196                          mutex_exit(&stp->sd_lock);
1180 1197                          return (error);
1181 1198                  }
1182 1199          }
1183 1200  
1184 1201          /*
1185 1202           * Loop terminates when uiop->uio_resid == 0.
1186 1203           */
1187 1204          rflg = 0;
1188 1205          waitflag = READWAIT;
1189 1206          q = _RD(stp->sd_wrq);
1190 1207          for (;;) {
1191 1208                  ASSERT(MUTEX_HELD(&stp->sd_lock));
1192 1209                  old_sd_flag = stp->sd_flag;
1193 1210                  mark = 0;
1194 1211                  delim = 0;
1195 1212                  first = 1;
1196 1213                  while ((bp = strget(stp, q, uiop, first, &error)) == NULL) {
1197 1214                          int done = 0;
1198 1215  
1199 1216                          ASSERT(MUTEX_HELD(&stp->sd_lock));
1200 1217  
1201 1218                          if (error != 0)
1202 1219                                  goto oops;
1203 1220  
1204 1221                          if (stp->sd_flag & (STRHUP|STREOF)) {
1205 1222                                  goto oops;
1206 1223                          }
1207 1224                          if (rflg && !(stp->sd_flag & STRDELIM)) {
1208 1225                                  goto oops;
1209 1226                          }
1210 1227                          /*
1211 1228                           * If a read(fd,buf,0) has been done, there is no
1212 1229                           * need to sleep. We always have zero bytes to
1213 1230                           * return.
1214 1231                           */
1215 1232                          if (uiop->uio_resid == 0) {
1216 1233                                  goto oops;
1217 1234                          }
1218 1235  
1219 1236                          qbackenable(q, 0);
1220 1237  
1221 1238                          TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_WAIT,
1222 1239                              "strread calls strwaitq:%p, %p, %p",
1223 1240                              vp, uiop, crp);
1224 1241                          if ((error = strwaitq(stp, waitflag, uiop->uio_resid,
1225 1242                              uiop->uio_fmode, -1, &done)) != 0 || done) {
1226 1243                                  TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_DONE,
1227 1244                                      "strread error or done:%p, %p, %p",
1228 1245                                      vp, uiop, crp);
1229 1246                                  if ((uiop->uio_fmode & FNDELAY) &&
1230 1247                                      (stp->sd_flag & OLDNDELAY) &&
1231 1248                                      (error == EAGAIN))
1232 1249                                          error = 0;
1233 1250                                  goto oops;
1234 1251                          }
1235 1252                          TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_AWAKE,
1236 1253                              "strread awakes:%p, %p, %p", vp, uiop, crp);
1237 1254                          if ((error = i_straccess(stp, JCREAD)) != 0) {
1238 1255                                  goto oops;
1239 1256                          }
1240 1257                          first = 0;
1241 1258                  }
1242 1259  
1243 1260                  ASSERT(MUTEX_HELD(&stp->sd_lock));
1244 1261                  ASSERT(bp);
1245 1262                  pri = bp->b_band;
1246 1263                  /*
1247 1264                   * Extract any mark information. If the message is not
1248 1265                   * completely consumed this information will be put in the mblk
1249 1266                   * that is putback.
1250 1267                   * If MSGMARKNEXT is set and the message is completely consumed
1251 1268                   * the STRATMARK flag will be set below. Likewise, if
1252 1269                   * MSGNOTMARKNEXT is set and the message is
1253 1270                   * completely consumed STRNOTATMARK will be set.
1254 1271                   *
1255 1272                   * For some unknown reason strread only breaks the read at the
1256 1273                   * last mark.
1257 1274                   */
1258 1275                  mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
1259 1276                  ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
1260 1277                      (MSGMARKNEXT|MSGNOTMARKNEXT));
1261 1278                  if (mark != 0 && bp == stp->sd_mark) {
1262 1279                          if (rflg) {
1263 1280                                  putback(stp, q, bp, pri);
1264 1281                                  goto oops;
1265 1282                          }
1266 1283                          mark |= _LASTMARK;
1267 1284                          stp->sd_mark = NULL;
1268 1285                  }
1269 1286                  if ((stp->sd_flag & STRDELIM) && (bp->b_flag & MSGDELIM))
1270 1287                          delim = 1;
1271 1288                  mutex_exit(&stp->sd_lock);
1272 1289  
1273 1290                  if (STREAM_NEEDSERVICE(stp))
1274 1291                          stream_runservice(stp);
1275 1292  
1276 1293                  type = bp->b_datap->db_type;
1277 1294  
1278 1295                  switch (type) {
1279 1296  
1280 1297                  case M_DATA:
1281 1298  ismdata:
1282 1299                          if (msgnodata(bp)) {
1283 1300                                  if (mark || delim) {
1284 1301                                          freemsg(bp);
1285 1302                                  } else if (rflg) {
1286 1303  
1287 1304                                          /*
1288 1305                                           * If already read data put zero
1289 1306                                           * length message back on queue else
1290 1307                                           * free msg and return 0.
1291 1308                                           */
1292 1309                                          bp->b_band = pri;
1293 1310                                          mutex_enter(&stp->sd_lock);
1294 1311                                          putback(stp, q, bp, pri);
1295 1312                                          mutex_exit(&stp->sd_lock);
1296 1313                                  } else {
1297 1314                                          freemsg(bp);
1298 1315                                  }
1299 1316                                  error =  0;
1300 1317                                  goto oops1;
1301 1318                          }
1302 1319  
1303 1320                          rflg = 1;
1304 1321                          waitflag |= NOINTR;
1305 1322                          bp = struiocopyout(bp, uiop, &error);
1306 1323                          if (error != 0)
1307 1324                                  goto oops1;
1308 1325  
1309 1326                          mutex_enter(&stp->sd_lock);
1310 1327                          if (bp) {
1311 1328                                  /*
1312 1329                                   * Have remaining data in message.
1313 1330                                   * Free msg if in discard mode.
1314 1331                                   */
1315 1332                                  if (stp->sd_read_opt & RD_MSGDIS) {
1316 1333                                          freemsg(bp);
1317 1334                                  } else {
1318 1335                                          bp->b_band = pri;
1319 1336                                          if ((mark & _LASTMARK) &&
1320 1337                                              (stp->sd_mark == NULL))
1321 1338                                                  stp->sd_mark = bp;
1322 1339                                          bp->b_flag |= mark & ~_LASTMARK;
1323 1340                                          if (delim)
1324 1341                                                  bp->b_flag |= MSGDELIM;
1325 1342                                          if (msgnodata(bp))
1326 1343                                                  freemsg(bp);
1327 1344                                          else
1328 1345                                                  putback(stp, q, bp, pri);
1329 1346                                  }
1330 1347                          } else {
1331 1348                                  /*
1332 1349                                   * Consumed the complete message.
1333 1350                                   * Move the MSG*MARKNEXT information
1334 1351                                   * to the stream head just in case
1335 1352                                   * the read queue becomes empty.
1336 1353                                   *
1337 1354                                   * If the stream head was at the mark
1338 1355                                   * (STRATMARK) before we dropped sd_lock above
1339 1356                                   * and some data was consumed then we have
1340 1357                                   * moved past the mark thus STRATMARK is
1341 1358                                   * cleared. However, if a message arrived in
1342 1359                                   * strrput during the copyout above causing
1343 1360                                   * STRATMARK to be set we can not clear that
1344 1361                                   * flag.
1345 1362                                   */
1346 1363                                  if (mark &
1347 1364                                      (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
1348 1365                                          if (mark & MSGMARKNEXT) {
1349 1366                                                  stp->sd_flag &= ~STRNOTATMARK;
1350 1367                                                  stp->sd_flag |= STRATMARK;
1351 1368                                          } else if (mark & MSGNOTMARKNEXT) {
1352 1369                                                  stp->sd_flag &= ~STRATMARK;
1353 1370                                                  stp->sd_flag |= STRNOTATMARK;
1354 1371                                          } else {
1355 1372                                                  stp->sd_flag &=
1356 1373                                                      ~(STRATMARK|STRNOTATMARK);
1357 1374                                          }
1358 1375                                  } else if (rflg && (old_sd_flag & STRATMARK)) {
1359 1376                                          stp->sd_flag &= ~STRATMARK;
1360 1377                                  }
1361 1378                          }
1362 1379  
1363 1380                          /*
1364 1381                           * Check for signal messages at the front of the read
1365 1382                           * queue and generate the signal(s) if appropriate.
1366 1383                           * The only signal that can be on queue is M_SIG at
1367 1384                           * this point.
1368 1385                           */
1369 1386                          while ((((bp = q->q_first)) != NULL) &&
1370 1387                              (bp->b_datap->db_type == M_SIG)) {
1371 1388                                  bp = getq_noenab(q, 0);
1372 1389                                  /*
1373 1390                                   * sd_lock is held so the content of the
1374 1391                                   * read queue can not change.
1375 1392                                   */
1376 1393                                  ASSERT(bp != NULL && DB_TYPE(bp) == M_SIG);
1377 1394                                  strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
1378 1395                                  mutex_exit(&stp->sd_lock);
1379 1396                                  freemsg(bp);
1380 1397                                  if (STREAM_NEEDSERVICE(stp))
1381 1398                                          stream_runservice(stp);
1382 1399                                  mutex_enter(&stp->sd_lock);
1383 1400                          }
1384 1401  
1385 1402                          if ((uiop->uio_resid == 0) || (mark & _LASTMARK) ||
1386 1403                              delim ||
1387 1404                              (stp->sd_read_opt & (RD_MSGDIS|RD_MSGNODIS))) {
1388 1405                                  goto oops;
1389 1406                          }
1390 1407                          continue;
1391 1408  
1392 1409                  case M_SIG:
1393 1410                          strsignal(stp, *bp->b_rptr, (int32_t)bp->b_band);
1394 1411                          freemsg(bp);
1395 1412                          mutex_enter(&stp->sd_lock);
1396 1413                          continue;
1397 1414  
1398 1415                  case M_PROTO:
1399 1416                  case M_PCPROTO:
1400 1417                          /*
1401 1418                           * Only data messages are readable.
1402 1419                           * Any others generate an error, unless
1403 1420                           * RD_PROTDIS or RD_PROTDAT is set.
1404 1421                           */
1405 1422                          if (stp->sd_read_opt & RD_PROTDAT) {
1406 1423                                  for (nbp = bp; nbp; nbp = nbp->b_next) {
1407 1424                                          if ((nbp->b_datap->db_type ==
1408 1425                                              M_PROTO) ||
1409 1426                                              (nbp->b_datap->db_type ==
1410 1427                                              M_PCPROTO)) {
1411 1428                                                  nbp->b_datap->db_type = M_DATA;
1412 1429                                          } else {
1413 1430                                                  break;
1414 1431                                          }
1415 1432                                  }
1416 1433                                  /*
1417 1434                                   * clear stream head hi pri flag based on
1418 1435                                   * first message
1419 1436                                   */
1420 1437                                  if (type == M_PCPROTO) {
1421 1438                                          mutex_enter(&stp->sd_lock);
1422 1439                                          stp->sd_flag &= ~STRPRI;
1423 1440                                          mutex_exit(&stp->sd_lock);
1424 1441                                  }
1425 1442                                  goto ismdata;
1426 1443                          } else if (stp->sd_read_opt & RD_PROTDIS) {
1427 1444                                  /*
1428 1445                                   * discard non-data messages
1429 1446                                   */
1430 1447                                  while (bp &&
1431 1448                                      ((bp->b_datap->db_type == M_PROTO) ||
1432 1449                                      (bp->b_datap->db_type == M_PCPROTO))) {
1433 1450                                          nbp = unlinkb(bp);
1434 1451                                          freeb(bp);
1435 1452                                          bp = nbp;
1436 1453                                  }
1437 1454                                  /*
1438 1455                                   * clear stream head hi pri flag based on
1439 1456                                   * first message
1440 1457                                   */
1441 1458                                  if (type == M_PCPROTO) {
1442 1459                                          mutex_enter(&stp->sd_lock);
1443 1460                                          stp->sd_flag &= ~STRPRI;
1444 1461                                          mutex_exit(&stp->sd_lock);
1445 1462                                  }
1446 1463                                  if (bp) {
1447 1464                                          bp->b_band = pri;
1448 1465                                          goto ismdata;
1449 1466                                  } else {
1450 1467                                          break;
1451 1468                                  }
1452 1469                          }
1453 1470                          /* FALLTHRU */
1454 1471                  case M_PASSFP:
1455 1472                          if ((bp->b_datap->db_type == M_PASSFP) &&
1456 1473                              (stp->sd_read_opt & RD_PROTDIS)) {
1457 1474                                  freemsg(bp);
1458 1475                                  break;
1459 1476                          }
1460 1477                          mutex_enter(&stp->sd_lock);
1461 1478                          putback(stp, q, bp, pri);
1462 1479                          mutex_exit(&stp->sd_lock);
1463 1480                          if (rflg == 0)
1464 1481                                  error = EBADMSG;
1465 1482                          goto oops1;
1466 1483  
1467 1484                  default:
1468 1485                          /*
1469 1486                           * Garbage on stream head read queue.
1470 1487                           */
1471 1488                          cmn_err(CE_WARN, "bad %x found at stream head\n",
1472 1489                              bp->b_datap->db_type);
1473 1490                          freemsg(bp);
1474 1491                          goto oops1;
1475 1492                  }
1476 1493                  mutex_enter(&stp->sd_lock);
1477 1494          }
1478 1495  oops:
1479 1496          mutex_exit(&stp->sd_lock);
1480 1497  oops1:
1481 1498          qbackenable(q, pri);
1482 1499          return (error);
1483 1500  #undef  _LASTMARK
1484 1501  }
1485 1502  
1486 1503  /*
1487 1504   * Default processing of M_PROTO/M_PCPROTO messages.
1488 1505   * Determine which wakeups and signals are needed.
1489 1506   * This can be replaced by a user-specified procedure for kernel users
1490 1507   * of STREAMS.
1491 1508   */
1492 1509  /* ARGSUSED */
1493 1510  mblk_t *
1494 1511  strrput_proto(vnode_t *vp, mblk_t *mp,
1495 1512      strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1496 1513      strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1497 1514  {
1498 1515          *wakeups = RSLEEP;
1499 1516          *allmsgsigs = 0;
1500 1517  
1501 1518          switch (mp->b_datap->db_type) {
1502 1519          case M_PROTO:
1503 1520                  if (mp->b_band == 0) {
1504 1521                          *firstmsgsigs = S_INPUT | S_RDNORM;
1505 1522                          *pollwakeups = POLLIN | POLLRDNORM;
1506 1523                  } else {
1507 1524                          *firstmsgsigs = S_INPUT | S_RDBAND;
1508 1525                          *pollwakeups = POLLIN | POLLRDBAND;
1509 1526                  }
1510 1527                  break;
1511 1528          case M_PCPROTO:
1512 1529                  *firstmsgsigs = S_HIPRI;
1513 1530                  *pollwakeups = POLLPRI;
1514 1531                  break;
1515 1532          }
1516 1533          return (mp);
1517 1534  }
1518 1535  
1519 1536  /*
1520 1537   * Default processing of everything but M_DATA, M_PROTO, M_PCPROTO and
1521 1538   * M_PASSFP messages.
1522 1539   * Determine which wakeups and signals are needed.
1523 1540   * This can be replaced by a user-specified procedure for kernel users
1524 1541   * of STREAMS.
1525 1542   */
1526 1543  /* ARGSUSED */
1527 1544  mblk_t *
1528 1545  strrput_misc(vnode_t *vp, mblk_t *mp,
1529 1546      strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1530 1547      strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1531 1548  {
1532 1549          *wakeups = 0;
1533 1550          *firstmsgsigs = 0;
1534 1551          *allmsgsigs = 0;
1535 1552          *pollwakeups = 0;
1536 1553          return (mp);
1537 1554  }
1538 1555  
1539 1556  /*
1540 1557   * Stream read put procedure.  Called from downstream driver/module
1541 1558   * with messages for the stream head.  Data, protocol, and in-stream
1542 1559   * signal messages are placed on the queue, others are handled directly.
1543 1560   */
1544 1561  int
1545 1562  strrput(queue_t *q, mblk_t *bp)
1546 1563  {
1547 1564          struct stdata   *stp;
1548 1565          ulong_t         rput_opt;
1549 1566          strwakeup_t     wakeups;
1550 1567          strsigset_t     firstmsgsigs;   /* Signals if first message on queue */
1551 1568          strsigset_t     allmsgsigs;     /* Signals for all messages */
1552 1569          strsigset_t     signals;        /* Signals events to generate */
1553 1570          strpollset_t    pollwakeups;
1554 1571          mblk_t          *nextbp;
1555 1572          uchar_t         band = 0;
1556 1573          int             hipri_sig;
1557 1574  
1558 1575          stp = (struct stdata *)q->q_ptr;
1559 1576          /*
1560 1577           * Use rput_opt for optimized access to the SR_ flags except
1561 1578           * SR_POLLIN. That flag has to be checked under sd_lock since it
1562 1579           * is modified by strpoll().
1563 1580           */
1564 1581          rput_opt = stp->sd_rput_opt;
1565 1582  
1566 1583          ASSERT(qclaimed(q));
1567 1584          TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_ENTER,
1568 1585              "strrput called with message type:q %p bp %p", q, bp);
1569 1586  
1570 1587          /*
1571 1588           * Perform initial processing and pass to the parameterized functions.
1572 1589           */
1573 1590          ASSERT(bp->b_next == NULL);
1574 1591  
1575 1592          switch (bp->b_datap->db_type) {
1576 1593          case M_DATA:
1577 1594                  /*
1578 1595                   * sockfs is the only consumer of STREOF and when it is set,
1579 1596                   * it implies that the receiver is not interested in receiving
1580 1597                   * any more data, hence the mblk is freed to prevent unnecessary
1581 1598                   * message queueing at the stream head.
1582 1599                   */
1583 1600                  if (stp->sd_flag == STREOF) {
1584 1601                          freemsg(bp);
1585 1602                          return (0);
1586 1603                  }
1587 1604                  if ((rput_opt & SR_IGN_ZEROLEN) &&
1588 1605                      bp->b_rptr == bp->b_wptr && msgnodata(bp)) {
1589 1606                          /*
1590 1607                           * Ignore zero-length M_DATA messages. These might be
1591 1608                           * generated by some transports.
1592 1609                           * The zero-length M_DATA messages, even if they
1593 1610                           * are ignored, should effect the atmark tracking and
1594 1611                           * should wake up a thread sleeping in strwaitmark.
1595 1612                           */
1596 1613                          mutex_enter(&stp->sd_lock);
1597 1614                          if (bp->b_flag & MSGMARKNEXT) {
1598 1615                                  /*
1599 1616                                   * Record the position of the mark either
1600 1617                                   * in q_last or in STRATMARK.
1601 1618                                   */
1602 1619                                  if (q->q_last != NULL) {
1603 1620                                          q->q_last->b_flag &= ~MSGNOTMARKNEXT;
1604 1621                                          q->q_last->b_flag |= MSGMARKNEXT;
1605 1622                                  } else {
1606 1623                                          stp->sd_flag &= ~STRNOTATMARK;
1607 1624                                          stp->sd_flag |= STRATMARK;
1608 1625                                  }
1609 1626                          } else if (bp->b_flag & MSGNOTMARKNEXT) {
1610 1627                                  /*
1611 1628                                   * Record that this is not the position of
1612 1629                                   * the mark either in q_last or in
1613 1630                                   * STRNOTATMARK.
1614 1631                                   */
1615 1632                                  if (q->q_last != NULL) {
1616 1633                                          q->q_last->b_flag &= ~MSGMARKNEXT;
1617 1634                                          q->q_last->b_flag |= MSGNOTMARKNEXT;
1618 1635                                  } else {
1619 1636                                          stp->sd_flag &= ~STRATMARK;
1620 1637                                          stp->sd_flag |= STRNOTATMARK;
1621 1638                                  }
1622 1639                          }
1623 1640                          if (stp->sd_flag & RSLEEP) {
1624 1641                                  stp->sd_flag &= ~RSLEEP;
1625 1642                                  cv_broadcast(&q->q_wait);
1626 1643                          }
1627 1644                          mutex_exit(&stp->sd_lock);
1628 1645                          freemsg(bp);
1629 1646                          return (0);
1630 1647                  }
1631 1648                  wakeups = RSLEEP;
1632 1649                  if (bp->b_band == 0) {
1633 1650                          firstmsgsigs = S_INPUT | S_RDNORM;
1634 1651                          pollwakeups = POLLIN | POLLRDNORM;
1635 1652                  } else {
1636 1653                          firstmsgsigs = S_INPUT | S_RDBAND;
1637 1654                          pollwakeups = POLLIN | POLLRDBAND;
1638 1655                  }
1639 1656                  if (rput_opt & SR_SIGALLDATA)
1640 1657                          allmsgsigs = firstmsgsigs;
1641 1658                  else
1642 1659                          allmsgsigs = 0;
1643 1660  
1644 1661                  mutex_enter(&stp->sd_lock);
1645 1662                  if ((rput_opt & SR_CONSOL_DATA) &&
1646 1663                      (q->q_last != NULL) &&
1647 1664                      (bp->b_flag & (MSGMARK|MSGDELIM)) == 0) {
1648 1665                          /*
1649 1666                           * Consolidate an M_DATA message onto an M_DATA,
1650 1667                           * M_PROTO, or M_PCPROTO by merging it with q_last.
1651 1668                           * The consolidation does not take place if
1652 1669                           * the old message is marked with either of the
1653 1670                           * marks or the delim flag or if the new
1654 1671                           * message is marked with MSGMARK. The MSGMARK
1655 1672                           * check is needed to handle the odd semantics of
1656 1673                           * MSGMARK where essentially the whole message
1657 1674                           * is to be treated as marked.
1658 1675                           * Carry any MSGMARKNEXT  and MSGNOTMARKNEXT from the
1659 1676                           * new message to the front of the b_cont chain.
1660 1677                           */
1661 1678                          mblk_t *lbp = q->q_last;
1662 1679                          unsigned char db_type = lbp->b_datap->db_type;
1663 1680  
1664 1681                          if ((db_type == M_DATA || db_type == M_PROTO ||
1665 1682                              db_type == M_PCPROTO) &&
1666 1683                              !(lbp->b_flag & (MSGDELIM|MSGMARK|MSGMARKNEXT))) {
1667 1684                                  rmvq_noenab(q, lbp);
1668 1685                                  /*
1669 1686                                   * The first message in the b_cont list
1670 1687                                   * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
1671 1688                                   * We need to handle the case where we
1672 1689                                   * are appending:
1673 1690                                   *
1674 1691                                   * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
1675 1692                                   * 2) a MSGMARKNEXT to a plain message.
1676 1693                                   * 3) a MSGNOTMARKNEXT to a plain message
1677 1694                                   * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
1678 1695                                   *    message.
1679 1696                                   *
1680 1697                                   * Thus we never append a MSGMARKNEXT or
1681 1698                                   * MSGNOTMARKNEXT to a MSGMARKNEXT message.
1682 1699                                   */
1683 1700                                  if (bp->b_flag & MSGMARKNEXT) {
1684 1701                                          lbp->b_flag |= MSGMARKNEXT;
1685 1702                                          lbp->b_flag &= ~MSGNOTMARKNEXT;
1686 1703                                          bp->b_flag &= ~MSGMARKNEXT;
1687 1704                                  } else if (bp->b_flag & MSGNOTMARKNEXT) {
1688 1705                                          lbp->b_flag |= MSGNOTMARKNEXT;
1689 1706                                          bp->b_flag &= ~MSGNOTMARKNEXT;
1690 1707                                  }
1691 1708  
1692 1709                                  linkb(lbp, bp);
1693 1710                                  bp = lbp;
1694 1711                                  /*
1695 1712                                   * The new message logically isn't the first
1696 1713                                   * even though the q_first check below thinks
1697 1714                                   * it is. Clear the firstmsgsigs to make it
1698 1715                                   * not appear to be first.
1699 1716                                   */
1700 1717                                  firstmsgsigs = 0;
1701 1718                          }
1702 1719                  }
1703 1720                  break;
1704 1721  
1705 1722          case M_PASSFP:
1706 1723                  wakeups = RSLEEP;
1707 1724                  allmsgsigs = 0;
1708 1725                  if (bp->b_band == 0) {
1709 1726                          firstmsgsigs = S_INPUT | S_RDNORM;
1710 1727                          pollwakeups = POLLIN | POLLRDNORM;
1711 1728                  } else {
1712 1729                          firstmsgsigs = S_INPUT | S_RDBAND;
1713 1730                          pollwakeups = POLLIN | POLLRDBAND;
1714 1731                  }
1715 1732                  mutex_enter(&stp->sd_lock);
1716 1733                  break;
1717 1734  
1718 1735          case M_PROTO:
1719 1736          case M_PCPROTO:
1720 1737                  ASSERT(stp->sd_rprotofunc != NULL);
1721 1738                  bp = (stp->sd_rprotofunc)(stp->sd_vnode, bp,
1722 1739                      &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1723 1740  #define ALLSIG  (S_INPUT|S_HIPRI|S_OUTPUT|S_MSG|S_ERROR|S_HANGUP|S_RDNORM|\
1724 1741                  S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)
1725 1742  #define ALLPOLL (POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLWRNORM|POLLRDBAND|\
1726 1743                  POLLWRBAND)
1727 1744  
1728 1745                  ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1729 1746                  ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1730 1747                  ASSERT((allmsgsigs & ~ALLSIG) == 0);
1731 1748                  ASSERT((pollwakeups & ~ALLPOLL) == 0);
1732 1749  
1733 1750                  mutex_enter(&stp->sd_lock);
1734 1751                  break;
1735 1752  
1736 1753          default:
1737 1754                  ASSERT(stp->sd_rmiscfunc != NULL);
1738 1755                  bp = (stp->sd_rmiscfunc)(stp->sd_vnode, bp,
1739 1756                      &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1740 1757                  ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1741 1758                  ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1742 1759                  ASSERT((allmsgsigs & ~ALLSIG) == 0);
1743 1760                  ASSERT((pollwakeups & ~ALLPOLL) == 0);
1744 1761  #undef  ALLSIG
1745 1762  #undef  ALLPOLL
1746 1763                  mutex_enter(&stp->sd_lock);
1747 1764                  break;
1748 1765          }
1749 1766          ASSERT(MUTEX_HELD(&stp->sd_lock));
1750 1767  
1751 1768          /* By default generate superset of signals */
1752 1769          signals = (firstmsgsigs | allmsgsigs);
1753 1770  
1754 1771          /*
1755 1772           * The  proto and misc functions can return multiple messages
1756 1773           * as a b_next chain. Such messages are processed separately.
1757 1774           */
1758 1775  one_more:
1759 1776          hipri_sig = 0;
1760 1777          if (bp == NULL) {
1761 1778                  nextbp = NULL;
1762 1779          } else {
1763 1780                  nextbp = bp->b_next;
1764 1781                  bp->b_next = NULL;
1765 1782  
1766 1783                  switch (bp->b_datap->db_type) {
1767 1784                  case M_PCPROTO:
1768 1785                          /*
1769 1786                           * Only one priority protocol message is allowed at the
1770 1787                           * stream head at a time.
1771 1788                           */
1772 1789                          if (stp->sd_flag & STRPRI) {
1773 1790                                  TRACE_0(TR_FAC_STREAMS_FR, TR_STRRPUT_PROTERR,
1774 1791                                      "M_PCPROTO already at head");
1775 1792                                  freemsg(bp);
1776 1793                                  mutex_exit(&stp->sd_lock);
1777 1794                                  goto done;
1778 1795                          }
1779 1796                          stp->sd_flag |= STRPRI;
1780 1797                          hipri_sig = 1;
1781 1798                          /* FALLTHRU */
1782 1799                  case M_DATA:
1783 1800                  case M_PROTO:
1784 1801                  case M_PASSFP:
1785 1802                          band = bp->b_band;
1786 1803                          /*
1787 1804                           * Marking doesn't work well when messages
1788 1805                           * are marked in more than one band.  We only
1789 1806                           * remember the last message received, even if
1790 1807                           * it is placed on the queue ahead of other
1791 1808                           * marked messages.
1792 1809                           */
1793 1810                          if (bp->b_flag & MSGMARK)
1794 1811                                  stp->sd_mark = bp;
1795 1812                          (void) putq(q, bp);
1796 1813  
1797 1814                          /*
1798 1815                           * If message is a PCPROTO message, always use
1799 1816                           * firstmsgsigs to determine if a signal should be
1800 1817                           * sent as strrput is the only place to send
1801 1818                           * signals for PCPROTO. Other messages are based on
1802 1819                           * the STRGETINPROG flag. The flag determines if
1803 1820                           * strrput or (k)strgetmsg will be responsible for
1804 1821                           * sending the signals, in the firstmsgsigs case.
1805 1822                           */
1806 1823                          if ((hipri_sig == 1) ||
1807 1824                              (((stp->sd_flag & STRGETINPROG) == 0) &&
1808 1825                              (q->q_first == bp)))
1809 1826                                  signals = (firstmsgsigs | allmsgsigs);
1810 1827                          else
1811 1828                                  signals = allmsgsigs;
1812 1829                          break;
1813 1830  
1814 1831                  default:
1815 1832                          mutex_exit(&stp->sd_lock);
1816 1833                          (void) strrput_nondata(q, bp);
1817 1834                          mutex_enter(&stp->sd_lock);
1818 1835                          break;
1819 1836                  }
1820 1837          }
1821 1838          ASSERT(MUTEX_HELD(&stp->sd_lock));
1822 1839          /*
1823 1840           * Wake sleeping read/getmsg and cancel deferred wakeup
1824 1841           */
1825 1842          if (wakeups & RSLEEP)
1826 1843                  stp->sd_wakeq &= ~RSLEEP;
1827 1844  
1828 1845          wakeups &= stp->sd_flag;
1829 1846          if (wakeups & RSLEEP) {
1830 1847                  stp->sd_flag &= ~RSLEEP;
1831 1848                  cv_broadcast(&q->q_wait);
1832 1849          }
1833 1850          if (wakeups & WSLEEP) {
1834 1851                  stp->sd_flag &= ~WSLEEP;
1835 1852                  cv_broadcast(&_WR(q)->q_wait);
1836 1853          }
1837 1854  
1838 1855          if (pollwakeups != 0) {
1839 1856                  if (pollwakeups == (POLLIN | POLLRDNORM)) {
1840 1857                          /*
1841 1858                           * Can't use rput_opt since it was not
1842 1859                           * read when sd_lock was held and SR_POLLIN is changed
1843 1860                           * by strpoll() under sd_lock.
1844 1861                           */
1845 1862                          if (!(stp->sd_rput_opt & SR_POLLIN))
1846 1863                                  goto no_pollwake;
1847 1864                          stp->sd_rput_opt &= ~SR_POLLIN;
1848 1865                  }
1849 1866                  mutex_exit(&stp->sd_lock);
1850 1867                  pollwakeup(&stp->sd_pollist, pollwakeups);
1851 1868                  mutex_enter(&stp->sd_lock);
1852 1869          }
1853 1870  no_pollwake:
1854 1871  
1855 1872          /*
1856 1873           * strsendsig can handle multiple signals with a
1857 1874           * single call.
1858 1875           */
1859 1876          if (stp->sd_sigflags & signals)
1860 1877                  strsendsig(stp->sd_siglist, signals, band, 0);
1861 1878          mutex_exit(&stp->sd_lock);
1862 1879  
1863 1880  
1864 1881  done:
1865 1882          if (nextbp == NULL)
1866 1883                  return (0);
1867 1884  
1868 1885          /*
1869 1886           * Any signals were handled the first time.
1870 1887           * Wakeups and pollwakeups are redone to avoid any race
1871 1888           * conditions - all the messages are not queued until the
1872 1889           * last message has been processed by strrput.
1873 1890           */
1874 1891          bp = nextbp;
1875 1892          signals = firstmsgsigs = allmsgsigs = 0;
1876 1893          mutex_enter(&stp->sd_lock);
1877 1894          goto one_more;
1878 1895  }
1879 1896  
1880 1897  static void
1881 1898  log_dupioc(queue_t *rq, mblk_t *bp)
1882 1899  {
1883 1900          queue_t *wq, *qp;
1884 1901          char *modnames, *mnp, *dname;
1885 1902          size_t maxmodstr;
1886 1903          boolean_t islast;
1887 1904  
1888 1905          /*
1889 1906           * Allocate a buffer large enough to hold the names of nstrpush modules
1890 1907           * and one driver, with spaces between and NUL terminator.  If we can't
1891 1908           * get memory, then we'll just log the driver name.
1892 1909           */
1893 1910          maxmodstr = nstrpush * (FMNAMESZ + 1);
1894 1911          mnp = modnames = kmem_alloc(maxmodstr, KM_NOSLEEP);
1895 1912  
1896 1913          /* march down write side to print log message down to the driver */
1897 1914          wq = WR(rq);
1898 1915  
1899 1916          /* make sure q_next doesn't shift around while we're grabbing data */
1900 1917          claimstr(wq);
1901 1918          qp = wq->q_next;
1902 1919          do {
1903 1920                  dname = Q2NAME(qp);
1904 1921                  islast = !SAMESTR(qp) || qp->q_next == NULL;
1905 1922                  if (modnames == NULL) {
1906 1923                          /*
1907 1924                           * If we don't have memory, then get the driver name in
1908 1925                           * the log where we can see it.  Note that memory
1909 1926                           * pressure is a possible cause of these sorts of bugs.
1910 1927                           */
1911 1928                          if (islast) {
1912 1929                                  modnames = dname;
1913 1930                                  maxmodstr = 0;
1914 1931                          }
1915 1932                  } else {
1916 1933                          mnp += snprintf(mnp, FMNAMESZ + 1, "%s", dname);
1917 1934                          if (!islast)
1918 1935                                  *mnp++ = ' ';
1919 1936                  }
1920 1937                  qp = qp->q_next;
1921 1938          } while (!islast);
1922 1939          releasestr(wq);
1923 1940          /* Cannot happen unless stream head is corrupt. */
1924 1941          ASSERT(modnames != NULL);
1925 1942          (void) strlog(rq->q_qinfo->qi_minfo->mi_idnum, 0, 1,
1926 1943              SL_CONSOLE|SL_TRACE|SL_ERROR,
1927 1944              "Warning: stream %p received duplicate %X M_IOC%s; module list: %s",
1928 1945              rq->q_ptr, ((struct iocblk *)bp->b_rptr)->ioc_cmd,
1929 1946              (DB_TYPE(bp) == M_IOCACK ? "ACK" : "NAK"), modnames);
1930 1947          if (maxmodstr != 0)
1931 1948                  kmem_free(modnames, maxmodstr);
1932 1949  }
1933 1950  
1934 1951  int
1935 1952  strrput_nondata(queue_t *q, mblk_t *bp)
1936 1953  {
1937 1954          struct stdata *stp;
1938 1955          struct iocblk *iocbp;
1939 1956          struct stroptions *sop;
1940 1957          struct copyreq *reqp;
1941 1958          struct copyresp *resp;
1942 1959          unsigned char bpri;
1943 1960          unsigned char  flushed_already = 0;
1944 1961  
1945 1962          stp = (struct stdata *)q->q_ptr;
1946 1963  
1947 1964          ASSERT(!(stp->sd_flag & STPLEX));
1948 1965          ASSERT(qclaimed(q));
1949 1966  
1950 1967          switch (bp->b_datap->db_type) {
1951 1968          case M_ERROR:
1952 1969                  /*
1953 1970                   * An error has occurred downstream, the errno is in the first
1954 1971                   * bytes of the message.
1955 1972                   */
1956 1973                  if ((bp->b_wptr - bp->b_rptr) == 2) {   /* New flavor */
1957 1974                          unsigned char rw = 0;
1958 1975  
1959 1976                          mutex_enter(&stp->sd_lock);
1960 1977                          if (*bp->b_rptr != NOERROR) {   /* read error */
1961 1978                                  if (*bp->b_rptr != 0) {
1962 1979                                          if (stp->sd_flag & STRDERR)
1963 1980                                                  flushed_already |= FLUSHR;
1964 1981                                          stp->sd_flag |= STRDERR;
1965 1982                                          rw |= FLUSHR;
1966 1983                                  } else {
1967 1984                                          stp->sd_flag &= ~STRDERR;
1968 1985                                  }
1969 1986                                  stp->sd_rerror = *bp->b_rptr;
1970 1987                          }
1971 1988                          bp->b_rptr++;
1972 1989                          if (*bp->b_rptr != NOERROR) {   /* write error */
1973 1990                                  if (*bp->b_rptr != 0) {
1974 1991                                          if (stp->sd_flag & STWRERR)
1975 1992                                                  flushed_already |= FLUSHW;
1976 1993                                          stp->sd_flag |= STWRERR;
1977 1994                                          rw |= FLUSHW;
1978 1995                                  } else {
1979 1996                                          stp->sd_flag &= ~STWRERR;
1980 1997                                  }
1981 1998                                  stp->sd_werror = *bp->b_rptr;
1982 1999                          }
1983 2000                          if (rw) {
1984 2001                                  TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_WAKE,
1985 2002                                      "strrput cv_broadcast:q %p, bp %p",
1986 2003                                      q, bp);
1987 2004                                  cv_broadcast(&q->q_wait); /* readers */
1988 2005                                  cv_broadcast(&_WR(q)->q_wait); /* writers */
1989 2006                                  cv_broadcast(&stp->sd_monitor); /* ioctllers */
1990 2007  
1991 2008                                  mutex_exit(&stp->sd_lock);
1992 2009                                  pollwakeup(&stp->sd_pollist, POLLERR);
1993 2010                                  mutex_enter(&stp->sd_lock);
1994 2011  
1995 2012                                  if (stp->sd_sigflags & S_ERROR)
1996 2013                                          strsendsig(stp->sd_siglist, S_ERROR, 0,
1997 2014                                              ((rw & FLUSHR) ? stp->sd_rerror :
1998 2015                                              stp->sd_werror));
1999 2016                                  mutex_exit(&stp->sd_lock);
2000 2017                                  /*
2001 2018                                   * Send the M_FLUSH only
2002 2019                                   * for the first M_ERROR
2003 2020                                   * message on the stream
2004 2021                                   */
2005 2022                                  if (flushed_already == rw) {
2006 2023                                          freemsg(bp);
2007 2024                                          return (0);
2008 2025                                  }
2009 2026  
2010 2027                                  bp->b_datap->db_type = M_FLUSH;
2011 2028                                  *bp->b_rptr = rw;
2012 2029                                  bp->b_wptr = bp->b_rptr + 1;
2013 2030                                  /*
2014 2031                                   * Protect against the driver
2015 2032                                   * passing up messages after
2016 2033                                   * it has done a qprocsoff
2017 2034                                   */
2018 2035                                  if (_OTHERQ(q)->q_next == NULL)
2019 2036                                          freemsg(bp);
2020 2037                                  else
2021 2038                                          qreply(q, bp);
2022 2039                                  return (0);
2023 2040                          } else
2024 2041                                  mutex_exit(&stp->sd_lock);
2025 2042                  } else if (*bp->b_rptr != 0) {          /* Old flavor */
2026 2043                                  if (stp->sd_flag & (STRDERR|STWRERR))
2027 2044                                          flushed_already = FLUSHRW;
2028 2045                                  mutex_enter(&stp->sd_lock);
2029 2046                                  stp->sd_flag |= (STRDERR|STWRERR);
2030 2047                                  stp->sd_rerror = *bp->b_rptr;
2031 2048                                  stp->sd_werror = *bp->b_rptr;
2032 2049                                  TRACE_2(TR_FAC_STREAMS_FR,
2033 2050                                      TR_STRRPUT_WAKE2,
2034 2051                                      "strrput wakeup #2:q %p, bp %p", q, bp);
2035 2052                                  cv_broadcast(&q->q_wait); /* the readers */
2036 2053                                  cv_broadcast(&_WR(q)->q_wait); /* the writers */
2037 2054                                  cv_broadcast(&stp->sd_monitor); /* ioctllers */
2038 2055  
2039 2056                                  mutex_exit(&stp->sd_lock);
2040 2057                                  pollwakeup(&stp->sd_pollist, POLLERR);
2041 2058                                  mutex_enter(&stp->sd_lock);
2042 2059  
2043 2060                                  if (stp->sd_sigflags & S_ERROR)
2044 2061                                          strsendsig(stp->sd_siglist, S_ERROR, 0,
2045 2062                                              (stp->sd_werror ? stp->sd_werror :
2046 2063                                              stp->sd_rerror));
2047 2064                                  mutex_exit(&stp->sd_lock);
2048 2065  
2049 2066                                  /*
2050 2067                                   * Send the M_FLUSH only
2051 2068                                   * for the first M_ERROR
2052 2069                                   * message on the stream
2053 2070                                   */
2054 2071                                  if (flushed_already != FLUSHRW) {
2055 2072                                          bp->b_datap->db_type = M_FLUSH;
2056 2073                                          *bp->b_rptr = FLUSHRW;
2057 2074                                          /*
2058 2075                                           * Protect against the driver passing up
2059 2076                                           * messages after it has done a
2060 2077                                           * qprocsoff.
2061 2078                                           */
2062 2079                                  if (_OTHERQ(q)->q_next == NULL)
2063 2080                                          freemsg(bp);
2064 2081                                  else
2065 2082                                          qreply(q, bp);
2066 2083                                  return (0);
2067 2084                                  }
2068 2085                  }
2069 2086                  freemsg(bp);
2070 2087                  return (0);
2071 2088  
2072 2089          case M_HANGUP:
2073 2090  
2074 2091                  freemsg(bp);
2075 2092                  mutex_enter(&stp->sd_lock);
2076 2093                  stp->sd_werror = ENXIO;
2077 2094                  stp->sd_flag |= STRHUP;
2078 2095                  stp->sd_flag &= ~(WSLEEP|RSLEEP);
2079 2096  
2080 2097                  /*
2081 2098                   * send signal if controlling tty
2082 2099                   */
2083 2100  
2084 2101                  if (stp->sd_sidp) {
2085 2102                          prsignal(stp->sd_sidp, SIGHUP);
2086 2103                          if (stp->sd_sidp != stp->sd_pgidp)
2087 2104                                  pgsignal(stp->sd_pgidp, SIGTSTP);
2088 2105                  }
2089 2106  
2090 2107                  /*
2091 2108                   * wake up read, write, and exception pollers and
2092 2109                   * reset wakeup mechanism.
2093 2110                   */
2094 2111                  cv_broadcast(&q->q_wait);       /* the readers */
2095 2112                  cv_broadcast(&_WR(q)->q_wait);  /* the writers */
2096 2113                  cv_broadcast(&stp->sd_monitor); /* the ioctllers */
2097 2114                  strhup(stp);
2098 2115                  mutex_exit(&stp->sd_lock);
2099 2116                  return (0);
2100 2117  
2101 2118          case M_UNHANGUP:
2102 2119                  freemsg(bp);
2103 2120                  mutex_enter(&stp->sd_lock);
2104 2121                  stp->sd_werror = 0;
2105 2122                  stp->sd_flag &= ~STRHUP;
2106 2123                  mutex_exit(&stp->sd_lock);
2107 2124                  return (0);
2108 2125  
2109 2126          case M_SIG:
2110 2127                  /*
2111 2128                   * Someone downstream wants to post a signal.  The
2112 2129                   * signal to post is contained in the first byte of the
2113 2130                   * message.  If the message would go on the front of
2114 2131                   * the queue, send a signal to the process group
2115 2132                   * (if not SIGPOLL) or to the siglist processes
2116 2133                   * (SIGPOLL).  If something is already on the queue,
2117 2134                   * OR if we are delivering a delayed suspend (*sigh*
2118 2135                   * another "tty" hack) and there's no one sleeping already,
2119 2136                   * just enqueue the message.
2120 2137                   */
2121 2138                  mutex_enter(&stp->sd_lock);
2122 2139                  if (q->q_first || (*bp->b_rptr == SIGTSTP &&
2123 2140                      !(stp->sd_flag & RSLEEP))) {
2124 2141                          (void) putq(q, bp);
2125 2142                          mutex_exit(&stp->sd_lock);
2126 2143                          return (0);
2127 2144                  }
2128 2145                  mutex_exit(&stp->sd_lock);
2129 2146                  /* FALLTHRU */
2130 2147  
2131 2148          case M_PCSIG:
2132 2149                  /*
2133 2150                   * Don't enqueue, just post the signal.
2134 2151                   */
2135 2152                  strsignal(stp, *bp->b_rptr, 0L);
2136 2153                  freemsg(bp);
2137 2154                  return (0);
2138 2155  
2139 2156          case M_CMD:
2140 2157                  if (MBLKL(bp) != sizeof (cmdblk_t)) {
2141 2158                          freemsg(bp);
2142 2159                          return (0);
2143 2160                  }
2144 2161  
2145 2162                  mutex_enter(&stp->sd_lock);
2146 2163                  if (stp->sd_flag & STRCMDWAIT) {
2147 2164                          ASSERT(stp->sd_cmdblk == NULL);
2148 2165                          stp->sd_cmdblk = bp;
2149 2166                          cv_broadcast(&stp->sd_monitor);
2150 2167                          mutex_exit(&stp->sd_lock);
2151 2168                  } else {
2152 2169                          mutex_exit(&stp->sd_lock);
2153 2170                          freemsg(bp);
2154 2171                  }
2155 2172                  return (0);
2156 2173  
2157 2174          case M_FLUSH:
2158 2175                  /*
2159 2176                   * Flush queues.  The indication of which queues to flush
2160 2177                   * is in the first byte of the message.  If the read queue
2161 2178                   * is specified, then flush it.  If FLUSHBAND is set, just
2162 2179                   * flush the band specified by the second byte of the message.
2163 2180                   *
2164 2181                   * If a module has issued a M_SETOPT to not flush hi
2165 2182                   * priority messages off of the stream head, then pass this
2166 2183                   * flag into the flushq code to preserve such messages.
2167 2184                   */
2168 2185  
2169 2186                  if (*bp->b_rptr & FLUSHR) {
2170 2187                          mutex_enter(&stp->sd_lock);
2171 2188                          if (*bp->b_rptr & FLUSHBAND) {
2172 2189                                  ASSERT((bp->b_wptr - bp->b_rptr) >= 2);
2173 2190                                  flushband(q, *(bp->b_rptr + 1), FLUSHALL);
2174 2191                          } else
2175 2192                                  flushq_common(q, FLUSHALL,
2176 2193                                      stp->sd_read_opt & RFLUSHPCPROT);
2177 2194                          if ((q->q_first == NULL) ||
2178 2195                              (q->q_first->b_datap->db_type < QPCTL))
2179 2196                                  stp->sd_flag &= ~STRPRI;
2180 2197                          else {
2181 2198                                  ASSERT(stp->sd_flag & STRPRI);
2182 2199                          }
2183 2200                          mutex_exit(&stp->sd_lock);
2184 2201                  }
2185 2202                  if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
2186 2203                          *bp->b_rptr &= ~FLUSHR;
2187 2204                          bp->b_flag |= MSGNOLOOP;
2188 2205                          /*
2189 2206                           * Protect against the driver passing up
2190 2207                           * messages after it has done a qprocsoff.
2191 2208                           */
2192 2209                          if (_OTHERQ(q)->q_next == NULL)
2193 2210                                  freemsg(bp);
2194 2211                          else
2195 2212                                  qreply(q, bp);
2196 2213                          return (0);
2197 2214                  }
2198 2215                  freemsg(bp);
2199 2216                  return (0);
2200 2217  
2201 2218          case M_IOCACK:
2202 2219          case M_IOCNAK:
2203 2220                  iocbp = (struct iocblk *)bp->b_rptr;
2204 2221                  /*
2205 2222                   * If not waiting for ACK or NAK then just free msg.
2206 2223                   * If incorrect id sequence number then just free msg.
2207 2224                   * If already have ACK or NAK for user then this is a
2208 2225                   *    duplicate, display a warning and free the msg.
2209 2226                   */
2210 2227                  mutex_enter(&stp->sd_lock);
2211 2228                  if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2212 2229                      (stp->sd_iocid != iocbp->ioc_id)) {
2213 2230                          /*
2214 2231                           * If the ACK/NAK is a dup, display a message
2215 2232                           * Dup is when sd_iocid == ioc_id, and
2216 2233                           * sd_iocblk == <valid ptr> or -1 (the former
2217 2234                           * is when an ioctl has been put on the stream
2218 2235                           * head, but has not yet been consumed, the
2219 2236                           * later is when it has been consumed).
2220 2237                           */
2221 2238                          if ((stp->sd_iocid == iocbp->ioc_id) &&
2222 2239                              (stp->sd_iocblk != NULL)) {
2223 2240                                  log_dupioc(q, bp);
2224 2241                          }
2225 2242                          freemsg(bp);
2226 2243                          mutex_exit(&stp->sd_lock);
2227 2244                          return (0);
2228 2245                  }
2229 2246  
2230 2247                  /*
2231 2248                   * Assign ACK or NAK to user and wake up.
2232 2249                   */
2233 2250                  stp->sd_iocblk = bp;
2234 2251                  cv_broadcast(&stp->sd_monitor);
2235 2252                  mutex_exit(&stp->sd_lock);
2236 2253                  return (0);
2237 2254  
2238 2255          case M_COPYIN:
2239 2256          case M_COPYOUT:
2240 2257                  reqp = (struct copyreq *)bp->b_rptr;
2241 2258  
2242 2259                  /*
2243 2260                   * If not waiting for ACK or NAK then just fail request.
2244 2261                   * If already have ACK, NAK, or copy request, then just
2245 2262                   * fail request.
2246 2263                   * If incorrect id sequence number then just fail request.
2247 2264                   */
2248 2265                  mutex_enter(&stp->sd_lock);
2249 2266                  if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2250 2267                      (stp->sd_iocid != reqp->cq_id)) {
2251 2268                          if (bp->b_cont) {
2252 2269                                  freemsg(bp->b_cont);
2253 2270                                  bp->b_cont = NULL;
2254 2271                          }
2255 2272                          bp->b_datap->db_type = M_IOCDATA;
2256 2273                          bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
2257 2274                          resp = (struct copyresp *)bp->b_rptr;
2258 2275                          resp->cp_rval = (caddr_t)1;     /* failure */
2259 2276                          mutex_exit(&stp->sd_lock);
2260 2277                          putnext(stp->sd_wrq, bp);
2261 2278                          return (0);
2262 2279                  }
2263 2280  
2264 2281                  /*
2265 2282                   * Assign copy request to user and wake up.
2266 2283                   */
2267 2284                  stp->sd_iocblk = bp;
2268 2285                  cv_broadcast(&stp->sd_monitor);
2269 2286                  mutex_exit(&stp->sd_lock);
2270 2287                  return (0);
2271 2288  
2272 2289          case M_SETOPTS:
2273 2290                  /*
2274 2291                   * Set stream head options (read option, write offset,
2275 2292                   * min/max packet size, and/or high/low water marks for
2276 2293                   * the read side only).
2277 2294                   */
2278 2295  
2279 2296                  bpri = 0;
2280 2297                  sop = (struct stroptions *)bp->b_rptr;
2281 2298                  mutex_enter(&stp->sd_lock);
2282 2299                  if (sop->so_flags & SO_READOPT) {
2283 2300                          switch (sop->so_readopt & RMODEMASK) {
2284 2301                          case RNORM:
2285 2302                                  stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
2286 2303                                  break;
2287 2304  
2288 2305                          case RMSGD:
2289 2306                                  stp->sd_read_opt =
2290 2307                                      ((stp->sd_read_opt & ~RD_MSGNODIS) |
2291 2308                                      RD_MSGDIS);
2292 2309                                  break;
2293 2310  
2294 2311                          case RMSGN:
2295 2312                                  stp->sd_read_opt =
2296 2313                                      ((stp->sd_read_opt & ~RD_MSGDIS) |
2297 2314                                      RD_MSGNODIS);
2298 2315                                  break;
2299 2316                          }
2300 2317                          switch (sop->so_readopt & RPROTMASK) {
2301 2318                          case RPROTNORM:
2302 2319                                  stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
2303 2320                                  break;
2304 2321  
2305 2322                          case RPROTDAT:
2306 2323                                  stp->sd_read_opt =
2307 2324                                      ((stp->sd_read_opt & ~RD_PROTDIS) |
2308 2325                                      RD_PROTDAT);
2309 2326                                  break;
2310 2327  
2311 2328                          case RPROTDIS:
2312 2329                                  stp->sd_read_opt =
2313 2330                                      ((stp->sd_read_opt & ~RD_PROTDAT) |
2314 2331                                      RD_PROTDIS);
2315 2332                                  break;
2316 2333                          }
2317 2334                          switch (sop->so_readopt & RFLUSHMASK) {
2318 2335                          case RFLUSHPCPROT:
2319 2336                                  /*
2320 2337                                   * This sets the stream head to NOT flush
2321 2338                                   * M_PCPROTO messages.
2322 2339                                   */
2323 2340                                  stp->sd_read_opt |= RFLUSHPCPROT;
2324 2341                                  break;
2325 2342                          }
2326 2343                  }
2327 2344                  if (sop->so_flags & SO_ERROPT) {
2328 2345                          switch (sop->so_erropt & RERRMASK) {
2329 2346                          case RERRNORM:
2330 2347                                  stp->sd_flag &= ~STRDERRNONPERSIST;
2331 2348                                  break;
2332 2349                          case RERRNONPERSIST:
2333 2350                                  stp->sd_flag |= STRDERRNONPERSIST;
2334 2351                                  break;
2335 2352                          }
2336 2353                          switch (sop->so_erropt & WERRMASK) {
2337 2354                          case WERRNORM:
2338 2355                                  stp->sd_flag &= ~STWRERRNONPERSIST;
2339 2356                                  break;
2340 2357                          case WERRNONPERSIST:
2341 2358                                  stp->sd_flag |= STWRERRNONPERSIST;
2342 2359                                  break;
2343 2360                          }
2344 2361                  }
2345 2362                  if (sop->so_flags & SO_COPYOPT) {
2346 2363                          if (sop->so_copyopt & ZCVMSAFE) {
2347 2364                                  stp->sd_copyflag |= STZCVMSAFE;
2348 2365                                  stp->sd_copyflag &= ~STZCVMUNSAFE;
2349 2366                          } else if (sop->so_copyopt & ZCVMUNSAFE) {
2350 2367                                  stp->sd_copyflag |= STZCVMUNSAFE;
2351 2368                                  stp->sd_copyflag &= ~STZCVMSAFE;
2352 2369                          }
2353 2370  
2354 2371                          if (sop->so_copyopt & COPYCACHED) {
2355 2372                                  stp->sd_copyflag |= STRCOPYCACHED;
2356 2373                          }
2357 2374                  }
2358 2375                  if (sop->so_flags & SO_WROFF)
2359 2376                          stp->sd_wroff = sop->so_wroff;
2360 2377                  if (sop->so_flags & SO_TAIL)
2361 2378                          stp->sd_tail = sop->so_tail;
2362 2379                  if (sop->so_flags & SO_MINPSZ)
2363 2380                          q->q_minpsz = sop->so_minpsz;
2364 2381                  if (sop->so_flags & SO_MAXPSZ)
2365 2382                          q->q_maxpsz = sop->so_maxpsz;
2366 2383                  if (sop->so_flags & SO_MAXBLK)
2367 2384                          stp->sd_maxblk = sop->so_maxblk;
2368 2385                  if (sop->so_flags & SO_HIWAT) {
2369 2386                          if (sop->so_flags & SO_BAND) {
2370 2387                                  if (strqset(q, QHIWAT,
2371 2388                                      sop->so_band, sop->so_hiwat)) {
2372 2389                                          cmn_err(CE_WARN, "strrput: could not "
2373 2390                                              "allocate qband\n");
2374 2391                                  } else {
2375 2392                                          bpri = sop->so_band;
2376 2393                                  }
2377 2394                          } else {
2378 2395                                  q->q_hiwat = sop->so_hiwat;
2379 2396                          }
2380 2397                  }
2381 2398                  if (sop->so_flags & SO_LOWAT) {
2382 2399                          if (sop->so_flags & SO_BAND) {
2383 2400                                  if (strqset(q, QLOWAT,
2384 2401                                      sop->so_band, sop->so_lowat)) {
2385 2402                                          cmn_err(CE_WARN, "strrput: could not "
2386 2403                                              "allocate qband\n");
2387 2404                                  } else {
2388 2405                                          bpri = sop->so_band;
2389 2406                                  }
2390 2407                          } else {
2391 2408                                  q->q_lowat = sop->so_lowat;
2392 2409                          }
2393 2410                  }
2394 2411                  if (sop->so_flags & SO_MREADON)
2395 2412                          stp->sd_flag |= SNDMREAD;
2396 2413                  if (sop->so_flags & SO_MREADOFF)
2397 2414                          stp->sd_flag &= ~SNDMREAD;
2398 2415                  if (sop->so_flags & SO_NDELON)
2399 2416                          stp->sd_flag |= OLDNDELAY;
2400 2417                  if (sop->so_flags & SO_NDELOFF)
2401 2418                          stp->sd_flag &= ~OLDNDELAY;
2402 2419                  if (sop->so_flags & SO_ISTTY)
2403 2420                          stp->sd_flag |= STRISTTY;
2404 2421                  if (sop->so_flags & SO_ISNTTY)
2405 2422                          stp->sd_flag &= ~STRISTTY;
2406 2423                  if (sop->so_flags & SO_TOSTOP)
2407 2424                          stp->sd_flag |= STRTOSTOP;
2408 2425                  if (sop->so_flags & SO_TONSTOP)
2409 2426                          stp->sd_flag &= ~STRTOSTOP;
2410 2427                  if (sop->so_flags & SO_DELIM)
2411 2428                          stp->sd_flag |= STRDELIM;
2412 2429                  if (sop->so_flags & SO_NODELIM)
2413 2430                          stp->sd_flag &= ~STRDELIM;
2414 2431  
2415 2432                  mutex_exit(&stp->sd_lock);
2416 2433                  freemsg(bp);
2417 2434  
2418 2435                  /* Check backenable in case the water marks changed */
2419 2436                  qbackenable(q, bpri);
2420 2437                  return (0);
2421 2438  
2422 2439          /*
2423 2440           * The following set of cases deal with situations where two stream
2424 2441           * heads are connected to each other (twisted streams).  These messages
2425 2442           * have no meaning at the stream head.
2426 2443           */
2427 2444          case M_BREAK:
2428 2445          case M_CTL:
2429 2446          case M_DELAY:
2430 2447          case M_START:
2431 2448          case M_STOP:
2432 2449          case M_IOCDATA:
2433 2450          case M_STARTI:
2434 2451          case M_STOPI:
2435 2452                  freemsg(bp);
2436 2453                  return (0);
2437 2454  
2438 2455          case M_IOCTL:
2439 2456                  /*
2440 2457                   * Always NAK this condition
2441 2458                   * (makes no sense)
2442 2459                   * If there is one or more threads in the read side
2443 2460                   * rwnext we have to defer the nacking until that thread
2444 2461                   * returns (in strget).
2445 2462                   */
2446 2463                  mutex_enter(&stp->sd_lock);
2447 2464                  if (stp->sd_struiodnak != 0) {
2448 2465                          /*
2449 2466                           * Defer NAK to the streamhead. Queue at the end
2450 2467                           * the list.
2451 2468                           */
2452 2469                          mblk_t *mp = stp->sd_struionak;
2453 2470  
2454 2471                          while (mp && mp->b_next)
2455 2472                                  mp = mp->b_next;
2456 2473                          if (mp)
2457 2474                                  mp->b_next = bp;
2458 2475                          else
2459 2476                                  stp->sd_struionak = bp;
2460 2477                          bp->b_next = NULL;
2461 2478                          mutex_exit(&stp->sd_lock);
2462 2479                          return (0);
2463 2480                  }
2464 2481                  mutex_exit(&stp->sd_lock);
2465 2482  
2466 2483                  bp->b_datap->db_type = M_IOCNAK;
2467 2484                  /*
2468 2485                   * Protect against the driver passing up
2469 2486                   * messages after it has done a qprocsoff.
2470 2487                   */
2471 2488                  if (_OTHERQ(q)->q_next == NULL)
2472 2489                          freemsg(bp);
2473 2490                  else
2474 2491                          qreply(q, bp);
2475 2492                  return (0);
2476 2493  
2477 2494          default:
2478 2495  #ifdef DEBUG
2479 2496                  cmn_err(CE_WARN,
2480 2497                      "bad message type %x received at stream head\n",
2481 2498                      bp->b_datap->db_type);
2482 2499  #endif
2483 2500                  freemsg(bp);
2484 2501                  return (0);
2485 2502          }
2486 2503  
2487 2504          /* NOTREACHED */
2488 2505  }
2489 2506  
2490 2507  /*
2491 2508   * Check if the stream pointed to by `stp' can be written to, and return an
2492 2509   * error code if not.  If `eiohup' is set, then return EIO if STRHUP is set.
2493 2510   * If `sigpipeok' is set and the SW_SIGPIPE option is enabled on the stream,
2494 2511   * then always return EPIPE and send a SIGPIPE to the invoking thread.
2495 2512   */
2496 2513  static int
2497 2514  strwriteable(struct stdata *stp, boolean_t eiohup, boolean_t sigpipeok)
2498 2515  {
2499 2516          int error;
2500 2517  
2501 2518          ASSERT(MUTEX_HELD(&stp->sd_lock));
2502 2519  
2503 2520          /*
2504 2521           * For modem support, POSIX states that on writes, EIO should
2505 2522           * be returned if the stream has been hung up.
2506 2523           */
2507 2524          if (eiohup && (stp->sd_flag & (STPLEX|STRHUP)) == STRHUP)
2508 2525                  error = EIO;
2509 2526          else
2510 2527                  error = strgeterr(stp, STRHUP|STPLEX|STWRERR, 0);
2511 2528  
2512 2529          if (error != 0) {
2513 2530                  if (!(stp->sd_flag & STPLEX) &&
2514 2531                      (stp->sd_wput_opt & SW_SIGPIPE) && sigpipeok) {
2515 2532                          tsignal(curthread, SIGPIPE);
2516 2533                          error = EPIPE;
2517 2534                  }
2518 2535          }
2519 2536  
2520 2537          return (error);
2521 2538  }
2522 2539  
2523 2540  /*
2524 2541   * Copyin and send data down a stream.
2525 2542   * The caller will allocate and copyin any control part that precedes the
2526 2543   * message and pass that in as mctl.
2527 2544   *
2528 2545   * Caller should *not* hold sd_lock.
2529 2546   * When EWOULDBLOCK is returned the caller has to redo the canputnext
2530 2547   * under sd_lock in order to avoid missing a backenabling wakeup.
2531 2548   *
2532 2549   * Use iosize = -1 to not send any M_DATA. iosize = 0 sends zero-length M_DATA.
2533 2550   *
2534 2551   * Set MSG_IGNFLOW in flags to ignore flow control for hipri messages.
2535 2552   * For sync streams we can only ignore flow control by reverting to using
2536 2553   * putnext.
  
    | 
      ↓ open down ↓ | 
    1474 lines elided | 
    
      ↑ open up ↑ | 
  
2537 2554   *
2538 2555   * If sd_maxblk is less than *iosize this routine might return without
2539 2556   * transferring all of *iosize. In all cases, on return *iosize will contain
2540 2557   * the amount of data that was transferred.
2541 2558   */
2542 2559  static int
2543 2560  strput(struct stdata *stp, mblk_t *mctl, struct uio *uiop, ssize_t *iosize,
2544 2561      int b_flag, int pri, int flags)
2545 2562  {
2546 2563          struiod_t uiod;
     2564 +        struct iovec buf[IOV_MAX_STACK];
     2565 +        int iovlen = 0;
2547 2566          mblk_t *mp;
2548 2567          queue_t *wqp = stp->sd_wrq;
2549 2568          int error = 0;
2550 2569          ssize_t count = *iosize;
2551 2570  
2552 2571          ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
2553 2572  
2554 2573          if (uiop != NULL && count >= 0)
2555 2574                  flags |= stp->sd_struiowrq ? STRUIO_POSTPONE : 0;
2556 2575  
2557 2576          if (!(flags & STRUIO_POSTPONE)) {
2558 2577                  /*
2559 2578                   * Use regular canputnext, strmakedata, putnext sequence.
2560 2579                   */
2561 2580                  if (pri == 0) {
2562 2581                          if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2563 2582                                  freemsg(mctl);
2564 2583                                  return (EWOULDBLOCK);
2565 2584                          }
2566 2585                  } else {
2567 2586                          if (!(flags & MSG_IGNFLOW) && !bcanputnext(wqp, pri)) {
2568 2587                                  freemsg(mctl);
2569 2588                                  return (EWOULDBLOCK);
2570 2589                          }
2571 2590                  }
2572 2591  
2573 2592                  if ((error = strmakedata(iosize, uiop, stp, flags,
2574 2593                      &mp)) != 0) {
2575 2594                          freemsg(mctl);
2576 2595                          /*
2577 2596                           * need to change return code to ENOMEM
2578 2597                           * so that this is not confused with
2579 2598                           * flow control, EAGAIN.
2580 2599                           */
2581 2600  
2582 2601                          if (error == EAGAIN)
2583 2602                                  return (ENOMEM);
2584 2603                          else
2585 2604                                  return (error);
2586 2605                  }
2587 2606                  if (mctl != NULL) {
2588 2607                          if (mctl->b_cont == NULL)
2589 2608                                  mctl->b_cont = mp;
2590 2609                          else if (mp != NULL)
2591 2610                                  linkb(mctl, mp);
2592 2611                          mp = mctl;
2593 2612                  } else if (mp == NULL)
2594 2613                          return (0);
2595 2614  
2596 2615                  mp->b_flag |= b_flag;
2597 2616                  mp->b_band = (uchar_t)pri;
2598 2617  
2599 2618                  if (flags & MSG_IGNFLOW) {
2600 2619                          /*
2601 2620                           * XXX Hack: Don't get stuck running service
2602 2621                           * procedures. This is needed for sockfs when
2603 2622                           * sending the unbind message out of the rput
2604 2623                           * procedure - we don't want a put procedure
2605 2624                           * to run service procedures.
2606 2625                           */
2607 2626                          putnext(wqp, mp);
2608 2627                  } else {
2609 2628                          stream_willservice(stp);
2610 2629                          putnext(wqp, mp);
2611 2630                          stream_runservice(stp);
2612 2631                  }
2613 2632                  return (0);
2614 2633          }
2615 2634          /*
2616 2635           * Stream supports rwnext() for the write side.
2617 2636           */
2618 2637          if ((error = strmakedata(iosize, uiop, stp, flags, &mp)) != 0) {
2619 2638                  freemsg(mctl);
2620 2639                  /*
2621 2640                   * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
2622 2641                   */
2623 2642                  return (error == EAGAIN ? ENOMEM : error);
2624 2643          }
2625 2644          if (mctl != NULL) {
2626 2645                  if (mctl->b_cont == NULL)
2627 2646                          mctl->b_cont = mp;
  
    | 
      ↓ open down ↓ | 
    71 lines elided | 
    
      ↑ open up ↑ | 
  
2628 2647                  else if (mp != NULL)
2629 2648                          linkb(mctl, mp);
2630 2649                  mp = mctl;
2631 2650          } else if (mp == NULL) {
2632 2651                  return (0);
2633 2652          }
2634 2653  
2635 2654          mp->b_flag |= b_flag;
2636 2655          mp->b_band = (uchar_t)pri;
2637 2656  
2638      -        (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
2639      -            sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
     2657 +        if (uiop->uio_iovcnt > IOV_MAX_STACK) {
     2658 +                iovlen = uiop->uio_iovcnt * sizeof (iovec_t);
     2659 +                uiod.d_iov = (struct iovec *)kmem_alloc(iovlen, KM_SLEEP);
     2660 +        } else {
     2661 +                uiod.d_iov = buf;
     2662 +        }
     2663 +
     2664 +        (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov, uiop->uio_iovcnt);
2640 2665          uiod.d_uio.uio_offset = 0;
2641 2666          uiod.d_mp = mp;
2642 2667          error = rwnext(wqp, &uiod);
2643 2668          if (! uiod.d_mp) {
2644 2669                  uioskip(uiop, *iosize);
     2670 +                if (iovlen != 0)
     2671 +                        kmem_free(uiod.d_iov, iovlen);
2645 2672                  return (error);
2646 2673          }
2647 2674          ASSERT(mp == uiod.d_mp);
2648 2675          if (error == EINVAL) {
2649 2676                  /*
2650 2677                   * The stream plumbing must have changed while
2651 2678                   * we were away, so just turn off rwnext()s.
2652 2679                   */
2653 2680                  error = 0;
2654 2681          } else if (error == EBUSY || error == EWOULDBLOCK) {
2655 2682                  /*
2656 2683                   * Couldn't enter a perimeter or took a page fault,
2657 2684                   * so fall-back to putnext().
2658 2685                   */
2659 2686                  error = 0;
2660 2687          } else {
2661 2688                  freemsg(mp);
     2689 +                if (iovlen != 0)
     2690 +                        kmem_free(uiod.d_iov, iovlen);
2662 2691                  return (error);
2663 2692          }
2664 2693          /* Have to check canput before consuming data from the uio */
2665 2694          if (pri == 0) {
2666 2695                  if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2667 2696                          freemsg(mp);
     2697 +                        if (iovlen != 0)
     2698 +                                kmem_free(uiod.d_iov, iovlen);
2668 2699                          return (EWOULDBLOCK);
2669 2700                  }
2670 2701          } else {
2671 2702                  if (!bcanputnext(wqp, pri) && !(flags & MSG_IGNFLOW)) {
2672 2703                          freemsg(mp);
     2704 +                        if (iovlen != 0)
     2705 +                                kmem_free(uiod.d_iov, iovlen);
2673 2706                          return (EWOULDBLOCK);
2674 2707                  }
2675 2708          }
2676 2709          ASSERT(mp == uiod.d_mp);
2677 2710          /* Copyin data from the uio */
2678 2711          if ((error = struioget(wqp, mp, &uiod, 0)) != 0) {
2679 2712                  freemsg(mp);
     2713 +                if (iovlen != 0)
     2714 +                        kmem_free(uiod.d_iov, iovlen);
2680 2715                  return (error);
2681 2716          }
2682 2717          uioskip(uiop, *iosize);
2683 2718          if (flags & MSG_IGNFLOW) {
2684 2719                  /*
2685 2720                   * XXX Hack: Don't get stuck running service procedures.
2686 2721                   * This is needed for sockfs when sending the unbind message
2687 2722                   * out of the rput procedure - we don't want a put procedure
2688 2723                   * to run service procedures.
2689 2724                   */
2690 2725                  putnext(wqp, mp);
2691 2726          } else {
2692 2727                  stream_willservice(stp);
2693 2728                  putnext(wqp, mp);
2694 2729                  stream_runservice(stp);
2695 2730          }
     2731 +        if (iovlen != 0)
     2732 +                kmem_free(uiod.d_iov, iovlen);
2696 2733          return (0);
2697 2734  }
2698 2735  
2699 2736  /*
2700 2737   * Write attempts to break the write request into messages conforming
2701 2738   * with the minimum and maximum packet sizes set downstream.
2702 2739   *
2703 2740   * Write will not block if downstream queue is full and
2704 2741   * O_NDELAY is set, otherwise it will block waiting for the queue to get room.
2705 2742   *
2706 2743   * A write of zero bytes gets packaged into a zero length message and sent
2707 2744   * downstream like any other message.
2708 2745   *
2709 2746   * If buffers of the requested sizes are not available, the write will
2710 2747   * sleep until the buffers become available.
2711 2748   *
2712 2749   * Write (if specified) will supply a write offset in a message if it
2713 2750   * makes sense. This can be specified by downstream modules as part of
2714 2751   * a M_SETOPTS message.  Write will not supply the write offset if it
2715 2752   * cannot supply any data in a buffer.  In other words, write will never
2716 2753   * send down an empty packet due to a write offset.
2717 2754   */
2718 2755  /* ARGSUSED2 */
2719 2756  int
2720 2757  strwrite(struct vnode *vp, struct uio *uiop, cred_t *crp)
2721 2758  {
2722 2759          return (strwrite_common(vp, uiop, crp, 0));
2723 2760  }
2724 2761  
2725 2762  /* ARGSUSED2 */
2726 2763  int
2727 2764  strwrite_common(struct vnode *vp, struct uio *uiop, cred_t *crp, int wflag)
2728 2765  {
2729 2766          struct stdata *stp;
2730 2767          struct queue *wqp;
2731 2768          ssize_t rmin, rmax;
2732 2769          ssize_t iosize;
2733 2770          int waitflag;
2734 2771          int tempmode;
2735 2772          int error = 0;
2736 2773          int b_flag;
2737 2774  
2738 2775          ASSERT(vp->v_stream);
2739 2776          stp = vp->v_stream;
2740 2777  
2741 2778          mutex_enter(&stp->sd_lock);
2742 2779  
2743 2780          if ((error = i_straccess(stp, JCWRITE)) != 0) {
2744 2781                  mutex_exit(&stp->sd_lock);
2745 2782                  return (error);
2746 2783          }
2747 2784  
2748 2785          if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
2749 2786                  error = strwriteable(stp, B_TRUE, B_TRUE);
2750 2787                  if (error != 0) {
2751 2788                          mutex_exit(&stp->sd_lock);
2752 2789                          return (error);
2753 2790                  }
2754 2791          }
2755 2792  
2756 2793          mutex_exit(&stp->sd_lock);
2757 2794  
2758 2795          wqp = stp->sd_wrq;
2759 2796  
2760 2797          /* get these values from them cached in the stream head */
2761 2798          rmin = stp->sd_qn_minpsz;
2762 2799          rmax = stp->sd_qn_maxpsz;
2763 2800  
2764 2801          /*
2765 2802           * Check the min/max packet size constraints.  If min packet size
2766 2803           * is non-zero, the write cannot be split into multiple messages
2767 2804           * and still guarantee the size constraints.
2768 2805           */
2769 2806          TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_IN, "strwrite in:q %p", wqp);
2770 2807  
2771 2808          ASSERT((rmax >= 0) || (rmax == INFPSZ));
2772 2809          if (rmax == 0) {
2773 2810                  return (0);
2774 2811          }
2775 2812          if (rmin > 0) {
2776 2813                  if (uiop->uio_resid < rmin) {
2777 2814                          TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2778 2815                              "strwrite out:q %p out %d error %d",
2779 2816                              wqp, 0, ERANGE);
2780 2817                          return (ERANGE);
2781 2818                  }
2782 2819                  if ((rmax != INFPSZ) && (uiop->uio_resid > rmax)) {
2783 2820                          TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2784 2821                              "strwrite out:q %p out %d error %d",
2785 2822                              wqp, 1, ERANGE);
2786 2823                          return (ERANGE);
2787 2824                  }
2788 2825          }
2789 2826  
2790 2827          /*
2791 2828           * Do until count satisfied or error.
2792 2829           */
2793 2830          waitflag = WRITEWAIT | wflag;
2794 2831          if (stp->sd_flag & OLDNDELAY)
2795 2832                  tempmode = uiop->uio_fmode & ~FNDELAY;
2796 2833          else
2797 2834                  tempmode = uiop->uio_fmode;
2798 2835  
2799 2836          if (rmax == INFPSZ)
2800 2837                  rmax = uiop->uio_resid;
2801 2838  
2802 2839          /*
2803 2840           * Note that tempmode does not get used in strput/strmakedata
2804 2841           * but only in strwaitq. The other routines use uio_fmode
2805 2842           * unmodified.
2806 2843           */
2807 2844  
2808 2845          /* LINTED: constant in conditional context */
2809 2846          while (1) {     /* breaks when uio_resid reaches zero */
2810 2847                  /*
2811 2848                   * Determine the size of the next message to be
2812 2849                   * packaged.  May have to break write into several
2813 2850                   * messages based on max packet size.
2814 2851                   */
2815 2852                  iosize = MIN(uiop->uio_resid, rmax);
2816 2853  
2817 2854                  /*
2818 2855                   * Put block downstream when flow control allows it.
2819 2856                   */
2820 2857                  if ((stp->sd_flag & STRDELIM) && (uiop->uio_resid == iosize))
2821 2858                          b_flag = MSGDELIM;
2822 2859                  else
2823 2860                          b_flag = 0;
2824 2861  
2825 2862                  for (;;) {
2826 2863                          int done = 0;
2827 2864  
2828 2865                          error = strput(stp, NULL, uiop, &iosize, b_flag, 0, 0);
2829 2866                          if (error == 0)
2830 2867                                  break;
2831 2868                          if (error != EWOULDBLOCK)
2832 2869                                  goto out;
2833 2870  
2834 2871                          mutex_enter(&stp->sd_lock);
2835 2872                          /*
2836 2873                           * Check for a missed wakeup.
2837 2874                           * Needed since strput did not hold sd_lock across
2838 2875                           * the canputnext.
2839 2876                           */
2840 2877                          if (canputnext(wqp)) {
2841 2878                                  /* Try again */
2842 2879                                  mutex_exit(&stp->sd_lock);
2843 2880                                  continue;
2844 2881                          }
2845 2882                          TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAIT,
2846 2883                              "strwrite wait:q %p wait", wqp);
2847 2884                          if ((error = strwaitq(stp, waitflag, (ssize_t)0,
2848 2885                              tempmode, -1, &done)) != 0 || done) {
2849 2886                                  mutex_exit(&stp->sd_lock);
2850 2887                                  if ((vp->v_type == VFIFO) &&
2851 2888                                      (uiop->uio_fmode & FNDELAY) &&
2852 2889                                      (error == EAGAIN))
2853 2890                                          error = 0;
2854 2891                                  goto out;
2855 2892                          }
2856 2893                          TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAKE,
2857 2894                              "strwrite wake:q %p awakes", wqp);
2858 2895                          if ((error = i_straccess(stp, JCWRITE)) != 0) {
2859 2896                                  mutex_exit(&stp->sd_lock);
2860 2897                                  goto out;
2861 2898                          }
2862 2899                          mutex_exit(&stp->sd_lock);
2863 2900                  }
2864 2901                  waitflag |= NOINTR;
2865 2902                  TRACE_2(TR_FAC_STREAMS_FR, TR_STRWRITE_RESID,
2866 2903                      "strwrite resid:q %p uiop %p", wqp, uiop);
2867 2904                  if (uiop->uio_resid) {
2868 2905                          /* Recheck for errors - needed for sockets */
2869 2906                          if ((stp->sd_wput_opt & SW_RECHECK_ERR) &&
2870 2907                              (stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
2871 2908                                  mutex_enter(&stp->sd_lock);
2872 2909                                  error = strwriteable(stp, B_FALSE, B_TRUE);
2873 2910                                  mutex_exit(&stp->sd_lock);
2874 2911                                  if (error != 0)
2875 2912                                          return (error);
2876 2913                          }
2877 2914                          continue;
2878 2915                  }
2879 2916                  break;
2880 2917          }
2881 2918  out:
2882 2919          /*
2883 2920           * For historical reasons, applications expect EAGAIN when a data
2884 2921           * mblk_t cannot be allocated, so change ENOMEM back to EAGAIN.
2885 2922           */
2886 2923          if (error == ENOMEM)
2887 2924                  error = EAGAIN;
2888 2925          TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2889 2926              "strwrite out:q %p out %d error %d", wqp, 2, error);
2890 2927          return (error);
2891 2928  }
2892 2929  
2893 2930  /*
2894 2931   * Stream head write service routine.
2895 2932   * Its job is to wake up any sleeping writers when a queue
2896 2933   * downstream needs data (part of the flow control in putq and getq).
2897 2934   * It also must wake anyone sleeping on a poll().
2898 2935   * For stream head right below mux module, it must also invoke put procedure
2899 2936   * of next downstream module.
2900 2937   */
2901 2938  int
2902 2939  strwsrv(queue_t *q)
2903 2940  {
2904 2941          struct stdata *stp;
2905 2942          queue_t *tq;
2906 2943          qband_t *qbp;
2907 2944          int i;
2908 2945          qband_t *myqbp;
2909 2946          int isevent;
2910 2947          unsigned char   qbf[NBAND];     /* band flushing backenable flags */
2911 2948  
2912 2949          TRACE_1(TR_FAC_STREAMS_FR,
2913 2950              TR_STRWSRV, "strwsrv:q %p", q);
2914 2951          stp = (struct stdata *)q->q_ptr;
2915 2952          ASSERT(qclaimed(q));
2916 2953          mutex_enter(&stp->sd_lock);
2917 2954          ASSERT(!(stp->sd_flag & STPLEX));
2918 2955  
2919 2956          if (stp->sd_flag & WSLEEP) {
2920 2957                  stp->sd_flag &= ~WSLEEP;
2921 2958                  cv_broadcast(&q->q_wait);
2922 2959          }
2923 2960          mutex_exit(&stp->sd_lock);
2924 2961  
2925 2962          /* The other end of a stream pipe went away. */
2926 2963          if ((tq = q->q_next) == NULL) {
2927 2964                  return (0);
2928 2965          }
2929 2966  
2930 2967          /* Find the next module forward that has a service procedure */
2931 2968          claimstr(q);
2932 2969          tq = q->q_nfsrv;
2933 2970          ASSERT(tq != NULL);
2934 2971  
2935 2972          if ((q->q_flag & QBACK)) {
2936 2973                  if ((tq->q_flag & QFULL)) {
2937 2974                          mutex_enter(QLOCK(tq));
2938 2975                          if (!(tq->q_flag & QFULL)) {
2939 2976                                  mutex_exit(QLOCK(tq));
2940 2977                                  goto wakeup;
2941 2978                          }
2942 2979                          /*
2943 2980                           * The queue must have become full again. Set QWANTW
2944 2981                           * again so strwsrv will be back enabled when
2945 2982                           * the queue becomes non-full next time.
2946 2983                           */
2947 2984                          tq->q_flag |= QWANTW;
2948 2985                          mutex_exit(QLOCK(tq));
2949 2986                  } else {
2950 2987                  wakeup:
2951 2988                          pollwakeup(&stp->sd_pollist, POLLWRNORM);
2952 2989                          mutex_enter(&stp->sd_lock);
2953 2990                          if (stp->sd_sigflags & S_WRNORM)
2954 2991                                  strsendsig(stp->sd_siglist, S_WRNORM, 0, 0);
2955 2992                          mutex_exit(&stp->sd_lock);
2956 2993                  }
2957 2994          }
2958 2995  
2959 2996          isevent = 0;
2960 2997          i = 1;
2961 2998          bzero((caddr_t)qbf, NBAND);
2962 2999          mutex_enter(QLOCK(tq));
2963 3000          if ((myqbp = q->q_bandp) != NULL)
2964 3001                  for (qbp = tq->q_bandp; qbp && myqbp; qbp = qbp->qb_next) {
2965 3002                          ASSERT(myqbp);
2966 3003                          if ((myqbp->qb_flag & QB_BACK)) {
2967 3004                                  if (qbp->qb_flag & QB_FULL) {
2968 3005                                          /*
2969 3006                                           * The band must have become full again.
2970 3007                                           * Set QB_WANTW again so strwsrv will
2971 3008                                           * be back enabled when the band becomes
2972 3009                                           * non-full next time.
2973 3010                                           */
2974 3011                                          qbp->qb_flag |= QB_WANTW;
2975 3012                                  } else {
2976 3013                                          isevent = 1;
2977 3014                                          qbf[i] = 1;
2978 3015                                  }
2979 3016                          }
2980 3017                          myqbp = myqbp->qb_next;
2981 3018                          i++;
2982 3019                  }
2983 3020          mutex_exit(QLOCK(tq));
2984 3021  
2985 3022          if (isevent) {
2986 3023                  for (i = tq->q_nband; i; i--) {
2987 3024                          if (qbf[i]) {
2988 3025                                  pollwakeup(&stp->sd_pollist, POLLWRBAND);
2989 3026                                  mutex_enter(&stp->sd_lock);
2990 3027                                  if (stp->sd_sigflags & S_WRBAND)
2991 3028                                          strsendsig(stp->sd_siglist, S_WRBAND,
2992 3029                                              (uchar_t)i, 0);
2993 3030                                  mutex_exit(&stp->sd_lock);
2994 3031                          }
2995 3032                  }
2996 3033          }
2997 3034  
2998 3035          releasestr(q);
2999 3036          return (0);
3000 3037  }
3001 3038  
3002 3039  /*
3003 3040   * Special case of strcopyin/strcopyout for copying
3004 3041   * struct strioctl that can deal with both data
3005 3042   * models.
3006 3043   */
3007 3044  
3008 3045  #ifdef  _LP64
3009 3046  
3010 3047  static int
3011 3048  strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3012 3049  {
3013 3050          struct  strioctl32 strioc32;
3014 3051          struct  strioctl *striocp;
3015 3052  
3016 3053          if (copyflag & U_TO_K) {
3017 3054                  ASSERT((copyflag & K_TO_K) == 0);
3018 3055  
3019 3056                  if ((flag & FMODELS) == DATAMODEL_ILP32) {
3020 3057                          if (copyin(from, &strioc32, sizeof (strioc32)))
3021 3058                                  return (EFAULT);
3022 3059  
3023 3060                          striocp = (struct strioctl *)to;
3024 3061                          striocp->ic_cmd = strioc32.ic_cmd;
3025 3062                          striocp->ic_timout = strioc32.ic_timout;
3026 3063                          striocp->ic_len = strioc32.ic_len;
3027 3064                          striocp->ic_dp  = (char *)(uintptr_t)strioc32.ic_dp;
3028 3065  
3029 3066                  } else { /* NATIVE data model */
3030 3067                          if (copyin(from, to, sizeof (struct strioctl))) {
3031 3068                                  return (EFAULT);
3032 3069                          } else {
3033 3070                                  return (0);
3034 3071                          }
3035 3072                  }
3036 3073          } else {
3037 3074                  ASSERT(copyflag & K_TO_K);
3038 3075                  bcopy(from, to, sizeof (struct strioctl));
3039 3076          }
3040 3077          return (0);
3041 3078  }
3042 3079  
3043 3080  static int
3044 3081  strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3045 3082  {
3046 3083          struct  strioctl32 strioc32;
3047 3084          struct  strioctl *striocp;
3048 3085  
3049 3086          if (copyflag & U_TO_K) {
3050 3087                  ASSERT((copyflag & K_TO_K) == 0);
3051 3088  
3052 3089                  if ((flag & FMODELS) == DATAMODEL_ILP32) {
3053 3090                          striocp = (struct strioctl *)from;
3054 3091                          strioc32.ic_cmd = striocp->ic_cmd;
3055 3092                          strioc32.ic_timout = striocp->ic_timout;
3056 3093                          strioc32.ic_len = striocp->ic_len;
3057 3094                          strioc32.ic_dp  = (caddr32_t)(uintptr_t)striocp->ic_dp;
3058 3095                          ASSERT((char *)(uintptr_t)strioc32.ic_dp ==
3059 3096                              striocp->ic_dp);
3060 3097  
3061 3098                          if (copyout(&strioc32, to, sizeof (strioc32)))
3062 3099                                  return (EFAULT);
3063 3100  
3064 3101                  } else { /* NATIVE data model */
3065 3102                          if (copyout(from, to, sizeof (struct strioctl))) {
3066 3103                                  return (EFAULT);
3067 3104                          } else {
3068 3105                                  return (0);
3069 3106                          }
3070 3107                  }
3071 3108          } else {
3072 3109                  ASSERT(copyflag & K_TO_K);
3073 3110                  bcopy(from, to, sizeof (struct strioctl));
3074 3111          }
3075 3112          return (0);
3076 3113  }
3077 3114  
3078 3115  #else   /* ! _LP64 */
3079 3116  
3080 3117  /* ARGSUSED2 */
3081 3118  static int
3082 3119  strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3083 3120  {
3084 3121          return (strcopyin(from, to, sizeof (struct strioctl), copyflag));
3085 3122  }
3086 3123  
3087 3124  /* ARGSUSED2 */
3088 3125  static int
3089 3126  strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3090 3127  {
3091 3128          return (strcopyout(from, to, sizeof (struct strioctl), copyflag));
3092 3129  }
3093 3130  
3094 3131  #endif  /* _LP64 */
3095 3132  
3096 3133  /*
3097 3134   * Determine type of job control semantics expected by user.  The
3098 3135   * possibilities are:
3099 3136   *      JCREAD  - Behaves like read() on fd; send SIGTTIN
3100 3137   *      JCWRITE - Behaves like write() on fd; send SIGTTOU if TOSTOP set
3101 3138   *      JCSETP  - Sets a value in the stream; send SIGTTOU, ignore TOSTOP
3102 3139   *      JCGETP  - Gets a value in the stream; no signals.
3103 3140   * See straccess in strsubr.c for usage of these values.
3104 3141   *
3105 3142   * This routine also returns -1 for I_STR as a special case; the
3106 3143   * caller must call again with the real ioctl number for
3107 3144   * classification.
3108 3145   */
3109 3146  static int
3110 3147  job_control_type(int cmd)
3111 3148  {
3112 3149          switch (cmd) {
3113 3150          case I_STR:
3114 3151                  return (-1);
3115 3152  
3116 3153          case I_RECVFD:
3117 3154          case I_E_RECVFD:
3118 3155                  return (JCREAD);
3119 3156  
3120 3157          case I_FDINSERT:
3121 3158          case I_SENDFD:
3122 3159                  return (JCWRITE);
3123 3160  
3124 3161          case TCSETA:
3125 3162          case TCSETAW:
3126 3163          case TCSETAF:
3127 3164          case TCSBRK:
3128 3165          case TCXONC:
3129 3166          case TCFLSH:
3130 3167          case TCDSET:    /* Obsolete */
3131 3168          case TIOCSWINSZ:
3132 3169          case TCSETS:
3133 3170          case TCSETSW:
3134 3171          case TCSETSF:
3135 3172          case TIOCSETD:
3136 3173          case TIOCHPCL:
3137 3174          case TIOCSETP:
3138 3175          case TIOCSETN:
3139 3176          case TIOCEXCL:
3140 3177          case TIOCNXCL:
3141 3178          case TIOCFLUSH:
3142 3179          case TIOCSETC:
3143 3180          case TIOCLBIS:
3144 3181          case TIOCLBIC:
3145 3182          case TIOCLSET:
3146 3183          case TIOCSBRK:
3147 3184          case TIOCCBRK:
3148 3185          case TIOCSDTR:
3149 3186          case TIOCCDTR:
3150 3187          case TIOCSLTC:
3151 3188          case TIOCSTOP:
3152 3189          case TIOCSTART:
3153 3190          case TIOCSTI:
3154 3191          case TIOCSPGRP:
3155 3192          case TIOCMSET:
3156 3193          case TIOCMBIS:
3157 3194          case TIOCMBIC:
3158 3195          case TIOCREMOTE:
3159 3196          case TIOCSIGNAL:
3160 3197          case LDSETT:
3161 3198          case LDSMAP:    /* Obsolete */
3162 3199          case DIOCSETP:
3163 3200          case I_FLUSH:
3164 3201          case I_SRDOPT:
3165 3202          case I_SETSIG:
3166 3203          case I_SWROPT:
3167 3204          case I_FLUSHBAND:
3168 3205          case I_SETCLTIME:
3169 3206          case I_SERROPT:
3170 3207          case I_ESETSIG:
  
    | 
      ↓ open down ↓ | 
    465 lines elided | 
    
      ↑ open up ↑ | 
  
3171 3208          case FIONBIO:
3172 3209          case FIOASYNC:
3173 3210          case FIOSETOWN:
3174 3211          case JBOOT:     /* Obsolete */
3175 3212          case JTERM:     /* Obsolete */
3176 3213          case JTIMOM:    /* Obsolete */
3177 3214          case JZOMBOOT:  /* Obsolete */
3178 3215          case JAGENT:    /* Obsolete */
3179 3216          case JTRUN:     /* Obsolete */
3180 3217          case JXTPROTO:  /* Obsolete */
     3218 +        case TIOCSETLD:
3181 3219                  return (JCSETP);
3182 3220          }
3183 3221  
3184 3222          return (JCGETP);
3185 3223  }
3186 3224  
3187 3225  /*
3188 3226   * ioctl for streams
3189 3227   */
3190 3228  int
3191 3229  strioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, int copyflag,
3192 3230      cred_t *crp, int *rvalp)
3193 3231  {
3194 3232          struct stdata *stp;
3195 3233          struct strcmd *scp;
3196 3234          struct strioctl strioc;
3197 3235          struct uio uio;
3198 3236          struct iovec iov;
3199 3237          int access;
3200 3238          mblk_t *mp;
3201 3239          int error = 0;
3202 3240          int done = 0;
3203 3241          ssize_t rmin, rmax;
3204 3242          queue_t *wrq;
3205 3243          queue_t *rdq;
3206 3244          boolean_t kioctl = B_FALSE;
3207 3245          uint32_t auditing = AU_AUDITING();
3208 3246  
3209 3247          if (flag & FKIOCTL) {
3210 3248                  copyflag = K_TO_K;
3211 3249                  kioctl = B_TRUE;
3212 3250          }
3213 3251          ASSERT(vp->v_stream);
3214 3252          ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
3215 3253          stp = vp->v_stream;
3216 3254  
3217 3255          TRACE_3(TR_FAC_STREAMS_FR, TR_IOCTL_ENTER,
3218 3256              "strioctl:stp %p cmd %X arg %lX", stp, cmd, arg);
3219 3257  
3220 3258          /*
3221 3259           * If the copy is kernel to kernel, make sure that the FNATIVE
3222 3260           * flag is set.  After this it would be a serious error to have
3223 3261           * no model flag.
3224 3262           */
3225 3263          if (copyflag == K_TO_K)
3226 3264                  flag = (flag & ~FMODELS) | FNATIVE;
3227 3265  
3228 3266          ASSERT((flag & FMODELS) != 0);
3229 3267  
3230 3268          wrq = stp->sd_wrq;
3231 3269          rdq = _RD(wrq);
3232 3270  
3233 3271          access = job_control_type(cmd);
3234 3272  
3235 3273          /* We should never see these here, should be handled by iwscn */
3236 3274          if (cmd == SRIOCSREDIR || cmd == SRIOCISREDIR)
3237 3275                  return (EINVAL);
3238 3276  
3239 3277          mutex_enter(&stp->sd_lock);
3240 3278          if ((access != -1) && ((error = i_straccess(stp, access)) != 0)) {
3241 3279                  mutex_exit(&stp->sd_lock);
3242 3280                  return (error);
3243 3281          }
3244 3282          mutex_exit(&stp->sd_lock);
3245 3283  
3246 3284          /*
3247 3285           * Check for sgttyb-related ioctls first, and complain as
3248 3286           * necessary.
3249 3287           */
3250 3288          switch (cmd) {
3251 3289          case TIOCGETP:
3252 3290          case TIOCSETP:
3253 3291          case TIOCSETN:
3254 3292                  if (sgttyb_handling >= 2 && !sgttyb_complaint) {
3255 3293                          sgttyb_complaint = B_TRUE;
3256 3294                          cmn_err(CE_NOTE,
3257 3295                              "application used obsolete TIOC[GS]ET");
3258 3296                  }
3259 3297                  if (sgttyb_handling >= 3) {
3260 3298                          tsignal(curthread, SIGSYS);
3261 3299                          return (EIO);
3262 3300                  }
3263 3301                  break;
3264 3302          }
3265 3303  
3266 3304          mutex_enter(&stp->sd_lock);
3267 3305  
3268 3306          switch (cmd) {
3269 3307          case I_RECVFD:
3270 3308          case I_E_RECVFD:
3271 3309          case I_PEEK:
3272 3310          case I_NREAD:
3273 3311          case FIONREAD:
3274 3312          case FIORDCHK:
3275 3313          case I_ATMARK:
3276 3314          case FIONBIO:
3277 3315          case FIOASYNC:
3278 3316                  if (stp->sd_flag & (STRDERR|STPLEX)) {
3279 3317                          error = strgeterr(stp, STRDERR|STPLEX, 0);
3280 3318                          if (error != 0) {
3281 3319                                  mutex_exit(&stp->sd_lock);
3282 3320                                  return (error);
3283 3321                          }
3284 3322                  }
3285 3323                  break;
3286 3324  
3287 3325          default:
3288 3326                  if (stp->sd_flag & (STRDERR|STWRERR|STPLEX)) {
3289 3327                          error = strgeterr(stp, STRDERR|STWRERR|STPLEX, 0);
3290 3328                          if (error != 0) {
3291 3329                                  mutex_exit(&stp->sd_lock);
3292 3330                                  return (error);
3293 3331                          }
3294 3332                  }
3295 3333          }
3296 3334  
3297 3335          mutex_exit(&stp->sd_lock);
3298 3336  
3299 3337          switch (cmd) {
3300 3338          default:
3301 3339                  /*
3302 3340                   * The stream head has hardcoded knowledge of a
3303 3341                   * miscellaneous collection of terminal-, keyboard- and
3304 3342                   * mouse-related ioctls, enumerated below.  This hardcoded
3305 3343                   * knowledge allows the stream head to automatically
3306 3344                   * convert transparent ioctl requests made by userland
3307 3345                   * programs into I_STR ioctls which many old STREAMS
3308 3346                   * modules and drivers require.
3309 3347                   *
3310 3348                   * No new ioctls should ever be added to this list.
3311 3349                   * Instead, the STREAMS module or driver should be written
3312 3350                   * to either handle transparent ioctls or require any
3313 3351                   * userland programs to use I_STR ioctls (by returning
3314 3352                   * EINVAL to any transparent ioctl requests).
3315 3353                   *
3316 3354                   * More importantly, removing ioctls from this list should
3317 3355                   * be done with the utmost care, since our STREAMS modules
3318 3356                   * and drivers *count* on the stream head performing this
3319 3357                   * conversion, and thus may panic while processing
3320 3358                   * transparent ioctl request for one of these ioctls (keep
3321 3359                   * in mind that third party modules and drivers may have
3322 3360                   * similar problems).
3323 3361                   */
3324 3362                  if (((cmd & IOCTYPE) == LDIOC) ||
3325 3363                      ((cmd & IOCTYPE) == tIOC) ||
3326 3364                      ((cmd & IOCTYPE) == TIOC) ||
3327 3365                      ((cmd & IOCTYPE) == KIOC) ||
3328 3366                      ((cmd & IOCTYPE) == MSIOC) ||
3329 3367                      ((cmd & IOCTYPE) == VUIOC)) {
3330 3368                          /*
3331 3369                           * The ioctl is a tty ioctl - set up strioc buffer
3332 3370                           * and call strdoioctl() to do the work.
3333 3371                           */
3334 3372                          if (stp->sd_flag & STRHUP)
3335 3373                                  return (ENXIO);
3336 3374                          strioc.ic_cmd = cmd;
3337 3375                          strioc.ic_timout = INFTIM;
3338 3376  
3339 3377                          switch (cmd) {
3340 3378  
3341 3379                          case TCXONC:
3342 3380                          case TCSBRK:
3343 3381                          case TCFLSH:
3344 3382                          case TCDSET:
3345 3383                                  {
3346 3384                                  int native_arg = (int)arg;
3347 3385                                  strioc.ic_len = sizeof (int);
3348 3386                                  strioc.ic_dp = (char *)&native_arg;
3349 3387                                  return (strdoioctl(stp, &strioc, flag,
3350 3388                                      K_TO_K, crp, rvalp));
3351 3389                                  }
3352 3390  
3353 3391                          case TCSETA:
3354 3392                          case TCSETAW:
3355 3393                          case TCSETAF:
3356 3394                                  strioc.ic_len = sizeof (struct termio);
3357 3395                                  strioc.ic_dp = (char *)arg;
3358 3396                                  return (strdoioctl(stp, &strioc, flag,
3359 3397                                      copyflag, crp, rvalp));
3360 3398  
3361 3399                          case TCSETS:
3362 3400                          case TCSETSW:
3363 3401                          case TCSETSF:
3364 3402                                  strioc.ic_len = sizeof (struct termios);
3365 3403                                  strioc.ic_dp = (char *)arg;
3366 3404                                  return (strdoioctl(stp, &strioc, flag,
3367 3405                                      copyflag, crp, rvalp));
3368 3406  
3369 3407                          case LDSETT:
3370 3408                                  strioc.ic_len = sizeof (struct termcb);
3371 3409                                  strioc.ic_dp = (char *)arg;
3372 3410                                  return (strdoioctl(stp, &strioc, flag,
3373 3411                                      copyflag, crp, rvalp));
3374 3412  
3375 3413                          case TIOCSETP:
3376 3414                                  strioc.ic_len = sizeof (struct sgttyb);
3377 3415                                  strioc.ic_dp = (char *)arg;
3378 3416                                  return (strdoioctl(stp, &strioc, flag,
3379 3417                                      copyflag, crp, rvalp));
3380 3418  
3381 3419                          case TIOCSTI:
3382 3420                                  if ((flag & FREAD) == 0 &&
3383 3421                                      secpolicy_sti(crp) != 0) {
3384 3422                                          return (EPERM);
3385 3423                                  }
3386 3424                                  mutex_enter(&stp->sd_lock);
3387 3425                                  mutex_enter(&curproc->p_splock);
3388 3426                                  if (stp->sd_sidp != curproc->p_sessp->s_sidp &&
3389 3427                                      secpolicy_sti(crp) != 0) {
3390 3428                                          mutex_exit(&curproc->p_splock);
3391 3429                                          mutex_exit(&stp->sd_lock);
3392 3430                                          return (EACCES);
3393 3431                                  }
3394 3432                                  mutex_exit(&curproc->p_splock);
3395 3433                                  mutex_exit(&stp->sd_lock);
3396 3434  
3397 3435                                  strioc.ic_len = sizeof (char);
3398 3436                                  strioc.ic_dp = (char *)arg;
3399 3437                                  return (strdoioctl(stp, &strioc, flag,
3400 3438                                      copyflag, crp, rvalp));
3401 3439  
3402 3440                          case TIOCSWINSZ:
3403 3441                                  strioc.ic_len = sizeof (struct winsize);
3404 3442                                  strioc.ic_dp = (char *)arg;
3405 3443                                  return (strdoioctl(stp, &strioc, flag,
3406 3444                                      copyflag, crp, rvalp));
3407 3445  
3408 3446                          case TIOCSSIZE:
3409 3447                                  strioc.ic_len = sizeof (struct ttysize);
3410 3448                                  strioc.ic_dp = (char *)arg;
3411 3449                                  return (strdoioctl(stp, &strioc, flag,
3412 3450                                      copyflag, crp, rvalp));
3413 3451  
3414 3452                          case TIOCSSOFTCAR:
3415 3453                          case KIOCTRANS:
3416 3454                          case KIOCTRANSABLE:
3417 3455                          case KIOCCMD:
3418 3456                          case KIOCSDIRECT:
3419 3457                          case KIOCSCOMPAT:
3420 3458                          case KIOCSKABORTEN:
3421 3459                          case KIOCSRPTDELAY:
3422 3460                          case KIOCSRPTRATE:
3423 3461                          case VUIDSFORMAT:
3424 3462                          case TIOCSPPS:
3425 3463                                  strioc.ic_len = sizeof (int);
3426 3464                                  strioc.ic_dp = (char *)arg;
3427 3465                                  return (strdoioctl(stp, &strioc, flag,
3428 3466                                      copyflag, crp, rvalp));
3429 3467  
3430 3468                          case KIOCSETKEY:
3431 3469                          case KIOCGETKEY:
3432 3470                                  strioc.ic_len = sizeof (struct kiockey);
3433 3471                                  strioc.ic_dp = (char *)arg;
3434 3472                                  return (strdoioctl(stp, &strioc, flag,
3435 3473                                      copyflag, crp, rvalp));
3436 3474  
3437 3475                          case KIOCSKEY:
3438 3476                          case KIOCGKEY:
3439 3477                                  strioc.ic_len = sizeof (struct kiockeymap);
3440 3478                                  strioc.ic_dp = (char *)arg;
3441 3479                                  return (strdoioctl(stp, &strioc, flag,
3442 3480                                      copyflag, crp, rvalp));
3443 3481  
3444 3482                          case KIOCSLED:
3445 3483                                  /* arg is a pointer to char */
3446 3484                                  strioc.ic_len = sizeof (char);
3447 3485                                  strioc.ic_dp = (char *)arg;
3448 3486                                  return (strdoioctl(stp, &strioc, flag,
3449 3487                                      copyflag, crp, rvalp));
3450 3488  
3451 3489                          case MSIOSETPARMS:
3452 3490                                  strioc.ic_len = sizeof (Ms_parms);
3453 3491                                  strioc.ic_dp = (char *)arg;
3454 3492                                  return (strdoioctl(stp, &strioc, flag,
3455 3493                                      copyflag, crp, rvalp));
3456 3494  
3457 3495                          case VUIDSADDR:
3458 3496                          case VUIDGADDR:
3459 3497                                  strioc.ic_len = sizeof (struct vuid_addr_probe);
3460 3498                                  strioc.ic_dp = (char *)arg;
3461 3499                                  return (strdoioctl(stp, &strioc, flag,
3462 3500                                      copyflag, crp, rvalp));
3463 3501  
3464 3502                          /*
3465 3503                           * These M_IOCTL's don't require any data to be sent
3466 3504                           * downstream, and the driver will allocate and link
3467 3505                           * on its own mblk_t upon M_IOCACK -- thus we set
3468 3506                           * ic_len to zero and set ic_dp to arg so we know
3469 3507                           * where to copyout to later.
3470 3508                           */
3471 3509                          case TIOCGSOFTCAR:
3472 3510                          case TIOCGWINSZ:
3473 3511                          case TIOCGSIZE:
3474 3512                          case KIOCGTRANS:
3475 3513                          case KIOCGTRANSABLE:
3476 3514                          case KIOCTYPE:
3477 3515                          case KIOCGDIRECT:
3478 3516                          case KIOCGCOMPAT:
3479 3517                          case KIOCLAYOUT:
3480 3518                          case KIOCGLED:
3481 3519                          case MSIOGETPARMS:
3482 3520                          case MSIOBUTTONS:
3483 3521                          case VUIDGFORMAT:
3484 3522                          case TIOCGPPS:
3485 3523                          case TIOCGPPSEV:
3486 3524                          case TCGETA:
3487 3525                          case TCGETS:
3488 3526                          case LDGETT:
3489 3527                          case TIOCGETP:
3490 3528                          case KIOCGRPTDELAY:
3491 3529                          case KIOCGRPTRATE:
3492 3530                                  strioc.ic_len = 0;
3493 3531                                  strioc.ic_dp = (char *)arg;
3494 3532                                  return (strdoioctl(stp, &strioc, flag,
3495 3533                                      copyflag, crp, rvalp));
3496 3534                          }
3497 3535                  }
3498 3536  
3499 3537                  /*
3500 3538                   * Unknown cmd - send it down as a transparent ioctl.
3501 3539                   */
3502 3540                  strioc.ic_cmd = cmd;
3503 3541                  strioc.ic_timout = INFTIM;
3504 3542                  strioc.ic_len = TRANSPARENT;
3505 3543                  strioc.ic_dp = (char *)&arg;
3506 3544  
3507 3545                  return (strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp));
3508 3546  
3509 3547          case I_STR:
3510 3548                  /*
3511 3549                   * Stream ioctl.  Read in an strioctl buffer from the user
3512 3550                   * along with any data specified and send it downstream.
3513 3551                   * Strdoioctl will wait allow only one ioctl message at
3514 3552                   * a time, and waits for the acknowledgement.
3515 3553                   */
3516 3554  
3517 3555                  if (stp->sd_flag & STRHUP)
3518 3556                          return (ENXIO);
3519 3557  
3520 3558                  error = strcopyin_strioctl((void *)arg, &strioc, flag,
3521 3559                      copyflag);
3522 3560                  if (error != 0)
3523 3561                          return (error);
3524 3562  
3525 3563                  if ((strioc.ic_len < 0) || (strioc.ic_timout < -1))
3526 3564                          return (EINVAL);
3527 3565  
3528 3566                  access = job_control_type(strioc.ic_cmd);
3529 3567                  mutex_enter(&stp->sd_lock);
3530 3568                  if ((access != -1) &&
3531 3569                      ((error = i_straccess(stp, access)) != 0)) {
3532 3570                          mutex_exit(&stp->sd_lock);
3533 3571                          return (error);
3534 3572                  }
3535 3573                  mutex_exit(&stp->sd_lock);
3536 3574  
3537 3575                  /*
3538 3576                   * The I_STR facility provides a trap door for malicious
3539 3577                   * code to send down bogus streamio(7I) ioctl commands to
3540 3578                   * unsuspecting STREAMS modules and drivers which expect to
3541 3579                   * only get these messages from the stream head.
3542 3580                   * Explicitly prohibit any streamio ioctls which can be
3543 3581                   * passed downstream by the stream head.  Note that we do
3544 3582                   * not block all streamio ioctls because the ioctl
3545 3583                   * numberspace is not well managed and thus it's possible
3546 3584                   * that a module or driver's ioctl numbers may accidentally
3547 3585                   * collide with them.
3548 3586                   */
3549 3587                  switch (strioc.ic_cmd) {
3550 3588                  case I_LINK:
3551 3589                  case I_PLINK:
3552 3590                  case I_UNLINK:
3553 3591                  case I_PUNLINK:
3554 3592                  case _I_GETPEERCRED:
3555 3593                  case _I_PLINK_LH:
3556 3594                          return (EINVAL);
3557 3595                  }
3558 3596  
3559 3597                  error = strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp);
3560 3598                  if (error == 0) {
3561 3599                          error = strcopyout_strioctl(&strioc, (void *)arg,
3562 3600                              flag, copyflag);
3563 3601                  }
3564 3602                  return (error);
3565 3603  
3566 3604          case _I_CMD:
3567 3605                  /*
3568 3606                   * Like I_STR, but without using M_IOC* messages and without
3569 3607                   * copyins/copyouts beyond the passed-in argument.
3570 3608                   */
3571 3609                  if (stp->sd_flag & STRHUP)
3572 3610                          return (ENXIO);
3573 3611  
3574 3612                  if ((scp = kmem_alloc(sizeof (strcmd_t), KM_NOSLEEP)) == NULL)
3575 3613                          return (ENOMEM);
3576 3614  
3577 3615                  if (copyin((void *)arg, scp, sizeof (strcmd_t))) {
3578 3616                          kmem_free(scp, sizeof (strcmd_t));
3579 3617                          return (EFAULT);
3580 3618                  }
3581 3619  
3582 3620                  access = job_control_type(scp->sc_cmd);
3583 3621                  mutex_enter(&stp->sd_lock);
3584 3622                  if (access != -1 && (error = i_straccess(stp, access)) != 0) {
3585 3623                          mutex_exit(&stp->sd_lock);
3586 3624                          kmem_free(scp, sizeof (strcmd_t));
3587 3625                          return (error);
3588 3626                  }
3589 3627                  mutex_exit(&stp->sd_lock);
3590 3628  
3591 3629                  *rvalp = 0;
3592 3630                  if ((error = strdocmd(stp, scp, crp)) == 0) {
3593 3631                          if (copyout(scp, (void *)arg, sizeof (strcmd_t)))
3594 3632                                  error = EFAULT;
3595 3633                  }
3596 3634                  kmem_free(scp, sizeof (strcmd_t));
3597 3635                  return (error);
3598 3636  
3599 3637          case I_NREAD:
3600 3638                  /*
3601 3639                   * Return number of bytes of data in first message
3602 3640                   * in queue in "arg" and return the number of messages
3603 3641                   * in queue in return value.
3604 3642                   */
3605 3643          {
3606 3644                  size_t  size;
3607 3645                  int     retval;
3608 3646                  int     count = 0;
3609 3647  
3610 3648                  mutex_enter(QLOCK(rdq));
3611 3649  
3612 3650                  size = msgdsize(rdq->q_first);
3613 3651                  for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3614 3652                          count++;
3615 3653  
3616 3654                  mutex_exit(QLOCK(rdq));
3617 3655                  if (stp->sd_struiordq) {
3618 3656                          infod_t infod;
3619 3657  
3620 3658                          infod.d_cmd = INFOD_COUNT;
3621 3659                          infod.d_count = 0;
3622 3660                          if (count == 0) {
3623 3661                                  infod.d_cmd |= INFOD_FIRSTBYTES;
3624 3662                                  infod.d_bytes = 0;
3625 3663                          }
3626 3664                          infod.d_res = 0;
3627 3665                          (void) infonext(rdq, &infod);
3628 3666                          count += infod.d_count;
3629 3667                          if (infod.d_res & INFOD_FIRSTBYTES)
3630 3668                                  size = infod.d_bytes;
3631 3669                  }
3632 3670  
3633 3671                  /*
3634 3672                   * Drop down from size_t to the "int" required by the
3635 3673                   * interface.  Cap at INT_MAX.
3636 3674                   */
3637 3675                  retval = MIN(size, INT_MAX);
3638 3676                  error = strcopyout(&retval, (void *)arg, sizeof (retval),
3639 3677                      copyflag);
3640 3678                  if (!error)
3641 3679                          *rvalp = count;
3642 3680                  return (error);
3643 3681          }
3644 3682  
3645 3683          case FIONREAD:
3646 3684                  /*
3647 3685                   * Return number of bytes of data in all data messages
3648 3686                   * in queue in "arg".
3649 3687                   */
3650 3688          {
3651 3689                  size_t  size = 0;
3652 3690                  int     retval;
3653 3691  
3654 3692                  mutex_enter(QLOCK(rdq));
3655 3693                  for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3656 3694                          size += msgdsize(mp);
3657 3695                  mutex_exit(QLOCK(rdq));
3658 3696  
3659 3697                  if (stp->sd_struiordq) {
3660 3698                          infod_t infod;
3661 3699  
3662 3700                          infod.d_cmd = INFOD_BYTES;
3663 3701                          infod.d_res = 0;
3664 3702                          infod.d_bytes = 0;
3665 3703                          (void) infonext(rdq, &infod);
3666 3704                          size += infod.d_bytes;
3667 3705                  }
3668 3706  
3669 3707                  /*
3670 3708                   * Drop down from size_t to the "int" required by the
3671 3709                   * interface.  Cap at INT_MAX.
3672 3710                   */
3673 3711                  retval = MIN(size, INT_MAX);
3674 3712                  error = strcopyout(&retval, (void *)arg, sizeof (retval),
3675 3713                      copyflag);
3676 3714  
3677 3715                  *rvalp = 0;
3678 3716                  return (error);
3679 3717          }
3680 3718          case FIORDCHK:
3681 3719                  /*
3682 3720                   * FIORDCHK does not use arg value (like FIONREAD),
3683 3721                   * instead a count is returned. I_NREAD value may
3684 3722                   * not be accurate but safe. The real thing to do is
3685 3723                   * to add the msgdsizes of all data  messages until
3686 3724                   * a non-data message.
3687 3725                   */
3688 3726          {
3689 3727                  size_t size = 0;
3690 3728  
3691 3729                  mutex_enter(QLOCK(rdq));
3692 3730                  for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3693 3731                          size += msgdsize(mp);
3694 3732                  mutex_exit(QLOCK(rdq));
3695 3733  
3696 3734                  if (stp->sd_struiordq) {
3697 3735                          infod_t infod;
3698 3736  
3699 3737                          infod.d_cmd = INFOD_BYTES;
3700 3738                          infod.d_res = 0;
3701 3739                          infod.d_bytes = 0;
3702 3740                          (void) infonext(rdq, &infod);
3703 3741                          size += infod.d_bytes;
3704 3742                  }
3705 3743  
3706 3744                  /*
3707 3745                   * Since ioctl returns an int, and memory sizes under
3708 3746                   * LP64 may not fit, we return INT_MAX if the count was
3709 3747                   * actually greater.
3710 3748                   */
3711 3749                  *rvalp = MIN(size, INT_MAX);
3712 3750                  return (0);
3713 3751          }
3714 3752  
3715 3753          case I_FIND:
3716 3754                  /*
3717 3755                   * Get module name.
3718 3756                   */
3719 3757          {
3720 3758                  char mname[FMNAMESZ + 1];
3721 3759                  queue_t *q;
3722 3760  
3723 3761                  error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3724 3762                      mname, FMNAMESZ + 1, NULL);
3725 3763                  if (error)
3726 3764                          return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3727 3765  
3728 3766                  /*
3729 3767                   * Return EINVAL if we're handed a bogus module name.
3730 3768                   */
3731 3769                  if (fmodsw_find(mname, FMODSW_LOAD) == NULL) {
3732 3770                          TRACE_0(TR_FAC_STREAMS_FR,
3733 3771                              TR_I_CANT_FIND, "couldn't I_FIND");
3734 3772                          return (EINVAL);
3735 3773                  }
3736 3774  
3737 3775                  *rvalp = 0;
3738 3776  
3739 3777                  /* Look downstream to see if module is there. */
3740 3778                  claimstr(stp->sd_wrq);
3741 3779                  for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
3742 3780                          if (q->q_flag & QREADR) {
3743 3781                                  q = NULL;
3744 3782                                  break;
3745 3783                          }
3746 3784                          if (strcmp(mname, Q2NAME(q)) == 0)
3747 3785                                  break;
3748 3786                  }
3749 3787                  releasestr(stp->sd_wrq);
3750 3788  
3751 3789                  *rvalp = (q ? 1 : 0);
3752 3790                  return (error);
3753 3791          }
3754 3792  
3755 3793          case I_PUSH:
3756 3794          case __I_PUSH_NOCTTY:
3757 3795                  /*
3758 3796                   * Push a module.
3759 3797                   * For the case __I_PUSH_NOCTTY push a module but
3760 3798                   * do not allocate controlling tty. See bugid 4025044
3761 3799                   */
3762 3800  
3763 3801          {
3764 3802                  char mname[FMNAMESZ + 1];
3765 3803                  fmodsw_impl_t *fp;
3766 3804                  dev_t dummydev;
3767 3805  
3768 3806                  if (stp->sd_flag & STRHUP)
3769 3807                          return (ENXIO);
3770 3808  
3771 3809                  /*
3772 3810                   * Get module name and look up in fmodsw.
3773 3811                   */
3774 3812                  error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3775 3813                      mname, FMNAMESZ + 1, NULL);
3776 3814                  if (error)
3777 3815                          return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3778 3816  
3779 3817                  if ((fp = fmodsw_find(mname, FMODSW_HOLD | FMODSW_LOAD)) ==
3780 3818                      NULL)
3781 3819                          return (EINVAL);
3782 3820  
3783 3821                  TRACE_2(TR_FAC_STREAMS_FR, TR_I_PUSH,
3784 3822                      "I_PUSH:fp %p stp %p", fp, stp);
3785 3823  
3786 3824                  if (error = strstartplumb(stp, flag, cmd)) {
3787 3825                          fmodsw_rele(fp);
3788 3826                          return (error);
3789 3827                  }
3790 3828  
3791 3829                  /*
3792 3830                   * See if any more modules can be pushed on this stream.
3793 3831                   * Note that this check must be done after strstartplumb()
3794 3832                   * since otherwise multiple threads issuing I_PUSHes on
3795 3833                   * the same stream will be able to exceed nstrpush.
3796 3834                   */
3797 3835                  mutex_enter(&stp->sd_lock);
3798 3836                  if (stp->sd_pushcnt >= nstrpush) {
3799 3837                          fmodsw_rele(fp);
3800 3838                          strendplumb(stp);
3801 3839                          mutex_exit(&stp->sd_lock);
3802 3840                          return (EINVAL);
3803 3841                  }
3804 3842                  mutex_exit(&stp->sd_lock);
3805 3843  
3806 3844                  /*
3807 3845                   * Push new module and call its open routine
3808 3846                   * via qattach().  Modules don't change device
3809 3847                   * numbers, so just ignore dummydev here.
3810 3848                   */
3811 3849                  dummydev = vp->v_rdev;
3812 3850                  if ((error = qattach(rdq, &dummydev, 0, crp, fp,
3813 3851                      B_FALSE)) == 0) {
3814 3852                          if (vp->v_type == VCHR && /* sorry, no pipes allowed */
3815 3853                              (cmd == I_PUSH) && (stp->sd_flag & STRISTTY)) {
3816 3854                                  /*
3817 3855                                   * try to allocate it as a controlling terminal
3818 3856                                   */
3819 3857                                  (void) strctty(stp);
3820 3858                          }
3821 3859                  }
3822 3860  
3823 3861                  mutex_enter(&stp->sd_lock);
3824 3862  
3825 3863                  /*
3826 3864                   * As a performance concern we are caching the values of
3827 3865                   * q_minpsz and q_maxpsz of the module below the stream
3828 3866                   * head in the stream head.
3829 3867                   */
3830 3868                  mutex_enter(QLOCK(stp->sd_wrq->q_next));
3831 3869                  rmin = stp->sd_wrq->q_next->q_minpsz;
3832 3870                  rmax = stp->sd_wrq->q_next->q_maxpsz;
3833 3871                  mutex_exit(QLOCK(stp->sd_wrq->q_next));
3834 3872  
3835 3873                  /* Do this processing here as a performance concern */
3836 3874                  if (strmsgsz != 0) {
3837 3875                          if (rmax == INFPSZ)
3838 3876                                  rmax = strmsgsz;
3839 3877                          else  {
3840 3878                                  if (vp->v_type == VFIFO)
3841 3879                                          rmax = MIN(PIPE_BUF, rmax);
3842 3880                                  else    rmax = MIN(strmsgsz, rmax);
3843 3881                          }
3844 3882                  }
3845 3883  
3846 3884                  mutex_enter(QLOCK(wrq));
3847 3885                  stp->sd_qn_minpsz = rmin;
3848 3886                  stp->sd_qn_maxpsz = rmax;
3849 3887                  mutex_exit(QLOCK(wrq));
3850 3888  
3851 3889                  strendplumb(stp);
3852 3890                  mutex_exit(&stp->sd_lock);
3853 3891                  return (error);
3854 3892          }
3855 3893  
3856 3894          case I_POP:
3857 3895          {
3858 3896                  queue_t *q;
3859 3897  
3860 3898                  if (stp->sd_flag & STRHUP)
3861 3899                          return (ENXIO);
3862 3900                  if (!wrq->q_next)       /* for broken pipes */
3863 3901                          return (EINVAL);
3864 3902  
3865 3903                  if (error = strstartplumb(stp, flag, cmd))
3866 3904                          return (error);
3867 3905  
3868 3906                  /*
3869 3907                   * If there is an anchor on this stream and popping
3870 3908                   * the current module would attempt to pop through the
3871 3909                   * anchor, then disallow the pop unless we have sufficient
3872 3910                   * privileges; take the cheapest (non-locking) check
3873 3911                   * first.
3874 3912                   */
3875 3913                  if (secpolicy_ip_config(crp, B_TRUE) != 0 ||
3876 3914                      (stp->sd_anchorzone != crgetzoneid(crp))) {
3877 3915                          mutex_enter(&stp->sd_lock);
3878 3916                          /*
3879 3917                           * Anchors only apply if there's at least one
3880 3918                           * module on the stream (sd_pushcnt > 0).
3881 3919                           */
3882 3920                          if (stp->sd_pushcnt > 0 &&
3883 3921                              stp->sd_pushcnt == stp->sd_anchor &&
3884 3922                              stp->sd_vnode->v_type != VFIFO) {
3885 3923                                  strendplumb(stp);
3886 3924                                  mutex_exit(&stp->sd_lock);
3887 3925                                  if (stp->sd_anchorzone != crgetzoneid(crp))
3888 3926                                          return (EINVAL);
3889 3927                                  /* Audit and report error */
3890 3928                                  return (secpolicy_ip_config(crp, B_FALSE));
3891 3929                          }
3892 3930                          mutex_exit(&stp->sd_lock);
3893 3931                  }
3894 3932  
3895 3933                  q = wrq->q_next;
3896 3934                  TRACE_2(TR_FAC_STREAMS_FR, TR_I_POP,
3897 3935                      "I_POP:%p from %p", q, stp);
3898 3936                  if (q->q_next == NULL || (q->q_flag & (QREADR|QISDRV))) {
3899 3937                          error = EINVAL;
3900 3938                  } else {
3901 3939                          qdetach(_RD(q), 1, flag, crp, B_FALSE);
3902 3940                          error = 0;
3903 3941                  }
3904 3942                  mutex_enter(&stp->sd_lock);
3905 3943  
3906 3944                  /*
3907 3945                   * As a performance concern we are caching the values of
3908 3946                   * q_minpsz and q_maxpsz of the module below the stream
3909 3947                   * head in the stream head.
3910 3948                   */
3911 3949                  mutex_enter(QLOCK(wrq->q_next));
3912 3950                  rmin = wrq->q_next->q_minpsz;
3913 3951                  rmax = wrq->q_next->q_maxpsz;
3914 3952                  mutex_exit(QLOCK(wrq->q_next));
3915 3953  
3916 3954                  /* Do this processing here as a performance concern */
3917 3955                  if (strmsgsz != 0) {
3918 3956                          if (rmax == INFPSZ)
3919 3957                                  rmax = strmsgsz;
3920 3958                          else  {
3921 3959                                  if (vp->v_type == VFIFO)
3922 3960                                          rmax = MIN(PIPE_BUF, rmax);
3923 3961                                  else    rmax = MIN(strmsgsz, rmax);
3924 3962                          }
3925 3963                  }
3926 3964  
3927 3965                  mutex_enter(QLOCK(wrq));
3928 3966                  stp->sd_qn_minpsz = rmin;
3929 3967                  stp->sd_qn_maxpsz = rmax;
3930 3968                  mutex_exit(QLOCK(wrq));
3931 3969  
3932 3970                  /* If we popped through the anchor, then reset the anchor. */
3933 3971                  if (stp->sd_pushcnt < stp->sd_anchor) {
3934 3972                          stp->sd_anchor = 0;
3935 3973                          stp->sd_anchorzone = 0;
3936 3974                  }
3937 3975                  strendplumb(stp);
3938 3976                  mutex_exit(&stp->sd_lock);
3939 3977                  return (error);
3940 3978          }
3941 3979  
3942 3980          case _I_MUXID2FD:
3943 3981          {
3944 3982                  /*
3945 3983                   * Create a fd for a I_PLINK'ed lower stream with a given
3946 3984                   * muxid.  With the fd, application can send down ioctls,
3947 3985                   * like I_LIST, to the previously I_PLINK'ed stream.  Note
3948 3986                   * that after getting the fd, the application has to do an
3949 3987                   * I_PUNLINK on the muxid before it can do any operation
3950 3988                   * on the lower stream.  This is required by spec1170.
3951 3989                   *
3952 3990                   * The fd used to do this ioctl should point to the same
3953 3991                   * controlling device used to do the I_PLINK.  If it uses
3954 3992                   * a different stream or an invalid muxid, I_MUXID2FD will
3955 3993                   * fail.  The error code is set to EINVAL.
3956 3994                   *
3957 3995                   * The intended use of this interface is the following.
3958 3996                   * An application I_PLINK'ed a stream and exits.  The fd
3959 3997                   * to the lower stream is gone.  Another application
3960 3998                   * wants to get a fd to the lower stream, it uses I_MUXID2FD.
3961 3999                   */
3962 4000                  int muxid = (int)arg;
3963 4001                  int fd;
3964 4002                  linkinfo_t *linkp;
3965 4003                  struct file *fp;
3966 4004                  netstack_t *ns;
3967 4005                  str_stack_t *ss;
3968 4006  
3969 4007                  /*
3970 4008                   * Do not allow the wildcard muxid.  This ioctl is not
3971 4009                   * intended to find arbitrary link.
3972 4010                   */
3973 4011                  if (muxid == 0) {
3974 4012                          return (EINVAL);
3975 4013                  }
3976 4014  
3977 4015                  ns = netstack_find_by_cred(crp);
3978 4016                  ASSERT(ns != NULL);
3979 4017                  ss = ns->netstack_str;
3980 4018                  ASSERT(ss != NULL);
3981 4019  
3982 4020                  mutex_enter(&muxifier);
3983 4021                  linkp = findlinks(vp->v_stream, muxid, LINKPERSIST, ss);
3984 4022                  if (linkp == NULL) {
3985 4023                          mutex_exit(&muxifier);
3986 4024                          netstack_rele(ss->ss_netstack);
3987 4025                          return (EINVAL);
3988 4026                  }
3989 4027  
3990 4028                  if ((fd = ufalloc(0)) == -1) {
3991 4029                          mutex_exit(&muxifier);
3992 4030                          netstack_rele(ss->ss_netstack);
3993 4031                          return (EMFILE);
3994 4032                  }
3995 4033                  fp = linkp->li_fpdown;
3996 4034                  mutex_enter(&fp->f_tlock);
3997 4035                  fp->f_count++;
3998 4036                  mutex_exit(&fp->f_tlock);
3999 4037                  mutex_exit(&muxifier);
4000 4038                  setf(fd, fp);
4001 4039                  *rvalp = fd;
4002 4040                  netstack_rele(ss->ss_netstack);
4003 4041                  return (0);
4004 4042          }
4005 4043  
4006 4044          case _I_INSERT:
4007 4045          {
4008 4046                  /*
4009 4047                   * To insert a module to a given position in a stream.
4010 4048                   * In the first release, only allow privileged user
4011 4049                   * to use this ioctl. Furthermore, the insert is only allowed
4012 4050                   * below an anchor if the zoneid is the same as the zoneid
4013 4051                   * which created the anchor.
4014 4052                   *
4015 4053                   * Note that we do not plan to support this ioctl
4016 4054                   * on pipes in the first release.  We want to learn more
4017 4055                   * about the implications of these ioctls before extending
4018 4056                   * their support.  And we do not think these features are
4019 4057                   * valuable for pipes.
4020 4058                   */
4021 4059                  STRUCT_DECL(strmodconf, strmodinsert);
4022 4060                  char mod_name[FMNAMESZ + 1];
4023 4061                  fmodsw_impl_t *fp;
4024 4062                  dev_t dummydev;
4025 4063                  queue_t *tmp_wrq;
4026 4064                  int pos;
4027 4065                  boolean_t is_insert;
4028 4066  
4029 4067                  STRUCT_INIT(strmodinsert, flag);
4030 4068                  if (stp->sd_flag & STRHUP)
4031 4069                          return (ENXIO);
4032 4070                  if (STRMATED(stp))
4033 4071                          return (EINVAL);
4034 4072                  if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4035 4073                          return (error);
4036 4074                  if (stp->sd_anchor != 0 &&
4037 4075                      stp->sd_anchorzone != crgetzoneid(crp))
4038 4076                          return (EINVAL);
4039 4077  
4040 4078                  error = strcopyin((void *)arg, STRUCT_BUF(strmodinsert),
4041 4079                      STRUCT_SIZE(strmodinsert), copyflag);
4042 4080                  if (error)
4043 4081                          return (error);
4044 4082  
4045 4083                  /*
4046 4084                   * Get module name and look up in fmodsw.
4047 4085                   */
4048 4086                  error = (copyflag & U_TO_K ? copyinstr :
4049 4087                      copystr)(STRUCT_FGETP(strmodinsert, mod_name),
4050 4088                      mod_name, FMNAMESZ + 1, NULL);
4051 4089                  if (error)
4052 4090                          return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4053 4091  
4054 4092                  if ((fp = fmodsw_find(mod_name, FMODSW_HOLD | FMODSW_LOAD)) ==
4055 4093                      NULL)
4056 4094                          return (EINVAL);
4057 4095  
4058 4096                  if (error = strstartplumb(stp, flag, cmd)) {
4059 4097                          fmodsw_rele(fp);
4060 4098                          return (error);
4061 4099                  }
4062 4100  
4063 4101                  /*
4064 4102                   * Is this _I_INSERT just like an I_PUSH?  We need to know
4065 4103                   * this because we do some optimizations if this is a
4066 4104                   * module being pushed.
4067 4105                   */
4068 4106                  pos = STRUCT_FGET(strmodinsert, pos);
4069 4107                  is_insert = (pos != 0);
4070 4108  
4071 4109                  /*
4072 4110                   * Make sure pos is valid.  Even though it is not an I_PUSH,
4073 4111                   * we impose the same limit on the number of modules in a
4074 4112                   * stream.
4075 4113                   */
4076 4114                  mutex_enter(&stp->sd_lock);
4077 4115                  if (stp->sd_pushcnt >= nstrpush || pos < 0 ||
4078 4116                      pos > stp->sd_pushcnt) {
4079 4117                          fmodsw_rele(fp);
4080 4118                          strendplumb(stp);
4081 4119                          mutex_exit(&stp->sd_lock);
4082 4120                          return (EINVAL);
4083 4121                  }
4084 4122                  if (stp->sd_anchor != 0) {
4085 4123                          /*
4086 4124                           * Is this insert below the anchor?
4087 4125                           * Pushcnt hasn't been increased yet hence
4088 4126                           * we test for greater than here, and greater or
4089 4127                           * equal after qattach.
4090 4128                           */
4091 4129                          if (pos > (stp->sd_pushcnt - stp->sd_anchor) &&
4092 4130                              stp->sd_anchorzone != crgetzoneid(crp)) {
4093 4131                                  fmodsw_rele(fp);
4094 4132                                  strendplumb(stp);
4095 4133                                  mutex_exit(&stp->sd_lock);
4096 4134                                  return (EPERM);
4097 4135                          }
4098 4136                  }
4099 4137  
4100 4138                  mutex_exit(&stp->sd_lock);
4101 4139  
4102 4140                  /*
4103 4141                   * First find the correct position this module to
4104 4142                   * be inserted.  We don't need to call claimstr()
4105 4143                   * as the stream should not be changing at this point.
4106 4144                   *
4107 4145                   * Insert new module and call its open routine
4108 4146                   * via qattach().  Modules don't change device
4109 4147                   * numbers, so just ignore dummydev here.
4110 4148                   */
4111 4149                  for (tmp_wrq = stp->sd_wrq; pos > 0;
4112 4150                      tmp_wrq = tmp_wrq->q_next, pos--) {
4113 4151                          ASSERT(SAMESTR(tmp_wrq));
4114 4152                  }
4115 4153                  dummydev = vp->v_rdev;
4116 4154                  if ((error = qattach(_RD(tmp_wrq), &dummydev, 0, crp,
4117 4155                      fp, is_insert)) != 0) {
4118 4156                          mutex_enter(&stp->sd_lock);
4119 4157                          strendplumb(stp);
4120 4158                          mutex_exit(&stp->sd_lock);
4121 4159                          return (error);
4122 4160                  }
4123 4161  
4124 4162                  mutex_enter(&stp->sd_lock);
4125 4163  
4126 4164                  /*
4127 4165                   * As a performance concern we are caching the values of
4128 4166                   * q_minpsz and q_maxpsz of the module below the stream
4129 4167                   * head in the stream head.
4130 4168                   */
4131 4169                  if (!is_insert) {
4132 4170                          mutex_enter(QLOCK(stp->sd_wrq->q_next));
4133 4171                          rmin = stp->sd_wrq->q_next->q_minpsz;
4134 4172                          rmax = stp->sd_wrq->q_next->q_maxpsz;
4135 4173                          mutex_exit(QLOCK(stp->sd_wrq->q_next));
4136 4174  
4137 4175                          /* Do this processing here as a performance concern */
4138 4176                          if (strmsgsz != 0) {
4139 4177                                  if (rmax == INFPSZ) {
4140 4178                                          rmax = strmsgsz;
4141 4179                                  } else  {
4142 4180                                          rmax = MIN(strmsgsz, rmax);
4143 4181                                  }
4144 4182                          }
4145 4183  
4146 4184                          mutex_enter(QLOCK(wrq));
4147 4185                          stp->sd_qn_minpsz = rmin;
4148 4186                          stp->sd_qn_maxpsz = rmax;
4149 4187                          mutex_exit(QLOCK(wrq));
4150 4188                  }
4151 4189  
4152 4190                  /*
4153 4191                   * Need to update the anchor value if this module is
4154 4192                   * inserted below the anchor point.
4155 4193                   */
4156 4194                  if (stp->sd_anchor != 0) {
4157 4195                          pos = STRUCT_FGET(strmodinsert, pos);
4158 4196                          if (pos >= (stp->sd_pushcnt - stp->sd_anchor))
4159 4197                                  stp->sd_anchor++;
4160 4198                  }
4161 4199  
4162 4200                  strendplumb(stp);
4163 4201                  mutex_exit(&stp->sd_lock);
4164 4202                  return (0);
4165 4203          }
4166 4204  
4167 4205          case _I_REMOVE:
4168 4206          {
4169 4207                  /*
4170 4208                   * To remove a module with a given name in a stream.  The
4171 4209                   * caller of this ioctl needs to provide both the name and
4172 4210                   * the position of the module to be removed.  This eliminates
4173 4211                   * the ambiguity of removal if a module is inserted/pushed
4174 4212                   * multiple times in a stream.  In the first release, only
4175 4213                   * allow privileged user to use this ioctl.
4176 4214                   * Furthermore, the remove is only allowed
4177 4215                   * below an anchor if the zoneid is the same as the zoneid
4178 4216                   * which created the anchor.
4179 4217                   *
4180 4218                   * Note that we do not plan to support this ioctl
4181 4219                   * on pipes in the first release.  We want to learn more
4182 4220                   * about the implications of these ioctls before extending
4183 4221                   * their support.  And we do not think these features are
4184 4222                   * valuable for pipes.
4185 4223                   *
4186 4224                   * Also note that _I_REMOVE cannot be used to remove a
4187 4225                   * driver or the stream head.
4188 4226                   */
4189 4227                  STRUCT_DECL(strmodconf, strmodremove);
4190 4228                  queue_t *q;
4191 4229                  int pos;
4192 4230                  char mod_name[FMNAMESZ + 1];
4193 4231                  boolean_t is_remove;
4194 4232  
4195 4233                  STRUCT_INIT(strmodremove, flag);
4196 4234                  if (stp->sd_flag & STRHUP)
4197 4235                          return (ENXIO);
4198 4236                  if (STRMATED(stp))
4199 4237                          return (EINVAL);
4200 4238                  if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4201 4239                          return (error);
4202 4240                  if (stp->sd_anchor != 0 &&
4203 4241                      stp->sd_anchorzone != crgetzoneid(crp))
4204 4242                          return (EINVAL);
4205 4243  
4206 4244                  error = strcopyin((void *)arg, STRUCT_BUF(strmodremove),
4207 4245                      STRUCT_SIZE(strmodremove), copyflag);
4208 4246                  if (error)
4209 4247                          return (error);
4210 4248  
4211 4249                  error = (copyflag & U_TO_K ? copyinstr :
4212 4250                      copystr)(STRUCT_FGETP(strmodremove, mod_name),
4213 4251                      mod_name, FMNAMESZ + 1, NULL);
4214 4252                  if (error)
4215 4253                          return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4216 4254  
4217 4255                  if ((error = strstartplumb(stp, flag, cmd)) != 0)
4218 4256                          return (error);
4219 4257  
4220 4258                  /*
4221 4259                   * Match the name of given module to the name of module at
4222 4260                   * the given position.
4223 4261                   */
4224 4262                  pos = STRUCT_FGET(strmodremove, pos);
4225 4263  
4226 4264                  is_remove = (pos != 0);
4227 4265                  for (q = stp->sd_wrq->q_next; SAMESTR(q) && pos > 0;
4228 4266                      q = q->q_next, pos--)
4229 4267                          ;
4230 4268                  if (pos > 0 || !SAMESTR(q) ||
4231 4269                      strcmp(Q2NAME(q), mod_name) != 0) {
4232 4270                          mutex_enter(&stp->sd_lock);
4233 4271                          strendplumb(stp);
4234 4272                          mutex_exit(&stp->sd_lock);
4235 4273                          return (EINVAL);
4236 4274                  }
4237 4275  
4238 4276                  /*
4239 4277                   * If the position is at or below an anchor, then the zoneid
4240 4278                   * must match the zoneid that created the anchor.
4241 4279                   */
4242 4280                  if (stp->sd_anchor != 0) {
4243 4281                          pos = STRUCT_FGET(strmodremove, pos);
4244 4282                          if (pos >= (stp->sd_pushcnt - stp->sd_anchor) &&
4245 4283                              stp->sd_anchorzone != crgetzoneid(crp)) {
4246 4284                                  mutex_enter(&stp->sd_lock);
4247 4285                                  strendplumb(stp);
4248 4286                                  mutex_exit(&stp->sd_lock);
4249 4287                                  return (EPERM);
4250 4288                          }
4251 4289                  }
4252 4290  
4253 4291  
4254 4292                  ASSERT(!(q->q_flag & QREADR));
4255 4293                  qdetach(_RD(q), 1, flag, crp, is_remove);
4256 4294  
4257 4295                  mutex_enter(&stp->sd_lock);
4258 4296  
4259 4297                  /*
4260 4298                   * As a performance concern we are caching the values of
4261 4299                   * q_minpsz and q_maxpsz of the module below the stream
4262 4300                   * head in the stream head.
4263 4301                   */
4264 4302                  if (!is_remove) {
4265 4303                          mutex_enter(QLOCK(wrq->q_next));
4266 4304                          rmin = wrq->q_next->q_minpsz;
4267 4305                          rmax = wrq->q_next->q_maxpsz;
4268 4306                          mutex_exit(QLOCK(wrq->q_next));
4269 4307  
4270 4308                          /* Do this processing here as a performance concern */
4271 4309                          if (strmsgsz != 0) {
4272 4310                                  if (rmax == INFPSZ)
4273 4311                                          rmax = strmsgsz;
4274 4312                                  else  {
4275 4313                                          if (vp->v_type == VFIFO)
4276 4314                                                  rmax = MIN(PIPE_BUF, rmax);
4277 4315                                          else    rmax = MIN(strmsgsz, rmax);
4278 4316                                  }
4279 4317                          }
4280 4318  
4281 4319                          mutex_enter(QLOCK(wrq));
4282 4320                          stp->sd_qn_minpsz = rmin;
4283 4321                          stp->sd_qn_maxpsz = rmax;
4284 4322                          mutex_exit(QLOCK(wrq));
4285 4323                  }
4286 4324  
4287 4325                  /*
4288 4326                   * Need to update the anchor value if this module is removed
4289 4327                   * at or below the anchor point.  If the removed module is at
4290 4328                   * the anchor point, remove the anchor for this stream if
4291 4329                   * there is no module above the anchor point.  Otherwise, if
4292 4330                   * the removed module is below the anchor point, decrement the
4293 4331                   * anchor point by 1.
4294 4332                   */
4295 4333                  if (stp->sd_anchor != 0) {
4296 4334                          pos = STRUCT_FGET(strmodremove, pos);
4297 4335                          if (pos == stp->sd_pushcnt - stp->sd_anchor + 1)
4298 4336                                  stp->sd_anchor = 0;
4299 4337                          else if (pos > (stp->sd_pushcnt - stp->sd_anchor + 1))
4300 4338                                  stp->sd_anchor--;
4301 4339                  }
4302 4340  
4303 4341                  strendplumb(stp);
4304 4342                  mutex_exit(&stp->sd_lock);
4305 4343                  return (0);
4306 4344          }
4307 4345  
4308 4346          case I_ANCHOR:
4309 4347                  /*
4310 4348                   * Set the anchor position on the stream to reside at
4311 4349                   * the top module (in other words, the top module
4312 4350                   * cannot be popped).  Anchors with a FIFO make no
4313 4351                   * obvious sense, so they're not allowed.
4314 4352                   */
4315 4353                  mutex_enter(&stp->sd_lock);
4316 4354  
4317 4355                  if (stp->sd_vnode->v_type == VFIFO) {
4318 4356                          mutex_exit(&stp->sd_lock);
4319 4357                          return (EINVAL);
4320 4358                  }
4321 4359                  /* Only allow the same zoneid to update the anchor */
4322 4360                  if (stp->sd_anchor != 0 &&
4323 4361                      stp->sd_anchorzone != crgetzoneid(crp)) {
4324 4362                          mutex_exit(&stp->sd_lock);
4325 4363                          return (EINVAL);
4326 4364                  }
4327 4365                  stp->sd_anchor = stp->sd_pushcnt;
4328 4366                  stp->sd_anchorzone = crgetzoneid(crp);
4329 4367                  mutex_exit(&stp->sd_lock);
4330 4368                  return (0);
4331 4369  
4332 4370          case I_LOOK:
4333 4371                  /*
4334 4372                   * Get name of first module downstream.
4335 4373                   * If no module, return an error.
4336 4374                   */
4337 4375                  claimstr(wrq);
4338 4376                  if (_SAMESTR(wrq) && wrq->q_next->q_next != NULL) {
4339 4377                          char *name = Q2NAME(wrq->q_next);
4340 4378  
4341 4379                          error = strcopyout(name, (void *)arg, strlen(name) + 1,
4342 4380                              copyflag);
4343 4381                          releasestr(wrq);
4344 4382                          return (error);
4345 4383                  }
4346 4384                  releasestr(wrq);
4347 4385                  return (EINVAL);
4348 4386  
4349 4387          case I_LINK:
4350 4388          case I_PLINK:
4351 4389                  /*
4352 4390                   * Link a multiplexor.
4353 4391                   */
4354 4392                  return (mlink(vp, cmd, (int)arg, crp, rvalp, 0));
4355 4393  
4356 4394          case _I_PLINK_LH:
4357 4395                  /*
4358 4396                   * Link a multiplexor: Call must originate from kernel.
4359 4397                   */
4360 4398                  if (kioctl)
4361 4399                          return (ldi_mlink_lh(vp, cmd, arg, crp, rvalp));
4362 4400  
4363 4401                  return (EINVAL);
4364 4402          case I_UNLINK:
4365 4403          case I_PUNLINK:
4366 4404                  /*
4367 4405                   * Unlink a multiplexor.
4368 4406                   * If arg is -1, unlink all links for which this is the
4369 4407                   * controlling stream.  Otherwise, arg is an index number
4370 4408                   * for a link to be removed.
4371 4409                   */
4372 4410          {
4373 4411                  struct linkinfo *linkp;
4374 4412                  int native_arg = (int)arg;
4375 4413                  int type;
4376 4414                  netstack_t *ns;
4377 4415                  str_stack_t *ss;
4378 4416  
4379 4417                  TRACE_1(TR_FAC_STREAMS_FR,
4380 4418                      TR_I_UNLINK, "I_UNLINK/I_PUNLINK:%p", stp);
4381 4419                  if (vp->v_type == VFIFO) {
4382 4420                          return (EINVAL);
4383 4421                  }
4384 4422                  if (cmd == I_UNLINK)
4385 4423                          type = LINKNORMAL;
4386 4424                  else    /* I_PUNLINK */
4387 4425                          type = LINKPERSIST;
4388 4426                  if (native_arg == 0) {
4389 4427                          return (EINVAL);
4390 4428                  }
4391 4429                  ns = netstack_find_by_cred(crp);
4392 4430                  ASSERT(ns != NULL);
4393 4431                  ss = ns->netstack_str;
4394 4432                  ASSERT(ss != NULL);
4395 4433  
4396 4434                  if (native_arg == MUXID_ALL)
4397 4435                          error = munlinkall(stp, type, crp, rvalp, ss);
4398 4436                  else {
4399 4437                          mutex_enter(&muxifier);
4400 4438                          if (!(linkp = findlinks(stp, (int)arg, type, ss))) {
4401 4439                                  /* invalid user supplied index number */
4402 4440                                  mutex_exit(&muxifier);
4403 4441                                  netstack_rele(ss->ss_netstack);
4404 4442                                  return (EINVAL);
4405 4443                          }
4406 4444                          /* munlink drops the muxifier lock */
4407 4445                          error = munlink(stp, linkp, type, crp, rvalp, ss);
4408 4446                  }
4409 4447                  netstack_rele(ss->ss_netstack);
4410 4448                  return (error);
4411 4449          }
4412 4450  
4413 4451          case I_FLUSH:
4414 4452                  /*
4415 4453                   * send a flush message downstream
4416 4454                   * flush message can indicate
4417 4455                   * FLUSHR - flush read queue
4418 4456                   * FLUSHW - flush write queue
4419 4457                   * FLUSHRW - flush read/write queue
4420 4458                   */
4421 4459                  if (stp->sd_flag & STRHUP)
4422 4460                          return (ENXIO);
4423 4461                  if (arg & ~FLUSHRW)
4424 4462                          return (EINVAL);
4425 4463  
4426 4464                  for (;;) {
4427 4465                          if (putnextctl1(stp->sd_wrq, M_FLUSH, (int)arg)) {
4428 4466                                  break;
4429 4467                          }
4430 4468                          if (error = strwaitbuf(1, BPRI_HI)) {
4431 4469                                  return (error);
4432 4470                          }
4433 4471                  }
4434 4472  
4435 4473                  /*
4436 4474                   * Send down an unsupported ioctl and wait for the nack
4437 4475                   * in order to allow the M_FLUSH to propagate back
4438 4476                   * up to the stream head.
4439 4477                   * Replaces if (qready()) runqueues();
4440 4478                   */
4441 4479                  strioc.ic_cmd = -1;     /* The unsupported ioctl */
4442 4480                  strioc.ic_timout = 0;
4443 4481                  strioc.ic_len = 0;
4444 4482                  strioc.ic_dp = NULL;
4445 4483                  (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4446 4484                  *rvalp = 0;
4447 4485                  return (0);
4448 4486  
4449 4487          case I_FLUSHBAND:
4450 4488          {
4451 4489                  struct bandinfo binfo;
4452 4490  
4453 4491                  error = strcopyin((void *)arg, &binfo, sizeof (binfo),
4454 4492                      copyflag);
4455 4493                  if (error)
4456 4494                          return (error);
4457 4495                  if (stp->sd_flag & STRHUP)
4458 4496                          return (ENXIO);
4459 4497                  if (binfo.bi_flag & ~FLUSHRW)
4460 4498                          return (EINVAL);
4461 4499                  while (!(mp = allocb(2, BPRI_HI))) {
4462 4500                          if (error = strwaitbuf(2, BPRI_HI))
4463 4501                                  return (error);
4464 4502                  }
4465 4503                  mp->b_datap->db_type = M_FLUSH;
4466 4504                  *mp->b_wptr++ = binfo.bi_flag | FLUSHBAND;
4467 4505                  *mp->b_wptr++ = binfo.bi_pri;
4468 4506                  putnext(stp->sd_wrq, mp);
4469 4507                  /*
4470 4508                   * Send down an unsupported ioctl and wait for the nack
4471 4509                   * in order to allow the M_FLUSH to propagate back
4472 4510                   * up to the stream head.
4473 4511                   * Replaces if (qready()) runqueues();
4474 4512                   */
4475 4513                  strioc.ic_cmd = -1;     /* The unsupported ioctl */
4476 4514                  strioc.ic_timout = 0;
4477 4515                  strioc.ic_len = 0;
4478 4516                  strioc.ic_dp = NULL;
4479 4517                  (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4480 4518                  *rvalp = 0;
4481 4519                  return (0);
4482 4520          }
4483 4521  
4484 4522          case I_SRDOPT:
4485 4523                  /*
4486 4524                   * Set read options
4487 4525                   *
4488 4526                   * RNORM - default stream mode
4489 4527                   * RMSGN - message no discard
4490 4528                   * RMSGD - message discard
4491 4529                   * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
4492 4530                   * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
4493 4531                   * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
4494 4532                   */
4495 4533                  if (arg & ~(RMODEMASK | RPROTMASK))
4496 4534                          return (EINVAL);
4497 4535  
4498 4536                  if ((arg & (RMSGD|RMSGN)) == (RMSGD|RMSGN))
4499 4537                          return (EINVAL);
4500 4538  
4501 4539                  mutex_enter(&stp->sd_lock);
4502 4540                  switch (arg & RMODEMASK) {
4503 4541                  case RNORM:
4504 4542                          stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
4505 4543                          break;
4506 4544                  case RMSGD:
4507 4545                          stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGNODIS) |
4508 4546                              RD_MSGDIS;
4509 4547                          break;
4510 4548                  case RMSGN:
4511 4549                          stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGDIS) |
4512 4550                              RD_MSGNODIS;
4513 4551                          break;
4514 4552                  }
4515 4553  
4516 4554                  switch (arg & RPROTMASK) {
4517 4555                  case RPROTNORM:
4518 4556                          stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
4519 4557                          break;
4520 4558  
4521 4559                  case RPROTDAT:
4522 4560                          stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDIS) |
4523 4561                              RD_PROTDAT);
4524 4562                          break;
4525 4563  
4526 4564                  case RPROTDIS:
4527 4565                          stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDAT) |
4528 4566                              RD_PROTDIS);
4529 4567                          break;
4530 4568                  }
4531 4569                  mutex_exit(&stp->sd_lock);
4532 4570                  return (0);
4533 4571  
4534 4572          case I_GRDOPT:
4535 4573                  /*
4536 4574                   * Get read option and return the value
4537 4575                   * to spot pointed to by arg
4538 4576                   */
4539 4577          {
4540 4578                  int rdopt;
4541 4579  
4542 4580                  rdopt = ((stp->sd_read_opt & RD_MSGDIS) ? RMSGD :
4543 4581                      ((stp->sd_read_opt & RD_MSGNODIS) ? RMSGN : RNORM));
4544 4582                  rdopt |= ((stp->sd_read_opt & RD_PROTDAT) ? RPROTDAT :
4545 4583                      ((stp->sd_read_opt & RD_PROTDIS) ? RPROTDIS : RPROTNORM));
4546 4584  
4547 4585                  return (strcopyout(&rdopt, (void *)arg, sizeof (int),
4548 4586                      copyflag));
4549 4587          }
4550 4588  
4551 4589          case I_SERROPT:
4552 4590                  /*
4553 4591                   * Set error options
4554 4592                   *
4555 4593                   * RERRNORM - persistent read errors
4556 4594                   * RERRNONPERSIST - non-persistent read errors
4557 4595                   * WERRNORM - persistent write errors
4558 4596                   * WERRNONPERSIST - non-persistent write errors
4559 4597                   */
4560 4598                  if (arg & ~(RERRMASK | WERRMASK))
4561 4599                          return (EINVAL);
4562 4600  
4563 4601                  mutex_enter(&stp->sd_lock);
4564 4602                  switch (arg & RERRMASK) {
4565 4603                  case RERRNORM:
4566 4604                          stp->sd_flag &= ~STRDERRNONPERSIST;
4567 4605                          break;
4568 4606                  case RERRNONPERSIST:
4569 4607                          stp->sd_flag |= STRDERRNONPERSIST;
4570 4608                          break;
4571 4609                  }
4572 4610                  switch (arg & WERRMASK) {
4573 4611                  case WERRNORM:
4574 4612                          stp->sd_flag &= ~STWRERRNONPERSIST;
4575 4613                          break;
4576 4614                  case WERRNONPERSIST:
4577 4615                          stp->sd_flag |= STWRERRNONPERSIST;
4578 4616                          break;
4579 4617                  }
4580 4618                  mutex_exit(&stp->sd_lock);
4581 4619                  return (0);
4582 4620  
4583 4621          case I_GERROPT:
4584 4622                  /*
4585 4623                   * Get error option and return the value
4586 4624                   * to spot pointed to by arg
4587 4625                   */
4588 4626          {
4589 4627                  int erropt = 0;
4590 4628  
4591 4629                  erropt |= (stp->sd_flag & STRDERRNONPERSIST) ? RERRNONPERSIST :
4592 4630                      RERRNORM;
4593 4631                  erropt |= (stp->sd_flag & STWRERRNONPERSIST) ? WERRNONPERSIST :
4594 4632                      WERRNORM;
4595 4633                  return (strcopyout(&erropt, (void *)arg, sizeof (int),
4596 4634                      copyflag));
4597 4635          }
4598 4636  
4599 4637          case I_SETSIG:
4600 4638                  /*
4601 4639                   * Register the calling proc to receive the SIGPOLL
4602 4640                   * signal based on the events given in arg.  If
4603 4641                   * arg is zero, remove the proc from register list.
4604 4642                   */
4605 4643          {
4606 4644                  strsig_t *ssp, *pssp;
4607 4645                  struct pid *pidp;
4608 4646  
4609 4647                  pssp = NULL;
4610 4648                  pidp = curproc->p_pidp;
4611 4649                  /*
4612 4650                   * Hold sd_lock to prevent traversal of sd_siglist while
4613 4651                   * it is modified.
4614 4652                   */
4615 4653                  mutex_enter(&stp->sd_lock);
4616 4654                  for (ssp = stp->sd_siglist; ssp && (ssp->ss_pidp != pidp);
4617 4655                      pssp = ssp, ssp = ssp->ss_next)
4618 4656                          ;
4619 4657  
4620 4658                  if (arg) {
4621 4659                          if (arg & ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4622 4660                              S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4623 4661                                  mutex_exit(&stp->sd_lock);
4624 4662                                  return (EINVAL);
4625 4663                          }
4626 4664                          if ((arg & S_BANDURG) && !(arg & S_RDBAND)) {
4627 4665                                  mutex_exit(&stp->sd_lock);
4628 4666                                  return (EINVAL);
4629 4667                          }
4630 4668  
4631 4669                          /*
4632 4670                           * If proc not already registered, add it
4633 4671                           * to list.
4634 4672                           */
4635 4673                          if (!ssp) {
4636 4674                                  ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4637 4675                                  ssp->ss_pidp = pidp;
4638 4676                                  ssp->ss_pid = pidp->pid_id;
4639 4677                                  ssp->ss_next = NULL;
4640 4678                                  if (pssp)
4641 4679                                          pssp->ss_next = ssp;
4642 4680                                  else
4643 4681                                          stp->sd_siglist = ssp;
4644 4682                                  mutex_enter(&pidlock);
4645 4683                                  PID_HOLD(pidp);
4646 4684                                  mutex_exit(&pidlock);
4647 4685                          }
4648 4686  
4649 4687                          /*
4650 4688                           * Set events.
4651 4689                           */
4652 4690                          ssp->ss_events = (int)arg;
4653 4691                  } else {
4654 4692                          /*
4655 4693                           * Remove proc from register list.
4656 4694                           */
4657 4695                          if (ssp) {
4658 4696                                  mutex_enter(&pidlock);
4659 4697                                  PID_RELE(pidp);
4660 4698                                  mutex_exit(&pidlock);
4661 4699                                  if (pssp)
4662 4700                                          pssp->ss_next = ssp->ss_next;
4663 4701                                  else
4664 4702                                          stp->sd_siglist = ssp->ss_next;
4665 4703                                  kmem_free(ssp, sizeof (strsig_t));
4666 4704                          } else {
4667 4705                                  mutex_exit(&stp->sd_lock);
4668 4706                                  return (EINVAL);
4669 4707                          }
4670 4708                  }
4671 4709  
4672 4710                  /*
4673 4711                   * Recalculate OR of sig events.
4674 4712                   */
4675 4713                  stp->sd_sigflags = 0;
4676 4714                  for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4677 4715                          stp->sd_sigflags |= ssp->ss_events;
4678 4716                  mutex_exit(&stp->sd_lock);
4679 4717                  return (0);
4680 4718          }
4681 4719  
4682 4720          case I_GETSIG:
4683 4721                  /*
4684 4722                   * Return (in arg) the current registration of events
4685 4723                   * for which the calling proc is to be signaled.
4686 4724                   */
4687 4725          {
4688 4726                  struct strsig *ssp;
4689 4727                  struct pid  *pidp;
4690 4728  
4691 4729                  pidp = curproc->p_pidp;
4692 4730                  mutex_enter(&stp->sd_lock);
4693 4731                  for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4694 4732                          if (ssp->ss_pidp == pidp) {
4695 4733                                  error = strcopyout(&ssp->ss_events, (void *)arg,
4696 4734                                      sizeof (int), copyflag);
4697 4735                                  mutex_exit(&stp->sd_lock);
4698 4736                                  return (error);
4699 4737                          }
4700 4738                  mutex_exit(&stp->sd_lock);
4701 4739                  return (EINVAL);
4702 4740          }
4703 4741  
4704 4742          case I_ESETSIG:
4705 4743                  /*
4706 4744                   * Register the ss_pid to receive the SIGPOLL
4707 4745                   * signal based on the events is ss_events arg.  If
4708 4746                   * ss_events is zero, remove the proc from register list.
4709 4747                   */
4710 4748          {
4711 4749                  struct strsig *ssp, *pssp;
4712 4750                  struct proc *proc;
4713 4751                  struct pid  *pidp;
4714 4752                  pid_t pid;
4715 4753                  struct strsigset ss;
4716 4754  
4717 4755                  error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4718 4756                  if (error)
4719 4757                          return (error);
4720 4758  
4721 4759                  pid = ss.ss_pid;
4722 4760  
4723 4761                  if (ss.ss_events != 0) {
4724 4762                          /*
4725 4763                           * Permissions check by sending signal 0.
4726 4764                           * Note that when kill fails it does a set_errno
4727 4765                           * causing the system call to fail.
4728 4766                           */
4729 4767                          error = kill(pid, 0);
4730 4768                          if (error) {
4731 4769                                  return (error);
4732 4770                          }
4733 4771                  }
4734 4772                  mutex_enter(&pidlock);
4735 4773                  if (pid == 0)
4736 4774                          proc = curproc;
4737 4775                  else if (pid < 0)
4738 4776                          proc = pgfind(-pid);
4739 4777                  else
4740 4778                          proc = prfind(pid);
4741 4779                  if (proc == NULL) {
4742 4780                          mutex_exit(&pidlock);
4743 4781                          return (ESRCH);
4744 4782                  }
4745 4783                  if (pid < 0)
4746 4784                          pidp = proc->p_pgidp;
4747 4785                  else
4748 4786                          pidp = proc->p_pidp;
4749 4787                  ASSERT(pidp);
4750 4788                  /*
4751 4789                   * Get a hold on the pid structure while referencing it.
4752 4790                   * There is a separate PID_HOLD should it be inserted
4753 4791                   * in the list below.
4754 4792                   */
4755 4793                  PID_HOLD(pidp);
4756 4794                  mutex_exit(&pidlock);
4757 4795  
4758 4796                  pssp = NULL;
4759 4797                  /*
4760 4798                   * Hold sd_lock to prevent traversal of sd_siglist while
4761 4799                   * it is modified.
4762 4800                   */
4763 4801                  mutex_enter(&stp->sd_lock);
4764 4802                  for (ssp = stp->sd_siglist; ssp && (ssp->ss_pid != pid);
4765 4803                      pssp = ssp, ssp = ssp->ss_next)
4766 4804                          ;
4767 4805  
4768 4806                  if (ss.ss_events) {
4769 4807                          if (ss.ss_events &
4770 4808                              ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4771 4809                              S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4772 4810                                  mutex_exit(&stp->sd_lock);
4773 4811                                  mutex_enter(&pidlock);
4774 4812                                  PID_RELE(pidp);
4775 4813                                  mutex_exit(&pidlock);
4776 4814                                  return (EINVAL);
4777 4815                          }
4778 4816                          if ((ss.ss_events & S_BANDURG) &&
4779 4817                              !(ss.ss_events & S_RDBAND)) {
4780 4818                                  mutex_exit(&stp->sd_lock);
4781 4819                                  mutex_enter(&pidlock);
4782 4820                                  PID_RELE(pidp);
4783 4821                                  mutex_exit(&pidlock);
4784 4822                                  return (EINVAL);
4785 4823                          }
4786 4824  
4787 4825                          /*
4788 4826                           * If proc not already registered, add it
4789 4827                           * to list.
4790 4828                           */
4791 4829                          if (!ssp) {
4792 4830                                  ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4793 4831                                  ssp->ss_pidp = pidp;
4794 4832                                  ssp->ss_pid = pid;
4795 4833                                  ssp->ss_next = NULL;
4796 4834                                  if (pssp)
4797 4835                                          pssp->ss_next = ssp;
4798 4836                                  else
4799 4837                                          stp->sd_siglist = ssp;
4800 4838                                  mutex_enter(&pidlock);
4801 4839                                  PID_HOLD(pidp);
4802 4840                                  mutex_exit(&pidlock);
4803 4841                          }
4804 4842  
4805 4843                          /*
4806 4844                           * Set events.
4807 4845                           */
4808 4846                          ssp->ss_events = ss.ss_events;
4809 4847                  } else {
4810 4848                          /*
4811 4849                           * Remove proc from register list.
4812 4850                           */
4813 4851                          if (ssp) {
4814 4852                                  mutex_enter(&pidlock);
4815 4853                                  PID_RELE(pidp);
4816 4854                                  mutex_exit(&pidlock);
4817 4855                                  if (pssp)
4818 4856                                          pssp->ss_next = ssp->ss_next;
4819 4857                                  else
4820 4858                                          stp->sd_siglist = ssp->ss_next;
4821 4859                                  kmem_free(ssp, sizeof (strsig_t));
4822 4860                          } else {
4823 4861                                  mutex_exit(&stp->sd_lock);
4824 4862                                  mutex_enter(&pidlock);
4825 4863                                  PID_RELE(pidp);
4826 4864                                  mutex_exit(&pidlock);
4827 4865                                  return (EINVAL);
4828 4866                          }
4829 4867                  }
4830 4868  
4831 4869                  /*
4832 4870                   * Recalculate OR of sig events.
4833 4871                   */
4834 4872                  stp->sd_sigflags = 0;
4835 4873                  for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4836 4874                          stp->sd_sigflags |= ssp->ss_events;
4837 4875                  mutex_exit(&stp->sd_lock);
4838 4876                  mutex_enter(&pidlock);
4839 4877                  PID_RELE(pidp);
4840 4878                  mutex_exit(&pidlock);
4841 4879                  return (0);
4842 4880          }
4843 4881  
4844 4882          case I_EGETSIG:
4845 4883                  /*
4846 4884                   * Return (in arg) the current registration of events
4847 4885                   * for which the calling proc is to be signaled.
4848 4886                   */
4849 4887          {
4850 4888                  struct strsig *ssp;
4851 4889                  struct proc *proc;
4852 4890                  pid_t pid;
4853 4891                  struct pid  *pidp;
4854 4892                  struct strsigset ss;
4855 4893  
4856 4894                  error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4857 4895                  if (error)
4858 4896                          return (error);
4859 4897  
4860 4898                  pid = ss.ss_pid;
4861 4899                  mutex_enter(&pidlock);
4862 4900                  if (pid == 0)
4863 4901                          proc = curproc;
4864 4902                  else if (pid < 0)
4865 4903                          proc = pgfind(-pid);
4866 4904                  else
4867 4905                          proc = prfind(pid);
4868 4906                  if (proc == NULL) {
4869 4907                          mutex_exit(&pidlock);
4870 4908                          return (ESRCH);
4871 4909                  }
4872 4910                  if (pid < 0)
4873 4911                          pidp = proc->p_pgidp;
4874 4912                  else
4875 4913                          pidp = proc->p_pidp;
4876 4914  
4877 4915                  /* Prevent the pidp from being reassigned */
4878 4916                  PID_HOLD(pidp);
4879 4917                  mutex_exit(&pidlock);
4880 4918  
4881 4919                  mutex_enter(&stp->sd_lock);
4882 4920                  for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4883 4921                          if (ssp->ss_pid == pid) {
4884 4922                                  ss.ss_pid = ssp->ss_pid;
4885 4923                                  ss.ss_events = ssp->ss_events;
4886 4924                                  error = strcopyout(&ss, (void *)arg,
4887 4925                                      sizeof (struct strsigset), copyflag);
4888 4926                                  mutex_exit(&stp->sd_lock);
4889 4927                                  mutex_enter(&pidlock);
4890 4928                                  PID_RELE(pidp);
4891 4929                                  mutex_exit(&pidlock);
4892 4930                                  return (error);
4893 4931                          }
4894 4932                  mutex_exit(&stp->sd_lock);
4895 4933                  mutex_enter(&pidlock);
4896 4934                  PID_RELE(pidp);
4897 4935                  mutex_exit(&pidlock);
4898 4936                  return (EINVAL);
4899 4937          }
4900 4938  
4901 4939          case I_PEEK:
4902 4940          {
4903 4941                  STRUCT_DECL(strpeek, strpeek);
4904 4942                  size_t n;
4905 4943                  mblk_t *fmp, *tmp_mp = NULL;
4906 4944  
4907 4945                  STRUCT_INIT(strpeek, flag);
4908 4946  
4909 4947                  error = strcopyin((void *)arg, STRUCT_BUF(strpeek),
4910 4948                      STRUCT_SIZE(strpeek), copyflag);
4911 4949                  if (error)
4912 4950                          return (error);
4913 4951  
4914 4952                  mutex_enter(QLOCK(rdq));
4915 4953                  /*
4916 4954                   * Skip the invalid messages
4917 4955                   */
4918 4956                  for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
4919 4957                          if (mp->b_datap->db_type != M_SIG)
4920 4958                                  break;
4921 4959  
4922 4960                  /*
4923 4961                   * If user has requested to peek at a high priority message
4924 4962                   * and first message is not, return 0
4925 4963                   */
4926 4964                  if (mp != NULL) {
4927 4965                          if ((STRUCT_FGET(strpeek, flags) & RS_HIPRI) &&
4928 4966                              queclass(mp) == QNORM) {
4929 4967                                  *rvalp = 0;
4930 4968                                  mutex_exit(QLOCK(rdq));
4931 4969                                  return (0);
4932 4970                          }
4933 4971                  } else if (stp->sd_struiordq == NULL ||
4934 4972                      (STRUCT_FGET(strpeek, flags) & RS_HIPRI)) {
4935 4973                          /*
4936 4974                           * No mblks to look at at the streamhead and
4937 4975                           * 1). This isn't a synch stream or
4938 4976                           * 2). This is a synch stream but caller wants high
4939 4977                           *      priority messages which is not supported by
4940 4978                           *      the synch stream. (it only supports QNORM)
4941 4979                           */
4942 4980                          *rvalp = 0;
4943 4981                          mutex_exit(QLOCK(rdq));
4944 4982                          return (0);
4945 4983                  }
4946 4984  
4947 4985                  fmp = mp;
4948 4986  
4949 4987                  if (mp && mp->b_datap->db_type == M_PASSFP) {
4950 4988                          mutex_exit(QLOCK(rdq));
4951 4989                          return (EBADMSG);
4952 4990                  }
4953 4991  
4954 4992                  ASSERT(mp == NULL || mp->b_datap->db_type == M_PCPROTO ||
4955 4993                      mp->b_datap->db_type == M_PROTO ||
4956 4994                      mp->b_datap->db_type == M_DATA);
4957 4995  
4958 4996                  if (mp && mp->b_datap->db_type == M_PCPROTO) {
4959 4997                          STRUCT_FSET(strpeek, flags, RS_HIPRI);
4960 4998                  } else {
4961 4999                          STRUCT_FSET(strpeek, flags, 0);
4962 5000                  }
4963 5001  
4964 5002  
4965 5003                  if (mp && ((tmp_mp = dupmsg(mp)) == NULL)) {
4966 5004                          mutex_exit(QLOCK(rdq));
4967 5005                          return (ENOSR);
4968 5006                  }
4969 5007                  mutex_exit(QLOCK(rdq));
4970 5008  
4971 5009                  /*
4972 5010                   * set mp = tmp_mp, so that I_PEEK processing can continue.
4973 5011                   * tmp_mp is used to free the dup'd message.
4974 5012                   */
4975 5013                  mp = tmp_mp;
4976 5014  
4977 5015                  uio.uio_fmode = 0;
4978 5016                  uio.uio_extflg = UIO_COPY_CACHED;
4979 5017                  uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
4980 5018                      UIO_SYSSPACE;
4981 5019                  uio.uio_limit = 0;
4982 5020                  /*
4983 5021                   * First process PROTO blocks, if any.
4984 5022                   * If user doesn't want to get ctl info by setting maxlen <= 0,
4985 5023                   * then set len to -1/0 and skip control blocks part.
4986 5024                   */
4987 5025                  if (STRUCT_FGET(strpeek, ctlbuf.maxlen) < 0)
4988 5026                          STRUCT_FSET(strpeek, ctlbuf.len, -1);
4989 5027                  else if (STRUCT_FGET(strpeek, ctlbuf.maxlen) == 0)
4990 5028                          STRUCT_FSET(strpeek, ctlbuf.len, 0);
4991 5029                  else {
4992 5030                          int     ctl_part = 0;
4993 5031  
4994 5032                          iov.iov_base = STRUCT_FGETP(strpeek, ctlbuf.buf);
4995 5033                          iov.iov_len = STRUCT_FGET(strpeek, ctlbuf.maxlen);
4996 5034                          uio.uio_iov = &iov;
4997 5035                          uio.uio_resid = iov.iov_len;
4998 5036                          uio.uio_loffset = 0;
4999 5037                          uio.uio_iovcnt = 1;
5000 5038                          while (mp && mp->b_datap->db_type != M_DATA &&
5001 5039                              uio.uio_resid >= 0) {
5002 5040                                  ASSERT(STRUCT_FGET(strpeek, flags) == 0 ?
5003 5041                                      mp->b_datap->db_type == M_PROTO :
5004 5042                                      mp->b_datap->db_type == M_PCPROTO);
5005 5043  
5006 5044                                  if ((n = MIN(uio.uio_resid,
5007 5045                                      mp->b_wptr - mp->b_rptr)) != 0 &&
5008 5046                                      (error = uiomove((char *)mp->b_rptr, n,
5009 5047                                      UIO_READ, &uio)) != 0) {
5010 5048                                          freemsg(tmp_mp);
5011 5049                                          return (error);
5012 5050                                  }
5013 5051                                  ctl_part = 1;
5014 5052                                  mp = mp->b_cont;
5015 5053                          }
5016 5054                          /* No ctl message */
5017 5055                          if (ctl_part == 0)
5018 5056                                  STRUCT_FSET(strpeek, ctlbuf.len, -1);
5019 5057                          else
5020 5058                                  STRUCT_FSET(strpeek, ctlbuf.len,
5021 5059                                      STRUCT_FGET(strpeek, ctlbuf.maxlen) -
5022 5060                                      uio.uio_resid);
5023 5061                  }
5024 5062  
5025 5063                  /*
5026 5064                   * Now process DATA blocks, if any.
5027 5065                   * If user doesn't want to get data info by setting maxlen <= 0,
5028 5066                   * then set len to -1/0 and skip data blocks part.
5029 5067                   */
5030 5068                  if (STRUCT_FGET(strpeek, databuf.maxlen) < 0)
5031 5069                          STRUCT_FSET(strpeek, databuf.len, -1);
5032 5070                  else if (STRUCT_FGET(strpeek, databuf.maxlen) == 0)
5033 5071                          STRUCT_FSET(strpeek, databuf.len, 0);
5034 5072                  else {
5035 5073                          int     data_part = 0;
5036 5074  
5037 5075                          iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
5038 5076                          iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen);
5039 5077                          uio.uio_iov = &iov;
5040 5078                          uio.uio_resid = iov.iov_len;
5041 5079                          uio.uio_loffset = 0;
5042 5080                          uio.uio_iovcnt = 1;
5043 5081                          while (mp && uio.uio_resid) {
5044 5082                                  if (mp->b_datap->db_type == M_DATA) {
5045 5083                                          if ((n = MIN(uio.uio_resid,
5046 5084                                              mp->b_wptr - mp->b_rptr)) != 0 &&
5047 5085                                              (error = uiomove((char *)mp->b_rptr,
5048 5086                                              n, UIO_READ, &uio)) != 0) {
5049 5087                                                  freemsg(tmp_mp);
5050 5088                                                  return (error);
5051 5089                                          }
5052 5090                                          data_part = 1;
5053 5091                                  }
5054 5092                                  ASSERT(data_part == 0 ||
5055 5093                                      mp->b_datap->db_type == M_DATA);
5056 5094                                  mp = mp->b_cont;
5057 5095                          }
5058 5096                          /* No data message */
5059 5097                          if (data_part == 0)
5060 5098                                  STRUCT_FSET(strpeek, databuf.len, -1);
5061 5099                          else
5062 5100                                  STRUCT_FSET(strpeek, databuf.len,
5063 5101                                      STRUCT_FGET(strpeek, databuf.maxlen) -
5064 5102                                      uio.uio_resid);
5065 5103                  }
5066 5104                  freemsg(tmp_mp);
5067 5105  
5068 5106                  /*
5069 5107                   * It is a synch stream and user wants to get
5070 5108                   * data (maxlen > 0).
5071 5109                   * uio setup is done by the codes that process DATA
5072 5110                   * blocks above.
5073 5111                   */
5074 5112                  if ((fmp == NULL) && STRUCT_FGET(strpeek, databuf.maxlen) > 0) {
5075 5113                          infod_t infod;
5076 5114  
5077 5115                          infod.d_cmd = INFOD_COPYOUT;
5078 5116                          infod.d_res = 0;
5079 5117                          infod.d_uiop = &uio;
5080 5118                          error = infonext(rdq, &infod);
5081 5119                          if (error == EINVAL || error == EBUSY)
5082 5120                                  error = 0;
5083 5121                          if (error)
5084 5122                                  return (error);
5085 5123                          STRUCT_FSET(strpeek, databuf.len, STRUCT_FGET(strpeek,
5086 5124                              databuf.maxlen) - uio.uio_resid);
5087 5125                          if (STRUCT_FGET(strpeek, databuf.len) == 0) {
5088 5126                                  /*
5089 5127                                   * No data found by the infonext().
5090 5128                                   */
5091 5129                                  STRUCT_FSET(strpeek, databuf.len, -1);
5092 5130                          }
5093 5131                  }
5094 5132                  error = strcopyout(STRUCT_BUF(strpeek), (void *)arg,
5095 5133                      STRUCT_SIZE(strpeek), copyflag);
5096 5134                  if (error) {
5097 5135                          return (error);
5098 5136                  }
5099 5137                  /*
5100 5138                   * If there is no message retrieved, set return code to 0
5101 5139                   * otherwise, set it to 1.
5102 5140                   */
5103 5141                  if (STRUCT_FGET(strpeek, ctlbuf.len) == -1 &&
5104 5142                      STRUCT_FGET(strpeek, databuf.len) == -1)
5105 5143                          *rvalp = 0;
5106 5144                  else
5107 5145                          *rvalp = 1;
5108 5146                  return (0);
5109 5147          }
5110 5148  
5111 5149          case I_FDINSERT:
5112 5150          {
5113 5151                  STRUCT_DECL(strfdinsert, strfdinsert);
5114 5152                  struct file *resftp;
5115 5153                  struct stdata *resstp;
5116 5154                  t_uscalar_t     ival;
5117 5155                  ssize_t msgsize;
5118 5156                  struct strbuf mctl;
5119 5157  
5120 5158                  STRUCT_INIT(strfdinsert, flag);
5121 5159                  if (stp->sd_flag & STRHUP)
5122 5160                          return (ENXIO);
5123 5161                  /*
5124 5162                   * STRDERR, STWRERR and STPLEX tested above.
5125 5163                   */
5126 5164                  error = strcopyin((void *)arg, STRUCT_BUF(strfdinsert),
5127 5165                      STRUCT_SIZE(strfdinsert), copyflag);
5128 5166                  if (error)
5129 5167                          return (error);
5130 5168  
5131 5169                  if (STRUCT_FGET(strfdinsert, offset) < 0 ||
5132 5170                      (STRUCT_FGET(strfdinsert, offset) %
5133 5171                      sizeof (t_uscalar_t)) != 0)
5134 5172                          return (EINVAL);
5135 5173                  if ((resftp = getf(STRUCT_FGET(strfdinsert, fildes))) != NULL) {
5136 5174                          if ((resstp = resftp->f_vnode->v_stream) == NULL) {
5137 5175                                  releasef(STRUCT_FGET(strfdinsert, fildes));
5138 5176                                  return (EINVAL);
5139 5177                          }
5140 5178                  } else
5141 5179                          return (EINVAL);
5142 5180  
5143 5181                  mutex_enter(&resstp->sd_lock);
5144 5182                  if (resstp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
5145 5183                          error = strgeterr(resstp,
5146 5184                              STRDERR|STWRERR|STRHUP|STPLEX, 0);
5147 5185                          if (error != 0) {
5148 5186                                  mutex_exit(&resstp->sd_lock);
5149 5187                                  releasef(STRUCT_FGET(strfdinsert, fildes));
5150 5188                                  return (error);
5151 5189                          }
5152 5190                  }
5153 5191                  mutex_exit(&resstp->sd_lock);
5154 5192  
5155 5193  #ifdef  _ILP32
5156 5194                  {
5157 5195                          queue_t *q;
5158 5196                          queue_t *mate = NULL;
5159 5197  
5160 5198                          /* get read queue of stream terminus */
5161 5199                          claimstr(resstp->sd_wrq);
5162 5200                          for (q = resstp->sd_wrq->q_next; q->q_next != NULL;
5163 5201                              q = q->q_next)
5164 5202                                  if (!STRMATED(resstp) && STREAM(q) != resstp &&
5165 5203                                      mate == NULL) {
5166 5204                                          ASSERT(q->q_qinfo->qi_srvp);
5167 5205                                          ASSERT(_OTHERQ(q)->q_qinfo->qi_srvp);
5168 5206                                          claimstr(q);
5169 5207                                          mate = q;
5170 5208                                  }
5171 5209                          q = _RD(q);
5172 5210                          if (mate)
5173 5211                                  releasestr(mate);
5174 5212                          releasestr(resstp->sd_wrq);
5175 5213                          ival = (t_uscalar_t)q;
5176 5214                  }
5177 5215  #else
5178 5216                  ival = (t_uscalar_t)getminor(resftp->f_vnode->v_rdev);
5179 5217  #endif  /* _ILP32 */
5180 5218  
5181 5219                  if (STRUCT_FGET(strfdinsert, ctlbuf.len) <
5182 5220                      STRUCT_FGET(strfdinsert, offset) + sizeof (t_uscalar_t)) {
5183 5221                          releasef(STRUCT_FGET(strfdinsert, fildes));
5184 5222                          return (EINVAL);
5185 5223                  }
5186 5224  
5187 5225                  /*
5188 5226                   * Check for legal flag value.
5189 5227                   */
5190 5228                  if (STRUCT_FGET(strfdinsert, flags) & ~RS_HIPRI) {
5191 5229                          releasef(STRUCT_FGET(strfdinsert, fildes));
5192 5230                          return (EINVAL);
5193 5231                  }
5194 5232  
5195 5233                  /* get these values from those cached in the stream head */
5196 5234                  mutex_enter(QLOCK(stp->sd_wrq));
5197 5235                  rmin = stp->sd_qn_minpsz;
5198 5236                  rmax = stp->sd_qn_maxpsz;
5199 5237                  mutex_exit(QLOCK(stp->sd_wrq));
5200 5238  
5201 5239                  /*
5202 5240                   * Make sure ctl and data sizes together fall within
5203 5241                   * the limits of the max and min receive packet sizes
5204 5242                   * and do not exceed system limit.  A negative data
5205 5243                   * length means that no data part is to be sent.
5206 5244                   */
5207 5245                  ASSERT((rmax >= 0) || (rmax == INFPSZ));
5208 5246                  if (rmax == 0) {
5209 5247                          releasef(STRUCT_FGET(strfdinsert, fildes));
5210 5248                          return (ERANGE);
5211 5249                  }
5212 5250                  if ((msgsize = STRUCT_FGET(strfdinsert, databuf.len)) < 0)
5213 5251                          msgsize = 0;
5214 5252                  if ((msgsize < rmin) ||
5215 5253                      ((msgsize > rmax) && (rmax != INFPSZ)) ||
5216 5254                      (STRUCT_FGET(strfdinsert, ctlbuf.len) > strctlsz)) {
5217 5255                          releasef(STRUCT_FGET(strfdinsert, fildes));
5218 5256                          return (ERANGE);
5219 5257                  }
5220 5258  
5221 5259                  mutex_enter(&stp->sd_lock);
5222 5260                  while (!(STRUCT_FGET(strfdinsert, flags) & RS_HIPRI) &&
5223 5261                      !canputnext(stp->sd_wrq)) {
5224 5262                          if ((error = strwaitq(stp, WRITEWAIT, (ssize_t)0,
5225 5263                              flag, -1, &done)) != 0 || done) {
5226 5264                                  mutex_exit(&stp->sd_lock);
5227 5265                                  releasef(STRUCT_FGET(strfdinsert, fildes));
5228 5266                                  return (error);
5229 5267                          }
5230 5268                          if ((error = i_straccess(stp, access)) != 0) {
5231 5269                                  mutex_exit(&stp->sd_lock);
5232 5270                                  releasef(
5233 5271                                      STRUCT_FGET(strfdinsert, fildes));
5234 5272                                  return (error);
5235 5273                          }
5236 5274                  }
5237 5275                  mutex_exit(&stp->sd_lock);
5238 5276  
5239 5277                  /*
5240 5278                   * Copy strfdinsert.ctlbuf into native form of
5241 5279                   * ctlbuf to pass down into strmakemsg().
5242 5280                   */
5243 5281                  mctl.maxlen = STRUCT_FGET(strfdinsert, ctlbuf.maxlen);
5244 5282                  mctl.len = STRUCT_FGET(strfdinsert, ctlbuf.len);
5245 5283                  mctl.buf = STRUCT_FGETP(strfdinsert, ctlbuf.buf);
5246 5284  
5247 5285                  iov.iov_base = STRUCT_FGETP(strfdinsert, databuf.buf);
5248 5286                  iov.iov_len = STRUCT_FGET(strfdinsert, databuf.len);
5249 5287                  uio.uio_iov = &iov;
5250 5288                  uio.uio_iovcnt = 1;
5251 5289                  uio.uio_loffset = 0;
5252 5290                  uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5253 5291                      UIO_SYSSPACE;
5254 5292                  uio.uio_fmode = 0;
5255 5293                  uio.uio_extflg = UIO_COPY_CACHED;
5256 5294                  uio.uio_resid = iov.iov_len;
5257 5295                  if ((error = strmakemsg(&mctl,
5258 5296                      &msgsize, &uio, stp,
5259 5297                      STRUCT_FGET(strfdinsert, flags), &mp)) != 0 || !mp) {
5260 5298                          STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5261 5299                          releasef(STRUCT_FGET(strfdinsert, fildes));
5262 5300                          return (error);
5263 5301                  }
5264 5302  
5265 5303                  STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5266 5304  
5267 5305                  /*
5268 5306                   * Place the possibly reencoded queue pointer 'offset' bytes
5269 5307                   * from the start of the control portion of the message.
5270 5308                   */
5271 5309                  *((t_uscalar_t *)(mp->b_rptr +
5272 5310                      STRUCT_FGET(strfdinsert, offset))) = ival;
5273 5311  
5274 5312                  /*
5275 5313                   * Put message downstream.
5276 5314                   */
5277 5315                  stream_willservice(stp);
5278 5316                  putnext(stp->sd_wrq, mp);
5279 5317                  stream_runservice(stp);
5280 5318                  releasef(STRUCT_FGET(strfdinsert, fildes));
5281 5319                  return (error);
5282 5320          }
5283 5321  
5284 5322          case I_SENDFD:
5285 5323          {
5286 5324                  struct file *fp;
5287 5325  
5288 5326                  if ((fp = getf((int)arg)) == NULL)
5289 5327                          return (EBADF);
5290 5328                  error = do_sendfp(stp, fp, crp);
5291 5329                  if (auditing) {
5292 5330                          audit_fdsend((int)arg, fp, error);
5293 5331                  }
5294 5332                  releasef((int)arg);
5295 5333                  return (error);
5296 5334          }
5297 5335  
5298 5336          case I_RECVFD:
5299 5337          case I_E_RECVFD:
5300 5338          {
5301 5339                  struct k_strrecvfd *srf;
5302 5340                  int i, fd;
5303 5341  
5304 5342                  mutex_enter(&stp->sd_lock);
5305 5343                  while (!(mp = getq(rdq))) {
5306 5344                          if (stp->sd_flag & (STRHUP|STREOF)) {
5307 5345                                  mutex_exit(&stp->sd_lock);
5308 5346                                  return (ENXIO);
5309 5347                          }
5310 5348                          if ((error = strwaitq(stp, GETWAIT, (ssize_t)0,
5311 5349                              flag, -1, &done)) != 0 || done) {
5312 5350                                  mutex_exit(&stp->sd_lock);
5313 5351                                  return (error);
5314 5352                          }
5315 5353                          if ((error = i_straccess(stp, access)) != 0) {
5316 5354                                  mutex_exit(&stp->sd_lock);
5317 5355                                  return (error);
5318 5356                          }
5319 5357                  }
5320 5358                  if (mp->b_datap->db_type != M_PASSFP) {
5321 5359                          putback(stp, rdq, mp, mp->b_band);
5322 5360                          mutex_exit(&stp->sd_lock);
5323 5361                          return (EBADMSG);
5324 5362                  }
5325 5363                  mutex_exit(&stp->sd_lock);
5326 5364  
5327 5365                  srf = (struct k_strrecvfd *)mp->b_rptr;
5328 5366                  if ((fd = ufalloc(0)) == -1) {
5329 5367                          mutex_enter(&stp->sd_lock);
5330 5368                          putback(stp, rdq, mp, mp->b_band);
5331 5369                          mutex_exit(&stp->sd_lock);
5332 5370                          return (EMFILE);
5333 5371                  }
5334 5372                  if (cmd == I_RECVFD) {
5335 5373                          struct o_strrecvfd      ostrfd;
5336 5374  
5337 5375                          /* check to see if uid/gid values are too large. */
5338 5376  
5339 5377                          if (srf->uid > (o_uid_t)USHRT_MAX ||
5340 5378                              srf->gid > (o_gid_t)USHRT_MAX) {
5341 5379                                  mutex_enter(&stp->sd_lock);
5342 5380                                  putback(stp, rdq, mp, mp->b_band);
5343 5381                                  mutex_exit(&stp->sd_lock);
5344 5382                                  setf(fd, NULL); /* release fd entry */
5345 5383                                  return (EOVERFLOW);
5346 5384                          }
5347 5385  
5348 5386                          ostrfd.fd = fd;
5349 5387                          ostrfd.uid = (o_uid_t)srf->uid;
5350 5388                          ostrfd.gid = (o_gid_t)srf->gid;
5351 5389  
5352 5390                          /* Null the filler bits */
5353 5391                          for (i = 0; i < 8; i++)
5354 5392                                  ostrfd.fill[i] = 0;
5355 5393  
5356 5394                          error = strcopyout(&ostrfd, (void *)arg,
5357 5395                              sizeof (struct o_strrecvfd), copyflag);
5358 5396                  } else {                /* I_E_RECVFD */
5359 5397                          struct strrecvfd        strfd;
5360 5398  
5361 5399                          strfd.fd = fd;
5362 5400                          strfd.uid = srf->uid;
5363 5401                          strfd.gid = srf->gid;
5364 5402  
5365 5403                          /* null the filler bits */
5366 5404                          for (i = 0; i < 8; i++)
5367 5405                                  strfd.fill[i] = 0;
5368 5406  
5369 5407                          error = strcopyout(&strfd, (void *)arg,
5370 5408                              sizeof (struct strrecvfd), copyflag);
5371 5409                  }
5372 5410  
5373 5411                  if (error) {
5374 5412                          setf(fd, NULL); /* release fd entry */
5375 5413                          mutex_enter(&stp->sd_lock);
5376 5414                          putback(stp, rdq, mp, mp->b_band);
5377 5415                          mutex_exit(&stp->sd_lock);
5378 5416                          return (error);
5379 5417                  }
5380 5418                  if (auditing) {
5381 5419                          audit_fdrecv(fd, srf->fp);
5382 5420                  }
5383 5421  
5384 5422                  /*
5385 5423                   * Always increment f_count since the freemsg() below will
5386 5424                   * always call free_passfp() which performs a closef().
5387 5425                   */
5388 5426                  mutex_enter(&srf->fp->f_tlock);
5389 5427                  srf->fp->f_count++;
5390 5428                  mutex_exit(&srf->fp->f_tlock);
5391 5429                  setf(fd, srf->fp);
5392 5430                  freemsg(mp);
5393 5431                  return (0);
5394 5432          }
5395 5433  
5396 5434          case I_SWROPT:
5397 5435                  /*
5398 5436                   * Set/clear the write options. arg is a bit
5399 5437                   * mask with any of the following bits set...
5400 5438                   *      SNDZERO - send zero length message
5401 5439                   *      SNDPIPE - send sigpipe to process if
5402 5440                   *              sd_werror is set and process is
5403 5441                   *              doing a write or putmsg.
5404 5442                   * The new stream head write options should reflect
5405 5443                   * what is in arg.
5406 5444                   */
5407 5445                  if (arg & ~(SNDZERO|SNDPIPE))
5408 5446                          return (EINVAL);
5409 5447  
5410 5448                  mutex_enter(&stp->sd_lock);
5411 5449                  stp->sd_wput_opt &= ~(SW_SIGPIPE|SW_SNDZERO);
5412 5450                  if (arg & SNDZERO)
5413 5451                          stp->sd_wput_opt |= SW_SNDZERO;
5414 5452                  if (arg & SNDPIPE)
5415 5453                          stp->sd_wput_opt |= SW_SIGPIPE;
5416 5454                  mutex_exit(&stp->sd_lock);
5417 5455                  return (0);
5418 5456  
5419 5457          case I_GWROPT:
5420 5458          {
5421 5459                  int wropt = 0;
5422 5460  
5423 5461                  if (stp->sd_wput_opt & SW_SNDZERO)
5424 5462                          wropt |= SNDZERO;
5425 5463                  if (stp->sd_wput_opt & SW_SIGPIPE)
5426 5464                          wropt |= SNDPIPE;
5427 5465                  return (strcopyout(&wropt, (void *)arg, sizeof (wropt),
5428 5466                      copyflag));
5429 5467          }
5430 5468  
5431 5469          case I_LIST:
5432 5470                  /*
5433 5471                   * Returns all the modules found on this stream,
5434 5472                   * upto the driver. If argument is NULL, return the
5435 5473                   * number of modules (including driver). If argument
5436 5474                   * is not NULL, copy the names into the structure
5437 5475                   * provided.
5438 5476                   */
5439 5477  
5440 5478          {
5441 5479                  queue_t *q;
5442 5480                  char *qname;
5443 5481                  int i, nmods;
5444 5482                  struct str_mlist *mlist;
5445 5483                  STRUCT_DECL(str_list, strlist);
5446 5484  
5447 5485                  if (arg == NULL) { /* Return number of modules plus driver */
5448 5486                          if (stp->sd_vnode->v_type == VFIFO)
5449 5487                                  *rvalp = stp->sd_pushcnt;
5450 5488                          else
5451 5489                                  *rvalp = stp->sd_pushcnt + 1;
5452 5490                          return (0);
5453 5491                  }
5454 5492  
5455 5493                  STRUCT_INIT(strlist, flag);
5456 5494  
5457 5495                  error = strcopyin((void *)arg, STRUCT_BUF(strlist),
5458 5496                      STRUCT_SIZE(strlist), copyflag);
5459 5497                  if (error != 0)
5460 5498                          return (error);
5461 5499  
5462 5500                  mlist = STRUCT_FGETP(strlist, sl_modlist);
5463 5501                  nmods = STRUCT_FGET(strlist, sl_nmods);
5464 5502                  if (nmods <= 0)
5465 5503                          return (EINVAL);
5466 5504  
5467 5505                  claimstr(stp->sd_wrq);
5468 5506                  q = stp->sd_wrq;
5469 5507                  for (i = 0; i < nmods && _SAMESTR(q); i++, q = q->q_next) {
5470 5508                          qname = Q2NAME(q->q_next);
5471 5509                          error = strcopyout(qname, &mlist[i], strlen(qname) + 1,
5472 5510                              copyflag);
5473 5511                          if (error != 0) {
5474 5512                                  releasestr(stp->sd_wrq);
5475 5513                                  return (error);
5476 5514                          }
5477 5515                  }
5478 5516                  releasestr(stp->sd_wrq);
5479 5517                  return (strcopyout(&i, (void *)arg, sizeof (int), copyflag));
5480 5518          }
5481 5519  
5482 5520          case I_CKBAND:
5483 5521          {
5484 5522                  queue_t *q;
5485 5523                  qband_t *qbp;
5486 5524  
5487 5525                  if ((arg < 0) || (arg >= NBAND))
5488 5526                          return (EINVAL);
5489 5527                  q = _RD(stp->sd_wrq);
5490 5528                  mutex_enter(QLOCK(q));
5491 5529                  if (arg > (int)q->q_nband) {
5492 5530                          *rvalp = 0;
5493 5531                  } else {
5494 5532                          if (arg == 0) {
5495 5533                                  if (q->q_first)
5496 5534                                          *rvalp = 1;
5497 5535                                  else
5498 5536                                          *rvalp = 0;
5499 5537                          } else {
5500 5538                                  qbp = q->q_bandp;
5501 5539                                  while (--arg > 0)
5502 5540                                          qbp = qbp->qb_next;
5503 5541                                  if (qbp->qb_first)
5504 5542                                          *rvalp = 1;
5505 5543                                  else
5506 5544                                          *rvalp = 0;
5507 5545                          }
5508 5546                  }
5509 5547                  mutex_exit(QLOCK(q));
5510 5548                  return (0);
5511 5549          }
5512 5550  
5513 5551          case I_GETBAND:
5514 5552          {
5515 5553                  int intpri;
5516 5554                  queue_t *q;
5517 5555  
5518 5556                  q = _RD(stp->sd_wrq);
5519 5557                  mutex_enter(QLOCK(q));
5520 5558                  mp = q->q_first;
5521 5559                  if (!mp) {
5522 5560                          mutex_exit(QLOCK(q));
5523 5561                          return (ENODATA);
5524 5562                  }
5525 5563                  intpri = (int)mp->b_band;
5526 5564                  error = strcopyout(&intpri, (void *)arg, sizeof (int),
5527 5565                      copyflag);
5528 5566                  mutex_exit(QLOCK(q));
5529 5567                  return (error);
5530 5568          }
5531 5569  
5532 5570          case I_ATMARK:
5533 5571          {
5534 5572                  queue_t *q;
5535 5573  
5536 5574                  if (arg & ~(ANYMARK|LASTMARK))
5537 5575                          return (EINVAL);
5538 5576                  q = _RD(stp->sd_wrq);
5539 5577                  mutex_enter(&stp->sd_lock);
5540 5578                  if ((stp->sd_flag & STRATMARK) && (arg == ANYMARK)) {
5541 5579                          *rvalp = 1;
5542 5580                  } else {
5543 5581                          mutex_enter(QLOCK(q));
5544 5582                          mp = q->q_first;
5545 5583  
5546 5584                          if (mp == NULL)
5547 5585                                  *rvalp = 0;
5548 5586                          else if ((arg == ANYMARK) && (mp->b_flag & MSGMARK))
5549 5587                                  *rvalp = 1;
5550 5588                          else if ((arg == LASTMARK) && (mp == stp->sd_mark))
5551 5589                                  *rvalp = 1;
5552 5590                          else
5553 5591                                  *rvalp = 0;
5554 5592                          mutex_exit(QLOCK(q));
5555 5593                  }
5556 5594                  mutex_exit(&stp->sd_lock);
5557 5595                  return (0);
5558 5596          }
5559 5597  
5560 5598          case I_CANPUT:
5561 5599          {
5562 5600                  char band;
5563 5601  
5564 5602                  if ((arg < 0) || (arg >= NBAND))
5565 5603                          return (EINVAL);
5566 5604                  band = (char)arg;
5567 5605                  *rvalp = bcanputnext(stp->sd_wrq, band);
5568 5606                  return (0);
5569 5607          }
5570 5608  
5571 5609          case I_SETCLTIME:
5572 5610          {
5573 5611                  int closetime;
5574 5612  
5575 5613                  error = strcopyin((void *)arg, &closetime, sizeof (int),
5576 5614                      copyflag);
5577 5615                  if (error)
5578 5616                          return (error);
5579 5617                  if (closetime < 0)
5580 5618                          return (EINVAL);
5581 5619  
5582 5620                  stp->sd_closetime = closetime;
5583 5621                  return (0);
5584 5622          }
5585 5623  
5586 5624          case I_GETCLTIME:
5587 5625          {
5588 5626                  int closetime;
5589 5627  
5590 5628                  closetime = stp->sd_closetime;
5591 5629                  return (strcopyout(&closetime, (void *)arg, sizeof (int),
5592 5630                      copyflag));
5593 5631          }
5594 5632  
5595 5633          case TIOCGSID:
5596 5634          {
5597 5635                  pid_t sid;
5598 5636  
5599 5637                  mutex_enter(&stp->sd_lock);
5600 5638                  if (stp->sd_sidp == NULL) {
5601 5639                          mutex_exit(&stp->sd_lock);
5602 5640                          return (ENOTTY);
5603 5641                  }
5604 5642                  sid = stp->sd_sidp->pid_id;
5605 5643                  mutex_exit(&stp->sd_lock);
5606 5644                  return (strcopyout(&sid, (void *)arg, sizeof (pid_t),
5607 5645                      copyflag));
5608 5646          }
5609 5647  
5610 5648          case TIOCSPGRP:
5611 5649          {
5612 5650                  pid_t pgrp;
5613 5651                  proc_t *q;
5614 5652                  pid_t   sid, fg_pgid, bg_pgid;
5615 5653  
5616 5654                  if (error = strcopyin((void *)arg, &pgrp, sizeof (pid_t),
5617 5655                      copyflag))
5618 5656                          return (error);
5619 5657                  mutex_enter(&stp->sd_lock);
5620 5658                  mutex_enter(&pidlock);
5621 5659                  if (stp->sd_sidp != ttoproc(curthread)->p_sessp->s_sidp) {
5622 5660                          mutex_exit(&pidlock);
5623 5661                          mutex_exit(&stp->sd_lock);
5624 5662                          return (ENOTTY);
5625 5663                  }
5626 5664                  if (pgrp == stp->sd_pgidp->pid_id) {
5627 5665                          mutex_exit(&pidlock);
5628 5666                          mutex_exit(&stp->sd_lock);
5629 5667                          return (0);
5630 5668                  }
5631 5669                  if (pgrp <= 0 || pgrp >= maxpid) {
5632 5670                          mutex_exit(&pidlock);
5633 5671                          mutex_exit(&stp->sd_lock);
5634 5672                          return (EINVAL);
5635 5673                  }
5636 5674                  if ((q = pgfind(pgrp)) == NULL ||
5637 5675                      q->p_sessp != ttoproc(curthread)->p_sessp) {
5638 5676                          mutex_exit(&pidlock);
5639 5677                          mutex_exit(&stp->sd_lock);
5640 5678                          return (EPERM);
5641 5679                  }
5642 5680                  sid = stp->sd_sidp->pid_id;
5643 5681                  fg_pgid = q->p_pgrp;
5644 5682                  bg_pgid = stp->sd_pgidp->pid_id;
5645 5683                  CL_SET_PROCESS_GROUP(curthread, sid, bg_pgid, fg_pgid);
5646 5684                  PID_RELE(stp->sd_pgidp);
5647 5685                  ctty_clear_sighuped();
5648 5686                  stp->sd_pgidp = q->p_pgidp;
5649 5687                  PID_HOLD(stp->sd_pgidp);
5650 5688                  mutex_exit(&pidlock);
5651 5689                  mutex_exit(&stp->sd_lock);
5652 5690                  return (0);
5653 5691          }
5654 5692  
5655 5693          case TIOCGPGRP:
5656 5694          {
5657 5695                  pid_t pgrp;
5658 5696  
5659 5697                  mutex_enter(&stp->sd_lock);
5660 5698                  if (stp->sd_sidp == NULL) {
5661 5699                          mutex_exit(&stp->sd_lock);
5662 5700                          return (ENOTTY);
5663 5701                  }
5664 5702                  pgrp = stp->sd_pgidp->pid_id;
5665 5703                  mutex_exit(&stp->sd_lock);
5666 5704                  return (strcopyout(&pgrp, (void *)arg, sizeof (pid_t),
5667 5705                      copyflag));
5668 5706          }
5669 5707  
5670 5708          case TIOCSCTTY:
5671 5709          {
5672 5710                  return (strctty(stp));
5673 5711          }
5674 5712  
5675 5713          case TIOCNOTTY:
5676 5714          {
5677 5715                  /* freectty() always assumes curproc. */
5678 5716                  if (freectty(B_FALSE) != 0)
5679 5717                          return (0);
5680 5718                  return (ENOTTY);
5681 5719          }
5682 5720  
5683 5721          case FIONBIO:
5684 5722          case FIOASYNC:
5685 5723                  return (0);     /* handled by the upper layer */
5686 5724          }
5687 5725  }
5688 5726  
5689 5727  /*
5690 5728   * Custom free routine used for M_PASSFP messages.
5691 5729   */
5692 5730  static void
5693 5731  free_passfp(struct k_strrecvfd *srf)
5694 5732  {
5695 5733          (void) closef(srf->fp);
5696 5734          kmem_free(srf, sizeof (struct k_strrecvfd) + sizeof (frtn_t));
5697 5735  }
5698 5736  
5699 5737  /* ARGSUSED */
5700 5738  int
5701 5739  do_sendfp(struct stdata *stp, struct file *fp, struct cred *cr)
5702 5740  {
5703 5741          queue_t *qp, *nextqp;
5704 5742          struct k_strrecvfd *srf;
5705 5743          mblk_t *mp;
5706 5744          frtn_t *frtnp;
5707 5745          size_t bufsize;
5708 5746          queue_t *mate = NULL;
5709 5747          syncq_t *sq = NULL;
5710 5748          int retval = 0;
5711 5749  
5712 5750          if (stp->sd_flag & STRHUP)
5713 5751                  return (ENXIO);
5714 5752  
5715 5753          claimstr(stp->sd_wrq);
5716 5754  
5717 5755          /* Fastpath, we have a pipe, and we are already mated, use it. */
5718 5756          if (STRMATED(stp)) {
5719 5757                  qp = _RD(stp->sd_mate->sd_wrq);
5720 5758                  claimstr(qp);
5721 5759                  mate = qp;
5722 5760          } else { /* Not already mated. */
5723 5761  
5724 5762                  /*
5725 5763                   * Walk the stream to the end of this one.
5726 5764                   * assumes that the claimstr() will prevent
5727 5765                   * plumbing between the stream head and the
5728 5766                   * driver from changing
5729 5767                   */
5730 5768                  qp = stp->sd_wrq;
5731 5769  
5732 5770                  /*
5733 5771                   * Loop until we reach the end of this stream.
5734 5772                   * On completion, qp points to the write queue
5735 5773                   * at the end of the stream, or the read queue
5736 5774                   * at the stream head if this is a fifo.
5737 5775                   */
5738 5776                  while (((qp = qp->q_next) != NULL) && _SAMESTR(qp))
5739 5777                          ;
5740 5778  
5741 5779                  /*
5742 5780                   * Just in case we get a q_next which is NULL, but
5743 5781                   * not at the end of the stream.  This is actually
5744 5782                   * broken, so we set an assert to catch it in
5745 5783                   * debug, and set an error and return if not debug.
5746 5784                   */
5747 5785                  ASSERT(qp);
5748 5786                  if (qp == NULL) {
5749 5787                          releasestr(stp->sd_wrq);
5750 5788                          return (EINVAL);
5751 5789                  }
5752 5790  
5753 5791                  /*
5754 5792                   * Enter the syncq for the driver, so (hopefully)
5755 5793                   * the queue values will not change on us.
5756 5794                   * XXXX - This will only prevent the race IFF only
5757 5795                   *   the write side modifies the q_next member, and
5758 5796                   *   the put procedure is protected by at least
5759 5797                   *   MT_PERQ.
5760 5798                   */
5761 5799                  if ((sq = qp->q_syncq) != NULL)
5762 5800                          entersq(sq, SQ_PUT);
5763 5801  
5764 5802                  /* Now get the q_next value from this qp. */
5765 5803                  nextqp = qp->q_next;
5766 5804  
5767 5805                  /*
5768 5806                   * If nextqp exists and the other stream is different
5769 5807                   * from this one claim the stream, set the mate, and
5770 5808                   * get the read queue at the stream head of the other
5771 5809                   * stream.  Assumes that nextqp was at least valid when
5772 5810                   * we got it.  Hopefully the entersq of the driver
5773 5811                   * will prevent it from changing on us.
5774 5812                   */
5775 5813                  if ((nextqp != NULL) && (STREAM(nextqp) != stp)) {
5776 5814                          ASSERT(qp->q_qinfo->qi_srvp);
5777 5815                          ASSERT(_OTHERQ(qp)->q_qinfo->qi_srvp);
5778 5816                          ASSERT(_OTHERQ(qp->q_next)->q_qinfo->qi_srvp);
5779 5817                          claimstr(nextqp);
5780 5818  
5781 5819                          /* Make sure we still have a q_next */
5782 5820                          if (nextqp != qp->q_next) {
5783 5821                                  releasestr(stp->sd_wrq);
5784 5822                                  releasestr(nextqp);
5785 5823                                  return (EINVAL);
5786 5824                          }
5787 5825  
5788 5826                          qp = _RD(STREAM(nextqp)->sd_wrq);
5789 5827                          mate = qp;
5790 5828                  }
5791 5829                  /* If we entered the synq above, leave it. */
5792 5830                  if (sq != NULL)
5793 5831                          leavesq(sq, SQ_PUT);
5794 5832          } /*  STRMATED(STP)  */
5795 5833  
5796 5834          /* XXX prevents substitution of the ops vector */
5797 5835          if (qp->q_qinfo != &strdata && qp->q_qinfo != &fifo_strdata) {
5798 5836                  retval = EINVAL;
5799 5837                  goto out;
5800 5838          }
5801 5839  
5802 5840          if (qp->q_flag & QFULL) {
5803 5841                  retval = EAGAIN;
5804 5842                  goto out;
5805 5843          }
5806 5844  
5807 5845          /*
5808 5846           * Since M_PASSFP messages include a file descriptor, we use
5809 5847           * esballoc() and specify a custom free routine (free_passfp()) that
5810 5848           * will close the descriptor as part of freeing the message.  For
5811 5849           * convenience, we stash the frtn_t right after the data block.
5812 5850           */
5813 5851          bufsize = sizeof (struct k_strrecvfd) + sizeof (frtn_t);
5814 5852          srf = kmem_alloc(bufsize, KM_NOSLEEP);
5815 5853          if (srf == NULL) {
5816 5854                  retval = EAGAIN;
5817 5855                  goto out;
5818 5856          }
5819 5857  
5820 5858          frtnp = (frtn_t *)(srf + 1);
5821 5859          frtnp->free_arg = (caddr_t)srf;
5822 5860          frtnp->free_func = free_passfp;
5823 5861  
5824 5862          mp = esballoc((uchar_t *)srf, bufsize, BPRI_MED, frtnp);
5825 5863          if (mp == NULL) {
5826 5864                  kmem_free(srf, bufsize);
5827 5865                  retval = EAGAIN;
5828 5866                  goto out;
5829 5867          }
5830 5868          mp->b_wptr += sizeof (struct k_strrecvfd);
5831 5869          mp->b_datap->db_type = M_PASSFP;
5832 5870  
5833 5871          srf->fp = fp;
5834 5872          srf->uid = crgetuid(curthread->t_cred);
5835 5873          srf->gid = crgetgid(curthread->t_cred);
5836 5874          mutex_enter(&fp->f_tlock);
5837 5875          fp->f_count++;
5838 5876          mutex_exit(&fp->f_tlock);
5839 5877  
5840 5878          put(qp, mp);
5841 5879  out:
5842 5880          releasestr(stp->sd_wrq);
5843 5881          if (mate)
5844 5882                  releasestr(mate);
5845 5883          return (retval);
5846 5884  }
5847 5885  
5848 5886  /*
5849 5887   * Send an ioctl message downstream and wait for acknowledgement.
5850 5888   * flags may be set to either U_TO_K or K_TO_K and a combination
5851 5889   * of STR_NOERROR or STR_NOSIG
5852 5890   * STR_NOSIG: Signals are essentially ignored or held and have
5853 5891   *      no effect for the duration of the call.
5854 5892   * STR_NOERROR: Ignores stream head read, write and hup errors.
5855 5893   *      Additionally, if an existing ioctl times out, it is assumed
5856 5894   *      lost and and this ioctl will continue as if the previous ioctl had
5857 5895   *      finished.  ETIME may be returned if this ioctl times out (i.e.
5858 5896   *      ic_timout is not INFTIM).  Non-stream head errors may be returned if
5859 5897   *      the ioc_error indicates that the driver/module had problems,
5860 5898   *      an EFAULT was found when accessing user data, a lack of
5861 5899   *      resources, etc.
5862 5900   */
5863 5901  int
5864 5902  strdoioctl(
5865 5903          struct stdata *stp,
5866 5904          struct strioctl *strioc,
5867 5905          int fflags,             /* file flags with model info */
5868 5906          int flag,
5869 5907          cred_t *crp,
5870 5908          int *rvalp)
5871 5909  {
5872 5910          mblk_t *bp;
5873 5911          struct iocblk *iocbp;
5874 5912          struct copyreq *reqp;
5875 5913          struct copyresp *resp;
5876 5914          int id;
5877 5915          int transparent = 0;
5878 5916          int error = 0;
5879 5917          int len = 0;
5880 5918          caddr_t taddr;
5881 5919          int copyflag = (flag & (U_TO_K | K_TO_K));
5882 5920          int sigflag = (flag & STR_NOSIG);
5883 5921          int errs;
5884 5922          uint_t waitflags;
5885 5923          boolean_t set_iocwaitne = B_FALSE;
5886 5924  
5887 5925          ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
5888 5926          ASSERT((fflags & FMODELS) != 0);
5889 5927  
5890 5928          TRACE_2(TR_FAC_STREAMS_FR,
5891 5929              TR_STRDOIOCTL,
5892 5930              "strdoioctl:stp %p strioc %p", stp, strioc);
5893 5931          if (strioc->ic_len == TRANSPARENT) {    /* send arg in M_DATA block */
5894 5932                  transparent = 1;
5895 5933                  strioc->ic_len = sizeof (intptr_t);
5896 5934          }
5897 5935  
5898 5936          if (strioc->ic_len < 0 || (strmsgsz > 0 && strioc->ic_len > strmsgsz))
5899 5937                  return (EINVAL);
5900 5938  
5901 5939          if ((bp = allocb_cred_wait(sizeof (union ioctypes), sigflag, &error,
5902 5940              crp, curproc->p_pid)) == NULL)
5903 5941                          return (error);
5904 5942  
5905 5943          bzero(bp->b_wptr, sizeof (union ioctypes));
5906 5944  
5907 5945          iocbp = (struct iocblk *)bp->b_wptr;
5908 5946          iocbp->ioc_count = strioc->ic_len;
5909 5947          iocbp->ioc_cmd = strioc->ic_cmd;
5910 5948          iocbp->ioc_flag = (fflags & FMODELS);
5911 5949  
5912 5950          crhold(crp);
5913 5951          iocbp->ioc_cr = crp;
5914 5952          DB_TYPE(bp) = M_IOCTL;
5915 5953          bp->b_wptr += sizeof (struct iocblk);
5916 5954  
5917 5955          if (flag & STR_NOERROR)
5918 5956                  errs = STPLEX;
5919 5957          else
5920 5958                  errs = STRHUP|STRDERR|STWRERR|STPLEX;
5921 5959  
5922 5960          /*
5923 5961           * If there is data to copy into ioctl block, do so.
5924 5962           */
5925 5963          if (iocbp->ioc_count > 0) {
5926 5964                  if (transparent)
5927 5965                          /*
5928 5966                           * Note: STR_NOERROR does not have an effect
5929 5967                           * in putiocd()
5930 5968                           */
5931 5969                          id = K_TO_K | sigflag;
5932 5970                  else
5933 5971                          id = flag;
5934 5972                  if ((error = putiocd(bp, strioc->ic_dp, id, crp)) != 0) {
5935 5973                          freemsg(bp);
5936 5974                          crfree(crp);
5937 5975                          return (error);
5938 5976                  }
5939 5977  
5940 5978                  /*
5941 5979                   * We could have slept copying in user pages.
5942 5980                   * Recheck the stream head state (the other end
5943 5981                   * of a pipe could have gone away).
5944 5982                   */
5945 5983                  if (stp->sd_flag & errs) {
5946 5984                          mutex_enter(&stp->sd_lock);
5947 5985                          error = strgeterr(stp, errs, 0);
5948 5986                          mutex_exit(&stp->sd_lock);
5949 5987                          if (error != 0) {
5950 5988                                  freemsg(bp);
5951 5989                                  crfree(crp);
5952 5990                                  return (error);
5953 5991                          }
5954 5992                  }
5955 5993          }
5956 5994          if (transparent)
5957 5995                  iocbp->ioc_count = TRANSPARENT;
5958 5996  
5959 5997          /*
5960 5998           * Block for up to STRTIMOUT milliseconds if there is an outstanding
5961 5999           * ioctl for this stream already running.  All processes
5962 6000           * sleeping here will be awakened as a result of an ACK
5963 6001           * or NAK being received for the outstanding ioctl, or
5964 6002           * as a result of the timer expiring on the outstanding
5965 6003           * ioctl (a failure), or as a result of any waiting
5966 6004           * process's timer expiring (also a failure).
5967 6005           */
5968 6006  
5969 6007          error = 0;
5970 6008          mutex_enter(&stp->sd_lock);
5971 6009          while ((stp->sd_flag & IOCWAIT) ||
5972 6010              (!set_iocwaitne && (stp->sd_flag & IOCWAITNE))) {
5973 6011                  clock_t cv_rval;
5974 6012  
5975 6013                  TRACE_0(TR_FAC_STREAMS_FR,
5976 6014                      TR_STRDOIOCTL_WAIT,
5977 6015                      "strdoioctl sleeps - IOCWAIT");
5978 6016                  cv_rval = str_cv_wait(&stp->sd_iocmonitor, &stp->sd_lock,
5979 6017                      STRTIMOUT, sigflag);
5980 6018                  if (cv_rval <= 0) {
5981 6019                          if (cv_rval == 0) {
5982 6020                                  error = EINTR;
5983 6021                          } else {
5984 6022                                  if (flag & STR_NOERROR) {
5985 6023                                          /*
5986 6024                                           * Terminating current ioctl in
5987 6025                                           * progress -- assume it got lost and
5988 6026                                           * wake up the other thread so that the
5989 6027                                           * operation completes.
5990 6028                                           */
5991 6029                                          if (!(stp->sd_flag & IOCWAITNE)) {
5992 6030                                                  set_iocwaitne = B_TRUE;
5993 6031                                                  stp->sd_flag |= IOCWAITNE;
5994 6032                                                  cv_broadcast(&stp->sd_monitor);
5995 6033                                          }
5996 6034                                          /*
5997 6035                                           * Otherwise, there's a running
5998 6036                                           * STR_NOERROR -- we have no choice
5999 6037                                           * here but to wait forever (or until
6000 6038                                           * interrupted).
6001 6039                                           */
6002 6040                                  } else {
6003 6041                                          /*
6004 6042                                           * pending ioctl has caused
6005 6043                                           * us to time out
6006 6044                                           */
6007 6045                                          error = ETIME;
6008 6046                                  }
6009 6047                          }
6010 6048                  } else if ((stp->sd_flag & errs)) {
6011 6049                          error = strgeterr(stp, errs, 0);
6012 6050                  }
6013 6051                  if (error) {
6014 6052                          mutex_exit(&stp->sd_lock);
6015 6053                          freemsg(bp);
6016 6054                          crfree(crp);
6017 6055                          return (error);
6018 6056                  }
6019 6057          }
6020 6058  
6021 6059          /*
6022 6060           * Have control of ioctl mechanism.
6023 6061           * Send down ioctl packet and wait for response.
6024 6062           */
6025 6063          if (stp->sd_iocblk != (mblk_t *)-1) {
6026 6064                  freemsg(stp->sd_iocblk);
6027 6065          }
6028 6066          stp->sd_iocblk = NULL;
6029 6067  
6030 6068          /*
6031 6069           * If this is marked with 'noerror' (internal; mostly
6032 6070           * I_{P,}{UN,}LINK), then make sure nobody else is able to get
6033 6071           * in here by setting IOCWAITNE.
6034 6072           */
6035 6073          waitflags = IOCWAIT;
6036 6074          if (flag & STR_NOERROR)
6037 6075                  waitflags |= IOCWAITNE;
6038 6076  
6039 6077          stp->sd_flag |= waitflags;
6040 6078  
6041 6079          /*
6042 6080           * Assign sequence number.
6043 6081           */
6044 6082          iocbp->ioc_id = stp->sd_iocid = getiocseqno();
6045 6083  
6046 6084          mutex_exit(&stp->sd_lock);
6047 6085  
6048 6086          TRACE_1(TR_FAC_STREAMS_FR,
6049 6087              TR_STRDOIOCTL_PUT, "strdoioctl put: stp %p", stp);
6050 6088          stream_willservice(stp);
6051 6089          putnext(stp->sd_wrq, bp);
6052 6090          stream_runservice(stp);
6053 6091  
6054 6092          /*
6055 6093           * Timed wait for acknowledgment.  The wait time is limited by the
6056 6094           * timeout value, which must be a positive integer (number of
6057 6095           * milliseconds) to wait, or 0 (use default value of STRTIMOUT
6058 6096           * milliseconds), or -1 (wait forever).  This will be awakened
6059 6097           * either by an ACK/NAK message arriving, the timer expiring, or
6060 6098           * the timer expiring on another ioctl waiting for control of the
6061 6099           * mechanism.
6062 6100           */
6063 6101  waitioc:
6064 6102          mutex_enter(&stp->sd_lock);
6065 6103  
6066 6104  
6067 6105          /*
6068 6106           * If the reply has already arrived, don't sleep.  If awakened from
6069 6107           * the sleep, fail only if the reply has not arrived by then.
6070 6108           * Otherwise, process the reply.
6071 6109           */
6072 6110          while (!stp->sd_iocblk) {
6073 6111                  clock_t cv_rval;
6074 6112  
6075 6113                  if (stp->sd_flag & errs) {
6076 6114                          error = strgeterr(stp, errs, 0);
6077 6115                          if (error != 0) {
6078 6116                                  stp->sd_flag &= ~waitflags;
6079 6117                                  cv_broadcast(&stp->sd_iocmonitor);
6080 6118                                  mutex_exit(&stp->sd_lock);
6081 6119                                  crfree(crp);
6082 6120                                  return (error);
6083 6121                          }
6084 6122                  }
6085 6123  
6086 6124                  TRACE_0(TR_FAC_STREAMS_FR,
6087 6125                      TR_STRDOIOCTL_WAIT2,
6088 6126                      "strdoioctl sleeps awaiting reply");
6089 6127                  ASSERT(error == 0);
6090 6128  
6091 6129                  cv_rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock,
6092 6130                      (strioc->ic_timout ?
6093 6131                      strioc->ic_timout * 1000 : STRTIMOUT), sigflag);
6094 6132  
6095 6133                  /*
6096 6134                   * There are four possible cases here: interrupt, timeout,
6097 6135                   * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
6098 6136                   * valid M_IOCTL reply).
6099 6137                   *
6100 6138                   * If we've been awakened by a STR_NOERROR ioctl on some other
6101 6139                   * thread, then sd_iocblk will still be NULL, and IOCWAITNE
6102 6140                   * will be set.  Pretend as if we just timed out.  Note that
6103 6141                   * this other thread waited at least STRTIMOUT before trying to
6104 6142                   * awaken our thread, so this is indistinguishable (even for
6105 6143                   * INFTIM) from the case where we failed with ETIME waiting on
6106 6144                   * IOCWAIT in the prior loop.
6107 6145                   */
6108 6146                  if (cv_rval > 0 && !(flag & STR_NOERROR) &&
6109 6147                      stp->sd_iocblk == NULL && (stp->sd_flag & IOCWAITNE)) {
6110 6148                          cv_rval = -1;
6111 6149                  }
6112 6150  
6113 6151                  /*
6114 6152                   * note: STR_NOERROR does not protect
6115 6153                   * us here.. use ic_timout < 0
6116 6154                   */
6117 6155                  if (cv_rval <= 0) {
6118 6156                          if (cv_rval == 0) {
6119 6157                                  error = EINTR;
6120 6158                          } else {
6121 6159                                  error =  ETIME;
6122 6160                          }
6123 6161                          /*
6124 6162                           * A message could have come in after we were scheduled
6125 6163                           * but before we were actually run.
6126 6164                           */
6127 6165                          bp = stp->sd_iocblk;
6128 6166                          stp->sd_iocblk = NULL;
6129 6167                          if (bp != NULL) {
6130 6168                                  if ((bp->b_datap->db_type == M_COPYIN) ||
6131 6169                                      (bp->b_datap->db_type == M_COPYOUT)) {
6132 6170                                          mutex_exit(&stp->sd_lock);
6133 6171                                          if (bp->b_cont) {
6134 6172                                                  freemsg(bp->b_cont);
6135 6173                                                  bp->b_cont = NULL;
6136 6174                                          }
6137 6175                                          bp->b_datap->db_type = M_IOCDATA;
6138 6176                                          bp->b_wptr = bp->b_rptr +
6139 6177                                              sizeof (struct copyresp);
6140 6178                                          resp = (struct copyresp *)bp->b_rptr;
6141 6179                                          resp->cp_rval =
6142 6180                                              (caddr_t)1; /* failure */
6143 6181                                          stream_willservice(stp);
6144 6182                                          putnext(stp->sd_wrq, bp);
6145 6183                                          stream_runservice(stp);
6146 6184                                          mutex_enter(&stp->sd_lock);
6147 6185                                  } else {
6148 6186                                          freemsg(bp);
6149 6187                                  }
6150 6188                          }
6151 6189                          stp->sd_flag &= ~waitflags;
6152 6190                          cv_broadcast(&stp->sd_iocmonitor);
6153 6191                          mutex_exit(&stp->sd_lock);
6154 6192                          crfree(crp);
6155 6193                          return (error);
6156 6194                  }
6157 6195          }
6158 6196          bp = stp->sd_iocblk;
6159 6197          /*
6160 6198           * Note: it is strictly impossible to get here with sd_iocblk set to
6161 6199           * -1.  This is because the initial loop above doesn't allow any new
6162 6200           * ioctls into the fray until all others have passed this point.
6163 6201           */
6164 6202          ASSERT(bp != NULL && bp != (mblk_t *)-1);
6165 6203          TRACE_1(TR_FAC_STREAMS_FR,
6166 6204              TR_STRDOIOCTL_ACK, "strdoioctl got reply: bp %p", bp);
6167 6205          if ((bp->b_datap->db_type == M_IOCACK) ||
6168 6206              (bp->b_datap->db_type == M_IOCNAK)) {
6169 6207                  /* for detection of duplicate ioctl replies */
6170 6208                  stp->sd_iocblk = (mblk_t *)-1;
6171 6209                  stp->sd_flag &= ~waitflags;
6172 6210                  cv_broadcast(&stp->sd_iocmonitor);
6173 6211                  mutex_exit(&stp->sd_lock);
6174 6212          } else {
6175 6213                  /*
6176 6214                   * flags not cleared here because we're still doing
6177 6215                   * copy in/out for ioctl.
6178 6216                   */
6179 6217                  stp->sd_iocblk = NULL;
6180 6218                  mutex_exit(&stp->sd_lock);
6181 6219          }
6182 6220  
6183 6221  
6184 6222          /*
6185 6223           * Have received acknowledgment.
6186 6224           */
6187 6225  
6188 6226          switch (bp->b_datap->db_type) {
6189 6227          case M_IOCACK:
6190 6228                  /*
6191 6229                   * Positive ack.
6192 6230                   */
6193 6231                  iocbp = (struct iocblk *)bp->b_rptr;
6194 6232  
6195 6233                  /*
6196 6234                   * Set error if indicated.
6197 6235                   */
6198 6236                  if (iocbp->ioc_error) {
6199 6237                          error = iocbp->ioc_error;
6200 6238                          break;
6201 6239                  }
6202 6240  
6203 6241                  /*
6204 6242                   * Set return value.
6205 6243                   */
6206 6244                  *rvalp = iocbp->ioc_rval;
6207 6245  
6208 6246                  /*
6209 6247                   * Data may have been returned in ACK message (ioc_count > 0).
6210 6248                   * If so, copy it out to the user's buffer.
6211 6249                   */
6212 6250                  if (iocbp->ioc_count && !transparent) {
6213 6251                          if (error = getiocd(bp, strioc->ic_dp, copyflag))
6214 6252                                  break;
6215 6253                  }
6216 6254                  if (!transparent) {
6217 6255                          if (len)        /* an M_COPYOUT was used with I_STR */
6218 6256                                  strioc->ic_len = len;
6219 6257                          else
6220 6258                                  strioc->ic_len = (int)iocbp->ioc_count;
6221 6259                  }
6222 6260                  break;
6223 6261  
6224 6262          case M_IOCNAK:
6225 6263                  /*
6226 6264                   * Negative ack.
6227 6265                   *
6228 6266                   * The only thing to do is set error as specified
6229 6267                   * in neg ack packet.
6230 6268                   */
6231 6269                  iocbp = (struct iocblk *)bp->b_rptr;
6232 6270  
6233 6271                  error = (iocbp->ioc_error ? iocbp->ioc_error : EINVAL);
6234 6272                  break;
6235 6273  
6236 6274          case M_COPYIN:
6237 6275                  /*
6238 6276                   * Driver or module has requested user ioctl data.
6239 6277                   */
6240 6278                  reqp = (struct copyreq *)bp->b_rptr;
6241 6279  
6242 6280                  /*
6243 6281                   * M_COPYIN should *never* have a message attached, though
6244 6282                   * it's harmless if it does -- thus, panic on a DEBUG
6245 6283                   * kernel and just free it on a non-DEBUG build.
6246 6284                   */
6247 6285                  ASSERT(bp->b_cont == NULL);
6248 6286                  if (bp->b_cont != NULL) {
6249 6287                          freemsg(bp->b_cont);
6250 6288                          bp->b_cont = NULL;
6251 6289                  }
6252 6290  
6253 6291                  error = putiocd(bp, reqp->cq_addr, flag, crp);
6254 6292                  if (error && bp->b_cont) {
6255 6293                          freemsg(bp->b_cont);
6256 6294                          bp->b_cont = NULL;
6257 6295                  }
6258 6296  
6259 6297                  bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6260 6298                  bp->b_datap->db_type = M_IOCDATA;
6261 6299  
6262 6300                  mblk_setcred(bp, crp, curproc->p_pid);
6263 6301                  resp = (struct copyresp *)bp->b_rptr;
6264 6302                  resp->cp_rval = (caddr_t)(uintptr_t)error;
6265 6303                  resp->cp_flag = (fflags & FMODELS);
6266 6304  
6267 6305                  stream_willservice(stp);
6268 6306                  putnext(stp->sd_wrq, bp);
6269 6307                  stream_runservice(stp);
6270 6308  
6271 6309                  if (error) {
6272 6310                          mutex_enter(&stp->sd_lock);
6273 6311                          stp->sd_flag &= ~waitflags;
6274 6312                          cv_broadcast(&stp->sd_iocmonitor);
6275 6313                          mutex_exit(&stp->sd_lock);
6276 6314                          crfree(crp);
6277 6315                          return (error);
6278 6316                  }
6279 6317  
6280 6318                  goto waitioc;
6281 6319  
6282 6320          case M_COPYOUT:
6283 6321                  /*
6284 6322                   * Driver or module has ioctl data for a user.
6285 6323                   */
6286 6324                  reqp = (struct copyreq *)bp->b_rptr;
6287 6325                  ASSERT(bp->b_cont != NULL);
6288 6326  
6289 6327                  /*
6290 6328                   * Always (transparent or non-transparent )
6291 6329                   * use the address specified in the request
6292 6330                   */
6293 6331                  taddr = reqp->cq_addr;
6294 6332                  if (!transparent)
6295 6333                          len = (int)reqp->cq_size;
6296 6334  
6297 6335                  /* copyout data to the provided address */
6298 6336                  error = getiocd(bp, taddr, copyflag);
6299 6337  
6300 6338                  freemsg(bp->b_cont);
6301 6339                  bp->b_cont = NULL;
6302 6340  
6303 6341                  bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6304 6342                  bp->b_datap->db_type = M_IOCDATA;
6305 6343  
6306 6344                  mblk_setcred(bp, crp, curproc->p_pid);
6307 6345                  resp = (struct copyresp *)bp->b_rptr;
6308 6346                  resp->cp_rval = (caddr_t)(uintptr_t)error;
6309 6347                  resp->cp_flag = (fflags & FMODELS);
6310 6348  
6311 6349                  stream_willservice(stp);
6312 6350                  putnext(stp->sd_wrq, bp);
6313 6351                  stream_runservice(stp);
6314 6352  
6315 6353                  if (error) {
6316 6354                          mutex_enter(&stp->sd_lock);
6317 6355                          stp->sd_flag &= ~waitflags;
6318 6356                          cv_broadcast(&stp->sd_iocmonitor);
6319 6357                          mutex_exit(&stp->sd_lock);
6320 6358                          crfree(crp);
6321 6359                          return (error);
6322 6360                  }
6323 6361                  goto waitioc;
6324 6362  
6325 6363          default:
6326 6364                  ASSERT(0);
6327 6365                  mutex_enter(&stp->sd_lock);
6328 6366                  stp->sd_flag &= ~waitflags;
6329 6367                  cv_broadcast(&stp->sd_iocmonitor);
6330 6368                  mutex_exit(&stp->sd_lock);
6331 6369                  break;
6332 6370          }
6333 6371  
6334 6372          freemsg(bp);
6335 6373          crfree(crp);
6336 6374          return (error);
6337 6375  }
6338 6376  
6339 6377  /*
6340 6378   * Send an M_CMD message downstream and wait for a reply.  This is a ptools
6341 6379   * special used to retrieve information from modules/drivers a stream without
6342 6380   * being subjected to flow control or interfering with pending messages on the
6343 6381   * stream (e.g. an ioctl in flight).
6344 6382   */
6345 6383  int
6346 6384  strdocmd(struct stdata *stp, struct strcmd *scp, cred_t *crp)
6347 6385  {
6348 6386          mblk_t *mp;
6349 6387          struct cmdblk *cmdp;
6350 6388          int error = 0;
6351 6389          int errs = STRHUP|STRDERR|STWRERR|STPLEX;
6352 6390          clock_t rval, timeout = STRTIMOUT;
6353 6391  
6354 6392          if (scp->sc_len < 0 || scp->sc_len > sizeof (scp->sc_buf) ||
6355 6393              scp->sc_timeout < -1)
6356 6394                  return (EINVAL);
6357 6395  
6358 6396          if (scp->sc_timeout > 0)
6359 6397                  timeout = scp->sc_timeout * MILLISEC;
6360 6398  
6361 6399          if ((mp = allocb_cred(sizeof (struct cmdblk), crp,
6362 6400              curproc->p_pid)) == NULL)
6363 6401                  return (ENOMEM);
6364 6402  
6365 6403          crhold(crp);
6366 6404  
6367 6405          cmdp = (struct cmdblk *)mp->b_wptr;
6368 6406          cmdp->cb_cr = crp;
6369 6407          cmdp->cb_cmd = scp->sc_cmd;
6370 6408          cmdp->cb_len = scp->sc_len;
6371 6409          cmdp->cb_error = 0;
6372 6410          mp->b_wptr += sizeof (struct cmdblk);
6373 6411  
6374 6412          DB_TYPE(mp) = M_CMD;
6375 6413          DB_CPID(mp) = curproc->p_pid;
6376 6414  
6377 6415          /*
6378 6416           * Copy in the payload.
6379 6417           */
6380 6418          if (cmdp->cb_len > 0) {
6381 6419                  mp->b_cont = allocb_cred(sizeof (scp->sc_buf), crp,
6382 6420                      curproc->p_pid);
6383 6421                  if (mp->b_cont == NULL) {
6384 6422                          error = ENOMEM;
6385 6423                          goto out;
6386 6424                  }
6387 6425  
6388 6426                  /* cb_len comes from sc_len, which has already been checked */
6389 6427                  ASSERT(cmdp->cb_len <= sizeof (scp->sc_buf));
6390 6428                  (void) bcopy(scp->sc_buf, mp->b_cont->b_wptr, cmdp->cb_len);
6391 6429                  mp->b_cont->b_wptr += cmdp->cb_len;
6392 6430                  DB_CPID(mp->b_cont) = curproc->p_pid;
6393 6431          }
6394 6432  
6395 6433          /*
6396 6434           * Since this mechanism is strictly for ptools, and since only one
6397 6435           * process can be grabbed at a time, we simply fail if there's
6398 6436           * currently an operation pending.
6399 6437           */
6400 6438          mutex_enter(&stp->sd_lock);
6401 6439          if (stp->sd_flag & STRCMDWAIT) {
6402 6440                  mutex_exit(&stp->sd_lock);
6403 6441                  error = EBUSY;
6404 6442                  goto out;
6405 6443          }
6406 6444          stp->sd_flag |= STRCMDWAIT;
6407 6445          ASSERT(stp->sd_cmdblk == NULL);
6408 6446          mutex_exit(&stp->sd_lock);
6409 6447  
6410 6448          putnext(stp->sd_wrq, mp);
6411 6449          mp = NULL;
6412 6450  
6413 6451          /*
6414 6452           * Timed wait for acknowledgment.  If the reply has already arrived,
6415 6453           * don't sleep.  If awakened from the sleep, fail only if the reply
6416 6454           * has not arrived by then.  Otherwise, process the reply.
6417 6455           */
6418 6456          mutex_enter(&stp->sd_lock);
6419 6457          while (stp->sd_cmdblk == NULL) {
6420 6458                  if (stp->sd_flag & errs) {
6421 6459                          if ((error = strgeterr(stp, errs, 0)) != 0)
6422 6460                                  goto waitout;
6423 6461                  }
6424 6462  
6425 6463                  rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock, timeout, 0);
6426 6464                  if (stp->sd_cmdblk != NULL)
6427 6465                          break;
6428 6466  
6429 6467                  if (rval <= 0) {
6430 6468                          error = (rval == 0) ? EINTR : ETIME;
6431 6469                          goto waitout;
6432 6470                  }
6433 6471          }
6434 6472  
6435 6473          /*
6436 6474           * We received a reply.
6437 6475           */
6438 6476          mp = stp->sd_cmdblk;
6439 6477          stp->sd_cmdblk = NULL;
6440 6478          ASSERT(mp != NULL && DB_TYPE(mp) == M_CMD);
6441 6479          ASSERT(stp->sd_flag & STRCMDWAIT);
6442 6480          stp->sd_flag &= ~STRCMDWAIT;
6443 6481          mutex_exit(&stp->sd_lock);
6444 6482  
6445 6483          cmdp = (struct cmdblk *)mp->b_rptr;
6446 6484          if ((error = cmdp->cb_error) != 0)
6447 6485                  goto out;
6448 6486  
6449 6487          /*
6450 6488           * Data may have been returned in the reply (cb_len > 0).
6451 6489           * If so, copy it out to the user's buffer.
6452 6490           */
6453 6491          if (cmdp->cb_len > 0) {
6454 6492                  if (mp->b_cont == NULL || MBLKL(mp->b_cont) < cmdp->cb_len) {
6455 6493                          error = EPROTO;
6456 6494                          goto out;
6457 6495                  }
6458 6496  
6459 6497                  cmdp->cb_len = MIN(cmdp->cb_len, sizeof (scp->sc_buf));
6460 6498                  (void) bcopy(mp->b_cont->b_rptr, scp->sc_buf, cmdp->cb_len);
6461 6499          }
6462 6500          scp->sc_len = cmdp->cb_len;
6463 6501  out:
6464 6502          freemsg(mp);
6465 6503          crfree(crp);
6466 6504          return (error);
6467 6505  waitout:
6468 6506          ASSERT(stp->sd_cmdblk == NULL);
6469 6507          stp->sd_flag &= ~STRCMDWAIT;
6470 6508          mutex_exit(&stp->sd_lock);
6471 6509          crfree(crp);
6472 6510          return (error);
6473 6511  }
6474 6512  
6475 6513  /*
6476 6514   * For the SunOS keyboard driver.
6477 6515   * Return the next available "ioctl" sequence number.
6478 6516   * Exported, so that streams modules can send "ioctl" messages
6479 6517   * downstream from their open routine.
6480 6518   */
6481 6519  int
6482 6520  getiocseqno(void)
6483 6521  {
6484 6522          int     i;
6485 6523  
6486 6524          mutex_enter(&strresources);
6487 6525          i = ++ioc_id;
6488 6526          mutex_exit(&strresources);
6489 6527          return (i);
6490 6528  }
6491 6529  
6492 6530  /*
6493 6531   * Get the next message from the read queue.  If the message is
6494 6532   * priority, STRPRI will have been set by strrput().  This flag
6495 6533   * should be reset only when the entire message at the front of the
6496 6534   * queue as been consumed.
6497 6535   *
6498 6536   * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
6499 6537   */
6500 6538  int
6501 6539  strgetmsg(
6502 6540          struct vnode *vp,
6503 6541          struct strbuf *mctl,
6504 6542          struct strbuf *mdata,
6505 6543          unsigned char *prip,
6506 6544          int *flagsp,
6507 6545          int fmode,
6508 6546          rval_t *rvp)
6509 6547  {
6510 6548          struct stdata *stp;
6511 6549          mblk_t *bp, *nbp;
6512 6550          mblk_t *savemp = NULL;
6513 6551          mblk_t *savemptail = NULL;
6514 6552          uint_t old_sd_flag;
6515 6553          int flg;
6516 6554          int more = 0;
6517 6555          int error = 0;
6518 6556          char first = 1;
6519 6557          uint_t mark;            /* Contains MSG*MARK and _LASTMARK */
6520 6558  #define _LASTMARK       0x8000  /* Distinct from MSG*MARK */
6521 6559          unsigned char pri = 0;
6522 6560          queue_t *q;
6523 6561          int     pr = 0;                 /* Partial read successful */
6524 6562          struct uio uios;
6525 6563          struct uio *uiop = &uios;
6526 6564          struct iovec iovs;
6527 6565          unsigned char type;
6528 6566  
6529 6567          TRACE_1(TR_FAC_STREAMS_FR, TR_STRGETMSG_ENTER,
6530 6568              "strgetmsg:%p", vp);
6531 6569  
6532 6570          ASSERT(vp->v_stream);
6533 6571          stp = vp->v_stream;
6534 6572          rvp->r_val1 = 0;
6535 6573  
6536 6574          mutex_enter(&stp->sd_lock);
6537 6575  
6538 6576          if ((error = i_straccess(stp, JCREAD)) != 0) {
6539 6577                  mutex_exit(&stp->sd_lock);
6540 6578                  return (error);
6541 6579          }
6542 6580  
6543 6581          if (stp->sd_flag & (STRDERR|STPLEX)) {
6544 6582                  error = strgeterr(stp, STRDERR|STPLEX, 0);
6545 6583                  if (error != 0) {
6546 6584                          mutex_exit(&stp->sd_lock);
6547 6585                          return (error);
6548 6586                  }
6549 6587          }
6550 6588          mutex_exit(&stp->sd_lock);
6551 6589  
6552 6590          switch (*flagsp) {
6553 6591          case MSG_HIPRI:
6554 6592                  if (*prip != 0)
6555 6593                          return (EINVAL);
6556 6594                  break;
6557 6595  
6558 6596          case MSG_ANY:
6559 6597          case MSG_BAND:
6560 6598                  break;
6561 6599  
6562 6600          default:
6563 6601                  return (EINVAL);
6564 6602          }
6565 6603          /*
6566 6604           * Setup uio and iov for data part
6567 6605           */
6568 6606          iovs.iov_base = mdata->buf;
6569 6607          iovs.iov_len = mdata->maxlen;
6570 6608          uios.uio_iov = &iovs;
6571 6609          uios.uio_iovcnt = 1;
6572 6610          uios.uio_loffset = 0;
6573 6611          uios.uio_segflg = UIO_USERSPACE;
6574 6612          uios.uio_fmode = 0;
6575 6613          uios.uio_extflg = UIO_COPY_CACHED;
6576 6614          uios.uio_resid = mdata->maxlen;
6577 6615          uios.uio_offset = 0;
6578 6616  
6579 6617          q = _RD(stp->sd_wrq);
6580 6618          mutex_enter(&stp->sd_lock);
6581 6619          old_sd_flag = stp->sd_flag;
6582 6620          mark = 0;
6583 6621          for (;;) {
6584 6622                  int done = 0;
6585 6623                  mblk_t *q_first = q->q_first;
6586 6624  
6587 6625                  /*
6588 6626                   * Get the next message of appropriate priority
6589 6627                   * from the stream head.  If the caller is interested
6590 6628                   * in band or hipri messages, then they should already
6591 6629                   * be enqueued at the stream head.  On the other hand
6592 6630                   * if the caller wants normal (band 0) messages, they
6593 6631                   * might be deferred in a synchronous stream and they
6594 6632                   * will need to be pulled up.
6595 6633                   *
6596 6634                   * After we have dequeued a message, we might find that
6597 6635                   * it was a deferred M_SIG that was enqueued at the
6598 6636                   * stream head.  It must now be posted as part of the
6599 6637                   * read by calling strsignal_nolock().
6600 6638                   *
6601 6639                   * Also note that strrput does not enqueue an M_PCSIG,
6602 6640                   * and there cannot be more than one hipri message,
6603 6641                   * so there was no need to have the M_PCSIG case.
6604 6642                   *
6605 6643                   * At some time it might be nice to try and wrap the
6606 6644                   * functionality of kstrgetmsg() and strgetmsg() into
6607 6645                   * a common routine so to reduce the amount of replicated
6608 6646                   * code (since they are extremely similar).
6609 6647                   */
6610 6648                  if (!(*flagsp & (MSG_HIPRI|MSG_BAND))) {
6611 6649                          /* Asking for normal, band0 data */
6612 6650                          bp = strget(stp, q, uiop, first, &error);
6613 6651                          ASSERT(MUTEX_HELD(&stp->sd_lock));
6614 6652                          if (bp != NULL) {
6615 6653                                  if (DB_TYPE(bp) == M_SIG) {
6616 6654                                          strsignal_nolock(stp, *bp->b_rptr,
6617 6655                                              bp->b_band);
6618 6656                                          freemsg(bp);
6619 6657                                          continue;
6620 6658                                  } else {
6621 6659                                          break;
6622 6660                                  }
6623 6661                          }
6624 6662                          if (error != 0)
6625 6663                                  goto getmout;
6626 6664  
6627 6665                  /*
6628 6666                   * We can't depend on the value of STRPRI here because
6629 6667                   * the stream head may be in transit. Therefore, we
6630 6668                   * must look at the type of the first message to
6631 6669                   * determine if a high priority messages is waiting
6632 6670                   */
6633 6671                  } else if ((*flagsp & MSG_HIPRI) && q_first != NULL &&
6634 6672                      DB_TYPE(q_first) >= QPCTL &&
6635 6673                      (bp = getq_noenab(q, 0)) != NULL) {
6636 6674                          /* Asked for HIPRI and got one */
6637 6675                          ASSERT(DB_TYPE(bp) >= QPCTL);
6638 6676                          break;
6639 6677                  } else if ((*flagsp & MSG_BAND) && q_first != NULL &&
6640 6678                      ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
6641 6679                      (bp = getq_noenab(q, 0)) != NULL) {
6642 6680                          /*
6643 6681                           * Asked for at least band "prip" and got either at
6644 6682                           * least that band or a hipri message.
6645 6683                           */
6646 6684                          ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
6647 6685                          if (DB_TYPE(bp) == M_SIG) {
6648 6686                                  strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
6649 6687                                  freemsg(bp);
6650 6688                                  continue;
6651 6689                          } else {
6652 6690                                  break;
6653 6691                          }
6654 6692                  }
6655 6693  
6656 6694                  /* No data. Time to sleep? */
6657 6695                  qbackenable(q, 0);
6658 6696  
6659 6697                  /*
6660 6698                   * If STRHUP or STREOF, return 0 length control and data.
6661 6699                   * If resid is 0, then a read(fd,buf,0) was done. Do not
6662 6700                   * sleep to satisfy this request because by default we have
6663 6701                   * zero bytes to return.
6664 6702                   */
6665 6703                  if ((stp->sd_flag & (STRHUP|STREOF)) || (mctl->maxlen == 0 &&
6666 6704                      mdata->maxlen == 0)) {
6667 6705                          mctl->len = mdata->len = 0;
6668 6706                          *flagsp = 0;
6669 6707                          mutex_exit(&stp->sd_lock);
6670 6708                          return (0);
6671 6709                  }
6672 6710                  TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_WAIT,
6673 6711                      "strgetmsg calls strwaitq:%p, %p",
6674 6712                      vp, uiop);
6675 6713                  if (((error = strwaitq(stp, GETWAIT, (ssize_t)0, fmode, -1,
6676 6714                      &done)) != 0) || done) {
6677 6715                          TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_DONE,
6678 6716                              "strgetmsg error or done:%p, %p",
6679 6717                              vp, uiop);
6680 6718                          mutex_exit(&stp->sd_lock);
6681 6719                          return (error);
6682 6720                  }
6683 6721                  TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_AWAKE,
6684 6722                      "strgetmsg awakes:%p, %p", vp, uiop);
6685 6723                  if ((error = i_straccess(stp, JCREAD)) != 0) {
6686 6724                          mutex_exit(&stp->sd_lock);
6687 6725                          return (error);
6688 6726                  }
6689 6727                  first = 0;
6690 6728          }
6691 6729          ASSERT(bp != NULL);
6692 6730          /*
6693 6731           * Extract any mark information. If the message is not completely
6694 6732           * consumed this information will be put in the mblk
6695 6733           * that is putback.
6696 6734           * If MSGMARKNEXT is set and the message is completely consumed
6697 6735           * the STRATMARK flag will be set below. Likewise, if
6698 6736           * MSGNOTMARKNEXT is set and the message is
6699 6737           * completely consumed STRNOTATMARK will be set.
6700 6738           */
6701 6739          mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
6702 6740          ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
6703 6741              (MSGMARKNEXT|MSGNOTMARKNEXT));
6704 6742          if (mark != 0 && bp == stp->sd_mark) {
6705 6743                  mark |= _LASTMARK;
6706 6744                  stp->sd_mark = NULL;
6707 6745          }
6708 6746          /*
6709 6747           * keep track of the original message type and priority
6710 6748           */
6711 6749          pri = bp->b_band;
6712 6750          type = bp->b_datap->db_type;
6713 6751          if (type == M_PASSFP) {
6714 6752                  if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
6715 6753                          stp->sd_mark = bp;
6716 6754                  bp->b_flag |= mark & ~_LASTMARK;
6717 6755                  putback(stp, q, bp, pri);
6718 6756                  qbackenable(q, pri);
6719 6757                  mutex_exit(&stp->sd_lock);
6720 6758                  return (EBADMSG);
6721 6759          }
6722 6760          ASSERT(type != M_SIG);
6723 6761  
6724 6762          /*
6725 6763           * Set this flag so strrput will not generate signals. Need to
6726 6764           * make sure this flag is cleared before leaving this routine
6727 6765           * else signals will stop being sent.
6728 6766           */
6729 6767          stp->sd_flag |= STRGETINPROG;
6730 6768          mutex_exit(&stp->sd_lock);
6731 6769  
6732 6770          if (STREAM_NEEDSERVICE(stp))
6733 6771                  stream_runservice(stp);
6734 6772  
6735 6773          /*
6736 6774           * Set HIPRI flag if message is priority.
6737 6775           */
6738 6776          if (type >= QPCTL)
6739 6777                  flg = MSG_HIPRI;
6740 6778          else
6741 6779                  flg = MSG_BAND;
6742 6780  
6743 6781          /*
6744 6782           * First process PROTO or PCPROTO blocks, if any.
6745 6783           */
6746 6784          if (mctl->maxlen >= 0 && type != M_DATA) {
6747 6785                  size_t  n, bcnt;
6748 6786                  char    *ubuf;
6749 6787  
6750 6788                  bcnt = mctl->maxlen;
6751 6789                  ubuf = mctl->buf;
6752 6790                  while (bp != NULL && bp->b_datap->db_type != M_DATA) {
6753 6791                          if ((n = MIN(bcnt, bp->b_wptr - bp->b_rptr)) != 0 &&
6754 6792                              copyout(bp->b_rptr, ubuf, n)) {
6755 6793                                  error = EFAULT;
6756 6794                                  mutex_enter(&stp->sd_lock);
6757 6795                                  /*
6758 6796                                   * clear stream head pri flag based on
6759 6797                                   * first message type
6760 6798                                   */
6761 6799                                  if (type >= QPCTL) {
6762 6800                                          ASSERT(type == M_PCPROTO);
6763 6801                                          stp->sd_flag &= ~STRPRI;
6764 6802                                  }
6765 6803                                  more = 0;
6766 6804                                  freemsg(bp);
6767 6805                                  goto getmout;
6768 6806                          }
6769 6807                          ubuf += n;
6770 6808                          bp->b_rptr += n;
6771 6809                          if (bp->b_rptr >= bp->b_wptr) {
6772 6810                                  nbp = bp;
6773 6811                                  bp = bp->b_cont;
6774 6812                                  freeb(nbp);
6775 6813                          }
6776 6814                          ASSERT(n <= bcnt);
6777 6815                          bcnt -= n;
6778 6816                          if (bcnt == 0)
6779 6817                                  break;
6780 6818                  }
6781 6819                  mctl->len = mctl->maxlen - bcnt;
6782 6820          } else
6783 6821                  mctl->len = -1;
6784 6822  
6785 6823          if (bp && bp->b_datap->db_type != M_DATA) {
6786 6824                  /*
6787 6825                   * More PROTO blocks in msg.
6788 6826                   */
6789 6827                  more |= MORECTL;
6790 6828                  savemp = bp;
6791 6829                  while (bp && bp->b_datap->db_type != M_DATA) {
6792 6830                          savemptail = bp;
6793 6831                          bp = bp->b_cont;
6794 6832                  }
6795 6833                  savemptail->b_cont = NULL;
6796 6834          }
6797 6835  
6798 6836          /*
6799 6837           * Now process DATA blocks, if any.
6800 6838           */
6801 6839          if (mdata->maxlen >= 0 && bp) {
6802 6840                  /*
6803 6841                   * struiocopyout will consume a potential zero-length
6804 6842                   * M_DATA even if uio_resid is zero.
6805 6843                   */
6806 6844                  size_t oldresid = uiop->uio_resid;
6807 6845  
6808 6846                  bp = struiocopyout(bp, uiop, &error);
6809 6847                  if (error != 0) {
6810 6848                          mutex_enter(&stp->sd_lock);
6811 6849                          /*
6812 6850                           * clear stream head hi pri flag based on
6813 6851                           * first message
6814 6852                           */
6815 6853                          if (type >= QPCTL) {
6816 6854                                  ASSERT(type == M_PCPROTO);
6817 6855                                  stp->sd_flag &= ~STRPRI;
6818 6856                          }
6819 6857                          more = 0;
6820 6858                          freemsg(savemp);
6821 6859                          goto getmout;
6822 6860                  }
6823 6861                  /*
6824 6862                   * (pr == 1) indicates a partial read.
6825 6863                   */
6826 6864                  if (oldresid > uiop->uio_resid)
6827 6865                          pr = 1;
6828 6866                  mdata->len = mdata->maxlen - uiop->uio_resid;
6829 6867          } else
6830 6868                  mdata->len = -1;
6831 6869  
6832 6870          if (bp) {                       /* more data blocks in msg */
6833 6871                  more |= MOREDATA;
6834 6872                  if (savemp)
6835 6873                          savemptail->b_cont = bp;
6836 6874                  else
6837 6875                          savemp = bp;
6838 6876          }
6839 6877  
6840 6878          mutex_enter(&stp->sd_lock);
6841 6879          if (savemp) {
6842 6880                  if (pr && (savemp->b_datap->db_type == M_DATA) &&
6843 6881                      msgnodata(savemp)) {
6844 6882                          /*
6845 6883                           * Avoid queuing a zero-length tail part of
6846 6884                           * a message. pr=1 indicates that we read some of
6847 6885                           * the message.
6848 6886                           */
6849 6887                          freemsg(savemp);
6850 6888                          more &= ~MOREDATA;
6851 6889                          /*
6852 6890                           * clear stream head hi pri flag based on
6853 6891                           * first message
6854 6892                           */
6855 6893                          if (type >= QPCTL) {
6856 6894                                  ASSERT(type == M_PCPROTO);
6857 6895                                  stp->sd_flag &= ~STRPRI;
6858 6896                          }
6859 6897                  } else {
6860 6898                          savemp->b_band = pri;
6861 6899                          /*
6862 6900                           * If the first message was HIPRI and the one we're
6863 6901                           * putting back isn't, then clear STRPRI, otherwise
6864 6902                           * set STRPRI again.  Note that we must set STRPRI
6865 6903                           * again since the flush logic in strrput_nondata()
6866 6904                           * may have cleared it while we had sd_lock dropped.
6867 6905                           */
6868 6906                          if (type >= QPCTL) {
6869 6907                                  ASSERT(type == M_PCPROTO);
6870 6908                                  if (queclass(savemp) < QPCTL)
6871 6909                                          stp->sd_flag &= ~STRPRI;
6872 6910                                  else
6873 6911                                          stp->sd_flag |= STRPRI;
6874 6912                          } else if (queclass(savemp) >= QPCTL) {
6875 6913                                  /*
6876 6914                                   * The first message was not a HIPRI message,
6877 6915                                   * but the one we are about to putback is.
6878 6916                                   * For simplicitly, we do not allow for HIPRI
6879 6917                                   * messages to be embedded in the message
6880 6918                                   * body, so just force it to same type as
6881 6919                                   * first message.
6882 6920                                   */
6883 6921                                  ASSERT(type == M_DATA || type == M_PROTO);
6884 6922                                  ASSERT(savemp->b_datap->db_type == M_PCPROTO);
6885 6923                                  savemp->b_datap->db_type = type;
6886 6924                          }
6887 6925                          if (mark != 0) {
6888 6926                                  savemp->b_flag |= mark & ~_LASTMARK;
6889 6927                                  if ((mark & _LASTMARK) &&
6890 6928                                      (stp->sd_mark == NULL)) {
6891 6929                                          /*
6892 6930                                           * If another marked message arrived
6893 6931                                           * while sd_lock was not held sd_mark
6894 6932                                           * would be non-NULL.
6895 6933                                           */
6896 6934                                          stp->sd_mark = savemp;
6897 6935                                  }
6898 6936                          }
6899 6937                          putback(stp, q, savemp, pri);
6900 6938                  }
6901 6939          } else {
6902 6940                  /*
6903 6941                   * The complete message was consumed.
6904 6942                   *
6905 6943                   * If another M_PCPROTO arrived while sd_lock was not held
6906 6944                   * it would have been discarded since STRPRI was still set.
6907 6945                   *
6908 6946                   * Move the MSG*MARKNEXT information
6909 6947                   * to the stream head just in case
6910 6948                   * the read queue becomes empty.
6911 6949                   * clear stream head hi pri flag based on
6912 6950                   * first message
6913 6951                   *
6914 6952                   * If the stream head was at the mark
6915 6953                   * (STRATMARK) before we dropped sd_lock above
6916 6954                   * and some data was consumed then we have
6917 6955                   * moved past the mark thus STRATMARK is
6918 6956                   * cleared. However, if a message arrived in
6919 6957                   * strrput during the copyout above causing
6920 6958                   * STRATMARK to be set we can not clear that
6921 6959                   * flag.
6922 6960                   */
6923 6961                  if (type >= QPCTL) {
6924 6962                          ASSERT(type == M_PCPROTO);
6925 6963                          stp->sd_flag &= ~STRPRI;
6926 6964                  }
6927 6965                  if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
6928 6966                          if (mark & MSGMARKNEXT) {
6929 6967                                  stp->sd_flag &= ~STRNOTATMARK;
6930 6968                                  stp->sd_flag |= STRATMARK;
6931 6969                          } else if (mark & MSGNOTMARKNEXT) {
6932 6970                                  stp->sd_flag &= ~STRATMARK;
6933 6971                                  stp->sd_flag |= STRNOTATMARK;
6934 6972                          } else {
6935 6973                                  stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
6936 6974                          }
6937 6975                  } else if (pr && (old_sd_flag & STRATMARK)) {
6938 6976                          stp->sd_flag &= ~STRATMARK;
6939 6977                  }
6940 6978          }
6941 6979  
6942 6980          *flagsp = flg;
6943 6981          *prip = pri;
6944 6982  
6945 6983          /*
6946 6984           * Getmsg cleanup processing - if the state of the queue has changed
6947 6985           * some signals may need to be sent and/or poll awakened.
6948 6986           */
6949 6987  getmout:
6950 6988          qbackenable(q, pri);
6951 6989  
6952 6990          /*
6953 6991           * We dropped the stream head lock above. Send all M_SIG messages
6954 6992           * before processing stream head for SIGPOLL messages.
6955 6993           */
6956 6994          ASSERT(MUTEX_HELD(&stp->sd_lock));
6957 6995          while ((bp = q->q_first) != NULL &&
6958 6996              (bp->b_datap->db_type == M_SIG)) {
6959 6997                  /*
6960 6998                   * sd_lock is held so the content of the read queue can not
6961 6999                   * change.
6962 7000                   */
6963 7001                  bp = getq(q);
6964 7002                  ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
6965 7003  
6966 7004                  strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
6967 7005                  mutex_exit(&stp->sd_lock);
6968 7006                  freemsg(bp);
6969 7007                  if (STREAM_NEEDSERVICE(stp))
6970 7008                          stream_runservice(stp);
6971 7009                  mutex_enter(&stp->sd_lock);
6972 7010          }
6973 7011  
6974 7012          /*
6975 7013           * stream head cannot change while we make the determination
6976 7014           * whether or not to send a signal. Drop the flag to allow strrput
6977 7015           * to send firstmsgsigs again.
6978 7016           */
6979 7017          stp->sd_flag &= ~STRGETINPROG;
6980 7018  
6981 7019          /*
6982 7020           * If the type of message at the front of the queue changed
6983 7021           * due to the receive the appropriate signals and pollwakeup events
6984 7022           * are generated. The type of changes are:
6985 7023           *      Processed a hipri message, q_first is not hipri.
6986 7024           *      Processed a band X message, and q_first is band Y.
6987 7025           * The generated signals and pollwakeups are identical to what
6988 7026           * strrput() generates should the message that is now on q_first
6989 7027           * arrive to an empty read queue.
6990 7028           *
6991 7029           * Note: only strrput will send a signal for a hipri message.
6992 7030           */
6993 7031          if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
6994 7032                  strsigset_t signals = 0;
6995 7033                  strpollset_t pollwakeups = 0;
6996 7034  
6997 7035                  if (flg & MSG_HIPRI) {
6998 7036                          /*
6999 7037                           * Removed a hipri message. Regular data at
7000 7038                           * the front of  the queue.
7001 7039                           */
7002 7040                          if (bp->b_band == 0) {
7003 7041                                  signals = S_INPUT | S_RDNORM;
7004 7042                                  pollwakeups = POLLIN | POLLRDNORM;
7005 7043                          } else {
7006 7044                                  signals = S_INPUT | S_RDBAND;
7007 7045                                  pollwakeups = POLLIN | POLLRDBAND;
7008 7046                          }
7009 7047                  } else if (pri != bp->b_band) {
7010 7048                          /*
7011 7049                           * The band is different for the new q_first.
7012 7050                           */
7013 7051                          if (bp->b_band == 0) {
7014 7052                                  signals = S_RDNORM;
7015 7053                                  pollwakeups = POLLIN | POLLRDNORM;
7016 7054                          } else {
7017 7055                                  signals = S_RDBAND;
7018 7056                                  pollwakeups = POLLIN | POLLRDBAND;
7019 7057                          }
7020 7058                  }
7021 7059  
7022 7060                  if (pollwakeups != 0) {
7023 7061                          if (pollwakeups == (POLLIN | POLLRDNORM)) {
7024 7062                                  if (!(stp->sd_rput_opt & SR_POLLIN))
7025 7063                                          goto no_pollwake;
7026 7064                                  stp->sd_rput_opt &= ~SR_POLLIN;
7027 7065                          }
7028 7066                          mutex_exit(&stp->sd_lock);
7029 7067                          pollwakeup(&stp->sd_pollist, pollwakeups);
7030 7068                          mutex_enter(&stp->sd_lock);
7031 7069                  }
7032 7070  no_pollwake:
7033 7071  
7034 7072                  if (stp->sd_sigflags & signals)
7035 7073                          strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7036 7074          }
7037 7075          mutex_exit(&stp->sd_lock);
7038 7076  
7039 7077          rvp->r_val1 = more;
7040 7078          return (error);
7041 7079  #undef  _LASTMARK
7042 7080  }
7043 7081  
7044 7082  /*
7045 7083   * Get the next message from the read queue.  If the message is
7046 7084   * priority, STRPRI will have been set by strrput().  This flag
7047 7085   * should be reset only when the entire message at the front of the
7048 7086   * queue as been consumed.
7049 7087   *
7050 7088   * If uiop is NULL all data is returned in mctlp.
7051 7089   * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
7052 7090   * not enabled.
7053 7091   * The timeout parameter is in milliseconds; -1 for infinity.
7054 7092   * This routine handles the consolidation private flags:
7055 7093   *      MSG_IGNERROR    Ignore any stream head error except STPLEX.
7056 7094   *      MSG_DELAYERROR  Defer the error check until the queue is empty.
7057 7095   *      MSG_HOLDSIG     Hold signals while waiting for data.
7058 7096   *      MSG_IPEEK       Only peek at messages.
7059 7097   *      MSG_DISCARDTAIL Discard the tail M_DATA part of the message
7060 7098   *                      that doesn't fit.
7061 7099   *      MSG_NOMARK      If the message is marked leave it on the queue.
7062 7100   *
7063 7101   * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
7064 7102   */
7065 7103  int
7066 7104  kstrgetmsg(
7067 7105          struct vnode *vp,
7068 7106          mblk_t **mctlp,
7069 7107          struct uio *uiop,
7070 7108          unsigned char *prip,
7071 7109          int *flagsp,
7072 7110          clock_t timout,
7073 7111          rval_t *rvp)
7074 7112  {
7075 7113          struct stdata *stp;
7076 7114          mblk_t *bp, *nbp;
7077 7115          mblk_t *savemp = NULL;
7078 7116          mblk_t *savemptail = NULL;
7079 7117          int flags;
7080 7118          uint_t old_sd_flag;
7081 7119          int flg;
7082 7120          int more = 0;
7083 7121          int error = 0;
7084 7122          char first = 1;
7085 7123          uint_t mark;            /* Contains MSG*MARK and _LASTMARK */
7086 7124  #define _LASTMARK       0x8000  /* Distinct from MSG*MARK */
7087 7125          unsigned char pri = 0;
7088 7126          queue_t *q;
7089 7127          int     pr = 0;                 /* Partial read successful */
7090 7128          unsigned char type;
7091 7129  
7092 7130          TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_ENTER,
7093 7131              "kstrgetmsg:%p", vp);
7094 7132  
7095 7133          ASSERT(vp->v_stream);
7096 7134          stp = vp->v_stream;
7097 7135          rvp->r_val1 = 0;
7098 7136  
7099 7137          mutex_enter(&stp->sd_lock);
7100 7138  
7101 7139          if ((error = i_straccess(stp, JCREAD)) != 0) {
7102 7140                  mutex_exit(&stp->sd_lock);
7103 7141                  return (error);
7104 7142          }
7105 7143  
7106 7144          flags = *flagsp;
7107 7145          if (stp->sd_flag & (STRDERR|STPLEX)) {
7108 7146                  if ((stp->sd_flag & STPLEX) ||
7109 7147                      (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == 0) {
7110 7148                          error = strgeterr(stp, STRDERR|STPLEX,
7111 7149                              (flags & MSG_IPEEK));
7112 7150                          if (error != 0) {
7113 7151                                  mutex_exit(&stp->sd_lock);
7114 7152                                  return (error);
7115 7153                          }
7116 7154                  }
7117 7155          }
7118 7156          mutex_exit(&stp->sd_lock);
7119 7157  
7120 7158          switch (flags & (MSG_HIPRI|MSG_ANY|MSG_BAND)) {
7121 7159          case MSG_HIPRI:
7122 7160                  if (*prip != 0)
7123 7161                          return (EINVAL);
7124 7162                  break;
7125 7163  
7126 7164          case MSG_ANY:
7127 7165          case MSG_BAND:
7128 7166                  break;
7129 7167  
7130 7168          default:
7131 7169                  return (EINVAL);
7132 7170          }
7133 7171  
7134 7172  retry:
7135 7173          q = _RD(stp->sd_wrq);
7136 7174          mutex_enter(&stp->sd_lock);
7137 7175          old_sd_flag = stp->sd_flag;
7138 7176          mark = 0;
7139 7177          for (;;) {
7140 7178                  int done = 0;
7141 7179                  int waitflag;
7142 7180                  int fmode;
7143 7181                  mblk_t *q_first = q->q_first;
7144 7182  
7145 7183                  /*
7146 7184                   * This section of the code operates just like the code
7147 7185                   * in strgetmsg().  There is a comment there about what
7148 7186                   * is going on here.
7149 7187                   */
7150 7188                  if (!(flags & (MSG_HIPRI|MSG_BAND))) {
7151 7189                          /* Asking for normal, band0 data */
7152 7190                          bp = strget(stp, q, uiop, first, &error);
7153 7191                          ASSERT(MUTEX_HELD(&stp->sd_lock));
7154 7192                          if (bp != NULL) {
7155 7193                                  if (DB_TYPE(bp) == M_SIG) {
7156 7194                                          strsignal_nolock(stp, *bp->b_rptr,
7157 7195                                              bp->b_band);
7158 7196                                          freemsg(bp);
7159 7197                                          continue;
7160 7198                                  } else {
7161 7199                                          break;
7162 7200                                  }
7163 7201                          }
7164 7202                          if (error != 0) {
7165 7203                                  goto getmout;
7166 7204                          }
7167 7205                  /*
7168 7206                   * We can't depend on the value of STRPRI here because
7169 7207                   * the stream head may be in transit. Therefore, we
7170 7208                   * must look at the type of the first message to
7171 7209                   * determine if a high priority messages is waiting
7172 7210                   */
7173 7211                  } else if ((flags & MSG_HIPRI) && q_first != NULL &&
7174 7212                      DB_TYPE(q_first) >= QPCTL &&
7175 7213                      (bp = getq_noenab(q, 0)) != NULL) {
7176 7214                          ASSERT(DB_TYPE(bp) >= QPCTL);
7177 7215                          break;
7178 7216                  } else if ((flags & MSG_BAND) && q_first != NULL &&
7179 7217                      ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
7180 7218                      (bp = getq_noenab(q, 0)) != NULL) {
7181 7219                          /*
7182 7220                           * Asked for at least band "prip" and got either at
7183 7221                           * least that band or a hipri message.
7184 7222                           */
7185 7223                          ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
7186 7224                          if (DB_TYPE(bp) == M_SIG) {
7187 7225                                  strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7188 7226                                  freemsg(bp);
7189 7227                                  continue;
7190 7228                          } else {
7191 7229                                  break;
7192 7230                          }
7193 7231                  }
7194 7232  
7195 7233                  /* No data. Time to sleep? */
7196 7234                  qbackenable(q, 0);
7197 7235  
7198 7236                  /*
7199 7237                   * Delayed error notification?
7200 7238                   */
7201 7239                  if ((stp->sd_flag & (STRDERR|STPLEX)) &&
7202 7240                      (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == MSG_DELAYERROR) {
7203 7241                          error = strgeterr(stp, STRDERR|STPLEX,
7204 7242                              (flags & MSG_IPEEK));
7205 7243                          if (error != 0) {
7206 7244                                  mutex_exit(&stp->sd_lock);
7207 7245                                  return (error);
7208 7246                          }
7209 7247                  }
7210 7248  
7211 7249                  /*
7212 7250                   * If STRHUP or STREOF, return 0 length control and data.
7213 7251                   * If a read(fd,buf,0) has been done, do not sleep, just
7214 7252                   * return.
7215 7253                   *
7216 7254                   * If mctlp == NULL and uiop == NULL, then the code will
7217 7255                   * do the strwaitq. This is an understood way of saying
7218 7256                   * sleep "polling" until a message is received.
7219 7257                   */
7220 7258                  if ((stp->sd_flag & (STRHUP|STREOF)) ||
7221 7259                      (uiop != NULL && uiop->uio_resid == 0)) {
7222 7260                          if (mctlp != NULL)
7223 7261                                  *mctlp = NULL;
7224 7262                          *flagsp = 0;
7225 7263                          mutex_exit(&stp->sd_lock);
7226 7264                          return (0);
7227 7265                  }
7228 7266  
7229 7267                  waitflag = GETWAIT;
7230 7268                  if (flags &
7231 7269                      (MSG_HOLDSIG|MSG_IGNERROR|MSG_IPEEK|MSG_DELAYERROR)) {
7232 7270                          if (flags & MSG_HOLDSIG)
7233 7271                                  waitflag |= STR_NOSIG;
7234 7272                          if (flags & MSG_IGNERROR)
7235 7273                                  waitflag |= STR_NOERROR;
7236 7274                          if (flags & MSG_IPEEK)
7237 7275                                  waitflag |= STR_PEEK;
7238 7276                          if (flags & MSG_DELAYERROR)
7239 7277                                  waitflag |= STR_DELAYERR;
7240 7278                  }
7241 7279                  if (uiop != NULL)
7242 7280                          fmode = uiop->uio_fmode;
7243 7281                  else
7244 7282                          fmode = 0;
7245 7283  
7246 7284                  TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_WAIT,
7247 7285                      "kstrgetmsg calls strwaitq:%p, %p",
7248 7286                      vp, uiop);
7249 7287                  if (((error = strwaitq(stp, waitflag, (ssize_t)0,
7250 7288                      fmode, timout, &done))) != 0 || done) {
7251 7289                          TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_DONE,
7252 7290                              "kstrgetmsg error or done:%p, %p",
7253 7291                              vp, uiop);
7254 7292                          mutex_exit(&stp->sd_lock);
7255 7293                          return (error);
7256 7294                  }
7257 7295                  TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_AWAKE,
7258 7296                      "kstrgetmsg awakes:%p, %p", vp, uiop);
7259 7297                  if ((error = i_straccess(stp, JCREAD)) != 0) {
7260 7298                          mutex_exit(&stp->sd_lock);
7261 7299                          return (error);
7262 7300                  }
7263 7301                  first = 0;
7264 7302          }
7265 7303          ASSERT(bp != NULL);
7266 7304          /*
7267 7305           * Extract any mark information. If the message is not completely
7268 7306           * consumed this information will be put in the mblk
7269 7307           * that is putback.
7270 7308           * If MSGMARKNEXT is set and the message is completely consumed
7271 7309           * the STRATMARK flag will be set below. Likewise, if
7272 7310           * MSGNOTMARKNEXT is set and the message is
7273 7311           * completely consumed STRNOTATMARK will be set.
7274 7312           */
7275 7313          mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
7276 7314          ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
7277 7315              (MSGMARKNEXT|MSGNOTMARKNEXT));
7278 7316          pri = bp->b_band;
7279 7317          if (mark != 0) {
7280 7318                  /*
7281 7319                   * If the caller doesn't want the mark return.
7282 7320                   * Used to implement MSG_WAITALL in sockets.
7283 7321                   */
7284 7322                  if (flags & MSG_NOMARK) {
7285 7323                          putback(stp, q, bp, pri);
7286 7324                          qbackenable(q, pri);
7287 7325                          mutex_exit(&stp->sd_lock);
7288 7326                          return (EWOULDBLOCK);
7289 7327                  }
7290 7328                  if (bp == stp->sd_mark) {
7291 7329                          mark |= _LASTMARK;
7292 7330                          stp->sd_mark = NULL;
7293 7331                  }
7294 7332          }
7295 7333  
7296 7334          /*
7297 7335           * keep track of the first message type
7298 7336           */
7299 7337          type = bp->b_datap->db_type;
7300 7338  
7301 7339          if (bp->b_datap->db_type == M_PASSFP) {
7302 7340                  if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7303 7341                          stp->sd_mark = bp;
7304 7342                  bp->b_flag |= mark & ~_LASTMARK;
7305 7343                  putback(stp, q, bp, pri);
7306 7344                  qbackenable(q, pri);
7307 7345                  mutex_exit(&stp->sd_lock);
7308 7346                  return (EBADMSG);
7309 7347          }
7310 7348          ASSERT(type != M_SIG);
7311 7349  
7312 7350          if (flags & MSG_IPEEK) {
7313 7351                  /*
7314 7352                   * Clear any struioflag - we do the uiomove over again
7315 7353                   * when peeking since it simplifies the code.
7316 7354                   *
7317 7355                   * Dup the message and put the original back on the queue.
7318 7356                   * If dupmsg() fails, try again with copymsg() to see if
7319 7357                   * there is indeed a shortage of memory.  dupmsg() may fail
7320 7358                   * if db_ref in any of the messages reaches its limit.
7321 7359                   */
7322 7360  
7323 7361                  if ((nbp = dupmsg(bp)) == NULL && (nbp = copymsg(bp)) == NULL) {
7324 7362                          /*
7325 7363                           * Restore the state of the stream head since we
7326 7364                           * need to drop sd_lock (strwaitbuf is sleeping).
7327 7365                           */
7328 7366                          size_t size = msgdsize(bp);
7329 7367  
7330 7368                          if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7331 7369                                  stp->sd_mark = bp;
7332 7370                          bp->b_flag |= mark & ~_LASTMARK;
7333 7371                          putback(stp, q, bp, pri);
7334 7372                          mutex_exit(&stp->sd_lock);
7335 7373                          error = strwaitbuf(size, BPRI_HI);
7336 7374                          if (error) {
7337 7375                                  /*
7338 7376                                   * There is no net change to the queue thus
7339 7377                                   * no need to qbackenable.
7340 7378                                   */
7341 7379                                  return (error);
7342 7380                          }
7343 7381                          goto retry;
7344 7382                  }
7345 7383  
7346 7384                  if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7347 7385                          stp->sd_mark = bp;
7348 7386                  bp->b_flag |= mark & ~_LASTMARK;
7349 7387                  putback(stp, q, bp, pri);
7350 7388                  bp = nbp;
7351 7389          }
7352 7390  
7353 7391          /*
7354 7392           * Set this flag so strrput will not generate signals. Need to
7355 7393           * make sure this flag is cleared before leaving this routine
7356 7394           * else signals will stop being sent.
7357 7395           */
7358 7396          stp->sd_flag |= STRGETINPROG;
7359 7397          mutex_exit(&stp->sd_lock);
7360 7398  
7361 7399          if ((stp->sd_rputdatafunc != NULL) && (DB_TYPE(bp) == M_DATA)) {
7362 7400                  mblk_t *tmp, *prevmp;
7363 7401  
7364 7402                  /*
7365 7403                   * Put first non-data mblk back to stream head and
7366 7404                   * cut the mblk chain so sd_rputdatafunc only sees
7367 7405                   * M_DATA mblks. We can skip the first mblk since it
7368 7406                   * is M_DATA according to the condition above.
7369 7407                   */
7370 7408                  for (prevmp = bp, tmp = bp->b_cont; tmp != NULL;
7371 7409                      prevmp = tmp, tmp = tmp->b_cont) {
7372 7410                          if (DB_TYPE(tmp) != M_DATA) {
7373 7411                                  prevmp->b_cont = NULL;
7374 7412                                  mutex_enter(&stp->sd_lock);
7375 7413                                  putback(stp, q, tmp, tmp->b_band);
7376 7414                                  mutex_exit(&stp->sd_lock);
7377 7415                                  break;
7378 7416                          }
7379 7417                  }
7380 7418  
7381 7419                  bp = (stp->sd_rputdatafunc)(stp->sd_vnode, bp,
7382 7420                      NULL, NULL, NULL, NULL);
7383 7421  
7384 7422                  if (bp == NULL)
7385 7423                          goto retry;
7386 7424          }
7387 7425  
7388 7426          if (STREAM_NEEDSERVICE(stp))
7389 7427                  stream_runservice(stp);
7390 7428  
7391 7429          /*
7392 7430           * Set HIPRI flag if message is priority.
7393 7431           */
7394 7432          if (type >= QPCTL)
7395 7433                  flg = MSG_HIPRI;
7396 7434          else
7397 7435                  flg = MSG_BAND;
7398 7436  
7399 7437          /*
7400 7438           * First process PROTO or PCPROTO blocks, if any.
7401 7439           */
7402 7440          if (mctlp != NULL && type != M_DATA) {
7403 7441                  mblk_t *nbp;
7404 7442  
7405 7443                  *mctlp = bp;
7406 7444                  while (bp->b_cont && bp->b_cont->b_datap->db_type != M_DATA)
7407 7445                          bp = bp->b_cont;
7408 7446                  nbp = bp->b_cont;
7409 7447                  bp->b_cont = NULL;
7410 7448                  bp = nbp;
7411 7449          }
7412 7450  
7413 7451          if (bp && bp->b_datap->db_type != M_DATA) {
7414 7452                  /*
7415 7453                   * More PROTO blocks in msg. Will only happen if mctlp is NULL.
7416 7454                   */
7417 7455                  more |= MORECTL;
7418 7456                  savemp = bp;
7419 7457                  while (bp && bp->b_datap->db_type != M_DATA) {
7420 7458                          savemptail = bp;
7421 7459                          bp = bp->b_cont;
7422 7460                  }
7423 7461                  savemptail->b_cont = NULL;
7424 7462          }
7425 7463  
7426 7464          /*
7427 7465           * Now process DATA blocks, if any.
7428 7466           */
7429 7467          if (uiop == NULL) {
7430 7468                  /* Append data to tail of mctlp */
7431 7469  
7432 7470                  if (mctlp != NULL) {
7433 7471                          mblk_t **mpp = mctlp;
7434 7472  
7435 7473                          while (*mpp != NULL)
7436 7474                                  mpp = &((*mpp)->b_cont);
7437 7475                          *mpp = bp;
7438 7476                          bp = NULL;
7439 7477                  }
7440 7478          } else if (uiop->uio_resid >= 0 && bp) {
7441 7479                  size_t oldresid = uiop->uio_resid;
7442 7480  
7443 7481                  /*
7444 7482                   * If a streams message is likely to consist
7445 7483                   * of many small mblks, it is pulled up into
7446 7484                   * one continuous chunk of memory.
7447 7485                   * The size of the first mblk may be bogus because
7448 7486                   * successive read() calls on the socket reduce
7449 7487                   * the size of this mblk until it is exhausted
7450 7488                   * and then the code walks on to the next. Thus
7451 7489                   * the size of the mblk may not be the original size
7452 7490                   * that was passed up, it's simply a remainder
7453 7491                   * and hence can be very small without any
7454 7492                   * implication that the packet is badly fragmented.
7455 7493                   * So the size of the possible second mblk is
7456 7494                   * used to spot a badly fragmented packet.
7457 7495                   * see longer comment at top of page
7458 7496                   * by mblk_pull_len declaration.
7459 7497                   */
7460 7498  
7461 7499                  if (bp->b_cont != NULL && MBLKL(bp->b_cont) < mblk_pull_len) {
7462 7500                          (void) pullupmsg(bp, -1);
7463 7501                  }
7464 7502  
7465 7503                  bp = struiocopyout(bp, uiop, &error);
7466 7504                  if (error != 0) {
7467 7505                          if (mctlp != NULL) {
7468 7506                                  freemsg(*mctlp);
7469 7507                                  *mctlp = NULL;
7470 7508                          } else
7471 7509                                  freemsg(savemp);
7472 7510                          mutex_enter(&stp->sd_lock);
7473 7511                          /*
7474 7512                           * clear stream head hi pri flag based on
7475 7513                           * first message
7476 7514                           */
7477 7515                          if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7478 7516                                  ASSERT(type == M_PCPROTO);
7479 7517                                  stp->sd_flag &= ~STRPRI;
7480 7518                          }
7481 7519                          more = 0;
7482 7520                          goto getmout;
7483 7521                  }
7484 7522                  /*
7485 7523                   * (pr == 1) indicates a partial read.
7486 7524                   */
7487 7525                  if (oldresid > uiop->uio_resid)
7488 7526                          pr = 1;
7489 7527          }
7490 7528  
7491 7529          if (bp) {                       /* more data blocks in msg */
7492 7530                  more |= MOREDATA;
7493 7531                  if (savemp)
7494 7532                          savemptail->b_cont = bp;
7495 7533                  else
7496 7534                          savemp = bp;
7497 7535          }
7498 7536  
7499 7537          mutex_enter(&stp->sd_lock);
7500 7538          if (savemp) {
7501 7539                  if (flags & (MSG_IPEEK|MSG_DISCARDTAIL)) {
7502 7540                          /*
7503 7541                           * When MSG_DISCARDTAIL is set or
7504 7542                           * when peeking discard any tail. When peeking this
7505 7543                           * is the tail of the dup that was copied out - the
7506 7544                           * message has already been putback on the queue.
7507 7545                           * Return MOREDATA to the caller even though the data
7508 7546                           * is discarded. This is used by sockets (to
7509 7547                           * set MSG_TRUNC).
7510 7548                           */
7511 7549                          freemsg(savemp);
7512 7550                          if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7513 7551                                  ASSERT(type == M_PCPROTO);
7514 7552                                  stp->sd_flag &= ~STRPRI;
7515 7553                          }
7516 7554                  } else if (pr && (savemp->b_datap->db_type == M_DATA) &&
7517 7555                      msgnodata(savemp)) {
7518 7556                          /*
7519 7557                           * Avoid queuing a zero-length tail part of
7520 7558                           * a message. pr=1 indicates that we read some of
7521 7559                           * the message.
7522 7560                           */
7523 7561                          freemsg(savemp);
7524 7562                          more &= ~MOREDATA;
7525 7563                          if (type >= QPCTL) {
7526 7564                                  ASSERT(type == M_PCPROTO);
7527 7565                                  stp->sd_flag &= ~STRPRI;
7528 7566                          }
7529 7567                  } else {
7530 7568                          savemp->b_band = pri;
7531 7569                          /*
7532 7570                           * If the first message was HIPRI and the one we're
7533 7571                           * putting back isn't, then clear STRPRI, otherwise
7534 7572                           * set STRPRI again.  Note that we must set STRPRI
7535 7573                           * again since the flush logic in strrput_nondata()
7536 7574                           * may have cleared it while we had sd_lock dropped.
7537 7575                           */
7538 7576  
7539 7577                          if (type >= QPCTL) {
7540 7578                                  ASSERT(type == M_PCPROTO);
7541 7579                                  if (queclass(savemp) < QPCTL)
7542 7580                                          stp->sd_flag &= ~STRPRI;
7543 7581                                  else
7544 7582                                          stp->sd_flag |= STRPRI;
7545 7583                          } else if (queclass(savemp) >= QPCTL) {
7546 7584                                  /*
7547 7585                                   * The first message was not a HIPRI message,
7548 7586                                   * but the one we are about to putback is.
7549 7587                                   * For simplicitly, we do not allow for HIPRI
7550 7588                                   * messages to be embedded in the message
7551 7589                                   * body, so just force it to same type as
7552 7590                                   * first message.
7553 7591                                   */
7554 7592                                  ASSERT(type == M_DATA || type == M_PROTO);
7555 7593                                  ASSERT(savemp->b_datap->db_type == M_PCPROTO);
7556 7594                                  savemp->b_datap->db_type = type;
7557 7595                          }
7558 7596                          if (mark != 0) {
7559 7597                                  if ((mark & _LASTMARK) &&
7560 7598                                      (stp->sd_mark == NULL)) {
7561 7599                                          /*
7562 7600                                           * If another marked message arrived
7563 7601                                           * while sd_lock was not held sd_mark
7564 7602                                           * would be non-NULL.
7565 7603                                           */
7566 7604                                          stp->sd_mark = savemp;
7567 7605                                  }
7568 7606                                  savemp->b_flag |= mark & ~_LASTMARK;
7569 7607                          }
7570 7608                          putback(stp, q, savemp, pri);
7571 7609                  }
7572 7610          } else if (!(flags & MSG_IPEEK)) {
7573 7611                  /*
7574 7612                   * The complete message was consumed.
7575 7613                   *
7576 7614                   * If another M_PCPROTO arrived while sd_lock was not held
7577 7615                   * it would have been discarded since STRPRI was still set.
7578 7616                   *
7579 7617                   * Move the MSG*MARKNEXT information
7580 7618                   * to the stream head just in case
7581 7619                   * the read queue becomes empty.
7582 7620                   * clear stream head hi pri flag based on
7583 7621                   * first message
7584 7622                   *
7585 7623                   * If the stream head was at the mark
7586 7624                   * (STRATMARK) before we dropped sd_lock above
7587 7625                   * and some data was consumed then we have
7588 7626                   * moved past the mark thus STRATMARK is
7589 7627                   * cleared. However, if a message arrived in
7590 7628                   * strrput during the copyout above causing
7591 7629                   * STRATMARK to be set we can not clear that
7592 7630                   * flag.
7593 7631                   * XXX A "perimeter" would help by single-threading strrput,
7594 7632                   * strread, strgetmsg and kstrgetmsg.
7595 7633                   */
7596 7634                  if (type >= QPCTL) {
7597 7635                          ASSERT(type == M_PCPROTO);
7598 7636                          stp->sd_flag &= ~STRPRI;
7599 7637                  }
7600 7638                  if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
7601 7639                          if (mark & MSGMARKNEXT) {
7602 7640                                  stp->sd_flag &= ~STRNOTATMARK;
7603 7641                                  stp->sd_flag |= STRATMARK;
7604 7642                          } else if (mark & MSGNOTMARKNEXT) {
7605 7643                                  stp->sd_flag &= ~STRATMARK;
7606 7644                                  stp->sd_flag |= STRNOTATMARK;
7607 7645                          } else {
7608 7646                                  stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
7609 7647                          }
7610 7648                  } else if (pr && (old_sd_flag & STRATMARK)) {
7611 7649                          stp->sd_flag &= ~STRATMARK;
7612 7650                  }
7613 7651          }
7614 7652  
7615 7653          *flagsp = flg;
7616 7654          *prip = pri;
7617 7655  
7618 7656          /*
7619 7657           * Getmsg cleanup processing - if the state of the queue has changed
7620 7658           * some signals may need to be sent and/or poll awakened.
7621 7659           */
7622 7660  getmout:
7623 7661          qbackenable(q, pri);
7624 7662  
7625 7663          /*
7626 7664           * We dropped the stream head lock above. Send all M_SIG messages
7627 7665           * before processing stream head for SIGPOLL messages.
7628 7666           */
7629 7667          ASSERT(MUTEX_HELD(&stp->sd_lock));
7630 7668          while ((bp = q->q_first) != NULL &&
7631 7669              (bp->b_datap->db_type == M_SIG)) {
7632 7670                  /*
7633 7671                   * sd_lock is held so the content of the read queue can not
7634 7672                   * change.
7635 7673                   */
7636 7674                  bp = getq(q);
7637 7675                  ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7638 7676  
7639 7677                  strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7640 7678                  mutex_exit(&stp->sd_lock);
7641 7679                  freemsg(bp);
7642 7680                  if (STREAM_NEEDSERVICE(stp))
7643 7681                          stream_runservice(stp);
7644 7682                  mutex_enter(&stp->sd_lock);
7645 7683          }
7646 7684  
7647 7685          /*
7648 7686           * stream head cannot change while we make the determination
7649 7687           * whether or not to send a signal. Drop the flag to allow strrput
7650 7688           * to send firstmsgsigs again.
7651 7689           */
7652 7690          stp->sd_flag &= ~STRGETINPROG;
7653 7691  
7654 7692          /*
7655 7693           * If the type of message at the front of the queue changed
7656 7694           * due to the receive the appropriate signals and pollwakeup events
7657 7695           * are generated. The type of changes are:
7658 7696           *      Processed a hipri message, q_first is not hipri.
7659 7697           *      Processed a band X message, and q_first is band Y.
7660 7698           * The generated signals and pollwakeups are identical to what
7661 7699           * strrput() generates should the message that is now on q_first
7662 7700           * arrive to an empty read queue.
7663 7701           *
7664 7702           * Note: only strrput will send a signal for a hipri message.
7665 7703           */
7666 7704          if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7667 7705                  strsigset_t signals = 0;
7668 7706                  strpollset_t pollwakeups = 0;
7669 7707  
7670 7708                  if (flg & MSG_HIPRI) {
7671 7709                          /*
7672 7710                           * Removed a hipri message. Regular data at
7673 7711                           * the front of  the queue.
7674 7712                           */
7675 7713                          if (bp->b_band == 0) {
7676 7714                                  signals = S_INPUT | S_RDNORM;
7677 7715                                  pollwakeups = POLLIN | POLLRDNORM;
7678 7716                          } else {
7679 7717                                  signals = S_INPUT | S_RDBAND;
7680 7718                                  pollwakeups = POLLIN | POLLRDBAND;
7681 7719                          }
7682 7720                  } else if (pri != bp->b_band) {
7683 7721                          /*
7684 7722                           * The band is different for the new q_first.
7685 7723                           */
7686 7724                          if (bp->b_band == 0) {
7687 7725                                  signals = S_RDNORM;
7688 7726                                  pollwakeups = POLLIN | POLLRDNORM;
7689 7727                          } else {
7690 7728                                  signals = S_RDBAND;
7691 7729                                  pollwakeups = POLLIN | POLLRDBAND;
7692 7730                          }
7693 7731                  }
7694 7732  
7695 7733                  if (pollwakeups != 0) {
7696 7734                          if (pollwakeups == (POLLIN | POLLRDNORM)) {
7697 7735                                  if (!(stp->sd_rput_opt & SR_POLLIN))
7698 7736                                          goto no_pollwake;
7699 7737                                  stp->sd_rput_opt &= ~SR_POLLIN;
7700 7738                          }
7701 7739                          mutex_exit(&stp->sd_lock);
7702 7740                          pollwakeup(&stp->sd_pollist, pollwakeups);
7703 7741                          mutex_enter(&stp->sd_lock);
7704 7742                  }
7705 7743  no_pollwake:
7706 7744  
7707 7745                  if (stp->sd_sigflags & signals)
7708 7746                          strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7709 7747          }
7710 7748          mutex_exit(&stp->sd_lock);
7711 7749  
7712 7750          rvp->r_val1 = more;
7713 7751          return (error);
7714 7752  #undef  _LASTMARK
7715 7753  }
7716 7754  
7717 7755  /*
7718 7756   * Put a message downstream.
7719 7757   *
7720 7758   * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7721 7759   */
7722 7760  int
7723 7761  strputmsg(
7724 7762          struct vnode *vp,
7725 7763          struct strbuf *mctl,
7726 7764          struct strbuf *mdata,
7727 7765          unsigned char pri,
7728 7766          int flag,
7729 7767          int fmode)
7730 7768  {
7731 7769          struct stdata *stp;
7732 7770          queue_t *wqp;
7733 7771          mblk_t *mp;
7734 7772          ssize_t msgsize;
7735 7773          ssize_t rmin, rmax;
7736 7774          int error;
7737 7775          struct uio uios;
7738 7776          struct uio *uiop = &uios;
7739 7777          struct iovec iovs;
7740 7778          int xpg4 = 0;
7741 7779  
7742 7780          ASSERT(vp->v_stream);
7743 7781          stp = vp->v_stream;
7744 7782          wqp = stp->sd_wrq;
7745 7783  
7746 7784          /*
7747 7785           * If it is an XPG4 application, we need to send
7748 7786           * SIGPIPE below
7749 7787           */
7750 7788  
7751 7789          xpg4 = (flag & MSG_XPG4) ? 1 : 0;
7752 7790          flag &= ~MSG_XPG4;
7753 7791  
7754 7792          if (AU_AUDITING())
7755 7793                  audit_strputmsg(vp, mctl, mdata, pri, flag, fmode);
7756 7794  
7757 7795          mutex_enter(&stp->sd_lock);
7758 7796  
7759 7797          if ((error = i_straccess(stp, JCWRITE)) != 0) {
7760 7798                  mutex_exit(&stp->sd_lock);
7761 7799                  return (error);
7762 7800          }
7763 7801  
7764 7802          if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7765 7803                  error = strwriteable(stp, B_FALSE, xpg4);
7766 7804                  if (error != 0) {
7767 7805                          mutex_exit(&stp->sd_lock);
7768 7806                          return (error);
7769 7807                  }
7770 7808          }
7771 7809  
7772 7810          mutex_exit(&stp->sd_lock);
7773 7811  
7774 7812          /*
7775 7813           * Check for legal flag value.
7776 7814           */
7777 7815          switch (flag) {
7778 7816          case MSG_HIPRI:
7779 7817                  if ((mctl->len < 0) || (pri != 0))
7780 7818                          return (EINVAL);
7781 7819                  break;
7782 7820          case MSG_BAND:
7783 7821                  break;
7784 7822  
7785 7823          default:
7786 7824                  return (EINVAL);
7787 7825          }
7788 7826  
7789 7827          TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_IN,
7790 7828              "strputmsg in:stp %p", stp);
7791 7829  
7792 7830          /* get these values from those cached in the stream head */
7793 7831          rmin = stp->sd_qn_minpsz;
7794 7832          rmax = stp->sd_qn_maxpsz;
7795 7833  
7796 7834          /*
7797 7835           * Make sure ctl and data sizes together fall within the
7798 7836           * limits of the max and min receive packet sizes and do
7799 7837           * not exceed system limit.
7800 7838           */
7801 7839          ASSERT((rmax >= 0) || (rmax == INFPSZ));
7802 7840          if (rmax == 0) {
7803 7841                  return (ERANGE);
7804 7842          }
7805 7843          /*
7806 7844           * Use the MAXIMUM of sd_maxblk and q_maxpsz.
7807 7845           * Needed to prevent partial failures in the strmakedata loop.
7808 7846           */
7809 7847          if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
7810 7848                  rmax = stp->sd_maxblk;
7811 7849  
7812 7850          if ((msgsize = mdata->len) < 0) {
7813 7851                  msgsize = 0;
7814 7852                  rmin = 0;       /* no range check for NULL data part */
7815 7853          }
7816 7854          if ((msgsize < rmin) ||
7817 7855              ((msgsize > rmax) && (rmax != INFPSZ)) ||
7818 7856              (mctl->len > strctlsz)) {
7819 7857                  return (ERANGE);
7820 7858          }
7821 7859  
7822 7860          /*
7823 7861           * Setup uio and iov for data part
7824 7862           */
7825 7863          iovs.iov_base = mdata->buf;
7826 7864          iovs.iov_len = msgsize;
7827 7865          uios.uio_iov = &iovs;
7828 7866          uios.uio_iovcnt = 1;
7829 7867          uios.uio_loffset = 0;
7830 7868          uios.uio_segflg = UIO_USERSPACE;
7831 7869          uios.uio_fmode = fmode;
7832 7870          uios.uio_extflg = UIO_COPY_DEFAULT;
7833 7871          uios.uio_resid = msgsize;
7834 7872          uios.uio_offset = 0;
7835 7873  
7836 7874          /* Ignore flow control in strput for HIPRI */
7837 7875          if (flag & MSG_HIPRI)
7838 7876                  flag |= MSG_IGNFLOW;
7839 7877  
7840 7878          for (;;) {
7841 7879                  int done = 0;
7842 7880  
7843 7881                  /*
7844 7882                   * strput will always free the ctl mblk - even when strput
7845 7883                   * fails.
7846 7884                   */
7847 7885                  if ((error = strmakectl(mctl, flag, fmode, &mp)) != 0) {
7848 7886                          TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7849 7887                              "strputmsg out:stp %p out %d error %d",
7850 7888                              stp, 1, error);
7851 7889                          return (error);
7852 7890                  }
7853 7891                  /*
7854 7892                   * Verify that the whole message can be transferred by
7855 7893                   * strput.
7856 7894                   */
7857 7895                  ASSERT(stp->sd_maxblk == INFPSZ ||
7858 7896                      stp->sd_maxblk >= mdata->len);
7859 7897  
7860 7898                  msgsize = mdata->len;
7861 7899                  error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
7862 7900                  mdata->len = msgsize;
7863 7901  
7864 7902                  if (error == 0)
7865 7903                          break;
7866 7904  
7867 7905                  if (error != EWOULDBLOCK)
7868 7906                          goto out;
7869 7907  
7870 7908                  mutex_enter(&stp->sd_lock);
7871 7909                  /*
7872 7910                   * Check for a missed wakeup.
7873 7911                   * Needed since strput did not hold sd_lock across
7874 7912                   * the canputnext.
7875 7913                   */
7876 7914                  if (bcanputnext(wqp, pri)) {
7877 7915                          /* Try again */
7878 7916                          mutex_exit(&stp->sd_lock);
7879 7917                          continue;
7880 7918                  }
7881 7919                  TRACE_2(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAIT,
7882 7920                      "strputmsg wait:stp %p waits pri %d", stp, pri);
7883 7921                  if (((error = strwaitq(stp, WRITEWAIT, (ssize_t)0, fmode, -1,
7884 7922                      &done)) != 0) || done) {
7885 7923                          mutex_exit(&stp->sd_lock);
7886 7924                          TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7887 7925                              "strputmsg out:q %p out %d error %d",
7888 7926                              stp, 0, error);
7889 7927                          return (error);
7890 7928                  }
7891 7929                  TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAKE,
7892 7930                      "strputmsg wake:stp %p wakes", stp);
7893 7931                  if ((error = i_straccess(stp, JCWRITE)) != 0) {
7894 7932                          mutex_exit(&stp->sd_lock);
7895 7933                          return (error);
7896 7934                  }
7897 7935                  mutex_exit(&stp->sd_lock);
7898 7936          }
7899 7937  out:
7900 7938          /*
7901 7939           * For historic reasons, applications expect EAGAIN
7902 7940           * when data mblk could not be allocated. so change
7903 7941           * ENOMEM back to EAGAIN
7904 7942           */
7905 7943          if (error == ENOMEM)
7906 7944                  error = EAGAIN;
7907 7945          TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7908 7946              "strputmsg out:stp %p out %d error %d", stp, 2, error);
7909 7947          return (error);
7910 7948  }
7911 7949  
7912 7950  /*
7913 7951   * Put a message downstream.
7914 7952   * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
7915 7953   * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
7916 7954   * and the fmode parameter.
7917 7955   *
7918 7956   * This routine handles the consolidation private flags:
7919 7957   *      MSG_IGNERROR    Ignore any stream head error except STPLEX.
7920 7958   *      MSG_HOLDSIG     Hold signals while waiting for data.
7921 7959   *      MSG_IGNFLOW     Don't check streams flow control.
7922 7960   *
7923 7961   * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7924 7962   */
7925 7963  int
7926 7964  kstrputmsg(
7927 7965          struct vnode *vp,
7928 7966          mblk_t *mctl,
7929 7967          struct uio *uiop,
7930 7968          ssize_t msgsize,
7931 7969          unsigned char pri,
7932 7970          int flag,
7933 7971          int fmode)
7934 7972  {
7935 7973          struct stdata *stp;
7936 7974          queue_t *wqp;
7937 7975          ssize_t rmin, rmax;
7938 7976          int error;
7939 7977  
7940 7978          ASSERT(vp->v_stream);
7941 7979          stp = vp->v_stream;
7942 7980          wqp = stp->sd_wrq;
7943 7981          if (AU_AUDITING())
7944 7982                  audit_strputmsg(vp, NULL, NULL, pri, flag, fmode);
7945 7983          if (mctl == NULL)
7946 7984                  return (EINVAL);
7947 7985  
7948 7986          mutex_enter(&stp->sd_lock);
7949 7987  
7950 7988          if ((error = i_straccess(stp, JCWRITE)) != 0) {
7951 7989                  mutex_exit(&stp->sd_lock);
7952 7990                  freemsg(mctl);
7953 7991                  return (error);
7954 7992          }
7955 7993  
7956 7994          if ((stp->sd_flag & STPLEX) || !(flag & MSG_IGNERROR)) {
7957 7995                  if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7958 7996                          error = strwriteable(stp, B_FALSE, B_TRUE);
7959 7997                          if (error != 0) {
7960 7998                                  mutex_exit(&stp->sd_lock);
7961 7999                                  freemsg(mctl);
7962 8000                                  return (error);
7963 8001                          }
7964 8002                  }
7965 8003          }
7966 8004  
7967 8005          mutex_exit(&stp->sd_lock);
7968 8006  
7969 8007          /*
7970 8008           * Check for legal flag value.
7971 8009           */
7972 8010          switch (flag & (MSG_HIPRI|MSG_BAND|MSG_ANY)) {
7973 8011          case MSG_HIPRI:
7974 8012                  if (pri != 0) {
7975 8013                          freemsg(mctl);
7976 8014                          return (EINVAL);
7977 8015                  }
7978 8016                  break;
7979 8017          case MSG_BAND:
7980 8018                  break;
7981 8019          default:
7982 8020                  freemsg(mctl);
7983 8021                  return (EINVAL);
7984 8022          }
7985 8023  
7986 8024          TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_IN,
7987 8025              "kstrputmsg in:stp %p", stp);
7988 8026  
7989 8027          /* get these values from those cached in the stream head */
7990 8028          rmin = stp->sd_qn_minpsz;
7991 8029          rmax = stp->sd_qn_maxpsz;
7992 8030  
7993 8031          /*
7994 8032           * Make sure ctl and data sizes together fall within the
7995 8033           * limits of the max and min receive packet sizes and do
7996 8034           * not exceed system limit.
7997 8035           */
7998 8036          ASSERT((rmax >= 0) || (rmax == INFPSZ));
7999 8037          if (rmax == 0) {
8000 8038                  freemsg(mctl);
8001 8039                  return (ERANGE);
8002 8040          }
8003 8041          /*
8004 8042           * Use the MAXIMUM of sd_maxblk and q_maxpsz.
8005 8043           * Needed to prevent partial failures in the strmakedata loop.
8006 8044           */
8007 8045          if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
8008 8046                  rmax = stp->sd_maxblk;
8009 8047  
8010 8048          if (uiop == NULL) {
8011 8049                  msgsize = -1;
8012 8050                  rmin = -1;      /* no range check for NULL data part */
8013 8051          } else {
8014 8052                  /* Use uio flags as well as the fmode parameter flags */
8015 8053                  fmode |= uiop->uio_fmode;
8016 8054  
8017 8055                  if ((msgsize < rmin) ||
8018 8056                      ((msgsize > rmax) && (rmax != INFPSZ))) {
8019 8057                          freemsg(mctl);
8020 8058                          return (ERANGE);
8021 8059                  }
8022 8060          }
8023 8061  
8024 8062          /* Ignore flow control in strput for HIPRI */
8025 8063          if (flag & MSG_HIPRI)
8026 8064                  flag |= MSG_IGNFLOW;
8027 8065  
8028 8066          for (;;) {
8029 8067                  int done = 0;
8030 8068                  int waitflag;
8031 8069                  mblk_t *mp;
8032 8070  
8033 8071                  /*
8034 8072                   * strput will always free the ctl mblk - even when strput
8035 8073                   * fails. If MSG_IGNFLOW is set then any error returned
8036 8074                   * will cause us to break the loop, so we don't need a copy
8037 8075                   * of the message. If MSG_IGNFLOW is not set, then we can
8038 8076                   * get hit by flow control and be forced to try again. In
8039 8077                   * this case we need to have a copy of the message. We
8040 8078                   * do this using copymsg since the message may get modified
8041 8079                   * by something below us.
8042 8080                   *
8043 8081                   * We've observed that many TPI providers do not check db_ref
8044 8082                   * on the control messages but blindly reuse them for the
8045 8083                   * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
8046 8084                   * friendly to such providers than using dupmsg. Also, note
8047 8085                   * that sockfs uses MSG_IGNFLOW for all TPI control messages.
8048 8086                   * Only data messages are subject to flow control, hence
8049 8087                   * subject to this copymsg.
8050 8088                   */
8051 8089                  if (flag & MSG_IGNFLOW) {
8052 8090                          mp = mctl;
8053 8091                          mctl = NULL;
8054 8092                  } else {
8055 8093                          do {
8056 8094                                  /*
8057 8095                                   * If a message has a free pointer, the message
8058 8096                                   * must be dupmsg to maintain this pointer.
8059 8097                                   * Code using this facility must be sure
8060 8098                                   * that modules below will not change the
8061 8099                                   * contents of the dblk without checking db_ref
8062 8100                                   * first. If db_ref is > 1, then the module
8063 8101                                   * needs to do a copymsg first. Otherwise,
8064 8102                                   * the contents of the dblk may become
8065 8103                                   * inconsistent because the freesmg/freeb below
8066 8104                                   * may end up calling atomic_add_32_nv.
8067 8105                                   * The atomic_add_32_nv in freeb (accessing
8068 8106                                   * all of db_ref, db_type, db_flags, and
8069 8107                                   * db_struioflag) does not prevent other threads
8070 8108                                   * from concurrently trying to modify e.g.
8071 8109                                   * db_type.
8072 8110                                   */
8073 8111                                  if (mctl->b_datap->db_frtnp != NULL)
8074 8112                                          mp = dupmsg(mctl);
8075 8113                                  else
8076 8114                                          mp = copymsg(mctl);
8077 8115  
8078 8116                                  if (mp != NULL)
8079 8117                                          break;
8080 8118  
8081 8119                                  error = strwaitbuf(msgdsize(mctl), BPRI_MED);
8082 8120                                  if (error) {
8083 8121                                          freemsg(mctl);
8084 8122                                          return (error);
8085 8123                                  }
8086 8124                          } while (mp == NULL);
8087 8125                  }
8088 8126                  /*
8089 8127                   * Verify that all of msgsize can be transferred by
8090 8128                   * strput.
8091 8129                   */
8092 8130                  ASSERT(stp->sd_maxblk == INFPSZ || stp->sd_maxblk >= msgsize);
8093 8131                  error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
8094 8132                  if (error == 0)
8095 8133                          break;
8096 8134  
8097 8135                  if (error != EWOULDBLOCK)
8098 8136                          goto out;
8099 8137  
8100 8138                  /*
8101 8139                   * IF MSG_IGNFLOW is set we should have broken out of loop
8102 8140                   * above.
8103 8141                   */
8104 8142                  ASSERT(!(flag & MSG_IGNFLOW));
8105 8143                  mutex_enter(&stp->sd_lock);
8106 8144                  /*
8107 8145                   * Check for a missed wakeup.
8108 8146                   * Needed since strput did not hold sd_lock across
8109 8147                   * the canputnext.
8110 8148                   */
8111 8149                  if (bcanputnext(wqp, pri)) {
8112 8150                          /* Try again */
8113 8151                          mutex_exit(&stp->sd_lock);
8114 8152                          continue;
8115 8153                  }
8116 8154                  TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAIT,
8117 8155                      "kstrputmsg wait:stp %p waits pri %d", stp, pri);
8118 8156  
8119 8157                  waitflag = WRITEWAIT;
8120 8158                  if (flag & (MSG_HOLDSIG|MSG_IGNERROR)) {
8121 8159                          if (flag & MSG_HOLDSIG)
8122 8160                                  waitflag |= STR_NOSIG;
8123 8161                          if (flag & MSG_IGNERROR)
8124 8162                                  waitflag |= STR_NOERROR;
8125 8163                  }
8126 8164                  if (((error = strwaitq(stp, waitflag,
8127 8165                      (ssize_t)0, fmode, -1, &done)) != 0) || done) {
8128 8166                          mutex_exit(&stp->sd_lock);
8129 8167                          TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8130 8168                              "kstrputmsg out:stp %p out %d error %d",
8131 8169                              stp, 0, error);
8132 8170                          freemsg(mctl);
8133 8171                          return (error);
8134 8172                  }
8135 8173                  TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAKE,
8136 8174                      "kstrputmsg wake:stp %p wakes", stp);
8137 8175                  if ((error = i_straccess(stp, JCWRITE)) != 0) {
8138 8176                          mutex_exit(&stp->sd_lock);
8139 8177                          freemsg(mctl);
8140 8178                          return (error);
8141 8179                  }
8142 8180                  mutex_exit(&stp->sd_lock);
8143 8181          }
8144 8182  out:
8145 8183          freemsg(mctl);
8146 8184          /*
8147 8185           * For historic reasons, applications expect EAGAIN
8148 8186           * when data mblk could not be allocated. so change
8149 8187           * ENOMEM back to EAGAIN
8150 8188           */
8151 8189          if (error == ENOMEM)
8152 8190                  error = EAGAIN;
8153 8191          TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8154 8192              "kstrputmsg out:stp %p out %d error %d", stp, 2, error);
8155 8193          return (error);
8156 8194  }
8157 8195  
8158 8196  /*
8159 8197   * Determines whether the necessary conditions are set on a stream
8160 8198   * for it to be readable, writeable, or have exceptions.
8161 8199   *
8162 8200   * strpoll handles the consolidation private events:
8163 8201   *      POLLNOERR       Do not return POLLERR even if there are stream
8164 8202   *                      head errors.
8165 8203   *                      Used by sockfs.
8166 8204   *      POLLRDDATA      Do not return POLLIN unless at least one message on
8167 8205   *                      the queue contains one or more M_DATA mblks. Thus
8168 8206   *                      when this flag is set a queue with only
8169 8207   *                      M_PROTO/M_PCPROTO mblks does not return POLLIN.
8170 8208   *                      Used by sockfs to ignore T_EXDATA_IND messages.
8171 8209   *
8172 8210   * Note: POLLRDDATA assumes that synch streams only return messages with
8173 8211   * an M_DATA attached (i.e. not messages consisting of only
8174 8212   * an M_PROTO/M_PCPROTO part).
8175 8213   */
8176 8214  int
8177 8215  strpoll(
8178 8216          struct stdata *stp,
8179 8217          short events_arg,
8180 8218          int anyyet,
8181 8219          short *reventsp,
8182 8220          struct pollhead **phpp)
8183 8221  {
8184 8222          int events = (ushort_t)events_arg;
8185 8223          int retevents = 0;
8186 8224          mblk_t *mp;
8187 8225          qband_t *qbp;
8188 8226          long sd_flags = stp->sd_flag;
8189 8227          int headlocked = 0;
8190 8228  
8191 8229          /*
8192 8230           * For performance, a single 'if' tests for most possible edge
8193 8231           * conditions in one shot
8194 8232           */
8195 8233          if (sd_flags & (STPLEX | STRDERR | STWRERR)) {
8196 8234                  if (sd_flags & STPLEX) {
8197 8235                          *reventsp = POLLNVAL;
8198 8236                          return (EINVAL);
8199 8237                  }
8200 8238                  if (((events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) &&
8201 8239                      (sd_flags & STRDERR)) ||
8202 8240                      ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) &&
8203 8241                      (sd_flags & STWRERR))) {
8204 8242                          if (!(events & POLLNOERR)) {
8205 8243                                  *reventsp = POLLERR;
8206 8244                                  return (0);
8207 8245                          }
8208 8246                  }
8209 8247          }
8210 8248          if (sd_flags & STRHUP) {
8211 8249                  retevents |= POLLHUP;
8212 8250          } else if (events & (POLLWRNORM | POLLWRBAND)) {
8213 8251                  queue_t *tq;
8214 8252                  queue_t *qp = stp->sd_wrq;
8215 8253  
8216 8254                  claimstr(qp);
8217 8255                  /* Find next module forward that has a service procedure */
8218 8256                  tq = qp->q_next->q_nfsrv;
8219 8257                  ASSERT(tq != NULL);
8220 8258  
8221 8259                  if (polllock(&stp->sd_pollist, QLOCK(tq)) != 0) {
8222 8260                          releasestr(qp);
8223 8261                          *reventsp = POLLNVAL;
8224 8262                          return (0);
8225 8263                  }
8226 8264                  if (events & POLLWRNORM) {
8227 8265                          queue_t *sqp;
8228 8266  
8229 8267                          if (tq->q_flag & QFULL)
8230 8268                                  /* ensure backq svc procedure runs */
8231 8269                                  tq->q_flag |= QWANTW;
8232 8270                          else if ((sqp = stp->sd_struiowrq) != NULL) {
8233 8271                                  /* Check sync stream barrier write q */
8234 8272                                  mutex_exit(QLOCK(tq));
8235 8273                                  if (polllock(&stp->sd_pollist,
8236 8274                                      QLOCK(sqp)) != 0) {
8237 8275                                          releasestr(qp);
8238 8276                                          *reventsp = POLLNVAL;
8239 8277                                          return (0);
8240 8278                                  }
8241 8279                                  if (sqp->q_flag & QFULL)
8242 8280                                          /* ensure pollwakeup() is done */
8243 8281                                          sqp->q_flag |= QWANTWSYNC;
8244 8282                                  else
8245 8283                                          retevents |= POLLOUT;
8246 8284                                  /* More write events to process ??? */
8247 8285                                  if (! (events & POLLWRBAND)) {
8248 8286                                          mutex_exit(QLOCK(sqp));
8249 8287                                          releasestr(qp);
8250 8288                                          goto chkrd;
8251 8289                                  }
8252 8290                                  mutex_exit(QLOCK(sqp));
8253 8291                                  if (polllock(&stp->sd_pollist,
8254 8292                                      QLOCK(tq)) != 0) {
8255 8293                                          releasestr(qp);
8256 8294                                          *reventsp = POLLNVAL;
8257 8295                                          return (0);
8258 8296                                  }
8259 8297                          } else
8260 8298                                  retevents |= POLLOUT;
8261 8299                  }
8262 8300                  if (events & POLLWRBAND) {
8263 8301                          qbp = tq->q_bandp;
8264 8302                          if (qbp) {
8265 8303                                  while (qbp) {
8266 8304                                          if (qbp->qb_flag & QB_FULL)
8267 8305                                                  qbp->qb_flag |= QB_WANTW;
8268 8306                                          else
8269 8307                                                  retevents |= POLLWRBAND;
8270 8308                                          qbp = qbp->qb_next;
8271 8309                                  }
8272 8310                          } else {
8273 8311                                  retevents |= POLLWRBAND;
8274 8312                          }
8275 8313                  }
8276 8314                  mutex_exit(QLOCK(tq));
8277 8315                  releasestr(qp);
8278 8316          }
8279 8317  chkrd:
8280 8318          if (sd_flags & STRPRI) {
8281 8319                  retevents |= (events & POLLPRI);
8282 8320          } else if (events & (POLLRDNORM | POLLRDBAND | POLLIN)) {
8283 8321                  queue_t *qp = _RD(stp->sd_wrq);
8284 8322                  int normevents = (events & (POLLIN | POLLRDNORM));
8285 8323  
8286 8324                  /*
8287 8325                   * Note: Need to do polllock() here since ps_lock may be
8288 8326                   * held. See bug 4191544.
8289 8327                   */
8290 8328                  if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8291 8329                          *reventsp = POLLNVAL;
8292 8330                          return (0);
8293 8331                  }
8294 8332                  headlocked = 1;
8295 8333                  mp = qp->q_first;
8296 8334                  while (mp) {
8297 8335                          /*
8298 8336                           * For POLLRDDATA we scan b_cont and b_next until we
8299 8337                           * find an M_DATA.
8300 8338                           */
8301 8339                          if ((events & POLLRDDATA) &&
8302 8340                              mp->b_datap->db_type != M_DATA) {
8303 8341                                  mblk_t *nmp = mp->b_cont;
8304 8342  
8305 8343                                  while (nmp != NULL &&
8306 8344                                      nmp->b_datap->db_type != M_DATA)
8307 8345                                          nmp = nmp->b_cont;
8308 8346                                  if (nmp == NULL) {
8309 8347                                          mp = mp->b_next;
8310 8348                                          continue;
8311 8349                                  }
8312 8350                          }
8313 8351                          if (mp->b_band == 0)
8314 8352                                  retevents |= normevents;
8315 8353                          else
8316 8354                                  retevents |= (events & (POLLIN | POLLRDBAND));
8317 8355                          break;
8318 8356                  }
8319 8357                  if (! (retevents & normevents) &&
8320 8358                      (stp->sd_wakeq & RSLEEP)) {
8321 8359                          /*
8322 8360                           * Sync stream barrier read queue has data.
8323 8361                           */
8324 8362                          retevents |= normevents;
8325 8363                  }
8326 8364                  /* Treat eof as normal data */
8327 8365                  if (sd_flags & STREOF)
8328 8366                          retevents |= normevents;
8329 8367          }
8330 8368  
8331 8369          *reventsp = (short)retevents;
8332 8370          if (retevents && !(events & POLLET)) {
8333 8371                  if (headlocked)
8334 8372                          mutex_exit(&stp->sd_lock);
8335 8373                  return (0);
8336 8374          }
8337 8375  
8338 8376          /*
8339 8377           * If poll() has not found any events yet, set up event cell
8340 8378           * to wake up the poll if a requested event occurs on this
8341 8379           * stream.  Check for collisions with outstanding poll requests.
8342 8380           */
8343 8381          if (!anyyet) {
8344 8382                  *phpp = &stp->sd_pollist;
8345 8383                  if (headlocked == 0) {
8346 8384                          if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8347 8385                                  *reventsp = POLLNVAL;
8348 8386                                  return (0);
8349 8387                          }
8350 8388                          headlocked = 1;
8351 8389                  }
8352 8390                  stp->sd_rput_opt |= SR_POLLIN;
8353 8391          }
8354 8392          if (headlocked)
8355 8393                  mutex_exit(&stp->sd_lock);
8356 8394          return (0);
8357 8395  }
8358 8396  
8359 8397  /*
8360 8398   * The purpose of putback() is to assure sleeping polls/reads
8361 8399   * are awakened when there are no new messages arriving at the,
8362 8400   * stream head, and a message is placed back on the read queue.
8363 8401   *
8364 8402   * sd_lock must be held when messages are placed back on stream
8365 8403   * head.  (getq() holds sd_lock when it removes messages from
8366 8404   * the queue)
8367 8405   */
8368 8406  
8369 8407  static void
8370 8408  putback(struct stdata *stp, queue_t *q, mblk_t *bp, int band)
8371 8409  {
8372 8410          mblk_t  *qfirst;
8373 8411          ASSERT(MUTEX_HELD(&stp->sd_lock));
8374 8412  
8375 8413          /*
8376 8414           * As a result of lock-step ordering around q_lock and sd_lock,
8377 8415           * it's possible for function calls like putnext() and
8378 8416           * canputnext() to get an inaccurate picture of how much
8379 8417           * data is really being processed at the stream head.
8380 8418           * We only consolidate with existing messages on the queue
8381 8419           * if the length of the message we want to put back is smaller
8382 8420           * than the queue hiwater mark.
8383 8421           */
8384 8422          if ((stp->sd_rput_opt & SR_CONSOL_DATA) &&
8385 8423              (DB_TYPE(bp) == M_DATA) && ((qfirst = q->q_first) != NULL) &&
8386 8424              (DB_TYPE(qfirst) == M_DATA) &&
8387 8425              ((qfirst->b_flag & (MSGMARK|MSGDELIM)) == 0) &&
8388 8426              ((bp->b_flag & (MSGMARK|MSGDELIM|MSGMARKNEXT)) == 0) &&
8389 8427              (mp_cont_len(bp, NULL) < q->q_hiwat)) {
8390 8428                  /*
8391 8429                   * We use the same logic as defined in strrput()
8392 8430                   * but in reverse as we are putting back onto the
8393 8431                   * queue and want to retain byte ordering.
8394 8432                   * Consolidate M_DATA messages with M_DATA ONLY.
8395 8433                   * strrput() allows the consolidation of M_DATA onto
8396 8434                   * M_PROTO | M_PCPROTO but not the other way round.
8397 8435                   *
8398 8436                   * The consolidation does not take place if the message
8399 8437                   * we are returning to the queue is marked with either
8400 8438                   * of the marks or the delim flag or if q_first
8401 8439                   * is marked with MSGMARK. The MSGMARK check is needed to
8402 8440                   * handle the odd semantics of MSGMARK where essentially
8403 8441                   * the whole message is to be treated as marked.
8404 8442                   * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from q_first
8405 8443                   * to the front of the b_cont chain.
8406 8444                   */
8407 8445                  rmvq_noenab(q, qfirst);
8408 8446  
8409 8447                  /*
8410 8448                   * The first message in the b_cont list
8411 8449                   * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
8412 8450                   * We need to handle the case where we
8413 8451                   * are appending:
8414 8452                   *
8415 8453                   * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
8416 8454                   * 2) a MSGMARKNEXT to a plain message.
8417 8455                   * 3) a MSGNOTMARKNEXT to a plain message
8418 8456                   * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
8419 8457                   *    message.
8420 8458                   *
8421 8459                   * Thus we never append a MSGMARKNEXT or
8422 8460                   * MSGNOTMARKNEXT to a MSGMARKNEXT message.
8423 8461                   */
8424 8462                  if (qfirst->b_flag & MSGMARKNEXT) {
8425 8463                          bp->b_flag |= MSGMARKNEXT;
8426 8464                          bp->b_flag &= ~MSGNOTMARKNEXT;
8427 8465                          qfirst->b_flag &= ~MSGMARKNEXT;
8428 8466                  } else if (qfirst->b_flag & MSGNOTMARKNEXT) {
8429 8467                          bp->b_flag |= MSGNOTMARKNEXT;
8430 8468                          qfirst->b_flag &= ~MSGNOTMARKNEXT;
8431 8469                  }
8432 8470  
8433 8471                  linkb(bp, qfirst);
8434 8472          }
8435 8473          (void) putbq(q, bp);
8436 8474  
8437 8475          /*
8438 8476           * A message may have come in when the sd_lock was dropped in the
8439 8477           * calling routine. If this is the case and STR*ATMARK info was
8440 8478           * received, need to move that from the stream head to the q_last
8441 8479           * so that SIOCATMARK can return the proper value.
8442 8480           */
8443 8481          if (stp->sd_flag & (STRATMARK | STRNOTATMARK)) {
8444 8482                  unsigned short *flagp = &q->q_last->b_flag;
8445 8483                  uint_t b_flag = (uint_t)*flagp;
8446 8484  
8447 8485                  if (stp->sd_flag & STRATMARK) {
8448 8486                          b_flag &= ~MSGNOTMARKNEXT;
8449 8487                          b_flag |= MSGMARKNEXT;
8450 8488                          stp->sd_flag &= ~STRATMARK;
8451 8489                  } else {
8452 8490                          b_flag &= ~MSGMARKNEXT;
8453 8491                          b_flag |= MSGNOTMARKNEXT;
8454 8492                          stp->sd_flag &= ~STRNOTATMARK;
8455 8493                  }
8456 8494                  *flagp = (unsigned short) b_flag;
8457 8495          }
8458 8496  
8459 8497  #ifdef  DEBUG
8460 8498          /*
8461 8499           * Make sure that the flags are not messed up.
8462 8500           */
8463 8501          {
8464 8502                  mblk_t *mp;
8465 8503                  mp = q->q_last;
8466 8504                  while (mp != NULL) {
8467 8505                          ASSERT((mp->b_flag & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
8468 8506                              (MSGMARKNEXT|MSGNOTMARKNEXT));
8469 8507                          mp = mp->b_cont;
8470 8508                  }
8471 8509          }
8472 8510  #endif
8473 8511          if (q->q_first == bp) {
8474 8512                  short pollevents;
8475 8513  
8476 8514                  if (stp->sd_flag & RSLEEP) {
8477 8515                          stp->sd_flag &= ~RSLEEP;
8478 8516                          cv_broadcast(&q->q_wait);
8479 8517                  }
8480 8518                  if (stp->sd_flag & STRPRI) {
8481 8519                          pollevents = POLLPRI;
8482 8520                  } else {
8483 8521                          if (band == 0) {
8484 8522                                  if (!(stp->sd_rput_opt & SR_POLLIN))
8485 8523                                          return;
8486 8524                                  stp->sd_rput_opt &= ~SR_POLLIN;
8487 8525                                  pollevents = POLLIN | POLLRDNORM;
8488 8526                          } else {
8489 8527                                  pollevents = POLLIN | POLLRDBAND;
8490 8528                          }
8491 8529                  }
8492 8530                  mutex_exit(&stp->sd_lock);
8493 8531                  pollwakeup(&stp->sd_pollist, pollevents);
8494 8532                  mutex_enter(&stp->sd_lock);
8495 8533          }
8496 8534  }
8497 8535  
8498 8536  /*
8499 8537   * Return the held vnode attached to the stream head of a
8500 8538   * given queue
8501 8539   * It is the responsibility of the calling routine to ensure
8502 8540   * that the queue does not go away (e.g. pop).
8503 8541   */
8504 8542  vnode_t *
8505 8543  strq2vp(queue_t *qp)
8506 8544  {
8507 8545          vnode_t *vp;
8508 8546          vp = STREAM(qp)->sd_vnode;
8509 8547          ASSERT(vp != NULL);
8510 8548          VN_HOLD(vp);
8511 8549          return (vp);
8512 8550  }
8513 8551  
8514 8552  /*
8515 8553   * return the stream head write queue for the given vp
8516 8554   * It is the responsibility of the calling routine to ensure
8517 8555   * that the stream or vnode do not close.
8518 8556   */
8519 8557  queue_t *
8520 8558  strvp2wq(vnode_t *vp)
8521 8559  {
8522 8560          ASSERT(vp->v_stream != NULL);
8523 8561          return (vp->v_stream->sd_wrq);
8524 8562  }
8525 8563  
8526 8564  /*
8527 8565   * pollwakeup stream head
8528 8566   * It is the responsibility of the calling routine to ensure
8529 8567   * that the stream or vnode do not close.
8530 8568   */
8531 8569  void
8532 8570  strpollwakeup(vnode_t *vp, short event)
8533 8571  {
8534 8572          ASSERT(vp->v_stream);
8535 8573          pollwakeup(&vp->v_stream->sd_pollist, event);
8536 8574  }
8537 8575  
8538 8576  /*
8539 8577   * Mate the stream heads of two vnodes together. If the two vnodes are the
8540 8578   * same, we just make the write-side point at the read-side -- otherwise,
8541 8579   * we do a full mate.  Only works on vnodes associated with streams that are
8542 8580   * still being built and thus have only a stream head.
8543 8581   */
8544 8582  void
8545 8583  strmate(vnode_t *vp1, vnode_t *vp2)
8546 8584  {
8547 8585          queue_t *wrq1 = strvp2wq(vp1);
8548 8586          queue_t *wrq2 = strvp2wq(vp2);
8549 8587  
8550 8588          /*
8551 8589           * Verify that there are no modules on the stream yet.  We also
8552 8590           * rely on the stream head always having a service procedure to
8553 8591           * avoid tweaking q_nfsrv.
8554 8592           */
8555 8593          ASSERT(wrq1->q_next == NULL && wrq2->q_next == NULL);
8556 8594          ASSERT(wrq1->q_qinfo->qi_srvp != NULL);
8557 8595          ASSERT(wrq2->q_qinfo->qi_srvp != NULL);
8558 8596  
8559 8597          /*
8560 8598           * If the queues are the same, just twist; otherwise do a full mate.
8561 8599           */
8562 8600          if (wrq1 == wrq2) {
8563 8601                  wrq1->q_next = _RD(wrq1);
8564 8602          } else {
8565 8603                  wrq1->q_next = _RD(wrq2);
8566 8604                  wrq2->q_next = _RD(wrq1);
8567 8605                  STREAM(wrq1)->sd_mate = STREAM(wrq2);
8568 8606                  STREAM(wrq1)->sd_flag |= STRMATE;
8569 8607                  STREAM(wrq2)->sd_mate = STREAM(wrq1);
8570 8608                  STREAM(wrq2)->sd_flag |= STRMATE;
8571 8609          }
8572 8610  }
8573 8611  
8574 8612  /*
8575 8613   * XXX will go away when console is correctly fixed.
8576 8614   * Clean up the console PIDS, from previous I_SETSIG,
8577 8615   * called only for cnopen which never calls strclean().
8578 8616   */
8579 8617  void
8580 8618  str_cn_clean(struct vnode *vp)
8581 8619  {
8582 8620          strsig_t *ssp, *pssp, *tssp;
8583 8621          struct stdata *stp;
8584 8622          struct pid  *pidp;
8585 8623          int update = 0;
8586 8624  
8587 8625          ASSERT(vp->v_stream);
8588 8626          stp = vp->v_stream;
8589 8627          pssp = NULL;
8590 8628          mutex_enter(&stp->sd_lock);
8591 8629          ssp = stp->sd_siglist;
8592 8630          while (ssp) {
8593 8631                  mutex_enter(&pidlock);
8594 8632                  pidp = ssp->ss_pidp;
8595 8633                  /*
8596 8634                   * Get rid of PID if the proc is gone.
8597 8635                   */
8598 8636                  if (pidp->pid_prinactive) {
8599 8637                          tssp = ssp->ss_next;
8600 8638                          if (pssp)
8601 8639                                  pssp->ss_next = tssp;
8602 8640                          else
8603 8641                                  stp->sd_siglist = tssp;
8604 8642                          ASSERT(pidp->pid_ref <= 1);
8605 8643                          PID_RELE(ssp->ss_pidp);
8606 8644                          mutex_exit(&pidlock);
8607 8645                          kmem_free(ssp, sizeof (strsig_t));
8608 8646                          update = 1;
8609 8647                          ssp = tssp;
8610 8648                          continue;
8611 8649                  } else
8612 8650                          mutex_exit(&pidlock);
8613 8651                  pssp = ssp;
8614 8652                  ssp = ssp->ss_next;
8615 8653          }
8616 8654          if (update) {
8617 8655                  stp->sd_sigflags = 0;
8618 8656                  for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
8619 8657                          stp->sd_sigflags |= ssp->ss_events;
8620 8658          }
8621 8659          mutex_exit(&stp->sd_lock);
8622 8660  }
8623 8661  
8624 8662  /*
8625 8663   * Return B_TRUE if there is data in the message, B_FALSE otherwise.
8626 8664   */
8627 8665  static boolean_t
8628 8666  msghasdata(mblk_t *bp)
8629 8667  {
8630 8668          for (; bp; bp = bp->b_cont)
8631 8669                  if (bp->b_datap->db_type == M_DATA) {
8632 8670                          ASSERT(bp->b_wptr >= bp->b_rptr);
8633 8671                          if (bp->b_wptr > bp->b_rptr)
8634 8672                                  return (B_TRUE);
8635 8673                  }
8636 8674          return (B_FALSE);
8637 8675  }
  
    | 
      ↓ open down ↓ | 
    5447 lines elided | 
    
      ↑ open up ↑ | 
  
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX