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 2017 Joyent, Inc.
28 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
31 #include <sys/types.h>
32 #include <sys/sysmacros.h>
33 #include <sys/param.h>
34 #include <sys/errno.h>
35 #include <sys/signal.h>
40 #include <sys/vnode.h>
42 #include <sys/stream.h>
43 #include <sys/strsubr.h>
44 #include <sys/stropts.h>
45 #include <sys/tihdr.h>
48 #include <sys/termio.h>
49 #include <sys/ttold.h>
50 #include <sys/systm.h>
52 #include <sys/cmn_err.h>
54 #include <sys/netstack.h>
55 #include <sys/priocntl.h>
56 #include <sys/jioctl.h>
57 #include <sys/procset.h>
58 #include <sys/session.h>
60 #include <sys/filio.h>
61 #include <sys/vtrace.h>
62 #include <sys/debug.h>
63 #include <sys/strredir.h>
64 #include <sys/fs/fifonode.h>
65 #include <sys/fs/snode.h>
66 #include <sys/strlog.h>
67 #include <sys/strsun.h>
68 #include <sys/project.h>
72 #include <sys/ptyvar.h>
73 #include <sys/vuid_event.h>
74 #include <sys/modctl.h>
75 #include <sys/sunddi.h>
76 #include <sys/sunldi_impl.h>
77 #include <sys/autoconf.h>
78 #include <sys/policy.h>
81 #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
;
3244 if (flag
& FKIOCTL
) {
3248 ASSERT(vp
->v_stream
);
3249 ASSERT(copyflag
== U_TO_K
|| copyflag
== K_TO_K
);
3252 TRACE_3(TR_FAC_STREAMS_FR
, TR_IOCTL_ENTER
,
3253 "strioctl:stp %p cmd %X arg %lX", stp
, cmd
, arg
);
3256 * If the copy is kernel to kernel, make sure that the FNATIVE
3257 * flag is set. After this it would be a serious error to have
3260 if (copyflag
== K_TO_K
)
3261 flag
= (flag
& ~FMODELS
) | FNATIVE
;
3263 ASSERT((flag
& FMODELS
) != 0);
3268 access
= job_control_type(cmd
);
3270 /* We should never see these here, should be handled by iwscn */
3271 if (cmd
== SRIOCSREDIR
|| cmd
== SRIOCISREDIR
)
3274 mutex_enter(&stp
->sd_lock
);
3275 if ((access
!= -1) && ((error
= i_straccess(stp
, access
)) != 0)) {
3276 mutex_exit(&stp
->sd_lock
);
3279 mutex_exit(&stp
->sd_lock
);
3282 * Check for sgttyb-related ioctls first, and complain as
3289 if (sgttyb_handling
>= 2 && !sgttyb_complaint
) {
3290 sgttyb_complaint
= B_TRUE
;
3292 "application used obsolete TIOC[GS]ET");
3294 if (sgttyb_handling
>= 3) {
3295 tsignal(curthread
, SIGSYS
);
3301 mutex_enter(&stp
->sd_lock
);
3313 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
3314 error
= strgeterr(stp
, STRDERR
|STPLEX
, 0);
3316 mutex_exit(&stp
->sd_lock
);
3323 if (stp
->sd_flag
& (STRDERR
|STWRERR
|STPLEX
)) {
3324 error
= strgeterr(stp
, STRDERR
|STWRERR
|STPLEX
, 0);
3326 mutex_exit(&stp
->sd_lock
);
3332 mutex_exit(&stp
->sd_lock
);
3337 * The stream head has hardcoded knowledge of a
3338 * miscellaneous collection of terminal-, keyboard- and
3339 * mouse-related ioctls, enumerated below. This hardcoded
3340 * knowledge allows the stream head to automatically
3341 * convert transparent ioctl requests made by userland
3342 * programs into I_STR ioctls which many old STREAMS
3343 * modules and drivers require.
3345 * No new ioctls should ever be added to this list.
3346 * Instead, the STREAMS module or driver should be written
3347 * to either handle transparent ioctls or require any
3348 * userland programs to use I_STR ioctls (by returning
3349 * EINVAL to any transparent ioctl requests).
3351 * More importantly, removing ioctls from this list should
3352 * be done with the utmost care, since our STREAMS modules
3353 * and drivers *count* on the stream head performing this
3354 * conversion, and thus may panic while processing
3355 * transparent ioctl request for one of these ioctls (keep
3356 * in mind that third party modules and drivers may have
3357 * similar problems).
3359 if (((cmd
& IOCTYPE
) == LDIOC
) ||
3360 ((cmd
& IOCTYPE
) == tIOC
) ||
3361 ((cmd
& IOCTYPE
) == TIOC
) ||
3362 ((cmd
& IOCTYPE
) == KIOC
) ||
3363 ((cmd
& IOCTYPE
) == MSIOC
) ||
3364 ((cmd
& IOCTYPE
) == VUIOC
)) {
3366 * The ioctl is a tty ioctl - set up strioc buffer
3367 * and call strdoioctl() to do the work.
3369 if (stp
->sd_flag
& STRHUP
)
3371 strioc
.ic_cmd
= cmd
;
3372 strioc
.ic_timout
= INFTIM
;
3381 int native_arg
= (int)arg
;
3382 strioc
.ic_len
= sizeof (int);
3383 strioc
.ic_dp
= (char *)&native_arg
;
3384 return (strdoioctl(stp
, &strioc
, flag
,
3385 K_TO_K
, crp
, rvalp
));
3391 strioc
.ic_len
= sizeof (struct termio
);
3392 strioc
.ic_dp
= (char *)arg
;
3393 return (strdoioctl(stp
, &strioc
, flag
,
3394 copyflag
, crp
, rvalp
));
3399 strioc
.ic_len
= sizeof (struct termios
);
3400 strioc
.ic_dp
= (char *)arg
;
3401 return (strdoioctl(stp
, &strioc
, flag
,
3402 copyflag
, crp
, rvalp
));
3405 strioc
.ic_len
= sizeof (struct termcb
);
3406 strioc
.ic_dp
= (char *)arg
;
3407 return (strdoioctl(stp
, &strioc
, flag
,
3408 copyflag
, crp
, rvalp
));
3411 strioc
.ic_len
= sizeof (struct sgttyb
);
3412 strioc
.ic_dp
= (char *)arg
;
3413 return (strdoioctl(stp
, &strioc
, flag
,
3414 copyflag
, crp
, rvalp
));
3417 if ((flag
& FREAD
) == 0 &&
3418 secpolicy_sti(crp
) != 0) {
3421 mutex_enter(&stp
->sd_lock
);
3422 mutex_enter(&curproc
->p_splock
);
3423 if (stp
->sd_sidp
!= curproc
->p_sessp
->s_sidp
&&
3424 secpolicy_sti(crp
) != 0) {
3425 mutex_exit(&curproc
->p_splock
);
3426 mutex_exit(&stp
->sd_lock
);
3429 mutex_exit(&curproc
->p_splock
);
3430 mutex_exit(&stp
->sd_lock
);
3432 strioc
.ic_len
= sizeof (char);
3433 strioc
.ic_dp
= (char *)arg
;
3434 return (strdoioctl(stp
, &strioc
, flag
,
3435 copyflag
, crp
, rvalp
));
3438 strioc
.ic_len
= sizeof (struct winsize
);
3439 strioc
.ic_dp
= (char *)arg
;
3440 return (strdoioctl(stp
, &strioc
, flag
,
3441 copyflag
, crp
, rvalp
));
3444 strioc
.ic_len
= sizeof (struct ttysize
);
3445 strioc
.ic_dp
= (char *)arg
;
3446 return (strdoioctl(stp
, &strioc
, flag
,
3447 copyflag
, crp
, rvalp
));
3460 strioc
.ic_len
= sizeof (int);
3461 strioc
.ic_dp
= (char *)arg
;
3462 return (strdoioctl(stp
, &strioc
, flag
,
3463 copyflag
, crp
, rvalp
));
3467 strioc
.ic_len
= sizeof (struct kiockey
);
3468 strioc
.ic_dp
= (char *)arg
;
3469 return (strdoioctl(stp
, &strioc
, flag
,
3470 copyflag
, crp
, rvalp
));
3474 strioc
.ic_len
= sizeof (struct kiockeymap
);
3475 strioc
.ic_dp
= (char *)arg
;
3476 return (strdoioctl(stp
, &strioc
, flag
,
3477 copyflag
, crp
, rvalp
));
3480 /* arg is a pointer to char */
3481 strioc
.ic_len
= sizeof (char);
3482 strioc
.ic_dp
= (char *)arg
;
3483 return (strdoioctl(stp
, &strioc
, flag
,
3484 copyflag
, crp
, rvalp
));
3487 strioc
.ic_len
= sizeof (Ms_parms
);
3488 strioc
.ic_dp
= (char *)arg
;
3489 return (strdoioctl(stp
, &strioc
, flag
,
3490 copyflag
, crp
, rvalp
));
3494 strioc
.ic_len
= sizeof (struct vuid_addr_probe
);
3495 strioc
.ic_dp
= (char *)arg
;
3496 return (strdoioctl(stp
, &strioc
, flag
,
3497 copyflag
, crp
, rvalp
));
3500 * These M_IOCTL's don't require any data to be sent
3501 * downstream, and the driver will allocate and link
3502 * on its own mblk_t upon M_IOCACK -- thus we set
3503 * ic_len to zero and set ic_dp to arg so we know
3504 * where to copyout to later.
3510 case KIOCGTRANSABLE
:
3528 strioc
.ic_dp
= (char *)arg
;
3529 return (strdoioctl(stp
, &strioc
, flag
,
3530 copyflag
, crp
, rvalp
));
3535 * Unknown cmd - send it down as a transparent ioctl.
3537 strioc
.ic_cmd
= cmd
;
3538 strioc
.ic_timout
= INFTIM
;
3539 strioc
.ic_len
= TRANSPARENT
;
3540 strioc
.ic_dp
= (char *)&arg
;
3542 return (strdoioctl(stp
, &strioc
, flag
, copyflag
, crp
, rvalp
));
3546 * Stream ioctl. Read in an strioctl buffer from the user
3547 * along with any data specified and send it downstream.
3548 * Strdoioctl will wait allow only one ioctl message at
3549 * a time, and waits for the acknowledgement.
3552 if (stp
->sd_flag
& STRHUP
)
3555 error
= strcopyin_strioctl((void *)arg
, &strioc
, flag
,
3560 if ((strioc
.ic_len
< 0) || (strioc
.ic_timout
< -1))
3563 access
= job_control_type(strioc
.ic_cmd
);
3564 mutex_enter(&stp
->sd_lock
);
3565 if ((access
!= -1) &&
3566 ((error
= i_straccess(stp
, access
)) != 0)) {
3567 mutex_exit(&stp
->sd_lock
);
3570 mutex_exit(&stp
->sd_lock
);
3573 * The I_STR facility provides a trap door for malicious
3574 * code to send down bogus streamio(7I) ioctl commands to
3575 * unsuspecting STREAMS modules and drivers which expect to
3576 * only get these messages from the stream head.
3577 * Explicitly prohibit any streamio ioctls which can be
3578 * passed downstream by the stream head. Note that we do
3579 * not block all streamio ioctls because the ioctl
3580 * numberspace is not well managed and thus it's possible
3581 * that a module or driver's ioctl numbers may accidentally
3582 * collide with them.
3584 switch (strioc
.ic_cmd
) {
3589 case _I_GETPEERCRED
:
3594 error
= strdoioctl(stp
, &strioc
, flag
, copyflag
, crp
, rvalp
);
3596 error
= strcopyout_strioctl(&strioc
, (void *)arg
,
3603 * Like I_STR, but without using M_IOC* messages and without
3604 * copyins/copyouts beyond the passed-in argument.
3606 if (stp
->sd_flag
& STRHUP
)
3609 if ((scp
= kmem_alloc(sizeof (strcmd_t
), KM_NOSLEEP
)) == NULL
)
3612 if (copyin((void *)arg
, scp
, sizeof (strcmd_t
))) {
3613 kmem_free(scp
, sizeof (strcmd_t
));
3617 access
= job_control_type(scp
->sc_cmd
);
3618 mutex_enter(&stp
->sd_lock
);
3619 if (access
!= -1 && (error
= i_straccess(stp
, access
)) != 0) {
3620 mutex_exit(&stp
->sd_lock
);
3621 kmem_free(scp
, sizeof (strcmd_t
));
3624 mutex_exit(&stp
->sd_lock
);
3627 if ((error
= strdocmd(stp
, scp
, crp
)) == 0) {
3628 if (copyout(scp
, (void *)arg
, sizeof (strcmd_t
)))
3631 kmem_free(scp
, sizeof (strcmd_t
));
3636 * Return number of bytes of data in first message
3637 * in queue in "arg" and return the number of messages
3638 * in queue in return value.
3645 mutex_enter(QLOCK(rdq
));
3647 size
= msgdsize(rdq
->q_first
);
3648 for (mp
= rdq
->q_first
; mp
!= NULL
; mp
= mp
->b_next
)
3651 mutex_exit(QLOCK(rdq
));
3652 if (stp
->sd_struiordq
) {
3655 infod
.d_cmd
= INFOD_COUNT
;
3658 infod
.d_cmd
|= INFOD_FIRSTBYTES
;
3662 (void) infonext(rdq
, &infod
);
3663 count
+= infod
.d_count
;
3664 if (infod
.d_res
& INFOD_FIRSTBYTES
)
3665 size
= infod
.d_bytes
;
3669 * Drop down from size_t to the "int" required by the
3670 * interface. Cap at INT_MAX.
3672 retval
= MIN(size
, INT_MAX
);
3673 error
= strcopyout(&retval
, (void *)arg
, sizeof (retval
),
3682 * Return number of bytes of data in all data messages
3683 * in queue in "arg".
3689 mutex_enter(QLOCK(rdq
));
3690 for (mp
= rdq
->q_first
; mp
!= NULL
; mp
= mp
->b_next
)
3691 size
+= msgdsize(mp
);
3692 mutex_exit(QLOCK(rdq
));
3694 if (stp
->sd_struiordq
) {
3697 infod
.d_cmd
= INFOD_BYTES
;
3700 (void) infonext(rdq
, &infod
);
3701 size
+= infod
.d_bytes
;
3705 * Drop down from size_t to the "int" required by the
3706 * interface. Cap at INT_MAX.
3708 retval
= MIN(size
, INT_MAX
);
3709 error
= strcopyout(&retval
, (void *)arg
, sizeof (retval
),
3717 * FIORDCHK does not use arg value (like FIONREAD),
3718 * instead a count is returned. I_NREAD value may
3719 * not be accurate but safe. The real thing to do is
3720 * to add the msgdsizes of all data messages until
3721 * a non-data message.
3726 mutex_enter(QLOCK(rdq
));
3727 for (mp
= rdq
->q_first
; mp
!= NULL
; mp
= mp
->b_next
)
3728 size
+= msgdsize(mp
);
3729 mutex_exit(QLOCK(rdq
));
3731 if (stp
->sd_struiordq
) {
3734 infod
.d_cmd
= INFOD_BYTES
;
3737 (void) infonext(rdq
, &infod
);
3738 size
+= infod
.d_bytes
;
3742 * Since ioctl returns an int, and memory sizes under
3743 * LP64 may not fit, we return INT_MAX if the count was
3746 *rvalp
= MIN(size
, INT_MAX
);
3755 char mname
[FMNAMESZ
+ 1];
3758 error
= (copyflag
& U_TO_K
? copyinstr
: copystr
)((void *)arg
,
3759 mname
, FMNAMESZ
+ 1, NULL
);
3761 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
3764 * Return EINVAL if we're handed a bogus module name.
3766 if (fmodsw_find(mname
, FMODSW_LOAD
) == NULL
) {
3767 TRACE_0(TR_FAC_STREAMS_FR
,
3768 TR_I_CANT_FIND
, "couldn't I_FIND");
3774 /* Look downstream to see if module is there. */
3775 claimstr(stp
->sd_wrq
);
3776 for (q
= stp
->sd_wrq
->q_next
; q
; q
= q
->q_next
) {
3777 if (q
->q_flag
& QREADR
) {
3781 if (strcmp(mname
, Q2NAME(q
)) == 0)
3784 releasestr(stp
->sd_wrq
);
3786 *rvalp
= (q
? 1 : 0);
3791 case __I_PUSH_NOCTTY
:
3794 * For the case __I_PUSH_NOCTTY push a module but
3795 * do not allocate controlling tty. See bugid 4025044
3799 char mname
[FMNAMESZ
+ 1];
3803 if (stp
->sd_flag
& STRHUP
)
3807 * Get module name and look up in fmodsw.
3809 error
= (copyflag
& U_TO_K
? copyinstr
: copystr
)((void *)arg
,
3810 mname
, FMNAMESZ
+ 1, NULL
);
3812 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
3814 if ((fp
= fmodsw_find(mname
, FMODSW_HOLD
| FMODSW_LOAD
)) ==
3818 TRACE_2(TR_FAC_STREAMS_FR
, TR_I_PUSH
,
3819 "I_PUSH:fp %p stp %p", fp
, stp
);
3822 * If the module is flagged as single-instance, then check
3823 * to see if the module is already pushed. If it is, return
3824 * as if the push was successful.
3826 if (fp
->f_qflag
& _QSINGLE_INSTANCE
) {
3829 claimstr(stp
->sd_wrq
);
3830 for (q
= stp
->sd_wrq
->q_next
; q
; q
= q
->q_next
) {
3831 if (q
->q_flag
& QREADR
) {
3835 if (strcmp(mname
, Q2NAME(q
)) == 0)
3838 releasestr(stp
->sd_wrq
);
3845 if (error
= strstartplumb(stp
, flag
, cmd
)) {
3851 * See if any more modules can be pushed on this stream.
3852 * Note that this check must be done after strstartplumb()
3853 * since otherwise multiple threads issuing I_PUSHes on
3854 * the same stream will be able to exceed nstrpush.
3856 mutex_enter(&stp
->sd_lock
);
3857 if (stp
->sd_pushcnt
>= nstrpush
) {
3860 mutex_exit(&stp
->sd_lock
);
3863 mutex_exit(&stp
->sd_lock
);
3866 * Push new module and call its open routine
3867 * via qattach(). Modules don't change device
3868 * numbers, so just ignore dummydev here.
3870 dummydev
= vp
->v_rdev
;
3871 if ((error
= qattach(rdq
, &dummydev
, 0, crp
, fp
,
3873 if (vp
->v_type
== VCHR
&& /* sorry, no pipes allowed */
3874 (cmd
== I_PUSH
) && (stp
->sd_flag
& STRISTTY
)) {
3876 * try to allocate it as a controlling terminal
3878 (void) strctty(stp
);
3882 mutex_enter(&stp
->sd_lock
);
3885 * As a performance concern we are caching the values of
3886 * q_minpsz and q_maxpsz of the module below the stream
3887 * head in the stream head.
3889 mutex_enter(QLOCK(stp
->sd_wrq
->q_next
));
3890 rmin
= stp
->sd_wrq
->q_next
->q_minpsz
;
3891 rmax
= stp
->sd_wrq
->q_next
->q_maxpsz
;
3892 mutex_exit(QLOCK(stp
->sd_wrq
->q_next
));
3894 /* Do this processing here as a performance concern */
3895 if (strmsgsz
!= 0) {
3899 if (vp
->v_type
== VFIFO
)
3900 rmax
= MIN(PIPE_BUF
, rmax
);
3901 else rmax
= MIN(strmsgsz
, rmax
);
3905 mutex_enter(QLOCK(wrq
));
3906 stp
->sd_qn_minpsz
= rmin
;
3907 stp
->sd_qn_maxpsz
= rmax
;
3908 mutex_exit(QLOCK(wrq
));
3911 mutex_exit(&stp
->sd_lock
);
3919 if (stp
->sd_flag
& STRHUP
)
3921 if (!wrq
->q_next
) /* for broken pipes */
3924 if (error
= strstartplumb(stp
, flag
, cmd
))
3928 * If there is an anchor on this stream and popping
3929 * the current module would attempt to pop through the
3930 * anchor, then disallow the pop unless we have sufficient
3931 * privileges; take the cheapest (non-locking) check
3934 if (secpolicy_ip_config(crp
, B_TRUE
) != 0 ||
3935 (stp
->sd_anchorzone
!= crgetzoneid(crp
))) {
3936 mutex_enter(&stp
->sd_lock
);
3938 * Anchors only apply if there's at least one
3939 * module on the stream (sd_pushcnt > 0).
3941 if (stp
->sd_pushcnt
> 0 &&
3942 stp
->sd_pushcnt
== stp
->sd_anchor
&&
3943 stp
->sd_vnode
->v_type
!= VFIFO
) {
3945 mutex_exit(&stp
->sd_lock
);
3946 if (stp
->sd_anchorzone
!= crgetzoneid(crp
))
3948 /* Audit and report error */
3949 return (secpolicy_ip_config(crp
, B_FALSE
));
3951 mutex_exit(&stp
->sd_lock
);
3955 TRACE_2(TR_FAC_STREAMS_FR
, TR_I_POP
,
3956 "I_POP:%p from %p", q
, stp
);
3957 if (q
->q_next
== NULL
|| (q
->q_flag
& (QREADR
|QISDRV
))) {
3960 qdetach(_RD(q
), 1, flag
, crp
, B_FALSE
);
3963 mutex_enter(&stp
->sd_lock
);
3966 * As a performance concern we are caching the values of
3967 * q_minpsz and q_maxpsz of the module below the stream
3968 * head in the stream head.
3970 mutex_enter(QLOCK(wrq
->q_next
));
3971 rmin
= wrq
->q_next
->q_minpsz
;
3972 rmax
= wrq
->q_next
->q_maxpsz
;
3973 mutex_exit(QLOCK(wrq
->q_next
));
3975 /* Do this processing here as a performance concern */
3976 if (strmsgsz
!= 0) {
3980 if (vp
->v_type
== VFIFO
)
3981 rmax
= MIN(PIPE_BUF
, rmax
);
3982 else rmax
= MIN(strmsgsz
, rmax
);
3986 mutex_enter(QLOCK(wrq
));
3987 stp
->sd_qn_minpsz
= rmin
;
3988 stp
->sd_qn_maxpsz
= rmax
;
3989 mutex_exit(QLOCK(wrq
));
3991 /* If we popped through the anchor, then reset the anchor. */
3992 if (stp
->sd_pushcnt
< stp
->sd_anchor
) {
3994 stp
->sd_anchorzone
= 0;
3997 mutex_exit(&stp
->sd_lock
);
4004 * Create a fd for a I_PLINK'ed lower stream with a given
4005 * muxid. With the fd, application can send down ioctls,
4006 * like I_LIST, to the previously I_PLINK'ed stream. Note
4007 * that after getting the fd, the application has to do an
4008 * I_PUNLINK on the muxid before it can do any operation
4009 * on the lower stream. This is required by spec1170.
4011 * The fd used to do this ioctl should point to the same
4012 * controlling device used to do the I_PLINK. If it uses
4013 * a different stream or an invalid muxid, I_MUXID2FD will
4014 * fail. The error code is set to EINVAL.
4016 * The intended use of this interface is the following.
4017 * An application I_PLINK'ed a stream and exits. The fd
4018 * to the lower stream is gone. Another application
4019 * wants to get a fd to the lower stream, it uses I_MUXID2FD.
4021 int muxid
= (int)arg
;
4029 * Do not allow the wildcard muxid. This ioctl is not
4030 * intended to find arbitrary link.
4036 ns
= netstack_find_by_cred(crp
);
4038 ss
= ns
->netstack_str
;
4041 mutex_enter(&muxifier
);
4042 linkp
= findlinks(vp
->v_stream
, muxid
, LINKPERSIST
, ss
);
4043 if (linkp
== NULL
) {
4044 mutex_exit(&muxifier
);
4045 netstack_rele(ss
->ss_netstack
);
4049 if ((fd
= ufalloc(0)) == -1) {
4050 mutex_exit(&muxifier
);
4051 netstack_rele(ss
->ss_netstack
);
4054 fp
= linkp
->li_fpdown
;
4055 mutex_enter(&fp
->f_tlock
);
4057 mutex_exit(&fp
->f_tlock
);
4058 mutex_exit(&muxifier
);
4061 netstack_rele(ss
->ss_netstack
);
4068 * To insert a module to a given position in a stream.
4069 * In the first release, only allow privileged user
4070 * to use this ioctl. Furthermore, the insert is only allowed
4071 * below an anchor if the zoneid is the same as the zoneid
4072 * which created the anchor.
4074 * Note that we do not plan to support this ioctl
4075 * on pipes in the first release. We want to learn more
4076 * about the implications of these ioctls before extending
4077 * their support. And we do not think these features are
4078 * valuable for pipes.
4080 STRUCT_DECL(strmodconf
, strmodinsert
);
4081 char mod_name
[FMNAMESZ
+ 1];
4086 boolean_t is_insert
;
4088 STRUCT_INIT(strmodinsert
, flag
);
4089 if (stp
->sd_flag
& STRHUP
)
4093 if ((error
= secpolicy_net_config(crp
, B_FALSE
)) != 0)
4095 if (stp
->sd_anchor
!= 0 &&
4096 stp
->sd_anchorzone
!= crgetzoneid(crp
))
4099 error
= strcopyin((void *)arg
, STRUCT_BUF(strmodinsert
),
4100 STRUCT_SIZE(strmodinsert
), copyflag
);
4105 * Get module name and look up in fmodsw.
4107 error
= (copyflag
& U_TO_K
? copyinstr
:
4108 copystr
)(STRUCT_FGETP(strmodinsert
, mod_name
),
4109 mod_name
, FMNAMESZ
+ 1, NULL
);
4111 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
4113 if ((fp
= fmodsw_find(mod_name
, FMODSW_HOLD
| FMODSW_LOAD
)) ==
4117 if (error
= strstartplumb(stp
, flag
, cmd
)) {
4123 * Is this _I_INSERT just like an I_PUSH? We need to know
4124 * this because we do some optimizations if this is a
4125 * module being pushed.
4127 pos
= STRUCT_FGET(strmodinsert
, pos
);
4128 is_insert
= (pos
!= 0);
4131 * Make sure pos is valid. Even though it is not an I_PUSH,
4132 * we impose the same limit on the number of modules in a
4135 mutex_enter(&stp
->sd_lock
);
4136 if (stp
->sd_pushcnt
>= nstrpush
|| pos
< 0 ||
4137 pos
> stp
->sd_pushcnt
) {
4140 mutex_exit(&stp
->sd_lock
);
4143 if (stp
->sd_anchor
!= 0) {
4145 * Is this insert below the anchor?
4146 * Pushcnt hasn't been increased yet hence
4147 * we test for greater than here, and greater or
4148 * equal after qattach.
4150 if (pos
> (stp
->sd_pushcnt
- stp
->sd_anchor
) &&
4151 stp
->sd_anchorzone
!= crgetzoneid(crp
)) {
4154 mutex_exit(&stp
->sd_lock
);
4159 mutex_exit(&stp
->sd_lock
);
4162 * First find the correct position this module to
4163 * be inserted. We don't need to call claimstr()
4164 * as the stream should not be changing at this point.
4166 * Insert new module and call its open routine
4167 * via qattach(). Modules don't change device
4168 * numbers, so just ignore dummydev here.
4170 for (tmp_wrq
= stp
->sd_wrq
; pos
> 0;
4171 tmp_wrq
= tmp_wrq
->q_next
, pos
--) {
4172 ASSERT(SAMESTR(tmp_wrq
));
4174 dummydev
= vp
->v_rdev
;
4175 if ((error
= qattach(_RD(tmp_wrq
), &dummydev
, 0, crp
,
4176 fp
, is_insert
)) != 0) {
4177 mutex_enter(&stp
->sd_lock
);
4179 mutex_exit(&stp
->sd_lock
);
4183 mutex_enter(&stp
->sd_lock
);
4186 * As a performance concern we are caching the values of
4187 * q_minpsz and q_maxpsz of the module below the stream
4188 * head in the stream head.
4191 mutex_enter(QLOCK(stp
->sd_wrq
->q_next
));
4192 rmin
= stp
->sd_wrq
->q_next
->q_minpsz
;
4193 rmax
= stp
->sd_wrq
->q_next
->q_maxpsz
;
4194 mutex_exit(QLOCK(stp
->sd_wrq
->q_next
));
4196 /* Do this processing here as a performance concern */
4197 if (strmsgsz
!= 0) {
4198 if (rmax
== INFPSZ
) {
4201 rmax
= MIN(strmsgsz
, rmax
);
4205 mutex_enter(QLOCK(wrq
));
4206 stp
->sd_qn_minpsz
= rmin
;
4207 stp
->sd_qn_maxpsz
= rmax
;
4208 mutex_exit(QLOCK(wrq
));
4212 * Need to update the anchor value if this module is
4213 * inserted below the anchor point.
4215 if (stp
->sd_anchor
!= 0) {
4216 pos
= STRUCT_FGET(strmodinsert
, pos
);
4217 if (pos
>= (stp
->sd_pushcnt
- stp
->sd_anchor
))
4222 mutex_exit(&stp
->sd_lock
);
4229 * To remove a module with a given name in a stream. The
4230 * caller of this ioctl needs to provide both the name and
4231 * the position of the module to be removed. This eliminates
4232 * the ambiguity of removal if a module is inserted/pushed
4233 * multiple times in a stream. In the first release, only
4234 * allow privileged user to use this ioctl.
4235 * Furthermore, the remove is only allowed
4236 * below an anchor if the zoneid is the same as the zoneid
4237 * which created the anchor.
4239 * Note that we do not plan to support this ioctl
4240 * on pipes in the first release. We want to learn more
4241 * about the implications of these ioctls before extending
4242 * their support. And we do not think these features are
4243 * valuable for pipes.
4245 * Also note that _I_REMOVE cannot be used to remove a
4246 * driver or the stream head.
4248 STRUCT_DECL(strmodconf
, strmodremove
);
4251 char mod_name
[FMNAMESZ
+ 1];
4252 boolean_t is_remove
;
4254 STRUCT_INIT(strmodremove
, flag
);
4255 if (stp
->sd_flag
& STRHUP
)
4259 if ((error
= secpolicy_net_config(crp
, B_FALSE
)) != 0)
4261 if (stp
->sd_anchor
!= 0 &&
4262 stp
->sd_anchorzone
!= crgetzoneid(crp
))
4265 error
= strcopyin((void *)arg
, STRUCT_BUF(strmodremove
),
4266 STRUCT_SIZE(strmodremove
), copyflag
);
4270 error
= (copyflag
& U_TO_K
? copyinstr
:
4271 copystr
)(STRUCT_FGETP(strmodremove
, mod_name
),
4272 mod_name
, FMNAMESZ
+ 1, NULL
);
4274 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
4276 if ((error
= strstartplumb(stp
, flag
, cmd
)) != 0)
4280 * Match the name of given module to the name of module at
4281 * the given position.
4283 pos
= STRUCT_FGET(strmodremove
, pos
);
4285 is_remove
= (pos
!= 0);
4286 for (q
= stp
->sd_wrq
->q_next
; SAMESTR(q
) && pos
> 0;
4287 q
= q
->q_next
, pos
--)
4289 if (pos
> 0 || !SAMESTR(q
) ||
4290 strcmp(Q2NAME(q
), mod_name
) != 0) {
4291 mutex_enter(&stp
->sd_lock
);
4293 mutex_exit(&stp
->sd_lock
);
4298 * If the position is at or below an anchor, then the zoneid
4299 * must match the zoneid that created the anchor.
4301 if (stp
->sd_anchor
!= 0) {
4302 pos
= STRUCT_FGET(strmodremove
, pos
);
4303 if (pos
>= (stp
->sd_pushcnt
- stp
->sd_anchor
) &&
4304 stp
->sd_anchorzone
!= crgetzoneid(crp
)) {
4305 mutex_enter(&stp
->sd_lock
);
4307 mutex_exit(&stp
->sd_lock
);
4313 ASSERT(!(q
->q_flag
& QREADR
));
4314 qdetach(_RD(q
), 1, flag
, crp
, is_remove
);
4316 mutex_enter(&stp
->sd_lock
);
4319 * As a performance concern we are caching the values of
4320 * q_minpsz and q_maxpsz of the module below the stream
4321 * head in the stream head.
4324 mutex_enter(QLOCK(wrq
->q_next
));
4325 rmin
= wrq
->q_next
->q_minpsz
;
4326 rmax
= wrq
->q_next
->q_maxpsz
;
4327 mutex_exit(QLOCK(wrq
->q_next
));
4329 /* Do this processing here as a performance concern */
4330 if (strmsgsz
!= 0) {
4334 if (vp
->v_type
== VFIFO
)
4335 rmax
= MIN(PIPE_BUF
, rmax
);
4336 else rmax
= MIN(strmsgsz
, rmax
);
4340 mutex_enter(QLOCK(wrq
));
4341 stp
->sd_qn_minpsz
= rmin
;
4342 stp
->sd_qn_maxpsz
= rmax
;
4343 mutex_exit(QLOCK(wrq
));
4347 * Need to update the anchor value if this module is removed
4348 * at or below the anchor point. If the removed module is at
4349 * the anchor point, remove the anchor for this stream if
4350 * there is no module above the anchor point. Otherwise, if
4351 * the removed module is below the anchor point, decrement the
4352 * anchor point by 1.
4354 if (stp
->sd_anchor
!= 0) {
4355 pos
= STRUCT_FGET(strmodremove
, pos
);
4356 if (pos
== stp
->sd_pushcnt
- stp
->sd_anchor
+ 1)
4358 else if (pos
> (stp
->sd_pushcnt
- stp
->sd_anchor
+ 1))
4363 mutex_exit(&stp
->sd_lock
);
4369 * Set the anchor position on the stream to reside at
4370 * the top module (in other words, the top module
4371 * cannot be popped). Anchors with a FIFO make no
4372 * obvious sense, so they're not allowed.
4374 mutex_enter(&stp
->sd_lock
);
4376 if (stp
->sd_vnode
->v_type
== VFIFO
) {
4377 mutex_exit(&stp
->sd_lock
);
4380 /* Only allow the same zoneid to update the anchor */
4381 if (stp
->sd_anchor
!= 0 &&
4382 stp
->sd_anchorzone
!= crgetzoneid(crp
)) {
4383 mutex_exit(&stp
->sd_lock
);
4386 stp
->sd_anchor
= stp
->sd_pushcnt
;
4387 stp
->sd_anchorzone
= crgetzoneid(crp
);
4388 mutex_exit(&stp
->sd_lock
);
4393 * Get name of first module downstream.
4394 * If no module, return an error.
4397 if (_SAMESTR(wrq
) && wrq
->q_next
->q_next
!= NULL
) {
4398 char *name
= Q2NAME(wrq
->q_next
);
4400 error
= strcopyout(name
, (void *)arg
, strlen(name
) + 1,
4411 * Link a multiplexor.
4413 return (mlink(vp
, cmd
, (int)arg
, crp
, rvalp
, 0));
4417 * Link a multiplexor: Call must originate from kernel.
4420 return (ldi_mlink_lh(vp
, cmd
, arg
, crp
, rvalp
));
4426 * Unlink a multiplexor.
4427 * If arg is -1, unlink all links for which this is the
4428 * controlling stream. Otherwise, arg is an index number
4429 * for a link to be removed.
4432 struct linkinfo
*linkp
;
4433 int native_arg
= (int)arg
;
4438 TRACE_1(TR_FAC_STREAMS_FR
,
4439 TR_I_UNLINK
, "I_UNLINK/I_PUNLINK:%p", stp
);
4440 if (vp
->v_type
== VFIFO
) {
4443 if (cmd
== I_UNLINK
)
4445 else /* I_PUNLINK */
4447 if (native_arg
== 0) {
4450 ns
= netstack_find_by_cred(crp
);
4452 ss
= ns
->netstack_str
;
4455 if (native_arg
== MUXID_ALL
)
4456 error
= munlinkall(stp
, type
, crp
, rvalp
, ss
);
4458 mutex_enter(&muxifier
);
4459 if (!(linkp
= findlinks(stp
, (int)arg
, type
, ss
))) {
4460 /* invalid user supplied index number */
4461 mutex_exit(&muxifier
);
4462 netstack_rele(ss
->ss_netstack
);
4465 /* munlink drops the muxifier lock */
4466 error
= munlink(stp
, linkp
, type
, crp
, rvalp
, ss
);
4468 netstack_rele(ss
->ss_netstack
);
4474 * send a flush message downstream
4475 * flush message can indicate
4476 * FLUSHR - flush read queue
4477 * FLUSHW - flush write queue
4478 * FLUSHRW - flush read/write queue
4480 if (stp
->sd_flag
& STRHUP
)
4486 if (putnextctl1(stp
->sd_wrq
, M_FLUSH
, (int)arg
)) {
4489 if (error
= strwaitbuf(1, BPRI_HI
)) {
4495 * Send down an unsupported ioctl and wait for the nack
4496 * in order to allow the M_FLUSH to propagate back
4497 * up to the stream head.
4498 * Replaces if (qready()) runqueues();
4500 strioc
.ic_cmd
= -1; /* The unsupported ioctl */
4501 strioc
.ic_timout
= 0;
4503 strioc
.ic_dp
= NULL
;
4504 (void) strdoioctl(stp
, &strioc
, flag
, K_TO_K
, crp
, rvalp
);
4510 struct bandinfo binfo
;
4512 error
= strcopyin((void *)arg
, &binfo
, sizeof (binfo
),
4516 if (stp
->sd_flag
& STRHUP
)
4518 if (binfo
.bi_flag
& ~FLUSHRW
)
4520 while (!(mp
= allocb(2, BPRI_HI
))) {
4521 if (error
= strwaitbuf(2, BPRI_HI
))
4524 mp
->b_datap
->db_type
= M_FLUSH
;
4525 *mp
->b_wptr
++ = binfo
.bi_flag
| FLUSHBAND
;
4526 *mp
->b_wptr
++ = binfo
.bi_pri
;
4527 putnext(stp
->sd_wrq
, mp
);
4529 * Send down an unsupported ioctl and wait for the nack
4530 * in order to allow the M_FLUSH to propagate back
4531 * up to the stream head.
4532 * Replaces if (qready()) runqueues();
4534 strioc
.ic_cmd
= -1; /* The unsupported ioctl */
4535 strioc
.ic_timout
= 0;
4537 strioc
.ic_dp
= NULL
;
4538 (void) strdoioctl(stp
, &strioc
, flag
, K_TO_K
, crp
, rvalp
);
4547 * RNORM - default stream mode
4548 * RMSGN - message no discard
4549 * RMSGD - message discard
4550 * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
4551 * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
4552 * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
4554 if (arg
& ~(RMODEMASK
| RPROTMASK
))
4557 if ((arg
& (RMSGD
|RMSGN
)) == (RMSGD
|RMSGN
))
4560 mutex_enter(&stp
->sd_lock
);
4561 switch (arg
& RMODEMASK
) {
4563 stp
->sd_read_opt
&= ~(RD_MSGDIS
| RD_MSGNODIS
);
4566 stp
->sd_read_opt
= (stp
->sd_read_opt
& ~RD_MSGNODIS
) |
4570 stp
->sd_read_opt
= (stp
->sd_read_opt
& ~RD_MSGDIS
) |
4575 switch (arg
& RPROTMASK
) {
4577 stp
->sd_read_opt
&= ~(RD_PROTDAT
| RD_PROTDIS
);
4581 stp
->sd_read_opt
= ((stp
->sd_read_opt
& ~RD_PROTDIS
) |
4586 stp
->sd_read_opt
= ((stp
->sd_read_opt
& ~RD_PROTDAT
) |
4590 mutex_exit(&stp
->sd_lock
);
4595 * Get read option and return the value
4596 * to spot pointed to by arg
4601 rdopt
= ((stp
->sd_read_opt
& RD_MSGDIS
) ? RMSGD
:
4602 ((stp
->sd_read_opt
& RD_MSGNODIS
) ? RMSGN
: RNORM
));
4603 rdopt
|= ((stp
->sd_read_opt
& RD_PROTDAT
) ? RPROTDAT
:
4604 ((stp
->sd_read_opt
& RD_PROTDIS
) ? RPROTDIS
: RPROTNORM
));
4606 return (strcopyout(&rdopt
, (void *)arg
, sizeof (int),
4614 * RERRNORM - persistent read errors
4615 * RERRNONPERSIST - non-persistent read errors
4616 * WERRNORM - persistent write errors
4617 * WERRNONPERSIST - non-persistent write errors
4619 if (arg
& ~(RERRMASK
| WERRMASK
))
4622 mutex_enter(&stp
->sd_lock
);
4623 switch (arg
& RERRMASK
) {
4625 stp
->sd_flag
&= ~STRDERRNONPERSIST
;
4627 case RERRNONPERSIST
:
4628 stp
->sd_flag
|= STRDERRNONPERSIST
;
4631 switch (arg
& WERRMASK
) {
4633 stp
->sd_flag
&= ~STWRERRNONPERSIST
;
4635 case WERRNONPERSIST
:
4636 stp
->sd_flag
|= STWRERRNONPERSIST
;
4639 mutex_exit(&stp
->sd_lock
);
4644 * Get error option and return the value
4645 * to spot pointed to by arg
4650 erropt
|= (stp
->sd_flag
& STRDERRNONPERSIST
) ? RERRNONPERSIST
:
4652 erropt
|= (stp
->sd_flag
& STWRERRNONPERSIST
) ? WERRNONPERSIST
:
4654 return (strcopyout(&erropt
, (void *)arg
, sizeof (int),
4660 * Register the calling proc to receive the SIGPOLL
4661 * signal based on the events given in arg. If
4662 * arg is zero, remove the proc from register list.
4665 strsig_t
*ssp
, *pssp
;
4669 pidp
= curproc
->p_pidp
;
4671 * Hold sd_lock to prevent traversal of sd_siglist while
4674 mutex_enter(&stp
->sd_lock
);
4675 for (ssp
= stp
->sd_siglist
; ssp
&& (ssp
->ss_pidp
!= pidp
);
4676 pssp
= ssp
, ssp
= ssp
->ss_next
)
4680 if (arg
& ~(S_INPUT
|S_HIPRI
|S_MSG
|S_HANGUP
|S_ERROR
|
4681 S_RDNORM
|S_WRNORM
|S_RDBAND
|S_WRBAND
|S_BANDURG
)) {
4682 mutex_exit(&stp
->sd_lock
);
4685 if ((arg
& S_BANDURG
) && !(arg
& S_RDBAND
)) {
4686 mutex_exit(&stp
->sd_lock
);
4691 * If proc not already registered, add it
4695 ssp
= kmem_alloc(sizeof (strsig_t
), KM_SLEEP
);
4696 ssp
->ss_pidp
= pidp
;
4697 ssp
->ss_pid
= pidp
->pid_id
;
4698 ssp
->ss_next
= NULL
;
4700 pssp
->ss_next
= ssp
;
4702 stp
->sd_siglist
= ssp
;
4703 mutex_enter(&pidlock
);
4705 mutex_exit(&pidlock
);
4711 ssp
->ss_events
= (int)arg
;
4714 * Remove proc from register list.
4717 mutex_enter(&pidlock
);
4719 mutex_exit(&pidlock
);
4721 pssp
->ss_next
= ssp
->ss_next
;
4723 stp
->sd_siglist
= ssp
->ss_next
;
4724 kmem_free(ssp
, sizeof (strsig_t
));
4726 mutex_exit(&stp
->sd_lock
);
4732 * Recalculate OR of sig events.
4734 stp
->sd_sigflags
= 0;
4735 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
4736 stp
->sd_sigflags
|= ssp
->ss_events
;
4737 mutex_exit(&stp
->sd_lock
);
4743 * Return (in arg) the current registration of events
4744 * for which the calling proc is to be signaled.
4750 pidp
= curproc
->p_pidp
;
4751 mutex_enter(&stp
->sd_lock
);
4752 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
4753 if (ssp
->ss_pidp
== pidp
) {
4754 error
= strcopyout(&ssp
->ss_events
, (void *)arg
,
4755 sizeof (int), copyflag
);
4756 mutex_exit(&stp
->sd_lock
);
4759 mutex_exit(&stp
->sd_lock
);
4765 * Register the ss_pid to receive the SIGPOLL
4766 * signal based on the events is ss_events arg. If
4767 * ss_events is zero, remove the proc from register list.
4770 struct strsig
*ssp
, *pssp
;
4774 struct strsigset ss
;
4776 error
= strcopyin((void *)arg
, &ss
, sizeof (ss
), copyflag
);
4782 if (ss
.ss_events
!= 0) {
4784 * Permissions check by sending signal 0.
4785 * Note that when kill fails it does a set_errno
4786 * causing the system call to fail.
4788 error
= kill(pid
, 0);
4793 mutex_enter(&pidlock
);
4797 proc
= pgfind(-pid
);
4801 mutex_exit(&pidlock
);
4805 pidp
= proc
->p_pgidp
;
4807 pidp
= proc
->p_pidp
;
4810 * Get a hold on the pid structure while referencing it.
4811 * There is a separate PID_HOLD should it be inserted
4812 * in the list below.
4815 mutex_exit(&pidlock
);
4819 * Hold sd_lock to prevent traversal of sd_siglist while
4822 mutex_enter(&stp
->sd_lock
);
4823 for (ssp
= stp
->sd_siglist
; ssp
&& (ssp
->ss_pid
!= pid
);
4824 pssp
= ssp
, ssp
= ssp
->ss_next
)
4829 ~(S_INPUT
|S_HIPRI
|S_MSG
|S_HANGUP
|S_ERROR
|
4830 S_RDNORM
|S_WRNORM
|S_RDBAND
|S_WRBAND
|S_BANDURG
)) {
4831 mutex_exit(&stp
->sd_lock
);
4832 mutex_enter(&pidlock
);
4834 mutex_exit(&pidlock
);
4837 if ((ss
.ss_events
& S_BANDURG
) &&
4838 !(ss
.ss_events
& S_RDBAND
)) {
4839 mutex_exit(&stp
->sd_lock
);
4840 mutex_enter(&pidlock
);
4842 mutex_exit(&pidlock
);
4847 * If proc not already registered, add it
4851 ssp
= kmem_alloc(sizeof (strsig_t
), KM_SLEEP
);
4852 ssp
->ss_pidp
= pidp
;
4854 ssp
->ss_next
= NULL
;
4856 pssp
->ss_next
= ssp
;
4858 stp
->sd_siglist
= ssp
;
4859 mutex_enter(&pidlock
);
4861 mutex_exit(&pidlock
);
4867 ssp
->ss_events
= ss
.ss_events
;
4870 * Remove proc from register list.
4873 mutex_enter(&pidlock
);
4875 mutex_exit(&pidlock
);
4877 pssp
->ss_next
= ssp
->ss_next
;
4879 stp
->sd_siglist
= ssp
->ss_next
;
4880 kmem_free(ssp
, sizeof (strsig_t
));
4882 mutex_exit(&stp
->sd_lock
);
4883 mutex_enter(&pidlock
);
4885 mutex_exit(&pidlock
);
4891 * Recalculate OR of sig events.
4893 stp
->sd_sigflags
= 0;
4894 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
4895 stp
->sd_sigflags
|= ssp
->ss_events
;
4896 mutex_exit(&stp
->sd_lock
);
4897 mutex_enter(&pidlock
);
4899 mutex_exit(&pidlock
);
4905 * Return (in arg) the current registration of events
4906 * for which the calling proc is to be signaled.
4913 struct strsigset ss
;
4915 error
= strcopyin((void *)arg
, &ss
, sizeof (ss
), copyflag
);
4920 mutex_enter(&pidlock
);
4924 proc
= pgfind(-pid
);
4928 mutex_exit(&pidlock
);
4932 pidp
= proc
->p_pgidp
;
4934 pidp
= proc
->p_pidp
;
4936 /* Prevent the pidp from being reassigned */
4938 mutex_exit(&pidlock
);
4940 mutex_enter(&stp
->sd_lock
);
4941 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
4942 if (ssp
->ss_pid
== pid
) {
4943 ss
.ss_pid
= ssp
->ss_pid
;
4944 ss
.ss_events
= ssp
->ss_events
;
4945 error
= strcopyout(&ss
, (void *)arg
,
4946 sizeof (struct strsigset
), copyflag
);
4947 mutex_exit(&stp
->sd_lock
);
4948 mutex_enter(&pidlock
);
4950 mutex_exit(&pidlock
);
4953 mutex_exit(&stp
->sd_lock
);
4954 mutex_enter(&pidlock
);
4956 mutex_exit(&pidlock
);
4962 STRUCT_DECL(strpeek
, strpeek
);
4964 mblk_t
*fmp
, *tmp_mp
= NULL
;
4966 STRUCT_INIT(strpeek
, flag
);
4968 error
= strcopyin((void *)arg
, STRUCT_BUF(strpeek
),
4969 STRUCT_SIZE(strpeek
), copyflag
);
4973 mutex_enter(QLOCK(rdq
));
4975 * Skip the invalid messages
4977 for (mp
= rdq
->q_first
; mp
!= NULL
; mp
= mp
->b_next
)
4978 if (mp
->b_datap
->db_type
!= M_SIG
)
4982 * If user has requested to peek at a high priority message
4983 * and first message is not, return 0
4986 if ((STRUCT_FGET(strpeek
, flags
) & RS_HIPRI
) &&
4987 queclass(mp
) == QNORM
) {
4989 mutex_exit(QLOCK(rdq
));
4992 } else if (stp
->sd_struiordq
== NULL
||
4993 (STRUCT_FGET(strpeek
, flags
) & RS_HIPRI
)) {
4995 * No mblks to look at at the streamhead and
4996 * 1). This isn't a synch stream or
4997 * 2). This is a synch stream but caller wants high
4998 * priority messages which is not supported by
4999 * the synch stream. (it only supports QNORM)
5002 mutex_exit(QLOCK(rdq
));
5008 if (mp
&& mp
->b_datap
->db_type
== M_PASSFP
) {
5009 mutex_exit(QLOCK(rdq
));
5013 ASSERT(mp
== NULL
|| mp
->b_datap
->db_type
== M_PCPROTO
||
5014 mp
->b_datap
->db_type
== M_PROTO
||
5015 mp
->b_datap
->db_type
== M_DATA
);
5017 if (mp
&& mp
->b_datap
->db_type
== M_PCPROTO
) {
5018 STRUCT_FSET(strpeek
, flags
, RS_HIPRI
);
5020 STRUCT_FSET(strpeek
, flags
, 0);
5024 if (mp
&& ((tmp_mp
= dupmsg(mp
)) == NULL
)) {
5025 mutex_exit(QLOCK(rdq
));
5028 mutex_exit(QLOCK(rdq
));
5031 * set mp = tmp_mp, so that I_PEEK processing can continue.
5032 * tmp_mp is used to free the dup'd message.
5037 uio
.uio_extflg
= UIO_COPY_CACHED
;
5038 uio
.uio_segflg
= (copyflag
== U_TO_K
) ? UIO_USERSPACE
:
5042 * First process PROTO blocks, if any.
5043 * If user doesn't want to get ctl info by setting maxlen <= 0,
5044 * then set len to -1/0 and skip control blocks part.
5046 if (STRUCT_FGET(strpeek
, ctlbuf
.maxlen
) < 0)
5047 STRUCT_FSET(strpeek
, ctlbuf
.len
, -1);
5048 else if (STRUCT_FGET(strpeek
, ctlbuf
.maxlen
) == 0)
5049 STRUCT_FSET(strpeek
, ctlbuf
.len
, 0);
5053 iov
.iov_base
= STRUCT_FGETP(strpeek
, ctlbuf
.buf
);
5054 iov
.iov_len
= STRUCT_FGET(strpeek
, ctlbuf
.maxlen
);
5056 uio
.uio_resid
= iov
.iov_len
;
5057 uio
.uio_loffset
= 0;
5059 while (mp
&& mp
->b_datap
->db_type
!= M_DATA
&&
5060 uio
.uio_resid
>= 0) {
5061 ASSERT(STRUCT_FGET(strpeek
, flags
) == 0 ?
5062 mp
->b_datap
->db_type
== M_PROTO
:
5063 mp
->b_datap
->db_type
== M_PCPROTO
);
5065 if ((n
= MIN(uio
.uio_resid
,
5066 mp
->b_wptr
- mp
->b_rptr
)) != 0 &&
5067 (error
= uiomove((char *)mp
->b_rptr
, n
,
5068 UIO_READ
, &uio
)) != 0) {
5075 /* No ctl message */
5077 STRUCT_FSET(strpeek
, ctlbuf
.len
, -1);
5079 STRUCT_FSET(strpeek
, ctlbuf
.len
,
5080 STRUCT_FGET(strpeek
, ctlbuf
.maxlen
) -
5085 * Now process DATA blocks, if any.
5086 * If user doesn't want to get data info by setting maxlen <= 0,
5087 * then set len to -1/0 and skip data blocks part.
5089 if (STRUCT_FGET(strpeek
, databuf
.maxlen
) < 0)
5090 STRUCT_FSET(strpeek
, databuf
.len
, -1);
5091 else if (STRUCT_FGET(strpeek
, databuf
.maxlen
) == 0)
5092 STRUCT_FSET(strpeek
, databuf
.len
, 0);
5096 iov
.iov_base
= STRUCT_FGETP(strpeek
, databuf
.buf
);
5097 iov
.iov_len
= STRUCT_FGET(strpeek
, databuf
.maxlen
);
5099 uio
.uio_resid
= iov
.iov_len
;
5100 uio
.uio_loffset
= 0;
5102 while (mp
&& uio
.uio_resid
) {
5103 if (mp
->b_datap
->db_type
== M_DATA
) {
5104 if ((n
= MIN(uio
.uio_resid
,
5105 mp
->b_wptr
- mp
->b_rptr
)) != 0 &&
5106 (error
= uiomove((char *)mp
->b_rptr
,
5107 n
, UIO_READ
, &uio
)) != 0) {
5113 ASSERT(data_part
== 0 ||
5114 mp
->b_datap
->db_type
== M_DATA
);
5117 /* No data message */
5119 STRUCT_FSET(strpeek
, databuf
.len
, -1);
5121 STRUCT_FSET(strpeek
, databuf
.len
,
5122 STRUCT_FGET(strpeek
, databuf
.maxlen
) -
5128 * It is a synch stream and user wants to get
5129 * data (maxlen > 0).
5130 * uio setup is done by the codes that process DATA
5133 if ((fmp
== NULL
) && STRUCT_FGET(strpeek
, databuf
.maxlen
) > 0) {
5136 infod
.d_cmd
= INFOD_COPYOUT
;
5138 infod
.d_uiop
= &uio
;
5139 error
= infonext(rdq
, &infod
);
5140 if (error
== EINVAL
|| error
== EBUSY
)
5144 STRUCT_FSET(strpeek
, databuf
.len
, STRUCT_FGET(strpeek
,
5145 databuf
.maxlen
) - uio
.uio_resid
);
5146 if (STRUCT_FGET(strpeek
, databuf
.len
) == 0) {
5148 * No data found by the infonext().
5150 STRUCT_FSET(strpeek
, databuf
.len
, -1);
5153 error
= strcopyout(STRUCT_BUF(strpeek
), (void *)arg
,
5154 STRUCT_SIZE(strpeek
), copyflag
);
5159 * If there is no message retrieved, set return code to 0
5160 * otherwise, set it to 1.
5162 if (STRUCT_FGET(strpeek
, ctlbuf
.len
) == -1 &&
5163 STRUCT_FGET(strpeek
, databuf
.len
) == -1)
5172 STRUCT_DECL(strfdinsert
, strfdinsert
);
5173 struct file
*resftp
;
5174 struct stdata
*resstp
;
5179 STRUCT_INIT(strfdinsert
, flag
);
5180 if (stp
->sd_flag
& STRHUP
)
5183 * STRDERR, STWRERR and STPLEX tested above.
5185 error
= strcopyin((void *)arg
, STRUCT_BUF(strfdinsert
),
5186 STRUCT_SIZE(strfdinsert
), copyflag
);
5190 if (STRUCT_FGET(strfdinsert
, offset
) < 0 ||
5191 (STRUCT_FGET(strfdinsert
, offset
) %
5192 sizeof (t_uscalar_t
)) != 0)
5194 if ((resftp
= getf(STRUCT_FGET(strfdinsert
, fildes
))) != NULL
) {
5195 if ((resstp
= resftp
->f_vnode
->v_stream
) == NULL
) {
5196 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5202 mutex_enter(&resstp
->sd_lock
);
5203 if (resstp
->sd_flag
& (STRDERR
|STWRERR
|STRHUP
|STPLEX
)) {
5204 error
= strgeterr(resstp
,
5205 STRDERR
|STWRERR
|STRHUP
|STPLEX
, 0);
5207 mutex_exit(&resstp
->sd_lock
);
5208 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5212 mutex_exit(&resstp
->sd_lock
);
5217 queue_t
*mate
= NULL
;
5219 /* get read queue of stream terminus */
5220 claimstr(resstp
->sd_wrq
);
5221 for (q
= resstp
->sd_wrq
->q_next
; q
->q_next
!= NULL
;
5223 if (!STRMATED(resstp
) && STREAM(q
) != resstp
&&
5225 ASSERT(q
->q_qinfo
->qi_srvp
);
5226 ASSERT(_OTHERQ(q
)->q_qinfo
->qi_srvp
);
5233 releasestr(resstp
->sd_wrq
);
5234 ival
= (t_uscalar_t
)q
;
5237 ival
= (t_uscalar_t
)getminor(resftp
->f_vnode
->v_rdev
);
5240 if (STRUCT_FGET(strfdinsert
, ctlbuf
.len
) <
5241 STRUCT_FGET(strfdinsert
, offset
) + sizeof (t_uscalar_t
)) {
5242 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5247 * Check for legal flag value.
5249 if (STRUCT_FGET(strfdinsert
, flags
) & ~RS_HIPRI
) {
5250 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5254 /* get these values from those cached in the stream head */
5255 mutex_enter(QLOCK(stp
->sd_wrq
));
5256 rmin
= stp
->sd_qn_minpsz
;
5257 rmax
= stp
->sd_qn_maxpsz
;
5258 mutex_exit(QLOCK(stp
->sd_wrq
));
5261 * Make sure ctl and data sizes together fall within
5262 * the limits of the max and min receive packet sizes
5263 * and do not exceed system limit. A negative data
5264 * length means that no data part is to be sent.
5266 ASSERT((rmax
>= 0) || (rmax
== INFPSZ
));
5268 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5271 if ((msgsize
= STRUCT_FGET(strfdinsert
, databuf
.len
)) < 0)
5273 if ((msgsize
< rmin
) ||
5274 ((msgsize
> rmax
) && (rmax
!= INFPSZ
)) ||
5275 (STRUCT_FGET(strfdinsert
, ctlbuf
.len
) > strctlsz
)) {
5276 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5280 mutex_enter(&stp
->sd_lock
);
5281 while (!(STRUCT_FGET(strfdinsert
, flags
) & RS_HIPRI
) &&
5282 !canputnext(stp
->sd_wrq
)) {
5283 if ((error
= strwaitq(stp
, WRITEWAIT
, (ssize_t
)0,
5284 flag
, -1, &done
)) != 0 || done
) {
5285 mutex_exit(&stp
->sd_lock
);
5286 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5289 if ((error
= i_straccess(stp
, access
)) != 0) {
5290 mutex_exit(&stp
->sd_lock
);
5292 STRUCT_FGET(strfdinsert
, fildes
));
5296 mutex_exit(&stp
->sd_lock
);
5299 * Copy strfdinsert.ctlbuf into native form of
5300 * ctlbuf to pass down into strmakemsg().
5302 mctl
.maxlen
= STRUCT_FGET(strfdinsert
, ctlbuf
.maxlen
);
5303 mctl
.len
= STRUCT_FGET(strfdinsert
, ctlbuf
.len
);
5304 mctl
.buf
= STRUCT_FGETP(strfdinsert
, ctlbuf
.buf
);
5306 iov
.iov_base
= STRUCT_FGETP(strfdinsert
, databuf
.buf
);
5307 iov
.iov_len
= STRUCT_FGET(strfdinsert
, databuf
.len
);
5310 uio
.uio_loffset
= 0;
5311 uio
.uio_segflg
= (copyflag
== U_TO_K
) ? UIO_USERSPACE
:
5314 uio
.uio_extflg
= UIO_COPY_CACHED
;
5315 uio
.uio_resid
= iov
.iov_len
;
5316 if ((error
= strmakemsg(&mctl
,
5317 &msgsize
, &uio
, stp
,
5318 STRUCT_FGET(strfdinsert
, flags
), &mp
)) != 0 || !mp
) {
5319 STRUCT_FSET(strfdinsert
, databuf
.len
, msgsize
);
5320 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5324 STRUCT_FSET(strfdinsert
, databuf
.len
, msgsize
);
5327 * Place the possibly reencoded queue pointer 'offset' bytes
5328 * from the start of the control portion of the message.
5330 *((t_uscalar_t
*)(mp
->b_rptr
+
5331 STRUCT_FGET(strfdinsert
, offset
))) = ival
;
5334 * Put message downstream.
5336 stream_willservice(stp
);
5337 putnext(stp
->sd_wrq
, mp
);
5338 stream_runservice(stp
);
5339 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5347 if ((fp
= getf((int)arg
)) == NULL
)
5349 error
= do_sendfp(stp
, fp
, crp
);
5357 struct k_strrecvfd
*srf
;
5360 mutex_enter(&stp
->sd_lock
);
5361 while (!(mp
= getq(rdq
))) {
5362 if (stp
->sd_flag
& (STRHUP
|STREOF
)) {
5363 mutex_exit(&stp
->sd_lock
);
5366 if ((error
= strwaitq(stp
, GETWAIT
, (ssize_t
)0,
5367 flag
, -1, &done
)) != 0 || done
) {
5368 mutex_exit(&stp
->sd_lock
);
5371 if ((error
= i_straccess(stp
, access
)) != 0) {
5372 mutex_exit(&stp
->sd_lock
);
5376 if (mp
->b_datap
->db_type
!= M_PASSFP
) {
5377 putback(stp
, rdq
, mp
, mp
->b_band
);
5378 mutex_exit(&stp
->sd_lock
);
5381 mutex_exit(&stp
->sd_lock
);
5383 srf
= (struct k_strrecvfd
*)mp
->b_rptr
;
5384 if ((fd
= ufalloc(0)) == -1) {
5385 mutex_enter(&stp
->sd_lock
);
5386 putback(stp
, rdq
, mp
, mp
->b_band
);
5387 mutex_exit(&stp
->sd_lock
);
5390 if (cmd
== I_RECVFD
) {
5391 struct o_strrecvfd ostrfd
;
5393 /* check to see if uid/gid values are too large. */
5395 if (srf
->uid
> (o_uid_t
)USHRT_MAX
||
5396 srf
->gid
> (o_gid_t
)USHRT_MAX
) {
5397 mutex_enter(&stp
->sd_lock
);
5398 putback(stp
, rdq
, mp
, mp
->b_band
);
5399 mutex_exit(&stp
->sd_lock
);
5400 setf(fd
, NULL
); /* release fd entry */
5405 ostrfd
.uid
= (o_uid_t
)srf
->uid
;
5406 ostrfd
.gid
= (o_gid_t
)srf
->gid
;
5408 /* Null the filler bits */
5409 for (i
= 0; i
< 8; i
++)
5412 error
= strcopyout(&ostrfd
, (void *)arg
,
5413 sizeof (struct o_strrecvfd
), copyflag
);
5414 } else { /* I_E_RECVFD */
5415 struct strrecvfd strfd
;
5418 strfd
.uid
= srf
->uid
;
5419 strfd
.gid
= srf
->gid
;
5421 /* null the filler bits */
5422 for (i
= 0; i
< 8; i
++)
5425 error
= strcopyout(&strfd
, (void *)arg
,
5426 sizeof (struct strrecvfd
), copyflag
);
5430 setf(fd
, NULL
); /* release fd entry */
5431 mutex_enter(&stp
->sd_lock
);
5432 putback(stp
, rdq
, mp
, mp
->b_band
);
5433 mutex_exit(&stp
->sd_lock
);
5438 * Always increment f_count since the freemsg() below will
5439 * always call free_passfp() which performs a closef().
5441 mutex_enter(&srf
->fp
->f_tlock
);
5443 mutex_exit(&srf
->fp
->f_tlock
);
5451 * Set/clear the write options. arg is a bit
5452 * mask with any of the following bits set...
5453 * SNDZERO - send zero length message
5454 * SNDPIPE - send sigpipe to process if
5455 * sd_werror is set and process is
5456 * doing a write or putmsg.
5457 * The new stream head write options should reflect
5460 if (arg
& ~(SNDZERO
|SNDPIPE
))
5463 mutex_enter(&stp
->sd_lock
);
5464 stp
->sd_wput_opt
&= ~(SW_SIGPIPE
|SW_SNDZERO
);
5466 stp
->sd_wput_opt
|= SW_SNDZERO
;
5468 stp
->sd_wput_opt
|= SW_SIGPIPE
;
5469 mutex_exit(&stp
->sd_lock
);
5476 if (stp
->sd_wput_opt
& SW_SNDZERO
)
5478 if (stp
->sd_wput_opt
& SW_SIGPIPE
)
5480 return (strcopyout(&wropt
, (void *)arg
, sizeof (wropt
),
5486 * Returns all the modules found on this stream,
5487 * upto the driver. If argument is NULL, return the
5488 * number of modules (including driver). If argument
5489 * is not NULL, copy the names into the structure
5497 struct str_mlist
*mlist
;
5498 STRUCT_DECL(str_list
, strlist
);
5500 if (arg
== (intptr_t)NULL
) {
5501 /* Return number of modules plus driver */
5502 if (stp
->sd_vnode
->v_type
== VFIFO
)
5503 *rvalp
= stp
->sd_pushcnt
;
5505 *rvalp
= stp
->sd_pushcnt
+ 1;
5509 STRUCT_INIT(strlist
, flag
);
5511 error
= strcopyin((void *)arg
, STRUCT_BUF(strlist
),
5512 STRUCT_SIZE(strlist
), copyflag
);
5516 mlist
= STRUCT_FGETP(strlist
, sl_modlist
);
5517 nmods
= STRUCT_FGET(strlist
, sl_nmods
);
5521 claimstr(stp
->sd_wrq
);
5523 for (i
= 0; i
< nmods
&& _SAMESTR(q
); i
++, q
= q
->q_next
) {
5524 qname
= Q2NAME(q
->q_next
);
5525 error
= strcopyout(qname
, &mlist
[i
], strlen(qname
) + 1,
5528 releasestr(stp
->sd_wrq
);
5532 releasestr(stp
->sd_wrq
);
5533 return (strcopyout(&i
, (void *)arg
, sizeof (int), copyflag
));
5541 if ((arg
< 0) || (arg
>= NBAND
))
5543 q
= _RD(stp
->sd_wrq
);
5544 mutex_enter(QLOCK(q
));
5545 if (arg
> (int)q
->q_nband
) {
5563 mutex_exit(QLOCK(q
));
5572 q
= _RD(stp
->sd_wrq
);
5573 mutex_enter(QLOCK(q
));
5576 mutex_exit(QLOCK(q
));
5579 intpri
= (int)mp
->b_band
;
5580 error
= strcopyout(&intpri
, (void *)arg
, sizeof (int),
5582 mutex_exit(QLOCK(q
));
5590 if (arg
& ~(ANYMARK
|LASTMARK
))
5592 q
= _RD(stp
->sd_wrq
);
5593 mutex_enter(&stp
->sd_lock
);
5594 if ((stp
->sd_flag
& STRATMARK
) && (arg
== ANYMARK
)) {
5597 mutex_enter(QLOCK(q
));
5602 else if ((arg
== ANYMARK
) && (mp
->b_flag
& MSGMARK
))
5604 else if ((arg
== LASTMARK
) && (mp
== stp
->sd_mark
))
5608 mutex_exit(QLOCK(q
));
5610 mutex_exit(&stp
->sd_lock
);
5618 if ((arg
< 0) || (arg
>= NBAND
))
5621 *rvalp
= bcanputnext(stp
->sd_wrq
, band
);
5629 error
= strcopyin((void *)arg
, &closetime
, sizeof (int),
5636 stp
->sd_closetime
= closetime
;
5644 closetime
= stp
->sd_closetime
;
5645 return (strcopyout(&closetime
, (void *)arg
, sizeof (int),
5653 mutex_enter(&stp
->sd_lock
);
5654 if (stp
->sd_sidp
== NULL
) {
5655 mutex_exit(&stp
->sd_lock
);
5658 sid
= stp
->sd_sidp
->pid_id
;
5659 mutex_exit(&stp
->sd_lock
);
5660 return (strcopyout(&sid
, (void *)arg
, sizeof (pid_t
),
5668 pid_t sid
, fg_pgid
, bg_pgid
;
5670 if (error
= strcopyin((void *)arg
, &pgrp
, sizeof (pid_t
),
5673 mutex_enter(&stp
->sd_lock
);
5674 mutex_enter(&pidlock
);
5675 if (stp
->sd_sidp
!= ttoproc(curthread
)->p_sessp
->s_sidp
) {
5676 mutex_exit(&pidlock
);
5677 mutex_exit(&stp
->sd_lock
);
5680 if (pgrp
== stp
->sd_pgidp
->pid_id
) {
5681 mutex_exit(&pidlock
);
5682 mutex_exit(&stp
->sd_lock
);
5685 if (pgrp
<= 0 || pgrp
>= maxpid
) {
5686 mutex_exit(&pidlock
);
5687 mutex_exit(&stp
->sd_lock
);
5690 if ((q
= pgfind(pgrp
)) == NULL
||
5691 q
->p_sessp
!= ttoproc(curthread
)->p_sessp
) {
5692 mutex_exit(&pidlock
);
5693 mutex_exit(&stp
->sd_lock
);
5696 sid
= stp
->sd_sidp
->pid_id
;
5697 fg_pgid
= q
->p_pgrp
;
5698 bg_pgid
= stp
->sd_pgidp
->pid_id
;
5699 CL_SET_PROCESS_GROUP(curthread
, sid
, bg_pgid
, fg_pgid
);
5700 PID_RELE(stp
->sd_pgidp
);
5701 ctty_clear_sighuped();
5702 stp
->sd_pgidp
= q
->p_pgidp
;
5703 PID_HOLD(stp
->sd_pgidp
);
5704 mutex_exit(&pidlock
);
5705 mutex_exit(&stp
->sd_lock
);
5713 mutex_enter(&stp
->sd_lock
);
5714 if (stp
->sd_sidp
== NULL
) {
5715 mutex_exit(&stp
->sd_lock
);
5718 pgrp
= stp
->sd_pgidp
->pid_id
;
5719 mutex_exit(&stp
->sd_lock
);
5720 return (strcopyout(&pgrp
, (void *)arg
, sizeof (pid_t
),
5726 return (strctty(stp
));
5731 /* freectty() always assumes curproc. */
5732 if (freectty(B_FALSE
) != 0)
5739 return (0); /* handled by the upper layer */
5744 * Custom free routine used for M_PASSFP messages.
5747 free_passfp(struct k_strrecvfd
*srf
)
5749 (void) closef(srf
->fp
);
5750 kmem_free(srf
, sizeof (struct k_strrecvfd
) + sizeof (frtn_t
));
5755 do_sendfp(struct stdata
*stp
, struct file
*fp
, struct cred
*cr
)
5757 queue_t
*qp
, *nextqp
;
5758 struct k_strrecvfd
*srf
;
5762 queue_t
*mate
= NULL
;
5766 if (stp
->sd_flag
& STRHUP
)
5769 claimstr(stp
->sd_wrq
);
5771 /* Fastpath, we have a pipe, and we are already mated, use it. */
5772 if (STRMATED(stp
)) {
5773 qp
= _RD(stp
->sd_mate
->sd_wrq
);
5776 } else { /* Not already mated. */
5779 * Walk the stream to the end of this one.
5780 * assumes that the claimstr() will prevent
5781 * plumbing between the stream head and the
5782 * driver from changing
5787 * Loop until we reach the end of this stream.
5788 * On completion, qp points to the write queue
5789 * at the end of the stream, or the read queue
5790 * at the stream head if this is a fifo.
5792 while (((qp
= qp
->q_next
) != NULL
) && _SAMESTR(qp
))
5796 * Just in case we get a q_next which is NULL, but
5797 * not at the end of the stream. This is actually
5798 * broken, so we set an assert to catch it in
5799 * debug, and set an error and return if not debug.
5803 releasestr(stp
->sd_wrq
);
5808 * Enter the syncq for the driver, so (hopefully)
5809 * the queue values will not change on us.
5810 * XXXX - This will only prevent the race IFF only
5811 * the write side modifies the q_next member, and
5812 * the put procedure is protected by at least
5815 if ((sq
= qp
->q_syncq
) != NULL
)
5816 entersq(sq
, SQ_PUT
);
5818 /* Now get the q_next value from this qp. */
5819 nextqp
= qp
->q_next
;
5822 * If nextqp exists and the other stream is different
5823 * from this one claim the stream, set the mate, and
5824 * get the read queue at the stream head of the other
5825 * stream. Assumes that nextqp was at least valid when
5826 * we got it. Hopefully the entersq of the driver
5827 * will prevent it from changing on us.
5829 if ((nextqp
!= NULL
) && (STREAM(nextqp
) != stp
)) {
5830 ASSERT(qp
->q_qinfo
->qi_srvp
);
5831 ASSERT(_OTHERQ(qp
)->q_qinfo
->qi_srvp
);
5832 ASSERT(_OTHERQ(qp
->q_next
)->q_qinfo
->qi_srvp
);
5835 /* Make sure we still have a q_next */
5836 if (nextqp
!= qp
->q_next
) {
5837 releasestr(stp
->sd_wrq
);
5842 qp
= _RD(STREAM(nextqp
)->sd_wrq
);
5845 /* If we entered the synq above, leave it. */
5847 leavesq(sq
, SQ_PUT
);
5848 } /* STRMATED(STP) */
5850 /* XXX prevents substitution of the ops vector */
5851 if (qp
->q_qinfo
!= &strdata
&& qp
->q_qinfo
!= &fifo_strdata
) {
5856 if (qp
->q_flag
& QFULL
) {
5862 * Since M_PASSFP messages include a file descriptor, we use
5863 * esballoc() and specify a custom free routine (free_passfp()) that
5864 * will close the descriptor as part of freeing the message. For
5865 * convenience, we stash the frtn_t right after the data block.
5867 bufsize
= sizeof (struct k_strrecvfd
) + sizeof (frtn_t
);
5868 srf
= kmem_alloc(bufsize
, KM_NOSLEEP
);
5874 frtnp
= (frtn_t
*)(srf
+ 1);
5875 frtnp
->free_arg
= (caddr_t
)srf
;
5876 frtnp
->free_func
= free_passfp
;
5878 mp
= esballoc((uchar_t
*)srf
, bufsize
, BPRI_MED
, frtnp
);
5880 kmem_free(srf
, bufsize
);
5884 mp
->b_wptr
+= sizeof (struct k_strrecvfd
);
5885 mp
->b_datap
->db_type
= M_PASSFP
;
5888 srf
->uid
= crgetuid(curthread
->t_cred
);
5889 srf
->gid
= crgetgid(curthread
->t_cred
);
5890 mutex_enter(&fp
->f_tlock
);
5892 mutex_exit(&fp
->f_tlock
);
5896 releasestr(stp
->sd_wrq
);
5903 * Send an ioctl message downstream and wait for acknowledgement.
5904 * flags may be set to either U_TO_K or K_TO_K and a combination
5905 * of STR_NOERROR or STR_NOSIG
5906 * STR_NOSIG: Signals are essentially ignored or held and have
5907 * no effect for the duration of the call.
5908 * STR_NOERROR: Ignores stream head read, write and hup errors.
5909 * Additionally, if an existing ioctl times out, it is assumed
5910 * lost and and this ioctl will continue as if the previous ioctl had
5911 * finished. ETIME may be returned if this ioctl times out (i.e.
5912 * ic_timout is not INFTIM). Non-stream head errors may be returned if
5913 * the ioc_error indicates that the driver/module had problems,
5914 * an EFAULT was found when accessing user data, a lack of
5920 struct strioctl
*strioc
,
5921 int fflags
, /* file flags with model info */
5927 struct iocblk
*iocbp
;
5928 struct copyreq
*reqp
;
5929 struct copyresp
*resp
;
5931 int transparent
= 0;
5935 int copyflag
= (flag
& (U_TO_K
| K_TO_K
));
5936 int sigflag
= (flag
& STR_NOSIG
);
5939 boolean_t set_iocwaitne
= B_FALSE
;
5941 ASSERT(copyflag
== U_TO_K
|| copyflag
== K_TO_K
);
5942 ASSERT((fflags
& FMODELS
) != 0);
5944 TRACE_2(TR_FAC_STREAMS_FR
,
5946 "strdoioctl:stp %p strioc %p", stp
, strioc
);
5947 if (strioc
->ic_len
== TRANSPARENT
) { /* send arg in M_DATA block */
5949 strioc
->ic_len
= sizeof (intptr_t);
5952 if (strioc
->ic_len
< 0 || (strmsgsz
> 0 && strioc
->ic_len
> strmsgsz
))
5955 if ((bp
= allocb_cred_wait(sizeof (union ioctypes
), sigflag
, &error
,
5956 crp
, curproc
->p_pid
)) == NULL
)
5959 bzero(bp
->b_wptr
, sizeof (union ioctypes
));
5961 iocbp
= (struct iocblk
*)bp
->b_wptr
;
5962 iocbp
->ioc_count
= strioc
->ic_len
;
5963 iocbp
->ioc_cmd
= strioc
->ic_cmd
;
5964 iocbp
->ioc_flag
= (fflags
& FMODELS
);
5967 iocbp
->ioc_cr
= crp
;
5968 DB_TYPE(bp
) = M_IOCTL
;
5969 bp
->b_wptr
+= sizeof (struct iocblk
);
5971 if (flag
& STR_NOERROR
)
5974 errs
= STRHUP
|STRDERR
|STWRERR
|STPLEX
;
5977 * If there is data to copy into ioctl block, do so.
5979 if (iocbp
->ioc_count
> 0) {
5982 * Note: STR_NOERROR does not have an effect
5985 id
= K_TO_K
| sigflag
;
5988 if ((error
= putiocd(bp
, strioc
->ic_dp
, id
, crp
)) != 0) {
5995 * We could have slept copying in user pages.
5996 * Recheck the stream head state (the other end
5997 * of a pipe could have gone away).
5999 if (stp
->sd_flag
& errs
) {
6000 mutex_enter(&stp
->sd_lock
);
6001 error
= strgeterr(stp
, errs
, 0);
6002 mutex_exit(&stp
->sd_lock
);
6011 iocbp
->ioc_count
= TRANSPARENT
;
6014 * Block for up to STRTIMOUT milliseconds if there is an outstanding
6015 * ioctl for this stream already running. All processes
6016 * sleeping here will be awakened as a result of an ACK
6017 * or NAK being received for the outstanding ioctl, or
6018 * as a result of the timer expiring on the outstanding
6019 * ioctl (a failure), or as a result of any waiting
6020 * process's timer expiring (also a failure).
6024 mutex_enter(&stp
->sd_lock
);
6025 while ((stp
->sd_flag
& IOCWAIT
) ||
6026 (!set_iocwaitne
&& (stp
->sd_flag
& IOCWAITNE
))) {
6029 TRACE_0(TR_FAC_STREAMS_FR
,
6031 "strdoioctl sleeps - IOCWAIT");
6032 cv_rval
= str_cv_wait(&stp
->sd_iocmonitor
, &stp
->sd_lock
,
6033 STRTIMOUT
, sigflag
);
6038 if (flag
& STR_NOERROR
) {
6040 * Terminating current ioctl in
6041 * progress -- assume it got lost and
6042 * wake up the other thread so that the
6043 * operation completes.
6045 if (!(stp
->sd_flag
& IOCWAITNE
)) {
6046 set_iocwaitne
= B_TRUE
;
6047 stp
->sd_flag
|= IOCWAITNE
;
6048 cv_broadcast(&stp
->sd_monitor
);
6051 * Otherwise, there's a running
6052 * STR_NOERROR -- we have no choice
6053 * here but to wait forever (or until
6058 * pending ioctl has caused
6064 } else if ((stp
->sd_flag
& errs
)) {
6065 error
= strgeterr(stp
, errs
, 0);
6068 mutex_exit(&stp
->sd_lock
);
6076 * Have control of ioctl mechanism.
6077 * Send down ioctl packet and wait for response.
6079 if (stp
->sd_iocblk
!= (mblk_t
*)-1) {
6080 freemsg(stp
->sd_iocblk
);
6082 stp
->sd_iocblk
= NULL
;
6085 * If this is marked with 'noerror' (internal; mostly
6086 * I_{P,}{UN,}LINK), then make sure nobody else is able to get
6087 * in here by setting IOCWAITNE.
6089 waitflags
= IOCWAIT
;
6090 if (flag
& STR_NOERROR
)
6091 waitflags
|= IOCWAITNE
;
6093 stp
->sd_flag
|= waitflags
;
6096 * Assign sequence number.
6098 iocbp
->ioc_id
= stp
->sd_iocid
= getiocseqno();
6100 mutex_exit(&stp
->sd_lock
);
6102 TRACE_1(TR_FAC_STREAMS_FR
,
6103 TR_STRDOIOCTL_PUT
, "strdoioctl put: stp %p", stp
);
6104 stream_willservice(stp
);
6105 putnext(stp
->sd_wrq
, bp
);
6106 stream_runservice(stp
);
6109 * Timed wait for acknowledgment. The wait time is limited by the
6110 * timeout value, which must be a positive integer (number of
6111 * milliseconds) to wait, or 0 (use default value of STRTIMOUT
6112 * milliseconds), or -1 (wait forever). This will be awakened
6113 * either by an ACK/NAK message arriving, the timer expiring, or
6114 * the timer expiring on another ioctl waiting for control of the
6118 mutex_enter(&stp
->sd_lock
);
6122 * If the reply has already arrived, don't sleep. If awakened from
6123 * the sleep, fail only if the reply has not arrived by then.
6124 * Otherwise, process the reply.
6126 while (!stp
->sd_iocblk
) {
6129 if (stp
->sd_flag
& errs
) {
6130 error
= strgeterr(stp
, errs
, 0);
6132 stp
->sd_flag
&= ~waitflags
;
6133 cv_broadcast(&stp
->sd_iocmonitor
);
6134 mutex_exit(&stp
->sd_lock
);
6140 TRACE_0(TR_FAC_STREAMS_FR
,
6141 TR_STRDOIOCTL_WAIT2
,
6142 "strdoioctl sleeps awaiting reply");
6145 cv_rval
= str_cv_wait(&stp
->sd_monitor
, &stp
->sd_lock
,
6146 (strioc
->ic_timout
?
6147 strioc
->ic_timout
* 1000 : STRTIMOUT
), sigflag
);
6150 * There are four possible cases here: interrupt, timeout,
6151 * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
6152 * valid M_IOCTL reply).
6154 * If we've been awakened by a STR_NOERROR ioctl on some other
6155 * thread, then sd_iocblk will still be NULL, and IOCWAITNE
6156 * will be set. Pretend as if we just timed out. Note that
6157 * this other thread waited at least STRTIMOUT before trying to
6158 * awaken our thread, so this is indistinguishable (even for
6159 * INFTIM) from the case where we failed with ETIME waiting on
6160 * IOCWAIT in the prior loop.
6162 if (cv_rval
> 0 && !(flag
& STR_NOERROR
) &&
6163 stp
->sd_iocblk
== NULL
&& (stp
->sd_flag
& IOCWAITNE
)) {
6168 * note: STR_NOERROR does not protect
6169 * us here.. use ic_timout < 0
6178 * A message could have come in after we were scheduled
6179 * but before we were actually run.
6181 bp
= stp
->sd_iocblk
;
6182 stp
->sd_iocblk
= NULL
;
6184 if ((bp
->b_datap
->db_type
== M_COPYIN
) ||
6185 (bp
->b_datap
->db_type
== M_COPYOUT
)) {
6186 mutex_exit(&stp
->sd_lock
);
6188 freemsg(bp
->b_cont
);
6191 bp
->b_datap
->db_type
= M_IOCDATA
;
6192 bp
->b_wptr
= bp
->b_rptr
+
6193 sizeof (struct copyresp
);
6194 resp
= (struct copyresp
*)bp
->b_rptr
;
6196 (caddr_t
)1; /* failure */
6197 stream_willservice(stp
);
6198 putnext(stp
->sd_wrq
, bp
);
6199 stream_runservice(stp
);
6200 mutex_enter(&stp
->sd_lock
);
6205 stp
->sd_flag
&= ~waitflags
;
6206 cv_broadcast(&stp
->sd_iocmonitor
);
6207 mutex_exit(&stp
->sd_lock
);
6212 bp
= stp
->sd_iocblk
;
6214 * Note: it is strictly impossible to get here with sd_iocblk set to
6215 * -1. This is because the initial loop above doesn't allow any new
6216 * ioctls into the fray until all others have passed this point.
6218 ASSERT(bp
!= NULL
&& bp
!= (mblk_t
*)-1);
6219 TRACE_1(TR_FAC_STREAMS_FR
,
6220 TR_STRDOIOCTL_ACK
, "strdoioctl got reply: bp %p", bp
);
6221 if ((bp
->b_datap
->db_type
== M_IOCACK
) ||
6222 (bp
->b_datap
->db_type
== M_IOCNAK
)) {
6223 /* for detection of duplicate ioctl replies */
6224 stp
->sd_iocblk
= (mblk_t
*)-1;
6225 stp
->sd_flag
&= ~waitflags
;
6226 cv_broadcast(&stp
->sd_iocmonitor
);
6227 mutex_exit(&stp
->sd_lock
);
6230 * flags not cleared here because we're still doing
6231 * copy in/out for ioctl.
6233 stp
->sd_iocblk
= NULL
;
6234 mutex_exit(&stp
->sd_lock
);
6239 * Have received acknowledgment.
6242 switch (bp
->b_datap
->db_type
) {
6247 iocbp
= (struct iocblk
*)bp
->b_rptr
;
6250 * Set error if indicated.
6252 if (iocbp
->ioc_error
) {
6253 error
= iocbp
->ioc_error
;
6260 *rvalp
= iocbp
->ioc_rval
;
6263 * Data may have been returned in ACK message (ioc_count > 0).
6264 * If so, copy it out to the user's buffer.
6266 if (iocbp
->ioc_count
&& !transparent
) {
6267 if (error
= getiocd(bp
, strioc
->ic_dp
, copyflag
))
6271 if (len
) /* an M_COPYOUT was used with I_STR */
6272 strioc
->ic_len
= len
;
6274 strioc
->ic_len
= (int)iocbp
->ioc_count
;
6282 * The only thing to do is set error as specified
6283 * in neg ack packet.
6285 iocbp
= (struct iocblk
*)bp
->b_rptr
;
6287 error
= (iocbp
->ioc_error
? iocbp
->ioc_error
: EINVAL
);
6292 * Driver or module has requested user ioctl data.
6294 reqp
= (struct copyreq
*)bp
->b_rptr
;
6297 * M_COPYIN should *never* have a message attached, though
6298 * it's harmless if it does -- thus, panic on a DEBUG
6299 * kernel and just free it on a non-DEBUG build.
6301 ASSERT(bp
->b_cont
== NULL
);
6302 if (bp
->b_cont
!= NULL
) {
6303 freemsg(bp
->b_cont
);
6307 error
= putiocd(bp
, reqp
->cq_addr
, flag
, crp
);
6308 if (error
&& bp
->b_cont
) {
6309 freemsg(bp
->b_cont
);
6313 bp
->b_wptr
= bp
->b_rptr
+ sizeof (struct copyresp
);
6314 bp
->b_datap
->db_type
= M_IOCDATA
;
6316 mblk_setcred(bp
, crp
, curproc
->p_pid
);
6317 resp
= (struct copyresp
*)bp
->b_rptr
;
6318 resp
->cp_rval
= (caddr_t
)(uintptr_t)error
;
6319 resp
->cp_flag
= (fflags
& FMODELS
);
6321 stream_willservice(stp
);
6322 putnext(stp
->sd_wrq
, bp
);
6323 stream_runservice(stp
);
6326 mutex_enter(&stp
->sd_lock
);
6327 stp
->sd_flag
&= ~waitflags
;
6328 cv_broadcast(&stp
->sd_iocmonitor
);
6329 mutex_exit(&stp
->sd_lock
);
6338 * Driver or module has ioctl data for a user.
6340 reqp
= (struct copyreq
*)bp
->b_rptr
;
6341 ASSERT(bp
->b_cont
!= NULL
);
6344 * Always (transparent or non-transparent )
6345 * use the address specified in the request
6347 taddr
= reqp
->cq_addr
;
6349 len
= (int)reqp
->cq_size
;
6351 /* copyout data to the provided address */
6352 error
= getiocd(bp
, taddr
, copyflag
);
6354 freemsg(bp
->b_cont
);
6357 bp
->b_wptr
= bp
->b_rptr
+ sizeof (struct copyresp
);
6358 bp
->b_datap
->db_type
= M_IOCDATA
;
6360 mblk_setcred(bp
, crp
, curproc
->p_pid
);
6361 resp
= (struct copyresp
*)bp
->b_rptr
;
6362 resp
->cp_rval
= (caddr_t
)(uintptr_t)error
;
6363 resp
->cp_flag
= (fflags
& FMODELS
);
6365 stream_willservice(stp
);
6366 putnext(stp
->sd_wrq
, bp
);
6367 stream_runservice(stp
);
6370 mutex_enter(&stp
->sd_lock
);
6371 stp
->sd_flag
&= ~waitflags
;
6372 cv_broadcast(&stp
->sd_iocmonitor
);
6373 mutex_exit(&stp
->sd_lock
);
6381 mutex_enter(&stp
->sd_lock
);
6382 stp
->sd_flag
&= ~waitflags
;
6383 cv_broadcast(&stp
->sd_iocmonitor
);
6384 mutex_exit(&stp
->sd_lock
);
6394 * Send an M_CMD message downstream and wait for a reply. This is a ptools
6395 * special used to retrieve information from modules/drivers a stream without
6396 * being subjected to flow control or interfering with pending messages on the
6397 * stream (e.g. an ioctl in flight).
6400 strdocmd(struct stdata
*stp
, struct strcmd
*scp
, cred_t
*crp
)
6403 struct cmdblk
*cmdp
;
6405 int errs
= STRHUP
|STRDERR
|STWRERR
|STPLEX
;
6406 clock_t rval
, timeout
= STRTIMOUT
;
6408 if (scp
->sc_len
< 0 || scp
->sc_len
> sizeof (scp
->sc_buf
) ||
6409 scp
->sc_timeout
< -1)
6412 if (scp
->sc_timeout
> 0)
6413 timeout
= scp
->sc_timeout
* MILLISEC
;
6415 if ((mp
= allocb_cred(sizeof (struct cmdblk
), crp
,
6416 curproc
->p_pid
)) == NULL
)
6421 cmdp
= (struct cmdblk
*)mp
->b_wptr
;
6423 cmdp
->cb_cmd
= scp
->sc_cmd
;
6424 cmdp
->cb_len
= scp
->sc_len
;
6426 mp
->b_wptr
+= sizeof (struct cmdblk
);
6428 DB_TYPE(mp
) = M_CMD
;
6429 DB_CPID(mp
) = curproc
->p_pid
;
6432 * Copy in the payload.
6434 if (cmdp
->cb_len
> 0) {
6435 mp
->b_cont
= allocb_cred(sizeof (scp
->sc_buf
), crp
,
6437 if (mp
->b_cont
== NULL
) {
6442 /* cb_len comes from sc_len, which has already been checked */
6443 ASSERT(cmdp
->cb_len
<= sizeof (scp
->sc_buf
));
6444 (void) bcopy(scp
->sc_buf
, mp
->b_cont
->b_wptr
, cmdp
->cb_len
);
6445 mp
->b_cont
->b_wptr
+= cmdp
->cb_len
;
6446 DB_CPID(mp
->b_cont
) = curproc
->p_pid
;
6450 * Since this mechanism is strictly for ptools, and since only one
6451 * process can be grabbed at a time, we simply fail if there's
6452 * currently an operation pending.
6454 mutex_enter(&stp
->sd_lock
);
6455 if (stp
->sd_flag
& STRCMDWAIT
) {
6456 mutex_exit(&stp
->sd_lock
);
6460 stp
->sd_flag
|= STRCMDWAIT
;
6461 ASSERT(stp
->sd_cmdblk
== NULL
);
6462 mutex_exit(&stp
->sd_lock
);
6464 putnext(stp
->sd_wrq
, mp
);
6468 * Timed wait for acknowledgment. If the reply has already arrived,
6469 * don't sleep. If awakened from the sleep, fail only if the reply
6470 * has not arrived by then. Otherwise, process the reply.
6472 mutex_enter(&stp
->sd_lock
);
6473 while (stp
->sd_cmdblk
== NULL
) {
6474 if (stp
->sd_flag
& errs
) {
6475 if ((error
= strgeterr(stp
, errs
, 0)) != 0)
6479 rval
= str_cv_wait(&stp
->sd_monitor
, &stp
->sd_lock
, timeout
, 0);
6480 if (stp
->sd_cmdblk
!= NULL
)
6484 error
= (rval
== 0) ? EINTR
: ETIME
;
6490 * We received a reply.
6492 mp
= stp
->sd_cmdblk
;
6493 stp
->sd_cmdblk
= NULL
;
6494 ASSERT(mp
!= NULL
&& DB_TYPE(mp
) == M_CMD
);
6495 ASSERT(stp
->sd_flag
& STRCMDWAIT
);
6496 stp
->sd_flag
&= ~STRCMDWAIT
;
6497 mutex_exit(&stp
->sd_lock
);
6499 cmdp
= (struct cmdblk
*)mp
->b_rptr
;
6500 if ((error
= cmdp
->cb_error
) != 0)
6504 * Data may have been returned in the reply (cb_len > 0).
6505 * If so, copy it out to the user's buffer.
6507 if (cmdp
->cb_len
> 0) {
6508 if (mp
->b_cont
== NULL
|| MBLKL(mp
->b_cont
) < cmdp
->cb_len
) {
6513 cmdp
->cb_len
= MIN(cmdp
->cb_len
, sizeof (scp
->sc_buf
));
6514 (void) bcopy(mp
->b_cont
->b_rptr
, scp
->sc_buf
, cmdp
->cb_len
);
6516 scp
->sc_len
= cmdp
->cb_len
;
6522 ASSERT(stp
->sd_cmdblk
== NULL
);
6523 stp
->sd_flag
&= ~STRCMDWAIT
;
6524 mutex_exit(&stp
->sd_lock
);
6530 * For the SunOS keyboard driver.
6531 * Return the next available "ioctl" sequence number.
6532 * Exported, so that streams modules can send "ioctl" messages
6533 * downstream from their open routine.
6540 mutex_enter(&strresources
);
6542 mutex_exit(&strresources
);
6547 * Get the next message from the read queue. If the message is
6548 * priority, STRPRI will have been set by strrput(). This flag
6549 * should be reset only when the entire message at the front of the
6550 * queue as been consumed.
6552 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
6557 struct strbuf
*mctl
,
6558 struct strbuf
*mdata
,
6559 unsigned char *prip
,
6566 mblk_t
*savemp
= NULL
;
6567 mblk_t
*savemptail
= NULL
;
6573 uint_t mark
; /* Contains MSG*MARK and _LASTMARK */
6574 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
6575 unsigned char pri
= 0;
6577 int pr
= 0; /* Partial read successful */
6579 struct uio
*uiop
= &uios
;
6583 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRGETMSG_ENTER
,
6584 "strgetmsg:%p", vp
);
6586 ASSERT(vp
->v_stream
);
6590 mutex_enter(&stp
->sd_lock
);
6592 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
6593 mutex_exit(&stp
->sd_lock
);
6597 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
6598 error
= strgeterr(stp
, STRDERR
|STPLEX
, 0);
6600 mutex_exit(&stp
->sd_lock
);
6604 mutex_exit(&stp
->sd_lock
);
6620 * Setup uio and iov for data part
6622 iovs
.iov_base
= mdata
->buf
;
6623 iovs
.iov_len
= mdata
->maxlen
;
6624 uios
.uio_iov
= &iovs
;
6625 uios
.uio_iovcnt
= 1;
6626 uios
.uio_loffset
= 0;
6627 uios
.uio_segflg
= UIO_USERSPACE
;
6629 uios
.uio_extflg
= UIO_COPY_CACHED
;
6630 uios
.uio_resid
= mdata
->maxlen
;
6631 uios
.uio_offset
= 0;
6633 q
= _RD(stp
->sd_wrq
);
6634 mutex_enter(&stp
->sd_lock
);
6635 old_sd_flag
= stp
->sd_flag
;
6639 mblk_t
*q_first
= q
->q_first
;
6642 * Get the next message of appropriate priority
6643 * from the stream head. If the caller is interested
6644 * in band or hipri messages, then they should already
6645 * be enqueued at the stream head. On the other hand
6646 * if the caller wants normal (band 0) messages, they
6647 * might be deferred in a synchronous stream and they
6648 * will need to be pulled up.
6650 * After we have dequeued a message, we might find that
6651 * it was a deferred M_SIG that was enqueued at the
6652 * stream head. It must now be posted as part of the
6653 * read by calling strsignal_nolock().
6655 * Also note that strrput does not enqueue an M_PCSIG,
6656 * and there cannot be more than one hipri message,
6657 * so there was no need to have the M_PCSIG case.
6659 * At some time it might be nice to try and wrap the
6660 * functionality of kstrgetmsg() and strgetmsg() into
6661 * a common routine so to reduce the amount of replicated
6662 * code (since they are extremely similar).
6664 if (!(*flagsp
& (MSG_HIPRI
|MSG_BAND
))) {
6665 /* Asking for normal, band0 data */
6666 bp
= strget(stp
, q
, uiop
, first
, &error
);
6667 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
6669 if (DB_TYPE(bp
) == M_SIG
) {
6670 strsignal_nolock(stp
, *bp
->b_rptr
,
6682 * We can't depend on the value of STRPRI here because
6683 * the stream head may be in transit. Therefore, we
6684 * must look at the type of the first message to
6685 * determine if a high priority messages is waiting
6687 } else if ((*flagsp
& MSG_HIPRI
) && q_first
!= NULL
&&
6688 DB_TYPE(q_first
) >= QPCTL
&&
6689 (bp
= getq_noenab(q
, 0)) != NULL
) {
6690 /* Asked for HIPRI and got one */
6691 ASSERT(DB_TYPE(bp
) >= QPCTL
);
6693 } else if ((*flagsp
& MSG_BAND
) && q_first
!= NULL
&&
6694 ((q_first
->b_band
>= *prip
) || DB_TYPE(q_first
) >= QPCTL
) &&
6695 (bp
= getq_noenab(q
, 0)) != NULL
) {
6697 * Asked for at least band "prip" and got either at
6698 * least that band or a hipri message.
6700 ASSERT(bp
->b_band
>= *prip
|| DB_TYPE(bp
) >= QPCTL
);
6701 if (DB_TYPE(bp
) == M_SIG
) {
6702 strsignal_nolock(stp
, *bp
->b_rptr
, bp
->b_band
);
6710 /* No data. Time to sleep? */
6714 * If STRHUP or STREOF, return 0 length control and data.
6715 * If resid is 0, then a read(fd,buf,0) was done. Do not
6716 * sleep to satisfy this request because by default we have
6717 * zero bytes to return.
6719 if ((stp
->sd_flag
& (STRHUP
|STREOF
)) || (mctl
->maxlen
== 0 &&
6720 mdata
->maxlen
== 0)) {
6721 mctl
->len
= mdata
->len
= 0;
6723 mutex_exit(&stp
->sd_lock
);
6726 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRGETMSG_WAIT
,
6727 "strgetmsg calls strwaitq:%p, %p",
6729 if (((error
= strwaitq(stp
, GETWAIT
, (ssize_t
)0, fmode
, -1,
6730 &done
)) != 0) || done
) {
6731 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRGETMSG_DONE
,
6732 "strgetmsg error or done:%p, %p",
6734 mutex_exit(&stp
->sd_lock
);
6737 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRGETMSG_AWAKE
,
6738 "strgetmsg awakes:%p, %p", vp
, uiop
);
6739 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
6740 mutex_exit(&stp
->sd_lock
);
6747 * Extract any mark information. If the message is not completely
6748 * consumed this information will be put in the mblk
6750 * If MSGMARKNEXT is set and the message is completely consumed
6751 * the STRATMARK flag will be set below. Likewise, if
6752 * MSGNOTMARKNEXT is set and the message is
6753 * completely consumed STRNOTATMARK will be set.
6755 mark
= bp
->b_flag
& (MSGMARK
| MSGMARKNEXT
| MSGNOTMARKNEXT
);
6756 ASSERT((mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
)) !=
6757 (MSGMARKNEXT
|MSGNOTMARKNEXT
));
6758 if (mark
!= 0 && bp
== stp
->sd_mark
) {
6760 stp
->sd_mark
= NULL
;
6763 * keep track of the original message type and priority
6766 type
= bp
->b_datap
->db_type
;
6767 if (type
== M_PASSFP
) {
6768 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
6770 bp
->b_flag
|= mark
& ~_LASTMARK
;
6771 putback(stp
, q
, bp
, pri
);
6772 qbackenable(q
, pri
);
6773 mutex_exit(&stp
->sd_lock
);
6776 ASSERT(type
!= M_SIG
);
6779 * Set this flag so strrput will not generate signals. Need to
6780 * make sure this flag is cleared before leaving this routine
6781 * else signals will stop being sent.
6783 stp
->sd_flag
|= STRGETINPROG
;
6784 mutex_exit(&stp
->sd_lock
);
6786 if (STREAM_NEEDSERVICE(stp
))
6787 stream_runservice(stp
);
6790 * Set HIPRI flag if message is priority.
6798 * First process PROTO or PCPROTO blocks, if any.
6800 if (mctl
->maxlen
>= 0 && type
!= M_DATA
) {
6804 bcnt
= mctl
->maxlen
;
6806 while (bp
!= NULL
&& bp
->b_datap
->db_type
!= M_DATA
) {
6807 if ((n
= MIN(bcnt
, bp
->b_wptr
- bp
->b_rptr
)) != 0 &&
6808 copyout(bp
->b_rptr
, ubuf
, n
)) {
6810 mutex_enter(&stp
->sd_lock
);
6812 * clear stream head pri flag based on
6813 * first message type
6815 if (type
>= QPCTL
) {
6816 ASSERT(type
== M_PCPROTO
);
6817 stp
->sd_flag
&= ~STRPRI
;
6825 if (bp
->b_rptr
>= bp
->b_wptr
) {
6835 mctl
->len
= mctl
->maxlen
- bcnt
;
6839 if (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
6841 * More PROTO blocks in msg.
6845 while (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
6849 savemptail
->b_cont
= NULL
;
6853 * Now process DATA blocks, if any.
6855 if (mdata
->maxlen
>= 0 && bp
) {
6857 * struiocopyout will consume a potential zero-length
6858 * M_DATA even if uio_resid is zero.
6860 size_t oldresid
= uiop
->uio_resid
;
6862 bp
= struiocopyout(bp
, uiop
, &error
);
6864 mutex_enter(&stp
->sd_lock
);
6866 * clear stream head hi pri flag based on
6869 if (type
>= QPCTL
) {
6870 ASSERT(type
== M_PCPROTO
);
6871 stp
->sd_flag
&= ~STRPRI
;
6878 * (pr == 1) indicates a partial read.
6880 if (oldresid
> uiop
->uio_resid
)
6882 mdata
->len
= mdata
->maxlen
- uiop
->uio_resid
;
6886 if (bp
) { /* more data blocks in msg */
6889 savemptail
->b_cont
= bp
;
6894 mutex_enter(&stp
->sd_lock
);
6896 if (pr
&& (savemp
->b_datap
->db_type
== M_DATA
) &&
6897 msgnodata(savemp
)) {
6899 * Avoid queuing a zero-length tail part of
6900 * a message. pr=1 indicates that we read some of
6906 * clear stream head hi pri flag based on
6909 if (type
>= QPCTL
) {
6910 ASSERT(type
== M_PCPROTO
);
6911 stp
->sd_flag
&= ~STRPRI
;
6914 savemp
->b_band
= pri
;
6916 * If the first message was HIPRI and the one we're
6917 * putting back isn't, then clear STRPRI, otherwise
6918 * set STRPRI again. Note that we must set STRPRI
6919 * again since the flush logic in strrput_nondata()
6920 * may have cleared it while we had sd_lock dropped.
6922 if (type
>= QPCTL
) {
6923 ASSERT(type
== M_PCPROTO
);
6924 if (queclass(savemp
) < QPCTL
)
6925 stp
->sd_flag
&= ~STRPRI
;
6927 stp
->sd_flag
|= STRPRI
;
6928 } else if (queclass(savemp
) >= QPCTL
) {
6930 * The first message was not a HIPRI message,
6931 * but the one we are about to putback is.
6932 * For simplicitly, we do not allow for HIPRI
6933 * messages to be embedded in the message
6934 * body, so just force it to same type as
6937 ASSERT(type
== M_DATA
|| type
== M_PROTO
);
6938 ASSERT(savemp
->b_datap
->db_type
== M_PCPROTO
);
6939 savemp
->b_datap
->db_type
= type
;
6942 savemp
->b_flag
|= mark
& ~_LASTMARK
;
6943 if ((mark
& _LASTMARK
) &&
6944 (stp
->sd_mark
== NULL
)) {
6946 * If another marked message arrived
6947 * while sd_lock was not held sd_mark
6948 * would be non-NULL.
6950 stp
->sd_mark
= savemp
;
6953 putback(stp
, q
, savemp
, pri
);
6957 * The complete message was consumed.
6959 * If another M_PCPROTO arrived while sd_lock was not held
6960 * it would have been discarded since STRPRI was still set.
6962 * Move the MSG*MARKNEXT information
6963 * to the stream head just in case
6964 * the read queue becomes empty.
6965 * clear stream head hi pri flag based on
6968 * If the stream head was at the mark
6969 * (STRATMARK) before we dropped sd_lock above
6970 * and some data was consumed then we have
6971 * moved past the mark thus STRATMARK is
6972 * cleared. However, if a message arrived in
6973 * strrput during the copyout above causing
6974 * STRATMARK to be set we can not clear that
6977 if (type
>= QPCTL
) {
6978 ASSERT(type
== M_PCPROTO
);
6979 stp
->sd_flag
&= ~STRPRI
;
6981 if (mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
|MSGMARK
)) {
6982 if (mark
& MSGMARKNEXT
) {
6983 stp
->sd_flag
&= ~STRNOTATMARK
;
6984 stp
->sd_flag
|= STRATMARK
;
6985 } else if (mark
& MSGNOTMARKNEXT
) {
6986 stp
->sd_flag
&= ~STRATMARK
;
6987 stp
->sd_flag
|= STRNOTATMARK
;
6989 stp
->sd_flag
&= ~(STRATMARK
|STRNOTATMARK
);
6991 } else if (pr
&& (old_sd_flag
& STRATMARK
)) {
6992 stp
->sd_flag
&= ~STRATMARK
;
7000 * Getmsg cleanup processing - if the state of the queue has changed
7001 * some signals may need to be sent and/or poll awakened.
7004 qbackenable(q
, pri
);
7007 * We dropped the stream head lock above. Send all M_SIG messages
7008 * before processing stream head for SIGPOLL messages.
7010 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
7011 while ((bp
= q
->q_first
) != NULL
&&
7012 (bp
->b_datap
->db_type
== M_SIG
)) {
7014 * sd_lock is held so the content of the read queue can not
7018 ASSERT(bp
!= NULL
&& bp
->b_datap
->db_type
== M_SIG
);
7020 strsignal_nolock(stp
, *bp
->b_rptr
, bp
->b_band
);
7021 mutex_exit(&stp
->sd_lock
);
7023 if (STREAM_NEEDSERVICE(stp
))
7024 stream_runservice(stp
);
7025 mutex_enter(&stp
->sd_lock
);
7029 * stream head cannot change while we make the determination
7030 * whether or not to send a signal. Drop the flag to allow strrput
7031 * to send firstmsgsigs again.
7033 stp
->sd_flag
&= ~STRGETINPROG
;
7036 * If the type of message at the front of the queue changed
7037 * due to the receive the appropriate signals and pollwakeup events
7038 * are generated. The type of changes are:
7039 * Processed a hipri message, q_first is not hipri.
7040 * Processed a band X message, and q_first is band Y.
7041 * The generated signals and pollwakeups are identical to what
7042 * strrput() generates should the message that is now on q_first
7043 * arrive to an empty read queue.
7045 * Note: only strrput will send a signal for a hipri message.
7047 if ((bp
= q
->q_first
) != NULL
&& !(stp
->sd_flag
& STRPRI
)) {
7048 strsigset_t signals
= 0;
7049 strpollset_t pollwakeups
= 0;
7051 if (flg
& MSG_HIPRI
) {
7053 * Removed a hipri message. Regular data at
7054 * the front of the queue.
7056 if (bp
->b_band
== 0) {
7057 signals
= S_INPUT
| S_RDNORM
;
7058 pollwakeups
= POLLIN
| POLLRDNORM
;
7060 signals
= S_INPUT
| S_RDBAND
;
7061 pollwakeups
= POLLIN
| POLLRDBAND
;
7063 } else if (pri
!= bp
->b_band
) {
7065 * The band is different for the new q_first.
7067 if (bp
->b_band
== 0) {
7069 pollwakeups
= POLLIN
| POLLRDNORM
;
7072 pollwakeups
= POLLIN
| POLLRDBAND
;
7076 if (pollwakeups
!= 0) {
7077 if (pollwakeups
== (POLLIN
| POLLRDNORM
)) {
7078 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
7080 stp
->sd_rput_opt
&= ~SR_POLLIN
;
7082 mutex_exit(&stp
->sd_lock
);
7083 pollwakeup(&stp
->sd_pollist
, pollwakeups
);
7084 mutex_enter(&stp
->sd_lock
);
7088 if (stp
->sd_sigflags
& signals
)
7089 strsendsig(stp
->sd_siglist
, signals
, bp
->b_band
, 0);
7091 mutex_exit(&stp
->sd_lock
);
7099 * Get the next message from the read queue. If the message is
7100 * priority, STRPRI will have been set by strrput(). This flag
7101 * should be reset only when the entire message at the front of the
7102 * queue as been consumed.
7104 * If uiop is NULL all data is returned in mctlp.
7105 * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
7107 * The timeout parameter is in milliseconds; -1 for infinity.
7108 * This routine handles the consolidation private flags:
7109 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7110 * MSG_DELAYERROR Defer the error check until the queue is empty.
7111 * MSG_HOLDSIG Hold signals while waiting for data.
7112 * MSG_IPEEK Only peek at messages.
7113 * MSG_DISCARDTAIL Discard the tail M_DATA part of the message
7115 * MSG_NOMARK If the message is marked leave it on the queue.
7117 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
7124 unsigned char *prip
,
7131 mblk_t
*savemp
= NULL
;
7132 mblk_t
*savemptail
= NULL
;
7139 uint_t mark
; /* Contains MSG*MARK and _LASTMARK */
7140 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
7141 unsigned char pri
= 0;
7143 int pr
= 0; /* Partial read successful */
7146 TRACE_1(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_ENTER
,
7147 "kstrgetmsg:%p", vp
);
7149 ASSERT(vp
->v_stream
);
7153 mutex_enter(&stp
->sd_lock
);
7155 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
7156 mutex_exit(&stp
->sd_lock
);
7161 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
7162 if ((stp
->sd_flag
& STPLEX
) ||
7163 (flags
& (MSG_IGNERROR
|MSG_DELAYERROR
)) == 0) {
7164 error
= strgeterr(stp
, STRDERR
|STPLEX
,
7165 (flags
& MSG_IPEEK
));
7167 mutex_exit(&stp
->sd_lock
);
7172 mutex_exit(&stp
->sd_lock
);
7174 switch (flags
& (MSG_HIPRI
|MSG_ANY
|MSG_BAND
)) {
7189 q
= _RD(stp
->sd_wrq
);
7190 mutex_enter(&stp
->sd_lock
);
7191 old_sd_flag
= stp
->sd_flag
;
7197 mblk_t
*q_first
= q
->q_first
;
7200 * This section of the code operates just like the code
7201 * in strgetmsg(). There is a comment there about what
7204 if (!(flags
& (MSG_HIPRI
|MSG_BAND
))) {
7205 /* Asking for normal, band0 data */
7206 bp
= strget(stp
, q
, uiop
, first
, &error
);
7207 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
7209 if (DB_TYPE(bp
) == M_SIG
) {
7210 strsignal_nolock(stp
, *bp
->b_rptr
,
7222 * We can't depend on the value of STRPRI here because
7223 * the stream head may be in transit. Therefore, we
7224 * must look at the type of the first message to
7225 * determine if a high priority messages is waiting
7227 } else if ((flags
& MSG_HIPRI
) && q_first
!= NULL
&&
7228 DB_TYPE(q_first
) >= QPCTL
&&
7229 (bp
= getq_noenab(q
, 0)) != NULL
) {
7230 ASSERT(DB_TYPE(bp
) >= QPCTL
);
7232 } else if ((flags
& MSG_BAND
) && q_first
!= NULL
&&
7233 ((q_first
->b_band
>= *prip
) || DB_TYPE(q_first
) >= QPCTL
) &&
7234 (bp
= getq_noenab(q
, 0)) != NULL
) {
7236 * Asked for at least band "prip" and got either at
7237 * least that band or a hipri message.
7239 ASSERT(bp
->b_band
>= *prip
|| DB_TYPE(bp
) >= QPCTL
);
7240 if (DB_TYPE(bp
) == M_SIG
) {
7241 strsignal_nolock(stp
, *bp
->b_rptr
, bp
->b_band
);
7249 /* No data. Time to sleep? */
7253 * Delayed error notification?
7255 if ((stp
->sd_flag
& (STRDERR
|STPLEX
)) &&
7256 (flags
& (MSG_IGNERROR
|MSG_DELAYERROR
)) == MSG_DELAYERROR
) {
7257 error
= strgeterr(stp
, STRDERR
|STPLEX
,
7258 (flags
& MSG_IPEEK
));
7260 mutex_exit(&stp
->sd_lock
);
7266 * If STRHUP or STREOF, return 0 length control and data.
7267 * If a read(fd,buf,0) has been done, do not sleep, just
7270 * If mctlp == NULL and uiop == NULL, then the code will
7271 * do the strwaitq. This is an understood way of saying
7272 * sleep "polling" until a message is received.
7274 if ((stp
->sd_flag
& (STRHUP
|STREOF
)) ||
7275 (uiop
!= NULL
&& uiop
->uio_resid
== 0)) {
7279 mutex_exit(&stp
->sd_lock
);
7285 (MSG_HOLDSIG
|MSG_IGNERROR
|MSG_IPEEK
|MSG_DELAYERROR
)) {
7286 if (flags
& MSG_HOLDSIG
)
7287 waitflag
|= STR_NOSIG
;
7288 if (flags
& MSG_IGNERROR
)
7289 waitflag
|= STR_NOERROR
;
7290 if (flags
& MSG_IPEEK
)
7291 waitflag
|= STR_PEEK
;
7292 if (flags
& MSG_DELAYERROR
)
7293 waitflag
|= STR_DELAYERR
;
7296 fmode
= uiop
->uio_fmode
;
7300 TRACE_2(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_WAIT
,
7301 "kstrgetmsg calls strwaitq:%p, %p",
7303 if (((error
= strwaitq(stp
, waitflag
, (ssize_t
)0,
7304 fmode
, timout
, &done
))) != 0 || done
) {
7305 TRACE_2(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_DONE
,
7306 "kstrgetmsg error or done:%p, %p",
7308 mutex_exit(&stp
->sd_lock
);
7311 TRACE_2(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_AWAKE
,
7312 "kstrgetmsg awakes:%p, %p", vp
, uiop
);
7313 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
7314 mutex_exit(&stp
->sd_lock
);
7321 * Extract any mark information. If the message is not completely
7322 * consumed this information will be put in the mblk
7324 * If MSGMARKNEXT is set and the message is completely consumed
7325 * the STRATMARK flag will be set below. Likewise, if
7326 * MSGNOTMARKNEXT is set and the message is
7327 * completely consumed STRNOTATMARK will be set.
7329 mark
= bp
->b_flag
& (MSGMARK
| MSGMARKNEXT
| MSGNOTMARKNEXT
);
7330 ASSERT((mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
)) !=
7331 (MSGMARKNEXT
|MSGNOTMARKNEXT
));
7335 * If the caller doesn't want the mark return.
7336 * Used to implement MSG_WAITALL in sockets.
7338 if (flags
& MSG_NOMARK
) {
7339 putback(stp
, q
, bp
, pri
);
7340 qbackenable(q
, pri
);
7341 mutex_exit(&stp
->sd_lock
);
7342 return (EWOULDBLOCK
);
7344 if (bp
== stp
->sd_mark
) {
7346 stp
->sd_mark
= NULL
;
7351 * keep track of the first message type
7353 type
= bp
->b_datap
->db_type
;
7355 if (bp
->b_datap
->db_type
== M_PASSFP
) {
7356 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
7358 bp
->b_flag
|= mark
& ~_LASTMARK
;
7359 putback(stp
, q
, bp
, pri
);
7360 qbackenable(q
, pri
);
7361 mutex_exit(&stp
->sd_lock
);
7364 ASSERT(type
!= M_SIG
);
7366 if (flags
& MSG_IPEEK
) {
7368 * Clear any struioflag - we do the uiomove over again
7369 * when peeking since it simplifies the code.
7371 * Dup the message and put the original back on the queue.
7372 * If dupmsg() fails, try again with copymsg() to see if
7373 * there is indeed a shortage of memory. dupmsg() may fail
7374 * if db_ref in any of the messages reaches its limit.
7377 if ((nbp
= dupmsg(bp
)) == NULL
&& (nbp
= copymsg(bp
)) == NULL
) {
7379 * Restore the state of the stream head since we
7380 * need to drop sd_lock (strwaitbuf is sleeping).
7382 size_t size
= msgdsize(bp
);
7384 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
7386 bp
->b_flag
|= mark
& ~_LASTMARK
;
7387 putback(stp
, q
, bp
, pri
);
7388 mutex_exit(&stp
->sd_lock
);
7389 error
= strwaitbuf(size
, BPRI_HI
);
7392 * There is no net change to the queue thus
7393 * no need to qbackenable.
7400 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
7402 bp
->b_flag
|= mark
& ~_LASTMARK
;
7403 putback(stp
, q
, bp
, pri
);
7408 * Set this flag so strrput will not generate signals. Need to
7409 * make sure this flag is cleared before leaving this routine
7410 * else signals will stop being sent.
7412 stp
->sd_flag
|= STRGETINPROG
;
7413 mutex_exit(&stp
->sd_lock
);
7415 if ((stp
->sd_rputdatafunc
!= NULL
) && (DB_TYPE(bp
) == M_DATA
)) {
7416 mblk_t
*tmp
, *prevmp
;
7419 * Put first non-data mblk back to stream head and
7420 * cut the mblk chain so sd_rputdatafunc only sees
7421 * M_DATA mblks. We can skip the first mblk since it
7422 * is M_DATA according to the condition above.
7424 for (prevmp
= bp
, tmp
= bp
->b_cont
; tmp
!= NULL
;
7425 prevmp
= tmp
, tmp
= tmp
->b_cont
) {
7426 if (DB_TYPE(tmp
) != M_DATA
) {
7427 prevmp
->b_cont
= NULL
;
7428 mutex_enter(&stp
->sd_lock
);
7429 putback(stp
, q
, tmp
, tmp
->b_band
);
7430 mutex_exit(&stp
->sd_lock
);
7435 bp
= (stp
->sd_rputdatafunc
)(stp
->sd_vnode
, bp
,
7436 NULL
, NULL
, NULL
, NULL
);
7442 if (STREAM_NEEDSERVICE(stp
))
7443 stream_runservice(stp
);
7446 * Set HIPRI flag if message is priority.
7454 * First process PROTO or PCPROTO blocks, if any.
7456 if (mctlp
!= NULL
&& type
!= M_DATA
) {
7460 while (bp
->b_cont
&& bp
->b_cont
->b_datap
->db_type
!= M_DATA
)
7467 if (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
7469 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
7473 while (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
7477 savemptail
->b_cont
= NULL
;
7481 * Now process DATA blocks, if any.
7484 /* Append data to tail of mctlp */
7486 if (mctlp
!= NULL
) {
7487 mblk_t
**mpp
= mctlp
;
7489 while (*mpp
!= NULL
)
7490 mpp
= &((*mpp
)->b_cont
);
7494 } else if (uiop
->uio_resid
>= 0 && bp
) {
7495 size_t oldresid
= uiop
->uio_resid
;
7498 * If a streams message is likely to consist
7499 * of many small mblks, it is pulled up into
7500 * one continuous chunk of memory.
7501 * The size of the first mblk may be bogus because
7502 * successive read() calls on the socket reduce
7503 * the size of this mblk until it is exhausted
7504 * and then the code walks on to the next. Thus
7505 * the size of the mblk may not be the original size
7506 * that was passed up, it's simply a remainder
7507 * and hence can be very small without any
7508 * implication that the packet is badly fragmented.
7509 * So the size of the possible second mblk is
7510 * used to spot a badly fragmented packet.
7511 * see longer comment at top of page
7512 * by mblk_pull_len declaration.
7515 if (bp
->b_cont
!= NULL
&& MBLKL(bp
->b_cont
) < mblk_pull_len
) {
7516 (void) pullupmsg(bp
, -1);
7519 bp
= struiocopyout(bp
, uiop
, &error
);
7521 if (mctlp
!= NULL
) {
7526 mutex_enter(&stp
->sd_lock
);
7528 * clear stream head hi pri flag based on
7531 if (!(flags
& MSG_IPEEK
) && (type
>= QPCTL
)) {
7532 ASSERT(type
== M_PCPROTO
);
7533 stp
->sd_flag
&= ~STRPRI
;
7539 * (pr == 1) indicates a partial read.
7541 if (oldresid
> uiop
->uio_resid
)
7545 if (bp
) { /* more data blocks in msg */
7548 savemptail
->b_cont
= bp
;
7553 mutex_enter(&stp
->sd_lock
);
7555 if (flags
& (MSG_IPEEK
|MSG_DISCARDTAIL
)) {
7557 * When MSG_DISCARDTAIL is set or
7558 * when peeking discard any tail. When peeking this
7559 * is the tail of the dup that was copied out - the
7560 * message has already been putback on the queue.
7561 * Return MOREDATA to the caller even though the data
7562 * is discarded. This is used by sockets (to
7566 if (!(flags
& MSG_IPEEK
) && (type
>= QPCTL
)) {
7567 ASSERT(type
== M_PCPROTO
);
7568 stp
->sd_flag
&= ~STRPRI
;
7570 } else if (pr
&& (savemp
->b_datap
->db_type
== M_DATA
) &&
7571 msgnodata(savemp
)) {
7573 * Avoid queuing a zero-length tail part of
7574 * a message. pr=1 indicates that we read some of
7579 if (type
>= QPCTL
) {
7580 ASSERT(type
== M_PCPROTO
);
7581 stp
->sd_flag
&= ~STRPRI
;
7584 savemp
->b_band
= pri
;
7586 * If the first message was HIPRI and the one we're
7587 * putting back isn't, then clear STRPRI, otherwise
7588 * set STRPRI again. Note that we must set STRPRI
7589 * again since the flush logic in strrput_nondata()
7590 * may have cleared it while we had sd_lock dropped.
7593 if (type
>= QPCTL
) {
7594 ASSERT(type
== M_PCPROTO
);
7595 if (queclass(savemp
) < QPCTL
)
7596 stp
->sd_flag
&= ~STRPRI
;
7598 stp
->sd_flag
|= STRPRI
;
7599 } else if (queclass(savemp
) >= QPCTL
) {
7601 * The first message was not a HIPRI message,
7602 * but the one we are about to putback is.
7603 * For simplicitly, we do not allow for HIPRI
7604 * messages to be embedded in the message
7605 * body, so just force it to same type as
7608 ASSERT(type
== M_DATA
|| type
== M_PROTO
);
7609 ASSERT(savemp
->b_datap
->db_type
== M_PCPROTO
);
7610 savemp
->b_datap
->db_type
= type
;
7613 if ((mark
& _LASTMARK
) &&
7614 (stp
->sd_mark
== NULL
)) {
7616 * If another marked message arrived
7617 * while sd_lock was not held sd_mark
7618 * would be non-NULL.
7620 stp
->sd_mark
= savemp
;
7622 savemp
->b_flag
|= mark
& ~_LASTMARK
;
7624 putback(stp
, q
, savemp
, pri
);
7626 } else if (!(flags
& MSG_IPEEK
)) {
7628 * The complete message was consumed.
7630 * If another M_PCPROTO arrived while sd_lock was not held
7631 * it would have been discarded since STRPRI was still set.
7633 * Move the MSG*MARKNEXT information
7634 * to the stream head just in case
7635 * the read queue becomes empty.
7636 * clear stream head hi pri flag based on
7639 * If the stream head was at the mark
7640 * (STRATMARK) before we dropped sd_lock above
7641 * and some data was consumed then we have
7642 * moved past the mark thus STRATMARK is
7643 * cleared. However, if a message arrived in
7644 * strrput during the copyout above causing
7645 * STRATMARK to be set we can not clear that
7647 * XXX A "perimeter" would help by single-threading strrput,
7648 * strread, strgetmsg and kstrgetmsg.
7650 if (type
>= QPCTL
) {
7651 ASSERT(type
== M_PCPROTO
);
7652 stp
->sd_flag
&= ~STRPRI
;
7654 if (mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
|MSGMARK
)) {
7655 if (mark
& MSGMARKNEXT
) {
7656 stp
->sd_flag
&= ~STRNOTATMARK
;
7657 stp
->sd_flag
|= STRATMARK
;
7658 } else if (mark
& MSGNOTMARKNEXT
) {
7659 stp
->sd_flag
&= ~STRATMARK
;
7660 stp
->sd_flag
|= STRNOTATMARK
;
7662 stp
->sd_flag
&= ~(STRATMARK
|STRNOTATMARK
);
7664 } else if (pr
&& (old_sd_flag
& STRATMARK
)) {
7665 stp
->sd_flag
&= ~STRATMARK
;
7673 * Getmsg cleanup processing - if the state of the queue has changed
7674 * some signals may need to be sent and/or poll awakened.
7677 qbackenable(q
, pri
);
7680 * We dropped the stream head lock above. Send all M_SIG messages
7681 * before processing stream head for SIGPOLL messages.
7683 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
7684 while ((bp
= q
->q_first
) != NULL
&&
7685 (bp
->b_datap
->db_type
== M_SIG
)) {
7687 * sd_lock is held so the content of the read queue can not
7691 ASSERT(bp
!= NULL
&& bp
->b_datap
->db_type
== M_SIG
);
7693 strsignal_nolock(stp
, *bp
->b_rptr
, bp
->b_band
);
7694 mutex_exit(&stp
->sd_lock
);
7696 if (STREAM_NEEDSERVICE(stp
))
7697 stream_runservice(stp
);
7698 mutex_enter(&stp
->sd_lock
);
7702 * stream head cannot change while we make the determination
7703 * whether or not to send a signal. Drop the flag to allow strrput
7704 * to send firstmsgsigs again.
7706 stp
->sd_flag
&= ~STRGETINPROG
;
7709 * If the type of message at the front of the queue changed
7710 * due to the receive the appropriate signals and pollwakeup events
7711 * are generated. The type of changes are:
7712 * Processed a hipri message, q_first is not hipri.
7713 * Processed a band X message, and q_first is band Y.
7714 * The generated signals and pollwakeups are identical to what
7715 * strrput() generates should the message that is now on q_first
7716 * arrive to an empty read queue.
7718 * Note: only strrput will send a signal for a hipri message.
7720 if ((bp
= q
->q_first
) != NULL
&& !(stp
->sd_flag
& STRPRI
)) {
7721 strsigset_t signals
= 0;
7722 strpollset_t pollwakeups
= 0;
7724 if (flg
& MSG_HIPRI
) {
7726 * Removed a hipri message. Regular data at
7727 * the front of the queue.
7729 if (bp
->b_band
== 0) {
7730 signals
= S_INPUT
| S_RDNORM
;
7731 pollwakeups
= POLLIN
| POLLRDNORM
;
7733 signals
= S_INPUT
| S_RDBAND
;
7734 pollwakeups
= POLLIN
| POLLRDBAND
;
7736 } else if (pri
!= bp
->b_band
) {
7738 * The band is different for the new q_first.
7740 if (bp
->b_band
== 0) {
7742 pollwakeups
= POLLIN
| POLLRDNORM
;
7745 pollwakeups
= POLLIN
| POLLRDBAND
;
7749 if (pollwakeups
!= 0) {
7750 if (pollwakeups
== (POLLIN
| POLLRDNORM
)) {
7751 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
7753 stp
->sd_rput_opt
&= ~SR_POLLIN
;
7755 mutex_exit(&stp
->sd_lock
);
7756 pollwakeup(&stp
->sd_pollist
, pollwakeups
);
7757 mutex_enter(&stp
->sd_lock
);
7761 if (stp
->sd_sigflags
& signals
)
7762 strsendsig(stp
->sd_siglist
, signals
, bp
->b_band
, 0);
7764 mutex_exit(&stp
->sd_lock
);
7772 * Put a message downstream.
7774 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7779 struct strbuf
*mctl
,
7780 struct strbuf
*mdata
,
7792 struct uio
*uiop
= &uios
;
7796 ASSERT(vp
->v_stream
);
7801 * If it is an XPG4 application, we need to send
7805 xpg4
= (flag
& MSG_XPG4
) ? 1 : 0;
7808 mutex_enter(&stp
->sd_lock
);
7810 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
7811 mutex_exit(&stp
->sd_lock
);
7815 if (stp
->sd_flag
& (STWRERR
|STRHUP
|STPLEX
)) {
7816 error
= strwriteable(stp
, B_FALSE
, xpg4
);
7818 mutex_exit(&stp
->sd_lock
);
7823 mutex_exit(&stp
->sd_lock
);
7826 * Check for legal flag value.
7830 if ((mctl
->len
< 0) || (pri
!= 0))
7840 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_IN
,
7841 "strputmsg in:stp %p", stp
);
7843 /* get these values from those cached in the stream head */
7844 rmin
= stp
->sd_qn_minpsz
;
7845 rmax
= stp
->sd_qn_maxpsz
;
7848 * Make sure ctl and data sizes together fall within the
7849 * limits of the max and min receive packet sizes and do
7850 * not exceed system limit.
7852 ASSERT((rmax
>= 0) || (rmax
== INFPSZ
));
7857 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
7858 * Needed to prevent partial failures in the strmakedata loop.
7860 if (stp
->sd_maxblk
!= INFPSZ
&& rmax
!= INFPSZ
&& rmax
< stp
->sd_maxblk
)
7861 rmax
= stp
->sd_maxblk
;
7863 if ((msgsize
= mdata
->len
) < 0) {
7865 rmin
= 0; /* no range check for NULL data part */
7867 if ((msgsize
< rmin
) ||
7868 ((msgsize
> rmax
) && (rmax
!= INFPSZ
)) ||
7869 (mctl
->len
> strctlsz
)) {
7874 * Setup uio and iov for data part
7876 iovs
.iov_base
= mdata
->buf
;
7877 iovs
.iov_len
= msgsize
;
7878 uios
.uio_iov
= &iovs
;
7879 uios
.uio_iovcnt
= 1;
7880 uios
.uio_loffset
= 0;
7881 uios
.uio_segflg
= UIO_USERSPACE
;
7882 uios
.uio_fmode
= fmode
;
7883 uios
.uio_extflg
= UIO_COPY_DEFAULT
;
7884 uios
.uio_resid
= msgsize
;
7885 uios
.uio_offset
= 0;
7887 /* Ignore flow control in strput for HIPRI */
7888 if (flag
& MSG_HIPRI
)
7889 flag
|= MSG_IGNFLOW
;
7895 * strput will always free the ctl mblk - even when strput
7898 if ((error
= strmakectl(mctl
, flag
, fmode
, &mp
)) != 0) {
7899 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_OUT
,
7900 "strputmsg out:stp %p out %d error %d",
7905 * Verify that the whole message can be transferred by
7908 ASSERT(stp
->sd_maxblk
== INFPSZ
||
7909 stp
->sd_maxblk
>= mdata
->len
);
7911 msgsize
= mdata
->len
;
7912 error
= strput(stp
, mp
, uiop
, &msgsize
, 0, pri
, flag
);
7913 mdata
->len
= msgsize
;
7918 if (error
!= EWOULDBLOCK
)
7921 mutex_enter(&stp
->sd_lock
);
7923 * Check for a missed wakeup.
7924 * Needed since strput did not hold sd_lock across
7927 if (bcanputnext(wqp
, pri
)) {
7929 mutex_exit(&stp
->sd_lock
);
7932 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_WAIT
,
7933 "strputmsg wait:stp %p waits pri %d", stp
, pri
);
7934 if (((error
= strwaitq(stp
, WRITEWAIT
, (ssize_t
)0, fmode
, -1,
7935 &done
)) != 0) || done
) {
7936 mutex_exit(&stp
->sd_lock
);
7937 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_OUT
,
7938 "strputmsg out:q %p out %d error %d",
7942 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_WAKE
,
7943 "strputmsg wake:stp %p wakes", stp
);
7944 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
7945 mutex_exit(&stp
->sd_lock
);
7948 mutex_exit(&stp
->sd_lock
);
7952 * For historic reasons, applications expect EAGAIN
7953 * when data mblk could not be allocated. so change
7954 * ENOMEM back to EAGAIN
7956 if (error
== ENOMEM
)
7958 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_OUT
,
7959 "strputmsg out:stp %p out %d error %d", stp
, 2, error
);
7964 * Put a message downstream.
7965 * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
7966 * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
7967 * and the fmode parameter.
7969 * This routine handles the consolidation private flags:
7970 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7971 * MSG_HOLDSIG Hold signals while waiting for data.
7972 * MSG_IGNFLOW Don't check streams flow control.
7974 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7991 ASSERT(vp
->v_stream
);
7997 mutex_enter(&stp
->sd_lock
);
7999 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
8000 mutex_exit(&stp
->sd_lock
);
8005 if ((stp
->sd_flag
& STPLEX
) || !(flag
& MSG_IGNERROR
)) {
8006 if (stp
->sd_flag
& (STWRERR
|STRHUP
|STPLEX
)) {
8007 error
= strwriteable(stp
, B_FALSE
, B_TRUE
);
8009 mutex_exit(&stp
->sd_lock
);
8016 mutex_exit(&stp
->sd_lock
);
8019 * Check for legal flag value.
8021 switch (flag
& (MSG_HIPRI
|MSG_BAND
|MSG_ANY
)) {
8035 TRACE_1(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_IN
,
8036 "kstrputmsg in:stp %p", stp
);
8038 /* get these values from those cached in the stream head */
8039 rmin
= stp
->sd_qn_minpsz
;
8040 rmax
= stp
->sd_qn_maxpsz
;
8043 * Make sure ctl and data sizes together fall within the
8044 * limits of the max and min receive packet sizes and do
8045 * not exceed system limit.
8047 ASSERT((rmax
>= 0) || (rmax
== INFPSZ
));
8053 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
8054 * Needed to prevent partial failures in the strmakedata loop.
8056 if (stp
->sd_maxblk
!= INFPSZ
&& rmax
!= INFPSZ
&& rmax
< stp
->sd_maxblk
)
8057 rmax
= stp
->sd_maxblk
;
8061 rmin
= -1; /* no range check for NULL data part */
8063 /* Use uio flags as well as the fmode parameter flags */
8064 fmode
|= uiop
->uio_fmode
;
8066 if ((msgsize
< rmin
) ||
8067 ((msgsize
> rmax
) && (rmax
!= INFPSZ
))) {
8073 /* Ignore flow control in strput for HIPRI */
8074 if (flag
& MSG_HIPRI
)
8075 flag
|= MSG_IGNFLOW
;
8083 * strput will always free the ctl mblk - even when strput
8084 * fails. If MSG_IGNFLOW is set then any error returned
8085 * will cause us to break the loop, so we don't need a copy
8086 * of the message. If MSG_IGNFLOW is not set, then we can
8087 * get hit by flow control and be forced to try again. In
8088 * this case we need to have a copy of the message. We
8089 * do this using copymsg since the message may get modified
8090 * by something below us.
8092 * We've observed that many TPI providers do not check db_ref
8093 * on the control messages but blindly reuse them for the
8094 * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
8095 * friendly to such providers than using dupmsg. Also, note
8096 * that sockfs uses MSG_IGNFLOW for all TPI control messages.
8097 * Only data messages are subject to flow control, hence
8098 * subject to this copymsg.
8100 if (flag
& MSG_IGNFLOW
) {
8106 * If a message has a free pointer, the message
8107 * must be dupmsg to maintain this pointer.
8108 * Code using this facility must be sure
8109 * that modules below will not change the
8110 * contents of the dblk without checking db_ref
8111 * first. If db_ref is > 1, then the module
8112 * needs to do a copymsg first. Otherwise,
8113 * the contents of the dblk may become
8114 * inconsistent because the freesmg/freeb below
8115 * may end up calling atomic_add_32_nv.
8116 * The atomic_add_32_nv in freeb (accessing
8117 * all of db_ref, db_type, db_flags, and
8118 * db_struioflag) does not prevent other threads
8119 * from concurrently trying to modify e.g.
8122 if (mctl
->b_datap
->db_frtnp
!= NULL
)
8130 error
= strwaitbuf(msgdsize(mctl
), BPRI_MED
);
8135 } while (mp
== NULL
);
8138 * Verify that all of msgsize can be transferred by
8141 ASSERT(stp
->sd_maxblk
== INFPSZ
|| stp
->sd_maxblk
>= msgsize
);
8142 error
= strput(stp
, mp
, uiop
, &msgsize
, 0, pri
, flag
);
8146 if (error
!= EWOULDBLOCK
)
8150 * IF MSG_IGNFLOW is set we should have broken out of loop
8153 ASSERT(!(flag
& MSG_IGNFLOW
));
8154 mutex_enter(&stp
->sd_lock
);
8156 * Check for a missed wakeup.
8157 * Needed since strput did not hold sd_lock across
8160 if (bcanputnext(wqp
, pri
)) {
8162 mutex_exit(&stp
->sd_lock
);
8165 TRACE_2(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_WAIT
,
8166 "kstrputmsg wait:stp %p waits pri %d", stp
, pri
);
8168 waitflag
= WRITEWAIT
;
8169 if (flag
& (MSG_HOLDSIG
|MSG_IGNERROR
)) {
8170 if (flag
& MSG_HOLDSIG
)
8171 waitflag
|= STR_NOSIG
;
8172 if (flag
& MSG_IGNERROR
)
8173 waitflag
|= STR_NOERROR
;
8175 if (((error
= strwaitq(stp
, waitflag
,
8176 (ssize_t
)0, fmode
, -1, &done
)) != 0) || done
) {
8177 mutex_exit(&stp
->sd_lock
);
8178 TRACE_3(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_OUT
,
8179 "kstrputmsg out:stp %p out %d error %d",
8184 TRACE_1(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_WAKE
,
8185 "kstrputmsg wake:stp %p wakes", stp
);
8186 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
8187 mutex_exit(&stp
->sd_lock
);
8191 mutex_exit(&stp
->sd_lock
);
8196 * For historic reasons, applications expect EAGAIN
8197 * when data mblk could not be allocated. so change
8198 * ENOMEM back to EAGAIN
8200 if (error
== ENOMEM
)
8202 TRACE_3(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_OUT
,
8203 "kstrputmsg out:stp %p out %d error %d", stp
, 2, error
);
8208 * Determines whether the necessary conditions are set on a stream
8209 * for it to be readable, writeable, or have exceptions.
8211 * strpoll handles the consolidation private events:
8212 * POLLNOERR Do not return POLLERR even if there are stream
8215 * POLLRDDATA Do not return POLLIN unless at least one message on
8216 * the queue contains one or more M_DATA mblks. Thus
8217 * when this flag is set a queue with only
8218 * M_PROTO/M_PCPROTO mblks does not return POLLIN.
8219 * Used by sockfs to ignore T_EXDATA_IND messages.
8221 * Note: POLLRDDATA assumes that synch streams only return messages with
8222 * an M_DATA attached (i.e. not messages consisting of only
8223 * an M_PROTO/M_PCPROTO part).
8226 strpoll(struct stdata
*stp
, short events_arg
, int anyyet
, short *reventsp
,
8227 struct pollhead
**phpp
)
8229 int events
= (ushort_t
)events_arg
;
8233 long sd_flags
= stp
->sd_flag
;
8237 * For performance, a single 'if' tests for most possible edge
8238 * conditions in one shot
8240 if (sd_flags
& (STPLEX
| STRDERR
| STWRERR
)) {
8241 if (sd_flags
& STPLEX
) {
8242 *reventsp
= POLLNVAL
;
8245 if (((events
& (POLLIN
| POLLRDNORM
| POLLRDBAND
| POLLPRI
)) &&
8246 (sd_flags
& STRDERR
)) ||
8247 ((events
& (POLLOUT
| POLLWRNORM
| POLLWRBAND
)) &&
8248 (sd_flags
& STWRERR
))) {
8249 if (!(events
& POLLNOERR
)) {
8250 *reventsp
= POLLERR
;
8255 if (sd_flags
& STRHUP
) {
8256 retevents
|= POLLHUP
;
8257 } else if (events
& (POLLWRNORM
| POLLWRBAND
)) {
8259 queue_t
*qp
= stp
->sd_wrq
;
8262 /* Find next module forward that has a service procedure */
8263 tq
= qp
->q_next
->q_nfsrv
;
8266 if (polllock(&stp
->sd_pollist
, QLOCK(tq
)) != 0) {
8268 *reventsp
= POLLNVAL
;
8271 if (events
& POLLWRNORM
) {
8274 if (tq
->q_flag
& QFULL
)
8275 /* ensure backq svc procedure runs */
8276 tq
->q_flag
|= QWANTW
;
8277 else if ((sqp
= stp
->sd_struiowrq
) != NULL
) {
8278 /* Check sync stream barrier write q */
8279 mutex_exit(QLOCK(tq
));
8280 if (polllock(&stp
->sd_pollist
,
8283 *reventsp
= POLLNVAL
;
8286 if (sqp
->q_flag
& QFULL
)
8287 /* ensure pollwakeup() is done */
8288 sqp
->q_flag
|= QWANTWSYNC
;
8290 retevents
|= POLLOUT
;
8291 /* More write events to process ??? */
8292 if (! (events
& POLLWRBAND
)) {
8293 mutex_exit(QLOCK(sqp
));
8297 mutex_exit(QLOCK(sqp
));
8298 if (polllock(&stp
->sd_pollist
,
8301 *reventsp
= POLLNVAL
;
8305 retevents
|= POLLOUT
;
8307 if (events
& POLLWRBAND
) {
8311 if (qbp
->qb_flag
& QB_FULL
)
8312 qbp
->qb_flag
|= QB_WANTW
;
8314 retevents
|= POLLWRBAND
;
8318 retevents
|= POLLWRBAND
;
8321 mutex_exit(QLOCK(tq
));
8325 if (sd_flags
& STRPRI
) {
8326 retevents
|= (events
& POLLPRI
);
8327 } else if (events
& (POLLRDNORM
| POLLRDBAND
| POLLIN
)) {
8328 queue_t
*qp
= _RD(stp
->sd_wrq
);
8329 int normevents
= (events
& (POLLIN
| POLLRDNORM
));
8332 * Note: Need to do polllock() here since ps_lock may be
8333 * held. See bug 4191544.
8335 if (polllock(&stp
->sd_pollist
, &stp
->sd_lock
) != 0) {
8336 *reventsp
= POLLNVAL
;
8343 * For POLLRDDATA we scan b_cont and b_next until we
8346 if ((events
& POLLRDDATA
) &&
8347 mp
->b_datap
->db_type
!= M_DATA
) {
8348 mblk_t
*nmp
= mp
->b_cont
;
8350 while (nmp
!= NULL
&&
8351 nmp
->b_datap
->db_type
!= M_DATA
)
8358 if (mp
->b_band
== 0)
8359 retevents
|= normevents
;
8361 retevents
|= (events
& (POLLIN
| POLLRDBAND
));
8364 if (!(retevents
& normevents
) && (stp
->sd_wakeq
& RSLEEP
)) {
8366 * Sync stream barrier read queue has data.
8368 retevents
|= normevents
;
8370 /* Treat eof as normal data */
8371 if (sd_flags
& STREOF
)
8372 retevents
|= normevents
;
8376 * Pass back a pollhead if no events are pending or if edge-triggering
8377 * has been configured on this resource.
8379 if ((retevents
== 0 && !anyyet
) || (events
& POLLET
)) {
8380 *phpp
= &stp
->sd_pollist
;
8381 if (headlocked
== 0) {
8382 if (polllock(&stp
->sd_pollist
, &stp
->sd_lock
) != 0) {
8383 *reventsp
= POLLNVAL
;
8388 stp
->sd_rput_opt
|= SR_POLLIN
;
8391 *reventsp
= (short)retevents
;
8393 mutex_exit(&stp
->sd_lock
);
8398 * The purpose of putback() is to assure sleeping polls/reads
8399 * are awakened when there are no new messages arriving at the,
8400 * stream head, and a message is placed back on the read queue.
8402 * sd_lock must be held when messages are placed back on stream
8403 * head. (getq() holds sd_lock when it removes messages from
8408 putback(struct stdata
*stp
, queue_t
*q
, mblk_t
*bp
, int band
)
8411 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
8414 * As a result of lock-step ordering around q_lock and sd_lock,
8415 * it's possible for function calls like putnext() and
8416 * canputnext() to get an inaccurate picture of how much
8417 * data is really being processed at the stream head.
8418 * We only consolidate with existing messages on the queue
8419 * if the length of the message we want to put back is smaller
8420 * than the queue hiwater mark.
8422 if ((stp
->sd_rput_opt
& SR_CONSOL_DATA
) &&
8423 (DB_TYPE(bp
) == M_DATA
) && ((qfirst
= q
->q_first
) != NULL
) &&
8424 (DB_TYPE(qfirst
) == M_DATA
) &&
8425 ((qfirst
->b_flag
& (MSGMARK
|MSGDELIM
)) == 0) &&
8426 ((bp
->b_flag
& (MSGMARK
|MSGDELIM
|MSGMARKNEXT
)) == 0) &&
8427 (mp_cont_len(bp
, NULL
) < q
->q_hiwat
)) {
8429 * We use the same logic as defined in strrput()
8430 * but in reverse as we are putting back onto the
8431 * queue and want to retain byte ordering.
8432 * Consolidate M_DATA messages with M_DATA ONLY.
8433 * strrput() allows the consolidation of M_DATA onto
8434 * M_PROTO | M_PCPROTO but not the other way round.
8436 * The consolidation does not take place if the message
8437 * we are returning to the queue is marked with either
8438 * of the marks or the delim flag or if q_first
8439 * is marked with MSGMARK. The MSGMARK check is needed to
8440 * handle the odd semantics of MSGMARK where essentially
8441 * the whole message is to be treated as marked.
8442 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from q_first
8443 * to the front of the b_cont chain.
8445 rmvq_noenab(q
, qfirst
);
8448 * The first message in the b_cont list
8449 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
8450 * We need to handle the case where we
8453 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
8454 * 2) a MSGMARKNEXT to a plain message.
8455 * 3) a MSGNOTMARKNEXT to a plain message
8456 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
8459 * Thus we never append a MSGMARKNEXT or
8460 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
8462 if (qfirst
->b_flag
& MSGMARKNEXT
) {
8463 bp
->b_flag
|= MSGMARKNEXT
;
8464 bp
->b_flag
&= ~MSGNOTMARKNEXT
;
8465 qfirst
->b_flag
&= ~MSGMARKNEXT
;
8466 } else if (qfirst
->b_flag
& MSGNOTMARKNEXT
) {
8467 bp
->b_flag
|= MSGNOTMARKNEXT
;
8468 qfirst
->b_flag
&= ~MSGNOTMARKNEXT
;
8473 (void) putbq(q
, bp
);
8476 * A message may have come in when the sd_lock was dropped in the
8477 * calling routine. If this is the case and STR*ATMARK info was
8478 * received, need to move that from the stream head to the q_last
8479 * so that SIOCATMARK can return the proper value.
8481 if (stp
->sd_flag
& (STRATMARK
| STRNOTATMARK
)) {
8482 unsigned short *flagp
= &q
->q_last
->b_flag
;
8483 uint_t b_flag
= (uint_t
)*flagp
;
8485 if (stp
->sd_flag
& STRATMARK
) {
8486 b_flag
&= ~MSGNOTMARKNEXT
;
8487 b_flag
|= MSGMARKNEXT
;
8488 stp
->sd_flag
&= ~STRATMARK
;
8490 b_flag
&= ~MSGMARKNEXT
;
8491 b_flag
|= MSGNOTMARKNEXT
;
8492 stp
->sd_flag
&= ~STRNOTATMARK
;
8494 *flagp
= (unsigned short) b_flag
;
8499 * Make sure that the flags are not messed up.
8504 while (mp
!= NULL
) {
8505 ASSERT((mp
->b_flag
& (MSGMARKNEXT
|MSGNOTMARKNEXT
)) !=
8506 (MSGMARKNEXT
|MSGNOTMARKNEXT
));
8511 if (q
->q_first
== bp
) {
8514 if (stp
->sd_flag
& RSLEEP
) {
8515 stp
->sd_flag
&= ~RSLEEP
;
8516 cv_broadcast(&q
->q_wait
);
8518 if (stp
->sd_flag
& STRPRI
) {
8519 pollevents
= POLLPRI
;
8522 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
8524 stp
->sd_rput_opt
&= ~SR_POLLIN
;
8525 pollevents
= POLLIN
| POLLRDNORM
;
8527 pollevents
= POLLIN
| POLLRDBAND
;
8530 mutex_exit(&stp
->sd_lock
);
8531 pollwakeup(&stp
->sd_pollist
, pollevents
);
8532 mutex_enter(&stp
->sd_lock
);
8537 * Return the held vnode attached to the stream head of a
8539 * It is the responsibility of the calling routine to ensure
8540 * that the queue does not go away (e.g. pop).
8543 strq2vp(queue_t
*qp
)
8546 vp
= STREAM(qp
)->sd_vnode
;
8553 * return the stream head write queue for the given vp
8554 * It is the responsibility of the calling routine to ensure
8555 * that the stream or vnode do not close.
8558 strvp2wq(vnode_t
*vp
)
8560 ASSERT(vp
->v_stream
!= NULL
);
8561 return (vp
->v_stream
->sd_wrq
);
8565 * pollwakeup stream head
8566 * It is the responsibility of the calling routine to ensure
8567 * that the stream or vnode do not close.
8570 strpollwakeup(vnode_t
*vp
, short event
)
8572 ASSERT(vp
->v_stream
);
8573 pollwakeup(&vp
->v_stream
->sd_pollist
, event
);
8577 * Mate the stream heads of two vnodes together. If the two vnodes are the
8578 * same, we just make the write-side point at the read-side -- otherwise,
8579 * we do a full mate. Only works on vnodes associated with streams that are
8580 * still being built and thus have only a stream head.
8583 strmate(vnode_t
*vp1
, vnode_t
*vp2
)
8585 queue_t
*wrq1
= strvp2wq(vp1
);
8586 queue_t
*wrq2
= strvp2wq(vp2
);
8589 * Verify that there are no modules on the stream yet. We also
8590 * rely on the stream head always having a service procedure to
8591 * avoid tweaking q_nfsrv.
8593 ASSERT(wrq1
->q_next
== NULL
&& wrq2
->q_next
== NULL
);
8594 ASSERT(wrq1
->q_qinfo
->qi_srvp
!= NULL
);
8595 ASSERT(wrq2
->q_qinfo
->qi_srvp
!= NULL
);
8598 * If the queues are the same, just twist; otherwise do a full mate.
8601 wrq1
->q_next
= _RD(wrq1
);
8603 wrq1
->q_next
= _RD(wrq2
);
8604 wrq2
->q_next
= _RD(wrq1
);
8605 STREAM(wrq1
)->sd_mate
= STREAM(wrq2
);
8606 STREAM(wrq1
)->sd_flag
|= STRMATE
;
8607 STREAM(wrq2
)->sd_mate
= STREAM(wrq1
);
8608 STREAM(wrq2
)->sd_flag
|= STRMATE
;
8613 * XXX will go away when console is correctly fixed.
8614 * Clean up the console PIDS, from previous I_SETSIG,
8615 * called only for cnopen which never calls strclean().
8618 str_cn_clean(struct vnode
*vp
)
8620 strsig_t
*ssp
, *pssp
, *tssp
;
8625 ASSERT(vp
->v_stream
);
8628 mutex_enter(&stp
->sd_lock
);
8629 ssp
= stp
->sd_siglist
;
8631 mutex_enter(&pidlock
);
8632 pidp
= ssp
->ss_pidp
;
8634 * Get rid of PID if the proc is gone.
8636 if (pidp
->pid_prinactive
) {
8637 tssp
= ssp
->ss_next
;
8639 pssp
->ss_next
= tssp
;
8641 stp
->sd_siglist
= tssp
;
8642 ASSERT(pidp
->pid_ref
<= 1);
8643 PID_RELE(ssp
->ss_pidp
);
8644 mutex_exit(&pidlock
);
8645 kmem_free(ssp
, sizeof (strsig_t
));
8650 mutex_exit(&pidlock
);
8655 stp
->sd_sigflags
= 0;
8656 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
8657 stp
->sd_sigflags
|= ssp
->ss_events
;
8659 mutex_exit(&stp
->sd_lock
);
8663 * Return B_TRUE if there is data in the message, B_FALSE otherwise.
8666 msghasdata(mblk_t
*bp
)
8668 for (; bp
; bp
= bp
->b_cont
)
8669 if (bp
->b_datap
->db_type
== M_DATA
) {
8670 ASSERT(bp
->b_wptr
>= bp
->b_rptr
);
8671 if (bp
->b_wptr
> bp
->b_rptr
)