Merge illumos-gate
[unleashed.git] / kernel / os / streamio.c
blob6909d3b405ad374d8ed853b9369e2e310c9f9485
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 /* All Rights Reserved */
26 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
27 * Copyright 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>
36 #include <sys/stat.h>
37 #include <sys/proc.h>
38 #include <sys/cred.h>
39 #include <sys/user.h>
40 #include <sys/vnode.h>
41 #include <sys/file.h>
42 #include <sys/stream.h>
43 #include <sys/strsubr.h>
44 #include <sys/stropts.h>
45 #include <sys/tihdr.h>
46 #include <sys/var.h>
47 #include <sys/poll.h>
48 #include <sys/termio.h>
49 #include <sys/ttold.h>
50 #include <sys/systm.h>
51 #include <sys/uio.h>
52 #include <sys/cmn_err.h>
53 #include <sys/sad.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>
59 #include <sys/kmem.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>
69 #include <sys/kbio.h>
70 #include <sys/msio.h>
71 #include <sys/tty.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>
79 #include <sys/dld.h>
80 #include <sys/zone.h>
81 #include <sys/limits.h>
84 * This define helps improve the readability of streams code while
85 * still maintaining a very old streams performance enhancement. The
86 * performance enhancement basically involved having all callers
87 * of straccess() perform the first check that straccess() will do
88 * locally before actually calling straccess(). (There by reducing
89 * the number of unnecessary calls to straccess().)
91 #define i_straccess(x, y) ((stp->sd_sidp == NULL) ? 0 : \
92 (stp->sd_vnode->v_type == VFIFO) ? 0 : \
93 straccess((x), (y)))
96 * what is mblk_pull_len?
98 * If a streams message consists of many short messages,
99 * a performance degradation occurs from copyout overhead.
100 * To decrease the per mblk overhead, messages that are
101 * likely to consist of many small mblks are pulled up into
102 * one continuous chunk of memory.
104 * To avoid the processing overhead of examining every
105 * mblk, a quick heuristic is used. If the first mblk in
106 * the message is shorter than mblk_pull_len, it is likely
107 * that the rest of the mblk will be short.
109 * This heuristic was decided upon after performance tests
110 * indicated that anything more complex slowed down the main
111 * code path.
113 #define MBLK_PULL_LEN 64
114 uint32_t mblk_pull_len = MBLK_PULL_LEN;
117 * The sgttyb_handling flag controls the handling of the old BSD
118 * TIOCGETP, TIOCSETP, and TIOCSETN ioctls as follows:
120 * 0 - Emit no warnings at all and retain old, broken behavior.
121 * 1 - Emit no warnings and silently handle new semantics.
122 * 2 - Send cmn_err(CE_NOTE) when either TIOCSETP or TIOCSETN is used
123 * (once per system invocation). Handle with new semantics.
124 * 3 - Send SIGSYS when any TIOCGETP, TIOCSETP, or TIOCSETN call is
125 * made (so that offenders drop core and are easy to debug).
127 * The "new semantics" are that TIOCGETP returns B38400 for
128 * sg_[io]speed if the corresponding value is over B38400, and that
129 * TIOCSET[PN] accept B38400 in these cases to mean "retain current
130 * bit rate."
132 int sgttyb_handling = 1;
133 static boolean_t sgttyb_complaint;
135 /* don't push drcompat module by default on Style-2 streams */
136 static int push_drcompat = 0;
139 * id value used to distinguish between different ioctl messages
141 static uint32_t ioc_id;
143 static void putback(struct stdata *, queue_t *, mblk_t *, int);
144 static void strcleanall(struct vnode *);
145 static int strwsrv(queue_t *);
146 static int strdocmd(struct stdata *, struct strcmd *, cred_t *);
149 * qinit and module_info structures for stream head read and write queues
151 struct module_info strm_info = { 0, "strrhead", 0, INFPSZ, STRHIGH, STRLOW };
152 struct module_info stwm_info = { 0, "strwhead", 0, 0, 0, 0 };
153 struct qinit strdata = { strrput, NULL, NULL, NULL, NULL, &strm_info };
154 struct qinit stwdata = { NULL, strwsrv, NULL, NULL, NULL, &stwm_info };
155 struct module_info fiform_info = { 0, "fifostrrhead", 0, PIPE_BUF, FIFOHIWAT,
156 FIFOLOWAT };
157 struct module_info fifowm_info = { 0, "fifostrwhead", 0, 0, 0, 0 };
158 struct qinit fifo_strdata = { strrput, NULL, NULL, NULL, NULL, &fiform_info };
159 struct qinit fifo_stwdata = { NULL, strwsrv, NULL, NULL, NULL, &fifowm_info };
161 extern kmutex_t strresources; /* protects global resources */
162 extern kmutex_t muxifier; /* single-threads multiplexor creation */
164 static boolean_t msghasdata(mblk_t *bp);
165 #define msgnodata(bp) (!msghasdata(bp))
168 * Stream head locking notes:
169 * There are four monitors associated with the stream head:
170 * 1. v_stream monitor: in stropen() and strclose() v_lock
171 * is held while the association of vnode and stream
172 * head is established or tested for.
173 * 2. open/close/push/pop monitor: sd_lock is held while each
174 * thread bids for exclusive access to this monitor
175 * for opening or closing a stream. In addition, this
176 * monitor is entered during pushes and pops. This
177 * guarantees that during plumbing operations there
178 * is only one thread trying to change the plumbing.
179 * Any other threads present in the stream are only
180 * using the plumbing.
181 * 3. read/write monitor: in the case of read, a thread holds
182 * sd_lock while trying to get data from the stream
183 * head queue. if there is none to fulfill a read
184 * request, it sets RSLEEP and calls cv_wait_sig() down
185 * in strwaitq() to await the arrival of new data.
186 * when new data arrives in strrput(), sd_lock is acquired
187 * before testing for RSLEEP and calling cv_broadcast().
188 * the behavior of strwrite(), strwsrv(), and WSLEEP
189 * mirror this.
190 * 4. ioctl monitor: sd_lock is gotten to ensure that only one
191 * thread is doing an ioctl at a time.
194 static int
195 push_mod(queue_t *qp, dev_t *devp, struct stdata *stp, const char *name,
196 int anchor, cred_t *crp, uint_t anchor_zoneid)
198 int error;
199 fmodsw_impl_t *fp;
201 if (stp->sd_flag & (STRHUP|STRDERR|STWRERR)) {
202 error = (stp->sd_flag & STRHUP) ? ENXIO : EIO;
203 return (error);
205 if (stp->sd_pushcnt >= nstrpush) {
206 return (EINVAL);
209 if ((fp = fmodsw_find(name, FMODSW_HOLD | FMODSW_LOAD)) == NULL) {
210 stp->sd_flag |= STREOPENFAIL;
211 return (EINVAL);
215 * push new module and call its open routine via qattach
217 if ((error = qattach(qp, devp, 0, crp, fp, B_FALSE)) != 0)
218 return (error);
221 * Check to see if caller wants a STREAMS anchor
222 * put at this place in the stream, and add if so.
224 mutex_enter(&stp->sd_lock);
225 if (anchor == stp->sd_pushcnt) {
226 stp->sd_anchor = stp->sd_pushcnt;
227 stp->sd_anchorzone = anchor_zoneid;
229 mutex_exit(&stp->sd_lock);
231 return (0);
235 * Open a stream device.
238 stropen(vnode_t *vp, dev_t *devp, int flag, cred_t *crp)
240 struct stdata *stp;
241 queue_t *qp;
242 int s;
243 dev_t dummydev, savedev;
244 struct autopush *ap;
245 struct dlautopush dlap;
246 int error = 0;
247 ssize_t rmin, rmax;
248 int cloneopen;
249 queue_t *brq;
250 major_t major;
251 str_stack_t *ss;
252 zoneid_t zoneid;
253 uint_t anchor;
256 * If the stream already exists, wait for any open in progress
257 * to complete, then call the open function of each module and
258 * driver in the stream. Otherwise create the stream.
260 TRACE_1(TR_FAC_STREAMS_FR, TR_STROPEN, "stropen:%p", vp);
261 retry:
262 mutex_enter(&vp->v_lock);
263 if ((stp = vp->v_stream) != NULL) {
266 * Waiting for stream to be created to device
267 * due to another open.
269 mutex_exit(&vp->v_lock);
271 if (STRMATED(stp)) {
272 struct stdata *strmatep = stp->sd_mate;
274 STRLOCKMATES(stp);
275 if (strmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
276 if (flag & (FNDELAY|FNONBLOCK)) {
277 error = EAGAIN;
278 mutex_exit(&strmatep->sd_lock);
279 goto ckreturn;
281 mutex_exit(&stp->sd_lock);
282 if (!cv_wait_sig(&strmatep->sd_monitor,
283 &strmatep->sd_lock)) {
284 error = EINTR;
285 mutex_exit(&strmatep->sd_lock);
286 mutex_enter(&stp->sd_lock);
287 goto ckreturn;
289 mutex_exit(&strmatep->sd_lock);
290 goto retry;
292 if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
293 if (flag & (FNDELAY|FNONBLOCK)) {
294 error = EAGAIN;
295 mutex_exit(&strmatep->sd_lock);
296 goto ckreturn;
298 mutex_exit(&strmatep->sd_lock);
299 if (!cv_wait_sig(&stp->sd_monitor,
300 &stp->sd_lock)) {
301 error = EINTR;
302 goto ckreturn;
304 mutex_exit(&stp->sd_lock);
305 goto retry;
308 if (stp->sd_flag & (STRDERR|STWRERR)) {
309 error = EIO;
310 mutex_exit(&strmatep->sd_lock);
311 goto ckreturn;
314 stp->sd_flag |= STWOPEN;
315 STRUNLOCKMATES(stp);
316 } else {
317 mutex_enter(&stp->sd_lock);
318 if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
319 if (flag & (FNDELAY|FNONBLOCK)) {
320 error = EAGAIN;
321 goto ckreturn;
323 if (!cv_wait_sig(&stp->sd_monitor,
324 &stp->sd_lock)) {
325 error = EINTR;
326 goto ckreturn;
328 mutex_exit(&stp->sd_lock);
329 goto retry; /* could be clone! */
332 if (stp->sd_flag & (STRDERR|STWRERR)) {
333 error = EIO;
334 goto ckreturn;
337 stp->sd_flag |= STWOPEN;
338 mutex_exit(&stp->sd_lock);
342 * Open all modules and devices down stream to notify
343 * that another user is streaming. For modules, set the
344 * last argument to MODOPEN and do not pass any open flags.
345 * Ignore dummydev since this is not the first open.
347 claimstr(stp->sd_wrq);
348 qp = stp->sd_wrq;
349 while (_SAMESTR(qp)) {
350 qp = qp->q_next;
351 if ((error = qreopen(_RD(qp), devp, flag, crp)) != 0)
352 break;
354 releasestr(stp->sd_wrq);
355 mutex_enter(&stp->sd_lock);
356 stp->sd_flag &= ~(STRHUP|STWOPEN|STRDERR|STWRERR);
357 stp->sd_rerror = 0;
358 stp->sd_werror = 0;
359 ckreturn:
360 cv_broadcast(&stp->sd_monitor);
361 mutex_exit(&stp->sd_lock);
362 return (error);
366 * This vnode isn't streaming. SPECFS already
367 * checked for multiple vnodes pointing to the
368 * same stream, so create a stream to the driver.
370 qp = allocq();
371 stp = shalloc(qp);
374 * Initialize stream head. shalloc() has given us
375 * exclusive access, and we have the vnode locked;
376 * we can do whatever we want with stp.
378 stp->sd_flag = STWOPEN;
379 stp->sd_siglist = NULL;
380 stp->sd_pollist.ph_list = NULL;
381 stp->sd_sigflags = 0;
382 stp->sd_mark = NULL;
383 stp->sd_closetime = STRTIMOUT;
384 stp->sd_sidp = NULL;
385 stp->sd_pgidp = NULL;
386 stp->sd_vnode = vp;
387 stp->sd_rerror = 0;
388 stp->sd_werror = 0;
389 stp->sd_wroff = 0;
390 stp->sd_tail = 0;
391 stp->sd_iocblk = NULL;
392 stp->sd_cmdblk = NULL;
393 stp->sd_pushcnt = 0;
394 stp->sd_qn_minpsz = 0;
395 stp->sd_qn_maxpsz = INFPSZ - 1; /* used to check for initialization */
396 stp->sd_maxblk = INFPSZ;
397 qp->q_ptr = _WR(qp)->q_ptr = stp;
398 STREAM(qp) = STREAM(_WR(qp)) = stp;
399 vp->v_stream = stp;
400 mutex_exit(&vp->v_lock);
401 if (vp->v_type == VFIFO) {
402 stp->sd_flag |= OLDNDELAY;
404 * This means, both for pipes and fifos
405 * strwrite will send SIGPIPE if the other
406 * end is closed. For putmsg it depends
407 * on whether it is a XPG4_2 application
408 * or not
410 stp->sd_wput_opt = SW_SIGPIPE;
412 /* setq might sleep in kmem_alloc - avoid holding locks. */
413 setq(qp, &fifo_strdata, &fifo_stwdata, NULL, QMTSAFE,
414 SQ_CI|SQ_CO, B_FALSE);
416 set_qend(qp);
417 stp->sd_strtab = fifo_getinfo();
418 _WR(qp)->q_nfsrv = _WR(qp);
419 qp->q_nfsrv = qp;
421 * Wake up others that are waiting for stream to be created.
423 mutex_enter(&stp->sd_lock);
425 * nothing is be pushed on stream yet, so
426 * optimized stream head packetsizes are just that
427 * of the read queue
429 stp->sd_qn_minpsz = qp->q_minpsz;
430 stp->sd_qn_maxpsz = qp->q_maxpsz;
431 stp->sd_flag &= ~STWOPEN;
432 goto fifo_opendone;
434 /* setq might sleep in kmem_alloc - avoid holding locks. */
435 setq(qp, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_FALSE);
437 set_qend(qp);
440 * Open driver and create stream to it (via qattach).
442 savedev = *devp;
443 cloneopen = (getmajor(*devp) == clone_major);
444 if ((error = qattach(qp, devp, flag, crp, NULL, B_FALSE)) != 0) {
445 mutex_enter(&vp->v_lock);
446 vp->v_stream = NULL;
447 mutex_exit(&vp->v_lock);
448 mutex_enter(&stp->sd_lock);
449 cv_broadcast(&stp->sd_monitor);
450 mutex_exit(&stp->sd_lock);
451 freeq(_RD(qp));
452 shfree(stp);
453 return (error);
456 * Set sd_strtab after open in order to handle clonable drivers
458 stp->sd_strtab = STREAMSTAB(getmajor(*devp));
461 * Historical note: dummydev used to be be prior to the initial
462 * open (via qattach above), which made the value seen
463 * inconsistent between an I_PUSH and an autopush of a module.
465 dummydev = *devp;
468 * For clone open of old style (Q not associated) network driver,
469 * push DRMODNAME module to handle DL_ATTACH/DL_DETACH
471 brq = _RD(_WR(qp)->q_next);
472 major = getmajor(*devp);
473 if (push_drcompat && cloneopen && NETWORK_DRV(major) &&
474 ((brq->q_flag & _QASSOCIATED) == 0)) {
475 if (push_mod(qp, &dummydev, stp, DRMODNAME, 0, crp, 0) != 0)
476 cmn_err(CE_WARN, "cannot push " DRMODNAME
477 " streams module");
480 if (!NETWORK_DRV(major)) {
481 savedev = *devp;
482 } else {
484 * For network devices, process differently based on the
485 * return value from dld_autopush():
487 * 0: the passed-in device points to a GLDv3 datalink with
488 * per-link autopush configuration; use that configuration
489 * and ignore any per-driver autopush configuration.
491 * 1: the passed-in device points to a physical GLDv3
492 * datalink without per-link autopush configuration. The
493 * passed in device was changed to refer to the actual
494 * physical device (if it's not already); we use that new
495 * device to look up any per-driver autopush configuration.
497 * -1: neither of the above cases applied; use the initial
498 * device to look up any per-driver autopush configuration.
500 switch (dld_autopush(&savedev, &dlap)) {
501 case 0:
502 zoneid = crgetzoneid(crp);
503 for (s = 0; s < dlap.dap_npush; s++) {
504 error = push_mod(qp, &dummydev, stp,
505 dlap.dap_aplist[s], dlap.dap_anchor, crp,
506 zoneid);
507 if (error != 0)
508 break;
510 goto opendone;
511 case 1:
512 break;
513 case -1:
514 savedev = *devp;
515 break;
519 * Find the autopush configuration based on "savedev". Start with the
520 * global zone. If not found check in the local zone.
522 zoneid = GLOBAL_ZONEID;
523 retryap:
524 ss = netstack_find_by_stackid(zoneid_to_netstackid(zoneid))->
525 netstack_str;
526 if ((ap = sad_ap_find_by_dev(savedev, ss)) == NULL) {
527 netstack_rele(ss->ss_netstack);
528 if (zoneid == GLOBAL_ZONEID) {
530 * None found. Also look in the zone's autopush table.
532 zoneid = crgetzoneid(crp);
533 if (zoneid != GLOBAL_ZONEID)
534 goto retryap;
536 goto opendone;
538 anchor = ap->ap_anchor;
539 zoneid = crgetzoneid(crp);
540 for (s = 0; s < ap->ap_npush; s++) {
541 error = push_mod(qp, &dummydev, stp, ap->ap_list[s],
542 anchor, crp, zoneid);
543 if (error != 0)
544 break;
546 sad_ap_rele(ap, ss);
547 netstack_rele(ss->ss_netstack);
549 opendone:
552 * let specfs know that open failed part way through
554 if (error) {
555 mutex_enter(&stp->sd_lock);
556 stp->sd_flag |= STREOPENFAIL;
557 mutex_exit(&stp->sd_lock);
561 * Wake up others that are waiting for stream to be created.
563 mutex_enter(&stp->sd_lock);
564 stp->sd_flag &= ~STWOPEN;
567 * As a performance concern we are caching the values of
568 * q_minpsz and q_maxpsz of the module below the stream
569 * head in the stream head.
571 mutex_enter(QLOCK(stp->sd_wrq->q_next));
572 rmin = stp->sd_wrq->q_next->q_minpsz;
573 rmax = stp->sd_wrq->q_next->q_maxpsz;
574 mutex_exit(QLOCK(stp->sd_wrq->q_next));
576 /* do this processing here as a performance concern */
577 if (strmsgsz != 0) {
578 if (rmax == INFPSZ)
579 rmax = strmsgsz;
580 else
581 rmax = MIN(strmsgsz, rmax);
584 mutex_enter(QLOCK(stp->sd_wrq));
585 stp->sd_qn_minpsz = rmin;
586 stp->sd_qn_maxpsz = rmax;
587 mutex_exit(QLOCK(stp->sd_wrq));
589 fifo_opendone:
590 cv_broadcast(&stp->sd_monitor);
591 mutex_exit(&stp->sd_lock);
592 return (error);
595 static int strsink(queue_t *, mblk_t *);
596 static struct qinit deadrend = {
597 strsink, NULL, NULL, NULL, NULL, &strm_info, NULL
599 static struct qinit deadwend = {
600 NULL, NULL, NULL, NULL, NULL, &stwm_info, NULL
604 * Close a stream.
605 * This is called from closef() on the last close of an open stream.
606 * Strclean() will already have removed the siglist and pollist
607 * information, so all that remains is to remove all multiplexor links
608 * for the stream, pop all the modules (and the driver), and free the
609 * stream structure.
613 strclose(struct vnode *vp, int flag, cred_t *crp)
615 struct stdata *stp;
616 queue_t *qp;
617 int rval;
618 int freestp = 1;
619 queue_t *rmq;
621 TRACE_1(TR_FAC_STREAMS_FR,
622 TR_STRCLOSE, "strclose:%p", vp);
623 ASSERT(vp->v_stream);
625 stp = vp->v_stream;
626 ASSERT(!(stp->sd_flag & STPLEX));
627 qp = stp->sd_wrq;
630 * Needed so that strpoll will return non-zero for this fd.
631 * Note that with POLLNOERR STRHUP does still cause POLLHUP.
633 mutex_enter(&stp->sd_lock);
634 stp->sd_flag |= STRHUP;
635 mutex_exit(&stp->sd_lock);
638 * If the registered process or process group did not have an
639 * open instance of this stream then strclean would not be
640 * called. Thus at the time of closing all remaining siglist entries
641 * are removed.
643 if (stp->sd_siglist != NULL)
644 strcleanall(vp);
646 ASSERT(stp->sd_siglist == NULL);
647 ASSERT(stp->sd_sigflags == 0);
649 if (STRMATED(stp)) {
650 struct stdata *strmatep = stp->sd_mate;
651 int waited = 1;
653 STRLOCKMATES(stp);
654 while (waited) {
655 waited = 0;
656 while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
657 mutex_exit(&strmatep->sd_lock);
658 cv_wait(&stp->sd_monitor, &stp->sd_lock);
659 mutex_exit(&stp->sd_lock);
660 STRLOCKMATES(stp);
661 waited = 1;
663 while (strmatep->sd_flag &
664 (STWOPEN|STRCLOSE|STRPLUMB)) {
665 mutex_exit(&stp->sd_lock);
666 cv_wait(&strmatep->sd_monitor,
667 &strmatep->sd_lock);
668 mutex_exit(&strmatep->sd_lock);
669 STRLOCKMATES(stp);
670 waited = 1;
673 stp->sd_flag |= STRCLOSE;
674 STRUNLOCKMATES(stp);
675 } else {
676 mutex_enter(&stp->sd_lock);
677 stp->sd_flag |= STRCLOSE;
678 mutex_exit(&stp->sd_lock);
681 ASSERT(qp->q_first == NULL); /* No more delayed write */
683 /* Check if an I_LINK was ever done on this stream */
684 if (stp->sd_flag & STRHASLINKS) {
685 netstack_t *ns;
686 str_stack_t *ss;
688 ns = netstack_find_by_cred(crp);
689 ASSERT(ns != NULL);
690 ss = ns->netstack_str;
691 ASSERT(ss != NULL);
693 (void) munlinkall(stp, LINKCLOSE|LINKNORMAL, crp, &rval, ss);
694 netstack_rele(ss->ss_netstack);
697 while (_SAMESTR(qp)) {
699 * Holding sd_lock prevents q_next from changing in
700 * this stream.
702 mutex_enter(&stp->sd_lock);
703 if (!(flag & (FNDELAY|FNONBLOCK)) && (stp->sd_closetime > 0)) {
706 * sleep until awakened by strwsrv() or timeout
708 for (;;) {
709 mutex_enter(QLOCK(qp->q_next));
710 if (!(qp->q_next->q_mblkcnt)) {
711 mutex_exit(QLOCK(qp->q_next));
712 break;
714 stp->sd_flag |= WSLEEP;
716 /* ensure strwsrv gets enabled */
717 qp->q_next->q_flag |= QWANTW;
718 mutex_exit(QLOCK(qp->q_next));
719 /* get out if we timed out or recv'd a signal */
720 if (str_cv_wait(&qp->q_wait, &stp->sd_lock,
721 stp->sd_closetime, 0) <= 0) {
722 break;
725 stp->sd_flag &= ~WSLEEP;
727 mutex_exit(&stp->sd_lock);
729 rmq = qp->q_next;
730 if (rmq->q_flag & QISDRV) {
731 ASSERT(!_SAMESTR(rmq));
732 wait_sq_svc(_RD(qp)->q_syncq);
735 qdetach(_RD(rmq), 1, flag, crp, B_FALSE);
739 * Since we call pollwakeup in close() now, the poll list should
740 * be empty in most cases. The only exception is the layered devices
741 * (e.g. the console drivers with redirection modules pushed on top
742 * of it). We have to do this after calling qdetach() because
743 * the redirection module won't have torn down the console
744 * redirection until after qdetach() has been invoked.
746 if (stp->sd_pollist.ph_list != NULL) {
747 pollwakeup(&stp->sd_pollist, POLLERR);
748 pollhead_clean(&stp->sd_pollist);
750 ASSERT(stp->sd_pollist.ph_list == NULL);
751 ASSERT(stp->sd_sidp == NULL);
752 ASSERT(stp->sd_pgidp == NULL);
754 /* Prevent qenable from re-enabling the stream head queue */
755 disable_svc(_RD(qp));
758 * Wait until service procedure of each queue is
759 * run, if QINSERVICE is set.
761 wait_svc(_RD(qp));
764 * Now, flush both queues.
766 flushq(_RD(qp), FLUSHALL);
767 flushq(qp, FLUSHALL);
770 * If the write queue of the stream head is pointing to a
771 * read queue, we have a twisted stream. If the read queue
772 * is alive, convert the stream head queues into a dead end.
773 * If the read queue is dead, free the dead pair.
775 if (qp->q_next && !_SAMESTR(qp)) {
776 if (qp->q_next->q_qinfo == &deadrend) { /* half-closed pipe */
777 flushq(qp->q_next, FLUSHALL); /* ensure no message */
778 shfree(qp->q_next->q_stream);
779 freeq(qp->q_next);
780 freeq(_RD(qp));
781 } else if (qp->q_next == _RD(qp)) { /* fifo */
782 freeq(_RD(qp));
783 } else { /* pipe */
784 freestp = 0;
786 * The q_info pointers are never accessed when
787 * SQLOCK is held.
789 ASSERT(qp->q_syncq == _RD(qp)->q_syncq);
790 mutex_enter(SQLOCK(qp->q_syncq));
791 qp->q_qinfo = &deadwend;
792 _RD(qp)->q_qinfo = &deadrend;
793 mutex_exit(SQLOCK(qp->q_syncq));
795 } else {
796 freeq(_RD(qp)); /* free stream head queue pair */
799 mutex_enter(&vp->v_lock);
800 if (stp->sd_iocblk) {
801 if (stp->sd_iocblk != (mblk_t *)-1) {
802 freemsg(stp->sd_iocblk);
804 stp->sd_iocblk = NULL;
806 stp->sd_vnode = NULL;
807 vp->v_stream = NULL;
808 mutex_exit(&vp->v_lock);
809 mutex_enter(&stp->sd_lock);
810 freemsg(stp->sd_cmdblk);
811 stp->sd_cmdblk = NULL;
812 stp->sd_flag &= ~STRCLOSE;
813 cv_broadcast(&stp->sd_monitor);
814 mutex_exit(&stp->sd_lock);
816 if (freestp)
817 shfree(stp);
818 return (0);
821 static int
822 strsink(queue_t *q, mblk_t *bp)
824 struct copyresp *resp;
826 switch (bp->b_datap->db_type) {
827 case M_FLUSH:
828 if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
829 *bp->b_rptr &= ~FLUSHR;
830 bp->b_flag |= MSGNOLOOP;
832 * Protect against the driver passing up
833 * messages after it has done a qprocsoff.
835 if (_OTHERQ(q)->q_next == NULL)
836 freemsg(bp);
837 else
838 qreply(q, bp);
839 } else {
840 freemsg(bp);
842 break;
844 case M_COPYIN:
845 case M_COPYOUT:
846 if (bp->b_cont) {
847 freemsg(bp->b_cont);
848 bp->b_cont = NULL;
850 bp->b_datap->db_type = M_IOCDATA;
851 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
852 resp = (struct copyresp *)bp->b_rptr;
853 resp->cp_rval = (caddr_t)1; /* failure */
855 * Protect against the driver passing up
856 * messages after it has done a qprocsoff.
858 if (_OTHERQ(q)->q_next == NULL)
859 freemsg(bp);
860 else
861 qreply(q, bp);
862 break;
864 case M_IOCTL:
865 if (bp->b_cont) {
866 freemsg(bp->b_cont);
867 bp->b_cont = NULL;
869 bp->b_datap->db_type = M_IOCNAK;
871 * Protect against the driver passing up
872 * messages after it has done a qprocsoff.
874 if (_OTHERQ(q)->q_next == NULL)
875 freemsg(bp);
876 else
877 qreply(q, bp);
878 break;
880 default:
881 freemsg(bp);
882 break;
885 return (0);
889 * Clean up after a process when it closes a stream. This is called
890 * from closef for all closes, whereas strclose is called only for the
891 * last close on a stream. The siglist is scanned for entries for the
892 * current process, and these are removed.
894 void
895 strclean(struct vnode *vp)
897 strsig_t *ssp, *pssp, *tssp;
898 stdata_t *stp;
899 int update = 0;
901 TRACE_1(TR_FAC_STREAMS_FR,
902 TR_STRCLEAN, "strclean:%p", vp);
903 stp = vp->v_stream;
904 pssp = NULL;
905 mutex_enter(&stp->sd_lock);
906 ssp = stp->sd_siglist;
907 while (ssp) {
908 if (ssp->ss_pidp == curproc->p_pidp) {
909 tssp = ssp->ss_next;
910 if (pssp)
911 pssp->ss_next = tssp;
912 else
913 stp->sd_siglist = tssp;
914 mutex_enter(&pidlock);
915 PID_RELE(ssp->ss_pidp);
916 mutex_exit(&pidlock);
917 kmem_free(ssp, sizeof (strsig_t));
918 update = 1;
919 ssp = tssp;
920 } else {
921 pssp = ssp;
922 ssp = ssp->ss_next;
925 if (update) {
926 stp->sd_sigflags = 0;
927 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
928 stp->sd_sigflags |= ssp->ss_events;
930 mutex_exit(&stp->sd_lock);
934 * Used on the last close to remove any remaining items on the siglist.
935 * These could be present on the siglist due to I_ESETSIG calls that
936 * use process groups or processed that do not have an open file descriptor
937 * for this stream (Such entries would not be removed by strclean).
939 static void
940 strcleanall(struct vnode *vp)
942 strsig_t *ssp, *nssp;
943 stdata_t *stp;
945 stp = vp->v_stream;
946 mutex_enter(&stp->sd_lock);
947 ssp = stp->sd_siglist;
948 stp->sd_siglist = NULL;
949 while (ssp) {
950 nssp = ssp->ss_next;
951 mutex_enter(&pidlock);
952 PID_RELE(ssp->ss_pidp);
953 mutex_exit(&pidlock);
954 kmem_free(ssp, sizeof (strsig_t));
955 ssp = nssp;
957 stp->sd_sigflags = 0;
958 mutex_exit(&stp->sd_lock);
962 * Retrieve the next message from the logical stream head read queue
963 * using either rwnext (if sync stream) or getq_noenab.
964 * It is the callers responsibility to call qbackenable after
965 * it is finished with the message. The caller should not call
966 * qbackenable until after any putback calls to avoid spurious backenabling.
968 mblk_t *
969 strget(struct stdata *stp, queue_t *q, struct uio *uiop, int first,
970 int *errorp)
972 mblk_t *bp;
973 int error;
974 ssize_t rbytes = 0;
976 /* Holding sd_lock prevents the read queue from changing */
977 ASSERT(MUTEX_HELD(&stp->sd_lock));
979 if (uiop != NULL && stp->sd_struiordq != NULL &&
980 q->q_first == NULL &&
981 (!first || (stp->sd_wakeq & RSLEEP))) {
983 * Stream supports rwnext() for the read side.
984 * If this is the first time we're called by e.g. strread
985 * only do the downcall if there is a deferred wakeup
986 * (registered in sd_wakeq).
988 struiod_t uiod;
989 struct iovec buf[IOV_MAX_STACK];
990 int iovlen = 0;
992 if (first)
993 stp->sd_wakeq &= ~RSLEEP;
995 if (uiop->uio_iovcnt > IOV_MAX_STACK) {
996 iovlen = uiop->uio_iovcnt * sizeof (iovec_t);
997 uiod.d_iov = kmem_alloc(iovlen, KM_SLEEP);
998 } else {
999 uiod.d_iov = buf;
1002 (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov, uiop->uio_iovcnt);
1003 uiod.d_mp = 0;
1005 * Mark that a thread is in rwnext on the read side
1006 * to prevent strrput from nacking ioctls immediately.
1007 * When the last concurrent rwnext returns
1008 * the ioctls are nack'ed.
1010 ASSERT(MUTEX_HELD(&stp->sd_lock));
1011 stp->sd_struiodnak++;
1013 * Note: rwnext will drop sd_lock.
1015 error = rwnext(q, &uiod);
1016 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
1017 mutex_enter(&stp->sd_lock);
1018 stp->sd_struiodnak--;
1019 while (stp->sd_struiodnak == 0 &&
1020 ((bp = stp->sd_struionak) != NULL)) {
1021 stp->sd_struionak = bp->b_next;
1022 bp->b_next = NULL;
1023 bp->b_datap->db_type = M_IOCNAK;
1025 * Protect against the driver passing up
1026 * messages after it has done a qprocsoff.
1028 if (_OTHERQ(q)->q_next == NULL)
1029 freemsg(bp);
1030 else {
1031 mutex_exit(&stp->sd_lock);
1032 qreply(q, bp);
1033 mutex_enter(&stp->sd_lock);
1036 ASSERT(MUTEX_HELD(&stp->sd_lock));
1037 if (error == 0 || error == EWOULDBLOCK) {
1038 if ((bp = uiod.d_mp) != NULL) {
1039 *errorp = 0;
1040 ASSERT(MUTEX_HELD(&stp->sd_lock));
1041 if (iovlen != 0)
1042 kmem_free(uiod.d_iov, iovlen);
1043 return (bp);
1045 error = 0;
1046 } else if (error == EINVAL) {
1048 * The stream plumbing must have
1049 * changed while we were away, so
1050 * just turn off rwnext()s.
1052 error = 0;
1053 } else if (error == EBUSY) {
1055 * The module might have data in transit using putnext
1056 * Fall back on waiting + getq.
1058 error = 0;
1059 } else {
1060 *errorp = error;
1061 ASSERT(MUTEX_HELD(&stp->sd_lock));
1062 if (iovlen != 0)
1063 kmem_free(uiod.d_iov, iovlen);
1064 return (NULL);
1067 if (iovlen != 0)
1068 kmem_free(uiod.d_iov, iovlen);
1071 * Try a getq in case a rwnext() generated mblk
1072 * has bubbled up via strrput().
1075 *errorp = 0;
1076 ASSERT(MUTEX_HELD(&stp->sd_lock));
1079 * If we have a valid uio, try and use this as a guide for how
1080 * many bytes to retrieve from the queue via getq_noenab().
1081 * Doing this can avoid unneccesary counting of overlong
1082 * messages in putback(). We currently only do this for sockets
1083 * and only if there is no sd_rputdatafunc hook.
1085 * The sd_rputdatafunc hook transforms the entire message
1086 * before any bytes in it can be given to a client. So, rbytes
1087 * must be 0 if there is a hook.
1089 if ((uiop != NULL) && (stp->sd_vnode->v_type == VSOCK) &&
1090 (stp->sd_rputdatafunc == NULL))
1091 rbytes = uiop->uio_resid;
1093 return (getq_noenab(q, rbytes));
1097 * Copy out the message pointed to by `bp' into the uio pointed to by `uiop'.
1098 * If the message does not fit in the uio the remainder of it is returned;
1099 * otherwise NULL is returned. Any embedded zero-length mblk_t's are
1100 * consumed, even if uio_resid reaches zero. On error, `*errorp' is set to
1101 * the error code, the message is consumed, and NULL is returned.
1103 static mblk_t *
1104 struiocopyout(mblk_t *bp, struct uio *uiop, int *errorp)
1106 int error;
1107 ptrdiff_t n;
1108 mblk_t *nbp;
1110 ASSERT(bp->b_wptr >= bp->b_rptr);
1112 do {
1113 if ((n = MIN(uiop->uio_resid, MBLKL(bp))) != 0) {
1114 ASSERT(n > 0);
1116 error = uiomove(bp->b_rptr, n, UIO_READ, uiop);
1117 if (error != 0) {
1118 freemsg(bp);
1119 *errorp = error;
1120 return (NULL);
1124 bp->b_rptr += n;
1125 while (bp != NULL && (bp->b_rptr >= bp->b_wptr)) {
1126 nbp = bp;
1127 bp = bp->b_cont;
1128 freeb(nbp);
1130 } while (bp != NULL && uiop->uio_resid > 0);
1132 *errorp = 0;
1133 return (bp);
1137 * Read a stream according to the mode flags in sd_flag:
1139 * (default mode) - Byte stream, msg boundaries are ignored
1140 * RD_MSGDIS (msg discard) - Read on msg boundaries and throw away
1141 * any data remaining in msg
1142 * RD_MSGNODIS (msg non-discard) - Read on msg boundaries and put back
1143 * any remaining data on head of read queue
1145 * Consume readable messages on the front of the queue until
1146 * ttolwp(curthread)->lwp_count
1147 * is satisfied, the readable messages are exhausted, or a message
1148 * boundary is reached in a message mode. If no data was read and
1149 * the stream was not opened with the NDELAY flag, block until data arrives.
1150 * Otherwise return the data read and update the count.
1152 * In default mode a 0 length message signifies end-of-file and terminates
1153 * a read in progress. The 0 length message is removed from the queue
1154 * only if it is the only message read (no data is read).
1156 * An attempt to read an M_PROTO or M_PCPROTO message results in an
1157 * EBADMSG error return, unless either RD_PROTDAT or RD_PROTDIS are set.
1158 * If RD_PROTDAT is set, M_PROTO and M_PCPROTO messages are read as data.
1159 * If RD_PROTDIS is set, the M_PROTO and M_PCPROTO parts of the message
1160 * are unlinked from and M_DATA blocks in the message, the protos are
1161 * thrown away, and the data is read.
1163 /* ARGSUSED */
1165 strread(struct vnode *vp, struct uio *uiop, cred_t *crp)
1167 struct stdata *stp;
1168 mblk_t *bp, *nbp;
1169 queue_t *q;
1170 int error = 0;
1171 uint_t old_sd_flag;
1172 int first;
1173 char rflg;
1174 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
1175 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
1176 short delim;
1177 unsigned char pri = 0;
1178 char waitflag;
1179 unsigned char type;
1181 TRACE_1(TR_FAC_STREAMS_FR,
1182 TR_STRREAD_ENTER, "strread:%p", vp);
1183 ASSERT(vp->v_stream);
1184 stp = vp->v_stream;
1186 mutex_enter(&stp->sd_lock);
1188 if ((error = i_straccess(stp, JCREAD)) != 0) {
1189 mutex_exit(&stp->sd_lock);
1190 return (error);
1193 if (stp->sd_flag & (STRDERR|STPLEX)) {
1194 error = strgeterr(stp, STRDERR|STPLEX, 0);
1195 if (error != 0) {
1196 mutex_exit(&stp->sd_lock);
1197 return (error);
1202 * Loop terminates when uiop->uio_resid == 0.
1204 rflg = 0;
1205 waitflag = READWAIT;
1206 q = _RD(stp->sd_wrq);
1207 for (;;) {
1208 ASSERT(MUTEX_HELD(&stp->sd_lock));
1209 old_sd_flag = stp->sd_flag;
1210 mark = 0;
1211 delim = 0;
1212 first = 1;
1213 while ((bp = strget(stp, q, uiop, first, &error)) == NULL) {
1214 int done = 0;
1216 ASSERT(MUTEX_HELD(&stp->sd_lock));
1218 if (error != 0)
1219 goto oops;
1221 if (stp->sd_flag & (STRHUP|STREOF)) {
1222 goto oops;
1224 if (rflg && !(stp->sd_flag & STRDELIM)) {
1225 goto oops;
1228 * If a read(fd,buf,0) has been done, there is no
1229 * need to sleep. We always have zero bytes to
1230 * return.
1232 if (uiop->uio_resid == 0) {
1233 goto oops;
1236 qbackenable(q, 0);
1238 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_WAIT,
1239 "strread calls strwaitq:%p, %p, %p",
1240 vp, uiop, crp);
1241 if ((error = strwaitq(stp, waitflag, uiop->uio_resid,
1242 uiop->uio_fmode, -1, &done)) != 0 || done) {
1243 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_DONE,
1244 "strread error or done:%p, %p, %p",
1245 vp, uiop, crp);
1246 if ((uiop->uio_fmode & FNDELAY) &&
1247 (stp->sd_flag & OLDNDELAY) &&
1248 (error == EAGAIN))
1249 error = 0;
1250 goto oops;
1252 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_AWAKE,
1253 "strread awakes:%p, %p, %p", vp, uiop, crp);
1254 if ((error = i_straccess(stp, JCREAD)) != 0) {
1255 goto oops;
1257 first = 0;
1260 ASSERT(MUTEX_HELD(&stp->sd_lock));
1261 ASSERT(bp);
1262 pri = bp->b_band;
1264 * Extract any mark information. If the message is not
1265 * completely consumed this information will be put in the mblk
1266 * that is putback.
1267 * If MSGMARKNEXT is set and the message is completely consumed
1268 * the STRATMARK flag will be set below. Likewise, if
1269 * MSGNOTMARKNEXT is set and the message is
1270 * completely consumed STRNOTATMARK will be set.
1272 * For some unknown reason strread only breaks the read at the
1273 * last mark.
1275 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
1276 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
1277 (MSGMARKNEXT|MSGNOTMARKNEXT));
1278 if (mark != 0 && bp == stp->sd_mark) {
1279 if (rflg) {
1280 putback(stp, q, bp, pri);
1281 goto oops;
1283 mark |= _LASTMARK;
1284 stp->sd_mark = NULL;
1286 if ((stp->sd_flag & STRDELIM) && (bp->b_flag & MSGDELIM))
1287 delim = 1;
1288 mutex_exit(&stp->sd_lock);
1290 if (STREAM_NEEDSERVICE(stp))
1291 stream_runservice(stp);
1293 type = bp->b_datap->db_type;
1295 switch (type) {
1297 case M_DATA:
1298 ismdata:
1299 if (msgnodata(bp)) {
1300 if (mark || delim) {
1301 freemsg(bp);
1302 } else if (rflg) {
1305 * If already read data put zero
1306 * length message back on queue else
1307 * free msg and return 0.
1309 bp->b_band = pri;
1310 mutex_enter(&stp->sd_lock);
1311 putback(stp, q, bp, pri);
1312 mutex_exit(&stp->sd_lock);
1313 } else {
1314 freemsg(bp);
1316 error = 0;
1317 goto oops1;
1320 rflg = 1;
1321 waitflag |= NOINTR;
1322 bp = struiocopyout(bp, uiop, &error);
1323 if (error != 0)
1324 goto oops1;
1326 mutex_enter(&stp->sd_lock);
1327 if (bp) {
1329 * Have remaining data in message.
1330 * Free msg if in discard mode.
1332 if (stp->sd_read_opt & RD_MSGDIS) {
1333 freemsg(bp);
1334 } else {
1335 bp->b_band = pri;
1336 if ((mark & _LASTMARK) &&
1337 (stp->sd_mark == NULL))
1338 stp->sd_mark = bp;
1339 bp->b_flag |= mark & ~_LASTMARK;
1340 if (delim)
1341 bp->b_flag |= MSGDELIM;
1342 if (msgnodata(bp))
1343 freemsg(bp);
1344 else
1345 putback(stp, q, bp, pri);
1347 } else {
1349 * Consumed the complete message.
1350 * Move the MSG*MARKNEXT information
1351 * to the stream head just in case
1352 * the read queue becomes empty.
1354 * If the stream head was at the mark
1355 * (STRATMARK) before we dropped sd_lock above
1356 * and some data was consumed then we have
1357 * moved past the mark thus STRATMARK is
1358 * cleared. However, if a message arrived in
1359 * strrput during the copyout above causing
1360 * STRATMARK to be set we can not clear that
1361 * flag.
1363 if (mark &
1364 (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
1365 if (mark & MSGMARKNEXT) {
1366 stp->sd_flag &= ~STRNOTATMARK;
1367 stp->sd_flag |= STRATMARK;
1368 } else if (mark & MSGNOTMARKNEXT) {
1369 stp->sd_flag &= ~STRATMARK;
1370 stp->sd_flag |= STRNOTATMARK;
1371 } else {
1372 stp->sd_flag &=
1373 ~(STRATMARK|STRNOTATMARK);
1375 } else if (rflg && (old_sd_flag & STRATMARK)) {
1376 stp->sd_flag &= ~STRATMARK;
1381 * Check for signal messages at the front of the read
1382 * queue and generate the signal(s) if appropriate.
1383 * The only signal that can be on queue is M_SIG at
1384 * this point.
1386 while ((((bp = q->q_first)) != NULL) &&
1387 (bp->b_datap->db_type == M_SIG)) {
1388 bp = getq_noenab(q, 0);
1390 * sd_lock is held so the content of the
1391 * read queue can not change.
1393 ASSERT(bp != NULL && DB_TYPE(bp) == M_SIG);
1394 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
1395 mutex_exit(&stp->sd_lock);
1396 freemsg(bp);
1397 if (STREAM_NEEDSERVICE(stp))
1398 stream_runservice(stp);
1399 mutex_enter(&stp->sd_lock);
1402 if ((uiop->uio_resid == 0) || (mark & _LASTMARK) ||
1403 delim ||
1404 (stp->sd_read_opt & (RD_MSGDIS|RD_MSGNODIS))) {
1405 goto oops;
1407 continue;
1409 case M_SIG:
1410 strsignal(stp, *bp->b_rptr, (int32_t)bp->b_band);
1411 freemsg(bp);
1412 mutex_enter(&stp->sd_lock);
1413 continue;
1415 case M_PROTO:
1416 case M_PCPROTO:
1418 * Only data messages are readable.
1419 * Any others generate an error, unless
1420 * RD_PROTDIS or RD_PROTDAT is set.
1422 if (stp->sd_read_opt & RD_PROTDAT) {
1423 for (nbp = bp; nbp; nbp = nbp->b_next) {
1424 if ((nbp->b_datap->db_type ==
1425 M_PROTO) ||
1426 (nbp->b_datap->db_type ==
1427 M_PCPROTO)) {
1428 nbp->b_datap->db_type = M_DATA;
1429 } else {
1430 break;
1434 * clear stream head hi pri flag based on
1435 * first message
1437 if (type == M_PCPROTO) {
1438 mutex_enter(&stp->sd_lock);
1439 stp->sd_flag &= ~STRPRI;
1440 mutex_exit(&stp->sd_lock);
1442 goto ismdata;
1443 } else if (stp->sd_read_opt & RD_PROTDIS) {
1445 * discard non-data messages
1447 while (bp &&
1448 ((bp->b_datap->db_type == M_PROTO) ||
1449 (bp->b_datap->db_type == M_PCPROTO))) {
1450 nbp = unlinkb(bp);
1451 freeb(bp);
1452 bp = nbp;
1455 * clear stream head hi pri flag based on
1456 * first message
1458 if (type == M_PCPROTO) {
1459 mutex_enter(&stp->sd_lock);
1460 stp->sd_flag &= ~STRPRI;
1461 mutex_exit(&stp->sd_lock);
1463 if (bp) {
1464 bp->b_band = pri;
1465 goto ismdata;
1466 } else {
1467 break;
1470 /* FALLTHRU */
1471 case M_PASSFP:
1472 if ((bp->b_datap->db_type == M_PASSFP) &&
1473 (stp->sd_read_opt & RD_PROTDIS)) {
1474 freemsg(bp);
1475 break;
1477 mutex_enter(&stp->sd_lock);
1478 putback(stp, q, bp, pri);
1479 mutex_exit(&stp->sd_lock);
1480 if (rflg == 0)
1481 error = EBADMSG;
1482 goto oops1;
1484 default:
1486 * Garbage on stream head read queue.
1488 cmn_err(CE_WARN, "bad %x found at stream head\n",
1489 bp->b_datap->db_type);
1490 freemsg(bp);
1491 goto oops1;
1493 mutex_enter(&stp->sd_lock);
1495 oops:
1496 mutex_exit(&stp->sd_lock);
1497 oops1:
1498 qbackenable(q, pri);
1499 return (error);
1500 #undef _LASTMARK
1504 * Default processing of M_PROTO/M_PCPROTO messages.
1505 * Determine which wakeups and signals are needed.
1506 * This can be replaced by a user-specified procedure for kernel users
1507 * of STREAMS.
1509 /* ARGSUSED */
1510 mblk_t *
1511 strrput_proto(vnode_t *vp, mblk_t *mp,
1512 strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1513 strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1515 *wakeups = RSLEEP;
1516 *allmsgsigs = 0;
1518 switch (mp->b_datap->db_type) {
1519 case M_PROTO:
1520 if (mp->b_band == 0) {
1521 *firstmsgsigs = S_INPUT | S_RDNORM;
1522 *pollwakeups = POLLIN | POLLRDNORM;
1523 } else {
1524 *firstmsgsigs = S_INPUT | S_RDBAND;
1525 *pollwakeups = POLLIN | POLLRDBAND;
1527 break;
1528 case M_PCPROTO:
1529 *firstmsgsigs = S_HIPRI;
1530 *pollwakeups = POLLPRI;
1531 break;
1533 return (mp);
1537 * Default processing of everything but M_DATA, M_PROTO, M_PCPROTO and
1538 * M_PASSFP messages.
1539 * Determine which wakeups and signals are needed.
1540 * This can be replaced by a user-specified procedure for kernel users
1541 * of STREAMS.
1543 /* ARGSUSED */
1544 mblk_t *
1545 strrput_misc(vnode_t *vp, mblk_t *mp,
1546 strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1547 strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1549 *wakeups = 0;
1550 *firstmsgsigs = 0;
1551 *allmsgsigs = 0;
1552 *pollwakeups = 0;
1553 return (mp);
1557 * Stream read put procedure. Called from downstream driver/module
1558 * with messages for the stream head. Data, protocol, and in-stream
1559 * signal messages are placed on the queue, others are handled directly.
1562 strrput(queue_t *q, mblk_t *bp)
1564 struct stdata *stp;
1565 ulong_t rput_opt;
1566 strwakeup_t wakeups;
1567 strsigset_t firstmsgsigs; /* Signals if first message on queue */
1568 strsigset_t allmsgsigs; /* Signals for all messages */
1569 strsigset_t signals; /* Signals events to generate */
1570 strpollset_t pollwakeups;
1571 mblk_t *nextbp;
1572 uchar_t band = 0;
1573 int hipri_sig;
1575 stp = (struct stdata *)q->q_ptr;
1577 * Use rput_opt for optimized access to the SR_ flags except
1578 * SR_POLLIN. That flag has to be checked under sd_lock since it
1579 * is modified by strpoll().
1581 rput_opt = stp->sd_rput_opt;
1583 ASSERT(qclaimed(q));
1584 TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_ENTER,
1585 "strrput called with message type:q %p bp %p", q, bp);
1588 * Perform initial processing and pass to the parameterized functions.
1590 ASSERT(bp->b_next == NULL);
1592 switch (bp->b_datap->db_type) {
1593 case M_DATA:
1595 * sockfs is the only consumer of STREOF and when it is set,
1596 * it implies that the receiver is not interested in receiving
1597 * any more data, hence the mblk is freed to prevent unnecessary
1598 * message queueing at the stream head.
1600 if (stp->sd_flag == STREOF) {
1601 freemsg(bp);
1602 return (0);
1604 if ((rput_opt & SR_IGN_ZEROLEN) &&
1605 bp->b_rptr == bp->b_wptr && msgnodata(bp)) {
1607 * Ignore zero-length M_DATA messages. These might be
1608 * generated by some transports.
1609 * The zero-length M_DATA messages, even if they
1610 * are ignored, should effect the atmark tracking and
1611 * should wake up a thread sleeping in strwaitmark.
1613 mutex_enter(&stp->sd_lock);
1614 if (bp->b_flag & MSGMARKNEXT) {
1616 * Record the position of the mark either
1617 * in q_last or in STRATMARK.
1619 if (q->q_last != NULL) {
1620 q->q_last->b_flag &= ~MSGNOTMARKNEXT;
1621 q->q_last->b_flag |= MSGMARKNEXT;
1622 } else {
1623 stp->sd_flag &= ~STRNOTATMARK;
1624 stp->sd_flag |= STRATMARK;
1626 } else if (bp->b_flag & MSGNOTMARKNEXT) {
1628 * Record that this is not the position of
1629 * the mark either in q_last or in
1630 * STRNOTATMARK.
1632 if (q->q_last != NULL) {
1633 q->q_last->b_flag &= ~MSGMARKNEXT;
1634 q->q_last->b_flag |= MSGNOTMARKNEXT;
1635 } else {
1636 stp->sd_flag &= ~STRATMARK;
1637 stp->sd_flag |= STRNOTATMARK;
1640 if (stp->sd_flag & RSLEEP) {
1641 stp->sd_flag &= ~RSLEEP;
1642 cv_broadcast(&q->q_wait);
1644 mutex_exit(&stp->sd_lock);
1645 freemsg(bp);
1646 return (0);
1648 wakeups = RSLEEP;
1649 if (bp->b_band == 0) {
1650 firstmsgsigs = S_INPUT | S_RDNORM;
1651 pollwakeups = POLLIN | POLLRDNORM;
1652 } else {
1653 firstmsgsigs = S_INPUT | S_RDBAND;
1654 pollwakeups = POLLIN | POLLRDBAND;
1656 if (rput_opt & SR_SIGALLDATA)
1657 allmsgsigs = firstmsgsigs;
1658 else
1659 allmsgsigs = 0;
1661 mutex_enter(&stp->sd_lock);
1662 if ((rput_opt & SR_CONSOL_DATA) &&
1663 (q->q_last != NULL) &&
1664 (bp->b_flag & (MSGMARK|MSGDELIM)) == 0) {
1666 * Consolidate an M_DATA message onto an M_DATA,
1667 * M_PROTO, or M_PCPROTO by merging it with q_last.
1668 * The consolidation does not take place if
1669 * the old message is marked with either of the
1670 * marks or the delim flag or if the new
1671 * message is marked with MSGMARK. The MSGMARK
1672 * check is needed to handle the odd semantics of
1673 * MSGMARK where essentially the whole message
1674 * is to be treated as marked.
1675 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from the
1676 * new message to the front of the b_cont chain.
1678 mblk_t *lbp = q->q_last;
1679 unsigned char db_type = lbp->b_datap->db_type;
1681 if ((db_type == M_DATA || db_type == M_PROTO ||
1682 db_type == M_PCPROTO) &&
1683 !(lbp->b_flag & (MSGDELIM|MSGMARK|MSGMARKNEXT))) {
1684 rmvq_noenab(q, lbp);
1686 * The first message in the b_cont list
1687 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
1688 * We need to handle the case where we
1689 * are appending:
1691 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
1692 * 2) a MSGMARKNEXT to a plain message.
1693 * 3) a MSGNOTMARKNEXT to a plain message
1694 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
1695 * message.
1697 * Thus we never append a MSGMARKNEXT or
1698 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
1700 if (bp->b_flag & MSGMARKNEXT) {
1701 lbp->b_flag |= MSGMARKNEXT;
1702 lbp->b_flag &= ~MSGNOTMARKNEXT;
1703 bp->b_flag &= ~MSGMARKNEXT;
1704 } else if (bp->b_flag & MSGNOTMARKNEXT) {
1705 lbp->b_flag |= MSGNOTMARKNEXT;
1706 bp->b_flag &= ~MSGNOTMARKNEXT;
1709 linkb(lbp, bp);
1710 bp = lbp;
1712 * The new message logically isn't the first
1713 * even though the q_first check below thinks
1714 * it is. Clear the firstmsgsigs to make it
1715 * not appear to be first.
1717 firstmsgsigs = 0;
1720 break;
1722 case M_PASSFP:
1723 wakeups = RSLEEP;
1724 allmsgsigs = 0;
1725 if (bp->b_band == 0) {
1726 firstmsgsigs = S_INPUT | S_RDNORM;
1727 pollwakeups = POLLIN | POLLRDNORM;
1728 } else {
1729 firstmsgsigs = S_INPUT | S_RDBAND;
1730 pollwakeups = POLLIN | POLLRDBAND;
1732 mutex_enter(&stp->sd_lock);
1733 break;
1735 case M_PROTO:
1736 case M_PCPROTO:
1737 ASSERT(stp->sd_rprotofunc != NULL);
1738 bp = (stp->sd_rprotofunc)(stp->sd_vnode, bp,
1739 &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1740 #define ALLSIG (S_INPUT|S_HIPRI|S_OUTPUT|S_MSG|S_ERROR|S_HANGUP|S_RDNORM|\
1741 S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)
1742 #define ALLPOLL (POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLWRNORM|POLLRDBAND|\
1743 POLLWRBAND)
1745 ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1746 ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1747 ASSERT((allmsgsigs & ~ALLSIG) == 0);
1748 ASSERT((pollwakeups & ~ALLPOLL) == 0);
1750 mutex_enter(&stp->sd_lock);
1751 break;
1753 default:
1754 ASSERT(stp->sd_rmiscfunc != NULL);
1755 bp = (stp->sd_rmiscfunc)(stp->sd_vnode, bp,
1756 &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1757 ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1758 ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1759 ASSERT((allmsgsigs & ~ALLSIG) == 0);
1760 ASSERT((pollwakeups & ~ALLPOLL) == 0);
1761 #undef ALLSIG
1762 #undef ALLPOLL
1763 mutex_enter(&stp->sd_lock);
1764 break;
1766 ASSERT(MUTEX_HELD(&stp->sd_lock));
1768 /* By default generate superset of signals */
1769 signals = (firstmsgsigs | allmsgsigs);
1772 * The proto and misc functions can return multiple messages
1773 * as a b_next chain. Such messages are processed separately.
1775 one_more:
1776 hipri_sig = 0;
1777 if (bp == NULL) {
1778 nextbp = NULL;
1779 } else {
1780 nextbp = bp->b_next;
1781 bp->b_next = NULL;
1783 switch (bp->b_datap->db_type) {
1784 case M_PCPROTO:
1786 * Only one priority protocol message is allowed at the
1787 * stream head at a time.
1789 if (stp->sd_flag & STRPRI) {
1790 TRACE_0(TR_FAC_STREAMS_FR, TR_STRRPUT_PROTERR,
1791 "M_PCPROTO already at head");
1792 freemsg(bp);
1793 mutex_exit(&stp->sd_lock);
1794 goto done;
1796 stp->sd_flag |= STRPRI;
1797 hipri_sig = 1;
1798 /* FALLTHRU */
1799 case M_DATA:
1800 case M_PROTO:
1801 case M_PASSFP:
1802 band = bp->b_band;
1804 * Marking doesn't work well when messages
1805 * are marked in more than one band. We only
1806 * remember the last message received, even if
1807 * it is placed on the queue ahead of other
1808 * marked messages.
1810 if (bp->b_flag & MSGMARK)
1811 stp->sd_mark = bp;
1812 (void) putq(q, bp);
1815 * If message is a PCPROTO message, always use
1816 * firstmsgsigs to determine if a signal should be
1817 * sent as strrput is the only place to send
1818 * signals for PCPROTO. Other messages are based on
1819 * the STRGETINPROG flag. The flag determines if
1820 * strrput or (k)strgetmsg will be responsible for
1821 * sending the signals, in the firstmsgsigs case.
1823 if ((hipri_sig == 1) ||
1824 (((stp->sd_flag & STRGETINPROG) == 0) &&
1825 (q->q_first == bp)))
1826 signals = (firstmsgsigs | allmsgsigs);
1827 else
1828 signals = allmsgsigs;
1829 break;
1831 default:
1832 mutex_exit(&stp->sd_lock);
1833 (void) strrput_nondata(q, bp);
1834 mutex_enter(&stp->sd_lock);
1835 break;
1838 ASSERT(MUTEX_HELD(&stp->sd_lock));
1840 * Wake sleeping read/getmsg and cancel deferred wakeup
1842 if (wakeups & RSLEEP)
1843 stp->sd_wakeq &= ~RSLEEP;
1845 wakeups &= stp->sd_flag;
1846 if (wakeups & RSLEEP) {
1847 stp->sd_flag &= ~RSLEEP;
1848 cv_broadcast(&q->q_wait);
1850 if (wakeups & WSLEEP) {
1851 stp->sd_flag &= ~WSLEEP;
1852 cv_broadcast(&_WR(q)->q_wait);
1855 if (pollwakeups != 0) {
1856 if (pollwakeups == (POLLIN | POLLRDNORM)) {
1858 * Can't use rput_opt since it was not
1859 * read when sd_lock was held and SR_POLLIN is changed
1860 * by strpoll() under sd_lock.
1862 if (!(stp->sd_rput_opt & SR_POLLIN))
1863 goto no_pollwake;
1864 stp->sd_rput_opt &= ~SR_POLLIN;
1866 mutex_exit(&stp->sd_lock);
1867 pollwakeup(&stp->sd_pollist, pollwakeups);
1868 mutex_enter(&stp->sd_lock);
1870 no_pollwake:
1873 * strsendsig can handle multiple signals with a
1874 * single call.
1876 if (stp->sd_sigflags & signals)
1877 strsendsig(stp->sd_siglist, signals, band, 0);
1878 mutex_exit(&stp->sd_lock);
1881 done:
1882 if (nextbp == NULL)
1883 return (0);
1886 * Any signals were handled the first time.
1887 * Wakeups and pollwakeups are redone to avoid any race
1888 * conditions - all the messages are not queued until the
1889 * last message has been processed by strrput.
1891 bp = nextbp;
1892 signals = firstmsgsigs = allmsgsigs = 0;
1893 mutex_enter(&stp->sd_lock);
1894 goto one_more;
1897 static void
1898 log_dupioc(queue_t *rq, mblk_t *bp)
1900 queue_t *wq, *qp;
1901 char *modnames, *mnp, *dname;
1902 size_t maxmodstr;
1903 boolean_t islast;
1906 * Allocate a buffer large enough to hold the names of nstrpush modules
1907 * and one driver, with spaces between and NUL terminator. If we can't
1908 * get memory, then we'll just log the driver name.
1910 maxmodstr = nstrpush * (FMNAMESZ + 1);
1911 mnp = modnames = kmem_alloc(maxmodstr, KM_NOSLEEP);
1913 /* march down write side to print log message down to the driver */
1914 wq = WR(rq);
1916 /* make sure q_next doesn't shift around while we're grabbing data */
1917 claimstr(wq);
1918 qp = wq->q_next;
1919 do {
1920 dname = Q2NAME(qp);
1921 islast = !SAMESTR(qp) || qp->q_next == NULL;
1922 if (modnames == NULL) {
1924 * If we don't have memory, then get the driver name in
1925 * the log where we can see it. Note that memory
1926 * pressure is a possible cause of these sorts of bugs.
1928 if (islast) {
1929 modnames = dname;
1930 maxmodstr = 0;
1932 } else {
1933 mnp += snprintf(mnp, FMNAMESZ + 1, "%s", dname);
1934 if (!islast)
1935 *mnp++ = ' ';
1937 qp = qp->q_next;
1938 } while (!islast);
1939 releasestr(wq);
1940 /* Cannot happen unless stream head is corrupt. */
1941 ASSERT(modnames != NULL);
1942 (void) strlog(rq->q_qinfo->qi_minfo->mi_idnum, 0, 1,
1943 SL_CONSOLE|SL_TRACE|SL_ERROR,
1944 "Warning: stream %p received duplicate %X M_IOC%s; module list: %s",
1945 rq->q_ptr, ((struct iocblk *)bp->b_rptr)->ioc_cmd,
1946 (DB_TYPE(bp) == M_IOCACK ? "ACK" : "NAK"), modnames);
1947 if (maxmodstr != 0)
1948 kmem_free(modnames, maxmodstr);
1952 strrput_nondata(queue_t *q, mblk_t *bp)
1954 struct stdata *stp;
1955 struct iocblk *iocbp;
1956 struct stroptions *sop;
1957 struct copyreq *reqp;
1958 struct copyresp *resp;
1959 unsigned char bpri;
1960 unsigned char flushed_already = 0;
1962 stp = (struct stdata *)q->q_ptr;
1964 ASSERT(!(stp->sd_flag & STPLEX));
1965 ASSERT(qclaimed(q));
1967 switch (bp->b_datap->db_type) {
1968 case M_ERROR:
1970 * An error has occurred downstream, the errno is in the first
1971 * bytes of the message.
1973 if ((bp->b_wptr - bp->b_rptr) == 2) { /* New flavor */
1974 unsigned char rw = 0;
1976 mutex_enter(&stp->sd_lock);
1977 if (*bp->b_rptr != NOERROR) { /* read error */
1978 if (*bp->b_rptr != 0) {
1979 if (stp->sd_flag & STRDERR)
1980 flushed_already |= FLUSHR;
1981 stp->sd_flag |= STRDERR;
1982 rw |= FLUSHR;
1983 } else {
1984 stp->sd_flag &= ~STRDERR;
1986 stp->sd_rerror = *bp->b_rptr;
1988 bp->b_rptr++;
1989 if (*bp->b_rptr != NOERROR) { /* write error */
1990 if (*bp->b_rptr != 0) {
1991 if (stp->sd_flag & STWRERR)
1992 flushed_already |= FLUSHW;
1993 stp->sd_flag |= STWRERR;
1994 rw |= FLUSHW;
1995 } else {
1996 stp->sd_flag &= ~STWRERR;
1998 stp->sd_werror = *bp->b_rptr;
2000 if (rw) {
2001 TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_WAKE,
2002 "strrput cv_broadcast:q %p, bp %p",
2003 q, bp);
2004 cv_broadcast(&q->q_wait); /* readers */
2005 cv_broadcast(&_WR(q)->q_wait); /* writers */
2006 cv_broadcast(&stp->sd_monitor); /* ioctllers */
2008 mutex_exit(&stp->sd_lock);
2009 pollwakeup(&stp->sd_pollist, POLLERR);
2010 mutex_enter(&stp->sd_lock);
2012 if (stp->sd_sigflags & S_ERROR)
2013 strsendsig(stp->sd_siglist, S_ERROR, 0,
2014 ((rw & FLUSHR) ? stp->sd_rerror :
2015 stp->sd_werror));
2016 mutex_exit(&stp->sd_lock);
2018 * Send the M_FLUSH only
2019 * for the first M_ERROR
2020 * message on the stream
2022 if (flushed_already == rw) {
2023 freemsg(bp);
2024 return (0);
2027 bp->b_datap->db_type = M_FLUSH;
2028 *bp->b_rptr = rw;
2029 bp->b_wptr = bp->b_rptr + 1;
2031 * Protect against the driver
2032 * passing up messages after
2033 * it has done a qprocsoff
2035 if (_OTHERQ(q)->q_next == NULL)
2036 freemsg(bp);
2037 else
2038 qreply(q, bp);
2039 return (0);
2040 } else
2041 mutex_exit(&stp->sd_lock);
2042 } else if (*bp->b_rptr != 0) { /* Old flavor */
2043 if (stp->sd_flag & (STRDERR|STWRERR))
2044 flushed_already = FLUSHRW;
2045 mutex_enter(&stp->sd_lock);
2046 stp->sd_flag |= (STRDERR|STWRERR);
2047 stp->sd_rerror = *bp->b_rptr;
2048 stp->sd_werror = *bp->b_rptr;
2049 TRACE_2(TR_FAC_STREAMS_FR,
2050 TR_STRRPUT_WAKE2,
2051 "strrput wakeup #2:q %p, bp %p", q, bp);
2052 cv_broadcast(&q->q_wait); /* the readers */
2053 cv_broadcast(&_WR(q)->q_wait); /* the writers */
2054 cv_broadcast(&stp->sd_monitor); /* ioctllers */
2056 mutex_exit(&stp->sd_lock);
2057 pollwakeup(&stp->sd_pollist, POLLERR);
2058 mutex_enter(&stp->sd_lock);
2060 if (stp->sd_sigflags & S_ERROR)
2061 strsendsig(stp->sd_siglist, S_ERROR, 0,
2062 (stp->sd_werror ? stp->sd_werror :
2063 stp->sd_rerror));
2064 mutex_exit(&stp->sd_lock);
2067 * Send the M_FLUSH only
2068 * for the first M_ERROR
2069 * message on the stream
2071 if (flushed_already != FLUSHRW) {
2072 bp->b_datap->db_type = M_FLUSH;
2073 *bp->b_rptr = FLUSHRW;
2075 * Protect against the driver passing up
2076 * messages after it has done a
2077 * qprocsoff.
2079 if (_OTHERQ(q)->q_next == NULL)
2080 freemsg(bp);
2081 else
2082 qreply(q, bp);
2083 return (0);
2086 freemsg(bp);
2087 return (0);
2089 case M_HANGUP:
2091 freemsg(bp);
2092 mutex_enter(&stp->sd_lock);
2093 stp->sd_werror = ENXIO;
2094 stp->sd_flag |= STRHUP;
2095 stp->sd_flag &= ~(WSLEEP|RSLEEP);
2098 * send signal if controlling tty
2101 if (stp->sd_sidp) {
2102 prsignal(stp->sd_sidp, SIGHUP);
2103 if (stp->sd_sidp != stp->sd_pgidp)
2104 pgsignal(stp->sd_pgidp, SIGTSTP);
2108 * wake up read, write, and exception pollers and
2109 * reset wakeup mechanism.
2111 cv_broadcast(&q->q_wait); /* the readers */
2112 cv_broadcast(&_WR(q)->q_wait); /* the writers */
2113 cv_broadcast(&stp->sd_monitor); /* the ioctllers */
2114 strhup(stp);
2115 mutex_exit(&stp->sd_lock);
2116 return (0);
2118 case M_UNHANGUP:
2119 freemsg(bp);
2120 mutex_enter(&stp->sd_lock);
2121 stp->sd_werror = 0;
2122 stp->sd_flag &= ~STRHUP;
2123 mutex_exit(&stp->sd_lock);
2124 return (0);
2126 case M_SIG:
2128 * Someone downstream wants to post a signal. The
2129 * signal to post is contained in the first byte of the
2130 * message. If the message would go on the front of
2131 * the queue, send a signal to the process group
2132 * (if not SIGPOLL) or to the siglist processes
2133 * (SIGPOLL). If something is already on the queue,
2134 * OR if we are delivering a delayed suspend (*sigh*
2135 * another "tty" hack) and there's no one sleeping already,
2136 * just enqueue the message.
2138 mutex_enter(&stp->sd_lock);
2139 if (q->q_first || (*bp->b_rptr == SIGTSTP &&
2140 !(stp->sd_flag & RSLEEP))) {
2141 (void) putq(q, bp);
2142 mutex_exit(&stp->sd_lock);
2143 return (0);
2145 mutex_exit(&stp->sd_lock);
2146 /* FALLTHRU */
2148 case M_PCSIG:
2150 * Don't enqueue, just post the signal.
2152 strsignal(stp, *bp->b_rptr, 0L);
2153 freemsg(bp);
2154 return (0);
2156 case M_CMD:
2157 if (MBLKL(bp) != sizeof (cmdblk_t)) {
2158 freemsg(bp);
2159 return (0);
2162 mutex_enter(&stp->sd_lock);
2163 if (stp->sd_flag & STRCMDWAIT) {
2164 ASSERT(stp->sd_cmdblk == NULL);
2165 stp->sd_cmdblk = bp;
2166 cv_broadcast(&stp->sd_monitor);
2167 mutex_exit(&stp->sd_lock);
2168 } else {
2169 mutex_exit(&stp->sd_lock);
2170 freemsg(bp);
2172 return (0);
2174 case M_FLUSH:
2176 * Flush queues. The indication of which queues to flush
2177 * is in the first byte of the message. If the read queue
2178 * is specified, then flush it. If FLUSHBAND is set, just
2179 * flush the band specified by the second byte of the message.
2181 * If a module has issued a M_SETOPT to not flush hi
2182 * priority messages off of the stream head, then pass this
2183 * flag into the flushq code to preserve such messages.
2186 if (*bp->b_rptr & FLUSHR) {
2187 mutex_enter(&stp->sd_lock);
2188 if (*bp->b_rptr & FLUSHBAND) {
2189 ASSERT((bp->b_wptr - bp->b_rptr) >= 2);
2190 flushband(q, *(bp->b_rptr + 1), FLUSHALL);
2191 } else
2192 flushq_common(q, FLUSHALL,
2193 stp->sd_read_opt & RFLUSHPCPROT);
2194 if ((q->q_first == NULL) ||
2195 (q->q_first->b_datap->db_type < QPCTL))
2196 stp->sd_flag &= ~STRPRI;
2197 else {
2198 ASSERT(stp->sd_flag & STRPRI);
2200 mutex_exit(&stp->sd_lock);
2202 if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
2203 *bp->b_rptr &= ~FLUSHR;
2204 bp->b_flag |= MSGNOLOOP;
2206 * Protect against the driver passing up
2207 * messages after it has done a qprocsoff.
2209 if (_OTHERQ(q)->q_next == NULL)
2210 freemsg(bp);
2211 else
2212 qreply(q, bp);
2213 return (0);
2215 freemsg(bp);
2216 return (0);
2218 case M_IOCACK:
2219 case M_IOCNAK:
2220 iocbp = (struct iocblk *)bp->b_rptr;
2222 * If not waiting for ACK or NAK then just free msg.
2223 * If incorrect id sequence number then just free msg.
2224 * If already have ACK or NAK for user then this is a
2225 * duplicate, display a warning and free the msg.
2227 mutex_enter(&stp->sd_lock);
2228 if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2229 (stp->sd_iocid != iocbp->ioc_id)) {
2231 * If the ACK/NAK is a dup, display a message
2232 * Dup is when sd_iocid == ioc_id, and
2233 * sd_iocblk == <valid ptr> or -1 (the former
2234 * is when an ioctl has been put on the stream
2235 * head, but has not yet been consumed, the
2236 * later is when it has been consumed).
2238 if ((stp->sd_iocid == iocbp->ioc_id) &&
2239 (stp->sd_iocblk != NULL)) {
2240 log_dupioc(q, bp);
2242 freemsg(bp);
2243 mutex_exit(&stp->sd_lock);
2244 return (0);
2248 * Assign ACK or NAK to user and wake up.
2250 stp->sd_iocblk = bp;
2251 cv_broadcast(&stp->sd_monitor);
2252 mutex_exit(&stp->sd_lock);
2253 return (0);
2255 case M_COPYIN:
2256 case M_COPYOUT:
2257 reqp = (struct copyreq *)bp->b_rptr;
2260 * If not waiting for ACK or NAK then just fail request.
2261 * If already have ACK, NAK, or copy request, then just
2262 * fail request.
2263 * If incorrect id sequence number then just fail request.
2265 mutex_enter(&stp->sd_lock);
2266 if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2267 (stp->sd_iocid != reqp->cq_id)) {
2268 if (bp->b_cont) {
2269 freemsg(bp->b_cont);
2270 bp->b_cont = NULL;
2272 bp->b_datap->db_type = M_IOCDATA;
2273 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
2274 resp = (struct copyresp *)bp->b_rptr;
2275 resp->cp_rval = (caddr_t)1; /* failure */
2276 mutex_exit(&stp->sd_lock);
2277 putnext(stp->sd_wrq, bp);
2278 return (0);
2282 * Assign copy request to user and wake up.
2284 stp->sd_iocblk = bp;
2285 cv_broadcast(&stp->sd_monitor);
2286 mutex_exit(&stp->sd_lock);
2287 return (0);
2289 case M_SETOPTS:
2291 * Set stream head options (read option, write offset,
2292 * min/max packet size, and/or high/low water marks for
2293 * the read side only).
2296 bpri = 0;
2297 sop = (struct stroptions *)bp->b_rptr;
2298 mutex_enter(&stp->sd_lock);
2299 if (sop->so_flags & SO_READOPT) {
2300 switch (sop->so_readopt & RMODEMASK) {
2301 case RNORM:
2302 stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
2303 break;
2305 case RMSGD:
2306 stp->sd_read_opt =
2307 ((stp->sd_read_opt & ~RD_MSGNODIS) |
2308 RD_MSGDIS);
2309 break;
2311 case RMSGN:
2312 stp->sd_read_opt =
2313 ((stp->sd_read_opt & ~RD_MSGDIS) |
2314 RD_MSGNODIS);
2315 break;
2317 switch (sop->so_readopt & RPROTMASK) {
2318 case RPROTNORM:
2319 stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
2320 break;
2322 case RPROTDAT:
2323 stp->sd_read_opt =
2324 ((stp->sd_read_opt & ~RD_PROTDIS) |
2325 RD_PROTDAT);
2326 break;
2328 case RPROTDIS:
2329 stp->sd_read_opt =
2330 ((stp->sd_read_opt & ~RD_PROTDAT) |
2331 RD_PROTDIS);
2332 break;
2334 switch (sop->so_readopt & RFLUSHMASK) {
2335 case RFLUSHPCPROT:
2337 * This sets the stream head to NOT flush
2338 * M_PCPROTO messages.
2340 stp->sd_read_opt |= RFLUSHPCPROT;
2341 break;
2344 if (sop->so_flags & SO_ERROPT) {
2345 switch (sop->so_erropt & RERRMASK) {
2346 case RERRNORM:
2347 stp->sd_flag &= ~STRDERRNONPERSIST;
2348 break;
2349 case RERRNONPERSIST:
2350 stp->sd_flag |= STRDERRNONPERSIST;
2351 break;
2353 switch (sop->so_erropt & WERRMASK) {
2354 case WERRNORM:
2355 stp->sd_flag &= ~STWRERRNONPERSIST;
2356 break;
2357 case WERRNONPERSIST:
2358 stp->sd_flag |= STWRERRNONPERSIST;
2359 break;
2362 if (sop->so_flags & SO_COPYOPT) {
2363 if (sop->so_copyopt & ZCVMSAFE) {
2364 stp->sd_copyflag |= STZCVMSAFE;
2365 stp->sd_copyflag &= ~STZCVMUNSAFE;
2366 } else if (sop->so_copyopt & ZCVMUNSAFE) {
2367 stp->sd_copyflag |= STZCVMUNSAFE;
2368 stp->sd_copyflag &= ~STZCVMSAFE;
2371 if (sop->so_copyopt & COPYCACHED) {
2372 stp->sd_copyflag |= STRCOPYCACHED;
2375 if (sop->so_flags & SO_WROFF)
2376 stp->sd_wroff = sop->so_wroff;
2377 if (sop->so_flags & SO_TAIL)
2378 stp->sd_tail = sop->so_tail;
2379 if (sop->so_flags & SO_MINPSZ)
2380 q->q_minpsz = sop->so_minpsz;
2381 if (sop->so_flags & SO_MAXPSZ)
2382 q->q_maxpsz = sop->so_maxpsz;
2383 if (sop->so_flags & SO_MAXBLK)
2384 stp->sd_maxblk = sop->so_maxblk;
2385 if (sop->so_flags & SO_HIWAT) {
2386 if (sop->so_flags & SO_BAND) {
2387 if (strqset(q, QHIWAT,
2388 sop->so_band, sop->so_hiwat)) {
2389 cmn_err(CE_WARN, "strrput: could not "
2390 "allocate qband\n");
2391 } else {
2392 bpri = sop->so_band;
2394 } else {
2395 q->q_hiwat = sop->so_hiwat;
2398 if (sop->so_flags & SO_LOWAT) {
2399 if (sop->so_flags & SO_BAND) {
2400 if (strqset(q, QLOWAT,
2401 sop->so_band, sop->so_lowat)) {
2402 cmn_err(CE_WARN, "strrput: could not "
2403 "allocate qband\n");
2404 } else {
2405 bpri = sop->so_band;
2407 } else {
2408 q->q_lowat = sop->so_lowat;
2411 if (sop->so_flags & SO_MREADON)
2412 stp->sd_flag |= SNDMREAD;
2413 if (sop->so_flags & SO_MREADOFF)
2414 stp->sd_flag &= ~SNDMREAD;
2415 if (sop->so_flags & SO_NDELON)
2416 stp->sd_flag |= OLDNDELAY;
2417 if (sop->so_flags & SO_NDELOFF)
2418 stp->sd_flag &= ~OLDNDELAY;
2419 if (sop->so_flags & SO_ISTTY)
2420 stp->sd_flag |= STRISTTY;
2421 if (sop->so_flags & SO_ISNTTY)
2422 stp->sd_flag &= ~STRISTTY;
2423 if (sop->so_flags & SO_TOSTOP)
2424 stp->sd_flag |= STRTOSTOP;
2425 if (sop->so_flags & SO_TONSTOP)
2426 stp->sd_flag &= ~STRTOSTOP;
2427 if (sop->so_flags & SO_DELIM)
2428 stp->sd_flag |= STRDELIM;
2429 if (sop->so_flags & SO_NODELIM)
2430 stp->sd_flag &= ~STRDELIM;
2432 mutex_exit(&stp->sd_lock);
2433 freemsg(bp);
2435 /* Check backenable in case the water marks changed */
2436 qbackenable(q, bpri);
2437 return (0);
2440 * The following set of cases deal with situations where two stream
2441 * heads are connected to each other (twisted streams). These messages
2442 * have no meaning at the stream head.
2444 case M_BREAK:
2445 case M_CTL:
2446 case M_DELAY:
2447 case M_START:
2448 case M_STOP:
2449 case M_IOCDATA:
2450 case M_STARTI:
2451 case M_STOPI:
2452 freemsg(bp);
2453 return (0);
2455 case M_IOCTL:
2457 * Always NAK this condition
2458 * (makes no sense)
2459 * If there is one or more threads in the read side
2460 * rwnext we have to defer the nacking until that thread
2461 * returns (in strget).
2463 mutex_enter(&stp->sd_lock);
2464 if (stp->sd_struiodnak != 0) {
2466 * Defer NAK to the streamhead. Queue at the end
2467 * the list.
2469 mblk_t *mp = stp->sd_struionak;
2471 while (mp && mp->b_next)
2472 mp = mp->b_next;
2473 if (mp)
2474 mp->b_next = bp;
2475 else
2476 stp->sd_struionak = bp;
2477 bp->b_next = NULL;
2478 mutex_exit(&stp->sd_lock);
2479 return (0);
2481 mutex_exit(&stp->sd_lock);
2483 bp->b_datap->db_type = M_IOCNAK;
2485 * Protect against the driver passing up
2486 * messages after it has done a qprocsoff.
2488 if (_OTHERQ(q)->q_next == NULL)
2489 freemsg(bp);
2490 else
2491 qreply(q, bp);
2492 return (0);
2494 default:
2495 #ifdef DEBUG
2496 cmn_err(CE_WARN,
2497 "bad message type %x received at stream head\n",
2498 bp->b_datap->db_type);
2499 #endif
2500 freemsg(bp);
2501 return (0);
2504 /* NOTREACHED */
2508 * Check if the stream pointed to by `stp' can be written to, and return an
2509 * error code if not. If `eiohup' is set, then return EIO if STRHUP is set.
2510 * If `sigpipeok' is set and the SW_SIGPIPE option is enabled on the stream,
2511 * then always return EPIPE and send a SIGPIPE to the invoking thread.
2513 static int
2514 strwriteable(struct stdata *stp, boolean_t eiohup, boolean_t sigpipeok)
2516 int error;
2518 ASSERT(MUTEX_HELD(&stp->sd_lock));
2521 * For modem support, POSIX states that on writes, EIO should
2522 * be returned if the stream has been hung up.
2524 if (eiohup && (stp->sd_flag & (STPLEX|STRHUP)) == STRHUP)
2525 error = EIO;
2526 else
2527 error = strgeterr(stp, STRHUP|STPLEX|STWRERR, 0);
2529 if (error != 0) {
2530 if (!(stp->sd_flag & STPLEX) &&
2531 (stp->sd_wput_opt & SW_SIGPIPE) && sigpipeok) {
2532 tsignal(curthread, SIGPIPE);
2533 error = EPIPE;
2537 return (error);
2541 * Copyin and send data down a stream.
2542 * The caller will allocate and copyin any control part that precedes the
2543 * message and pass that in as mctl.
2545 * Caller should *not* hold sd_lock.
2546 * When EWOULDBLOCK is returned the caller has to redo the canputnext
2547 * under sd_lock in order to avoid missing a backenabling wakeup.
2549 * Use iosize = -1 to not send any M_DATA. iosize = 0 sends zero-length M_DATA.
2551 * Set MSG_IGNFLOW in flags to ignore flow control for hipri messages.
2552 * For sync streams we can only ignore flow control by reverting to using
2553 * putnext.
2555 * If sd_maxblk is less than *iosize this routine might return without
2556 * transferring all of *iosize. In all cases, on return *iosize will contain
2557 * the amount of data that was transferred.
2559 static int
2560 strput(struct stdata *stp, mblk_t *mctl, struct uio *uiop, ssize_t *iosize,
2561 int b_flag, int pri, int flags)
2563 struiod_t uiod;
2564 struct iovec buf[IOV_MAX_STACK];
2565 int iovlen = 0;
2566 mblk_t *mp;
2567 queue_t *wqp = stp->sd_wrq;
2568 int error = 0;
2569 ssize_t count = *iosize;
2571 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
2573 if (uiop != NULL && count >= 0)
2574 flags |= stp->sd_struiowrq ? STRUIO_POSTPONE : 0;
2576 if (!(flags & STRUIO_POSTPONE)) {
2578 * Use regular canputnext, strmakedata, putnext sequence.
2580 if (pri == 0) {
2581 if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2582 freemsg(mctl);
2583 return (EWOULDBLOCK);
2585 } else {
2586 if (!(flags & MSG_IGNFLOW) && !bcanputnext(wqp, pri)) {
2587 freemsg(mctl);
2588 return (EWOULDBLOCK);
2592 if ((error = strmakedata(iosize, uiop, stp, flags,
2593 &mp)) != 0) {
2594 freemsg(mctl);
2596 * need to change return code to ENOMEM
2597 * so that this is not confused with
2598 * flow control, EAGAIN.
2601 if (error == EAGAIN)
2602 return (ENOMEM);
2603 else
2604 return (error);
2606 if (mctl != NULL) {
2607 if (mctl->b_cont == NULL)
2608 mctl->b_cont = mp;
2609 else if (mp != NULL)
2610 linkb(mctl, mp);
2611 mp = mctl;
2612 } else if (mp == NULL)
2613 return (0);
2615 mp->b_flag |= b_flag;
2616 mp->b_band = (uchar_t)pri;
2618 if (flags & MSG_IGNFLOW) {
2620 * XXX Hack: Don't get stuck running service
2621 * procedures. This is needed for sockfs when
2622 * sending the unbind message out of the rput
2623 * procedure - we don't want a put procedure
2624 * to run service procedures.
2626 putnext(wqp, mp);
2627 } else {
2628 stream_willservice(stp);
2629 putnext(wqp, mp);
2630 stream_runservice(stp);
2632 return (0);
2635 * Stream supports rwnext() for the write side.
2637 if ((error = strmakedata(iosize, uiop, stp, flags, &mp)) != 0) {
2638 freemsg(mctl);
2640 * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
2642 return (error == EAGAIN ? ENOMEM : error);
2644 if (mctl != NULL) {
2645 if (mctl->b_cont == NULL)
2646 mctl->b_cont = mp;
2647 else if (mp != NULL)
2648 linkb(mctl, mp);
2649 mp = mctl;
2650 } else if (mp == NULL) {
2651 return (0);
2654 mp->b_flag |= b_flag;
2655 mp->b_band = (uchar_t)pri;
2657 if (uiop->uio_iovcnt > IOV_MAX_STACK) {
2658 iovlen = uiop->uio_iovcnt * sizeof (iovec_t);
2659 uiod.d_iov = kmem_alloc(iovlen, KM_SLEEP);
2660 } else {
2661 uiod.d_iov = buf;
2664 (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov, uiop->uio_iovcnt);
2665 uiod.d_uio.uio_offset = 0;
2666 uiod.d_mp = mp;
2667 error = rwnext(wqp, &uiod);
2668 if (! uiod.d_mp) {
2669 uioskip(uiop, *iosize);
2670 if (iovlen != 0)
2671 kmem_free(uiod.d_iov, iovlen);
2672 return (error);
2674 ASSERT(mp == uiod.d_mp);
2675 if (error == EINVAL) {
2677 * The stream plumbing must have changed while
2678 * we were away, so just turn off rwnext()s.
2680 error = 0;
2681 } else if (error == EBUSY || error == EWOULDBLOCK) {
2683 * Couldn't enter a perimeter or took a page fault,
2684 * so fall-back to putnext().
2686 error = 0;
2687 } else {
2688 freemsg(mp);
2689 if (iovlen != 0)
2690 kmem_free(uiod.d_iov, iovlen);
2691 return (error);
2693 /* Have to check canput before consuming data from the uio */
2694 if (pri == 0) {
2695 if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2696 freemsg(mp);
2697 if (iovlen != 0)
2698 kmem_free(uiod.d_iov, iovlen);
2699 return (EWOULDBLOCK);
2701 } else {
2702 if (!bcanputnext(wqp, pri) && !(flags & MSG_IGNFLOW)) {
2703 freemsg(mp);
2704 if (iovlen != 0)
2705 kmem_free(uiod.d_iov, iovlen);
2706 return (EWOULDBLOCK);
2709 ASSERT(mp == uiod.d_mp);
2710 /* Copyin data from the uio */
2711 if ((error = struioget(wqp, mp, &uiod, 0)) != 0) {
2712 freemsg(mp);
2713 if (iovlen != 0)
2714 kmem_free(uiod.d_iov, iovlen);
2715 return (error);
2717 uioskip(uiop, *iosize);
2718 if (flags & MSG_IGNFLOW) {
2720 * XXX Hack: Don't get stuck running service procedures.
2721 * This is needed for sockfs when sending the unbind message
2722 * out of the rput procedure - we don't want a put procedure
2723 * to run service procedures.
2725 putnext(wqp, mp);
2726 } else {
2727 stream_willservice(stp);
2728 putnext(wqp, mp);
2729 stream_runservice(stp);
2731 if (iovlen != 0)
2732 kmem_free(uiod.d_iov, iovlen);
2733 return (0);
2737 * Write attempts to break the write request into messages conforming
2738 * with the minimum and maximum packet sizes set downstream.
2740 * Write will not block if downstream queue is full and
2741 * O_NDELAY is set, otherwise it will block waiting for the queue to get room.
2743 * A write of zero bytes gets packaged into a zero length message and sent
2744 * downstream like any other message.
2746 * If buffers of the requested sizes are not available, the write will
2747 * sleep until the buffers become available.
2749 * Write (if specified) will supply a write offset in a message if it
2750 * makes sense. This can be specified by downstream modules as part of
2751 * a M_SETOPTS message. Write will not supply the write offset if it
2752 * cannot supply any data in a buffer. In other words, write will never
2753 * send down an empty packet due to a write offset.
2755 /* ARGSUSED2 */
2757 strwrite(struct vnode *vp, struct uio *uiop, cred_t *crp)
2759 return (strwrite_common(vp, uiop, crp, 0));
2762 /* ARGSUSED2 */
2764 strwrite_common(struct vnode *vp, struct uio *uiop, cred_t *crp, int wflag)
2766 struct stdata *stp;
2767 struct queue *wqp;
2768 ssize_t rmin, rmax;
2769 ssize_t iosize;
2770 int waitflag;
2771 int tempmode;
2772 int error = 0;
2773 int b_flag;
2775 ASSERT(vp->v_stream);
2776 stp = vp->v_stream;
2778 mutex_enter(&stp->sd_lock);
2780 if ((error = i_straccess(stp, JCWRITE)) != 0) {
2781 mutex_exit(&stp->sd_lock);
2782 return (error);
2785 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
2786 error = strwriteable(stp, B_TRUE, B_TRUE);
2787 if (error != 0) {
2788 mutex_exit(&stp->sd_lock);
2789 return (error);
2793 mutex_exit(&stp->sd_lock);
2795 wqp = stp->sd_wrq;
2797 /* get these values from them cached in the stream head */
2798 rmin = stp->sd_qn_minpsz;
2799 rmax = stp->sd_qn_maxpsz;
2802 * Check the min/max packet size constraints. If min packet size
2803 * is non-zero, the write cannot be split into multiple messages
2804 * and still guarantee the size constraints.
2806 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_IN, "strwrite in:q %p", wqp);
2808 ASSERT((rmax >= 0) || (rmax == INFPSZ));
2809 if (rmax == 0) {
2810 return (0);
2812 if (rmin > 0) {
2813 if (uiop->uio_resid < rmin) {
2814 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2815 "strwrite out:q %p out %d error %d",
2816 wqp, 0, ERANGE);
2817 return (ERANGE);
2819 if ((rmax != INFPSZ) && (uiop->uio_resid > rmax)) {
2820 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2821 "strwrite out:q %p out %d error %d",
2822 wqp, 1, ERANGE);
2823 return (ERANGE);
2828 * Do until count satisfied or error.
2830 waitflag = WRITEWAIT | wflag;
2831 if (stp->sd_flag & OLDNDELAY)
2832 tempmode = uiop->uio_fmode & ~FNDELAY;
2833 else
2834 tempmode = uiop->uio_fmode;
2836 if (rmax == INFPSZ)
2837 rmax = uiop->uio_resid;
2840 * Note that tempmode does not get used in strput/strmakedata
2841 * but only in strwaitq. The other routines use uio_fmode
2842 * unmodified.
2845 while (1) { /* breaks when uio_resid reaches zero */
2847 * Determine the size of the next message to be
2848 * packaged. May have to break write into several
2849 * messages based on max packet size.
2851 iosize = MIN(uiop->uio_resid, rmax);
2854 * Put block downstream when flow control allows it.
2856 if ((stp->sd_flag & STRDELIM) && (uiop->uio_resid == iosize))
2857 b_flag = MSGDELIM;
2858 else
2859 b_flag = 0;
2861 for (;;) {
2862 int done = 0;
2864 error = strput(stp, NULL, uiop, &iosize, b_flag, 0, 0);
2865 if (error == 0)
2866 break;
2867 if (error != EWOULDBLOCK)
2868 goto out;
2870 mutex_enter(&stp->sd_lock);
2872 * Check for a missed wakeup.
2873 * Needed since strput did not hold sd_lock across
2874 * the canputnext.
2876 if (canputnext(wqp)) {
2877 /* Try again */
2878 mutex_exit(&stp->sd_lock);
2879 continue;
2881 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAIT,
2882 "strwrite wait:q %p wait", wqp);
2883 if ((error = strwaitq(stp, waitflag, (ssize_t)0,
2884 tempmode, -1, &done)) != 0 || done) {
2885 mutex_exit(&stp->sd_lock);
2886 if ((vp->v_type == VFIFO) &&
2887 (uiop->uio_fmode & FNDELAY) &&
2888 (error == EAGAIN))
2889 error = 0;
2890 goto out;
2892 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAKE,
2893 "strwrite wake:q %p awakes", wqp);
2894 if ((error = i_straccess(stp, JCWRITE)) != 0) {
2895 mutex_exit(&stp->sd_lock);
2896 goto out;
2898 mutex_exit(&stp->sd_lock);
2900 waitflag |= NOINTR;
2901 TRACE_2(TR_FAC_STREAMS_FR, TR_STRWRITE_RESID,
2902 "strwrite resid:q %p uiop %p", wqp, uiop);
2903 if (uiop->uio_resid) {
2904 /* Recheck for errors - needed for sockets */
2905 if ((stp->sd_wput_opt & SW_RECHECK_ERR) &&
2906 (stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
2907 mutex_enter(&stp->sd_lock);
2908 error = strwriteable(stp, B_FALSE, B_TRUE);
2909 mutex_exit(&stp->sd_lock);
2910 if (error != 0)
2911 return (error);
2913 continue;
2915 break;
2917 out:
2919 * For historical reasons, applications expect EAGAIN when a data
2920 * mblk_t cannot be allocated, so change ENOMEM back to EAGAIN.
2922 if (error == ENOMEM)
2923 error = EAGAIN;
2924 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2925 "strwrite out:q %p out %d error %d", wqp, 2, error);
2926 return (error);
2930 * Stream head write service routine.
2931 * Its job is to wake up any sleeping writers when a queue
2932 * downstream needs data (part of the flow control in putq and getq).
2933 * It also must wake anyone sleeping on a poll().
2934 * For stream head right below mux module, it must also invoke put procedure
2935 * of next downstream module.
2938 strwsrv(queue_t *q)
2940 struct stdata *stp;
2941 queue_t *tq;
2942 qband_t *qbp;
2943 int i;
2944 qband_t *myqbp;
2945 int isevent;
2946 unsigned char qbf[NBAND]; /* band flushing backenable flags */
2948 TRACE_1(TR_FAC_STREAMS_FR,
2949 TR_STRWSRV, "strwsrv:q %p", q);
2950 stp = (struct stdata *)q->q_ptr;
2951 ASSERT(qclaimed(q));
2952 mutex_enter(&stp->sd_lock);
2953 ASSERT(!(stp->sd_flag & STPLEX));
2955 if (stp->sd_flag & WSLEEP) {
2956 stp->sd_flag &= ~WSLEEP;
2957 cv_broadcast(&q->q_wait);
2959 mutex_exit(&stp->sd_lock);
2961 /* The other end of a stream pipe went away. */
2962 if ((tq = q->q_next) == NULL) {
2963 return (0);
2966 /* Find the next module forward that has a service procedure */
2967 claimstr(q);
2968 tq = q->q_nfsrv;
2969 ASSERT(tq != NULL);
2971 if ((q->q_flag & QBACK)) {
2972 if ((tq->q_flag & QFULL)) {
2973 mutex_enter(QLOCK(tq));
2974 if (!(tq->q_flag & QFULL)) {
2975 mutex_exit(QLOCK(tq));
2976 goto wakeup;
2979 * The queue must have become full again. Set QWANTW
2980 * again so strwsrv will be back enabled when
2981 * the queue becomes non-full next time.
2983 tq->q_flag |= QWANTW;
2984 mutex_exit(QLOCK(tq));
2985 } else {
2986 wakeup:
2987 pollwakeup(&stp->sd_pollist, POLLWRNORM);
2988 mutex_enter(&stp->sd_lock);
2989 if (stp->sd_sigflags & S_WRNORM)
2990 strsendsig(stp->sd_siglist, S_WRNORM, 0, 0);
2991 mutex_exit(&stp->sd_lock);
2995 isevent = 0;
2996 i = 1;
2997 bzero((caddr_t)qbf, NBAND);
2998 mutex_enter(QLOCK(tq));
2999 if ((myqbp = q->q_bandp) != NULL)
3000 for (qbp = tq->q_bandp; qbp && myqbp; qbp = qbp->qb_next) {
3001 ASSERT(myqbp);
3002 if ((myqbp->qb_flag & QB_BACK)) {
3003 if (qbp->qb_flag & QB_FULL) {
3005 * The band must have become full again.
3006 * Set QB_WANTW again so strwsrv will
3007 * be back enabled when the band becomes
3008 * non-full next time.
3010 qbp->qb_flag |= QB_WANTW;
3011 } else {
3012 isevent = 1;
3013 qbf[i] = 1;
3016 myqbp = myqbp->qb_next;
3017 i++;
3019 mutex_exit(QLOCK(tq));
3021 if (isevent) {
3022 for (i = tq->q_nband; i; i--) {
3023 if (qbf[i]) {
3024 pollwakeup(&stp->sd_pollist, POLLWRBAND);
3025 mutex_enter(&stp->sd_lock);
3026 if (stp->sd_sigflags & S_WRBAND)
3027 strsendsig(stp->sd_siglist, S_WRBAND,
3028 (uchar_t)i, 0);
3029 mutex_exit(&stp->sd_lock);
3034 releasestr(q);
3035 return (0);
3039 * Special case of strcopyin/strcopyout for copying
3040 * struct strioctl that can deal with both data
3041 * models.
3044 #ifdef _LP64
3046 static int
3047 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3049 struct strioctl32 strioc32;
3050 struct strioctl *striocp;
3052 if (copyflag & U_TO_K) {
3053 ASSERT((copyflag & K_TO_K) == 0);
3055 if ((flag & FMODELS) == DATAMODEL_ILP32) {
3056 if (copyin(from, &strioc32, sizeof (strioc32)))
3057 return (EFAULT);
3059 striocp = (struct strioctl *)to;
3060 striocp->ic_cmd = strioc32.ic_cmd;
3061 striocp->ic_timout = strioc32.ic_timout;
3062 striocp->ic_len = strioc32.ic_len;
3063 striocp->ic_dp = (char *)(uintptr_t)strioc32.ic_dp;
3065 } else { /* NATIVE data model */
3066 if (copyin(from, to, sizeof (struct strioctl))) {
3067 return (EFAULT);
3068 } else {
3069 return (0);
3072 } else {
3073 ASSERT(copyflag & K_TO_K);
3074 bcopy(from, to, sizeof (struct strioctl));
3076 return (0);
3079 static int
3080 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3082 struct strioctl32 strioc32;
3083 struct strioctl *striocp;
3085 if (copyflag & U_TO_K) {
3086 ASSERT((copyflag & K_TO_K) == 0);
3088 if ((flag & FMODELS) == DATAMODEL_ILP32) {
3089 striocp = (struct strioctl *)from;
3090 strioc32.ic_cmd = striocp->ic_cmd;
3091 strioc32.ic_timout = striocp->ic_timout;
3092 strioc32.ic_len = striocp->ic_len;
3093 strioc32.ic_dp = (caddr32_t)(uintptr_t)striocp->ic_dp;
3094 ASSERT((char *)(uintptr_t)strioc32.ic_dp ==
3095 striocp->ic_dp);
3097 if (copyout(&strioc32, to, sizeof (strioc32)))
3098 return (EFAULT);
3100 } else { /* NATIVE data model */
3101 if (copyout(from, to, sizeof (struct strioctl))) {
3102 return (EFAULT);
3103 } else {
3104 return (0);
3107 } else {
3108 ASSERT(copyflag & K_TO_K);
3109 bcopy(from, to, sizeof (struct strioctl));
3111 return (0);
3114 #else /* ! _LP64 */
3116 /* ARGSUSED2 */
3117 static int
3118 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3120 return (strcopyin(from, to, sizeof (struct strioctl), copyflag));
3123 /* ARGSUSED2 */
3124 static int
3125 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3127 return (strcopyout(from, to, sizeof (struct strioctl), copyflag));
3130 #endif /* _LP64 */
3133 * Determine type of job control semantics expected by user. The
3134 * possibilities are:
3135 * JCREAD - Behaves like read() on fd; send SIGTTIN
3136 * JCWRITE - Behaves like write() on fd; send SIGTTOU if TOSTOP set
3137 * JCSETP - Sets a value in the stream; send SIGTTOU, ignore TOSTOP
3138 * JCGETP - Gets a value in the stream; no signals.
3139 * See straccess in strsubr.c for usage of these values.
3141 * This routine also returns -1 for I_STR as a special case; the
3142 * caller must call again with the real ioctl number for
3143 * classification.
3145 static int
3146 job_control_type(int cmd)
3148 switch (cmd) {
3149 case I_STR:
3150 return (-1);
3152 case I_RECVFD:
3153 case I_E_RECVFD:
3154 return (JCREAD);
3156 case I_FDINSERT:
3157 case I_SENDFD:
3158 return (JCWRITE);
3160 case TCSETA:
3161 case TCSETAW:
3162 case TCSETAF:
3163 case TCSBRK:
3164 case TCXONC:
3165 case TCFLSH:
3166 case TCDSET: /* Obsolete */
3167 case TIOCSWINSZ:
3168 case TCSETS:
3169 case TCSETSW:
3170 case TCSETSF:
3171 case TIOCSETD:
3172 case TIOCHPCL:
3173 case TIOCSETP:
3174 case TIOCSETN:
3175 case TIOCEXCL:
3176 case TIOCNXCL:
3177 case TIOCFLUSH:
3178 case TIOCSETC:
3179 case TIOCLBIS:
3180 case TIOCLBIC:
3181 case TIOCLSET:
3182 case TIOCSBRK:
3183 case TIOCCBRK:
3184 case TIOCSDTR:
3185 case TIOCCDTR:
3186 case TIOCSLTC:
3187 case TIOCSTOP:
3188 case TIOCSTART:
3189 case TIOCSTI:
3190 case TIOCSPGRP:
3191 case TIOCMSET:
3192 case TIOCMBIS:
3193 case TIOCMBIC:
3194 case TIOCREMOTE:
3195 case TIOCSIGNAL:
3196 case LDSETT:
3197 case LDSMAP: /* Obsolete */
3198 case DIOCSETP:
3199 case I_FLUSH:
3200 case I_SRDOPT:
3201 case I_SETSIG:
3202 case I_SWROPT:
3203 case I_FLUSHBAND:
3204 case I_SETCLTIME:
3205 case I_SERROPT:
3206 case I_ESETSIG:
3207 case FIONBIO:
3208 case FIOASYNC:
3209 case FIOSETOWN:
3210 case JBOOT: /* Obsolete */
3211 case JTERM: /* Obsolete */
3212 case JTIMOM: /* Obsolete */
3213 case JZOMBOOT: /* Obsolete */
3214 case JAGENT: /* Obsolete */
3215 case JTRUN: /* Obsolete */
3216 case JXTPROTO: /* Obsolete */
3217 return (JCSETP);
3220 return (JCGETP);
3224 * ioctl for streams
3227 strioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, int copyflag,
3228 cred_t *crp, int *rvalp)
3230 struct stdata *stp;
3231 struct strcmd *scp;
3232 struct strioctl strioc;
3233 struct uio uio;
3234 struct iovec iov;
3235 int access;
3236 mblk_t *mp;
3237 int error = 0;
3238 int done = 0;
3239 ssize_t rmin, rmax;
3240 queue_t *wrq;
3241 queue_t *rdq;
3242 boolean_t kioctl = B_FALSE;
3244 if (flag & FKIOCTL) {
3245 copyflag = K_TO_K;
3246 kioctl = B_TRUE;
3248 ASSERT(vp->v_stream);
3249 ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
3250 stp = vp->v_stream;
3252 TRACE_3(TR_FAC_STREAMS_FR, TR_IOCTL_ENTER,
3253 "strioctl:stp %p cmd %X arg %lX", stp, cmd, arg);
3256 * If the copy is kernel to kernel, make sure that the FNATIVE
3257 * flag is set. After this it would be a serious error to have
3258 * no model flag.
3260 if (copyflag == K_TO_K)
3261 flag = (flag & ~FMODELS) | FNATIVE;
3263 ASSERT((flag & FMODELS) != 0);
3265 wrq = stp->sd_wrq;
3266 rdq = _RD(wrq);
3268 access = job_control_type(cmd);
3270 /* We should never see these here, should be handled by iwscn */
3271 if (cmd == SRIOCSREDIR || cmd == SRIOCISREDIR)
3272 return (EINVAL);
3274 mutex_enter(&stp->sd_lock);
3275 if ((access != -1) && ((error = i_straccess(stp, access)) != 0)) {
3276 mutex_exit(&stp->sd_lock);
3277 return (error);
3279 mutex_exit(&stp->sd_lock);
3282 * Check for sgttyb-related ioctls first, and complain as
3283 * necessary.
3285 switch (cmd) {
3286 case TIOCGETP:
3287 case TIOCSETP:
3288 case TIOCSETN:
3289 if (sgttyb_handling >= 2 && !sgttyb_complaint) {
3290 sgttyb_complaint = B_TRUE;
3291 cmn_err(CE_NOTE,
3292 "application used obsolete TIOC[GS]ET");
3294 if (sgttyb_handling >= 3) {
3295 tsignal(curthread, SIGSYS);
3296 return (EIO);
3298 break;
3301 mutex_enter(&stp->sd_lock);
3303 switch (cmd) {
3304 case I_RECVFD:
3305 case I_E_RECVFD:
3306 case I_PEEK:
3307 case I_NREAD:
3308 case FIONREAD:
3309 case FIORDCHK:
3310 case I_ATMARK:
3311 case FIONBIO:
3312 case FIOASYNC:
3313 if (stp->sd_flag & (STRDERR|STPLEX)) {
3314 error = strgeterr(stp, STRDERR|STPLEX, 0);
3315 if (error != 0) {
3316 mutex_exit(&stp->sd_lock);
3317 return (error);
3320 break;
3322 default:
3323 if (stp->sd_flag & (STRDERR|STWRERR|STPLEX)) {
3324 error = strgeterr(stp, STRDERR|STWRERR|STPLEX, 0);
3325 if (error != 0) {
3326 mutex_exit(&stp->sd_lock);
3327 return (error);
3332 mutex_exit(&stp->sd_lock);
3334 switch (cmd) {
3335 default:
3337 * The stream head has hardcoded knowledge of a
3338 * miscellaneous collection of terminal-, keyboard- and
3339 * mouse-related ioctls, enumerated below. This hardcoded
3340 * knowledge allows the stream head to automatically
3341 * convert transparent ioctl requests made by userland
3342 * programs into I_STR ioctls which many old STREAMS
3343 * modules and drivers require.
3345 * No new ioctls should ever be added to this list.
3346 * Instead, the STREAMS module or driver should be written
3347 * to either handle transparent ioctls or require any
3348 * userland programs to use I_STR ioctls (by returning
3349 * EINVAL to any transparent ioctl requests).
3351 * More importantly, removing ioctls from this list should
3352 * be done with the utmost care, since our STREAMS modules
3353 * and drivers *count* on the stream head performing this
3354 * conversion, and thus may panic while processing
3355 * transparent ioctl request for one of these ioctls (keep
3356 * in mind that third party modules and drivers may have
3357 * similar problems).
3359 if (((cmd & IOCTYPE) == LDIOC) ||
3360 ((cmd & IOCTYPE) == tIOC) ||
3361 ((cmd & IOCTYPE) == TIOC) ||
3362 ((cmd & IOCTYPE) == KIOC) ||
3363 ((cmd & IOCTYPE) == MSIOC) ||
3364 ((cmd & IOCTYPE) == VUIOC)) {
3366 * The ioctl is a tty ioctl - set up strioc buffer
3367 * and call strdoioctl() to do the work.
3369 if (stp->sd_flag & STRHUP)
3370 return (ENXIO);
3371 strioc.ic_cmd = cmd;
3372 strioc.ic_timout = INFTIM;
3374 switch (cmd) {
3376 case TCXONC:
3377 case TCSBRK:
3378 case TCFLSH:
3379 case TCDSET:
3381 int native_arg = (int)arg;
3382 strioc.ic_len = sizeof (int);
3383 strioc.ic_dp = (char *)&native_arg;
3384 return (strdoioctl(stp, &strioc, flag,
3385 K_TO_K, crp, rvalp));
3388 case TCSETA:
3389 case TCSETAW:
3390 case TCSETAF:
3391 strioc.ic_len = sizeof (struct termio);
3392 strioc.ic_dp = (char *)arg;
3393 return (strdoioctl(stp, &strioc, flag,
3394 copyflag, crp, rvalp));
3396 case TCSETS:
3397 case TCSETSW:
3398 case TCSETSF:
3399 strioc.ic_len = sizeof (struct termios);
3400 strioc.ic_dp = (char *)arg;
3401 return (strdoioctl(stp, &strioc, flag,
3402 copyflag, crp, rvalp));
3404 case LDSETT:
3405 strioc.ic_len = sizeof (struct termcb);
3406 strioc.ic_dp = (char *)arg;
3407 return (strdoioctl(stp, &strioc, flag,
3408 copyflag, crp, rvalp));
3410 case TIOCSETP:
3411 strioc.ic_len = sizeof (struct sgttyb);
3412 strioc.ic_dp = (char *)arg;
3413 return (strdoioctl(stp, &strioc, flag,
3414 copyflag, crp, rvalp));
3416 case TIOCSTI:
3417 if ((flag & FREAD) == 0 &&
3418 secpolicy_sti(crp) != 0) {
3419 return (EPERM);
3421 mutex_enter(&stp->sd_lock);
3422 mutex_enter(&curproc->p_splock);
3423 if (stp->sd_sidp != curproc->p_sessp->s_sidp &&
3424 secpolicy_sti(crp) != 0) {
3425 mutex_exit(&curproc->p_splock);
3426 mutex_exit(&stp->sd_lock);
3427 return (EACCES);
3429 mutex_exit(&curproc->p_splock);
3430 mutex_exit(&stp->sd_lock);
3432 strioc.ic_len = sizeof (char);
3433 strioc.ic_dp = (char *)arg;
3434 return (strdoioctl(stp, &strioc, flag,
3435 copyflag, crp, rvalp));
3437 case TIOCSWINSZ:
3438 strioc.ic_len = sizeof (struct winsize);
3439 strioc.ic_dp = (char *)arg;
3440 return (strdoioctl(stp, &strioc, flag,
3441 copyflag, crp, rvalp));
3443 case TIOCSSIZE:
3444 strioc.ic_len = sizeof (struct ttysize);
3445 strioc.ic_dp = (char *)arg;
3446 return (strdoioctl(stp, &strioc, flag,
3447 copyflag, crp, rvalp));
3449 case TIOCSSOFTCAR:
3450 case KIOCTRANS:
3451 case KIOCTRANSABLE:
3452 case KIOCCMD:
3453 case KIOCSDIRECT:
3454 case KIOCSCOMPAT:
3455 case KIOCSKABORTEN:
3456 case KIOCSRPTDELAY:
3457 case KIOCSRPTRATE:
3458 case VUIDSFORMAT:
3459 case TIOCSPPS:
3460 strioc.ic_len = sizeof (int);
3461 strioc.ic_dp = (char *)arg;
3462 return (strdoioctl(stp, &strioc, flag,
3463 copyflag, crp, rvalp));
3465 case KIOCSETKEY:
3466 case KIOCGETKEY:
3467 strioc.ic_len = sizeof (struct kiockey);
3468 strioc.ic_dp = (char *)arg;
3469 return (strdoioctl(stp, &strioc, flag,
3470 copyflag, crp, rvalp));
3472 case KIOCSKEY:
3473 case KIOCGKEY:
3474 strioc.ic_len = sizeof (struct kiockeymap);
3475 strioc.ic_dp = (char *)arg;
3476 return (strdoioctl(stp, &strioc, flag,
3477 copyflag, crp, rvalp));
3479 case KIOCSLED:
3480 /* arg is a pointer to char */
3481 strioc.ic_len = sizeof (char);
3482 strioc.ic_dp = (char *)arg;
3483 return (strdoioctl(stp, &strioc, flag,
3484 copyflag, crp, rvalp));
3486 case MSIOSETPARMS:
3487 strioc.ic_len = sizeof (Ms_parms);
3488 strioc.ic_dp = (char *)arg;
3489 return (strdoioctl(stp, &strioc, flag,
3490 copyflag, crp, rvalp));
3492 case VUIDSADDR:
3493 case VUIDGADDR:
3494 strioc.ic_len = sizeof (struct vuid_addr_probe);
3495 strioc.ic_dp = (char *)arg;
3496 return (strdoioctl(stp, &strioc, flag,
3497 copyflag, crp, rvalp));
3500 * These M_IOCTL's don't require any data to be sent
3501 * downstream, and the driver will allocate and link
3502 * on its own mblk_t upon M_IOCACK -- thus we set
3503 * ic_len to zero and set ic_dp to arg so we know
3504 * where to copyout to later.
3506 case TIOCGSOFTCAR:
3507 case TIOCGWINSZ:
3508 case TIOCGSIZE:
3509 case KIOCGTRANS:
3510 case KIOCGTRANSABLE:
3511 case KIOCTYPE:
3512 case KIOCGDIRECT:
3513 case KIOCGCOMPAT:
3514 case KIOCLAYOUT:
3515 case KIOCGLED:
3516 case MSIOGETPARMS:
3517 case MSIOBUTTONS:
3518 case VUIDGFORMAT:
3519 case TIOCGPPS:
3520 case TIOCGPPSEV:
3521 case TCGETA:
3522 case TCGETS:
3523 case LDGETT:
3524 case TIOCGETP:
3525 case KIOCGRPTDELAY:
3526 case KIOCGRPTRATE:
3527 strioc.ic_len = 0;
3528 strioc.ic_dp = (char *)arg;
3529 return (strdoioctl(stp, &strioc, flag,
3530 copyflag, crp, rvalp));
3535 * Unknown cmd - send it down as a transparent ioctl.
3537 strioc.ic_cmd = cmd;
3538 strioc.ic_timout = INFTIM;
3539 strioc.ic_len = TRANSPARENT;
3540 strioc.ic_dp = (char *)&arg;
3542 return (strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp));
3544 case I_STR:
3546 * Stream ioctl. Read in an strioctl buffer from the user
3547 * along with any data specified and send it downstream.
3548 * Strdoioctl will wait allow only one ioctl message at
3549 * a time, and waits for the acknowledgement.
3552 if (stp->sd_flag & STRHUP)
3553 return (ENXIO);
3555 error = strcopyin_strioctl((void *)arg, &strioc, flag,
3556 copyflag);
3557 if (error != 0)
3558 return (error);
3560 if ((strioc.ic_len < 0) || (strioc.ic_timout < -1))
3561 return (EINVAL);
3563 access = job_control_type(strioc.ic_cmd);
3564 mutex_enter(&stp->sd_lock);
3565 if ((access != -1) &&
3566 ((error = i_straccess(stp, access)) != 0)) {
3567 mutex_exit(&stp->sd_lock);
3568 return (error);
3570 mutex_exit(&stp->sd_lock);
3573 * The I_STR facility provides a trap door for malicious
3574 * code to send down bogus streamio(7I) ioctl commands to
3575 * unsuspecting STREAMS modules and drivers which expect to
3576 * only get these messages from the stream head.
3577 * Explicitly prohibit any streamio ioctls which can be
3578 * passed downstream by the stream head. Note that we do
3579 * not block all streamio ioctls because the ioctl
3580 * numberspace is not well managed and thus it's possible
3581 * that a module or driver's ioctl numbers may accidentally
3582 * collide with them.
3584 switch (strioc.ic_cmd) {
3585 case I_LINK:
3586 case I_PLINK:
3587 case I_UNLINK:
3588 case I_PUNLINK:
3589 case _I_GETPEERCRED:
3590 case _I_PLINK_LH:
3591 return (EINVAL);
3594 error = strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp);
3595 if (error == 0) {
3596 error = strcopyout_strioctl(&strioc, (void *)arg,
3597 flag, copyflag);
3599 return (error);
3601 case _I_CMD:
3603 * Like I_STR, but without using M_IOC* messages and without
3604 * copyins/copyouts beyond the passed-in argument.
3606 if (stp->sd_flag & STRHUP)
3607 return (ENXIO);
3609 if ((scp = kmem_alloc(sizeof (strcmd_t), KM_NOSLEEP)) == NULL)
3610 return (ENOMEM);
3612 if (copyin((void *)arg, scp, sizeof (strcmd_t))) {
3613 kmem_free(scp, sizeof (strcmd_t));
3614 return (EFAULT);
3617 access = job_control_type(scp->sc_cmd);
3618 mutex_enter(&stp->sd_lock);
3619 if (access != -1 && (error = i_straccess(stp, access)) != 0) {
3620 mutex_exit(&stp->sd_lock);
3621 kmem_free(scp, sizeof (strcmd_t));
3622 return (error);
3624 mutex_exit(&stp->sd_lock);
3626 *rvalp = 0;
3627 if ((error = strdocmd(stp, scp, crp)) == 0) {
3628 if (copyout(scp, (void *)arg, sizeof (strcmd_t)))
3629 error = EFAULT;
3631 kmem_free(scp, sizeof (strcmd_t));
3632 return (error);
3634 case I_NREAD:
3636 * Return number of bytes of data in first message
3637 * in queue in "arg" and return the number of messages
3638 * in queue in return value.
3641 size_t size;
3642 int retval;
3643 int count = 0;
3645 mutex_enter(QLOCK(rdq));
3647 size = msgdsize(rdq->q_first);
3648 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3649 count++;
3651 mutex_exit(QLOCK(rdq));
3652 if (stp->sd_struiordq) {
3653 infod_t infod;
3655 infod.d_cmd = INFOD_COUNT;
3656 infod.d_count = 0;
3657 if (count == 0) {
3658 infod.d_cmd |= INFOD_FIRSTBYTES;
3659 infod.d_bytes = 0;
3661 infod.d_res = 0;
3662 (void) infonext(rdq, &infod);
3663 count += infod.d_count;
3664 if (infod.d_res & INFOD_FIRSTBYTES)
3665 size = infod.d_bytes;
3669 * Drop down from size_t to the "int" required by the
3670 * interface. Cap at INT_MAX.
3672 retval = MIN(size, INT_MAX);
3673 error = strcopyout(&retval, (void *)arg, sizeof (retval),
3674 copyflag);
3675 if (!error)
3676 *rvalp = count;
3677 return (error);
3680 case FIONREAD:
3682 * Return number of bytes of data in all data messages
3683 * in queue in "arg".
3686 size_t size = 0;
3687 int retval;
3689 mutex_enter(QLOCK(rdq));
3690 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3691 size += msgdsize(mp);
3692 mutex_exit(QLOCK(rdq));
3694 if (stp->sd_struiordq) {
3695 infod_t infod;
3697 infod.d_cmd = INFOD_BYTES;
3698 infod.d_res = 0;
3699 infod.d_bytes = 0;
3700 (void) infonext(rdq, &infod);
3701 size += infod.d_bytes;
3705 * Drop down from size_t to the "int" required by the
3706 * interface. Cap at INT_MAX.
3708 retval = MIN(size, INT_MAX);
3709 error = strcopyout(&retval, (void *)arg, sizeof (retval),
3710 copyflag);
3712 *rvalp = 0;
3713 return (error);
3715 case FIORDCHK:
3717 * FIORDCHK does not use arg value (like FIONREAD),
3718 * instead a count is returned. I_NREAD value may
3719 * not be accurate but safe. The real thing to do is
3720 * to add the msgdsizes of all data messages until
3721 * a non-data message.
3724 size_t size = 0;
3726 mutex_enter(QLOCK(rdq));
3727 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3728 size += msgdsize(mp);
3729 mutex_exit(QLOCK(rdq));
3731 if (stp->sd_struiordq) {
3732 infod_t infod;
3734 infod.d_cmd = INFOD_BYTES;
3735 infod.d_res = 0;
3736 infod.d_bytes = 0;
3737 (void) infonext(rdq, &infod);
3738 size += infod.d_bytes;
3742 * Since ioctl returns an int, and memory sizes under
3743 * LP64 may not fit, we return INT_MAX if the count was
3744 * actually greater.
3746 *rvalp = MIN(size, INT_MAX);
3747 return (0);
3750 case I_FIND:
3752 * Get module name.
3755 char mname[FMNAMESZ + 1];
3756 queue_t *q;
3758 error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3759 mname, FMNAMESZ + 1, NULL);
3760 if (error)
3761 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3764 * Return EINVAL if we're handed a bogus module name.
3766 if (fmodsw_find(mname, FMODSW_LOAD) == NULL) {
3767 TRACE_0(TR_FAC_STREAMS_FR,
3768 TR_I_CANT_FIND, "couldn't I_FIND");
3769 return (EINVAL);
3772 *rvalp = 0;
3774 /* Look downstream to see if module is there. */
3775 claimstr(stp->sd_wrq);
3776 for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
3777 if (q->q_flag & QREADR) {
3778 q = NULL;
3779 break;
3781 if (strcmp(mname, Q2NAME(q)) == 0)
3782 break;
3784 releasestr(stp->sd_wrq);
3786 *rvalp = (q ? 1 : 0);
3787 return (error);
3790 case I_PUSH:
3791 case __I_PUSH_NOCTTY:
3793 * Push a module.
3794 * For the case __I_PUSH_NOCTTY push a module but
3795 * do not allocate controlling tty. See bugid 4025044
3799 char mname[FMNAMESZ + 1];
3800 fmodsw_impl_t *fp;
3801 dev_t dummydev;
3803 if (stp->sd_flag & STRHUP)
3804 return (ENXIO);
3807 * Get module name and look up in fmodsw.
3809 error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3810 mname, FMNAMESZ + 1, NULL);
3811 if (error)
3812 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3814 if ((fp = fmodsw_find(mname, FMODSW_HOLD | FMODSW_LOAD)) ==
3815 NULL)
3816 return (EINVAL);
3818 TRACE_2(TR_FAC_STREAMS_FR, TR_I_PUSH,
3819 "I_PUSH:fp %p stp %p", fp, stp);
3822 * If the module is flagged as single-instance, then check
3823 * to see if the module is already pushed. If it is, return
3824 * as if the push was successful.
3826 if (fp->f_qflag & _QSINGLE_INSTANCE) {
3827 queue_t *q;
3829 claimstr(stp->sd_wrq);
3830 for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
3831 if (q->q_flag & QREADR) {
3832 q = NULL;
3833 break;
3835 if (strcmp(mname, Q2NAME(q)) == 0)
3836 break;
3838 releasestr(stp->sd_wrq);
3839 if (q != NULL) {
3840 fmodsw_rele(fp);
3841 return (0);
3845 if (error = strstartplumb(stp, flag, cmd)) {
3846 fmodsw_rele(fp);
3847 return (error);
3851 * See if any more modules can be pushed on this stream.
3852 * Note that this check must be done after strstartplumb()
3853 * since otherwise multiple threads issuing I_PUSHes on
3854 * the same stream will be able to exceed nstrpush.
3856 mutex_enter(&stp->sd_lock);
3857 if (stp->sd_pushcnt >= nstrpush) {
3858 fmodsw_rele(fp);
3859 strendplumb(stp);
3860 mutex_exit(&stp->sd_lock);
3861 return (EINVAL);
3863 mutex_exit(&stp->sd_lock);
3866 * Push new module and call its open routine
3867 * via qattach(). Modules don't change device
3868 * numbers, so just ignore dummydev here.
3870 dummydev = vp->v_rdev;
3871 if ((error = qattach(rdq, &dummydev, 0, crp, fp,
3872 B_FALSE)) == 0) {
3873 if (vp->v_type == VCHR && /* sorry, no pipes allowed */
3874 (cmd == I_PUSH) && (stp->sd_flag & STRISTTY)) {
3876 * try to allocate it as a controlling terminal
3878 (void) strctty(stp);
3882 mutex_enter(&stp->sd_lock);
3885 * As a performance concern we are caching the values of
3886 * q_minpsz and q_maxpsz of the module below the stream
3887 * head in the stream head.
3889 mutex_enter(QLOCK(stp->sd_wrq->q_next));
3890 rmin = stp->sd_wrq->q_next->q_minpsz;
3891 rmax = stp->sd_wrq->q_next->q_maxpsz;
3892 mutex_exit(QLOCK(stp->sd_wrq->q_next));
3894 /* Do this processing here as a performance concern */
3895 if (strmsgsz != 0) {
3896 if (rmax == INFPSZ)
3897 rmax = strmsgsz;
3898 else {
3899 if (vp->v_type == VFIFO)
3900 rmax = MIN(PIPE_BUF, rmax);
3901 else rmax = MIN(strmsgsz, rmax);
3905 mutex_enter(QLOCK(wrq));
3906 stp->sd_qn_minpsz = rmin;
3907 stp->sd_qn_maxpsz = rmax;
3908 mutex_exit(QLOCK(wrq));
3910 strendplumb(stp);
3911 mutex_exit(&stp->sd_lock);
3912 return (error);
3915 case I_POP:
3917 queue_t *q;
3919 if (stp->sd_flag & STRHUP)
3920 return (ENXIO);
3921 if (!wrq->q_next) /* for broken pipes */
3922 return (EINVAL);
3924 if (error = strstartplumb(stp, flag, cmd))
3925 return (error);
3928 * If there is an anchor on this stream and popping
3929 * the current module would attempt to pop through the
3930 * anchor, then disallow the pop unless we have sufficient
3931 * privileges; take the cheapest (non-locking) check
3932 * first.
3934 if (secpolicy_ip_config(crp, B_TRUE) != 0 ||
3935 (stp->sd_anchorzone != crgetzoneid(crp))) {
3936 mutex_enter(&stp->sd_lock);
3938 * Anchors only apply if there's at least one
3939 * module on the stream (sd_pushcnt > 0).
3941 if (stp->sd_pushcnt > 0 &&
3942 stp->sd_pushcnt == stp->sd_anchor &&
3943 stp->sd_vnode->v_type != VFIFO) {
3944 strendplumb(stp);
3945 mutex_exit(&stp->sd_lock);
3946 if (stp->sd_anchorzone != crgetzoneid(crp))
3947 return (EINVAL);
3948 /* Audit and report error */
3949 return (secpolicy_ip_config(crp, B_FALSE));
3951 mutex_exit(&stp->sd_lock);
3954 q = wrq->q_next;
3955 TRACE_2(TR_FAC_STREAMS_FR, TR_I_POP,
3956 "I_POP:%p from %p", q, stp);
3957 if (q->q_next == NULL || (q->q_flag & (QREADR|QISDRV))) {
3958 error = EINVAL;
3959 } else {
3960 qdetach(_RD(q), 1, flag, crp, B_FALSE);
3961 error = 0;
3963 mutex_enter(&stp->sd_lock);
3966 * As a performance concern we are caching the values of
3967 * q_minpsz and q_maxpsz of the module below the stream
3968 * head in the stream head.
3970 mutex_enter(QLOCK(wrq->q_next));
3971 rmin = wrq->q_next->q_minpsz;
3972 rmax = wrq->q_next->q_maxpsz;
3973 mutex_exit(QLOCK(wrq->q_next));
3975 /* Do this processing here as a performance concern */
3976 if (strmsgsz != 0) {
3977 if (rmax == INFPSZ)
3978 rmax = strmsgsz;
3979 else {
3980 if (vp->v_type == VFIFO)
3981 rmax = MIN(PIPE_BUF, rmax);
3982 else rmax = MIN(strmsgsz, rmax);
3986 mutex_enter(QLOCK(wrq));
3987 stp->sd_qn_minpsz = rmin;
3988 stp->sd_qn_maxpsz = rmax;
3989 mutex_exit(QLOCK(wrq));
3991 /* If we popped through the anchor, then reset the anchor. */
3992 if (stp->sd_pushcnt < stp->sd_anchor) {
3993 stp->sd_anchor = 0;
3994 stp->sd_anchorzone = 0;
3996 strendplumb(stp);
3997 mutex_exit(&stp->sd_lock);
3998 return (error);
4001 case _I_MUXID2FD:
4004 * Create a fd for a I_PLINK'ed lower stream with a given
4005 * muxid. With the fd, application can send down ioctls,
4006 * like I_LIST, to the previously I_PLINK'ed stream. Note
4007 * that after getting the fd, the application has to do an
4008 * I_PUNLINK on the muxid before it can do any operation
4009 * on the lower stream. This is required by spec1170.
4011 * The fd used to do this ioctl should point to the same
4012 * controlling device used to do the I_PLINK. If it uses
4013 * a different stream or an invalid muxid, I_MUXID2FD will
4014 * fail. The error code is set to EINVAL.
4016 * The intended use of this interface is the following.
4017 * An application I_PLINK'ed a stream and exits. The fd
4018 * to the lower stream is gone. Another application
4019 * wants to get a fd to the lower stream, it uses I_MUXID2FD.
4021 int muxid = (int)arg;
4022 int fd;
4023 linkinfo_t *linkp;
4024 struct file *fp;
4025 netstack_t *ns;
4026 str_stack_t *ss;
4029 * Do not allow the wildcard muxid. This ioctl is not
4030 * intended to find arbitrary link.
4032 if (muxid == 0) {
4033 return (EINVAL);
4036 ns = netstack_find_by_cred(crp);
4037 ASSERT(ns != NULL);
4038 ss = ns->netstack_str;
4039 ASSERT(ss != NULL);
4041 mutex_enter(&muxifier);
4042 linkp = findlinks(vp->v_stream, muxid, LINKPERSIST, ss);
4043 if (linkp == NULL) {
4044 mutex_exit(&muxifier);
4045 netstack_rele(ss->ss_netstack);
4046 return (EINVAL);
4049 if ((fd = ufalloc(0)) == -1) {
4050 mutex_exit(&muxifier);
4051 netstack_rele(ss->ss_netstack);
4052 return (EMFILE);
4054 fp = linkp->li_fpdown;
4055 mutex_enter(&fp->f_tlock);
4056 fp->f_count++;
4057 mutex_exit(&fp->f_tlock);
4058 mutex_exit(&muxifier);
4059 setf(fd, fp);
4060 *rvalp = fd;
4061 netstack_rele(ss->ss_netstack);
4062 return (0);
4065 case _I_INSERT:
4068 * To insert a module to a given position in a stream.
4069 * In the first release, only allow privileged user
4070 * to use this ioctl. Furthermore, the insert is only allowed
4071 * below an anchor if the zoneid is the same as the zoneid
4072 * which created the anchor.
4074 * Note that we do not plan to support this ioctl
4075 * on pipes in the first release. We want to learn more
4076 * about the implications of these ioctls before extending
4077 * their support. And we do not think these features are
4078 * valuable for pipes.
4080 STRUCT_DECL(strmodconf, strmodinsert);
4081 char mod_name[FMNAMESZ + 1];
4082 fmodsw_impl_t *fp;
4083 dev_t dummydev;
4084 queue_t *tmp_wrq;
4085 int pos;
4086 boolean_t is_insert;
4088 STRUCT_INIT(strmodinsert, flag);
4089 if (stp->sd_flag & STRHUP)
4090 return (ENXIO);
4091 if (STRMATED(stp))
4092 return (EINVAL);
4093 if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4094 return (error);
4095 if (stp->sd_anchor != 0 &&
4096 stp->sd_anchorzone != crgetzoneid(crp))
4097 return (EINVAL);
4099 error = strcopyin((void *)arg, STRUCT_BUF(strmodinsert),
4100 STRUCT_SIZE(strmodinsert), copyflag);
4101 if (error)
4102 return (error);
4105 * Get module name and look up in fmodsw.
4107 error = (copyflag & U_TO_K ? copyinstr :
4108 copystr)(STRUCT_FGETP(strmodinsert, mod_name),
4109 mod_name, FMNAMESZ + 1, NULL);
4110 if (error)
4111 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4113 if ((fp = fmodsw_find(mod_name, FMODSW_HOLD | FMODSW_LOAD)) ==
4114 NULL)
4115 return (EINVAL);
4117 if (error = strstartplumb(stp, flag, cmd)) {
4118 fmodsw_rele(fp);
4119 return (error);
4123 * Is this _I_INSERT just like an I_PUSH? We need to know
4124 * this because we do some optimizations if this is a
4125 * module being pushed.
4127 pos = STRUCT_FGET(strmodinsert, pos);
4128 is_insert = (pos != 0);
4131 * Make sure pos is valid. Even though it is not an I_PUSH,
4132 * we impose the same limit on the number of modules in a
4133 * stream.
4135 mutex_enter(&stp->sd_lock);
4136 if (stp->sd_pushcnt >= nstrpush || pos < 0 ||
4137 pos > stp->sd_pushcnt) {
4138 fmodsw_rele(fp);
4139 strendplumb(stp);
4140 mutex_exit(&stp->sd_lock);
4141 return (EINVAL);
4143 if (stp->sd_anchor != 0) {
4145 * Is this insert below the anchor?
4146 * Pushcnt hasn't been increased yet hence
4147 * we test for greater than here, and greater or
4148 * equal after qattach.
4150 if (pos > (stp->sd_pushcnt - stp->sd_anchor) &&
4151 stp->sd_anchorzone != crgetzoneid(crp)) {
4152 fmodsw_rele(fp);
4153 strendplumb(stp);
4154 mutex_exit(&stp->sd_lock);
4155 return (EPERM);
4159 mutex_exit(&stp->sd_lock);
4162 * First find the correct position this module to
4163 * be inserted. We don't need to call claimstr()
4164 * as the stream should not be changing at this point.
4166 * Insert new module and call its open routine
4167 * via qattach(). Modules don't change device
4168 * numbers, so just ignore dummydev here.
4170 for (tmp_wrq = stp->sd_wrq; pos > 0;
4171 tmp_wrq = tmp_wrq->q_next, pos--) {
4172 ASSERT(SAMESTR(tmp_wrq));
4174 dummydev = vp->v_rdev;
4175 if ((error = qattach(_RD(tmp_wrq), &dummydev, 0, crp,
4176 fp, is_insert)) != 0) {
4177 mutex_enter(&stp->sd_lock);
4178 strendplumb(stp);
4179 mutex_exit(&stp->sd_lock);
4180 return (error);
4183 mutex_enter(&stp->sd_lock);
4186 * As a performance concern we are caching the values of
4187 * q_minpsz and q_maxpsz of the module below the stream
4188 * head in the stream head.
4190 if (!is_insert) {
4191 mutex_enter(QLOCK(stp->sd_wrq->q_next));
4192 rmin = stp->sd_wrq->q_next->q_minpsz;
4193 rmax = stp->sd_wrq->q_next->q_maxpsz;
4194 mutex_exit(QLOCK(stp->sd_wrq->q_next));
4196 /* Do this processing here as a performance concern */
4197 if (strmsgsz != 0) {
4198 if (rmax == INFPSZ) {
4199 rmax = strmsgsz;
4200 } else {
4201 rmax = MIN(strmsgsz, rmax);
4205 mutex_enter(QLOCK(wrq));
4206 stp->sd_qn_minpsz = rmin;
4207 stp->sd_qn_maxpsz = rmax;
4208 mutex_exit(QLOCK(wrq));
4212 * Need to update the anchor value if this module is
4213 * inserted below the anchor point.
4215 if (stp->sd_anchor != 0) {
4216 pos = STRUCT_FGET(strmodinsert, pos);
4217 if (pos >= (stp->sd_pushcnt - stp->sd_anchor))
4218 stp->sd_anchor++;
4221 strendplumb(stp);
4222 mutex_exit(&stp->sd_lock);
4223 return (0);
4226 case _I_REMOVE:
4229 * To remove a module with a given name in a stream. The
4230 * caller of this ioctl needs to provide both the name and
4231 * the position of the module to be removed. This eliminates
4232 * the ambiguity of removal if a module is inserted/pushed
4233 * multiple times in a stream. In the first release, only
4234 * allow privileged user to use this ioctl.
4235 * Furthermore, the remove is only allowed
4236 * below an anchor if the zoneid is the same as the zoneid
4237 * which created the anchor.
4239 * Note that we do not plan to support this ioctl
4240 * on pipes in the first release. We want to learn more
4241 * about the implications of these ioctls before extending
4242 * their support. And we do not think these features are
4243 * valuable for pipes.
4245 * Also note that _I_REMOVE cannot be used to remove a
4246 * driver or the stream head.
4248 STRUCT_DECL(strmodconf, strmodremove);
4249 queue_t *q;
4250 int pos;
4251 char mod_name[FMNAMESZ + 1];
4252 boolean_t is_remove;
4254 STRUCT_INIT(strmodremove, flag);
4255 if (stp->sd_flag & STRHUP)
4256 return (ENXIO);
4257 if (STRMATED(stp))
4258 return (EINVAL);
4259 if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4260 return (error);
4261 if (stp->sd_anchor != 0 &&
4262 stp->sd_anchorzone != crgetzoneid(crp))
4263 return (EINVAL);
4265 error = strcopyin((void *)arg, STRUCT_BUF(strmodremove),
4266 STRUCT_SIZE(strmodremove), copyflag);
4267 if (error)
4268 return (error);
4270 error = (copyflag & U_TO_K ? copyinstr :
4271 copystr)(STRUCT_FGETP(strmodremove, mod_name),
4272 mod_name, FMNAMESZ + 1, NULL);
4273 if (error)
4274 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4276 if ((error = strstartplumb(stp, flag, cmd)) != 0)
4277 return (error);
4280 * Match the name of given module to the name of module at
4281 * the given position.
4283 pos = STRUCT_FGET(strmodremove, pos);
4285 is_remove = (pos != 0);
4286 for (q = stp->sd_wrq->q_next; SAMESTR(q) && pos > 0;
4287 q = q->q_next, pos--)
4289 if (pos > 0 || !SAMESTR(q) ||
4290 strcmp(Q2NAME(q), mod_name) != 0) {
4291 mutex_enter(&stp->sd_lock);
4292 strendplumb(stp);
4293 mutex_exit(&stp->sd_lock);
4294 return (EINVAL);
4298 * If the position is at or below an anchor, then the zoneid
4299 * must match the zoneid that created the anchor.
4301 if (stp->sd_anchor != 0) {
4302 pos = STRUCT_FGET(strmodremove, pos);
4303 if (pos >= (stp->sd_pushcnt - stp->sd_anchor) &&
4304 stp->sd_anchorzone != crgetzoneid(crp)) {
4305 mutex_enter(&stp->sd_lock);
4306 strendplumb(stp);
4307 mutex_exit(&stp->sd_lock);
4308 return (EPERM);
4313 ASSERT(!(q->q_flag & QREADR));
4314 qdetach(_RD(q), 1, flag, crp, is_remove);
4316 mutex_enter(&stp->sd_lock);
4319 * As a performance concern we are caching the values of
4320 * q_minpsz and q_maxpsz of the module below the stream
4321 * head in the stream head.
4323 if (!is_remove) {
4324 mutex_enter(QLOCK(wrq->q_next));
4325 rmin = wrq->q_next->q_minpsz;
4326 rmax = wrq->q_next->q_maxpsz;
4327 mutex_exit(QLOCK(wrq->q_next));
4329 /* Do this processing here as a performance concern */
4330 if (strmsgsz != 0) {
4331 if (rmax == INFPSZ)
4332 rmax = strmsgsz;
4333 else {
4334 if (vp->v_type == VFIFO)
4335 rmax = MIN(PIPE_BUF, rmax);
4336 else rmax = MIN(strmsgsz, rmax);
4340 mutex_enter(QLOCK(wrq));
4341 stp->sd_qn_minpsz = rmin;
4342 stp->sd_qn_maxpsz = rmax;
4343 mutex_exit(QLOCK(wrq));
4347 * Need to update the anchor value if this module is removed
4348 * at or below the anchor point. If the removed module is at
4349 * the anchor point, remove the anchor for this stream if
4350 * there is no module above the anchor point. Otherwise, if
4351 * the removed module is below the anchor point, decrement the
4352 * anchor point by 1.
4354 if (stp->sd_anchor != 0) {
4355 pos = STRUCT_FGET(strmodremove, pos);
4356 if (pos == stp->sd_pushcnt - stp->sd_anchor + 1)
4357 stp->sd_anchor = 0;
4358 else if (pos > (stp->sd_pushcnt - stp->sd_anchor + 1))
4359 stp->sd_anchor--;
4362 strendplumb(stp);
4363 mutex_exit(&stp->sd_lock);
4364 return (0);
4367 case I_ANCHOR:
4369 * Set the anchor position on the stream to reside at
4370 * the top module (in other words, the top module
4371 * cannot be popped). Anchors with a FIFO make no
4372 * obvious sense, so they're not allowed.
4374 mutex_enter(&stp->sd_lock);
4376 if (stp->sd_vnode->v_type == VFIFO) {
4377 mutex_exit(&stp->sd_lock);
4378 return (EINVAL);
4380 /* Only allow the same zoneid to update the anchor */
4381 if (stp->sd_anchor != 0 &&
4382 stp->sd_anchorzone != crgetzoneid(crp)) {
4383 mutex_exit(&stp->sd_lock);
4384 return (EINVAL);
4386 stp->sd_anchor = stp->sd_pushcnt;
4387 stp->sd_anchorzone = crgetzoneid(crp);
4388 mutex_exit(&stp->sd_lock);
4389 return (0);
4391 case I_LOOK:
4393 * Get name of first module downstream.
4394 * If no module, return an error.
4396 claimstr(wrq);
4397 if (_SAMESTR(wrq) && wrq->q_next->q_next != NULL) {
4398 char *name = Q2NAME(wrq->q_next);
4400 error = strcopyout(name, (void *)arg, strlen(name) + 1,
4401 copyflag);
4402 releasestr(wrq);
4403 return (error);
4405 releasestr(wrq);
4406 return (EINVAL);
4408 case I_LINK:
4409 case I_PLINK:
4411 * Link a multiplexor.
4413 return (mlink(vp, cmd, (int)arg, crp, rvalp, 0));
4415 case _I_PLINK_LH:
4417 * Link a multiplexor: Call must originate from kernel.
4419 if (kioctl)
4420 return (ldi_mlink_lh(vp, cmd, arg, crp, rvalp));
4422 return (EINVAL);
4423 case I_UNLINK:
4424 case I_PUNLINK:
4426 * Unlink a multiplexor.
4427 * If arg is -1, unlink all links for which this is the
4428 * controlling stream. Otherwise, arg is an index number
4429 * for a link to be removed.
4432 struct linkinfo *linkp;
4433 int native_arg = (int)arg;
4434 int type;
4435 netstack_t *ns;
4436 str_stack_t *ss;
4438 TRACE_1(TR_FAC_STREAMS_FR,
4439 TR_I_UNLINK, "I_UNLINK/I_PUNLINK:%p", stp);
4440 if (vp->v_type == VFIFO) {
4441 return (EINVAL);
4443 if (cmd == I_UNLINK)
4444 type = LINKNORMAL;
4445 else /* I_PUNLINK */
4446 type = LINKPERSIST;
4447 if (native_arg == 0) {
4448 return (EINVAL);
4450 ns = netstack_find_by_cred(crp);
4451 ASSERT(ns != NULL);
4452 ss = ns->netstack_str;
4453 ASSERT(ss != NULL);
4455 if (native_arg == MUXID_ALL)
4456 error = munlinkall(stp, type, crp, rvalp, ss);
4457 else {
4458 mutex_enter(&muxifier);
4459 if (!(linkp = findlinks(stp, (int)arg, type, ss))) {
4460 /* invalid user supplied index number */
4461 mutex_exit(&muxifier);
4462 netstack_rele(ss->ss_netstack);
4463 return (EINVAL);
4465 /* munlink drops the muxifier lock */
4466 error = munlink(stp, linkp, type, crp, rvalp, ss);
4468 netstack_rele(ss->ss_netstack);
4469 return (error);
4472 case I_FLUSH:
4474 * send a flush message downstream
4475 * flush message can indicate
4476 * FLUSHR - flush read queue
4477 * FLUSHW - flush write queue
4478 * FLUSHRW - flush read/write queue
4480 if (stp->sd_flag & STRHUP)
4481 return (ENXIO);
4482 if (arg & ~FLUSHRW)
4483 return (EINVAL);
4485 for (;;) {
4486 if (putnextctl1(stp->sd_wrq, M_FLUSH, (int)arg)) {
4487 break;
4489 if (error = strwaitbuf(1, BPRI_HI)) {
4490 return (error);
4495 * Send down an unsupported ioctl and wait for the nack
4496 * in order to allow the M_FLUSH to propagate back
4497 * up to the stream head.
4498 * Replaces if (qready()) runqueues();
4500 strioc.ic_cmd = -1; /* The unsupported ioctl */
4501 strioc.ic_timout = 0;
4502 strioc.ic_len = 0;
4503 strioc.ic_dp = NULL;
4504 (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4505 *rvalp = 0;
4506 return (0);
4508 case I_FLUSHBAND:
4510 struct bandinfo binfo;
4512 error = strcopyin((void *)arg, &binfo, sizeof (binfo),
4513 copyflag);
4514 if (error)
4515 return (error);
4516 if (stp->sd_flag & STRHUP)
4517 return (ENXIO);
4518 if (binfo.bi_flag & ~FLUSHRW)
4519 return (EINVAL);
4520 while (!(mp = allocb(2, BPRI_HI))) {
4521 if (error = strwaitbuf(2, BPRI_HI))
4522 return (error);
4524 mp->b_datap->db_type = M_FLUSH;
4525 *mp->b_wptr++ = binfo.bi_flag | FLUSHBAND;
4526 *mp->b_wptr++ = binfo.bi_pri;
4527 putnext(stp->sd_wrq, mp);
4529 * Send down an unsupported ioctl and wait for the nack
4530 * in order to allow the M_FLUSH to propagate back
4531 * up to the stream head.
4532 * Replaces if (qready()) runqueues();
4534 strioc.ic_cmd = -1; /* The unsupported ioctl */
4535 strioc.ic_timout = 0;
4536 strioc.ic_len = 0;
4537 strioc.ic_dp = NULL;
4538 (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4539 *rvalp = 0;
4540 return (0);
4543 case I_SRDOPT:
4545 * Set read options
4547 * RNORM - default stream mode
4548 * RMSGN - message no discard
4549 * RMSGD - message discard
4550 * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
4551 * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
4552 * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
4554 if (arg & ~(RMODEMASK | RPROTMASK))
4555 return (EINVAL);
4557 if ((arg & (RMSGD|RMSGN)) == (RMSGD|RMSGN))
4558 return (EINVAL);
4560 mutex_enter(&stp->sd_lock);
4561 switch (arg & RMODEMASK) {
4562 case RNORM:
4563 stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
4564 break;
4565 case RMSGD:
4566 stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGNODIS) |
4567 RD_MSGDIS;
4568 break;
4569 case RMSGN:
4570 stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGDIS) |
4571 RD_MSGNODIS;
4572 break;
4575 switch (arg & RPROTMASK) {
4576 case RPROTNORM:
4577 stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
4578 break;
4580 case RPROTDAT:
4581 stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDIS) |
4582 RD_PROTDAT);
4583 break;
4585 case RPROTDIS:
4586 stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDAT) |
4587 RD_PROTDIS);
4588 break;
4590 mutex_exit(&stp->sd_lock);
4591 return (0);
4593 case I_GRDOPT:
4595 * Get read option and return the value
4596 * to spot pointed to by arg
4599 int rdopt;
4601 rdopt = ((stp->sd_read_opt & RD_MSGDIS) ? RMSGD :
4602 ((stp->sd_read_opt & RD_MSGNODIS) ? RMSGN : RNORM));
4603 rdopt |= ((stp->sd_read_opt & RD_PROTDAT) ? RPROTDAT :
4604 ((stp->sd_read_opt & RD_PROTDIS) ? RPROTDIS : RPROTNORM));
4606 return (strcopyout(&rdopt, (void *)arg, sizeof (int),
4607 copyflag));
4610 case I_SERROPT:
4612 * Set error options
4614 * RERRNORM - persistent read errors
4615 * RERRNONPERSIST - non-persistent read errors
4616 * WERRNORM - persistent write errors
4617 * WERRNONPERSIST - non-persistent write errors
4619 if (arg & ~(RERRMASK | WERRMASK))
4620 return (EINVAL);
4622 mutex_enter(&stp->sd_lock);
4623 switch (arg & RERRMASK) {
4624 case RERRNORM:
4625 stp->sd_flag &= ~STRDERRNONPERSIST;
4626 break;
4627 case RERRNONPERSIST:
4628 stp->sd_flag |= STRDERRNONPERSIST;
4629 break;
4631 switch (arg & WERRMASK) {
4632 case WERRNORM:
4633 stp->sd_flag &= ~STWRERRNONPERSIST;
4634 break;
4635 case WERRNONPERSIST:
4636 stp->sd_flag |= STWRERRNONPERSIST;
4637 break;
4639 mutex_exit(&stp->sd_lock);
4640 return (0);
4642 case I_GERROPT:
4644 * Get error option and return the value
4645 * to spot pointed to by arg
4648 int erropt = 0;
4650 erropt |= (stp->sd_flag & STRDERRNONPERSIST) ? RERRNONPERSIST :
4651 RERRNORM;
4652 erropt |= (stp->sd_flag & STWRERRNONPERSIST) ? WERRNONPERSIST :
4653 WERRNORM;
4654 return (strcopyout(&erropt, (void *)arg, sizeof (int),
4655 copyflag));
4658 case I_SETSIG:
4660 * Register the calling proc to receive the SIGPOLL
4661 * signal based on the events given in arg. If
4662 * arg is zero, remove the proc from register list.
4665 strsig_t *ssp, *pssp;
4666 struct pid *pidp;
4668 pssp = NULL;
4669 pidp = curproc->p_pidp;
4671 * Hold sd_lock to prevent traversal of sd_siglist while
4672 * it is modified.
4674 mutex_enter(&stp->sd_lock);
4675 for (ssp = stp->sd_siglist; ssp && (ssp->ss_pidp != pidp);
4676 pssp = ssp, ssp = ssp->ss_next)
4679 if (arg) {
4680 if (arg & ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4681 S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4682 mutex_exit(&stp->sd_lock);
4683 return (EINVAL);
4685 if ((arg & S_BANDURG) && !(arg & S_RDBAND)) {
4686 mutex_exit(&stp->sd_lock);
4687 return (EINVAL);
4691 * If proc not already registered, add it
4692 * to list.
4694 if (!ssp) {
4695 ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4696 ssp->ss_pidp = pidp;
4697 ssp->ss_pid = pidp->pid_id;
4698 ssp->ss_next = NULL;
4699 if (pssp)
4700 pssp->ss_next = ssp;
4701 else
4702 stp->sd_siglist = ssp;
4703 mutex_enter(&pidlock);
4704 PID_HOLD(pidp);
4705 mutex_exit(&pidlock);
4709 * Set events.
4711 ssp->ss_events = (int)arg;
4712 } else {
4714 * Remove proc from register list.
4716 if (ssp) {
4717 mutex_enter(&pidlock);
4718 PID_RELE(pidp);
4719 mutex_exit(&pidlock);
4720 if (pssp)
4721 pssp->ss_next = ssp->ss_next;
4722 else
4723 stp->sd_siglist = ssp->ss_next;
4724 kmem_free(ssp, sizeof (strsig_t));
4725 } else {
4726 mutex_exit(&stp->sd_lock);
4727 return (EINVAL);
4732 * Recalculate OR of sig events.
4734 stp->sd_sigflags = 0;
4735 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4736 stp->sd_sigflags |= ssp->ss_events;
4737 mutex_exit(&stp->sd_lock);
4738 return (0);
4741 case I_GETSIG:
4743 * Return (in arg) the current registration of events
4744 * for which the calling proc is to be signaled.
4747 struct strsig *ssp;
4748 struct pid *pidp;
4750 pidp = curproc->p_pidp;
4751 mutex_enter(&stp->sd_lock);
4752 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4753 if (ssp->ss_pidp == pidp) {
4754 error = strcopyout(&ssp->ss_events, (void *)arg,
4755 sizeof (int), copyflag);
4756 mutex_exit(&stp->sd_lock);
4757 return (error);
4759 mutex_exit(&stp->sd_lock);
4760 return (EINVAL);
4763 case I_ESETSIG:
4765 * Register the ss_pid to receive the SIGPOLL
4766 * signal based on the events is ss_events arg. If
4767 * ss_events is zero, remove the proc from register list.
4770 struct strsig *ssp, *pssp;
4771 struct proc *proc;
4772 struct pid *pidp;
4773 pid_t pid;
4774 struct strsigset ss;
4776 error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4777 if (error)
4778 return (error);
4780 pid = ss.ss_pid;
4782 if (ss.ss_events != 0) {
4784 * Permissions check by sending signal 0.
4785 * Note that when kill fails it does a set_errno
4786 * causing the system call to fail.
4788 error = kill(pid, 0);
4789 if (error) {
4790 return (error);
4793 mutex_enter(&pidlock);
4794 if (pid == 0)
4795 proc = curproc;
4796 else if (pid < 0)
4797 proc = pgfind(-pid);
4798 else
4799 proc = prfind(pid);
4800 if (proc == NULL) {
4801 mutex_exit(&pidlock);
4802 return (ESRCH);
4804 if (pid < 0)
4805 pidp = proc->p_pgidp;
4806 else
4807 pidp = proc->p_pidp;
4808 ASSERT(pidp);
4810 * Get a hold on the pid structure while referencing it.
4811 * There is a separate PID_HOLD should it be inserted
4812 * in the list below.
4814 PID_HOLD(pidp);
4815 mutex_exit(&pidlock);
4817 pssp = NULL;
4819 * Hold sd_lock to prevent traversal of sd_siglist while
4820 * it is modified.
4822 mutex_enter(&stp->sd_lock);
4823 for (ssp = stp->sd_siglist; ssp && (ssp->ss_pid != pid);
4824 pssp = ssp, ssp = ssp->ss_next)
4827 if (ss.ss_events) {
4828 if (ss.ss_events &
4829 ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4830 S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4831 mutex_exit(&stp->sd_lock);
4832 mutex_enter(&pidlock);
4833 PID_RELE(pidp);
4834 mutex_exit(&pidlock);
4835 return (EINVAL);
4837 if ((ss.ss_events & S_BANDURG) &&
4838 !(ss.ss_events & S_RDBAND)) {
4839 mutex_exit(&stp->sd_lock);
4840 mutex_enter(&pidlock);
4841 PID_RELE(pidp);
4842 mutex_exit(&pidlock);
4843 return (EINVAL);
4847 * If proc not already registered, add it
4848 * to list.
4850 if (!ssp) {
4851 ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4852 ssp->ss_pidp = pidp;
4853 ssp->ss_pid = pid;
4854 ssp->ss_next = NULL;
4855 if (pssp)
4856 pssp->ss_next = ssp;
4857 else
4858 stp->sd_siglist = ssp;
4859 mutex_enter(&pidlock);
4860 PID_HOLD(pidp);
4861 mutex_exit(&pidlock);
4865 * Set events.
4867 ssp->ss_events = ss.ss_events;
4868 } else {
4870 * Remove proc from register list.
4872 if (ssp) {
4873 mutex_enter(&pidlock);
4874 PID_RELE(pidp);
4875 mutex_exit(&pidlock);
4876 if (pssp)
4877 pssp->ss_next = ssp->ss_next;
4878 else
4879 stp->sd_siglist = ssp->ss_next;
4880 kmem_free(ssp, sizeof (strsig_t));
4881 } else {
4882 mutex_exit(&stp->sd_lock);
4883 mutex_enter(&pidlock);
4884 PID_RELE(pidp);
4885 mutex_exit(&pidlock);
4886 return (EINVAL);
4891 * Recalculate OR of sig events.
4893 stp->sd_sigflags = 0;
4894 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4895 stp->sd_sigflags |= ssp->ss_events;
4896 mutex_exit(&stp->sd_lock);
4897 mutex_enter(&pidlock);
4898 PID_RELE(pidp);
4899 mutex_exit(&pidlock);
4900 return (0);
4903 case I_EGETSIG:
4905 * Return (in arg) the current registration of events
4906 * for which the calling proc is to be signaled.
4909 struct strsig *ssp;
4910 struct proc *proc;
4911 pid_t pid;
4912 struct pid *pidp;
4913 struct strsigset ss;
4915 error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4916 if (error)
4917 return (error);
4919 pid = ss.ss_pid;
4920 mutex_enter(&pidlock);
4921 if (pid == 0)
4922 proc = curproc;
4923 else if (pid < 0)
4924 proc = pgfind(-pid);
4925 else
4926 proc = prfind(pid);
4927 if (proc == NULL) {
4928 mutex_exit(&pidlock);
4929 return (ESRCH);
4931 if (pid < 0)
4932 pidp = proc->p_pgidp;
4933 else
4934 pidp = proc->p_pidp;
4936 /* Prevent the pidp from being reassigned */
4937 PID_HOLD(pidp);
4938 mutex_exit(&pidlock);
4940 mutex_enter(&stp->sd_lock);
4941 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4942 if (ssp->ss_pid == pid) {
4943 ss.ss_pid = ssp->ss_pid;
4944 ss.ss_events = ssp->ss_events;
4945 error = strcopyout(&ss, (void *)arg,
4946 sizeof (struct strsigset), copyflag);
4947 mutex_exit(&stp->sd_lock);
4948 mutex_enter(&pidlock);
4949 PID_RELE(pidp);
4950 mutex_exit(&pidlock);
4951 return (error);
4953 mutex_exit(&stp->sd_lock);
4954 mutex_enter(&pidlock);
4955 PID_RELE(pidp);
4956 mutex_exit(&pidlock);
4957 return (EINVAL);
4960 case I_PEEK:
4962 STRUCT_DECL(strpeek, strpeek);
4963 size_t n;
4964 mblk_t *fmp, *tmp_mp = NULL;
4966 STRUCT_INIT(strpeek, flag);
4968 error = strcopyin((void *)arg, STRUCT_BUF(strpeek),
4969 STRUCT_SIZE(strpeek), copyflag);
4970 if (error)
4971 return (error);
4973 mutex_enter(QLOCK(rdq));
4975 * Skip the invalid messages
4977 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
4978 if (mp->b_datap->db_type != M_SIG)
4979 break;
4982 * If user has requested to peek at a high priority message
4983 * and first message is not, return 0
4985 if (mp != NULL) {
4986 if ((STRUCT_FGET(strpeek, flags) & RS_HIPRI) &&
4987 queclass(mp) == QNORM) {
4988 *rvalp = 0;
4989 mutex_exit(QLOCK(rdq));
4990 return (0);
4992 } else if (stp->sd_struiordq == NULL ||
4993 (STRUCT_FGET(strpeek, flags) & RS_HIPRI)) {
4995 * No mblks to look at at the streamhead and
4996 * 1). This isn't a synch stream or
4997 * 2). This is a synch stream but caller wants high
4998 * priority messages which is not supported by
4999 * the synch stream. (it only supports QNORM)
5001 *rvalp = 0;
5002 mutex_exit(QLOCK(rdq));
5003 return (0);
5006 fmp = mp;
5008 if (mp && mp->b_datap->db_type == M_PASSFP) {
5009 mutex_exit(QLOCK(rdq));
5010 return (EBADMSG);
5013 ASSERT(mp == NULL || mp->b_datap->db_type == M_PCPROTO ||
5014 mp->b_datap->db_type == M_PROTO ||
5015 mp->b_datap->db_type == M_DATA);
5017 if (mp && mp->b_datap->db_type == M_PCPROTO) {
5018 STRUCT_FSET(strpeek, flags, RS_HIPRI);
5019 } else {
5020 STRUCT_FSET(strpeek, flags, 0);
5024 if (mp && ((tmp_mp = dupmsg(mp)) == NULL)) {
5025 mutex_exit(QLOCK(rdq));
5026 return (ENOSR);
5028 mutex_exit(QLOCK(rdq));
5031 * set mp = tmp_mp, so that I_PEEK processing can continue.
5032 * tmp_mp is used to free the dup'd message.
5034 mp = tmp_mp;
5036 uio.uio_fmode = 0;
5037 uio.uio_extflg = UIO_COPY_CACHED;
5038 uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5039 UIO_SYSSPACE;
5040 uio.uio_limit = 0;
5042 * First process PROTO blocks, if any.
5043 * If user doesn't want to get ctl info by setting maxlen <= 0,
5044 * then set len to -1/0 and skip control blocks part.
5046 if (STRUCT_FGET(strpeek, ctlbuf.maxlen) < 0)
5047 STRUCT_FSET(strpeek, ctlbuf.len, -1);
5048 else if (STRUCT_FGET(strpeek, ctlbuf.maxlen) == 0)
5049 STRUCT_FSET(strpeek, ctlbuf.len, 0);
5050 else {
5051 int ctl_part = 0;
5053 iov.iov_base = STRUCT_FGETP(strpeek, ctlbuf.buf);
5054 iov.iov_len = STRUCT_FGET(strpeek, ctlbuf.maxlen);
5055 uio.uio_iov = &iov;
5056 uio.uio_resid = iov.iov_len;
5057 uio.uio_loffset = 0;
5058 uio.uio_iovcnt = 1;
5059 while (mp && mp->b_datap->db_type != M_DATA &&
5060 uio.uio_resid >= 0) {
5061 ASSERT(STRUCT_FGET(strpeek, flags) == 0 ?
5062 mp->b_datap->db_type == M_PROTO :
5063 mp->b_datap->db_type == M_PCPROTO);
5065 if ((n = MIN(uio.uio_resid,
5066 mp->b_wptr - mp->b_rptr)) != 0 &&
5067 (error = uiomove((char *)mp->b_rptr, n,
5068 UIO_READ, &uio)) != 0) {
5069 freemsg(tmp_mp);
5070 return (error);
5072 ctl_part = 1;
5073 mp = mp->b_cont;
5075 /* No ctl message */
5076 if (ctl_part == 0)
5077 STRUCT_FSET(strpeek, ctlbuf.len, -1);
5078 else
5079 STRUCT_FSET(strpeek, ctlbuf.len,
5080 STRUCT_FGET(strpeek, ctlbuf.maxlen) -
5081 uio.uio_resid);
5085 * Now process DATA blocks, if any.
5086 * If user doesn't want to get data info by setting maxlen <= 0,
5087 * then set len to -1/0 and skip data blocks part.
5089 if (STRUCT_FGET(strpeek, databuf.maxlen) < 0)
5090 STRUCT_FSET(strpeek, databuf.len, -1);
5091 else if (STRUCT_FGET(strpeek, databuf.maxlen) == 0)
5092 STRUCT_FSET(strpeek, databuf.len, 0);
5093 else {
5094 int data_part = 0;
5096 iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
5097 iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen);
5098 uio.uio_iov = &iov;
5099 uio.uio_resid = iov.iov_len;
5100 uio.uio_loffset = 0;
5101 uio.uio_iovcnt = 1;
5102 while (mp && uio.uio_resid) {
5103 if (mp->b_datap->db_type == M_DATA) {
5104 if ((n = MIN(uio.uio_resid,
5105 mp->b_wptr - mp->b_rptr)) != 0 &&
5106 (error = uiomove((char *)mp->b_rptr,
5107 n, UIO_READ, &uio)) != 0) {
5108 freemsg(tmp_mp);
5109 return (error);
5111 data_part = 1;
5113 ASSERT(data_part == 0 ||
5114 mp->b_datap->db_type == M_DATA);
5115 mp = mp->b_cont;
5117 /* No data message */
5118 if (data_part == 0)
5119 STRUCT_FSET(strpeek, databuf.len, -1);
5120 else
5121 STRUCT_FSET(strpeek, databuf.len,
5122 STRUCT_FGET(strpeek, databuf.maxlen) -
5123 uio.uio_resid);
5125 freemsg(tmp_mp);
5128 * It is a synch stream and user wants to get
5129 * data (maxlen > 0).
5130 * uio setup is done by the codes that process DATA
5131 * blocks above.
5133 if ((fmp == NULL) && STRUCT_FGET(strpeek, databuf.maxlen) > 0) {
5134 infod_t infod;
5136 infod.d_cmd = INFOD_COPYOUT;
5137 infod.d_res = 0;
5138 infod.d_uiop = &uio;
5139 error = infonext(rdq, &infod);
5140 if (error == EINVAL || error == EBUSY)
5141 error = 0;
5142 if (error)
5143 return (error);
5144 STRUCT_FSET(strpeek, databuf.len, STRUCT_FGET(strpeek,
5145 databuf.maxlen) - uio.uio_resid);
5146 if (STRUCT_FGET(strpeek, databuf.len) == 0) {
5148 * No data found by the infonext().
5150 STRUCT_FSET(strpeek, databuf.len, -1);
5153 error = strcopyout(STRUCT_BUF(strpeek), (void *)arg,
5154 STRUCT_SIZE(strpeek), copyflag);
5155 if (error) {
5156 return (error);
5159 * If there is no message retrieved, set return code to 0
5160 * otherwise, set it to 1.
5162 if (STRUCT_FGET(strpeek, ctlbuf.len) == -1 &&
5163 STRUCT_FGET(strpeek, databuf.len) == -1)
5164 *rvalp = 0;
5165 else
5166 *rvalp = 1;
5167 return (0);
5170 case I_FDINSERT:
5172 STRUCT_DECL(strfdinsert, strfdinsert);
5173 struct file *resftp;
5174 struct stdata *resstp;
5175 t_uscalar_t ival;
5176 ssize_t msgsize;
5177 struct strbuf mctl;
5179 STRUCT_INIT(strfdinsert, flag);
5180 if (stp->sd_flag & STRHUP)
5181 return (ENXIO);
5183 * STRDERR, STWRERR and STPLEX tested above.
5185 error = strcopyin((void *)arg, STRUCT_BUF(strfdinsert),
5186 STRUCT_SIZE(strfdinsert), copyflag);
5187 if (error)
5188 return (error);
5190 if (STRUCT_FGET(strfdinsert, offset) < 0 ||
5191 (STRUCT_FGET(strfdinsert, offset) %
5192 sizeof (t_uscalar_t)) != 0)
5193 return (EINVAL);
5194 if ((resftp = getf(STRUCT_FGET(strfdinsert, fildes))) != NULL) {
5195 if ((resstp = resftp->f_vnode->v_stream) == NULL) {
5196 releasef(STRUCT_FGET(strfdinsert, fildes));
5197 return (EINVAL);
5199 } else
5200 return (EINVAL);
5202 mutex_enter(&resstp->sd_lock);
5203 if (resstp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
5204 error = strgeterr(resstp,
5205 STRDERR|STWRERR|STRHUP|STPLEX, 0);
5206 if (error != 0) {
5207 mutex_exit(&resstp->sd_lock);
5208 releasef(STRUCT_FGET(strfdinsert, fildes));
5209 return (error);
5212 mutex_exit(&resstp->sd_lock);
5214 #ifdef _ILP32
5216 queue_t *q;
5217 queue_t *mate = NULL;
5219 /* get read queue of stream terminus */
5220 claimstr(resstp->sd_wrq);
5221 for (q = resstp->sd_wrq->q_next; q->q_next != NULL;
5222 q = q->q_next)
5223 if (!STRMATED(resstp) && STREAM(q) != resstp &&
5224 mate == NULL) {
5225 ASSERT(q->q_qinfo->qi_srvp);
5226 ASSERT(_OTHERQ(q)->q_qinfo->qi_srvp);
5227 claimstr(q);
5228 mate = q;
5230 q = _RD(q);
5231 if (mate)
5232 releasestr(mate);
5233 releasestr(resstp->sd_wrq);
5234 ival = (t_uscalar_t)q;
5236 #else
5237 ival = (t_uscalar_t)getminor(resftp->f_vnode->v_rdev);
5238 #endif /* _ILP32 */
5240 if (STRUCT_FGET(strfdinsert, ctlbuf.len) <
5241 STRUCT_FGET(strfdinsert, offset) + sizeof (t_uscalar_t)) {
5242 releasef(STRUCT_FGET(strfdinsert, fildes));
5243 return (EINVAL);
5247 * Check for legal flag value.
5249 if (STRUCT_FGET(strfdinsert, flags) & ~RS_HIPRI) {
5250 releasef(STRUCT_FGET(strfdinsert, fildes));
5251 return (EINVAL);
5254 /* get these values from those cached in the stream head */
5255 mutex_enter(QLOCK(stp->sd_wrq));
5256 rmin = stp->sd_qn_minpsz;
5257 rmax = stp->sd_qn_maxpsz;
5258 mutex_exit(QLOCK(stp->sd_wrq));
5261 * Make sure ctl and data sizes together fall within
5262 * the limits of the max and min receive packet sizes
5263 * and do not exceed system limit. A negative data
5264 * length means that no data part is to be sent.
5266 ASSERT((rmax >= 0) || (rmax == INFPSZ));
5267 if (rmax == 0) {
5268 releasef(STRUCT_FGET(strfdinsert, fildes));
5269 return (ERANGE);
5271 if ((msgsize = STRUCT_FGET(strfdinsert, databuf.len)) < 0)
5272 msgsize = 0;
5273 if ((msgsize < rmin) ||
5274 ((msgsize > rmax) && (rmax != INFPSZ)) ||
5275 (STRUCT_FGET(strfdinsert, ctlbuf.len) > strctlsz)) {
5276 releasef(STRUCT_FGET(strfdinsert, fildes));
5277 return (ERANGE);
5280 mutex_enter(&stp->sd_lock);
5281 while (!(STRUCT_FGET(strfdinsert, flags) & RS_HIPRI) &&
5282 !canputnext(stp->sd_wrq)) {
5283 if ((error = strwaitq(stp, WRITEWAIT, (ssize_t)0,
5284 flag, -1, &done)) != 0 || done) {
5285 mutex_exit(&stp->sd_lock);
5286 releasef(STRUCT_FGET(strfdinsert, fildes));
5287 return (error);
5289 if ((error = i_straccess(stp, access)) != 0) {
5290 mutex_exit(&stp->sd_lock);
5291 releasef(
5292 STRUCT_FGET(strfdinsert, fildes));
5293 return (error);
5296 mutex_exit(&stp->sd_lock);
5299 * Copy strfdinsert.ctlbuf into native form of
5300 * ctlbuf to pass down into strmakemsg().
5302 mctl.maxlen = STRUCT_FGET(strfdinsert, ctlbuf.maxlen);
5303 mctl.len = STRUCT_FGET(strfdinsert, ctlbuf.len);
5304 mctl.buf = STRUCT_FGETP(strfdinsert, ctlbuf.buf);
5306 iov.iov_base = STRUCT_FGETP(strfdinsert, databuf.buf);
5307 iov.iov_len = STRUCT_FGET(strfdinsert, databuf.len);
5308 uio.uio_iov = &iov;
5309 uio.uio_iovcnt = 1;
5310 uio.uio_loffset = 0;
5311 uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5312 UIO_SYSSPACE;
5313 uio.uio_fmode = 0;
5314 uio.uio_extflg = UIO_COPY_CACHED;
5315 uio.uio_resid = iov.iov_len;
5316 if ((error = strmakemsg(&mctl,
5317 &msgsize, &uio, stp,
5318 STRUCT_FGET(strfdinsert, flags), &mp)) != 0 || !mp) {
5319 STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5320 releasef(STRUCT_FGET(strfdinsert, fildes));
5321 return (error);
5324 STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5327 * Place the possibly reencoded queue pointer 'offset' bytes
5328 * from the start of the control portion of the message.
5330 *((t_uscalar_t *)(mp->b_rptr +
5331 STRUCT_FGET(strfdinsert, offset))) = ival;
5334 * Put message downstream.
5336 stream_willservice(stp);
5337 putnext(stp->sd_wrq, mp);
5338 stream_runservice(stp);
5339 releasef(STRUCT_FGET(strfdinsert, fildes));
5340 return (error);
5343 case I_SENDFD:
5345 struct file *fp;
5347 if ((fp = getf((int)arg)) == NULL)
5348 return (EBADF);
5349 error = do_sendfp(stp, fp, crp);
5350 releasef((int)arg);
5351 return (error);
5354 case I_RECVFD:
5355 case I_E_RECVFD:
5357 struct k_strrecvfd *srf;
5358 int i, fd;
5360 mutex_enter(&stp->sd_lock);
5361 while (!(mp = getq(rdq))) {
5362 if (stp->sd_flag & (STRHUP|STREOF)) {
5363 mutex_exit(&stp->sd_lock);
5364 return (ENXIO);
5366 if ((error = strwaitq(stp, GETWAIT, (ssize_t)0,
5367 flag, -1, &done)) != 0 || done) {
5368 mutex_exit(&stp->sd_lock);
5369 return (error);
5371 if ((error = i_straccess(stp, access)) != 0) {
5372 mutex_exit(&stp->sd_lock);
5373 return (error);
5376 if (mp->b_datap->db_type != M_PASSFP) {
5377 putback(stp, rdq, mp, mp->b_band);
5378 mutex_exit(&stp->sd_lock);
5379 return (EBADMSG);
5381 mutex_exit(&stp->sd_lock);
5383 srf = (struct k_strrecvfd *)mp->b_rptr;
5384 if ((fd = ufalloc(0)) == -1) {
5385 mutex_enter(&stp->sd_lock);
5386 putback(stp, rdq, mp, mp->b_band);
5387 mutex_exit(&stp->sd_lock);
5388 return (EMFILE);
5390 if (cmd == I_RECVFD) {
5391 struct o_strrecvfd ostrfd;
5393 /* check to see if uid/gid values are too large. */
5395 if (srf->uid > (o_uid_t)USHRT_MAX ||
5396 srf->gid > (o_gid_t)USHRT_MAX) {
5397 mutex_enter(&stp->sd_lock);
5398 putback(stp, rdq, mp, mp->b_band);
5399 mutex_exit(&stp->sd_lock);
5400 setf(fd, NULL); /* release fd entry */
5401 return (EOVERFLOW);
5404 ostrfd.fd = fd;
5405 ostrfd.uid = (o_uid_t)srf->uid;
5406 ostrfd.gid = (o_gid_t)srf->gid;
5408 /* Null the filler bits */
5409 for (i = 0; i < 8; i++)
5410 ostrfd.fill[i] = 0;
5412 error = strcopyout(&ostrfd, (void *)arg,
5413 sizeof (struct o_strrecvfd), copyflag);
5414 } else { /* I_E_RECVFD */
5415 struct strrecvfd strfd;
5417 strfd.fd = fd;
5418 strfd.uid = srf->uid;
5419 strfd.gid = srf->gid;
5421 /* null the filler bits */
5422 for (i = 0; i < 8; i++)
5423 strfd.fill[i] = 0;
5425 error = strcopyout(&strfd, (void *)arg,
5426 sizeof (struct strrecvfd), copyflag);
5429 if (error) {
5430 setf(fd, NULL); /* release fd entry */
5431 mutex_enter(&stp->sd_lock);
5432 putback(stp, rdq, mp, mp->b_band);
5433 mutex_exit(&stp->sd_lock);
5434 return (error);
5438 * Always increment f_count since the freemsg() below will
5439 * always call free_passfp() which performs a closef().
5441 mutex_enter(&srf->fp->f_tlock);
5442 srf->fp->f_count++;
5443 mutex_exit(&srf->fp->f_tlock);
5444 setf(fd, srf->fp);
5445 freemsg(mp);
5446 return (0);
5449 case I_SWROPT:
5451 * Set/clear the write options. arg is a bit
5452 * mask with any of the following bits set...
5453 * SNDZERO - send zero length message
5454 * SNDPIPE - send sigpipe to process if
5455 * sd_werror is set and process is
5456 * doing a write or putmsg.
5457 * The new stream head write options should reflect
5458 * what is in arg.
5460 if (arg & ~(SNDZERO|SNDPIPE))
5461 return (EINVAL);
5463 mutex_enter(&stp->sd_lock);
5464 stp->sd_wput_opt &= ~(SW_SIGPIPE|SW_SNDZERO);
5465 if (arg & SNDZERO)
5466 stp->sd_wput_opt |= SW_SNDZERO;
5467 if (arg & SNDPIPE)
5468 stp->sd_wput_opt |= SW_SIGPIPE;
5469 mutex_exit(&stp->sd_lock);
5470 return (0);
5472 case I_GWROPT:
5474 int wropt = 0;
5476 if (stp->sd_wput_opt & SW_SNDZERO)
5477 wropt |= SNDZERO;
5478 if (stp->sd_wput_opt & SW_SIGPIPE)
5479 wropt |= SNDPIPE;
5480 return (strcopyout(&wropt, (void *)arg, sizeof (wropt),
5481 copyflag));
5484 case I_LIST:
5486 * Returns all the modules found on this stream,
5487 * upto the driver. If argument is NULL, return the
5488 * number of modules (including driver). If argument
5489 * is not NULL, copy the names into the structure
5490 * provided.
5494 queue_t *q;
5495 char *qname;
5496 int i, nmods;
5497 struct str_mlist *mlist;
5498 STRUCT_DECL(str_list, strlist);
5500 if (arg == (intptr_t)NULL) {
5501 /* Return number of modules plus driver */
5502 if (stp->sd_vnode->v_type == VFIFO)
5503 *rvalp = stp->sd_pushcnt;
5504 else
5505 *rvalp = stp->sd_pushcnt + 1;
5506 return (0);
5509 STRUCT_INIT(strlist, flag);
5511 error = strcopyin((void *)arg, STRUCT_BUF(strlist),
5512 STRUCT_SIZE(strlist), copyflag);
5513 if (error != 0)
5514 return (error);
5516 mlist = STRUCT_FGETP(strlist, sl_modlist);
5517 nmods = STRUCT_FGET(strlist, sl_nmods);
5518 if (nmods <= 0)
5519 return (EINVAL);
5521 claimstr(stp->sd_wrq);
5522 q = stp->sd_wrq;
5523 for (i = 0; i < nmods && _SAMESTR(q); i++, q = q->q_next) {
5524 qname = Q2NAME(q->q_next);
5525 error = strcopyout(qname, &mlist[i], strlen(qname) + 1,
5526 copyflag);
5527 if (error != 0) {
5528 releasestr(stp->sd_wrq);
5529 return (error);
5532 releasestr(stp->sd_wrq);
5533 return (strcopyout(&i, (void *)arg, sizeof (int), copyflag));
5536 case I_CKBAND:
5538 queue_t *q;
5539 qband_t *qbp;
5541 if ((arg < 0) || (arg >= NBAND))
5542 return (EINVAL);
5543 q = _RD(stp->sd_wrq);
5544 mutex_enter(QLOCK(q));
5545 if (arg > (int)q->q_nband) {
5546 *rvalp = 0;
5547 } else {
5548 if (arg == 0) {
5549 if (q->q_first)
5550 *rvalp = 1;
5551 else
5552 *rvalp = 0;
5553 } else {
5554 qbp = q->q_bandp;
5555 while (--arg > 0)
5556 qbp = qbp->qb_next;
5557 if (qbp->qb_first)
5558 *rvalp = 1;
5559 else
5560 *rvalp = 0;
5563 mutex_exit(QLOCK(q));
5564 return (0);
5567 case I_GETBAND:
5569 int intpri;
5570 queue_t *q;
5572 q = _RD(stp->sd_wrq);
5573 mutex_enter(QLOCK(q));
5574 mp = q->q_first;
5575 if (!mp) {
5576 mutex_exit(QLOCK(q));
5577 return (ENODATA);
5579 intpri = (int)mp->b_band;
5580 error = strcopyout(&intpri, (void *)arg, sizeof (int),
5581 copyflag);
5582 mutex_exit(QLOCK(q));
5583 return (error);
5586 case I_ATMARK:
5588 queue_t *q;
5590 if (arg & ~(ANYMARK|LASTMARK))
5591 return (EINVAL);
5592 q = _RD(stp->sd_wrq);
5593 mutex_enter(&stp->sd_lock);
5594 if ((stp->sd_flag & STRATMARK) && (arg == ANYMARK)) {
5595 *rvalp = 1;
5596 } else {
5597 mutex_enter(QLOCK(q));
5598 mp = q->q_first;
5600 if (mp == NULL)
5601 *rvalp = 0;
5602 else if ((arg == ANYMARK) && (mp->b_flag & MSGMARK))
5603 *rvalp = 1;
5604 else if ((arg == LASTMARK) && (mp == stp->sd_mark))
5605 *rvalp = 1;
5606 else
5607 *rvalp = 0;
5608 mutex_exit(QLOCK(q));
5610 mutex_exit(&stp->sd_lock);
5611 return (0);
5614 case I_CANPUT:
5616 char band;
5618 if ((arg < 0) || (arg >= NBAND))
5619 return (EINVAL);
5620 band = (char)arg;
5621 *rvalp = bcanputnext(stp->sd_wrq, band);
5622 return (0);
5625 case I_SETCLTIME:
5627 int closetime;
5629 error = strcopyin((void *)arg, &closetime, sizeof (int),
5630 copyflag);
5631 if (error)
5632 return (error);
5633 if (closetime < 0)
5634 return (EINVAL);
5636 stp->sd_closetime = closetime;
5637 return (0);
5640 case I_GETCLTIME:
5642 int closetime;
5644 closetime = stp->sd_closetime;
5645 return (strcopyout(&closetime, (void *)arg, sizeof (int),
5646 copyflag));
5649 case TIOCGSID:
5651 pid_t sid;
5653 mutex_enter(&stp->sd_lock);
5654 if (stp->sd_sidp == NULL) {
5655 mutex_exit(&stp->sd_lock);
5656 return (ENOTTY);
5658 sid = stp->sd_sidp->pid_id;
5659 mutex_exit(&stp->sd_lock);
5660 return (strcopyout(&sid, (void *)arg, sizeof (pid_t),
5661 copyflag));
5664 case TIOCSPGRP:
5666 pid_t pgrp;
5667 proc_t *q;
5668 pid_t sid, fg_pgid, bg_pgid;
5670 if (error = strcopyin((void *)arg, &pgrp, sizeof (pid_t),
5671 copyflag))
5672 return (error);
5673 mutex_enter(&stp->sd_lock);
5674 mutex_enter(&pidlock);
5675 if (stp->sd_sidp != ttoproc(curthread)->p_sessp->s_sidp) {
5676 mutex_exit(&pidlock);
5677 mutex_exit(&stp->sd_lock);
5678 return (ENOTTY);
5680 if (pgrp == stp->sd_pgidp->pid_id) {
5681 mutex_exit(&pidlock);
5682 mutex_exit(&stp->sd_lock);
5683 return (0);
5685 if (pgrp <= 0 || pgrp >= maxpid) {
5686 mutex_exit(&pidlock);
5687 mutex_exit(&stp->sd_lock);
5688 return (EINVAL);
5690 if ((q = pgfind(pgrp)) == NULL ||
5691 q->p_sessp != ttoproc(curthread)->p_sessp) {
5692 mutex_exit(&pidlock);
5693 mutex_exit(&stp->sd_lock);
5694 return (EPERM);
5696 sid = stp->sd_sidp->pid_id;
5697 fg_pgid = q->p_pgrp;
5698 bg_pgid = stp->sd_pgidp->pid_id;
5699 CL_SET_PROCESS_GROUP(curthread, sid, bg_pgid, fg_pgid);
5700 PID_RELE(stp->sd_pgidp);
5701 ctty_clear_sighuped();
5702 stp->sd_pgidp = q->p_pgidp;
5703 PID_HOLD(stp->sd_pgidp);
5704 mutex_exit(&pidlock);
5705 mutex_exit(&stp->sd_lock);
5706 return (0);
5709 case TIOCGPGRP:
5711 pid_t pgrp;
5713 mutex_enter(&stp->sd_lock);
5714 if (stp->sd_sidp == NULL) {
5715 mutex_exit(&stp->sd_lock);
5716 return (ENOTTY);
5718 pgrp = stp->sd_pgidp->pid_id;
5719 mutex_exit(&stp->sd_lock);
5720 return (strcopyout(&pgrp, (void *)arg, sizeof (pid_t),
5721 copyflag));
5724 case TIOCSCTTY:
5726 return (strctty(stp));
5729 case TIOCNOTTY:
5731 /* freectty() always assumes curproc. */
5732 if (freectty(B_FALSE) != 0)
5733 return (0);
5734 return (ENOTTY);
5737 case FIONBIO:
5738 case FIOASYNC:
5739 return (0); /* handled by the upper layer */
5744 * Custom free routine used for M_PASSFP messages.
5746 static void
5747 free_passfp(struct k_strrecvfd *srf)
5749 (void) closef(srf->fp);
5750 kmem_free(srf, sizeof (struct k_strrecvfd) + sizeof (frtn_t));
5753 /* ARGSUSED */
5755 do_sendfp(struct stdata *stp, struct file *fp, struct cred *cr)
5757 queue_t *qp, *nextqp;
5758 struct k_strrecvfd *srf;
5759 mblk_t *mp;
5760 frtn_t *frtnp;
5761 size_t bufsize;
5762 queue_t *mate = NULL;
5763 syncq_t *sq = NULL;
5764 int retval = 0;
5766 if (stp->sd_flag & STRHUP)
5767 return (ENXIO);
5769 claimstr(stp->sd_wrq);
5771 /* Fastpath, we have a pipe, and we are already mated, use it. */
5772 if (STRMATED(stp)) {
5773 qp = _RD(stp->sd_mate->sd_wrq);
5774 claimstr(qp);
5775 mate = qp;
5776 } else { /* Not already mated. */
5779 * Walk the stream to the end of this one.
5780 * assumes that the claimstr() will prevent
5781 * plumbing between the stream head and the
5782 * driver from changing
5784 qp = stp->sd_wrq;
5787 * Loop until we reach the end of this stream.
5788 * On completion, qp points to the write queue
5789 * at the end of the stream, or the read queue
5790 * at the stream head if this is a fifo.
5792 while (((qp = qp->q_next) != NULL) && _SAMESTR(qp))
5796 * Just in case we get a q_next which is NULL, but
5797 * not at the end of the stream. This is actually
5798 * broken, so we set an assert to catch it in
5799 * debug, and set an error and return if not debug.
5801 ASSERT(qp);
5802 if (qp == NULL) {
5803 releasestr(stp->sd_wrq);
5804 return (EINVAL);
5808 * Enter the syncq for the driver, so (hopefully)
5809 * the queue values will not change on us.
5810 * XXXX - This will only prevent the race IFF only
5811 * the write side modifies the q_next member, and
5812 * the put procedure is protected by at least
5813 * MT_PERQ.
5815 if ((sq = qp->q_syncq) != NULL)
5816 entersq(sq, SQ_PUT);
5818 /* Now get the q_next value from this qp. */
5819 nextqp = qp->q_next;
5822 * If nextqp exists and the other stream is different
5823 * from this one claim the stream, set the mate, and
5824 * get the read queue at the stream head of the other
5825 * stream. Assumes that nextqp was at least valid when
5826 * we got it. Hopefully the entersq of the driver
5827 * will prevent it from changing on us.
5829 if ((nextqp != NULL) && (STREAM(nextqp) != stp)) {
5830 ASSERT(qp->q_qinfo->qi_srvp);
5831 ASSERT(_OTHERQ(qp)->q_qinfo->qi_srvp);
5832 ASSERT(_OTHERQ(qp->q_next)->q_qinfo->qi_srvp);
5833 claimstr(nextqp);
5835 /* Make sure we still have a q_next */
5836 if (nextqp != qp->q_next) {
5837 releasestr(stp->sd_wrq);
5838 releasestr(nextqp);
5839 return (EINVAL);
5842 qp = _RD(STREAM(nextqp)->sd_wrq);
5843 mate = qp;
5845 /* If we entered the synq above, leave it. */
5846 if (sq != NULL)
5847 leavesq(sq, SQ_PUT);
5848 } /* STRMATED(STP) */
5850 /* XXX prevents substitution of the ops vector */
5851 if (qp->q_qinfo != &strdata && qp->q_qinfo != &fifo_strdata) {
5852 retval = EINVAL;
5853 goto out;
5856 if (qp->q_flag & QFULL) {
5857 retval = EAGAIN;
5858 goto out;
5862 * Since M_PASSFP messages include a file descriptor, we use
5863 * esballoc() and specify a custom free routine (free_passfp()) that
5864 * will close the descriptor as part of freeing the message. For
5865 * convenience, we stash the frtn_t right after the data block.
5867 bufsize = sizeof (struct k_strrecvfd) + sizeof (frtn_t);
5868 srf = kmem_alloc(bufsize, KM_NOSLEEP);
5869 if (srf == NULL) {
5870 retval = EAGAIN;
5871 goto out;
5874 frtnp = (frtn_t *)(srf + 1);
5875 frtnp->free_arg = (caddr_t)srf;
5876 frtnp->free_func = free_passfp;
5878 mp = esballoc((uchar_t *)srf, bufsize, BPRI_MED, frtnp);
5879 if (mp == NULL) {
5880 kmem_free(srf, bufsize);
5881 retval = EAGAIN;
5882 goto out;
5884 mp->b_wptr += sizeof (struct k_strrecvfd);
5885 mp->b_datap->db_type = M_PASSFP;
5887 srf->fp = fp;
5888 srf->uid = crgetuid(curthread->t_cred);
5889 srf->gid = crgetgid(curthread->t_cred);
5890 mutex_enter(&fp->f_tlock);
5891 fp->f_count++;
5892 mutex_exit(&fp->f_tlock);
5894 put(qp, mp);
5895 out:
5896 releasestr(stp->sd_wrq);
5897 if (mate)
5898 releasestr(mate);
5899 return (retval);
5903 * Send an ioctl message downstream and wait for acknowledgement.
5904 * flags may be set to either U_TO_K or K_TO_K and a combination
5905 * of STR_NOERROR or STR_NOSIG
5906 * STR_NOSIG: Signals are essentially ignored or held and have
5907 * no effect for the duration of the call.
5908 * STR_NOERROR: Ignores stream head read, write and hup errors.
5909 * Additionally, if an existing ioctl times out, it is assumed
5910 * lost and and this ioctl will continue as if the previous ioctl had
5911 * finished. ETIME may be returned if this ioctl times out (i.e.
5912 * ic_timout is not INFTIM). Non-stream head errors may be returned if
5913 * the ioc_error indicates that the driver/module had problems,
5914 * an EFAULT was found when accessing user data, a lack of
5915 * resources, etc.
5918 strdoioctl(
5919 struct stdata *stp,
5920 struct strioctl *strioc,
5921 int fflags, /* file flags with model info */
5922 int flag,
5923 cred_t *crp,
5924 int *rvalp)
5926 mblk_t *bp;
5927 struct iocblk *iocbp;
5928 struct copyreq *reqp;
5929 struct copyresp *resp;
5930 int id;
5931 int transparent = 0;
5932 int error = 0;
5933 int len = 0;
5934 caddr_t taddr;
5935 int copyflag = (flag & (U_TO_K | K_TO_K));
5936 int sigflag = (flag & STR_NOSIG);
5937 int errs;
5938 uint_t waitflags;
5939 boolean_t set_iocwaitne = B_FALSE;
5941 ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
5942 ASSERT((fflags & FMODELS) != 0);
5944 TRACE_2(TR_FAC_STREAMS_FR,
5945 TR_STRDOIOCTL,
5946 "strdoioctl:stp %p strioc %p", stp, strioc);
5947 if (strioc->ic_len == TRANSPARENT) { /* send arg in M_DATA block */
5948 transparent = 1;
5949 strioc->ic_len = sizeof (intptr_t);
5952 if (strioc->ic_len < 0 || (strmsgsz > 0 && strioc->ic_len > strmsgsz))
5953 return (EINVAL);
5955 if ((bp = allocb_cred_wait(sizeof (union ioctypes), sigflag, &error,
5956 crp, curproc->p_pid)) == NULL)
5957 return (error);
5959 bzero(bp->b_wptr, sizeof (union ioctypes));
5961 iocbp = (struct iocblk *)bp->b_wptr;
5962 iocbp->ioc_count = strioc->ic_len;
5963 iocbp->ioc_cmd = strioc->ic_cmd;
5964 iocbp->ioc_flag = (fflags & FMODELS);
5966 crhold(crp);
5967 iocbp->ioc_cr = crp;
5968 DB_TYPE(bp) = M_IOCTL;
5969 bp->b_wptr += sizeof (struct iocblk);
5971 if (flag & STR_NOERROR)
5972 errs = STPLEX;
5973 else
5974 errs = STRHUP|STRDERR|STWRERR|STPLEX;
5977 * If there is data to copy into ioctl block, do so.
5979 if (iocbp->ioc_count > 0) {
5980 if (transparent)
5982 * Note: STR_NOERROR does not have an effect
5983 * in putiocd()
5985 id = K_TO_K | sigflag;
5986 else
5987 id = flag;
5988 if ((error = putiocd(bp, strioc->ic_dp, id, crp)) != 0) {
5989 freemsg(bp);
5990 crfree(crp);
5991 return (error);
5995 * We could have slept copying in user pages.
5996 * Recheck the stream head state (the other end
5997 * of a pipe could have gone away).
5999 if (stp->sd_flag & errs) {
6000 mutex_enter(&stp->sd_lock);
6001 error = strgeterr(stp, errs, 0);
6002 mutex_exit(&stp->sd_lock);
6003 if (error != 0) {
6004 freemsg(bp);
6005 crfree(crp);
6006 return (error);
6010 if (transparent)
6011 iocbp->ioc_count = TRANSPARENT;
6014 * Block for up to STRTIMOUT milliseconds if there is an outstanding
6015 * ioctl for this stream already running. All processes
6016 * sleeping here will be awakened as a result of an ACK
6017 * or NAK being received for the outstanding ioctl, or
6018 * as a result of the timer expiring on the outstanding
6019 * ioctl (a failure), or as a result of any waiting
6020 * process's timer expiring (also a failure).
6023 error = 0;
6024 mutex_enter(&stp->sd_lock);
6025 while ((stp->sd_flag & IOCWAIT) ||
6026 (!set_iocwaitne && (stp->sd_flag & IOCWAITNE))) {
6027 clock_t cv_rval;
6029 TRACE_0(TR_FAC_STREAMS_FR,
6030 TR_STRDOIOCTL_WAIT,
6031 "strdoioctl sleeps - IOCWAIT");
6032 cv_rval = str_cv_wait(&stp->sd_iocmonitor, &stp->sd_lock,
6033 STRTIMOUT, sigflag);
6034 if (cv_rval <= 0) {
6035 if (cv_rval == 0) {
6036 error = EINTR;
6037 } else {
6038 if (flag & STR_NOERROR) {
6040 * Terminating current ioctl in
6041 * progress -- assume it got lost and
6042 * wake up the other thread so that the
6043 * operation completes.
6045 if (!(stp->sd_flag & IOCWAITNE)) {
6046 set_iocwaitne = B_TRUE;
6047 stp->sd_flag |= IOCWAITNE;
6048 cv_broadcast(&stp->sd_monitor);
6051 * Otherwise, there's a running
6052 * STR_NOERROR -- we have no choice
6053 * here but to wait forever (or until
6054 * interrupted).
6056 } else {
6058 * pending ioctl has caused
6059 * us to time out
6061 error = ETIME;
6064 } else if ((stp->sd_flag & errs)) {
6065 error = strgeterr(stp, errs, 0);
6067 if (error) {
6068 mutex_exit(&stp->sd_lock);
6069 freemsg(bp);
6070 crfree(crp);
6071 return (error);
6076 * Have control of ioctl mechanism.
6077 * Send down ioctl packet and wait for response.
6079 if (stp->sd_iocblk != (mblk_t *)-1) {
6080 freemsg(stp->sd_iocblk);
6082 stp->sd_iocblk = NULL;
6085 * If this is marked with 'noerror' (internal; mostly
6086 * I_{P,}{UN,}LINK), then make sure nobody else is able to get
6087 * in here by setting IOCWAITNE.
6089 waitflags = IOCWAIT;
6090 if (flag & STR_NOERROR)
6091 waitflags |= IOCWAITNE;
6093 stp->sd_flag |= waitflags;
6096 * Assign sequence number.
6098 iocbp->ioc_id = stp->sd_iocid = getiocseqno();
6100 mutex_exit(&stp->sd_lock);
6102 TRACE_1(TR_FAC_STREAMS_FR,
6103 TR_STRDOIOCTL_PUT, "strdoioctl put: stp %p", stp);
6104 stream_willservice(stp);
6105 putnext(stp->sd_wrq, bp);
6106 stream_runservice(stp);
6109 * Timed wait for acknowledgment. The wait time is limited by the
6110 * timeout value, which must be a positive integer (number of
6111 * milliseconds) to wait, or 0 (use default value of STRTIMOUT
6112 * milliseconds), or -1 (wait forever). This will be awakened
6113 * either by an ACK/NAK message arriving, the timer expiring, or
6114 * the timer expiring on another ioctl waiting for control of the
6115 * mechanism.
6117 waitioc:
6118 mutex_enter(&stp->sd_lock);
6122 * If the reply has already arrived, don't sleep. If awakened from
6123 * the sleep, fail only if the reply has not arrived by then.
6124 * Otherwise, process the reply.
6126 while (!stp->sd_iocblk) {
6127 clock_t cv_rval;
6129 if (stp->sd_flag & errs) {
6130 error = strgeterr(stp, errs, 0);
6131 if (error != 0) {
6132 stp->sd_flag &= ~waitflags;
6133 cv_broadcast(&stp->sd_iocmonitor);
6134 mutex_exit(&stp->sd_lock);
6135 crfree(crp);
6136 return (error);
6140 TRACE_0(TR_FAC_STREAMS_FR,
6141 TR_STRDOIOCTL_WAIT2,
6142 "strdoioctl sleeps awaiting reply");
6143 ASSERT(error == 0);
6145 cv_rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock,
6146 (strioc->ic_timout ?
6147 strioc->ic_timout * 1000 : STRTIMOUT), sigflag);
6150 * There are four possible cases here: interrupt, timeout,
6151 * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
6152 * valid M_IOCTL reply).
6154 * If we've been awakened by a STR_NOERROR ioctl on some other
6155 * thread, then sd_iocblk will still be NULL, and IOCWAITNE
6156 * will be set. Pretend as if we just timed out. Note that
6157 * this other thread waited at least STRTIMOUT before trying to
6158 * awaken our thread, so this is indistinguishable (even for
6159 * INFTIM) from the case where we failed with ETIME waiting on
6160 * IOCWAIT in the prior loop.
6162 if (cv_rval > 0 && !(flag & STR_NOERROR) &&
6163 stp->sd_iocblk == NULL && (stp->sd_flag & IOCWAITNE)) {
6164 cv_rval = -1;
6168 * note: STR_NOERROR does not protect
6169 * us here.. use ic_timout < 0
6171 if (cv_rval <= 0) {
6172 if (cv_rval == 0) {
6173 error = EINTR;
6174 } else {
6175 error = ETIME;
6178 * A message could have come in after we were scheduled
6179 * but before we were actually run.
6181 bp = stp->sd_iocblk;
6182 stp->sd_iocblk = NULL;
6183 if (bp != NULL) {
6184 if ((bp->b_datap->db_type == M_COPYIN) ||
6185 (bp->b_datap->db_type == M_COPYOUT)) {
6186 mutex_exit(&stp->sd_lock);
6187 if (bp->b_cont) {
6188 freemsg(bp->b_cont);
6189 bp->b_cont = NULL;
6191 bp->b_datap->db_type = M_IOCDATA;
6192 bp->b_wptr = bp->b_rptr +
6193 sizeof (struct copyresp);
6194 resp = (struct copyresp *)bp->b_rptr;
6195 resp->cp_rval =
6196 (caddr_t)1; /* failure */
6197 stream_willservice(stp);
6198 putnext(stp->sd_wrq, bp);
6199 stream_runservice(stp);
6200 mutex_enter(&stp->sd_lock);
6201 } else {
6202 freemsg(bp);
6205 stp->sd_flag &= ~waitflags;
6206 cv_broadcast(&stp->sd_iocmonitor);
6207 mutex_exit(&stp->sd_lock);
6208 crfree(crp);
6209 return (error);
6212 bp = stp->sd_iocblk;
6214 * Note: it is strictly impossible to get here with sd_iocblk set to
6215 * -1. This is because the initial loop above doesn't allow any new
6216 * ioctls into the fray until all others have passed this point.
6218 ASSERT(bp != NULL && bp != (mblk_t *)-1);
6219 TRACE_1(TR_FAC_STREAMS_FR,
6220 TR_STRDOIOCTL_ACK, "strdoioctl got reply: bp %p", bp);
6221 if ((bp->b_datap->db_type == M_IOCACK) ||
6222 (bp->b_datap->db_type == M_IOCNAK)) {
6223 /* for detection of duplicate ioctl replies */
6224 stp->sd_iocblk = (mblk_t *)-1;
6225 stp->sd_flag &= ~waitflags;
6226 cv_broadcast(&stp->sd_iocmonitor);
6227 mutex_exit(&stp->sd_lock);
6228 } else {
6230 * flags not cleared here because we're still doing
6231 * copy in/out for ioctl.
6233 stp->sd_iocblk = NULL;
6234 mutex_exit(&stp->sd_lock);
6239 * Have received acknowledgment.
6242 switch (bp->b_datap->db_type) {
6243 case M_IOCACK:
6245 * Positive ack.
6247 iocbp = (struct iocblk *)bp->b_rptr;
6250 * Set error if indicated.
6252 if (iocbp->ioc_error) {
6253 error = iocbp->ioc_error;
6254 break;
6258 * Set return value.
6260 *rvalp = iocbp->ioc_rval;
6263 * Data may have been returned in ACK message (ioc_count > 0).
6264 * If so, copy it out to the user's buffer.
6266 if (iocbp->ioc_count && !transparent) {
6267 if (error = getiocd(bp, strioc->ic_dp, copyflag))
6268 break;
6270 if (!transparent) {
6271 if (len) /* an M_COPYOUT was used with I_STR */
6272 strioc->ic_len = len;
6273 else
6274 strioc->ic_len = (int)iocbp->ioc_count;
6276 break;
6278 case M_IOCNAK:
6280 * Negative ack.
6282 * The only thing to do is set error as specified
6283 * in neg ack packet.
6285 iocbp = (struct iocblk *)bp->b_rptr;
6287 error = (iocbp->ioc_error ? iocbp->ioc_error : EINVAL);
6288 break;
6290 case M_COPYIN:
6292 * Driver or module has requested user ioctl data.
6294 reqp = (struct copyreq *)bp->b_rptr;
6297 * M_COPYIN should *never* have a message attached, though
6298 * it's harmless if it does -- thus, panic on a DEBUG
6299 * kernel and just free it on a non-DEBUG build.
6301 ASSERT(bp->b_cont == NULL);
6302 if (bp->b_cont != NULL) {
6303 freemsg(bp->b_cont);
6304 bp->b_cont = NULL;
6307 error = putiocd(bp, reqp->cq_addr, flag, crp);
6308 if (error && bp->b_cont) {
6309 freemsg(bp->b_cont);
6310 bp->b_cont = NULL;
6313 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6314 bp->b_datap->db_type = M_IOCDATA;
6316 mblk_setcred(bp, crp, curproc->p_pid);
6317 resp = (struct copyresp *)bp->b_rptr;
6318 resp->cp_rval = (caddr_t)(uintptr_t)error;
6319 resp->cp_flag = (fflags & FMODELS);
6321 stream_willservice(stp);
6322 putnext(stp->sd_wrq, bp);
6323 stream_runservice(stp);
6325 if (error) {
6326 mutex_enter(&stp->sd_lock);
6327 stp->sd_flag &= ~waitflags;
6328 cv_broadcast(&stp->sd_iocmonitor);
6329 mutex_exit(&stp->sd_lock);
6330 crfree(crp);
6331 return (error);
6334 goto waitioc;
6336 case M_COPYOUT:
6338 * Driver or module has ioctl data for a user.
6340 reqp = (struct copyreq *)bp->b_rptr;
6341 ASSERT(bp->b_cont != NULL);
6344 * Always (transparent or non-transparent )
6345 * use the address specified in the request
6347 taddr = reqp->cq_addr;
6348 if (!transparent)
6349 len = (int)reqp->cq_size;
6351 /* copyout data to the provided address */
6352 error = getiocd(bp, taddr, copyflag);
6354 freemsg(bp->b_cont);
6355 bp->b_cont = NULL;
6357 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6358 bp->b_datap->db_type = M_IOCDATA;
6360 mblk_setcred(bp, crp, curproc->p_pid);
6361 resp = (struct copyresp *)bp->b_rptr;
6362 resp->cp_rval = (caddr_t)(uintptr_t)error;
6363 resp->cp_flag = (fflags & FMODELS);
6365 stream_willservice(stp);
6366 putnext(stp->sd_wrq, bp);
6367 stream_runservice(stp);
6369 if (error) {
6370 mutex_enter(&stp->sd_lock);
6371 stp->sd_flag &= ~waitflags;
6372 cv_broadcast(&stp->sd_iocmonitor);
6373 mutex_exit(&stp->sd_lock);
6374 crfree(crp);
6375 return (error);
6377 goto waitioc;
6379 default:
6380 ASSERT(0);
6381 mutex_enter(&stp->sd_lock);
6382 stp->sd_flag &= ~waitflags;
6383 cv_broadcast(&stp->sd_iocmonitor);
6384 mutex_exit(&stp->sd_lock);
6385 break;
6388 freemsg(bp);
6389 crfree(crp);
6390 return (error);
6394 * Send an M_CMD message downstream and wait for a reply. This is a ptools
6395 * special used to retrieve information from modules/drivers a stream without
6396 * being subjected to flow control or interfering with pending messages on the
6397 * stream (e.g. an ioctl in flight).
6400 strdocmd(struct stdata *stp, struct strcmd *scp, cred_t *crp)
6402 mblk_t *mp;
6403 struct cmdblk *cmdp;
6404 int error = 0;
6405 int errs = STRHUP|STRDERR|STWRERR|STPLEX;
6406 clock_t rval, timeout = STRTIMOUT;
6408 if (scp->sc_len < 0 || scp->sc_len > sizeof (scp->sc_buf) ||
6409 scp->sc_timeout < -1)
6410 return (EINVAL);
6412 if (scp->sc_timeout > 0)
6413 timeout = scp->sc_timeout * MILLISEC;
6415 if ((mp = allocb_cred(sizeof (struct cmdblk), crp,
6416 curproc->p_pid)) == NULL)
6417 return (ENOMEM);
6419 crhold(crp);
6421 cmdp = (struct cmdblk *)mp->b_wptr;
6422 cmdp->cb_cr = crp;
6423 cmdp->cb_cmd = scp->sc_cmd;
6424 cmdp->cb_len = scp->sc_len;
6425 cmdp->cb_error = 0;
6426 mp->b_wptr += sizeof (struct cmdblk);
6428 DB_TYPE(mp) = M_CMD;
6429 DB_CPID(mp) = curproc->p_pid;
6432 * Copy in the payload.
6434 if (cmdp->cb_len > 0) {
6435 mp->b_cont = allocb_cred(sizeof (scp->sc_buf), crp,
6436 curproc->p_pid);
6437 if (mp->b_cont == NULL) {
6438 error = ENOMEM;
6439 goto out;
6442 /* cb_len comes from sc_len, which has already been checked */
6443 ASSERT(cmdp->cb_len <= sizeof (scp->sc_buf));
6444 (void) bcopy(scp->sc_buf, mp->b_cont->b_wptr, cmdp->cb_len);
6445 mp->b_cont->b_wptr += cmdp->cb_len;
6446 DB_CPID(mp->b_cont) = curproc->p_pid;
6450 * Since this mechanism is strictly for ptools, and since only one
6451 * process can be grabbed at a time, we simply fail if there's
6452 * currently an operation pending.
6454 mutex_enter(&stp->sd_lock);
6455 if (stp->sd_flag & STRCMDWAIT) {
6456 mutex_exit(&stp->sd_lock);
6457 error = EBUSY;
6458 goto out;
6460 stp->sd_flag |= STRCMDWAIT;
6461 ASSERT(stp->sd_cmdblk == NULL);
6462 mutex_exit(&stp->sd_lock);
6464 putnext(stp->sd_wrq, mp);
6465 mp = NULL;
6468 * Timed wait for acknowledgment. If the reply has already arrived,
6469 * don't sleep. If awakened from the sleep, fail only if the reply
6470 * has not arrived by then. Otherwise, process the reply.
6472 mutex_enter(&stp->sd_lock);
6473 while (stp->sd_cmdblk == NULL) {
6474 if (stp->sd_flag & errs) {
6475 if ((error = strgeterr(stp, errs, 0)) != 0)
6476 goto waitout;
6479 rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock, timeout, 0);
6480 if (stp->sd_cmdblk != NULL)
6481 break;
6483 if (rval <= 0) {
6484 error = (rval == 0) ? EINTR : ETIME;
6485 goto waitout;
6490 * We received a reply.
6492 mp = stp->sd_cmdblk;
6493 stp->sd_cmdblk = NULL;
6494 ASSERT(mp != NULL && DB_TYPE(mp) == M_CMD);
6495 ASSERT(stp->sd_flag & STRCMDWAIT);
6496 stp->sd_flag &= ~STRCMDWAIT;
6497 mutex_exit(&stp->sd_lock);
6499 cmdp = (struct cmdblk *)mp->b_rptr;
6500 if ((error = cmdp->cb_error) != 0)
6501 goto out;
6504 * Data may have been returned in the reply (cb_len > 0).
6505 * If so, copy it out to the user's buffer.
6507 if (cmdp->cb_len > 0) {
6508 if (mp->b_cont == NULL || MBLKL(mp->b_cont) < cmdp->cb_len) {
6509 error = EPROTO;
6510 goto out;
6513 cmdp->cb_len = MIN(cmdp->cb_len, sizeof (scp->sc_buf));
6514 (void) bcopy(mp->b_cont->b_rptr, scp->sc_buf, cmdp->cb_len);
6516 scp->sc_len = cmdp->cb_len;
6517 out:
6518 freemsg(mp);
6519 crfree(crp);
6520 return (error);
6521 waitout:
6522 ASSERT(stp->sd_cmdblk == NULL);
6523 stp->sd_flag &= ~STRCMDWAIT;
6524 mutex_exit(&stp->sd_lock);
6525 crfree(crp);
6526 return (error);
6530 * For the SunOS keyboard driver.
6531 * Return the next available "ioctl" sequence number.
6532 * Exported, so that streams modules can send "ioctl" messages
6533 * downstream from their open routine.
6536 getiocseqno(void)
6538 int i;
6540 mutex_enter(&strresources);
6541 i = ++ioc_id;
6542 mutex_exit(&strresources);
6543 return (i);
6547 * Get the next message from the read queue. If the message is
6548 * priority, STRPRI will have been set by strrput(). This flag
6549 * should be reset only when the entire message at the front of the
6550 * queue as been consumed.
6552 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
6555 strgetmsg(
6556 struct vnode *vp,
6557 struct strbuf *mctl,
6558 struct strbuf *mdata,
6559 unsigned char *prip,
6560 int *flagsp,
6561 int fmode,
6562 rval_t *rvp)
6564 struct stdata *stp;
6565 mblk_t *bp, *nbp;
6566 mblk_t *savemp = NULL;
6567 mblk_t *savemptail = NULL;
6568 uint_t old_sd_flag;
6569 int flg;
6570 int more = 0;
6571 int error = 0;
6572 char first = 1;
6573 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
6574 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
6575 unsigned char pri = 0;
6576 queue_t *q;
6577 int pr = 0; /* Partial read successful */
6578 struct uio uios;
6579 struct uio *uiop = &uios;
6580 struct iovec iovs;
6581 unsigned char type;
6583 TRACE_1(TR_FAC_STREAMS_FR, TR_STRGETMSG_ENTER,
6584 "strgetmsg:%p", vp);
6586 ASSERT(vp->v_stream);
6587 stp = vp->v_stream;
6588 rvp->r_val1 = 0;
6590 mutex_enter(&stp->sd_lock);
6592 if ((error = i_straccess(stp, JCREAD)) != 0) {
6593 mutex_exit(&stp->sd_lock);
6594 return (error);
6597 if (stp->sd_flag & (STRDERR|STPLEX)) {
6598 error = strgeterr(stp, STRDERR|STPLEX, 0);
6599 if (error != 0) {
6600 mutex_exit(&stp->sd_lock);
6601 return (error);
6604 mutex_exit(&stp->sd_lock);
6606 switch (*flagsp) {
6607 case MSG_HIPRI:
6608 if (*prip != 0)
6609 return (EINVAL);
6610 break;
6612 case MSG_ANY:
6613 case MSG_BAND:
6614 break;
6616 default:
6617 return (EINVAL);
6620 * Setup uio and iov for data part
6622 iovs.iov_base = mdata->buf;
6623 iovs.iov_len = mdata->maxlen;
6624 uios.uio_iov = &iovs;
6625 uios.uio_iovcnt = 1;
6626 uios.uio_loffset = 0;
6627 uios.uio_segflg = UIO_USERSPACE;
6628 uios.uio_fmode = 0;
6629 uios.uio_extflg = UIO_COPY_CACHED;
6630 uios.uio_resid = mdata->maxlen;
6631 uios.uio_offset = 0;
6633 q = _RD(stp->sd_wrq);
6634 mutex_enter(&stp->sd_lock);
6635 old_sd_flag = stp->sd_flag;
6636 mark = 0;
6637 for (;;) {
6638 int done = 0;
6639 mblk_t *q_first = q->q_first;
6642 * Get the next message of appropriate priority
6643 * from the stream head. If the caller is interested
6644 * in band or hipri messages, then they should already
6645 * be enqueued at the stream head. On the other hand
6646 * if the caller wants normal (band 0) messages, they
6647 * might be deferred in a synchronous stream and they
6648 * will need to be pulled up.
6650 * After we have dequeued a message, we might find that
6651 * it was a deferred M_SIG that was enqueued at the
6652 * stream head. It must now be posted as part of the
6653 * read by calling strsignal_nolock().
6655 * Also note that strrput does not enqueue an M_PCSIG,
6656 * and there cannot be more than one hipri message,
6657 * so there was no need to have the M_PCSIG case.
6659 * At some time it might be nice to try and wrap the
6660 * functionality of kstrgetmsg() and strgetmsg() into
6661 * a common routine so to reduce the amount of replicated
6662 * code (since they are extremely similar).
6664 if (!(*flagsp & (MSG_HIPRI|MSG_BAND))) {
6665 /* Asking for normal, band0 data */
6666 bp = strget(stp, q, uiop, first, &error);
6667 ASSERT(MUTEX_HELD(&stp->sd_lock));
6668 if (bp != NULL) {
6669 if (DB_TYPE(bp) == M_SIG) {
6670 strsignal_nolock(stp, *bp->b_rptr,
6671 bp->b_band);
6672 freemsg(bp);
6673 continue;
6674 } else {
6675 break;
6678 if (error != 0)
6679 goto getmout;
6682 * We can't depend on the value of STRPRI here because
6683 * the stream head may be in transit. Therefore, we
6684 * must look at the type of the first message to
6685 * determine if a high priority messages is waiting
6687 } else if ((*flagsp & MSG_HIPRI) && q_first != NULL &&
6688 DB_TYPE(q_first) >= QPCTL &&
6689 (bp = getq_noenab(q, 0)) != NULL) {
6690 /* Asked for HIPRI and got one */
6691 ASSERT(DB_TYPE(bp) >= QPCTL);
6692 break;
6693 } else if ((*flagsp & MSG_BAND) && q_first != NULL &&
6694 ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
6695 (bp = getq_noenab(q, 0)) != NULL) {
6697 * Asked for at least band "prip" and got either at
6698 * least that band or a hipri message.
6700 ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
6701 if (DB_TYPE(bp) == M_SIG) {
6702 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
6703 freemsg(bp);
6704 continue;
6705 } else {
6706 break;
6710 /* No data. Time to sleep? */
6711 qbackenable(q, 0);
6714 * If STRHUP or STREOF, return 0 length control and data.
6715 * If resid is 0, then a read(fd,buf,0) was done. Do not
6716 * sleep to satisfy this request because by default we have
6717 * zero bytes to return.
6719 if ((stp->sd_flag & (STRHUP|STREOF)) || (mctl->maxlen == 0 &&
6720 mdata->maxlen == 0)) {
6721 mctl->len = mdata->len = 0;
6722 *flagsp = 0;
6723 mutex_exit(&stp->sd_lock);
6724 return (0);
6726 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_WAIT,
6727 "strgetmsg calls strwaitq:%p, %p",
6728 vp, uiop);
6729 if (((error = strwaitq(stp, GETWAIT, (ssize_t)0, fmode, -1,
6730 &done)) != 0) || done) {
6731 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_DONE,
6732 "strgetmsg error or done:%p, %p",
6733 vp, uiop);
6734 mutex_exit(&stp->sd_lock);
6735 return (error);
6737 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_AWAKE,
6738 "strgetmsg awakes:%p, %p", vp, uiop);
6739 if ((error = i_straccess(stp, JCREAD)) != 0) {
6740 mutex_exit(&stp->sd_lock);
6741 return (error);
6743 first = 0;
6745 ASSERT(bp != NULL);
6747 * Extract any mark information. If the message is not completely
6748 * consumed this information will be put in the mblk
6749 * that is putback.
6750 * If MSGMARKNEXT is set and the message is completely consumed
6751 * the STRATMARK flag will be set below. Likewise, if
6752 * MSGNOTMARKNEXT is set and the message is
6753 * completely consumed STRNOTATMARK will be set.
6755 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
6756 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
6757 (MSGMARKNEXT|MSGNOTMARKNEXT));
6758 if (mark != 0 && bp == stp->sd_mark) {
6759 mark |= _LASTMARK;
6760 stp->sd_mark = NULL;
6763 * keep track of the original message type and priority
6765 pri = bp->b_band;
6766 type = bp->b_datap->db_type;
6767 if (type == M_PASSFP) {
6768 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
6769 stp->sd_mark = bp;
6770 bp->b_flag |= mark & ~_LASTMARK;
6771 putback(stp, q, bp, pri);
6772 qbackenable(q, pri);
6773 mutex_exit(&stp->sd_lock);
6774 return (EBADMSG);
6776 ASSERT(type != M_SIG);
6779 * Set this flag so strrput will not generate signals. Need to
6780 * make sure this flag is cleared before leaving this routine
6781 * else signals will stop being sent.
6783 stp->sd_flag |= STRGETINPROG;
6784 mutex_exit(&stp->sd_lock);
6786 if (STREAM_NEEDSERVICE(stp))
6787 stream_runservice(stp);
6790 * Set HIPRI flag if message is priority.
6792 if (type >= QPCTL)
6793 flg = MSG_HIPRI;
6794 else
6795 flg = MSG_BAND;
6798 * First process PROTO or PCPROTO blocks, if any.
6800 if (mctl->maxlen >= 0 && type != M_DATA) {
6801 size_t n, bcnt;
6802 char *ubuf;
6804 bcnt = mctl->maxlen;
6805 ubuf = mctl->buf;
6806 while (bp != NULL && bp->b_datap->db_type != M_DATA) {
6807 if ((n = MIN(bcnt, bp->b_wptr - bp->b_rptr)) != 0 &&
6808 copyout(bp->b_rptr, ubuf, n)) {
6809 error = EFAULT;
6810 mutex_enter(&stp->sd_lock);
6812 * clear stream head pri flag based on
6813 * first message type
6815 if (type >= QPCTL) {
6816 ASSERT(type == M_PCPROTO);
6817 stp->sd_flag &= ~STRPRI;
6819 more = 0;
6820 freemsg(bp);
6821 goto getmout;
6823 ubuf += n;
6824 bp->b_rptr += n;
6825 if (bp->b_rptr >= bp->b_wptr) {
6826 nbp = bp;
6827 bp = bp->b_cont;
6828 freeb(nbp);
6830 ASSERT(n <= bcnt);
6831 bcnt -= n;
6832 if (bcnt == 0)
6833 break;
6835 mctl->len = mctl->maxlen - bcnt;
6836 } else
6837 mctl->len = -1;
6839 if (bp && bp->b_datap->db_type != M_DATA) {
6841 * More PROTO blocks in msg.
6843 more |= MORECTL;
6844 savemp = bp;
6845 while (bp && bp->b_datap->db_type != M_DATA) {
6846 savemptail = bp;
6847 bp = bp->b_cont;
6849 savemptail->b_cont = NULL;
6853 * Now process DATA blocks, if any.
6855 if (mdata->maxlen >= 0 && bp) {
6857 * struiocopyout will consume a potential zero-length
6858 * M_DATA even if uio_resid is zero.
6860 size_t oldresid = uiop->uio_resid;
6862 bp = struiocopyout(bp, uiop, &error);
6863 if (error != 0) {
6864 mutex_enter(&stp->sd_lock);
6866 * clear stream head hi pri flag based on
6867 * first message
6869 if (type >= QPCTL) {
6870 ASSERT(type == M_PCPROTO);
6871 stp->sd_flag &= ~STRPRI;
6873 more = 0;
6874 freemsg(savemp);
6875 goto getmout;
6878 * (pr == 1) indicates a partial read.
6880 if (oldresid > uiop->uio_resid)
6881 pr = 1;
6882 mdata->len = mdata->maxlen - uiop->uio_resid;
6883 } else
6884 mdata->len = -1;
6886 if (bp) { /* more data blocks in msg */
6887 more |= MOREDATA;
6888 if (savemp)
6889 savemptail->b_cont = bp;
6890 else
6891 savemp = bp;
6894 mutex_enter(&stp->sd_lock);
6895 if (savemp) {
6896 if (pr && (savemp->b_datap->db_type == M_DATA) &&
6897 msgnodata(savemp)) {
6899 * Avoid queuing a zero-length tail part of
6900 * a message. pr=1 indicates that we read some of
6901 * the message.
6903 freemsg(savemp);
6904 more &= ~MOREDATA;
6906 * clear stream head hi pri flag based on
6907 * first message
6909 if (type >= QPCTL) {
6910 ASSERT(type == M_PCPROTO);
6911 stp->sd_flag &= ~STRPRI;
6913 } else {
6914 savemp->b_band = pri;
6916 * If the first message was HIPRI and the one we're
6917 * putting back isn't, then clear STRPRI, otherwise
6918 * set STRPRI again. Note that we must set STRPRI
6919 * again since the flush logic in strrput_nondata()
6920 * may have cleared it while we had sd_lock dropped.
6922 if (type >= QPCTL) {
6923 ASSERT(type == M_PCPROTO);
6924 if (queclass(savemp) < QPCTL)
6925 stp->sd_flag &= ~STRPRI;
6926 else
6927 stp->sd_flag |= STRPRI;
6928 } else if (queclass(savemp) >= QPCTL) {
6930 * The first message was not a HIPRI message,
6931 * but the one we are about to putback is.
6932 * For simplicitly, we do not allow for HIPRI
6933 * messages to be embedded in the message
6934 * body, so just force it to same type as
6935 * first message.
6937 ASSERT(type == M_DATA || type == M_PROTO);
6938 ASSERT(savemp->b_datap->db_type == M_PCPROTO);
6939 savemp->b_datap->db_type = type;
6941 if (mark != 0) {
6942 savemp->b_flag |= mark & ~_LASTMARK;
6943 if ((mark & _LASTMARK) &&
6944 (stp->sd_mark == NULL)) {
6946 * If another marked message arrived
6947 * while sd_lock was not held sd_mark
6948 * would be non-NULL.
6950 stp->sd_mark = savemp;
6953 putback(stp, q, savemp, pri);
6955 } else {
6957 * The complete message was consumed.
6959 * If another M_PCPROTO arrived while sd_lock was not held
6960 * it would have been discarded since STRPRI was still set.
6962 * Move the MSG*MARKNEXT information
6963 * to the stream head just in case
6964 * the read queue becomes empty.
6965 * clear stream head hi pri flag based on
6966 * first message
6968 * If the stream head was at the mark
6969 * (STRATMARK) before we dropped sd_lock above
6970 * and some data was consumed then we have
6971 * moved past the mark thus STRATMARK is
6972 * cleared. However, if a message arrived in
6973 * strrput during the copyout above causing
6974 * STRATMARK to be set we can not clear that
6975 * flag.
6977 if (type >= QPCTL) {
6978 ASSERT(type == M_PCPROTO);
6979 stp->sd_flag &= ~STRPRI;
6981 if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
6982 if (mark & MSGMARKNEXT) {
6983 stp->sd_flag &= ~STRNOTATMARK;
6984 stp->sd_flag |= STRATMARK;
6985 } else if (mark & MSGNOTMARKNEXT) {
6986 stp->sd_flag &= ~STRATMARK;
6987 stp->sd_flag |= STRNOTATMARK;
6988 } else {
6989 stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
6991 } else if (pr && (old_sd_flag & STRATMARK)) {
6992 stp->sd_flag &= ~STRATMARK;
6996 *flagsp = flg;
6997 *prip = pri;
7000 * Getmsg cleanup processing - if the state of the queue has changed
7001 * some signals may need to be sent and/or poll awakened.
7003 getmout:
7004 qbackenable(q, pri);
7007 * We dropped the stream head lock above. Send all M_SIG messages
7008 * before processing stream head for SIGPOLL messages.
7010 ASSERT(MUTEX_HELD(&stp->sd_lock));
7011 while ((bp = q->q_first) != NULL &&
7012 (bp->b_datap->db_type == M_SIG)) {
7014 * sd_lock is held so the content of the read queue can not
7015 * change.
7017 bp = getq(q);
7018 ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7020 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7021 mutex_exit(&stp->sd_lock);
7022 freemsg(bp);
7023 if (STREAM_NEEDSERVICE(stp))
7024 stream_runservice(stp);
7025 mutex_enter(&stp->sd_lock);
7029 * stream head cannot change while we make the determination
7030 * whether or not to send a signal. Drop the flag to allow strrput
7031 * to send firstmsgsigs again.
7033 stp->sd_flag &= ~STRGETINPROG;
7036 * If the type of message at the front of the queue changed
7037 * due to the receive the appropriate signals and pollwakeup events
7038 * are generated. The type of changes are:
7039 * Processed a hipri message, q_first is not hipri.
7040 * Processed a band X message, and q_first is band Y.
7041 * The generated signals and pollwakeups are identical to what
7042 * strrput() generates should the message that is now on q_first
7043 * arrive to an empty read queue.
7045 * Note: only strrput will send a signal for a hipri message.
7047 if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7048 strsigset_t signals = 0;
7049 strpollset_t pollwakeups = 0;
7051 if (flg & MSG_HIPRI) {
7053 * Removed a hipri message. Regular data at
7054 * the front of the queue.
7056 if (bp->b_band == 0) {
7057 signals = S_INPUT | S_RDNORM;
7058 pollwakeups = POLLIN | POLLRDNORM;
7059 } else {
7060 signals = S_INPUT | S_RDBAND;
7061 pollwakeups = POLLIN | POLLRDBAND;
7063 } else if (pri != bp->b_band) {
7065 * The band is different for the new q_first.
7067 if (bp->b_band == 0) {
7068 signals = S_RDNORM;
7069 pollwakeups = POLLIN | POLLRDNORM;
7070 } else {
7071 signals = S_RDBAND;
7072 pollwakeups = POLLIN | POLLRDBAND;
7076 if (pollwakeups != 0) {
7077 if (pollwakeups == (POLLIN | POLLRDNORM)) {
7078 if (!(stp->sd_rput_opt & SR_POLLIN))
7079 goto no_pollwake;
7080 stp->sd_rput_opt &= ~SR_POLLIN;
7082 mutex_exit(&stp->sd_lock);
7083 pollwakeup(&stp->sd_pollist, pollwakeups);
7084 mutex_enter(&stp->sd_lock);
7086 no_pollwake:
7088 if (stp->sd_sigflags & signals)
7089 strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7091 mutex_exit(&stp->sd_lock);
7093 rvp->r_val1 = more;
7094 return (error);
7095 #undef _LASTMARK
7099 * Get the next message from the read queue. If the message is
7100 * priority, STRPRI will have been set by strrput(). This flag
7101 * should be reset only when the entire message at the front of the
7102 * queue as been consumed.
7104 * If uiop is NULL all data is returned in mctlp.
7105 * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
7106 * not enabled.
7107 * The timeout parameter is in milliseconds; -1 for infinity.
7108 * This routine handles the consolidation private flags:
7109 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7110 * MSG_DELAYERROR Defer the error check until the queue is empty.
7111 * MSG_HOLDSIG Hold signals while waiting for data.
7112 * MSG_IPEEK Only peek at messages.
7113 * MSG_DISCARDTAIL Discard the tail M_DATA part of the message
7114 * that doesn't fit.
7115 * MSG_NOMARK If the message is marked leave it on the queue.
7117 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
7120 kstrgetmsg(
7121 struct vnode *vp,
7122 mblk_t **mctlp,
7123 struct uio *uiop,
7124 unsigned char *prip,
7125 int *flagsp,
7126 clock_t timout,
7127 rval_t *rvp)
7129 struct stdata *stp;
7130 mblk_t *bp, *nbp;
7131 mblk_t *savemp = NULL;
7132 mblk_t *savemptail = NULL;
7133 int flags;
7134 uint_t old_sd_flag;
7135 int flg;
7136 int more = 0;
7137 int error = 0;
7138 char first = 1;
7139 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
7140 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
7141 unsigned char pri = 0;
7142 queue_t *q;
7143 int pr = 0; /* Partial read successful */
7144 unsigned char type;
7146 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_ENTER,
7147 "kstrgetmsg:%p", vp);
7149 ASSERT(vp->v_stream);
7150 stp = vp->v_stream;
7151 rvp->r_val1 = 0;
7153 mutex_enter(&stp->sd_lock);
7155 if ((error = i_straccess(stp, JCREAD)) != 0) {
7156 mutex_exit(&stp->sd_lock);
7157 return (error);
7160 flags = *flagsp;
7161 if (stp->sd_flag & (STRDERR|STPLEX)) {
7162 if ((stp->sd_flag & STPLEX) ||
7163 (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == 0) {
7164 error = strgeterr(stp, STRDERR|STPLEX,
7165 (flags & MSG_IPEEK));
7166 if (error != 0) {
7167 mutex_exit(&stp->sd_lock);
7168 return (error);
7172 mutex_exit(&stp->sd_lock);
7174 switch (flags & (MSG_HIPRI|MSG_ANY|MSG_BAND)) {
7175 case MSG_HIPRI:
7176 if (*prip != 0)
7177 return (EINVAL);
7178 break;
7180 case MSG_ANY:
7181 case MSG_BAND:
7182 break;
7184 default:
7185 return (EINVAL);
7188 retry:
7189 q = _RD(stp->sd_wrq);
7190 mutex_enter(&stp->sd_lock);
7191 old_sd_flag = stp->sd_flag;
7192 mark = 0;
7193 for (;;) {
7194 int done = 0;
7195 int waitflag;
7196 int fmode;
7197 mblk_t *q_first = q->q_first;
7200 * This section of the code operates just like the code
7201 * in strgetmsg(). There is a comment there about what
7202 * is going on here.
7204 if (!(flags & (MSG_HIPRI|MSG_BAND))) {
7205 /* Asking for normal, band0 data */
7206 bp = strget(stp, q, uiop, first, &error);
7207 ASSERT(MUTEX_HELD(&stp->sd_lock));
7208 if (bp != NULL) {
7209 if (DB_TYPE(bp) == M_SIG) {
7210 strsignal_nolock(stp, *bp->b_rptr,
7211 bp->b_band);
7212 freemsg(bp);
7213 continue;
7214 } else {
7215 break;
7218 if (error != 0) {
7219 goto getmout;
7222 * We can't depend on the value of STRPRI here because
7223 * the stream head may be in transit. Therefore, we
7224 * must look at the type of the first message to
7225 * determine if a high priority messages is waiting
7227 } else if ((flags & MSG_HIPRI) && q_first != NULL &&
7228 DB_TYPE(q_first) >= QPCTL &&
7229 (bp = getq_noenab(q, 0)) != NULL) {
7230 ASSERT(DB_TYPE(bp) >= QPCTL);
7231 break;
7232 } else if ((flags & MSG_BAND) && q_first != NULL &&
7233 ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
7234 (bp = getq_noenab(q, 0)) != NULL) {
7236 * Asked for at least band "prip" and got either at
7237 * least that band or a hipri message.
7239 ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
7240 if (DB_TYPE(bp) == M_SIG) {
7241 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7242 freemsg(bp);
7243 continue;
7244 } else {
7245 break;
7249 /* No data. Time to sleep? */
7250 qbackenable(q, 0);
7253 * Delayed error notification?
7255 if ((stp->sd_flag & (STRDERR|STPLEX)) &&
7256 (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == MSG_DELAYERROR) {
7257 error = strgeterr(stp, STRDERR|STPLEX,
7258 (flags & MSG_IPEEK));
7259 if (error != 0) {
7260 mutex_exit(&stp->sd_lock);
7261 return (error);
7266 * If STRHUP or STREOF, return 0 length control and data.
7267 * If a read(fd,buf,0) has been done, do not sleep, just
7268 * return.
7270 * If mctlp == NULL and uiop == NULL, then the code will
7271 * do the strwaitq. This is an understood way of saying
7272 * sleep "polling" until a message is received.
7274 if ((stp->sd_flag & (STRHUP|STREOF)) ||
7275 (uiop != NULL && uiop->uio_resid == 0)) {
7276 if (mctlp != NULL)
7277 *mctlp = NULL;
7278 *flagsp = 0;
7279 mutex_exit(&stp->sd_lock);
7280 return (0);
7283 waitflag = GETWAIT;
7284 if (flags &
7285 (MSG_HOLDSIG|MSG_IGNERROR|MSG_IPEEK|MSG_DELAYERROR)) {
7286 if (flags & MSG_HOLDSIG)
7287 waitflag |= STR_NOSIG;
7288 if (flags & MSG_IGNERROR)
7289 waitflag |= STR_NOERROR;
7290 if (flags & MSG_IPEEK)
7291 waitflag |= STR_PEEK;
7292 if (flags & MSG_DELAYERROR)
7293 waitflag |= STR_DELAYERR;
7295 if (uiop != NULL)
7296 fmode = uiop->uio_fmode;
7297 else
7298 fmode = 0;
7300 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_WAIT,
7301 "kstrgetmsg calls strwaitq:%p, %p",
7302 vp, uiop);
7303 if (((error = strwaitq(stp, waitflag, (ssize_t)0,
7304 fmode, timout, &done))) != 0 || done) {
7305 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_DONE,
7306 "kstrgetmsg error or done:%p, %p",
7307 vp, uiop);
7308 mutex_exit(&stp->sd_lock);
7309 return (error);
7311 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_AWAKE,
7312 "kstrgetmsg awakes:%p, %p", vp, uiop);
7313 if ((error = i_straccess(stp, JCREAD)) != 0) {
7314 mutex_exit(&stp->sd_lock);
7315 return (error);
7317 first = 0;
7319 ASSERT(bp != NULL);
7321 * Extract any mark information. If the message is not completely
7322 * consumed this information will be put in the mblk
7323 * that is putback.
7324 * If MSGMARKNEXT is set and the message is completely consumed
7325 * the STRATMARK flag will be set below. Likewise, if
7326 * MSGNOTMARKNEXT is set and the message is
7327 * completely consumed STRNOTATMARK will be set.
7329 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
7330 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
7331 (MSGMARKNEXT|MSGNOTMARKNEXT));
7332 pri = bp->b_band;
7333 if (mark != 0) {
7335 * If the caller doesn't want the mark return.
7336 * Used to implement MSG_WAITALL in sockets.
7338 if (flags & MSG_NOMARK) {
7339 putback(stp, q, bp, pri);
7340 qbackenable(q, pri);
7341 mutex_exit(&stp->sd_lock);
7342 return (EWOULDBLOCK);
7344 if (bp == stp->sd_mark) {
7345 mark |= _LASTMARK;
7346 stp->sd_mark = NULL;
7351 * keep track of the first message type
7353 type = bp->b_datap->db_type;
7355 if (bp->b_datap->db_type == M_PASSFP) {
7356 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7357 stp->sd_mark = bp;
7358 bp->b_flag |= mark & ~_LASTMARK;
7359 putback(stp, q, bp, pri);
7360 qbackenable(q, pri);
7361 mutex_exit(&stp->sd_lock);
7362 return (EBADMSG);
7364 ASSERT(type != M_SIG);
7366 if (flags & MSG_IPEEK) {
7368 * Clear any struioflag - we do the uiomove over again
7369 * when peeking since it simplifies the code.
7371 * Dup the message and put the original back on the queue.
7372 * If dupmsg() fails, try again with copymsg() to see if
7373 * there is indeed a shortage of memory. dupmsg() may fail
7374 * if db_ref in any of the messages reaches its limit.
7377 if ((nbp = dupmsg(bp)) == NULL && (nbp = copymsg(bp)) == NULL) {
7379 * Restore the state of the stream head since we
7380 * need to drop sd_lock (strwaitbuf is sleeping).
7382 size_t size = msgdsize(bp);
7384 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7385 stp->sd_mark = bp;
7386 bp->b_flag |= mark & ~_LASTMARK;
7387 putback(stp, q, bp, pri);
7388 mutex_exit(&stp->sd_lock);
7389 error = strwaitbuf(size, BPRI_HI);
7390 if (error) {
7392 * There is no net change to the queue thus
7393 * no need to qbackenable.
7395 return (error);
7397 goto retry;
7400 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7401 stp->sd_mark = bp;
7402 bp->b_flag |= mark & ~_LASTMARK;
7403 putback(stp, q, bp, pri);
7404 bp = nbp;
7408 * Set this flag so strrput will not generate signals. Need to
7409 * make sure this flag is cleared before leaving this routine
7410 * else signals will stop being sent.
7412 stp->sd_flag |= STRGETINPROG;
7413 mutex_exit(&stp->sd_lock);
7415 if ((stp->sd_rputdatafunc != NULL) && (DB_TYPE(bp) == M_DATA)) {
7416 mblk_t *tmp, *prevmp;
7419 * Put first non-data mblk back to stream head and
7420 * cut the mblk chain so sd_rputdatafunc only sees
7421 * M_DATA mblks. We can skip the first mblk since it
7422 * is M_DATA according to the condition above.
7424 for (prevmp = bp, tmp = bp->b_cont; tmp != NULL;
7425 prevmp = tmp, tmp = tmp->b_cont) {
7426 if (DB_TYPE(tmp) != M_DATA) {
7427 prevmp->b_cont = NULL;
7428 mutex_enter(&stp->sd_lock);
7429 putback(stp, q, tmp, tmp->b_band);
7430 mutex_exit(&stp->sd_lock);
7431 break;
7435 bp = (stp->sd_rputdatafunc)(stp->sd_vnode, bp,
7436 NULL, NULL, NULL, NULL);
7438 if (bp == NULL)
7439 goto retry;
7442 if (STREAM_NEEDSERVICE(stp))
7443 stream_runservice(stp);
7446 * Set HIPRI flag if message is priority.
7448 if (type >= QPCTL)
7449 flg = MSG_HIPRI;
7450 else
7451 flg = MSG_BAND;
7454 * First process PROTO or PCPROTO blocks, if any.
7456 if (mctlp != NULL && type != M_DATA) {
7457 mblk_t *nbp;
7459 *mctlp = bp;
7460 while (bp->b_cont && bp->b_cont->b_datap->db_type != M_DATA)
7461 bp = bp->b_cont;
7462 nbp = bp->b_cont;
7463 bp->b_cont = NULL;
7464 bp = nbp;
7467 if (bp && bp->b_datap->db_type != M_DATA) {
7469 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
7471 more |= MORECTL;
7472 savemp = bp;
7473 while (bp && bp->b_datap->db_type != M_DATA) {
7474 savemptail = bp;
7475 bp = bp->b_cont;
7477 savemptail->b_cont = NULL;
7481 * Now process DATA blocks, if any.
7483 if (uiop == NULL) {
7484 /* Append data to tail of mctlp */
7486 if (mctlp != NULL) {
7487 mblk_t **mpp = mctlp;
7489 while (*mpp != NULL)
7490 mpp = &((*mpp)->b_cont);
7491 *mpp = bp;
7492 bp = NULL;
7494 } else if (uiop->uio_resid >= 0 && bp) {
7495 size_t oldresid = uiop->uio_resid;
7498 * If a streams message is likely to consist
7499 * of many small mblks, it is pulled up into
7500 * one continuous chunk of memory.
7501 * The size of the first mblk may be bogus because
7502 * successive read() calls on the socket reduce
7503 * the size of this mblk until it is exhausted
7504 * and then the code walks on to the next. Thus
7505 * the size of the mblk may not be the original size
7506 * that was passed up, it's simply a remainder
7507 * and hence can be very small without any
7508 * implication that the packet is badly fragmented.
7509 * So the size of the possible second mblk is
7510 * used to spot a badly fragmented packet.
7511 * see longer comment at top of page
7512 * by mblk_pull_len declaration.
7515 if (bp->b_cont != NULL && MBLKL(bp->b_cont) < mblk_pull_len) {
7516 (void) pullupmsg(bp, -1);
7519 bp = struiocopyout(bp, uiop, &error);
7520 if (error != 0) {
7521 if (mctlp != NULL) {
7522 freemsg(*mctlp);
7523 *mctlp = NULL;
7524 } else
7525 freemsg(savemp);
7526 mutex_enter(&stp->sd_lock);
7528 * clear stream head hi pri flag based on
7529 * first message
7531 if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7532 ASSERT(type == M_PCPROTO);
7533 stp->sd_flag &= ~STRPRI;
7535 more = 0;
7536 goto getmout;
7539 * (pr == 1) indicates a partial read.
7541 if (oldresid > uiop->uio_resid)
7542 pr = 1;
7545 if (bp) { /* more data blocks in msg */
7546 more |= MOREDATA;
7547 if (savemp)
7548 savemptail->b_cont = bp;
7549 else
7550 savemp = bp;
7553 mutex_enter(&stp->sd_lock);
7554 if (savemp) {
7555 if (flags & (MSG_IPEEK|MSG_DISCARDTAIL)) {
7557 * When MSG_DISCARDTAIL is set or
7558 * when peeking discard any tail. When peeking this
7559 * is the tail of the dup that was copied out - the
7560 * message has already been putback on the queue.
7561 * Return MOREDATA to the caller even though the data
7562 * is discarded. This is used by sockets (to
7563 * set MSG_TRUNC).
7565 freemsg(savemp);
7566 if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7567 ASSERT(type == M_PCPROTO);
7568 stp->sd_flag &= ~STRPRI;
7570 } else if (pr && (savemp->b_datap->db_type == M_DATA) &&
7571 msgnodata(savemp)) {
7573 * Avoid queuing a zero-length tail part of
7574 * a message. pr=1 indicates that we read some of
7575 * the message.
7577 freemsg(savemp);
7578 more &= ~MOREDATA;
7579 if (type >= QPCTL) {
7580 ASSERT(type == M_PCPROTO);
7581 stp->sd_flag &= ~STRPRI;
7583 } else {
7584 savemp->b_band = pri;
7586 * If the first message was HIPRI and the one we're
7587 * putting back isn't, then clear STRPRI, otherwise
7588 * set STRPRI again. Note that we must set STRPRI
7589 * again since the flush logic in strrput_nondata()
7590 * may have cleared it while we had sd_lock dropped.
7593 if (type >= QPCTL) {
7594 ASSERT(type == M_PCPROTO);
7595 if (queclass(savemp) < QPCTL)
7596 stp->sd_flag &= ~STRPRI;
7597 else
7598 stp->sd_flag |= STRPRI;
7599 } else if (queclass(savemp) >= QPCTL) {
7601 * The first message was not a HIPRI message,
7602 * but the one we are about to putback is.
7603 * For simplicitly, we do not allow for HIPRI
7604 * messages to be embedded in the message
7605 * body, so just force it to same type as
7606 * first message.
7608 ASSERT(type == M_DATA || type == M_PROTO);
7609 ASSERT(savemp->b_datap->db_type == M_PCPROTO);
7610 savemp->b_datap->db_type = type;
7612 if (mark != 0) {
7613 if ((mark & _LASTMARK) &&
7614 (stp->sd_mark == NULL)) {
7616 * If another marked message arrived
7617 * while sd_lock was not held sd_mark
7618 * would be non-NULL.
7620 stp->sd_mark = savemp;
7622 savemp->b_flag |= mark & ~_LASTMARK;
7624 putback(stp, q, savemp, pri);
7626 } else if (!(flags & MSG_IPEEK)) {
7628 * The complete message was consumed.
7630 * If another M_PCPROTO arrived while sd_lock was not held
7631 * it would have been discarded since STRPRI was still set.
7633 * Move the MSG*MARKNEXT information
7634 * to the stream head just in case
7635 * the read queue becomes empty.
7636 * clear stream head hi pri flag based on
7637 * first message
7639 * If the stream head was at the mark
7640 * (STRATMARK) before we dropped sd_lock above
7641 * and some data was consumed then we have
7642 * moved past the mark thus STRATMARK is
7643 * cleared. However, if a message arrived in
7644 * strrput during the copyout above causing
7645 * STRATMARK to be set we can not clear that
7646 * flag.
7647 * XXX A "perimeter" would help by single-threading strrput,
7648 * strread, strgetmsg and kstrgetmsg.
7650 if (type >= QPCTL) {
7651 ASSERT(type == M_PCPROTO);
7652 stp->sd_flag &= ~STRPRI;
7654 if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
7655 if (mark & MSGMARKNEXT) {
7656 stp->sd_flag &= ~STRNOTATMARK;
7657 stp->sd_flag |= STRATMARK;
7658 } else if (mark & MSGNOTMARKNEXT) {
7659 stp->sd_flag &= ~STRATMARK;
7660 stp->sd_flag |= STRNOTATMARK;
7661 } else {
7662 stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
7664 } else if (pr && (old_sd_flag & STRATMARK)) {
7665 stp->sd_flag &= ~STRATMARK;
7669 *flagsp = flg;
7670 *prip = pri;
7673 * Getmsg cleanup processing - if the state of the queue has changed
7674 * some signals may need to be sent and/or poll awakened.
7676 getmout:
7677 qbackenable(q, pri);
7680 * We dropped the stream head lock above. Send all M_SIG messages
7681 * before processing stream head for SIGPOLL messages.
7683 ASSERT(MUTEX_HELD(&stp->sd_lock));
7684 while ((bp = q->q_first) != NULL &&
7685 (bp->b_datap->db_type == M_SIG)) {
7687 * sd_lock is held so the content of the read queue can not
7688 * change.
7690 bp = getq(q);
7691 ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7693 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7694 mutex_exit(&stp->sd_lock);
7695 freemsg(bp);
7696 if (STREAM_NEEDSERVICE(stp))
7697 stream_runservice(stp);
7698 mutex_enter(&stp->sd_lock);
7702 * stream head cannot change while we make the determination
7703 * whether or not to send a signal. Drop the flag to allow strrput
7704 * to send firstmsgsigs again.
7706 stp->sd_flag &= ~STRGETINPROG;
7709 * If the type of message at the front of the queue changed
7710 * due to the receive the appropriate signals and pollwakeup events
7711 * are generated. The type of changes are:
7712 * Processed a hipri message, q_first is not hipri.
7713 * Processed a band X message, and q_first is band Y.
7714 * The generated signals and pollwakeups are identical to what
7715 * strrput() generates should the message that is now on q_first
7716 * arrive to an empty read queue.
7718 * Note: only strrput will send a signal for a hipri message.
7720 if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7721 strsigset_t signals = 0;
7722 strpollset_t pollwakeups = 0;
7724 if (flg & MSG_HIPRI) {
7726 * Removed a hipri message. Regular data at
7727 * the front of the queue.
7729 if (bp->b_band == 0) {
7730 signals = S_INPUT | S_RDNORM;
7731 pollwakeups = POLLIN | POLLRDNORM;
7732 } else {
7733 signals = S_INPUT | S_RDBAND;
7734 pollwakeups = POLLIN | POLLRDBAND;
7736 } else if (pri != bp->b_band) {
7738 * The band is different for the new q_first.
7740 if (bp->b_band == 0) {
7741 signals = S_RDNORM;
7742 pollwakeups = POLLIN | POLLRDNORM;
7743 } else {
7744 signals = S_RDBAND;
7745 pollwakeups = POLLIN | POLLRDBAND;
7749 if (pollwakeups != 0) {
7750 if (pollwakeups == (POLLIN | POLLRDNORM)) {
7751 if (!(stp->sd_rput_opt & SR_POLLIN))
7752 goto no_pollwake;
7753 stp->sd_rput_opt &= ~SR_POLLIN;
7755 mutex_exit(&stp->sd_lock);
7756 pollwakeup(&stp->sd_pollist, pollwakeups);
7757 mutex_enter(&stp->sd_lock);
7759 no_pollwake:
7761 if (stp->sd_sigflags & signals)
7762 strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7764 mutex_exit(&stp->sd_lock);
7766 rvp->r_val1 = more;
7767 return (error);
7768 #undef _LASTMARK
7772 * Put a message downstream.
7774 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7777 strputmsg(
7778 struct vnode *vp,
7779 struct strbuf *mctl,
7780 struct strbuf *mdata,
7781 unsigned char pri,
7782 int flag,
7783 int fmode)
7785 struct stdata *stp;
7786 queue_t *wqp;
7787 mblk_t *mp;
7788 ssize_t msgsize;
7789 ssize_t rmin, rmax;
7790 int error;
7791 struct uio uios;
7792 struct uio *uiop = &uios;
7793 struct iovec iovs;
7794 int xpg4 = 0;
7796 ASSERT(vp->v_stream);
7797 stp = vp->v_stream;
7798 wqp = stp->sd_wrq;
7801 * If it is an XPG4 application, we need to send
7802 * SIGPIPE below
7805 xpg4 = (flag & MSG_XPG4) ? 1 : 0;
7806 flag &= ~MSG_XPG4;
7808 mutex_enter(&stp->sd_lock);
7810 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7811 mutex_exit(&stp->sd_lock);
7812 return (error);
7815 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7816 error = strwriteable(stp, B_FALSE, xpg4);
7817 if (error != 0) {
7818 mutex_exit(&stp->sd_lock);
7819 return (error);
7823 mutex_exit(&stp->sd_lock);
7826 * Check for legal flag value.
7828 switch (flag) {
7829 case MSG_HIPRI:
7830 if ((mctl->len < 0) || (pri != 0))
7831 return (EINVAL);
7832 break;
7833 case MSG_BAND:
7834 break;
7836 default:
7837 return (EINVAL);
7840 TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_IN,
7841 "strputmsg in:stp %p", stp);
7843 /* get these values from those cached in the stream head */
7844 rmin = stp->sd_qn_minpsz;
7845 rmax = stp->sd_qn_maxpsz;
7848 * Make sure ctl and data sizes together fall within the
7849 * limits of the max and min receive packet sizes and do
7850 * not exceed system limit.
7852 ASSERT((rmax >= 0) || (rmax == INFPSZ));
7853 if (rmax == 0) {
7854 return (ERANGE);
7857 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
7858 * Needed to prevent partial failures in the strmakedata loop.
7860 if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
7861 rmax = stp->sd_maxblk;
7863 if ((msgsize = mdata->len) < 0) {
7864 msgsize = 0;
7865 rmin = 0; /* no range check for NULL data part */
7867 if ((msgsize < rmin) ||
7868 ((msgsize > rmax) && (rmax != INFPSZ)) ||
7869 (mctl->len > strctlsz)) {
7870 return (ERANGE);
7874 * Setup uio and iov for data part
7876 iovs.iov_base = mdata->buf;
7877 iovs.iov_len = msgsize;
7878 uios.uio_iov = &iovs;
7879 uios.uio_iovcnt = 1;
7880 uios.uio_loffset = 0;
7881 uios.uio_segflg = UIO_USERSPACE;
7882 uios.uio_fmode = fmode;
7883 uios.uio_extflg = UIO_COPY_DEFAULT;
7884 uios.uio_resid = msgsize;
7885 uios.uio_offset = 0;
7887 /* Ignore flow control in strput for HIPRI */
7888 if (flag & MSG_HIPRI)
7889 flag |= MSG_IGNFLOW;
7891 for (;;) {
7892 int done = 0;
7895 * strput will always free the ctl mblk - even when strput
7896 * fails.
7898 if ((error = strmakectl(mctl, flag, fmode, &mp)) != 0) {
7899 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7900 "strputmsg out:stp %p out %d error %d",
7901 stp, 1, error);
7902 return (error);
7905 * Verify that the whole message can be transferred by
7906 * strput.
7908 ASSERT(stp->sd_maxblk == INFPSZ ||
7909 stp->sd_maxblk >= mdata->len);
7911 msgsize = mdata->len;
7912 error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
7913 mdata->len = msgsize;
7915 if (error == 0)
7916 break;
7918 if (error != EWOULDBLOCK)
7919 goto out;
7921 mutex_enter(&stp->sd_lock);
7923 * Check for a missed wakeup.
7924 * Needed since strput did not hold sd_lock across
7925 * the canputnext.
7927 if (bcanputnext(wqp, pri)) {
7928 /* Try again */
7929 mutex_exit(&stp->sd_lock);
7930 continue;
7932 TRACE_2(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAIT,
7933 "strputmsg wait:stp %p waits pri %d", stp, pri);
7934 if (((error = strwaitq(stp, WRITEWAIT, (ssize_t)0, fmode, -1,
7935 &done)) != 0) || done) {
7936 mutex_exit(&stp->sd_lock);
7937 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7938 "strputmsg out:q %p out %d error %d",
7939 stp, 0, error);
7940 return (error);
7942 TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAKE,
7943 "strputmsg wake:stp %p wakes", stp);
7944 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7945 mutex_exit(&stp->sd_lock);
7946 return (error);
7948 mutex_exit(&stp->sd_lock);
7950 out:
7952 * For historic reasons, applications expect EAGAIN
7953 * when data mblk could not be allocated. so change
7954 * ENOMEM back to EAGAIN
7956 if (error == ENOMEM)
7957 error = EAGAIN;
7958 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7959 "strputmsg out:stp %p out %d error %d", stp, 2, error);
7960 return (error);
7964 * Put a message downstream.
7965 * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
7966 * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
7967 * and the fmode parameter.
7969 * This routine handles the consolidation private flags:
7970 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7971 * MSG_HOLDSIG Hold signals while waiting for data.
7972 * MSG_IGNFLOW Don't check streams flow control.
7974 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7977 kstrputmsg(
7978 struct vnode *vp,
7979 mblk_t *mctl,
7980 struct uio *uiop,
7981 ssize_t msgsize,
7982 unsigned char pri,
7983 int flag,
7984 int fmode)
7986 struct stdata *stp;
7987 queue_t *wqp;
7988 ssize_t rmin, rmax;
7989 int error;
7991 ASSERT(vp->v_stream);
7992 stp = vp->v_stream;
7993 wqp = stp->sd_wrq;
7994 if (mctl == NULL)
7995 return (EINVAL);
7997 mutex_enter(&stp->sd_lock);
7999 if ((error = i_straccess(stp, JCWRITE)) != 0) {
8000 mutex_exit(&stp->sd_lock);
8001 freemsg(mctl);
8002 return (error);
8005 if ((stp->sd_flag & STPLEX) || !(flag & MSG_IGNERROR)) {
8006 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
8007 error = strwriteable(stp, B_FALSE, B_TRUE);
8008 if (error != 0) {
8009 mutex_exit(&stp->sd_lock);
8010 freemsg(mctl);
8011 return (error);
8016 mutex_exit(&stp->sd_lock);
8019 * Check for legal flag value.
8021 switch (flag & (MSG_HIPRI|MSG_BAND|MSG_ANY)) {
8022 case MSG_HIPRI:
8023 if (pri != 0) {
8024 freemsg(mctl);
8025 return (EINVAL);
8027 break;
8028 case MSG_BAND:
8029 break;
8030 default:
8031 freemsg(mctl);
8032 return (EINVAL);
8035 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_IN,
8036 "kstrputmsg in:stp %p", stp);
8038 /* get these values from those cached in the stream head */
8039 rmin = stp->sd_qn_minpsz;
8040 rmax = stp->sd_qn_maxpsz;
8043 * Make sure ctl and data sizes together fall within the
8044 * limits of the max and min receive packet sizes and do
8045 * not exceed system limit.
8047 ASSERT((rmax >= 0) || (rmax == INFPSZ));
8048 if (rmax == 0) {
8049 freemsg(mctl);
8050 return (ERANGE);
8053 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
8054 * Needed to prevent partial failures in the strmakedata loop.
8056 if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
8057 rmax = stp->sd_maxblk;
8059 if (uiop == NULL) {
8060 msgsize = -1;
8061 rmin = -1; /* no range check for NULL data part */
8062 } else {
8063 /* Use uio flags as well as the fmode parameter flags */
8064 fmode |= uiop->uio_fmode;
8066 if ((msgsize < rmin) ||
8067 ((msgsize > rmax) && (rmax != INFPSZ))) {
8068 freemsg(mctl);
8069 return (ERANGE);
8073 /* Ignore flow control in strput for HIPRI */
8074 if (flag & MSG_HIPRI)
8075 flag |= MSG_IGNFLOW;
8077 for (;;) {
8078 int done = 0;
8079 int waitflag;
8080 mblk_t *mp;
8083 * strput will always free the ctl mblk - even when strput
8084 * fails. If MSG_IGNFLOW is set then any error returned
8085 * will cause us to break the loop, so we don't need a copy
8086 * of the message. If MSG_IGNFLOW is not set, then we can
8087 * get hit by flow control and be forced to try again. In
8088 * this case we need to have a copy of the message. We
8089 * do this using copymsg since the message may get modified
8090 * by something below us.
8092 * We've observed that many TPI providers do not check db_ref
8093 * on the control messages but blindly reuse them for the
8094 * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
8095 * friendly to such providers than using dupmsg. Also, note
8096 * that sockfs uses MSG_IGNFLOW for all TPI control messages.
8097 * Only data messages are subject to flow control, hence
8098 * subject to this copymsg.
8100 if (flag & MSG_IGNFLOW) {
8101 mp = mctl;
8102 mctl = NULL;
8103 } else {
8104 do {
8106 * If a message has a free pointer, the message
8107 * must be dupmsg to maintain this pointer.
8108 * Code using this facility must be sure
8109 * that modules below will not change the
8110 * contents of the dblk without checking db_ref
8111 * first. If db_ref is > 1, then the module
8112 * needs to do a copymsg first. Otherwise,
8113 * the contents of the dblk may become
8114 * inconsistent because the freesmg/freeb below
8115 * may end up calling atomic_add_32_nv.
8116 * The atomic_add_32_nv in freeb (accessing
8117 * all of db_ref, db_type, db_flags, and
8118 * db_struioflag) does not prevent other threads
8119 * from concurrently trying to modify e.g.
8120 * db_type.
8122 if (mctl->b_datap->db_frtnp != NULL)
8123 mp = dupmsg(mctl);
8124 else
8125 mp = copymsg(mctl);
8127 if (mp != NULL)
8128 break;
8130 error = strwaitbuf(msgdsize(mctl), BPRI_MED);
8131 if (error) {
8132 freemsg(mctl);
8133 return (error);
8135 } while (mp == NULL);
8138 * Verify that all of msgsize can be transferred by
8139 * strput.
8141 ASSERT(stp->sd_maxblk == INFPSZ || stp->sd_maxblk >= msgsize);
8142 error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
8143 if (error == 0)
8144 break;
8146 if (error != EWOULDBLOCK)
8147 goto out;
8150 * IF MSG_IGNFLOW is set we should have broken out of loop
8151 * above.
8153 ASSERT(!(flag & MSG_IGNFLOW));
8154 mutex_enter(&stp->sd_lock);
8156 * Check for a missed wakeup.
8157 * Needed since strput did not hold sd_lock across
8158 * the canputnext.
8160 if (bcanputnext(wqp, pri)) {
8161 /* Try again */
8162 mutex_exit(&stp->sd_lock);
8163 continue;
8165 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAIT,
8166 "kstrputmsg wait:stp %p waits pri %d", stp, pri);
8168 waitflag = WRITEWAIT;
8169 if (flag & (MSG_HOLDSIG|MSG_IGNERROR)) {
8170 if (flag & MSG_HOLDSIG)
8171 waitflag |= STR_NOSIG;
8172 if (flag & MSG_IGNERROR)
8173 waitflag |= STR_NOERROR;
8175 if (((error = strwaitq(stp, waitflag,
8176 (ssize_t)0, fmode, -1, &done)) != 0) || done) {
8177 mutex_exit(&stp->sd_lock);
8178 TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8179 "kstrputmsg out:stp %p out %d error %d",
8180 stp, 0, error);
8181 freemsg(mctl);
8182 return (error);
8184 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAKE,
8185 "kstrputmsg wake:stp %p wakes", stp);
8186 if ((error = i_straccess(stp, JCWRITE)) != 0) {
8187 mutex_exit(&stp->sd_lock);
8188 freemsg(mctl);
8189 return (error);
8191 mutex_exit(&stp->sd_lock);
8193 out:
8194 freemsg(mctl);
8196 * For historic reasons, applications expect EAGAIN
8197 * when data mblk could not be allocated. so change
8198 * ENOMEM back to EAGAIN
8200 if (error == ENOMEM)
8201 error = EAGAIN;
8202 TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8203 "kstrputmsg out:stp %p out %d error %d", stp, 2, error);
8204 return (error);
8208 * Determines whether the necessary conditions are set on a stream
8209 * for it to be readable, writeable, or have exceptions.
8211 * strpoll handles the consolidation private events:
8212 * POLLNOERR Do not return POLLERR even if there are stream
8213 * head errors.
8214 * Used by sockfs.
8215 * POLLRDDATA Do not return POLLIN unless at least one message on
8216 * the queue contains one or more M_DATA mblks. Thus
8217 * when this flag is set a queue with only
8218 * M_PROTO/M_PCPROTO mblks does not return POLLIN.
8219 * Used by sockfs to ignore T_EXDATA_IND messages.
8221 * Note: POLLRDDATA assumes that synch streams only return messages with
8222 * an M_DATA attached (i.e. not messages consisting of only
8223 * an M_PROTO/M_PCPROTO part).
8226 strpoll(struct stdata *stp, short events_arg, int anyyet, short *reventsp,
8227 struct pollhead **phpp)
8229 int events = (ushort_t)events_arg;
8230 int retevents = 0;
8231 mblk_t *mp;
8232 qband_t *qbp;
8233 long sd_flags = stp->sd_flag;
8234 int headlocked = 0;
8237 * For performance, a single 'if' tests for most possible edge
8238 * conditions in one shot
8240 if (sd_flags & (STPLEX | STRDERR | STWRERR)) {
8241 if (sd_flags & STPLEX) {
8242 *reventsp = POLLNVAL;
8243 return (EINVAL);
8245 if (((events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) &&
8246 (sd_flags & STRDERR)) ||
8247 ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) &&
8248 (sd_flags & STWRERR))) {
8249 if (!(events & POLLNOERR)) {
8250 *reventsp = POLLERR;
8251 return (0);
8255 if (sd_flags & STRHUP) {
8256 retevents |= POLLHUP;
8257 } else if (events & (POLLWRNORM | POLLWRBAND)) {
8258 queue_t *tq;
8259 queue_t *qp = stp->sd_wrq;
8261 claimstr(qp);
8262 /* Find next module forward that has a service procedure */
8263 tq = qp->q_next->q_nfsrv;
8264 ASSERT(tq != NULL);
8266 if (polllock(&stp->sd_pollist, QLOCK(tq)) != 0) {
8267 releasestr(qp);
8268 *reventsp = POLLNVAL;
8269 return (0);
8271 if (events & POLLWRNORM) {
8272 queue_t *sqp;
8274 if (tq->q_flag & QFULL)
8275 /* ensure backq svc procedure runs */
8276 tq->q_flag |= QWANTW;
8277 else if ((sqp = stp->sd_struiowrq) != NULL) {
8278 /* Check sync stream barrier write q */
8279 mutex_exit(QLOCK(tq));
8280 if (polllock(&stp->sd_pollist,
8281 QLOCK(sqp)) != 0) {
8282 releasestr(qp);
8283 *reventsp = POLLNVAL;
8284 return (0);
8286 if (sqp->q_flag & QFULL)
8287 /* ensure pollwakeup() is done */
8288 sqp->q_flag |= QWANTWSYNC;
8289 else
8290 retevents |= POLLOUT;
8291 /* More write events to process ??? */
8292 if (! (events & POLLWRBAND)) {
8293 mutex_exit(QLOCK(sqp));
8294 releasestr(qp);
8295 goto chkrd;
8297 mutex_exit(QLOCK(sqp));
8298 if (polllock(&stp->sd_pollist,
8299 QLOCK(tq)) != 0) {
8300 releasestr(qp);
8301 *reventsp = POLLNVAL;
8302 return (0);
8304 } else
8305 retevents |= POLLOUT;
8307 if (events & POLLWRBAND) {
8308 qbp = tq->q_bandp;
8309 if (qbp) {
8310 while (qbp) {
8311 if (qbp->qb_flag & QB_FULL)
8312 qbp->qb_flag |= QB_WANTW;
8313 else
8314 retevents |= POLLWRBAND;
8315 qbp = qbp->qb_next;
8317 } else {
8318 retevents |= POLLWRBAND;
8321 mutex_exit(QLOCK(tq));
8322 releasestr(qp);
8324 chkrd:
8325 if (sd_flags & STRPRI) {
8326 retevents |= (events & POLLPRI);
8327 } else if (events & (POLLRDNORM | POLLRDBAND | POLLIN)) {
8328 queue_t *qp = _RD(stp->sd_wrq);
8329 int normevents = (events & (POLLIN | POLLRDNORM));
8332 * Note: Need to do polllock() here since ps_lock may be
8333 * held. See bug 4191544.
8335 if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8336 *reventsp = POLLNVAL;
8337 return (0);
8339 headlocked = 1;
8340 mp = qp->q_first;
8341 while (mp) {
8343 * For POLLRDDATA we scan b_cont and b_next until we
8344 * find an M_DATA.
8346 if ((events & POLLRDDATA) &&
8347 mp->b_datap->db_type != M_DATA) {
8348 mblk_t *nmp = mp->b_cont;
8350 while (nmp != NULL &&
8351 nmp->b_datap->db_type != M_DATA)
8352 nmp = nmp->b_cont;
8353 if (nmp == NULL) {
8354 mp = mp->b_next;
8355 continue;
8358 if (mp->b_band == 0)
8359 retevents |= normevents;
8360 else
8361 retevents |= (events & (POLLIN | POLLRDBAND));
8362 break;
8364 if (!(retevents & normevents) && (stp->sd_wakeq & RSLEEP)) {
8366 * Sync stream barrier read queue has data.
8368 retevents |= normevents;
8370 /* Treat eof as normal data */
8371 if (sd_flags & STREOF)
8372 retevents |= normevents;
8376 * Pass back a pollhead if no events are pending or if edge-triggering
8377 * has been configured on this resource.
8379 if ((retevents == 0 && !anyyet) || (events & POLLET)) {
8380 *phpp = &stp->sd_pollist;
8381 if (headlocked == 0) {
8382 if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8383 *reventsp = POLLNVAL;
8384 return (0);
8386 headlocked = 1;
8388 stp->sd_rput_opt |= SR_POLLIN;
8391 *reventsp = (short)retevents;
8392 if (headlocked)
8393 mutex_exit(&stp->sd_lock);
8394 return (0);
8398 * The purpose of putback() is to assure sleeping polls/reads
8399 * are awakened when there are no new messages arriving at the,
8400 * stream head, and a message is placed back on the read queue.
8402 * sd_lock must be held when messages are placed back on stream
8403 * head. (getq() holds sd_lock when it removes messages from
8404 * the queue)
8407 static void
8408 putback(struct stdata *stp, queue_t *q, mblk_t *bp, int band)
8410 mblk_t *qfirst;
8411 ASSERT(MUTEX_HELD(&stp->sd_lock));
8414 * As a result of lock-step ordering around q_lock and sd_lock,
8415 * it's possible for function calls like putnext() and
8416 * canputnext() to get an inaccurate picture of how much
8417 * data is really being processed at the stream head.
8418 * We only consolidate with existing messages on the queue
8419 * if the length of the message we want to put back is smaller
8420 * than the queue hiwater mark.
8422 if ((stp->sd_rput_opt & SR_CONSOL_DATA) &&
8423 (DB_TYPE(bp) == M_DATA) && ((qfirst = q->q_first) != NULL) &&
8424 (DB_TYPE(qfirst) == M_DATA) &&
8425 ((qfirst->b_flag & (MSGMARK|MSGDELIM)) == 0) &&
8426 ((bp->b_flag & (MSGMARK|MSGDELIM|MSGMARKNEXT)) == 0) &&
8427 (mp_cont_len(bp, NULL) < q->q_hiwat)) {
8429 * We use the same logic as defined in strrput()
8430 * but in reverse as we are putting back onto the
8431 * queue and want to retain byte ordering.
8432 * Consolidate M_DATA messages with M_DATA ONLY.
8433 * strrput() allows the consolidation of M_DATA onto
8434 * M_PROTO | M_PCPROTO but not the other way round.
8436 * The consolidation does not take place if the message
8437 * we are returning to the queue is marked with either
8438 * of the marks or the delim flag or if q_first
8439 * is marked with MSGMARK. The MSGMARK check is needed to
8440 * handle the odd semantics of MSGMARK where essentially
8441 * the whole message is to be treated as marked.
8442 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from q_first
8443 * to the front of the b_cont chain.
8445 rmvq_noenab(q, qfirst);
8448 * The first message in the b_cont list
8449 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
8450 * We need to handle the case where we
8451 * are appending:
8453 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
8454 * 2) a MSGMARKNEXT to a plain message.
8455 * 3) a MSGNOTMARKNEXT to a plain message
8456 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
8457 * message.
8459 * Thus we never append a MSGMARKNEXT or
8460 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
8462 if (qfirst->b_flag & MSGMARKNEXT) {
8463 bp->b_flag |= MSGMARKNEXT;
8464 bp->b_flag &= ~MSGNOTMARKNEXT;
8465 qfirst->b_flag &= ~MSGMARKNEXT;
8466 } else if (qfirst->b_flag & MSGNOTMARKNEXT) {
8467 bp->b_flag |= MSGNOTMARKNEXT;
8468 qfirst->b_flag &= ~MSGNOTMARKNEXT;
8471 linkb(bp, qfirst);
8473 (void) putbq(q, bp);
8476 * A message may have come in when the sd_lock was dropped in the
8477 * calling routine. If this is the case and STR*ATMARK info was
8478 * received, need to move that from the stream head to the q_last
8479 * so that SIOCATMARK can return the proper value.
8481 if (stp->sd_flag & (STRATMARK | STRNOTATMARK)) {
8482 unsigned short *flagp = &q->q_last->b_flag;
8483 uint_t b_flag = (uint_t)*flagp;
8485 if (stp->sd_flag & STRATMARK) {
8486 b_flag &= ~MSGNOTMARKNEXT;
8487 b_flag |= MSGMARKNEXT;
8488 stp->sd_flag &= ~STRATMARK;
8489 } else {
8490 b_flag &= ~MSGMARKNEXT;
8491 b_flag |= MSGNOTMARKNEXT;
8492 stp->sd_flag &= ~STRNOTATMARK;
8494 *flagp = (unsigned short) b_flag;
8497 #ifdef DEBUG
8499 * Make sure that the flags are not messed up.
8502 mblk_t *mp;
8503 mp = q->q_last;
8504 while (mp != NULL) {
8505 ASSERT((mp->b_flag & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
8506 (MSGMARKNEXT|MSGNOTMARKNEXT));
8507 mp = mp->b_cont;
8510 #endif
8511 if (q->q_first == bp) {
8512 short pollevents;
8514 if (stp->sd_flag & RSLEEP) {
8515 stp->sd_flag &= ~RSLEEP;
8516 cv_broadcast(&q->q_wait);
8518 if (stp->sd_flag & STRPRI) {
8519 pollevents = POLLPRI;
8520 } else {
8521 if (band == 0) {
8522 if (!(stp->sd_rput_opt & SR_POLLIN))
8523 return;
8524 stp->sd_rput_opt &= ~SR_POLLIN;
8525 pollevents = POLLIN | POLLRDNORM;
8526 } else {
8527 pollevents = POLLIN | POLLRDBAND;
8530 mutex_exit(&stp->sd_lock);
8531 pollwakeup(&stp->sd_pollist, pollevents);
8532 mutex_enter(&stp->sd_lock);
8537 * Return the held vnode attached to the stream head of a
8538 * given queue
8539 * It is the responsibility of the calling routine to ensure
8540 * that the queue does not go away (e.g. pop).
8542 vnode_t *
8543 strq2vp(queue_t *qp)
8545 vnode_t *vp;
8546 vp = STREAM(qp)->sd_vnode;
8547 ASSERT(vp != NULL);
8548 VN_HOLD(vp);
8549 return (vp);
8553 * return the stream head write queue for the given vp
8554 * It is the responsibility of the calling routine to ensure
8555 * that the stream or vnode do not close.
8557 queue_t *
8558 strvp2wq(vnode_t *vp)
8560 ASSERT(vp->v_stream != NULL);
8561 return (vp->v_stream->sd_wrq);
8565 * pollwakeup stream head
8566 * It is the responsibility of the calling routine to ensure
8567 * that the stream or vnode do not close.
8569 void
8570 strpollwakeup(vnode_t *vp, short event)
8572 ASSERT(vp->v_stream);
8573 pollwakeup(&vp->v_stream->sd_pollist, event);
8577 * Mate the stream heads of two vnodes together. If the two vnodes are the
8578 * same, we just make the write-side point at the read-side -- otherwise,
8579 * we do a full mate. Only works on vnodes associated with streams that are
8580 * still being built and thus have only a stream head.
8582 void
8583 strmate(vnode_t *vp1, vnode_t *vp2)
8585 queue_t *wrq1 = strvp2wq(vp1);
8586 queue_t *wrq2 = strvp2wq(vp2);
8589 * Verify that there are no modules on the stream yet. We also
8590 * rely on the stream head always having a service procedure to
8591 * avoid tweaking q_nfsrv.
8593 ASSERT(wrq1->q_next == NULL && wrq2->q_next == NULL);
8594 ASSERT(wrq1->q_qinfo->qi_srvp != NULL);
8595 ASSERT(wrq2->q_qinfo->qi_srvp != NULL);
8598 * If the queues are the same, just twist; otherwise do a full mate.
8600 if (wrq1 == wrq2) {
8601 wrq1->q_next = _RD(wrq1);
8602 } else {
8603 wrq1->q_next = _RD(wrq2);
8604 wrq2->q_next = _RD(wrq1);
8605 STREAM(wrq1)->sd_mate = STREAM(wrq2);
8606 STREAM(wrq1)->sd_flag |= STRMATE;
8607 STREAM(wrq2)->sd_mate = STREAM(wrq1);
8608 STREAM(wrq2)->sd_flag |= STRMATE;
8613 * XXX will go away when console is correctly fixed.
8614 * Clean up the console PIDS, from previous I_SETSIG,
8615 * called only for cnopen which never calls strclean().
8617 void
8618 str_cn_clean(struct vnode *vp)
8620 strsig_t *ssp, *pssp, *tssp;
8621 struct stdata *stp;
8622 struct pid *pidp;
8623 int update = 0;
8625 ASSERT(vp->v_stream);
8626 stp = vp->v_stream;
8627 pssp = NULL;
8628 mutex_enter(&stp->sd_lock);
8629 ssp = stp->sd_siglist;
8630 while (ssp) {
8631 mutex_enter(&pidlock);
8632 pidp = ssp->ss_pidp;
8634 * Get rid of PID if the proc is gone.
8636 if (pidp->pid_prinactive) {
8637 tssp = ssp->ss_next;
8638 if (pssp)
8639 pssp->ss_next = tssp;
8640 else
8641 stp->sd_siglist = tssp;
8642 ASSERT(pidp->pid_ref <= 1);
8643 PID_RELE(ssp->ss_pidp);
8644 mutex_exit(&pidlock);
8645 kmem_free(ssp, sizeof (strsig_t));
8646 update = 1;
8647 ssp = tssp;
8648 continue;
8649 } else
8650 mutex_exit(&pidlock);
8651 pssp = ssp;
8652 ssp = ssp->ss_next;
8654 if (update) {
8655 stp->sd_sigflags = 0;
8656 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
8657 stp->sd_sigflags |= ssp->ss_events;
8659 mutex_exit(&stp->sd_lock);
8663 * Return B_TRUE if there is data in the message, B_FALSE otherwise.
8665 static boolean_t
8666 msghasdata(mblk_t *bp)
8668 for (; bp; bp = bp->b_cont)
8669 if (bp->b_datap->db_type == M_DATA) {
8670 ASSERT(bp->b_wptr >= bp->b_rptr);
8671 if (bp->b_wptr > bp->b_rptr)
8672 return (B_TRUE);
8674 return (B_FALSE);