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