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