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>
85 * This define helps improve the readability of streams code while
86 * still maintaining a very old streams performance enhancement. The
87 * performance enhancement basically involved having all callers
88 * of straccess() perform the first check that straccess() will do
89 * locally before actually calling straccess(). (There by reducing
90 * the number of unnecessary calls to straccess().)
92 #define i_straccess(x, y) ((stp->sd_sidp == NULL) ? 0 : \
93 (stp->sd_vnode->v_type == VFIFO) ? 0 : \
97 * what is mblk_pull_len?
99 * If a streams message consists of many short messages,
100 * a performance degradation occurs from copyout overhead.
101 * To decrease the per mblk overhead, messages that are
102 * likely to consist of many small mblks are pulled up into
103 * one continuous chunk of memory.
105 * To avoid the processing overhead of examining every
106 * mblk, a quick heuristic is used. If the first mblk in
107 * the message is shorter than mblk_pull_len, it is likely
108 * that the rest of the mblk will be short.
110 * This heuristic was decided upon after performance tests
111 * indicated that anything more complex slowed down the main
114 #define MBLK_PULL_LEN 64
115 uint32_t mblk_pull_len
= MBLK_PULL_LEN
;
118 * The sgttyb_handling flag controls the handling of the old BSD
119 * TIOCGETP, TIOCSETP, and TIOCSETN ioctls as follows:
121 * 0 - Emit no warnings at all and retain old, broken behavior.
122 * 1 - Emit no warnings and silently handle new semantics.
123 * 2 - Send cmn_err(CE_NOTE) when either TIOCSETP or TIOCSETN is used
124 * (once per system invocation). Handle with new semantics.
125 * 3 - Send SIGSYS when any TIOCGETP, TIOCSETP, or TIOCSETN call is
126 * made (so that offenders drop core and are easy to debug).
128 * The "new semantics" are that TIOCGETP returns B38400 for
129 * sg_[io]speed if the corresponding value is over B38400, and that
130 * TIOCSET[PN] accept B38400 in these cases to mean "retain current
133 int sgttyb_handling
= 1;
134 static boolean_t sgttyb_complaint
;
136 /* don't push drcompat module by default on Style-2 streams */
137 static int push_drcompat
= 0;
140 * id value used to distinguish between different ioctl messages
142 static uint32_t ioc_id
;
144 static void putback(struct stdata
*, queue_t
*, mblk_t
*, int);
145 static void strcleanall(struct vnode
*);
146 static int strwsrv(queue_t
*);
147 static int strdocmd(struct stdata
*, struct strcmd
*, cred_t
*);
150 * qinit and module_info structures for stream head read and write queues
152 struct module_info strm_info
= { 0, "strrhead", 0, INFPSZ
, STRHIGH
, STRLOW
};
153 struct module_info stwm_info
= { 0, "strwhead", 0, 0, 0, 0 };
154 struct qinit strdata
= { strrput
, NULL
, NULL
, NULL
, NULL
, &strm_info
};
155 struct qinit stwdata
= { NULL
, strwsrv
, NULL
, NULL
, NULL
, &stwm_info
};
156 struct module_info fiform_info
= { 0, "fifostrrhead", 0, PIPE_BUF
, FIFOHIWAT
,
158 struct module_info fifowm_info
= { 0, "fifostrwhead", 0, 0, 0, 0 };
159 struct qinit fifo_strdata
= { strrput
, NULL
, NULL
, NULL
, NULL
, &fiform_info
};
160 struct qinit fifo_stwdata
= { NULL
, strwsrv
, NULL
, NULL
, NULL
, &fifowm_info
};
162 extern kmutex_t strresources
; /* protects global resources */
163 extern kmutex_t muxifier
; /* single-threads multiplexor creation */
165 static boolean_t
msghasdata(mblk_t
*bp
);
166 #define msgnodata(bp) (!msghasdata(bp))
169 * Stream head locking notes:
170 * There are four monitors associated with the stream head:
171 * 1. v_stream monitor: in stropen() and strclose() v_lock
172 * is held while the association of vnode and stream
173 * head is established or tested for.
174 * 2. open/close/push/pop monitor: sd_lock is held while each
175 * thread bids for exclusive access to this monitor
176 * for opening or closing a stream. In addition, this
177 * monitor is entered during pushes and pops. This
178 * guarantees that during plumbing operations there
179 * is only one thread trying to change the plumbing.
180 * Any other threads present in the stream are only
181 * using the plumbing.
182 * 3. read/write monitor: in the case of read, a thread holds
183 * sd_lock while trying to get data from the stream
184 * head queue. if there is none to fulfill a read
185 * request, it sets RSLEEP and calls cv_wait_sig() down
186 * in strwaitq() to await the arrival of new data.
187 * when new data arrives in strrput(), sd_lock is acquired
188 * before testing for RSLEEP and calling cv_broadcast().
189 * the behavior of strwrite(), strwsrv(), and WSLEEP
191 * 4. ioctl monitor: sd_lock is gotten to ensure that only one
192 * thread is doing an ioctl at a time.
196 push_mod(queue_t
*qp
, dev_t
*devp
, struct stdata
*stp
, const char *name
,
197 int anchor
, cred_t
*crp
, uint_t anchor_zoneid
)
202 if (stp
->sd_flag
& (STRHUP
|STRDERR
|STWRERR
)) {
203 error
= (stp
->sd_flag
& STRHUP
) ? ENXIO
: EIO
;
206 if (stp
->sd_pushcnt
>= nstrpush
) {
210 if ((fp
= fmodsw_find(name
, FMODSW_HOLD
| FMODSW_LOAD
)) == NULL
) {
211 stp
->sd_flag
|= STREOPENFAIL
;
216 * push new module and call its open routine via qattach
218 if ((error
= qattach(qp
, devp
, 0, crp
, fp
, B_FALSE
)) != 0)
222 * Check to see if caller wants a STREAMS anchor
223 * put at this place in the stream, and add if so.
225 mutex_enter(&stp
->sd_lock
);
226 if (anchor
== stp
->sd_pushcnt
) {
227 stp
->sd_anchor
= stp
->sd_pushcnt
;
228 stp
->sd_anchorzone
= anchor_zoneid
;
230 mutex_exit(&stp
->sd_lock
);
236 * Open a stream device.
239 stropen(vnode_t
*vp
, dev_t
*devp
, int flag
, cred_t
*crp
)
244 dev_t dummydev
, savedev
;
246 struct dlautopush dlap
;
257 * If the stream already exists, wait for any open in progress
258 * to complete, then call the open function of each module and
259 * driver in the stream. Otherwise create the stream.
261 TRACE_1(TR_FAC_STREAMS_FR
, TR_STROPEN
, "stropen:%p", vp
);
263 mutex_enter(&vp
->v_lock
);
264 if ((stp
= vp
->v_stream
) != NULL
) {
267 * Waiting for stream to be created to device
268 * due to another open.
270 mutex_exit(&vp
->v_lock
);
273 struct stdata
*strmatep
= stp
->sd_mate
;
276 if (strmatep
->sd_flag
& (STWOPEN
|STRCLOSE
|STRPLUMB
)) {
277 if (flag
& (FNDELAY
|FNONBLOCK
)) {
279 mutex_exit(&strmatep
->sd_lock
);
282 mutex_exit(&stp
->sd_lock
);
283 if (!cv_wait_sig(&strmatep
->sd_monitor
,
284 &strmatep
->sd_lock
)) {
286 mutex_exit(&strmatep
->sd_lock
);
287 mutex_enter(&stp
->sd_lock
);
290 mutex_exit(&strmatep
->sd_lock
);
293 if (stp
->sd_flag
& (STWOPEN
|STRCLOSE
|STRPLUMB
)) {
294 if (flag
& (FNDELAY
|FNONBLOCK
)) {
296 mutex_exit(&strmatep
->sd_lock
);
299 mutex_exit(&strmatep
->sd_lock
);
300 if (!cv_wait_sig(&stp
->sd_monitor
,
305 mutex_exit(&stp
->sd_lock
);
309 if (stp
->sd_flag
& (STRDERR
|STWRERR
)) {
311 mutex_exit(&strmatep
->sd_lock
);
315 stp
->sd_flag
|= STWOPEN
;
318 mutex_enter(&stp
->sd_lock
);
319 if (stp
->sd_flag
& (STWOPEN
|STRCLOSE
|STRPLUMB
)) {
320 if (flag
& (FNDELAY
|FNONBLOCK
)) {
324 if (!cv_wait_sig(&stp
->sd_monitor
,
329 mutex_exit(&stp
->sd_lock
);
330 goto retry
; /* could be clone! */
333 if (stp
->sd_flag
& (STRDERR
|STWRERR
)) {
338 stp
->sd_flag
|= STWOPEN
;
339 mutex_exit(&stp
->sd_lock
);
343 * Open all modules and devices down stream to notify
344 * that another user is streaming. For modules, set the
345 * last argument to MODOPEN and do not pass any open flags.
346 * Ignore dummydev since this is not the first open.
348 claimstr(stp
->sd_wrq
);
350 while (_SAMESTR(qp
)) {
352 if ((error
= qreopen(_RD(qp
), devp
, flag
, crp
)) != 0)
355 releasestr(stp
->sd_wrq
);
356 mutex_enter(&stp
->sd_lock
);
357 stp
->sd_flag
&= ~(STRHUP
|STWOPEN
|STRDERR
|STWRERR
);
361 cv_broadcast(&stp
->sd_monitor
);
362 mutex_exit(&stp
->sd_lock
);
367 * This vnode isn't streaming. SPECFS already
368 * checked for multiple vnodes pointing to the
369 * same stream, so create a stream to the driver.
375 * Initialize stream head. shalloc() has given us
376 * exclusive access, and we have the vnode locked;
377 * we can do whatever we want with stp.
379 stp
->sd_flag
= STWOPEN
;
380 stp
->sd_siglist
= NULL
;
381 stp
->sd_pollist
.ph_list
= NULL
;
382 stp
->sd_sigflags
= 0;
384 stp
->sd_closetime
= STRTIMOUT
;
386 stp
->sd_pgidp
= NULL
;
392 stp
->sd_iocblk
= NULL
;
393 stp
->sd_cmdblk
= NULL
;
395 stp
->sd_qn_minpsz
= 0;
396 stp
->sd_qn_maxpsz
= INFPSZ
- 1; /* used to check for initialization */
397 stp
->sd_maxblk
= INFPSZ
;
398 qp
->q_ptr
= _WR(qp
)->q_ptr
= stp
;
399 STREAM(qp
) = STREAM(_WR(qp
)) = stp
;
401 mutex_exit(&vp
->v_lock
);
402 if (vp
->v_type
== VFIFO
) {
403 stp
->sd_flag
|= OLDNDELAY
;
405 * This means, both for pipes and fifos
406 * strwrite will send SIGPIPE if the other
407 * end is closed. For putmsg it depends
408 * on whether it is a XPG4_2 application
411 stp
->sd_wput_opt
= SW_SIGPIPE
;
413 /* setq might sleep in kmem_alloc - avoid holding locks. */
414 setq(qp
, &fifo_strdata
, &fifo_stwdata
, NULL
, QMTSAFE
,
415 SQ_CI
|SQ_CO
, B_FALSE
);
418 stp
->sd_strtab
= fifo_getinfo();
419 _WR(qp
)->q_nfsrv
= _WR(qp
);
422 * Wake up others that are waiting for stream to be created.
424 mutex_enter(&stp
->sd_lock
);
426 * nothing is be pushed on stream yet, so
427 * optimized stream head packetsizes are just that
430 stp
->sd_qn_minpsz
= qp
->q_minpsz
;
431 stp
->sd_qn_maxpsz
= qp
->q_maxpsz
;
432 stp
->sd_flag
&= ~STWOPEN
;
435 /* setq might sleep in kmem_alloc - avoid holding locks. */
436 setq(qp
, &strdata
, &stwdata
, NULL
, QMTSAFE
, SQ_CI
|SQ_CO
, B_FALSE
);
441 * Open driver and create stream to it (via qattach).
444 cloneopen
= (getmajor(*devp
) == clone_major
);
445 if ((error
= qattach(qp
, devp
, flag
, crp
, NULL
, B_FALSE
)) != 0) {
446 mutex_enter(&vp
->v_lock
);
448 mutex_exit(&vp
->v_lock
);
449 mutex_enter(&stp
->sd_lock
);
450 cv_broadcast(&stp
->sd_monitor
);
451 mutex_exit(&stp
->sd_lock
);
457 * Set sd_strtab after open in order to handle clonable drivers
459 stp
->sd_strtab
= STREAMSTAB(getmajor(*devp
));
462 * Historical note: dummydev used to be be prior to the initial
463 * open (via qattach above), which made the value seen
464 * inconsistent between an I_PUSH and an autopush of a module.
469 * For clone open of old style (Q not associated) network driver,
470 * push DRMODNAME module to handle DL_ATTACH/DL_DETACH
472 brq
= _RD(_WR(qp
)->q_next
);
473 major
= getmajor(*devp
);
474 if (push_drcompat
&& cloneopen
&& NETWORK_DRV(major
) &&
475 ((brq
->q_flag
& _QASSOCIATED
) == 0)) {
476 if (push_mod(qp
, &dummydev
, stp
, DRMODNAME
, 0, crp
, 0) != 0)
477 cmn_err(CE_WARN
, "cannot push " DRMODNAME
481 if (!NETWORK_DRV(major
)) {
485 * For network devices, process differently based on the
486 * return value from dld_autopush():
488 * 0: the passed-in device points to a GLDv3 datalink with
489 * per-link autopush configuration; use that configuration
490 * and ignore any per-driver autopush configuration.
492 * 1: the passed-in device points to a physical GLDv3
493 * datalink without per-link autopush configuration. The
494 * passed in device was changed to refer to the actual
495 * physical device (if it's not already); we use that new
496 * device to look up any per-driver autopush configuration.
498 * -1: neither of the above cases applied; use the initial
499 * device to look up any per-driver autopush configuration.
501 switch (dld_autopush(&savedev
, &dlap
)) {
503 zoneid
= crgetzoneid(crp
);
504 for (s
= 0; s
< dlap
.dap_npush
; s
++) {
505 error
= push_mod(qp
, &dummydev
, stp
,
506 dlap
.dap_aplist
[s
], dlap
.dap_anchor
, crp
,
520 * Find the autopush configuration based on "savedev". Start with the
521 * global zone. If not found check in the local zone.
523 zoneid
= GLOBAL_ZONEID
;
525 ss
= netstack_find_by_stackid(zoneid_to_netstackid(zoneid
))->
527 if ((ap
= sad_ap_find_by_dev(savedev
, ss
)) == NULL
) {
528 netstack_rele(ss
->ss_netstack
);
529 if (zoneid
== GLOBAL_ZONEID
) {
531 * None found. Also look in the zone's autopush table.
533 zoneid
= crgetzoneid(crp
);
534 if (zoneid
!= GLOBAL_ZONEID
)
539 anchor
= ap
->ap_anchor
;
540 zoneid
= crgetzoneid(crp
);
541 for (s
= 0; s
< ap
->ap_npush
; s
++) {
542 error
= push_mod(qp
, &dummydev
, stp
, ap
->ap_list
[s
],
543 anchor
, crp
, zoneid
);
548 netstack_rele(ss
->ss_netstack
);
553 * let specfs know that open failed part way through
556 mutex_enter(&stp
->sd_lock
);
557 stp
->sd_flag
|= STREOPENFAIL
;
558 mutex_exit(&stp
->sd_lock
);
562 * Wake up others that are waiting for stream to be created.
564 mutex_enter(&stp
->sd_lock
);
565 stp
->sd_flag
&= ~STWOPEN
;
568 * As a performance concern we are caching the values of
569 * q_minpsz and q_maxpsz of the module below the stream
570 * head in the stream head.
572 mutex_enter(QLOCK(stp
->sd_wrq
->q_next
));
573 rmin
= stp
->sd_wrq
->q_next
->q_minpsz
;
574 rmax
= stp
->sd_wrq
->q_next
->q_maxpsz
;
575 mutex_exit(QLOCK(stp
->sd_wrq
->q_next
));
577 /* do this processing here as a performance concern */
582 rmax
= MIN(strmsgsz
, rmax
);
585 mutex_enter(QLOCK(stp
->sd_wrq
));
586 stp
->sd_qn_minpsz
= rmin
;
587 stp
->sd_qn_maxpsz
= rmax
;
588 mutex_exit(QLOCK(stp
->sd_wrq
));
591 cv_broadcast(&stp
->sd_monitor
);
592 mutex_exit(&stp
->sd_lock
);
596 static int strsink(queue_t
*, mblk_t
*);
597 static struct qinit deadrend
= {
598 strsink
, NULL
, NULL
, NULL
, NULL
, &strm_info
, NULL
600 static struct qinit deadwend
= {
601 NULL
, NULL
, NULL
, NULL
, NULL
, &stwm_info
, NULL
606 * This is called from closef() on the last close of an open stream.
607 * Strclean() will already have removed the siglist and pollist
608 * information, so all that remains is to remove all multiplexor links
609 * for the stream, pop all the modules (and the driver), and free the
614 strclose(struct vnode
*vp
, int flag
, cred_t
*crp
)
622 TRACE_1(TR_FAC_STREAMS_FR
,
623 TR_STRCLOSE
, "strclose:%p", vp
);
624 ASSERT(vp
->v_stream
);
627 ASSERT(!(stp
->sd_flag
& STPLEX
));
631 * Needed so that strpoll will return non-zero for this fd.
632 * Note that with POLLNOERR STRHUP does still cause POLLHUP.
634 mutex_enter(&stp
->sd_lock
);
635 stp
->sd_flag
|= STRHUP
;
636 mutex_exit(&stp
->sd_lock
);
639 * If the registered process or process group did not have an
640 * open instance of this stream then strclean would not be
641 * called. Thus at the time of closing all remaining siglist entries
644 if (stp
->sd_siglist
!= NULL
)
647 ASSERT(stp
->sd_siglist
== NULL
);
648 ASSERT(stp
->sd_sigflags
== 0);
651 struct stdata
*strmatep
= stp
->sd_mate
;
657 while (stp
->sd_flag
& (STWOPEN
|STRCLOSE
|STRPLUMB
)) {
658 mutex_exit(&strmatep
->sd_lock
);
659 cv_wait(&stp
->sd_monitor
, &stp
->sd_lock
);
660 mutex_exit(&stp
->sd_lock
);
664 while (strmatep
->sd_flag
&
665 (STWOPEN
|STRCLOSE
|STRPLUMB
)) {
666 mutex_exit(&stp
->sd_lock
);
667 cv_wait(&strmatep
->sd_monitor
,
669 mutex_exit(&strmatep
->sd_lock
);
674 stp
->sd_flag
|= STRCLOSE
;
677 mutex_enter(&stp
->sd_lock
);
678 stp
->sd_flag
|= STRCLOSE
;
679 mutex_exit(&stp
->sd_lock
);
682 ASSERT(qp
->q_first
== NULL
); /* No more delayed write */
684 /* Check if an I_LINK was ever done on this stream */
685 if (stp
->sd_flag
& STRHASLINKS
) {
689 ns
= netstack_find_by_cred(crp
);
691 ss
= ns
->netstack_str
;
694 (void) munlinkall(stp
, LINKCLOSE
|LINKNORMAL
, crp
, &rval
, ss
);
695 netstack_rele(ss
->ss_netstack
);
698 while (_SAMESTR(qp
)) {
700 * Holding sd_lock prevents q_next from changing in
703 mutex_enter(&stp
->sd_lock
);
704 if (!(flag
& (FNDELAY
|FNONBLOCK
)) && (stp
->sd_closetime
> 0)) {
707 * sleep until awakened by strwsrv() or timeout
710 mutex_enter(QLOCK(qp
->q_next
));
711 if (!(qp
->q_next
->q_mblkcnt
)) {
712 mutex_exit(QLOCK(qp
->q_next
));
715 stp
->sd_flag
|= WSLEEP
;
717 /* ensure strwsrv gets enabled */
718 qp
->q_next
->q_flag
|= QWANTW
;
719 mutex_exit(QLOCK(qp
->q_next
));
720 /* get out if we timed out or recv'd a signal */
721 if (str_cv_wait(&qp
->q_wait
, &stp
->sd_lock
,
722 stp
->sd_closetime
, 0) <= 0) {
726 stp
->sd_flag
&= ~WSLEEP
;
728 mutex_exit(&stp
->sd_lock
);
731 if (rmq
->q_flag
& QISDRV
) {
732 ASSERT(!_SAMESTR(rmq
));
733 wait_sq_svc(_RD(qp
)->q_syncq
);
736 qdetach(_RD(rmq
), 1, flag
, crp
, B_FALSE
);
740 * Since we call pollwakeup in close() now, the poll list should
741 * be empty in most cases. The only exception is the layered devices
742 * (e.g. the console drivers with redirection modules pushed on top
743 * of it). We have to do this after calling qdetach() because
744 * the redirection module won't have torn down the console
745 * redirection until after qdetach() has been invoked.
747 if (stp
->sd_pollist
.ph_list
!= NULL
) {
748 pollwakeup(&stp
->sd_pollist
, POLLERR
);
749 pollhead_clean(&stp
->sd_pollist
);
751 ASSERT(stp
->sd_pollist
.ph_list
== NULL
);
752 ASSERT(stp
->sd_sidp
== NULL
);
753 ASSERT(stp
->sd_pgidp
== NULL
);
755 /* Prevent qenable from re-enabling the stream head queue */
756 disable_svc(_RD(qp
));
759 * Wait until service procedure of each queue is
760 * run, if QINSERVICE is set.
765 * Now, flush both queues.
767 flushq(_RD(qp
), FLUSHALL
);
768 flushq(qp
, FLUSHALL
);
771 * If the write queue of the stream head is pointing to a
772 * read queue, we have a twisted stream. If the read queue
773 * is alive, convert the stream head queues into a dead end.
774 * If the read queue is dead, free the dead pair.
776 if (qp
->q_next
&& !_SAMESTR(qp
)) {
777 if (qp
->q_next
->q_qinfo
== &deadrend
) { /* half-closed pipe */
778 flushq(qp
->q_next
, FLUSHALL
); /* ensure no message */
779 shfree(qp
->q_next
->q_stream
);
782 } else if (qp
->q_next
== _RD(qp
)) { /* fifo */
787 * The q_info pointers are never accessed when
790 ASSERT(qp
->q_syncq
== _RD(qp
)->q_syncq
);
791 mutex_enter(SQLOCK(qp
->q_syncq
));
792 qp
->q_qinfo
= &deadwend
;
793 _RD(qp
)->q_qinfo
= &deadrend
;
794 mutex_exit(SQLOCK(qp
->q_syncq
));
797 freeq(_RD(qp
)); /* free stream head queue pair */
800 mutex_enter(&vp
->v_lock
);
801 if (stp
->sd_iocblk
) {
802 if (stp
->sd_iocblk
!= (mblk_t
*)-1) {
803 freemsg(stp
->sd_iocblk
);
805 stp
->sd_iocblk
= NULL
;
807 stp
->sd_vnode
= NULL
;
809 mutex_exit(&vp
->v_lock
);
810 mutex_enter(&stp
->sd_lock
);
811 freemsg(stp
->sd_cmdblk
);
812 stp
->sd_cmdblk
= NULL
;
813 stp
->sd_flag
&= ~STRCLOSE
;
814 cv_broadcast(&stp
->sd_monitor
);
815 mutex_exit(&stp
->sd_lock
);
823 strsink(queue_t
*q
, mblk_t
*bp
)
825 struct copyresp
*resp
;
827 switch (bp
->b_datap
->db_type
) {
829 if ((*bp
->b_rptr
& FLUSHW
) && !(bp
->b_flag
& MSGNOLOOP
)) {
830 *bp
->b_rptr
&= ~FLUSHR
;
831 bp
->b_flag
|= MSGNOLOOP
;
833 * Protect against the driver passing up
834 * messages after it has done a qprocsoff.
836 if (_OTHERQ(q
)->q_next
== NULL
)
851 bp
->b_datap
->db_type
= M_IOCDATA
;
852 bp
->b_wptr
= bp
->b_rptr
+ sizeof (struct copyresp
);
853 resp
= (struct copyresp
*)bp
->b_rptr
;
854 resp
->cp_rval
= (caddr_t
)1; /* failure */
856 * Protect against the driver passing up
857 * messages after it has done a qprocsoff.
859 if (_OTHERQ(q
)->q_next
== NULL
)
870 bp
->b_datap
->db_type
= M_IOCNAK
;
872 * Protect against the driver passing up
873 * messages after it has done a qprocsoff.
875 if (_OTHERQ(q
)->q_next
== NULL
)
890 * Clean up after a process when it closes a stream. This is called
891 * from closef for all closes, whereas strclose is called only for the
892 * last close on a stream. The siglist is scanned for entries for the
893 * current process, and these are removed.
896 strclean(struct vnode
*vp
)
898 strsig_t
*ssp
, *pssp
, *tssp
;
902 TRACE_1(TR_FAC_STREAMS_FR
,
903 TR_STRCLEAN
, "strclean:%p", vp
);
906 mutex_enter(&stp
->sd_lock
);
907 ssp
= stp
->sd_siglist
;
909 if (ssp
->ss_pidp
== curproc
->p_pidp
) {
912 pssp
->ss_next
= tssp
;
914 stp
->sd_siglist
= tssp
;
915 mutex_enter(&pidlock
);
916 PID_RELE(ssp
->ss_pidp
);
917 mutex_exit(&pidlock
);
918 kmem_free(ssp
, sizeof (strsig_t
));
927 stp
->sd_sigflags
= 0;
928 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
929 stp
->sd_sigflags
|= ssp
->ss_events
;
931 mutex_exit(&stp
->sd_lock
);
935 * Used on the last close to remove any remaining items on the siglist.
936 * These could be present on the siglist due to I_ESETSIG calls that
937 * use process groups or processed that do not have an open file descriptor
938 * for this stream (Such entries would not be removed by strclean).
941 strcleanall(struct vnode
*vp
)
943 strsig_t
*ssp
, *nssp
;
947 mutex_enter(&stp
->sd_lock
);
948 ssp
= stp
->sd_siglist
;
949 stp
->sd_siglist
= NULL
;
952 mutex_enter(&pidlock
);
953 PID_RELE(ssp
->ss_pidp
);
954 mutex_exit(&pidlock
);
955 kmem_free(ssp
, sizeof (strsig_t
));
958 stp
->sd_sigflags
= 0;
959 mutex_exit(&stp
->sd_lock
);
963 * Retrieve the next message from the logical stream head read queue
964 * using either rwnext (if sync stream) or getq_noenab.
965 * It is the callers responsibility to call qbackenable after
966 * it is finished with the message. The caller should not call
967 * qbackenable until after any putback calls to avoid spurious backenabling.
970 strget(struct stdata
*stp
, queue_t
*q
, struct uio
*uiop
, int first
,
977 /* Holding sd_lock prevents the read queue from changing */
978 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
980 if (uiop
!= NULL
&& stp
->sd_struiordq
!= NULL
&&
981 q
->q_first
== NULL
&&
982 (!first
|| (stp
->sd_wakeq
& RSLEEP
))) {
984 * Stream supports rwnext() for the read side.
985 * If this is the first time we're called by e.g. strread
986 * only do the downcall if there is a deferred wakeup
987 * (registered in sd_wakeq).
990 struct iovec buf
[IOV_MAX_STACK
];
994 stp
->sd_wakeq
&= ~RSLEEP
;
996 if (uiop
->uio_iovcnt
> IOV_MAX_STACK
) {
997 iovlen
= uiop
->uio_iovcnt
* sizeof (iovec_t
);
998 uiod
.d_iov
= kmem_alloc(iovlen
, KM_SLEEP
);
1003 (void) uiodup(uiop
, &uiod
.d_uio
, uiod
.d_iov
, uiop
->uio_iovcnt
);
1006 * Mark that a thread is in rwnext on the read side
1007 * to prevent strrput from nacking ioctls immediately.
1008 * When the last concurrent rwnext returns
1009 * the ioctls are nack'ed.
1011 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1012 stp
->sd_struiodnak
++;
1014 * Note: rwnext will drop sd_lock.
1016 error
= rwnext(q
, &uiod
);
1017 ASSERT(MUTEX_NOT_HELD(&stp
->sd_lock
));
1018 mutex_enter(&stp
->sd_lock
);
1019 stp
->sd_struiodnak
--;
1020 while (stp
->sd_struiodnak
== 0 &&
1021 ((bp
= stp
->sd_struionak
) != NULL
)) {
1022 stp
->sd_struionak
= bp
->b_next
;
1024 bp
->b_datap
->db_type
= M_IOCNAK
;
1026 * Protect against the driver passing up
1027 * messages after it has done a qprocsoff.
1029 if (_OTHERQ(q
)->q_next
== NULL
)
1032 mutex_exit(&stp
->sd_lock
);
1034 mutex_enter(&stp
->sd_lock
);
1037 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1038 if (error
== 0 || error
== EWOULDBLOCK
) {
1039 if ((bp
= uiod
.d_mp
) != NULL
) {
1041 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1043 kmem_free(uiod
.d_iov
, iovlen
);
1047 } else if (error
== EINVAL
) {
1049 * The stream plumbing must have
1050 * changed while we were away, so
1051 * just turn off rwnext()s.
1054 } else if (error
== EBUSY
) {
1056 * The module might have data in transit using putnext
1057 * Fall back on waiting + getq.
1062 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1064 kmem_free(uiod
.d_iov
, iovlen
);
1069 kmem_free(uiod
.d_iov
, iovlen
);
1072 * Try a getq in case a rwnext() generated mblk
1073 * has bubbled up via strrput().
1077 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1080 * If we have a valid uio, try and use this as a guide for how
1081 * many bytes to retrieve from the queue via getq_noenab().
1082 * Doing this can avoid unneccesary counting of overlong
1083 * messages in putback(). We currently only do this for sockets
1084 * and only if there is no sd_rputdatafunc hook.
1086 * The sd_rputdatafunc hook transforms the entire message
1087 * before any bytes in it can be given to a client. So, rbytes
1088 * must be 0 if there is a hook.
1090 if ((uiop
!= NULL
) && (stp
->sd_vnode
->v_type
== VSOCK
) &&
1091 (stp
->sd_rputdatafunc
== NULL
))
1092 rbytes
= uiop
->uio_resid
;
1094 return (getq_noenab(q
, rbytes
));
1098 * Copy out the message pointed to by `bp' into the uio pointed to by `uiop'.
1099 * If the message does not fit in the uio the remainder of it is returned;
1100 * otherwise NULL is returned. Any embedded zero-length mblk_t's are
1101 * consumed, even if uio_resid reaches zero. On error, `*errorp' is set to
1102 * the error code, the message is consumed, and NULL is returned.
1105 struiocopyout(mblk_t
*bp
, struct uio
*uiop
, int *errorp
)
1111 ASSERT(bp
->b_wptr
>= bp
->b_rptr
);
1114 if ((n
= MIN(uiop
->uio_resid
, MBLKL(bp
))) != 0) {
1117 error
= uiomove(bp
->b_rptr
, n
, UIO_READ
, uiop
);
1126 while (bp
!= NULL
&& (bp
->b_rptr
>= bp
->b_wptr
)) {
1131 } while (bp
!= NULL
&& uiop
->uio_resid
> 0);
1138 * Read a stream according to the mode flags in sd_flag:
1140 * (default mode) - Byte stream, msg boundaries are ignored
1141 * RD_MSGDIS (msg discard) - Read on msg boundaries and throw away
1142 * any data remaining in msg
1143 * RD_MSGNODIS (msg non-discard) - Read on msg boundaries and put back
1144 * any remaining data on head of read queue
1146 * Consume readable messages on the front of the queue until
1147 * ttolwp(curthread)->lwp_count
1148 * is satisfied, the readable messages are exhausted, or a message
1149 * boundary is reached in a message mode. If no data was read and
1150 * the stream was not opened with the NDELAY flag, block until data arrives.
1151 * Otherwise return the data read and update the count.
1153 * In default mode a 0 length message signifies end-of-file and terminates
1154 * a read in progress. The 0 length message is removed from the queue
1155 * only if it is the only message read (no data is read).
1157 * An attempt to read an M_PROTO or M_PCPROTO message results in an
1158 * EBADMSG error return, unless either RD_PROTDAT or RD_PROTDIS are set.
1159 * If RD_PROTDAT is set, M_PROTO and M_PCPROTO messages are read as data.
1160 * If RD_PROTDIS is set, the M_PROTO and M_PCPROTO parts of the message
1161 * are unlinked from and M_DATA blocks in the message, the protos are
1162 * thrown away, and the data is read.
1166 strread(struct vnode
*vp
, struct uio
*uiop
, cred_t
*crp
)
1175 uint_t mark
; /* Contains MSG*MARK and _LASTMARK */
1176 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
1178 unsigned char pri
= 0;
1182 TRACE_1(TR_FAC_STREAMS_FR
,
1183 TR_STRREAD_ENTER
, "strread:%p", vp
);
1184 ASSERT(vp
->v_stream
);
1187 mutex_enter(&stp
->sd_lock
);
1189 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
1190 mutex_exit(&stp
->sd_lock
);
1194 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
1195 error
= strgeterr(stp
, STRDERR
|STPLEX
, 0);
1197 mutex_exit(&stp
->sd_lock
);
1203 * Loop terminates when uiop->uio_resid == 0.
1206 waitflag
= READWAIT
;
1207 q
= _RD(stp
->sd_wrq
);
1209 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1210 old_sd_flag
= stp
->sd_flag
;
1214 while ((bp
= strget(stp
, q
, uiop
, first
, &error
)) == NULL
) {
1217 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1222 if (stp
->sd_flag
& (STRHUP
|STREOF
)) {
1225 if (rflg
&& !(stp
->sd_flag
& STRDELIM
)) {
1229 * If a read(fd,buf,0) has been done, there is no
1230 * need to sleep. We always have zero bytes to
1233 if (uiop
->uio_resid
== 0) {
1239 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRREAD_WAIT
,
1240 "strread calls strwaitq:%p, %p, %p",
1242 if ((error
= strwaitq(stp
, waitflag
, uiop
->uio_resid
,
1243 uiop
->uio_fmode
, -1, &done
)) != 0 || done
) {
1244 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRREAD_DONE
,
1245 "strread error or done:%p, %p, %p",
1247 if ((uiop
->uio_fmode
& FNDELAY
) &&
1248 (stp
->sd_flag
& OLDNDELAY
) &&
1253 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRREAD_AWAKE
,
1254 "strread awakes:%p, %p, %p", vp
, uiop
, crp
);
1255 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
1261 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1265 * Extract any mark information. If the message is not
1266 * completely consumed this information will be put in the mblk
1268 * If MSGMARKNEXT is set and the message is completely consumed
1269 * the STRATMARK flag will be set below. Likewise, if
1270 * MSGNOTMARKNEXT is set and the message is
1271 * completely consumed STRNOTATMARK will be set.
1273 * For some unknown reason strread only breaks the read at the
1276 mark
= bp
->b_flag
& (MSGMARK
| MSGMARKNEXT
| MSGNOTMARKNEXT
);
1277 ASSERT((mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
)) !=
1278 (MSGMARKNEXT
|MSGNOTMARKNEXT
));
1279 if (mark
!= 0 && bp
== stp
->sd_mark
) {
1281 putback(stp
, q
, bp
, pri
);
1285 stp
->sd_mark
= NULL
;
1287 if ((stp
->sd_flag
& STRDELIM
) && (bp
->b_flag
& MSGDELIM
))
1289 mutex_exit(&stp
->sd_lock
);
1291 if (STREAM_NEEDSERVICE(stp
))
1292 stream_runservice(stp
);
1294 type
= bp
->b_datap
->db_type
;
1300 if (msgnodata(bp
)) {
1301 if (mark
|| delim
) {
1306 * If already read data put zero
1307 * length message back on queue else
1308 * free msg and return 0.
1311 mutex_enter(&stp
->sd_lock
);
1312 putback(stp
, q
, bp
, pri
);
1313 mutex_exit(&stp
->sd_lock
);
1323 bp
= struiocopyout(bp
, uiop
, &error
);
1327 mutex_enter(&stp
->sd_lock
);
1330 * Have remaining data in message.
1331 * Free msg if in discard mode.
1333 if (stp
->sd_read_opt
& RD_MSGDIS
) {
1337 if ((mark
& _LASTMARK
) &&
1338 (stp
->sd_mark
== NULL
))
1340 bp
->b_flag
|= mark
& ~_LASTMARK
;
1342 bp
->b_flag
|= MSGDELIM
;
1346 putback(stp
, q
, bp
, pri
);
1350 * Consumed the complete message.
1351 * Move the MSG*MARKNEXT information
1352 * to the stream head just in case
1353 * the read queue becomes empty.
1355 * If the stream head was at the mark
1356 * (STRATMARK) before we dropped sd_lock above
1357 * and some data was consumed then we have
1358 * moved past the mark thus STRATMARK is
1359 * cleared. However, if a message arrived in
1360 * strrput during the copyout above causing
1361 * STRATMARK to be set we can not clear that
1365 (MSGMARKNEXT
|MSGNOTMARKNEXT
|MSGMARK
)) {
1366 if (mark
& MSGMARKNEXT
) {
1367 stp
->sd_flag
&= ~STRNOTATMARK
;
1368 stp
->sd_flag
|= STRATMARK
;
1369 } else if (mark
& MSGNOTMARKNEXT
) {
1370 stp
->sd_flag
&= ~STRATMARK
;
1371 stp
->sd_flag
|= STRNOTATMARK
;
1374 ~(STRATMARK
|STRNOTATMARK
);
1376 } else if (rflg
&& (old_sd_flag
& STRATMARK
)) {
1377 stp
->sd_flag
&= ~STRATMARK
;
1382 * Check for signal messages at the front of the read
1383 * queue and generate the signal(s) if appropriate.
1384 * The only signal that can be on queue is M_SIG at
1387 while ((((bp
= q
->q_first
)) != NULL
) &&
1388 (bp
->b_datap
->db_type
== M_SIG
)) {
1389 bp
= getq_noenab(q
, 0);
1391 * sd_lock is held so the content of the
1392 * read queue can not change.
1394 ASSERT(bp
!= NULL
&& DB_TYPE(bp
) == M_SIG
);
1395 strsignal_nolock(stp
, *bp
->b_rptr
, bp
->b_band
);
1396 mutex_exit(&stp
->sd_lock
);
1398 if (STREAM_NEEDSERVICE(stp
))
1399 stream_runservice(stp
);
1400 mutex_enter(&stp
->sd_lock
);
1403 if ((uiop
->uio_resid
== 0) || (mark
& _LASTMARK
) ||
1405 (stp
->sd_read_opt
& (RD_MSGDIS
|RD_MSGNODIS
))) {
1411 strsignal(stp
, *bp
->b_rptr
, (int32_t)bp
->b_band
);
1413 mutex_enter(&stp
->sd_lock
);
1419 * Only data messages are readable.
1420 * Any others generate an error, unless
1421 * RD_PROTDIS or RD_PROTDAT is set.
1423 if (stp
->sd_read_opt
& RD_PROTDAT
) {
1424 for (nbp
= bp
; nbp
; nbp
= nbp
->b_next
) {
1425 if ((nbp
->b_datap
->db_type
==
1427 (nbp
->b_datap
->db_type
==
1429 nbp
->b_datap
->db_type
= M_DATA
;
1435 * clear stream head hi pri flag based on
1438 if (type
== M_PCPROTO
) {
1439 mutex_enter(&stp
->sd_lock
);
1440 stp
->sd_flag
&= ~STRPRI
;
1441 mutex_exit(&stp
->sd_lock
);
1444 } else if (stp
->sd_read_opt
& RD_PROTDIS
) {
1446 * discard non-data messages
1449 ((bp
->b_datap
->db_type
== M_PROTO
) ||
1450 (bp
->b_datap
->db_type
== M_PCPROTO
))) {
1456 * clear stream head hi pri flag based on
1459 if (type
== M_PCPROTO
) {
1460 mutex_enter(&stp
->sd_lock
);
1461 stp
->sd_flag
&= ~STRPRI
;
1462 mutex_exit(&stp
->sd_lock
);
1473 if ((bp
->b_datap
->db_type
== M_PASSFP
) &&
1474 (stp
->sd_read_opt
& RD_PROTDIS
)) {
1478 mutex_enter(&stp
->sd_lock
);
1479 putback(stp
, q
, bp
, pri
);
1480 mutex_exit(&stp
->sd_lock
);
1487 * Garbage on stream head read queue.
1489 cmn_err(CE_WARN
, "bad %x found at stream head\n",
1490 bp
->b_datap
->db_type
);
1494 mutex_enter(&stp
->sd_lock
);
1497 mutex_exit(&stp
->sd_lock
);
1499 qbackenable(q
, pri
);
1505 * Default processing of M_PROTO/M_PCPROTO messages.
1506 * Determine which wakeups and signals are needed.
1507 * This can be replaced by a user-specified procedure for kernel users
1512 strrput_proto(vnode_t
*vp
, mblk_t
*mp
,
1513 strwakeup_t
*wakeups
, strsigset_t
*firstmsgsigs
,
1514 strsigset_t
*allmsgsigs
, strpollset_t
*pollwakeups
)
1519 switch (mp
->b_datap
->db_type
) {
1521 if (mp
->b_band
== 0) {
1522 *firstmsgsigs
= S_INPUT
| S_RDNORM
;
1523 *pollwakeups
= POLLIN
| POLLRDNORM
;
1525 *firstmsgsigs
= S_INPUT
| S_RDBAND
;
1526 *pollwakeups
= POLLIN
| POLLRDBAND
;
1530 *firstmsgsigs
= S_HIPRI
;
1531 *pollwakeups
= POLLPRI
;
1538 * Default processing of everything but M_DATA, M_PROTO, M_PCPROTO and
1539 * M_PASSFP messages.
1540 * Determine which wakeups and signals are needed.
1541 * This can be replaced by a user-specified procedure for kernel users
1546 strrput_misc(vnode_t
*vp
, mblk_t
*mp
,
1547 strwakeup_t
*wakeups
, strsigset_t
*firstmsgsigs
,
1548 strsigset_t
*allmsgsigs
, strpollset_t
*pollwakeups
)
1558 * Stream read put procedure. Called from downstream driver/module
1559 * with messages for the stream head. Data, protocol, and in-stream
1560 * signal messages are placed on the queue, others are handled directly.
1563 strrput(queue_t
*q
, mblk_t
*bp
)
1567 strwakeup_t wakeups
;
1568 strsigset_t firstmsgsigs
; /* Signals if first message on queue */
1569 strsigset_t allmsgsigs
; /* Signals for all messages */
1570 strsigset_t signals
; /* Signals events to generate */
1571 strpollset_t pollwakeups
;
1576 stp
= (struct stdata
*)q
->q_ptr
;
1578 * Use rput_opt for optimized access to the SR_ flags except
1579 * SR_POLLIN. That flag has to be checked under sd_lock since it
1580 * is modified by strpoll().
1582 rput_opt
= stp
->sd_rput_opt
;
1584 ASSERT(qclaimed(q
));
1585 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRRPUT_ENTER
,
1586 "strrput called with message type:q %p bp %p", q
, bp
);
1589 * Perform initial processing and pass to the parameterized functions.
1591 ASSERT(bp
->b_next
== NULL
);
1593 switch (bp
->b_datap
->db_type
) {
1596 * sockfs is the only consumer of STREOF and when it is set,
1597 * it implies that the receiver is not interested in receiving
1598 * any more data, hence the mblk is freed to prevent unnecessary
1599 * message queueing at the stream head.
1601 if (stp
->sd_flag
== STREOF
) {
1605 if ((rput_opt
& SR_IGN_ZEROLEN
) &&
1606 bp
->b_rptr
== bp
->b_wptr
&& msgnodata(bp
)) {
1608 * Ignore zero-length M_DATA messages. These might be
1609 * generated by some transports.
1610 * The zero-length M_DATA messages, even if they
1611 * are ignored, should effect the atmark tracking and
1612 * should wake up a thread sleeping in strwaitmark.
1614 mutex_enter(&stp
->sd_lock
);
1615 if (bp
->b_flag
& MSGMARKNEXT
) {
1617 * Record the position of the mark either
1618 * in q_last or in STRATMARK.
1620 if (q
->q_last
!= NULL
) {
1621 q
->q_last
->b_flag
&= ~MSGNOTMARKNEXT
;
1622 q
->q_last
->b_flag
|= MSGMARKNEXT
;
1624 stp
->sd_flag
&= ~STRNOTATMARK
;
1625 stp
->sd_flag
|= STRATMARK
;
1627 } else if (bp
->b_flag
& MSGNOTMARKNEXT
) {
1629 * Record that this is not the position of
1630 * the mark either in q_last or in
1633 if (q
->q_last
!= NULL
) {
1634 q
->q_last
->b_flag
&= ~MSGMARKNEXT
;
1635 q
->q_last
->b_flag
|= MSGNOTMARKNEXT
;
1637 stp
->sd_flag
&= ~STRATMARK
;
1638 stp
->sd_flag
|= STRNOTATMARK
;
1641 if (stp
->sd_flag
& RSLEEP
) {
1642 stp
->sd_flag
&= ~RSLEEP
;
1643 cv_broadcast(&q
->q_wait
);
1645 mutex_exit(&stp
->sd_lock
);
1650 if (bp
->b_band
== 0) {
1651 firstmsgsigs
= S_INPUT
| S_RDNORM
;
1652 pollwakeups
= POLLIN
| POLLRDNORM
;
1654 firstmsgsigs
= S_INPUT
| S_RDBAND
;
1655 pollwakeups
= POLLIN
| POLLRDBAND
;
1657 if (rput_opt
& SR_SIGALLDATA
)
1658 allmsgsigs
= firstmsgsigs
;
1662 mutex_enter(&stp
->sd_lock
);
1663 if ((rput_opt
& SR_CONSOL_DATA
) &&
1664 (q
->q_last
!= NULL
) &&
1665 (bp
->b_flag
& (MSGMARK
|MSGDELIM
)) == 0) {
1667 * Consolidate an M_DATA message onto an M_DATA,
1668 * M_PROTO, or M_PCPROTO by merging it with q_last.
1669 * The consolidation does not take place if
1670 * the old message is marked with either of the
1671 * marks or the delim flag or if the new
1672 * message is marked with MSGMARK. The MSGMARK
1673 * check is needed to handle the odd semantics of
1674 * MSGMARK where essentially the whole message
1675 * is to be treated as marked.
1676 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from the
1677 * new message to the front of the b_cont chain.
1679 mblk_t
*lbp
= q
->q_last
;
1680 unsigned char db_type
= lbp
->b_datap
->db_type
;
1682 if ((db_type
== M_DATA
|| db_type
== M_PROTO
||
1683 db_type
== M_PCPROTO
) &&
1684 !(lbp
->b_flag
& (MSGDELIM
|MSGMARK
|MSGMARKNEXT
))) {
1685 rmvq_noenab(q
, lbp
);
1687 * The first message in the b_cont list
1688 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
1689 * We need to handle the case where we
1692 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
1693 * 2) a MSGMARKNEXT to a plain message.
1694 * 3) a MSGNOTMARKNEXT to a plain message
1695 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
1698 * Thus we never append a MSGMARKNEXT or
1699 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
1701 if (bp
->b_flag
& MSGMARKNEXT
) {
1702 lbp
->b_flag
|= MSGMARKNEXT
;
1703 lbp
->b_flag
&= ~MSGNOTMARKNEXT
;
1704 bp
->b_flag
&= ~MSGMARKNEXT
;
1705 } else if (bp
->b_flag
& MSGNOTMARKNEXT
) {
1706 lbp
->b_flag
|= MSGNOTMARKNEXT
;
1707 bp
->b_flag
&= ~MSGNOTMARKNEXT
;
1713 * The new message logically isn't the first
1714 * even though the q_first check below thinks
1715 * it is. Clear the firstmsgsigs to make it
1716 * not appear to be first.
1726 if (bp
->b_band
== 0) {
1727 firstmsgsigs
= S_INPUT
| S_RDNORM
;
1728 pollwakeups
= POLLIN
| POLLRDNORM
;
1730 firstmsgsigs
= S_INPUT
| S_RDBAND
;
1731 pollwakeups
= POLLIN
| POLLRDBAND
;
1733 mutex_enter(&stp
->sd_lock
);
1738 ASSERT(stp
->sd_rprotofunc
!= NULL
);
1739 bp
= (stp
->sd_rprotofunc
)(stp
->sd_vnode
, bp
,
1740 &wakeups
, &firstmsgsigs
, &allmsgsigs
, &pollwakeups
);
1741 #define ALLSIG (S_INPUT|S_HIPRI|S_OUTPUT|S_MSG|S_ERROR|S_HANGUP|S_RDNORM|\
1742 S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)
1743 #define ALLPOLL (POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLWRNORM|POLLRDBAND|\
1746 ASSERT((wakeups
& ~(RSLEEP
|WSLEEP
)) == 0);
1747 ASSERT((firstmsgsigs
& ~ALLSIG
) == 0);
1748 ASSERT((allmsgsigs
& ~ALLSIG
) == 0);
1749 ASSERT((pollwakeups
& ~ALLPOLL
) == 0);
1751 mutex_enter(&stp
->sd_lock
);
1755 ASSERT(stp
->sd_rmiscfunc
!= NULL
);
1756 bp
= (stp
->sd_rmiscfunc
)(stp
->sd_vnode
, bp
,
1757 &wakeups
, &firstmsgsigs
, &allmsgsigs
, &pollwakeups
);
1758 ASSERT((wakeups
& ~(RSLEEP
|WSLEEP
)) == 0);
1759 ASSERT((firstmsgsigs
& ~ALLSIG
) == 0);
1760 ASSERT((allmsgsigs
& ~ALLSIG
) == 0);
1761 ASSERT((pollwakeups
& ~ALLPOLL
) == 0);
1764 mutex_enter(&stp
->sd_lock
);
1767 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1769 /* By default generate superset of signals */
1770 signals
= (firstmsgsigs
| allmsgsigs
);
1773 * The proto and misc functions can return multiple messages
1774 * as a b_next chain. Such messages are processed separately.
1781 nextbp
= bp
->b_next
;
1784 switch (bp
->b_datap
->db_type
) {
1787 * Only one priority protocol message is allowed at the
1788 * stream head at a time.
1790 if (stp
->sd_flag
& STRPRI
) {
1791 TRACE_0(TR_FAC_STREAMS_FR
, TR_STRRPUT_PROTERR
,
1792 "M_PCPROTO already at head");
1794 mutex_exit(&stp
->sd_lock
);
1797 stp
->sd_flag
|= STRPRI
;
1805 * Marking doesn't work well when messages
1806 * are marked in more than one band. We only
1807 * remember the last message received, even if
1808 * it is placed on the queue ahead of other
1811 if (bp
->b_flag
& MSGMARK
)
1816 * If message is a PCPROTO message, always use
1817 * firstmsgsigs to determine if a signal should be
1818 * sent as strrput is the only place to send
1819 * signals for PCPROTO. Other messages are based on
1820 * the STRGETINPROG flag. The flag determines if
1821 * strrput or (k)strgetmsg will be responsible for
1822 * sending the signals, in the firstmsgsigs case.
1824 if ((hipri_sig
== 1) ||
1825 (((stp
->sd_flag
& STRGETINPROG
) == 0) &&
1826 (q
->q_first
== bp
)))
1827 signals
= (firstmsgsigs
| allmsgsigs
);
1829 signals
= allmsgsigs
;
1833 mutex_exit(&stp
->sd_lock
);
1834 (void) strrput_nondata(q
, bp
);
1835 mutex_enter(&stp
->sd_lock
);
1839 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
1841 * Wake sleeping read/getmsg and cancel deferred wakeup
1843 if (wakeups
& RSLEEP
)
1844 stp
->sd_wakeq
&= ~RSLEEP
;
1846 wakeups
&= stp
->sd_flag
;
1847 if (wakeups
& RSLEEP
) {
1848 stp
->sd_flag
&= ~RSLEEP
;
1849 cv_broadcast(&q
->q_wait
);
1851 if (wakeups
& WSLEEP
) {
1852 stp
->sd_flag
&= ~WSLEEP
;
1853 cv_broadcast(&_WR(q
)->q_wait
);
1856 if (pollwakeups
!= 0) {
1857 if (pollwakeups
== (POLLIN
| POLLRDNORM
)) {
1859 * Can't use rput_opt since it was not
1860 * read when sd_lock was held and SR_POLLIN is changed
1861 * by strpoll() under sd_lock.
1863 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
1865 stp
->sd_rput_opt
&= ~SR_POLLIN
;
1867 mutex_exit(&stp
->sd_lock
);
1868 pollwakeup(&stp
->sd_pollist
, pollwakeups
);
1869 mutex_enter(&stp
->sd_lock
);
1874 * strsendsig can handle multiple signals with a
1877 if (stp
->sd_sigflags
& signals
)
1878 strsendsig(stp
->sd_siglist
, signals
, band
, 0);
1879 mutex_exit(&stp
->sd_lock
);
1887 * Any signals were handled the first time.
1888 * Wakeups and pollwakeups are redone to avoid any race
1889 * conditions - all the messages are not queued until the
1890 * last message has been processed by strrput.
1893 signals
= firstmsgsigs
= allmsgsigs
= 0;
1894 mutex_enter(&stp
->sd_lock
);
1899 log_dupioc(queue_t
*rq
, mblk_t
*bp
)
1902 char *modnames
, *mnp
, *dname
;
1907 * Allocate a buffer large enough to hold the names of nstrpush modules
1908 * and one driver, with spaces between and NUL terminator. If we can't
1909 * get memory, then we'll just log the driver name.
1911 maxmodstr
= nstrpush
* (FMNAMESZ
+ 1);
1912 mnp
= modnames
= kmem_alloc(maxmodstr
, KM_NOSLEEP
);
1914 /* march down write side to print log message down to the driver */
1917 /* make sure q_next doesn't shift around while we're grabbing data */
1922 islast
= !SAMESTR(qp
) || qp
->q_next
== NULL
;
1923 if (modnames
== NULL
) {
1925 * If we don't have memory, then get the driver name in
1926 * the log where we can see it. Note that memory
1927 * pressure is a possible cause of these sorts of bugs.
1934 mnp
+= snprintf(mnp
, FMNAMESZ
+ 1, "%s", dname
);
1941 /* Cannot happen unless stream head is corrupt. */
1942 ASSERT(modnames
!= NULL
);
1943 (void) strlog(rq
->q_qinfo
->qi_minfo
->mi_idnum
, 0, 1,
1944 SL_CONSOLE
|SL_TRACE
|SL_ERROR
,
1945 "Warning: stream %p received duplicate %X M_IOC%s; module list: %s",
1946 rq
->q_ptr
, ((struct iocblk
*)bp
->b_rptr
)->ioc_cmd
,
1947 (DB_TYPE(bp
) == M_IOCACK
? "ACK" : "NAK"), modnames
);
1949 kmem_free(modnames
, maxmodstr
);
1953 strrput_nondata(queue_t
*q
, mblk_t
*bp
)
1956 struct iocblk
*iocbp
;
1957 struct stroptions
*sop
;
1958 struct copyreq
*reqp
;
1959 struct copyresp
*resp
;
1961 unsigned char flushed_already
= 0;
1963 stp
= (struct stdata
*)q
->q_ptr
;
1965 ASSERT(!(stp
->sd_flag
& STPLEX
));
1966 ASSERT(qclaimed(q
));
1968 switch (bp
->b_datap
->db_type
) {
1971 * An error has occurred downstream, the errno is in the first
1972 * bytes of the message.
1974 if ((bp
->b_wptr
- bp
->b_rptr
) == 2) { /* New flavor */
1975 unsigned char rw
= 0;
1977 mutex_enter(&stp
->sd_lock
);
1978 if (*bp
->b_rptr
!= NOERROR
) { /* read error */
1979 if (*bp
->b_rptr
!= 0) {
1980 if (stp
->sd_flag
& STRDERR
)
1981 flushed_already
|= FLUSHR
;
1982 stp
->sd_flag
|= STRDERR
;
1985 stp
->sd_flag
&= ~STRDERR
;
1987 stp
->sd_rerror
= *bp
->b_rptr
;
1990 if (*bp
->b_rptr
!= NOERROR
) { /* write error */
1991 if (*bp
->b_rptr
!= 0) {
1992 if (stp
->sd_flag
& STWRERR
)
1993 flushed_already
|= FLUSHW
;
1994 stp
->sd_flag
|= STWRERR
;
1997 stp
->sd_flag
&= ~STWRERR
;
1999 stp
->sd_werror
= *bp
->b_rptr
;
2002 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRRPUT_WAKE
,
2003 "strrput cv_broadcast:q %p, bp %p",
2005 cv_broadcast(&q
->q_wait
); /* readers */
2006 cv_broadcast(&_WR(q
)->q_wait
); /* writers */
2007 cv_broadcast(&stp
->sd_monitor
); /* ioctllers */
2009 mutex_exit(&stp
->sd_lock
);
2010 pollwakeup(&stp
->sd_pollist
, POLLERR
);
2011 mutex_enter(&stp
->sd_lock
);
2013 if (stp
->sd_sigflags
& S_ERROR
)
2014 strsendsig(stp
->sd_siglist
, S_ERROR
, 0,
2015 ((rw
& FLUSHR
) ? stp
->sd_rerror
:
2017 mutex_exit(&stp
->sd_lock
);
2019 * Send the M_FLUSH only
2020 * for the first M_ERROR
2021 * message on the stream
2023 if (flushed_already
== rw
) {
2028 bp
->b_datap
->db_type
= M_FLUSH
;
2030 bp
->b_wptr
= bp
->b_rptr
+ 1;
2032 * Protect against the driver
2033 * passing up messages after
2034 * it has done a qprocsoff
2036 if (_OTHERQ(q
)->q_next
== NULL
)
2042 mutex_exit(&stp
->sd_lock
);
2043 } else if (*bp
->b_rptr
!= 0) { /* Old flavor */
2044 if (stp
->sd_flag
& (STRDERR
|STWRERR
))
2045 flushed_already
= FLUSHRW
;
2046 mutex_enter(&stp
->sd_lock
);
2047 stp
->sd_flag
|= (STRDERR
|STWRERR
);
2048 stp
->sd_rerror
= *bp
->b_rptr
;
2049 stp
->sd_werror
= *bp
->b_rptr
;
2050 TRACE_2(TR_FAC_STREAMS_FR
,
2052 "strrput wakeup #2:q %p, bp %p", q
, bp
);
2053 cv_broadcast(&q
->q_wait
); /* the readers */
2054 cv_broadcast(&_WR(q
)->q_wait
); /* the writers */
2055 cv_broadcast(&stp
->sd_monitor
); /* ioctllers */
2057 mutex_exit(&stp
->sd_lock
);
2058 pollwakeup(&stp
->sd_pollist
, POLLERR
);
2059 mutex_enter(&stp
->sd_lock
);
2061 if (stp
->sd_sigflags
& S_ERROR
)
2062 strsendsig(stp
->sd_siglist
, S_ERROR
, 0,
2063 (stp
->sd_werror
? stp
->sd_werror
:
2065 mutex_exit(&stp
->sd_lock
);
2068 * Send the M_FLUSH only
2069 * for the first M_ERROR
2070 * message on the stream
2072 if (flushed_already
!= FLUSHRW
) {
2073 bp
->b_datap
->db_type
= M_FLUSH
;
2074 *bp
->b_rptr
= FLUSHRW
;
2076 * Protect against the driver passing up
2077 * messages after it has done a
2080 if (_OTHERQ(q
)->q_next
== NULL
)
2093 mutex_enter(&stp
->sd_lock
);
2094 stp
->sd_werror
= ENXIO
;
2095 stp
->sd_flag
|= STRHUP
;
2096 stp
->sd_flag
&= ~(WSLEEP
|RSLEEP
);
2099 * send signal if controlling tty
2103 prsignal(stp
->sd_sidp
, SIGHUP
);
2104 if (stp
->sd_sidp
!= stp
->sd_pgidp
)
2105 pgsignal(stp
->sd_pgidp
, SIGTSTP
);
2109 * wake up read, write, and exception pollers and
2110 * reset wakeup mechanism.
2112 cv_broadcast(&q
->q_wait
); /* the readers */
2113 cv_broadcast(&_WR(q
)->q_wait
); /* the writers */
2114 cv_broadcast(&stp
->sd_monitor
); /* the ioctllers */
2116 mutex_exit(&stp
->sd_lock
);
2121 mutex_enter(&stp
->sd_lock
);
2123 stp
->sd_flag
&= ~STRHUP
;
2124 mutex_exit(&stp
->sd_lock
);
2129 * Someone downstream wants to post a signal. The
2130 * signal to post is contained in the first byte of the
2131 * message. If the message would go on the front of
2132 * the queue, send a signal to the process group
2133 * (if not SIGPOLL) or to the siglist processes
2134 * (SIGPOLL). If something is already on the queue,
2135 * OR if we are delivering a delayed suspend (*sigh*
2136 * another "tty" hack) and there's no one sleeping already,
2137 * just enqueue the message.
2139 mutex_enter(&stp
->sd_lock
);
2140 if (q
->q_first
|| (*bp
->b_rptr
== SIGTSTP
&&
2141 !(stp
->sd_flag
& RSLEEP
))) {
2143 mutex_exit(&stp
->sd_lock
);
2146 mutex_exit(&stp
->sd_lock
);
2151 * Don't enqueue, just post the signal.
2153 strsignal(stp
, *bp
->b_rptr
, 0L);
2158 if (MBLKL(bp
) != sizeof (cmdblk_t
)) {
2163 mutex_enter(&stp
->sd_lock
);
2164 if (stp
->sd_flag
& STRCMDWAIT
) {
2165 ASSERT(stp
->sd_cmdblk
== NULL
);
2166 stp
->sd_cmdblk
= bp
;
2167 cv_broadcast(&stp
->sd_monitor
);
2168 mutex_exit(&stp
->sd_lock
);
2170 mutex_exit(&stp
->sd_lock
);
2177 * Flush queues. The indication of which queues to flush
2178 * is in the first byte of the message. If the read queue
2179 * is specified, then flush it. If FLUSHBAND is set, just
2180 * flush the band specified by the second byte of the message.
2182 * If a module has issued a M_SETOPT to not flush hi
2183 * priority messages off of the stream head, then pass this
2184 * flag into the flushq code to preserve such messages.
2187 if (*bp
->b_rptr
& FLUSHR
) {
2188 mutex_enter(&stp
->sd_lock
);
2189 if (*bp
->b_rptr
& FLUSHBAND
) {
2190 ASSERT((bp
->b_wptr
- bp
->b_rptr
) >= 2);
2191 flushband(q
, *(bp
->b_rptr
+ 1), FLUSHALL
);
2193 flushq_common(q
, FLUSHALL
,
2194 stp
->sd_read_opt
& RFLUSHPCPROT
);
2195 if ((q
->q_first
== NULL
) ||
2196 (q
->q_first
->b_datap
->db_type
< QPCTL
))
2197 stp
->sd_flag
&= ~STRPRI
;
2199 ASSERT(stp
->sd_flag
& STRPRI
);
2201 mutex_exit(&stp
->sd_lock
);
2203 if ((*bp
->b_rptr
& FLUSHW
) && !(bp
->b_flag
& MSGNOLOOP
)) {
2204 *bp
->b_rptr
&= ~FLUSHR
;
2205 bp
->b_flag
|= MSGNOLOOP
;
2207 * Protect against the driver passing up
2208 * messages after it has done a qprocsoff.
2210 if (_OTHERQ(q
)->q_next
== NULL
)
2221 iocbp
= (struct iocblk
*)bp
->b_rptr
;
2223 * If not waiting for ACK or NAK then just free msg.
2224 * If incorrect id sequence number then just free msg.
2225 * If already have ACK or NAK for user then this is a
2226 * duplicate, display a warning and free the msg.
2228 mutex_enter(&stp
->sd_lock
);
2229 if ((stp
->sd_flag
& IOCWAIT
) == 0 || stp
->sd_iocblk
||
2230 (stp
->sd_iocid
!= iocbp
->ioc_id
)) {
2232 * If the ACK/NAK is a dup, display a message
2233 * Dup is when sd_iocid == ioc_id, and
2234 * sd_iocblk == <valid ptr> or -1 (the former
2235 * is when an ioctl has been put on the stream
2236 * head, but has not yet been consumed, the
2237 * later is when it has been consumed).
2239 if ((stp
->sd_iocid
== iocbp
->ioc_id
) &&
2240 (stp
->sd_iocblk
!= NULL
)) {
2244 mutex_exit(&stp
->sd_lock
);
2249 * Assign ACK or NAK to user and wake up.
2251 stp
->sd_iocblk
= bp
;
2252 cv_broadcast(&stp
->sd_monitor
);
2253 mutex_exit(&stp
->sd_lock
);
2258 reqp
= (struct copyreq
*)bp
->b_rptr
;
2261 * If not waiting for ACK or NAK then just fail request.
2262 * If already have ACK, NAK, or copy request, then just
2264 * If incorrect id sequence number then just fail request.
2266 mutex_enter(&stp
->sd_lock
);
2267 if ((stp
->sd_flag
& IOCWAIT
) == 0 || stp
->sd_iocblk
||
2268 (stp
->sd_iocid
!= reqp
->cq_id
)) {
2270 freemsg(bp
->b_cont
);
2273 bp
->b_datap
->db_type
= M_IOCDATA
;
2274 bp
->b_wptr
= bp
->b_rptr
+ sizeof (struct copyresp
);
2275 resp
= (struct copyresp
*)bp
->b_rptr
;
2276 resp
->cp_rval
= (caddr_t
)1; /* failure */
2277 mutex_exit(&stp
->sd_lock
);
2278 putnext(stp
->sd_wrq
, bp
);
2283 * Assign copy request to user and wake up.
2285 stp
->sd_iocblk
= bp
;
2286 cv_broadcast(&stp
->sd_monitor
);
2287 mutex_exit(&stp
->sd_lock
);
2292 * Set stream head options (read option, write offset,
2293 * min/max packet size, and/or high/low water marks for
2294 * the read side only).
2298 sop
= (struct stroptions
*)bp
->b_rptr
;
2299 mutex_enter(&stp
->sd_lock
);
2300 if (sop
->so_flags
& SO_READOPT
) {
2301 switch (sop
->so_readopt
& RMODEMASK
) {
2303 stp
->sd_read_opt
&= ~(RD_MSGDIS
| RD_MSGNODIS
);
2308 ((stp
->sd_read_opt
& ~RD_MSGNODIS
) |
2314 ((stp
->sd_read_opt
& ~RD_MSGDIS
) |
2318 switch (sop
->so_readopt
& RPROTMASK
) {
2320 stp
->sd_read_opt
&= ~(RD_PROTDAT
| RD_PROTDIS
);
2325 ((stp
->sd_read_opt
& ~RD_PROTDIS
) |
2331 ((stp
->sd_read_opt
& ~RD_PROTDAT
) |
2335 switch (sop
->so_readopt
& RFLUSHMASK
) {
2338 * This sets the stream head to NOT flush
2339 * M_PCPROTO messages.
2341 stp
->sd_read_opt
|= RFLUSHPCPROT
;
2345 if (sop
->so_flags
& SO_ERROPT
) {
2346 switch (sop
->so_erropt
& RERRMASK
) {
2348 stp
->sd_flag
&= ~STRDERRNONPERSIST
;
2350 case RERRNONPERSIST
:
2351 stp
->sd_flag
|= STRDERRNONPERSIST
;
2354 switch (sop
->so_erropt
& WERRMASK
) {
2356 stp
->sd_flag
&= ~STWRERRNONPERSIST
;
2358 case WERRNONPERSIST
:
2359 stp
->sd_flag
|= STWRERRNONPERSIST
;
2363 if (sop
->so_flags
& SO_COPYOPT
) {
2364 if (sop
->so_copyopt
& ZCVMSAFE
) {
2365 stp
->sd_copyflag
|= STZCVMSAFE
;
2366 stp
->sd_copyflag
&= ~STZCVMUNSAFE
;
2367 } else if (sop
->so_copyopt
& ZCVMUNSAFE
) {
2368 stp
->sd_copyflag
|= STZCVMUNSAFE
;
2369 stp
->sd_copyflag
&= ~STZCVMSAFE
;
2372 if (sop
->so_copyopt
& COPYCACHED
) {
2373 stp
->sd_copyflag
|= STRCOPYCACHED
;
2376 if (sop
->so_flags
& SO_WROFF
)
2377 stp
->sd_wroff
= sop
->so_wroff
;
2378 if (sop
->so_flags
& SO_TAIL
)
2379 stp
->sd_tail
= sop
->so_tail
;
2380 if (sop
->so_flags
& SO_MINPSZ
)
2381 q
->q_minpsz
= sop
->so_minpsz
;
2382 if (sop
->so_flags
& SO_MAXPSZ
)
2383 q
->q_maxpsz
= sop
->so_maxpsz
;
2384 if (sop
->so_flags
& SO_MAXBLK
)
2385 stp
->sd_maxblk
= sop
->so_maxblk
;
2386 if (sop
->so_flags
& SO_HIWAT
) {
2387 if (sop
->so_flags
& SO_BAND
) {
2388 if (strqset(q
, QHIWAT
,
2389 sop
->so_band
, sop
->so_hiwat
)) {
2390 cmn_err(CE_WARN
, "strrput: could not "
2391 "allocate qband\n");
2393 bpri
= sop
->so_band
;
2396 q
->q_hiwat
= sop
->so_hiwat
;
2399 if (sop
->so_flags
& SO_LOWAT
) {
2400 if (sop
->so_flags
& SO_BAND
) {
2401 if (strqset(q
, QLOWAT
,
2402 sop
->so_band
, sop
->so_lowat
)) {
2403 cmn_err(CE_WARN
, "strrput: could not "
2404 "allocate qband\n");
2406 bpri
= sop
->so_band
;
2409 q
->q_lowat
= sop
->so_lowat
;
2412 if (sop
->so_flags
& SO_MREADON
)
2413 stp
->sd_flag
|= SNDMREAD
;
2414 if (sop
->so_flags
& SO_MREADOFF
)
2415 stp
->sd_flag
&= ~SNDMREAD
;
2416 if (sop
->so_flags
& SO_NDELON
)
2417 stp
->sd_flag
|= OLDNDELAY
;
2418 if (sop
->so_flags
& SO_NDELOFF
)
2419 stp
->sd_flag
&= ~OLDNDELAY
;
2420 if (sop
->so_flags
& SO_ISTTY
)
2421 stp
->sd_flag
|= STRISTTY
;
2422 if (sop
->so_flags
& SO_ISNTTY
)
2423 stp
->sd_flag
&= ~STRISTTY
;
2424 if (sop
->so_flags
& SO_TOSTOP
)
2425 stp
->sd_flag
|= STRTOSTOP
;
2426 if (sop
->so_flags
& SO_TONSTOP
)
2427 stp
->sd_flag
&= ~STRTOSTOP
;
2428 if (sop
->so_flags
& SO_DELIM
)
2429 stp
->sd_flag
|= STRDELIM
;
2430 if (sop
->so_flags
& SO_NODELIM
)
2431 stp
->sd_flag
&= ~STRDELIM
;
2433 mutex_exit(&stp
->sd_lock
);
2436 /* Check backenable in case the water marks changed */
2437 qbackenable(q
, bpri
);
2441 * The following set of cases deal with situations where two stream
2442 * heads are connected to each other (twisted streams). These messages
2443 * have no meaning at the stream head.
2458 * Always NAK this condition
2460 * If there is one or more threads in the read side
2461 * rwnext we have to defer the nacking until that thread
2462 * returns (in strget).
2464 mutex_enter(&stp
->sd_lock
);
2465 if (stp
->sd_struiodnak
!= 0) {
2467 * Defer NAK to the streamhead. Queue at the end
2470 mblk_t
*mp
= stp
->sd_struionak
;
2472 while (mp
&& mp
->b_next
)
2477 stp
->sd_struionak
= bp
;
2479 mutex_exit(&stp
->sd_lock
);
2482 mutex_exit(&stp
->sd_lock
);
2484 bp
->b_datap
->db_type
= M_IOCNAK
;
2486 * Protect against the driver passing up
2487 * messages after it has done a qprocsoff.
2489 if (_OTHERQ(q
)->q_next
== NULL
)
2498 "bad message type %x received at stream head\n",
2499 bp
->b_datap
->db_type
);
2509 * Check if the stream pointed to by `stp' can be written to, and return an
2510 * error code if not. If `eiohup' is set, then return EIO if STRHUP is set.
2511 * If `sigpipeok' is set and the SW_SIGPIPE option is enabled on the stream,
2512 * then always return EPIPE and send a SIGPIPE to the invoking thread.
2515 strwriteable(struct stdata
*stp
, boolean_t eiohup
, boolean_t sigpipeok
)
2519 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
2522 * For modem support, POSIX states that on writes, EIO should
2523 * be returned if the stream has been hung up.
2525 if (eiohup
&& (stp
->sd_flag
& (STPLEX
|STRHUP
)) == STRHUP
)
2528 error
= strgeterr(stp
, STRHUP
|STPLEX
|STWRERR
, 0);
2531 if (!(stp
->sd_flag
& STPLEX
) &&
2532 (stp
->sd_wput_opt
& SW_SIGPIPE
) && sigpipeok
) {
2533 tsignal(curthread
, SIGPIPE
);
2542 * Copyin and send data down a stream.
2543 * The caller will allocate and copyin any control part that precedes the
2544 * message and pass that in as mctl.
2546 * Caller should *not* hold sd_lock.
2547 * When EWOULDBLOCK is returned the caller has to redo the canputnext
2548 * under sd_lock in order to avoid missing a backenabling wakeup.
2550 * Use iosize = -1 to not send any M_DATA. iosize = 0 sends zero-length M_DATA.
2552 * Set MSG_IGNFLOW in flags to ignore flow control for hipri messages.
2553 * For sync streams we can only ignore flow control by reverting to using
2556 * If sd_maxblk is less than *iosize this routine might return without
2557 * transferring all of *iosize. In all cases, on return *iosize will contain
2558 * the amount of data that was transferred.
2561 strput(struct stdata
*stp
, mblk_t
*mctl
, struct uio
*uiop
, ssize_t
*iosize
,
2562 int b_flag
, int pri
, int flags
)
2565 struct iovec buf
[IOV_MAX_STACK
];
2568 queue_t
*wqp
= stp
->sd_wrq
;
2570 ssize_t count
= *iosize
;
2572 ASSERT(MUTEX_NOT_HELD(&stp
->sd_lock
));
2574 if (uiop
!= NULL
&& count
>= 0)
2575 flags
|= stp
->sd_struiowrq
? STRUIO_POSTPONE
: 0;
2577 if (!(flags
& STRUIO_POSTPONE
)) {
2579 * Use regular canputnext, strmakedata, putnext sequence.
2582 if (!canputnext(wqp
) && !(flags
& MSG_IGNFLOW
)) {
2584 return (EWOULDBLOCK
);
2587 if (!(flags
& MSG_IGNFLOW
) && !bcanputnext(wqp
, pri
)) {
2589 return (EWOULDBLOCK
);
2593 if ((error
= strmakedata(iosize
, uiop
, stp
, flags
,
2597 * need to change return code to ENOMEM
2598 * so that this is not confused with
2599 * flow control, EAGAIN.
2602 if (error
== EAGAIN
)
2608 if (mctl
->b_cont
== NULL
)
2610 else if (mp
!= NULL
)
2613 } else if (mp
== NULL
)
2616 mp
->b_flag
|= b_flag
;
2617 mp
->b_band
= (uchar_t
)pri
;
2619 if (flags
& MSG_IGNFLOW
) {
2621 * XXX Hack: Don't get stuck running service
2622 * procedures. This is needed for sockfs when
2623 * sending the unbind message out of the rput
2624 * procedure - we don't want a put procedure
2625 * to run service procedures.
2629 stream_willservice(stp
);
2631 stream_runservice(stp
);
2636 * Stream supports rwnext() for the write side.
2638 if ((error
= strmakedata(iosize
, uiop
, stp
, flags
, &mp
)) != 0) {
2641 * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
2643 return (error
== EAGAIN
? ENOMEM
: error
);
2646 if (mctl
->b_cont
== NULL
)
2648 else if (mp
!= NULL
)
2651 } else if (mp
== NULL
) {
2655 mp
->b_flag
|= b_flag
;
2656 mp
->b_band
= (uchar_t
)pri
;
2658 if (uiop
->uio_iovcnt
> IOV_MAX_STACK
) {
2659 iovlen
= uiop
->uio_iovcnt
* sizeof (iovec_t
);
2660 uiod
.d_iov
= kmem_alloc(iovlen
, KM_SLEEP
);
2665 (void) uiodup(uiop
, &uiod
.d_uio
, uiod
.d_iov
, uiop
->uio_iovcnt
);
2666 uiod
.d_uio
.uio_offset
= 0;
2668 error
= rwnext(wqp
, &uiod
);
2670 uioskip(uiop
, *iosize
);
2672 kmem_free(uiod
.d_iov
, iovlen
);
2675 ASSERT(mp
== uiod
.d_mp
);
2676 if (error
== EINVAL
) {
2678 * The stream plumbing must have changed while
2679 * we were away, so just turn off rwnext()s.
2682 } else if (error
== EBUSY
|| error
== EWOULDBLOCK
) {
2684 * Couldn't enter a perimeter or took a page fault,
2685 * so fall-back to putnext().
2691 kmem_free(uiod
.d_iov
, iovlen
);
2694 /* Have to check canput before consuming data from the uio */
2696 if (!canputnext(wqp
) && !(flags
& MSG_IGNFLOW
)) {
2699 kmem_free(uiod
.d_iov
, iovlen
);
2700 return (EWOULDBLOCK
);
2703 if (!bcanputnext(wqp
, pri
) && !(flags
& MSG_IGNFLOW
)) {
2706 kmem_free(uiod
.d_iov
, iovlen
);
2707 return (EWOULDBLOCK
);
2710 ASSERT(mp
== uiod
.d_mp
);
2711 /* Copyin data from the uio */
2712 if ((error
= struioget(wqp
, mp
, &uiod
, 0)) != 0) {
2715 kmem_free(uiod
.d_iov
, iovlen
);
2718 uioskip(uiop
, *iosize
);
2719 if (flags
& MSG_IGNFLOW
) {
2721 * XXX Hack: Don't get stuck running service procedures.
2722 * This is needed for sockfs when sending the unbind message
2723 * out of the rput procedure - we don't want a put procedure
2724 * to run service procedures.
2728 stream_willservice(stp
);
2730 stream_runservice(stp
);
2733 kmem_free(uiod
.d_iov
, iovlen
);
2738 * Write attempts to break the write request into messages conforming
2739 * with the minimum and maximum packet sizes set downstream.
2741 * Write will not block if downstream queue is full and
2742 * O_NDELAY is set, otherwise it will block waiting for the queue to get room.
2744 * A write of zero bytes gets packaged into a zero length message and sent
2745 * downstream like any other message.
2747 * If buffers of the requested sizes are not available, the write will
2748 * sleep until the buffers become available.
2750 * Write (if specified) will supply a write offset in a message if it
2751 * makes sense. This can be specified by downstream modules as part of
2752 * a M_SETOPTS message. Write will not supply the write offset if it
2753 * cannot supply any data in a buffer. In other words, write will never
2754 * send down an empty packet due to a write offset.
2758 strwrite(struct vnode
*vp
, struct uio
*uiop
, cred_t
*crp
)
2760 return (strwrite_common(vp
, uiop
, crp
, 0));
2765 strwrite_common(struct vnode
*vp
, struct uio
*uiop
, cred_t
*crp
, int wflag
)
2776 ASSERT(vp
->v_stream
);
2779 mutex_enter(&stp
->sd_lock
);
2781 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
2782 mutex_exit(&stp
->sd_lock
);
2786 if (stp
->sd_flag
& (STWRERR
|STRHUP
|STPLEX
)) {
2787 error
= strwriteable(stp
, B_TRUE
, B_TRUE
);
2789 mutex_exit(&stp
->sd_lock
);
2794 mutex_exit(&stp
->sd_lock
);
2798 /* get these values from them cached in the stream head */
2799 rmin
= stp
->sd_qn_minpsz
;
2800 rmax
= stp
->sd_qn_maxpsz
;
2803 * Check the min/max packet size constraints. If min packet size
2804 * is non-zero, the write cannot be split into multiple messages
2805 * and still guarantee the size constraints.
2807 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRWRITE_IN
, "strwrite in:q %p", wqp
);
2809 ASSERT((rmax
>= 0) || (rmax
== INFPSZ
));
2814 if (uiop
->uio_resid
< rmin
) {
2815 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRWRITE_OUT
,
2816 "strwrite out:q %p out %d error %d",
2820 if ((rmax
!= INFPSZ
) && (uiop
->uio_resid
> rmax
)) {
2821 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRWRITE_OUT
,
2822 "strwrite out:q %p out %d error %d",
2829 * Do until count satisfied or error.
2831 waitflag
= WRITEWAIT
| wflag
;
2832 if (stp
->sd_flag
& OLDNDELAY
)
2833 tempmode
= uiop
->uio_fmode
& ~FNDELAY
;
2835 tempmode
= uiop
->uio_fmode
;
2838 rmax
= uiop
->uio_resid
;
2841 * Note that tempmode does not get used in strput/strmakedata
2842 * but only in strwaitq. The other routines use uio_fmode
2846 while (1) { /* breaks when uio_resid reaches zero */
2848 * Determine the size of the next message to be
2849 * packaged. May have to break write into several
2850 * messages based on max packet size.
2852 iosize
= MIN(uiop
->uio_resid
, rmax
);
2855 * Put block downstream when flow control allows it.
2857 if ((stp
->sd_flag
& STRDELIM
) && (uiop
->uio_resid
== iosize
))
2865 error
= strput(stp
, NULL
, uiop
, &iosize
, b_flag
, 0, 0);
2868 if (error
!= EWOULDBLOCK
)
2871 mutex_enter(&stp
->sd_lock
);
2873 * Check for a missed wakeup.
2874 * Needed since strput did not hold sd_lock across
2877 if (canputnext(wqp
)) {
2879 mutex_exit(&stp
->sd_lock
);
2882 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRWRITE_WAIT
,
2883 "strwrite wait:q %p wait", wqp
);
2884 if ((error
= strwaitq(stp
, waitflag
, (ssize_t
)0,
2885 tempmode
, -1, &done
)) != 0 || done
) {
2886 mutex_exit(&stp
->sd_lock
);
2887 if ((vp
->v_type
== VFIFO
) &&
2888 (uiop
->uio_fmode
& FNDELAY
) &&
2893 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRWRITE_WAKE
,
2894 "strwrite wake:q %p awakes", wqp
);
2895 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
2896 mutex_exit(&stp
->sd_lock
);
2899 mutex_exit(&stp
->sd_lock
);
2902 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRWRITE_RESID
,
2903 "strwrite resid:q %p uiop %p", wqp
, uiop
);
2904 if (uiop
->uio_resid
) {
2905 /* Recheck for errors - needed for sockets */
2906 if ((stp
->sd_wput_opt
& SW_RECHECK_ERR
) &&
2907 (stp
->sd_flag
& (STWRERR
|STRHUP
|STPLEX
))) {
2908 mutex_enter(&stp
->sd_lock
);
2909 error
= strwriteable(stp
, B_FALSE
, B_TRUE
);
2910 mutex_exit(&stp
->sd_lock
);
2920 * For historical reasons, applications expect EAGAIN when a data
2921 * mblk_t cannot be allocated, so change ENOMEM back to EAGAIN.
2923 if (error
== ENOMEM
)
2925 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRWRITE_OUT
,
2926 "strwrite out:q %p out %d error %d", wqp
, 2, error
);
2931 * Stream head write service routine.
2932 * Its job is to wake up any sleeping writers when a queue
2933 * downstream needs data (part of the flow control in putq and getq).
2934 * It also must wake anyone sleeping on a poll().
2935 * For stream head right below mux module, it must also invoke put procedure
2936 * of next downstream module.
2947 unsigned char qbf
[NBAND
]; /* band flushing backenable flags */
2949 TRACE_1(TR_FAC_STREAMS_FR
,
2950 TR_STRWSRV
, "strwsrv:q %p", q
);
2951 stp
= (struct stdata
*)q
->q_ptr
;
2952 ASSERT(qclaimed(q
));
2953 mutex_enter(&stp
->sd_lock
);
2954 ASSERT(!(stp
->sd_flag
& STPLEX
));
2956 if (stp
->sd_flag
& WSLEEP
) {
2957 stp
->sd_flag
&= ~WSLEEP
;
2958 cv_broadcast(&q
->q_wait
);
2960 mutex_exit(&stp
->sd_lock
);
2962 /* The other end of a stream pipe went away. */
2963 if ((tq
= q
->q_next
) == NULL
) {
2967 /* Find the next module forward that has a service procedure */
2972 if ((q
->q_flag
& QBACK
)) {
2973 if ((tq
->q_flag
& QFULL
)) {
2974 mutex_enter(QLOCK(tq
));
2975 if (!(tq
->q_flag
& QFULL
)) {
2976 mutex_exit(QLOCK(tq
));
2980 * The queue must have become full again. Set QWANTW
2981 * again so strwsrv will be back enabled when
2982 * the queue becomes non-full next time.
2984 tq
->q_flag
|= QWANTW
;
2985 mutex_exit(QLOCK(tq
));
2988 pollwakeup(&stp
->sd_pollist
, POLLWRNORM
);
2989 mutex_enter(&stp
->sd_lock
);
2990 if (stp
->sd_sigflags
& S_WRNORM
)
2991 strsendsig(stp
->sd_siglist
, S_WRNORM
, 0, 0);
2992 mutex_exit(&stp
->sd_lock
);
2998 bzero((caddr_t
)qbf
, NBAND
);
2999 mutex_enter(QLOCK(tq
));
3000 if ((myqbp
= q
->q_bandp
) != NULL
)
3001 for (qbp
= tq
->q_bandp
; qbp
&& myqbp
; qbp
= qbp
->qb_next
) {
3003 if ((myqbp
->qb_flag
& QB_BACK
)) {
3004 if (qbp
->qb_flag
& QB_FULL
) {
3006 * The band must have become full again.
3007 * Set QB_WANTW again so strwsrv will
3008 * be back enabled when the band becomes
3009 * non-full next time.
3011 qbp
->qb_flag
|= QB_WANTW
;
3017 myqbp
= myqbp
->qb_next
;
3020 mutex_exit(QLOCK(tq
));
3023 for (i
= tq
->q_nband
; i
; i
--) {
3025 pollwakeup(&stp
->sd_pollist
, POLLWRBAND
);
3026 mutex_enter(&stp
->sd_lock
);
3027 if (stp
->sd_sigflags
& S_WRBAND
)
3028 strsendsig(stp
->sd_siglist
, S_WRBAND
,
3030 mutex_exit(&stp
->sd_lock
);
3040 * Special case of strcopyin/strcopyout for copying
3041 * struct strioctl that can deal with both data
3048 strcopyin_strioctl(void *from
, void *to
, int flag
, int copyflag
)
3050 struct strioctl32 strioc32
;
3051 struct strioctl
*striocp
;
3053 if (copyflag
& U_TO_K
) {
3054 ASSERT((copyflag
& K_TO_K
) == 0);
3056 if ((flag
& FMODELS
) == DATAMODEL_ILP32
) {
3057 if (copyin(from
, &strioc32
, sizeof (strioc32
)))
3060 striocp
= (struct strioctl
*)to
;
3061 striocp
->ic_cmd
= strioc32
.ic_cmd
;
3062 striocp
->ic_timout
= strioc32
.ic_timout
;
3063 striocp
->ic_len
= strioc32
.ic_len
;
3064 striocp
->ic_dp
= (char *)(uintptr_t)strioc32
.ic_dp
;
3066 } else { /* NATIVE data model */
3067 if (copyin(from
, to
, sizeof (struct strioctl
))) {
3074 ASSERT(copyflag
& K_TO_K
);
3075 bcopy(from
, to
, sizeof (struct strioctl
));
3081 strcopyout_strioctl(void *from
, void *to
, int flag
, int copyflag
)
3083 struct strioctl32 strioc32
;
3084 struct strioctl
*striocp
;
3086 if (copyflag
& U_TO_K
) {
3087 ASSERT((copyflag
& K_TO_K
) == 0);
3089 if ((flag
& FMODELS
) == DATAMODEL_ILP32
) {
3090 striocp
= (struct strioctl
*)from
;
3091 strioc32
.ic_cmd
= striocp
->ic_cmd
;
3092 strioc32
.ic_timout
= striocp
->ic_timout
;
3093 strioc32
.ic_len
= striocp
->ic_len
;
3094 strioc32
.ic_dp
= (caddr32_t
)(uintptr_t)striocp
->ic_dp
;
3095 ASSERT((char *)(uintptr_t)strioc32
.ic_dp
==
3098 if (copyout(&strioc32
, to
, sizeof (strioc32
)))
3101 } else { /* NATIVE data model */
3102 if (copyout(from
, to
, sizeof (struct strioctl
))) {
3109 ASSERT(copyflag
& K_TO_K
);
3110 bcopy(from
, to
, sizeof (struct strioctl
));
3119 strcopyin_strioctl(void *from
, void *to
, int flag
, int copyflag
)
3121 return (strcopyin(from
, to
, sizeof (struct strioctl
), copyflag
));
3126 strcopyout_strioctl(void *from
, void *to
, int flag
, int copyflag
)
3128 return (strcopyout(from
, to
, sizeof (struct strioctl
), copyflag
));
3134 * Determine type of job control semantics expected by user. The
3135 * possibilities are:
3136 * JCREAD - Behaves like read() on fd; send SIGTTIN
3137 * JCWRITE - Behaves like write() on fd; send SIGTTOU if TOSTOP set
3138 * JCSETP - Sets a value in the stream; send SIGTTOU, ignore TOSTOP
3139 * JCGETP - Gets a value in the stream; no signals.
3140 * See straccess in strsubr.c for usage of these values.
3142 * This routine also returns -1 for I_STR as a special case; the
3143 * caller must call again with the real ioctl number for
3147 job_control_type(int cmd
)
3167 case TCDSET
: /* Obsolete */
3198 case LDSMAP
: /* Obsolete */
3211 case JBOOT
: /* Obsolete */
3212 case JTERM
: /* Obsolete */
3213 case JTIMOM
: /* Obsolete */
3214 case JZOMBOOT
: /* Obsolete */
3215 case JAGENT
: /* Obsolete */
3216 case JTRUN
: /* Obsolete */
3217 case JXTPROTO
: /* Obsolete */
3228 strioctl(struct vnode
*vp
, int cmd
, intptr_t arg
, int flag
, int copyflag
,
3229 cred_t
*crp
, int *rvalp
)
3233 struct strioctl strioc
;
3243 boolean_t kioctl
= B_FALSE
;
3244 uint32_t auditing
= AU_AUDITING();
3246 if (flag
& FKIOCTL
) {
3250 ASSERT(vp
->v_stream
);
3251 ASSERT(copyflag
== U_TO_K
|| copyflag
== K_TO_K
);
3254 TRACE_3(TR_FAC_STREAMS_FR
, TR_IOCTL_ENTER
,
3255 "strioctl:stp %p cmd %X arg %lX", stp
, cmd
, arg
);
3258 * If the copy is kernel to kernel, make sure that the FNATIVE
3259 * flag is set. After this it would be a serious error to have
3262 if (copyflag
== K_TO_K
)
3263 flag
= (flag
& ~FMODELS
) | FNATIVE
;
3265 ASSERT((flag
& FMODELS
) != 0);
3270 access
= job_control_type(cmd
);
3272 /* We should never see these here, should be handled by iwscn */
3273 if (cmd
== SRIOCSREDIR
|| cmd
== SRIOCISREDIR
)
3276 mutex_enter(&stp
->sd_lock
);
3277 if ((access
!= -1) && ((error
= i_straccess(stp
, access
)) != 0)) {
3278 mutex_exit(&stp
->sd_lock
);
3281 mutex_exit(&stp
->sd_lock
);
3284 * Check for sgttyb-related ioctls first, and complain as
3291 if (sgttyb_handling
>= 2 && !sgttyb_complaint
) {
3292 sgttyb_complaint
= B_TRUE
;
3294 "application used obsolete TIOC[GS]ET");
3296 if (sgttyb_handling
>= 3) {
3297 tsignal(curthread
, SIGSYS
);
3303 mutex_enter(&stp
->sd_lock
);
3315 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
3316 error
= strgeterr(stp
, STRDERR
|STPLEX
, 0);
3318 mutex_exit(&stp
->sd_lock
);
3325 if (stp
->sd_flag
& (STRDERR
|STWRERR
|STPLEX
)) {
3326 error
= strgeterr(stp
, STRDERR
|STWRERR
|STPLEX
, 0);
3328 mutex_exit(&stp
->sd_lock
);
3334 mutex_exit(&stp
->sd_lock
);
3339 * The stream head has hardcoded knowledge of a
3340 * miscellaneous collection of terminal-, keyboard- and
3341 * mouse-related ioctls, enumerated below. This hardcoded
3342 * knowledge allows the stream head to automatically
3343 * convert transparent ioctl requests made by userland
3344 * programs into I_STR ioctls which many old STREAMS
3345 * modules and drivers require.
3347 * No new ioctls should ever be added to this list.
3348 * Instead, the STREAMS module or driver should be written
3349 * to either handle transparent ioctls or require any
3350 * userland programs to use I_STR ioctls (by returning
3351 * EINVAL to any transparent ioctl requests).
3353 * More importantly, removing ioctls from this list should
3354 * be done with the utmost care, since our STREAMS modules
3355 * and drivers *count* on the stream head performing this
3356 * conversion, and thus may panic while processing
3357 * transparent ioctl request for one of these ioctls (keep
3358 * in mind that third party modules and drivers may have
3359 * similar problems).
3361 if (((cmd
& IOCTYPE
) == LDIOC
) ||
3362 ((cmd
& IOCTYPE
) == tIOC
) ||
3363 ((cmd
& IOCTYPE
) == TIOC
) ||
3364 ((cmd
& IOCTYPE
) == KIOC
) ||
3365 ((cmd
& IOCTYPE
) == MSIOC
) ||
3366 ((cmd
& IOCTYPE
) == VUIOC
)) {
3368 * The ioctl is a tty ioctl - set up strioc buffer
3369 * and call strdoioctl() to do the work.
3371 if (stp
->sd_flag
& STRHUP
)
3373 strioc
.ic_cmd
= cmd
;
3374 strioc
.ic_timout
= INFTIM
;
3383 int native_arg
= (int)arg
;
3384 strioc
.ic_len
= sizeof (int);
3385 strioc
.ic_dp
= (char *)&native_arg
;
3386 return (strdoioctl(stp
, &strioc
, flag
,
3387 K_TO_K
, crp
, rvalp
));
3393 strioc
.ic_len
= sizeof (struct termio
);
3394 strioc
.ic_dp
= (char *)arg
;
3395 return (strdoioctl(stp
, &strioc
, flag
,
3396 copyflag
, crp
, rvalp
));
3401 strioc
.ic_len
= sizeof (struct termios
);
3402 strioc
.ic_dp
= (char *)arg
;
3403 return (strdoioctl(stp
, &strioc
, flag
,
3404 copyflag
, crp
, rvalp
));
3407 strioc
.ic_len
= sizeof (struct termcb
);
3408 strioc
.ic_dp
= (char *)arg
;
3409 return (strdoioctl(stp
, &strioc
, flag
,
3410 copyflag
, crp
, rvalp
));
3413 strioc
.ic_len
= sizeof (struct sgttyb
);
3414 strioc
.ic_dp
= (char *)arg
;
3415 return (strdoioctl(stp
, &strioc
, flag
,
3416 copyflag
, crp
, rvalp
));
3419 if ((flag
& FREAD
) == 0 &&
3420 secpolicy_sti(crp
) != 0) {
3423 mutex_enter(&stp
->sd_lock
);
3424 mutex_enter(&curproc
->p_splock
);
3425 if (stp
->sd_sidp
!= curproc
->p_sessp
->s_sidp
&&
3426 secpolicy_sti(crp
) != 0) {
3427 mutex_exit(&curproc
->p_splock
);
3428 mutex_exit(&stp
->sd_lock
);
3431 mutex_exit(&curproc
->p_splock
);
3432 mutex_exit(&stp
->sd_lock
);
3434 strioc
.ic_len
= sizeof (char);
3435 strioc
.ic_dp
= (char *)arg
;
3436 return (strdoioctl(stp
, &strioc
, flag
,
3437 copyflag
, crp
, rvalp
));
3440 strioc
.ic_len
= sizeof (struct winsize
);
3441 strioc
.ic_dp
= (char *)arg
;
3442 return (strdoioctl(stp
, &strioc
, flag
,
3443 copyflag
, crp
, rvalp
));
3446 strioc
.ic_len
= sizeof (struct ttysize
);
3447 strioc
.ic_dp
= (char *)arg
;
3448 return (strdoioctl(stp
, &strioc
, flag
,
3449 copyflag
, crp
, rvalp
));
3462 strioc
.ic_len
= sizeof (int);
3463 strioc
.ic_dp
= (char *)arg
;
3464 return (strdoioctl(stp
, &strioc
, flag
,
3465 copyflag
, crp
, rvalp
));
3469 strioc
.ic_len
= sizeof (struct kiockey
);
3470 strioc
.ic_dp
= (char *)arg
;
3471 return (strdoioctl(stp
, &strioc
, flag
,
3472 copyflag
, crp
, rvalp
));
3476 strioc
.ic_len
= sizeof (struct kiockeymap
);
3477 strioc
.ic_dp
= (char *)arg
;
3478 return (strdoioctl(stp
, &strioc
, flag
,
3479 copyflag
, crp
, rvalp
));
3482 /* arg is a pointer to char */
3483 strioc
.ic_len
= sizeof (char);
3484 strioc
.ic_dp
= (char *)arg
;
3485 return (strdoioctl(stp
, &strioc
, flag
,
3486 copyflag
, crp
, rvalp
));
3489 strioc
.ic_len
= sizeof (Ms_parms
);
3490 strioc
.ic_dp
= (char *)arg
;
3491 return (strdoioctl(stp
, &strioc
, flag
,
3492 copyflag
, crp
, rvalp
));
3496 strioc
.ic_len
= sizeof (struct vuid_addr_probe
);
3497 strioc
.ic_dp
= (char *)arg
;
3498 return (strdoioctl(stp
, &strioc
, flag
,
3499 copyflag
, crp
, rvalp
));
3502 * These M_IOCTL's don't require any data to be sent
3503 * downstream, and the driver will allocate and link
3504 * on its own mblk_t upon M_IOCACK -- thus we set
3505 * ic_len to zero and set ic_dp to arg so we know
3506 * where to copyout to later.
3512 case KIOCGTRANSABLE
:
3530 strioc
.ic_dp
= (char *)arg
;
3531 return (strdoioctl(stp
, &strioc
, flag
,
3532 copyflag
, crp
, rvalp
));
3537 * Unknown cmd - send it down as a transparent ioctl.
3539 strioc
.ic_cmd
= cmd
;
3540 strioc
.ic_timout
= INFTIM
;
3541 strioc
.ic_len
= TRANSPARENT
;
3542 strioc
.ic_dp
= (char *)&arg
;
3544 return (strdoioctl(stp
, &strioc
, flag
, copyflag
, crp
, rvalp
));
3548 * Stream ioctl. Read in an strioctl buffer from the user
3549 * along with any data specified and send it downstream.
3550 * Strdoioctl will wait allow only one ioctl message at
3551 * a time, and waits for the acknowledgement.
3554 if (stp
->sd_flag
& STRHUP
)
3557 error
= strcopyin_strioctl((void *)arg
, &strioc
, flag
,
3562 if ((strioc
.ic_len
< 0) || (strioc
.ic_timout
< -1))
3565 access
= job_control_type(strioc
.ic_cmd
);
3566 mutex_enter(&stp
->sd_lock
);
3567 if ((access
!= -1) &&
3568 ((error
= i_straccess(stp
, access
)) != 0)) {
3569 mutex_exit(&stp
->sd_lock
);
3572 mutex_exit(&stp
->sd_lock
);
3575 * The I_STR facility provides a trap door for malicious
3576 * code to send down bogus streamio(7I) ioctl commands to
3577 * unsuspecting STREAMS modules and drivers which expect to
3578 * only get these messages from the stream head.
3579 * Explicitly prohibit any streamio ioctls which can be
3580 * passed downstream by the stream head. Note that we do
3581 * not block all streamio ioctls because the ioctl
3582 * numberspace is not well managed and thus it's possible
3583 * that a module or driver's ioctl numbers may accidentally
3584 * collide with them.
3586 switch (strioc
.ic_cmd
) {
3591 case _I_GETPEERCRED
:
3596 error
= strdoioctl(stp
, &strioc
, flag
, copyflag
, crp
, rvalp
);
3598 error
= strcopyout_strioctl(&strioc
, (void *)arg
,
3605 * Like I_STR, but without using M_IOC* messages and without
3606 * copyins/copyouts beyond the passed-in argument.
3608 if (stp
->sd_flag
& STRHUP
)
3611 if ((scp
= kmem_alloc(sizeof (strcmd_t
), KM_NOSLEEP
)) == NULL
)
3614 if (copyin((void *)arg
, scp
, sizeof (strcmd_t
))) {
3615 kmem_free(scp
, sizeof (strcmd_t
));
3619 access
= job_control_type(scp
->sc_cmd
);
3620 mutex_enter(&stp
->sd_lock
);
3621 if (access
!= -1 && (error
= i_straccess(stp
, access
)) != 0) {
3622 mutex_exit(&stp
->sd_lock
);
3623 kmem_free(scp
, sizeof (strcmd_t
));
3626 mutex_exit(&stp
->sd_lock
);
3629 if ((error
= strdocmd(stp
, scp
, crp
)) == 0) {
3630 if (copyout(scp
, (void *)arg
, sizeof (strcmd_t
)))
3633 kmem_free(scp
, sizeof (strcmd_t
));
3638 * Return number of bytes of data in first message
3639 * in queue in "arg" and return the number of messages
3640 * in queue in return value.
3647 mutex_enter(QLOCK(rdq
));
3649 size
= msgdsize(rdq
->q_first
);
3650 for (mp
= rdq
->q_first
; mp
!= NULL
; mp
= mp
->b_next
)
3653 mutex_exit(QLOCK(rdq
));
3654 if (stp
->sd_struiordq
) {
3657 infod
.d_cmd
= INFOD_COUNT
;
3660 infod
.d_cmd
|= INFOD_FIRSTBYTES
;
3664 (void) infonext(rdq
, &infod
);
3665 count
+= infod
.d_count
;
3666 if (infod
.d_res
& INFOD_FIRSTBYTES
)
3667 size
= infod
.d_bytes
;
3671 * Drop down from size_t to the "int" required by the
3672 * interface. Cap at INT_MAX.
3674 retval
= MIN(size
, INT_MAX
);
3675 error
= strcopyout(&retval
, (void *)arg
, sizeof (retval
),
3684 * Return number of bytes of data in all data messages
3685 * in queue in "arg".
3691 mutex_enter(QLOCK(rdq
));
3692 for (mp
= rdq
->q_first
; mp
!= NULL
; mp
= mp
->b_next
)
3693 size
+= msgdsize(mp
);
3694 mutex_exit(QLOCK(rdq
));
3696 if (stp
->sd_struiordq
) {
3699 infod
.d_cmd
= INFOD_BYTES
;
3702 (void) infonext(rdq
, &infod
);
3703 size
+= infod
.d_bytes
;
3707 * Drop down from size_t to the "int" required by the
3708 * interface. Cap at INT_MAX.
3710 retval
= MIN(size
, INT_MAX
);
3711 error
= strcopyout(&retval
, (void *)arg
, sizeof (retval
),
3719 * FIORDCHK does not use arg value (like FIONREAD),
3720 * instead a count is returned. I_NREAD value may
3721 * not be accurate but safe. The real thing to do is
3722 * to add the msgdsizes of all data messages until
3723 * a non-data message.
3728 mutex_enter(QLOCK(rdq
));
3729 for (mp
= rdq
->q_first
; mp
!= NULL
; mp
= mp
->b_next
)
3730 size
+= msgdsize(mp
);
3731 mutex_exit(QLOCK(rdq
));
3733 if (stp
->sd_struiordq
) {
3736 infod
.d_cmd
= INFOD_BYTES
;
3739 (void) infonext(rdq
, &infod
);
3740 size
+= infod
.d_bytes
;
3744 * Since ioctl returns an int, and memory sizes under
3745 * LP64 may not fit, we return INT_MAX if the count was
3748 *rvalp
= MIN(size
, INT_MAX
);
3757 char mname
[FMNAMESZ
+ 1];
3760 error
= (copyflag
& U_TO_K
? copyinstr
: copystr
)((void *)arg
,
3761 mname
, FMNAMESZ
+ 1, NULL
);
3763 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
3766 * Return EINVAL if we're handed a bogus module name.
3768 if (fmodsw_find(mname
, FMODSW_LOAD
) == NULL
) {
3769 TRACE_0(TR_FAC_STREAMS_FR
,
3770 TR_I_CANT_FIND
, "couldn't I_FIND");
3776 /* Look downstream to see if module is there. */
3777 claimstr(stp
->sd_wrq
);
3778 for (q
= stp
->sd_wrq
->q_next
; q
; q
= q
->q_next
) {
3779 if (q
->q_flag
& QREADR
) {
3783 if (strcmp(mname
, Q2NAME(q
)) == 0)
3786 releasestr(stp
->sd_wrq
);
3788 *rvalp
= (q
? 1 : 0);
3793 case __I_PUSH_NOCTTY
:
3796 * For the case __I_PUSH_NOCTTY push a module but
3797 * do not allocate controlling tty. See bugid 4025044
3801 char mname
[FMNAMESZ
+ 1];
3805 if (stp
->sd_flag
& STRHUP
)
3809 * Get module name and look up in fmodsw.
3811 error
= (copyflag
& U_TO_K
? copyinstr
: copystr
)((void *)arg
,
3812 mname
, FMNAMESZ
+ 1, NULL
);
3814 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
3816 if ((fp
= fmodsw_find(mname
, FMODSW_HOLD
| FMODSW_LOAD
)) ==
3820 TRACE_2(TR_FAC_STREAMS_FR
, TR_I_PUSH
,
3821 "I_PUSH:fp %p stp %p", fp
, stp
);
3824 * If the module is flagged as single-instance, then check
3825 * to see if the module is already pushed. If it is, return
3826 * as if the push was successful.
3828 if (fp
->f_qflag
& _QSINGLE_INSTANCE
) {
3831 claimstr(stp
->sd_wrq
);
3832 for (q
= stp
->sd_wrq
->q_next
; q
; q
= q
->q_next
) {
3833 if (q
->q_flag
& QREADR
) {
3837 if (strcmp(mname
, Q2NAME(q
)) == 0)
3840 releasestr(stp
->sd_wrq
);
3847 if (error
= strstartplumb(stp
, flag
, cmd
)) {
3853 * See if any more modules can be pushed on this stream.
3854 * Note that this check must be done after strstartplumb()
3855 * since otherwise multiple threads issuing I_PUSHes on
3856 * the same stream will be able to exceed nstrpush.
3858 mutex_enter(&stp
->sd_lock
);
3859 if (stp
->sd_pushcnt
>= nstrpush
) {
3862 mutex_exit(&stp
->sd_lock
);
3865 mutex_exit(&stp
->sd_lock
);
3868 * Push new module and call its open routine
3869 * via qattach(). Modules don't change device
3870 * numbers, so just ignore dummydev here.
3872 dummydev
= vp
->v_rdev
;
3873 if ((error
= qattach(rdq
, &dummydev
, 0, crp
, fp
,
3875 if (vp
->v_type
== VCHR
&& /* sorry, no pipes allowed */
3876 (cmd
== I_PUSH
) && (stp
->sd_flag
& STRISTTY
)) {
3878 * try to allocate it as a controlling terminal
3880 (void) strctty(stp
);
3884 mutex_enter(&stp
->sd_lock
);
3887 * As a performance concern we are caching the values of
3888 * q_minpsz and q_maxpsz of the module below the stream
3889 * head in the stream head.
3891 mutex_enter(QLOCK(stp
->sd_wrq
->q_next
));
3892 rmin
= stp
->sd_wrq
->q_next
->q_minpsz
;
3893 rmax
= stp
->sd_wrq
->q_next
->q_maxpsz
;
3894 mutex_exit(QLOCK(stp
->sd_wrq
->q_next
));
3896 /* Do this processing here as a performance concern */
3897 if (strmsgsz
!= 0) {
3901 if (vp
->v_type
== VFIFO
)
3902 rmax
= MIN(PIPE_BUF
, rmax
);
3903 else rmax
= MIN(strmsgsz
, rmax
);
3907 mutex_enter(QLOCK(wrq
));
3908 stp
->sd_qn_minpsz
= rmin
;
3909 stp
->sd_qn_maxpsz
= rmax
;
3910 mutex_exit(QLOCK(wrq
));
3913 mutex_exit(&stp
->sd_lock
);
3921 if (stp
->sd_flag
& STRHUP
)
3923 if (!wrq
->q_next
) /* for broken pipes */
3926 if (error
= strstartplumb(stp
, flag
, cmd
))
3930 * If there is an anchor on this stream and popping
3931 * the current module would attempt to pop through the
3932 * anchor, then disallow the pop unless we have sufficient
3933 * privileges; take the cheapest (non-locking) check
3936 if (secpolicy_ip_config(crp
, B_TRUE
) != 0 ||
3937 (stp
->sd_anchorzone
!= crgetzoneid(crp
))) {
3938 mutex_enter(&stp
->sd_lock
);
3940 * Anchors only apply if there's at least one
3941 * module on the stream (sd_pushcnt > 0).
3943 if (stp
->sd_pushcnt
> 0 &&
3944 stp
->sd_pushcnt
== stp
->sd_anchor
&&
3945 stp
->sd_vnode
->v_type
!= VFIFO
) {
3947 mutex_exit(&stp
->sd_lock
);
3948 if (stp
->sd_anchorzone
!= crgetzoneid(crp
))
3950 /* Audit and report error */
3951 return (secpolicy_ip_config(crp
, B_FALSE
));
3953 mutex_exit(&stp
->sd_lock
);
3957 TRACE_2(TR_FAC_STREAMS_FR
, TR_I_POP
,
3958 "I_POP:%p from %p", q
, stp
);
3959 if (q
->q_next
== NULL
|| (q
->q_flag
& (QREADR
|QISDRV
))) {
3962 qdetach(_RD(q
), 1, flag
, crp
, B_FALSE
);
3965 mutex_enter(&stp
->sd_lock
);
3968 * As a performance concern we are caching the values of
3969 * q_minpsz and q_maxpsz of the module below the stream
3970 * head in the stream head.
3972 mutex_enter(QLOCK(wrq
->q_next
));
3973 rmin
= wrq
->q_next
->q_minpsz
;
3974 rmax
= wrq
->q_next
->q_maxpsz
;
3975 mutex_exit(QLOCK(wrq
->q_next
));
3977 /* Do this processing here as a performance concern */
3978 if (strmsgsz
!= 0) {
3982 if (vp
->v_type
== VFIFO
)
3983 rmax
= MIN(PIPE_BUF
, rmax
);
3984 else rmax
= MIN(strmsgsz
, rmax
);
3988 mutex_enter(QLOCK(wrq
));
3989 stp
->sd_qn_minpsz
= rmin
;
3990 stp
->sd_qn_maxpsz
= rmax
;
3991 mutex_exit(QLOCK(wrq
));
3993 /* If we popped through the anchor, then reset the anchor. */
3994 if (stp
->sd_pushcnt
< stp
->sd_anchor
) {
3996 stp
->sd_anchorzone
= 0;
3999 mutex_exit(&stp
->sd_lock
);
4006 * Create a fd for a I_PLINK'ed lower stream with a given
4007 * muxid. With the fd, application can send down ioctls,
4008 * like I_LIST, to the previously I_PLINK'ed stream. Note
4009 * that after getting the fd, the application has to do an
4010 * I_PUNLINK on the muxid before it can do any operation
4011 * on the lower stream. This is required by spec1170.
4013 * The fd used to do this ioctl should point to the same
4014 * controlling device used to do the I_PLINK. If it uses
4015 * a different stream or an invalid muxid, I_MUXID2FD will
4016 * fail. The error code is set to EINVAL.
4018 * The intended use of this interface is the following.
4019 * An application I_PLINK'ed a stream and exits. The fd
4020 * to the lower stream is gone. Another application
4021 * wants to get a fd to the lower stream, it uses I_MUXID2FD.
4023 int muxid
= (int)arg
;
4031 * Do not allow the wildcard muxid. This ioctl is not
4032 * intended to find arbitrary link.
4038 ns
= netstack_find_by_cred(crp
);
4040 ss
= ns
->netstack_str
;
4043 mutex_enter(&muxifier
);
4044 linkp
= findlinks(vp
->v_stream
, muxid
, LINKPERSIST
, ss
);
4045 if (linkp
== NULL
) {
4046 mutex_exit(&muxifier
);
4047 netstack_rele(ss
->ss_netstack
);
4051 if ((fd
= ufalloc(0)) == -1) {
4052 mutex_exit(&muxifier
);
4053 netstack_rele(ss
->ss_netstack
);
4056 fp
= linkp
->li_fpdown
;
4057 mutex_enter(&fp
->f_tlock
);
4059 mutex_exit(&fp
->f_tlock
);
4060 mutex_exit(&muxifier
);
4063 netstack_rele(ss
->ss_netstack
);
4070 * To insert a module to a given position in a stream.
4071 * In the first release, only allow privileged user
4072 * to use this ioctl. Furthermore, the insert is only allowed
4073 * below an anchor if the zoneid is the same as the zoneid
4074 * which created the anchor.
4076 * Note that we do not plan to support this ioctl
4077 * on pipes in the first release. We want to learn more
4078 * about the implications of these ioctls before extending
4079 * their support. And we do not think these features are
4080 * valuable for pipes.
4082 STRUCT_DECL(strmodconf
, strmodinsert
);
4083 char mod_name
[FMNAMESZ
+ 1];
4088 boolean_t is_insert
;
4090 STRUCT_INIT(strmodinsert
, flag
);
4091 if (stp
->sd_flag
& STRHUP
)
4095 if ((error
= secpolicy_net_config(crp
, B_FALSE
)) != 0)
4097 if (stp
->sd_anchor
!= 0 &&
4098 stp
->sd_anchorzone
!= crgetzoneid(crp
))
4101 error
= strcopyin((void *)arg
, STRUCT_BUF(strmodinsert
),
4102 STRUCT_SIZE(strmodinsert
), copyflag
);
4107 * Get module name and look up in fmodsw.
4109 error
= (copyflag
& U_TO_K
? copyinstr
:
4110 copystr
)(STRUCT_FGETP(strmodinsert
, mod_name
),
4111 mod_name
, FMNAMESZ
+ 1, NULL
);
4113 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
4115 if ((fp
= fmodsw_find(mod_name
, FMODSW_HOLD
| FMODSW_LOAD
)) ==
4119 if (error
= strstartplumb(stp
, flag
, cmd
)) {
4125 * Is this _I_INSERT just like an I_PUSH? We need to know
4126 * this because we do some optimizations if this is a
4127 * module being pushed.
4129 pos
= STRUCT_FGET(strmodinsert
, pos
);
4130 is_insert
= (pos
!= 0);
4133 * Make sure pos is valid. Even though it is not an I_PUSH,
4134 * we impose the same limit on the number of modules in a
4137 mutex_enter(&stp
->sd_lock
);
4138 if (stp
->sd_pushcnt
>= nstrpush
|| pos
< 0 ||
4139 pos
> stp
->sd_pushcnt
) {
4142 mutex_exit(&stp
->sd_lock
);
4145 if (stp
->sd_anchor
!= 0) {
4147 * Is this insert below the anchor?
4148 * Pushcnt hasn't been increased yet hence
4149 * we test for greater than here, and greater or
4150 * equal after qattach.
4152 if (pos
> (stp
->sd_pushcnt
- stp
->sd_anchor
) &&
4153 stp
->sd_anchorzone
!= crgetzoneid(crp
)) {
4156 mutex_exit(&stp
->sd_lock
);
4161 mutex_exit(&stp
->sd_lock
);
4164 * First find the correct position this module to
4165 * be inserted. We don't need to call claimstr()
4166 * as the stream should not be changing at this point.
4168 * Insert new module and call its open routine
4169 * via qattach(). Modules don't change device
4170 * numbers, so just ignore dummydev here.
4172 for (tmp_wrq
= stp
->sd_wrq
; pos
> 0;
4173 tmp_wrq
= tmp_wrq
->q_next
, pos
--) {
4174 ASSERT(SAMESTR(tmp_wrq
));
4176 dummydev
= vp
->v_rdev
;
4177 if ((error
= qattach(_RD(tmp_wrq
), &dummydev
, 0, crp
,
4178 fp
, is_insert
)) != 0) {
4179 mutex_enter(&stp
->sd_lock
);
4181 mutex_exit(&stp
->sd_lock
);
4185 mutex_enter(&stp
->sd_lock
);
4188 * As a performance concern we are caching the values of
4189 * q_minpsz and q_maxpsz of the module below the stream
4190 * head in the stream head.
4193 mutex_enter(QLOCK(stp
->sd_wrq
->q_next
));
4194 rmin
= stp
->sd_wrq
->q_next
->q_minpsz
;
4195 rmax
= stp
->sd_wrq
->q_next
->q_maxpsz
;
4196 mutex_exit(QLOCK(stp
->sd_wrq
->q_next
));
4198 /* Do this processing here as a performance concern */
4199 if (strmsgsz
!= 0) {
4200 if (rmax
== INFPSZ
) {
4203 rmax
= MIN(strmsgsz
, rmax
);
4207 mutex_enter(QLOCK(wrq
));
4208 stp
->sd_qn_minpsz
= rmin
;
4209 stp
->sd_qn_maxpsz
= rmax
;
4210 mutex_exit(QLOCK(wrq
));
4214 * Need to update the anchor value if this module is
4215 * inserted below the anchor point.
4217 if (stp
->sd_anchor
!= 0) {
4218 pos
= STRUCT_FGET(strmodinsert
, pos
);
4219 if (pos
>= (stp
->sd_pushcnt
- stp
->sd_anchor
))
4224 mutex_exit(&stp
->sd_lock
);
4231 * To remove a module with a given name in a stream. The
4232 * caller of this ioctl needs to provide both the name and
4233 * the position of the module to be removed. This eliminates
4234 * the ambiguity of removal if a module is inserted/pushed
4235 * multiple times in a stream. In the first release, only
4236 * allow privileged user to use this ioctl.
4237 * Furthermore, the remove is only allowed
4238 * below an anchor if the zoneid is the same as the zoneid
4239 * which created the anchor.
4241 * Note that we do not plan to support this ioctl
4242 * on pipes in the first release. We want to learn more
4243 * about the implications of these ioctls before extending
4244 * their support. And we do not think these features are
4245 * valuable for pipes.
4247 * Also note that _I_REMOVE cannot be used to remove a
4248 * driver or the stream head.
4250 STRUCT_DECL(strmodconf
, strmodremove
);
4253 char mod_name
[FMNAMESZ
+ 1];
4254 boolean_t is_remove
;
4256 STRUCT_INIT(strmodremove
, flag
);
4257 if (stp
->sd_flag
& STRHUP
)
4261 if ((error
= secpolicy_net_config(crp
, B_FALSE
)) != 0)
4263 if (stp
->sd_anchor
!= 0 &&
4264 stp
->sd_anchorzone
!= crgetzoneid(crp
))
4267 error
= strcopyin((void *)arg
, STRUCT_BUF(strmodremove
),
4268 STRUCT_SIZE(strmodremove
), copyflag
);
4272 error
= (copyflag
& U_TO_K
? copyinstr
:
4273 copystr
)(STRUCT_FGETP(strmodremove
, mod_name
),
4274 mod_name
, FMNAMESZ
+ 1, NULL
);
4276 return ((error
== ENAMETOOLONG
) ? EINVAL
: EFAULT
);
4278 if ((error
= strstartplumb(stp
, flag
, cmd
)) != 0)
4282 * Match the name of given module to the name of module at
4283 * the given position.
4285 pos
= STRUCT_FGET(strmodremove
, pos
);
4287 is_remove
= (pos
!= 0);
4288 for (q
= stp
->sd_wrq
->q_next
; SAMESTR(q
) && pos
> 0;
4289 q
= q
->q_next
, pos
--)
4291 if (pos
> 0 || !SAMESTR(q
) ||
4292 strcmp(Q2NAME(q
), mod_name
) != 0) {
4293 mutex_enter(&stp
->sd_lock
);
4295 mutex_exit(&stp
->sd_lock
);
4300 * If the position is at or below an anchor, then the zoneid
4301 * must match the zoneid that created the anchor.
4303 if (stp
->sd_anchor
!= 0) {
4304 pos
= STRUCT_FGET(strmodremove
, pos
);
4305 if (pos
>= (stp
->sd_pushcnt
- stp
->sd_anchor
) &&
4306 stp
->sd_anchorzone
!= crgetzoneid(crp
)) {
4307 mutex_enter(&stp
->sd_lock
);
4309 mutex_exit(&stp
->sd_lock
);
4315 ASSERT(!(q
->q_flag
& QREADR
));
4316 qdetach(_RD(q
), 1, flag
, crp
, is_remove
);
4318 mutex_enter(&stp
->sd_lock
);
4321 * As a performance concern we are caching the values of
4322 * q_minpsz and q_maxpsz of the module below the stream
4323 * head in the stream head.
4326 mutex_enter(QLOCK(wrq
->q_next
));
4327 rmin
= wrq
->q_next
->q_minpsz
;
4328 rmax
= wrq
->q_next
->q_maxpsz
;
4329 mutex_exit(QLOCK(wrq
->q_next
));
4331 /* Do this processing here as a performance concern */
4332 if (strmsgsz
!= 0) {
4336 if (vp
->v_type
== VFIFO
)
4337 rmax
= MIN(PIPE_BUF
, rmax
);
4338 else rmax
= MIN(strmsgsz
, rmax
);
4342 mutex_enter(QLOCK(wrq
));
4343 stp
->sd_qn_minpsz
= rmin
;
4344 stp
->sd_qn_maxpsz
= rmax
;
4345 mutex_exit(QLOCK(wrq
));
4349 * Need to update the anchor value if this module is removed
4350 * at or below the anchor point. If the removed module is at
4351 * the anchor point, remove the anchor for this stream if
4352 * there is no module above the anchor point. Otherwise, if
4353 * the removed module is below the anchor point, decrement the
4354 * anchor point by 1.
4356 if (stp
->sd_anchor
!= 0) {
4357 pos
= STRUCT_FGET(strmodremove
, pos
);
4358 if (pos
== stp
->sd_pushcnt
- stp
->sd_anchor
+ 1)
4360 else if (pos
> (stp
->sd_pushcnt
- stp
->sd_anchor
+ 1))
4365 mutex_exit(&stp
->sd_lock
);
4371 * Set the anchor position on the stream to reside at
4372 * the top module (in other words, the top module
4373 * cannot be popped). Anchors with a FIFO make no
4374 * obvious sense, so they're not allowed.
4376 mutex_enter(&stp
->sd_lock
);
4378 if (stp
->sd_vnode
->v_type
== VFIFO
) {
4379 mutex_exit(&stp
->sd_lock
);
4382 /* Only allow the same zoneid to update the anchor */
4383 if (stp
->sd_anchor
!= 0 &&
4384 stp
->sd_anchorzone
!= crgetzoneid(crp
)) {
4385 mutex_exit(&stp
->sd_lock
);
4388 stp
->sd_anchor
= stp
->sd_pushcnt
;
4389 stp
->sd_anchorzone
= crgetzoneid(crp
);
4390 mutex_exit(&stp
->sd_lock
);
4395 * Get name of first module downstream.
4396 * If no module, return an error.
4399 if (_SAMESTR(wrq
) && wrq
->q_next
->q_next
!= NULL
) {
4400 char *name
= Q2NAME(wrq
->q_next
);
4402 error
= strcopyout(name
, (void *)arg
, strlen(name
) + 1,
4413 * Link a multiplexor.
4415 return (mlink(vp
, cmd
, (int)arg
, crp
, rvalp
, 0));
4419 * Link a multiplexor: Call must originate from kernel.
4422 return (ldi_mlink_lh(vp
, cmd
, arg
, crp
, rvalp
));
4428 * Unlink a multiplexor.
4429 * If arg is -1, unlink all links for which this is the
4430 * controlling stream. Otherwise, arg is an index number
4431 * for a link to be removed.
4434 struct linkinfo
*linkp
;
4435 int native_arg
= (int)arg
;
4440 TRACE_1(TR_FAC_STREAMS_FR
,
4441 TR_I_UNLINK
, "I_UNLINK/I_PUNLINK:%p", stp
);
4442 if (vp
->v_type
== VFIFO
) {
4445 if (cmd
== I_UNLINK
)
4447 else /* I_PUNLINK */
4449 if (native_arg
== 0) {
4452 ns
= netstack_find_by_cred(crp
);
4454 ss
= ns
->netstack_str
;
4457 if (native_arg
== MUXID_ALL
)
4458 error
= munlinkall(stp
, type
, crp
, rvalp
, ss
);
4460 mutex_enter(&muxifier
);
4461 if (!(linkp
= findlinks(stp
, (int)arg
, type
, ss
))) {
4462 /* invalid user supplied index number */
4463 mutex_exit(&muxifier
);
4464 netstack_rele(ss
->ss_netstack
);
4467 /* munlink drops the muxifier lock */
4468 error
= munlink(stp
, linkp
, type
, crp
, rvalp
, ss
);
4470 netstack_rele(ss
->ss_netstack
);
4476 * send a flush message downstream
4477 * flush message can indicate
4478 * FLUSHR - flush read queue
4479 * FLUSHW - flush write queue
4480 * FLUSHRW - flush read/write queue
4482 if (stp
->sd_flag
& STRHUP
)
4488 if (putnextctl1(stp
->sd_wrq
, M_FLUSH
, (int)arg
)) {
4491 if (error
= strwaitbuf(1, BPRI_HI
)) {
4497 * Send down an unsupported ioctl and wait for the nack
4498 * in order to allow the M_FLUSH to propagate back
4499 * up to the stream head.
4500 * Replaces if (qready()) runqueues();
4502 strioc
.ic_cmd
= -1; /* The unsupported ioctl */
4503 strioc
.ic_timout
= 0;
4505 strioc
.ic_dp
= NULL
;
4506 (void) strdoioctl(stp
, &strioc
, flag
, K_TO_K
, crp
, rvalp
);
4512 struct bandinfo binfo
;
4514 error
= strcopyin((void *)arg
, &binfo
, sizeof (binfo
),
4518 if (stp
->sd_flag
& STRHUP
)
4520 if (binfo
.bi_flag
& ~FLUSHRW
)
4522 while (!(mp
= allocb(2, BPRI_HI
))) {
4523 if (error
= strwaitbuf(2, BPRI_HI
))
4526 mp
->b_datap
->db_type
= M_FLUSH
;
4527 *mp
->b_wptr
++ = binfo
.bi_flag
| FLUSHBAND
;
4528 *mp
->b_wptr
++ = binfo
.bi_pri
;
4529 putnext(stp
->sd_wrq
, mp
);
4531 * Send down an unsupported ioctl and wait for the nack
4532 * in order to allow the M_FLUSH to propagate back
4533 * up to the stream head.
4534 * Replaces if (qready()) runqueues();
4536 strioc
.ic_cmd
= -1; /* The unsupported ioctl */
4537 strioc
.ic_timout
= 0;
4539 strioc
.ic_dp
= NULL
;
4540 (void) strdoioctl(stp
, &strioc
, flag
, K_TO_K
, crp
, rvalp
);
4549 * RNORM - default stream mode
4550 * RMSGN - message no discard
4551 * RMSGD - message discard
4552 * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
4553 * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
4554 * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
4556 if (arg
& ~(RMODEMASK
| RPROTMASK
))
4559 if ((arg
& (RMSGD
|RMSGN
)) == (RMSGD
|RMSGN
))
4562 mutex_enter(&stp
->sd_lock
);
4563 switch (arg
& RMODEMASK
) {
4565 stp
->sd_read_opt
&= ~(RD_MSGDIS
| RD_MSGNODIS
);
4568 stp
->sd_read_opt
= (stp
->sd_read_opt
& ~RD_MSGNODIS
) |
4572 stp
->sd_read_opt
= (stp
->sd_read_opt
& ~RD_MSGDIS
) |
4577 switch (arg
& RPROTMASK
) {
4579 stp
->sd_read_opt
&= ~(RD_PROTDAT
| RD_PROTDIS
);
4583 stp
->sd_read_opt
= ((stp
->sd_read_opt
& ~RD_PROTDIS
) |
4588 stp
->sd_read_opt
= ((stp
->sd_read_opt
& ~RD_PROTDAT
) |
4592 mutex_exit(&stp
->sd_lock
);
4597 * Get read option and return the value
4598 * to spot pointed to by arg
4603 rdopt
= ((stp
->sd_read_opt
& RD_MSGDIS
) ? RMSGD
:
4604 ((stp
->sd_read_opt
& RD_MSGNODIS
) ? RMSGN
: RNORM
));
4605 rdopt
|= ((stp
->sd_read_opt
& RD_PROTDAT
) ? RPROTDAT
:
4606 ((stp
->sd_read_opt
& RD_PROTDIS
) ? RPROTDIS
: RPROTNORM
));
4608 return (strcopyout(&rdopt
, (void *)arg
, sizeof (int),
4616 * RERRNORM - persistent read errors
4617 * RERRNONPERSIST - non-persistent read errors
4618 * WERRNORM - persistent write errors
4619 * WERRNONPERSIST - non-persistent write errors
4621 if (arg
& ~(RERRMASK
| WERRMASK
))
4624 mutex_enter(&stp
->sd_lock
);
4625 switch (arg
& RERRMASK
) {
4627 stp
->sd_flag
&= ~STRDERRNONPERSIST
;
4629 case RERRNONPERSIST
:
4630 stp
->sd_flag
|= STRDERRNONPERSIST
;
4633 switch (arg
& WERRMASK
) {
4635 stp
->sd_flag
&= ~STWRERRNONPERSIST
;
4637 case WERRNONPERSIST
:
4638 stp
->sd_flag
|= STWRERRNONPERSIST
;
4641 mutex_exit(&stp
->sd_lock
);
4646 * Get error option and return the value
4647 * to spot pointed to by arg
4652 erropt
|= (stp
->sd_flag
& STRDERRNONPERSIST
) ? RERRNONPERSIST
:
4654 erropt
|= (stp
->sd_flag
& STWRERRNONPERSIST
) ? WERRNONPERSIST
:
4656 return (strcopyout(&erropt
, (void *)arg
, sizeof (int),
4662 * Register the calling proc to receive the SIGPOLL
4663 * signal based on the events given in arg. If
4664 * arg is zero, remove the proc from register list.
4667 strsig_t
*ssp
, *pssp
;
4671 pidp
= curproc
->p_pidp
;
4673 * Hold sd_lock to prevent traversal of sd_siglist while
4676 mutex_enter(&stp
->sd_lock
);
4677 for (ssp
= stp
->sd_siglist
; ssp
&& (ssp
->ss_pidp
!= pidp
);
4678 pssp
= ssp
, ssp
= ssp
->ss_next
)
4682 if (arg
& ~(S_INPUT
|S_HIPRI
|S_MSG
|S_HANGUP
|S_ERROR
|
4683 S_RDNORM
|S_WRNORM
|S_RDBAND
|S_WRBAND
|S_BANDURG
)) {
4684 mutex_exit(&stp
->sd_lock
);
4687 if ((arg
& S_BANDURG
) && !(arg
& S_RDBAND
)) {
4688 mutex_exit(&stp
->sd_lock
);
4693 * If proc not already registered, add it
4697 ssp
= kmem_alloc(sizeof (strsig_t
), KM_SLEEP
);
4698 ssp
->ss_pidp
= pidp
;
4699 ssp
->ss_pid
= pidp
->pid_id
;
4700 ssp
->ss_next
= NULL
;
4702 pssp
->ss_next
= ssp
;
4704 stp
->sd_siglist
= ssp
;
4705 mutex_enter(&pidlock
);
4707 mutex_exit(&pidlock
);
4713 ssp
->ss_events
= (int)arg
;
4716 * Remove proc from register list.
4719 mutex_enter(&pidlock
);
4721 mutex_exit(&pidlock
);
4723 pssp
->ss_next
= ssp
->ss_next
;
4725 stp
->sd_siglist
= ssp
->ss_next
;
4726 kmem_free(ssp
, sizeof (strsig_t
));
4728 mutex_exit(&stp
->sd_lock
);
4734 * Recalculate OR of sig events.
4736 stp
->sd_sigflags
= 0;
4737 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
4738 stp
->sd_sigflags
|= ssp
->ss_events
;
4739 mutex_exit(&stp
->sd_lock
);
4745 * Return (in arg) the current registration of events
4746 * for which the calling proc is to be signaled.
4752 pidp
= curproc
->p_pidp
;
4753 mutex_enter(&stp
->sd_lock
);
4754 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
4755 if (ssp
->ss_pidp
== pidp
) {
4756 error
= strcopyout(&ssp
->ss_events
, (void *)arg
,
4757 sizeof (int), copyflag
);
4758 mutex_exit(&stp
->sd_lock
);
4761 mutex_exit(&stp
->sd_lock
);
4767 * Register the ss_pid to receive the SIGPOLL
4768 * signal based on the events is ss_events arg. If
4769 * ss_events is zero, remove the proc from register list.
4772 struct strsig
*ssp
, *pssp
;
4776 struct strsigset ss
;
4778 error
= strcopyin((void *)arg
, &ss
, sizeof (ss
), copyflag
);
4784 if (ss
.ss_events
!= 0) {
4786 * Permissions check by sending signal 0.
4787 * Note that when kill fails it does a set_errno
4788 * causing the system call to fail.
4790 error
= kill(pid
, 0);
4795 mutex_enter(&pidlock
);
4799 proc
= pgfind(-pid
);
4803 mutex_exit(&pidlock
);
4807 pidp
= proc
->p_pgidp
;
4809 pidp
= proc
->p_pidp
;
4812 * Get a hold on the pid structure while referencing it.
4813 * There is a separate PID_HOLD should it be inserted
4814 * in the list below.
4817 mutex_exit(&pidlock
);
4821 * Hold sd_lock to prevent traversal of sd_siglist while
4824 mutex_enter(&stp
->sd_lock
);
4825 for (ssp
= stp
->sd_siglist
; ssp
&& (ssp
->ss_pid
!= pid
);
4826 pssp
= ssp
, ssp
= ssp
->ss_next
)
4831 ~(S_INPUT
|S_HIPRI
|S_MSG
|S_HANGUP
|S_ERROR
|
4832 S_RDNORM
|S_WRNORM
|S_RDBAND
|S_WRBAND
|S_BANDURG
)) {
4833 mutex_exit(&stp
->sd_lock
);
4834 mutex_enter(&pidlock
);
4836 mutex_exit(&pidlock
);
4839 if ((ss
.ss_events
& S_BANDURG
) &&
4840 !(ss
.ss_events
& S_RDBAND
)) {
4841 mutex_exit(&stp
->sd_lock
);
4842 mutex_enter(&pidlock
);
4844 mutex_exit(&pidlock
);
4849 * If proc not already registered, add it
4853 ssp
= kmem_alloc(sizeof (strsig_t
), KM_SLEEP
);
4854 ssp
->ss_pidp
= pidp
;
4856 ssp
->ss_next
= NULL
;
4858 pssp
->ss_next
= ssp
;
4860 stp
->sd_siglist
= ssp
;
4861 mutex_enter(&pidlock
);
4863 mutex_exit(&pidlock
);
4869 ssp
->ss_events
= ss
.ss_events
;
4872 * Remove proc from register list.
4875 mutex_enter(&pidlock
);
4877 mutex_exit(&pidlock
);
4879 pssp
->ss_next
= ssp
->ss_next
;
4881 stp
->sd_siglist
= ssp
->ss_next
;
4882 kmem_free(ssp
, sizeof (strsig_t
));
4884 mutex_exit(&stp
->sd_lock
);
4885 mutex_enter(&pidlock
);
4887 mutex_exit(&pidlock
);
4893 * Recalculate OR of sig events.
4895 stp
->sd_sigflags
= 0;
4896 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
4897 stp
->sd_sigflags
|= ssp
->ss_events
;
4898 mutex_exit(&stp
->sd_lock
);
4899 mutex_enter(&pidlock
);
4901 mutex_exit(&pidlock
);
4907 * Return (in arg) the current registration of events
4908 * for which the calling proc is to be signaled.
4915 struct strsigset ss
;
4917 error
= strcopyin((void *)arg
, &ss
, sizeof (ss
), copyflag
);
4922 mutex_enter(&pidlock
);
4926 proc
= pgfind(-pid
);
4930 mutex_exit(&pidlock
);
4934 pidp
= proc
->p_pgidp
;
4936 pidp
= proc
->p_pidp
;
4938 /* Prevent the pidp from being reassigned */
4940 mutex_exit(&pidlock
);
4942 mutex_enter(&stp
->sd_lock
);
4943 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
4944 if (ssp
->ss_pid
== pid
) {
4945 ss
.ss_pid
= ssp
->ss_pid
;
4946 ss
.ss_events
= ssp
->ss_events
;
4947 error
= strcopyout(&ss
, (void *)arg
,
4948 sizeof (struct strsigset
), copyflag
);
4949 mutex_exit(&stp
->sd_lock
);
4950 mutex_enter(&pidlock
);
4952 mutex_exit(&pidlock
);
4955 mutex_exit(&stp
->sd_lock
);
4956 mutex_enter(&pidlock
);
4958 mutex_exit(&pidlock
);
4964 STRUCT_DECL(strpeek
, strpeek
);
4966 mblk_t
*fmp
, *tmp_mp
= NULL
;
4968 STRUCT_INIT(strpeek
, flag
);
4970 error
= strcopyin((void *)arg
, STRUCT_BUF(strpeek
),
4971 STRUCT_SIZE(strpeek
), copyflag
);
4975 mutex_enter(QLOCK(rdq
));
4977 * Skip the invalid messages
4979 for (mp
= rdq
->q_first
; mp
!= NULL
; mp
= mp
->b_next
)
4980 if (mp
->b_datap
->db_type
!= M_SIG
)
4984 * If user has requested to peek at a high priority message
4985 * and first message is not, return 0
4988 if ((STRUCT_FGET(strpeek
, flags
) & RS_HIPRI
) &&
4989 queclass(mp
) == QNORM
) {
4991 mutex_exit(QLOCK(rdq
));
4994 } else if (stp
->sd_struiordq
== NULL
||
4995 (STRUCT_FGET(strpeek
, flags
) & RS_HIPRI
)) {
4997 * No mblks to look at at the streamhead and
4998 * 1). This isn't a synch stream or
4999 * 2). This is a synch stream but caller wants high
5000 * priority messages which is not supported by
5001 * the synch stream. (it only supports QNORM)
5004 mutex_exit(QLOCK(rdq
));
5010 if (mp
&& mp
->b_datap
->db_type
== M_PASSFP
) {
5011 mutex_exit(QLOCK(rdq
));
5015 ASSERT(mp
== NULL
|| mp
->b_datap
->db_type
== M_PCPROTO
||
5016 mp
->b_datap
->db_type
== M_PROTO
||
5017 mp
->b_datap
->db_type
== M_DATA
);
5019 if (mp
&& mp
->b_datap
->db_type
== M_PCPROTO
) {
5020 STRUCT_FSET(strpeek
, flags
, RS_HIPRI
);
5022 STRUCT_FSET(strpeek
, flags
, 0);
5026 if (mp
&& ((tmp_mp
= dupmsg(mp
)) == NULL
)) {
5027 mutex_exit(QLOCK(rdq
));
5030 mutex_exit(QLOCK(rdq
));
5033 * set mp = tmp_mp, so that I_PEEK processing can continue.
5034 * tmp_mp is used to free the dup'd message.
5039 uio
.uio_extflg
= UIO_COPY_CACHED
;
5040 uio
.uio_segflg
= (copyflag
== U_TO_K
) ? UIO_USERSPACE
:
5044 * First process PROTO blocks, if any.
5045 * If user doesn't want to get ctl info by setting maxlen <= 0,
5046 * then set len to -1/0 and skip control blocks part.
5048 if (STRUCT_FGET(strpeek
, ctlbuf
.maxlen
) < 0)
5049 STRUCT_FSET(strpeek
, ctlbuf
.len
, -1);
5050 else if (STRUCT_FGET(strpeek
, ctlbuf
.maxlen
) == 0)
5051 STRUCT_FSET(strpeek
, ctlbuf
.len
, 0);
5055 iov
.iov_base
= STRUCT_FGETP(strpeek
, ctlbuf
.buf
);
5056 iov
.iov_len
= STRUCT_FGET(strpeek
, ctlbuf
.maxlen
);
5058 uio
.uio_resid
= iov
.iov_len
;
5059 uio
.uio_loffset
= 0;
5061 while (mp
&& mp
->b_datap
->db_type
!= M_DATA
&&
5062 uio
.uio_resid
>= 0) {
5063 ASSERT(STRUCT_FGET(strpeek
, flags
) == 0 ?
5064 mp
->b_datap
->db_type
== M_PROTO
:
5065 mp
->b_datap
->db_type
== M_PCPROTO
);
5067 if ((n
= MIN(uio
.uio_resid
,
5068 mp
->b_wptr
- mp
->b_rptr
)) != 0 &&
5069 (error
= uiomove((char *)mp
->b_rptr
, n
,
5070 UIO_READ
, &uio
)) != 0) {
5077 /* No ctl message */
5079 STRUCT_FSET(strpeek
, ctlbuf
.len
, -1);
5081 STRUCT_FSET(strpeek
, ctlbuf
.len
,
5082 STRUCT_FGET(strpeek
, ctlbuf
.maxlen
) -
5087 * Now process DATA blocks, if any.
5088 * If user doesn't want to get data info by setting maxlen <= 0,
5089 * then set len to -1/0 and skip data blocks part.
5091 if (STRUCT_FGET(strpeek
, databuf
.maxlen
) < 0)
5092 STRUCT_FSET(strpeek
, databuf
.len
, -1);
5093 else if (STRUCT_FGET(strpeek
, databuf
.maxlen
) == 0)
5094 STRUCT_FSET(strpeek
, databuf
.len
, 0);
5098 iov
.iov_base
= STRUCT_FGETP(strpeek
, databuf
.buf
);
5099 iov
.iov_len
= STRUCT_FGET(strpeek
, databuf
.maxlen
);
5101 uio
.uio_resid
= iov
.iov_len
;
5102 uio
.uio_loffset
= 0;
5104 while (mp
&& uio
.uio_resid
) {
5105 if (mp
->b_datap
->db_type
== M_DATA
) {
5106 if ((n
= MIN(uio
.uio_resid
,
5107 mp
->b_wptr
- mp
->b_rptr
)) != 0 &&
5108 (error
= uiomove((char *)mp
->b_rptr
,
5109 n
, UIO_READ
, &uio
)) != 0) {
5115 ASSERT(data_part
== 0 ||
5116 mp
->b_datap
->db_type
== M_DATA
);
5119 /* No data message */
5121 STRUCT_FSET(strpeek
, databuf
.len
, -1);
5123 STRUCT_FSET(strpeek
, databuf
.len
,
5124 STRUCT_FGET(strpeek
, databuf
.maxlen
) -
5130 * It is a synch stream and user wants to get
5131 * data (maxlen > 0).
5132 * uio setup is done by the codes that process DATA
5135 if ((fmp
== NULL
) && STRUCT_FGET(strpeek
, databuf
.maxlen
) > 0) {
5138 infod
.d_cmd
= INFOD_COPYOUT
;
5140 infod
.d_uiop
= &uio
;
5141 error
= infonext(rdq
, &infod
);
5142 if (error
== EINVAL
|| error
== EBUSY
)
5146 STRUCT_FSET(strpeek
, databuf
.len
, STRUCT_FGET(strpeek
,
5147 databuf
.maxlen
) - uio
.uio_resid
);
5148 if (STRUCT_FGET(strpeek
, databuf
.len
) == 0) {
5150 * No data found by the infonext().
5152 STRUCT_FSET(strpeek
, databuf
.len
, -1);
5155 error
= strcopyout(STRUCT_BUF(strpeek
), (void *)arg
,
5156 STRUCT_SIZE(strpeek
), copyflag
);
5161 * If there is no message retrieved, set return code to 0
5162 * otherwise, set it to 1.
5164 if (STRUCT_FGET(strpeek
, ctlbuf
.len
) == -1 &&
5165 STRUCT_FGET(strpeek
, databuf
.len
) == -1)
5174 STRUCT_DECL(strfdinsert
, strfdinsert
);
5175 struct file
*resftp
;
5176 struct stdata
*resstp
;
5181 STRUCT_INIT(strfdinsert
, flag
);
5182 if (stp
->sd_flag
& STRHUP
)
5185 * STRDERR, STWRERR and STPLEX tested above.
5187 error
= strcopyin((void *)arg
, STRUCT_BUF(strfdinsert
),
5188 STRUCT_SIZE(strfdinsert
), copyflag
);
5192 if (STRUCT_FGET(strfdinsert
, offset
) < 0 ||
5193 (STRUCT_FGET(strfdinsert
, offset
) %
5194 sizeof (t_uscalar_t
)) != 0)
5196 if ((resftp
= getf(STRUCT_FGET(strfdinsert
, fildes
))) != NULL
) {
5197 if ((resstp
= resftp
->f_vnode
->v_stream
) == NULL
) {
5198 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5204 mutex_enter(&resstp
->sd_lock
);
5205 if (resstp
->sd_flag
& (STRDERR
|STWRERR
|STRHUP
|STPLEX
)) {
5206 error
= strgeterr(resstp
,
5207 STRDERR
|STWRERR
|STRHUP
|STPLEX
, 0);
5209 mutex_exit(&resstp
->sd_lock
);
5210 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5214 mutex_exit(&resstp
->sd_lock
);
5219 queue_t
*mate
= NULL
;
5221 /* get read queue of stream terminus */
5222 claimstr(resstp
->sd_wrq
);
5223 for (q
= resstp
->sd_wrq
->q_next
; q
->q_next
!= NULL
;
5225 if (!STRMATED(resstp
) && STREAM(q
) != resstp
&&
5227 ASSERT(q
->q_qinfo
->qi_srvp
);
5228 ASSERT(_OTHERQ(q
)->q_qinfo
->qi_srvp
);
5235 releasestr(resstp
->sd_wrq
);
5236 ival
= (t_uscalar_t
)q
;
5239 ival
= (t_uscalar_t
)getminor(resftp
->f_vnode
->v_rdev
);
5242 if (STRUCT_FGET(strfdinsert
, ctlbuf
.len
) <
5243 STRUCT_FGET(strfdinsert
, offset
) + sizeof (t_uscalar_t
)) {
5244 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5249 * Check for legal flag value.
5251 if (STRUCT_FGET(strfdinsert
, flags
) & ~RS_HIPRI
) {
5252 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5256 /* get these values from those cached in the stream head */
5257 mutex_enter(QLOCK(stp
->sd_wrq
));
5258 rmin
= stp
->sd_qn_minpsz
;
5259 rmax
= stp
->sd_qn_maxpsz
;
5260 mutex_exit(QLOCK(stp
->sd_wrq
));
5263 * Make sure ctl and data sizes together fall within
5264 * the limits of the max and min receive packet sizes
5265 * and do not exceed system limit. A negative data
5266 * length means that no data part is to be sent.
5268 ASSERT((rmax
>= 0) || (rmax
== INFPSZ
));
5270 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5273 if ((msgsize
= STRUCT_FGET(strfdinsert
, databuf
.len
)) < 0)
5275 if ((msgsize
< rmin
) ||
5276 ((msgsize
> rmax
) && (rmax
!= INFPSZ
)) ||
5277 (STRUCT_FGET(strfdinsert
, ctlbuf
.len
) > strctlsz
)) {
5278 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5282 mutex_enter(&stp
->sd_lock
);
5283 while (!(STRUCT_FGET(strfdinsert
, flags
) & RS_HIPRI
) &&
5284 !canputnext(stp
->sd_wrq
)) {
5285 if ((error
= strwaitq(stp
, WRITEWAIT
, (ssize_t
)0,
5286 flag
, -1, &done
)) != 0 || done
) {
5287 mutex_exit(&stp
->sd_lock
);
5288 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5291 if ((error
= i_straccess(stp
, access
)) != 0) {
5292 mutex_exit(&stp
->sd_lock
);
5294 STRUCT_FGET(strfdinsert
, fildes
));
5298 mutex_exit(&stp
->sd_lock
);
5301 * Copy strfdinsert.ctlbuf into native form of
5302 * ctlbuf to pass down into strmakemsg().
5304 mctl
.maxlen
= STRUCT_FGET(strfdinsert
, ctlbuf
.maxlen
);
5305 mctl
.len
= STRUCT_FGET(strfdinsert
, ctlbuf
.len
);
5306 mctl
.buf
= STRUCT_FGETP(strfdinsert
, ctlbuf
.buf
);
5308 iov
.iov_base
= STRUCT_FGETP(strfdinsert
, databuf
.buf
);
5309 iov
.iov_len
= STRUCT_FGET(strfdinsert
, databuf
.len
);
5312 uio
.uio_loffset
= 0;
5313 uio
.uio_segflg
= (copyflag
== U_TO_K
) ? UIO_USERSPACE
:
5316 uio
.uio_extflg
= UIO_COPY_CACHED
;
5317 uio
.uio_resid
= iov
.iov_len
;
5318 if ((error
= strmakemsg(&mctl
,
5319 &msgsize
, &uio
, stp
,
5320 STRUCT_FGET(strfdinsert
, flags
), &mp
)) != 0 || !mp
) {
5321 STRUCT_FSET(strfdinsert
, databuf
.len
, msgsize
);
5322 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5326 STRUCT_FSET(strfdinsert
, databuf
.len
, msgsize
);
5329 * Place the possibly reencoded queue pointer 'offset' bytes
5330 * from the start of the control portion of the message.
5332 *((t_uscalar_t
*)(mp
->b_rptr
+
5333 STRUCT_FGET(strfdinsert
, offset
))) = ival
;
5336 * Put message downstream.
5338 stream_willservice(stp
);
5339 putnext(stp
->sd_wrq
, mp
);
5340 stream_runservice(stp
);
5341 releasef(STRUCT_FGET(strfdinsert
, fildes
));
5349 if ((fp
= getf((int)arg
)) == NULL
)
5351 error
= do_sendfp(stp
, fp
, crp
);
5353 audit_fdsend((int)arg
, fp
, error
);
5362 struct k_strrecvfd
*srf
;
5365 mutex_enter(&stp
->sd_lock
);
5366 while (!(mp
= getq(rdq
))) {
5367 if (stp
->sd_flag
& (STRHUP
|STREOF
)) {
5368 mutex_exit(&stp
->sd_lock
);
5371 if ((error
= strwaitq(stp
, GETWAIT
, (ssize_t
)0,
5372 flag
, -1, &done
)) != 0 || done
) {
5373 mutex_exit(&stp
->sd_lock
);
5376 if ((error
= i_straccess(stp
, access
)) != 0) {
5377 mutex_exit(&stp
->sd_lock
);
5381 if (mp
->b_datap
->db_type
!= M_PASSFP
) {
5382 putback(stp
, rdq
, mp
, mp
->b_band
);
5383 mutex_exit(&stp
->sd_lock
);
5386 mutex_exit(&stp
->sd_lock
);
5388 srf
= (struct k_strrecvfd
*)mp
->b_rptr
;
5389 if ((fd
= ufalloc(0)) == -1) {
5390 mutex_enter(&stp
->sd_lock
);
5391 putback(stp
, rdq
, mp
, mp
->b_band
);
5392 mutex_exit(&stp
->sd_lock
);
5395 if (cmd
== I_RECVFD
) {
5396 struct o_strrecvfd ostrfd
;
5398 /* check to see if uid/gid values are too large. */
5400 if (srf
->uid
> (o_uid_t
)USHRT_MAX
||
5401 srf
->gid
> (o_gid_t
)USHRT_MAX
) {
5402 mutex_enter(&stp
->sd_lock
);
5403 putback(stp
, rdq
, mp
, mp
->b_band
);
5404 mutex_exit(&stp
->sd_lock
);
5405 setf(fd
, NULL
); /* release fd entry */
5410 ostrfd
.uid
= (o_uid_t
)srf
->uid
;
5411 ostrfd
.gid
= (o_gid_t
)srf
->gid
;
5413 /* Null the filler bits */
5414 for (i
= 0; i
< 8; i
++)
5417 error
= strcopyout(&ostrfd
, (void *)arg
,
5418 sizeof (struct o_strrecvfd
), copyflag
);
5419 } else { /* I_E_RECVFD */
5420 struct strrecvfd strfd
;
5423 strfd
.uid
= srf
->uid
;
5424 strfd
.gid
= srf
->gid
;
5426 /* null the filler bits */
5427 for (i
= 0; i
< 8; i
++)
5430 error
= strcopyout(&strfd
, (void *)arg
,
5431 sizeof (struct strrecvfd
), copyflag
);
5435 setf(fd
, NULL
); /* release fd entry */
5436 mutex_enter(&stp
->sd_lock
);
5437 putback(stp
, rdq
, mp
, mp
->b_band
);
5438 mutex_exit(&stp
->sd_lock
);
5442 audit_fdrecv(fd
, srf
->fp
);
5446 * Always increment f_count since the freemsg() below will
5447 * always call free_passfp() which performs a closef().
5449 mutex_enter(&srf
->fp
->f_tlock
);
5451 mutex_exit(&srf
->fp
->f_tlock
);
5459 * Set/clear the write options. arg is a bit
5460 * mask with any of the following bits set...
5461 * SNDZERO - send zero length message
5462 * SNDPIPE - send sigpipe to process if
5463 * sd_werror is set and process is
5464 * doing a write or putmsg.
5465 * The new stream head write options should reflect
5468 if (arg
& ~(SNDZERO
|SNDPIPE
))
5471 mutex_enter(&stp
->sd_lock
);
5472 stp
->sd_wput_opt
&= ~(SW_SIGPIPE
|SW_SNDZERO
);
5474 stp
->sd_wput_opt
|= SW_SNDZERO
;
5476 stp
->sd_wput_opt
|= SW_SIGPIPE
;
5477 mutex_exit(&stp
->sd_lock
);
5484 if (stp
->sd_wput_opt
& SW_SNDZERO
)
5486 if (stp
->sd_wput_opt
& SW_SIGPIPE
)
5488 return (strcopyout(&wropt
, (void *)arg
, sizeof (wropt
),
5494 * Returns all the modules found on this stream,
5495 * upto the driver. If argument is NULL, return the
5496 * number of modules (including driver). If argument
5497 * is not NULL, copy the names into the structure
5505 struct str_mlist
*mlist
;
5506 STRUCT_DECL(str_list
, strlist
);
5508 if (arg
== (intptr_t)NULL
) {
5509 /* Return number of modules plus driver */
5510 if (stp
->sd_vnode
->v_type
== VFIFO
)
5511 *rvalp
= stp
->sd_pushcnt
;
5513 *rvalp
= stp
->sd_pushcnt
+ 1;
5517 STRUCT_INIT(strlist
, flag
);
5519 error
= strcopyin((void *)arg
, STRUCT_BUF(strlist
),
5520 STRUCT_SIZE(strlist
), copyflag
);
5524 mlist
= STRUCT_FGETP(strlist
, sl_modlist
);
5525 nmods
= STRUCT_FGET(strlist
, sl_nmods
);
5529 claimstr(stp
->sd_wrq
);
5531 for (i
= 0; i
< nmods
&& _SAMESTR(q
); i
++, q
= q
->q_next
) {
5532 qname
= Q2NAME(q
->q_next
);
5533 error
= strcopyout(qname
, &mlist
[i
], strlen(qname
) + 1,
5536 releasestr(stp
->sd_wrq
);
5540 releasestr(stp
->sd_wrq
);
5541 return (strcopyout(&i
, (void *)arg
, sizeof (int), copyflag
));
5549 if ((arg
< 0) || (arg
>= NBAND
))
5551 q
= _RD(stp
->sd_wrq
);
5552 mutex_enter(QLOCK(q
));
5553 if (arg
> (int)q
->q_nband
) {
5571 mutex_exit(QLOCK(q
));
5580 q
= _RD(stp
->sd_wrq
);
5581 mutex_enter(QLOCK(q
));
5584 mutex_exit(QLOCK(q
));
5587 intpri
= (int)mp
->b_band
;
5588 error
= strcopyout(&intpri
, (void *)arg
, sizeof (int),
5590 mutex_exit(QLOCK(q
));
5598 if (arg
& ~(ANYMARK
|LASTMARK
))
5600 q
= _RD(stp
->sd_wrq
);
5601 mutex_enter(&stp
->sd_lock
);
5602 if ((stp
->sd_flag
& STRATMARK
) && (arg
== ANYMARK
)) {
5605 mutex_enter(QLOCK(q
));
5610 else if ((arg
== ANYMARK
) && (mp
->b_flag
& MSGMARK
))
5612 else if ((arg
== LASTMARK
) && (mp
== stp
->sd_mark
))
5616 mutex_exit(QLOCK(q
));
5618 mutex_exit(&stp
->sd_lock
);
5626 if ((arg
< 0) || (arg
>= NBAND
))
5629 *rvalp
= bcanputnext(stp
->sd_wrq
, band
);
5637 error
= strcopyin((void *)arg
, &closetime
, sizeof (int),
5644 stp
->sd_closetime
= closetime
;
5652 closetime
= stp
->sd_closetime
;
5653 return (strcopyout(&closetime
, (void *)arg
, sizeof (int),
5661 mutex_enter(&stp
->sd_lock
);
5662 if (stp
->sd_sidp
== NULL
) {
5663 mutex_exit(&stp
->sd_lock
);
5666 sid
= stp
->sd_sidp
->pid_id
;
5667 mutex_exit(&stp
->sd_lock
);
5668 return (strcopyout(&sid
, (void *)arg
, sizeof (pid_t
),
5676 pid_t sid
, fg_pgid
, bg_pgid
;
5678 if (error
= strcopyin((void *)arg
, &pgrp
, sizeof (pid_t
),
5681 mutex_enter(&stp
->sd_lock
);
5682 mutex_enter(&pidlock
);
5683 if (stp
->sd_sidp
!= ttoproc(curthread
)->p_sessp
->s_sidp
) {
5684 mutex_exit(&pidlock
);
5685 mutex_exit(&stp
->sd_lock
);
5688 if (pgrp
== stp
->sd_pgidp
->pid_id
) {
5689 mutex_exit(&pidlock
);
5690 mutex_exit(&stp
->sd_lock
);
5693 if (pgrp
<= 0 || pgrp
>= maxpid
) {
5694 mutex_exit(&pidlock
);
5695 mutex_exit(&stp
->sd_lock
);
5698 if ((q
= pgfind(pgrp
)) == NULL
||
5699 q
->p_sessp
!= ttoproc(curthread
)->p_sessp
) {
5700 mutex_exit(&pidlock
);
5701 mutex_exit(&stp
->sd_lock
);
5704 sid
= stp
->sd_sidp
->pid_id
;
5705 fg_pgid
= q
->p_pgrp
;
5706 bg_pgid
= stp
->sd_pgidp
->pid_id
;
5707 CL_SET_PROCESS_GROUP(curthread
, sid
, bg_pgid
, fg_pgid
);
5708 PID_RELE(stp
->sd_pgidp
);
5709 ctty_clear_sighuped();
5710 stp
->sd_pgidp
= q
->p_pgidp
;
5711 PID_HOLD(stp
->sd_pgidp
);
5712 mutex_exit(&pidlock
);
5713 mutex_exit(&stp
->sd_lock
);
5721 mutex_enter(&stp
->sd_lock
);
5722 if (stp
->sd_sidp
== NULL
) {
5723 mutex_exit(&stp
->sd_lock
);
5726 pgrp
= stp
->sd_pgidp
->pid_id
;
5727 mutex_exit(&stp
->sd_lock
);
5728 return (strcopyout(&pgrp
, (void *)arg
, sizeof (pid_t
),
5734 return (strctty(stp
));
5739 /* freectty() always assumes curproc. */
5740 if (freectty(B_FALSE
) != 0)
5747 return (0); /* handled by the upper layer */
5752 * Custom free routine used for M_PASSFP messages.
5755 free_passfp(struct k_strrecvfd
*srf
)
5757 (void) closef(srf
->fp
);
5758 kmem_free(srf
, sizeof (struct k_strrecvfd
) + sizeof (frtn_t
));
5763 do_sendfp(struct stdata
*stp
, struct file
*fp
, struct cred
*cr
)
5765 queue_t
*qp
, *nextqp
;
5766 struct k_strrecvfd
*srf
;
5770 queue_t
*mate
= NULL
;
5774 if (stp
->sd_flag
& STRHUP
)
5777 claimstr(stp
->sd_wrq
);
5779 /* Fastpath, we have a pipe, and we are already mated, use it. */
5780 if (STRMATED(stp
)) {
5781 qp
= _RD(stp
->sd_mate
->sd_wrq
);
5784 } else { /* Not already mated. */
5787 * Walk the stream to the end of this one.
5788 * assumes that the claimstr() will prevent
5789 * plumbing between the stream head and the
5790 * driver from changing
5795 * Loop until we reach the end of this stream.
5796 * On completion, qp points to the write queue
5797 * at the end of the stream, or the read queue
5798 * at the stream head if this is a fifo.
5800 while (((qp
= qp
->q_next
) != NULL
) && _SAMESTR(qp
))
5804 * Just in case we get a q_next which is NULL, but
5805 * not at the end of the stream. This is actually
5806 * broken, so we set an assert to catch it in
5807 * debug, and set an error and return if not debug.
5811 releasestr(stp
->sd_wrq
);
5816 * Enter the syncq for the driver, so (hopefully)
5817 * the queue values will not change on us.
5818 * XXXX - This will only prevent the race IFF only
5819 * the write side modifies the q_next member, and
5820 * the put procedure is protected by at least
5823 if ((sq
= qp
->q_syncq
) != NULL
)
5824 entersq(sq
, SQ_PUT
);
5826 /* Now get the q_next value from this qp. */
5827 nextqp
= qp
->q_next
;
5830 * If nextqp exists and the other stream is different
5831 * from this one claim the stream, set the mate, and
5832 * get the read queue at the stream head of the other
5833 * stream. Assumes that nextqp was at least valid when
5834 * we got it. Hopefully the entersq of the driver
5835 * will prevent it from changing on us.
5837 if ((nextqp
!= NULL
) && (STREAM(nextqp
) != stp
)) {
5838 ASSERT(qp
->q_qinfo
->qi_srvp
);
5839 ASSERT(_OTHERQ(qp
)->q_qinfo
->qi_srvp
);
5840 ASSERT(_OTHERQ(qp
->q_next
)->q_qinfo
->qi_srvp
);
5843 /* Make sure we still have a q_next */
5844 if (nextqp
!= qp
->q_next
) {
5845 releasestr(stp
->sd_wrq
);
5850 qp
= _RD(STREAM(nextqp
)->sd_wrq
);
5853 /* If we entered the synq above, leave it. */
5855 leavesq(sq
, SQ_PUT
);
5856 } /* STRMATED(STP) */
5858 /* XXX prevents substitution of the ops vector */
5859 if (qp
->q_qinfo
!= &strdata
&& qp
->q_qinfo
!= &fifo_strdata
) {
5864 if (qp
->q_flag
& QFULL
) {
5870 * Since M_PASSFP messages include a file descriptor, we use
5871 * esballoc() and specify a custom free routine (free_passfp()) that
5872 * will close the descriptor as part of freeing the message. For
5873 * convenience, we stash the frtn_t right after the data block.
5875 bufsize
= sizeof (struct k_strrecvfd
) + sizeof (frtn_t
);
5876 srf
= kmem_alloc(bufsize
, KM_NOSLEEP
);
5882 frtnp
= (frtn_t
*)(srf
+ 1);
5883 frtnp
->free_arg
= (caddr_t
)srf
;
5884 frtnp
->free_func
= free_passfp
;
5886 mp
= esballoc((uchar_t
*)srf
, bufsize
, BPRI_MED
, frtnp
);
5888 kmem_free(srf
, bufsize
);
5892 mp
->b_wptr
+= sizeof (struct k_strrecvfd
);
5893 mp
->b_datap
->db_type
= M_PASSFP
;
5896 srf
->uid
= crgetuid(curthread
->t_cred
);
5897 srf
->gid
= crgetgid(curthread
->t_cred
);
5898 mutex_enter(&fp
->f_tlock
);
5900 mutex_exit(&fp
->f_tlock
);
5904 releasestr(stp
->sd_wrq
);
5911 * Send an ioctl message downstream and wait for acknowledgement.
5912 * flags may be set to either U_TO_K or K_TO_K and a combination
5913 * of STR_NOERROR or STR_NOSIG
5914 * STR_NOSIG: Signals are essentially ignored or held and have
5915 * no effect for the duration of the call.
5916 * STR_NOERROR: Ignores stream head read, write and hup errors.
5917 * Additionally, if an existing ioctl times out, it is assumed
5918 * lost and and this ioctl will continue as if the previous ioctl had
5919 * finished. ETIME may be returned if this ioctl times out (i.e.
5920 * ic_timout is not INFTIM). Non-stream head errors may be returned if
5921 * the ioc_error indicates that the driver/module had problems,
5922 * an EFAULT was found when accessing user data, a lack of
5928 struct strioctl
*strioc
,
5929 int fflags
, /* file flags with model info */
5935 struct iocblk
*iocbp
;
5936 struct copyreq
*reqp
;
5937 struct copyresp
*resp
;
5939 int transparent
= 0;
5943 int copyflag
= (flag
& (U_TO_K
| K_TO_K
));
5944 int sigflag
= (flag
& STR_NOSIG
);
5947 boolean_t set_iocwaitne
= B_FALSE
;
5949 ASSERT(copyflag
== U_TO_K
|| copyflag
== K_TO_K
);
5950 ASSERT((fflags
& FMODELS
) != 0);
5952 TRACE_2(TR_FAC_STREAMS_FR
,
5954 "strdoioctl:stp %p strioc %p", stp
, strioc
);
5955 if (strioc
->ic_len
== TRANSPARENT
) { /* send arg in M_DATA block */
5957 strioc
->ic_len
= sizeof (intptr_t);
5960 if (strioc
->ic_len
< 0 || (strmsgsz
> 0 && strioc
->ic_len
> strmsgsz
))
5963 if ((bp
= allocb_cred_wait(sizeof (union ioctypes
), sigflag
, &error
,
5964 crp
, curproc
->p_pid
)) == NULL
)
5967 bzero(bp
->b_wptr
, sizeof (union ioctypes
));
5969 iocbp
= (struct iocblk
*)bp
->b_wptr
;
5970 iocbp
->ioc_count
= strioc
->ic_len
;
5971 iocbp
->ioc_cmd
= strioc
->ic_cmd
;
5972 iocbp
->ioc_flag
= (fflags
& FMODELS
);
5975 iocbp
->ioc_cr
= crp
;
5976 DB_TYPE(bp
) = M_IOCTL
;
5977 bp
->b_wptr
+= sizeof (struct iocblk
);
5979 if (flag
& STR_NOERROR
)
5982 errs
= STRHUP
|STRDERR
|STWRERR
|STPLEX
;
5985 * If there is data to copy into ioctl block, do so.
5987 if (iocbp
->ioc_count
> 0) {
5990 * Note: STR_NOERROR does not have an effect
5993 id
= K_TO_K
| sigflag
;
5996 if ((error
= putiocd(bp
, strioc
->ic_dp
, id
, crp
)) != 0) {
6003 * We could have slept copying in user pages.
6004 * Recheck the stream head state (the other end
6005 * of a pipe could have gone away).
6007 if (stp
->sd_flag
& errs
) {
6008 mutex_enter(&stp
->sd_lock
);
6009 error
= strgeterr(stp
, errs
, 0);
6010 mutex_exit(&stp
->sd_lock
);
6019 iocbp
->ioc_count
= TRANSPARENT
;
6022 * Block for up to STRTIMOUT milliseconds if there is an outstanding
6023 * ioctl for this stream already running. All processes
6024 * sleeping here will be awakened as a result of an ACK
6025 * or NAK being received for the outstanding ioctl, or
6026 * as a result of the timer expiring on the outstanding
6027 * ioctl (a failure), or as a result of any waiting
6028 * process's timer expiring (also a failure).
6032 mutex_enter(&stp
->sd_lock
);
6033 while ((stp
->sd_flag
& IOCWAIT
) ||
6034 (!set_iocwaitne
&& (stp
->sd_flag
& IOCWAITNE
))) {
6037 TRACE_0(TR_FAC_STREAMS_FR
,
6039 "strdoioctl sleeps - IOCWAIT");
6040 cv_rval
= str_cv_wait(&stp
->sd_iocmonitor
, &stp
->sd_lock
,
6041 STRTIMOUT
, sigflag
);
6046 if (flag
& STR_NOERROR
) {
6048 * Terminating current ioctl in
6049 * progress -- assume it got lost and
6050 * wake up the other thread so that the
6051 * operation completes.
6053 if (!(stp
->sd_flag
& IOCWAITNE
)) {
6054 set_iocwaitne
= B_TRUE
;
6055 stp
->sd_flag
|= IOCWAITNE
;
6056 cv_broadcast(&stp
->sd_monitor
);
6059 * Otherwise, there's a running
6060 * STR_NOERROR -- we have no choice
6061 * here but to wait forever (or until
6066 * pending ioctl has caused
6072 } else if ((stp
->sd_flag
& errs
)) {
6073 error
= strgeterr(stp
, errs
, 0);
6076 mutex_exit(&stp
->sd_lock
);
6084 * Have control of ioctl mechanism.
6085 * Send down ioctl packet and wait for response.
6087 if (stp
->sd_iocblk
!= (mblk_t
*)-1) {
6088 freemsg(stp
->sd_iocblk
);
6090 stp
->sd_iocblk
= NULL
;
6093 * If this is marked with 'noerror' (internal; mostly
6094 * I_{P,}{UN,}LINK), then make sure nobody else is able to get
6095 * in here by setting IOCWAITNE.
6097 waitflags
= IOCWAIT
;
6098 if (flag
& STR_NOERROR
)
6099 waitflags
|= IOCWAITNE
;
6101 stp
->sd_flag
|= waitflags
;
6104 * Assign sequence number.
6106 iocbp
->ioc_id
= stp
->sd_iocid
= getiocseqno();
6108 mutex_exit(&stp
->sd_lock
);
6110 TRACE_1(TR_FAC_STREAMS_FR
,
6111 TR_STRDOIOCTL_PUT
, "strdoioctl put: stp %p", stp
);
6112 stream_willservice(stp
);
6113 putnext(stp
->sd_wrq
, bp
);
6114 stream_runservice(stp
);
6117 * Timed wait for acknowledgment. The wait time is limited by the
6118 * timeout value, which must be a positive integer (number of
6119 * milliseconds) to wait, or 0 (use default value of STRTIMOUT
6120 * milliseconds), or -1 (wait forever). This will be awakened
6121 * either by an ACK/NAK message arriving, the timer expiring, or
6122 * the timer expiring on another ioctl waiting for control of the
6126 mutex_enter(&stp
->sd_lock
);
6130 * If the reply has already arrived, don't sleep. If awakened from
6131 * the sleep, fail only if the reply has not arrived by then.
6132 * Otherwise, process the reply.
6134 while (!stp
->sd_iocblk
) {
6137 if (stp
->sd_flag
& errs
) {
6138 error
= strgeterr(stp
, errs
, 0);
6140 stp
->sd_flag
&= ~waitflags
;
6141 cv_broadcast(&stp
->sd_iocmonitor
);
6142 mutex_exit(&stp
->sd_lock
);
6148 TRACE_0(TR_FAC_STREAMS_FR
,
6149 TR_STRDOIOCTL_WAIT2
,
6150 "strdoioctl sleeps awaiting reply");
6153 cv_rval
= str_cv_wait(&stp
->sd_monitor
, &stp
->sd_lock
,
6154 (strioc
->ic_timout
?
6155 strioc
->ic_timout
* 1000 : STRTIMOUT
), sigflag
);
6158 * There are four possible cases here: interrupt, timeout,
6159 * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
6160 * valid M_IOCTL reply).
6162 * If we've been awakened by a STR_NOERROR ioctl on some other
6163 * thread, then sd_iocblk will still be NULL, and IOCWAITNE
6164 * will be set. Pretend as if we just timed out. Note that
6165 * this other thread waited at least STRTIMOUT before trying to
6166 * awaken our thread, so this is indistinguishable (even for
6167 * INFTIM) from the case where we failed with ETIME waiting on
6168 * IOCWAIT in the prior loop.
6170 if (cv_rval
> 0 && !(flag
& STR_NOERROR
) &&
6171 stp
->sd_iocblk
== NULL
&& (stp
->sd_flag
& IOCWAITNE
)) {
6176 * note: STR_NOERROR does not protect
6177 * us here.. use ic_timout < 0
6186 * A message could have come in after we were scheduled
6187 * but before we were actually run.
6189 bp
= stp
->sd_iocblk
;
6190 stp
->sd_iocblk
= NULL
;
6192 if ((bp
->b_datap
->db_type
== M_COPYIN
) ||
6193 (bp
->b_datap
->db_type
== M_COPYOUT
)) {
6194 mutex_exit(&stp
->sd_lock
);
6196 freemsg(bp
->b_cont
);
6199 bp
->b_datap
->db_type
= M_IOCDATA
;
6200 bp
->b_wptr
= bp
->b_rptr
+
6201 sizeof (struct copyresp
);
6202 resp
= (struct copyresp
*)bp
->b_rptr
;
6204 (caddr_t
)1; /* failure */
6205 stream_willservice(stp
);
6206 putnext(stp
->sd_wrq
, bp
);
6207 stream_runservice(stp
);
6208 mutex_enter(&stp
->sd_lock
);
6213 stp
->sd_flag
&= ~waitflags
;
6214 cv_broadcast(&stp
->sd_iocmonitor
);
6215 mutex_exit(&stp
->sd_lock
);
6220 bp
= stp
->sd_iocblk
;
6222 * Note: it is strictly impossible to get here with sd_iocblk set to
6223 * -1. This is because the initial loop above doesn't allow any new
6224 * ioctls into the fray until all others have passed this point.
6226 ASSERT(bp
!= NULL
&& bp
!= (mblk_t
*)-1);
6227 TRACE_1(TR_FAC_STREAMS_FR
,
6228 TR_STRDOIOCTL_ACK
, "strdoioctl got reply: bp %p", bp
);
6229 if ((bp
->b_datap
->db_type
== M_IOCACK
) ||
6230 (bp
->b_datap
->db_type
== M_IOCNAK
)) {
6231 /* for detection of duplicate ioctl replies */
6232 stp
->sd_iocblk
= (mblk_t
*)-1;
6233 stp
->sd_flag
&= ~waitflags
;
6234 cv_broadcast(&stp
->sd_iocmonitor
);
6235 mutex_exit(&stp
->sd_lock
);
6238 * flags not cleared here because we're still doing
6239 * copy in/out for ioctl.
6241 stp
->sd_iocblk
= NULL
;
6242 mutex_exit(&stp
->sd_lock
);
6247 * Have received acknowledgment.
6250 switch (bp
->b_datap
->db_type
) {
6255 iocbp
= (struct iocblk
*)bp
->b_rptr
;
6258 * Set error if indicated.
6260 if (iocbp
->ioc_error
) {
6261 error
= iocbp
->ioc_error
;
6268 *rvalp
= iocbp
->ioc_rval
;
6271 * Data may have been returned in ACK message (ioc_count > 0).
6272 * If so, copy it out to the user's buffer.
6274 if (iocbp
->ioc_count
&& !transparent
) {
6275 if (error
= getiocd(bp
, strioc
->ic_dp
, copyflag
))
6279 if (len
) /* an M_COPYOUT was used with I_STR */
6280 strioc
->ic_len
= len
;
6282 strioc
->ic_len
= (int)iocbp
->ioc_count
;
6290 * The only thing to do is set error as specified
6291 * in neg ack packet.
6293 iocbp
= (struct iocblk
*)bp
->b_rptr
;
6295 error
= (iocbp
->ioc_error
? iocbp
->ioc_error
: EINVAL
);
6300 * Driver or module has requested user ioctl data.
6302 reqp
= (struct copyreq
*)bp
->b_rptr
;
6305 * M_COPYIN should *never* have a message attached, though
6306 * it's harmless if it does -- thus, panic on a DEBUG
6307 * kernel and just free it on a non-DEBUG build.
6309 ASSERT(bp
->b_cont
== NULL
);
6310 if (bp
->b_cont
!= NULL
) {
6311 freemsg(bp
->b_cont
);
6315 error
= putiocd(bp
, reqp
->cq_addr
, flag
, crp
);
6316 if (error
&& bp
->b_cont
) {
6317 freemsg(bp
->b_cont
);
6321 bp
->b_wptr
= bp
->b_rptr
+ sizeof (struct copyresp
);
6322 bp
->b_datap
->db_type
= M_IOCDATA
;
6324 mblk_setcred(bp
, crp
, curproc
->p_pid
);
6325 resp
= (struct copyresp
*)bp
->b_rptr
;
6326 resp
->cp_rval
= (caddr_t
)(uintptr_t)error
;
6327 resp
->cp_flag
= (fflags
& FMODELS
);
6329 stream_willservice(stp
);
6330 putnext(stp
->sd_wrq
, bp
);
6331 stream_runservice(stp
);
6334 mutex_enter(&stp
->sd_lock
);
6335 stp
->sd_flag
&= ~waitflags
;
6336 cv_broadcast(&stp
->sd_iocmonitor
);
6337 mutex_exit(&stp
->sd_lock
);
6346 * Driver or module has ioctl data for a user.
6348 reqp
= (struct copyreq
*)bp
->b_rptr
;
6349 ASSERT(bp
->b_cont
!= NULL
);
6352 * Always (transparent or non-transparent )
6353 * use the address specified in the request
6355 taddr
= reqp
->cq_addr
;
6357 len
= (int)reqp
->cq_size
;
6359 /* copyout data to the provided address */
6360 error
= getiocd(bp
, taddr
, copyflag
);
6362 freemsg(bp
->b_cont
);
6365 bp
->b_wptr
= bp
->b_rptr
+ sizeof (struct copyresp
);
6366 bp
->b_datap
->db_type
= M_IOCDATA
;
6368 mblk_setcred(bp
, crp
, curproc
->p_pid
);
6369 resp
= (struct copyresp
*)bp
->b_rptr
;
6370 resp
->cp_rval
= (caddr_t
)(uintptr_t)error
;
6371 resp
->cp_flag
= (fflags
& FMODELS
);
6373 stream_willservice(stp
);
6374 putnext(stp
->sd_wrq
, bp
);
6375 stream_runservice(stp
);
6378 mutex_enter(&stp
->sd_lock
);
6379 stp
->sd_flag
&= ~waitflags
;
6380 cv_broadcast(&stp
->sd_iocmonitor
);
6381 mutex_exit(&stp
->sd_lock
);
6389 mutex_enter(&stp
->sd_lock
);
6390 stp
->sd_flag
&= ~waitflags
;
6391 cv_broadcast(&stp
->sd_iocmonitor
);
6392 mutex_exit(&stp
->sd_lock
);
6402 * Send an M_CMD message downstream and wait for a reply. This is a ptools
6403 * special used to retrieve information from modules/drivers a stream without
6404 * being subjected to flow control or interfering with pending messages on the
6405 * stream (e.g. an ioctl in flight).
6408 strdocmd(struct stdata
*stp
, struct strcmd
*scp
, cred_t
*crp
)
6411 struct cmdblk
*cmdp
;
6413 int errs
= STRHUP
|STRDERR
|STWRERR
|STPLEX
;
6414 clock_t rval
, timeout
= STRTIMOUT
;
6416 if (scp
->sc_len
< 0 || scp
->sc_len
> sizeof (scp
->sc_buf
) ||
6417 scp
->sc_timeout
< -1)
6420 if (scp
->sc_timeout
> 0)
6421 timeout
= scp
->sc_timeout
* MILLISEC
;
6423 if ((mp
= allocb_cred(sizeof (struct cmdblk
), crp
,
6424 curproc
->p_pid
)) == NULL
)
6429 cmdp
= (struct cmdblk
*)mp
->b_wptr
;
6431 cmdp
->cb_cmd
= scp
->sc_cmd
;
6432 cmdp
->cb_len
= scp
->sc_len
;
6434 mp
->b_wptr
+= sizeof (struct cmdblk
);
6436 DB_TYPE(mp
) = M_CMD
;
6437 DB_CPID(mp
) = curproc
->p_pid
;
6440 * Copy in the payload.
6442 if (cmdp
->cb_len
> 0) {
6443 mp
->b_cont
= allocb_cred(sizeof (scp
->sc_buf
), crp
,
6445 if (mp
->b_cont
== NULL
) {
6450 /* cb_len comes from sc_len, which has already been checked */
6451 ASSERT(cmdp
->cb_len
<= sizeof (scp
->sc_buf
));
6452 (void) bcopy(scp
->sc_buf
, mp
->b_cont
->b_wptr
, cmdp
->cb_len
);
6453 mp
->b_cont
->b_wptr
+= cmdp
->cb_len
;
6454 DB_CPID(mp
->b_cont
) = curproc
->p_pid
;
6458 * Since this mechanism is strictly for ptools, and since only one
6459 * process can be grabbed at a time, we simply fail if there's
6460 * currently an operation pending.
6462 mutex_enter(&stp
->sd_lock
);
6463 if (stp
->sd_flag
& STRCMDWAIT
) {
6464 mutex_exit(&stp
->sd_lock
);
6468 stp
->sd_flag
|= STRCMDWAIT
;
6469 ASSERT(stp
->sd_cmdblk
== NULL
);
6470 mutex_exit(&stp
->sd_lock
);
6472 putnext(stp
->sd_wrq
, mp
);
6476 * Timed wait for acknowledgment. If the reply has already arrived,
6477 * don't sleep. If awakened from the sleep, fail only if the reply
6478 * has not arrived by then. Otherwise, process the reply.
6480 mutex_enter(&stp
->sd_lock
);
6481 while (stp
->sd_cmdblk
== NULL
) {
6482 if (stp
->sd_flag
& errs
) {
6483 if ((error
= strgeterr(stp
, errs
, 0)) != 0)
6487 rval
= str_cv_wait(&stp
->sd_monitor
, &stp
->sd_lock
, timeout
, 0);
6488 if (stp
->sd_cmdblk
!= NULL
)
6492 error
= (rval
== 0) ? EINTR
: ETIME
;
6498 * We received a reply.
6500 mp
= stp
->sd_cmdblk
;
6501 stp
->sd_cmdblk
= NULL
;
6502 ASSERT(mp
!= NULL
&& DB_TYPE(mp
) == M_CMD
);
6503 ASSERT(stp
->sd_flag
& STRCMDWAIT
);
6504 stp
->sd_flag
&= ~STRCMDWAIT
;
6505 mutex_exit(&stp
->sd_lock
);
6507 cmdp
= (struct cmdblk
*)mp
->b_rptr
;
6508 if ((error
= cmdp
->cb_error
) != 0)
6512 * Data may have been returned in the reply (cb_len > 0).
6513 * If so, copy it out to the user's buffer.
6515 if (cmdp
->cb_len
> 0) {
6516 if (mp
->b_cont
== NULL
|| MBLKL(mp
->b_cont
) < cmdp
->cb_len
) {
6521 cmdp
->cb_len
= MIN(cmdp
->cb_len
, sizeof (scp
->sc_buf
));
6522 (void) bcopy(mp
->b_cont
->b_rptr
, scp
->sc_buf
, cmdp
->cb_len
);
6524 scp
->sc_len
= cmdp
->cb_len
;
6530 ASSERT(stp
->sd_cmdblk
== NULL
);
6531 stp
->sd_flag
&= ~STRCMDWAIT
;
6532 mutex_exit(&stp
->sd_lock
);
6538 * For the SunOS keyboard driver.
6539 * Return the next available "ioctl" sequence number.
6540 * Exported, so that streams modules can send "ioctl" messages
6541 * downstream from their open routine.
6548 mutex_enter(&strresources
);
6550 mutex_exit(&strresources
);
6555 * Get the next message from the read queue. If the message is
6556 * priority, STRPRI will have been set by strrput(). This flag
6557 * should be reset only when the entire message at the front of the
6558 * queue as been consumed.
6560 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
6565 struct strbuf
*mctl
,
6566 struct strbuf
*mdata
,
6567 unsigned char *prip
,
6574 mblk_t
*savemp
= NULL
;
6575 mblk_t
*savemptail
= NULL
;
6581 uint_t mark
; /* Contains MSG*MARK and _LASTMARK */
6582 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
6583 unsigned char pri
= 0;
6585 int pr
= 0; /* Partial read successful */
6587 struct uio
*uiop
= &uios
;
6591 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRGETMSG_ENTER
,
6592 "strgetmsg:%p", vp
);
6594 ASSERT(vp
->v_stream
);
6598 mutex_enter(&stp
->sd_lock
);
6600 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
6601 mutex_exit(&stp
->sd_lock
);
6605 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
6606 error
= strgeterr(stp
, STRDERR
|STPLEX
, 0);
6608 mutex_exit(&stp
->sd_lock
);
6612 mutex_exit(&stp
->sd_lock
);
6628 * Setup uio and iov for data part
6630 iovs
.iov_base
= mdata
->buf
;
6631 iovs
.iov_len
= mdata
->maxlen
;
6632 uios
.uio_iov
= &iovs
;
6633 uios
.uio_iovcnt
= 1;
6634 uios
.uio_loffset
= 0;
6635 uios
.uio_segflg
= UIO_USERSPACE
;
6637 uios
.uio_extflg
= UIO_COPY_CACHED
;
6638 uios
.uio_resid
= mdata
->maxlen
;
6639 uios
.uio_offset
= 0;
6641 q
= _RD(stp
->sd_wrq
);
6642 mutex_enter(&stp
->sd_lock
);
6643 old_sd_flag
= stp
->sd_flag
;
6647 mblk_t
*q_first
= q
->q_first
;
6650 * Get the next message of appropriate priority
6651 * from the stream head. If the caller is interested
6652 * in band or hipri messages, then they should already
6653 * be enqueued at the stream head. On the other hand
6654 * if the caller wants normal (band 0) messages, they
6655 * might be deferred in a synchronous stream and they
6656 * will need to be pulled up.
6658 * After we have dequeued a message, we might find that
6659 * it was a deferred M_SIG that was enqueued at the
6660 * stream head. It must now be posted as part of the
6661 * read by calling strsignal_nolock().
6663 * Also note that strrput does not enqueue an M_PCSIG,
6664 * and there cannot be more than one hipri message,
6665 * so there was no need to have the M_PCSIG case.
6667 * At some time it might be nice to try and wrap the
6668 * functionality of kstrgetmsg() and strgetmsg() into
6669 * a common routine so to reduce the amount of replicated
6670 * code (since they are extremely similar).
6672 if (!(*flagsp
& (MSG_HIPRI
|MSG_BAND
))) {
6673 /* Asking for normal, band0 data */
6674 bp
= strget(stp
, q
, uiop
, first
, &error
);
6675 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
6677 if (DB_TYPE(bp
) == M_SIG
) {
6678 strsignal_nolock(stp
, *bp
->b_rptr
,
6690 * We can't depend on the value of STRPRI here because
6691 * the stream head may be in transit. Therefore, we
6692 * must look at the type of the first message to
6693 * determine if a high priority messages is waiting
6695 } else if ((*flagsp
& MSG_HIPRI
) && q_first
!= NULL
&&
6696 DB_TYPE(q_first
) >= QPCTL
&&
6697 (bp
= getq_noenab(q
, 0)) != NULL
) {
6698 /* Asked for HIPRI and got one */
6699 ASSERT(DB_TYPE(bp
) >= QPCTL
);
6701 } else if ((*flagsp
& MSG_BAND
) && q_first
!= NULL
&&
6702 ((q_first
->b_band
>= *prip
) || DB_TYPE(q_first
) >= QPCTL
) &&
6703 (bp
= getq_noenab(q
, 0)) != NULL
) {
6705 * Asked for at least band "prip" and got either at
6706 * least that band or a hipri message.
6708 ASSERT(bp
->b_band
>= *prip
|| DB_TYPE(bp
) >= QPCTL
);
6709 if (DB_TYPE(bp
) == M_SIG
) {
6710 strsignal_nolock(stp
, *bp
->b_rptr
, bp
->b_band
);
6718 /* No data. Time to sleep? */
6722 * If STRHUP or STREOF, return 0 length control and data.
6723 * If resid is 0, then a read(fd,buf,0) was done. Do not
6724 * sleep to satisfy this request because by default we have
6725 * zero bytes to return.
6727 if ((stp
->sd_flag
& (STRHUP
|STREOF
)) || (mctl
->maxlen
== 0 &&
6728 mdata
->maxlen
== 0)) {
6729 mctl
->len
= mdata
->len
= 0;
6731 mutex_exit(&stp
->sd_lock
);
6734 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRGETMSG_WAIT
,
6735 "strgetmsg calls strwaitq:%p, %p",
6737 if (((error
= strwaitq(stp
, GETWAIT
, (ssize_t
)0, fmode
, -1,
6738 &done
)) != 0) || done
) {
6739 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRGETMSG_DONE
,
6740 "strgetmsg error or done:%p, %p",
6742 mutex_exit(&stp
->sd_lock
);
6745 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRGETMSG_AWAKE
,
6746 "strgetmsg awakes:%p, %p", vp
, uiop
);
6747 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
6748 mutex_exit(&stp
->sd_lock
);
6755 * Extract any mark information. If the message is not completely
6756 * consumed this information will be put in the mblk
6758 * If MSGMARKNEXT is set and the message is completely consumed
6759 * the STRATMARK flag will be set below. Likewise, if
6760 * MSGNOTMARKNEXT is set and the message is
6761 * completely consumed STRNOTATMARK will be set.
6763 mark
= bp
->b_flag
& (MSGMARK
| MSGMARKNEXT
| MSGNOTMARKNEXT
);
6764 ASSERT((mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
)) !=
6765 (MSGMARKNEXT
|MSGNOTMARKNEXT
));
6766 if (mark
!= 0 && bp
== stp
->sd_mark
) {
6768 stp
->sd_mark
= NULL
;
6771 * keep track of the original message type and priority
6774 type
= bp
->b_datap
->db_type
;
6775 if (type
== M_PASSFP
) {
6776 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
6778 bp
->b_flag
|= mark
& ~_LASTMARK
;
6779 putback(stp
, q
, bp
, pri
);
6780 qbackenable(q
, pri
);
6781 mutex_exit(&stp
->sd_lock
);
6784 ASSERT(type
!= M_SIG
);
6787 * Set this flag so strrput will not generate signals. Need to
6788 * make sure this flag is cleared before leaving this routine
6789 * else signals will stop being sent.
6791 stp
->sd_flag
|= STRGETINPROG
;
6792 mutex_exit(&stp
->sd_lock
);
6794 if (STREAM_NEEDSERVICE(stp
))
6795 stream_runservice(stp
);
6798 * Set HIPRI flag if message is priority.
6806 * First process PROTO or PCPROTO blocks, if any.
6808 if (mctl
->maxlen
>= 0 && type
!= M_DATA
) {
6812 bcnt
= mctl
->maxlen
;
6814 while (bp
!= NULL
&& bp
->b_datap
->db_type
!= M_DATA
) {
6815 if ((n
= MIN(bcnt
, bp
->b_wptr
- bp
->b_rptr
)) != 0 &&
6816 copyout(bp
->b_rptr
, ubuf
, n
)) {
6818 mutex_enter(&stp
->sd_lock
);
6820 * clear stream head pri flag based on
6821 * first message type
6823 if (type
>= QPCTL
) {
6824 ASSERT(type
== M_PCPROTO
);
6825 stp
->sd_flag
&= ~STRPRI
;
6833 if (bp
->b_rptr
>= bp
->b_wptr
) {
6843 mctl
->len
= mctl
->maxlen
- bcnt
;
6847 if (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
6849 * More PROTO blocks in msg.
6853 while (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
6857 savemptail
->b_cont
= NULL
;
6861 * Now process DATA blocks, if any.
6863 if (mdata
->maxlen
>= 0 && bp
) {
6865 * struiocopyout will consume a potential zero-length
6866 * M_DATA even if uio_resid is zero.
6868 size_t oldresid
= uiop
->uio_resid
;
6870 bp
= struiocopyout(bp
, uiop
, &error
);
6872 mutex_enter(&stp
->sd_lock
);
6874 * clear stream head hi pri flag based on
6877 if (type
>= QPCTL
) {
6878 ASSERT(type
== M_PCPROTO
);
6879 stp
->sd_flag
&= ~STRPRI
;
6886 * (pr == 1) indicates a partial read.
6888 if (oldresid
> uiop
->uio_resid
)
6890 mdata
->len
= mdata
->maxlen
- uiop
->uio_resid
;
6894 if (bp
) { /* more data blocks in msg */
6897 savemptail
->b_cont
= bp
;
6902 mutex_enter(&stp
->sd_lock
);
6904 if (pr
&& (savemp
->b_datap
->db_type
== M_DATA
) &&
6905 msgnodata(savemp
)) {
6907 * Avoid queuing a zero-length tail part of
6908 * a message. pr=1 indicates that we read some of
6914 * clear stream head hi pri flag based on
6917 if (type
>= QPCTL
) {
6918 ASSERT(type
== M_PCPROTO
);
6919 stp
->sd_flag
&= ~STRPRI
;
6922 savemp
->b_band
= pri
;
6924 * If the first message was HIPRI and the one we're
6925 * putting back isn't, then clear STRPRI, otherwise
6926 * set STRPRI again. Note that we must set STRPRI
6927 * again since the flush logic in strrput_nondata()
6928 * may have cleared it while we had sd_lock dropped.
6930 if (type
>= QPCTL
) {
6931 ASSERT(type
== M_PCPROTO
);
6932 if (queclass(savemp
) < QPCTL
)
6933 stp
->sd_flag
&= ~STRPRI
;
6935 stp
->sd_flag
|= STRPRI
;
6936 } else if (queclass(savemp
) >= QPCTL
) {
6938 * The first message was not a HIPRI message,
6939 * but the one we are about to putback is.
6940 * For simplicitly, we do not allow for HIPRI
6941 * messages to be embedded in the message
6942 * body, so just force it to same type as
6945 ASSERT(type
== M_DATA
|| type
== M_PROTO
);
6946 ASSERT(savemp
->b_datap
->db_type
== M_PCPROTO
);
6947 savemp
->b_datap
->db_type
= type
;
6950 savemp
->b_flag
|= mark
& ~_LASTMARK
;
6951 if ((mark
& _LASTMARK
) &&
6952 (stp
->sd_mark
== NULL
)) {
6954 * If another marked message arrived
6955 * while sd_lock was not held sd_mark
6956 * would be non-NULL.
6958 stp
->sd_mark
= savemp
;
6961 putback(stp
, q
, savemp
, pri
);
6965 * The complete message was consumed.
6967 * If another M_PCPROTO arrived while sd_lock was not held
6968 * it would have been discarded since STRPRI was still set.
6970 * Move the MSG*MARKNEXT information
6971 * to the stream head just in case
6972 * the read queue becomes empty.
6973 * clear stream head hi pri flag based on
6976 * If the stream head was at the mark
6977 * (STRATMARK) before we dropped sd_lock above
6978 * and some data was consumed then we have
6979 * moved past the mark thus STRATMARK is
6980 * cleared. However, if a message arrived in
6981 * strrput during the copyout above causing
6982 * STRATMARK to be set we can not clear that
6985 if (type
>= QPCTL
) {
6986 ASSERT(type
== M_PCPROTO
);
6987 stp
->sd_flag
&= ~STRPRI
;
6989 if (mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
|MSGMARK
)) {
6990 if (mark
& MSGMARKNEXT
) {
6991 stp
->sd_flag
&= ~STRNOTATMARK
;
6992 stp
->sd_flag
|= STRATMARK
;
6993 } else if (mark
& MSGNOTMARKNEXT
) {
6994 stp
->sd_flag
&= ~STRATMARK
;
6995 stp
->sd_flag
|= STRNOTATMARK
;
6997 stp
->sd_flag
&= ~(STRATMARK
|STRNOTATMARK
);
6999 } else if (pr
&& (old_sd_flag
& STRATMARK
)) {
7000 stp
->sd_flag
&= ~STRATMARK
;
7008 * Getmsg cleanup processing - if the state of the queue has changed
7009 * some signals may need to be sent and/or poll awakened.
7012 qbackenable(q
, pri
);
7015 * We dropped the stream head lock above. Send all M_SIG messages
7016 * before processing stream head for SIGPOLL messages.
7018 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
7019 while ((bp
= q
->q_first
) != NULL
&&
7020 (bp
->b_datap
->db_type
== M_SIG
)) {
7022 * sd_lock is held so the content of the read queue can not
7026 ASSERT(bp
!= NULL
&& bp
->b_datap
->db_type
== M_SIG
);
7028 strsignal_nolock(stp
, *bp
->b_rptr
, bp
->b_band
);
7029 mutex_exit(&stp
->sd_lock
);
7031 if (STREAM_NEEDSERVICE(stp
))
7032 stream_runservice(stp
);
7033 mutex_enter(&stp
->sd_lock
);
7037 * stream head cannot change while we make the determination
7038 * whether or not to send a signal. Drop the flag to allow strrput
7039 * to send firstmsgsigs again.
7041 stp
->sd_flag
&= ~STRGETINPROG
;
7044 * If the type of message at the front of the queue changed
7045 * due to the receive the appropriate signals and pollwakeup events
7046 * are generated. The type of changes are:
7047 * Processed a hipri message, q_first is not hipri.
7048 * Processed a band X message, and q_first is band Y.
7049 * The generated signals and pollwakeups are identical to what
7050 * strrput() generates should the message that is now on q_first
7051 * arrive to an empty read queue.
7053 * Note: only strrput will send a signal for a hipri message.
7055 if ((bp
= q
->q_first
) != NULL
&& !(stp
->sd_flag
& STRPRI
)) {
7056 strsigset_t signals
= 0;
7057 strpollset_t pollwakeups
= 0;
7059 if (flg
& MSG_HIPRI
) {
7061 * Removed a hipri message. Regular data at
7062 * the front of the queue.
7064 if (bp
->b_band
== 0) {
7065 signals
= S_INPUT
| S_RDNORM
;
7066 pollwakeups
= POLLIN
| POLLRDNORM
;
7068 signals
= S_INPUT
| S_RDBAND
;
7069 pollwakeups
= POLLIN
| POLLRDBAND
;
7071 } else if (pri
!= bp
->b_band
) {
7073 * The band is different for the new q_first.
7075 if (bp
->b_band
== 0) {
7077 pollwakeups
= POLLIN
| POLLRDNORM
;
7080 pollwakeups
= POLLIN
| POLLRDBAND
;
7084 if (pollwakeups
!= 0) {
7085 if (pollwakeups
== (POLLIN
| POLLRDNORM
)) {
7086 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
7088 stp
->sd_rput_opt
&= ~SR_POLLIN
;
7090 mutex_exit(&stp
->sd_lock
);
7091 pollwakeup(&stp
->sd_pollist
, pollwakeups
);
7092 mutex_enter(&stp
->sd_lock
);
7096 if (stp
->sd_sigflags
& signals
)
7097 strsendsig(stp
->sd_siglist
, signals
, bp
->b_band
, 0);
7099 mutex_exit(&stp
->sd_lock
);
7107 * Get the next message from the read queue. If the message is
7108 * priority, STRPRI will have been set by strrput(). This flag
7109 * should be reset only when the entire message at the front of the
7110 * queue as been consumed.
7112 * If uiop is NULL all data is returned in mctlp.
7113 * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
7115 * The timeout parameter is in milliseconds; -1 for infinity.
7116 * This routine handles the consolidation private flags:
7117 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7118 * MSG_DELAYERROR Defer the error check until the queue is empty.
7119 * MSG_HOLDSIG Hold signals while waiting for data.
7120 * MSG_IPEEK Only peek at messages.
7121 * MSG_DISCARDTAIL Discard the tail M_DATA part of the message
7123 * MSG_NOMARK If the message is marked leave it on the queue.
7125 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
7132 unsigned char *prip
,
7139 mblk_t
*savemp
= NULL
;
7140 mblk_t
*savemptail
= NULL
;
7147 uint_t mark
; /* Contains MSG*MARK and _LASTMARK */
7148 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
7149 unsigned char pri
= 0;
7151 int pr
= 0; /* Partial read successful */
7154 TRACE_1(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_ENTER
,
7155 "kstrgetmsg:%p", vp
);
7157 ASSERT(vp
->v_stream
);
7161 mutex_enter(&stp
->sd_lock
);
7163 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
7164 mutex_exit(&stp
->sd_lock
);
7169 if (stp
->sd_flag
& (STRDERR
|STPLEX
)) {
7170 if ((stp
->sd_flag
& STPLEX
) ||
7171 (flags
& (MSG_IGNERROR
|MSG_DELAYERROR
)) == 0) {
7172 error
= strgeterr(stp
, STRDERR
|STPLEX
,
7173 (flags
& MSG_IPEEK
));
7175 mutex_exit(&stp
->sd_lock
);
7180 mutex_exit(&stp
->sd_lock
);
7182 switch (flags
& (MSG_HIPRI
|MSG_ANY
|MSG_BAND
)) {
7197 q
= _RD(stp
->sd_wrq
);
7198 mutex_enter(&stp
->sd_lock
);
7199 old_sd_flag
= stp
->sd_flag
;
7205 mblk_t
*q_first
= q
->q_first
;
7208 * This section of the code operates just like the code
7209 * in strgetmsg(). There is a comment there about what
7212 if (!(flags
& (MSG_HIPRI
|MSG_BAND
))) {
7213 /* Asking for normal, band0 data */
7214 bp
= strget(stp
, q
, uiop
, first
, &error
);
7215 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
7217 if (DB_TYPE(bp
) == M_SIG
) {
7218 strsignal_nolock(stp
, *bp
->b_rptr
,
7230 * We can't depend on the value of STRPRI here because
7231 * the stream head may be in transit. Therefore, we
7232 * must look at the type of the first message to
7233 * determine if a high priority messages is waiting
7235 } else if ((flags
& MSG_HIPRI
) && q_first
!= NULL
&&
7236 DB_TYPE(q_first
) >= QPCTL
&&
7237 (bp
= getq_noenab(q
, 0)) != NULL
) {
7238 ASSERT(DB_TYPE(bp
) >= QPCTL
);
7240 } else if ((flags
& MSG_BAND
) && q_first
!= NULL
&&
7241 ((q_first
->b_band
>= *prip
) || DB_TYPE(q_first
) >= QPCTL
) &&
7242 (bp
= getq_noenab(q
, 0)) != NULL
) {
7244 * Asked for at least band "prip" and got either at
7245 * least that band or a hipri message.
7247 ASSERT(bp
->b_band
>= *prip
|| DB_TYPE(bp
) >= QPCTL
);
7248 if (DB_TYPE(bp
) == M_SIG
) {
7249 strsignal_nolock(stp
, *bp
->b_rptr
, bp
->b_band
);
7257 /* No data. Time to sleep? */
7261 * Delayed error notification?
7263 if ((stp
->sd_flag
& (STRDERR
|STPLEX
)) &&
7264 (flags
& (MSG_IGNERROR
|MSG_DELAYERROR
)) == MSG_DELAYERROR
) {
7265 error
= strgeterr(stp
, STRDERR
|STPLEX
,
7266 (flags
& MSG_IPEEK
));
7268 mutex_exit(&stp
->sd_lock
);
7274 * If STRHUP or STREOF, return 0 length control and data.
7275 * If a read(fd,buf,0) has been done, do not sleep, just
7278 * If mctlp == NULL and uiop == NULL, then the code will
7279 * do the strwaitq. This is an understood way of saying
7280 * sleep "polling" until a message is received.
7282 if ((stp
->sd_flag
& (STRHUP
|STREOF
)) ||
7283 (uiop
!= NULL
&& uiop
->uio_resid
== 0)) {
7287 mutex_exit(&stp
->sd_lock
);
7293 (MSG_HOLDSIG
|MSG_IGNERROR
|MSG_IPEEK
|MSG_DELAYERROR
)) {
7294 if (flags
& MSG_HOLDSIG
)
7295 waitflag
|= STR_NOSIG
;
7296 if (flags
& MSG_IGNERROR
)
7297 waitflag
|= STR_NOERROR
;
7298 if (flags
& MSG_IPEEK
)
7299 waitflag
|= STR_PEEK
;
7300 if (flags
& MSG_DELAYERROR
)
7301 waitflag
|= STR_DELAYERR
;
7304 fmode
= uiop
->uio_fmode
;
7308 TRACE_2(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_WAIT
,
7309 "kstrgetmsg calls strwaitq:%p, %p",
7311 if (((error
= strwaitq(stp
, waitflag
, (ssize_t
)0,
7312 fmode
, timout
, &done
))) != 0 || done
) {
7313 TRACE_2(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_DONE
,
7314 "kstrgetmsg error or done:%p, %p",
7316 mutex_exit(&stp
->sd_lock
);
7319 TRACE_2(TR_FAC_STREAMS_FR
, TR_KSTRGETMSG_AWAKE
,
7320 "kstrgetmsg awakes:%p, %p", vp
, uiop
);
7321 if ((error
= i_straccess(stp
, JCREAD
)) != 0) {
7322 mutex_exit(&stp
->sd_lock
);
7329 * Extract any mark information. If the message is not completely
7330 * consumed this information will be put in the mblk
7332 * If MSGMARKNEXT is set and the message is completely consumed
7333 * the STRATMARK flag will be set below. Likewise, if
7334 * MSGNOTMARKNEXT is set and the message is
7335 * completely consumed STRNOTATMARK will be set.
7337 mark
= bp
->b_flag
& (MSGMARK
| MSGMARKNEXT
| MSGNOTMARKNEXT
);
7338 ASSERT((mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
)) !=
7339 (MSGMARKNEXT
|MSGNOTMARKNEXT
));
7343 * If the caller doesn't want the mark return.
7344 * Used to implement MSG_WAITALL in sockets.
7346 if (flags
& MSG_NOMARK
) {
7347 putback(stp
, q
, bp
, pri
);
7348 qbackenable(q
, pri
);
7349 mutex_exit(&stp
->sd_lock
);
7350 return (EWOULDBLOCK
);
7352 if (bp
== stp
->sd_mark
) {
7354 stp
->sd_mark
= NULL
;
7359 * keep track of the first message type
7361 type
= bp
->b_datap
->db_type
;
7363 if (bp
->b_datap
->db_type
== M_PASSFP
) {
7364 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
7366 bp
->b_flag
|= mark
& ~_LASTMARK
;
7367 putback(stp
, q
, bp
, pri
);
7368 qbackenable(q
, pri
);
7369 mutex_exit(&stp
->sd_lock
);
7372 ASSERT(type
!= M_SIG
);
7374 if (flags
& MSG_IPEEK
) {
7376 * Clear any struioflag - we do the uiomove over again
7377 * when peeking since it simplifies the code.
7379 * Dup the message and put the original back on the queue.
7380 * If dupmsg() fails, try again with copymsg() to see if
7381 * there is indeed a shortage of memory. dupmsg() may fail
7382 * if db_ref in any of the messages reaches its limit.
7385 if ((nbp
= dupmsg(bp
)) == NULL
&& (nbp
= copymsg(bp
)) == NULL
) {
7387 * Restore the state of the stream head since we
7388 * need to drop sd_lock (strwaitbuf is sleeping).
7390 size_t size
= msgdsize(bp
);
7392 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
7394 bp
->b_flag
|= mark
& ~_LASTMARK
;
7395 putback(stp
, q
, bp
, pri
);
7396 mutex_exit(&stp
->sd_lock
);
7397 error
= strwaitbuf(size
, BPRI_HI
);
7400 * There is no net change to the queue thus
7401 * no need to qbackenable.
7408 if ((mark
& _LASTMARK
) && (stp
->sd_mark
== NULL
))
7410 bp
->b_flag
|= mark
& ~_LASTMARK
;
7411 putback(stp
, q
, bp
, pri
);
7416 * Set this flag so strrput will not generate signals. Need to
7417 * make sure this flag is cleared before leaving this routine
7418 * else signals will stop being sent.
7420 stp
->sd_flag
|= STRGETINPROG
;
7421 mutex_exit(&stp
->sd_lock
);
7423 if ((stp
->sd_rputdatafunc
!= NULL
) && (DB_TYPE(bp
) == M_DATA
)) {
7424 mblk_t
*tmp
, *prevmp
;
7427 * Put first non-data mblk back to stream head and
7428 * cut the mblk chain so sd_rputdatafunc only sees
7429 * M_DATA mblks. We can skip the first mblk since it
7430 * is M_DATA according to the condition above.
7432 for (prevmp
= bp
, tmp
= bp
->b_cont
; tmp
!= NULL
;
7433 prevmp
= tmp
, tmp
= tmp
->b_cont
) {
7434 if (DB_TYPE(tmp
) != M_DATA
) {
7435 prevmp
->b_cont
= NULL
;
7436 mutex_enter(&stp
->sd_lock
);
7437 putback(stp
, q
, tmp
, tmp
->b_band
);
7438 mutex_exit(&stp
->sd_lock
);
7443 bp
= (stp
->sd_rputdatafunc
)(stp
->sd_vnode
, bp
,
7444 NULL
, NULL
, NULL
, NULL
);
7450 if (STREAM_NEEDSERVICE(stp
))
7451 stream_runservice(stp
);
7454 * Set HIPRI flag if message is priority.
7462 * First process PROTO or PCPROTO blocks, if any.
7464 if (mctlp
!= NULL
&& type
!= M_DATA
) {
7468 while (bp
->b_cont
&& bp
->b_cont
->b_datap
->db_type
!= M_DATA
)
7475 if (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
7477 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
7481 while (bp
&& bp
->b_datap
->db_type
!= M_DATA
) {
7485 savemptail
->b_cont
= NULL
;
7489 * Now process DATA blocks, if any.
7492 /* Append data to tail of mctlp */
7494 if (mctlp
!= NULL
) {
7495 mblk_t
**mpp
= mctlp
;
7497 while (*mpp
!= NULL
)
7498 mpp
= &((*mpp
)->b_cont
);
7502 } else if (uiop
->uio_resid
>= 0 && bp
) {
7503 size_t oldresid
= uiop
->uio_resid
;
7506 * If a streams message is likely to consist
7507 * of many small mblks, it is pulled up into
7508 * one continuous chunk of memory.
7509 * The size of the first mblk may be bogus because
7510 * successive read() calls on the socket reduce
7511 * the size of this mblk until it is exhausted
7512 * and then the code walks on to the next. Thus
7513 * the size of the mblk may not be the original size
7514 * that was passed up, it's simply a remainder
7515 * and hence can be very small without any
7516 * implication that the packet is badly fragmented.
7517 * So the size of the possible second mblk is
7518 * used to spot a badly fragmented packet.
7519 * see longer comment at top of page
7520 * by mblk_pull_len declaration.
7523 if (bp
->b_cont
!= NULL
&& MBLKL(bp
->b_cont
) < mblk_pull_len
) {
7524 (void) pullupmsg(bp
, -1);
7527 bp
= struiocopyout(bp
, uiop
, &error
);
7529 if (mctlp
!= NULL
) {
7534 mutex_enter(&stp
->sd_lock
);
7536 * clear stream head hi pri flag based on
7539 if (!(flags
& MSG_IPEEK
) && (type
>= QPCTL
)) {
7540 ASSERT(type
== M_PCPROTO
);
7541 stp
->sd_flag
&= ~STRPRI
;
7547 * (pr == 1) indicates a partial read.
7549 if (oldresid
> uiop
->uio_resid
)
7553 if (bp
) { /* more data blocks in msg */
7556 savemptail
->b_cont
= bp
;
7561 mutex_enter(&stp
->sd_lock
);
7563 if (flags
& (MSG_IPEEK
|MSG_DISCARDTAIL
)) {
7565 * When MSG_DISCARDTAIL is set or
7566 * when peeking discard any tail. When peeking this
7567 * is the tail of the dup that was copied out - the
7568 * message has already been putback on the queue.
7569 * Return MOREDATA to the caller even though the data
7570 * is discarded. This is used by sockets (to
7574 if (!(flags
& MSG_IPEEK
) && (type
>= QPCTL
)) {
7575 ASSERT(type
== M_PCPROTO
);
7576 stp
->sd_flag
&= ~STRPRI
;
7578 } else if (pr
&& (savemp
->b_datap
->db_type
== M_DATA
) &&
7579 msgnodata(savemp
)) {
7581 * Avoid queuing a zero-length tail part of
7582 * a message. pr=1 indicates that we read some of
7587 if (type
>= QPCTL
) {
7588 ASSERT(type
== M_PCPROTO
);
7589 stp
->sd_flag
&= ~STRPRI
;
7592 savemp
->b_band
= pri
;
7594 * If the first message was HIPRI and the one we're
7595 * putting back isn't, then clear STRPRI, otherwise
7596 * set STRPRI again. Note that we must set STRPRI
7597 * again since the flush logic in strrput_nondata()
7598 * may have cleared it while we had sd_lock dropped.
7601 if (type
>= QPCTL
) {
7602 ASSERT(type
== M_PCPROTO
);
7603 if (queclass(savemp
) < QPCTL
)
7604 stp
->sd_flag
&= ~STRPRI
;
7606 stp
->sd_flag
|= STRPRI
;
7607 } else if (queclass(savemp
) >= QPCTL
) {
7609 * The first message was not a HIPRI message,
7610 * but the one we are about to putback is.
7611 * For simplicitly, we do not allow for HIPRI
7612 * messages to be embedded in the message
7613 * body, so just force it to same type as
7616 ASSERT(type
== M_DATA
|| type
== M_PROTO
);
7617 ASSERT(savemp
->b_datap
->db_type
== M_PCPROTO
);
7618 savemp
->b_datap
->db_type
= type
;
7621 if ((mark
& _LASTMARK
) &&
7622 (stp
->sd_mark
== NULL
)) {
7624 * If another marked message arrived
7625 * while sd_lock was not held sd_mark
7626 * would be non-NULL.
7628 stp
->sd_mark
= savemp
;
7630 savemp
->b_flag
|= mark
& ~_LASTMARK
;
7632 putback(stp
, q
, savemp
, pri
);
7634 } else if (!(flags
& MSG_IPEEK
)) {
7636 * The complete message was consumed.
7638 * If another M_PCPROTO arrived while sd_lock was not held
7639 * it would have been discarded since STRPRI was still set.
7641 * Move the MSG*MARKNEXT information
7642 * to the stream head just in case
7643 * the read queue becomes empty.
7644 * clear stream head hi pri flag based on
7647 * If the stream head was at the mark
7648 * (STRATMARK) before we dropped sd_lock above
7649 * and some data was consumed then we have
7650 * moved past the mark thus STRATMARK is
7651 * cleared. However, if a message arrived in
7652 * strrput during the copyout above causing
7653 * STRATMARK to be set we can not clear that
7655 * XXX A "perimeter" would help by single-threading strrput,
7656 * strread, strgetmsg and kstrgetmsg.
7658 if (type
>= QPCTL
) {
7659 ASSERT(type
== M_PCPROTO
);
7660 stp
->sd_flag
&= ~STRPRI
;
7662 if (mark
& (MSGMARKNEXT
|MSGNOTMARKNEXT
|MSGMARK
)) {
7663 if (mark
& MSGMARKNEXT
) {
7664 stp
->sd_flag
&= ~STRNOTATMARK
;
7665 stp
->sd_flag
|= STRATMARK
;
7666 } else if (mark
& MSGNOTMARKNEXT
) {
7667 stp
->sd_flag
&= ~STRATMARK
;
7668 stp
->sd_flag
|= STRNOTATMARK
;
7670 stp
->sd_flag
&= ~(STRATMARK
|STRNOTATMARK
);
7672 } else if (pr
&& (old_sd_flag
& STRATMARK
)) {
7673 stp
->sd_flag
&= ~STRATMARK
;
7681 * Getmsg cleanup processing - if the state of the queue has changed
7682 * some signals may need to be sent and/or poll awakened.
7685 qbackenable(q
, pri
);
7688 * We dropped the stream head lock above. Send all M_SIG messages
7689 * before processing stream head for SIGPOLL messages.
7691 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
7692 while ((bp
= q
->q_first
) != NULL
&&
7693 (bp
->b_datap
->db_type
== M_SIG
)) {
7695 * sd_lock is held so the content of the read queue can not
7699 ASSERT(bp
!= NULL
&& bp
->b_datap
->db_type
== M_SIG
);
7701 strsignal_nolock(stp
, *bp
->b_rptr
, bp
->b_band
);
7702 mutex_exit(&stp
->sd_lock
);
7704 if (STREAM_NEEDSERVICE(stp
))
7705 stream_runservice(stp
);
7706 mutex_enter(&stp
->sd_lock
);
7710 * stream head cannot change while we make the determination
7711 * whether or not to send a signal. Drop the flag to allow strrput
7712 * to send firstmsgsigs again.
7714 stp
->sd_flag
&= ~STRGETINPROG
;
7717 * If the type of message at the front of the queue changed
7718 * due to the receive the appropriate signals and pollwakeup events
7719 * are generated. The type of changes are:
7720 * Processed a hipri message, q_first is not hipri.
7721 * Processed a band X message, and q_first is band Y.
7722 * The generated signals and pollwakeups are identical to what
7723 * strrput() generates should the message that is now on q_first
7724 * arrive to an empty read queue.
7726 * Note: only strrput will send a signal for a hipri message.
7728 if ((bp
= q
->q_first
) != NULL
&& !(stp
->sd_flag
& STRPRI
)) {
7729 strsigset_t signals
= 0;
7730 strpollset_t pollwakeups
= 0;
7732 if (flg
& MSG_HIPRI
) {
7734 * Removed a hipri message. Regular data at
7735 * the front of the queue.
7737 if (bp
->b_band
== 0) {
7738 signals
= S_INPUT
| S_RDNORM
;
7739 pollwakeups
= POLLIN
| POLLRDNORM
;
7741 signals
= S_INPUT
| S_RDBAND
;
7742 pollwakeups
= POLLIN
| POLLRDBAND
;
7744 } else if (pri
!= bp
->b_band
) {
7746 * The band is different for the new q_first.
7748 if (bp
->b_band
== 0) {
7750 pollwakeups
= POLLIN
| POLLRDNORM
;
7753 pollwakeups
= POLLIN
| POLLRDBAND
;
7757 if (pollwakeups
!= 0) {
7758 if (pollwakeups
== (POLLIN
| POLLRDNORM
)) {
7759 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
7761 stp
->sd_rput_opt
&= ~SR_POLLIN
;
7763 mutex_exit(&stp
->sd_lock
);
7764 pollwakeup(&stp
->sd_pollist
, pollwakeups
);
7765 mutex_enter(&stp
->sd_lock
);
7769 if (stp
->sd_sigflags
& signals
)
7770 strsendsig(stp
->sd_siglist
, signals
, bp
->b_band
, 0);
7772 mutex_exit(&stp
->sd_lock
);
7780 * Put a message downstream.
7782 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7787 struct strbuf
*mctl
,
7788 struct strbuf
*mdata
,
7800 struct uio
*uiop
= &uios
;
7804 ASSERT(vp
->v_stream
);
7809 * If it is an XPG4 application, we need to send
7813 xpg4
= (flag
& MSG_XPG4
) ? 1 : 0;
7817 audit_strputmsg(vp
, mctl
, mdata
, pri
, flag
, fmode
);
7819 mutex_enter(&stp
->sd_lock
);
7821 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
7822 mutex_exit(&stp
->sd_lock
);
7826 if (stp
->sd_flag
& (STWRERR
|STRHUP
|STPLEX
)) {
7827 error
= strwriteable(stp
, B_FALSE
, xpg4
);
7829 mutex_exit(&stp
->sd_lock
);
7834 mutex_exit(&stp
->sd_lock
);
7837 * Check for legal flag value.
7841 if ((mctl
->len
< 0) || (pri
!= 0))
7851 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_IN
,
7852 "strputmsg in:stp %p", stp
);
7854 /* get these values from those cached in the stream head */
7855 rmin
= stp
->sd_qn_minpsz
;
7856 rmax
= stp
->sd_qn_maxpsz
;
7859 * Make sure ctl and data sizes together fall within the
7860 * limits of the max and min receive packet sizes and do
7861 * not exceed system limit.
7863 ASSERT((rmax
>= 0) || (rmax
== INFPSZ
));
7868 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
7869 * Needed to prevent partial failures in the strmakedata loop.
7871 if (stp
->sd_maxblk
!= INFPSZ
&& rmax
!= INFPSZ
&& rmax
< stp
->sd_maxblk
)
7872 rmax
= stp
->sd_maxblk
;
7874 if ((msgsize
= mdata
->len
) < 0) {
7876 rmin
= 0; /* no range check for NULL data part */
7878 if ((msgsize
< rmin
) ||
7879 ((msgsize
> rmax
) && (rmax
!= INFPSZ
)) ||
7880 (mctl
->len
> strctlsz
)) {
7885 * Setup uio and iov for data part
7887 iovs
.iov_base
= mdata
->buf
;
7888 iovs
.iov_len
= msgsize
;
7889 uios
.uio_iov
= &iovs
;
7890 uios
.uio_iovcnt
= 1;
7891 uios
.uio_loffset
= 0;
7892 uios
.uio_segflg
= UIO_USERSPACE
;
7893 uios
.uio_fmode
= fmode
;
7894 uios
.uio_extflg
= UIO_COPY_DEFAULT
;
7895 uios
.uio_resid
= msgsize
;
7896 uios
.uio_offset
= 0;
7898 /* Ignore flow control in strput for HIPRI */
7899 if (flag
& MSG_HIPRI
)
7900 flag
|= MSG_IGNFLOW
;
7906 * strput will always free the ctl mblk - even when strput
7909 if ((error
= strmakectl(mctl
, flag
, fmode
, &mp
)) != 0) {
7910 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_OUT
,
7911 "strputmsg out:stp %p out %d error %d",
7916 * Verify that the whole message can be transferred by
7919 ASSERT(stp
->sd_maxblk
== INFPSZ
||
7920 stp
->sd_maxblk
>= mdata
->len
);
7922 msgsize
= mdata
->len
;
7923 error
= strput(stp
, mp
, uiop
, &msgsize
, 0, pri
, flag
);
7924 mdata
->len
= msgsize
;
7929 if (error
!= EWOULDBLOCK
)
7932 mutex_enter(&stp
->sd_lock
);
7934 * Check for a missed wakeup.
7935 * Needed since strput did not hold sd_lock across
7938 if (bcanputnext(wqp
, pri
)) {
7940 mutex_exit(&stp
->sd_lock
);
7943 TRACE_2(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_WAIT
,
7944 "strputmsg wait:stp %p waits pri %d", stp
, pri
);
7945 if (((error
= strwaitq(stp
, WRITEWAIT
, (ssize_t
)0, fmode
, -1,
7946 &done
)) != 0) || done
) {
7947 mutex_exit(&stp
->sd_lock
);
7948 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_OUT
,
7949 "strputmsg out:q %p out %d error %d",
7953 TRACE_1(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_WAKE
,
7954 "strputmsg wake:stp %p wakes", stp
);
7955 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
7956 mutex_exit(&stp
->sd_lock
);
7959 mutex_exit(&stp
->sd_lock
);
7963 * For historic reasons, applications expect EAGAIN
7964 * when data mblk could not be allocated. so change
7965 * ENOMEM back to EAGAIN
7967 if (error
== ENOMEM
)
7969 TRACE_3(TR_FAC_STREAMS_FR
, TR_STRPUTMSG_OUT
,
7970 "strputmsg out:stp %p out %d error %d", stp
, 2, error
);
7975 * Put a message downstream.
7976 * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
7977 * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
7978 * and the fmode parameter.
7980 * This routine handles the consolidation private flags:
7981 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7982 * MSG_HOLDSIG Hold signals while waiting for data.
7983 * MSG_IGNFLOW Don't check streams flow control.
7985 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
8002 ASSERT(vp
->v_stream
);
8006 audit_strputmsg(vp
, NULL
, NULL
, pri
, flag
, fmode
);
8010 mutex_enter(&stp
->sd_lock
);
8012 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
8013 mutex_exit(&stp
->sd_lock
);
8018 if ((stp
->sd_flag
& STPLEX
) || !(flag
& MSG_IGNERROR
)) {
8019 if (stp
->sd_flag
& (STWRERR
|STRHUP
|STPLEX
)) {
8020 error
= strwriteable(stp
, B_FALSE
, B_TRUE
);
8022 mutex_exit(&stp
->sd_lock
);
8029 mutex_exit(&stp
->sd_lock
);
8032 * Check for legal flag value.
8034 switch (flag
& (MSG_HIPRI
|MSG_BAND
|MSG_ANY
)) {
8048 TRACE_1(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_IN
,
8049 "kstrputmsg in:stp %p", stp
);
8051 /* get these values from those cached in the stream head */
8052 rmin
= stp
->sd_qn_minpsz
;
8053 rmax
= stp
->sd_qn_maxpsz
;
8056 * Make sure ctl and data sizes together fall within the
8057 * limits of the max and min receive packet sizes and do
8058 * not exceed system limit.
8060 ASSERT((rmax
>= 0) || (rmax
== INFPSZ
));
8066 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
8067 * Needed to prevent partial failures in the strmakedata loop.
8069 if (stp
->sd_maxblk
!= INFPSZ
&& rmax
!= INFPSZ
&& rmax
< stp
->sd_maxblk
)
8070 rmax
= stp
->sd_maxblk
;
8074 rmin
= -1; /* no range check for NULL data part */
8076 /* Use uio flags as well as the fmode parameter flags */
8077 fmode
|= uiop
->uio_fmode
;
8079 if ((msgsize
< rmin
) ||
8080 ((msgsize
> rmax
) && (rmax
!= INFPSZ
))) {
8086 /* Ignore flow control in strput for HIPRI */
8087 if (flag
& MSG_HIPRI
)
8088 flag
|= MSG_IGNFLOW
;
8096 * strput will always free the ctl mblk - even when strput
8097 * fails. If MSG_IGNFLOW is set then any error returned
8098 * will cause us to break the loop, so we don't need a copy
8099 * of the message. If MSG_IGNFLOW is not set, then we can
8100 * get hit by flow control and be forced to try again. In
8101 * this case we need to have a copy of the message. We
8102 * do this using copymsg since the message may get modified
8103 * by something below us.
8105 * We've observed that many TPI providers do not check db_ref
8106 * on the control messages but blindly reuse them for the
8107 * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
8108 * friendly to such providers than using dupmsg. Also, note
8109 * that sockfs uses MSG_IGNFLOW for all TPI control messages.
8110 * Only data messages are subject to flow control, hence
8111 * subject to this copymsg.
8113 if (flag
& MSG_IGNFLOW
) {
8119 * If a message has a free pointer, the message
8120 * must be dupmsg to maintain this pointer.
8121 * Code using this facility must be sure
8122 * that modules below will not change the
8123 * contents of the dblk without checking db_ref
8124 * first. If db_ref is > 1, then the module
8125 * needs to do a copymsg first. Otherwise,
8126 * the contents of the dblk may become
8127 * inconsistent because the freesmg/freeb below
8128 * may end up calling atomic_add_32_nv.
8129 * The atomic_add_32_nv in freeb (accessing
8130 * all of db_ref, db_type, db_flags, and
8131 * db_struioflag) does not prevent other threads
8132 * from concurrently trying to modify e.g.
8135 if (mctl
->b_datap
->db_frtnp
!= NULL
)
8143 error
= strwaitbuf(msgdsize(mctl
), BPRI_MED
);
8148 } while (mp
== NULL
);
8151 * Verify that all of msgsize can be transferred by
8154 ASSERT(stp
->sd_maxblk
== INFPSZ
|| stp
->sd_maxblk
>= msgsize
);
8155 error
= strput(stp
, mp
, uiop
, &msgsize
, 0, pri
, flag
);
8159 if (error
!= EWOULDBLOCK
)
8163 * IF MSG_IGNFLOW is set we should have broken out of loop
8166 ASSERT(!(flag
& MSG_IGNFLOW
));
8167 mutex_enter(&stp
->sd_lock
);
8169 * Check for a missed wakeup.
8170 * Needed since strput did not hold sd_lock across
8173 if (bcanputnext(wqp
, pri
)) {
8175 mutex_exit(&stp
->sd_lock
);
8178 TRACE_2(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_WAIT
,
8179 "kstrputmsg wait:stp %p waits pri %d", stp
, pri
);
8181 waitflag
= WRITEWAIT
;
8182 if (flag
& (MSG_HOLDSIG
|MSG_IGNERROR
)) {
8183 if (flag
& MSG_HOLDSIG
)
8184 waitflag
|= STR_NOSIG
;
8185 if (flag
& MSG_IGNERROR
)
8186 waitflag
|= STR_NOERROR
;
8188 if (((error
= strwaitq(stp
, waitflag
,
8189 (ssize_t
)0, fmode
, -1, &done
)) != 0) || done
) {
8190 mutex_exit(&stp
->sd_lock
);
8191 TRACE_3(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_OUT
,
8192 "kstrputmsg out:stp %p out %d error %d",
8197 TRACE_1(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_WAKE
,
8198 "kstrputmsg wake:stp %p wakes", stp
);
8199 if ((error
= i_straccess(stp
, JCWRITE
)) != 0) {
8200 mutex_exit(&stp
->sd_lock
);
8204 mutex_exit(&stp
->sd_lock
);
8209 * For historic reasons, applications expect EAGAIN
8210 * when data mblk could not be allocated. so change
8211 * ENOMEM back to EAGAIN
8213 if (error
== ENOMEM
)
8215 TRACE_3(TR_FAC_STREAMS_FR
, TR_KSTRPUTMSG_OUT
,
8216 "kstrputmsg out:stp %p out %d error %d", stp
, 2, error
);
8221 * Determines whether the necessary conditions are set on a stream
8222 * for it to be readable, writeable, or have exceptions.
8224 * strpoll handles the consolidation private events:
8225 * POLLNOERR Do not return POLLERR even if there are stream
8228 * POLLRDDATA Do not return POLLIN unless at least one message on
8229 * the queue contains one or more M_DATA mblks. Thus
8230 * when this flag is set a queue with only
8231 * M_PROTO/M_PCPROTO mblks does not return POLLIN.
8232 * Used by sockfs to ignore T_EXDATA_IND messages.
8234 * Note: POLLRDDATA assumes that synch streams only return messages with
8235 * an M_DATA attached (i.e. not messages consisting of only
8236 * an M_PROTO/M_PCPROTO part).
8239 strpoll(struct stdata
*stp
, short events_arg
, int anyyet
, short *reventsp
,
8240 struct pollhead
**phpp
)
8242 int events
= (ushort_t
)events_arg
;
8246 long sd_flags
= stp
->sd_flag
;
8250 * For performance, a single 'if' tests for most possible edge
8251 * conditions in one shot
8253 if (sd_flags
& (STPLEX
| STRDERR
| STWRERR
)) {
8254 if (sd_flags
& STPLEX
) {
8255 *reventsp
= POLLNVAL
;
8258 if (((events
& (POLLIN
| POLLRDNORM
| POLLRDBAND
| POLLPRI
)) &&
8259 (sd_flags
& STRDERR
)) ||
8260 ((events
& (POLLOUT
| POLLWRNORM
| POLLWRBAND
)) &&
8261 (sd_flags
& STWRERR
))) {
8262 if (!(events
& POLLNOERR
)) {
8263 *reventsp
= POLLERR
;
8268 if (sd_flags
& STRHUP
) {
8269 retevents
|= POLLHUP
;
8270 } else if (events
& (POLLWRNORM
| POLLWRBAND
)) {
8272 queue_t
*qp
= stp
->sd_wrq
;
8275 /* Find next module forward that has a service procedure */
8276 tq
= qp
->q_next
->q_nfsrv
;
8279 if (polllock(&stp
->sd_pollist
, QLOCK(tq
)) != 0) {
8281 *reventsp
= POLLNVAL
;
8284 if (events
& POLLWRNORM
) {
8287 if (tq
->q_flag
& QFULL
)
8288 /* ensure backq svc procedure runs */
8289 tq
->q_flag
|= QWANTW
;
8290 else if ((sqp
= stp
->sd_struiowrq
) != NULL
) {
8291 /* Check sync stream barrier write q */
8292 mutex_exit(QLOCK(tq
));
8293 if (polllock(&stp
->sd_pollist
,
8296 *reventsp
= POLLNVAL
;
8299 if (sqp
->q_flag
& QFULL
)
8300 /* ensure pollwakeup() is done */
8301 sqp
->q_flag
|= QWANTWSYNC
;
8303 retevents
|= POLLOUT
;
8304 /* More write events to process ??? */
8305 if (! (events
& POLLWRBAND
)) {
8306 mutex_exit(QLOCK(sqp
));
8310 mutex_exit(QLOCK(sqp
));
8311 if (polllock(&stp
->sd_pollist
,
8314 *reventsp
= POLLNVAL
;
8318 retevents
|= POLLOUT
;
8320 if (events
& POLLWRBAND
) {
8324 if (qbp
->qb_flag
& QB_FULL
)
8325 qbp
->qb_flag
|= QB_WANTW
;
8327 retevents
|= POLLWRBAND
;
8331 retevents
|= POLLWRBAND
;
8334 mutex_exit(QLOCK(tq
));
8338 if (sd_flags
& STRPRI
) {
8339 retevents
|= (events
& POLLPRI
);
8340 } else if (events
& (POLLRDNORM
| POLLRDBAND
| POLLIN
)) {
8341 queue_t
*qp
= _RD(stp
->sd_wrq
);
8342 int normevents
= (events
& (POLLIN
| POLLRDNORM
));
8345 * Note: Need to do polllock() here since ps_lock may be
8346 * held. See bug 4191544.
8348 if (polllock(&stp
->sd_pollist
, &stp
->sd_lock
) != 0) {
8349 *reventsp
= POLLNVAL
;
8356 * For POLLRDDATA we scan b_cont and b_next until we
8359 if ((events
& POLLRDDATA
) &&
8360 mp
->b_datap
->db_type
!= M_DATA
) {
8361 mblk_t
*nmp
= mp
->b_cont
;
8363 while (nmp
!= NULL
&&
8364 nmp
->b_datap
->db_type
!= M_DATA
)
8371 if (mp
->b_band
== 0)
8372 retevents
|= normevents
;
8374 retevents
|= (events
& (POLLIN
| POLLRDBAND
));
8377 if (!(retevents
& normevents
) && (stp
->sd_wakeq
& RSLEEP
)) {
8379 * Sync stream barrier read queue has data.
8381 retevents
|= normevents
;
8383 /* Treat eof as normal data */
8384 if (sd_flags
& STREOF
)
8385 retevents
|= normevents
;
8389 * Pass back a pollhead if no events are pending or if edge-triggering
8390 * has been configured on this resource.
8392 if ((retevents
== 0 && !anyyet
) || (events
& POLLET
)) {
8393 *phpp
= &stp
->sd_pollist
;
8394 if (headlocked
== 0) {
8395 if (polllock(&stp
->sd_pollist
, &stp
->sd_lock
) != 0) {
8396 *reventsp
= POLLNVAL
;
8401 stp
->sd_rput_opt
|= SR_POLLIN
;
8404 *reventsp
= (short)retevents
;
8406 mutex_exit(&stp
->sd_lock
);
8411 * The purpose of putback() is to assure sleeping polls/reads
8412 * are awakened when there are no new messages arriving at the,
8413 * stream head, and a message is placed back on the read queue.
8415 * sd_lock must be held when messages are placed back on stream
8416 * head. (getq() holds sd_lock when it removes messages from
8421 putback(struct stdata
*stp
, queue_t
*q
, mblk_t
*bp
, int band
)
8424 ASSERT(MUTEX_HELD(&stp
->sd_lock
));
8427 * As a result of lock-step ordering around q_lock and sd_lock,
8428 * it's possible for function calls like putnext() and
8429 * canputnext() to get an inaccurate picture of how much
8430 * data is really being processed at the stream head.
8431 * We only consolidate with existing messages on the queue
8432 * if the length of the message we want to put back is smaller
8433 * than the queue hiwater mark.
8435 if ((stp
->sd_rput_opt
& SR_CONSOL_DATA
) &&
8436 (DB_TYPE(bp
) == M_DATA
) && ((qfirst
= q
->q_first
) != NULL
) &&
8437 (DB_TYPE(qfirst
) == M_DATA
) &&
8438 ((qfirst
->b_flag
& (MSGMARK
|MSGDELIM
)) == 0) &&
8439 ((bp
->b_flag
& (MSGMARK
|MSGDELIM
|MSGMARKNEXT
)) == 0) &&
8440 (mp_cont_len(bp
, NULL
) < q
->q_hiwat
)) {
8442 * We use the same logic as defined in strrput()
8443 * but in reverse as we are putting back onto the
8444 * queue and want to retain byte ordering.
8445 * Consolidate M_DATA messages with M_DATA ONLY.
8446 * strrput() allows the consolidation of M_DATA onto
8447 * M_PROTO | M_PCPROTO but not the other way round.
8449 * The consolidation does not take place if the message
8450 * we are returning to the queue is marked with either
8451 * of the marks or the delim flag or if q_first
8452 * is marked with MSGMARK. The MSGMARK check is needed to
8453 * handle the odd semantics of MSGMARK where essentially
8454 * the whole message is to be treated as marked.
8455 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from q_first
8456 * to the front of the b_cont chain.
8458 rmvq_noenab(q
, qfirst
);
8461 * The first message in the b_cont list
8462 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
8463 * We need to handle the case where we
8466 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
8467 * 2) a MSGMARKNEXT to a plain message.
8468 * 3) a MSGNOTMARKNEXT to a plain message
8469 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
8472 * Thus we never append a MSGMARKNEXT or
8473 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
8475 if (qfirst
->b_flag
& MSGMARKNEXT
) {
8476 bp
->b_flag
|= MSGMARKNEXT
;
8477 bp
->b_flag
&= ~MSGNOTMARKNEXT
;
8478 qfirst
->b_flag
&= ~MSGMARKNEXT
;
8479 } else if (qfirst
->b_flag
& MSGNOTMARKNEXT
) {
8480 bp
->b_flag
|= MSGNOTMARKNEXT
;
8481 qfirst
->b_flag
&= ~MSGNOTMARKNEXT
;
8486 (void) putbq(q
, bp
);
8489 * A message may have come in when the sd_lock was dropped in the
8490 * calling routine. If this is the case and STR*ATMARK info was
8491 * received, need to move that from the stream head to the q_last
8492 * so that SIOCATMARK can return the proper value.
8494 if (stp
->sd_flag
& (STRATMARK
| STRNOTATMARK
)) {
8495 unsigned short *flagp
= &q
->q_last
->b_flag
;
8496 uint_t b_flag
= (uint_t
)*flagp
;
8498 if (stp
->sd_flag
& STRATMARK
) {
8499 b_flag
&= ~MSGNOTMARKNEXT
;
8500 b_flag
|= MSGMARKNEXT
;
8501 stp
->sd_flag
&= ~STRATMARK
;
8503 b_flag
&= ~MSGMARKNEXT
;
8504 b_flag
|= MSGNOTMARKNEXT
;
8505 stp
->sd_flag
&= ~STRNOTATMARK
;
8507 *flagp
= (unsigned short) b_flag
;
8512 * Make sure that the flags are not messed up.
8517 while (mp
!= NULL
) {
8518 ASSERT((mp
->b_flag
& (MSGMARKNEXT
|MSGNOTMARKNEXT
)) !=
8519 (MSGMARKNEXT
|MSGNOTMARKNEXT
));
8524 if (q
->q_first
== bp
) {
8527 if (stp
->sd_flag
& RSLEEP
) {
8528 stp
->sd_flag
&= ~RSLEEP
;
8529 cv_broadcast(&q
->q_wait
);
8531 if (stp
->sd_flag
& STRPRI
) {
8532 pollevents
= POLLPRI
;
8535 if (!(stp
->sd_rput_opt
& SR_POLLIN
))
8537 stp
->sd_rput_opt
&= ~SR_POLLIN
;
8538 pollevents
= POLLIN
| POLLRDNORM
;
8540 pollevents
= POLLIN
| POLLRDBAND
;
8543 mutex_exit(&stp
->sd_lock
);
8544 pollwakeup(&stp
->sd_pollist
, pollevents
);
8545 mutex_enter(&stp
->sd_lock
);
8550 * Return the held vnode attached to the stream head of a
8552 * It is the responsibility of the calling routine to ensure
8553 * that the queue does not go away (e.g. pop).
8556 strq2vp(queue_t
*qp
)
8559 vp
= STREAM(qp
)->sd_vnode
;
8566 * return the stream head write queue for the given vp
8567 * It is the responsibility of the calling routine to ensure
8568 * that the stream or vnode do not close.
8571 strvp2wq(vnode_t
*vp
)
8573 ASSERT(vp
->v_stream
!= NULL
);
8574 return (vp
->v_stream
->sd_wrq
);
8578 * pollwakeup stream head
8579 * It is the responsibility of the calling routine to ensure
8580 * that the stream or vnode do not close.
8583 strpollwakeup(vnode_t
*vp
, short event
)
8585 ASSERT(vp
->v_stream
);
8586 pollwakeup(&vp
->v_stream
->sd_pollist
, event
);
8590 * Mate the stream heads of two vnodes together. If the two vnodes are the
8591 * same, we just make the write-side point at the read-side -- otherwise,
8592 * we do a full mate. Only works on vnodes associated with streams that are
8593 * still being built and thus have only a stream head.
8596 strmate(vnode_t
*vp1
, vnode_t
*vp2
)
8598 queue_t
*wrq1
= strvp2wq(vp1
);
8599 queue_t
*wrq2
= strvp2wq(vp2
);
8602 * Verify that there are no modules on the stream yet. We also
8603 * rely on the stream head always having a service procedure to
8604 * avoid tweaking q_nfsrv.
8606 ASSERT(wrq1
->q_next
== NULL
&& wrq2
->q_next
== NULL
);
8607 ASSERT(wrq1
->q_qinfo
->qi_srvp
!= NULL
);
8608 ASSERT(wrq2
->q_qinfo
->qi_srvp
!= NULL
);
8611 * If the queues are the same, just twist; otherwise do a full mate.
8614 wrq1
->q_next
= _RD(wrq1
);
8616 wrq1
->q_next
= _RD(wrq2
);
8617 wrq2
->q_next
= _RD(wrq1
);
8618 STREAM(wrq1
)->sd_mate
= STREAM(wrq2
);
8619 STREAM(wrq1
)->sd_flag
|= STRMATE
;
8620 STREAM(wrq2
)->sd_mate
= STREAM(wrq1
);
8621 STREAM(wrq2
)->sd_flag
|= STRMATE
;
8626 * XXX will go away when console is correctly fixed.
8627 * Clean up the console PIDS, from previous I_SETSIG,
8628 * called only for cnopen which never calls strclean().
8631 str_cn_clean(struct vnode
*vp
)
8633 strsig_t
*ssp
, *pssp
, *tssp
;
8638 ASSERT(vp
->v_stream
);
8641 mutex_enter(&stp
->sd_lock
);
8642 ssp
= stp
->sd_siglist
;
8644 mutex_enter(&pidlock
);
8645 pidp
= ssp
->ss_pidp
;
8647 * Get rid of PID if the proc is gone.
8649 if (pidp
->pid_prinactive
) {
8650 tssp
= ssp
->ss_next
;
8652 pssp
->ss_next
= tssp
;
8654 stp
->sd_siglist
= tssp
;
8655 ASSERT(pidp
->pid_ref
<= 1);
8656 PID_RELE(ssp
->ss_pidp
);
8657 mutex_exit(&pidlock
);
8658 kmem_free(ssp
, sizeof (strsig_t
));
8663 mutex_exit(&pidlock
);
8668 stp
->sd_sigflags
= 0;
8669 for (ssp
= stp
->sd_siglist
; ssp
; ssp
= ssp
->ss_next
)
8670 stp
->sd_sigflags
|= ssp
->ss_events
;
8672 mutex_exit(&stp
->sd_lock
);
8676 * Return B_TRUE if there is data in the message, B_FALSE otherwise.
8679 msghasdata(mblk_t
*bp
)
8681 for (; bp
; bp
= bp
->b_cont
)
8682 if (bp
->b_datap
->db_type
== M_DATA
) {
8683 ASSERT(bp
->b_wptr
>= bp
->b_rptr
);
8684 if (bp
->b_wptr
> bp
->b_rptr
)