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]
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>
39 #include <sys/vnode.h>
41 #include <sys/stream.h>
42 #include <sys/strsubr.h>
43 #include <sys/stropts.h>
44 #include <sys/tihdr.h>
47 #include <sys/termio.h>
48 #include <sys/ttold.h>
49 #include <sys/systm.h>
51 #include <sys/cmn_err.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>
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>
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>
80 #include <sys/limits.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 : \
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
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
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
,
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
190 * 4. ioctl monitor: sd_lock is gotten to ensure that only one
191 * thread is doing an ioctl at a time.
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
)
201 if (stp
->sd_flag
& (STRHUP
|STRDERR
|STWRERR
)) {
202 error
= (stp
->sd_flag
& STRHUP
) ? ENXIO
: EIO
;
205 if (stp
->sd_pushcnt
>= nstrpush
) {
209 if ((fp
= fmodsw_find(name
, FMODSW_HOLD
| FMODSW_LOAD
)) == NULL
) {
210 stp
->sd_flag
|= STREOPENFAIL
;
215 * push new module and call its open routine via qattach
217 if ((error
= qattach(qp
, devp
, 0, crp
, fp
, B_FALSE
)) != 0)
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
);
235 * Open a stream device.
238 stropen(vnode_t
*vp
, dev_t
*devp
, int flag
, cred_t
*crp
)
243 dev_t dummydev
, savedev
;
245 struct dlautopush dlap
;
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
);
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
);
272 struct stdata
*strmatep
= stp
->sd_mate
;
275 if (strmatep
->sd_flag
& (STWOPEN
|STRCLOSE
|STRPLUMB
)) {
276 if (flag
& (FNDELAY
|FNONBLOCK
)) {
278 mutex_exit(&strmatep
->sd_lock
);
281 mutex_exit(&stp
->sd_lock
);
282 if (!cv_wait_sig(&strmatep
->sd_monitor
,
283 &strmatep
->sd_lock
)) {
285 mutex_exit(&strmatep
->sd_lock
);
286 mutex_enter(&stp
->sd_lock
);
289 mutex_exit(&strmatep
->sd_lock
);
292 if (stp
->sd_flag
& (STWOPEN
|STRCLOSE
|STRPLUMB
)) {
293 if (flag
& (FNDELAY
|FNONBLOCK
)) {
295 mutex_exit(&strmatep
->sd_lock
);
298 mutex_exit(&strmatep
->sd_lock
);
299 if (!cv_wait_sig(&stp
->sd_monitor
,
304 mutex_exit(&stp
->sd_lock
);
308 if (stp
->sd_flag
& (STRDERR
|STWRERR
)) {
310 mutex_exit(&strmatep
->sd_lock
);
314 stp
->sd_flag
|= STWOPEN
;
317 mutex_enter(&stp
->sd_lock
);
318 if (stp
->sd_flag
& (STWOPEN
|STRCLOSE
|STRPLUMB
)) {
319 if (flag
& (FNDELAY
|FNONBLOCK
)) {
323 if (!cv_wait_sig(&stp
->sd_monitor
,
328 mutex_exit(&stp
->sd_lock
);
329 goto retry
; /* could be clone! */
332 if (stp
->sd_flag
& (STRDERR
|STWRERR
)) {
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
);
349 while (_SAMESTR(qp
)) {
351 if ((error
= qreopen(_RD(qp
), devp
, flag
, crp
)) != 0)
354 releasestr(stp
->sd_wrq
);
355 mutex_enter(&stp
->sd_lock
);
356 stp
->sd_flag
&= ~(STRHUP
|STWOPEN
|STRDERR
|STWRERR
);
360 cv_broadcast(&stp
->sd_monitor
);
361 mutex_exit(&stp
->sd_lock
);
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.
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;
383 stp
->sd_closetime
= STRTIMOUT
;
385 stp
->sd_pgidp
= NULL
;
391 stp
->sd_iocblk
= NULL
;
392 stp
->sd_cmdblk
= NULL
;
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
;
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
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
);
417 stp
->sd_strtab
= fifo_getinfo();
418 _WR(qp
)->q_nfsrv
= _WR(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
429 stp
->sd_qn_minpsz
= qp
->q_minpsz
;
430 stp
->sd_qn_maxpsz
= qp
->q_maxpsz
;
431 stp
->sd_flag
&= ~STWOPEN
;
434 /* setq might sleep in kmem_alloc - avoid holding locks. */
435 setq(qp
, &strdata
, &stwdata
, NULL
, QMTSAFE
, SQ_CI
|SQ_CO
, B_FALSE
);
440 * Open driver and create stream to it (via qattach).
443 cloneopen
= (getmajor(*devp
) == clone_major
);
444 if ((error
= qattach(qp
, devp
, flag
, crp
, NULL
, B_FALSE
)) != 0) {
445 mutex_enter(&vp
->v_lock
);
447 mutex_exit(&vp
->v_lock
);
448 mutex_enter(&stp
->sd_lock
);
449 cv_broadcast(&stp
->sd_monitor
);
450 mutex_exit(&stp
->sd_lock
);
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.
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
480 if (!NETWORK_DRV(major
)) {
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
)) {
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
,
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
;
524 ss
= netstack_find_by_stackid(zoneid_to_netstackid(zoneid
))->
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
)
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
);
547 netstack_rele(ss
->ss_netstack
);
552 * let specfs know that open failed part way through
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 */
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
));
590 cv_broadcast(&stp
->sd_monitor
);
591 mutex_exit(&stp
->sd_lock
);
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
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
613 strclose(struct vnode
*vp
, int flag
, cred_t
*crp
)
621 TRACE_1(TR_FAC_STREAMS_FR
,
622 TR_STRCLOSE
, "strclose:%p", vp
);
623 ASSERT(vp
->v_stream
);
626 ASSERT(!(stp
->sd_flag
& STPLEX
));
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
643 if (stp
->sd_siglist
!= NULL
)
646 ASSERT(stp
->sd_siglist
== NULL
);
647 ASSERT(stp
->sd_sigflags
== 0);
650 struct stdata
*strmatep
= stp
->sd_mate
;
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
);
663 while (strmatep
->sd_flag
&
664 (STWOPEN
|STRCLOSE
|STRPLUMB
)) {
665 mutex_exit(&stp
->sd_lock
);
666 cv_wait(&strmatep
->sd_monitor
,
668 mutex_exit(&strmatep
->sd_lock
);
673 stp
->sd_flag
|= STRCLOSE
;
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
) {
688 ns
= netstack_find_by_cred(crp
);
690 ss
= ns
->netstack_str
;
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
702 mutex_enter(&stp
->sd_lock
);
703 if (!(flag
& (FNDELAY
|FNONBLOCK
)) && (stp
->sd_closetime
> 0)) {
706 * sleep until awakened by strwsrv() or timeout
709 mutex_enter(QLOCK(qp
->q_next
));
710 if (!(qp
->q_next
->q_mblkcnt
)) {
711 mutex_exit(QLOCK(qp
->q_next
));
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) {
725 stp
->sd_flag
&= ~WSLEEP
;
727 mutex_exit(&stp
->sd_lock
);
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.
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
);
781 } else if (qp
->q_next
== _RD(qp
)) { /* fifo */
786 * The q_info pointers are never accessed when
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
));
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
;
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
);
822 strsink(queue_t
*q
, mblk_t
*bp
)
824 struct copyresp
*resp
;
826 switch (bp
->b_datap
->db_type
) {
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
)
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
)
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
)
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.
895 strclean(struct vnode
*vp
)
897 strsig_t
*ssp
, *pssp
, *tssp
;
901 TRACE_1(TR_FAC_STREAMS_FR
,
902 TR_STRCLEAN
, "strclean:%p", vp
);
905 mutex_enter(&stp
->sd_lock
);
906 ssp
= stp
->sd_siglist
;
908 if (ssp
->ss_pidp
== curproc
->p_pidp
) {
911 pssp
->ss_next
= tssp
;
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
));
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).
940 strcleanall(struct vnode
*vp
)
942 strsig_t
*ssp
, *nssp
;
946 mutex_enter(&stp
->sd_lock
);
947 ssp
= stp
->sd_siglist
;
948 stp
->sd_siglist
= NULL
;
951 mutex_enter(&pidlock
);
952 PID_RELE(ssp
->ss_pidp
);
953 mutex_exit(&pidlock
);
954 kmem_free(ssp
, sizeof (strsig_t
));
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.
969 strget(struct stdata
*stp
, queue_t
*q
, struct uio
*uiop
, int first
,
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).
989 struct iovec buf
[IOV_MAX_STACK
];
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
);
1002 (void) uiodup(uiop
, &uiod
.d_uio
, uiod
.d_iov
, uiop
->uio_iovcnt
);
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
;
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
)
1031 mutex_exit(&stp
->sd_lock
);
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
) {
1040 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1042 kmem_free(uiod
.d_iov
, iovlen
);
1046 } else if (error
== EINVAL
) {
1048 * The stream plumbing must have
1049 * changed while we were away, so
1050 * just turn off rwnext()s.
1053 } else if (error
== EBUSY
) {
1055 * The module might have data in transit using putnext
1056 * Fall back on waiting + getq.
1061 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1063 kmem_free(uiod
.d_iov
, iovlen
);
1068 kmem_free(uiod
.d_iov
, iovlen
);
1071 * Try a getq in case a rwnext() generated mblk
1072 * has bubbled up via strrput().
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.
1104 struiocopyout(mblk_t
*bp
, struct uio
*uiop
, int *errorp
)
1110 ASSERT(bp
->b_wptr
>= bp
->b_rptr
);
1113 if ((n
= MIN(uiop
->uio_resid
, MBLKL(bp
))) != 0) {
1116 error
= uiomove(bp
->b_rptr
, n
, UIO_READ
, uiop
);
1125 while (bp
!= NULL
&& (bp
->b_rptr
>= bp
->b_wptr
)) {
1130 } while (bp
!= NULL
&& uiop
->uio_resid
> 0);
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.
1165 strread(struct vnode
*vp
, struct uio
*uiop
, cred_t
*crp
)
1174 uint_t mark
; /* Contains MSG*MARK and _LASTMARK */
1175 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
1177 unsigned char pri
= 0;
1181 TRACE_1(TR_FAC_STREAMS_FR
,
1182 TR_STRREAD_ENTER
, "strread:%p", vp
);
1183 ASSERT(vp
->v_stream
);
1186 mutex_enter(&stp
->sd_lock
);
1188 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
1189 mutex_exit(&stp
->sd_lock
);
1193 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
1194 error
= strgeterr(stp
, STRDERR
|STPLEX
, 0);
1196 mutex_exit(&stp
->sd_lock
);
1202 * Loop terminates when uiop->uio_resid == 0.
1205 waitflag
= READWAIT
;
1206 q
= _RD(stp
->sd_wrq
);
1208 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1209 old_sd_flag
= stp
->sd_flag
;
1213 while ((bp
= strget(stp
, q
, uiop
, first
, &error
)) == NULL
) {
1216 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1221 if (stp
->sd_flag
& (STRHUP
|STREOF
)) {
1224 if (rflg
&& !(stp
->sd_flag
& STRDELIM
)) {
1228 * If a read(fd,buf,0) has been done, there is no
1229 * need to sleep. We always have zero bytes to
1232 if (uiop
->uio_resid
== 0) {
1238 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRREAD_WAIT
,
1239 "strread calls strwaitq:%p, %p, %p",
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",
1246 if ((uiop
->uio_fmode
& FNDELAY
) &&
1247 (stp
->sd_flag
& OLDNDELAY
) &&
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) {
1260 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1264 * Extract any mark information. If the message is not
1265 * completely consumed this information will be put in the mblk
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
1275 mark
= bp
->b_flag
& (MSGMARK
| MSGMARKNEXT
| MSGNOTMARKNEXT
);
1276 ASSERT((mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
)) !=
1277 (MSGMARKNEXT
|MSGNOTMARKNEXT
));
1278 if (mark
!= 0 && bp
== stp
->sd_mark
) {
1280 putback(stp
, q
, bp
, pri
);
1284 stp
->sd_mark
= NULL
;
1286 if ((stp
->sd_flag
& STRDELIM
) && (bp
->b_flag
& MSGDELIM
))
1288 mutex_exit(&stp
->sd_lock
);
1290 if (STREAM_NEEDSERVICE(stp
))
1291 stream_runservice(stp
);
1293 type
= bp
->b_datap
->db_type
;
1299 if (msgnodata(bp
)) {
1300 if (mark
|| delim
) {
1305 * If already read data put zero
1306 * length message back on queue else
1307 * free msg and return 0.
1310 mutex_enter(&stp
->sd_lock
);
1311 putback(stp
, q
, bp
, pri
);
1312 mutex_exit(&stp
->sd_lock
);
1322 bp
= struiocopyout(bp
, uiop
, &error
);
1326 mutex_enter(&stp
->sd_lock
);
1329 * Have remaining data in message.
1330 * Free msg if in discard mode.
1332 if (stp
->sd_read_opt
& RD_MSGDIS
) {
1336 if ((mark
& _LASTMARK
) &&
1337 (stp
->sd_mark
== NULL
))
1339 bp
->b_flag
|= mark
& ~_LASTMARK
;
1341 bp
->b_flag
|= MSGDELIM
;
1345 putback(stp
, q
, bp
, pri
);
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
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
;
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
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
);
1397 if (STREAM_NEEDSERVICE(stp
))
1398 stream_runservice(stp
);
1399 mutex_enter(&stp
->sd_lock
);
1402 if ((uiop
->uio_resid
== 0) || (mark
& _LASTMARK
) ||
1404 (stp
->sd_read_opt
& (RD_MSGDIS
|RD_MSGNODIS
))) {
1410 strsignal(stp
, *bp
->b_rptr
, (int32_t)bp
->b_band
);
1412 mutex_enter(&stp
->sd_lock
);
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
==
1426 (nbp
->b_datap
->db_type
==
1428 nbp
->b_datap
->db_type
= M_DATA
;
1434 * clear stream head hi pri flag based on
1437 if (type
== M_PCPROTO
) {
1438 mutex_enter(&stp
->sd_lock
);
1439 stp
->sd_flag
&= ~STRPRI
;
1440 mutex_exit(&stp
->sd_lock
);
1443 } else if (stp
->sd_read_opt
& RD_PROTDIS
) {
1445 * discard non-data messages
1448 ((bp
->b_datap
->db_type
== M_PROTO
) ||
1449 (bp
->b_datap
->db_type
== M_PCPROTO
))) {
1455 * clear stream head hi pri flag based on
1458 if (type
== M_PCPROTO
) {
1459 mutex_enter(&stp
->sd_lock
);
1460 stp
->sd_flag
&= ~STRPRI
;
1461 mutex_exit(&stp
->sd_lock
);
1472 if ((bp
->b_datap
->db_type
== M_PASSFP
) &&
1473 (stp
->sd_read_opt
& RD_PROTDIS
)) {
1477 mutex_enter(&stp
->sd_lock
);
1478 putback(stp
, q
, bp
, pri
);
1479 mutex_exit(&stp
->sd_lock
);
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
);
1493 mutex_enter(&stp
->sd_lock
);
1496 mutex_exit(&stp
->sd_lock
);
1498 qbackenable(q
, pri
);
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
1511 strrput_proto(vnode_t
*vp
, mblk_t
*mp
,
1512 strwakeup_t
*wakeups
, strsigset_t
*firstmsgsigs
,
1513 strsigset_t
*allmsgsigs
, strpollset_t
*pollwakeups
)
1518 switch (mp
->b_datap
->db_type
) {
1520 if (mp
->b_band
== 0) {
1521 *firstmsgsigs
= S_INPUT
| S_RDNORM
;
1522 *pollwakeups
= POLLIN
| POLLRDNORM
;
1524 *firstmsgsigs
= S_INPUT
| S_RDBAND
;
1525 *pollwakeups
= POLLIN
| POLLRDBAND
;
1529 *firstmsgsigs
= S_HIPRI
;
1530 *pollwakeups
= POLLPRI
;
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
1545 strrput_misc(vnode_t
*vp
, mblk_t
*mp
,
1546 strwakeup_t
*wakeups
, strsigset_t
*firstmsgsigs
,
1547 strsigset_t
*allmsgsigs
, strpollset_t
*pollwakeups
)
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
)
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
;
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
) {
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
) {
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
;
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
1632 if (q
->q_last
!= NULL
) {
1633 q
->q_last
->b_flag
&= ~MSGMARKNEXT
;
1634 q
->q_last
->b_flag
|= MSGNOTMARKNEXT
;
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
);
1649 if (bp
->b_band
== 0) {
1650 firstmsgsigs
= S_INPUT
| S_RDNORM
;
1651 pollwakeups
= POLLIN
| POLLRDNORM
;
1653 firstmsgsigs
= S_INPUT
| S_RDBAND
;
1654 pollwakeups
= POLLIN
| POLLRDBAND
;
1656 if (rput_opt
& SR_SIGALLDATA
)
1657 allmsgsigs
= firstmsgsigs
;
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
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
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
;
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.
1725 if (bp
->b_band
== 0) {
1726 firstmsgsigs
= S_INPUT
| S_RDNORM
;
1727 pollwakeups
= POLLIN
| POLLRDNORM
;
1729 firstmsgsigs
= S_INPUT
| S_RDBAND
;
1730 pollwakeups
= POLLIN
| POLLRDBAND
;
1732 mutex_enter(&stp
->sd_lock
);
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|\
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
);
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);
1763 mutex_enter(&stp
->sd_lock
);
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.
1780 nextbp
= bp
->b_next
;
1783 switch (bp
->b_datap
->db_type
) {
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");
1793 mutex_exit(&stp
->sd_lock
);
1796 stp
->sd_flag
|= STRPRI
;
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
1810 if (bp
->b_flag
& MSGMARK
)
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
);
1828 signals
= allmsgsigs
;
1832 mutex_exit(&stp
->sd_lock
);
1833 (void) strrput_nondata(q
, bp
);
1834 mutex_enter(&stp
->sd_lock
);
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
))
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
);
1873 * strsendsig can handle multiple signals with a
1876 if (stp
->sd_sigflags
& signals
)
1877 strsendsig(stp
->sd_siglist
, signals
, band
, 0);
1878 mutex_exit(&stp
->sd_lock
);
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.
1892 signals
= firstmsgsigs
= allmsgsigs
= 0;
1893 mutex_enter(&stp
->sd_lock
);
1898 log_dupioc(queue_t
*rq
, mblk_t
*bp
)
1901 char *modnames
, *mnp
, *dname
;
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 */
1916 /* make sure q_next doesn't shift around while we're grabbing data */
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.
1933 mnp
+= snprintf(mnp
, FMNAMESZ
+ 1, "%s", dname
);
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
);
1948 kmem_free(modnames
, maxmodstr
);
1952 strrput_nondata(queue_t
*q
, mblk_t
*bp
)
1955 struct iocblk
*iocbp
;
1956 struct stroptions
*sop
;
1957 struct copyreq
*reqp
;
1958 struct copyresp
*resp
;
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
) {
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
;
1984 stp
->sd_flag
&= ~STRDERR
;
1986 stp
->sd_rerror
= *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
;
1996 stp
->sd_flag
&= ~STWRERR
;
1998 stp
->sd_werror
= *bp
->b_rptr
;
2001 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRRPUT_WAKE
,
2002 "strrput cv_broadcast:q %p, bp %p",
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
:
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
) {
2027 bp
->b_datap
->db_type
= M_FLUSH
;
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
)
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
,
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
:
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
2079 if (_OTHERQ(q
)->q_next
== NULL
)
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
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 */
2115 mutex_exit(&stp
->sd_lock
);
2120 mutex_enter(&stp
->sd_lock
);
2122 stp
->sd_flag
&= ~STRHUP
;
2123 mutex_exit(&stp
->sd_lock
);
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
))) {
2142 mutex_exit(&stp
->sd_lock
);
2145 mutex_exit(&stp
->sd_lock
);
2150 * Don't enqueue, just post the signal.
2152 strsignal(stp
, *bp
->b_rptr
, 0L);
2157 if (MBLKL(bp
) != sizeof (cmdblk_t
)) {
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
);
2169 mutex_exit(&stp
->sd_lock
);
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
);
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
;
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
)
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
)) {
2243 mutex_exit(&stp
->sd_lock
);
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
);
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
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
)) {
2269 freemsg(bp
->b_cont
);
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
);
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
);
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).
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
) {
2302 stp
->sd_read_opt
&= ~(RD_MSGDIS
| RD_MSGNODIS
);
2307 ((stp
->sd_read_opt
& ~RD_MSGNODIS
) |
2313 ((stp
->sd_read_opt
& ~RD_MSGDIS
) |
2317 switch (sop
->so_readopt
& RPROTMASK
) {
2319 stp
->sd_read_opt
&= ~(RD_PROTDAT
| RD_PROTDIS
);
2324 ((stp
->sd_read_opt
& ~RD_PROTDIS
) |
2330 ((stp
->sd_read_opt
& ~RD_PROTDAT
) |
2334 switch (sop
->so_readopt
& RFLUSHMASK
) {
2337 * This sets the stream head to NOT flush
2338 * M_PCPROTO messages.
2340 stp
->sd_read_opt
|= RFLUSHPCPROT
;
2344 if (sop
->so_flags
& SO_ERROPT
) {
2345 switch (sop
->so_erropt
& RERRMASK
) {
2347 stp
->sd_flag
&= ~STRDERRNONPERSIST
;
2349 case RERRNONPERSIST
:
2350 stp
->sd_flag
|= STRDERRNONPERSIST
;
2353 switch (sop
->so_erropt
& WERRMASK
) {
2355 stp
->sd_flag
&= ~STWRERRNONPERSIST
;
2357 case WERRNONPERSIST
:
2358 stp
->sd_flag
|= STWRERRNONPERSIST
;
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");
2392 bpri
= sop
->so_band
;
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");
2405 bpri
= sop
->so_band
;
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
);
2435 /* Check backenable in case the water marks changed */
2436 qbackenable(q
, bpri
);
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.
2457 * Always NAK this condition
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
2469 mblk_t
*mp
= stp
->sd_struionak
;
2471 while (mp
&& mp
->b_next
)
2476 stp
->sd_struionak
= bp
;
2478 mutex_exit(&stp
->sd_lock
);
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
)
2497 "bad message type %x received at stream head\n",
2498 bp
->b_datap
->db_type
);
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.
2514 strwriteable(struct stdata
*stp
, boolean_t eiohup
, boolean_t sigpipeok
)
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
)
2527 error
= strgeterr(stp
, STRHUP
|STPLEX
|STWRERR
, 0);
2530 if (!(stp
->sd_flag
& STPLEX
) &&
2531 (stp
->sd_wput_opt
& SW_SIGPIPE
) && sigpipeok
) {
2532 tsignal(curthread
, SIGPIPE
);
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
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.
2560 strput(struct stdata
*stp
, mblk_t
*mctl
, struct uio
*uiop
, ssize_t
*iosize
,
2561 int b_flag
, int pri
, int flags
)
2564 struct iovec buf
[IOV_MAX_STACK
];
2567 queue_t
*wqp
= stp
->sd_wrq
;
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.
2581 if (!canputnext(wqp
) && !(flags
& MSG_IGNFLOW
)) {
2583 return (EWOULDBLOCK
);
2586 if (!(flags
& MSG_IGNFLOW
) && !bcanputnext(wqp
, pri
)) {
2588 return (EWOULDBLOCK
);
2592 if ((error
= strmakedata(iosize
, uiop
, stp
, flags
,
2596 * need to change return code to ENOMEM
2597 * so that this is not confused with
2598 * flow control, EAGAIN.
2601 if (error
== EAGAIN
)
2607 if (mctl
->b_cont
== NULL
)
2609 else if (mp
!= NULL
)
2612 } else if (mp
== NULL
)
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.
2628 stream_willservice(stp
);
2630 stream_runservice(stp
);
2635 * Stream supports rwnext() for the write side.
2637 if ((error
= strmakedata(iosize
, uiop
, stp
, flags
, &mp
)) != 0) {
2640 * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
2642 return (error
== EAGAIN
? ENOMEM
: error
);
2645 if (mctl
->b_cont
== NULL
)
2647 else if (mp
!= NULL
)
2650 } else if (mp
== NULL
) {
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
);
2664 (void) uiodup(uiop
, &uiod
.d_uio
, uiod
.d_iov
, uiop
->uio_iovcnt
);
2665 uiod
.d_uio
.uio_offset
= 0;
2667 error
= rwnext(wqp
, &uiod
);
2669 uioskip(uiop
, *iosize
);
2671 kmem_free(uiod
.d_iov
, iovlen
);
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.
2681 } else if (error
== EBUSY
|| error
== EWOULDBLOCK
) {
2683 * Couldn't enter a perimeter or took a page fault,
2684 * so fall-back to putnext().
2690 kmem_free(uiod
.d_iov
, iovlen
);
2693 /* Have to check canput before consuming data from the uio */
2695 if (!canputnext(wqp
) && !(flags
& MSG_IGNFLOW
)) {
2698 kmem_free(uiod
.d_iov
, iovlen
);
2699 return (EWOULDBLOCK
);
2702 if (!bcanputnext(wqp
, pri
) && !(flags
& MSG_IGNFLOW
)) {
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) {
2714 kmem_free(uiod
.d_iov
, iovlen
);
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.
2727 stream_willservice(stp
);
2729 stream_runservice(stp
);
2732 kmem_free(uiod
.d_iov
, iovlen
);
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.
2757 strwrite(struct vnode
*vp
, struct uio
*uiop
, cred_t
*crp
)
2759 return (strwrite_common(vp
, uiop
, crp
, 0));
2764 strwrite_common(struct vnode
*vp
, struct uio
*uiop
, cred_t
*crp
, int wflag
)
2775 ASSERT(vp
->v_stream
);
2778 mutex_enter(&stp
->sd_lock
);
2780 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
2781 mutex_exit(&stp
->sd_lock
);
2785 if (stp
->sd_flag
& (STWRERR
|STRHUP
|STPLEX
)) {
2786 error
= strwriteable(stp
, B_TRUE
, B_TRUE
);
2788 mutex_exit(&stp
->sd_lock
);
2793 mutex_exit(&stp
->sd_lock
);
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
));
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",
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",
2828 * Do until count satisfied or error.
2830 waitflag
= WRITEWAIT
| wflag
;
2831 if (stp
->sd_flag
& OLDNDELAY
)
2832 tempmode
= uiop
->uio_fmode
& ~FNDELAY
;
2834 tempmode
= uiop
->uio_fmode
;
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
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
))
2864 error
= strput(stp
, NULL
, uiop
, &iosize
, b_flag
, 0, 0);
2867 if (error
!= EWOULDBLOCK
)
2870 mutex_enter(&stp
->sd_lock
);
2872 * Check for a missed wakeup.
2873 * Needed since strput did not hold sd_lock across
2876 if (canputnext(wqp
)) {
2878 mutex_exit(&stp
->sd_lock
);
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
) &&
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
);
2898 mutex_exit(&stp
->sd_lock
);
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
);
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
)
2924 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRWRITE_OUT
,
2925 "strwrite out:q %p out %d error %d", wqp
, 2, 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.
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
) {
2966 /* Find the next module forward that has a service procedure */
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
));
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
));
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
);
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
) {
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
;
3016 myqbp
= myqbp
->qb_next
;
3019 mutex_exit(QLOCK(tq
));
3022 for (i
= tq
->q_nband
; i
; 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
,
3029 mutex_exit(&stp
->sd_lock
);
3039 * Special case of strcopyin/strcopyout for copying
3040 * struct strioctl that can deal with both data
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
)))
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
))) {
3073 ASSERT(copyflag
& K_TO_K
);
3074 bcopy(from
, to
, sizeof (struct strioctl
));
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
==
3097 if (copyout(&strioc32
, to
, sizeof (strioc32
)))
3100 } else { /* NATIVE data model */
3101 if (copyout(from
, to
, sizeof (struct strioctl
))) {
3108 ASSERT(copyflag
& K_TO_K
);
3109 bcopy(from
, to
, sizeof (struct strioctl
));
3118 strcopyin_strioctl(void *from
, void *to
, int flag
, int copyflag
)
3120 return (strcopyin(from
, to
, sizeof (struct strioctl
), copyflag
));
3125 strcopyout_strioctl(void *from
, void *to
, int flag
, int copyflag
)
3127 return (strcopyout(from
, to
, sizeof (struct strioctl
), copyflag
));
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
3146 job_control_type(int cmd
)
3166 case TCDSET
: /* Obsolete */
3197 case LDSMAP
: /* Obsolete */
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 */
3227 strioctl(struct vnode
*vp
, int cmd
, intptr_t arg
, int flag
, int copyflag
,
3228 cred_t
*crp
, int *rvalp
)
3232 struct strioctl strioc
;
3242 boolean_t kioctl
= B_FALSE
;
3243 uint32_t auditing
= AU_AUDITING();
3245 if (flag
& FKIOCTL
) {
3249 ASSERT(vp
->v_stream
);
3250 ASSERT(copyflag
== U_TO_K
|| copyflag
== K_TO_K
);
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
3261 if (copyflag
== K_TO_K
)
3262 flag
= (flag
& ~FMODELS
) | FNATIVE
;
3264 ASSERT((flag
& FMODELS
) != 0);
3269 access
= job_control_type(cmd
);
3271 /* We should never see these here, should be handled by iwscn */
3272 if (cmd
== SRIOCSREDIR
|| cmd
== SRIOCISREDIR
)
3275 mutex_enter(&stp
->sd_lock
);
3276 if ((access
!= -1) && ((error
= i_straccess(stp
, access
)) != 0)) {
3277 mutex_exit(&stp
->sd_lock
);
3280 mutex_exit(&stp
->sd_lock
);
3283 * Check for sgttyb-related ioctls first, and complain as
3290 if (sgttyb_handling
>= 2 && !sgttyb_complaint
) {
3291 sgttyb_complaint
= B_TRUE
;
3293 "application used obsolete TIOC[GS]ET");
3295 if (sgttyb_handling
>= 3) {
3296 tsignal(curthread
, SIGSYS
);
3302 mutex_enter(&stp
->sd_lock
);
3314 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
3315 error
= strgeterr(stp
, STRDERR
|STPLEX
, 0);
3317 mutex_exit(&stp
->sd_lock
);
3324 if (stp
->sd_flag
& (STRDERR
|STWRERR
|STPLEX
)) {
3325 error
= strgeterr(stp
, STRDERR
|STWRERR
|STPLEX
, 0);
3327 mutex_exit(&stp
->sd_lock
);
3333 mutex_exit(&stp
->sd_lock
);
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
)
3372 strioc
.ic_cmd
= cmd
;
3373 strioc
.ic_timout
= INFTIM
;
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
));
3392 strioc
.ic_len
= sizeof (struct termio
);
3393 strioc
.ic_dp
= (char *)arg
;
3394 return (strdoioctl(stp
, &strioc
, flag
,
3395 copyflag
, crp
, rvalp
));
3400 strioc
.ic_len
= sizeof (struct termios
);
3401 strioc
.ic_dp
= (char *)arg
;
3402 return (strdoioctl(stp
, &strioc
, flag
,
3403 copyflag
, crp
, rvalp
));
3406 strioc
.ic_len
= sizeof (struct termcb
);
3407 strioc
.ic_dp
= (char *)arg
;
3408 return (strdoioctl(stp
, &strioc
, flag
,
3409 copyflag
, crp
, rvalp
));
3412 strioc
.ic_len
= sizeof (struct sgttyb
);
3413 strioc
.ic_dp
= (char *)arg
;
3414 return (strdoioctl(stp
, &strioc
, flag
,
3415 copyflag
, crp
, rvalp
));
3418 if ((flag
& FREAD
) == 0 &&
3419 secpolicy_sti(crp
) != 0) {
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
);
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
));
3439 strioc
.ic_len
= sizeof (struct winsize
);
3440 strioc
.ic_dp
= (char *)arg
;
3441 return (strdoioctl(stp
, &strioc
, flag
,
3442 copyflag
, crp
, rvalp
));
3445 strioc
.ic_len
= sizeof (struct ttysize
);
3446 strioc
.ic_dp
= (char *)arg
;
3447 return (strdoioctl(stp
, &strioc
, flag
,
3448 copyflag
, crp
, rvalp
));
3461 strioc
.ic_len
= sizeof (int);
3462 strioc
.ic_dp
= (char *)arg
;
3463 return (strdoioctl(stp
, &strioc
, flag
,
3464 copyflag
, crp
, rvalp
));
3468 strioc
.ic_len
= sizeof (struct kiockey
);
3469 strioc
.ic_dp
= (char *)arg
;
3470 return (strdoioctl(stp
, &strioc
, flag
,
3471 copyflag
, crp
, rvalp
));
3475 strioc
.ic_len
= sizeof (struct kiockeymap
);
3476 strioc
.ic_dp
= (char *)arg
;
3477 return (strdoioctl(stp
, &strioc
, flag
,
3478 copyflag
, crp
, rvalp
));
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
));
3488 strioc
.ic_len
= sizeof (Ms_parms
);
3489 strioc
.ic_dp
= (char *)arg
;
3490 return (strdoioctl(stp
, &strioc
, flag
,
3491 copyflag
, crp
, rvalp
));
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.
3511 case KIOCGTRANSABLE
:
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
));
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
)
3556 error
= strcopyin_strioctl((void *)arg
, &strioc
, flag
,
3561 if ((strioc
.ic_len
< 0) || (strioc
.ic_timout
< -1))
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
);
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
) {
3590 case _I_GETPEERCRED
:
3595 error
= strdoioctl(stp
, &strioc
, flag
, copyflag
, crp
, rvalp
);
3597 error
= strcopyout_strioctl(&strioc
, (void *)arg
,
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
)
3610 if ((scp
= kmem_alloc(sizeof (strcmd_t
), KM_NOSLEEP
)) == NULL
)
3613 if (copyin((void *)arg
, scp
, sizeof (strcmd_t
))) {
3614 kmem_free(scp
, sizeof (strcmd_t
));
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
));
3625 mutex_exit(&stp
->sd_lock
);
3628 if ((error
= strdocmd(stp
, scp
, crp
)) == 0) {
3629 if (copyout(scp
, (void *)arg
, sizeof (strcmd_t
)))
3632 kmem_free(scp
, sizeof (strcmd_t
));
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.
3646 mutex_enter(QLOCK(rdq
));
3648 size
= msgdsize(rdq
->q_first
);
3649 for (mp
= rdq
->q_first
; mp
!= NULL
; mp
= mp
->b_next
)
3652 mutex_exit(QLOCK(rdq
));
3653 if (stp
->sd_struiordq
) {
3656 infod
.d_cmd
= INFOD_COUNT
;
3659 infod
.d_cmd
|= INFOD_FIRSTBYTES
;
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
),
3683 * Return number of bytes of data in all data messages
3684 * in queue in "arg".
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
) {
3698 infod
.d_cmd
= INFOD_BYTES
;
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
),
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.
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
) {
3735 infod
.d_cmd
= INFOD_BYTES
;
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
3747 *rvalp
= MIN(size
, INT_MAX
);
3756 char mname
[FMNAMESZ
+ 1];
3759 error
= (copyflag
& U_TO_K
? copyinstr
: copystr
)((void *)arg
,
3760 mname
, FMNAMESZ
+ 1, NULL
);
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");
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
) {
3782 if (strcmp(mname
, Q2NAME(q
)) == 0)
3785 releasestr(stp
->sd_wrq
);
3787 *rvalp
= (q
? 1 : 0);
3792 case __I_PUSH_NOCTTY
:
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];
3804 if (stp
->sd_flag
& STRHUP
)
3808 * Get module name and look up in fmodsw.
3810 error
= (copyflag
& U_TO_K
? copyinstr
: copystr
)((void *)arg
,
3811 mname
, FMNAMESZ
+ 1, NULL
);
3813 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
3815 if ((fp
= fmodsw_find(mname
, FMODSW_HOLD
| FMODSW_LOAD
)) ==
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
)) {
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
) {
3837 mutex_exit(&stp
->sd_lock
);
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
,
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) {
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
));
3888 mutex_exit(&stp
->sd_lock
);
3896 if (stp
->sd_flag
& STRHUP
)
3898 if (!wrq
->q_next
) /* for broken pipes */
3901 if (error
= strstartplumb(stp
, flag
, cmd
))
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
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
) {
3922 mutex_exit(&stp
->sd_lock
);
3923 if (stp
->sd_anchorzone
!= crgetzoneid(crp
))
3925 /* Audit and report error */
3926 return (secpolicy_ip_config(crp
, B_FALSE
));
3928 mutex_exit(&stp
->sd_lock
);
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
))) {
3937 qdetach(_RD(q
), 1, flag
, crp
, B_FALSE
);
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) {
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
) {
3971 stp
->sd_anchorzone
= 0;
3974 mutex_exit(&stp
->sd_lock
);
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
;
4006 * Do not allow the wildcard muxid. This ioctl is not
4007 * intended to find arbitrary link.
4013 ns
= netstack_find_by_cred(crp
);
4015 ss
= ns
->netstack_str
;
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
);
4026 if ((fd
= ufalloc(0)) == -1) {
4027 mutex_exit(&muxifier
);
4028 netstack_rele(ss
->ss_netstack
);
4031 fp
= linkp
->li_fpdown
;
4032 mutex_enter(&fp
->f_tlock
);
4034 mutex_exit(&fp
->f_tlock
);
4035 mutex_exit(&muxifier
);
4038 netstack_rele(ss
->ss_netstack
);
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];
4063 boolean_t is_insert
;
4065 STRUCT_INIT(strmodinsert
, flag
);
4066 if (stp
->sd_flag
& STRHUP
)
4070 if ((error
= secpolicy_net_config(crp
, B_FALSE
)) != 0)
4072 if (stp
->sd_anchor
!= 0 &&
4073 stp
->sd_anchorzone
!= crgetzoneid(crp
))
4076 error
= strcopyin((void *)arg
, STRUCT_BUF(strmodinsert
),
4077 STRUCT_SIZE(strmodinsert
), copyflag
);
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
);
4088 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
4090 if ((fp
= fmodsw_find(mod_name
, FMODSW_HOLD
| FMODSW_LOAD
)) ==
4094 if (error
= strstartplumb(stp
, flag
, cmd
)) {
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
4112 mutex_enter(&stp
->sd_lock
);
4113 if (stp
->sd_pushcnt
>= nstrpush
|| pos
< 0 ||
4114 pos
> stp
->sd_pushcnt
) {
4117 mutex_exit(&stp
->sd_lock
);
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
)) {
4131 mutex_exit(&stp
->sd_lock
);
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
);
4156 mutex_exit(&stp
->sd_lock
);
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.
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
) {
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
))
4199 mutex_exit(&stp
->sd_lock
);
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
);
4228 char mod_name
[FMNAMESZ
+ 1];
4229 boolean_t is_remove
;
4231 STRUCT_INIT(strmodremove
, flag
);
4232 if (stp
->sd_flag
& STRHUP
)
4236 if ((error
= secpolicy_net_config(crp
, B_FALSE
)) != 0)
4238 if (stp
->sd_anchor
!= 0 &&
4239 stp
->sd_anchorzone
!= crgetzoneid(crp
))
4242 error
= strcopyin((void *)arg
, STRUCT_BUF(strmodremove
),
4243 STRUCT_SIZE(strmodremove
), copyflag
);
4247 error
= (copyflag
& U_TO_K
? copyinstr
:
4248 copystr
)(STRUCT_FGETP(strmodremove
, mod_name
),
4249 mod_name
, FMNAMESZ
+ 1, NULL
);
4251 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
4253 if ((error
= strstartplumb(stp
, flag
, cmd
)) != 0)
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
);
4270 mutex_exit(&stp
->sd_lock
);
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
);
4284 mutex_exit(&stp
->sd_lock
);
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.
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) {
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)
4335 else if (pos
> (stp
->sd_pushcnt
- stp
->sd_anchor
+ 1))
4340 mutex_exit(&stp
->sd_lock
);
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
);
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
);
4363 stp
->sd_anchor
= stp
->sd_pushcnt
;
4364 stp
->sd_anchorzone
= crgetzoneid(crp
);
4365 mutex_exit(&stp
->sd_lock
);
4370 * Get name of first module downstream.
4371 * If no module, return an error.
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,
4388 * Link a multiplexor.
4390 return (mlink(vp
, cmd
, (int)arg
, crp
, rvalp
, 0));
4394 * Link a multiplexor: Call must originate from kernel.
4397 return (ldi_mlink_lh(vp
, cmd
, arg
, crp
, rvalp
));
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
;
4415 TRACE_1(TR_FAC_STREAMS_FR
,
4416 TR_I_UNLINK
, "I_UNLINK/I_PUNLINK:%p", stp
);
4417 if (vp
->v_type
== VFIFO
) {
4420 if (cmd
== I_UNLINK
)
4422 else /* I_PUNLINK */
4424 if (native_arg
== 0) {
4427 ns
= netstack_find_by_cred(crp
);
4429 ss
= ns
->netstack_str
;
4432 if (native_arg
== MUXID_ALL
)
4433 error
= munlinkall(stp
, type
, crp
, rvalp
, ss
);
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
);
4442 /* munlink drops the muxifier lock */
4443 error
= munlink(stp
, linkp
, type
, crp
, rvalp
, ss
);
4445 netstack_rele(ss
->ss_netstack
);
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
)
4463 if (putnextctl1(stp
->sd_wrq
, M_FLUSH
, (int)arg
)) {
4466 if (error
= strwaitbuf(1, BPRI_HI
)) {
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;
4480 strioc
.ic_dp
= NULL
;
4481 (void) strdoioctl(stp
, &strioc
, flag
, K_TO_K
, crp
, rvalp
);
4487 struct bandinfo binfo
;
4489 error
= strcopyin((void *)arg
, &binfo
, sizeof (binfo
),
4493 if (stp
->sd_flag
& STRHUP
)
4495 if (binfo
.bi_flag
& ~FLUSHRW
)
4497 while (!(mp
= allocb(2, BPRI_HI
))) {
4498 if (error
= strwaitbuf(2, BPRI_HI
))
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;
4514 strioc
.ic_dp
= NULL
;
4515 (void) strdoioctl(stp
, &strioc
, flag
, K_TO_K
, crp
, rvalp
);
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
))
4534 if ((arg
& (RMSGD
|RMSGN
)) == (RMSGD
|RMSGN
))
4537 mutex_enter(&stp
->sd_lock
);
4538 switch (arg
& RMODEMASK
) {
4540 stp
->sd_read_opt
&= ~(RD_MSGDIS
| RD_MSGNODIS
);
4543 stp
->sd_read_opt
= (stp
->sd_read_opt
& ~RD_MSGNODIS
) |
4547 stp
->sd_read_opt
= (stp
->sd_read_opt
& ~RD_MSGDIS
) |
4552 switch (arg
& RPROTMASK
) {
4554 stp
->sd_read_opt
&= ~(RD_PROTDAT
| RD_PROTDIS
);
4558 stp
->sd_read_opt
= ((stp
->sd_read_opt
& ~RD_PROTDIS
) |
4563 stp
->sd_read_opt
= ((stp
->sd_read_opt
& ~RD_PROTDAT
) |
4567 mutex_exit(&stp
->sd_lock
);
4572 * Get read option and return the value
4573 * to spot pointed to by arg
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),
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
))
4599 mutex_enter(&stp
->sd_lock
);
4600 switch (arg
& RERRMASK
) {
4602 stp
->sd_flag
&= ~STRDERRNONPERSIST
;
4604 case RERRNONPERSIST
:
4605 stp
->sd_flag
|= STRDERRNONPERSIST
;
4608 switch (arg
& WERRMASK
) {
4610 stp
->sd_flag
&= ~STWRERRNONPERSIST
;
4612 case WERRNONPERSIST
:
4613 stp
->sd_flag
|= STWRERRNONPERSIST
;
4616 mutex_exit(&stp
->sd_lock
);
4621 * Get error option and return the value
4622 * to spot pointed to by arg
4627 erropt
|= (stp
->sd_flag
& STRDERRNONPERSIST
) ? RERRNONPERSIST
:
4629 erropt
|= (stp
->sd_flag
& STWRERRNONPERSIST
) ? WERRNONPERSIST
:
4631 return (strcopyout(&erropt
, (void *)arg
, sizeof (int),
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
;
4646 pidp
= curproc
->p_pidp
;
4648 * Hold sd_lock to prevent traversal of sd_siglist while
4651 mutex_enter(&stp
->sd_lock
);
4652 for (ssp
= stp
->sd_siglist
; ssp
&& (ssp
->ss_pidp
!= pidp
);
4653 pssp
= ssp
, ssp
= ssp
->ss_next
)
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
);
4662 if ((arg
& S_BANDURG
) && !(arg
& S_RDBAND
)) {
4663 mutex_exit(&stp
->sd_lock
);
4668 * If proc not already registered, add it
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
;
4677 pssp
->ss_next
= ssp
;
4679 stp
->sd_siglist
= ssp
;
4680 mutex_enter(&pidlock
);
4682 mutex_exit(&pidlock
);
4688 ssp
->ss_events
= (int)arg
;
4691 * Remove proc from register list.
4694 mutex_enter(&pidlock
);
4696 mutex_exit(&pidlock
);
4698 pssp
->ss_next
= ssp
->ss_next
;
4700 stp
->sd_siglist
= ssp
->ss_next
;
4701 kmem_free(ssp
, sizeof (strsig_t
));
4703 mutex_exit(&stp
->sd_lock
);
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
);
4720 * Return (in arg) the current registration of events
4721 * for which the calling proc is to be signaled.
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
);
4736 mutex_exit(&stp
->sd_lock
);
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
;
4751 struct strsigset ss
;
4753 error
= strcopyin((void *)arg
, &ss
, sizeof (ss
), copyflag
);
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);
4770 mutex_enter(&pidlock
);
4774 proc
= pgfind(-pid
);
4778 mutex_exit(&pidlock
);
4782 pidp
= proc
->p_pgidp
;
4784 pidp
= proc
->p_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.
4792 mutex_exit(&pidlock
);
4796 * Hold sd_lock to prevent traversal of sd_siglist while
4799 mutex_enter(&stp
->sd_lock
);
4800 for (ssp
= stp
->sd_siglist
; ssp
&& (ssp
->ss_pid
!= pid
);
4801 pssp
= ssp
, ssp
= ssp
->ss_next
)
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
);
4811 mutex_exit(&pidlock
);
4814 if ((ss
.ss_events
& S_BANDURG
) &&
4815 !(ss
.ss_events
& S_RDBAND
)) {
4816 mutex_exit(&stp
->sd_lock
);
4817 mutex_enter(&pidlock
);
4819 mutex_exit(&pidlock
);
4824 * If proc not already registered, add it
4828 ssp
= kmem_alloc(sizeof (strsig_t
), KM_SLEEP
);
4829 ssp
->ss_pidp
= pidp
;
4831 ssp
->ss_next
= NULL
;
4833 pssp
->ss_next
= ssp
;
4835 stp
->sd_siglist
= ssp
;
4836 mutex_enter(&pidlock
);
4838 mutex_exit(&pidlock
);
4844 ssp
->ss_events
= ss
.ss_events
;
4847 * Remove proc from register list.
4850 mutex_enter(&pidlock
);
4852 mutex_exit(&pidlock
);
4854 pssp
->ss_next
= ssp
->ss_next
;
4856 stp
->sd_siglist
= ssp
->ss_next
;
4857 kmem_free(ssp
, sizeof (strsig_t
));
4859 mutex_exit(&stp
->sd_lock
);
4860 mutex_enter(&pidlock
);
4862 mutex_exit(&pidlock
);
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
);
4876 mutex_exit(&pidlock
);
4882 * Return (in arg) the current registration of events
4883 * for which the calling proc is to be signaled.
4890 struct strsigset ss
;
4892 error
= strcopyin((void *)arg
, &ss
, sizeof (ss
), copyflag
);
4897 mutex_enter(&pidlock
);
4901 proc
= pgfind(-pid
);
4905 mutex_exit(&pidlock
);
4909 pidp
= proc
->p_pgidp
;
4911 pidp
= proc
->p_pidp
;
4913 /* Prevent the pidp from being reassigned */
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
);
4927 mutex_exit(&pidlock
);
4930 mutex_exit(&stp
->sd_lock
);
4931 mutex_enter(&pidlock
);
4933 mutex_exit(&pidlock
);
4939 STRUCT_DECL(strpeek
, strpeek
);
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
);
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
)
4959 * If user has requested to peek at a high priority message
4960 * and first message is not, return 0
4963 if ((STRUCT_FGET(strpeek
, flags
) & RS_HIPRI
) &&
4964 queclass(mp
) == QNORM
) {
4966 mutex_exit(QLOCK(rdq
));
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)
4979 mutex_exit(QLOCK(rdq
));
4985 if (mp
&& mp
->b_datap
->db_type
== M_PASSFP
) {
4986 mutex_exit(QLOCK(rdq
));
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
);
4997 STRUCT_FSET(strpeek
, flags
, 0);
5001 if (mp
&& ((tmp_mp
= dupmsg(mp
)) == NULL
)) {
5002 mutex_exit(QLOCK(rdq
));
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.
5014 uio
.uio_extflg
= UIO_COPY_CACHED
;
5015 uio
.uio_segflg
= (copyflag
== U_TO_K
) ? UIO_USERSPACE
:
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);
5030 iov
.iov_base
= STRUCT_FGETP(strpeek
, ctlbuf
.buf
);
5031 iov
.iov_len
= STRUCT_FGET(strpeek
, ctlbuf
.maxlen
);
5033 uio
.uio_resid
= iov
.iov_len
;
5034 uio
.uio_loffset
= 0;
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) {
5052 /* No ctl message */
5054 STRUCT_FSET(strpeek
, ctlbuf
.len
, -1);
5056 STRUCT_FSET(strpeek
, ctlbuf
.len
,
5057 STRUCT_FGET(strpeek
, ctlbuf
.maxlen
) -
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);
5073 iov
.iov_base
= STRUCT_FGETP(strpeek
, databuf
.buf
);
5074 iov
.iov_len
= STRUCT_FGET(strpeek
, databuf
.maxlen
);
5076 uio
.uio_resid
= iov
.iov_len
;
5077 uio
.uio_loffset
= 0;
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) {
5090 ASSERT(data_part
== 0 ||
5091 mp
->b_datap
->db_type
== M_DATA
);
5094 /* No data message */
5096 STRUCT_FSET(strpeek
, databuf
.len
, -1);
5098 STRUCT_FSET(strpeek
, databuf
.len
,
5099 STRUCT_FGET(strpeek
, databuf
.maxlen
) -
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
5110 if ((fmp
== NULL
) && STRUCT_FGET(strpeek
, databuf
.maxlen
) > 0) {
5113 infod
.d_cmd
= INFOD_COPYOUT
;
5115 infod
.d_uiop
= &uio
;
5116 error
= infonext(rdq
, &infod
);
5117 if (error
== EINVAL
|| error
== EBUSY
)
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
);
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)
5149 STRUCT_DECL(strfdinsert
, strfdinsert
);
5150 struct file
*resftp
;
5151 struct stdata
*resstp
;
5156 STRUCT_INIT(strfdinsert
, flag
);
5157 if (stp
->sd_flag
& STRHUP
)
5160 * STRDERR, STWRERR and STPLEX tested above.
5162 error
= strcopyin((void *)arg
, STRUCT_BUF(strfdinsert
),
5163 STRUCT_SIZE(strfdinsert
), copyflag
);
5167 if (STRUCT_FGET(strfdinsert
, offset
) < 0 ||
5168 (STRUCT_FGET(strfdinsert
, offset
) %
5169 sizeof (t_uscalar_t
)) != 0)
5171 if ((resftp
= getf(STRUCT_FGET(strfdinsert
, fildes
))) != NULL
) {
5172 if ((resstp
= resftp
->f_vnode
->v_stream
) == NULL
) {
5173 releasef(STRUCT_FGET(strfdinsert
, fildes
));
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);
5184 mutex_exit(&resstp
->sd_lock
);
5185 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5189 mutex_exit(&resstp
->sd_lock
);
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
;
5200 if (!STRMATED(resstp
) && STREAM(q
) != resstp
&&
5202 ASSERT(q
->q_qinfo
->qi_srvp
);
5203 ASSERT(_OTHERQ(q
)->q_qinfo
->qi_srvp
);
5210 releasestr(resstp
->sd_wrq
);
5211 ival
= (t_uscalar_t
)q
;
5214 ival
= (t_uscalar_t
)getminor(resftp
->f_vnode
->v_rdev
);
5217 if (STRUCT_FGET(strfdinsert
, ctlbuf
.len
) <
5218 STRUCT_FGET(strfdinsert
, offset
) + sizeof (t_uscalar_t
)) {
5219 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5224 * Check for legal flag value.
5226 if (STRUCT_FGET(strfdinsert
, flags
) & ~RS_HIPRI
) {
5227 releasef(STRUCT_FGET(strfdinsert
, fildes
));
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
));
5245 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5248 if ((msgsize
= STRUCT_FGET(strfdinsert
, databuf
.len
)) < 0)
5250 if ((msgsize
< rmin
) ||
5251 ((msgsize
> rmax
) && (rmax
!= INFPSZ
)) ||
5252 (STRUCT_FGET(strfdinsert
, ctlbuf
.len
) > strctlsz
)) {
5253 releasef(STRUCT_FGET(strfdinsert
, fildes
));
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
));
5266 if ((error
= i_straccess(stp
, access
)) != 0) {
5267 mutex_exit(&stp
->sd_lock
);
5269 STRUCT_FGET(strfdinsert
, fildes
));
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
);
5287 uio
.uio_loffset
= 0;
5288 uio
.uio_segflg
= (copyflag
== U_TO_K
) ? UIO_USERSPACE
:
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
));
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
));
5324 if ((fp
= getf((int)arg
)) == NULL
)
5326 error
= do_sendfp(stp
, fp
, crp
);
5328 audit_fdsend((int)arg
, fp
, error
);
5337 struct k_strrecvfd
*srf
;
5340 mutex_enter(&stp
->sd_lock
);
5341 while (!(mp
= getq(rdq
))) {
5342 if (stp
->sd_flag
& (STRHUP
|STREOF
)) {
5343 mutex_exit(&stp
->sd_lock
);
5346 if ((error
= strwaitq(stp
, GETWAIT
, (ssize_t
)0,
5347 flag
, -1, &done
)) != 0 || done
) {
5348 mutex_exit(&stp
->sd_lock
);
5351 if ((error
= i_straccess(stp
, access
)) != 0) {
5352 mutex_exit(&stp
->sd_lock
);
5356 if (mp
->b_datap
->db_type
!= M_PASSFP
) {
5357 putback(stp
, rdq
, mp
, mp
->b_band
);
5358 mutex_exit(&stp
->sd_lock
);
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
);
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 */
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
++)
5392 error
= strcopyout(&ostrfd
, (void *)arg
,
5393 sizeof (struct o_strrecvfd
), copyflag
);
5394 } else { /* I_E_RECVFD */
5395 struct strrecvfd strfd
;
5398 strfd
.uid
= srf
->uid
;
5399 strfd
.gid
= srf
->gid
;
5401 /* null the filler bits */
5402 for (i
= 0; i
< 8; i
++)
5405 error
= strcopyout(&strfd
, (void *)arg
,
5406 sizeof (struct strrecvfd
), copyflag
);
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
);
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
);
5426 mutex_exit(&srf
->fp
->f_tlock
);
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
5443 if (arg
& ~(SNDZERO
|SNDPIPE
))
5446 mutex_enter(&stp
->sd_lock
);
5447 stp
->sd_wput_opt
&= ~(SW_SIGPIPE
|SW_SNDZERO
);
5449 stp
->sd_wput_opt
|= SW_SNDZERO
;
5451 stp
->sd_wput_opt
|= SW_SIGPIPE
;
5452 mutex_exit(&stp
->sd_lock
);
5459 if (stp
->sd_wput_opt
& SW_SNDZERO
)
5461 if (stp
->sd_wput_opt
& SW_SIGPIPE
)
5463 return (strcopyout(&wropt
, (void *)arg
, sizeof (wropt
),
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
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
;
5488 *rvalp
= stp
->sd_pushcnt
+ 1;
5492 STRUCT_INIT(strlist
, flag
);
5494 error
= strcopyin((void *)arg
, STRUCT_BUF(strlist
),
5495 STRUCT_SIZE(strlist
), copyflag
);
5499 mlist
= STRUCT_FGETP(strlist
, sl_modlist
);
5500 nmods
= STRUCT_FGET(strlist
, sl_nmods
);
5504 claimstr(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,
5511 releasestr(stp
->sd_wrq
);
5515 releasestr(stp
->sd_wrq
);
5516 return (strcopyout(&i
, (void *)arg
, sizeof (int), copyflag
));
5524 if ((arg
< 0) || (arg
>= NBAND
))
5526 q
= _RD(stp
->sd_wrq
);
5527 mutex_enter(QLOCK(q
));
5528 if (arg
> (int)q
->q_nband
) {
5546 mutex_exit(QLOCK(q
));
5555 q
= _RD(stp
->sd_wrq
);
5556 mutex_enter(QLOCK(q
));
5559 mutex_exit(QLOCK(q
));
5562 intpri
= (int)mp
->b_band
;
5563 error
= strcopyout(&intpri
, (void *)arg
, sizeof (int),
5565 mutex_exit(QLOCK(q
));
5573 if (arg
& ~(ANYMARK
|LASTMARK
))
5575 q
= _RD(stp
->sd_wrq
);
5576 mutex_enter(&stp
->sd_lock
);
5577 if ((stp
->sd_flag
& STRATMARK
) && (arg
== ANYMARK
)) {
5580 mutex_enter(QLOCK(q
));
5585 else if ((arg
== ANYMARK
) && (mp
->b_flag
& MSGMARK
))
5587 else if ((arg
== LASTMARK
) && (mp
== stp
->sd_mark
))
5591 mutex_exit(QLOCK(q
));
5593 mutex_exit(&stp
->sd_lock
);
5601 if ((arg
< 0) || (arg
>= NBAND
))
5604 *rvalp
= bcanputnext(stp
->sd_wrq
, band
);
5612 error
= strcopyin((void *)arg
, &closetime
, sizeof (int),
5619 stp
->sd_closetime
= closetime
;
5627 closetime
= stp
->sd_closetime
;
5628 return (strcopyout(&closetime
, (void *)arg
, sizeof (int),
5636 mutex_enter(&stp
->sd_lock
);
5637 if (stp
->sd_sidp
== NULL
) {
5638 mutex_exit(&stp
->sd_lock
);
5641 sid
= stp
->sd_sidp
->pid_id
;
5642 mutex_exit(&stp
->sd_lock
);
5643 return (strcopyout(&sid
, (void *)arg
, sizeof (pid_t
),
5651 pid_t sid
, fg_pgid
, bg_pgid
;
5653 if (error
= strcopyin((void *)arg
, &pgrp
, sizeof (pid_t
),
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
);
5663 if (pgrp
== stp
->sd_pgidp
->pid_id
) {
5664 mutex_exit(&pidlock
);
5665 mutex_exit(&stp
->sd_lock
);
5668 if (pgrp
<= 0 || pgrp
>= maxpid
) {
5669 mutex_exit(&pidlock
);
5670 mutex_exit(&stp
->sd_lock
);
5673 if ((q
= pgfind(pgrp
)) == NULL
||
5674 q
->p_sessp
!= ttoproc(curthread
)->p_sessp
) {
5675 mutex_exit(&pidlock
);
5676 mutex_exit(&stp
->sd_lock
);
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
);
5696 mutex_enter(&stp
->sd_lock
);
5697 if (stp
->sd_sidp
== NULL
) {
5698 mutex_exit(&stp
->sd_lock
);
5701 pgrp
= stp
->sd_pgidp
->pid_id
;
5702 mutex_exit(&stp
->sd_lock
);
5703 return (strcopyout(&pgrp
, (void *)arg
, sizeof (pid_t
),
5709 return (strctty(stp
));
5714 /* freectty() always assumes curproc. */
5715 if (freectty(B_FALSE
) != 0)
5722 return (0); /* handled by the upper layer */
5727 * Custom free routine used for M_PASSFP messages.
5730 free_passfp(struct k_strrecvfd
*srf
)
5732 (void) closef(srf
->fp
);
5733 kmem_free(srf
, sizeof (struct k_strrecvfd
) + sizeof (frtn_t
));
5738 do_sendfp(struct stdata
*stp
, struct file
*fp
, struct cred
*cr
)
5740 queue_t
*qp
, *nextqp
;
5741 struct k_strrecvfd
*srf
;
5745 queue_t
*mate
= NULL
;
5749 if (stp
->sd_flag
& STRHUP
)
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
);
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
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.
5786 releasestr(stp
->sd_wrq
);
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
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
);
5818 /* Make sure we still have a q_next */
5819 if (nextqp
!= qp
->q_next
) {
5820 releasestr(stp
->sd_wrq
);
5825 qp
= _RD(STREAM(nextqp
)->sd_wrq
);
5828 /* If we entered the synq above, leave it. */
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
) {
5839 if (qp
->q_flag
& QFULL
) {
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
);
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
);
5863 kmem_free(srf
, bufsize
);
5867 mp
->b_wptr
+= sizeof (struct k_strrecvfd
);
5868 mp
->b_datap
->db_type
= M_PASSFP
;
5871 srf
->uid
= crgetuid(curthread
->t_cred
);
5872 srf
->gid
= crgetgid(curthread
->t_cred
);
5873 mutex_enter(&fp
->f_tlock
);
5875 mutex_exit(&fp
->f_tlock
);
5879 releasestr(stp
->sd_wrq
);
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
5903 struct strioctl
*strioc
,
5904 int fflags
, /* file flags with model info */
5910 struct iocblk
*iocbp
;
5911 struct copyreq
*reqp
;
5912 struct copyresp
*resp
;
5914 int transparent
= 0;
5918 int copyflag
= (flag
& (U_TO_K
| K_TO_K
));
5919 int sigflag
= (flag
& STR_NOSIG
);
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
,
5929 "strdoioctl:stp %p strioc %p", stp
, strioc
);
5930 if (strioc
->ic_len
== TRANSPARENT
) { /* send arg in M_DATA block */
5932 strioc
->ic_len
= sizeof (intptr_t);
5935 if (strioc
->ic_len
< 0 || (strmsgsz
> 0 && strioc
->ic_len
> strmsgsz
))
5938 if ((bp
= allocb_cred_wait(sizeof (union ioctypes
), sigflag
, &error
,
5939 crp
, curproc
->p_pid
)) == NULL
)
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
);
5950 iocbp
->ioc_cr
= crp
;
5951 DB_TYPE(bp
) = M_IOCTL
;
5952 bp
->b_wptr
+= sizeof (struct iocblk
);
5954 if (flag
& STR_NOERROR
)
5957 errs
= STRHUP
|STRDERR
|STWRERR
|STPLEX
;
5960 * If there is data to copy into ioctl block, do so.
5962 if (iocbp
->ioc_count
> 0) {
5965 * Note: STR_NOERROR does not have an effect
5968 id
= K_TO_K
| sigflag
;
5971 if ((error
= putiocd(bp
, strioc
->ic_dp
, id
, crp
)) != 0) {
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
);
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).
6007 mutex_enter(&stp
->sd_lock
);
6008 while ((stp
->sd_flag
& IOCWAIT
) ||
6009 (!set_iocwaitne
&& (stp
->sd_flag
& IOCWAITNE
))) {
6012 TRACE_0(TR_FAC_STREAMS_FR
,
6014 "strdoioctl sleeps - IOCWAIT");
6015 cv_rval
= str_cv_wait(&stp
->sd_iocmonitor
, &stp
->sd_lock
,
6016 STRTIMOUT
, sigflag
);
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
6041 * pending ioctl has caused
6047 } else if ((stp
->sd_flag
& errs
)) {
6048 error
= strgeterr(stp
, errs
, 0);
6051 mutex_exit(&stp
->sd_lock
);
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
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
) {
6112 if (stp
->sd_flag
& errs
) {
6113 error
= strgeterr(stp
, errs
, 0);
6115 stp
->sd_flag
&= ~waitflags
;
6116 cv_broadcast(&stp
->sd_iocmonitor
);
6117 mutex_exit(&stp
->sd_lock
);
6123 TRACE_0(TR_FAC_STREAMS_FR
,
6124 TR_STRDOIOCTL_WAIT2
,
6125 "strdoioctl sleeps awaiting reply");
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
)) {
6151 * note: STR_NOERROR does not protect
6152 * us here.. use ic_timout < 0
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
;
6167 if ((bp
->b_datap
->db_type
== M_COPYIN
) ||
6168 (bp
->b_datap
->db_type
== M_COPYOUT
)) {
6169 mutex_exit(&stp
->sd_lock
);
6171 freemsg(bp
->b_cont
);
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
;
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
);
6188 stp
->sd_flag
&= ~waitflags
;
6189 cv_broadcast(&stp
->sd_iocmonitor
);
6190 mutex_exit(&stp
->sd_lock
);
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
);
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
) {
6230 iocbp
= (struct iocblk
*)bp
->b_rptr
;
6233 * Set error if indicated.
6235 if (iocbp
->ioc_error
) {
6236 error
= iocbp
->ioc_error
;
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
))
6254 if (len
) /* an M_COPYOUT was used with I_STR */
6255 strioc
->ic_len
= len
;
6257 strioc
->ic_len
= (int)iocbp
->ioc_count
;
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
);
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
);
6290 error
= putiocd(bp
, reqp
->cq_addr
, flag
, crp
);
6291 if (error
&& bp
->b_cont
) {
6292 freemsg(bp
->b_cont
);
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
);
6309 mutex_enter(&stp
->sd_lock
);
6310 stp
->sd_flag
&= ~waitflags
;
6311 cv_broadcast(&stp
->sd_iocmonitor
);
6312 mutex_exit(&stp
->sd_lock
);
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
;
6332 len
= (int)reqp
->cq_size
;
6334 /* copyout data to the provided address */
6335 error
= getiocd(bp
, taddr
, copyflag
);
6337 freemsg(bp
->b_cont
);
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
);
6353 mutex_enter(&stp
->sd_lock
);
6354 stp
->sd_flag
&= ~waitflags
;
6355 cv_broadcast(&stp
->sd_iocmonitor
);
6356 mutex_exit(&stp
->sd_lock
);
6364 mutex_enter(&stp
->sd_lock
);
6365 stp
->sd_flag
&= ~waitflags
;
6366 cv_broadcast(&stp
->sd_iocmonitor
);
6367 mutex_exit(&stp
->sd_lock
);
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
)
6386 struct cmdblk
*cmdp
;
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)
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
)
6404 cmdp
= (struct cmdblk
*)mp
->b_wptr
;
6406 cmdp
->cb_cmd
= scp
->sc_cmd
;
6407 cmdp
->cb_len
= scp
->sc_len
;
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
,
6420 if (mp
->b_cont
== NULL
) {
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
);
6443 stp
->sd_flag
|= STRCMDWAIT
;
6444 ASSERT(stp
->sd_cmdblk
== NULL
);
6445 mutex_exit(&stp
->sd_lock
);
6447 putnext(stp
->sd_wrq
, mp
);
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)
6462 rval
= str_cv_wait(&stp
->sd_monitor
, &stp
->sd_lock
, timeout
, 0);
6463 if (stp
->sd_cmdblk
!= NULL
)
6467 error
= (rval
== 0) ? EINTR
: ETIME
;
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)
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
) {
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
;
6505 ASSERT(stp
->sd_cmdblk
== NULL
);
6506 stp
->sd_flag
&= ~STRCMDWAIT
;
6507 mutex_exit(&stp
->sd_lock
);
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.
6523 mutex_enter(&strresources
);
6525 mutex_exit(&strresources
);
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.
6540 struct strbuf
*mctl
,
6541 struct strbuf
*mdata
,
6542 unsigned char *prip
,
6549 mblk_t
*savemp
= NULL
;
6550 mblk_t
*savemptail
= NULL
;
6556 uint_t mark
; /* Contains MSG*MARK and _LASTMARK */
6557 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
6558 unsigned char pri
= 0;
6560 int pr
= 0; /* Partial read successful */
6562 struct uio
*uiop
= &uios
;
6566 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRGETMSG_ENTER
,
6567 "strgetmsg:%p", vp
);
6569 ASSERT(vp
->v_stream
);
6573 mutex_enter(&stp
->sd_lock
);
6575 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
6576 mutex_exit(&stp
->sd_lock
);
6580 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
6581 error
= strgeterr(stp
, STRDERR
|STPLEX
, 0);
6583 mutex_exit(&stp
->sd_lock
);
6587 mutex_exit(&stp
->sd_lock
);
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
;
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
;
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
));
6652 if (DB_TYPE(bp
) == M_SIG
) {
6653 strsignal_nolock(stp
, *bp
->b_rptr
,
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
);
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
);
6693 /* No data. Time to sleep? */
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;
6706 mutex_exit(&stp
->sd_lock
);
6709 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRGETMSG_WAIT
,
6710 "strgetmsg calls strwaitq:%p, %p",
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",
6717 mutex_exit(&stp
->sd_lock
);
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
);
6730 * Extract any mark information. If the message is not completely
6731 * consumed this information will be put in the mblk
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
) {
6743 stp
->sd_mark
= NULL
;
6746 * keep track of the original message type and priority
6749 type
= bp
->b_datap
->db_type
;
6750 if (type
== M_PASSFP
) {
6751 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
6753 bp
->b_flag
|= mark
& ~_LASTMARK
;
6754 putback(stp
, q
, bp
, pri
);
6755 qbackenable(q
, pri
);
6756 mutex_exit(&stp
->sd_lock
);
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.
6781 * First process PROTO or PCPROTO blocks, if any.
6783 if (mctl
->maxlen
>= 0 && type
!= M_DATA
) {
6787 bcnt
= mctl
->maxlen
;
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
)) {
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
;
6808 if (bp
->b_rptr
>= bp
->b_wptr
) {
6818 mctl
->len
= mctl
->maxlen
- bcnt
;
6822 if (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
6824 * More PROTO blocks in msg.
6828 while (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
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
);
6847 mutex_enter(&stp
->sd_lock
);
6849 * clear stream head hi pri flag based on
6852 if (type
>= QPCTL
) {
6853 ASSERT(type
== M_PCPROTO
);
6854 stp
->sd_flag
&= ~STRPRI
;
6861 * (pr == 1) indicates a partial read.
6863 if (oldresid
> uiop
->uio_resid
)
6865 mdata
->len
= mdata
->maxlen
- uiop
->uio_resid
;
6869 if (bp
) { /* more data blocks in msg */
6872 savemptail
->b_cont
= bp
;
6877 mutex_enter(&stp
->sd_lock
);
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
6889 * clear stream head hi pri flag based on
6892 if (type
>= QPCTL
) {
6893 ASSERT(type
== M_PCPROTO
);
6894 stp
->sd_flag
&= ~STRPRI
;
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
;
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
6920 ASSERT(type
== M_DATA
|| type
== M_PROTO
);
6921 ASSERT(savemp
->b_datap
->db_type
== M_PCPROTO
);
6922 savemp
->b_datap
->db_type
= type
;
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
);
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
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
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
;
6972 stp
->sd_flag
&= ~(STRATMARK
|STRNOTATMARK
);
6974 } else if (pr
&& (old_sd_flag
& STRATMARK
)) {
6975 stp
->sd_flag
&= ~STRATMARK
;
6983 * Getmsg cleanup processing - if the state of the queue has changed
6984 * some signals may need to be sent and/or poll awakened.
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
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
);
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
;
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) {
7052 pollwakeups
= POLLIN
| POLLRDNORM
;
7055 pollwakeups
= POLLIN
| POLLRDBAND
;
7059 if (pollwakeups
!= 0) {
7060 if (pollwakeups
== (POLLIN
| POLLRDNORM
)) {
7061 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
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
);
7071 if (stp
->sd_sigflags
& signals
)
7072 strsendsig(stp
->sd_siglist
, signals
, bp
->b_band
, 0);
7074 mutex_exit(&stp
->sd_lock
);
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
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
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.
7107 unsigned char *prip
,
7114 mblk_t
*savemp
= NULL
;
7115 mblk_t
*savemptail
= NULL
;
7122 uint_t mark
; /* Contains MSG*MARK and _LASTMARK */
7123 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
7124 unsigned char pri
= 0;
7126 int pr
= 0; /* Partial read successful */
7129 TRACE_1(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_ENTER
,
7130 "kstrgetmsg:%p", vp
);
7132 ASSERT(vp
->v_stream
);
7136 mutex_enter(&stp
->sd_lock
);
7138 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
7139 mutex_exit(&stp
->sd_lock
);
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
));
7150 mutex_exit(&stp
->sd_lock
);
7155 mutex_exit(&stp
->sd_lock
);
7157 switch (flags
& (MSG_HIPRI
|MSG_ANY
|MSG_BAND
)) {
7172 q
= _RD(stp
->sd_wrq
);
7173 mutex_enter(&stp
->sd_lock
);
7174 old_sd_flag
= stp
->sd_flag
;
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
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
));
7192 if (DB_TYPE(bp
) == M_SIG
) {
7193 strsignal_nolock(stp
, *bp
->b_rptr
,
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
);
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
);
7232 /* No data. Time to sleep? */
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
));
7243 mutex_exit(&stp
->sd_lock
);
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
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)) {
7262 mutex_exit(&stp
->sd_lock
);
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
;
7279 fmode
= uiop
->uio_fmode
;
7283 TRACE_2(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_WAIT
,
7284 "kstrgetmsg calls strwaitq:%p, %p",
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",
7291 mutex_exit(&stp
->sd_lock
);
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
);
7304 * Extract any mark information. If the message is not completely
7305 * consumed this information will be put in the mblk
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
));
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
) {
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
))
7341 bp
->b_flag
|= mark
& ~_LASTMARK
;
7342 putback(stp
, q
, bp
, pri
);
7343 qbackenable(q
, pri
);
7344 mutex_exit(&stp
->sd_lock
);
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
))
7369 bp
->b_flag
|= mark
& ~_LASTMARK
;
7370 putback(stp
, q
, bp
, pri
);
7371 mutex_exit(&stp
->sd_lock
);
7372 error
= strwaitbuf(size
, BPRI_HI
);
7375 * There is no net change to the queue thus
7376 * no need to qbackenable.
7383 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
7385 bp
->b_flag
|= mark
& ~_LASTMARK
;
7386 putback(stp
, q
, bp
, pri
);
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
);
7418 bp
= (stp
->sd_rputdatafunc
)(stp
->sd_vnode
, bp
,
7419 NULL
, NULL
, NULL
, NULL
);
7425 if (STREAM_NEEDSERVICE(stp
))
7426 stream_runservice(stp
);
7429 * Set HIPRI flag if message is priority.
7437 * First process PROTO or PCPROTO blocks, if any.
7439 if (mctlp
!= NULL
&& type
!= M_DATA
) {
7443 while (bp
->b_cont
&& bp
->b_cont
->b_datap
->db_type
!= M_DATA
)
7450 if (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
7452 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
7456 while (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
7460 savemptail
->b_cont
= NULL
;
7464 * Now process DATA blocks, if any.
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
);
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
);
7504 if (mctlp
!= NULL
) {
7509 mutex_enter(&stp
->sd_lock
);
7511 * clear stream head hi pri flag based on
7514 if (!(flags
& MSG_IPEEK
) && (type
>= QPCTL
)) {
7515 ASSERT(type
== M_PCPROTO
);
7516 stp
->sd_flag
&= ~STRPRI
;
7522 * (pr == 1) indicates a partial read.
7524 if (oldresid
> uiop
->uio_resid
)
7528 if (bp
) { /* more data blocks in msg */
7531 savemptail
->b_cont
= bp
;
7536 mutex_enter(&stp
->sd_lock
);
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
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
7562 if (type
>= QPCTL
) {
7563 ASSERT(type
== M_PCPROTO
);
7564 stp
->sd_flag
&= ~STRPRI
;
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
;
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
7591 ASSERT(type
== M_DATA
|| type
== M_PROTO
);
7592 ASSERT(savemp
->b_datap
->db_type
== M_PCPROTO
);
7593 savemp
->b_datap
->db_type
= type
;
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
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
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
;
7645 stp
->sd_flag
&= ~(STRATMARK
|STRNOTATMARK
);
7647 } else if (pr
&& (old_sd_flag
& STRATMARK
)) {
7648 stp
->sd_flag
&= ~STRATMARK
;
7656 * Getmsg cleanup processing - if the state of the queue has changed
7657 * some signals may need to be sent and/or poll awakened.
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
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
);
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
;
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) {
7725 pollwakeups
= POLLIN
| POLLRDNORM
;
7728 pollwakeups
= POLLIN
| POLLRDBAND
;
7732 if (pollwakeups
!= 0) {
7733 if (pollwakeups
== (POLLIN
| POLLRDNORM
)) {
7734 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
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
);
7744 if (stp
->sd_sigflags
& signals
)
7745 strsendsig(stp
->sd_siglist
, signals
, bp
->b_band
, 0);
7747 mutex_exit(&stp
->sd_lock
);
7755 * Put a message downstream.
7757 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7762 struct strbuf
*mctl
,
7763 struct strbuf
*mdata
,
7775 struct uio
*uiop
= &uios
;
7779 ASSERT(vp
->v_stream
);
7784 * If it is an XPG4 application, we need to send
7788 xpg4
= (flag
& MSG_XPG4
) ? 1 : 0;
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
);
7801 if (stp
->sd_flag
& (STWRERR
|STRHUP
|STPLEX
)) {
7802 error
= strwriteable(stp
, B_FALSE
, xpg4
);
7804 mutex_exit(&stp
->sd_lock
);
7809 mutex_exit(&stp
->sd_lock
);
7812 * Check for legal flag value.
7816 if ((mctl
->len
< 0) || (pri
!= 0))
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
));
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) {
7851 rmin
= 0; /* no range check for NULL data part */
7853 if ((msgsize
< rmin
) ||
7854 ((msgsize
> rmax
) && (rmax
!= INFPSZ
)) ||
7855 (mctl
->len
> strctlsz
)) {
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
;
7881 * strput will always free the ctl mblk - even when strput
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",
7891 * Verify that the whole message can be transferred by
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
;
7904 if (error
!= EWOULDBLOCK
)
7907 mutex_enter(&stp
->sd_lock
);
7909 * Check for a missed wakeup.
7910 * Needed since strput did not hold sd_lock across
7913 if (bcanputnext(wqp
, pri
)) {
7915 mutex_exit(&stp
->sd_lock
);
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",
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
);
7934 mutex_exit(&stp
->sd_lock
);
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
)
7944 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_OUT
,
7945 "strputmsg out:stp %p out %d error %d", stp
, 2, 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.
7977 ASSERT(vp
->v_stream
);
7981 audit_strputmsg(vp
, NULL
, NULL
, pri
, flag
, fmode
);
7985 mutex_enter(&stp
->sd_lock
);
7987 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
7988 mutex_exit(&stp
->sd_lock
);
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
);
7997 mutex_exit(&stp
->sd_lock
);
8004 mutex_exit(&stp
->sd_lock
);
8007 * Check for legal flag value.
8009 switch (flag
& (MSG_HIPRI
|MSG_BAND
|MSG_ANY
)) {
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
));
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
;
8049 rmin
= -1; /* no range check for NULL data part */
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
))) {
8061 /* Ignore flow control in strput for HIPRI */
8062 if (flag
& MSG_HIPRI
)
8063 flag
|= MSG_IGNFLOW
;
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
) {
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.
8110 if (mctl
->b_datap
->db_frtnp
!= NULL
)
8118 error
= strwaitbuf(msgdsize(mctl
), BPRI_MED
);
8123 } while (mp
== NULL
);
8126 * Verify that all of msgsize can be transferred by
8129 ASSERT(stp
->sd_maxblk
== INFPSZ
|| stp
->sd_maxblk
>= msgsize
);
8130 error
= strput(stp
, mp
, uiop
, &msgsize
, 0, pri
, flag
);
8134 if (error
!= EWOULDBLOCK
)
8138 * IF MSG_IGNFLOW is set we should have broken out of loop
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
8148 if (bcanputnext(wqp
, pri
)) {
8150 mutex_exit(&stp
->sd_lock
);
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",
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
);
8179 mutex_exit(&stp
->sd_lock
);
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
)
8190 TRACE_3(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_OUT
,
8191 "kstrputmsg out:stp %p out %d error %d", stp
, 2, 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
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).
8219 struct pollhead
**phpp
)
8221 int events
= (ushort_t
)events_arg
;
8225 long sd_flags
= stp
->sd_flag
;
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
;
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
;
8247 if (sd_flags
& STRHUP
) {
8248 retevents
|= POLLHUP
;
8249 } else if (events
& (POLLWRNORM
| POLLWRBAND
)) {
8251 queue_t
*qp
= stp
->sd_wrq
;
8254 /* Find next module forward that has a service procedure */
8255 tq
= qp
->q_next
->q_nfsrv
;
8258 if (polllock(&stp
->sd_pollist
, QLOCK(tq
)) != 0) {
8260 *reventsp
= POLLNVAL
;
8263 if (events
& POLLWRNORM
) {
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
,
8275 *reventsp
= POLLNVAL
;
8278 if (sqp
->q_flag
& QFULL
)
8279 /* ensure pollwakeup() is done */
8280 sqp
->q_flag
|= QWANTWSYNC
;
8282 retevents
|= POLLOUT
;
8283 /* More write events to process ??? */
8284 if (! (events
& POLLWRBAND
)) {
8285 mutex_exit(QLOCK(sqp
));
8289 mutex_exit(QLOCK(sqp
));
8290 if (polllock(&stp
->sd_pollist
,
8293 *reventsp
= POLLNVAL
;
8297 retevents
|= POLLOUT
;
8299 if (events
& POLLWRBAND
) {
8303 if (qbp
->qb_flag
& QB_FULL
)
8304 qbp
->qb_flag
|= QB_WANTW
;
8306 retevents
|= POLLWRBAND
;
8310 retevents
|= POLLWRBAND
;
8313 mutex_exit(QLOCK(tq
));
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
;
8335 * For POLLRDDATA we scan b_cont and b_next until we
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
)
8350 if (mp
->b_band
== 0)
8351 retevents
|= normevents
;
8353 retevents
|= (events
& (POLLIN
| POLLRDBAND
));
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
)) {
8371 mutex_exit(&stp
->sd_lock
);
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.
8381 *phpp
= &stp
->sd_pollist
;
8382 if (headlocked
== 0) {
8383 if (polllock(&stp
->sd_pollist
, &stp
->sd_lock
) != 0) {
8384 *reventsp
= POLLNVAL
;
8389 stp
->sd_rput_opt
|= SR_POLLIN
;
8392 mutex_exit(&stp
->sd_lock
);
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
8407 putback(struct stdata
*stp
, queue_t
*q
, mblk_t
*bp
, int band
)
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
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
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
;
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
;
8489 b_flag
&= ~MSGMARKNEXT
;
8490 b_flag
|= MSGNOTMARKNEXT
;
8491 stp
->sd_flag
&= ~STRNOTATMARK
;
8493 *flagp
= (unsigned short) b_flag
;
8498 * Make sure that the flags are not messed up.
8503 while (mp
!= NULL
) {
8504 ASSERT((mp
->b_flag
& (MSGMARKNEXT
|MSGNOTMARKNEXT
)) !=
8505 (MSGMARKNEXT
|MSGNOTMARKNEXT
));
8510 if (q
->q_first
== bp
) {
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
;
8521 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
8523 stp
->sd_rput_opt
&= ~SR_POLLIN
;
8524 pollevents
= POLLIN
| POLLRDNORM
;
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
8538 * It is the responsibility of the calling routine to ensure
8539 * that the queue does not go away (e.g. pop).
8542 strq2vp(queue_t
*qp
)
8545 vp
= STREAM(qp
)->sd_vnode
;
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.
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.
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.
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.
8600 wrq1
->q_next
= _RD(wrq1
);
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().
8617 str_cn_clean(struct vnode
*vp
)
8619 strsig_t
*ssp
, *pssp
, *tssp
;
8624 ASSERT(vp
->v_stream
);
8627 mutex_enter(&stp
->sd_lock
);
8628 ssp
= stp
->sd_siglist
;
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
;
8638 pssp
->ss_next
= tssp
;
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
));
8649 mutex_exit(&pidlock
);
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.
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
)