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