loader: Minor cosmetic changes.
[unleashed.git] / kernel / os / streamio.c
blob337f9ff49f970155c3320258bbdc0445f45c58f1
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All Rights Reserved */
26 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Copyright 2015, Joyent, Inc. All rights reserved.
30 #include <sys/types.h>
31 #include <sys/sysmacros.h>
32 #include <sys/param.h>
33 #include <sys/errno.h>
34 #include <sys/signal.h>
35 #include <sys/stat.h>
36 #include <sys/proc.h>
37 #include <sys/cred.h>
38 #include <sys/user.h>
39 #include <sys/vnode.h>
40 #include <sys/file.h>
41 #include <sys/stream.h>
42 #include <sys/strsubr.h>
43 #include <sys/stropts.h>
44 #include <sys/tihdr.h>
45 #include <sys/var.h>
46 #include <sys/poll.h>
47 #include <sys/termio.h>
48 #include <sys/ttold.h>
49 #include <sys/systm.h>
50 #include <sys/uio.h>
51 #include <sys/cmn_err.h>
52 #include <sys/sad.h>
53 #include <sys/netstack.h>
54 #include <sys/priocntl.h>
55 #include <sys/jioctl.h>
56 #include <sys/procset.h>
57 #include <sys/session.h>
58 #include <sys/kmem.h>
59 #include <sys/filio.h>
60 #include <sys/vtrace.h>
61 #include <sys/debug.h>
62 #include <sys/strredir.h>
63 #include <sys/fs/fifonode.h>
64 #include <sys/fs/snode.h>
65 #include <sys/strlog.h>
66 #include <sys/strsun.h>
67 #include <sys/project.h>
68 #include <sys/kbio.h>
69 #include <sys/msio.h>
70 #include <sys/tty.h>
71 #include <sys/ptyvar.h>
72 #include <sys/vuid_event.h>
73 #include <sys/modctl.h>
74 #include <sys/sunddi.h>
75 #include <sys/sunldi_impl.h>
76 #include <sys/autoconf.h>
77 #include <sys/policy.h>
78 #include <sys/dld.h>
79 #include <sys/zone.h>
80 #include <sys/limits.h>
81 #include <c2/audit.h>
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().)
91 #define i_straccess(x, y) ((stp->sd_sidp == NULL) ? 0 : \
92 (stp->sd_vnode->v_type == VFIFO) ? 0 : \
93 straccess((x), (y)))
96 * what is mblk_pull_len?
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.
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.
109 * This heuristic was decided upon after performance tests
110 * indicated that anything more complex slowed down the main
111 * code path.
113 #define MBLK_PULL_LEN 64
114 uint32_t mblk_pull_len = MBLK_PULL_LEN;
117 * The sgttyb_handling flag controls the handling of the old BSD
118 * TIOCGETP, TIOCSETP, and TIOCSETN ioctls as follows:
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).
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."
132 int sgttyb_handling = 1;
133 static boolean_t sgttyb_complaint;
135 /* don't push drcompat module by default on Style-2 streams */
136 static int push_drcompat = 0;
139 * id value used to distinguish between different ioctl messages
141 static uint32_t ioc_id;
143 static void putback(struct stdata *, queue_t *, mblk_t *, int);
144 static void strcleanall(struct vnode *);
145 static int strwsrv(queue_t *);
146 static int strdocmd(struct stdata *, struct strcmd *, cred_t *);
149 * qinit and module_info structures for stream head read and write queues
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 };
161 extern kmutex_t strresources; /* protects global resources */
162 extern kmutex_t muxifier; /* single-threads multiplexor creation */
164 static boolean_t msghasdata(mblk_t *bp);
165 #define msgnodata(bp) (!msghasdata(bp))
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.
194 static int
195 push_mod(queue_t *qp, dev_t *devp, struct stdata *stp, const char *name,
196 int anchor, cred_t *crp, uint_t anchor_zoneid)
198 int error;
199 fmodsw_impl_t *fp;
201 if (stp->sd_flag & (STRHUP|STRDERR|STWRERR)) {
202 error = (stp->sd_flag & STRHUP) ? ENXIO : EIO;
203 return (error);
205 if (stp->sd_pushcnt >= nstrpush) {
206 return (EINVAL);
209 if ((fp = fmodsw_find(name, FMODSW_HOLD | FMODSW_LOAD)) == NULL) {
210 stp->sd_flag |= STREOPENFAIL;
211 return (EINVAL);
215 * push new module and call its open routine via qattach
217 if ((error = qattach(qp, devp, 0, crp, fp, B_FALSE)) != 0)
218 return (error);
221 * Check to see if caller wants a STREAMS anchor
222 * put at this place in the stream, and add if so.
224 mutex_enter(&stp->sd_lock);
225 if (anchor == stp->sd_pushcnt) {
226 stp->sd_anchor = stp->sd_pushcnt;
227 stp->sd_anchorzone = anchor_zoneid;
229 mutex_exit(&stp->sd_lock);
231 return (0);
235 * Open a stream device.
238 stropen(vnode_t *vp, dev_t *devp, int flag, cred_t *crp)
240 struct stdata *stp;
241 queue_t *qp;
242 int s;
243 dev_t dummydev, savedev;
244 struct autopush *ap;
245 struct dlautopush dlap;
246 int error = 0;
247 ssize_t rmin, rmax;
248 int cloneopen;
249 queue_t *brq;
250 major_t major;
251 str_stack_t *ss;
252 zoneid_t zoneid;
253 uint_t anchor;
256 * If the stream already exists, wait for any open in progress
257 * to complete, then call the open function of each module and
258 * driver in the stream. Otherwise create the stream.
260 TRACE_1(TR_FAC_STREAMS_FR, TR_STROPEN, "stropen:%p", vp);
261 retry:
262 mutex_enter(&vp->v_lock);
263 if ((stp = vp->v_stream) != NULL) {
266 * Waiting for stream to be created to device
267 * due to another open.
269 mutex_exit(&vp->v_lock);
271 if (STRMATED(stp)) {
272 struct stdata *strmatep = stp->sd_mate;
274 STRLOCKMATES(stp);
275 if (strmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
276 if (flag & (FNDELAY|FNONBLOCK)) {
277 error = EAGAIN;
278 mutex_exit(&strmatep->sd_lock);
279 goto ckreturn;
281 mutex_exit(&stp->sd_lock);
282 if (!cv_wait_sig(&strmatep->sd_monitor,
283 &strmatep->sd_lock)) {
284 error = EINTR;
285 mutex_exit(&strmatep->sd_lock);
286 mutex_enter(&stp->sd_lock);
287 goto ckreturn;
289 mutex_exit(&strmatep->sd_lock);
290 goto retry;
292 if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
293 if (flag & (FNDELAY|FNONBLOCK)) {
294 error = EAGAIN;
295 mutex_exit(&strmatep->sd_lock);
296 goto ckreturn;
298 mutex_exit(&strmatep->sd_lock);
299 if (!cv_wait_sig(&stp->sd_monitor,
300 &stp->sd_lock)) {
301 error = EINTR;
302 goto ckreturn;
304 mutex_exit(&stp->sd_lock);
305 goto retry;
308 if (stp->sd_flag & (STRDERR|STWRERR)) {
309 error = EIO;
310 mutex_exit(&strmatep->sd_lock);
311 goto ckreturn;
314 stp->sd_flag |= STWOPEN;
315 STRUNLOCKMATES(stp);
316 } else {
317 mutex_enter(&stp->sd_lock);
318 if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
319 if (flag & (FNDELAY|FNONBLOCK)) {
320 error = EAGAIN;
321 goto ckreturn;
323 if (!cv_wait_sig(&stp->sd_monitor,
324 &stp->sd_lock)) {
325 error = EINTR;
326 goto ckreturn;
328 mutex_exit(&stp->sd_lock);
329 goto retry; /* could be clone! */
332 if (stp->sd_flag & (STRDERR|STWRERR)) {
333 error = EIO;
334 goto ckreturn;
337 stp->sd_flag |= STWOPEN;
338 mutex_exit(&stp->sd_lock);
342 * Open all modules and devices down stream to notify
343 * that another user is streaming. For modules, set the
344 * last argument to MODOPEN and do not pass any open flags.
345 * Ignore dummydev since this is not the first open.
347 claimstr(stp->sd_wrq);
348 qp = stp->sd_wrq;
349 while (_SAMESTR(qp)) {
350 qp = qp->q_next;
351 if ((error = qreopen(_RD(qp), devp, flag, crp)) != 0)
352 break;
354 releasestr(stp->sd_wrq);
355 mutex_enter(&stp->sd_lock);
356 stp->sd_flag &= ~(STRHUP|STWOPEN|STRDERR|STWRERR);
357 stp->sd_rerror = 0;
358 stp->sd_werror = 0;
359 ckreturn:
360 cv_broadcast(&stp->sd_monitor);
361 mutex_exit(&stp->sd_lock);
362 return (error);
366 * This vnode isn't streaming. SPECFS already
367 * checked for multiple vnodes pointing to the
368 * same stream, so create a stream to the driver.
370 qp = allocq();
371 stp = shalloc(qp);
374 * Initialize stream head. shalloc() has given us
375 * exclusive access, and we have the vnode locked;
376 * we can do whatever we want with stp.
378 stp->sd_flag = STWOPEN;
379 stp->sd_siglist = NULL;
380 stp->sd_pollist.ph_list = NULL;
381 stp->sd_sigflags = 0;
382 stp->sd_mark = NULL;
383 stp->sd_closetime = STRTIMOUT;
384 stp->sd_sidp = NULL;
385 stp->sd_pgidp = NULL;
386 stp->sd_vnode = vp;
387 stp->sd_rerror = 0;
388 stp->sd_werror = 0;
389 stp->sd_wroff = 0;
390 stp->sd_tail = 0;
391 stp->sd_iocblk = NULL;
392 stp->sd_cmdblk = NULL;
393 stp->sd_pushcnt = 0;
394 stp->sd_qn_minpsz = 0;
395 stp->sd_qn_maxpsz = INFPSZ - 1; /* used to check for initialization */
396 stp->sd_maxblk = INFPSZ;
397 qp->q_ptr = _WR(qp)->q_ptr = stp;
398 STREAM(qp) = STREAM(_WR(qp)) = stp;
399 vp->v_stream = stp;
400 mutex_exit(&vp->v_lock);
401 if (vp->v_type == VFIFO) {
402 stp->sd_flag |= OLDNDELAY;
404 * This means, both for pipes and fifos
405 * strwrite will send SIGPIPE if the other
406 * end is closed. For putmsg it depends
407 * on whether it is a XPG4_2 application
408 * or not
410 stp->sd_wput_opt = SW_SIGPIPE;
412 /* setq might sleep in kmem_alloc - avoid holding locks. */
413 setq(qp, &fifo_strdata, &fifo_stwdata, NULL, QMTSAFE,
414 SQ_CI|SQ_CO, B_FALSE);
416 set_qend(qp);
417 stp->sd_strtab = fifo_getinfo();
418 _WR(qp)->q_nfsrv = _WR(qp);
419 qp->q_nfsrv = qp;
421 * Wake up others that are waiting for stream to be created.
423 mutex_enter(&stp->sd_lock);
425 * nothing is be pushed on stream yet, so
426 * optimized stream head packetsizes are just that
427 * of the read queue
429 stp->sd_qn_minpsz = qp->q_minpsz;
430 stp->sd_qn_maxpsz = qp->q_maxpsz;
431 stp->sd_flag &= ~STWOPEN;
432 goto fifo_opendone;
434 /* setq might sleep in kmem_alloc - avoid holding locks. */
435 setq(qp, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_FALSE);
437 set_qend(qp);
440 * Open driver and create stream to it (via qattach).
442 savedev = *devp;
443 cloneopen = (getmajor(*devp) == clone_major);
444 if ((error = qattach(qp, devp, flag, crp, NULL, B_FALSE)) != 0) {
445 mutex_enter(&vp->v_lock);
446 vp->v_stream = NULL;
447 mutex_exit(&vp->v_lock);
448 mutex_enter(&stp->sd_lock);
449 cv_broadcast(&stp->sd_monitor);
450 mutex_exit(&stp->sd_lock);
451 freeq(_RD(qp));
452 shfree(stp);
453 return (error);
456 * Set sd_strtab after open in order to handle clonable drivers
458 stp->sd_strtab = STREAMSTAB(getmajor(*devp));
461 * Historical note: dummydev used to be be prior to the initial
462 * open (via qattach above), which made the value seen
463 * inconsistent between an I_PUSH and an autopush of a module.
465 dummydev = *devp;
468 * For clone open of old style (Q not associated) network driver,
469 * push DRMODNAME module to handle DL_ATTACH/DL_DETACH
471 brq = _RD(_WR(qp)->q_next);
472 major = getmajor(*devp);
473 if (push_drcompat && cloneopen && NETWORK_DRV(major) &&
474 ((brq->q_flag & _QASSOCIATED) == 0)) {
475 if (push_mod(qp, &dummydev, stp, DRMODNAME, 0, crp, 0) != 0)
476 cmn_err(CE_WARN, "cannot push " DRMODNAME
477 " streams module");
480 if (!NETWORK_DRV(major)) {
481 savedev = *devp;
482 } else {
484 * For network devices, process differently based on the
485 * return value from dld_autopush():
487 * 0: the passed-in device points to a GLDv3 datalink with
488 * per-link autopush configuration; use that configuration
489 * and ignore any per-driver autopush configuration.
491 * 1: the passed-in device points to a physical GLDv3
492 * datalink without per-link autopush configuration. The
493 * passed in device was changed to refer to the actual
494 * physical device (if it's not already); we use that new
495 * device to look up any per-driver autopush configuration.
497 * -1: neither of the above cases applied; use the initial
498 * device to look up any per-driver autopush configuration.
500 switch (dld_autopush(&savedev, &dlap)) {
501 case 0:
502 zoneid = crgetzoneid(crp);
503 for (s = 0; s < dlap.dap_npush; s++) {
504 error = push_mod(qp, &dummydev, stp,
505 dlap.dap_aplist[s], dlap.dap_anchor, crp,
506 zoneid);
507 if (error != 0)
508 break;
510 goto opendone;
511 case 1:
512 break;
513 case -1:
514 savedev = *devp;
515 break;
519 * Find the autopush configuration based on "savedev". Start with the
520 * global zone. If not found check in the local zone.
522 zoneid = GLOBAL_ZONEID;
523 retryap:
524 ss = netstack_find_by_stackid(zoneid_to_netstackid(zoneid))->
525 netstack_str;
526 if ((ap = sad_ap_find_by_dev(savedev, ss)) == NULL) {
527 netstack_rele(ss->ss_netstack);
528 if (zoneid == GLOBAL_ZONEID) {
530 * None found. Also look in the zone's autopush table.
532 zoneid = crgetzoneid(crp);
533 if (zoneid != GLOBAL_ZONEID)
534 goto retryap;
536 goto opendone;
538 anchor = ap->ap_anchor;
539 zoneid = crgetzoneid(crp);
540 for (s = 0; s < ap->ap_npush; s++) {
541 error = push_mod(qp, &dummydev, stp, ap->ap_list[s],
542 anchor, crp, zoneid);
543 if (error != 0)
544 break;
546 sad_ap_rele(ap, ss);
547 netstack_rele(ss->ss_netstack);
549 opendone:
552 * let specfs know that open failed part way through
554 if (error) {
555 mutex_enter(&stp->sd_lock);
556 stp->sd_flag |= STREOPENFAIL;
557 mutex_exit(&stp->sd_lock);
561 * Wake up others that are waiting for stream to be created.
563 mutex_enter(&stp->sd_lock);
564 stp->sd_flag &= ~STWOPEN;
567 * As a performance concern we are caching the values of
568 * q_minpsz and q_maxpsz of the module below the stream
569 * head in the stream head.
571 mutex_enter(QLOCK(stp->sd_wrq->q_next));
572 rmin = stp->sd_wrq->q_next->q_minpsz;
573 rmax = stp->sd_wrq->q_next->q_maxpsz;
574 mutex_exit(QLOCK(stp->sd_wrq->q_next));
576 /* do this processing here as a performance concern */
577 if (strmsgsz != 0) {
578 if (rmax == INFPSZ)
579 rmax = strmsgsz;
580 else
581 rmax = MIN(strmsgsz, rmax);
584 mutex_enter(QLOCK(stp->sd_wrq));
585 stp->sd_qn_minpsz = rmin;
586 stp->sd_qn_maxpsz = rmax;
587 mutex_exit(QLOCK(stp->sd_wrq));
589 fifo_opendone:
590 cv_broadcast(&stp->sd_monitor);
591 mutex_exit(&stp->sd_lock);
592 return (error);
595 static int strsink(queue_t *, mblk_t *);
596 static struct qinit deadrend = {
597 strsink, NULL, NULL, NULL, NULL, &strm_info, NULL
599 static struct qinit deadwend = {
600 NULL, NULL, NULL, NULL, NULL, &stwm_info, NULL
604 * Close a stream.
605 * This is called from closef() on the last close of an open stream.
606 * Strclean() will already have removed the siglist and pollist
607 * information, so all that remains is to remove all multiplexor links
608 * for the stream, pop all the modules (and the driver), and free the
609 * stream structure.
613 strclose(struct vnode *vp, int flag, cred_t *crp)
615 struct stdata *stp;
616 queue_t *qp;
617 int rval;
618 int freestp = 1;
619 queue_t *rmq;
621 TRACE_1(TR_FAC_STREAMS_FR,
622 TR_STRCLOSE, "strclose:%p", vp);
623 ASSERT(vp->v_stream);
625 stp = vp->v_stream;
626 ASSERT(!(stp->sd_flag & STPLEX));
627 qp = stp->sd_wrq;
630 * Needed so that strpoll will return non-zero for this fd.
631 * Note that with POLLNOERR STRHUP does still cause POLLHUP.
633 mutex_enter(&stp->sd_lock);
634 stp->sd_flag |= STRHUP;
635 mutex_exit(&stp->sd_lock);
638 * If the registered process or process group did not have an
639 * open instance of this stream then strclean would not be
640 * called. Thus at the time of closing all remaining siglist entries
641 * are removed.
643 if (stp->sd_siglist != NULL)
644 strcleanall(vp);
646 ASSERT(stp->sd_siglist == NULL);
647 ASSERT(stp->sd_sigflags == 0);
649 if (STRMATED(stp)) {
650 struct stdata *strmatep = stp->sd_mate;
651 int waited = 1;
653 STRLOCKMATES(stp);
654 while (waited) {
655 waited = 0;
656 while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
657 mutex_exit(&strmatep->sd_lock);
658 cv_wait(&stp->sd_monitor, &stp->sd_lock);
659 mutex_exit(&stp->sd_lock);
660 STRLOCKMATES(stp);
661 waited = 1;
663 while (strmatep->sd_flag &
664 (STWOPEN|STRCLOSE|STRPLUMB)) {
665 mutex_exit(&stp->sd_lock);
666 cv_wait(&strmatep->sd_monitor,
667 &strmatep->sd_lock);
668 mutex_exit(&strmatep->sd_lock);
669 STRLOCKMATES(stp);
670 waited = 1;
673 stp->sd_flag |= STRCLOSE;
674 STRUNLOCKMATES(stp);
675 } else {
676 mutex_enter(&stp->sd_lock);
677 stp->sd_flag |= STRCLOSE;
678 mutex_exit(&stp->sd_lock);
681 ASSERT(qp->q_first == NULL); /* No more delayed write */
683 /* Check if an I_LINK was ever done on this stream */
684 if (stp->sd_flag & STRHASLINKS) {
685 netstack_t *ns;
686 str_stack_t *ss;
688 ns = netstack_find_by_cred(crp);
689 ASSERT(ns != NULL);
690 ss = ns->netstack_str;
691 ASSERT(ss != NULL);
693 (void) munlinkall(stp, LINKCLOSE|LINKNORMAL, crp, &rval, ss);
694 netstack_rele(ss->ss_netstack);
697 while (_SAMESTR(qp)) {
699 * Holding sd_lock prevents q_next from changing in
700 * this stream.
702 mutex_enter(&stp->sd_lock);
703 if (!(flag & (FNDELAY|FNONBLOCK)) && (stp->sd_closetime > 0)) {
706 * sleep until awakened by strwsrv() or timeout
708 for (;;) {
709 mutex_enter(QLOCK(qp->q_next));
710 if (!(qp->q_next->q_mblkcnt)) {
711 mutex_exit(QLOCK(qp->q_next));
712 break;
714 stp->sd_flag |= WSLEEP;
716 /* ensure strwsrv gets enabled */
717 qp->q_next->q_flag |= QWANTW;
718 mutex_exit(QLOCK(qp->q_next));
719 /* get out if we timed out or recv'd a signal */
720 if (str_cv_wait(&qp->q_wait, &stp->sd_lock,
721 stp->sd_closetime, 0) <= 0) {
722 break;
725 stp->sd_flag &= ~WSLEEP;
727 mutex_exit(&stp->sd_lock);
729 rmq = qp->q_next;
730 if (rmq->q_flag & QISDRV) {
731 ASSERT(!_SAMESTR(rmq));
732 wait_sq_svc(_RD(qp)->q_syncq);
735 qdetach(_RD(rmq), 1, flag, crp, B_FALSE);
739 * Since we call pollwakeup in close() now, the poll list should
740 * be empty in most cases. The only exception is the layered devices
741 * (e.g. the console drivers with redirection modules pushed on top
742 * of it). We have to do this after calling qdetach() because
743 * the redirection module won't have torn down the console
744 * redirection until after qdetach() has been invoked.
746 if (stp->sd_pollist.ph_list != NULL) {
747 pollwakeup(&stp->sd_pollist, POLLERR);
748 pollhead_clean(&stp->sd_pollist);
750 ASSERT(stp->sd_pollist.ph_list == NULL);
751 ASSERT(stp->sd_sidp == NULL);
752 ASSERT(stp->sd_pgidp == NULL);
754 /* Prevent qenable from re-enabling the stream head queue */
755 disable_svc(_RD(qp));
758 * Wait until service procedure of each queue is
759 * run, if QINSERVICE is set.
761 wait_svc(_RD(qp));
764 * Now, flush both queues.
766 flushq(_RD(qp), FLUSHALL);
767 flushq(qp, FLUSHALL);
770 * If the write queue of the stream head is pointing to a
771 * read queue, we have a twisted stream. If the read queue
772 * is alive, convert the stream head queues into a dead end.
773 * If the read queue is dead, free the dead pair.
775 if (qp->q_next && !_SAMESTR(qp)) {
776 if (qp->q_next->q_qinfo == &deadrend) { /* half-closed pipe */
777 flushq(qp->q_next, FLUSHALL); /* ensure no message */
778 shfree(qp->q_next->q_stream);
779 freeq(qp->q_next);
780 freeq(_RD(qp));
781 } else if (qp->q_next == _RD(qp)) { /* fifo */
782 freeq(_RD(qp));
783 } else { /* pipe */
784 freestp = 0;
786 * The q_info pointers are never accessed when
787 * SQLOCK is held.
789 ASSERT(qp->q_syncq == _RD(qp)->q_syncq);
790 mutex_enter(SQLOCK(qp->q_syncq));
791 qp->q_qinfo = &deadwend;
792 _RD(qp)->q_qinfo = &deadrend;
793 mutex_exit(SQLOCK(qp->q_syncq));
795 } else {
796 freeq(_RD(qp)); /* free stream head queue pair */
799 mutex_enter(&vp->v_lock);
800 if (stp->sd_iocblk) {
801 if (stp->sd_iocblk != (mblk_t *)-1) {
802 freemsg(stp->sd_iocblk);
804 stp->sd_iocblk = NULL;
806 stp->sd_vnode = NULL;
807 vp->v_stream = NULL;
808 mutex_exit(&vp->v_lock);
809 mutex_enter(&stp->sd_lock);
810 freemsg(stp->sd_cmdblk);
811 stp->sd_cmdblk = NULL;
812 stp->sd_flag &= ~STRCLOSE;
813 cv_broadcast(&stp->sd_monitor);
814 mutex_exit(&stp->sd_lock);
816 if (freestp)
817 shfree(stp);
818 return (0);
821 static int
822 strsink(queue_t *q, mblk_t *bp)
824 struct copyresp *resp;
826 switch (bp->b_datap->db_type) {
827 case M_FLUSH:
828 if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
829 *bp->b_rptr &= ~FLUSHR;
830 bp->b_flag |= MSGNOLOOP;
832 * Protect against the driver passing up
833 * messages after it has done a qprocsoff.
835 if (_OTHERQ(q)->q_next == NULL)
836 freemsg(bp);
837 else
838 qreply(q, bp);
839 } else {
840 freemsg(bp);
842 break;
844 case M_COPYIN:
845 case M_COPYOUT:
846 if (bp->b_cont) {
847 freemsg(bp->b_cont);
848 bp->b_cont = NULL;
850 bp->b_datap->db_type = M_IOCDATA;
851 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
852 resp = (struct copyresp *)bp->b_rptr;
853 resp->cp_rval = (caddr_t)1; /* failure */
855 * Protect against the driver passing up
856 * messages after it has done a qprocsoff.
858 if (_OTHERQ(q)->q_next == NULL)
859 freemsg(bp);
860 else
861 qreply(q, bp);
862 break;
864 case M_IOCTL:
865 if (bp->b_cont) {
866 freemsg(bp->b_cont);
867 bp->b_cont = NULL;
869 bp->b_datap->db_type = M_IOCNAK;
871 * Protect against the driver passing up
872 * messages after it has done a qprocsoff.
874 if (_OTHERQ(q)->q_next == NULL)
875 freemsg(bp);
876 else
877 qreply(q, bp);
878 break;
880 default:
881 freemsg(bp);
882 break;
885 return (0);
889 * Clean up after a process when it closes a stream. This is called
890 * from closef for all closes, whereas strclose is called only for the
891 * last close on a stream. The siglist is scanned for entries for the
892 * current process, and these are removed.
894 void
895 strclean(struct vnode *vp)
897 strsig_t *ssp, *pssp, *tssp;
898 stdata_t *stp;
899 int update = 0;
901 TRACE_1(TR_FAC_STREAMS_FR,
902 TR_STRCLEAN, "strclean:%p", vp);
903 stp = vp->v_stream;
904 pssp = NULL;
905 mutex_enter(&stp->sd_lock);
906 ssp = stp->sd_siglist;
907 while (ssp) {
908 if (ssp->ss_pidp == curproc->p_pidp) {
909 tssp = ssp->ss_next;
910 if (pssp)
911 pssp->ss_next = tssp;
912 else
913 stp->sd_siglist = tssp;
914 mutex_enter(&pidlock);
915 PID_RELE(ssp->ss_pidp);
916 mutex_exit(&pidlock);
917 kmem_free(ssp, sizeof (strsig_t));
918 update = 1;
919 ssp = tssp;
920 } else {
921 pssp = ssp;
922 ssp = ssp->ss_next;
925 if (update) {
926 stp->sd_sigflags = 0;
927 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
928 stp->sd_sigflags |= ssp->ss_events;
930 mutex_exit(&stp->sd_lock);
934 * Used on the last close to remove any remaining items on the siglist.
935 * These could be present on the siglist due to I_ESETSIG calls that
936 * use process groups or processed that do not have an open file descriptor
937 * for this stream (Such entries would not be removed by strclean).
939 static void
940 strcleanall(struct vnode *vp)
942 strsig_t *ssp, *nssp;
943 stdata_t *stp;
945 stp = vp->v_stream;
946 mutex_enter(&stp->sd_lock);
947 ssp = stp->sd_siglist;
948 stp->sd_siglist = NULL;
949 while (ssp) {
950 nssp = ssp->ss_next;
951 mutex_enter(&pidlock);
952 PID_RELE(ssp->ss_pidp);
953 mutex_exit(&pidlock);
954 kmem_free(ssp, sizeof (strsig_t));
955 ssp = nssp;
957 stp->sd_sigflags = 0;
958 mutex_exit(&stp->sd_lock);
962 * Retrieve the next message from the logical stream head read queue
963 * using either rwnext (if sync stream) or getq_noenab.
964 * It is the callers responsibility to call qbackenable after
965 * it is finished with the message. The caller should not call
966 * qbackenable until after any putback calls to avoid spurious backenabling.
968 mblk_t *
969 strget(struct stdata *stp, queue_t *q, struct uio *uiop, int first,
970 int *errorp)
972 mblk_t *bp;
973 int error;
974 ssize_t rbytes = 0;
976 /* Holding sd_lock prevents the read queue from changing */
977 ASSERT(MUTEX_HELD(&stp->sd_lock));
979 if (uiop != NULL && stp->sd_struiordq != NULL &&
980 q->q_first == NULL &&
981 (!first || (stp->sd_wakeq & RSLEEP))) {
983 * Stream supports rwnext() for the read side.
984 * If this is the first time we're called by e.g. strread
985 * only do the downcall if there is a deferred wakeup
986 * (registered in sd_wakeq).
988 struiod_t uiod;
989 struct iovec buf[IOV_MAX_STACK];
990 int iovlen = 0;
992 if (first)
993 stp->sd_wakeq &= ~RSLEEP;
995 if (uiop->uio_iovcnt > IOV_MAX_STACK) {
996 iovlen = uiop->uio_iovcnt * sizeof (iovec_t);
997 uiod.d_iov = kmem_alloc(iovlen, KM_SLEEP);
998 } else {
999 uiod.d_iov = buf;
1002 (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov, uiop->uio_iovcnt);
1003 uiod.d_mp = 0;
1005 * Mark that a thread is in rwnext on the read side
1006 * to prevent strrput from nacking ioctls immediately.
1007 * When the last concurrent rwnext returns
1008 * the ioctls are nack'ed.
1010 ASSERT(MUTEX_HELD(&stp->sd_lock));
1011 stp->sd_struiodnak++;
1013 * Note: rwnext will drop sd_lock.
1015 error = rwnext(q, &uiod);
1016 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
1017 mutex_enter(&stp->sd_lock);
1018 stp->sd_struiodnak--;
1019 while (stp->sd_struiodnak == 0 &&
1020 ((bp = stp->sd_struionak) != NULL)) {
1021 stp->sd_struionak = bp->b_next;
1022 bp->b_next = NULL;
1023 bp->b_datap->db_type = M_IOCNAK;
1025 * Protect against the driver passing up
1026 * messages after it has done a qprocsoff.
1028 if (_OTHERQ(q)->q_next == NULL)
1029 freemsg(bp);
1030 else {
1031 mutex_exit(&stp->sd_lock);
1032 qreply(q, bp);
1033 mutex_enter(&stp->sd_lock);
1036 ASSERT(MUTEX_HELD(&stp->sd_lock));
1037 if (error == 0 || error == EWOULDBLOCK) {
1038 if ((bp = uiod.d_mp) != NULL) {
1039 *errorp = 0;
1040 ASSERT(MUTEX_HELD(&stp->sd_lock));
1041 if (iovlen != 0)
1042 kmem_free(uiod.d_iov, iovlen);
1043 return (bp);
1045 error = 0;
1046 } else if (error == EINVAL) {
1048 * The stream plumbing must have
1049 * changed while we were away, so
1050 * just turn off rwnext()s.
1052 error = 0;
1053 } else if (error == EBUSY) {
1055 * The module might have data in transit using putnext
1056 * Fall back on waiting + getq.
1058 error = 0;
1059 } else {
1060 *errorp = error;
1061 ASSERT(MUTEX_HELD(&stp->sd_lock));
1062 if (iovlen != 0)
1063 kmem_free(uiod.d_iov, iovlen);
1064 return (NULL);
1067 if (iovlen != 0)
1068 kmem_free(uiod.d_iov, iovlen);
1071 * Try a getq in case a rwnext() generated mblk
1072 * has bubbled up via strrput().
1075 *errorp = 0;
1076 ASSERT(MUTEX_HELD(&stp->sd_lock));
1079 * If we have a valid uio, try and use this as a guide for how
1080 * many bytes to retrieve from the queue via getq_noenab().
1081 * Doing this can avoid unneccesary counting of overlong
1082 * messages in putback(). We currently only do this for sockets
1083 * and only if there is no sd_rputdatafunc hook.
1085 * The sd_rputdatafunc hook transforms the entire message
1086 * before any bytes in it can be given to a client. So, rbytes
1087 * must be 0 if there is a hook.
1089 if ((uiop != NULL) && (stp->sd_vnode->v_type == VSOCK) &&
1090 (stp->sd_rputdatafunc == NULL))
1091 rbytes = uiop->uio_resid;
1093 return (getq_noenab(q, rbytes));
1097 * Copy out the message pointed to by `bp' into the uio pointed to by `uiop'.
1098 * If the message does not fit in the uio the remainder of it is returned;
1099 * otherwise NULL is returned. Any embedded zero-length mblk_t's are
1100 * consumed, even if uio_resid reaches zero. On error, `*errorp' is set to
1101 * the error code, the message is consumed, and NULL is returned.
1103 static mblk_t *
1104 struiocopyout(mblk_t *bp, struct uio *uiop, int *errorp)
1106 int error;
1107 ptrdiff_t n;
1108 mblk_t *nbp;
1110 ASSERT(bp->b_wptr >= bp->b_rptr);
1112 do {
1113 if ((n = MIN(uiop->uio_resid, MBLKL(bp))) != 0) {
1114 ASSERT(n > 0);
1116 error = uiomove(bp->b_rptr, n, UIO_READ, uiop);
1117 if (error != 0) {
1118 freemsg(bp);
1119 *errorp = error;
1120 return (NULL);
1124 bp->b_rptr += n;
1125 while (bp != NULL && (bp->b_rptr >= bp->b_wptr)) {
1126 nbp = bp;
1127 bp = bp->b_cont;
1128 freeb(nbp);
1130 } while (bp != NULL && uiop->uio_resid > 0);
1132 *errorp = 0;
1133 return (bp);
1137 * Read a stream according to the mode flags in sd_flag:
1139 * (default mode) - Byte stream, msg boundaries are ignored
1140 * RD_MSGDIS (msg discard) - Read on msg boundaries and throw away
1141 * any data remaining in msg
1142 * RD_MSGNODIS (msg non-discard) - Read on msg boundaries and put back
1143 * any remaining data on head of read queue
1145 * Consume readable messages on the front of the queue until
1146 * ttolwp(curthread)->lwp_count
1147 * is satisfied, the readable messages are exhausted, or a message
1148 * boundary is reached in a message mode. If no data was read and
1149 * the stream was not opened with the NDELAY flag, block until data arrives.
1150 * Otherwise return the data read and update the count.
1152 * In default mode a 0 length message signifies end-of-file and terminates
1153 * a read in progress. The 0 length message is removed from the queue
1154 * only if it is the only message read (no data is read).
1156 * An attempt to read an M_PROTO or M_PCPROTO message results in an
1157 * EBADMSG error return, unless either RD_PROTDAT or RD_PROTDIS are set.
1158 * If RD_PROTDAT is set, M_PROTO and M_PCPROTO messages are read as data.
1159 * If RD_PROTDIS is set, the M_PROTO and M_PCPROTO parts of the message
1160 * are unlinked from and M_DATA blocks in the message, the protos are
1161 * thrown away, and the data is read.
1163 /* ARGSUSED */
1165 strread(struct vnode *vp, struct uio *uiop, cred_t *crp)
1167 struct stdata *stp;
1168 mblk_t *bp, *nbp;
1169 queue_t *q;
1170 int error = 0;
1171 uint_t old_sd_flag;
1172 int first;
1173 char rflg;
1174 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
1175 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
1176 short delim;
1177 unsigned char pri = 0;
1178 char waitflag;
1179 unsigned char type;
1181 TRACE_1(TR_FAC_STREAMS_FR,
1182 TR_STRREAD_ENTER, "strread:%p", vp);
1183 ASSERT(vp->v_stream);
1184 stp = vp->v_stream;
1186 mutex_enter(&stp->sd_lock);
1188 if ((error = i_straccess(stp, JCREAD)) != 0) {
1189 mutex_exit(&stp->sd_lock);
1190 return (error);
1193 if (stp->sd_flag & (STRDERR|STPLEX)) {
1194 error = strgeterr(stp, STRDERR|STPLEX, 0);
1195 if (error != 0) {
1196 mutex_exit(&stp->sd_lock);
1197 return (error);
1202 * Loop terminates when uiop->uio_resid == 0.
1204 rflg = 0;
1205 waitflag = READWAIT;
1206 q = _RD(stp->sd_wrq);
1207 for (;;) {
1208 ASSERT(MUTEX_HELD(&stp->sd_lock));
1209 old_sd_flag = stp->sd_flag;
1210 mark = 0;
1211 delim = 0;
1212 first = 1;
1213 while ((bp = strget(stp, q, uiop, first, &error)) == NULL) {
1214 int done = 0;
1216 ASSERT(MUTEX_HELD(&stp->sd_lock));
1218 if (error != 0)
1219 goto oops;
1221 if (stp->sd_flag & (STRHUP|STREOF)) {
1222 goto oops;
1224 if (rflg && !(stp->sd_flag & STRDELIM)) {
1225 goto oops;
1228 * If a read(fd,buf,0) has been done, there is no
1229 * need to sleep. We always have zero bytes to
1230 * return.
1232 if (uiop->uio_resid == 0) {
1233 goto oops;
1236 qbackenable(q, 0);
1238 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_WAIT,
1239 "strread calls strwaitq:%p, %p, %p",
1240 vp, uiop, crp);
1241 if ((error = strwaitq(stp, waitflag, uiop->uio_resid,
1242 uiop->uio_fmode, -1, &done)) != 0 || done) {
1243 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_DONE,
1244 "strread error or done:%p, %p, %p",
1245 vp, uiop, crp);
1246 if ((uiop->uio_fmode & FNDELAY) &&
1247 (stp->sd_flag & OLDNDELAY) &&
1248 (error == EAGAIN))
1249 error = 0;
1250 goto oops;
1252 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_AWAKE,
1253 "strread awakes:%p, %p, %p", vp, uiop, crp);
1254 if ((error = i_straccess(stp, JCREAD)) != 0) {
1255 goto oops;
1257 first = 0;
1260 ASSERT(MUTEX_HELD(&stp->sd_lock));
1261 ASSERT(bp);
1262 pri = bp->b_band;
1264 * Extract any mark information. If the message is not
1265 * completely consumed this information will be put in the mblk
1266 * that is putback.
1267 * If MSGMARKNEXT is set and the message is completely consumed
1268 * the STRATMARK flag will be set below. Likewise, if
1269 * MSGNOTMARKNEXT is set and the message is
1270 * completely consumed STRNOTATMARK will be set.
1272 * For some unknown reason strread only breaks the read at the
1273 * last mark.
1275 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
1276 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
1277 (MSGMARKNEXT|MSGNOTMARKNEXT));
1278 if (mark != 0 && bp == stp->sd_mark) {
1279 if (rflg) {
1280 putback(stp, q, bp, pri);
1281 goto oops;
1283 mark |= _LASTMARK;
1284 stp->sd_mark = NULL;
1286 if ((stp->sd_flag & STRDELIM) && (bp->b_flag & MSGDELIM))
1287 delim = 1;
1288 mutex_exit(&stp->sd_lock);
1290 if (STREAM_NEEDSERVICE(stp))
1291 stream_runservice(stp);
1293 type = bp->b_datap->db_type;
1295 switch (type) {
1297 case M_DATA:
1298 ismdata:
1299 if (msgnodata(bp)) {
1300 if (mark || delim) {
1301 freemsg(bp);
1302 } else if (rflg) {
1305 * If already read data put zero
1306 * length message back on queue else
1307 * free msg and return 0.
1309 bp->b_band = pri;
1310 mutex_enter(&stp->sd_lock);
1311 putback(stp, q, bp, pri);
1312 mutex_exit(&stp->sd_lock);
1313 } else {
1314 freemsg(bp);
1316 error = 0;
1317 goto oops1;
1320 rflg = 1;
1321 waitflag |= NOINTR;
1322 bp = struiocopyout(bp, uiop, &error);
1323 if (error != 0)
1324 goto oops1;
1326 mutex_enter(&stp->sd_lock);
1327 if (bp) {
1329 * Have remaining data in message.
1330 * Free msg if in discard mode.
1332 if (stp->sd_read_opt & RD_MSGDIS) {
1333 freemsg(bp);
1334 } else {
1335 bp->b_band = pri;
1336 if ((mark & _LASTMARK) &&
1337 (stp->sd_mark == NULL))
1338 stp->sd_mark = bp;
1339 bp->b_flag |= mark & ~_LASTMARK;
1340 if (delim)
1341 bp->b_flag |= MSGDELIM;
1342 if (msgnodata(bp))
1343 freemsg(bp);
1344 else
1345 putback(stp, q, bp, pri);
1347 } else {
1349 * Consumed the complete message.
1350 * Move the MSG*MARKNEXT information
1351 * to the stream head just in case
1352 * the read queue becomes empty.
1354 * If the stream head was at the mark
1355 * (STRATMARK) before we dropped sd_lock above
1356 * and some data was consumed then we have
1357 * moved past the mark thus STRATMARK is
1358 * cleared. However, if a message arrived in
1359 * strrput during the copyout above causing
1360 * STRATMARK to be set we can not clear that
1361 * flag.
1363 if (mark &
1364 (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
1365 if (mark & MSGMARKNEXT) {
1366 stp->sd_flag &= ~STRNOTATMARK;
1367 stp->sd_flag |= STRATMARK;
1368 } else if (mark & MSGNOTMARKNEXT) {
1369 stp->sd_flag &= ~STRATMARK;
1370 stp->sd_flag |= STRNOTATMARK;
1371 } else {
1372 stp->sd_flag &=
1373 ~(STRATMARK|STRNOTATMARK);
1375 } else if (rflg && (old_sd_flag & STRATMARK)) {
1376 stp->sd_flag &= ~STRATMARK;
1381 * Check for signal messages at the front of the read
1382 * queue and generate the signal(s) if appropriate.
1383 * The only signal that can be on queue is M_SIG at
1384 * this point.
1386 while ((((bp = q->q_first)) != NULL) &&
1387 (bp->b_datap->db_type == M_SIG)) {
1388 bp = getq_noenab(q, 0);
1390 * sd_lock is held so the content of the
1391 * read queue can not change.
1393 ASSERT(bp != NULL && DB_TYPE(bp) == M_SIG);
1394 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
1395 mutex_exit(&stp->sd_lock);
1396 freemsg(bp);
1397 if (STREAM_NEEDSERVICE(stp))
1398 stream_runservice(stp);
1399 mutex_enter(&stp->sd_lock);
1402 if ((uiop->uio_resid == 0) || (mark & _LASTMARK) ||
1403 delim ||
1404 (stp->sd_read_opt & (RD_MSGDIS|RD_MSGNODIS))) {
1405 goto oops;
1407 continue;
1409 case M_SIG:
1410 strsignal(stp, *bp->b_rptr, (int32_t)bp->b_band);
1411 freemsg(bp);
1412 mutex_enter(&stp->sd_lock);
1413 continue;
1415 case M_PROTO:
1416 case M_PCPROTO:
1418 * Only data messages are readable.
1419 * Any others generate an error, unless
1420 * RD_PROTDIS or RD_PROTDAT is set.
1422 if (stp->sd_read_opt & RD_PROTDAT) {
1423 for (nbp = bp; nbp; nbp = nbp->b_next) {
1424 if ((nbp->b_datap->db_type ==
1425 M_PROTO) ||
1426 (nbp->b_datap->db_type ==
1427 M_PCPROTO)) {
1428 nbp->b_datap->db_type = M_DATA;
1429 } else {
1430 break;
1434 * clear stream head hi pri flag based on
1435 * first message
1437 if (type == M_PCPROTO) {
1438 mutex_enter(&stp->sd_lock);
1439 stp->sd_flag &= ~STRPRI;
1440 mutex_exit(&stp->sd_lock);
1442 goto ismdata;
1443 } else if (stp->sd_read_opt & RD_PROTDIS) {
1445 * discard non-data messages
1447 while (bp &&
1448 ((bp->b_datap->db_type == M_PROTO) ||
1449 (bp->b_datap->db_type == M_PCPROTO))) {
1450 nbp = unlinkb(bp);
1451 freeb(bp);
1452 bp = nbp;
1455 * clear stream head hi pri flag based on
1456 * first message
1458 if (type == M_PCPROTO) {
1459 mutex_enter(&stp->sd_lock);
1460 stp->sd_flag &= ~STRPRI;
1461 mutex_exit(&stp->sd_lock);
1463 if (bp) {
1464 bp->b_band = pri;
1465 goto ismdata;
1466 } else {
1467 break;
1470 /* FALLTHRU */
1471 case M_PASSFP:
1472 if ((bp->b_datap->db_type == M_PASSFP) &&
1473 (stp->sd_read_opt & RD_PROTDIS)) {
1474 freemsg(bp);
1475 break;
1477 mutex_enter(&stp->sd_lock);
1478 putback(stp, q, bp, pri);
1479 mutex_exit(&stp->sd_lock);
1480 if (rflg == 0)
1481 error = EBADMSG;
1482 goto oops1;
1484 default:
1486 * Garbage on stream head read queue.
1488 cmn_err(CE_WARN, "bad %x found at stream head\n",
1489 bp->b_datap->db_type);
1490 freemsg(bp);
1491 goto oops1;
1493 mutex_enter(&stp->sd_lock);
1495 oops:
1496 mutex_exit(&stp->sd_lock);
1497 oops1:
1498 qbackenable(q, pri);
1499 return (error);
1500 #undef _LASTMARK
1504 * Default processing of M_PROTO/M_PCPROTO messages.
1505 * Determine which wakeups and signals are needed.
1506 * This can be replaced by a user-specified procedure for kernel users
1507 * of STREAMS.
1509 /* ARGSUSED */
1510 mblk_t *
1511 strrput_proto(vnode_t *vp, mblk_t *mp,
1512 strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1513 strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1515 *wakeups = RSLEEP;
1516 *allmsgsigs = 0;
1518 switch (mp->b_datap->db_type) {
1519 case M_PROTO:
1520 if (mp->b_band == 0) {
1521 *firstmsgsigs = S_INPUT | S_RDNORM;
1522 *pollwakeups = POLLIN | POLLRDNORM;
1523 } else {
1524 *firstmsgsigs = S_INPUT | S_RDBAND;
1525 *pollwakeups = POLLIN | POLLRDBAND;
1527 break;
1528 case M_PCPROTO:
1529 *firstmsgsigs = S_HIPRI;
1530 *pollwakeups = POLLPRI;
1531 break;
1533 return (mp);
1537 * Default processing of everything but M_DATA, M_PROTO, M_PCPROTO and
1538 * M_PASSFP messages.
1539 * Determine which wakeups and signals are needed.
1540 * This can be replaced by a user-specified procedure for kernel users
1541 * of STREAMS.
1543 /* ARGSUSED */
1544 mblk_t *
1545 strrput_misc(vnode_t *vp, mblk_t *mp,
1546 strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1547 strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1549 *wakeups = 0;
1550 *firstmsgsigs = 0;
1551 *allmsgsigs = 0;
1552 *pollwakeups = 0;
1553 return (mp);
1557 * Stream read put procedure. Called from downstream driver/module
1558 * with messages for the stream head. Data, protocol, and in-stream
1559 * signal messages are placed on the queue, others are handled directly.
1562 strrput(queue_t *q, mblk_t *bp)
1564 struct stdata *stp;
1565 ulong_t rput_opt;
1566 strwakeup_t wakeups;
1567 strsigset_t firstmsgsigs; /* Signals if first message on queue */
1568 strsigset_t allmsgsigs; /* Signals for all messages */
1569 strsigset_t signals; /* Signals events to generate */
1570 strpollset_t pollwakeups;
1571 mblk_t *nextbp;
1572 uchar_t band = 0;
1573 int hipri_sig;
1575 stp = (struct stdata *)q->q_ptr;
1577 * Use rput_opt for optimized access to the SR_ flags except
1578 * SR_POLLIN. That flag has to be checked under sd_lock since it
1579 * is modified by strpoll().
1581 rput_opt = stp->sd_rput_opt;
1583 ASSERT(qclaimed(q));
1584 TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_ENTER,
1585 "strrput called with message type:q %p bp %p", q, bp);
1588 * Perform initial processing and pass to the parameterized functions.
1590 ASSERT(bp->b_next == NULL);
1592 switch (bp->b_datap->db_type) {
1593 case M_DATA:
1595 * sockfs is the only consumer of STREOF and when it is set,
1596 * it implies that the receiver is not interested in receiving
1597 * any more data, hence the mblk is freed to prevent unnecessary
1598 * message queueing at the stream head.
1600 if (stp->sd_flag == STREOF) {
1601 freemsg(bp);
1602 return (0);
1604 if ((rput_opt & SR_IGN_ZEROLEN) &&
1605 bp->b_rptr == bp->b_wptr && msgnodata(bp)) {
1607 * Ignore zero-length M_DATA messages. These might be
1608 * generated by some transports.
1609 * The zero-length M_DATA messages, even if they
1610 * are ignored, should effect the atmark tracking and
1611 * should wake up a thread sleeping in strwaitmark.
1613 mutex_enter(&stp->sd_lock);
1614 if (bp->b_flag & MSGMARKNEXT) {
1616 * Record the position of the mark either
1617 * in q_last or in STRATMARK.
1619 if (q->q_last != NULL) {
1620 q->q_last->b_flag &= ~MSGNOTMARKNEXT;
1621 q->q_last->b_flag |= MSGMARKNEXT;
1622 } else {
1623 stp->sd_flag &= ~STRNOTATMARK;
1624 stp->sd_flag |= STRATMARK;
1626 } else if (bp->b_flag & MSGNOTMARKNEXT) {
1628 * Record that this is not the position of
1629 * the mark either in q_last or in
1630 * STRNOTATMARK.
1632 if (q->q_last != NULL) {
1633 q->q_last->b_flag &= ~MSGMARKNEXT;
1634 q->q_last->b_flag |= MSGNOTMARKNEXT;
1635 } else {
1636 stp->sd_flag &= ~STRATMARK;
1637 stp->sd_flag |= STRNOTATMARK;
1640 if (stp->sd_flag & RSLEEP) {
1641 stp->sd_flag &= ~RSLEEP;
1642 cv_broadcast(&q->q_wait);
1644 mutex_exit(&stp->sd_lock);
1645 freemsg(bp);
1646 return (0);
1648 wakeups = RSLEEP;
1649 if (bp->b_band == 0) {
1650 firstmsgsigs = S_INPUT | S_RDNORM;
1651 pollwakeups = POLLIN | POLLRDNORM;
1652 } else {
1653 firstmsgsigs = S_INPUT | S_RDBAND;
1654 pollwakeups = POLLIN | POLLRDBAND;
1656 if (rput_opt & SR_SIGALLDATA)
1657 allmsgsigs = firstmsgsigs;
1658 else
1659 allmsgsigs = 0;
1661 mutex_enter(&stp->sd_lock);
1662 if ((rput_opt & SR_CONSOL_DATA) &&
1663 (q->q_last != NULL) &&
1664 (bp->b_flag & (MSGMARK|MSGDELIM)) == 0) {
1666 * Consolidate an M_DATA message onto an M_DATA,
1667 * M_PROTO, or M_PCPROTO by merging it with q_last.
1668 * The consolidation does not take place if
1669 * the old message is marked with either of the
1670 * marks or the delim flag or if the new
1671 * message is marked with MSGMARK. The MSGMARK
1672 * check is needed to handle the odd semantics of
1673 * MSGMARK where essentially the whole message
1674 * is to be treated as marked.
1675 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from the
1676 * new message to the front of the b_cont chain.
1678 mblk_t *lbp = q->q_last;
1679 unsigned char db_type = lbp->b_datap->db_type;
1681 if ((db_type == M_DATA || db_type == M_PROTO ||
1682 db_type == M_PCPROTO) &&
1683 !(lbp->b_flag & (MSGDELIM|MSGMARK|MSGMARKNEXT))) {
1684 rmvq_noenab(q, lbp);
1686 * The first message in the b_cont list
1687 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
1688 * We need to handle the case where we
1689 * are appending:
1691 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
1692 * 2) a MSGMARKNEXT to a plain message.
1693 * 3) a MSGNOTMARKNEXT to a plain message
1694 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
1695 * message.
1697 * Thus we never append a MSGMARKNEXT or
1698 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
1700 if (bp->b_flag & MSGMARKNEXT) {
1701 lbp->b_flag |= MSGMARKNEXT;
1702 lbp->b_flag &= ~MSGNOTMARKNEXT;
1703 bp->b_flag &= ~MSGMARKNEXT;
1704 } else if (bp->b_flag & MSGNOTMARKNEXT) {
1705 lbp->b_flag |= MSGNOTMARKNEXT;
1706 bp->b_flag &= ~MSGNOTMARKNEXT;
1709 linkb(lbp, bp);
1710 bp = lbp;
1712 * The new message logically isn't the first
1713 * even though the q_first check below thinks
1714 * it is. Clear the firstmsgsigs to make it
1715 * not appear to be first.
1717 firstmsgsigs = 0;
1720 break;
1722 case M_PASSFP:
1723 wakeups = RSLEEP;
1724 allmsgsigs = 0;
1725 if (bp->b_band == 0) {
1726 firstmsgsigs = S_INPUT | S_RDNORM;
1727 pollwakeups = POLLIN | POLLRDNORM;
1728 } else {
1729 firstmsgsigs = S_INPUT | S_RDBAND;
1730 pollwakeups = POLLIN | POLLRDBAND;
1732 mutex_enter(&stp->sd_lock);
1733 break;
1735 case M_PROTO:
1736 case M_PCPROTO:
1737 ASSERT(stp->sd_rprotofunc != NULL);
1738 bp = (stp->sd_rprotofunc)(stp->sd_vnode, bp,
1739 &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1740 #define ALLSIG (S_INPUT|S_HIPRI|S_OUTPUT|S_MSG|S_ERROR|S_HANGUP|S_RDNORM|\
1741 S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)
1742 #define ALLPOLL (POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLWRNORM|POLLRDBAND|\
1743 POLLWRBAND)
1745 ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1746 ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1747 ASSERT((allmsgsigs & ~ALLSIG) == 0);
1748 ASSERT((pollwakeups & ~ALLPOLL) == 0);
1750 mutex_enter(&stp->sd_lock);
1751 break;
1753 default:
1754 ASSERT(stp->sd_rmiscfunc != NULL);
1755 bp = (stp->sd_rmiscfunc)(stp->sd_vnode, bp,
1756 &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1757 ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1758 ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1759 ASSERT((allmsgsigs & ~ALLSIG) == 0);
1760 ASSERT((pollwakeups & ~ALLPOLL) == 0);
1761 #undef ALLSIG
1762 #undef ALLPOLL
1763 mutex_enter(&stp->sd_lock);
1764 break;
1766 ASSERT(MUTEX_HELD(&stp->sd_lock));
1768 /* By default generate superset of signals */
1769 signals = (firstmsgsigs | allmsgsigs);
1772 * The proto and misc functions can return multiple messages
1773 * as a b_next chain. Such messages are processed separately.
1775 one_more:
1776 hipri_sig = 0;
1777 if (bp == NULL) {
1778 nextbp = NULL;
1779 } else {
1780 nextbp = bp->b_next;
1781 bp->b_next = NULL;
1783 switch (bp->b_datap->db_type) {
1784 case M_PCPROTO:
1786 * Only one priority protocol message is allowed at the
1787 * stream head at a time.
1789 if (stp->sd_flag & STRPRI) {
1790 TRACE_0(TR_FAC_STREAMS_FR, TR_STRRPUT_PROTERR,
1791 "M_PCPROTO already at head");
1792 freemsg(bp);
1793 mutex_exit(&stp->sd_lock);
1794 goto done;
1796 stp->sd_flag |= STRPRI;
1797 hipri_sig = 1;
1798 /* FALLTHRU */
1799 case M_DATA:
1800 case M_PROTO:
1801 case M_PASSFP:
1802 band = bp->b_band;
1804 * Marking doesn't work well when messages
1805 * are marked in more than one band. We only
1806 * remember the last message received, even if
1807 * it is placed on the queue ahead of other
1808 * marked messages.
1810 if (bp->b_flag & MSGMARK)
1811 stp->sd_mark = bp;
1812 (void) putq(q, bp);
1815 * If message is a PCPROTO message, always use
1816 * firstmsgsigs to determine if a signal should be
1817 * sent as strrput is the only place to send
1818 * signals for PCPROTO. Other messages are based on
1819 * the STRGETINPROG flag. The flag determines if
1820 * strrput or (k)strgetmsg will be responsible for
1821 * sending the signals, in the firstmsgsigs case.
1823 if ((hipri_sig == 1) ||
1824 (((stp->sd_flag & STRGETINPROG) == 0) &&
1825 (q->q_first == bp)))
1826 signals = (firstmsgsigs | allmsgsigs);
1827 else
1828 signals = allmsgsigs;
1829 break;
1831 default:
1832 mutex_exit(&stp->sd_lock);
1833 (void) strrput_nondata(q, bp);
1834 mutex_enter(&stp->sd_lock);
1835 break;
1838 ASSERT(MUTEX_HELD(&stp->sd_lock));
1840 * Wake sleeping read/getmsg and cancel deferred wakeup
1842 if (wakeups & RSLEEP)
1843 stp->sd_wakeq &= ~RSLEEP;
1845 wakeups &= stp->sd_flag;
1846 if (wakeups & RSLEEP) {
1847 stp->sd_flag &= ~RSLEEP;
1848 cv_broadcast(&q->q_wait);
1850 if (wakeups & WSLEEP) {
1851 stp->sd_flag &= ~WSLEEP;
1852 cv_broadcast(&_WR(q)->q_wait);
1855 if (pollwakeups != 0) {
1856 if (pollwakeups == (POLLIN | POLLRDNORM)) {
1858 * Can't use rput_opt since it was not
1859 * read when sd_lock was held and SR_POLLIN is changed
1860 * by strpoll() under sd_lock.
1862 if (!(stp->sd_rput_opt & SR_POLLIN))
1863 goto no_pollwake;
1864 stp->sd_rput_opt &= ~SR_POLLIN;
1866 mutex_exit(&stp->sd_lock);
1867 pollwakeup(&stp->sd_pollist, pollwakeups);
1868 mutex_enter(&stp->sd_lock);
1870 no_pollwake:
1873 * strsendsig can handle multiple signals with a
1874 * single call.
1876 if (stp->sd_sigflags & signals)
1877 strsendsig(stp->sd_siglist, signals, band, 0);
1878 mutex_exit(&stp->sd_lock);
1881 done:
1882 if (nextbp == NULL)
1883 return (0);
1886 * Any signals were handled the first time.
1887 * Wakeups and pollwakeups are redone to avoid any race
1888 * conditions - all the messages are not queued until the
1889 * last message has been processed by strrput.
1891 bp = nextbp;
1892 signals = firstmsgsigs = allmsgsigs = 0;
1893 mutex_enter(&stp->sd_lock);
1894 goto one_more;
1897 static void
1898 log_dupioc(queue_t *rq, mblk_t *bp)
1900 queue_t *wq, *qp;
1901 char *modnames, *mnp, *dname;
1902 size_t maxmodstr;
1903 boolean_t islast;
1906 * Allocate a buffer large enough to hold the names of nstrpush modules
1907 * and one driver, with spaces between and NUL terminator. If we can't
1908 * get memory, then we'll just log the driver name.
1910 maxmodstr = nstrpush * (FMNAMESZ + 1);
1911 mnp = modnames = kmem_alloc(maxmodstr, KM_NOSLEEP);
1913 /* march down write side to print log message down to the driver */
1914 wq = WR(rq);
1916 /* make sure q_next doesn't shift around while we're grabbing data */
1917 claimstr(wq);
1918 qp = wq->q_next;
1919 do {
1920 dname = Q2NAME(qp);
1921 islast = !SAMESTR(qp) || qp->q_next == NULL;
1922 if (modnames == NULL) {
1924 * If we don't have memory, then get the driver name in
1925 * the log where we can see it. Note that memory
1926 * pressure is a possible cause of these sorts of bugs.
1928 if (islast) {
1929 modnames = dname;
1930 maxmodstr = 0;
1932 } else {
1933 mnp += snprintf(mnp, FMNAMESZ + 1, "%s", dname);
1934 if (!islast)
1935 *mnp++ = ' ';
1937 qp = qp->q_next;
1938 } while (!islast);
1939 releasestr(wq);
1940 /* Cannot happen unless stream head is corrupt. */
1941 ASSERT(modnames != NULL);
1942 (void) strlog(rq->q_qinfo->qi_minfo->mi_idnum, 0, 1,
1943 SL_CONSOLE|SL_TRACE|SL_ERROR,
1944 "Warning: stream %p received duplicate %X M_IOC%s; module list: %s",
1945 rq->q_ptr, ((struct iocblk *)bp->b_rptr)->ioc_cmd,
1946 (DB_TYPE(bp) == M_IOCACK ? "ACK" : "NAK"), modnames);
1947 if (maxmodstr != 0)
1948 kmem_free(modnames, maxmodstr);
1952 strrput_nondata(queue_t *q, mblk_t *bp)
1954 struct stdata *stp;
1955 struct iocblk *iocbp;
1956 struct stroptions *sop;
1957 struct copyreq *reqp;
1958 struct copyresp *resp;
1959 unsigned char bpri;
1960 unsigned char flushed_already = 0;
1962 stp = (struct stdata *)q->q_ptr;
1964 ASSERT(!(stp->sd_flag & STPLEX));
1965 ASSERT(qclaimed(q));
1967 switch (bp->b_datap->db_type) {
1968 case M_ERROR:
1970 * An error has occurred downstream, the errno is in the first
1971 * bytes of the message.
1973 if ((bp->b_wptr - bp->b_rptr) == 2) { /* New flavor */
1974 unsigned char rw = 0;
1976 mutex_enter(&stp->sd_lock);
1977 if (*bp->b_rptr != NOERROR) { /* read error */
1978 if (*bp->b_rptr != 0) {
1979 if (stp->sd_flag & STRDERR)
1980 flushed_already |= FLUSHR;
1981 stp->sd_flag |= STRDERR;
1982 rw |= FLUSHR;
1983 } else {
1984 stp->sd_flag &= ~STRDERR;
1986 stp->sd_rerror = *bp->b_rptr;
1988 bp->b_rptr++;
1989 if (*bp->b_rptr != NOERROR) { /* write error */
1990 if (*bp->b_rptr != 0) {
1991 if (stp->sd_flag & STWRERR)
1992 flushed_already |= FLUSHW;
1993 stp->sd_flag |= STWRERR;
1994 rw |= FLUSHW;
1995 } else {
1996 stp->sd_flag &= ~STWRERR;
1998 stp->sd_werror = *bp->b_rptr;
2000 if (rw) {
2001 TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_WAKE,
2002 "strrput cv_broadcast:q %p, bp %p",
2003 q, bp);
2004 cv_broadcast(&q->q_wait); /* readers */
2005 cv_broadcast(&_WR(q)->q_wait); /* writers */
2006 cv_broadcast(&stp->sd_monitor); /* ioctllers */
2008 mutex_exit(&stp->sd_lock);
2009 pollwakeup(&stp->sd_pollist, POLLERR);
2010 mutex_enter(&stp->sd_lock);
2012 if (stp->sd_sigflags & S_ERROR)
2013 strsendsig(stp->sd_siglist, S_ERROR, 0,
2014 ((rw & FLUSHR) ? stp->sd_rerror :
2015 stp->sd_werror));
2016 mutex_exit(&stp->sd_lock);
2018 * Send the M_FLUSH only
2019 * for the first M_ERROR
2020 * message on the stream
2022 if (flushed_already == rw) {
2023 freemsg(bp);
2024 return (0);
2027 bp->b_datap->db_type = M_FLUSH;
2028 *bp->b_rptr = rw;
2029 bp->b_wptr = bp->b_rptr + 1;
2031 * Protect against the driver
2032 * passing up messages after
2033 * it has done a qprocsoff
2035 if (_OTHERQ(q)->q_next == NULL)
2036 freemsg(bp);
2037 else
2038 qreply(q, bp);
2039 return (0);
2040 } else
2041 mutex_exit(&stp->sd_lock);
2042 } else if (*bp->b_rptr != 0) { /* Old flavor */
2043 if (stp->sd_flag & (STRDERR|STWRERR))
2044 flushed_already = FLUSHRW;
2045 mutex_enter(&stp->sd_lock);
2046 stp->sd_flag |= (STRDERR|STWRERR);
2047 stp->sd_rerror = *bp->b_rptr;
2048 stp->sd_werror = *bp->b_rptr;
2049 TRACE_2(TR_FAC_STREAMS_FR,
2050 TR_STRRPUT_WAKE2,
2051 "strrput wakeup #2:q %p, bp %p", q, bp);
2052 cv_broadcast(&q->q_wait); /* the readers */
2053 cv_broadcast(&_WR(q)->q_wait); /* the writers */
2054 cv_broadcast(&stp->sd_monitor); /* ioctllers */
2056 mutex_exit(&stp->sd_lock);
2057 pollwakeup(&stp->sd_pollist, POLLERR);
2058 mutex_enter(&stp->sd_lock);
2060 if (stp->sd_sigflags & S_ERROR)
2061 strsendsig(stp->sd_siglist, S_ERROR, 0,
2062 (stp->sd_werror ? stp->sd_werror :
2063 stp->sd_rerror));
2064 mutex_exit(&stp->sd_lock);
2067 * Send the M_FLUSH only
2068 * for the first M_ERROR
2069 * message on the stream
2071 if (flushed_already != FLUSHRW) {
2072 bp->b_datap->db_type = M_FLUSH;
2073 *bp->b_rptr = FLUSHRW;
2075 * Protect against the driver passing up
2076 * messages after it has done a
2077 * qprocsoff.
2079 if (_OTHERQ(q)->q_next == NULL)
2080 freemsg(bp);
2081 else
2082 qreply(q, bp);
2083 return (0);
2086 freemsg(bp);
2087 return (0);
2089 case M_HANGUP:
2091 freemsg(bp);
2092 mutex_enter(&stp->sd_lock);
2093 stp->sd_werror = ENXIO;
2094 stp->sd_flag |= STRHUP;
2095 stp->sd_flag &= ~(WSLEEP|RSLEEP);
2098 * send signal if controlling tty
2101 if (stp->sd_sidp) {
2102 prsignal(stp->sd_sidp, SIGHUP);
2103 if (stp->sd_sidp != stp->sd_pgidp)
2104 pgsignal(stp->sd_pgidp, SIGTSTP);
2108 * wake up read, write, and exception pollers and
2109 * reset wakeup mechanism.
2111 cv_broadcast(&q->q_wait); /* the readers */
2112 cv_broadcast(&_WR(q)->q_wait); /* the writers */
2113 cv_broadcast(&stp->sd_monitor); /* the ioctllers */
2114 strhup(stp);
2115 mutex_exit(&stp->sd_lock);
2116 return (0);
2118 case M_UNHANGUP:
2119 freemsg(bp);
2120 mutex_enter(&stp->sd_lock);
2121 stp->sd_werror = 0;
2122 stp->sd_flag &= ~STRHUP;
2123 mutex_exit(&stp->sd_lock);
2124 return (0);
2126 case M_SIG:
2128 * Someone downstream wants to post a signal. The
2129 * signal to post is contained in the first byte of the
2130 * message. If the message would go on the front of
2131 * the queue, send a signal to the process group
2132 * (if not SIGPOLL) or to the siglist processes
2133 * (SIGPOLL). If something is already on the queue,
2134 * OR if we are delivering a delayed suspend (*sigh*
2135 * another "tty" hack) and there's no one sleeping already,
2136 * just enqueue the message.
2138 mutex_enter(&stp->sd_lock);
2139 if (q->q_first || (*bp->b_rptr == SIGTSTP &&
2140 !(stp->sd_flag & RSLEEP))) {
2141 (void) putq(q, bp);
2142 mutex_exit(&stp->sd_lock);
2143 return (0);
2145 mutex_exit(&stp->sd_lock);
2146 /* FALLTHRU */
2148 case M_PCSIG:
2150 * Don't enqueue, just post the signal.
2152 strsignal(stp, *bp->b_rptr, 0L);
2153 freemsg(bp);
2154 return (0);
2156 case M_CMD:
2157 if (MBLKL(bp) != sizeof (cmdblk_t)) {
2158 freemsg(bp);
2159 return (0);
2162 mutex_enter(&stp->sd_lock);
2163 if (stp->sd_flag & STRCMDWAIT) {
2164 ASSERT(stp->sd_cmdblk == NULL);
2165 stp->sd_cmdblk = bp;
2166 cv_broadcast(&stp->sd_monitor);
2167 mutex_exit(&stp->sd_lock);
2168 } else {
2169 mutex_exit(&stp->sd_lock);
2170 freemsg(bp);
2172 return (0);
2174 case M_FLUSH:
2176 * Flush queues. The indication of which queues to flush
2177 * is in the first byte of the message. If the read queue
2178 * is specified, then flush it. If FLUSHBAND is set, just
2179 * flush the band specified by the second byte of the message.
2181 * If a module has issued a M_SETOPT to not flush hi
2182 * priority messages off of the stream head, then pass this
2183 * flag into the flushq code to preserve such messages.
2186 if (*bp->b_rptr & FLUSHR) {
2187 mutex_enter(&stp->sd_lock);
2188 if (*bp->b_rptr & FLUSHBAND) {
2189 ASSERT((bp->b_wptr - bp->b_rptr) >= 2);
2190 flushband(q, *(bp->b_rptr + 1), FLUSHALL);
2191 } else
2192 flushq_common(q, FLUSHALL,
2193 stp->sd_read_opt & RFLUSHPCPROT);
2194 if ((q->q_first == NULL) ||
2195 (q->q_first->b_datap->db_type < QPCTL))
2196 stp->sd_flag &= ~STRPRI;
2197 else {
2198 ASSERT(stp->sd_flag & STRPRI);
2200 mutex_exit(&stp->sd_lock);
2202 if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
2203 *bp->b_rptr &= ~FLUSHR;
2204 bp->b_flag |= MSGNOLOOP;
2206 * Protect against the driver passing up
2207 * messages after it has done a qprocsoff.
2209 if (_OTHERQ(q)->q_next == NULL)
2210 freemsg(bp);
2211 else
2212 qreply(q, bp);
2213 return (0);
2215 freemsg(bp);
2216 return (0);
2218 case M_IOCACK:
2219 case M_IOCNAK:
2220 iocbp = (struct iocblk *)bp->b_rptr;
2222 * If not waiting for ACK or NAK then just free msg.
2223 * If incorrect id sequence number then just free msg.
2224 * If already have ACK or NAK for user then this is a
2225 * duplicate, display a warning and free the msg.
2227 mutex_enter(&stp->sd_lock);
2228 if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2229 (stp->sd_iocid != iocbp->ioc_id)) {
2231 * If the ACK/NAK is a dup, display a message
2232 * Dup is when sd_iocid == ioc_id, and
2233 * sd_iocblk == <valid ptr> or -1 (the former
2234 * is when an ioctl has been put on the stream
2235 * head, but has not yet been consumed, the
2236 * later is when it has been consumed).
2238 if ((stp->sd_iocid == iocbp->ioc_id) &&
2239 (stp->sd_iocblk != NULL)) {
2240 log_dupioc(q, bp);
2242 freemsg(bp);
2243 mutex_exit(&stp->sd_lock);
2244 return (0);
2248 * Assign ACK or NAK to user and wake up.
2250 stp->sd_iocblk = bp;
2251 cv_broadcast(&stp->sd_monitor);
2252 mutex_exit(&stp->sd_lock);
2253 return (0);
2255 case M_COPYIN:
2256 case M_COPYOUT:
2257 reqp = (struct copyreq *)bp->b_rptr;
2260 * If not waiting for ACK or NAK then just fail request.
2261 * If already have ACK, NAK, or copy request, then just
2262 * fail request.
2263 * If incorrect id sequence number then just fail request.
2265 mutex_enter(&stp->sd_lock);
2266 if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2267 (stp->sd_iocid != reqp->cq_id)) {
2268 if (bp->b_cont) {
2269 freemsg(bp->b_cont);
2270 bp->b_cont = NULL;
2272 bp->b_datap->db_type = M_IOCDATA;
2273 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
2274 resp = (struct copyresp *)bp->b_rptr;
2275 resp->cp_rval = (caddr_t)1; /* failure */
2276 mutex_exit(&stp->sd_lock);
2277 putnext(stp->sd_wrq, bp);
2278 return (0);
2282 * Assign copy request to user and wake up.
2284 stp->sd_iocblk = bp;
2285 cv_broadcast(&stp->sd_monitor);
2286 mutex_exit(&stp->sd_lock);
2287 return (0);
2289 case M_SETOPTS:
2291 * Set stream head options (read option, write offset,
2292 * min/max packet size, and/or high/low water marks for
2293 * the read side only).
2296 bpri = 0;
2297 sop = (struct stroptions *)bp->b_rptr;
2298 mutex_enter(&stp->sd_lock);
2299 if (sop->so_flags & SO_READOPT) {
2300 switch (sop->so_readopt & RMODEMASK) {
2301 case RNORM:
2302 stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
2303 break;
2305 case RMSGD:
2306 stp->sd_read_opt =
2307 ((stp->sd_read_opt & ~RD_MSGNODIS) |
2308 RD_MSGDIS);
2309 break;
2311 case RMSGN:
2312 stp->sd_read_opt =
2313 ((stp->sd_read_opt & ~RD_MSGDIS) |
2314 RD_MSGNODIS);
2315 break;
2317 switch (sop->so_readopt & RPROTMASK) {
2318 case RPROTNORM:
2319 stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
2320 break;
2322 case RPROTDAT:
2323 stp->sd_read_opt =
2324 ((stp->sd_read_opt & ~RD_PROTDIS) |
2325 RD_PROTDAT);
2326 break;
2328 case RPROTDIS:
2329 stp->sd_read_opt =
2330 ((stp->sd_read_opt & ~RD_PROTDAT) |
2331 RD_PROTDIS);
2332 break;
2334 switch (sop->so_readopt & RFLUSHMASK) {
2335 case RFLUSHPCPROT:
2337 * This sets the stream head to NOT flush
2338 * M_PCPROTO messages.
2340 stp->sd_read_opt |= RFLUSHPCPROT;
2341 break;
2344 if (sop->so_flags & SO_ERROPT) {
2345 switch (sop->so_erropt & RERRMASK) {
2346 case RERRNORM:
2347 stp->sd_flag &= ~STRDERRNONPERSIST;
2348 break;
2349 case RERRNONPERSIST:
2350 stp->sd_flag |= STRDERRNONPERSIST;
2351 break;
2353 switch (sop->so_erropt & WERRMASK) {
2354 case WERRNORM:
2355 stp->sd_flag &= ~STWRERRNONPERSIST;
2356 break;
2357 case WERRNONPERSIST:
2358 stp->sd_flag |= STWRERRNONPERSIST;
2359 break;
2362 if (sop->so_flags & SO_COPYOPT) {
2363 if (sop->so_copyopt & ZCVMSAFE) {
2364 stp->sd_copyflag |= STZCVMSAFE;
2365 stp->sd_copyflag &= ~STZCVMUNSAFE;
2366 } else if (sop->so_copyopt & ZCVMUNSAFE) {
2367 stp->sd_copyflag |= STZCVMUNSAFE;
2368 stp->sd_copyflag &= ~STZCVMSAFE;
2371 if (sop->so_copyopt & COPYCACHED) {
2372 stp->sd_copyflag |= STRCOPYCACHED;
2375 if (sop->so_flags & SO_WROFF)
2376 stp->sd_wroff = sop->so_wroff;
2377 if (sop->so_flags & SO_TAIL)
2378 stp->sd_tail = sop->so_tail;
2379 if (sop->so_flags & SO_MINPSZ)
2380 q->q_minpsz = sop->so_minpsz;
2381 if (sop->so_flags & SO_MAXPSZ)
2382 q->q_maxpsz = sop->so_maxpsz;
2383 if (sop->so_flags & SO_MAXBLK)
2384 stp->sd_maxblk = sop->so_maxblk;
2385 if (sop->so_flags & SO_HIWAT) {
2386 if (sop->so_flags & SO_BAND) {
2387 if (strqset(q, QHIWAT,
2388 sop->so_band, sop->so_hiwat)) {
2389 cmn_err(CE_WARN, "strrput: could not "
2390 "allocate qband\n");
2391 } else {
2392 bpri = sop->so_band;
2394 } else {
2395 q->q_hiwat = sop->so_hiwat;
2398 if (sop->so_flags & SO_LOWAT) {
2399 if (sop->so_flags & SO_BAND) {
2400 if (strqset(q, QLOWAT,
2401 sop->so_band, sop->so_lowat)) {
2402 cmn_err(CE_WARN, "strrput: could not "
2403 "allocate qband\n");
2404 } else {
2405 bpri = sop->so_band;
2407 } else {
2408 q->q_lowat = sop->so_lowat;
2411 if (sop->so_flags & SO_MREADON)
2412 stp->sd_flag |= SNDMREAD;
2413 if (sop->so_flags & SO_MREADOFF)
2414 stp->sd_flag &= ~SNDMREAD;
2415 if (sop->so_flags & SO_NDELON)
2416 stp->sd_flag |= OLDNDELAY;
2417 if (sop->so_flags & SO_NDELOFF)
2418 stp->sd_flag &= ~OLDNDELAY;
2419 if (sop->so_flags & SO_ISTTY)
2420 stp->sd_flag |= STRISTTY;
2421 if (sop->so_flags & SO_ISNTTY)
2422 stp->sd_flag &= ~STRISTTY;
2423 if (sop->so_flags & SO_TOSTOP)
2424 stp->sd_flag |= STRTOSTOP;
2425 if (sop->so_flags & SO_TONSTOP)
2426 stp->sd_flag &= ~STRTOSTOP;
2427 if (sop->so_flags & SO_DELIM)
2428 stp->sd_flag |= STRDELIM;
2429 if (sop->so_flags & SO_NODELIM)
2430 stp->sd_flag &= ~STRDELIM;
2432 mutex_exit(&stp->sd_lock);
2433 freemsg(bp);
2435 /* Check backenable in case the water marks changed */
2436 qbackenable(q, bpri);
2437 return (0);
2440 * The following set of cases deal with situations where two stream
2441 * heads are connected to each other (twisted streams). These messages
2442 * have no meaning at the stream head.
2444 case M_BREAK:
2445 case M_CTL:
2446 case M_DELAY:
2447 case M_START:
2448 case M_STOP:
2449 case M_IOCDATA:
2450 case M_STARTI:
2451 case M_STOPI:
2452 freemsg(bp);
2453 return (0);
2455 case M_IOCTL:
2457 * Always NAK this condition
2458 * (makes no sense)
2459 * If there is one or more threads in the read side
2460 * rwnext we have to defer the nacking until that thread
2461 * returns (in strget).
2463 mutex_enter(&stp->sd_lock);
2464 if (stp->sd_struiodnak != 0) {
2466 * Defer NAK to the streamhead. Queue at the end
2467 * the list.
2469 mblk_t *mp = stp->sd_struionak;
2471 while (mp && mp->b_next)
2472 mp = mp->b_next;
2473 if (mp)
2474 mp->b_next = bp;
2475 else
2476 stp->sd_struionak = bp;
2477 bp->b_next = NULL;
2478 mutex_exit(&stp->sd_lock);
2479 return (0);
2481 mutex_exit(&stp->sd_lock);
2483 bp->b_datap->db_type = M_IOCNAK;
2485 * Protect against the driver passing up
2486 * messages after it has done a qprocsoff.
2488 if (_OTHERQ(q)->q_next == NULL)
2489 freemsg(bp);
2490 else
2491 qreply(q, bp);
2492 return (0);
2494 default:
2495 #ifdef DEBUG
2496 cmn_err(CE_WARN,
2497 "bad message type %x received at stream head\n",
2498 bp->b_datap->db_type);
2499 #endif
2500 freemsg(bp);
2501 return (0);
2504 /* NOTREACHED */
2508 * Check if the stream pointed to by `stp' can be written to, and return an
2509 * error code if not. If `eiohup' is set, then return EIO if STRHUP is set.
2510 * If `sigpipeok' is set and the SW_SIGPIPE option is enabled on the stream,
2511 * then always return EPIPE and send a SIGPIPE to the invoking thread.
2513 static int
2514 strwriteable(struct stdata *stp, boolean_t eiohup, boolean_t sigpipeok)
2516 int error;
2518 ASSERT(MUTEX_HELD(&stp->sd_lock));
2521 * For modem support, POSIX states that on writes, EIO should
2522 * be returned if the stream has been hung up.
2524 if (eiohup && (stp->sd_flag & (STPLEX|STRHUP)) == STRHUP)
2525 error = EIO;
2526 else
2527 error = strgeterr(stp, STRHUP|STPLEX|STWRERR, 0);
2529 if (error != 0) {
2530 if (!(stp->sd_flag & STPLEX) &&
2531 (stp->sd_wput_opt & SW_SIGPIPE) && sigpipeok) {
2532 tsignal(curthread, SIGPIPE);
2533 error = EPIPE;
2537 return (error);
2541 * Copyin and send data down a stream.
2542 * The caller will allocate and copyin any control part that precedes the
2543 * message and pass that in as mctl.
2545 * Caller should *not* hold sd_lock.
2546 * When EWOULDBLOCK is returned the caller has to redo the canputnext
2547 * under sd_lock in order to avoid missing a backenabling wakeup.
2549 * Use iosize = -1 to not send any M_DATA. iosize = 0 sends zero-length M_DATA.
2551 * Set MSG_IGNFLOW in flags to ignore flow control for hipri messages.
2552 * For sync streams we can only ignore flow control by reverting to using
2553 * putnext.
2555 * If sd_maxblk is less than *iosize this routine might return without
2556 * transferring all of *iosize. In all cases, on return *iosize will contain
2557 * the amount of data that was transferred.
2559 static int
2560 strput(struct stdata *stp, mblk_t *mctl, struct uio *uiop, ssize_t *iosize,
2561 int b_flag, int pri, int flags)
2563 struiod_t uiod;
2564 struct iovec buf[IOV_MAX_STACK];
2565 int iovlen = 0;
2566 mblk_t *mp;
2567 queue_t *wqp = stp->sd_wrq;
2568 int error = 0;
2569 ssize_t count = *iosize;
2571 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
2573 if (uiop != NULL && count >= 0)
2574 flags |= stp->sd_struiowrq ? STRUIO_POSTPONE : 0;
2576 if (!(flags & STRUIO_POSTPONE)) {
2578 * Use regular canputnext, strmakedata, putnext sequence.
2580 if (pri == 0) {
2581 if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2582 freemsg(mctl);
2583 return (EWOULDBLOCK);
2585 } else {
2586 if (!(flags & MSG_IGNFLOW) && !bcanputnext(wqp, pri)) {
2587 freemsg(mctl);
2588 return (EWOULDBLOCK);
2592 if ((error = strmakedata(iosize, uiop, stp, flags,
2593 &mp)) != 0) {
2594 freemsg(mctl);
2596 * need to change return code to ENOMEM
2597 * so that this is not confused with
2598 * flow control, EAGAIN.
2601 if (error == EAGAIN)
2602 return (ENOMEM);
2603 else
2604 return (error);
2606 if (mctl != NULL) {
2607 if (mctl->b_cont == NULL)
2608 mctl->b_cont = mp;
2609 else if (mp != NULL)
2610 linkb(mctl, mp);
2611 mp = mctl;
2612 } else if (mp == NULL)
2613 return (0);
2615 mp->b_flag |= b_flag;
2616 mp->b_band = (uchar_t)pri;
2618 if (flags & MSG_IGNFLOW) {
2620 * XXX Hack: Don't get stuck running service
2621 * procedures. This is needed for sockfs when
2622 * sending the unbind message out of the rput
2623 * procedure - we don't want a put procedure
2624 * to run service procedures.
2626 putnext(wqp, mp);
2627 } else {
2628 stream_willservice(stp);
2629 putnext(wqp, mp);
2630 stream_runservice(stp);
2632 return (0);
2635 * Stream supports rwnext() for the write side.
2637 if ((error = strmakedata(iosize, uiop, stp, flags, &mp)) != 0) {
2638 freemsg(mctl);
2640 * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
2642 return (error == EAGAIN ? ENOMEM : error);
2644 if (mctl != NULL) {
2645 if (mctl->b_cont == NULL)
2646 mctl->b_cont = mp;
2647 else if (mp != NULL)
2648 linkb(mctl, mp);
2649 mp = mctl;
2650 } else if (mp == NULL) {
2651 return (0);
2654 mp->b_flag |= b_flag;
2655 mp->b_band = (uchar_t)pri;
2657 if (uiop->uio_iovcnt > IOV_MAX_STACK) {
2658 iovlen = uiop->uio_iovcnt * sizeof (iovec_t);
2659 uiod.d_iov = kmem_alloc(iovlen, KM_SLEEP);
2660 } else {
2661 uiod.d_iov = buf;
2664 (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov, uiop->uio_iovcnt);
2665 uiod.d_uio.uio_offset = 0;
2666 uiod.d_mp = mp;
2667 error = rwnext(wqp, &uiod);
2668 if (! uiod.d_mp) {
2669 uioskip(uiop, *iosize);
2670 if (iovlen != 0)
2671 kmem_free(uiod.d_iov, iovlen);
2672 return (error);
2674 ASSERT(mp == uiod.d_mp);
2675 if (error == EINVAL) {
2677 * The stream plumbing must have changed while
2678 * we were away, so just turn off rwnext()s.
2680 error = 0;
2681 } else if (error == EBUSY || error == EWOULDBLOCK) {
2683 * Couldn't enter a perimeter or took a page fault,
2684 * so fall-back to putnext().
2686 error = 0;
2687 } else {
2688 freemsg(mp);
2689 if (iovlen != 0)
2690 kmem_free(uiod.d_iov, iovlen);
2691 return (error);
2693 /* Have to check canput before consuming data from the uio */
2694 if (pri == 0) {
2695 if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2696 freemsg(mp);
2697 if (iovlen != 0)
2698 kmem_free(uiod.d_iov, iovlen);
2699 return (EWOULDBLOCK);
2701 } else {
2702 if (!bcanputnext(wqp, pri) && !(flags & MSG_IGNFLOW)) {
2703 freemsg(mp);
2704 if (iovlen != 0)
2705 kmem_free(uiod.d_iov, iovlen);
2706 return (EWOULDBLOCK);
2709 ASSERT(mp == uiod.d_mp);
2710 /* Copyin data from the uio */
2711 if ((error = struioget(wqp, mp, &uiod, 0)) != 0) {
2712 freemsg(mp);
2713 if (iovlen != 0)
2714 kmem_free(uiod.d_iov, iovlen);
2715 return (error);
2717 uioskip(uiop, *iosize);
2718 if (flags & MSG_IGNFLOW) {
2720 * XXX Hack: Don't get stuck running service procedures.
2721 * This is needed for sockfs when sending the unbind message
2722 * out of the rput procedure - we don't want a put procedure
2723 * to run service procedures.
2725 putnext(wqp, mp);
2726 } else {
2727 stream_willservice(stp);
2728 putnext(wqp, mp);
2729 stream_runservice(stp);
2731 if (iovlen != 0)
2732 kmem_free(uiod.d_iov, iovlen);
2733 return (0);
2737 * Write attempts to break the write request into messages conforming
2738 * with the minimum and maximum packet sizes set downstream.
2740 * Write will not block if downstream queue is full and
2741 * O_NDELAY is set, otherwise it will block waiting for the queue to get room.
2743 * A write of zero bytes gets packaged into a zero length message and sent
2744 * downstream like any other message.
2746 * If buffers of the requested sizes are not available, the write will
2747 * sleep until the buffers become available.
2749 * Write (if specified) will supply a write offset in a message if it
2750 * makes sense. This can be specified by downstream modules as part of
2751 * a M_SETOPTS message. Write will not supply the write offset if it
2752 * cannot supply any data in a buffer. In other words, write will never
2753 * send down an empty packet due to a write offset.
2755 /* ARGSUSED2 */
2757 strwrite(struct vnode *vp, struct uio *uiop, cred_t *crp)
2759 return (strwrite_common(vp, uiop, crp, 0));
2762 /* ARGSUSED2 */
2764 strwrite_common(struct vnode *vp, struct uio *uiop, cred_t *crp, int wflag)
2766 struct stdata *stp;
2767 struct queue *wqp;
2768 ssize_t rmin, rmax;
2769 ssize_t iosize;
2770 int waitflag;
2771 int tempmode;
2772 int error = 0;
2773 int b_flag;
2775 ASSERT(vp->v_stream);
2776 stp = vp->v_stream;
2778 mutex_enter(&stp->sd_lock);
2780 if ((error = i_straccess(stp, JCWRITE)) != 0) {
2781 mutex_exit(&stp->sd_lock);
2782 return (error);
2785 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
2786 error = strwriteable(stp, B_TRUE, B_TRUE);
2787 if (error != 0) {
2788 mutex_exit(&stp->sd_lock);
2789 return (error);
2793 mutex_exit(&stp->sd_lock);
2795 wqp = stp->sd_wrq;
2797 /* get these values from them cached in the stream head */
2798 rmin = stp->sd_qn_minpsz;
2799 rmax = stp->sd_qn_maxpsz;
2802 * Check the min/max packet size constraints. If min packet size
2803 * is non-zero, the write cannot be split into multiple messages
2804 * and still guarantee the size constraints.
2806 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_IN, "strwrite in:q %p", wqp);
2808 ASSERT((rmax >= 0) || (rmax == INFPSZ));
2809 if (rmax == 0) {
2810 return (0);
2812 if (rmin > 0) {
2813 if (uiop->uio_resid < rmin) {
2814 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2815 "strwrite out:q %p out %d error %d",
2816 wqp, 0, ERANGE);
2817 return (ERANGE);
2819 if ((rmax != INFPSZ) && (uiop->uio_resid > rmax)) {
2820 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2821 "strwrite out:q %p out %d error %d",
2822 wqp, 1, ERANGE);
2823 return (ERANGE);
2828 * Do until count satisfied or error.
2830 waitflag = WRITEWAIT | wflag;
2831 if (stp->sd_flag & OLDNDELAY)
2832 tempmode = uiop->uio_fmode & ~FNDELAY;
2833 else
2834 tempmode = uiop->uio_fmode;
2836 if (rmax == INFPSZ)
2837 rmax = uiop->uio_resid;
2840 * Note that tempmode does not get used in strput/strmakedata
2841 * but only in strwaitq. The other routines use uio_fmode
2842 * unmodified.
2845 while (1) { /* breaks when uio_resid reaches zero */
2847 * Determine the size of the next message to be
2848 * packaged. May have to break write into several
2849 * messages based on max packet size.
2851 iosize = MIN(uiop->uio_resid, rmax);
2854 * Put block downstream when flow control allows it.
2856 if ((stp->sd_flag & STRDELIM) && (uiop->uio_resid == iosize))
2857 b_flag = MSGDELIM;
2858 else
2859 b_flag = 0;
2861 for (;;) {
2862 int done = 0;
2864 error = strput(stp, NULL, uiop, &iosize, b_flag, 0, 0);
2865 if (error == 0)
2866 break;
2867 if (error != EWOULDBLOCK)
2868 goto out;
2870 mutex_enter(&stp->sd_lock);
2872 * Check for a missed wakeup.
2873 * Needed since strput did not hold sd_lock across
2874 * the canputnext.
2876 if (canputnext(wqp)) {
2877 /* Try again */
2878 mutex_exit(&stp->sd_lock);
2879 continue;
2881 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAIT,
2882 "strwrite wait:q %p wait", wqp);
2883 if ((error = strwaitq(stp, waitflag, (ssize_t)0,
2884 tempmode, -1, &done)) != 0 || done) {
2885 mutex_exit(&stp->sd_lock);
2886 if ((vp->v_type == VFIFO) &&
2887 (uiop->uio_fmode & FNDELAY) &&
2888 (error == EAGAIN))
2889 error = 0;
2890 goto out;
2892 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAKE,
2893 "strwrite wake:q %p awakes", wqp);
2894 if ((error = i_straccess(stp, JCWRITE)) != 0) {
2895 mutex_exit(&stp->sd_lock);
2896 goto out;
2898 mutex_exit(&stp->sd_lock);
2900 waitflag |= NOINTR;
2901 TRACE_2(TR_FAC_STREAMS_FR, TR_STRWRITE_RESID,
2902 "strwrite resid:q %p uiop %p", wqp, uiop);
2903 if (uiop->uio_resid) {
2904 /* Recheck for errors - needed for sockets */
2905 if ((stp->sd_wput_opt & SW_RECHECK_ERR) &&
2906 (stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
2907 mutex_enter(&stp->sd_lock);
2908 error = strwriteable(stp, B_FALSE, B_TRUE);
2909 mutex_exit(&stp->sd_lock);
2910 if (error != 0)
2911 return (error);
2913 continue;
2915 break;
2917 out:
2919 * For historical reasons, applications expect EAGAIN when a data
2920 * mblk_t cannot be allocated, so change ENOMEM back to EAGAIN.
2922 if (error == ENOMEM)
2923 error = EAGAIN;
2924 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2925 "strwrite out:q %p out %d error %d", wqp, 2, error);
2926 return (error);
2930 * Stream head write service routine.
2931 * Its job is to wake up any sleeping writers when a queue
2932 * downstream needs data (part of the flow control in putq and getq).
2933 * It also must wake anyone sleeping on a poll().
2934 * For stream head right below mux module, it must also invoke put procedure
2935 * of next downstream module.
2938 strwsrv(queue_t *q)
2940 struct stdata *stp;
2941 queue_t *tq;
2942 qband_t *qbp;
2943 int i;
2944 qband_t *myqbp;
2945 int isevent;
2946 unsigned char qbf[NBAND]; /* band flushing backenable flags */
2948 TRACE_1(TR_FAC_STREAMS_FR,
2949 TR_STRWSRV, "strwsrv:q %p", q);
2950 stp = (struct stdata *)q->q_ptr;
2951 ASSERT(qclaimed(q));
2952 mutex_enter(&stp->sd_lock);
2953 ASSERT(!(stp->sd_flag & STPLEX));
2955 if (stp->sd_flag & WSLEEP) {
2956 stp->sd_flag &= ~WSLEEP;
2957 cv_broadcast(&q->q_wait);
2959 mutex_exit(&stp->sd_lock);
2961 /* The other end of a stream pipe went away. */
2962 if ((tq = q->q_next) == NULL) {
2963 return (0);
2966 /* Find the next module forward that has a service procedure */
2967 claimstr(q);
2968 tq = q->q_nfsrv;
2969 ASSERT(tq != NULL);
2971 if ((q->q_flag & QBACK)) {
2972 if ((tq->q_flag & QFULL)) {
2973 mutex_enter(QLOCK(tq));
2974 if (!(tq->q_flag & QFULL)) {
2975 mutex_exit(QLOCK(tq));
2976 goto wakeup;
2979 * The queue must have become full again. Set QWANTW
2980 * again so strwsrv will be back enabled when
2981 * the queue becomes non-full next time.
2983 tq->q_flag |= QWANTW;
2984 mutex_exit(QLOCK(tq));
2985 } else {
2986 wakeup:
2987 pollwakeup(&stp->sd_pollist, POLLWRNORM);
2988 mutex_enter(&stp->sd_lock);
2989 if (stp->sd_sigflags & S_WRNORM)
2990 strsendsig(stp->sd_siglist, S_WRNORM, 0, 0);
2991 mutex_exit(&stp->sd_lock);
2995 isevent = 0;
2996 i = 1;
2997 bzero((caddr_t)qbf, NBAND);
2998 mutex_enter(QLOCK(tq));
2999 if ((myqbp = q->q_bandp) != NULL)
3000 for (qbp = tq->q_bandp; qbp && myqbp; qbp = qbp->qb_next) {
3001 ASSERT(myqbp);
3002 if ((myqbp->qb_flag & QB_BACK)) {
3003 if (qbp->qb_flag & QB_FULL) {
3005 * The band must have become full again.
3006 * Set QB_WANTW again so strwsrv will
3007 * be back enabled when the band becomes
3008 * non-full next time.
3010 qbp->qb_flag |= QB_WANTW;
3011 } else {
3012 isevent = 1;
3013 qbf[i] = 1;
3016 myqbp = myqbp->qb_next;
3017 i++;
3019 mutex_exit(QLOCK(tq));
3021 if (isevent) {
3022 for (i = tq->q_nband; i; i--) {
3023 if (qbf[i]) {
3024 pollwakeup(&stp->sd_pollist, POLLWRBAND);
3025 mutex_enter(&stp->sd_lock);
3026 if (stp->sd_sigflags & S_WRBAND)
3027 strsendsig(stp->sd_siglist, S_WRBAND,
3028 (uchar_t)i, 0);
3029 mutex_exit(&stp->sd_lock);
3034 releasestr(q);
3035 return (0);
3039 * Special case of strcopyin/strcopyout for copying
3040 * struct strioctl that can deal with both data
3041 * models.
3044 #ifdef _LP64
3046 static int
3047 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3049 struct strioctl32 strioc32;
3050 struct strioctl *striocp;
3052 if (copyflag & U_TO_K) {
3053 ASSERT((copyflag & K_TO_K) == 0);
3055 if ((flag & FMODELS) == DATAMODEL_ILP32) {
3056 if (copyin(from, &strioc32, sizeof (strioc32)))
3057 return (EFAULT);
3059 striocp = (struct strioctl *)to;
3060 striocp->ic_cmd = strioc32.ic_cmd;
3061 striocp->ic_timout = strioc32.ic_timout;
3062 striocp->ic_len = strioc32.ic_len;
3063 striocp->ic_dp = (char *)(uintptr_t)strioc32.ic_dp;
3065 } else { /* NATIVE data model */
3066 if (copyin(from, to, sizeof (struct strioctl))) {
3067 return (EFAULT);
3068 } else {
3069 return (0);
3072 } else {
3073 ASSERT(copyflag & K_TO_K);
3074 bcopy(from, to, sizeof (struct strioctl));
3076 return (0);
3079 static int
3080 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3082 struct strioctl32 strioc32;
3083 struct strioctl *striocp;
3085 if (copyflag & U_TO_K) {
3086 ASSERT((copyflag & K_TO_K) == 0);
3088 if ((flag & FMODELS) == DATAMODEL_ILP32) {
3089 striocp = (struct strioctl *)from;
3090 strioc32.ic_cmd = striocp->ic_cmd;
3091 strioc32.ic_timout = striocp->ic_timout;
3092 strioc32.ic_len = striocp->ic_len;
3093 strioc32.ic_dp = (caddr32_t)(uintptr_t)striocp->ic_dp;
3094 ASSERT((char *)(uintptr_t)strioc32.ic_dp ==
3095 striocp->ic_dp);
3097 if (copyout(&strioc32, to, sizeof (strioc32)))
3098 return (EFAULT);
3100 } else { /* NATIVE data model */
3101 if (copyout(from, to, sizeof (struct strioctl))) {
3102 return (EFAULT);
3103 } else {
3104 return (0);
3107 } else {
3108 ASSERT(copyflag & K_TO_K);
3109 bcopy(from, to, sizeof (struct strioctl));
3111 return (0);
3114 #else /* ! _LP64 */
3116 /* ARGSUSED2 */
3117 static int
3118 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3120 return (strcopyin(from, to, sizeof (struct strioctl), copyflag));
3123 /* ARGSUSED2 */
3124 static int
3125 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3127 return (strcopyout(from, to, sizeof (struct strioctl), copyflag));
3130 #endif /* _LP64 */
3133 * Determine type of job control semantics expected by user. The
3134 * possibilities are:
3135 * JCREAD - Behaves like read() on fd; send SIGTTIN
3136 * JCWRITE - Behaves like write() on fd; send SIGTTOU if TOSTOP set
3137 * JCSETP - Sets a value in the stream; send SIGTTOU, ignore TOSTOP
3138 * JCGETP - Gets a value in the stream; no signals.
3139 * See straccess in strsubr.c for usage of these values.
3141 * This routine also returns -1 for I_STR as a special case; the
3142 * caller must call again with the real ioctl number for
3143 * classification.
3145 static int
3146 job_control_type(int cmd)
3148 switch (cmd) {
3149 case I_STR:
3150 return (-1);
3152 case I_RECVFD:
3153 case I_E_RECVFD:
3154 return (JCREAD);
3156 case I_FDINSERT:
3157 case I_SENDFD:
3158 return (JCWRITE);
3160 case TCSETA:
3161 case TCSETAW:
3162 case TCSETAF:
3163 case TCSBRK:
3164 case TCXONC:
3165 case TCFLSH:
3166 case TCDSET: /* Obsolete */
3167 case TIOCSWINSZ:
3168 case TCSETS:
3169 case TCSETSW:
3170 case TCSETSF:
3171 case TIOCSETD:
3172 case TIOCHPCL:
3173 case TIOCSETP:
3174 case TIOCSETN:
3175 case TIOCEXCL:
3176 case TIOCNXCL:
3177 case TIOCFLUSH:
3178 case TIOCSETC:
3179 case TIOCLBIS:
3180 case TIOCLBIC:
3181 case TIOCLSET:
3182 case TIOCSBRK:
3183 case TIOCCBRK:
3184 case TIOCSDTR:
3185 case TIOCCDTR:
3186 case TIOCSLTC:
3187 case TIOCSTOP:
3188 case TIOCSTART:
3189 case TIOCSTI:
3190 case TIOCSPGRP:
3191 case TIOCMSET:
3192 case TIOCMBIS:
3193 case TIOCMBIC:
3194 case TIOCREMOTE:
3195 case TIOCSIGNAL:
3196 case LDSETT:
3197 case LDSMAP: /* Obsolete */
3198 case DIOCSETP:
3199 case I_FLUSH:
3200 case I_SRDOPT:
3201 case I_SETSIG:
3202 case I_SWROPT:
3203 case I_FLUSHBAND:
3204 case I_SETCLTIME:
3205 case I_SERROPT:
3206 case I_ESETSIG:
3207 case FIONBIO:
3208 case FIOASYNC:
3209 case FIOSETOWN:
3210 case JBOOT: /* Obsolete */
3211 case JTERM: /* Obsolete */
3212 case JTIMOM: /* Obsolete */
3213 case JZOMBOOT: /* Obsolete */
3214 case JAGENT: /* Obsolete */
3215 case JTRUN: /* Obsolete */
3216 case JXTPROTO: /* Obsolete */
3217 return (JCSETP);
3220 return (JCGETP);
3224 * ioctl for streams
3227 strioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, int copyflag,
3228 cred_t *crp, int *rvalp)
3230 struct stdata *stp;
3231 struct strcmd *scp;
3232 struct strioctl strioc;
3233 struct uio uio;
3234 struct iovec iov;
3235 int access;
3236 mblk_t *mp;
3237 int error = 0;
3238 int done = 0;
3239 ssize_t rmin, rmax;
3240 queue_t *wrq;
3241 queue_t *rdq;
3242 boolean_t kioctl = B_FALSE;
3243 uint32_t auditing = AU_AUDITING();
3245 if (flag & FKIOCTL) {
3246 copyflag = K_TO_K;
3247 kioctl = B_TRUE;
3249 ASSERT(vp->v_stream);
3250 ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
3251 stp = vp->v_stream;
3253 TRACE_3(TR_FAC_STREAMS_FR, TR_IOCTL_ENTER,
3254 "strioctl:stp %p cmd %X arg %lX", stp, cmd, arg);
3257 * If the copy is kernel to kernel, make sure that the FNATIVE
3258 * flag is set. After this it would be a serious error to have
3259 * no model flag.
3261 if (copyflag == K_TO_K)
3262 flag = (flag & ~FMODELS) | FNATIVE;
3264 ASSERT((flag & FMODELS) != 0);
3266 wrq = stp->sd_wrq;
3267 rdq = _RD(wrq);
3269 access = job_control_type(cmd);
3271 /* We should never see these here, should be handled by iwscn */
3272 if (cmd == SRIOCSREDIR || cmd == SRIOCISREDIR)
3273 return (EINVAL);
3275 mutex_enter(&stp->sd_lock);
3276 if ((access != -1) && ((error = i_straccess(stp, access)) != 0)) {
3277 mutex_exit(&stp->sd_lock);
3278 return (error);
3280 mutex_exit(&stp->sd_lock);
3283 * Check for sgttyb-related ioctls first, and complain as
3284 * necessary.
3286 switch (cmd) {
3287 case TIOCGETP:
3288 case TIOCSETP:
3289 case TIOCSETN:
3290 if (sgttyb_handling >= 2 && !sgttyb_complaint) {
3291 sgttyb_complaint = B_TRUE;
3292 cmn_err(CE_NOTE,
3293 "application used obsolete TIOC[GS]ET");
3295 if (sgttyb_handling >= 3) {
3296 tsignal(curthread, SIGSYS);
3297 return (EIO);
3299 break;
3302 mutex_enter(&stp->sd_lock);
3304 switch (cmd) {
3305 case I_RECVFD:
3306 case I_E_RECVFD:
3307 case I_PEEK:
3308 case I_NREAD:
3309 case FIONREAD:
3310 case FIORDCHK:
3311 case I_ATMARK:
3312 case FIONBIO:
3313 case FIOASYNC:
3314 if (stp->sd_flag & (STRDERR|STPLEX)) {
3315 error = strgeterr(stp, STRDERR|STPLEX, 0);
3316 if (error != 0) {
3317 mutex_exit(&stp->sd_lock);
3318 return (error);
3321 break;
3323 default:
3324 if (stp->sd_flag & (STRDERR|STWRERR|STPLEX)) {
3325 error = strgeterr(stp, STRDERR|STWRERR|STPLEX, 0);
3326 if (error != 0) {
3327 mutex_exit(&stp->sd_lock);
3328 return (error);
3333 mutex_exit(&stp->sd_lock);
3335 switch (cmd) {
3336 default:
3338 * The stream head has hardcoded knowledge of a
3339 * miscellaneous collection of terminal-, keyboard- and
3340 * mouse-related ioctls, enumerated below. This hardcoded
3341 * knowledge allows the stream head to automatically
3342 * convert transparent ioctl requests made by userland
3343 * programs into I_STR ioctls which many old STREAMS
3344 * modules and drivers require.
3346 * No new ioctls should ever be added to this list.
3347 * Instead, the STREAMS module or driver should be written
3348 * to either handle transparent ioctls or require any
3349 * userland programs to use I_STR ioctls (by returning
3350 * EINVAL to any transparent ioctl requests).
3352 * More importantly, removing ioctls from this list should
3353 * be done with the utmost care, since our STREAMS modules
3354 * and drivers *count* on the stream head performing this
3355 * conversion, and thus may panic while processing
3356 * transparent ioctl request for one of these ioctls (keep
3357 * in mind that third party modules and drivers may have
3358 * similar problems).
3360 if (((cmd & IOCTYPE) == LDIOC) ||
3361 ((cmd & IOCTYPE) == tIOC) ||
3362 ((cmd & IOCTYPE) == TIOC) ||
3363 ((cmd & IOCTYPE) == KIOC) ||
3364 ((cmd & IOCTYPE) == MSIOC) ||
3365 ((cmd & IOCTYPE) == VUIOC)) {
3367 * The ioctl is a tty ioctl - set up strioc buffer
3368 * and call strdoioctl() to do the work.
3370 if (stp->sd_flag & STRHUP)
3371 return (ENXIO);
3372 strioc.ic_cmd = cmd;
3373 strioc.ic_timout = INFTIM;
3375 switch (cmd) {
3377 case TCXONC:
3378 case TCSBRK:
3379 case TCFLSH:
3380 case TCDSET:
3382 int native_arg = (int)arg;
3383 strioc.ic_len = sizeof (int);
3384 strioc.ic_dp = (char *)&native_arg;
3385 return (strdoioctl(stp, &strioc, flag,
3386 K_TO_K, crp, rvalp));
3389 case TCSETA:
3390 case TCSETAW:
3391 case TCSETAF:
3392 strioc.ic_len = sizeof (struct termio);
3393 strioc.ic_dp = (char *)arg;
3394 return (strdoioctl(stp, &strioc, flag,
3395 copyflag, crp, rvalp));
3397 case TCSETS:
3398 case TCSETSW:
3399 case TCSETSF:
3400 strioc.ic_len = sizeof (struct termios);
3401 strioc.ic_dp = (char *)arg;
3402 return (strdoioctl(stp, &strioc, flag,
3403 copyflag, crp, rvalp));
3405 case LDSETT:
3406 strioc.ic_len = sizeof (struct termcb);
3407 strioc.ic_dp = (char *)arg;
3408 return (strdoioctl(stp, &strioc, flag,
3409 copyflag, crp, rvalp));
3411 case TIOCSETP:
3412 strioc.ic_len = sizeof (struct sgttyb);
3413 strioc.ic_dp = (char *)arg;
3414 return (strdoioctl(stp, &strioc, flag,
3415 copyflag, crp, rvalp));
3417 case TIOCSTI:
3418 if ((flag & FREAD) == 0 &&
3419 secpolicy_sti(crp) != 0) {
3420 return (EPERM);
3422 mutex_enter(&stp->sd_lock);
3423 mutex_enter(&curproc->p_splock);
3424 if (stp->sd_sidp != curproc->p_sessp->s_sidp &&
3425 secpolicy_sti(crp) != 0) {
3426 mutex_exit(&curproc->p_splock);
3427 mutex_exit(&stp->sd_lock);
3428 return (EACCES);
3430 mutex_exit(&curproc->p_splock);
3431 mutex_exit(&stp->sd_lock);
3433 strioc.ic_len = sizeof (char);
3434 strioc.ic_dp = (char *)arg;
3435 return (strdoioctl(stp, &strioc, flag,
3436 copyflag, crp, rvalp));
3438 case TIOCSWINSZ:
3439 strioc.ic_len = sizeof (struct winsize);
3440 strioc.ic_dp = (char *)arg;
3441 return (strdoioctl(stp, &strioc, flag,
3442 copyflag, crp, rvalp));
3444 case TIOCSSIZE:
3445 strioc.ic_len = sizeof (struct ttysize);
3446 strioc.ic_dp = (char *)arg;
3447 return (strdoioctl(stp, &strioc, flag,
3448 copyflag, crp, rvalp));
3450 case TIOCSSOFTCAR:
3451 case KIOCTRANS:
3452 case KIOCTRANSABLE:
3453 case KIOCCMD:
3454 case KIOCSDIRECT:
3455 case KIOCSCOMPAT:
3456 case KIOCSKABORTEN:
3457 case KIOCSRPTDELAY:
3458 case KIOCSRPTRATE:
3459 case VUIDSFORMAT:
3460 case TIOCSPPS:
3461 strioc.ic_len = sizeof (int);
3462 strioc.ic_dp = (char *)arg;
3463 return (strdoioctl(stp, &strioc, flag,
3464 copyflag, crp, rvalp));
3466 case KIOCSETKEY:
3467 case KIOCGETKEY:
3468 strioc.ic_len = sizeof (struct kiockey);
3469 strioc.ic_dp = (char *)arg;
3470 return (strdoioctl(stp, &strioc, flag,
3471 copyflag, crp, rvalp));
3473 case KIOCSKEY:
3474 case KIOCGKEY:
3475 strioc.ic_len = sizeof (struct kiockeymap);
3476 strioc.ic_dp = (char *)arg;
3477 return (strdoioctl(stp, &strioc, flag,
3478 copyflag, crp, rvalp));
3480 case KIOCSLED:
3481 /* arg is a pointer to char */
3482 strioc.ic_len = sizeof (char);
3483 strioc.ic_dp = (char *)arg;
3484 return (strdoioctl(stp, &strioc, flag,
3485 copyflag, crp, rvalp));
3487 case MSIOSETPARMS:
3488 strioc.ic_len = sizeof (Ms_parms);
3489 strioc.ic_dp = (char *)arg;
3490 return (strdoioctl(stp, &strioc, flag,
3491 copyflag, crp, rvalp));
3493 case VUIDSADDR:
3494 case VUIDGADDR:
3495 strioc.ic_len = sizeof (struct vuid_addr_probe);
3496 strioc.ic_dp = (char *)arg;
3497 return (strdoioctl(stp, &strioc, flag,
3498 copyflag, crp, rvalp));
3501 * These M_IOCTL's don't require any data to be sent
3502 * downstream, and the driver will allocate and link
3503 * on its own mblk_t upon M_IOCACK -- thus we set
3504 * ic_len to zero and set ic_dp to arg so we know
3505 * where to copyout to later.
3507 case TIOCGSOFTCAR:
3508 case TIOCGWINSZ:
3509 case TIOCGSIZE:
3510 case KIOCGTRANS:
3511 case KIOCGTRANSABLE:
3512 case KIOCTYPE:
3513 case KIOCGDIRECT:
3514 case KIOCGCOMPAT:
3515 case KIOCLAYOUT:
3516 case KIOCGLED:
3517 case MSIOGETPARMS:
3518 case MSIOBUTTONS:
3519 case VUIDGFORMAT:
3520 case TIOCGPPS:
3521 case TIOCGPPSEV:
3522 case TCGETA:
3523 case TCGETS:
3524 case LDGETT:
3525 case TIOCGETP:
3526 case KIOCGRPTDELAY:
3527 case KIOCGRPTRATE:
3528 strioc.ic_len = 0;
3529 strioc.ic_dp = (char *)arg;
3530 return (strdoioctl(stp, &strioc, flag,
3531 copyflag, crp, rvalp));
3536 * Unknown cmd - send it down as a transparent ioctl.
3538 strioc.ic_cmd = cmd;
3539 strioc.ic_timout = INFTIM;
3540 strioc.ic_len = TRANSPARENT;
3541 strioc.ic_dp = (char *)&arg;
3543 return (strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp));
3545 case I_STR:
3547 * Stream ioctl. Read in an strioctl buffer from the user
3548 * along with any data specified and send it downstream.
3549 * Strdoioctl will wait allow only one ioctl message at
3550 * a time, and waits for the acknowledgement.
3553 if (stp->sd_flag & STRHUP)
3554 return (ENXIO);
3556 error = strcopyin_strioctl((void *)arg, &strioc, flag,
3557 copyflag);
3558 if (error != 0)
3559 return (error);
3561 if ((strioc.ic_len < 0) || (strioc.ic_timout < -1))
3562 return (EINVAL);
3564 access = job_control_type(strioc.ic_cmd);
3565 mutex_enter(&stp->sd_lock);
3566 if ((access != -1) &&
3567 ((error = i_straccess(stp, access)) != 0)) {
3568 mutex_exit(&stp->sd_lock);
3569 return (error);
3571 mutex_exit(&stp->sd_lock);
3574 * The I_STR facility provides a trap door for malicious
3575 * code to send down bogus streamio(7I) ioctl commands to
3576 * unsuspecting STREAMS modules and drivers which expect to
3577 * only get these messages from the stream head.
3578 * Explicitly prohibit any streamio ioctls which can be
3579 * passed downstream by the stream head. Note that we do
3580 * not block all streamio ioctls because the ioctl
3581 * numberspace is not well managed and thus it's possible
3582 * that a module or driver's ioctl numbers may accidentally
3583 * collide with them.
3585 switch (strioc.ic_cmd) {
3586 case I_LINK:
3587 case I_PLINK:
3588 case I_UNLINK:
3589 case I_PUNLINK:
3590 case _I_GETPEERCRED:
3591 case _I_PLINK_LH:
3592 return (EINVAL);
3595 error = strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp);
3596 if (error == 0) {
3597 error = strcopyout_strioctl(&strioc, (void *)arg,
3598 flag, copyflag);
3600 return (error);
3602 case _I_CMD:
3604 * Like I_STR, but without using M_IOC* messages and without
3605 * copyins/copyouts beyond the passed-in argument.
3607 if (stp->sd_flag & STRHUP)
3608 return (ENXIO);
3610 if ((scp = kmem_alloc(sizeof (strcmd_t), KM_NOSLEEP)) == NULL)
3611 return (ENOMEM);
3613 if (copyin((void *)arg, scp, sizeof (strcmd_t))) {
3614 kmem_free(scp, sizeof (strcmd_t));
3615 return (EFAULT);
3618 access = job_control_type(scp->sc_cmd);
3619 mutex_enter(&stp->sd_lock);
3620 if (access != -1 && (error = i_straccess(stp, access)) != 0) {
3621 mutex_exit(&stp->sd_lock);
3622 kmem_free(scp, sizeof (strcmd_t));
3623 return (error);
3625 mutex_exit(&stp->sd_lock);
3627 *rvalp = 0;
3628 if ((error = strdocmd(stp, scp, crp)) == 0) {
3629 if (copyout(scp, (void *)arg, sizeof (strcmd_t)))
3630 error = EFAULT;
3632 kmem_free(scp, sizeof (strcmd_t));
3633 return (error);
3635 case I_NREAD:
3637 * Return number of bytes of data in first message
3638 * in queue in "arg" and return the number of messages
3639 * in queue in return value.
3642 size_t size;
3643 int retval;
3644 int count = 0;
3646 mutex_enter(QLOCK(rdq));
3648 size = msgdsize(rdq->q_first);
3649 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3650 count++;
3652 mutex_exit(QLOCK(rdq));
3653 if (stp->sd_struiordq) {
3654 infod_t infod;
3656 infod.d_cmd = INFOD_COUNT;
3657 infod.d_count = 0;
3658 if (count == 0) {
3659 infod.d_cmd |= INFOD_FIRSTBYTES;
3660 infod.d_bytes = 0;
3662 infod.d_res = 0;
3663 (void) infonext(rdq, &infod);
3664 count += infod.d_count;
3665 if (infod.d_res & INFOD_FIRSTBYTES)
3666 size = infod.d_bytes;
3670 * Drop down from size_t to the "int" required by the
3671 * interface. Cap at INT_MAX.
3673 retval = MIN(size, INT_MAX);
3674 error = strcopyout(&retval, (void *)arg, sizeof (retval),
3675 copyflag);
3676 if (!error)
3677 *rvalp = count;
3678 return (error);
3681 case FIONREAD:
3683 * Return number of bytes of data in all data messages
3684 * in queue in "arg".
3687 size_t size = 0;
3688 int retval;
3690 mutex_enter(QLOCK(rdq));
3691 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3692 size += msgdsize(mp);
3693 mutex_exit(QLOCK(rdq));
3695 if (stp->sd_struiordq) {
3696 infod_t infod;
3698 infod.d_cmd = INFOD_BYTES;
3699 infod.d_res = 0;
3700 infod.d_bytes = 0;
3701 (void) infonext(rdq, &infod);
3702 size += infod.d_bytes;
3706 * Drop down from size_t to the "int" required by the
3707 * interface. Cap at INT_MAX.
3709 retval = MIN(size, INT_MAX);
3710 error = strcopyout(&retval, (void *)arg, sizeof (retval),
3711 copyflag);
3713 *rvalp = 0;
3714 return (error);
3716 case FIORDCHK:
3718 * FIORDCHK does not use arg value (like FIONREAD),
3719 * instead a count is returned. I_NREAD value may
3720 * not be accurate but safe. The real thing to do is
3721 * to add the msgdsizes of all data messages until
3722 * a non-data message.
3725 size_t size = 0;
3727 mutex_enter(QLOCK(rdq));
3728 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3729 size += msgdsize(mp);
3730 mutex_exit(QLOCK(rdq));
3732 if (stp->sd_struiordq) {
3733 infod_t infod;
3735 infod.d_cmd = INFOD_BYTES;
3736 infod.d_res = 0;
3737 infod.d_bytes = 0;
3738 (void) infonext(rdq, &infod);
3739 size += infod.d_bytes;
3743 * Since ioctl returns an int, and memory sizes under
3744 * LP64 may not fit, we return INT_MAX if the count was
3745 * actually greater.
3747 *rvalp = MIN(size, INT_MAX);
3748 return (0);
3751 case I_FIND:
3753 * Get module name.
3756 char mname[FMNAMESZ + 1];
3757 queue_t *q;
3759 error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3760 mname, FMNAMESZ + 1, NULL);
3761 if (error)
3762 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3765 * Return EINVAL if we're handed a bogus module name.
3767 if (fmodsw_find(mname, FMODSW_LOAD) == NULL) {
3768 TRACE_0(TR_FAC_STREAMS_FR,
3769 TR_I_CANT_FIND, "couldn't I_FIND");
3770 return (EINVAL);
3773 *rvalp = 0;
3775 /* Look downstream to see if module is there. */
3776 claimstr(stp->sd_wrq);
3777 for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
3778 if (q->q_flag & QREADR) {
3779 q = NULL;
3780 break;
3782 if (strcmp(mname, Q2NAME(q)) == 0)
3783 break;
3785 releasestr(stp->sd_wrq);
3787 *rvalp = (q ? 1 : 0);
3788 return (error);
3791 case I_PUSH:
3792 case __I_PUSH_NOCTTY:
3794 * Push a module.
3795 * For the case __I_PUSH_NOCTTY push a module but
3796 * do not allocate controlling tty. See bugid 4025044
3800 char mname[FMNAMESZ + 1];
3801 fmodsw_impl_t *fp;
3802 dev_t dummydev;
3804 if (stp->sd_flag & STRHUP)
3805 return (ENXIO);
3808 * Get module name and look up in fmodsw.
3810 error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3811 mname, FMNAMESZ + 1, NULL);
3812 if (error)
3813 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3815 if ((fp = fmodsw_find(mname, FMODSW_HOLD | FMODSW_LOAD)) ==
3816 NULL)
3817 return (EINVAL);
3819 TRACE_2(TR_FAC_STREAMS_FR, TR_I_PUSH,
3820 "I_PUSH:fp %p stp %p", fp, stp);
3822 if (error = strstartplumb(stp, flag, cmd)) {
3823 fmodsw_rele(fp);
3824 return (error);
3828 * See if any more modules can be pushed on this stream.
3829 * Note that this check must be done after strstartplumb()
3830 * since otherwise multiple threads issuing I_PUSHes on
3831 * the same stream will be able to exceed nstrpush.
3833 mutex_enter(&stp->sd_lock);
3834 if (stp->sd_pushcnt >= nstrpush) {
3835 fmodsw_rele(fp);
3836 strendplumb(stp);
3837 mutex_exit(&stp->sd_lock);
3838 return (EINVAL);
3840 mutex_exit(&stp->sd_lock);
3843 * Push new module and call its open routine
3844 * via qattach(). Modules don't change device
3845 * numbers, so just ignore dummydev here.
3847 dummydev = vp->v_rdev;
3848 if ((error = qattach(rdq, &dummydev, 0, crp, fp,
3849 B_FALSE)) == 0) {
3850 if (vp->v_type == VCHR && /* sorry, no pipes allowed */
3851 (cmd == I_PUSH) && (stp->sd_flag & STRISTTY)) {
3853 * try to allocate it as a controlling terminal
3855 (void) strctty(stp);
3859 mutex_enter(&stp->sd_lock);
3862 * As a performance concern we are caching the values of
3863 * q_minpsz and q_maxpsz of the module below the stream
3864 * head in the stream head.
3866 mutex_enter(QLOCK(stp->sd_wrq->q_next));
3867 rmin = stp->sd_wrq->q_next->q_minpsz;
3868 rmax = stp->sd_wrq->q_next->q_maxpsz;
3869 mutex_exit(QLOCK(stp->sd_wrq->q_next));
3871 /* Do this processing here as a performance concern */
3872 if (strmsgsz != 0) {
3873 if (rmax == INFPSZ)
3874 rmax = strmsgsz;
3875 else {
3876 if (vp->v_type == VFIFO)
3877 rmax = MIN(PIPE_BUF, rmax);
3878 else rmax = MIN(strmsgsz, rmax);
3882 mutex_enter(QLOCK(wrq));
3883 stp->sd_qn_minpsz = rmin;
3884 stp->sd_qn_maxpsz = rmax;
3885 mutex_exit(QLOCK(wrq));
3887 strendplumb(stp);
3888 mutex_exit(&stp->sd_lock);
3889 return (error);
3892 case I_POP:
3894 queue_t *q;
3896 if (stp->sd_flag & STRHUP)
3897 return (ENXIO);
3898 if (!wrq->q_next) /* for broken pipes */
3899 return (EINVAL);
3901 if (error = strstartplumb(stp, flag, cmd))
3902 return (error);
3905 * If there is an anchor on this stream and popping
3906 * the current module would attempt to pop through the
3907 * anchor, then disallow the pop unless we have sufficient
3908 * privileges; take the cheapest (non-locking) check
3909 * first.
3911 if (secpolicy_ip_config(crp, B_TRUE) != 0 ||
3912 (stp->sd_anchorzone != crgetzoneid(crp))) {
3913 mutex_enter(&stp->sd_lock);
3915 * Anchors only apply if there's at least one
3916 * module on the stream (sd_pushcnt > 0).
3918 if (stp->sd_pushcnt > 0 &&
3919 stp->sd_pushcnt == stp->sd_anchor &&
3920 stp->sd_vnode->v_type != VFIFO) {
3921 strendplumb(stp);
3922 mutex_exit(&stp->sd_lock);
3923 if (stp->sd_anchorzone != crgetzoneid(crp))
3924 return (EINVAL);
3925 /* Audit and report error */
3926 return (secpolicy_ip_config(crp, B_FALSE));
3928 mutex_exit(&stp->sd_lock);
3931 q = wrq->q_next;
3932 TRACE_2(TR_FAC_STREAMS_FR, TR_I_POP,
3933 "I_POP:%p from %p", q, stp);
3934 if (q->q_next == NULL || (q->q_flag & (QREADR|QISDRV))) {
3935 error = EINVAL;
3936 } else {
3937 qdetach(_RD(q), 1, flag, crp, B_FALSE);
3938 error = 0;
3940 mutex_enter(&stp->sd_lock);
3943 * As a performance concern we are caching the values of
3944 * q_minpsz and q_maxpsz of the module below the stream
3945 * head in the stream head.
3947 mutex_enter(QLOCK(wrq->q_next));
3948 rmin = wrq->q_next->q_minpsz;
3949 rmax = wrq->q_next->q_maxpsz;
3950 mutex_exit(QLOCK(wrq->q_next));
3952 /* Do this processing here as a performance concern */
3953 if (strmsgsz != 0) {
3954 if (rmax == INFPSZ)
3955 rmax = strmsgsz;
3956 else {
3957 if (vp->v_type == VFIFO)
3958 rmax = MIN(PIPE_BUF, rmax);
3959 else rmax = MIN(strmsgsz, rmax);
3963 mutex_enter(QLOCK(wrq));
3964 stp->sd_qn_minpsz = rmin;
3965 stp->sd_qn_maxpsz = rmax;
3966 mutex_exit(QLOCK(wrq));
3968 /* If we popped through the anchor, then reset the anchor. */
3969 if (stp->sd_pushcnt < stp->sd_anchor) {
3970 stp->sd_anchor = 0;
3971 stp->sd_anchorzone = 0;
3973 strendplumb(stp);
3974 mutex_exit(&stp->sd_lock);
3975 return (error);
3978 case _I_MUXID2FD:
3981 * Create a fd for a I_PLINK'ed lower stream with a given
3982 * muxid. With the fd, application can send down ioctls,
3983 * like I_LIST, to the previously I_PLINK'ed stream. Note
3984 * that after getting the fd, the application has to do an
3985 * I_PUNLINK on the muxid before it can do any operation
3986 * on the lower stream. This is required by spec1170.
3988 * The fd used to do this ioctl should point to the same
3989 * controlling device used to do the I_PLINK. If it uses
3990 * a different stream or an invalid muxid, I_MUXID2FD will
3991 * fail. The error code is set to EINVAL.
3993 * The intended use of this interface is the following.
3994 * An application I_PLINK'ed a stream and exits. The fd
3995 * to the lower stream is gone. Another application
3996 * wants to get a fd to the lower stream, it uses I_MUXID2FD.
3998 int muxid = (int)arg;
3999 int fd;
4000 linkinfo_t *linkp;
4001 struct file *fp;
4002 netstack_t *ns;
4003 str_stack_t *ss;
4006 * Do not allow the wildcard muxid. This ioctl is not
4007 * intended to find arbitrary link.
4009 if (muxid == 0) {
4010 return (EINVAL);
4013 ns = netstack_find_by_cred(crp);
4014 ASSERT(ns != NULL);
4015 ss = ns->netstack_str;
4016 ASSERT(ss != NULL);
4018 mutex_enter(&muxifier);
4019 linkp = findlinks(vp->v_stream, muxid, LINKPERSIST, ss);
4020 if (linkp == NULL) {
4021 mutex_exit(&muxifier);
4022 netstack_rele(ss->ss_netstack);
4023 return (EINVAL);
4026 if ((fd = ufalloc(0)) == -1) {
4027 mutex_exit(&muxifier);
4028 netstack_rele(ss->ss_netstack);
4029 return (EMFILE);
4031 fp = linkp->li_fpdown;
4032 mutex_enter(&fp->f_tlock);
4033 fp->f_count++;
4034 mutex_exit(&fp->f_tlock);
4035 mutex_exit(&muxifier);
4036 setf(fd, fp);
4037 *rvalp = fd;
4038 netstack_rele(ss->ss_netstack);
4039 return (0);
4042 case _I_INSERT:
4045 * To insert a module to a given position in a stream.
4046 * In the first release, only allow privileged user
4047 * to use this ioctl. Furthermore, the insert is only allowed
4048 * below an anchor if the zoneid is the same as the zoneid
4049 * which created the anchor.
4051 * Note that we do not plan to support this ioctl
4052 * on pipes in the first release. We want to learn more
4053 * about the implications of these ioctls before extending
4054 * their support. And we do not think these features are
4055 * valuable for pipes.
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;
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);
4076 error = strcopyin((void *)arg, STRUCT_BUF(strmodinsert),
4077 STRUCT_SIZE(strmodinsert), copyflag);
4078 if (error)
4079 return (error);
4082 * Get module name and look up in fmodsw.
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);
4090 if ((fp = fmodsw_find(mod_name, FMODSW_HOLD | FMODSW_LOAD)) ==
4091 NULL)
4092 return (EINVAL);
4094 if (error = strstartplumb(stp, flag, cmd)) {
4095 fmodsw_rele(fp);
4096 return (error);
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.
4104 pos = STRUCT_FGET(strmodinsert, pos);
4105 is_insert = (pos != 0);
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.
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);
4120 if (stp->sd_anchor != 0) {
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.
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);
4136 mutex_exit(&stp->sd_lock);
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.
4143 * Insert new module and call its open routine
4144 * via qattach(). Modules don't change device
4145 * numbers, so just ignore dummydev here.
4147 for (tmp_wrq = stp->sd_wrq; pos > 0;
4148 tmp_wrq = tmp_wrq->q_next, pos--) {
4149 ASSERT(SAMESTR(tmp_wrq));
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);
4160 mutex_enter(&stp->sd_lock);
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.
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));
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);
4182 mutex_enter(QLOCK(wrq));
4183 stp->sd_qn_minpsz = rmin;
4184 stp->sd_qn_maxpsz = rmax;
4185 mutex_exit(QLOCK(wrq));
4189 * Need to update the anchor value if this module is
4190 * inserted below the anchor point.
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++;
4198 strendplumb(stp);
4199 mutex_exit(&stp->sd_lock);
4200 return (0);
4203 case _I_REMOVE:
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.
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.
4222 * Also note that _I_REMOVE cannot be used to remove a
4223 * driver or the stream head.
4225 STRUCT_DECL(strmodconf, strmodremove);
4226 queue_t *q;
4227 int pos;
4228 char mod_name[FMNAMESZ + 1];
4229 boolean_t is_remove;
4231 STRUCT_INIT(strmodremove, flag);
4232 if (stp->sd_flag & STRHUP)
4233 return (ENXIO);
4234 if (STRMATED(stp))
4235 return (EINVAL);
4236 if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4237 return (error);
4238 if (stp->sd_anchor != 0 &&
4239 stp->sd_anchorzone != crgetzoneid(crp))
4240 return (EINVAL);
4242 error = strcopyin((void *)arg, STRUCT_BUF(strmodremove),
4243 STRUCT_SIZE(strmodremove), copyflag);
4244 if (error)
4245 return (error);
4247 error = (copyflag & U_TO_K ? copyinstr :
4248 copystr)(STRUCT_FGETP(strmodremove, mod_name),
4249 mod_name, FMNAMESZ + 1, NULL);
4250 if (error)
4251 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4253 if ((error = strstartplumb(stp, flag, cmd)) != 0)
4254 return (error);
4257 * Match the name of given module to the name of module at
4258 * the given position.
4260 pos = STRUCT_FGET(strmodremove, pos);
4262 is_remove = (pos != 0);
4263 for (q = stp->sd_wrq->q_next; SAMESTR(q) && pos > 0;
4264 q = q->q_next, pos--)
4266 if (pos > 0 || !SAMESTR(q) ||
4267 strcmp(Q2NAME(q), mod_name) != 0) {
4268 mutex_enter(&stp->sd_lock);
4269 strendplumb(stp);
4270 mutex_exit(&stp->sd_lock);
4271 return (EINVAL);
4275 * If the position is at or below an anchor, then the zoneid
4276 * must match the zoneid that created the anchor.
4278 if (stp->sd_anchor != 0) {
4279 pos = STRUCT_FGET(strmodremove, pos);
4280 if (pos >= (stp->sd_pushcnt - stp->sd_anchor) &&
4281 stp->sd_anchorzone != crgetzoneid(crp)) {
4282 mutex_enter(&stp->sd_lock);
4283 strendplumb(stp);
4284 mutex_exit(&stp->sd_lock);
4285 return (EPERM);
4290 ASSERT(!(q->q_flag & QREADR));
4291 qdetach(_RD(q), 1, flag, crp, is_remove);
4293 mutex_enter(&stp->sd_lock);
4296 * As a performance concern we are caching the values of
4297 * q_minpsz and q_maxpsz of the module below the stream
4298 * head in the stream head.
4300 if (!is_remove) {
4301 mutex_enter(QLOCK(wrq->q_next));
4302 rmin = wrq->q_next->q_minpsz;
4303 rmax = wrq->q_next->q_maxpsz;
4304 mutex_exit(QLOCK(wrq->q_next));
4306 /* Do this processing here as a performance concern */
4307 if (strmsgsz != 0) {
4308 if (rmax == INFPSZ)
4309 rmax = strmsgsz;
4310 else {
4311 if (vp->v_type == VFIFO)
4312 rmax = MIN(PIPE_BUF, rmax);
4313 else rmax = MIN(strmsgsz, rmax);
4317 mutex_enter(QLOCK(wrq));
4318 stp->sd_qn_minpsz = rmin;
4319 stp->sd_qn_maxpsz = rmax;
4320 mutex_exit(QLOCK(wrq));
4324 * Need to update the anchor value if this module is removed
4325 * at or below the anchor point. If the removed module is at
4326 * the anchor point, remove the anchor for this stream if
4327 * there is no module above the anchor point. Otherwise, if
4328 * the removed module is below the anchor point, decrement the
4329 * anchor point by 1.
4331 if (stp->sd_anchor != 0) {
4332 pos = STRUCT_FGET(strmodremove, pos);
4333 if (pos == stp->sd_pushcnt - stp->sd_anchor + 1)
4334 stp->sd_anchor = 0;
4335 else if (pos > (stp->sd_pushcnt - stp->sd_anchor + 1))
4336 stp->sd_anchor--;
4339 strendplumb(stp);
4340 mutex_exit(&stp->sd_lock);
4341 return (0);
4344 case I_ANCHOR:
4346 * Set the anchor position on the stream to reside at
4347 * the top module (in other words, the top module
4348 * cannot be popped). Anchors with a FIFO make no
4349 * obvious sense, so they're not allowed.
4351 mutex_enter(&stp->sd_lock);
4353 if (stp->sd_vnode->v_type == VFIFO) {
4354 mutex_exit(&stp->sd_lock);
4355 return (EINVAL);
4357 /* Only allow the same zoneid to update the anchor */
4358 if (stp->sd_anchor != 0 &&
4359 stp->sd_anchorzone != crgetzoneid(crp)) {
4360 mutex_exit(&stp->sd_lock);
4361 return (EINVAL);
4363 stp->sd_anchor = stp->sd_pushcnt;
4364 stp->sd_anchorzone = crgetzoneid(crp);
4365 mutex_exit(&stp->sd_lock);
4366 return (0);
4368 case I_LOOK:
4370 * Get name of first module downstream.
4371 * If no module, return an error.
4373 claimstr(wrq);
4374 if (_SAMESTR(wrq) && wrq->q_next->q_next != NULL) {
4375 char *name = Q2NAME(wrq->q_next);
4377 error = strcopyout(name, (void *)arg, strlen(name) + 1,
4378 copyflag);
4379 releasestr(wrq);
4380 return (error);
4382 releasestr(wrq);
4383 return (EINVAL);
4385 case I_LINK:
4386 case I_PLINK:
4388 * Link a multiplexor.
4390 return (mlink(vp, cmd, (int)arg, crp, rvalp, 0));
4392 case _I_PLINK_LH:
4394 * Link a multiplexor: Call must originate from kernel.
4396 if (kioctl)
4397 return (ldi_mlink_lh(vp, cmd, arg, crp, rvalp));
4399 return (EINVAL);
4400 case I_UNLINK:
4401 case I_PUNLINK:
4403 * Unlink a multiplexor.
4404 * If arg is -1, unlink all links for which this is the
4405 * controlling stream. Otherwise, arg is an index number
4406 * for a link to be removed.
4409 struct linkinfo *linkp;
4410 int native_arg = (int)arg;
4411 int type;
4412 netstack_t *ns;
4413 str_stack_t *ss;
4415 TRACE_1(TR_FAC_STREAMS_FR,
4416 TR_I_UNLINK, "I_UNLINK/I_PUNLINK:%p", stp);
4417 if (vp->v_type == VFIFO) {
4418 return (EINVAL);
4420 if (cmd == I_UNLINK)
4421 type = LINKNORMAL;
4422 else /* I_PUNLINK */
4423 type = LINKPERSIST;
4424 if (native_arg == 0) {
4425 return (EINVAL);
4427 ns = netstack_find_by_cred(crp);
4428 ASSERT(ns != NULL);
4429 ss = ns->netstack_str;
4430 ASSERT(ss != NULL);
4432 if (native_arg == MUXID_ALL)
4433 error = munlinkall(stp, type, crp, rvalp, ss);
4434 else {
4435 mutex_enter(&muxifier);
4436 if (!(linkp = findlinks(stp, (int)arg, type, ss))) {
4437 /* invalid user supplied index number */
4438 mutex_exit(&muxifier);
4439 netstack_rele(ss->ss_netstack);
4440 return (EINVAL);
4442 /* munlink drops the muxifier lock */
4443 error = munlink(stp, linkp, type, crp, rvalp, ss);
4445 netstack_rele(ss->ss_netstack);
4446 return (error);
4449 case I_FLUSH:
4451 * send a flush message downstream
4452 * flush message can indicate
4453 * FLUSHR - flush read queue
4454 * FLUSHW - flush write queue
4455 * FLUSHRW - flush read/write queue
4457 if (stp->sd_flag & STRHUP)
4458 return (ENXIO);
4459 if (arg & ~FLUSHRW)
4460 return (EINVAL);
4462 for (;;) {
4463 if (putnextctl1(stp->sd_wrq, M_FLUSH, (int)arg)) {
4464 break;
4466 if (error = strwaitbuf(1, BPRI_HI)) {
4467 return (error);
4472 * Send down an unsupported ioctl and wait for the nack
4473 * in order to allow the M_FLUSH to propagate back
4474 * up to the stream head.
4475 * Replaces if (qready()) runqueues();
4477 strioc.ic_cmd = -1; /* The unsupported ioctl */
4478 strioc.ic_timout = 0;
4479 strioc.ic_len = 0;
4480 strioc.ic_dp = NULL;
4481 (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4482 *rvalp = 0;
4483 return (0);
4485 case I_FLUSHBAND:
4487 struct bandinfo binfo;
4489 error = strcopyin((void *)arg, &binfo, sizeof (binfo),
4490 copyflag);
4491 if (error)
4492 return (error);
4493 if (stp->sd_flag & STRHUP)
4494 return (ENXIO);
4495 if (binfo.bi_flag & ~FLUSHRW)
4496 return (EINVAL);
4497 while (!(mp = allocb(2, BPRI_HI))) {
4498 if (error = strwaitbuf(2, BPRI_HI))
4499 return (error);
4501 mp->b_datap->db_type = M_FLUSH;
4502 *mp->b_wptr++ = binfo.bi_flag | FLUSHBAND;
4503 *mp->b_wptr++ = binfo.bi_pri;
4504 putnext(stp->sd_wrq, mp);
4506 * Send down an unsupported ioctl and wait for the nack
4507 * in order to allow the M_FLUSH to propagate back
4508 * up to the stream head.
4509 * Replaces if (qready()) runqueues();
4511 strioc.ic_cmd = -1; /* The unsupported ioctl */
4512 strioc.ic_timout = 0;
4513 strioc.ic_len = 0;
4514 strioc.ic_dp = NULL;
4515 (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4516 *rvalp = 0;
4517 return (0);
4520 case I_SRDOPT:
4522 * Set read options
4524 * RNORM - default stream mode
4525 * RMSGN - message no discard
4526 * RMSGD - message discard
4527 * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
4528 * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
4529 * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
4531 if (arg & ~(RMODEMASK | RPROTMASK))
4532 return (EINVAL);
4534 if ((arg & (RMSGD|RMSGN)) == (RMSGD|RMSGN))
4535 return (EINVAL);
4537 mutex_enter(&stp->sd_lock);
4538 switch (arg & RMODEMASK) {
4539 case RNORM:
4540 stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
4541 break;
4542 case RMSGD:
4543 stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGNODIS) |
4544 RD_MSGDIS;
4545 break;
4546 case RMSGN:
4547 stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGDIS) |
4548 RD_MSGNODIS;
4549 break;
4552 switch (arg & RPROTMASK) {
4553 case RPROTNORM:
4554 stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
4555 break;
4557 case RPROTDAT:
4558 stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDIS) |
4559 RD_PROTDAT);
4560 break;
4562 case RPROTDIS:
4563 stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDAT) |
4564 RD_PROTDIS);
4565 break;
4567 mutex_exit(&stp->sd_lock);
4568 return (0);
4570 case I_GRDOPT:
4572 * Get read option and return the value
4573 * to spot pointed to by arg
4576 int rdopt;
4578 rdopt = ((stp->sd_read_opt & RD_MSGDIS) ? RMSGD :
4579 ((stp->sd_read_opt & RD_MSGNODIS) ? RMSGN : RNORM));
4580 rdopt |= ((stp->sd_read_opt & RD_PROTDAT) ? RPROTDAT :
4581 ((stp->sd_read_opt & RD_PROTDIS) ? RPROTDIS : RPROTNORM));
4583 return (strcopyout(&rdopt, (void *)arg, sizeof (int),
4584 copyflag));
4587 case I_SERROPT:
4589 * Set error options
4591 * RERRNORM - persistent read errors
4592 * RERRNONPERSIST - non-persistent read errors
4593 * WERRNORM - persistent write errors
4594 * WERRNONPERSIST - non-persistent write errors
4596 if (arg & ~(RERRMASK | WERRMASK))
4597 return (EINVAL);
4599 mutex_enter(&stp->sd_lock);
4600 switch (arg & RERRMASK) {
4601 case RERRNORM:
4602 stp->sd_flag &= ~STRDERRNONPERSIST;
4603 break;
4604 case RERRNONPERSIST:
4605 stp->sd_flag |= STRDERRNONPERSIST;
4606 break;
4608 switch (arg & WERRMASK) {
4609 case WERRNORM:
4610 stp->sd_flag &= ~STWRERRNONPERSIST;
4611 break;
4612 case WERRNONPERSIST:
4613 stp->sd_flag |= STWRERRNONPERSIST;
4614 break;
4616 mutex_exit(&stp->sd_lock);
4617 return (0);
4619 case I_GERROPT:
4621 * Get error option and return the value
4622 * to spot pointed to by arg
4625 int erropt = 0;
4627 erropt |= (stp->sd_flag & STRDERRNONPERSIST) ? RERRNONPERSIST :
4628 RERRNORM;
4629 erropt |= (stp->sd_flag & STWRERRNONPERSIST) ? WERRNONPERSIST :
4630 WERRNORM;
4631 return (strcopyout(&erropt, (void *)arg, sizeof (int),
4632 copyflag));
4635 case I_SETSIG:
4637 * Register the calling proc to receive the SIGPOLL
4638 * signal based on the events given in arg. If
4639 * arg is zero, remove the proc from register list.
4642 strsig_t *ssp, *pssp;
4643 struct pid *pidp;
4645 pssp = NULL;
4646 pidp = curproc->p_pidp;
4648 * Hold sd_lock to prevent traversal of sd_siglist while
4649 * it is modified.
4651 mutex_enter(&stp->sd_lock);
4652 for (ssp = stp->sd_siglist; ssp && (ssp->ss_pidp != pidp);
4653 pssp = ssp, ssp = ssp->ss_next)
4656 if (arg) {
4657 if (arg & ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4658 S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4659 mutex_exit(&stp->sd_lock);
4660 return (EINVAL);
4662 if ((arg & S_BANDURG) && !(arg & S_RDBAND)) {
4663 mutex_exit(&stp->sd_lock);
4664 return (EINVAL);
4668 * If proc not already registered, add it
4669 * to list.
4671 if (!ssp) {
4672 ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4673 ssp->ss_pidp = pidp;
4674 ssp->ss_pid = pidp->pid_id;
4675 ssp->ss_next = NULL;
4676 if (pssp)
4677 pssp->ss_next = ssp;
4678 else
4679 stp->sd_siglist = ssp;
4680 mutex_enter(&pidlock);
4681 PID_HOLD(pidp);
4682 mutex_exit(&pidlock);
4686 * Set events.
4688 ssp->ss_events = (int)arg;
4689 } else {
4691 * Remove proc from register list.
4693 if (ssp) {
4694 mutex_enter(&pidlock);
4695 PID_RELE(pidp);
4696 mutex_exit(&pidlock);
4697 if (pssp)
4698 pssp->ss_next = ssp->ss_next;
4699 else
4700 stp->sd_siglist = ssp->ss_next;
4701 kmem_free(ssp, sizeof (strsig_t));
4702 } else {
4703 mutex_exit(&stp->sd_lock);
4704 return (EINVAL);
4709 * Recalculate OR of sig events.
4711 stp->sd_sigflags = 0;
4712 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4713 stp->sd_sigflags |= ssp->ss_events;
4714 mutex_exit(&stp->sd_lock);
4715 return (0);
4718 case I_GETSIG:
4720 * Return (in arg) the current registration of events
4721 * for which the calling proc is to be signaled.
4724 struct strsig *ssp;
4725 struct pid *pidp;
4727 pidp = curproc->p_pidp;
4728 mutex_enter(&stp->sd_lock);
4729 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4730 if (ssp->ss_pidp == pidp) {
4731 error = strcopyout(&ssp->ss_events, (void *)arg,
4732 sizeof (int), copyflag);
4733 mutex_exit(&stp->sd_lock);
4734 return (error);
4736 mutex_exit(&stp->sd_lock);
4737 return (EINVAL);
4740 case I_ESETSIG:
4742 * Register the ss_pid to receive the SIGPOLL
4743 * signal based on the events is ss_events arg. If
4744 * ss_events is zero, remove the proc from register list.
4747 struct strsig *ssp, *pssp;
4748 struct proc *proc;
4749 struct pid *pidp;
4750 pid_t pid;
4751 struct strsigset ss;
4753 error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4754 if (error)
4755 return (error);
4757 pid = ss.ss_pid;
4759 if (ss.ss_events != 0) {
4761 * Permissions check by sending signal 0.
4762 * Note that when kill fails it does a set_errno
4763 * causing the system call to fail.
4765 error = kill(pid, 0);
4766 if (error) {
4767 return (error);
4770 mutex_enter(&pidlock);
4771 if (pid == 0)
4772 proc = curproc;
4773 else if (pid < 0)
4774 proc = pgfind(-pid);
4775 else
4776 proc = prfind(pid);
4777 if (proc == NULL) {
4778 mutex_exit(&pidlock);
4779 return (ESRCH);
4781 if (pid < 0)
4782 pidp = proc->p_pgidp;
4783 else
4784 pidp = proc->p_pidp;
4785 ASSERT(pidp);
4787 * Get a hold on the pid structure while referencing it.
4788 * There is a separate PID_HOLD should it be inserted
4789 * in the list below.
4791 PID_HOLD(pidp);
4792 mutex_exit(&pidlock);
4794 pssp = NULL;
4796 * Hold sd_lock to prevent traversal of sd_siglist while
4797 * it is modified.
4799 mutex_enter(&stp->sd_lock);
4800 for (ssp = stp->sd_siglist; ssp && (ssp->ss_pid != pid);
4801 pssp = ssp, ssp = ssp->ss_next)
4804 if (ss.ss_events) {
4805 if (ss.ss_events &
4806 ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4807 S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4808 mutex_exit(&stp->sd_lock);
4809 mutex_enter(&pidlock);
4810 PID_RELE(pidp);
4811 mutex_exit(&pidlock);
4812 return (EINVAL);
4814 if ((ss.ss_events & S_BANDURG) &&
4815 !(ss.ss_events & S_RDBAND)) {
4816 mutex_exit(&stp->sd_lock);
4817 mutex_enter(&pidlock);
4818 PID_RELE(pidp);
4819 mutex_exit(&pidlock);
4820 return (EINVAL);
4824 * If proc not already registered, add it
4825 * to list.
4827 if (!ssp) {
4828 ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4829 ssp->ss_pidp = pidp;
4830 ssp->ss_pid = pid;
4831 ssp->ss_next = NULL;
4832 if (pssp)
4833 pssp->ss_next = ssp;
4834 else
4835 stp->sd_siglist = ssp;
4836 mutex_enter(&pidlock);
4837 PID_HOLD(pidp);
4838 mutex_exit(&pidlock);
4842 * Set events.
4844 ssp->ss_events = ss.ss_events;
4845 } else {
4847 * Remove proc from register list.
4849 if (ssp) {
4850 mutex_enter(&pidlock);
4851 PID_RELE(pidp);
4852 mutex_exit(&pidlock);
4853 if (pssp)
4854 pssp->ss_next = ssp->ss_next;
4855 else
4856 stp->sd_siglist = ssp->ss_next;
4857 kmem_free(ssp, sizeof (strsig_t));
4858 } else {
4859 mutex_exit(&stp->sd_lock);
4860 mutex_enter(&pidlock);
4861 PID_RELE(pidp);
4862 mutex_exit(&pidlock);
4863 return (EINVAL);
4868 * Recalculate OR of sig events.
4870 stp->sd_sigflags = 0;
4871 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4872 stp->sd_sigflags |= ssp->ss_events;
4873 mutex_exit(&stp->sd_lock);
4874 mutex_enter(&pidlock);
4875 PID_RELE(pidp);
4876 mutex_exit(&pidlock);
4877 return (0);
4880 case I_EGETSIG:
4882 * Return (in arg) the current registration of events
4883 * for which the calling proc is to be signaled.
4886 struct strsig *ssp;
4887 struct proc *proc;
4888 pid_t pid;
4889 struct pid *pidp;
4890 struct strsigset ss;
4892 error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4893 if (error)
4894 return (error);
4896 pid = ss.ss_pid;
4897 mutex_enter(&pidlock);
4898 if (pid == 0)
4899 proc = curproc;
4900 else if (pid < 0)
4901 proc = pgfind(-pid);
4902 else
4903 proc = prfind(pid);
4904 if (proc == NULL) {
4905 mutex_exit(&pidlock);
4906 return (ESRCH);
4908 if (pid < 0)
4909 pidp = proc->p_pgidp;
4910 else
4911 pidp = proc->p_pidp;
4913 /* Prevent the pidp from being reassigned */
4914 PID_HOLD(pidp);
4915 mutex_exit(&pidlock);
4917 mutex_enter(&stp->sd_lock);
4918 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4919 if (ssp->ss_pid == pid) {
4920 ss.ss_pid = ssp->ss_pid;
4921 ss.ss_events = ssp->ss_events;
4922 error = strcopyout(&ss, (void *)arg,
4923 sizeof (struct strsigset), copyflag);
4924 mutex_exit(&stp->sd_lock);
4925 mutex_enter(&pidlock);
4926 PID_RELE(pidp);
4927 mutex_exit(&pidlock);
4928 return (error);
4930 mutex_exit(&stp->sd_lock);
4931 mutex_enter(&pidlock);
4932 PID_RELE(pidp);
4933 mutex_exit(&pidlock);
4934 return (EINVAL);
4937 case I_PEEK:
4939 STRUCT_DECL(strpeek, strpeek);
4940 size_t n;
4941 mblk_t *fmp, *tmp_mp = NULL;
4943 STRUCT_INIT(strpeek, flag);
4945 error = strcopyin((void *)arg, STRUCT_BUF(strpeek),
4946 STRUCT_SIZE(strpeek), copyflag);
4947 if (error)
4948 return (error);
4950 mutex_enter(QLOCK(rdq));
4952 * Skip the invalid messages
4954 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
4955 if (mp->b_datap->db_type != M_SIG)
4956 break;
4959 * If user has requested to peek at a high priority message
4960 * and first message is not, return 0
4962 if (mp != NULL) {
4963 if ((STRUCT_FGET(strpeek, flags) & RS_HIPRI) &&
4964 queclass(mp) == QNORM) {
4965 *rvalp = 0;
4966 mutex_exit(QLOCK(rdq));
4967 return (0);
4969 } else if (stp->sd_struiordq == NULL ||
4970 (STRUCT_FGET(strpeek, flags) & RS_HIPRI)) {
4972 * No mblks to look at at the streamhead and
4973 * 1). This isn't a synch stream or
4974 * 2). This is a synch stream but caller wants high
4975 * priority messages which is not supported by
4976 * the synch stream. (it only supports QNORM)
4978 *rvalp = 0;
4979 mutex_exit(QLOCK(rdq));
4980 return (0);
4983 fmp = mp;
4985 if (mp && mp->b_datap->db_type == M_PASSFP) {
4986 mutex_exit(QLOCK(rdq));
4987 return (EBADMSG);
4990 ASSERT(mp == NULL || mp->b_datap->db_type == M_PCPROTO ||
4991 mp->b_datap->db_type == M_PROTO ||
4992 mp->b_datap->db_type == M_DATA);
4994 if (mp && mp->b_datap->db_type == M_PCPROTO) {
4995 STRUCT_FSET(strpeek, flags, RS_HIPRI);
4996 } else {
4997 STRUCT_FSET(strpeek, flags, 0);
5001 if (mp && ((tmp_mp = dupmsg(mp)) == NULL)) {
5002 mutex_exit(QLOCK(rdq));
5003 return (ENOSR);
5005 mutex_exit(QLOCK(rdq));
5008 * set mp = tmp_mp, so that I_PEEK processing can continue.
5009 * tmp_mp is used to free the dup'd message.
5011 mp = tmp_mp;
5013 uio.uio_fmode = 0;
5014 uio.uio_extflg = UIO_COPY_CACHED;
5015 uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5016 UIO_SYSSPACE;
5017 uio.uio_limit = 0;
5019 * First process PROTO blocks, if any.
5020 * If user doesn't want to get ctl info by setting maxlen <= 0,
5021 * then set len to -1/0 and skip control blocks part.
5023 if (STRUCT_FGET(strpeek, ctlbuf.maxlen) < 0)
5024 STRUCT_FSET(strpeek, ctlbuf.len, -1);
5025 else if (STRUCT_FGET(strpeek, ctlbuf.maxlen) == 0)
5026 STRUCT_FSET(strpeek, ctlbuf.len, 0);
5027 else {
5028 int ctl_part = 0;
5030 iov.iov_base = STRUCT_FGETP(strpeek, ctlbuf.buf);
5031 iov.iov_len = STRUCT_FGET(strpeek, ctlbuf.maxlen);
5032 uio.uio_iov = &iov;
5033 uio.uio_resid = iov.iov_len;
5034 uio.uio_loffset = 0;
5035 uio.uio_iovcnt = 1;
5036 while (mp && mp->b_datap->db_type != M_DATA &&
5037 uio.uio_resid >= 0) {
5038 ASSERT(STRUCT_FGET(strpeek, flags) == 0 ?
5039 mp->b_datap->db_type == M_PROTO :
5040 mp->b_datap->db_type == M_PCPROTO);
5042 if ((n = MIN(uio.uio_resid,
5043 mp->b_wptr - mp->b_rptr)) != 0 &&
5044 (error = uiomove((char *)mp->b_rptr, n,
5045 UIO_READ, &uio)) != 0) {
5046 freemsg(tmp_mp);
5047 return (error);
5049 ctl_part = 1;
5050 mp = mp->b_cont;
5052 /* No ctl message */
5053 if (ctl_part == 0)
5054 STRUCT_FSET(strpeek, ctlbuf.len, -1);
5055 else
5056 STRUCT_FSET(strpeek, ctlbuf.len,
5057 STRUCT_FGET(strpeek, ctlbuf.maxlen) -
5058 uio.uio_resid);
5062 * Now process DATA blocks, if any.
5063 * If user doesn't want to get data info by setting maxlen <= 0,
5064 * then set len to -1/0 and skip data blocks part.
5066 if (STRUCT_FGET(strpeek, databuf.maxlen) < 0)
5067 STRUCT_FSET(strpeek, databuf.len, -1);
5068 else if (STRUCT_FGET(strpeek, databuf.maxlen) == 0)
5069 STRUCT_FSET(strpeek, databuf.len, 0);
5070 else {
5071 int data_part = 0;
5073 iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
5074 iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen);
5075 uio.uio_iov = &iov;
5076 uio.uio_resid = iov.iov_len;
5077 uio.uio_loffset = 0;
5078 uio.uio_iovcnt = 1;
5079 while (mp && uio.uio_resid) {
5080 if (mp->b_datap->db_type == M_DATA) {
5081 if ((n = MIN(uio.uio_resid,
5082 mp->b_wptr - mp->b_rptr)) != 0 &&
5083 (error = uiomove((char *)mp->b_rptr,
5084 n, UIO_READ, &uio)) != 0) {
5085 freemsg(tmp_mp);
5086 return (error);
5088 data_part = 1;
5090 ASSERT(data_part == 0 ||
5091 mp->b_datap->db_type == M_DATA);
5092 mp = mp->b_cont;
5094 /* No data message */
5095 if (data_part == 0)
5096 STRUCT_FSET(strpeek, databuf.len, -1);
5097 else
5098 STRUCT_FSET(strpeek, databuf.len,
5099 STRUCT_FGET(strpeek, databuf.maxlen) -
5100 uio.uio_resid);
5102 freemsg(tmp_mp);
5105 * It is a synch stream and user wants to get
5106 * data (maxlen > 0).
5107 * uio setup is done by the codes that process DATA
5108 * blocks above.
5110 if ((fmp == NULL) && STRUCT_FGET(strpeek, databuf.maxlen) > 0) {
5111 infod_t infod;
5113 infod.d_cmd = INFOD_COPYOUT;
5114 infod.d_res = 0;
5115 infod.d_uiop = &uio;
5116 error = infonext(rdq, &infod);
5117 if (error == EINVAL || error == EBUSY)
5118 error = 0;
5119 if (error)
5120 return (error);
5121 STRUCT_FSET(strpeek, databuf.len, STRUCT_FGET(strpeek,
5122 databuf.maxlen) - uio.uio_resid);
5123 if (STRUCT_FGET(strpeek, databuf.len) == 0) {
5125 * No data found by the infonext().
5127 STRUCT_FSET(strpeek, databuf.len, -1);
5130 error = strcopyout(STRUCT_BUF(strpeek), (void *)arg,
5131 STRUCT_SIZE(strpeek), copyflag);
5132 if (error) {
5133 return (error);
5136 * If there is no message retrieved, set return code to 0
5137 * otherwise, set it to 1.
5139 if (STRUCT_FGET(strpeek, ctlbuf.len) == -1 &&
5140 STRUCT_FGET(strpeek, databuf.len) == -1)
5141 *rvalp = 0;
5142 else
5143 *rvalp = 1;
5144 return (0);
5147 case I_FDINSERT:
5149 STRUCT_DECL(strfdinsert, strfdinsert);
5150 struct file *resftp;
5151 struct stdata *resstp;
5152 t_uscalar_t ival;
5153 ssize_t msgsize;
5154 struct strbuf mctl;
5156 STRUCT_INIT(strfdinsert, flag);
5157 if (stp->sd_flag & STRHUP)
5158 return (ENXIO);
5160 * STRDERR, STWRERR and STPLEX tested above.
5162 error = strcopyin((void *)arg, STRUCT_BUF(strfdinsert),
5163 STRUCT_SIZE(strfdinsert), copyflag);
5164 if (error)
5165 return (error);
5167 if (STRUCT_FGET(strfdinsert, offset) < 0 ||
5168 (STRUCT_FGET(strfdinsert, offset) %
5169 sizeof (t_uscalar_t)) != 0)
5170 return (EINVAL);
5171 if ((resftp = getf(STRUCT_FGET(strfdinsert, fildes))) != NULL) {
5172 if ((resstp = resftp->f_vnode->v_stream) == NULL) {
5173 releasef(STRUCT_FGET(strfdinsert, fildes));
5174 return (EINVAL);
5176 } else
5177 return (EINVAL);
5179 mutex_enter(&resstp->sd_lock);
5180 if (resstp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
5181 error = strgeterr(resstp,
5182 STRDERR|STWRERR|STRHUP|STPLEX, 0);
5183 if (error != 0) {
5184 mutex_exit(&resstp->sd_lock);
5185 releasef(STRUCT_FGET(strfdinsert, fildes));
5186 return (error);
5189 mutex_exit(&resstp->sd_lock);
5191 #ifdef _ILP32
5193 queue_t *q;
5194 queue_t *mate = NULL;
5196 /* get read queue of stream terminus */
5197 claimstr(resstp->sd_wrq);
5198 for (q = resstp->sd_wrq->q_next; q->q_next != NULL;
5199 q = q->q_next)
5200 if (!STRMATED(resstp) && STREAM(q) != resstp &&
5201 mate == NULL) {
5202 ASSERT(q->q_qinfo->qi_srvp);
5203 ASSERT(_OTHERQ(q)->q_qinfo->qi_srvp);
5204 claimstr(q);
5205 mate = q;
5207 q = _RD(q);
5208 if (mate)
5209 releasestr(mate);
5210 releasestr(resstp->sd_wrq);
5211 ival = (t_uscalar_t)q;
5213 #else
5214 ival = (t_uscalar_t)getminor(resftp->f_vnode->v_rdev);
5215 #endif /* _ILP32 */
5217 if (STRUCT_FGET(strfdinsert, ctlbuf.len) <
5218 STRUCT_FGET(strfdinsert, offset) + sizeof (t_uscalar_t)) {
5219 releasef(STRUCT_FGET(strfdinsert, fildes));
5220 return (EINVAL);
5224 * Check for legal flag value.
5226 if (STRUCT_FGET(strfdinsert, flags) & ~RS_HIPRI) {
5227 releasef(STRUCT_FGET(strfdinsert, fildes));
5228 return (EINVAL);
5231 /* get these values from those cached in the stream head */
5232 mutex_enter(QLOCK(stp->sd_wrq));
5233 rmin = stp->sd_qn_minpsz;
5234 rmax = stp->sd_qn_maxpsz;
5235 mutex_exit(QLOCK(stp->sd_wrq));
5238 * Make sure ctl and data sizes together fall within
5239 * the limits of the max and min receive packet sizes
5240 * and do not exceed system limit. A negative data
5241 * length means that no data part is to be sent.
5243 ASSERT((rmax >= 0) || (rmax == INFPSZ));
5244 if (rmax == 0) {
5245 releasef(STRUCT_FGET(strfdinsert, fildes));
5246 return (ERANGE);
5248 if ((msgsize = STRUCT_FGET(strfdinsert, databuf.len)) < 0)
5249 msgsize = 0;
5250 if ((msgsize < rmin) ||
5251 ((msgsize > rmax) && (rmax != INFPSZ)) ||
5252 (STRUCT_FGET(strfdinsert, ctlbuf.len) > strctlsz)) {
5253 releasef(STRUCT_FGET(strfdinsert, fildes));
5254 return (ERANGE);
5257 mutex_enter(&stp->sd_lock);
5258 while (!(STRUCT_FGET(strfdinsert, flags) & RS_HIPRI) &&
5259 !canputnext(stp->sd_wrq)) {
5260 if ((error = strwaitq(stp, WRITEWAIT, (ssize_t)0,
5261 flag, -1, &done)) != 0 || done) {
5262 mutex_exit(&stp->sd_lock);
5263 releasef(STRUCT_FGET(strfdinsert, fildes));
5264 return (error);
5266 if ((error = i_straccess(stp, access)) != 0) {
5267 mutex_exit(&stp->sd_lock);
5268 releasef(
5269 STRUCT_FGET(strfdinsert, fildes));
5270 return (error);
5273 mutex_exit(&stp->sd_lock);
5276 * Copy strfdinsert.ctlbuf into native form of
5277 * ctlbuf to pass down into strmakemsg().
5279 mctl.maxlen = STRUCT_FGET(strfdinsert, ctlbuf.maxlen);
5280 mctl.len = STRUCT_FGET(strfdinsert, ctlbuf.len);
5281 mctl.buf = STRUCT_FGETP(strfdinsert, ctlbuf.buf);
5283 iov.iov_base = STRUCT_FGETP(strfdinsert, databuf.buf);
5284 iov.iov_len = STRUCT_FGET(strfdinsert, databuf.len);
5285 uio.uio_iov = &iov;
5286 uio.uio_iovcnt = 1;
5287 uio.uio_loffset = 0;
5288 uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5289 UIO_SYSSPACE;
5290 uio.uio_fmode = 0;
5291 uio.uio_extflg = UIO_COPY_CACHED;
5292 uio.uio_resid = iov.iov_len;
5293 if ((error = strmakemsg(&mctl,
5294 &msgsize, &uio, stp,
5295 STRUCT_FGET(strfdinsert, flags), &mp)) != 0 || !mp) {
5296 STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5297 releasef(STRUCT_FGET(strfdinsert, fildes));
5298 return (error);
5301 STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5304 * Place the possibly reencoded queue pointer 'offset' bytes
5305 * from the start of the control portion of the message.
5307 *((t_uscalar_t *)(mp->b_rptr +
5308 STRUCT_FGET(strfdinsert, offset))) = ival;
5311 * Put message downstream.
5313 stream_willservice(stp);
5314 putnext(stp->sd_wrq, mp);
5315 stream_runservice(stp);
5316 releasef(STRUCT_FGET(strfdinsert, fildes));
5317 return (error);
5320 case I_SENDFD:
5322 struct file *fp;
5324 if ((fp = getf((int)arg)) == NULL)
5325 return (EBADF);
5326 error = do_sendfp(stp, fp, crp);
5327 if (auditing) {
5328 audit_fdsend((int)arg, fp, error);
5330 releasef((int)arg);
5331 return (error);
5334 case I_RECVFD:
5335 case I_E_RECVFD:
5337 struct k_strrecvfd *srf;
5338 int i, fd;
5340 mutex_enter(&stp->sd_lock);
5341 while (!(mp = getq(rdq))) {
5342 if (stp->sd_flag & (STRHUP|STREOF)) {
5343 mutex_exit(&stp->sd_lock);
5344 return (ENXIO);
5346 if ((error = strwaitq(stp, GETWAIT, (ssize_t)0,
5347 flag, -1, &done)) != 0 || done) {
5348 mutex_exit(&stp->sd_lock);
5349 return (error);
5351 if ((error = i_straccess(stp, access)) != 0) {
5352 mutex_exit(&stp->sd_lock);
5353 return (error);
5356 if (mp->b_datap->db_type != M_PASSFP) {
5357 putback(stp, rdq, mp, mp->b_band);
5358 mutex_exit(&stp->sd_lock);
5359 return (EBADMSG);
5361 mutex_exit(&stp->sd_lock);
5363 srf = (struct k_strrecvfd *)mp->b_rptr;
5364 if ((fd = ufalloc(0)) == -1) {
5365 mutex_enter(&stp->sd_lock);
5366 putback(stp, rdq, mp, mp->b_band);
5367 mutex_exit(&stp->sd_lock);
5368 return (EMFILE);
5370 if (cmd == I_RECVFD) {
5371 struct o_strrecvfd ostrfd;
5373 /* check to see if uid/gid values are too large. */
5375 if (srf->uid > (o_uid_t)USHRT_MAX ||
5376 srf->gid > (o_gid_t)USHRT_MAX) {
5377 mutex_enter(&stp->sd_lock);
5378 putback(stp, rdq, mp, mp->b_band);
5379 mutex_exit(&stp->sd_lock);
5380 setf(fd, NULL); /* release fd entry */
5381 return (EOVERFLOW);
5384 ostrfd.fd = fd;
5385 ostrfd.uid = (o_uid_t)srf->uid;
5386 ostrfd.gid = (o_gid_t)srf->gid;
5388 /* Null the filler bits */
5389 for (i = 0; i < 8; i++)
5390 ostrfd.fill[i] = 0;
5392 error = strcopyout(&ostrfd, (void *)arg,
5393 sizeof (struct o_strrecvfd), copyflag);
5394 } else { /* I_E_RECVFD */
5395 struct strrecvfd strfd;
5397 strfd.fd = fd;
5398 strfd.uid = srf->uid;
5399 strfd.gid = srf->gid;
5401 /* null the filler bits */
5402 for (i = 0; i < 8; i++)
5403 strfd.fill[i] = 0;
5405 error = strcopyout(&strfd, (void *)arg,
5406 sizeof (struct strrecvfd), copyflag);
5409 if (error) {
5410 setf(fd, NULL); /* release fd entry */
5411 mutex_enter(&stp->sd_lock);
5412 putback(stp, rdq, mp, mp->b_band);
5413 mutex_exit(&stp->sd_lock);
5414 return (error);
5416 if (auditing) {
5417 audit_fdrecv(fd, srf->fp);
5421 * Always increment f_count since the freemsg() below will
5422 * always call free_passfp() which performs a closef().
5424 mutex_enter(&srf->fp->f_tlock);
5425 srf->fp->f_count++;
5426 mutex_exit(&srf->fp->f_tlock);
5427 setf(fd, srf->fp);
5428 freemsg(mp);
5429 return (0);
5432 case I_SWROPT:
5434 * Set/clear the write options. arg is a bit
5435 * mask with any of the following bits set...
5436 * SNDZERO - send zero length message
5437 * SNDPIPE - send sigpipe to process if
5438 * sd_werror is set and process is
5439 * doing a write or putmsg.
5440 * The new stream head write options should reflect
5441 * what is in arg.
5443 if (arg & ~(SNDZERO|SNDPIPE))
5444 return (EINVAL);
5446 mutex_enter(&stp->sd_lock);
5447 stp->sd_wput_opt &= ~(SW_SIGPIPE|SW_SNDZERO);
5448 if (arg & SNDZERO)
5449 stp->sd_wput_opt |= SW_SNDZERO;
5450 if (arg & SNDPIPE)
5451 stp->sd_wput_opt |= SW_SIGPIPE;
5452 mutex_exit(&stp->sd_lock);
5453 return (0);
5455 case I_GWROPT:
5457 int wropt = 0;
5459 if (stp->sd_wput_opt & SW_SNDZERO)
5460 wropt |= SNDZERO;
5461 if (stp->sd_wput_opt & SW_SIGPIPE)
5462 wropt |= SNDPIPE;
5463 return (strcopyout(&wropt, (void *)arg, sizeof (wropt),
5464 copyflag));
5467 case I_LIST:
5469 * Returns all the modules found on this stream,
5470 * upto the driver. If argument is NULL, return the
5471 * number of modules (including driver). If argument
5472 * is not NULL, copy the names into the structure
5473 * provided.
5477 queue_t *q;
5478 char *qname;
5479 int i, nmods;
5480 struct str_mlist *mlist;
5481 STRUCT_DECL(str_list, strlist);
5483 if (arg == (intptr_t)NULL) {
5484 /* Return number of modules plus driver */
5485 if (stp->sd_vnode->v_type == VFIFO)
5486 *rvalp = stp->sd_pushcnt;
5487 else
5488 *rvalp = stp->sd_pushcnt + 1;
5489 return (0);
5492 STRUCT_INIT(strlist, flag);
5494 error = strcopyin((void *)arg, STRUCT_BUF(strlist),
5495 STRUCT_SIZE(strlist), copyflag);
5496 if (error != 0)
5497 return (error);
5499 mlist = STRUCT_FGETP(strlist, sl_modlist);
5500 nmods = STRUCT_FGET(strlist, sl_nmods);
5501 if (nmods <= 0)
5502 return (EINVAL);
5504 claimstr(stp->sd_wrq);
5505 q = stp->sd_wrq;
5506 for (i = 0; i < nmods && _SAMESTR(q); i++, q = q->q_next) {
5507 qname = Q2NAME(q->q_next);
5508 error = strcopyout(qname, &mlist[i], strlen(qname) + 1,
5509 copyflag);
5510 if (error != 0) {
5511 releasestr(stp->sd_wrq);
5512 return (error);
5515 releasestr(stp->sd_wrq);
5516 return (strcopyout(&i, (void *)arg, sizeof (int), copyflag));
5519 case I_CKBAND:
5521 queue_t *q;
5522 qband_t *qbp;
5524 if ((arg < 0) || (arg >= NBAND))
5525 return (EINVAL);
5526 q = _RD(stp->sd_wrq);
5527 mutex_enter(QLOCK(q));
5528 if (arg > (int)q->q_nband) {
5529 *rvalp = 0;
5530 } else {
5531 if (arg == 0) {
5532 if (q->q_first)
5533 *rvalp = 1;
5534 else
5535 *rvalp = 0;
5536 } else {
5537 qbp = q->q_bandp;
5538 while (--arg > 0)
5539 qbp = qbp->qb_next;
5540 if (qbp->qb_first)
5541 *rvalp = 1;
5542 else
5543 *rvalp = 0;
5546 mutex_exit(QLOCK(q));
5547 return (0);
5550 case I_GETBAND:
5552 int intpri;
5553 queue_t *q;
5555 q = _RD(stp->sd_wrq);
5556 mutex_enter(QLOCK(q));
5557 mp = q->q_first;
5558 if (!mp) {
5559 mutex_exit(QLOCK(q));
5560 return (ENODATA);
5562 intpri = (int)mp->b_band;
5563 error = strcopyout(&intpri, (void *)arg, sizeof (int),
5564 copyflag);
5565 mutex_exit(QLOCK(q));
5566 return (error);
5569 case I_ATMARK:
5571 queue_t *q;
5573 if (arg & ~(ANYMARK|LASTMARK))
5574 return (EINVAL);
5575 q = _RD(stp->sd_wrq);
5576 mutex_enter(&stp->sd_lock);
5577 if ((stp->sd_flag & STRATMARK) && (arg == ANYMARK)) {
5578 *rvalp = 1;
5579 } else {
5580 mutex_enter(QLOCK(q));
5581 mp = q->q_first;
5583 if (mp == NULL)
5584 *rvalp = 0;
5585 else if ((arg == ANYMARK) && (mp->b_flag & MSGMARK))
5586 *rvalp = 1;
5587 else if ((arg == LASTMARK) && (mp == stp->sd_mark))
5588 *rvalp = 1;
5589 else
5590 *rvalp = 0;
5591 mutex_exit(QLOCK(q));
5593 mutex_exit(&stp->sd_lock);
5594 return (0);
5597 case I_CANPUT:
5599 char band;
5601 if ((arg < 0) || (arg >= NBAND))
5602 return (EINVAL);
5603 band = (char)arg;
5604 *rvalp = bcanputnext(stp->sd_wrq, band);
5605 return (0);
5608 case I_SETCLTIME:
5610 int closetime;
5612 error = strcopyin((void *)arg, &closetime, sizeof (int),
5613 copyflag);
5614 if (error)
5615 return (error);
5616 if (closetime < 0)
5617 return (EINVAL);
5619 stp->sd_closetime = closetime;
5620 return (0);
5623 case I_GETCLTIME:
5625 int closetime;
5627 closetime = stp->sd_closetime;
5628 return (strcopyout(&closetime, (void *)arg, sizeof (int),
5629 copyflag));
5632 case TIOCGSID:
5634 pid_t sid;
5636 mutex_enter(&stp->sd_lock);
5637 if (stp->sd_sidp == NULL) {
5638 mutex_exit(&stp->sd_lock);
5639 return (ENOTTY);
5641 sid = stp->sd_sidp->pid_id;
5642 mutex_exit(&stp->sd_lock);
5643 return (strcopyout(&sid, (void *)arg, sizeof (pid_t),
5644 copyflag));
5647 case TIOCSPGRP:
5649 pid_t pgrp;
5650 proc_t *q;
5651 pid_t sid, fg_pgid, bg_pgid;
5653 if (error = strcopyin((void *)arg, &pgrp, sizeof (pid_t),
5654 copyflag))
5655 return (error);
5656 mutex_enter(&stp->sd_lock);
5657 mutex_enter(&pidlock);
5658 if (stp->sd_sidp != ttoproc(curthread)->p_sessp->s_sidp) {
5659 mutex_exit(&pidlock);
5660 mutex_exit(&stp->sd_lock);
5661 return (ENOTTY);
5663 if (pgrp == stp->sd_pgidp->pid_id) {
5664 mutex_exit(&pidlock);
5665 mutex_exit(&stp->sd_lock);
5666 return (0);
5668 if (pgrp <= 0 || pgrp >= maxpid) {
5669 mutex_exit(&pidlock);
5670 mutex_exit(&stp->sd_lock);
5671 return (EINVAL);
5673 if ((q = pgfind(pgrp)) == NULL ||
5674 q->p_sessp != ttoproc(curthread)->p_sessp) {
5675 mutex_exit(&pidlock);
5676 mutex_exit(&stp->sd_lock);
5677 return (EPERM);
5679 sid = stp->sd_sidp->pid_id;
5680 fg_pgid = q->p_pgrp;
5681 bg_pgid = stp->sd_pgidp->pid_id;
5682 CL_SET_PROCESS_GROUP(curthread, sid, bg_pgid, fg_pgid);
5683 PID_RELE(stp->sd_pgidp);
5684 ctty_clear_sighuped();
5685 stp->sd_pgidp = q->p_pgidp;
5686 PID_HOLD(stp->sd_pgidp);
5687 mutex_exit(&pidlock);
5688 mutex_exit(&stp->sd_lock);
5689 return (0);
5692 case TIOCGPGRP:
5694 pid_t pgrp;
5696 mutex_enter(&stp->sd_lock);
5697 if (stp->sd_sidp == NULL) {
5698 mutex_exit(&stp->sd_lock);
5699 return (ENOTTY);
5701 pgrp = stp->sd_pgidp->pid_id;
5702 mutex_exit(&stp->sd_lock);
5703 return (strcopyout(&pgrp, (void *)arg, sizeof (pid_t),
5704 copyflag));
5707 case TIOCSCTTY:
5709 return (strctty(stp));
5712 case TIOCNOTTY:
5714 /* freectty() always assumes curproc. */
5715 if (freectty(B_FALSE) != 0)
5716 return (0);
5717 return (ENOTTY);
5720 case FIONBIO:
5721 case FIOASYNC:
5722 return (0); /* handled by the upper layer */
5727 * Custom free routine used for M_PASSFP messages.
5729 static void
5730 free_passfp(struct k_strrecvfd *srf)
5732 (void) closef(srf->fp);
5733 kmem_free(srf, sizeof (struct k_strrecvfd) + sizeof (frtn_t));
5736 /* ARGSUSED */
5738 do_sendfp(struct stdata *stp, struct file *fp, struct cred *cr)
5740 queue_t *qp, *nextqp;
5741 struct k_strrecvfd *srf;
5742 mblk_t *mp;
5743 frtn_t *frtnp;
5744 size_t bufsize;
5745 queue_t *mate = NULL;
5746 syncq_t *sq = NULL;
5747 int retval = 0;
5749 if (stp->sd_flag & STRHUP)
5750 return (ENXIO);
5752 claimstr(stp->sd_wrq);
5754 /* Fastpath, we have a pipe, and we are already mated, use it. */
5755 if (STRMATED(stp)) {
5756 qp = _RD(stp->sd_mate->sd_wrq);
5757 claimstr(qp);
5758 mate = qp;
5759 } else { /* Not already mated. */
5762 * Walk the stream to the end of this one.
5763 * assumes that the claimstr() will prevent
5764 * plumbing between the stream head and the
5765 * driver from changing
5767 qp = stp->sd_wrq;
5770 * Loop until we reach the end of this stream.
5771 * On completion, qp points to the write queue
5772 * at the end of the stream, or the read queue
5773 * at the stream head if this is a fifo.
5775 while (((qp = qp->q_next) != NULL) && _SAMESTR(qp))
5779 * Just in case we get a q_next which is NULL, but
5780 * not at the end of the stream. This is actually
5781 * broken, so we set an assert to catch it in
5782 * debug, and set an error and return if not debug.
5784 ASSERT(qp);
5785 if (qp == NULL) {
5786 releasestr(stp->sd_wrq);
5787 return (EINVAL);
5791 * Enter the syncq for the driver, so (hopefully)
5792 * the queue values will not change on us.
5793 * XXXX - This will only prevent the race IFF only
5794 * the write side modifies the q_next member, and
5795 * the put procedure is protected by at least
5796 * MT_PERQ.
5798 if ((sq = qp->q_syncq) != NULL)
5799 entersq(sq, SQ_PUT);
5801 /* Now get the q_next value from this qp. */
5802 nextqp = qp->q_next;
5805 * If nextqp exists and the other stream is different
5806 * from this one claim the stream, set the mate, and
5807 * get the read queue at the stream head of the other
5808 * stream. Assumes that nextqp was at least valid when
5809 * we got it. Hopefully the entersq of the driver
5810 * will prevent it from changing on us.
5812 if ((nextqp != NULL) && (STREAM(nextqp) != stp)) {
5813 ASSERT(qp->q_qinfo->qi_srvp);
5814 ASSERT(_OTHERQ(qp)->q_qinfo->qi_srvp);
5815 ASSERT(_OTHERQ(qp->q_next)->q_qinfo->qi_srvp);
5816 claimstr(nextqp);
5818 /* Make sure we still have a q_next */
5819 if (nextqp != qp->q_next) {
5820 releasestr(stp->sd_wrq);
5821 releasestr(nextqp);
5822 return (EINVAL);
5825 qp = _RD(STREAM(nextqp)->sd_wrq);
5826 mate = qp;
5828 /* If we entered the synq above, leave it. */
5829 if (sq != NULL)
5830 leavesq(sq, SQ_PUT);
5831 } /* STRMATED(STP) */
5833 /* XXX prevents substitution of the ops vector */
5834 if (qp->q_qinfo != &strdata && qp->q_qinfo != &fifo_strdata) {
5835 retval = EINVAL;
5836 goto out;
5839 if (qp->q_flag & QFULL) {
5840 retval = EAGAIN;
5841 goto out;
5845 * Since M_PASSFP messages include a file descriptor, we use
5846 * esballoc() and specify a custom free routine (free_passfp()) that
5847 * will close the descriptor as part of freeing the message. For
5848 * convenience, we stash the frtn_t right after the data block.
5850 bufsize = sizeof (struct k_strrecvfd) + sizeof (frtn_t);
5851 srf = kmem_alloc(bufsize, KM_NOSLEEP);
5852 if (srf == NULL) {
5853 retval = EAGAIN;
5854 goto out;
5857 frtnp = (frtn_t *)(srf + 1);
5858 frtnp->free_arg = (caddr_t)srf;
5859 frtnp->free_func = free_passfp;
5861 mp = esballoc((uchar_t *)srf, bufsize, BPRI_MED, frtnp);
5862 if (mp == NULL) {
5863 kmem_free(srf, bufsize);
5864 retval = EAGAIN;
5865 goto out;
5867 mp->b_wptr += sizeof (struct k_strrecvfd);
5868 mp->b_datap->db_type = M_PASSFP;
5870 srf->fp = fp;
5871 srf->uid = crgetuid(curthread->t_cred);
5872 srf->gid = crgetgid(curthread->t_cred);
5873 mutex_enter(&fp->f_tlock);
5874 fp->f_count++;
5875 mutex_exit(&fp->f_tlock);
5877 put(qp, mp);
5878 out:
5879 releasestr(stp->sd_wrq);
5880 if (mate)
5881 releasestr(mate);
5882 return (retval);
5886 * Send an ioctl message downstream and wait for acknowledgement.
5887 * flags may be set to either U_TO_K or K_TO_K and a combination
5888 * of STR_NOERROR or STR_NOSIG
5889 * STR_NOSIG: Signals are essentially ignored or held and have
5890 * no effect for the duration of the call.
5891 * STR_NOERROR: Ignores stream head read, write and hup errors.
5892 * Additionally, if an existing ioctl times out, it is assumed
5893 * lost and and this ioctl will continue as if the previous ioctl had
5894 * finished. ETIME may be returned if this ioctl times out (i.e.
5895 * ic_timout is not INFTIM). Non-stream head errors may be returned if
5896 * the ioc_error indicates that the driver/module had problems,
5897 * an EFAULT was found when accessing user data, a lack of
5898 * resources, etc.
5901 strdoioctl(
5902 struct stdata *stp,
5903 struct strioctl *strioc,
5904 int fflags, /* file flags with model info */
5905 int flag,
5906 cred_t *crp,
5907 int *rvalp)
5909 mblk_t *bp;
5910 struct iocblk *iocbp;
5911 struct copyreq *reqp;
5912 struct copyresp *resp;
5913 int id;
5914 int transparent = 0;
5915 int error = 0;
5916 int len = 0;
5917 caddr_t taddr;
5918 int copyflag = (flag & (U_TO_K | K_TO_K));
5919 int sigflag = (flag & STR_NOSIG);
5920 int errs;
5921 uint_t waitflags;
5922 boolean_t set_iocwaitne = B_FALSE;
5924 ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
5925 ASSERT((fflags & FMODELS) != 0);
5927 TRACE_2(TR_FAC_STREAMS_FR,
5928 TR_STRDOIOCTL,
5929 "strdoioctl:stp %p strioc %p", stp, strioc);
5930 if (strioc->ic_len == TRANSPARENT) { /* send arg in M_DATA block */
5931 transparent = 1;
5932 strioc->ic_len = sizeof (intptr_t);
5935 if (strioc->ic_len < 0 || (strmsgsz > 0 && strioc->ic_len > strmsgsz))
5936 return (EINVAL);
5938 if ((bp = allocb_cred_wait(sizeof (union ioctypes), sigflag, &error,
5939 crp, curproc->p_pid)) == NULL)
5940 return (error);
5942 bzero(bp->b_wptr, sizeof (union ioctypes));
5944 iocbp = (struct iocblk *)bp->b_wptr;
5945 iocbp->ioc_count = strioc->ic_len;
5946 iocbp->ioc_cmd = strioc->ic_cmd;
5947 iocbp->ioc_flag = (fflags & FMODELS);
5949 crhold(crp);
5950 iocbp->ioc_cr = crp;
5951 DB_TYPE(bp) = M_IOCTL;
5952 bp->b_wptr += sizeof (struct iocblk);
5954 if (flag & STR_NOERROR)
5955 errs = STPLEX;
5956 else
5957 errs = STRHUP|STRDERR|STWRERR|STPLEX;
5960 * If there is data to copy into ioctl block, do so.
5962 if (iocbp->ioc_count > 0) {
5963 if (transparent)
5965 * Note: STR_NOERROR does not have an effect
5966 * in putiocd()
5968 id = K_TO_K | sigflag;
5969 else
5970 id = flag;
5971 if ((error = putiocd(bp, strioc->ic_dp, id, crp)) != 0) {
5972 freemsg(bp);
5973 crfree(crp);
5974 return (error);
5978 * We could have slept copying in user pages.
5979 * Recheck the stream head state (the other end
5980 * of a pipe could have gone away).
5982 if (stp->sd_flag & errs) {
5983 mutex_enter(&stp->sd_lock);
5984 error = strgeterr(stp, errs, 0);
5985 mutex_exit(&stp->sd_lock);
5986 if (error != 0) {
5987 freemsg(bp);
5988 crfree(crp);
5989 return (error);
5993 if (transparent)
5994 iocbp->ioc_count = TRANSPARENT;
5997 * Block for up to STRTIMOUT milliseconds if there is an outstanding
5998 * ioctl for this stream already running. All processes
5999 * sleeping here will be awakened as a result of an ACK
6000 * or NAK being received for the outstanding ioctl, or
6001 * as a result of the timer expiring on the outstanding
6002 * ioctl (a failure), or as a result of any waiting
6003 * process's timer expiring (also a failure).
6006 error = 0;
6007 mutex_enter(&stp->sd_lock);
6008 while ((stp->sd_flag & IOCWAIT) ||
6009 (!set_iocwaitne && (stp->sd_flag & IOCWAITNE))) {
6010 clock_t cv_rval;
6012 TRACE_0(TR_FAC_STREAMS_FR,
6013 TR_STRDOIOCTL_WAIT,
6014 "strdoioctl sleeps - IOCWAIT");
6015 cv_rval = str_cv_wait(&stp->sd_iocmonitor, &stp->sd_lock,
6016 STRTIMOUT, sigflag);
6017 if (cv_rval <= 0) {
6018 if (cv_rval == 0) {
6019 error = EINTR;
6020 } else {
6021 if (flag & STR_NOERROR) {
6023 * Terminating current ioctl in
6024 * progress -- assume it got lost and
6025 * wake up the other thread so that the
6026 * operation completes.
6028 if (!(stp->sd_flag & IOCWAITNE)) {
6029 set_iocwaitne = B_TRUE;
6030 stp->sd_flag |= IOCWAITNE;
6031 cv_broadcast(&stp->sd_monitor);
6034 * Otherwise, there's a running
6035 * STR_NOERROR -- we have no choice
6036 * here but to wait forever (or until
6037 * interrupted).
6039 } else {
6041 * pending ioctl has caused
6042 * us to time out
6044 error = ETIME;
6047 } else if ((stp->sd_flag & errs)) {
6048 error = strgeterr(stp, errs, 0);
6050 if (error) {
6051 mutex_exit(&stp->sd_lock);
6052 freemsg(bp);
6053 crfree(crp);
6054 return (error);
6059 * Have control of ioctl mechanism.
6060 * Send down ioctl packet and wait for response.
6062 if (stp->sd_iocblk != (mblk_t *)-1) {
6063 freemsg(stp->sd_iocblk);
6065 stp->sd_iocblk = NULL;
6068 * If this is marked with 'noerror' (internal; mostly
6069 * I_{P,}{UN,}LINK), then make sure nobody else is able to get
6070 * in here by setting IOCWAITNE.
6072 waitflags = IOCWAIT;
6073 if (flag & STR_NOERROR)
6074 waitflags |= IOCWAITNE;
6076 stp->sd_flag |= waitflags;
6079 * Assign sequence number.
6081 iocbp->ioc_id = stp->sd_iocid = getiocseqno();
6083 mutex_exit(&stp->sd_lock);
6085 TRACE_1(TR_FAC_STREAMS_FR,
6086 TR_STRDOIOCTL_PUT, "strdoioctl put: stp %p", stp);
6087 stream_willservice(stp);
6088 putnext(stp->sd_wrq, bp);
6089 stream_runservice(stp);
6092 * Timed wait for acknowledgment. The wait time is limited by the
6093 * timeout value, which must be a positive integer (number of
6094 * milliseconds) to wait, or 0 (use default value of STRTIMOUT
6095 * milliseconds), or -1 (wait forever). This will be awakened
6096 * either by an ACK/NAK message arriving, the timer expiring, or
6097 * the timer expiring on another ioctl waiting for control of the
6098 * mechanism.
6100 waitioc:
6101 mutex_enter(&stp->sd_lock);
6105 * If the reply has already arrived, don't sleep. If awakened from
6106 * the sleep, fail only if the reply has not arrived by then.
6107 * Otherwise, process the reply.
6109 while (!stp->sd_iocblk) {
6110 clock_t cv_rval;
6112 if (stp->sd_flag & errs) {
6113 error = strgeterr(stp, errs, 0);
6114 if (error != 0) {
6115 stp->sd_flag &= ~waitflags;
6116 cv_broadcast(&stp->sd_iocmonitor);
6117 mutex_exit(&stp->sd_lock);
6118 crfree(crp);
6119 return (error);
6123 TRACE_0(TR_FAC_STREAMS_FR,
6124 TR_STRDOIOCTL_WAIT2,
6125 "strdoioctl sleeps awaiting reply");
6126 ASSERT(error == 0);
6128 cv_rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock,
6129 (strioc->ic_timout ?
6130 strioc->ic_timout * 1000 : STRTIMOUT), sigflag);
6133 * There are four possible cases here: interrupt, timeout,
6134 * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
6135 * valid M_IOCTL reply).
6137 * If we've been awakened by a STR_NOERROR ioctl on some other
6138 * thread, then sd_iocblk will still be NULL, and IOCWAITNE
6139 * will be set. Pretend as if we just timed out. Note that
6140 * this other thread waited at least STRTIMOUT before trying to
6141 * awaken our thread, so this is indistinguishable (even for
6142 * INFTIM) from the case where we failed with ETIME waiting on
6143 * IOCWAIT in the prior loop.
6145 if (cv_rval > 0 && !(flag & STR_NOERROR) &&
6146 stp->sd_iocblk == NULL && (stp->sd_flag & IOCWAITNE)) {
6147 cv_rval = -1;
6151 * note: STR_NOERROR does not protect
6152 * us here.. use ic_timout < 0
6154 if (cv_rval <= 0) {
6155 if (cv_rval == 0) {
6156 error = EINTR;
6157 } else {
6158 error = ETIME;
6161 * A message could have come in after we were scheduled
6162 * but before we were actually run.
6164 bp = stp->sd_iocblk;
6165 stp->sd_iocblk = NULL;
6166 if (bp != NULL) {
6167 if ((bp->b_datap->db_type == M_COPYIN) ||
6168 (bp->b_datap->db_type == M_COPYOUT)) {
6169 mutex_exit(&stp->sd_lock);
6170 if (bp->b_cont) {
6171 freemsg(bp->b_cont);
6172 bp->b_cont = NULL;
6174 bp->b_datap->db_type = M_IOCDATA;
6175 bp->b_wptr = bp->b_rptr +
6176 sizeof (struct copyresp);
6177 resp = (struct copyresp *)bp->b_rptr;
6178 resp->cp_rval =
6179 (caddr_t)1; /* failure */
6180 stream_willservice(stp);
6181 putnext(stp->sd_wrq, bp);
6182 stream_runservice(stp);
6183 mutex_enter(&stp->sd_lock);
6184 } else {
6185 freemsg(bp);
6188 stp->sd_flag &= ~waitflags;
6189 cv_broadcast(&stp->sd_iocmonitor);
6190 mutex_exit(&stp->sd_lock);
6191 crfree(crp);
6192 return (error);
6195 bp = stp->sd_iocblk;
6197 * Note: it is strictly impossible to get here with sd_iocblk set to
6198 * -1. This is because the initial loop above doesn't allow any new
6199 * ioctls into the fray until all others have passed this point.
6201 ASSERT(bp != NULL && bp != (mblk_t *)-1);
6202 TRACE_1(TR_FAC_STREAMS_FR,
6203 TR_STRDOIOCTL_ACK, "strdoioctl got reply: bp %p", bp);
6204 if ((bp->b_datap->db_type == M_IOCACK) ||
6205 (bp->b_datap->db_type == M_IOCNAK)) {
6206 /* for detection of duplicate ioctl replies */
6207 stp->sd_iocblk = (mblk_t *)-1;
6208 stp->sd_flag &= ~waitflags;
6209 cv_broadcast(&stp->sd_iocmonitor);
6210 mutex_exit(&stp->sd_lock);
6211 } else {
6213 * flags not cleared here because we're still doing
6214 * copy in/out for ioctl.
6216 stp->sd_iocblk = NULL;
6217 mutex_exit(&stp->sd_lock);
6222 * Have received acknowledgment.
6225 switch (bp->b_datap->db_type) {
6226 case M_IOCACK:
6228 * Positive ack.
6230 iocbp = (struct iocblk *)bp->b_rptr;
6233 * Set error if indicated.
6235 if (iocbp->ioc_error) {
6236 error = iocbp->ioc_error;
6237 break;
6241 * Set return value.
6243 *rvalp = iocbp->ioc_rval;
6246 * Data may have been returned in ACK message (ioc_count > 0).
6247 * If so, copy it out to the user's buffer.
6249 if (iocbp->ioc_count && !transparent) {
6250 if (error = getiocd(bp, strioc->ic_dp, copyflag))
6251 break;
6253 if (!transparent) {
6254 if (len) /* an M_COPYOUT was used with I_STR */
6255 strioc->ic_len = len;
6256 else
6257 strioc->ic_len = (int)iocbp->ioc_count;
6259 break;
6261 case M_IOCNAK:
6263 * Negative ack.
6265 * The only thing to do is set error as specified
6266 * in neg ack packet.
6268 iocbp = (struct iocblk *)bp->b_rptr;
6270 error = (iocbp->ioc_error ? iocbp->ioc_error : EINVAL);
6271 break;
6273 case M_COPYIN:
6275 * Driver or module has requested user ioctl data.
6277 reqp = (struct copyreq *)bp->b_rptr;
6280 * M_COPYIN should *never* have a message attached, though
6281 * it's harmless if it does -- thus, panic on a DEBUG
6282 * kernel and just free it on a non-DEBUG build.
6284 ASSERT(bp->b_cont == NULL);
6285 if (bp->b_cont != NULL) {
6286 freemsg(bp->b_cont);
6287 bp->b_cont = NULL;
6290 error = putiocd(bp, reqp->cq_addr, flag, crp);
6291 if (error && bp->b_cont) {
6292 freemsg(bp->b_cont);
6293 bp->b_cont = NULL;
6296 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6297 bp->b_datap->db_type = M_IOCDATA;
6299 mblk_setcred(bp, crp, curproc->p_pid);
6300 resp = (struct copyresp *)bp->b_rptr;
6301 resp->cp_rval = (caddr_t)(uintptr_t)error;
6302 resp->cp_flag = (fflags & FMODELS);
6304 stream_willservice(stp);
6305 putnext(stp->sd_wrq, bp);
6306 stream_runservice(stp);
6308 if (error) {
6309 mutex_enter(&stp->sd_lock);
6310 stp->sd_flag &= ~waitflags;
6311 cv_broadcast(&stp->sd_iocmonitor);
6312 mutex_exit(&stp->sd_lock);
6313 crfree(crp);
6314 return (error);
6317 goto waitioc;
6319 case M_COPYOUT:
6321 * Driver or module has ioctl data for a user.
6323 reqp = (struct copyreq *)bp->b_rptr;
6324 ASSERT(bp->b_cont != NULL);
6327 * Always (transparent or non-transparent )
6328 * use the address specified in the request
6330 taddr = reqp->cq_addr;
6331 if (!transparent)
6332 len = (int)reqp->cq_size;
6334 /* copyout data to the provided address */
6335 error = getiocd(bp, taddr, copyflag);
6337 freemsg(bp->b_cont);
6338 bp->b_cont = NULL;
6340 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6341 bp->b_datap->db_type = M_IOCDATA;
6343 mblk_setcred(bp, crp, curproc->p_pid);
6344 resp = (struct copyresp *)bp->b_rptr;
6345 resp->cp_rval = (caddr_t)(uintptr_t)error;
6346 resp->cp_flag = (fflags & FMODELS);
6348 stream_willservice(stp);
6349 putnext(stp->sd_wrq, bp);
6350 stream_runservice(stp);
6352 if (error) {
6353 mutex_enter(&stp->sd_lock);
6354 stp->sd_flag &= ~waitflags;
6355 cv_broadcast(&stp->sd_iocmonitor);
6356 mutex_exit(&stp->sd_lock);
6357 crfree(crp);
6358 return (error);
6360 goto waitioc;
6362 default:
6363 ASSERT(0);
6364 mutex_enter(&stp->sd_lock);
6365 stp->sd_flag &= ~waitflags;
6366 cv_broadcast(&stp->sd_iocmonitor);
6367 mutex_exit(&stp->sd_lock);
6368 break;
6371 freemsg(bp);
6372 crfree(crp);
6373 return (error);
6377 * Send an M_CMD message downstream and wait for a reply. This is a ptools
6378 * special used to retrieve information from modules/drivers a stream without
6379 * being subjected to flow control or interfering with pending messages on the
6380 * stream (e.g. an ioctl in flight).
6383 strdocmd(struct stdata *stp, struct strcmd *scp, cred_t *crp)
6385 mblk_t *mp;
6386 struct cmdblk *cmdp;
6387 int error = 0;
6388 int errs = STRHUP|STRDERR|STWRERR|STPLEX;
6389 clock_t rval, timeout = STRTIMOUT;
6391 if (scp->sc_len < 0 || scp->sc_len > sizeof (scp->sc_buf) ||
6392 scp->sc_timeout < -1)
6393 return (EINVAL);
6395 if (scp->sc_timeout > 0)
6396 timeout = scp->sc_timeout * MILLISEC;
6398 if ((mp = allocb_cred(sizeof (struct cmdblk), crp,
6399 curproc->p_pid)) == NULL)
6400 return (ENOMEM);
6402 crhold(crp);
6404 cmdp = (struct cmdblk *)mp->b_wptr;
6405 cmdp->cb_cr = crp;
6406 cmdp->cb_cmd = scp->sc_cmd;
6407 cmdp->cb_len = scp->sc_len;
6408 cmdp->cb_error = 0;
6409 mp->b_wptr += sizeof (struct cmdblk);
6411 DB_TYPE(mp) = M_CMD;
6412 DB_CPID(mp) = curproc->p_pid;
6415 * Copy in the payload.
6417 if (cmdp->cb_len > 0) {
6418 mp->b_cont = allocb_cred(sizeof (scp->sc_buf), crp,
6419 curproc->p_pid);
6420 if (mp->b_cont == NULL) {
6421 error = ENOMEM;
6422 goto out;
6425 /* cb_len comes from sc_len, which has already been checked */
6426 ASSERT(cmdp->cb_len <= sizeof (scp->sc_buf));
6427 (void) bcopy(scp->sc_buf, mp->b_cont->b_wptr, cmdp->cb_len);
6428 mp->b_cont->b_wptr += cmdp->cb_len;
6429 DB_CPID(mp->b_cont) = curproc->p_pid;
6433 * Since this mechanism is strictly for ptools, and since only one
6434 * process can be grabbed at a time, we simply fail if there's
6435 * currently an operation pending.
6437 mutex_enter(&stp->sd_lock);
6438 if (stp->sd_flag & STRCMDWAIT) {
6439 mutex_exit(&stp->sd_lock);
6440 error = EBUSY;
6441 goto out;
6443 stp->sd_flag |= STRCMDWAIT;
6444 ASSERT(stp->sd_cmdblk == NULL);
6445 mutex_exit(&stp->sd_lock);
6447 putnext(stp->sd_wrq, mp);
6448 mp = NULL;
6451 * Timed wait for acknowledgment. If the reply has already arrived,
6452 * don't sleep. If awakened from the sleep, fail only if the reply
6453 * has not arrived by then. Otherwise, process the reply.
6455 mutex_enter(&stp->sd_lock);
6456 while (stp->sd_cmdblk == NULL) {
6457 if (stp->sd_flag & errs) {
6458 if ((error = strgeterr(stp, errs, 0)) != 0)
6459 goto waitout;
6462 rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock, timeout, 0);
6463 if (stp->sd_cmdblk != NULL)
6464 break;
6466 if (rval <= 0) {
6467 error = (rval == 0) ? EINTR : ETIME;
6468 goto waitout;
6473 * We received a reply.
6475 mp = stp->sd_cmdblk;
6476 stp->sd_cmdblk = NULL;
6477 ASSERT(mp != NULL && DB_TYPE(mp) == M_CMD);
6478 ASSERT(stp->sd_flag & STRCMDWAIT);
6479 stp->sd_flag &= ~STRCMDWAIT;
6480 mutex_exit(&stp->sd_lock);
6482 cmdp = (struct cmdblk *)mp->b_rptr;
6483 if ((error = cmdp->cb_error) != 0)
6484 goto out;
6487 * Data may have been returned in the reply (cb_len > 0).
6488 * If so, copy it out to the user's buffer.
6490 if (cmdp->cb_len > 0) {
6491 if (mp->b_cont == NULL || MBLKL(mp->b_cont) < cmdp->cb_len) {
6492 error = EPROTO;
6493 goto out;
6496 cmdp->cb_len = MIN(cmdp->cb_len, sizeof (scp->sc_buf));
6497 (void) bcopy(mp->b_cont->b_rptr, scp->sc_buf, cmdp->cb_len);
6499 scp->sc_len = cmdp->cb_len;
6500 out:
6501 freemsg(mp);
6502 crfree(crp);
6503 return (error);
6504 waitout:
6505 ASSERT(stp->sd_cmdblk == NULL);
6506 stp->sd_flag &= ~STRCMDWAIT;
6507 mutex_exit(&stp->sd_lock);
6508 crfree(crp);
6509 return (error);
6513 * For the SunOS keyboard driver.
6514 * Return the next available "ioctl" sequence number.
6515 * Exported, so that streams modules can send "ioctl" messages
6516 * downstream from their open routine.
6519 getiocseqno(void)
6521 int i;
6523 mutex_enter(&strresources);
6524 i = ++ioc_id;
6525 mutex_exit(&strresources);
6526 return (i);
6530 * Get the next message from the read queue. If the message is
6531 * priority, STRPRI will have been set by strrput(). This flag
6532 * should be reset only when the entire message at the front of the
6533 * queue as been consumed.
6535 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
6538 strgetmsg(
6539 struct vnode *vp,
6540 struct strbuf *mctl,
6541 struct strbuf *mdata,
6542 unsigned char *prip,
6543 int *flagsp,
6544 int fmode,
6545 rval_t *rvp)
6547 struct stdata *stp;
6548 mblk_t *bp, *nbp;
6549 mblk_t *savemp = NULL;
6550 mblk_t *savemptail = NULL;
6551 uint_t old_sd_flag;
6552 int flg;
6553 int more = 0;
6554 int error = 0;
6555 char first = 1;
6556 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
6557 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
6558 unsigned char pri = 0;
6559 queue_t *q;
6560 int pr = 0; /* Partial read successful */
6561 struct uio uios;
6562 struct uio *uiop = &uios;
6563 struct iovec iovs;
6564 unsigned char type;
6566 TRACE_1(TR_FAC_STREAMS_FR, TR_STRGETMSG_ENTER,
6567 "strgetmsg:%p", vp);
6569 ASSERT(vp->v_stream);
6570 stp = vp->v_stream;
6571 rvp->r_val1 = 0;
6573 mutex_enter(&stp->sd_lock);
6575 if ((error = i_straccess(stp, JCREAD)) != 0) {
6576 mutex_exit(&stp->sd_lock);
6577 return (error);
6580 if (stp->sd_flag & (STRDERR|STPLEX)) {
6581 error = strgeterr(stp, STRDERR|STPLEX, 0);
6582 if (error != 0) {
6583 mutex_exit(&stp->sd_lock);
6584 return (error);
6587 mutex_exit(&stp->sd_lock);
6589 switch (*flagsp) {
6590 case MSG_HIPRI:
6591 if (*prip != 0)
6592 return (EINVAL);
6593 break;
6595 case MSG_ANY:
6596 case MSG_BAND:
6597 break;
6599 default:
6600 return (EINVAL);
6603 * Setup uio and iov for data part
6605 iovs.iov_base = mdata->buf;
6606 iovs.iov_len = mdata->maxlen;
6607 uios.uio_iov = &iovs;
6608 uios.uio_iovcnt = 1;
6609 uios.uio_loffset = 0;
6610 uios.uio_segflg = UIO_USERSPACE;
6611 uios.uio_fmode = 0;
6612 uios.uio_extflg = UIO_COPY_CACHED;
6613 uios.uio_resid = mdata->maxlen;
6614 uios.uio_offset = 0;
6616 q = _RD(stp->sd_wrq);
6617 mutex_enter(&stp->sd_lock);
6618 old_sd_flag = stp->sd_flag;
6619 mark = 0;
6620 for (;;) {
6621 int done = 0;
6622 mblk_t *q_first = q->q_first;
6625 * Get the next message of appropriate priority
6626 * from the stream head. If the caller is interested
6627 * in band or hipri messages, then they should already
6628 * be enqueued at the stream head. On the other hand
6629 * if the caller wants normal (band 0) messages, they
6630 * might be deferred in a synchronous stream and they
6631 * will need to be pulled up.
6633 * After we have dequeued a message, we might find that
6634 * it was a deferred M_SIG that was enqueued at the
6635 * stream head. It must now be posted as part of the
6636 * read by calling strsignal_nolock().
6638 * Also note that strrput does not enqueue an M_PCSIG,
6639 * and there cannot be more than one hipri message,
6640 * so there was no need to have the M_PCSIG case.
6642 * At some time it might be nice to try and wrap the
6643 * functionality of kstrgetmsg() and strgetmsg() into
6644 * a common routine so to reduce the amount of replicated
6645 * code (since they are extremely similar).
6647 if (!(*flagsp & (MSG_HIPRI|MSG_BAND))) {
6648 /* Asking for normal, band0 data */
6649 bp = strget(stp, q, uiop, first, &error);
6650 ASSERT(MUTEX_HELD(&stp->sd_lock));
6651 if (bp != NULL) {
6652 if (DB_TYPE(bp) == M_SIG) {
6653 strsignal_nolock(stp, *bp->b_rptr,
6654 bp->b_band);
6655 freemsg(bp);
6656 continue;
6657 } else {
6658 break;
6661 if (error != 0)
6662 goto getmout;
6665 * We can't depend on the value of STRPRI here because
6666 * the stream head may be in transit. Therefore, we
6667 * must look at the type of the first message to
6668 * determine if a high priority messages is waiting
6670 } else if ((*flagsp & MSG_HIPRI) && q_first != NULL &&
6671 DB_TYPE(q_first) >= QPCTL &&
6672 (bp = getq_noenab(q, 0)) != NULL) {
6673 /* Asked for HIPRI and got one */
6674 ASSERT(DB_TYPE(bp) >= QPCTL);
6675 break;
6676 } else if ((*flagsp & MSG_BAND) && q_first != NULL &&
6677 ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
6678 (bp = getq_noenab(q, 0)) != NULL) {
6680 * Asked for at least band "prip" and got either at
6681 * least that band or a hipri message.
6683 ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
6684 if (DB_TYPE(bp) == M_SIG) {
6685 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
6686 freemsg(bp);
6687 continue;
6688 } else {
6689 break;
6693 /* No data. Time to sleep? */
6694 qbackenable(q, 0);
6697 * If STRHUP or STREOF, return 0 length control and data.
6698 * If resid is 0, then a read(fd,buf,0) was done. Do not
6699 * sleep to satisfy this request because by default we have
6700 * zero bytes to return.
6702 if ((stp->sd_flag & (STRHUP|STREOF)) || (mctl->maxlen == 0 &&
6703 mdata->maxlen == 0)) {
6704 mctl->len = mdata->len = 0;
6705 *flagsp = 0;
6706 mutex_exit(&stp->sd_lock);
6707 return (0);
6709 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_WAIT,
6710 "strgetmsg calls strwaitq:%p, %p",
6711 vp, uiop);
6712 if (((error = strwaitq(stp, GETWAIT, (ssize_t)0, fmode, -1,
6713 &done)) != 0) || done) {
6714 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_DONE,
6715 "strgetmsg error or done:%p, %p",
6716 vp, uiop);
6717 mutex_exit(&stp->sd_lock);
6718 return (error);
6720 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_AWAKE,
6721 "strgetmsg awakes:%p, %p", vp, uiop);
6722 if ((error = i_straccess(stp, JCREAD)) != 0) {
6723 mutex_exit(&stp->sd_lock);
6724 return (error);
6726 first = 0;
6728 ASSERT(bp != NULL);
6730 * Extract any mark information. If the message is not completely
6731 * consumed this information will be put in the mblk
6732 * that is putback.
6733 * If MSGMARKNEXT is set and the message is completely consumed
6734 * the STRATMARK flag will be set below. Likewise, if
6735 * MSGNOTMARKNEXT is set and the message is
6736 * completely consumed STRNOTATMARK will be set.
6738 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
6739 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
6740 (MSGMARKNEXT|MSGNOTMARKNEXT));
6741 if (mark != 0 && bp == stp->sd_mark) {
6742 mark |= _LASTMARK;
6743 stp->sd_mark = NULL;
6746 * keep track of the original message type and priority
6748 pri = bp->b_band;
6749 type = bp->b_datap->db_type;
6750 if (type == M_PASSFP) {
6751 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
6752 stp->sd_mark = bp;
6753 bp->b_flag |= mark & ~_LASTMARK;
6754 putback(stp, q, bp, pri);
6755 qbackenable(q, pri);
6756 mutex_exit(&stp->sd_lock);
6757 return (EBADMSG);
6759 ASSERT(type != M_SIG);
6762 * Set this flag so strrput will not generate signals. Need to
6763 * make sure this flag is cleared before leaving this routine
6764 * else signals will stop being sent.
6766 stp->sd_flag |= STRGETINPROG;
6767 mutex_exit(&stp->sd_lock);
6769 if (STREAM_NEEDSERVICE(stp))
6770 stream_runservice(stp);
6773 * Set HIPRI flag if message is priority.
6775 if (type >= QPCTL)
6776 flg = MSG_HIPRI;
6777 else
6778 flg = MSG_BAND;
6781 * First process PROTO or PCPROTO blocks, if any.
6783 if (mctl->maxlen >= 0 && type != M_DATA) {
6784 size_t n, bcnt;
6785 char *ubuf;
6787 bcnt = mctl->maxlen;
6788 ubuf = mctl->buf;
6789 while (bp != NULL && bp->b_datap->db_type != M_DATA) {
6790 if ((n = MIN(bcnt, bp->b_wptr - bp->b_rptr)) != 0 &&
6791 copyout(bp->b_rptr, ubuf, n)) {
6792 error = EFAULT;
6793 mutex_enter(&stp->sd_lock);
6795 * clear stream head pri flag based on
6796 * first message type
6798 if (type >= QPCTL) {
6799 ASSERT(type == M_PCPROTO);
6800 stp->sd_flag &= ~STRPRI;
6802 more = 0;
6803 freemsg(bp);
6804 goto getmout;
6806 ubuf += n;
6807 bp->b_rptr += n;
6808 if (bp->b_rptr >= bp->b_wptr) {
6809 nbp = bp;
6810 bp = bp->b_cont;
6811 freeb(nbp);
6813 ASSERT(n <= bcnt);
6814 bcnt -= n;
6815 if (bcnt == 0)
6816 break;
6818 mctl->len = mctl->maxlen - bcnt;
6819 } else
6820 mctl->len = -1;
6822 if (bp && bp->b_datap->db_type != M_DATA) {
6824 * More PROTO blocks in msg.
6826 more |= MORECTL;
6827 savemp = bp;
6828 while (bp && bp->b_datap->db_type != M_DATA) {
6829 savemptail = bp;
6830 bp = bp->b_cont;
6832 savemptail->b_cont = NULL;
6836 * Now process DATA blocks, if any.
6838 if (mdata->maxlen >= 0 && bp) {
6840 * struiocopyout will consume a potential zero-length
6841 * M_DATA even if uio_resid is zero.
6843 size_t oldresid = uiop->uio_resid;
6845 bp = struiocopyout(bp, uiop, &error);
6846 if (error != 0) {
6847 mutex_enter(&stp->sd_lock);
6849 * clear stream head hi pri flag based on
6850 * first message
6852 if (type >= QPCTL) {
6853 ASSERT(type == M_PCPROTO);
6854 stp->sd_flag &= ~STRPRI;
6856 more = 0;
6857 freemsg(savemp);
6858 goto getmout;
6861 * (pr == 1) indicates a partial read.
6863 if (oldresid > uiop->uio_resid)
6864 pr = 1;
6865 mdata->len = mdata->maxlen - uiop->uio_resid;
6866 } else
6867 mdata->len = -1;
6869 if (bp) { /* more data blocks in msg */
6870 more |= MOREDATA;
6871 if (savemp)
6872 savemptail->b_cont = bp;
6873 else
6874 savemp = bp;
6877 mutex_enter(&stp->sd_lock);
6878 if (savemp) {
6879 if (pr && (savemp->b_datap->db_type == M_DATA) &&
6880 msgnodata(savemp)) {
6882 * Avoid queuing a zero-length tail part of
6883 * a message. pr=1 indicates that we read some of
6884 * the message.
6886 freemsg(savemp);
6887 more &= ~MOREDATA;
6889 * clear stream head hi pri flag based on
6890 * first message
6892 if (type >= QPCTL) {
6893 ASSERT(type == M_PCPROTO);
6894 stp->sd_flag &= ~STRPRI;
6896 } else {
6897 savemp->b_band = pri;
6899 * If the first message was HIPRI and the one we're
6900 * putting back isn't, then clear STRPRI, otherwise
6901 * set STRPRI again. Note that we must set STRPRI
6902 * again since the flush logic in strrput_nondata()
6903 * may have cleared it while we had sd_lock dropped.
6905 if (type >= QPCTL) {
6906 ASSERT(type == M_PCPROTO);
6907 if (queclass(savemp) < QPCTL)
6908 stp->sd_flag &= ~STRPRI;
6909 else
6910 stp->sd_flag |= STRPRI;
6911 } else if (queclass(savemp) >= QPCTL) {
6913 * The first message was not a HIPRI message,
6914 * but the one we are about to putback is.
6915 * For simplicitly, we do not allow for HIPRI
6916 * messages to be embedded in the message
6917 * body, so just force it to same type as
6918 * first message.
6920 ASSERT(type == M_DATA || type == M_PROTO);
6921 ASSERT(savemp->b_datap->db_type == M_PCPROTO);
6922 savemp->b_datap->db_type = type;
6924 if (mark != 0) {
6925 savemp->b_flag |= mark & ~_LASTMARK;
6926 if ((mark & _LASTMARK) &&
6927 (stp->sd_mark == NULL)) {
6929 * If another marked message arrived
6930 * while sd_lock was not held sd_mark
6931 * would be non-NULL.
6933 stp->sd_mark = savemp;
6936 putback(stp, q, savemp, pri);
6938 } else {
6940 * The complete message was consumed.
6942 * If another M_PCPROTO arrived while sd_lock was not held
6943 * it would have been discarded since STRPRI was still set.
6945 * Move the MSG*MARKNEXT information
6946 * to the stream head just in case
6947 * the read queue becomes empty.
6948 * clear stream head hi pri flag based on
6949 * first message
6951 * If the stream head was at the mark
6952 * (STRATMARK) before we dropped sd_lock above
6953 * and some data was consumed then we have
6954 * moved past the mark thus STRATMARK is
6955 * cleared. However, if a message arrived in
6956 * strrput during the copyout above causing
6957 * STRATMARK to be set we can not clear that
6958 * flag.
6960 if (type >= QPCTL) {
6961 ASSERT(type == M_PCPROTO);
6962 stp->sd_flag &= ~STRPRI;
6964 if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
6965 if (mark & MSGMARKNEXT) {
6966 stp->sd_flag &= ~STRNOTATMARK;
6967 stp->sd_flag |= STRATMARK;
6968 } else if (mark & MSGNOTMARKNEXT) {
6969 stp->sd_flag &= ~STRATMARK;
6970 stp->sd_flag |= STRNOTATMARK;
6971 } else {
6972 stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
6974 } else if (pr && (old_sd_flag & STRATMARK)) {
6975 stp->sd_flag &= ~STRATMARK;
6979 *flagsp = flg;
6980 *prip = pri;
6983 * Getmsg cleanup processing - if the state of the queue has changed
6984 * some signals may need to be sent and/or poll awakened.
6986 getmout:
6987 qbackenable(q, pri);
6990 * We dropped the stream head lock above. Send all M_SIG messages
6991 * before processing stream head for SIGPOLL messages.
6993 ASSERT(MUTEX_HELD(&stp->sd_lock));
6994 while ((bp = q->q_first) != NULL &&
6995 (bp->b_datap->db_type == M_SIG)) {
6997 * sd_lock is held so the content of the read queue can not
6998 * change.
7000 bp = getq(q);
7001 ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7003 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7004 mutex_exit(&stp->sd_lock);
7005 freemsg(bp);
7006 if (STREAM_NEEDSERVICE(stp))
7007 stream_runservice(stp);
7008 mutex_enter(&stp->sd_lock);
7012 * stream head cannot change while we make the determination
7013 * whether or not to send a signal. Drop the flag to allow strrput
7014 * to send firstmsgsigs again.
7016 stp->sd_flag &= ~STRGETINPROG;
7019 * If the type of message at the front of the queue changed
7020 * due to the receive the appropriate signals and pollwakeup events
7021 * are generated. The type of changes are:
7022 * Processed a hipri message, q_first is not hipri.
7023 * Processed a band X message, and q_first is band Y.
7024 * The generated signals and pollwakeups are identical to what
7025 * strrput() generates should the message that is now on q_first
7026 * arrive to an empty read queue.
7028 * Note: only strrput will send a signal for a hipri message.
7030 if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7031 strsigset_t signals = 0;
7032 strpollset_t pollwakeups = 0;
7034 if (flg & MSG_HIPRI) {
7036 * Removed a hipri message. Regular data at
7037 * the front of the queue.
7039 if (bp->b_band == 0) {
7040 signals = S_INPUT | S_RDNORM;
7041 pollwakeups = POLLIN | POLLRDNORM;
7042 } else {
7043 signals = S_INPUT | S_RDBAND;
7044 pollwakeups = POLLIN | POLLRDBAND;
7046 } else if (pri != bp->b_band) {
7048 * The band is different for the new q_first.
7050 if (bp->b_band == 0) {
7051 signals = S_RDNORM;
7052 pollwakeups = POLLIN | POLLRDNORM;
7053 } else {
7054 signals = S_RDBAND;
7055 pollwakeups = POLLIN | POLLRDBAND;
7059 if (pollwakeups != 0) {
7060 if (pollwakeups == (POLLIN | POLLRDNORM)) {
7061 if (!(stp->sd_rput_opt & SR_POLLIN))
7062 goto no_pollwake;
7063 stp->sd_rput_opt &= ~SR_POLLIN;
7065 mutex_exit(&stp->sd_lock);
7066 pollwakeup(&stp->sd_pollist, pollwakeups);
7067 mutex_enter(&stp->sd_lock);
7069 no_pollwake:
7071 if (stp->sd_sigflags & signals)
7072 strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7074 mutex_exit(&stp->sd_lock);
7076 rvp->r_val1 = more;
7077 return (error);
7078 #undef _LASTMARK
7082 * Get the next message from the read queue. If the message is
7083 * priority, STRPRI will have been set by strrput(). This flag
7084 * should be reset only when the entire message at the front of the
7085 * queue as been consumed.
7087 * If uiop is NULL all data is returned in mctlp.
7088 * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
7089 * not enabled.
7090 * The timeout parameter is in milliseconds; -1 for infinity.
7091 * This routine handles the consolidation private flags:
7092 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7093 * MSG_DELAYERROR Defer the error check until the queue is empty.
7094 * MSG_HOLDSIG Hold signals while waiting for data.
7095 * MSG_IPEEK Only peek at messages.
7096 * MSG_DISCARDTAIL Discard the tail M_DATA part of the message
7097 * that doesn't fit.
7098 * MSG_NOMARK If the message is marked leave it on the queue.
7100 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
7103 kstrgetmsg(
7104 struct vnode *vp,
7105 mblk_t **mctlp,
7106 struct uio *uiop,
7107 unsigned char *prip,
7108 int *flagsp,
7109 clock_t timout,
7110 rval_t *rvp)
7112 struct stdata *stp;
7113 mblk_t *bp, *nbp;
7114 mblk_t *savemp = NULL;
7115 mblk_t *savemptail = NULL;
7116 int flags;
7117 uint_t old_sd_flag;
7118 int flg;
7119 int more = 0;
7120 int error = 0;
7121 char first = 1;
7122 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
7123 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
7124 unsigned char pri = 0;
7125 queue_t *q;
7126 int pr = 0; /* Partial read successful */
7127 unsigned char type;
7129 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_ENTER,
7130 "kstrgetmsg:%p", vp);
7132 ASSERT(vp->v_stream);
7133 stp = vp->v_stream;
7134 rvp->r_val1 = 0;
7136 mutex_enter(&stp->sd_lock);
7138 if ((error = i_straccess(stp, JCREAD)) != 0) {
7139 mutex_exit(&stp->sd_lock);
7140 return (error);
7143 flags = *flagsp;
7144 if (stp->sd_flag & (STRDERR|STPLEX)) {
7145 if ((stp->sd_flag & STPLEX) ||
7146 (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == 0) {
7147 error = strgeterr(stp, STRDERR|STPLEX,
7148 (flags & MSG_IPEEK));
7149 if (error != 0) {
7150 mutex_exit(&stp->sd_lock);
7151 return (error);
7155 mutex_exit(&stp->sd_lock);
7157 switch (flags & (MSG_HIPRI|MSG_ANY|MSG_BAND)) {
7158 case MSG_HIPRI:
7159 if (*prip != 0)
7160 return (EINVAL);
7161 break;
7163 case MSG_ANY:
7164 case MSG_BAND:
7165 break;
7167 default:
7168 return (EINVAL);
7171 retry:
7172 q = _RD(stp->sd_wrq);
7173 mutex_enter(&stp->sd_lock);
7174 old_sd_flag = stp->sd_flag;
7175 mark = 0;
7176 for (;;) {
7177 int done = 0;
7178 int waitflag;
7179 int fmode;
7180 mblk_t *q_first = q->q_first;
7183 * This section of the code operates just like the code
7184 * in strgetmsg(). There is a comment there about what
7185 * is going on here.
7187 if (!(flags & (MSG_HIPRI|MSG_BAND))) {
7188 /* Asking for normal, band0 data */
7189 bp = strget(stp, q, uiop, first, &error);
7190 ASSERT(MUTEX_HELD(&stp->sd_lock));
7191 if (bp != NULL) {
7192 if (DB_TYPE(bp) == M_SIG) {
7193 strsignal_nolock(stp, *bp->b_rptr,
7194 bp->b_band);
7195 freemsg(bp);
7196 continue;
7197 } else {
7198 break;
7201 if (error != 0) {
7202 goto getmout;
7205 * We can't depend on the value of STRPRI here because
7206 * the stream head may be in transit. Therefore, we
7207 * must look at the type of the first message to
7208 * determine if a high priority messages is waiting
7210 } else if ((flags & MSG_HIPRI) && q_first != NULL &&
7211 DB_TYPE(q_first) >= QPCTL &&
7212 (bp = getq_noenab(q, 0)) != NULL) {
7213 ASSERT(DB_TYPE(bp) >= QPCTL);
7214 break;
7215 } else if ((flags & MSG_BAND) && q_first != NULL &&
7216 ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
7217 (bp = getq_noenab(q, 0)) != NULL) {
7219 * Asked for at least band "prip" and got either at
7220 * least that band or a hipri message.
7222 ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
7223 if (DB_TYPE(bp) == M_SIG) {
7224 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7225 freemsg(bp);
7226 continue;
7227 } else {
7228 break;
7232 /* No data. Time to sleep? */
7233 qbackenable(q, 0);
7236 * Delayed error notification?
7238 if ((stp->sd_flag & (STRDERR|STPLEX)) &&
7239 (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == MSG_DELAYERROR) {
7240 error = strgeterr(stp, STRDERR|STPLEX,
7241 (flags & MSG_IPEEK));
7242 if (error != 0) {
7243 mutex_exit(&stp->sd_lock);
7244 return (error);
7249 * If STRHUP or STREOF, return 0 length control and data.
7250 * If a read(fd,buf,0) has been done, do not sleep, just
7251 * return.
7253 * If mctlp == NULL and uiop == NULL, then the code will
7254 * do the strwaitq. This is an understood way of saying
7255 * sleep "polling" until a message is received.
7257 if ((stp->sd_flag & (STRHUP|STREOF)) ||
7258 (uiop != NULL && uiop->uio_resid == 0)) {
7259 if (mctlp != NULL)
7260 *mctlp = NULL;
7261 *flagsp = 0;
7262 mutex_exit(&stp->sd_lock);
7263 return (0);
7266 waitflag = GETWAIT;
7267 if (flags &
7268 (MSG_HOLDSIG|MSG_IGNERROR|MSG_IPEEK|MSG_DELAYERROR)) {
7269 if (flags & MSG_HOLDSIG)
7270 waitflag |= STR_NOSIG;
7271 if (flags & MSG_IGNERROR)
7272 waitflag |= STR_NOERROR;
7273 if (flags & MSG_IPEEK)
7274 waitflag |= STR_PEEK;
7275 if (flags & MSG_DELAYERROR)
7276 waitflag |= STR_DELAYERR;
7278 if (uiop != NULL)
7279 fmode = uiop->uio_fmode;
7280 else
7281 fmode = 0;
7283 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_WAIT,
7284 "kstrgetmsg calls strwaitq:%p, %p",
7285 vp, uiop);
7286 if (((error = strwaitq(stp, waitflag, (ssize_t)0,
7287 fmode, timout, &done))) != 0 || done) {
7288 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_DONE,
7289 "kstrgetmsg error or done:%p, %p",
7290 vp, uiop);
7291 mutex_exit(&stp->sd_lock);
7292 return (error);
7294 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_AWAKE,
7295 "kstrgetmsg awakes:%p, %p", vp, uiop);
7296 if ((error = i_straccess(stp, JCREAD)) != 0) {
7297 mutex_exit(&stp->sd_lock);
7298 return (error);
7300 first = 0;
7302 ASSERT(bp != NULL);
7304 * Extract any mark information. If the message is not completely
7305 * consumed this information will be put in the mblk
7306 * that is putback.
7307 * If MSGMARKNEXT is set and the message is completely consumed
7308 * the STRATMARK flag will be set below. Likewise, if
7309 * MSGNOTMARKNEXT is set and the message is
7310 * completely consumed STRNOTATMARK will be set.
7312 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
7313 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
7314 (MSGMARKNEXT|MSGNOTMARKNEXT));
7315 pri = bp->b_band;
7316 if (mark != 0) {
7318 * If the caller doesn't want the mark return.
7319 * Used to implement MSG_WAITALL in sockets.
7321 if (flags & MSG_NOMARK) {
7322 putback(stp, q, bp, pri);
7323 qbackenable(q, pri);
7324 mutex_exit(&stp->sd_lock);
7325 return (EWOULDBLOCK);
7327 if (bp == stp->sd_mark) {
7328 mark |= _LASTMARK;
7329 stp->sd_mark = NULL;
7334 * keep track of the first message type
7336 type = bp->b_datap->db_type;
7338 if (bp->b_datap->db_type == M_PASSFP) {
7339 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7340 stp->sd_mark = bp;
7341 bp->b_flag |= mark & ~_LASTMARK;
7342 putback(stp, q, bp, pri);
7343 qbackenable(q, pri);
7344 mutex_exit(&stp->sd_lock);
7345 return (EBADMSG);
7347 ASSERT(type != M_SIG);
7349 if (flags & MSG_IPEEK) {
7351 * Clear any struioflag - we do the uiomove over again
7352 * when peeking since it simplifies the code.
7354 * Dup the message and put the original back on the queue.
7355 * If dupmsg() fails, try again with copymsg() to see if
7356 * there is indeed a shortage of memory. dupmsg() may fail
7357 * if db_ref in any of the messages reaches its limit.
7360 if ((nbp = dupmsg(bp)) == NULL && (nbp = copymsg(bp)) == NULL) {
7362 * Restore the state of the stream head since we
7363 * need to drop sd_lock (strwaitbuf is sleeping).
7365 size_t size = msgdsize(bp);
7367 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7368 stp->sd_mark = bp;
7369 bp->b_flag |= mark & ~_LASTMARK;
7370 putback(stp, q, bp, pri);
7371 mutex_exit(&stp->sd_lock);
7372 error = strwaitbuf(size, BPRI_HI);
7373 if (error) {
7375 * There is no net change to the queue thus
7376 * no need to qbackenable.
7378 return (error);
7380 goto retry;
7383 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7384 stp->sd_mark = bp;
7385 bp->b_flag |= mark & ~_LASTMARK;
7386 putback(stp, q, bp, pri);
7387 bp = nbp;
7391 * Set this flag so strrput will not generate signals. Need to
7392 * make sure this flag is cleared before leaving this routine
7393 * else signals will stop being sent.
7395 stp->sd_flag |= STRGETINPROG;
7396 mutex_exit(&stp->sd_lock);
7398 if ((stp->sd_rputdatafunc != NULL) && (DB_TYPE(bp) == M_DATA)) {
7399 mblk_t *tmp, *prevmp;
7402 * Put first non-data mblk back to stream head and
7403 * cut the mblk chain so sd_rputdatafunc only sees
7404 * M_DATA mblks. We can skip the first mblk since it
7405 * is M_DATA according to the condition above.
7407 for (prevmp = bp, tmp = bp->b_cont; tmp != NULL;
7408 prevmp = tmp, tmp = tmp->b_cont) {
7409 if (DB_TYPE(tmp) != M_DATA) {
7410 prevmp->b_cont = NULL;
7411 mutex_enter(&stp->sd_lock);
7412 putback(stp, q, tmp, tmp->b_band);
7413 mutex_exit(&stp->sd_lock);
7414 break;
7418 bp = (stp->sd_rputdatafunc)(stp->sd_vnode, bp,
7419 NULL, NULL, NULL, NULL);
7421 if (bp == NULL)
7422 goto retry;
7425 if (STREAM_NEEDSERVICE(stp))
7426 stream_runservice(stp);
7429 * Set HIPRI flag if message is priority.
7431 if (type >= QPCTL)
7432 flg = MSG_HIPRI;
7433 else
7434 flg = MSG_BAND;
7437 * First process PROTO or PCPROTO blocks, if any.
7439 if (mctlp != NULL && type != M_DATA) {
7440 mblk_t *nbp;
7442 *mctlp = bp;
7443 while (bp->b_cont && bp->b_cont->b_datap->db_type != M_DATA)
7444 bp = bp->b_cont;
7445 nbp = bp->b_cont;
7446 bp->b_cont = NULL;
7447 bp = nbp;
7450 if (bp && bp->b_datap->db_type != M_DATA) {
7452 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
7454 more |= MORECTL;
7455 savemp = bp;
7456 while (bp && bp->b_datap->db_type != M_DATA) {
7457 savemptail = bp;
7458 bp = bp->b_cont;
7460 savemptail->b_cont = NULL;
7464 * Now process DATA blocks, if any.
7466 if (uiop == NULL) {
7467 /* Append data to tail of mctlp */
7469 if (mctlp != NULL) {
7470 mblk_t **mpp = mctlp;
7472 while (*mpp != NULL)
7473 mpp = &((*mpp)->b_cont);
7474 *mpp = bp;
7475 bp = NULL;
7477 } else if (uiop->uio_resid >= 0 && bp) {
7478 size_t oldresid = uiop->uio_resid;
7481 * If a streams message is likely to consist
7482 * of many small mblks, it is pulled up into
7483 * one continuous chunk of memory.
7484 * The size of the first mblk may be bogus because
7485 * successive read() calls on the socket reduce
7486 * the size of this mblk until it is exhausted
7487 * and then the code walks on to the next. Thus
7488 * the size of the mblk may not be the original size
7489 * that was passed up, it's simply a remainder
7490 * and hence can be very small without any
7491 * implication that the packet is badly fragmented.
7492 * So the size of the possible second mblk is
7493 * used to spot a badly fragmented packet.
7494 * see longer comment at top of page
7495 * by mblk_pull_len declaration.
7498 if (bp->b_cont != NULL && MBLKL(bp->b_cont) < mblk_pull_len) {
7499 (void) pullupmsg(bp, -1);
7502 bp = struiocopyout(bp, uiop, &error);
7503 if (error != 0) {
7504 if (mctlp != NULL) {
7505 freemsg(*mctlp);
7506 *mctlp = NULL;
7507 } else
7508 freemsg(savemp);
7509 mutex_enter(&stp->sd_lock);
7511 * clear stream head hi pri flag based on
7512 * first message
7514 if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7515 ASSERT(type == M_PCPROTO);
7516 stp->sd_flag &= ~STRPRI;
7518 more = 0;
7519 goto getmout;
7522 * (pr == 1) indicates a partial read.
7524 if (oldresid > uiop->uio_resid)
7525 pr = 1;
7528 if (bp) { /* more data blocks in msg */
7529 more |= MOREDATA;
7530 if (savemp)
7531 savemptail->b_cont = bp;
7532 else
7533 savemp = bp;
7536 mutex_enter(&stp->sd_lock);
7537 if (savemp) {
7538 if (flags & (MSG_IPEEK|MSG_DISCARDTAIL)) {
7540 * When MSG_DISCARDTAIL is set or
7541 * when peeking discard any tail. When peeking this
7542 * is the tail of the dup that was copied out - the
7543 * message has already been putback on the queue.
7544 * Return MOREDATA to the caller even though the data
7545 * is discarded. This is used by sockets (to
7546 * set MSG_TRUNC).
7548 freemsg(savemp);
7549 if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7550 ASSERT(type == M_PCPROTO);
7551 stp->sd_flag &= ~STRPRI;
7553 } else if (pr && (savemp->b_datap->db_type == M_DATA) &&
7554 msgnodata(savemp)) {
7556 * Avoid queuing a zero-length tail part of
7557 * a message. pr=1 indicates that we read some of
7558 * the message.
7560 freemsg(savemp);
7561 more &= ~MOREDATA;
7562 if (type >= QPCTL) {
7563 ASSERT(type == M_PCPROTO);
7564 stp->sd_flag &= ~STRPRI;
7566 } else {
7567 savemp->b_band = pri;
7569 * If the first message was HIPRI and the one we're
7570 * putting back isn't, then clear STRPRI, otherwise
7571 * set STRPRI again. Note that we must set STRPRI
7572 * again since the flush logic in strrput_nondata()
7573 * may have cleared it while we had sd_lock dropped.
7576 if (type >= QPCTL) {
7577 ASSERT(type == M_PCPROTO);
7578 if (queclass(savemp) < QPCTL)
7579 stp->sd_flag &= ~STRPRI;
7580 else
7581 stp->sd_flag |= STRPRI;
7582 } else if (queclass(savemp) >= QPCTL) {
7584 * The first message was not a HIPRI message,
7585 * but the one we are about to putback is.
7586 * For simplicitly, we do not allow for HIPRI
7587 * messages to be embedded in the message
7588 * body, so just force it to same type as
7589 * first message.
7591 ASSERT(type == M_DATA || type == M_PROTO);
7592 ASSERT(savemp->b_datap->db_type == M_PCPROTO);
7593 savemp->b_datap->db_type = type;
7595 if (mark != 0) {
7596 if ((mark & _LASTMARK) &&
7597 (stp->sd_mark == NULL)) {
7599 * If another marked message arrived
7600 * while sd_lock was not held sd_mark
7601 * would be non-NULL.
7603 stp->sd_mark = savemp;
7605 savemp->b_flag |= mark & ~_LASTMARK;
7607 putback(stp, q, savemp, pri);
7609 } else if (!(flags & MSG_IPEEK)) {
7611 * The complete message was consumed.
7613 * If another M_PCPROTO arrived while sd_lock was not held
7614 * it would have been discarded since STRPRI was still set.
7616 * Move the MSG*MARKNEXT information
7617 * to the stream head just in case
7618 * the read queue becomes empty.
7619 * clear stream head hi pri flag based on
7620 * first message
7622 * If the stream head was at the mark
7623 * (STRATMARK) before we dropped sd_lock above
7624 * and some data was consumed then we have
7625 * moved past the mark thus STRATMARK is
7626 * cleared. However, if a message arrived in
7627 * strrput during the copyout above causing
7628 * STRATMARK to be set we can not clear that
7629 * flag.
7630 * XXX A "perimeter" would help by single-threading strrput,
7631 * strread, strgetmsg and kstrgetmsg.
7633 if (type >= QPCTL) {
7634 ASSERT(type == M_PCPROTO);
7635 stp->sd_flag &= ~STRPRI;
7637 if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
7638 if (mark & MSGMARKNEXT) {
7639 stp->sd_flag &= ~STRNOTATMARK;
7640 stp->sd_flag |= STRATMARK;
7641 } else if (mark & MSGNOTMARKNEXT) {
7642 stp->sd_flag &= ~STRATMARK;
7643 stp->sd_flag |= STRNOTATMARK;
7644 } else {
7645 stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
7647 } else if (pr && (old_sd_flag & STRATMARK)) {
7648 stp->sd_flag &= ~STRATMARK;
7652 *flagsp = flg;
7653 *prip = pri;
7656 * Getmsg cleanup processing - if the state of the queue has changed
7657 * some signals may need to be sent and/or poll awakened.
7659 getmout:
7660 qbackenable(q, pri);
7663 * We dropped the stream head lock above. Send all M_SIG messages
7664 * before processing stream head for SIGPOLL messages.
7666 ASSERT(MUTEX_HELD(&stp->sd_lock));
7667 while ((bp = q->q_first) != NULL &&
7668 (bp->b_datap->db_type == M_SIG)) {
7670 * sd_lock is held so the content of the read queue can not
7671 * change.
7673 bp = getq(q);
7674 ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7676 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7677 mutex_exit(&stp->sd_lock);
7678 freemsg(bp);
7679 if (STREAM_NEEDSERVICE(stp))
7680 stream_runservice(stp);
7681 mutex_enter(&stp->sd_lock);
7685 * stream head cannot change while we make the determination
7686 * whether or not to send a signal. Drop the flag to allow strrput
7687 * to send firstmsgsigs again.
7689 stp->sd_flag &= ~STRGETINPROG;
7692 * If the type of message at the front of the queue changed
7693 * due to the receive the appropriate signals and pollwakeup events
7694 * are generated. The type of changes are:
7695 * Processed a hipri message, q_first is not hipri.
7696 * Processed a band X message, and q_first is band Y.
7697 * The generated signals and pollwakeups are identical to what
7698 * strrput() generates should the message that is now on q_first
7699 * arrive to an empty read queue.
7701 * Note: only strrput will send a signal for a hipri message.
7703 if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7704 strsigset_t signals = 0;
7705 strpollset_t pollwakeups = 0;
7707 if (flg & MSG_HIPRI) {
7709 * Removed a hipri message. Regular data at
7710 * the front of the queue.
7712 if (bp->b_band == 0) {
7713 signals = S_INPUT | S_RDNORM;
7714 pollwakeups = POLLIN | POLLRDNORM;
7715 } else {
7716 signals = S_INPUT | S_RDBAND;
7717 pollwakeups = POLLIN | POLLRDBAND;
7719 } else if (pri != bp->b_band) {
7721 * The band is different for the new q_first.
7723 if (bp->b_band == 0) {
7724 signals = S_RDNORM;
7725 pollwakeups = POLLIN | POLLRDNORM;
7726 } else {
7727 signals = S_RDBAND;
7728 pollwakeups = POLLIN | POLLRDBAND;
7732 if (pollwakeups != 0) {
7733 if (pollwakeups == (POLLIN | POLLRDNORM)) {
7734 if (!(stp->sd_rput_opt & SR_POLLIN))
7735 goto no_pollwake;
7736 stp->sd_rput_opt &= ~SR_POLLIN;
7738 mutex_exit(&stp->sd_lock);
7739 pollwakeup(&stp->sd_pollist, pollwakeups);
7740 mutex_enter(&stp->sd_lock);
7742 no_pollwake:
7744 if (stp->sd_sigflags & signals)
7745 strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7747 mutex_exit(&stp->sd_lock);
7749 rvp->r_val1 = more;
7750 return (error);
7751 #undef _LASTMARK
7755 * Put a message downstream.
7757 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7760 strputmsg(
7761 struct vnode *vp,
7762 struct strbuf *mctl,
7763 struct strbuf *mdata,
7764 unsigned char pri,
7765 int flag,
7766 int fmode)
7768 struct stdata *stp;
7769 queue_t *wqp;
7770 mblk_t *mp;
7771 ssize_t msgsize;
7772 ssize_t rmin, rmax;
7773 int error;
7774 struct uio uios;
7775 struct uio *uiop = &uios;
7776 struct iovec iovs;
7777 int xpg4 = 0;
7779 ASSERT(vp->v_stream);
7780 stp = vp->v_stream;
7781 wqp = stp->sd_wrq;
7784 * If it is an XPG4 application, we need to send
7785 * SIGPIPE below
7788 xpg4 = (flag & MSG_XPG4) ? 1 : 0;
7789 flag &= ~MSG_XPG4;
7791 if (AU_AUDITING())
7792 audit_strputmsg(vp, mctl, mdata, pri, flag, fmode);
7794 mutex_enter(&stp->sd_lock);
7796 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7797 mutex_exit(&stp->sd_lock);
7798 return (error);
7801 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7802 error = strwriteable(stp, B_FALSE, xpg4);
7803 if (error != 0) {
7804 mutex_exit(&stp->sd_lock);
7805 return (error);
7809 mutex_exit(&stp->sd_lock);
7812 * Check for legal flag value.
7814 switch (flag) {
7815 case MSG_HIPRI:
7816 if ((mctl->len < 0) || (pri != 0))
7817 return (EINVAL);
7818 break;
7819 case MSG_BAND:
7820 break;
7822 default:
7823 return (EINVAL);
7826 TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_IN,
7827 "strputmsg in:stp %p", stp);
7829 /* get these values from those cached in the stream head */
7830 rmin = stp->sd_qn_minpsz;
7831 rmax = stp->sd_qn_maxpsz;
7834 * Make sure ctl and data sizes together fall within the
7835 * limits of the max and min receive packet sizes and do
7836 * not exceed system limit.
7838 ASSERT((rmax >= 0) || (rmax == INFPSZ));
7839 if (rmax == 0) {
7840 return (ERANGE);
7843 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
7844 * Needed to prevent partial failures in the strmakedata loop.
7846 if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
7847 rmax = stp->sd_maxblk;
7849 if ((msgsize = mdata->len) < 0) {
7850 msgsize = 0;
7851 rmin = 0; /* no range check for NULL data part */
7853 if ((msgsize < rmin) ||
7854 ((msgsize > rmax) && (rmax != INFPSZ)) ||
7855 (mctl->len > strctlsz)) {
7856 return (ERANGE);
7860 * Setup uio and iov for data part
7862 iovs.iov_base = mdata->buf;
7863 iovs.iov_len = msgsize;
7864 uios.uio_iov = &iovs;
7865 uios.uio_iovcnt = 1;
7866 uios.uio_loffset = 0;
7867 uios.uio_segflg = UIO_USERSPACE;
7868 uios.uio_fmode = fmode;
7869 uios.uio_extflg = UIO_COPY_DEFAULT;
7870 uios.uio_resid = msgsize;
7871 uios.uio_offset = 0;
7873 /* Ignore flow control in strput for HIPRI */
7874 if (flag & MSG_HIPRI)
7875 flag |= MSG_IGNFLOW;
7877 for (;;) {
7878 int done = 0;
7881 * strput will always free the ctl mblk - even when strput
7882 * fails.
7884 if ((error = strmakectl(mctl, flag, fmode, &mp)) != 0) {
7885 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7886 "strputmsg out:stp %p out %d error %d",
7887 stp, 1, error);
7888 return (error);
7891 * Verify that the whole message can be transferred by
7892 * strput.
7894 ASSERT(stp->sd_maxblk == INFPSZ ||
7895 stp->sd_maxblk >= mdata->len);
7897 msgsize = mdata->len;
7898 error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
7899 mdata->len = msgsize;
7901 if (error == 0)
7902 break;
7904 if (error != EWOULDBLOCK)
7905 goto out;
7907 mutex_enter(&stp->sd_lock);
7909 * Check for a missed wakeup.
7910 * Needed since strput did not hold sd_lock across
7911 * the canputnext.
7913 if (bcanputnext(wqp, pri)) {
7914 /* Try again */
7915 mutex_exit(&stp->sd_lock);
7916 continue;
7918 TRACE_2(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAIT,
7919 "strputmsg wait:stp %p waits pri %d", stp, pri);
7920 if (((error = strwaitq(stp, WRITEWAIT, (ssize_t)0, fmode, -1,
7921 &done)) != 0) || done) {
7922 mutex_exit(&stp->sd_lock);
7923 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7924 "strputmsg out:q %p out %d error %d",
7925 stp, 0, error);
7926 return (error);
7928 TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAKE,
7929 "strputmsg wake:stp %p wakes", stp);
7930 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7931 mutex_exit(&stp->sd_lock);
7932 return (error);
7934 mutex_exit(&stp->sd_lock);
7936 out:
7938 * For historic reasons, applications expect EAGAIN
7939 * when data mblk could not be allocated. so change
7940 * ENOMEM back to EAGAIN
7942 if (error == ENOMEM)
7943 error = EAGAIN;
7944 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7945 "strputmsg out:stp %p out %d error %d", stp, 2, error);
7946 return (error);
7950 * Put a message downstream.
7951 * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
7952 * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
7953 * and the fmode parameter.
7955 * This routine handles the consolidation private flags:
7956 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7957 * MSG_HOLDSIG Hold signals while waiting for data.
7958 * MSG_IGNFLOW Don't check streams flow control.
7960 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7963 kstrputmsg(
7964 struct vnode *vp,
7965 mblk_t *mctl,
7966 struct uio *uiop,
7967 ssize_t msgsize,
7968 unsigned char pri,
7969 int flag,
7970 int fmode)
7972 struct stdata *stp;
7973 queue_t *wqp;
7974 ssize_t rmin, rmax;
7975 int error;
7977 ASSERT(vp->v_stream);
7978 stp = vp->v_stream;
7979 wqp = stp->sd_wrq;
7980 if (AU_AUDITING())
7981 audit_strputmsg(vp, NULL, NULL, pri, flag, fmode);
7982 if (mctl == NULL)
7983 return (EINVAL);
7985 mutex_enter(&stp->sd_lock);
7987 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7988 mutex_exit(&stp->sd_lock);
7989 freemsg(mctl);
7990 return (error);
7993 if ((stp->sd_flag & STPLEX) || !(flag & MSG_IGNERROR)) {
7994 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7995 error = strwriteable(stp, B_FALSE, B_TRUE);
7996 if (error != 0) {
7997 mutex_exit(&stp->sd_lock);
7998 freemsg(mctl);
7999 return (error);
8004 mutex_exit(&stp->sd_lock);
8007 * Check for legal flag value.
8009 switch (flag & (MSG_HIPRI|MSG_BAND|MSG_ANY)) {
8010 case MSG_HIPRI:
8011 if (pri != 0) {
8012 freemsg(mctl);
8013 return (EINVAL);
8015 break;
8016 case MSG_BAND:
8017 break;
8018 default:
8019 freemsg(mctl);
8020 return (EINVAL);
8023 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_IN,
8024 "kstrputmsg in:stp %p", stp);
8026 /* get these values from those cached in the stream head */
8027 rmin = stp->sd_qn_minpsz;
8028 rmax = stp->sd_qn_maxpsz;
8031 * Make sure ctl and data sizes together fall within the
8032 * limits of the max and min receive packet sizes and do
8033 * not exceed system limit.
8035 ASSERT((rmax >= 0) || (rmax == INFPSZ));
8036 if (rmax == 0) {
8037 freemsg(mctl);
8038 return (ERANGE);
8041 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
8042 * Needed to prevent partial failures in the strmakedata loop.
8044 if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
8045 rmax = stp->sd_maxblk;
8047 if (uiop == NULL) {
8048 msgsize = -1;
8049 rmin = -1; /* no range check for NULL data part */
8050 } else {
8051 /* Use uio flags as well as the fmode parameter flags */
8052 fmode |= uiop->uio_fmode;
8054 if ((msgsize < rmin) ||
8055 ((msgsize > rmax) && (rmax != INFPSZ))) {
8056 freemsg(mctl);
8057 return (ERANGE);
8061 /* Ignore flow control in strput for HIPRI */
8062 if (flag & MSG_HIPRI)
8063 flag |= MSG_IGNFLOW;
8065 for (;;) {
8066 int done = 0;
8067 int waitflag;
8068 mblk_t *mp;
8071 * strput will always free the ctl mblk - even when strput
8072 * fails. If MSG_IGNFLOW is set then any error returned
8073 * will cause us to break the loop, so we don't need a copy
8074 * of the message. If MSG_IGNFLOW is not set, then we can
8075 * get hit by flow control and be forced to try again. In
8076 * this case we need to have a copy of the message. We
8077 * do this using copymsg since the message may get modified
8078 * by something below us.
8080 * We've observed that many TPI providers do not check db_ref
8081 * on the control messages but blindly reuse them for the
8082 * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
8083 * friendly to such providers than using dupmsg. Also, note
8084 * that sockfs uses MSG_IGNFLOW for all TPI control messages.
8085 * Only data messages are subject to flow control, hence
8086 * subject to this copymsg.
8088 if (flag & MSG_IGNFLOW) {
8089 mp = mctl;
8090 mctl = NULL;
8091 } else {
8092 do {
8094 * If a message has a free pointer, the message
8095 * must be dupmsg to maintain this pointer.
8096 * Code using this facility must be sure
8097 * that modules below will not change the
8098 * contents of the dblk without checking db_ref
8099 * first. If db_ref is > 1, then the module
8100 * needs to do a copymsg first. Otherwise,
8101 * the contents of the dblk may become
8102 * inconsistent because the freesmg/freeb below
8103 * may end up calling atomic_add_32_nv.
8104 * The atomic_add_32_nv in freeb (accessing
8105 * all of db_ref, db_type, db_flags, and
8106 * db_struioflag) does not prevent other threads
8107 * from concurrently trying to modify e.g.
8108 * db_type.
8110 if (mctl->b_datap->db_frtnp != NULL)
8111 mp = dupmsg(mctl);
8112 else
8113 mp = copymsg(mctl);
8115 if (mp != NULL)
8116 break;
8118 error = strwaitbuf(msgdsize(mctl), BPRI_MED);
8119 if (error) {
8120 freemsg(mctl);
8121 return (error);
8123 } while (mp == NULL);
8126 * Verify that all of msgsize can be transferred by
8127 * strput.
8129 ASSERT(stp->sd_maxblk == INFPSZ || stp->sd_maxblk >= msgsize);
8130 error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
8131 if (error == 0)
8132 break;
8134 if (error != EWOULDBLOCK)
8135 goto out;
8138 * IF MSG_IGNFLOW is set we should have broken out of loop
8139 * above.
8141 ASSERT(!(flag & MSG_IGNFLOW));
8142 mutex_enter(&stp->sd_lock);
8144 * Check for a missed wakeup.
8145 * Needed since strput did not hold sd_lock across
8146 * the canputnext.
8148 if (bcanputnext(wqp, pri)) {
8149 /* Try again */
8150 mutex_exit(&stp->sd_lock);
8151 continue;
8153 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAIT,
8154 "kstrputmsg wait:stp %p waits pri %d", stp, pri);
8156 waitflag = WRITEWAIT;
8157 if (flag & (MSG_HOLDSIG|MSG_IGNERROR)) {
8158 if (flag & MSG_HOLDSIG)
8159 waitflag |= STR_NOSIG;
8160 if (flag & MSG_IGNERROR)
8161 waitflag |= STR_NOERROR;
8163 if (((error = strwaitq(stp, waitflag,
8164 (ssize_t)0, fmode, -1, &done)) != 0) || done) {
8165 mutex_exit(&stp->sd_lock);
8166 TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8167 "kstrputmsg out:stp %p out %d error %d",
8168 stp, 0, error);
8169 freemsg(mctl);
8170 return (error);
8172 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAKE,
8173 "kstrputmsg wake:stp %p wakes", stp);
8174 if ((error = i_straccess(stp, JCWRITE)) != 0) {
8175 mutex_exit(&stp->sd_lock);
8176 freemsg(mctl);
8177 return (error);
8179 mutex_exit(&stp->sd_lock);
8181 out:
8182 freemsg(mctl);
8184 * For historic reasons, applications expect EAGAIN
8185 * when data mblk could not be allocated. so change
8186 * ENOMEM back to EAGAIN
8188 if (error == ENOMEM)
8189 error = EAGAIN;
8190 TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8191 "kstrputmsg out:stp %p out %d error %d", stp, 2, error);
8192 return (error);
8196 * Determines whether the necessary conditions are set on a stream
8197 * for it to be readable, writeable, or have exceptions.
8199 * strpoll handles the consolidation private events:
8200 * POLLNOERR Do not return POLLERR even if there are stream
8201 * head errors.
8202 * Used by sockfs.
8203 * POLLRDDATA Do not return POLLIN unless at least one message on
8204 * the queue contains one or more M_DATA mblks. Thus
8205 * when this flag is set a queue with only
8206 * M_PROTO/M_PCPROTO mblks does not return POLLIN.
8207 * Used by sockfs to ignore T_EXDATA_IND messages.
8209 * Note: POLLRDDATA assumes that synch streams only return messages with
8210 * an M_DATA attached (i.e. not messages consisting of only
8211 * an M_PROTO/M_PCPROTO part).
8214 strpoll(
8215 struct stdata *stp,
8216 short events_arg,
8217 int anyyet,
8218 short *reventsp,
8219 struct pollhead **phpp)
8221 int events = (ushort_t)events_arg;
8222 int retevents = 0;
8223 mblk_t *mp;
8224 qband_t *qbp;
8225 long sd_flags = stp->sd_flag;
8226 int headlocked = 0;
8229 * For performance, a single 'if' tests for most possible edge
8230 * conditions in one shot
8232 if (sd_flags & (STPLEX | STRDERR | STWRERR)) {
8233 if (sd_flags & STPLEX) {
8234 *reventsp = POLLNVAL;
8235 return (EINVAL);
8237 if (((events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) &&
8238 (sd_flags & STRDERR)) ||
8239 ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) &&
8240 (sd_flags & STWRERR))) {
8241 if (!(events & POLLNOERR)) {
8242 *reventsp = POLLERR;
8243 return (0);
8247 if (sd_flags & STRHUP) {
8248 retevents |= POLLHUP;
8249 } else if (events & (POLLWRNORM | POLLWRBAND)) {
8250 queue_t *tq;
8251 queue_t *qp = stp->sd_wrq;
8253 claimstr(qp);
8254 /* Find next module forward that has a service procedure */
8255 tq = qp->q_next->q_nfsrv;
8256 ASSERT(tq != NULL);
8258 if (polllock(&stp->sd_pollist, QLOCK(tq)) != 0) {
8259 releasestr(qp);
8260 *reventsp = POLLNVAL;
8261 return (0);
8263 if (events & POLLWRNORM) {
8264 queue_t *sqp;
8266 if (tq->q_flag & QFULL)
8267 /* ensure backq svc procedure runs */
8268 tq->q_flag |= QWANTW;
8269 else if ((sqp = stp->sd_struiowrq) != NULL) {
8270 /* Check sync stream barrier write q */
8271 mutex_exit(QLOCK(tq));
8272 if (polllock(&stp->sd_pollist,
8273 QLOCK(sqp)) != 0) {
8274 releasestr(qp);
8275 *reventsp = POLLNVAL;
8276 return (0);
8278 if (sqp->q_flag & QFULL)
8279 /* ensure pollwakeup() is done */
8280 sqp->q_flag |= QWANTWSYNC;
8281 else
8282 retevents |= POLLOUT;
8283 /* More write events to process ??? */
8284 if (! (events & POLLWRBAND)) {
8285 mutex_exit(QLOCK(sqp));
8286 releasestr(qp);
8287 goto chkrd;
8289 mutex_exit(QLOCK(sqp));
8290 if (polllock(&stp->sd_pollist,
8291 QLOCK(tq)) != 0) {
8292 releasestr(qp);
8293 *reventsp = POLLNVAL;
8294 return (0);
8296 } else
8297 retevents |= POLLOUT;
8299 if (events & POLLWRBAND) {
8300 qbp = tq->q_bandp;
8301 if (qbp) {
8302 while (qbp) {
8303 if (qbp->qb_flag & QB_FULL)
8304 qbp->qb_flag |= QB_WANTW;
8305 else
8306 retevents |= POLLWRBAND;
8307 qbp = qbp->qb_next;
8309 } else {
8310 retevents |= POLLWRBAND;
8313 mutex_exit(QLOCK(tq));
8314 releasestr(qp);
8316 chkrd:
8317 if (sd_flags & STRPRI) {
8318 retevents |= (events & POLLPRI);
8319 } else if (events & (POLLRDNORM | POLLRDBAND | POLLIN)) {
8320 queue_t *qp = _RD(stp->sd_wrq);
8321 int normevents = (events & (POLLIN | POLLRDNORM));
8324 * Note: Need to do polllock() here since ps_lock may be
8325 * held. See bug 4191544.
8327 if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8328 *reventsp = POLLNVAL;
8329 return (0);
8331 headlocked = 1;
8332 mp = qp->q_first;
8333 while (mp) {
8335 * For POLLRDDATA we scan b_cont and b_next until we
8336 * find an M_DATA.
8338 if ((events & POLLRDDATA) &&
8339 mp->b_datap->db_type != M_DATA) {
8340 mblk_t *nmp = mp->b_cont;
8342 while (nmp != NULL &&
8343 nmp->b_datap->db_type != M_DATA)
8344 nmp = nmp->b_cont;
8345 if (nmp == NULL) {
8346 mp = mp->b_next;
8347 continue;
8350 if (mp->b_band == 0)
8351 retevents |= normevents;
8352 else
8353 retevents |= (events & (POLLIN | POLLRDBAND));
8354 break;
8356 if (! (retevents & normevents) &&
8357 (stp->sd_wakeq & RSLEEP)) {
8359 * Sync stream barrier read queue has data.
8361 retevents |= normevents;
8363 /* Treat eof as normal data */
8364 if (sd_flags & STREOF)
8365 retevents |= normevents;
8368 *reventsp = (short)retevents;
8369 if (retevents && !(events & POLLET)) {
8370 if (headlocked)
8371 mutex_exit(&stp->sd_lock);
8372 return (0);
8376 * If poll() has not found any events yet, set up event cell
8377 * to wake up the poll if a requested event occurs on this
8378 * stream. Check for collisions with outstanding poll requests.
8380 if (!anyyet) {
8381 *phpp = &stp->sd_pollist;
8382 if (headlocked == 0) {
8383 if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8384 *reventsp = POLLNVAL;
8385 return (0);
8387 headlocked = 1;
8389 stp->sd_rput_opt |= SR_POLLIN;
8391 if (headlocked)
8392 mutex_exit(&stp->sd_lock);
8393 return (0);
8397 * The purpose of putback() is to assure sleeping polls/reads
8398 * are awakened when there are no new messages arriving at the,
8399 * stream head, and a message is placed back on the read queue.
8401 * sd_lock must be held when messages are placed back on stream
8402 * head. (getq() holds sd_lock when it removes messages from
8403 * the queue)
8406 static void
8407 putback(struct stdata *stp, queue_t *q, mblk_t *bp, int band)
8409 mblk_t *qfirst;
8410 ASSERT(MUTEX_HELD(&stp->sd_lock));
8413 * As a result of lock-step ordering around q_lock and sd_lock,
8414 * it's possible for function calls like putnext() and
8415 * canputnext() to get an inaccurate picture of how much
8416 * data is really being processed at the stream head.
8417 * We only consolidate with existing messages on the queue
8418 * if the length of the message we want to put back is smaller
8419 * than the queue hiwater mark.
8421 if ((stp->sd_rput_opt & SR_CONSOL_DATA) &&
8422 (DB_TYPE(bp) == M_DATA) && ((qfirst = q->q_first) != NULL) &&
8423 (DB_TYPE(qfirst) == M_DATA) &&
8424 ((qfirst->b_flag & (MSGMARK|MSGDELIM)) == 0) &&
8425 ((bp->b_flag & (MSGMARK|MSGDELIM|MSGMARKNEXT)) == 0) &&
8426 (mp_cont_len(bp, NULL) < q->q_hiwat)) {
8428 * We use the same logic as defined in strrput()
8429 * but in reverse as we are putting back onto the
8430 * queue and want to retain byte ordering.
8431 * Consolidate M_DATA messages with M_DATA ONLY.
8432 * strrput() allows the consolidation of M_DATA onto
8433 * M_PROTO | M_PCPROTO but not the other way round.
8435 * The consolidation does not take place if the message
8436 * we are returning to the queue is marked with either
8437 * of the marks or the delim flag or if q_first
8438 * is marked with MSGMARK. The MSGMARK check is needed to
8439 * handle the odd semantics of MSGMARK where essentially
8440 * the whole message is to be treated as marked.
8441 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from q_first
8442 * to the front of the b_cont chain.
8444 rmvq_noenab(q, qfirst);
8447 * The first message in the b_cont list
8448 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
8449 * We need to handle the case where we
8450 * are appending:
8452 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
8453 * 2) a MSGMARKNEXT to a plain message.
8454 * 3) a MSGNOTMARKNEXT to a plain message
8455 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
8456 * message.
8458 * Thus we never append a MSGMARKNEXT or
8459 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
8461 if (qfirst->b_flag & MSGMARKNEXT) {
8462 bp->b_flag |= MSGMARKNEXT;
8463 bp->b_flag &= ~MSGNOTMARKNEXT;
8464 qfirst->b_flag &= ~MSGMARKNEXT;
8465 } else if (qfirst->b_flag & MSGNOTMARKNEXT) {
8466 bp->b_flag |= MSGNOTMARKNEXT;
8467 qfirst->b_flag &= ~MSGNOTMARKNEXT;
8470 linkb(bp, qfirst);
8472 (void) putbq(q, bp);
8475 * A message may have come in when the sd_lock was dropped in the
8476 * calling routine. If this is the case and STR*ATMARK info was
8477 * received, need to move that from the stream head to the q_last
8478 * so that SIOCATMARK can return the proper value.
8480 if (stp->sd_flag & (STRATMARK | STRNOTATMARK)) {
8481 unsigned short *flagp = &q->q_last->b_flag;
8482 uint_t b_flag = (uint_t)*flagp;
8484 if (stp->sd_flag & STRATMARK) {
8485 b_flag &= ~MSGNOTMARKNEXT;
8486 b_flag |= MSGMARKNEXT;
8487 stp->sd_flag &= ~STRATMARK;
8488 } else {
8489 b_flag &= ~MSGMARKNEXT;
8490 b_flag |= MSGNOTMARKNEXT;
8491 stp->sd_flag &= ~STRNOTATMARK;
8493 *flagp = (unsigned short) b_flag;
8496 #ifdef DEBUG
8498 * Make sure that the flags are not messed up.
8501 mblk_t *mp;
8502 mp = q->q_last;
8503 while (mp != NULL) {
8504 ASSERT((mp->b_flag & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
8505 (MSGMARKNEXT|MSGNOTMARKNEXT));
8506 mp = mp->b_cont;
8509 #endif
8510 if (q->q_first == bp) {
8511 short pollevents;
8513 if (stp->sd_flag & RSLEEP) {
8514 stp->sd_flag &= ~RSLEEP;
8515 cv_broadcast(&q->q_wait);
8517 if (stp->sd_flag & STRPRI) {
8518 pollevents = POLLPRI;
8519 } else {
8520 if (band == 0) {
8521 if (!(stp->sd_rput_opt & SR_POLLIN))
8522 return;
8523 stp->sd_rput_opt &= ~SR_POLLIN;
8524 pollevents = POLLIN | POLLRDNORM;
8525 } else {
8526 pollevents = POLLIN | POLLRDBAND;
8529 mutex_exit(&stp->sd_lock);
8530 pollwakeup(&stp->sd_pollist, pollevents);
8531 mutex_enter(&stp->sd_lock);
8536 * Return the held vnode attached to the stream head of a
8537 * given queue
8538 * It is the responsibility of the calling routine to ensure
8539 * that the queue does not go away (e.g. pop).
8541 vnode_t *
8542 strq2vp(queue_t *qp)
8544 vnode_t *vp;
8545 vp = STREAM(qp)->sd_vnode;
8546 ASSERT(vp != NULL);
8547 VN_HOLD(vp);
8548 return (vp);
8552 * return the stream head write queue for the given vp
8553 * It is the responsibility of the calling routine to ensure
8554 * that the stream or vnode do not close.
8556 queue_t *
8557 strvp2wq(vnode_t *vp)
8559 ASSERT(vp->v_stream != NULL);
8560 return (vp->v_stream->sd_wrq);
8564 * pollwakeup stream head
8565 * It is the responsibility of the calling routine to ensure
8566 * that the stream or vnode do not close.
8568 void
8569 strpollwakeup(vnode_t *vp, short event)
8571 ASSERT(vp->v_stream);
8572 pollwakeup(&vp->v_stream->sd_pollist, event);
8576 * Mate the stream heads of two vnodes together. If the two vnodes are the
8577 * same, we just make the write-side point at the read-side -- otherwise,
8578 * we do a full mate. Only works on vnodes associated with streams that are
8579 * still being built and thus have only a stream head.
8581 void
8582 strmate(vnode_t *vp1, vnode_t *vp2)
8584 queue_t *wrq1 = strvp2wq(vp1);
8585 queue_t *wrq2 = strvp2wq(vp2);
8588 * Verify that there are no modules on the stream yet. We also
8589 * rely on the stream head always having a service procedure to
8590 * avoid tweaking q_nfsrv.
8592 ASSERT(wrq1->q_next == NULL && wrq2->q_next == NULL);
8593 ASSERT(wrq1->q_qinfo->qi_srvp != NULL);
8594 ASSERT(wrq2->q_qinfo->qi_srvp != NULL);
8597 * If the queues are the same, just twist; otherwise do a full mate.
8599 if (wrq1 == wrq2) {
8600 wrq1->q_next = _RD(wrq1);
8601 } else {
8602 wrq1->q_next = _RD(wrq2);
8603 wrq2->q_next = _RD(wrq1);
8604 STREAM(wrq1)->sd_mate = STREAM(wrq2);
8605 STREAM(wrq1)->sd_flag |= STRMATE;
8606 STREAM(wrq2)->sd_mate = STREAM(wrq1);
8607 STREAM(wrq2)->sd_flag |= STRMATE;
8612 * XXX will go away when console is correctly fixed.
8613 * Clean up the console PIDS, from previous I_SETSIG,
8614 * called only for cnopen which never calls strclean().
8616 void
8617 str_cn_clean(struct vnode *vp)
8619 strsig_t *ssp, *pssp, *tssp;
8620 struct stdata *stp;
8621 struct pid *pidp;
8622 int update = 0;
8624 ASSERT(vp->v_stream);
8625 stp = vp->v_stream;
8626 pssp = NULL;
8627 mutex_enter(&stp->sd_lock);
8628 ssp = stp->sd_siglist;
8629 while (ssp) {
8630 mutex_enter(&pidlock);
8631 pidp = ssp->ss_pidp;
8633 * Get rid of PID if the proc is gone.
8635 if (pidp->pid_prinactive) {
8636 tssp = ssp->ss_next;
8637 if (pssp)
8638 pssp->ss_next = tssp;
8639 else
8640 stp->sd_siglist = tssp;
8641 ASSERT(pidp->pid_ref <= 1);
8642 PID_RELE(ssp->ss_pidp);
8643 mutex_exit(&pidlock);
8644 kmem_free(ssp, sizeof (strsig_t));
8645 update = 1;
8646 ssp = tssp;
8647 continue;
8648 } else
8649 mutex_exit(&pidlock);
8650 pssp = ssp;
8651 ssp = ssp->ss_next;
8653 if (update) {
8654 stp->sd_sigflags = 0;
8655 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
8656 stp->sd_sigflags |= ssp->ss_events;
8658 mutex_exit(&stp->sd_lock);
8662 * Return B_TRUE if there is data in the message, B_FALSE otherwise.
8664 static boolean_t
8665 msghasdata(mblk_t *bp)
8667 for (; bp; bp = bp->b_cont)
8668 if (bp->b_datap->db_type == M_DATA) {
8669 ASSERT(bp->b_wptr >= bp->b_rptr);
8670 if (bp->b_wptr > bp->b_rptr)
8671 return (B_TRUE);
8673 return (B_FALSE);