Merge commit 'ea01a15a654b9e1c7b37d958f4d1911882ed7781'
[unleashed.git] / kernel / os / streamio.c
blob758706907bced43b15e49cd79b81793d3d9ff66c
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>
82 #include <c2/audit.h>
85 * This define helps improve the readability of streams code while
86 * still maintaining a very old streams performance enhancement. The
87 * performance enhancement basically involved having all callers
88 * of straccess() perform the first check that straccess() will do
89 * locally before actually calling straccess(). (There by reducing
90 * the number of unnecessary calls to straccess().)
92 #define i_straccess(x, y) ((stp->sd_sidp == NULL) ? 0 : \
93 (stp->sd_vnode->v_type == VFIFO) ? 0 : \
94 straccess((x), (y)))
97 * what is mblk_pull_len?
99 * If a streams message consists of many short messages,
100 * a performance degradation occurs from copyout overhead.
101 * To decrease the per mblk overhead, messages that are
102 * likely to consist of many small mblks are pulled up into
103 * one continuous chunk of memory.
105 * To avoid the processing overhead of examining every
106 * mblk, a quick heuristic is used. If the first mblk in
107 * the message is shorter than mblk_pull_len, it is likely
108 * that the rest of the mblk will be short.
110 * This heuristic was decided upon after performance tests
111 * indicated that anything more complex slowed down the main
112 * code path.
114 #define MBLK_PULL_LEN 64
115 uint32_t mblk_pull_len = MBLK_PULL_LEN;
118 * The sgttyb_handling flag controls the handling of the old BSD
119 * TIOCGETP, TIOCSETP, and TIOCSETN ioctls as follows:
121 * 0 - Emit no warnings at all and retain old, broken behavior.
122 * 1 - Emit no warnings and silently handle new semantics.
123 * 2 - Send cmn_err(CE_NOTE) when either TIOCSETP or TIOCSETN is used
124 * (once per system invocation). Handle with new semantics.
125 * 3 - Send SIGSYS when any TIOCGETP, TIOCSETP, or TIOCSETN call is
126 * made (so that offenders drop core and are easy to debug).
128 * The "new semantics" are that TIOCGETP returns B38400 for
129 * sg_[io]speed if the corresponding value is over B38400, and that
130 * TIOCSET[PN] accept B38400 in these cases to mean "retain current
131 * bit rate."
133 int sgttyb_handling = 1;
134 static boolean_t sgttyb_complaint;
136 /* don't push drcompat module by default on Style-2 streams */
137 static int push_drcompat = 0;
140 * id value used to distinguish between different ioctl messages
142 static uint32_t ioc_id;
144 static void putback(struct stdata *, queue_t *, mblk_t *, int);
145 static void strcleanall(struct vnode *);
146 static int strwsrv(queue_t *);
147 static int strdocmd(struct stdata *, struct strcmd *, cred_t *);
150 * qinit and module_info structures for stream head read and write queues
152 struct module_info strm_info = { 0, "strrhead", 0, INFPSZ, STRHIGH, STRLOW };
153 struct module_info stwm_info = { 0, "strwhead", 0, 0, 0, 0 };
154 struct qinit strdata = { strrput, NULL, NULL, NULL, NULL, &strm_info };
155 struct qinit stwdata = { NULL, strwsrv, NULL, NULL, NULL, &stwm_info };
156 struct module_info fiform_info = { 0, "fifostrrhead", 0, PIPE_BUF, FIFOHIWAT,
157 FIFOLOWAT };
158 struct module_info fifowm_info = { 0, "fifostrwhead", 0, 0, 0, 0 };
159 struct qinit fifo_strdata = { strrput, NULL, NULL, NULL, NULL, &fiform_info };
160 struct qinit fifo_stwdata = { NULL, strwsrv, NULL, NULL, NULL, &fifowm_info };
162 extern kmutex_t strresources; /* protects global resources */
163 extern kmutex_t muxifier; /* single-threads multiplexor creation */
165 static boolean_t msghasdata(mblk_t *bp);
166 #define msgnodata(bp) (!msghasdata(bp))
169 * Stream head locking notes:
170 * There are four monitors associated with the stream head:
171 * 1. v_stream monitor: in stropen() and strclose() v_lock
172 * is held while the association of vnode and stream
173 * head is established or tested for.
174 * 2. open/close/push/pop monitor: sd_lock is held while each
175 * thread bids for exclusive access to this monitor
176 * for opening or closing a stream. In addition, this
177 * monitor is entered during pushes and pops. This
178 * guarantees that during plumbing operations there
179 * is only one thread trying to change the plumbing.
180 * Any other threads present in the stream are only
181 * using the plumbing.
182 * 3. read/write monitor: in the case of read, a thread holds
183 * sd_lock while trying to get data from the stream
184 * head queue. if there is none to fulfill a read
185 * request, it sets RSLEEP and calls cv_wait_sig() down
186 * in strwaitq() to await the arrival of new data.
187 * when new data arrives in strrput(), sd_lock is acquired
188 * before testing for RSLEEP and calling cv_broadcast().
189 * the behavior of strwrite(), strwsrv(), and WSLEEP
190 * mirror this.
191 * 4. ioctl monitor: sd_lock is gotten to ensure that only one
192 * thread is doing an ioctl at a time.
195 static int
196 push_mod(queue_t *qp, dev_t *devp, struct stdata *stp, const char *name,
197 int anchor, cred_t *crp, uint_t anchor_zoneid)
199 int error;
200 fmodsw_impl_t *fp;
202 if (stp->sd_flag & (STRHUP|STRDERR|STWRERR)) {
203 error = (stp->sd_flag & STRHUP) ? ENXIO : EIO;
204 return (error);
206 if (stp->sd_pushcnt >= nstrpush) {
207 return (EINVAL);
210 if ((fp = fmodsw_find(name, FMODSW_HOLD | FMODSW_LOAD)) == NULL) {
211 stp->sd_flag |= STREOPENFAIL;
212 return (EINVAL);
216 * push new module and call its open routine via qattach
218 if ((error = qattach(qp, devp, 0, crp, fp, B_FALSE)) != 0)
219 return (error);
222 * Check to see if caller wants a STREAMS anchor
223 * put at this place in the stream, and add if so.
225 mutex_enter(&stp->sd_lock);
226 if (anchor == stp->sd_pushcnt) {
227 stp->sd_anchor = stp->sd_pushcnt;
228 stp->sd_anchorzone = anchor_zoneid;
230 mutex_exit(&stp->sd_lock);
232 return (0);
236 * Open a stream device.
239 stropen(vnode_t *vp, dev_t *devp, int flag, cred_t *crp)
241 struct stdata *stp;
242 queue_t *qp;
243 int s;
244 dev_t dummydev, savedev;
245 struct autopush *ap;
246 struct dlautopush dlap;
247 int error = 0;
248 ssize_t rmin, rmax;
249 int cloneopen;
250 queue_t *brq;
251 major_t major;
252 str_stack_t *ss;
253 zoneid_t zoneid;
254 uint_t anchor;
257 * If the stream already exists, wait for any open in progress
258 * to complete, then call the open function of each module and
259 * driver in the stream. Otherwise create the stream.
261 TRACE_1(TR_FAC_STREAMS_FR, TR_STROPEN, "stropen:%p", vp);
262 retry:
263 mutex_enter(&vp->v_lock);
264 if ((stp = vp->v_stream) != NULL) {
267 * Waiting for stream to be created to device
268 * due to another open.
270 mutex_exit(&vp->v_lock);
272 if (STRMATED(stp)) {
273 struct stdata *strmatep = stp->sd_mate;
275 STRLOCKMATES(stp);
276 if (strmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
277 if (flag & (FNDELAY|FNONBLOCK)) {
278 error = EAGAIN;
279 mutex_exit(&strmatep->sd_lock);
280 goto ckreturn;
282 mutex_exit(&stp->sd_lock);
283 if (!cv_wait_sig(&strmatep->sd_monitor,
284 &strmatep->sd_lock)) {
285 error = EINTR;
286 mutex_exit(&strmatep->sd_lock);
287 mutex_enter(&stp->sd_lock);
288 goto ckreturn;
290 mutex_exit(&strmatep->sd_lock);
291 goto retry;
293 if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
294 if (flag & (FNDELAY|FNONBLOCK)) {
295 error = EAGAIN;
296 mutex_exit(&strmatep->sd_lock);
297 goto ckreturn;
299 mutex_exit(&strmatep->sd_lock);
300 if (!cv_wait_sig(&stp->sd_monitor,
301 &stp->sd_lock)) {
302 error = EINTR;
303 goto ckreturn;
305 mutex_exit(&stp->sd_lock);
306 goto retry;
309 if (stp->sd_flag & (STRDERR|STWRERR)) {
310 error = EIO;
311 mutex_exit(&strmatep->sd_lock);
312 goto ckreturn;
315 stp->sd_flag |= STWOPEN;
316 STRUNLOCKMATES(stp);
317 } else {
318 mutex_enter(&stp->sd_lock);
319 if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
320 if (flag & (FNDELAY|FNONBLOCK)) {
321 error = EAGAIN;
322 goto ckreturn;
324 if (!cv_wait_sig(&stp->sd_monitor,
325 &stp->sd_lock)) {
326 error = EINTR;
327 goto ckreturn;
329 mutex_exit(&stp->sd_lock);
330 goto retry; /* could be clone! */
333 if (stp->sd_flag & (STRDERR|STWRERR)) {
334 error = EIO;
335 goto ckreturn;
338 stp->sd_flag |= STWOPEN;
339 mutex_exit(&stp->sd_lock);
343 * Open all modules and devices down stream to notify
344 * that another user is streaming. For modules, set the
345 * last argument to MODOPEN and do not pass any open flags.
346 * Ignore dummydev since this is not the first open.
348 claimstr(stp->sd_wrq);
349 qp = stp->sd_wrq;
350 while (_SAMESTR(qp)) {
351 qp = qp->q_next;
352 if ((error = qreopen(_RD(qp), devp, flag, crp)) != 0)
353 break;
355 releasestr(stp->sd_wrq);
356 mutex_enter(&stp->sd_lock);
357 stp->sd_flag &= ~(STRHUP|STWOPEN|STRDERR|STWRERR);
358 stp->sd_rerror = 0;
359 stp->sd_werror = 0;
360 ckreturn:
361 cv_broadcast(&stp->sd_monitor);
362 mutex_exit(&stp->sd_lock);
363 return (error);
367 * This vnode isn't streaming. SPECFS already
368 * checked for multiple vnodes pointing to the
369 * same stream, so create a stream to the driver.
371 qp = allocq();
372 stp = shalloc(qp);
375 * Initialize stream head. shalloc() has given us
376 * exclusive access, and we have the vnode locked;
377 * we can do whatever we want with stp.
379 stp->sd_flag = STWOPEN;
380 stp->sd_siglist = NULL;
381 stp->sd_pollist.ph_list = NULL;
382 stp->sd_sigflags = 0;
383 stp->sd_mark = NULL;
384 stp->sd_closetime = STRTIMOUT;
385 stp->sd_sidp = NULL;
386 stp->sd_pgidp = NULL;
387 stp->sd_vnode = vp;
388 stp->sd_rerror = 0;
389 stp->sd_werror = 0;
390 stp->sd_wroff = 0;
391 stp->sd_tail = 0;
392 stp->sd_iocblk = NULL;
393 stp->sd_cmdblk = NULL;
394 stp->sd_pushcnt = 0;
395 stp->sd_qn_minpsz = 0;
396 stp->sd_qn_maxpsz = INFPSZ - 1; /* used to check for initialization */
397 stp->sd_maxblk = INFPSZ;
398 qp->q_ptr = _WR(qp)->q_ptr = stp;
399 STREAM(qp) = STREAM(_WR(qp)) = stp;
400 vp->v_stream = stp;
401 mutex_exit(&vp->v_lock);
402 if (vp->v_type == VFIFO) {
403 stp->sd_flag |= OLDNDELAY;
405 * This means, both for pipes and fifos
406 * strwrite will send SIGPIPE if the other
407 * end is closed. For putmsg it depends
408 * on whether it is a XPG4_2 application
409 * or not
411 stp->sd_wput_opt = SW_SIGPIPE;
413 /* setq might sleep in kmem_alloc - avoid holding locks. */
414 setq(qp, &fifo_strdata, &fifo_stwdata, NULL, QMTSAFE,
415 SQ_CI|SQ_CO, B_FALSE);
417 set_qend(qp);
418 stp->sd_strtab = fifo_getinfo();
419 _WR(qp)->q_nfsrv = _WR(qp);
420 qp->q_nfsrv = qp;
422 * Wake up others that are waiting for stream to be created.
424 mutex_enter(&stp->sd_lock);
426 * nothing is be pushed on stream yet, so
427 * optimized stream head packetsizes are just that
428 * of the read queue
430 stp->sd_qn_minpsz = qp->q_minpsz;
431 stp->sd_qn_maxpsz = qp->q_maxpsz;
432 stp->sd_flag &= ~STWOPEN;
433 goto fifo_opendone;
435 /* setq might sleep in kmem_alloc - avoid holding locks. */
436 setq(qp, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_FALSE);
438 set_qend(qp);
441 * Open driver and create stream to it (via qattach).
443 savedev = *devp;
444 cloneopen = (getmajor(*devp) == clone_major);
445 if ((error = qattach(qp, devp, flag, crp, NULL, B_FALSE)) != 0) {
446 mutex_enter(&vp->v_lock);
447 vp->v_stream = NULL;
448 mutex_exit(&vp->v_lock);
449 mutex_enter(&stp->sd_lock);
450 cv_broadcast(&stp->sd_monitor);
451 mutex_exit(&stp->sd_lock);
452 freeq(_RD(qp));
453 shfree(stp);
454 return (error);
457 * Set sd_strtab after open in order to handle clonable drivers
459 stp->sd_strtab = STREAMSTAB(getmajor(*devp));
462 * Historical note: dummydev used to be be prior to the initial
463 * open (via qattach above), which made the value seen
464 * inconsistent between an I_PUSH and an autopush of a module.
466 dummydev = *devp;
469 * For clone open of old style (Q not associated) network driver,
470 * push DRMODNAME module to handle DL_ATTACH/DL_DETACH
472 brq = _RD(_WR(qp)->q_next);
473 major = getmajor(*devp);
474 if (push_drcompat && cloneopen && NETWORK_DRV(major) &&
475 ((brq->q_flag & _QASSOCIATED) == 0)) {
476 if (push_mod(qp, &dummydev, stp, DRMODNAME, 0, crp, 0) != 0)
477 cmn_err(CE_WARN, "cannot push " DRMODNAME
478 " streams module");
481 if (!NETWORK_DRV(major)) {
482 savedev = *devp;
483 } else {
485 * For network devices, process differently based on the
486 * return value from dld_autopush():
488 * 0: the passed-in device points to a GLDv3 datalink with
489 * per-link autopush configuration; use that configuration
490 * and ignore any per-driver autopush configuration.
492 * 1: the passed-in device points to a physical GLDv3
493 * datalink without per-link autopush configuration. The
494 * passed in device was changed to refer to the actual
495 * physical device (if it's not already); we use that new
496 * device to look up any per-driver autopush configuration.
498 * -1: neither of the above cases applied; use the initial
499 * device to look up any per-driver autopush configuration.
501 switch (dld_autopush(&savedev, &dlap)) {
502 case 0:
503 zoneid = crgetzoneid(crp);
504 for (s = 0; s < dlap.dap_npush; s++) {
505 error = push_mod(qp, &dummydev, stp,
506 dlap.dap_aplist[s], dlap.dap_anchor, crp,
507 zoneid);
508 if (error != 0)
509 break;
511 goto opendone;
512 case 1:
513 break;
514 case -1:
515 savedev = *devp;
516 break;
520 * Find the autopush configuration based on "savedev". Start with the
521 * global zone. If not found check in the local zone.
523 zoneid = GLOBAL_ZONEID;
524 retryap:
525 ss = netstack_find_by_stackid(zoneid_to_netstackid(zoneid))->
526 netstack_str;
527 if ((ap = sad_ap_find_by_dev(savedev, ss)) == NULL) {
528 netstack_rele(ss->ss_netstack);
529 if (zoneid == GLOBAL_ZONEID) {
531 * None found. Also look in the zone's autopush table.
533 zoneid = crgetzoneid(crp);
534 if (zoneid != GLOBAL_ZONEID)
535 goto retryap;
537 goto opendone;
539 anchor = ap->ap_anchor;
540 zoneid = crgetzoneid(crp);
541 for (s = 0; s < ap->ap_npush; s++) {
542 error = push_mod(qp, &dummydev, stp, ap->ap_list[s],
543 anchor, crp, zoneid);
544 if (error != 0)
545 break;
547 sad_ap_rele(ap, ss);
548 netstack_rele(ss->ss_netstack);
550 opendone:
553 * let specfs know that open failed part way through
555 if (error) {
556 mutex_enter(&stp->sd_lock);
557 stp->sd_flag |= STREOPENFAIL;
558 mutex_exit(&stp->sd_lock);
562 * Wake up others that are waiting for stream to be created.
564 mutex_enter(&stp->sd_lock);
565 stp->sd_flag &= ~STWOPEN;
568 * As a performance concern we are caching the values of
569 * q_minpsz and q_maxpsz of the module below the stream
570 * head in the stream head.
572 mutex_enter(QLOCK(stp->sd_wrq->q_next));
573 rmin = stp->sd_wrq->q_next->q_minpsz;
574 rmax = stp->sd_wrq->q_next->q_maxpsz;
575 mutex_exit(QLOCK(stp->sd_wrq->q_next));
577 /* do this processing here as a performance concern */
578 if (strmsgsz != 0) {
579 if (rmax == INFPSZ)
580 rmax = strmsgsz;
581 else
582 rmax = MIN(strmsgsz, rmax);
585 mutex_enter(QLOCK(stp->sd_wrq));
586 stp->sd_qn_minpsz = rmin;
587 stp->sd_qn_maxpsz = rmax;
588 mutex_exit(QLOCK(stp->sd_wrq));
590 fifo_opendone:
591 cv_broadcast(&stp->sd_monitor);
592 mutex_exit(&stp->sd_lock);
593 return (error);
596 static int strsink(queue_t *, mblk_t *);
597 static struct qinit deadrend = {
598 strsink, NULL, NULL, NULL, NULL, &strm_info, NULL
600 static struct qinit deadwend = {
601 NULL, NULL, NULL, NULL, NULL, &stwm_info, NULL
605 * Close a stream.
606 * This is called from closef() on the last close of an open stream.
607 * Strclean() will already have removed the siglist and pollist
608 * information, so all that remains is to remove all multiplexor links
609 * for the stream, pop all the modules (and the driver), and free the
610 * stream structure.
614 strclose(struct vnode *vp, int flag, cred_t *crp)
616 struct stdata *stp;
617 queue_t *qp;
618 int rval;
619 int freestp = 1;
620 queue_t *rmq;
622 TRACE_1(TR_FAC_STREAMS_FR,
623 TR_STRCLOSE, "strclose:%p", vp);
624 ASSERT(vp->v_stream);
626 stp = vp->v_stream;
627 ASSERT(!(stp->sd_flag & STPLEX));
628 qp = stp->sd_wrq;
631 * Needed so that strpoll will return non-zero for this fd.
632 * Note that with POLLNOERR STRHUP does still cause POLLHUP.
634 mutex_enter(&stp->sd_lock);
635 stp->sd_flag |= STRHUP;
636 mutex_exit(&stp->sd_lock);
639 * If the registered process or process group did not have an
640 * open instance of this stream then strclean would not be
641 * called. Thus at the time of closing all remaining siglist entries
642 * are removed.
644 if (stp->sd_siglist != NULL)
645 strcleanall(vp);
647 ASSERT(stp->sd_siglist == NULL);
648 ASSERT(stp->sd_sigflags == 0);
650 if (STRMATED(stp)) {
651 struct stdata *strmatep = stp->sd_mate;
652 int waited = 1;
654 STRLOCKMATES(stp);
655 while (waited) {
656 waited = 0;
657 while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
658 mutex_exit(&strmatep->sd_lock);
659 cv_wait(&stp->sd_monitor, &stp->sd_lock);
660 mutex_exit(&stp->sd_lock);
661 STRLOCKMATES(stp);
662 waited = 1;
664 while (strmatep->sd_flag &
665 (STWOPEN|STRCLOSE|STRPLUMB)) {
666 mutex_exit(&stp->sd_lock);
667 cv_wait(&strmatep->sd_monitor,
668 &strmatep->sd_lock);
669 mutex_exit(&strmatep->sd_lock);
670 STRLOCKMATES(stp);
671 waited = 1;
674 stp->sd_flag |= STRCLOSE;
675 STRUNLOCKMATES(stp);
676 } else {
677 mutex_enter(&stp->sd_lock);
678 stp->sd_flag |= STRCLOSE;
679 mutex_exit(&stp->sd_lock);
682 ASSERT(qp->q_first == NULL); /* No more delayed write */
684 /* Check if an I_LINK was ever done on this stream */
685 if (stp->sd_flag & STRHASLINKS) {
686 netstack_t *ns;
687 str_stack_t *ss;
689 ns = netstack_find_by_cred(crp);
690 ASSERT(ns != NULL);
691 ss = ns->netstack_str;
692 ASSERT(ss != NULL);
694 (void) munlinkall(stp, LINKCLOSE|LINKNORMAL, crp, &rval, ss);
695 netstack_rele(ss->ss_netstack);
698 while (_SAMESTR(qp)) {
700 * Holding sd_lock prevents q_next from changing in
701 * this stream.
703 mutex_enter(&stp->sd_lock);
704 if (!(flag & (FNDELAY|FNONBLOCK)) && (stp->sd_closetime > 0)) {
707 * sleep until awakened by strwsrv() or timeout
709 for (;;) {
710 mutex_enter(QLOCK(qp->q_next));
711 if (!(qp->q_next->q_mblkcnt)) {
712 mutex_exit(QLOCK(qp->q_next));
713 break;
715 stp->sd_flag |= WSLEEP;
717 /* ensure strwsrv gets enabled */
718 qp->q_next->q_flag |= QWANTW;
719 mutex_exit(QLOCK(qp->q_next));
720 /* get out if we timed out or recv'd a signal */
721 if (str_cv_wait(&qp->q_wait, &stp->sd_lock,
722 stp->sd_closetime, 0) <= 0) {
723 break;
726 stp->sd_flag &= ~WSLEEP;
728 mutex_exit(&stp->sd_lock);
730 rmq = qp->q_next;
731 if (rmq->q_flag & QISDRV) {
732 ASSERT(!_SAMESTR(rmq));
733 wait_sq_svc(_RD(qp)->q_syncq);
736 qdetach(_RD(rmq), 1, flag, crp, B_FALSE);
740 * Since we call pollwakeup in close() now, the poll list should
741 * be empty in most cases. The only exception is the layered devices
742 * (e.g. the console drivers with redirection modules pushed on top
743 * of it). We have to do this after calling qdetach() because
744 * the redirection module won't have torn down the console
745 * redirection until after qdetach() has been invoked.
747 if (stp->sd_pollist.ph_list != NULL) {
748 pollwakeup(&stp->sd_pollist, POLLERR);
749 pollhead_clean(&stp->sd_pollist);
751 ASSERT(stp->sd_pollist.ph_list == NULL);
752 ASSERT(stp->sd_sidp == NULL);
753 ASSERT(stp->sd_pgidp == NULL);
755 /* Prevent qenable from re-enabling the stream head queue */
756 disable_svc(_RD(qp));
759 * Wait until service procedure of each queue is
760 * run, if QINSERVICE is set.
762 wait_svc(_RD(qp));
765 * Now, flush both queues.
767 flushq(_RD(qp), FLUSHALL);
768 flushq(qp, FLUSHALL);
771 * If the write queue of the stream head is pointing to a
772 * read queue, we have a twisted stream. If the read queue
773 * is alive, convert the stream head queues into a dead end.
774 * If the read queue is dead, free the dead pair.
776 if (qp->q_next && !_SAMESTR(qp)) {
777 if (qp->q_next->q_qinfo == &deadrend) { /* half-closed pipe */
778 flushq(qp->q_next, FLUSHALL); /* ensure no message */
779 shfree(qp->q_next->q_stream);
780 freeq(qp->q_next);
781 freeq(_RD(qp));
782 } else if (qp->q_next == _RD(qp)) { /* fifo */
783 freeq(_RD(qp));
784 } else { /* pipe */
785 freestp = 0;
787 * The q_info pointers are never accessed when
788 * SQLOCK is held.
790 ASSERT(qp->q_syncq == _RD(qp)->q_syncq);
791 mutex_enter(SQLOCK(qp->q_syncq));
792 qp->q_qinfo = &deadwend;
793 _RD(qp)->q_qinfo = &deadrend;
794 mutex_exit(SQLOCK(qp->q_syncq));
796 } else {
797 freeq(_RD(qp)); /* free stream head queue pair */
800 mutex_enter(&vp->v_lock);
801 if (stp->sd_iocblk) {
802 if (stp->sd_iocblk != (mblk_t *)-1) {
803 freemsg(stp->sd_iocblk);
805 stp->sd_iocblk = NULL;
807 stp->sd_vnode = NULL;
808 vp->v_stream = NULL;
809 mutex_exit(&vp->v_lock);
810 mutex_enter(&stp->sd_lock);
811 freemsg(stp->sd_cmdblk);
812 stp->sd_cmdblk = NULL;
813 stp->sd_flag &= ~STRCLOSE;
814 cv_broadcast(&stp->sd_monitor);
815 mutex_exit(&stp->sd_lock);
817 if (freestp)
818 shfree(stp);
819 return (0);
822 static int
823 strsink(queue_t *q, mblk_t *bp)
825 struct copyresp *resp;
827 switch (bp->b_datap->db_type) {
828 case M_FLUSH:
829 if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
830 *bp->b_rptr &= ~FLUSHR;
831 bp->b_flag |= MSGNOLOOP;
833 * Protect against the driver passing up
834 * messages after it has done a qprocsoff.
836 if (_OTHERQ(q)->q_next == NULL)
837 freemsg(bp);
838 else
839 qreply(q, bp);
840 } else {
841 freemsg(bp);
843 break;
845 case M_COPYIN:
846 case M_COPYOUT:
847 if (bp->b_cont) {
848 freemsg(bp->b_cont);
849 bp->b_cont = NULL;
851 bp->b_datap->db_type = M_IOCDATA;
852 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
853 resp = (struct copyresp *)bp->b_rptr;
854 resp->cp_rval = (caddr_t)1; /* failure */
856 * Protect against the driver passing up
857 * messages after it has done a qprocsoff.
859 if (_OTHERQ(q)->q_next == NULL)
860 freemsg(bp);
861 else
862 qreply(q, bp);
863 break;
865 case M_IOCTL:
866 if (bp->b_cont) {
867 freemsg(bp->b_cont);
868 bp->b_cont = NULL;
870 bp->b_datap->db_type = M_IOCNAK;
872 * Protect against the driver passing up
873 * messages after it has done a qprocsoff.
875 if (_OTHERQ(q)->q_next == NULL)
876 freemsg(bp);
877 else
878 qreply(q, bp);
879 break;
881 default:
882 freemsg(bp);
883 break;
886 return (0);
890 * Clean up after a process when it closes a stream. This is called
891 * from closef for all closes, whereas strclose is called only for the
892 * last close on a stream. The siglist is scanned for entries for the
893 * current process, and these are removed.
895 void
896 strclean(struct vnode *vp)
898 strsig_t *ssp, *pssp, *tssp;
899 stdata_t *stp;
900 int update = 0;
902 TRACE_1(TR_FAC_STREAMS_FR,
903 TR_STRCLEAN, "strclean:%p", vp);
904 stp = vp->v_stream;
905 pssp = NULL;
906 mutex_enter(&stp->sd_lock);
907 ssp = stp->sd_siglist;
908 while (ssp) {
909 if (ssp->ss_pidp == curproc->p_pidp) {
910 tssp = ssp->ss_next;
911 if (pssp)
912 pssp->ss_next = tssp;
913 else
914 stp->sd_siglist = tssp;
915 mutex_enter(&pidlock);
916 PID_RELE(ssp->ss_pidp);
917 mutex_exit(&pidlock);
918 kmem_free(ssp, sizeof (strsig_t));
919 update = 1;
920 ssp = tssp;
921 } else {
922 pssp = ssp;
923 ssp = ssp->ss_next;
926 if (update) {
927 stp->sd_sigflags = 0;
928 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
929 stp->sd_sigflags |= ssp->ss_events;
931 mutex_exit(&stp->sd_lock);
935 * Used on the last close to remove any remaining items on the siglist.
936 * These could be present on the siglist due to I_ESETSIG calls that
937 * use process groups or processed that do not have an open file descriptor
938 * for this stream (Such entries would not be removed by strclean).
940 static void
941 strcleanall(struct vnode *vp)
943 strsig_t *ssp, *nssp;
944 stdata_t *stp;
946 stp = vp->v_stream;
947 mutex_enter(&stp->sd_lock);
948 ssp = stp->sd_siglist;
949 stp->sd_siglist = NULL;
950 while (ssp) {
951 nssp = ssp->ss_next;
952 mutex_enter(&pidlock);
953 PID_RELE(ssp->ss_pidp);
954 mutex_exit(&pidlock);
955 kmem_free(ssp, sizeof (strsig_t));
956 ssp = nssp;
958 stp->sd_sigflags = 0;
959 mutex_exit(&stp->sd_lock);
963 * Retrieve the next message from the logical stream head read queue
964 * using either rwnext (if sync stream) or getq_noenab.
965 * It is the callers responsibility to call qbackenable after
966 * it is finished with the message. The caller should not call
967 * qbackenable until after any putback calls to avoid spurious backenabling.
969 mblk_t *
970 strget(struct stdata *stp, queue_t *q, struct uio *uiop, int first,
971 int *errorp)
973 mblk_t *bp;
974 int error;
975 ssize_t rbytes = 0;
977 /* Holding sd_lock prevents the read queue from changing */
978 ASSERT(MUTEX_HELD(&stp->sd_lock));
980 if (uiop != NULL && stp->sd_struiordq != NULL &&
981 q->q_first == NULL &&
982 (!first || (stp->sd_wakeq & RSLEEP))) {
984 * Stream supports rwnext() for the read side.
985 * If this is the first time we're called by e.g. strread
986 * only do the downcall if there is a deferred wakeup
987 * (registered in sd_wakeq).
989 struiod_t uiod;
990 struct iovec buf[IOV_MAX_STACK];
991 int iovlen = 0;
993 if (first)
994 stp->sd_wakeq &= ~RSLEEP;
996 if (uiop->uio_iovcnt > IOV_MAX_STACK) {
997 iovlen = uiop->uio_iovcnt * sizeof (iovec_t);
998 uiod.d_iov = kmem_alloc(iovlen, KM_SLEEP);
999 } else {
1000 uiod.d_iov = buf;
1003 (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov, uiop->uio_iovcnt);
1004 uiod.d_mp = 0;
1006 * Mark that a thread is in rwnext on the read side
1007 * to prevent strrput from nacking ioctls immediately.
1008 * When the last concurrent rwnext returns
1009 * the ioctls are nack'ed.
1011 ASSERT(MUTEX_HELD(&stp->sd_lock));
1012 stp->sd_struiodnak++;
1014 * Note: rwnext will drop sd_lock.
1016 error = rwnext(q, &uiod);
1017 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
1018 mutex_enter(&stp->sd_lock);
1019 stp->sd_struiodnak--;
1020 while (stp->sd_struiodnak == 0 &&
1021 ((bp = stp->sd_struionak) != NULL)) {
1022 stp->sd_struionak = bp->b_next;
1023 bp->b_next = NULL;
1024 bp->b_datap->db_type = M_IOCNAK;
1026 * Protect against the driver passing up
1027 * messages after it has done a qprocsoff.
1029 if (_OTHERQ(q)->q_next == NULL)
1030 freemsg(bp);
1031 else {
1032 mutex_exit(&stp->sd_lock);
1033 qreply(q, bp);
1034 mutex_enter(&stp->sd_lock);
1037 ASSERT(MUTEX_HELD(&stp->sd_lock));
1038 if (error == 0 || error == EWOULDBLOCK) {
1039 if ((bp = uiod.d_mp) != NULL) {
1040 *errorp = 0;
1041 ASSERT(MUTEX_HELD(&stp->sd_lock));
1042 if (iovlen != 0)
1043 kmem_free(uiod.d_iov, iovlen);
1044 return (bp);
1046 error = 0;
1047 } else if (error == EINVAL) {
1049 * The stream plumbing must have
1050 * changed while we were away, so
1051 * just turn off rwnext()s.
1053 error = 0;
1054 } else if (error == EBUSY) {
1056 * The module might have data in transit using putnext
1057 * Fall back on waiting + getq.
1059 error = 0;
1060 } else {
1061 *errorp = error;
1062 ASSERT(MUTEX_HELD(&stp->sd_lock));
1063 if (iovlen != 0)
1064 kmem_free(uiod.d_iov, iovlen);
1065 return (NULL);
1068 if (iovlen != 0)
1069 kmem_free(uiod.d_iov, iovlen);
1072 * Try a getq in case a rwnext() generated mblk
1073 * has bubbled up via strrput().
1076 *errorp = 0;
1077 ASSERT(MUTEX_HELD(&stp->sd_lock));
1080 * If we have a valid uio, try and use this as a guide for how
1081 * many bytes to retrieve from the queue via getq_noenab().
1082 * Doing this can avoid unneccesary counting of overlong
1083 * messages in putback(). We currently only do this for sockets
1084 * and only if there is no sd_rputdatafunc hook.
1086 * The sd_rputdatafunc hook transforms the entire message
1087 * before any bytes in it can be given to a client. So, rbytes
1088 * must be 0 if there is a hook.
1090 if ((uiop != NULL) && (stp->sd_vnode->v_type == VSOCK) &&
1091 (stp->sd_rputdatafunc == NULL))
1092 rbytes = uiop->uio_resid;
1094 return (getq_noenab(q, rbytes));
1098 * Copy out the message pointed to by `bp' into the uio pointed to by `uiop'.
1099 * If the message does not fit in the uio the remainder of it is returned;
1100 * otherwise NULL is returned. Any embedded zero-length mblk_t's are
1101 * consumed, even if uio_resid reaches zero. On error, `*errorp' is set to
1102 * the error code, the message is consumed, and NULL is returned.
1104 static mblk_t *
1105 struiocopyout(mblk_t *bp, struct uio *uiop, int *errorp)
1107 int error;
1108 ptrdiff_t n;
1109 mblk_t *nbp;
1111 ASSERT(bp->b_wptr >= bp->b_rptr);
1113 do {
1114 if ((n = MIN(uiop->uio_resid, MBLKL(bp))) != 0) {
1115 ASSERT(n > 0);
1117 error = uiomove(bp->b_rptr, n, UIO_READ, uiop);
1118 if (error != 0) {
1119 freemsg(bp);
1120 *errorp = error;
1121 return (NULL);
1125 bp->b_rptr += n;
1126 while (bp != NULL && (bp->b_rptr >= bp->b_wptr)) {
1127 nbp = bp;
1128 bp = bp->b_cont;
1129 freeb(nbp);
1131 } while (bp != NULL && uiop->uio_resid > 0);
1133 *errorp = 0;
1134 return (bp);
1138 * Read a stream according to the mode flags in sd_flag:
1140 * (default mode) - Byte stream, msg boundaries are ignored
1141 * RD_MSGDIS (msg discard) - Read on msg boundaries and throw away
1142 * any data remaining in msg
1143 * RD_MSGNODIS (msg non-discard) - Read on msg boundaries and put back
1144 * any remaining data on head of read queue
1146 * Consume readable messages on the front of the queue until
1147 * ttolwp(curthread)->lwp_count
1148 * is satisfied, the readable messages are exhausted, or a message
1149 * boundary is reached in a message mode. If no data was read and
1150 * the stream was not opened with the NDELAY flag, block until data arrives.
1151 * Otherwise return the data read and update the count.
1153 * In default mode a 0 length message signifies end-of-file and terminates
1154 * a read in progress. The 0 length message is removed from the queue
1155 * only if it is the only message read (no data is read).
1157 * An attempt to read an M_PROTO or M_PCPROTO message results in an
1158 * EBADMSG error return, unless either RD_PROTDAT or RD_PROTDIS are set.
1159 * If RD_PROTDAT is set, M_PROTO and M_PCPROTO messages are read as data.
1160 * If RD_PROTDIS is set, the M_PROTO and M_PCPROTO parts of the message
1161 * are unlinked from and M_DATA blocks in the message, the protos are
1162 * thrown away, and the data is read.
1164 /* ARGSUSED */
1166 strread(struct vnode *vp, struct uio *uiop, cred_t *crp)
1168 struct stdata *stp;
1169 mblk_t *bp, *nbp;
1170 queue_t *q;
1171 int error = 0;
1172 uint_t old_sd_flag;
1173 int first;
1174 char rflg;
1175 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
1176 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
1177 short delim;
1178 unsigned char pri = 0;
1179 char waitflag;
1180 unsigned char type;
1182 TRACE_1(TR_FAC_STREAMS_FR,
1183 TR_STRREAD_ENTER, "strread:%p", vp);
1184 ASSERT(vp->v_stream);
1185 stp = vp->v_stream;
1187 mutex_enter(&stp->sd_lock);
1189 if ((error = i_straccess(stp, JCREAD)) != 0) {
1190 mutex_exit(&stp->sd_lock);
1191 return (error);
1194 if (stp->sd_flag & (STRDERR|STPLEX)) {
1195 error = strgeterr(stp, STRDERR|STPLEX, 0);
1196 if (error != 0) {
1197 mutex_exit(&stp->sd_lock);
1198 return (error);
1203 * Loop terminates when uiop->uio_resid == 0.
1205 rflg = 0;
1206 waitflag = READWAIT;
1207 q = _RD(stp->sd_wrq);
1208 for (;;) {
1209 ASSERT(MUTEX_HELD(&stp->sd_lock));
1210 old_sd_flag = stp->sd_flag;
1211 mark = 0;
1212 delim = 0;
1213 first = 1;
1214 while ((bp = strget(stp, q, uiop, first, &error)) == NULL) {
1215 int done = 0;
1217 ASSERT(MUTEX_HELD(&stp->sd_lock));
1219 if (error != 0)
1220 goto oops;
1222 if (stp->sd_flag & (STRHUP|STREOF)) {
1223 goto oops;
1225 if (rflg && !(stp->sd_flag & STRDELIM)) {
1226 goto oops;
1229 * If a read(fd,buf,0) has been done, there is no
1230 * need to sleep. We always have zero bytes to
1231 * return.
1233 if (uiop->uio_resid == 0) {
1234 goto oops;
1237 qbackenable(q, 0);
1239 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_WAIT,
1240 "strread calls strwaitq:%p, %p, %p",
1241 vp, uiop, crp);
1242 if ((error = strwaitq(stp, waitflag, uiop->uio_resid,
1243 uiop->uio_fmode, -1, &done)) != 0 || done) {
1244 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_DONE,
1245 "strread error or done:%p, %p, %p",
1246 vp, uiop, crp);
1247 if ((uiop->uio_fmode & FNDELAY) &&
1248 (stp->sd_flag & OLDNDELAY) &&
1249 (error == EAGAIN))
1250 error = 0;
1251 goto oops;
1253 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_AWAKE,
1254 "strread awakes:%p, %p, %p", vp, uiop, crp);
1255 if ((error = i_straccess(stp, JCREAD)) != 0) {
1256 goto oops;
1258 first = 0;
1261 ASSERT(MUTEX_HELD(&stp->sd_lock));
1262 ASSERT(bp);
1263 pri = bp->b_band;
1265 * Extract any mark information. If the message is not
1266 * completely consumed this information will be put in the mblk
1267 * that is putback.
1268 * If MSGMARKNEXT is set and the message is completely consumed
1269 * the STRATMARK flag will be set below. Likewise, if
1270 * MSGNOTMARKNEXT is set and the message is
1271 * completely consumed STRNOTATMARK will be set.
1273 * For some unknown reason strread only breaks the read at the
1274 * last mark.
1276 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
1277 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
1278 (MSGMARKNEXT|MSGNOTMARKNEXT));
1279 if (mark != 0 && bp == stp->sd_mark) {
1280 if (rflg) {
1281 putback(stp, q, bp, pri);
1282 goto oops;
1284 mark |= _LASTMARK;
1285 stp->sd_mark = NULL;
1287 if ((stp->sd_flag & STRDELIM) && (bp->b_flag & MSGDELIM))
1288 delim = 1;
1289 mutex_exit(&stp->sd_lock);
1291 if (STREAM_NEEDSERVICE(stp))
1292 stream_runservice(stp);
1294 type = bp->b_datap->db_type;
1296 switch (type) {
1298 case M_DATA:
1299 ismdata:
1300 if (msgnodata(bp)) {
1301 if (mark || delim) {
1302 freemsg(bp);
1303 } else if (rflg) {
1306 * If already read data put zero
1307 * length message back on queue else
1308 * free msg and return 0.
1310 bp->b_band = pri;
1311 mutex_enter(&stp->sd_lock);
1312 putback(stp, q, bp, pri);
1313 mutex_exit(&stp->sd_lock);
1314 } else {
1315 freemsg(bp);
1317 error = 0;
1318 goto oops1;
1321 rflg = 1;
1322 waitflag |= NOINTR;
1323 bp = struiocopyout(bp, uiop, &error);
1324 if (error != 0)
1325 goto oops1;
1327 mutex_enter(&stp->sd_lock);
1328 if (bp) {
1330 * Have remaining data in message.
1331 * Free msg if in discard mode.
1333 if (stp->sd_read_opt & RD_MSGDIS) {
1334 freemsg(bp);
1335 } else {
1336 bp->b_band = pri;
1337 if ((mark & _LASTMARK) &&
1338 (stp->sd_mark == NULL))
1339 stp->sd_mark = bp;
1340 bp->b_flag |= mark & ~_LASTMARK;
1341 if (delim)
1342 bp->b_flag |= MSGDELIM;
1343 if (msgnodata(bp))
1344 freemsg(bp);
1345 else
1346 putback(stp, q, bp, pri);
1348 } else {
1350 * Consumed the complete message.
1351 * Move the MSG*MARKNEXT information
1352 * to the stream head just in case
1353 * the read queue becomes empty.
1355 * If the stream head was at the mark
1356 * (STRATMARK) before we dropped sd_lock above
1357 * and some data was consumed then we have
1358 * moved past the mark thus STRATMARK is
1359 * cleared. However, if a message arrived in
1360 * strrput during the copyout above causing
1361 * STRATMARK to be set we can not clear that
1362 * flag.
1364 if (mark &
1365 (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
1366 if (mark & MSGMARKNEXT) {
1367 stp->sd_flag &= ~STRNOTATMARK;
1368 stp->sd_flag |= STRATMARK;
1369 } else if (mark & MSGNOTMARKNEXT) {
1370 stp->sd_flag &= ~STRATMARK;
1371 stp->sd_flag |= STRNOTATMARK;
1372 } else {
1373 stp->sd_flag &=
1374 ~(STRATMARK|STRNOTATMARK);
1376 } else if (rflg && (old_sd_flag & STRATMARK)) {
1377 stp->sd_flag &= ~STRATMARK;
1382 * Check for signal messages at the front of the read
1383 * queue and generate the signal(s) if appropriate.
1384 * The only signal that can be on queue is M_SIG at
1385 * this point.
1387 while ((((bp = q->q_first)) != NULL) &&
1388 (bp->b_datap->db_type == M_SIG)) {
1389 bp = getq_noenab(q, 0);
1391 * sd_lock is held so the content of the
1392 * read queue can not change.
1394 ASSERT(bp != NULL && DB_TYPE(bp) == M_SIG);
1395 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
1396 mutex_exit(&stp->sd_lock);
1397 freemsg(bp);
1398 if (STREAM_NEEDSERVICE(stp))
1399 stream_runservice(stp);
1400 mutex_enter(&stp->sd_lock);
1403 if ((uiop->uio_resid == 0) || (mark & _LASTMARK) ||
1404 delim ||
1405 (stp->sd_read_opt & (RD_MSGDIS|RD_MSGNODIS))) {
1406 goto oops;
1408 continue;
1410 case M_SIG:
1411 strsignal(stp, *bp->b_rptr, (int32_t)bp->b_band);
1412 freemsg(bp);
1413 mutex_enter(&stp->sd_lock);
1414 continue;
1416 case M_PROTO:
1417 case M_PCPROTO:
1419 * Only data messages are readable.
1420 * Any others generate an error, unless
1421 * RD_PROTDIS or RD_PROTDAT is set.
1423 if (stp->sd_read_opt & RD_PROTDAT) {
1424 for (nbp = bp; nbp; nbp = nbp->b_next) {
1425 if ((nbp->b_datap->db_type ==
1426 M_PROTO) ||
1427 (nbp->b_datap->db_type ==
1428 M_PCPROTO)) {
1429 nbp->b_datap->db_type = M_DATA;
1430 } else {
1431 break;
1435 * clear stream head hi pri flag based on
1436 * first message
1438 if (type == M_PCPROTO) {
1439 mutex_enter(&stp->sd_lock);
1440 stp->sd_flag &= ~STRPRI;
1441 mutex_exit(&stp->sd_lock);
1443 goto ismdata;
1444 } else if (stp->sd_read_opt & RD_PROTDIS) {
1446 * discard non-data messages
1448 while (bp &&
1449 ((bp->b_datap->db_type == M_PROTO) ||
1450 (bp->b_datap->db_type == M_PCPROTO))) {
1451 nbp = unlinkb(bp);
1452 freeb(bp);
1453 bp = nbp;
1456 * clear stream head hi pri flag based on
1457 * first message
1459 if (type == M_PCPROTO) {
1460 mutex_enter(&stp->sd_lock);
1461 stp->sd_flag &= ~STRPRI;
1462 mutex_exit(&stp->sd_lock);
1464 if (bp) {
1465 bp->b_band = pri;
1466 goto ismdata;
1467 } else {
1468 break;
1471 /* FALLTHRU */
1472 case M_PASSFP:
1473 if ((bp->b_datap->db_type == M_PASSFP) &&
1474 (stp->sd_read_opt & RD_PROTDIS)) {
1475 freemsg(bp);
1476 break;
1478 mutex_enter(&stp->sd_lock);
1479 putback(stp, q, bp, pri);
1480 mutex_exit(&stp->sd_lock);
1481 if (rflg == 0)
1482 error = EBADMSG;
1483 goto oops1;
1485 default:
1487 * Garbage on stream head read queue.
1489 cmn_err(CE_WARN, "bad %x found at stream head\n",
1490 bp->b_datap->db_type);
1491 freemsg(bp);
1492 goto oops1;
1494 mutex_enter(&stp->sd_lock);
1496 oops:
1497 mutex_exit(&stp->sd_lock);
1498 oops1:
1499 qbackenable(q, pri);
1500 return (error);
1501 #undef _LASTMARK
1505 * Default processing of M_PROTO/M_PCPROTO messages.
1506 * Determine which wakeups and signals are needed.
1507 * This can be replaced by a user-specified procedure for kernel users
1508 * of STREAMS.
1510 /* ARGSUSED */
1511 mblk_t *
1512 strrput_proto(vnode_t *vp, mblk_t *mp,
1513 strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1514 strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1516 *wakeups = RSLEEP;
1517 *allmsgsigs = 0;
1519 switch (mp->b_datap->db_type) {
1520 case M_PROTO:
1521 if (mp->b_band == 0) {
1522 *firstmsgsigs = S_INPUT | S_RDNORM;
1523 *pollwakeups = POLLIN | POLLRDNORM;
1524 } else {
1525 *firstmsgsigs = S_INPUT | S_RDBAND;
1526 *pollwakeups = POLLIN | POLLRDBAND;
1528 break;
1529 case M_PCPROTO:
1530 *firstmsgsigs = S_HIPRI;
1531 *pollwakeups = POLLPRI;
1532 break;
1534 return (mp);
1538 * Default processing of everything but M_DATA, M_PROTO, M_PCPROTO and
1539 * M_PASSFP messages.
1540 * Determine which wakeups and signals are needed.
1541 * This can be replaced by a user-specified procedure for kernel users
1542 * of STREAMS.
1544 /* ARGSUSED */
1545 mblk_t *
1546 strrput_misc(vnode_t *vp, mblk_t *mp,
1547 strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1548 strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1550 *wakeups = 0;
1551 *firstmsgsigs = 0;
1552 *allmsgsigs = 0;
1553 *pollwakeups = 0;
1554 return (mp);
1558 * Stream read put procedure. Called from downstream driver/module
1559 * with messages for the stream head. Data, protocol, and in-stream
1560 * signal messages are placed on the queue, others are handled directly.
1563 strrput(queue_t *q, mblk_t *bp)
1565 struct stdata *stp;
1566 ulong_t rput_opt;
1567 strwakeup_t wakeups;
1568 strsigset_t firstmsgsigs; /* Signals if first message on queue */
1569 strsigset_t allmsgsigs; /* Signals for all messages */
1570 strsigset_t signals; /* Signals events to generate */
1571 strpollset_t pollwakeups;
1572 mblk_t *nextbp;
1573 uchar_t band = 0;
1574 int hipri_sig;
1576 stp = (struct stdata *)q->q_ptr;
1578 * Use rput_opt for optimized access to the SR_ flags except
1579 * SR_POLLIN. That flag has to be checked under sd_lock since it
1580 * is modified by strpoll().
1582 rput_opt = stp->sd_rput_opt;
1584 ASSERT(qclaimed(q));
1585 TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_ENTER,
1586 "strrput called with message type:q %p bp %p", q, bp);
1589 * Perform initial processing and pass to the parameterized functions.
1591 ASSERT(bp->b_next == NULL);
1593 switch (bp->b_datap->db_type) {
1594 case M_DATA:
1596 * sockfs is the only consumer of STREOF and when it is set,
1597 * it implies that the receiver is not interested in receiving
1598 * any more data, hence the mblk is freed to prevent unnecessary
1599 * message queueing at the stream head.
1601 if (stp->sd_flag == STREOF) {
1602 freemsg(bp);
1603 return (0);
1605 if ((rput_opt & SR_IGN_ZEROLEN) &&
1606 bp->b_rptr == bp->b_wptr && msgnodata(bp)) {
1608 * Ignore zero-length M_DATA messages. These might be
1609 * generated by some transports.
1610 * The zero-length M_DATA messages, even if they
1611 * are ignored, should effect the atmark tracking and
1612 * should wake up a thread sleeping in strwaitmark.
1614 mutex_enter(&stp->sd_lock);
1615 if (bp->b_flag & MSGMARKNEXT) {
1617 * Record the position of the mark either
1618 * in q_last or in STRATMARK.
1620 if (q->q_last != NULL) {
1621 q->q_last->b_flag &= ~MSGNOTMARKNEXT;
1622 q->q_last->b_flag |= MSGMARKNEXT;
1623 } else {
1624 stp->sd_flag &= ~STRNOTATMARK;
1625 stp->sd_flag |= STRATMARK;
1627 } else if (bp->b_flag & MSGNOTMARKNEXT) {
1629 * Record that this is not the position of
1630 * the mark either in q_last or in
1631 * STRNOTATMARK.
1633 if (q->q_last != NULL) {
1634 q->q_last->b_flag &= ~MSGMARKNEXT;
1635 q->q_last->b_flag |= MSGNOTMARKNEXT;
1636 } else {
1637 stp->sd_flag &= ~STRATMARK;
1638 stp->sd_flag |= STRNOTATMARK;
1641 if (stp->sd_flag & RSLEEP) {
1642 stp->sd_flag &= ~RSLEEP;
1643 cv_broadcast(&q->q_wait);
1645 mutex_exit(&stp->sd_lock);
1646 freemsg(bp);
1647 return (0);
1649 wakeups = RSLEEP;
1650 if (bp->b_band == 0) {
1651 firstmsgsigs = S_INPUT | S_RDNORM;
1652 pollwakeups = POLLIN | POLLRDNORM;
1653 } else {
1654 firstmsgsigs = S_INPUT | S_RDBAND;
1655 pollwakeups = POLLIN | POLLRDBAND;
1657 if (rput_opt & SR_SIGALLDATA)
1658 allmsgsigs = firstmsgsigs;
1659 else
1660 allmsgsigs = 0;
1662 mutex_enter(&stp->sd_lock);
1663 if ((rput_opt & SR_CONSOL_DATA) &&
1664 (q->q_last != NULL) &&
1665 (bp->b_flag & (MSGMARK|MSGDELIM)) == 0) {
1667 * Consolidate an M_DATA message onto an M_DATA,
1668 * M_PROTO, or M_PCPROTO by merging it with q_last.
1669 * The consolidation does not take place if
1670 * the old message is marked with either of the
1671 * marks or the delim flag or if the new
1672 * message is marked with MSGMARK. The MSGMARK
1673 * check is needed to handle the odd semantics of
1674 * MSGMARK where essentially the whole message
1675 * is to be treated as marked.
1676 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from the
1677 * new message to the front of the b_cont chain.
1679 mblk_t *lbp = q->q_last;
1680 unsigned char db_type = lbp->b_datap->db_type;
1682 if ((db_type == M_DATA || db_type == M_PROTO ||
1683 db_type == M_PCPROTO) &&
1684 !(lbp->b_flag & (MSGDELIM|MSGMARK|MSGMARKNEXT))) {
1685 rmvq_noenab(q, lbp);
1687 * The first message in the b_cont list
1688 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
1689 * We need to handle the case where we
1690 * are appending:
1692 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
1693 * 2) a MSGMARKNEXT to a plain message.
1694 * 3) a MSGNOTMARKNEXT to a plain message
1695 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
1696 * message.
1698 * Thus we never append a MSGMARKNEXT or
1699 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
1701 if (bp->b_flag & MSGMARKNEXT) {
1702 lbp->b_flag |= MSGMARKNEXT;
1703 lbp->b_flag &= ~MSGNOTMARKNEXT;
1704 bp->b_flag &= ~MSGMARKNEXT;
1705 } else if (bp->b_flag & MSGNOTMARKNEXT) {
1706 lbp->b_flag |= MSGNOTMARKNEXT;
1707 bp->b_flag &= ~MSGNOTMARKNEXT;
1710 linkb(lbp, bp);
1711 bp = lbp;
1713 * The new message logically isn't the first
1714 * even though the q_first check below thinks
1715 * it is. Clear the firstmsgsigs to make it
1716 * not appear to be first.
1718 firstmsgsigs = 0;
1721 break;
1723 case M_PASSFP:
1724 wakeups = RSLEEP;
1725 allmsgsigs = 0;
1726 if (bp->b_band == 0) {
1727 firstmsgsigs = S_INPUT | S_RDNORM;
1728 pollwakeups = POLLIN | POLLRDNORM;
1729 } else {
1730 firstmsgsigs = S_INPUT | S_RDBAND;
1731 pollwakeups = POLLIN | POLLRDBAND;
1733 mutex_enter(&stp->sd_lock);
1734 break;
1736 case M_PROTO:
1737 case M_PCPROTO:
1738 ASSERT(stp->sd_rprotofunc != NULL);
1739 bp = (stp->sd_rprotofunc)(stp->sd_vnode, bp,
1740 &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1741 #define ALLSIG (S_INPUT|S_HIPRI|S_OUTPUT|S_MSG|S_ERROR|S_HANGUP|S_RDNORM|\
1742 S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)
1743 #define ALLPOLL (POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLWRNORM|POLLRDBAND|\
1744 POLLWRBAND)
1746 ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1747 ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1748 ASSERT((allmsgsigs & ~ALLSIG) == 0);
1749 ASSERT((pollwakeups & ~ALLPOLL) == 0);
1751 mutex_enter(&stp->sd_lock);
1752 break;
1754 default:
1755 ASSERT(stp->sd_rmiscfunc != NULL);
1756 bp = (stp->sd_rmiscfunc)(stp->sd_vnode, bp,
1757 &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1758 ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1759 ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1760 ASSERT((allmsgsigs & ~ALLSIG) == 0);
1761 ASSERT((pollwakeups & ~ALLPOLL) == 0);
1762 #undef ALLSIG
1763 #undef ALLPOLL
1764 mutex_enter(&stp->sd_lock);
1765 break;
1767 ASSERT(MUTEX_HELD(&stp->sd_lock));
1769 /* By default generate superset of signals */
1770 signals = (firstmsgsigs | allmsgsigs);
1773 * The proto and misc functions can return multiple messages
1774 * as a b_next chain. Such messages are processed separately.
1776 one_more:
1777 hipri_sig = 0;
1778 if (bp == NULL) {
1779 nextbp = NULL;
1780 } else {
1781 nextbp = bp->b_next;
1782 bp->b_next = NULL;
1784 switch (bp->b_datap->db_type) {
1785 case M_PCPROTO:
1787 * Only one priority protocol message is allowed at the
1788 * stream head at a time.
1790 if (stp->sd_flag & STRPRI) {
1791 TRACE_0(TR_FAC_STREAMS_FR, TR_STRRPUT_PROTERR,
1792 "M_PCPROTO already at head");
1793 freemsg(bp);
1794 mutex_exit(&stp->sd_lock);
1795 goto done;
1797 stp->sd_flag |= STRPRI;
1798 hipri_sig = 1;
1799 /* FALLTHRU */
1800 case M_DATA:
1801 case M_PROTO:
1802 case M_PASSFP:
1803 band = bp->b_band;
1805 * Marking doesn't work well when messages
1806 * are marked in more than one band. We only
1807 * remember the last message received, even if
1808 * it is placed on the queue ahead of other
1809 * marked messages.
1811 if (bp->b_flag & MSGMARK)
1812 stp->sd_mark = bp;
1813 (void) putq(q, bp);
1816 * If message is a PCPROTO message, always use
1817 * firstmsgsigs to determine if a signal should be
1818 * sent as strrput is the only place to send
1819 * signals for PCPROTO. Other messages are based on
1820 * the STRGETINPROG flag. The flag determines if
1821 * strrput or (k)strgetmsg will be responsible for
1822 * sending the signals, in the firstmsgsigs case.
1824 if ((hipri_sig == 1) ||
1825 (((stp->sd_flag & STRGETINPROG) == 0) &&
1826 (q->q_first == bp)))
1827 signals = (firstmsgsigs | allmsgsigs);
1828 else
1829 signals = allmsgsigs;
1830 break;
1832 default:
1833 mutex_exit(&stp->sd_lock);
1834 (void) strrput_nondata(q, bp);
1835 mutex_enter(&stp->sd_lock);
1836 break;
1839 ASSERT(MUTEX_HELD(&stp->sd_lock));
1841 * Wake sleeping read/getmsg and cancel deferred wakeup
1843 if (wakeups & RSLEEP)
1844 stp->sd_wakeq &= ~RSLEEP;
1846 wakeups &= stp->sd_flag;
1847 if (wakeups & RSLEEP) {
1848 stp->sd_flag &= ~RSLEEP;
1849 cv_broadcast(&q->q_wait);
1851 if (wakeups & WSLEEP) {
1852 stp->sd_flag &= ~WSLEEP;
1853 cv_broadcast(&_WR(q)->q_wait);
1856 if (pollwakeups != 0) {
1857 if (pollwakeups == (POLLIN | POLLRDNORM)) {
1859 * Can't use rput_opt since it was not
1860 * read when sd_lock was held and SR_POLLIN is changed
1861 * by strpoll() under sd_lock.
1863 if (!(stp->sd_rput_opt & SR_POLLIN))
1864 goto no_pollwake;
1865 stp->sd_rput_opt &= ~SR_POLLIN;
1867 mutex_exit(&stp->sd_lock);
1868 pollwakeup(&stp->sd_pollist, pollwakeups);
1869 mutex_enter(&stp->sd_lock);
1871 no_pollwake:
1874 * strsendsig can handle multiple signals with a
1875 * single call.
1877 if (stp->sd_sigflags & signals)
1878 strsendsig(stp->sd_siglist, signals, band, 0);
1879 mutex_exit(&stp->sd_lock);
1882 done:
1883 if (nextbp == NULL)
1884 return (0);
1887 * Any signals were handled the first time.
1888 * Wakeups and pollwakeups are redone to avoid any race
1889 * conditions - all the messages are not queued until the
1890 * last message has been processed by strrput.
1892 bp = nextbp;
1893 signals = firstmsgsigs = allmsgsigs = 0;
1894 mutex_enter(&stp->sd_lock);
1895 goto one_more;
1898 static void
1899 log_dupioc(queue_t *rq, mblk_t *bp)
1901 queue_t *wq, *qp;
1902 char *modnames, *mnp, *dname;
1903 size_t maxmodstr;
1904 boolean_t islast;
1907 * Allocate a buffer large enough to hold the names of nstrpush modules
1908 * and one driver, with spaces between and NUL terminator. If we can't
1909 * get memory, then we'll just log the driver name.
1911 maxmodstr = nstrpush * (FMNAMESZ + 1);
1912 mnp = modnames = kmem_alloc(maxmodstr, KM_NOSLEEP);
1914 /* march down write side to print log message down to the driver */
1915 wq = WR(rq);
1917 /* make sure q_next doesn't shift around while we're grabbing data */
1918 claimstr(wq);
1919 qp = wq->q_next;
1920 do {
1921 dname = Q2NAME(qp);
1922 islast = !SAMESTR(qp) || qp->q_next == NULL;
1923 if (modnames == NULL) {
1925 * If we don't have memory, then get the driver name in
1926 * the log where we can see it. Note that memory
1927 * pressure is a possible cause of these sorts of bugs.
1929 if (islast) {
1930 modnames = dname;
1931 maxmodstr = 0;
1933 } else {
1934 mnp += snprintf(mnp, FMNAMESZ + 1, "%s", dname);
1935 if (!islast)
1936 *mnp++ = ' ';
1938 qp = qp->q_next;
1939 } while (!islast);
1940 releasestr(wq);
1941 /* Cannot happen unless stream head is corrupt. */
1942 ASSERT(modnames != NULL);
1943 (void) strlog(rq->q_qinfo->qi_minfo->mi_idnum, 0, 1,
1944 SL_CONSOLE|SL_TRACE|SL_ERROR,
1945 "Warning: stream %p received duplicate %X M_IOC%s; module list: %s",
1946 rq->q_ptr, ((struct iocblk *)bp->b_rptr)->ioc_cmd,
1947 (DB_TYPE(bp) == M_IOCACK ? "ACK" : "NAK"), modnames);
1948 if (maxmodstr != 0)
1949 kmem_free(modnames, maxmodstr);
1953 strrput_nondata(queue_t *q, mblk_t *bp)
1955 struct stdata *stp;
1956 struct iocblk *iocbp;
1957 struct stroptions *sop;
1958 struct copyreq *reqp;
1959 struct copyresp *resp;
1960 unsigned char bpri;
1961 unsigned char flushed_already = 0;
1963 stp = (struct stdata *)q->q_ptr;
1965 ASSERT(!(stp->sd_flag & STPLEX));
1966 ASSERT(qclaimed(q));
1968 switch (bp->b_datap->db_type) {
1969 case M_ERROR:
1971 * An error has occurred downstream, the errno is in the first
1972 * bytes of the message.
1974 if ((bp->b_wptr - bp->b_rptr) == 2) { /* New flavor */
1975 unsigned char rw = 0;
1977 mutex_enter(&stp->sd_lock);
1978 if (*bp->b_rptr != NOERROR) { /* read error */
1979 if (*bp->b_rptr != 0) {
1980 if (stp->sd_flag & STRDERR)
1981 flushed_already |= FLUSHR;
1982 stp->sd_flag |= STRDERR;
1983 rw |= FLUSHR;
1984 } else {
1985 stp->sd_flag &= ~STRDERR;
1987 stp->sd_rerror = *bp->b_rptr;
1989 bp->b_rptr++;
1990 if (*bp->b_rptr != NOERROR) { /* write error */
1991 if (*bp->b_rptr != 0) {
1992 if (stp->sd_flag & STWRERR)
1993 flushed_already |= FLUSHW;
1994 stp->sd_flag |= STWRERR;
1995 rw |= FLUSHW;
1996 } else {
1997 stp->sd_flag &= ~STWRERR;
1999 stp->sd_werror = *bp->b_rptr;
2001 if (rw) {
2002 TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_WAKE,
2003 "strrput cv_broadcast:q %p, bp %p",
2004 q, bp);
2005 cv_broadcast(&q->q_wait); /* readers */
2006 cv_broadcast(&_WR(q)->q_wait); /* writers */
2007 cv_broadcast(&stp->sd_monitor); /* ioctllers */
2009 mutex_exit(&stp->sd_lock);
2010 pollwakeup(&stp->sd_pollist, POLLERR);
2011 mutex_enter(&stp->sd_lock);
2013 if (stp->sd_sigflags & S_ERROR)
2014 strsendsig(stp->sd_siglist, S_ERROR, 0,
2015 ((rw & FLUSHR) ? stp->sd_rerror :
2016 stp->sd_werror));
2017 mutex_exit(&stp->sd_lock);
2019 * Send the M_FLUSH only
2020 * for the first M_ERROR
2021 * message on the stream
2023 if (flushed_already == rw) {
2024 freemsg(bp);
2025 return (0);
2028 bp->b_datap->db_type = M_FLUSH;
2029 *bp->b_rptr = rw;
2030 bp->b_wptr = bp->b_rptr + 1;
2032 * Protect against the driver
2033 * passing up messages after
2034 * it has done a qprocsoff
2036 if (_OTHERQ(q)->q_next == NULL)
2037 freemsg(bp);
2038 else
2039 qreply(q, bp);
2040 return (0);
2041 } else
2042 mutex_exit(&stp->sd_lock);
2043 } else if (*bp->b_rptr != 0) { /* Old flavor */
2044 if (stp->sd_flag & (STRDERR|STWRERR))
2045 flushed_already = FLUSHRW;
2046 mutex_enter(&stp->sd_lock);
2047 stp->sd_flag |= (STRDERR|STWRERR);
2048 stp->sd_rerror = *bp->b_rptr;
2049 stp->sd_werror = *bp->b_rptr;
2050 TRACE_2(TR_FAC_STREAMS_FR,
2051 TR_STRRPUT_WAKE2,
2052 "strrput wakeup #2:q %p, bp %p", q, bp);
2053 cv_broadcast(&q->q_wait); /* the readers */
2054 cv_broadcast(&_WR(q)->q_wait); /* the writers */
2055 cv_broadcast(&stp->sd_monitor); /* ioctllers */
2057 mutex_exit(&stp->sd_lock);
2058 pollwakeup(&stp->sd_pollist, POLLERR);
2059 mutex_enter(&stp->sd_lock);
2061 if (stp->sd_sigflags & S_ERROR)
2062 strsendsig(stp->sd_siglist, S_ERROR, 0,
2063 (stp->sd_werror ? stp->sd_werror :
2064 stp->sd_rerror));
2065 mutex_exit(&stp->sd_lock);
2068 * Send the M_FLUSH only
2069 * for the first M_ERROR
2070 * message on the stream
2072 if (flushed_already != FLUSHRW) {
2073 bp->b_datap->db_type = M_FLUSH;
2074 *bp->b_rptr = FLUSHRW;
2076 * Protect against the driver passing up
2077 * messages after it has done a
2078 * qprocsoff.
2080 if (_OTHERQ(q)->q_next == NULL)
2081 freemsg(bp);
2082 else
2083 qreply(q, bp);
2084 return (0);
2087 freemsg(bp);
2088 return (0);
2090 case M_HANGUP:
2092 freemsg(bp);
2093 mutex_enter(&stp->sd_lock);
2094 stp->sd_werror = ENXIO;
2095 stp->sd_flag |= STRHUP;
2096 stp->sd_flag &= ~(WSLEEP|RSLEEP);
2099 * send signal if controlling tty
2102 if (stp->sd_sidp) {
2103 prsignal(stp->sd_sidp, SIGHUP);
2104 if (stp->sd_sidp != stp->sd_pgidp)
2105 pgsignal(stp->sd_pgidp, SIGTSTP);
2109 * wake up read, write, and exception pollers and
2110 * reset wakeup mechanism.
2112 cv_broadcast(&q->q_wait); /* the readers */
2113 cv_broadcast(&_WR(q)->q_wait); /* the writers */
2114 cv_broadcast(&stp->sd_monitor); /* the ioctllers */
2115 strhup(stp);
2116 mutex_exit(&stp->sd_lock);
2117 return (0);
2119 case M_UNHANGUP:
2120 freemsg(bp);
2121 mutex_enter(&stp->sd_lock);
2122 stp->sd_werror = 0;
2123 stp->sd_flag &= ~STRHUP;
2124 mutex_exit(&stp->sd_lock);
2125 return (0);
2127 case M_SIG:
2129 * Someone downstream wants to post a signal. The
2130 * signal to post is contained in the first byte of the
2131 * message. If the message would go on the front of
2132 * the queue, send a signal to the process group
2133 * (if not SIGPOLL) or to the siglist processes
2134 * (SIGPOLL). If something is already on the queue,
2135 * OR if we are delivering a delayed suspend (*sigh*
2136 * another "tty" hack) and there's no one sleeping already,
2137 * just enqueue the message.
2139 mutex_enter(&stp->sd_lock);
2140 if (q->q_first || (*bp->b_rptr == SIGTSTP &&
2141 !(stp->sd_flag & RSLEEP))) {
2142 (void) putq(q, bp);
2143 mutex_exit(&stp->sd_lock);
2144 return (0);
2146 mutex_exit(&stp->sd_lock);
2147 /* FALLTHRU */
2149 case M_PCSIG:
2151 * Don't enqueue, just post the signal.
2153 strsignal(stp, *bp->b_rptr, 0L);
2154 freemsg(bp);
2155 return (0);
2157 case M_CMD:
2158 if (MBLKL(bp) != sizeof (cmdblk_t)) {
2159 freemsg(bp);
2160 return (0);
2163 mutex_enter(&stp->sd_lock);
2164 if (stp->sd_flag & STRCMDWAIT) {
2165 ASSERT(stp->sd_cmdblk == NULL);
2166 stp->sd_cmdblk = bp;
2167 cv_broadcast(&stp->sd_monitor);
2168 mutex_exit(&stp->sd_lock);
2169 } else {
2170 mutex_exit(&stp->sd_lock);
2171 freemsg(bp);
2173 return (0);
2175 case M_FLUSH:
2177 * Flush queues. The indication of which queues to flush
2178 * is in the first byte of the message. If the read queue
2179 * is specified, then flush it. If FLUSHBAND is set, just
2180 * flush the band specified by the second byte of the message.
2182 * If a module has issued a M_SETOPT to not flush hi
2183 * priority messages off of the stream head, then pass this
2184 * flag into the flushq code to preserve such messages.
2187 if (*bp->b_rptr & FLUSHR) {
2188 mutex_enter(&stp->sd_lock);
2189 if (*bp->b_rptr & FLUSHBAND) {
2190 ASSERT((bp->b_wptr - bp->b_rptr) >= 2);
2191 flushband(q, *(bp->b_rptr + 1), FLUSHALL);
2192 } else
2193 flushq_common(q, FLUSHALL,
2194 stp->sd_read_opt & RFLUSHPCPROT);
2195 if ((q->q_first == NULL) ||
2196 (q->q_first->b_datap->db_type < QPCTL))
2197 stp->sd_flag &= ~STRPRI;
2198 else {
2199 ASSERT(stp->sd_flag & STRPRI);
2201 mutex_exit(&stp->sd_lock);
2203 if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
2204 *bp->b_rptr &= ~FLUSHR;
2205 bp->b_flag |= MSGNOLOOP;
2207 * Protect against the driver passing up
2208 * messages after it has done a qprocsoff.
2210 if (_OTHERQ(q)->q_next == NULL)
2211 freemsg(bp);
2212 else
2213 qreply(q, bp);
2214 return (0);
2216 freemsg(bp);
2217 return (0);
2219 case M_IOCACK:
2220 case M_IOCNAK:
2221 iocbp = (struct iocblk *)bp->b_rptr;
2223 * If not waiting for ACK or NAK then just free msg.
2224 * If incorrect id sequence number then just free msg.
2225 * If already have ACK or NAK for user then this is a
2226 * duplicate, display a warning and free the msg.
2228 mutex_enter(&stp->sd_lock);
2229 if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2230 (stp->sd_iocid != iocbp->ioc_id)) {
2232 * If the ACK/NAK is a dup, display a message
2233 * Dup is when sd_iocid == ioc_id, and
2234 * sd_iocblk == <valid ptr> or -1 (the former
2235 * is when an ioctl has been put on the stream
2236 * head, but has not yet been consumed, the
2237 * later is when it has been consumed).
2239 if ((stp->sd_iocid == iocbp->ioc_id) &&
2240 (stp->sd_iocblk != NULL)) {
2241 log_dupioc(q, bp);
2243 freemsg(bp);
2244 mutex_exit(&stp->sd_lock);
2245 return (0);
2249 * Assign ACK or NAK to user and wake up.
2251 stp->sd_iocblk = bp;
2252 cv_broadcast(&stp->sd_monitor);
2253 mutex_exit(&stp->sd_lock);
2254 return (0);
2256 case M_COPYIN:
2257 case M_COPYOUT:
2258 reqp = (struct copyreq *)bp->b_rptr;
2261 * If not waiting for ACK or NAK then just fail request.
2262 * If already have ACK, NAK, or copy request, then just
2263 * fail request.
2264 * If incorrect id sequence number then just fail request.
2266 mutex_enter(&stp->sd_lock);
2267 if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2268 (stp->sd_iocid != reqp->cq_id)) {
2269 if (bp->b_cont) {
2270 freemsg(bp->b_cont);
2271 bp->b_cont = NULL;
2273 bp->b_datap->db_type = M_IOCDATA;
2274 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
2275 resp = (struct copyresp *)bp->b_rptr;
2276 resp->cp_rval = (caddr_t)1; /* failure */
2277 mutex_exit(&stp->sd_lock);
2278 putnext(stp->sd_wrq, bp);
2279 return (0);
2283 * Assign copy request to user and wake up.
2285 stp->sd_iocblk = bp;
2286 cv_broadcast(&stp->sd_monitor);
2287 mutex_exit(&stp->sd_lock);
2288 return (0);
2290 case M_SETOPTS:
2292 * Set stream head options (read option, write offset,
2293 * min/max packet size, and/or high/low water marks for
2294 * the read side only).
2297 bpri = 0;
2298 sop = (struct stroptions *)bp->b_rptr;
2299 mutex_enter(&stp->sd_lock);
2300 if (sop->so_flags & SO_READOPT) {
2301 switch (sop->so_readopt & RMODEMASK) {
2302 case RNORM:
2303 stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
2304 break;
2306 case RMSGD:
2307 stp->sd_read_opt =
2308 ((stp->sd_read_opt & ~RD_MSGNODIS) |
2309 RD_MSGDIS);
2310 break;
2312 case RMSGN:
2313 stp->sd_read_opt =
2314 ((stp->sd_read_opt & ~RD_MSGDIS) |
2315 RD_MSGNODIS);
2316 break;
2318 switch (sop->so_readopt & RPROTMASK) {
2319 case RPROTNORM:
2320 stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
2321 break;
2323 case RPROTDAT:
2324 stp->sd_read_opt =
2325 ((stp->sd_read_opt & ~RD_PROTDIS) |
2326 RD_PROTDAT);
2327 break;
2329 case RPROTDIS:
2330 stp->sd_read_opt =
2331 ((stp->sd_read_opt & ~RD_PROTDAT) |
2332 RD_PROTDIS);
2333 break;
2335 switch (sop->so_readopt & RFLUSHMASK) {
2336 case RFLUSHPCPROT:
2338 * This sets the stream head to NOT flush
2339 * M_PCPROTO messages.
2341 stp->sd_read_opt |= RFLUSHPCPROT;
2342 break;
2345 if (sop->so_flags & SO_ERROPT) {
2346 switch (sop->so_erropt & RERRMASK) {
2347 case RERRNORM:
2348 stp->sd_flag &= ~STRDERRNONPERSIST;
2349 break;
2350 case RERRNONPERSIST:
2351 stp->sd_flag |= STRDERRNONPERSIST;
2352 break;
2354 switch (sop->so_erropt & WERRMASK) {
2355 case WERRNORM:
2356 stp->sd_flag &= ~STWRERRNONPERSIST;
2357 break;
2358 case WERRNONPERSIST:
2359 stp->sd_flag |= STWRERRNONPERSIST;
2360 break;
2363 if (sop->so_flags & SO_COPYOPT) {
2364 if (sop->so_copyopt & ZCVMSAFE) {
2365 stp->sd_copyflag |= STZCVMSAFE;
2366 stp->sd_copyflag &= ~STZCVMUNSAFE;
2367 } else if (sop->so_copyopt & ZCVMUNSAFE) {
2368 stp->sd_copyflag |= STZCVMUNSAFE;
2369 stp->sd_copyflag &= ~STZCVMSAFE;
2372 if (sop->so_copyopt & COPYCACHED) {
2373 stp->sd_copyflag |= STRCOPYCACHED;
2376 if (sop->so_flags & SO_WROFF)
2377 stp->sd_wroff = sop->so_wroff;
2378 if (sop->so_flags & SO_TAIL)
2379 stp->sd_tail = sop->so_tail;
2380 if (sop->so_flags & SO_MINPSZ)
2381 q->q_minpsz = sop->so_minpsz;
2382 if (sop->so_flags & SO_MAXPSZ)
2383 q->q_maxpsz = sop->so_maxpsz;
2384 if (sop->so_flags & SO_MAXBLK)
2385 stp->sd_maxblk = sop->so_maxblk;
2386 if (sop->so_flags & SO_HIWAT) {
2387 if (sop->so_flags & SO_BAND) {
2388 if (strqset(q, QHIWAT,
2389 sop->so_band, sop->so_hiwat)) {
2390 cmn_err(CE_WARN, "strrput: could not "
2391 "allocate qband\n");
2392 } else {
2393 bpri = sop->so_band;
2395 } else {
2396 q->q_hiwat = sop->so_hiwat;
2399 if (sop->so_flags & SO_LOWAT) {
2400 if (sop->so_flags & SO_BAND) {
2401 if (strqset(q, QLOWAT,
2402 sop->so_band, sop->so_lowat)) {
2403 cmn_err(CE_WARN, "strrput: could not "
2404 "allocate qband\n");
2405 } else {
2406 bpri = sop->so_band;
2408 } else {
2409 q->q_lowat = sop->so_lowat;
2412 if (sop->so_flags & SO_MREADON)
2413 stp->sd_flag |= SNDMREAD;
2414 if (sop->so_flags & SO_MREADOFF)
2415 stp->sd_flag &= ~SNDMREAD;
2416 if (sop->so_flags & SO_NDELON)
2417 stp->sd_flag |= OLDNDELAY;
2418 if (sop->so_flags & SO_NDELOFF)
2419 stp->sd_flag &= ~OLDNDELAY;
2420 if (sop->so_flags & SO_ISTTY)
2421 stp->sd_flag |= STRISTTY;
2422 if (sop->so_flags & SO_ISNTTY)
2423 stp->sd_flag &= ~STRISTTY;
2424 if (sop->so_flags & SO_TOSTOP)
2425 stp->sd_flag |= STRTOSTOP;
2426 if (sop->so_flags & SO_TONSTOP)
2427 stp->sd_flag &= ~STRTOSTOP;
2428 if (sop->so_flags & SO_DELIM)
2429 stp->sd_flag |= STRDELIM;
2430 if (sop->so_flags & SO_NODELIM)
2431 stp->sd_flag &= ~STRDELIM;
2433 mutex_exit(&stp->sd_lock);
2434 freemsg(bp);
2436 /* Check backenable in case the water marks changed */
2437 qbackenable(q, bpri);
2438 return (0);
2441 * The following set of cases deal with situations where two stream
2442 * heads are connected to each other (twisted streams). These messages
2443 * have no meaning at the stream head.
2445 case M_BREAK:
2446 case M_CTL:
2447 case M_DELAY:
2448 case M_START:
2449 case M_STOP:
2450 case M_IOCDATA:
2451 case M_STARTI:
2452 case M_STOPI:
2453 freemsg(bp);
2454 return (0);
2456 case M_IOCTL:
2458 * Always NAK this condition
2459 * (makes no sense)
2460 * If there is one or more threads in the read side
2461 * rwnext we have to defer the nacking until that thread
2462 * returns (in strget).
2464 mutex_enter(&stp->sd_lock);
2465 if (stp->sd_struiodnak != 0) {
2467 * Defer NAK to the streamhead. Queue at the end
2468 * the list.
2470 mblk_t *mp = stp->sd_struionak;
2472 while (mp && mp->b_next)
2473 mp = mp->b_next;
2474 if (mp)
2475 mp->b_next = bp;
2476 else
2477 stp->sd_struionak = bp;
2478 bp->b_next = NULL;
2479 mutex_exit(&stp->sd_lock);
2480 return (0);
2482 mutex_exit(&stp->sd_lock);
2484 bp->b_datap->db_type = M_IOCNAK;
2486 * Protect against the driver passing up
2487 * messages after it has done a qprocsoff.
2489 if (_OTHERQ(q)->q_next == NULL)
2490 freemsg(bp);
2491 else
2492 qreply(q, bp);
2493 return (0);
2495 default:
2496 #ifdef DEBUG
2497 cmn_err(CE_WARN,
2498 "bad message type %x received at stream head\n",
2499 bp->b_datap->db_type);
2500 #endif
2501 freemsg(bp);
2502 return (0);
2505 /* NOTREACHED */
2509 * Check if the stream pointed to by `stp' can be written to, and return an
2510 * error code if not. If `eiohup' is set, then return EIO if STRHUP is set.
2511 * If `sigpipeok' is set and the SW_SIGPIPE option is enabled on the stream,
2512 * then always return EPIPE and send a SIGPIPE to the invoking thread.
2514 static int
2515 strwriteable(struct stdata *stp, boolean_t eiohup, boolean_t sigpipeok)
2517 int error;
2519 ASSERT(MUTEX_HELD(&stp->sd_lock));
2522 * For modem support, POSIX states that on writes, EIO should
2523 * be returned if the stream has been hung up.
2525 if (eiohup && (stp->sd_flag & (STPLEX|STRHUP)) == STRHUP)
2526 error = EIO;
2527 else
2528 error = strgeterr(stp, STRHUP|STPLEX|STWRERR, 0);
2530 if (error != 0) {
2531 if (!(stp->sd_flag & STPLEX) &&
2532 (stp->sd_wput_opt & SW_SIGPIPE) && sigpipeok) {
2533 tsignal(curthread, SIGPIPE);
2534 error = EPIPE;
2538 return (error);
2542 * Copyin and send data down a stream.
2543 * The caller will allocate and copyin any control part that precedes the
2544 * message and pass that in as mctl.
2546 * Caller should *not* hold sd_lock.
2547 * When EWOULDBLOCK is returned the caller has to redo the canputnext
2548 * under sd_lock in order to avoid missing a backenabling wakeup.
2550 * Use iosize = -1 to not send any M_DATA. iosize = 0 sends zero-length M_DATA.
2552 * Set MSG_IGNFLOW in flags to ignore flow control for hipri messages.
2553 * For sync streams we can only ignore flow control by reverting to using
2554 * putnext.
2556 * If sd_maxblk is less than *iosize this routine might return without
2557 * transferring all of *iosize. In all cases, on return *iosize will contain
2558 * the amount of data that was transferred.
2560 static int
2561 strput(struct stdata *stp, mblk_t *mctl, struct uio *uiop, ssize_t *iosize,
2562 int b_flag, int pri, int flags)
2564 struiod_t uiod;
2565 struct iovec buf[IOV_MAX_STACK];
2566 int iovlen = 0;
2567 mblk_t *mp;
2568 queue_t *wqp = stp->sd_wrq;
2569 int error = 0;
2570 ssize_t count = *iosize;
2572 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
2574 if (uiop != NULL && count >= 0)
2575 flags |= stp->sd_struiowrq ? STRUIO_POSTPONE : 0;
2577 if (!(flags & STRUIO_POSTPONE)) {
2579 * Use regular canputnext, strmakedata, putnext sequence.
2581 if (pri == 0) {
2582 if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2583 freemsg(mctl);
2584 return (EWOULDBLOCK);
2586 } else {
2587 if (!(flags & MSG_IGNFLOW) && !bcanputnext(wqp, pri)) {
2588 freemsg(mctl);
2589 return (EWOULDBLOCK);
2593 if ((error = strmakedata(iosize, uiop, stp, flags,
2594 &mp)) != 0) {
2595 freemsg(mctl);
2597 * need to change return code to ENOMEM
2598 * so that this is not confused with
2599 * flow control, EAGAIN.
2602 if (error == EAGAIN)
2603 return (ENOMEM);
2604 else
2605 return (error);
2607 if (mctl != NULL) {
2608 if (mctl->b_cont == NULL)
2609 mctl->b_cont = mp;
2610 else if (mp != NULL)
2611 linkb(mctl, mp);
2612 mp = mctl;
2613 } else if (mp == NULL)
2614 return (0);
2616 mp->b_flag |= b_flag;
2617 mp->b_band = (uchar_t)pri;
2619 if (flags & MSG_IGNFLOW) {
2621 * XXX Hack: Don't get stuck running service
2622 * procedures. This is needed for sockfs when
2623 * sending the unbind message out of the rput
2624 * procedure - we don't want a put procedure
2625 * to run service procedures.
2627 putnext(wqp, mp);
2628 } else {
2629 stream_willservice(stp);
2630 putnext(wqp, mp);
2631 stream_runservice(stp);
2633 return (0);
2636 * Stream supports rwnext() for the write side.
2638 if ((error = strmakedata(iosize, uiop, stp, flags, &mp)) != 0) {
2639 freemsg(mctl);
2641 * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
2643 return (error == EAGAIN ? ENOMEM : error);
2645 if (mctl != NULL) {
2646 if (mctl->b_cont == NULL)
2647 mctl->b_cont = mp;
2648 else if (mp != NULL)
2649 linkb(mctl, mp);
2650 mp = mctl;
2651 } else if (mp == NULL) {
2652 return (0);
2655 mp->b_flag |= b_flag;
2656 mp->b_band = (uchar_t)pri;
2658 if (uiop->uio_iovcnt > IOV_MAX_STACK) {
2659 iovlen = uiop->uio_iovcnt * sizeof (iovec_t);
2660 uiod.d_iov = kmem_alloc(iovlen, KM_SLEEP);
2661 } else {
2662 uiod.d_iov = buf;
2665 (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov, uiop->uio_iovcnt);
2666 uiod.d_uio.uio_offset = 0;
2667 uiod.d_mp = mp;
2668 error = rwnext(wqp, &uiod);
2669 if (! uiod.d_mp) {
2670 uioskip(uiop, *iosize);
2671 if (iovlen != 0)
2672 kmem_free(uiod.d_iov, iovlen);
2673 return (error);
2675 ASSERT(mp == uiod.d_mp);
2676 if (error == EINVAL) {
2678 * The stream plumbing must have changed while
2679 * we were away, so just turn off rwnext()s.
2681 error = 0;
2682 } else if (error == EBUSY || error == EWOULDBLOCK) {
2684 * Couldn't enter a perimeter or took a page fault,
2685 * so fall-back to putnext().
2687 error = 0;
2688 } else {
2689 freemsg(mp);
2690 if (iovlen != 0)
2691 kmem_free(uiod.d_iov, iovlen);
2692 return (error);
2694 /* Have to check canput before consuming data from the uio */
2695 if (pri == 0) {
2696 if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2697 freemsg(mp);
2698 if (iovlen != 0)
2699 kmem_free(uiod.d_iov, iovlen);
2700 return (EWOULDBLOCK);
2702 } else {
2703 if (!bcanputnext(wqp, pri) && !(flags & MSG_IGNFLOW)) {
2704 freemsg(mp);
2705 if (iovlen != 0)
2706 kmem_free(uiod.d_iov, iovlen);
2707 return (EWOULDBLOCK);
2710 ASSERT(mp == uiod.d_mp);
2711 /* Copyin data from the uio */
2712 if ((error = struioget(wqp, mp, &uiod, 0)) != 0) {
2713 freemsg(mp);
2714 if (iovlen != 0)
2715 kmem_free(uiod.d_iov, iovlen);
2716 return (error);
2718 uioskip(uiop, *iosize);
2719 if (flags & MSG_IGNFLOW) {
2721 * XXX Hack: Don't get stuck running service procedures.
2722 * This is needed for sockfs when sending the unbind message
2723 * out of the rput procedure - we don't want a put procedure
2724 * to run service procedures.
2726 putnext(wqp, mp);
2727 } else {
2728 stream_willservice(stp);
2729 putnext(wqp, mp);
2730 stream_runservice(stp);
2732 if (iovlen != 0)
2733 kmem_free(uiod.d_iov, iovlen);
2734 return (0);
2738 * Write attempts to break the write request into messages conforming
2739 * with the minimum and maximum packet sizes set downstream.
2741 * Write will not block if downstream queue is full and
2742 * O_NDELAY is set, otherwise it will block waiting for the queue to get room.
2744 * A write of zero bytes gets packaged into a zero length message and sent
2745 * downstream like any other message.
2747 * If buffers of the requested sizes are not available, the write will
2748 * sleep until the buffers become available.
2750 * Write (if specified) will supply a write offset in a message if it
2751 * makes sense. This can be specified by downstream modules as part of
2752 * a M_SETOPTS message. Write will not supply the write offset if it
2753 * cannot supply any data in a buffer. In other words, write will never
2754 * send down an empty packet due to a write offset.
2756 /* ARGSUSED2 */
2758 strwrite(struct vnode *vp, struct uio *uiop, cred_t *crp)
2760 return (strwrite_common(vp, uiop, crp, 0));
2763 /* ARGSUSED2 */
2765 strwrite_common(struct vnode *vp, struct uio *uiop, cred_t *crp, int wflag)
2767 struct stdata *stp;
2768 struct queue *wqp;
2769 ssize_t rmin, rmax;
2770 ssize_t iosize;
2771 int waitflag;
2772 int tempmode;
2773 int error = 0;
2774 int b_flag;
2776 ASSERT(vp->v_stream);
2777 stp = vp->v_stream;
2779 mutex_enter(&stp->sd_lock);
2781 if ((error = i_straccess(stp, JCWRITE)) != 0) {
2782 mutex_exit(&stp->sd_lock);
2783 return (error);
2786 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
2787 error = strwriteable(stp, B_TRUE, B_TRUE);
2788 if (error != 0) {
2789 mutex_exit(&stp->sd_lock);
2790 return (error);
2794 mutex_exit(&stp->sd_lock);
2796 wqp = stp->sd_wrq;
2798 /* get these values from them cached in the stream head */
2799 rmin = stp->sd_qn_minpsz;
2800 rmax = stp->sd_qn_maxpsz;
2803 * Check the min/max packet size constraints. If min packet size
2804 * is non-zero, the write cannot be split into multiple messages
2805 * and still guarantee the size constraints.
2807 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_IN, "strwrite in:q %p", wqp);
2809 ASSERT((rmax >= 0) || (rmax == INFPSZ));
2810 if (rmax == 0) {
2811 return (0);
2813 if (rmin > 0) {
2814 if (uiop->uio_resid < rmin) {
2815 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2816 "strwrite out:q %p out %d error %d",
2817 wqp, 0, ERANGE);
2818 return (ERANGE);
2820 if ((rmax != INFPSZ) && (uiop->uio_resid > rmax)) {
2821 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2822 "strwrite out:q %p out %d error %d",
2823 wqp, 1, ERANGE);
2824 return (ERANGE);
2829 * Do until count satisfied or error.
2831 waitflag = WRITEWAIT | wflag;
2832 if (stp->sd_flag & OLDNDELAY)
2833 tempmode = uiop->uio_fmode & ~FNDELAY;
2834 else
2835 tempmode = uiop->uio_fmode;
2837 if (rmax == INFPSZ)
2838 rmax = uiop->uio_resid;
2841 * Note that tempmode does not get used in strput/strmakedata
2842 * but only in strwaitq. The other routines use uio_fmode
2843 * unmodified.
2846 while (1) { /* breaks when uio_resid reaches zero */
2848 * Determine the size of the next message to be
2849 * packaged. May have to break write into several
2850 * messages based on max packet size.
2852 iosize = MIN(uiop->uio_resid, rmax);
2855 * Put block downstream when flow control allows it.
2857 if ((stp->sd_flag & STRDELIM) && (uiop->uio_resid == iosize))
2858 b_flag = MSGDELIM;
2859 else
2860 b_flag = 0;
2862 for (;;) {
2863 int done = 0;
2865 error = strput(stp, NULL, uiop, &iosize, b_flag, 0, 0);
2866 if (error == 0)
2867 break;
2868 if (error != EWOULDBLOCK)
2869 goto out;
2871 mutex_enter(&stp->sd_lock);
2873 * Check for a missed wakeup.
2874 * Needed since strput did not hold sd_lock across
2875 * the canputnext.
2877 if (canputnext(wqp)) {
2878 /* Try again */
2879 mutex_exit(&stp->sd_lock);
2880 continue;
2882 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAIT,
2883 "strwrite wait:q %p wait", wqp);
2884 if ((error = strwaitq(stp, waitflag, (ssize_t)0,
2885 tempmode, -1, &done)) != 0 || done) {
2886 mutex_exit(&stp->sd_lock);
2887 if ((vp->v_type == VFIFO) &&
2888 (uiop->uio_fmode & FNDELAY) &&
2889 (error == EAGAIN))
2890 error = 0;
2891 goto out;
2893 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAKE,
2894 "strwrite wake:q %p awakes", wqp);
2895 if ((error = i_straccess(stp, JCWRITE)) != 0) {
2896 mutex_exit(&stp->sd_lock);
2897 goto out;
2899 mutex_exit(&stp->sd_lock);
2901 waitflag |= NOINTR;
2902 TRACE_2(TR_FAC_STREAMS_FR, TR_STRWRITE_RESID,
2903 "strwrite resid:q %p uiop %p", wqp, uiop);
2904 if (uiop->uio_resid) {
2905 /* Recheck for errors - needed for sockets */
2906 if ((stp->sd_wput_opt & SW_RECHECK_ERR) &&
2907 (stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
2908 mutex_enter(&stp->sd_lock);
2909 error = strwriteable(stp, B_FALSE, B_TRUE);
2910 mutex_exit(&stp->sd_lock);
2911 if (error != 0)
2912 return (error);
2914 continue;
2916 break;
2918 out:
2920 * For historical reasons, applications expect EAGAIN when a data
2921 * mblk_t cannot be allocated, so change ENOMEM back to EAGAIN.
2923 if (error == ENOMEM)
2924 error = EAGAIN;
2925 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2926 "strwrite out:q %p out %d error %d", wqp, 2, error);
2927 return (error);
2931 * Stream head write service routine.
2932 * Its job is to wake up any sleeping writers when a queue
2933 * downstream needs data (part of the flow control in putq and getq).
2934 * It also must wake anyone sleeping on a poll().
2935 * For stream head right below mux module, it must also invoke put procedure
2936 * of next downstream module.
2939 strwsrv(queue_t *q)
2941 struct stdata *stp;
2942 queue_t *tq;
2943 qband_t *qbp;
2944 int i;
2945 qband_t *myqbp;
2946 int isevent;
2947 unsigned char qbf[NBAND]; /* band flushing backenable flags */
2949 TRACE_1(TR_FAC_STREAMS_FR,
2950 TR_STRWSRV, "strwsrv:q %p", q);
2951 stp = (struct stdata *)q->q_ptr;
2952 ASSERT(qclaimed(q));
2953 mutex_enter(&stp->sd_lock);
2954 ASSERT(!(stp->sd_flag & STPLEX));
2956 if (stp->sd_flag & WSLEEP) {
2957 stp->sd_flag &= ~WSLEEP;
2958 cv_broadcast(&q->q_wait);
2960 mutex_exit(&stp->sd_lock);
2962 /* The other end of a stream pipe went away. */
2963 if ((tq = q->q_next) == NULL) {
2964 return (0);
2967 /* Find the next module forward that has a service procedure */
2968 claimstr(q);
2969 tq = q->q_nfsrv;
2970 ASSERT(tq != NULL);
2972 if ((q->q_flag & QBACK)) {
2973 if ((tq->q_flag & QFULL)) {
2974 mutex_enter(QLOCK(tq));
2975 if (!(tq->q_flag & QFULL)) {
2976 mutex_exit(QLOCK(tq));
2977 goto wakeup;
2980 * The queue must have become full again. Set QWANTW
2981 * again so strwsrv will be back enabled when
2982 * the queue becomes non-full next time.
2984 tq->q_flag |= QWANTW;
2985 mutex_exit(QLOCK(tq));
2986 } else {
2987 wakeup:
2988 pollwakeup(&stp->sd_pollist, POLLWRNORM);
2989 mutex_enter(&stp->sd_lock);
2990 if (stp->sd_sigflags & S_WRNORM)
2991 strsendsig(stp->sd_siglist, S_WRNORM, 0, 0);
2992 mutex_exit(&stp->sd_lock);
2996 isevent = 0;
2997 i = 1;
2998 bzero((caddr_t)qbf, NBAND);
2999 mutex_enter(QLOCK(tq));
3000 if ((myqbp = q->q_bandp) != NULL)
3001 for (qbp = tq->q_bandp; qbp && myqbp; qbp = qbp->qb_next) {
3002 ASSERT(myqbp);
3003 if ((myqbp->qb_flag & QB_BACK)) {
3004 if (qbp->qb_flag & QB_FULL) {
3006 * The band must have become full again.
3007 * Set QB_WANTW again so strwsrv will
3008 * be back enabled when the band becomes
3009 * non-full next time.
3011 qbp->qb_flag |= QB_WANTW;
3012 } else {
3013 isevent = 1;
3014 qbf[i] = 1;
3017 myqbp = myqbp->qb_next;
3018 i++;
3020 mutex_exit(QLOCK(tq));
3022 if (isevent) {
3023 for (i = tq->q_nband; i; i--) {
3024 if (qbf[i]) {
3025 pollwakeup(&stp->sd_pollist, POLLWRBAND);
3026 mutex_enter(&stp->sd_lock);
3027 if (stp->sd_sigflags & S_WRBAND)
3028 strsendsig(stp->sd_siglist, S_WRBAND,
3029 (uchar_t)i, 0);
3030 mutex_exit(&stp->sd_lock);
3035 releasestr(q);
3036 return (0);
3040 * Special case of strcopyin/strcopyout for copying
3041 * struct strioctl that can deal with both data
3042 * models.
3045 #ifdef _LP64
3047 static int
3048 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3050 struct strioctl32 strioc32;
3051 struct strioctl *striocp;
3053 if (copyflag & U_TO_K) {
3054 ASSERT((copyflag & K_TO_K) == 0);
3056 if ((flag & FMODELS) == DATAMODEL_ILP32) {
3057 if (copyin(from, &strioc32, sizeof (strioc32)))
3058 return (EFAULT);
3060 striocp = (struct strioctl *)to;
3061 striocp->ic_cmd = strioc32.ic_cmd;
3062 striocp->ic_timout = strioc32.ic_timout;
3063 striocp->ic_len = strioc32.ic_len;
3064 striocp->ic_dp = (char *)(uintptr_t)strioc32.ic_dp;
3066 } else { /* NATIVE data model */
3067 if (copyin(from, to, sizeof (struct strioctl))) {
3068 return (EFAULT);
3069 } else {
3070 return (0);
3073 } else {
3074 ASSERT(copyflag & K_TO_K);
3075 bcopy(from, to, sizeof (struct strioctl));
3077 return (0);
3080 static int
3081 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3083 struct strioctl32 strioc32;
3084 struct strioctl *striocp;
3086 if (copyflag & U_TO_K) {
3087 ASSERT((copyflag & K_TO_K) == 0);
3089 if ((flag & FMODELS) == DATAMODEL_ILP32) {
3090 striocp = (struct strioctl *)from;
3091 strioc32.ic_cmd = striocp->ic_cmd;
3092 strioc32.ic_timout = striocp->ic_timout;
3093 strioc32.ic_len = striocp->ic_len;
3094 strioc32.ic_dp = (caddr32_t)(uintptr_t)striocp->ic_dp;
3095 ASSERT((char *)(uintptr_t)strioc32.ic_dp ==
3096 striocp->ic_dp);
3098 if (copyout(&strioc32, to, sizeof (strioc32)))
3099 return (EFAULT);
3101 } else { /* NATIVE data model */
3102 if (copyout(from, to, sizeof (struct strioctl))) {
3103 return (EFAULT);
3104 } else {
3105 return (0);
3108 } else {
3109 ASSERT(copyflag & K_TO_K);
3110 bcopy(from, to, sizeof (struct strioctl));
3112 return (0);
3115 #else /* ! _LP64 */
3117 /* ARGSUSED2 */
3118 static int
3119 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3121 return (strcopyin(from, to, sizeof (struct strioctl), copyflag));
3124 /* ARGSUSED2 */
3125 static int
3126 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3128 return (strcopyout(from, to, sizeof (struct strioctl), copyflag));
3131 #endif /* _LP64 */
3134 * Determine type of job control semantics expected by user. The
3135 * possibilities are:
3136 * JCREAD - Behaves like read() on fd; send SIGTTIN
3137 * JCWRITE - Behaves like write() on fd; send SIGTTOU if TOSTOP set
3138 * JCSETP - Sets a value in the stream; send SIGTTOU, ignore TOSTOP
3139 * JCGETP - Gets a value in the stream; no signals.
3140 * See straccess in strsubr.c for usage of these values.
3142 * This routine also returns -1 for I_STR as a special case; the
3143 * caller must call again with the real ioctl number for
3144 * classification.
3146 static int
3147 job_control_type(int cmd)
3149 switch (cmd) {
3150 case I_STR:
3151 return (-1);
3153 case I_RECVFD:
3154 case I_E_RECVFD:
3155 return (JCREAD);
3157 case I_FDINSERT:
3158 case I_SENDFD:
3159 return (JCWRITE);
3161 case TCSETA:
3162 case TCSETAW:
3163 case TCSETAF:
3164 case TCSBRK:
3165 case TCXONC:
3166 case TCFLSH:
3167 case TCDSET: /* Obsolete */
3168 case TIOCSWINSZ:
3169 case TCSETS:
3170 case TCSETSW:
3171 case TCSETSF:
3172 case TIOCSETD:
3173 case TIOCHPCL:
3174 case TIOCSETP:
3175 case TIOCSETN:
3176 case TIOCEXCL:
3177 case TIOCNXCL:
3178 case TIOCFLUSH:
3179 case TIOCSETC:
3180 case TIOCLBIS:
3181 case TIOCLBIC:
3182 case TIOCLSET:
3183 case TIOCSBRK:
3184 case TIOCCBRK:
3185 case TIOCSDTR:
3186 case TIOCCDTR:
3187 case TIOCSLTC:
3188 case TIOCSTOP:
3189 case TIOCSTART:
3190 case TIOCSTI:
3191 case TIOCSPGRP:
3192 case TIOCMSET:
3193 case TIOCMBIS:
3194 case TIOCMBIC:
3195 case TIOCREMOTE:
3196 case TIOCSIGNAL:
3197 case LDSETT:
3198 case LDSMAP: /* Obsolete */
3199 case DIOCSETP:
3200 case I_FLUSH:
3201 case I_SRDOPT:
3202 case I_SETSIG:
3203 case I_SWROPT:
3204 case I_FLUSHBAND:
3205 case I_SETCLTIME:
3206 case I_SERROPT:
3207 case I_ESETSIG:
3208 case FIONBIO:
3209 case FIOASYNC:
3210 case FIOSETOWN:
3211 case JBOOT: /* Obsolete */
3212 case JTERM: /* Obsolete */
3213 case JTIMOM: /* Obsolete */
3214 case JZOMBOOT: /* Obsolete */
3215 case JAGENT: /* Obsolete */
3216 case JTRUN: /* Obsolete */
3217 case JXTPROTO: /* Obsolete */
3218 return (JCSETP);
3221 return (JCGETP);
3225 * ioctl for streams
3228 strioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, int copyflag,
3229 cred_t *crp, int *rvalp)
3231 struct stdata *stp;
3232 struct strcmd *scp;
3233 struct strioctl strioc;
3234 struct uio uio;
3235 struct iovec iov;
3236 int access;
3237 mblk_t *mp;
3238 int error = 0;
3239 int done = 0;
3240 ssize_t rmin, rmax;
3241 queue_t *wrq;
3242 queue_t *rdq;
3243 boolean_t kioctl = B_FALSE;
3244 uint32_t auditing = AU_AUDITING();
3246 if (flag & FKIOCTL) {
3247 copyflag = K_TO_K;
3248 kioctl = B_TRUE;
3250 ASSERT(vp->v_stream);
3251 ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
3252 stp = vp->v_stream;
3254 TRACE_3(TR_FAC_STREAMS_FR, TR_IOCTL_ENTER,
3255 "strioctl:stp %p cmd %X arg %lX", stp, cmd, arg);
3258 * If the copy is kernel to kernel, make sure that the FNATIVE
3259 * flag is set. After this it would be a serious error to have
3260 * no model flag.
3262 if (copyflag == K_TO_K)
3263 flag = (flag & ~FMODELS) | FNATIVE;
3265 ASSERT((flag & FMODELS) != 0);
3267 wrq = stp->sd_wrq;
3268 rdq = _RD(wrq);
3270 access = job_control_type(cmd);
3272 /* We should never see these here, should be handled by iwscn */
3273 if (cmd == SRIOCSREDIR || cmd == SRIOCISREDIR)
3274 return (EINVAL);
3276 mutex_enter(&stp->sd_lock);
3277 if ((access != -1) && ((error = i_straccess(stp, access)) != 0)) {
3278 mutex_exit(&stp->sd_lock);
3279 return (error);
3281 mutex_exit(&stp->sd_lock);
3284 * Check for sgttyb-related ioctls first, and complain as
3285 * necessary.
3287 switch (cmd) {
3288 case TIOCGETP:
3289 case TIOCSETP:
3290 case TIOCSETN:
3291 if (sgttyb_handling >= 2 && !sgttyb_complaint) {
3292 sgttyb_complaint = B_TRUE;
3293 cmn_err(CE_NOTE,
3294 "application used obsolete TIOC[GS]ET");
3296 if (sgttyb_handling >= 3) {
3297 tsignal(curthread, SIGSYS);
3298 return (EIO);
3300 break;
3303 mutex_enter(&stp->sd_lock);
3305 switch (cmd) {
3306 case I_RECVFD:
3307 case I_E_RECVFD:
3308 case I_PEEK:
3309 case I_NREAD:
3310 case FIONREAD:
3311 case FIORDCHK:
3312 case I_ATMARK:
3313 case FIONBIO:
3314 case FIOASYNC:
3315 if (stp->sd_flag & (STRDERR|STPLEX)) {
3316 error = strgeterr(stp, STRDERR|STPLEX, 0);
3317 if (error != 0) {
3318 mutex_exit(&stp->sd_lock);
3319 return (error);
3322 break;
3324 default:
3325 if (stp->sd_flag & (STRDERR|STWRERR|STPLEX)) {
3326 error = strgeterr(stp, STRDERR|STWRERR|STPLEX, 0);
3327 if (error != 0) {
3328 mutex_exit(&stp->sd_lock);
3329 return (error);
3334 mutex_exit(&stp->sd_lock);
3336 switch (cmd) {
3337 default:
3339 * The stream head has hardcoded knowledge of a
3340 * miscellaneous collection of terminal-, keyboard- and
3341 * mouse-related ioctls, enumerated below. This hardcoded
3342 * knowledge allows the stream head to automatically
3343 * convert transparent ioctl requests made by userland
3344 * programs into I_STR ioctls which many old STREAMS
3345 * modules and drivers require.
3347 * No new ioctls should ever be added to this list.
3348 * Instead, the STREAMS module or driver should be written
3349 * to either handle transparent ioctls or require any
3350 * userland programs to use I_STR ioctls (by returning
3351 * EINVAL to any transparent ioctl requests).
3353 * More importantly, removing ioctls from this list should
3354 * be done with the utmost care, since our STREAMS modules
3355 * and drivers *count* on the stream head performing this
3356 * conversion, and thus may panic while processing
3357 * transparent ioctl request for one of these ioctls (keep
3358 * in mind that third party modules and drivers may have
3359 * similar problems).
3361 if (((cmd & IOCTYPE) == LDIOC) ||
3362 ((cmd & IOCTYPE) == tIOC) ||
3363 ((cmd & IOCTYPE) == TIOC) ||
3364 ((cmd & IOCTYPE) == KIOC) ||
3365 ((cmd & IOCTYPE) == MSIOC) ||
3366 ((cmd & IOCTYPE) == VUIOC)) {
3368 * The ioctl is a tty ioctl - set up strioc buffer
3369 * and call strdoioctl() to do the work.
3371 if (stp->sd_flag & STRHUP)
3372 return (ENXIO);
3373 strioc.ic_cmd = cmd;
3374 strioc.ic_timout = INFTIM;
3376 switch (cmd) {
3378 case TCXONC:
3379 case TCSBRK:
3380 case TCFLSH:
3381 case TCDSET:
3383 int native_arg = (int)arg;
3384 strioc.ic_len = sizeof (int);
3385 strioc.ic_dp = (char *)&native_arg;
3386 return (strdoioctl(stp, &strioc, flag,
3387 K_TO_K, crp, rvalp));
3390 case TCSETA:
3391 case TCSETAW:
3392 case TCSETAF:
3393 strioc.ic_len = sizeof (struct termio);
3394 strioc.ic_dp = (char *)arg;
3395 return (strdoioctl(stp, &strioc, flag,
3396 copyflag, crp, rvalp));
3398 case TCSETS:
3399 case TCSETSW:
3400 case TCSETSF:
3401 strioc.ic_len = sizeof (struct termios);
3402 strioc.ic_dp = (char *)arg;
3403 return (strdoioctl(stp, &strioc, flag,
3404 copyflag, crp, rvalp));
3406 case LDSETT:
3407 strioc.ic_len = sizeof (struct termcb);
3408 strioc.ic_dp = (char *)arg;
3409 return (strdoioctl(stp, &strioc, flag,
3410 copyflag, crp, rvalp));
3412 case TIOCSETP:
3413 strioc.ic_len = sizeof (struct sgttyb);
3414 strioc.ic_dp = (char *)arg;
3415 return (strdoioctl(stp, &strioc, flag,
3416 copyflag, crp, rvalp));
3418 case TIOCSTI:
3419 if ((flag & FREAD) == 0 &&
3420 secpolicy_sti(crp) != 0) {
3421 return (EPERM);
3423 mutex_enter(&stp->sd_lock);
3424 mutex_enter(&curproc->p_splock);
3425 if (stp->sd_sidp != curproc->p_sessp->s_sidp &&
3426 secpolicy_sti(crp) != 0) {
3427 mutex_exit(&curproc->p_splock);
3428 mutex_exit(&stp->sd_lock);
3429 return (EACCES);
3431 mutex_exit(&curproc->p_splock);
3432 mutex_exit(&stp->sd_lock);
3434 strioc.ic_len = sizeof (char);
3435 strioc.ic_dp = (char *)arg;
3436 return (strdoioctl(stp, &strioc, flag,
3437 copyflag, crp, rvalp));
3439 case TIOCSWINSZ:
3440 strioc.ic_len = sizeof (struct winsize);
3441 strioc.ic_dp = (char *)arg;
3442 return (strdoioctl(stp, &strioc, flag,
3443 copyflag, crp, rvalp));
3445 case TIOCSSIZE:
3446 strioc.ic_len = sizeof (struct ttysize);
3447 strioc.ic_dp = (char *)arg;
3448 return (strdoioctl(stp, &strioc, flag,
3449 copyflag, crp, rvalp));
3451 case TIOCSSOFTCAR:
3452 case KIOCTRANS:
3453 case KIOCTRANSABLE:
3454 case KIOCCMD:
3455 case KIOCSDIRECT:
3456 case KIOCSCOMPAT:
3457 case KIOCSKABORTEN:
3458 case KIOCSRPTDELAY:
3459 case KIOCSRPTRATE:
3460 case VUIDSFORMAT:
3461 case TIOCSPPS:
3462 strioc.ic_len = sizeof (int);
3463 strioc.ic_dp = (char *)arg;
3464 return (strdoioctl(stp, &strioc, flag,
3465 copyflag, crp, rvalp));
3467 case KIOCSETKEY:
3468 case KIOCGETKEY:
3469 strioc.ic_len = sizeof (struct kiockey);
3470 strioc.ic_dp = (char *)arg;
3471 return (strdoioctl(stp, &strioc, flag,
3472 copyflag, crp, rvalp));
3474 case KIOCSKEY:
3475 case KIOCGKEY:
3476 strioc.ic_len = sizeof (struct kiockeymap);
3477 strioc.ic_dp = (char *)arg;
3478 return (strdoioctl(stp, &strioc, flag,
3479 copyflag, crp, rvalp));
3481 case KIOCSLED:
3482 /* arg is a pointer to char */
3483 strioc.ic_len = sizeof (char);
3484 strioc.ic_dp = (char *)arg;
3485 return (strdoioctl(stp, &strioc, flag,
3486 copyflag, crp, rvalp));
3488 case MSIOSETPARMS:
3489 strioc.ic_len = sizeof (Ms_parms);
3490 strioc.ic_dp = (char *)arg;
3491 return (strdoioctl(stp, &strioc, flag,
3492 copyflag, crp, rvalp));
3494 case VUIDSADDR:
3495 case VUIDGADDR:
3496 strioc.ic_len = sizeof (struct vuid_addr_probe);
3497 strioc.ic_dp = (char *)arg;
3498 return (strdoioctl(stp, &strioc, flag,
3499 copyflag, crp, rvalp));
3502 * These M_IOCTL's don't require any data to be sent
3503 * downstream, and the driver will allocate and link
3504 * on its own mblk_t upon M_IOCACK -- thus we set
3505 * ic_len to zero and set ic_dp to arg so we know
3506 * where to copyout to later.
3508 case TIOCGSOFTCAR:
3509 case TIOCGWINSZ:
3510 case TIOCGSIZE:
3511 case KIOCGTRANS:
3512 case KIOCGTRANSABLE:
3513 case KIOCTYPE:
3514 case KIOCGDIRECT:
3515 case KIOCGCOMPAT:
3516 case KIOCLAYOUT:
3517 case KIOCGLED:
3518 case MSIOGETPARMS:
3519 case MSIOBUTTONS:
3520 case VUIDGFORMAT:
3521 case TIOCGPPS:
3522 case TIOCGPPSEV:
3523 case TCGETA:
3524 case TCGETS:
3525 case LDGETT:
3526 case TIOCGETP:
3527 case KIOCGRPTDELAY:
3528 case KIOCGRPTRATE:
3529 strioc.ic_len = 0;
3530 strioc.ic_dp = (char *)arg;
3531 return (strdoioctl(stp, &strioc, flag,
3532 copyflag, crp, rvalp));
3537 * Unknown cmd - send it down as a transparent ioctl.
3539 strioc.ic_cmd = cmd;
3540 strioc.ic_timout = INFTIM;
3541 strioc.ic_len = TRANSPARENT;
3542 strioc.ic_dp = (char *)&arg;
3544 return (strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp));
3546 case I_STR:
3548 * Stream ioctl. Read in an strioctl buffer from the user
3549 * along with any data specified and send it downstream.
3550 * Strdoioctl will wait allow only one ioctl message at
3551 * a time, and waits for the acknowledgement.
3554 if (stp->sd_flag & STRHUP)
3555 return (ENXIO);
3557 error = strcopyin_strioctl((void *)arg, &strioc, flag,
3558 copyflag);
3559 if (error != 0)
3560 return (error);
3562 if ((strioc.ic_len < 0) || (strioc.ic_timout < -1))
3563 return (EINVAL);
3565 access = job_control_type(strioc.ic_cmd);
3566 mutex_enter(&stp->sd_lock);
3567 if ((access != -1) &&
3568 ((error = i_straccess(stp, access)) != 0)) {
3569 mutex_exit(&stp->sd_lock);
3570 return (error);
3572 mutex_exit(&stp->sd_lock);
3575 * The I_STR facility provides a trap door for malicious
3576 * code to send down bogus streamio(7I) ioctl commands to
3577 * unsuspecting STREAMS modules and drivers which expect to
3578 * only get these messages from the stream head.
3579 * Explicitly prohibit any streamio ioctls which can be
3580 * passed downstream by the stream head. Note that we do
3581 * not block all streamio ioctls because the ioctl
3582 * numberspace is not well managed and thus it's possible
3583 * that a module or driver's ioctl numbers may accidentally
3584 * collide with them.
3586 switch (strioc.ic_cmd) {
3587 case I_LINK:
3588 case I_PLINK:
3589 case I_UNLINK:
3590 case I_PUNLINK:
3591 case _I_GETPEERCRED:
3592 case _I_PLINK_LH:
3593 return (EINVAL);
3596 error = strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp);
3597 if (error == 0) {
3598 error = strcopyout_strioctl(&strioc, (void *)arg,
3599 flag, copyflag);
3601 return (error);
3603 case _I_CMD:
3605 * Like I_STR, but without using M_IOC* messages and without
3606 * copyins/copyouts beyond the passed-in argument.
3608 if (stp->sd_flag & STRHUP)
3609 return (ENXIO);
3611 if ((scp = kmem_alloc(sizeof (strcmd_t), KM_NOSLEEP)) == NULL)
3612 return (ENOMEM);
3614 if (copyin((void *)arg, scp, sizeof (strcmd_t))) {
3615 kmem_free(scp, sizeof (strcmd_t));
3616 return (EFAULT);
3619 access = job_control_type(scp->sc_cmd);
3620 mutex_enter(&stp->sd_lock);
3621 if (access != -1 && (error = i_straccess(stp, access)) != 0) {
3622 mutex_exit(&stp->sd_lock);
3623 kmem_free(scp, sizeof (strcmd_t));
3624 return (error);
3626 mutex_exit(&stp->sd_lock);
3628 *rvalp = 0;
3629 if ((error = strdocmd(stp, scp, crp)) == 0) {
3630 if (copyout(scp, (void *)arg, sizeof (strcmd_t)))
3631 error = EFAULT;
3633 kmem_free(scp, sizeof (strcmd_t));
3634 return (error);
3636 case I_NREAD:
3638 * Return number of bytes of data in first message
3639 * in queue in "arg" and return the number of messages
3640 * in queue in return value.
3643 size_t size;
3644 int retval;
3645 int count = 0;
3647 mutex_enter(QLOCK(rdq));
3649 size = msgdsize(rdq->q_first);
3650 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3651 count++;
3653 mutex_exit(QLOCK(rdq));
3654 if (stp->sd_struiordq) {
3655 infod_t infod;
3657 infod.d_cmd = INFOD_COUNT;
3658 infod.d_count = 0;
3659 if (count == 0) {
3660 infod.d_cmd |= INFOD_FIRSTBYTES;
3661 infod.d_bytes = 0;
3663 infod.d_res = 0;
3664 (void) infonext(rdq, &infod);
3665 count += infod.d_count;
3666 if (infod.d_res & INFOD_FIRSTBYTES)
3667 size = infod.d_bytes;
3671 * Drop down from size_t to the "int" required by the
3672 * interface. Cap at INT_MAX.
3674 retval = MIN(size, INT_MAX);
3675 error = strcopyout(&retval, (void *)arg, sizeof (retval),
3676 copyflag);
3677 if (!error)
3678 *rvalp = count;
3679 return (error);
3682 case FIONREAD:
3684 * Return number of bytes of data in all data messages
3685 * in queue in "arg".
3688 size_t size = 0;
3689 int retval;
3691 mutex_enter(QLOCK(rdq));
3692 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3693 size += msgdsize(mp);
3694 mutex_exit(QLOCK(rdq));
3696 if (stp->sd_struiordq) {
3697 infod_t infod;
3699 infod.d_cmd = INFOD_BYTES;
3700 infod.d_res = 0;
3701 infod.d_bytes = 0;
3702 (void) infonext(rdq, &infod);
3703 size += infod.d_bytes;
3707 * Drop down from size_t to the "int" required by the
3708 * interface. Cap at INT_MAX.
3710 retval = MIN(size, INT_MAX);
3711 error = strcopyout(&retval, (void *)arg, sizeof (retval),
3712 copyflag);
3714 *rvalp = 0;
3715 return (error);
3717 case FIORDCHK:
3719 * FIORDCHK does not use arg value (like FIONREAD),
3720 * instead a count is returned. I_NREAD value may
3721 * not be accurate but safe. The real thing to do is
3722 * to add the msgdsizes of all data messages until
3723 * a non-data message.
3726 size_t size = 0;
3728 mutex_enter(QLOCK(rdq));
3729 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3730 size += msgdsize(mp);
3731 mutex_exit(QLOCK(rdq));
3733 if (stp->sd_struiordq) {
3734 infod_t infod;
3736 infod.d_cmd = INFOD_BYTES;
3737 infod.d_res = 0;
3738 infod.d_bytes = 0;
3739 (void) infonext(rdq, &infod);
3740 size += infod.d_bytes;
3744 * Since ioctl returns an int, and memory sizes under
3745 * LP64 may not fit, we return INT_MAX if the count was
3746 * actually greater.
3748 *rvalp = MIN(size, INT_MAX);
3749 return (0);
3752 case I_FIND:
3754 * Get module name.
3757 char mname[FMNAMESZ + 1];
3758 queue_t *q;
3760 error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3761 mname, FMNAMESZ + 1, NULL);
3762 if (error)
3763 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3766 * Return EINVAL if we're handed a bogus module name.
3768 if (fmodsw_find(mname, FMODSW_LOAD) == NULL) {
3769 TRACE_0(TR_FAC_STREAMS_FR,
3770 TR_I_CANT_FIND, "couldn't I_FIND");
3771 return (EINVAL);
3774 *rvalp = 0;
3776 /* Look downstream to see if module is there. */
3777 claimstr(stp->sd_wrq);
3778 for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
3779 if (q->q_flag & QREADR) {
3780 q = NULL;
3781 break;
3783 if (strcmp(mname, Q2NAME(q)) == 0)
3784 break;
3786 releasestr(stp->sd_wrq);
3788 *rvalp = (q ? 1 : 0);
3789 return (error);
3792 case I_PUSH:
3793 case __I_PUSH_NOCTTY:
3795 * Push a module.
3796 * For the case __I_PUSH_NOCTTY push a module but
3797 * do not allocate controlling tty. See bugid 4025044
3801 char mname[FMNAMESZ + 1];
3802 fmodsw_impl_t *fp;
3803 dev_t dummydev;
3805 if (stp->sd_flag & STRHUP)
3806 return (ENXIO);
3809 * Get module name and look up in fmodsw.
3811 error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3812 mname, FMNAMESZ + 1, NULL);
3813 if (error)
3814 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3816 if ((fp = fmodsw_find(mname, FMODSW_HOLD | FMODSW_LOAD)) ==
3817 NULL)
3818 return (EINVAL);
3820 TRACE_2(TR_FAC_STREAMS_FR, TR_I_PUSH,
3821 "I_PUSH:fp %p stp %p", fp, stp);
3824 * If the module is flagged as single-instance, then check
3825 * to see if the module is already pushed. If it is, return
3826 * as if the push was successful.
3828 if (fp->f_qflag & _QSINGLE_INSTANCE) {
3829 queue_t *q;
3831 claimstr(stp->sd_wrq);
3832 for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
3833 if (q->q_flag & QREADR) {
3834 q = NULL;
3835 break;
3837 if (strcmp(mname, Q2NAME(q)) == 0)
3838 break;
3840 releasestr(stp->sd_wrq);
3841 if (q != NULL) {
3842 fmodsw_rele(fp);
3843 return (0);
3847 if (error = strstartplumb(stp, flag, cmd)) {
3848 fmodsw_rele(fp);
3849 return (error);
3853 * See if any more modules can be pushed on this stream.
3854 * Note that this check must be done after strstartplumb()
3855 * since otherwise multiple threads issuing I_PUSHes on
3856 * the same stream will be able to exceed nstrpush.
3858 mutex_enter(&stp->sd_lock);
3859 if (stp->sd_pushcnt >= nstrpush) {
3860 fmodsw_rele(fp);
3861 strendplumb(stp);
3862 mutex_exit(&stp->sd_lock);
3863 return (EINVAL);
3865 mutex_exit(&stp->sd_lock);
3868 * Push new module and call its open routine
3869 * via qattach(). Modules don't change device
3870 * numbers, so just ignore dummydev here.
3872 dummydev = vp->v_rdev;
3873 if ((error = qattach(rdq, &dummydev, 0, crp, fp,
3874 B_FALSE)) == 0) {
3875 if (vp->v_type == VCHR && /* sorry, no pipes allowed */
3876 (cmd == I_PUSH) && (stp->sd_flag & STRISTTY)) {
3878 * try to allocate it as a controlling terminal
3880 (void) strctty(stp);
3884 mutex_enter(&stp->sd_lock);
3887 * As a performance concern we are caching the values of
3888 * q_minpsz and q_maxpsz of the module below the stream
3889 * head in the stream head.
3891 mutex_enter(QLOCK(stp->sd_wrq->q_next));
3892 rmin = stp->sd_wrq->q_next->q_minpsz;
3893 rmax = stp->sd_wrq->q_next->q_maxpsz;
3894 mutex_exit(QLOCK(stp->sd_wrq->q_next));
3896 /* Do this processing here as a performance concern */
3897 if (strmsgsz != 0) {
3898 if (rmax == INFPSZ)
3899 rmax = strmsgsz;
3900 else {
3901 if (vp->v_type == VFIFO)
3902 rmax = MIN(PIPE_BUF, rmax);
3903 else rmax = MIN(strmsgsz, rmax);
3907 mutex_enter(QLOCK(wrq));
3908 stp->sd_qn_minpsz = rmin;
3909 stp->sd_qn_maxpsz = rmax;
3910 mutex_exit(QLOCK(wrq));
3912 strendplumb(stp);
3913 mutex_exit(&stp->sd_lock);
3914 return (error);
3917 case I_POP:
3919 queue_t *q;
3921 if (stp->sd_flag & STRHUP)
3922 return (ENXIO);
3923 if (!wrq->q_next) /* for broken pipes */
3924 return (EINVAL);
3926 if (error = strstartplumb(stp, flag, cmd))
3927 return (error);
3930 * If there is an anchor on this stream and popping
3931 * the current module would attempt to pop through the
3932 * anchor, then disallow the pop unless we have sufficient
3933 * privileges; take the cheapest (non-locking) check
3934 * first.
3936 if (secpolicy_ip_config(crp, B_TRUE) != 0 ||
3937 (stp->sd_anchorzone != crgetzoneid(crp))) {
3938 mutex_enter(&stp->sd_lock);
3940 * Anchors only apply if there's at least one
3941 * module on the stream (sd_pushcnt > 0).
3943 if (stp->sd_pushcnt > 0 &&
3944 stp->sd_pushcnt == stp->sd_anchor &&
3945 stp->sd_vnode->v_type != VFIFO) {
3946 strendplumb(stp);
3947 mutex_exit(&stp->sd_lock);
3948 if (stp->sd_anchorzone != crgetzoneid(crp))
3949 return (EINVAL);
3950 /* Audit and report error */
3951 return (secpolicy_ip_config(crp, B_FALSE));
3953 mutex_exit(&stp->sd_lock);
3956 q = wrq->q_next;
3957 TRACE_2(TR_FAC_STREAMS_FR, TR_I_POP,
3958 "I_POP:%p from %p", q, stp);
3959 if (q->q_next == NULL || (q->q_flag & (QREADR|QISDRV))) {
3960 error = EINVAL;
3961 } else {
3962 qdetach(_RD(q), 1, flag, crp, B_FALSE);
3963 error = 0;
3965 mutex_enter(&stp->sd_lock);
3968 * As a performance concern we are caching the values of
3969 * q_minpsz and q_maxpsz of the module below the stream
3970 * head in the stream head.
3972 mutex_enter(QLOCK(wrq->q_next));
3973 rmin = wrq->q_next->q_minpsz;
3974 rmax = wrq->q_next->q_maxpsz;
3975 mutex_exit(QLOCK(wrq->q_next));
3977 /* Do this processing here as a performance concern */
3978 if (strmsgsz != 0) {
3979 if (rmax == INFPSZ)
3980 rmax = strmsgsz;
3981 else {
3982 if (vp->v_type == VFIFO)
3983 rmax = MIN(PIPE_BUF, rmax);
3984 else rmax = MIN(strmsgsz, rmax);
3988 mutex_enter(QLOCK(wrq));
3989 stp->sd_qn_minpsz = rmin;
3990 stp->sd_qn_maxpsz = rmax;
3991 mutex_exit(QLOCK(wrq));
3993 /* If we popped through the anchor, then reset the anchor. */
3994 if (stp->sd_pushcnt < stp->sd_anchor) {
3995 stp->sd_anchor = 0;
3996 stp->sd_anchorzone = 0;
3998 strendplumb(stp);
3999 mutex_exit(&stp->sd_lock);
4000 return (error);
4003 case _I_MUXID2FD:
4006 * Create a fd for a I_PLINK'ed lower stream with a given
4007 * muxid. With the fd, application can send down ioctls,
4008 * like I_LIST, to the previously I_PLINK'ed stream. Note
4009 * that after getting the fd, the application has to do an
4010 * I_PUNLINK on the muxid before it can do any operation
4011 * on the lower stream. This is required by spec1170.
4013 * The fd used to do this ioctl should point to the same
4014 * controlling device used to do the I_PLINK. If it uses
4015 * a different stream or an invalid muxid, I_MUXID2FD will
4016 * fail. The error code is set to EINVAL.
4018 * The intended use of this interface is the following.
4019 * An application I_PLINK'ed a stream and exits. The fd
4020 * to the lower stream is gone. Another application
4021 * wants to get a fd to the lower stream, it uses I_MUXID2FD.
4023 int muxid = (int)arg;
4024 int fd;
4025 linkinfo_t *linkp;
4026 struct file *fp;
4027 netstack_t *ns;
4028 str_stack_t *ss;
4031 * Do not allow the wildcard muxid. This ioctl is not
4032 * intended to find arbitrary link.
4034 if (muxid == 0) {
4035 return (EINVAL);
4038 ns = netstack_find_by_cred(crp);
4039 ASSERT(ns != NULL);
4040 ss = ns->netstack_str;
4041 ASSERT(ss != NULL);
4043 mutex_enter(&muxifier);
4044 linkp = findlinks(vp->v_stream, muxid, LINKPERSIST, ss);
4045 if (linkp == NULL) {
4046 mutex_exit(&muxifier);
4047 netstack_rele(ss->ss_netstack);
4048 return (EINVAL);
4051 if ((fd = ufalloc(0)) == -1) {
4052 mutex_exit(&muxifier);
4053 netstack_rele(ss->ss_netstack);
4054 return (EMFILE);
4056 fp = linkp->li_fpdown;
4057 mutex_enter(&fp->f_tlock);
4058 fp->f_count++;
4059 mutex_exit(&fp->f_tlock);
4060 mutex_exit(&muxifier);
4061 setf(fd, fp);
4062 *rvalp = fd;
4063 netstack_rele(ss->ss_netstack);
4064 return (0);
4067 case _I_INSERT:
4070 * To insert a module to a given position in a stream.
4071 * In the first release, only allow privileged user
4072 * to use this ioctl. Furthermore, the insert is only allowed
4073 * below an anchor if the zoneid is the same as the zoneid
4074 * which created the anchor.
4076 * Note that we do not plan to support this ioctl
4077 * on pipes in the first release. We want to learn more
4078 * about the implications of these ioctls before extending
4079 * their support. And we do not think these features are
4080 * valuable for pipes.
4082 STRUCT_DECL(strmodconf, strmodinsert);
4083 char mod_name[FMNAMESZ + 1];
4084 fmodsw_impl_t *fp;
4085 dev_t dummydev;
4086 queue_t *tmp_wrq;
4087 int pos;
4088 boolean_t is_insert;
4090 STRUCT_INIT(strmodinsert, flag);
4091 if (stp->sd_flag & STRHUP)
4092 return (ENXIO);
4093 if (STRMATED(stp))
4094 return (EINVAL);
4095 if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4096 return (error);
4097 if (stp->sd_anchor != 0 &&
4098 stp->sd_anchorzone != crgetzoneid(crp))
4099 return (EINVAL);
4101 error = strcopyin((void *)arg, STRUCT_BUF(strmodinsert),
4102 STRUCT_SIZE(strmodinsert), copyflag);
4103 if (error)
4104 return (error);
4107 * Get module name and look up in fmodsw.
4109 error = (copyflag & U_TO_K ? copyinstr :
4110 copystr)(STRUCT_FGETP(strmodinsert, mod_name),
4111 mod_name, FMNAMESZ + 1, NULL);
4112 if (error)
4113 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4115 if ((fp = fmodsw_find(mod_name, FMODSW_HOLD | FMODSW_LOAD)) ==
4116 NULL)
4117 return (EINVAL);
4119 if (error = strstartplumb(stp, flag, cmd)) {
4120 fmodsw_rele(fp);
4121 return (error);
4125 * Is this _I_INSERT just like an I_PUSH? We need to know
4126 * this because we do some optimizations if this is a
4127 * module being pushed.
4129 pos = STRUCT_FGET(strmodinsert, pos);
4130 is_insert = (pos != 0);
4133 * Make sure pos is valid. Even though it is not an I_PUSH,
4134 * we impose the same limit on the number of modules in a
4135 * stream.
4137 mutex_enter(&stp->sd_lock);
4138 if (stp->sd_pushcnt >= nstrpush || pos < 0 ||
4139 pos > stp->sd_pushcnt) {
4140 fmodsw_rele(fp);
4141 strendplumb(stp);
4142 mutex_exit(&stp->sd_lock);
4143 return (EINVAL);
4145 if (stp->sd_anchor != 0) {
4147 * Is this insert below the anchor?
4148 * Pushcnt hasn't been increased yet hence
4149 * we test for greater than here, and greater or
4150 * equal after qattach.
4152 if (pos > (stp->sd_pushcnt - stp->sd_anchor) &&
4153 stp->sd_anchorzone != crgetzoneid(crp)) {
4154 fmodsw_rele(fp);
4155 strendplumb(stp);
4156 mutex_exit(&stp->sd_lock);
4157 return (EPERM);
4161 mutex_exit(&stp->sd_lock);
4164 * First find the correct position this module to
4165 * be inserted. We don't need to call claimstr()
4166 * as the stream should not be changing at this point.
4168 * Insert new module and call its open routine
4169 * via qattach(). Modules don't change device
4170 * numbers, so just ignore dummydev here.
4172 for (tmp_wrq = stp->sd_wrq; pos > 0;
4173 tmp_wrq = tmp_wrq->q_next, pos--) {
4174 ASSERT(SAMESTR(tmp_wrq));
4176 dummydev = vp->v_rdev;
4177 if ((error = qattach(_RD(tmp_wrq), &dummydev, 0, crp,
4178 fp, is_insert)) != 0) {
4179 mutex_enter(&stp->sd_lock);
4180 strendplumb(stp);
4181 mutex_exit(&stp->sd_lock);
4182 return (error);
4185 mutex_enter(&stp->sd_lock);
4188 * As a performance concern we are caching the values of
4189 * q_minpsz and q_maxpsz of the module below the stream
4190 * head in the stream head.
4192 if (!is_insert) {
4193 mutex_enter(QLOCK(stp->sd_wrq->q_next));
4194 rmin = stp->sd_wrq->q_next->q_minpsz;
4195 rmax = stp->sd_wrq->q_next->q_maxpsz;
4196 mutex_exit(QLOCK(stp->sd_wrq->q_next));
4198 /* Do this processing here as a performance concern */
4199 if (strmsgsz != 0) {
4200 if (rmax == INFPSZ) {
4201 rmax = strmsgsz;
4202 } else {
4203 rmax = MIN(strmsgsz, rmax);
4207 mutex_enter(QLOCK(wrq));
4208 stp->sd_qn_minpsz = rmin;
4209 stp->sd_qn_maxpsz = rmax;
4210 mutex_exit(QLOCK(wrq));
4214 * Need to update the anchor value if this module is
4215 * inserted below the anchor point.
4217 if (stp->sd_anchor != 0) {
4218 pos = STRUCT_FGET(strmodinsert, pos);
4219 if (pos >= (stp->sd_pushcnt - stp->sd_anchor))
4220 stp->sd_anchor++;
4223 strendplumb(stp);
4224 mutex_exit(&stp->sd_lock);
4225 return (0);
4228 case _I_REMOVE:
4231 * To remove a module with a given name in a stream. The
4232 * caller of this ioctl needs to provide both the name and
4233 * the position of the module to be removed. This eliminates
4234 * the ambiguity of removal if a module is inserted/pushed
4235 * multiple times in a stream. In the first release, only
4236 * allow privileged user to use this ioctl.
4237 * Furthermore, the remove is only allowed
4238 * below an anchor if the zoneid is the same as the zoneid
4239 * which created the anchor.
4241 * Note that we do not plan to support this ioctl
4242 * on pipes in the first release. We want to learn more
4243 * about the implications of these ioctls before extending
4244 * their support. And we do not think these features are
4245 * valuable for pipes.
4247 * Also note that _I_REMOVE cannot be used to remove a
4248 * driver or the stream head.
4250 STRUCT_DECL(strmodconf, strmodremove);
4251 queue_t *q;
4252 int pos;
4253 char mod_name[FMNAMESZ + 1];
4254 boolean_t is_remove;
4256 STRUCT_INIT(strmodremove, flag);
4257 if (stp->sd_flag & STRHUP)
4258 return (ENXIO);
4259 if (STRMATED(stp))
4260 return (EINVAL);
4261 if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4262 return (error);
4263 if (stp->sd_anchor != 0 &&
4264 stp->sd_anchorzone != crgetzoneid(crp))
4265 return (EINVAL);
4267 error = strcopyin((void *)arg, STRUCT_BUF(strmodremove),
4268 STRUCT_SIZE(strmodremove), copyflag);
4269 if (error)
4270 return (error);
4272 error = (copyflag & U_TO_K ? copyinstr :
4273 copystr)(STRUCT_FGETP(strmodremove, mod_name),
4274 mod_name, FMNAMESZ + 1, NULL);
4275 if (error)
4276 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4278 if ((error = strstartplumb(stp, flag, cmd)) != 0)
4279 return (error);
4282 * Match the name of given module to the name of module at
4283 * the given position.
4285 pos = STRUCT_FGET(strmodremove, pos);
4287 is_remove = (pos != 0);
4288 for (q = stp->sd_wrq->q_next; SAMESTR(q) && pos > 0;
4289 q = q->q_next, pos--)
4291 if (pos > 0 || !SAMESTR(q) ||
4292 strcmp(Q2NAME(q), mod_name) != 0) {
4293 mutex_enter(&stp->sd_lock);
4294 strendplumb(stp);
4295 mutex_exit(&stp->sd_lock);
4296 return (EINVAL);
4300 * If the position is at or below an anchor, then the zoneid
4301 * must match the zoneid that created the anchor.
4303 if (stp->sd_anchor != 0) {
4304 pos = STRUCT_FGET(strmodremove, pos);
4305 if (pos >= (stp->sd_pushcnt - stp->sd_anchor) &&
4306 stp->sd_anchorzone != crgetzoneid(crp)) {
4307 mutex_enter(&stp->sd_lock);
4308 strendplumb(stp);
4309 mutex_exit(&stp->sd_lock);
4310 return (EPERM);
4315 ASSERT(!(q->q_flag & QREADR));
4316 qdetach(_RD(q), 1, flag, crp, is_remove);
4318 mutex_enter(&stp->sd_lock);
4321 * As a performance concern we are caching the values of
4322 * q_minpsz and q_maxpsz of the module below the stream
4323 * head in the stream head.
4325 if (!is_remove) {
4326 mutex_enter(QLOCK(wrq->q_next));
4327 rmin = wrq->q_next->q_minpsz;
4328 rmax = wrq->q_next->q_maxpsz;
4329 mutex_exit(QLOCK(wrq->q_next));
4331 /* Do this processing here as a performance concern */
4332 if (strmsgsz != 0) {
4333 if (rmax == INFPSZ)
4334 rmax = strmsgsz;
4335 else {
4336 if (vp->v_type == VFIFO)
4337 rmax = MIN(PIPE_BUF, rmax);
4338 else rmax = MIN(strmsgsz, rmax);
4342 mutex_enter(QLOCK(wrq));
4343 stp->sd_qn_minpsz = rmin;
4344 stp->sd_qn_maxpsz = rmax;
4345 mutex_exit(QLOCK(wrq));
4349 * Need to update the anchor value if this module is removed
4350 * at or below the anchor point. If the removed module is at
4351 * the anchor point, remove the anchor for this stream if
4352 * there is no module above the anchor point. Otherwise, if
4353 * the removed module is below the anchor point, decrement the
4354 * anchor point by 1.
4356 if (stp->sd_anchor != 0) {
4357 pos = STRUCT_FGET(strmodremove, pos);
4358 if (pos == stp->sd_pushcnt - stp->sd_anchor + 1)
4359 stp->sd_anchor = 0;
4360 else if (pos > (stp->sd_pushcnt - stp->sd_anchor + 1))
4361 stp->sd_anchor--;
4364 strendplumb(stp);
4365 mutex_exit(&stp->sd_lock);
4366 return (0);
4369 case I_ANCHOR:
4371 * Set the anchor position on the stream to reside at
4372 * the top module (in other words, the top module
4373 * cannot be popped). Anchors with a FIFO make no
4374 * obvious sense, so they're not allowed.
4376 mutex_enter(&stp->sd_lock);
4378 if (stp->sd_vnode->v_type == VFIFO) {
4379 mutex_exit(&stp->sd_lock);
4380 return (EINVAL);
4382 /* Only allow the same zoneid to update the anchor */
4383 if (stp->sd_anchor != 0 &&
4384 stp->sd_anchorzone != crgetzoneid(crp)) {
4385 mutex_exit(&stp->sd_lock);
4386 return (EINVAL);
4388 stp->sd_anchor = stp->sd_pushcnt;
4389 stp->sd_anchorzone = crgetzoneid(crp);
4390 mutex_exit(&stp->sd_lock);
4391 return (0);
4393 case I_LOOK:
4395 * Get name of first module downstream.
4396 * If no module, return an error.
4398 claimstr(wrq);
4399 if (_SAMESTR(wrq) && wrq->q_next->q_next != NULL) {
4400 char *name = Q2NAME(wrq->q_next);
4402 error = strcopyout(name, (void *)arg, strlen(name) + 1,
4403 copyflag);
4404 releasestr(wrq);
4405 return (error);
4407 releasestr(wrq);
4408 return (EINVAL);
4410 case I_LINK:
4411 case I_PLINK:
4413 * Link a multiplexor.
4415 return (mlink(vp, cmd, (int)arg, crp, rvalp, 0));
4417 case _I_PLINK_LH:
4419 * Link a multiplexor: Call must originate from kernel.
4421 if (kioctl)
4422 return (ldi_mlink_lh(vp, cmd, arg, crp, rvalp));
4424 return (EINVAL);
4425 case I_UNLINK:
4426 case I_PUNLINK:
4428 * Unlink a multiplexor.
4429 * If arg is -1, unlink all links for which this is the
4430 * controlling stream. Otherwise, arg is an index number
4431 * for a link to be removed.
4434 struct linkinfo *linkp;
4435 int native_arg = (int)arg;
4436 int type;
4437 netstack_t *ns;
4438 str_stack_t *ss;
4440 TRACE_1(TR_FAC_STREAMS_FR,
4441 TR_I_UNLINK, "I_UNLINK/I_PUNLINK:%p", stp);
4442 if (vp->v_type == VFIFO) {
4443 return (EINVAL);
4445 if (cmd == I_UNLINK)
4446 type = LINKNORMAL;
4447 else /* I_PUNLINK */
4448 type = LINKPERSIST;
4449 if (native_arg == 0) {
4450 return (EINVAL);
4452 ns = netstack_find_by_cred(crp);
4453 ASSERT(ns != NULL);
4454 ss = ns->netstack_str;
4455 ASSERT(ss != NULL);
4457 if (native_arg == MUXID_ALL)
4458 error = munlinkall(stp, type, crp, rvalp, ss);
4459 else {
4460 mutex_enter(&muxifier);
4461 if (!(linkp = findlinks(stp, (int)arg, type, ss))) {
4462 /* invalid user supplied index number */
4463 mutex_exit(&muxifier);
4464 netstack_rele(ss->ss_netstack);
4465 return (EINVAL);
4467 /* munlink drops the muxifier lock */
4468 error = munlink(stp, linkp, type, crp, rvalp, ss);
4470 netstack_rele(ss->ss_netstack);
4471 return (error);
4474 case I_FLUSH:
4476 * send a flush message downstream
4477 * flush message can indicate
4478 * FLUSHR - flush read queue
4479 * FLUSHW - flush write queue
4480 * FLUSHRW - flush read/write queue
4482 if (stp->sd_flag & STRHUP)
4483 return (ENXIO);
4484 if (arg & ~FLUSHRW)
4485 return (EINVAL);
4487 for (;;) {
4488 if (putnextctl1(stp->sd_wrq, M_FLUSH, (int)arg)) {
4489 break;
4491 if (error = strwaitbuf(1, BPRI_HI)) {
4492 return (error);
4497 * Send down an unsupported ioctl and wait for the nack
4498 * in order to allow the M_FLUSH to propagate back
4499 * up to the stream head.
4500 * Replaces if (qready()) runqueues();
4502 strioc.ic_cmd = -1; /* The unsupported ioctl */
4503 strioc.ic_timout = 0;
4504 strioc.ic_len = 0;
4505 strioc.ic_dp = NULL;
4506 (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4507 *rvalp = 0;
4508 return (0);
4510 case I_FLUSHBAND:
4512 struct bandinfo binfo;
4514 error = strcopyin((void *)arg, &binfo, sizeof (binfo),
4515 copyflag);
4516 if (error)
4517 return (error);
4518 if (stp->sd_flag & STRHUP)
4519 return (ENXIO);
4520 if (binfo.bi_flag & ~FLUSHRW)
4521 return (EINVAL);
4522 while (!(mp = allocb(2, BPRI_HI))) {
4523 if (error = strwaitbuf(2, BPRI_HI))
4524 return (error);
4526 mp->b_datap->db_type = M_FLUSH;
4527 *mp->b_wptr++ = binfo.bi_flag | FLUSHBAND;
4528 *mp->b_wptr++ = binfo.bi_pri;
4529 putnext(stp->sd_wrq, mp);
4531 * Send down an unsupported ioctl and wait for the nack
4532 * in order to allow the M_FLUSH to propagate back
4533 * up to the stream head.
4534 * Replaces if (qready()) runqueues();
4536 strioc.ic_cmd = -1; /* The unsupported ioctl */
4537 strioc.ic_timout = 0;
4538 strioc.ic_len = 0;
4539 strioc.ic_dp = NULL;
4540 (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4541 *rvalp = 0;
4542 return (0);
4545 case I_SRDOPT:
4547 * Set read options
4549 * RNORM - default stream mode
4550 * RMSGN - message no discard
4551 * RMSGD - message discard
4552 * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
4553 * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
4554 * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
4556 if (arg & ~(RMODEMASK | RPROTMASK))
4557 return (EINVAL);
4559 if ((arg & (RMSGD|RMSGN)) == (RMSGD|RMSGN))
4560 return (EINVAL);
4562 mutex_enter(&stp->sd_lock);
4563 switch (arg & RMODEMASK) {
4564 case RNORM:
4565 stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
4566 break;
4567 case RMSGD:
4568 stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGNODIS) |
4569 RD_MSGDIS;
4570 break;
4571 case RMSGN:
4572 stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGDIS) |
4573 RD_MSGNODIS;
4574 break;
4577 switch (arg & RPROTMASK) {
4578 case RPROTNORM:
4579 stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
4580 break;
4582 case RPROTDAT:
4583 stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDIS) |
4584 RD_PROTDAT);
4585 break;
4587 case RPROTDIS:
4588 stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDAT) |
4589 RD_PROTDIS);
4590 break;
4592 mutex_exit(&stp->sd_lock);
4593 return (0);
4595 case I_GRDOPT:
4597 * Get read option and return the value
4598 * to spot pointed to by arg
4601 int rdopt;
4603 rdopt = ((stp->sd_read_opt & RD_MSGDIS) ? RMSGD :
4604 ((stp->sd_read_opt & RD_MSGNODIS) ? RMSGN : RNORM));
4605 rdopt |= ((stp->sd_read_opt & RD_PROTDAT) ? RPROTDAT :
4606 ((stp->sd_read_opt & RD_PROTDIS) ? RPROTDIS : RPROTNORM));
4608 return (strcopyout(&rdopt, (void *)arg, sizeof (int),
4609 copyflag));
4612 case I_SERROPT:
4614 * Set error options
4616 * RERRNORM - persistent read errors
4617 * RERRNONPERSIST - non-persistent read errors
4618 * WERRNORM - persistent write errors
4619 * WERRNONPERSIST - non-persistent write errors
4621 if (arg & ~(RERRMASK | WERRMASK))
4622 return (EINVAL);
4624 mutex_enter(&stp->sd_lock);
4625 switch (arg & RERRMASK) {
4626 case RERRNORM:
4627 stp->sd_flag &= ~STRDERRNONPERSIST;
4628 break;
4629 case RERRNONPERSIST:
4630 stp->sd_flag |= STRDERRNONPERSIST;
4631 break;
4633 switch (arg & WERRMASK) {
4634 case WERRNORM:
4635 stp->sd_flag &= ~STWRERRNONPERSIST;
4636 break;
4637 case WERRNONPERSIST:
4638 stp->sd_flag |= STWRERRNONPERSIST;
4639 break;
4641 mutex_exit(&stp->sd_lock);
4642 return (0);
4644 case I_GERROPT:
4646 * Get error option and return the value
4647 * to spot pointed to by arg
4650 int erropt = 0;
4652 erropt |= (stp->sd_flag & STRDERRNONPERSIST) ? RERRNONPERSIST :
4653 RERRNORM;
4654 erropt |= (stp->sd_flag & STWRERRNONPERSIST) ? WERRNONPERSIST :
4655 WERRNORM;
4656 return (strcopyout(&erropt, (void *)arg, sizeof (int),
4657 copyflag));
4660 case I_SETSIG:
4662 * Register the calling proc to receive the SIGPOLL
4663 * signal based on the events given in arg. If
4664 * arg is zero, remove the proc from register list.
4667 strsig_t *ssp, *pssp;
4668 struct pid *pidp;
4670 pssp = NULL;
4671 pidp = curproc->p_pidp;
4673 * Hold sd_lock to prevent traversal of sd_siglist while
4674 * it is modified.
4676 mutex_enter(&stp->sd_lock);
4677 for (ssp = stp->sd_siglist; ssp && (ssp->ss_pidp != pidp);
4678 pssp = ssp, ssp = ssp->ss_next)
4681 if (arg) {
4682 if (arg & ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4683 S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4684 mutex_exit(&stp->sd_lock);
4685 return (EINVAL);
4687 if ((arg & S_BANDURG) && !(arg & S_RDBAND)) {
4688 mutex_exit(&stp->sd_lock);
4689 return (EINVAL);
4693 * If proc not already registered, add it
4694 * to list.
4696 if (!ssp) {
4697 ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4698 ssp->ss_pidp = pidp;
4699 ssp->ss_pid = pidp->pid_id;
4700 ssp->ss_next = NULL;
4701 if (pssp)
4702 pssp->ss_next = ssp;
4703 else
4704 stp->sd_siglist = ssp;
4705 mutex_enter(&pidlock);
4706 PID_HOLD(pidp);
4707 mutex_exit(&pidlock);
4711 * Set events.
4713 ssp->ss_events = (int)arg;
4714 } else {
4716 * Remove proc from register list.
4718 if (ssp) {
4719 mutex_enter(&pidlock);
4720 PID_RELE(pidp);
4721 mutex_exit(&pidlock);
4722 if (pssp)
4723 pssp->ss_next = ssp->ss_next;
4724 else
4725 stp->sd_siglist = ssp->ss_next;
4726 kmem_free(ssp, sizeof (strsig_t));
4727 } else {
4728 mutex_exit(&stp->sd_lock);
4729 return (EINVAL);
4734 * Recalculate OR of sig events.
4736 stp->sd_sigflags = 0;
4737 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4738 stp->sd_sigflags |= ssp->ss_events;
4739 mutex_exit(&stp->sd_lock);
4740 return (0);
4743 case I_GETSIG:
4745 * Return (in arg) the current registration of events
4746 * for which the calling proc is to be signaled.
4749 struct strsig *ssp;
4750 struct pid *pidp;
4752 pidp = curproc->p_pidp;
4753 mutex_enter(&stp->sd_lock);
4754 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4755 if (ssp->ss_pidp == pidp) {
4756 error = strcopyout(&ssp->ss_events, (void *)arg,
4757 sizeof (int), copyflag);
4758 mutex_exit(&stp->sd_lock);
4759 return (error);
4761 mutex_exit(&stp->sd_lock);
4762 return (EINVAL);
4765 case I_ESETSIG:
4767 * Register the ss_pid to receive the SIGPOLL
4768 * signal based on the events is ss_events arg. If
4769 * ss_events is zero, remove the proc from register list.
4772 struct strsig *ssp, *pssp;
4773 struct proc *proc;
4774 struct pid *pidp;
4775 pid_t pid;
4776 struct strsigset ss;
4778 error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4779 if (error)
4780 return (error);
4782 pid = ss.ss_pid;
4784 if (ss.ss_events != 0) {
4786 * Permissions check by sending signal 0.
4787 * Note that when kill fails it does a set_errno
4788 * causing the system call to fail.
4790 error = kill(pid, 0);
4791 if (error) {
4792 return (error);
4795 mutex_enter(&pidlock);
4796 if (pid == 0)
4797 proc = curproc;
4798 else if (pid < 0)
4799 proc = pgfind(-pid);
4800 else
4801 proc = prfind(pid);
4802 if (proc == NULL) {
4803 mutex_exit(&pidlock);
4804 return (ESRCH);
4806 if (pid < 0)
4807 pidp = proc->p_pgidp;
4808 else
4809 pidp = proc->p_pidp;
4810 ASSERT(pidp);
4812 * Get a hold on the pid structure while referencing it.
4813 * There is a separate PID_HOLD should it be inserted
4814 * in the list below.
4816 PID_HOLD(pidp);
4817 mutex_exit(&pidlock);
4819 pssp = NULL;
4821 * Hold sd_lock to prevent traversal of sd_siglist while
4822 * it is modified.
4824 mutex_enter(&stp->sd_lock);
4825 for (ssp = stp->sd_siglist; ssp && (ssp->ss_pid != pid);
4826 pssp = ssp, ssp = ssp->ss_next)
4829 if (ss.ss_events) {
4830 if (ss.ss_events &
4831 ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4832 S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4833 mutex_exit(&stp->sd_lock);
4834 mutex_enter(&pidlock);
4835 PID_RELE(pidp);
4836 mutex_exit(&pidlock);
4837 return (EINVAL);
4839 if ((ss.ss_events & S_BANDURG) &&
4840 !(ss.ss_events & S_RDBAND)) {
4841 mutex_exit(&stp->sd_lock);
4842 mutex_enter(&pidlock);
4843 PID_RELE(pidp);
4844 mutex_exit(&pidlock);
4845 return (EINVAL);
4849 * If proc not already registered, add it
4850 * to list.
4852 if (!ssp) {
4853 ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4854 ssp->ss_pidp = pidp;
4855 ssp->ss_pid = pid;
4856 ssp->ss_next = NULL;
4857 if (pssp)
4858 pssp->ss_next = ssp;
4859 else
4860 stp->sd_siglist = ssp;
4861 mutex_enter(&pidlock);
4862 PID_HOLD(pidp);
4863 mutex_exit(&pidlock);
4867 * Set events.
4869 ssp->ss_events = ss.ss_events;
4870 } else {
4872 * Remove proc from register list.
4874 if (ssp) {
4875 mutex_enter(&pidlock);
4876 PID_RELE(pidp);
4877 mutex_exit(&pidlock);
4878 if (pssp)
4879 pssp->ss_next = ssp->ss_next;
4880 else
4881 stp->sd_siglist = ssp->ss_next;
4882 kmem_free(ssp, sizeof (strsig_t));
4883 } else {
4884 mutex_exit(&stp->sd_lock);
4885 mutex_enter(&pidlock);
4886 PID_RELE(pidp);
4887 mutex_exit(&pidlock);
4888 return (EINVAL);
4893 * Recalculate OR of sig events.
4895 stp->sd_sigflags = 0;
4896 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4897 stp->sd_sigflags |= ssp->ss_events;
4898 mutex_exit(&stp->sd_lock);
4899 mutex_enter(&pidlock);
4900 PID_RELE(pidp);
4901 mutex_exit(&pidlock);
4902 return (0);
4905 case I_EGETSIG:
4907 * Return (in arg) the current registration of events
4908 * for which the calling proc is to be signaled.
4911 struct strsig *ssp;
4912 struct proc *proc;
4913 pid_t pid;
4914 struct pid *pidp;
4915 struct strsigset ss;
4917 error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4918 if (error)
4919 return (error);
4921 pid = ss.ss_pid;
4922 mutex_enter(&pidlock);
4923 if (pid == 0)
4924 proc = curproc;
4925 else if (pid < 0)
4926 proc = pgfind(-pid);
4927 else
4928 proc = prfind(pid);
4929 if (proc == NULL) {
4930 mutex_exit(&pidlock);
4931 return (ESRCH);
4933 if (pid < 0)
4934 pidp = proc->p_pgidp;
4935 else
4936 pidp = proc->p_pidp;
4938 /* Prevent the pidp from being reassigned */
4939 PID_HOLD(pidp);
4940 mutex_exit(&pidlock);
4942 mutex_enter(&stp->sd_lock);
4943 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4944 if (ssp->ss_pid == pid) {
4945 ss.ss_pid = ssp->ss_pid;
4946 ss.ss_events = ssp->ss_events;
4947 error = strcopyout(&ss, (void *)arg,
4948 sizeof (struct strsigset), copyflag);
4949 mutex_exit(&stp->sd_lock);
4950 mutex_enter(&pidlock);
4951 PID_RELE(pidp);
4952 mutex_exit(&pidlock);
4953 return (error);
4955 mutex_exit(&stp->sd_lock);
4956 mutex_enter(&pidlock);
4957 PID_RELE(pidp);
4958 mutex_exit(&pidlock);
4959 return (EINVAL);
4962 case I_PEEK:
4964 STRUCT_DECL(strpeek, strpeek);
4965 size_t n;
4966 mblk_t *fmp, *tmp_mp = NULL;
4968 STRUCT_INIT(strpeek, flag);
4970 error = strcopyin((void *)arg, STRUCT_BUF(strpeek),
4971 STRUCT_SIZE(strpeek), copyflag);
4972 if (error)
4973 return (error);
4975 mutex_enter(QLOCK(rdq));
4977 * Skip the invalid messages
4979 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
4980 if (mp->b_datap->db_type != M_SIG)
4981 break;
4984 * If user has requested to peek at a high priority message
4985 * and first message is not, return 0
4987 if (mp != NULL) {
4988 if ((STRUCT_FGET(strpeek, flags) & RS_HIPRI) &&
4989 queclass(mp) == QNORM) {
4990 *rvalp = 0;
4991 mutex_exit(QLOCK(rdq));
4992 return (0);
4994 } else if (stp->sd_struiordq == NULL ||
4995 (STRUCT_FGET(strpeek, flags) & RS_HIPRI)) {
4997 * No mblks to look at at the streamhead and
4998 * 1). This isn't a synch stream or
4999 * 2). This is a synch stream but caller wants high
5000 * priority messages which is not supported by
5001 * the synch stream. (it only supports QNORM)
5003 *rvalp = 0;
5004 mutex_exit(QLOCK(rdq));
5005 return (0);
5008 fmp = mp;
5010 if (mp && mp->b_datap->db_type == M_PASSFP) {
5011 mutex_exit(QLOCK(rdq));
5012 return (EBADMSG);
5015 ASSERT(mp == NULL || mp->b_datap->db_type == M_PCPROTO ||
5016 mp->b_datap->db_type == M_PROTO ||
5017 mp->b_datap->db_type == M_DATA);
5019 if (mp && mp->b_datap->db_type == M_PCPROTO) {
5020 STRUCT_FSET(strpeek, flags, RS_HIPRI);
5021 } else {
5022 STRUCT_FSET(strpeek, flags, 0);
5026 if (mp && ((tmp_mp = dupmsg(mp)) == NULL)) {
5027 mutex_exit(QLOCK(rdq));
5028 return (ENOSR);
5030 mutex_exit(QLOCK(rdq));
5033 * set mp = tmp_mp, so that I_PEEK processing can continue.
5034 * tmp_mp is used to free the dup'd message.
5036 mp = tmp_mp;
5038 uio.uio_fmode = 0;
5039 uio.uio_extflg = UIO_COPY_CACHED;
5040 uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5041 UIO_SYSSPACE;
5042 uio.uio_limit = 0;
5044 * First process PROTO blocks, if any.
5045 * If user doesn't want to get ctl info by setting maxlen <= 0,
5046 * then set len to -1/0 and skip control blocks part.
5048 if (STRUCT_FGET(strpeek, ctlbuf.maxlen) < 0)
5049 STRUCT_FSET(strpeek, ctlbuf.len, -1);
5050 else if (STRUCT_FGET(strpeek, ctlbuf.maxlen) == 0)
5051 STRUCT_FSET(strpeek, ctlbuf.len, 0);
5052 else {
5053 int ctl_part = 0;
5055 iov.iov_base = STRUCT_FGETP(strpeek, ctlbuf.buf);
5056 iov.iov_len = STRUCT_FGET(strpeek, ctlbuf.maxlen);
5057 uio.uio_iov = &iov;
5058 uio.uio_resid = iov.iov_len;
5059 uio.uio_loffset = 0;
5060 uio.uio_iovcnt = 1;
5061 while (mp && mp->b_datap->db_type != M_DATA &&
5062 uio.uio_resid >= 0) {
5063 ASSERT(STRUCT_FGET(strpeek, flags) == 0 ?
5064 mp->b_datap->db_type == M_PROTO :
5065 mp->b_datap->db_type == M_PCPROTO);
5067 if ((n = MIN(uio.uio_resid,
5068 mp->b_wptr - mp->b_rptr)) != 0 &&
5069 (error = uiomove((char *)mp->b_rptr, n,
5070 UIO_READ, &uio)) != 0) {
5071 freemsg(tmp_mp);
5072 return (error);
5074 ctl_part = 1;
5075 mp = mp->b_cont;
5077 /* No ctl message */
5078 if (ctl_part == 0)
5079 STRUCT_FSET(strpeek, ctlbuf.len, -1);
5080 else
5081 STRUCT_FSET(strpeek, ctlbuf.len,
5082 STRUCT_FGET(strpeek, ctlbuf.maxlen) -
5083 uio.uio_resid);
5087 * Now process DATA blocks, if any.
5088 * If user doesn't want to get data info by setting maxlen <= 0,
5089 * then set len to -1/0 and skip data blocks part.
5091 if (STRUCT_FGET(strpeek, databuf.maxlen) < 0)
5092 STRUCT_FSET(strpeek, databuf.len, -1);
5093 else if (STRUCT_FGET(strpeek, databuf.maxlen) == 0)
5094 STRUCT_FSET(strpeek, databuf.len, 0);
5095 else {
5096 int data_part = 0;
5098 iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
5099 iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen);
5100 uio.uio_iov = &iov;
5101 uio.uio_resid = iov.iov_len;
5102 uio.uio_loffset = 0;
5103 uio.uio_iovcnt = 1;
5104 while (mp && uio.uio_resid) {
5105 if (mp->b_datap->db_type == M_DATA) {
5106 if ((n = MIN(uio.uio_resid,
5107 mp->b_wptr - mp->b_rptr)) != 0 &&
5108 (error = uiomove((char *)mp->b_rptr,
5109 n, UIO_READ, &uio)) != 0) {
5110 freemsg(tmp_mp);
5111 return (error);
5113 data_part = 1;
5115 ASSERT(data_part == 0 ||
5116 mp->b_datap->db_type == M_DATA);
5117 mp = mp->b_cont;
5119 /* No data message */
5120 if (data_part == 0)
5121 STRUCT_FSET(strpeek, databuf.len, -1);
5122 else
5123 STRUCT_FSET(strpeek, databuf.len,
5124 STRUCT_FGET(strpeek, databuf.maxlen) -
5125 uio.uio_resid);
5127 freemsg(tmp_mp);
5130 * It is a synch stream and user wants to get
5131 * data (maxlen > 0).
5132 * uio setup is done by the codes that process DATA
5133 * blocks above.
5135 if ((fmp == NULL) && STRUCT_FGET(strpeek, databuf.maxlen) > 0) {
5136 infod_t infod;
5138 infod.d_cmd = INFOD_COPYOUT;
5139 infod.d_res = 0;
5140 infod.d_uiop = &uio;
5141 error = infonext(rdq, &infod);
5142 if (error == EINVAL || error == EBUSY)
5143 error = 0;
5144 if (error)
5145 return (error);
5146 STRUCT_FSET(strpeek, databuf.len, STRUCT_FGET(strpeek,
5147 databuf.maxlen) - uio.uio_resid);
5148 if (STRUCT_FGET(strpeek, databuf.len) == 0) {
5150 * No data found by the infonext().
5152 STRUCT_FSET(strpeek, databuf.len, -1);
5155 error = strcopyout(STRUCT_BUF(strpeek), (void *)arg,
5156 STRUCT_SIZE(strpeek), copyflag);
5157 if (error) {
5158 return (error);
5161 * If there is no message retrieved, set return code to 0
5162 * otherwise, set it to 1.
5164 if (STRUCT_FGET(strpeek, ctlbuf.len) == -1 &&
5165 STRUCT_FGET(strpeek, databuf.len) == -1)
5166 *rvalp = 0;
5167 else
5168 *rvalp = 1;
5169 return (0);
5172 case I_FDINSERT:
5174 STRUCT_DECL(strfdinsert, strfdinsert);
5175 struct file *resftp;
5176 struct stdata *resstp;
5177 t_uscalar_t ival;
5178 ssize_t msgsize;
5179 struct strbuf mctl;
5181 STRUCT_INIT(strfdinsert, flag);
5182 if (stp->sd_flag & STRHUP)
5183 return (ENXIO);
5185 * STRDERR, STWRERR and STPLEX tested above.
5187 error = strcopyin((void *)arg, STRUCT_BUF(strfdinsert),
5188 STRUCT_SIZE(strfdinsert), copyflag);
5189 if (error)
5190 return (error);
5192 if (STRUCT_FGET(strfdinsert, offset) < 0 ||
5193 (STRUCT_FGET(strfdinsert, offset) %
5194 sizeof (t_uscalar_t)) != 0)
5195 return (EINVAL);
5196 if ((resftp = getf(STRUCT_FGET(strfdinsert, fildes))) != NULL) {
5197 if ((resstp = resftp->f_vnode->v_stream) == NULL) {
5198 releasef(STRUCT_FGET(strfdinsert, fildes));
5199 return (EINVAL);
5201 } else
5202 return (EINVAL);
5204 mutex_enter(&resstp->sd_lock);
5205 if (resstp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
5206 error = strgeterr(resstp,
5207 STRDERR|STWRERR|STRHUP|STPLEX, 0);
5208 if (error != 0) {
5209 mutex_exit(&resstp->sd_lock);
5210 releasef(STRUCT_FGET(strfdinsert, fildes));
5211 return (error);
5214 mutex_exit(&resstp->sd_lock);
5216 #ifdef _ILP32
5218 queue_t *q;
5219 queue_t *mate = NULL;
5221 /* get read queue of stream terminus */
5222 claimstr(resstp->sd_wrq);
5223 for (q = resstp->sd_wrq->q_next; q->q_next != NULL;
5224 q = q->q_next)
5225 if (!STRMATED(resstp) && STREAM(q) != resstp &&
5226 mate == NULL) {
5227 ASSERT(q->q_qinfo->qi_srvp);
5228 ASSERT(_OTHERQ(q)->q_qinfo->qi_srvp);
5229 claimstr(q);
5230 mate = q;
5232 q = _RD(q);
5233 if (mate)
5234 releasestr(mate);
5235 releasestr(resstp->sd_wrq);
5236 ival = (t_uscalar_t)q;
5238 #else
5239 ival = (t_uscalar_t)getminor(resftp->f_vnode->v_rdev);
5240 #endif /* _ILP32 */
5242 if (STRUCT_FGET(strfdinsert, ctlbuf.len) <
5243 STRUCT_FGET(strfdinsert, offset) + sizeof (t_uscalar_t)) {
5244 releasef(STRUCT_FGET(strfdinsert, fildes));
5245 return (EINVAL);
5249 * Check for legal flag value.
5251 if (STRUCT_FGET(strfdinsert, flags) & ~RS_HIPRI) {
5252 releasef(STRUCT_FGET(strfdinsert, fildes));
5253 return (EINVAL);
5256 /* get these values from those cached in the stream head */
5257 mutex_enter(QLOCK(stp->sd_wrq));
5258 rmin = stp->sd_qn_minpsz;
5259 rmax = stp->sd_qn_maxpsz;
5260 mutex_exit(QLOCK(stp->sd_wrq));
5263 * Make sure ctl and data sizes together fall within
5264 * the limits of the max and min receive packet sizes
5265 * and do not exceed system limit. A negative data
5266 * length means that no data part is to be sent.
5268 ASSERT((rmax >= 0) || (rmax == INFPSZ));
5269 if (rmax == 0) {
5270 releasef(STRUCT_FGET(strfdinsert, fildes));
5271 return (ERANGE);
5273 if ((msgsize = STRUCT_FGET(strfdinsert, databuf.len)) < 0)
5274 msgsize = 0;
5275 if ((msgsize < rmin) ||
5276 ((msgsize > rmax) && (rmax != INFPSZ)) ||
5277 (STRUCT_FGET(strfdinsert, ctlbuf.len) > strctlsz)) {
5278 releasef(STRUCT_FGET(strfdinsert, fildes));
5279 return (ERANGE);
5282 mutex_enter(&stp->sd_lock);
5283 while (!(STRUCT_FGET(strfdinsert, flags) & RS_HIPRI) &&
5284 !canputnext(stp->sd_wrq)) {
5285 if ((error = strwaitq(stp, WRITEWAIT, (ssize_t)0,
5286 flag, -1, &done)) != 0 || done) {
5287 mutex_exit(&stp->sd_lock);
5288 releasef(STRUCT_FGET(strfdinsert, fildes));
5289 return (error);
5291 if ((error = i_straccess(stp, access)) != 0) {
5292 mutex_exit(&stp->sd_lock);
5293 releasef(
5294 STRUCT_FGET(strfdinsert, fildes));
5295 return (error);
5298 mutex_exit(&stp->sd_lock);
5301 * Copy strfdinsert.ctlbuf into native form of
5302 * ctlbuf to pass down into strmakemsg().
5304 mctl.maxlen = STRUCT_FGET(strfdinsert, ctlbuf.maxlen);
5305 mctl.len = STRUCT_FGET(strfdinsert, ctlbuf.len);
5306 mctl.buf = STRUCT_FGETP(strfdinsert, ctlbuf.buf);
5308 iov.iov_base = STRUCT_FGETP(strfdinsert, databuf.buf);
5309 iov.iov_len = STRUCT_FGET(strfdinsert, databuf.len);
5310 uio.uio_iov = &iov;
5311 uio.uio_iovcnt = 1;
5312 uio.uio_loffset = 0;
5313 uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5314 UIO_SYSSPACE;
5315 uio.uio_fmode = 0;
5316 uio.uio_extflg = UIO_COPY_CACHED;
5317 uio.uio_resid = iov.iov_len;
5318 if ((error = strmakemsg(&mctl,
5319 &msgsize, &uio, stp,
5320 STRUCT_FGET(strfdinsert, flags), &mp)) != 0 || !mp) {
5321 STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5322 releasef(STRUCT_FGET(strfdinsert, fildes));
5323 return (error);
5326 STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5329 * Place the possibly reencoded queue pointer 'offset' bytes
5330 * from the start of the control portion of the message.
5332 *((t_uscalar_t *)(mp->b_rptr +
5333 STRUCT_FGET(strfdinsert, offset))) = ival;
5336 * Put message downstream.
5338 stream_willservice(stp);
5339 putnext(stp->sd_wrq, mp);
5340 stream_runservice(stp);
5341 releasef(STRUCT_FGET(strfdinsert, fildes));
5342 return (error);
5345 case I_SENDFD:
5347 struct file *fp;
5349 if ((fp = getf((int)arg)) == NULL)
5350 return (EBADF);
5351 error = do_sendfp(stp, fp, crp);
5352 if (auditing) {
5353 audit_fdsend((int)arg, fp, error);
5355 releasef((int)arg);
5356 return (error);
5359 case I_RECVFD:
5360 case I_E_RECVFD:
5362 struct k_strrecvfd *srf;
5363 int i, fd;
5365 mutex_enter(&stp->sd_lock);
5366 while (!(mp = getq(rdq))) {
5367 if (stp->sd_flag & (STRHUP|STREOF)) {
5368 mutex_exit(&stp->sd_lock);
5369 return (ENXIO);
5371 if ((error = strwaitq(stp, GETWAIT, (ssize_t)0,
5372 flag, -1, &done)) != 0 || done) {
5373 mutex_exit(&stp->sd_lock);
5374 return (error);
5376 if ((error = i_straccess(stp, access)) != 0) {
5377 mutex_exit(&stp->sd_lock);
5378 return (error);
5381 if (mp->b_datap->db_type != M_PASSFP) {
5382 putback(stp, rdq, mp, mp->b_band);
5383 mutex_exit(&stp->sd_lock);
5384 return (EBADMSG);
5386 mutex_exit(&stp->sd_lock);
5388 srf = (struct k_strrecvfd *)mp->b_rptr;
5389 if ((fd = ufalloc(0)) == -1) {
5390 mutex_enter(&stp->sd_lock);
5391 putback(stp, rdq, mp, mp->b_band);
5392 mutex_exit(&stp->sd_lock);
5393 return (EMFILE);
5395 if (cmd == I_RECVFD) {
5396 struct o_strrecvfd ostrfd;
5398 /* check to see if uid/gid values are too large. */
5400 if (srf->uid > (o_uid_t)USHRT_MAX ||
5401 srf->gid > (o_gid_t)USHRT_MAX) {
5402 mutex_enter(&stp->sd_lock);
5403 putback(stp, rdq, mp, mp->b_band);
5404 mutex_exit(&stp->sd_lock);
5405 setf(fd, NULL); /* release fd entry */
5406 return (EOVERFLOW);
5409 ostrfd.fd = fd;
5410 ostrfd.uid = (o_uid_t)srf->uid;
5411 ostrfd.gid = (o_gid_t)srf->gid;
5413 /* Null the filler bits */
5414 for (i = 0; i < 8; i++)
5415 ostrfd.fill[i] = 0;
5417 error = strcopyout(&ostrfd, (void *)arg,
5418 sizeof (struct o_strrecvfd), copyflag);
5419 } else { /* I_E_RECVFD */
5420 struct strrecvfd strfd;
5422 strfd.fd = fd;
5423 strfd.uid = srf->uid;
5424 strfd.gid = srf->gid;
5426 /* null the filler bits */
5427 for (i = 0; i < 8; i++)
5428 strfd.fill[i] = 0;
5430 error = strcopyout(&strfd, (void *)arg,
5431 sizeof (struct strrecvfd), copyflag);
5434 if (error) {
5435 setf(fd, NULL); /* release fd entry */
5436 mutex_enter(&stp->sd_lock);
5437 putback(stp, rdq, mp, mp->b_band);
5438 mutex_exit(&stp->sd_lock);
5439 return (error);
5441 if (auditing) {
5442 audit_fdrecv(fd, srf->fp);
5446 * Always increment f_count since the freemsg() below will
5447 * always call free_passfp() which performs a closef().
5449 mutex_enter(&srf->fp->f_tlock);
5450 srf->fp->f_count++;
5451 mutex_exit(&srf->fp->f_tlock);
5452 setf(fd, srf->fp);
5453 freemsg(mp);
5454 return (0);
5457 case I_SWROPT:
5459 * Set/clear the write options. arg is a bit
5460 * mask with any of the following bits set...
5461 * SNDZERO - send zero length message
5462 * SNDPIPE - send sigpipe to process if
5463 * sd_werror is set and process is
5464 * doing a write or putmsg.
5465 * The new stream head write options should reflect
5466 * what is in arg.
5468 if (arg & ~(SNDZERO|SNDPIPE))
5469 return (EINVAL);
5471 mutex_enter(&stp->sd_lock);
5472 stp->sd_wput_opt &= ~(SW_SIGPIPE|SW_SNDZERO);
5473 if (arg & SNDZERO)
5474 stp->sd_wput_opt |= SW_SNDZERO;
5475 if (arg & SNDPIPE)
5476 stp->sd_wput_opt |= SW_SIGPIPE;
5477 mutex_exit(&stp->sd_lock);
5478 return (0);
5480 case I_GWROPT:
5482 int wropt = 0;
5484 if (stp->sd_wput_opt & SW_SNDZERO)
5485 wropt |= SNDZERO;
5486 if (stp->sd_wput_opt & SW_SIGPIPE)
5487 wropt |= SNDPIPE;
5488 return (strcopyout(&wropt, (void *)arg, sizeof (wropt),
5489 copyflag));
5492 case I_LIST:
5494 * Returns all the modules found on this stream,
5495 * upto the driver. If argument is NULL, return the
5496 * number of modules (including driver). If argument
5497 * is not NULL, copy the names into the structure
5498 * provided.
5502 queue_t *q;
5503 char *qname;
5504 int i, nmods;
5505 struct str_mlist *mlist;
5506 STRUCT_DECL(str_list, strlist);
5508 if (arg == (intptr_t)NULL) {
5509 /* Return number of modules plus driver */
5510 if (stp->sd_vnode->v_type == VFIFO)
5511 *rvalp = stp->sd_pushcnt;
5512 else
5513 *rvalp = stp->sd_pushcnt + 1;
5514 return (0);
5517 STRUCT_INIT(strlist, flag);
5519 error = strcopyin((void *)arg, STRUCT_BUF(strlist),
5520 STRUCT_SIZE(strlist), copyflag);
5521 if (error != 0)
5522 return (error);
5524 mlist = STRUCT_FGETP(strlist, sl_modlist);
5525 nmods = STRUCT_FGET(strlist, sl_nmods);
5526 if (nmods <= 0)
5527 return (EINVAL);
5529 claimstr(stp->sd_wrq);
5530 q = stp->sd_wrq;
5531 for (i = 0; i < nmods && _SAMESTR(q); i++, q = q->q_next) {
5532 qname = Q2NAME(q->q_next);
5533 error = strcopyout(qname, &mlist[i], strlen(qname) + 1,
5534 copyflag);
5535 if (error != 0) {
5536 releasestr(stp->sd_wrq);
5537 return (error);
5540 releasestr(stp->sd_wrq);
5541 return (strcopyout(&i, (void *)arg, sizeof (int), copyflag));
5544 case I_CKBAND:
5546 queue_t *q;
5547 qband_t *qbp;
5549 if ((arg < 0) || (arg >= NBAND))
5550 return (EINVAL);
5551 q = _RD(stp->sd_wrq);
5552 mutex_enter(QLOCK(q));
5553 if (arg > (int)q->q_nband) {
5554 *rvalp = 0;
5555 } else {
5556 if (arg == 0) {
5557 if (q->q_first)
5558 *rvalp = 1;
5559 else
5560 *rvalp = 0;
5561 } else {
5562 qbp = q->q_bandp;
5563 while (--arg > 0)
5564 qbp = qbp->qb_next;
5565 if (qbp->qb_first)
5566 *rvalp = 1;
5567 else
5568 *rvalp = 0;
5571 mutex_exit(QLOCK(q));
5572 return (0);
5575 case I_GETBAND:
5577 int intpri;
5578 queue_t *q;
5580 q = _RD(stp->sd_wrq);
5581 mutex_enter(QLOCK(q));
5582 mp = q->q_first;
5583 if (!mp) {
5584 mutex_exit(QLOCK(q));
5585 return (ENODATA);
5587 intpri = (int)mp->b_band;
5588 error = strcopyout(&intpri, (void *)arg, sizeof (int),
5589 copyflag);
5590 mutex_exit(QLOCK(q));
5591 return (error);
5594 case I_ATMARK:
5596 queue_t *q;
5598 if (arg & ~(ANYMARK|LASTMARK))
5599 return (EINVAL);
5600 q = _RD(stp->sd_wrq);
5601 mutex_enter(&stp->sd_lock);
5602 if ((stp->sd_flag & STRATMARK) && (arg == ANYMARK)) {
5603 *rvalp = 1;
5604 } else {
5605 mutex_enter(QLOCK(q));
5606 mp = q->q_first;
5608 if (mp == NULL)
5609 *rvalp = 0;
5610 else if ((arg == ANYMARK) && (mp->b_flag & MSGMARK))
5611 *rvalp = 1;
5612 else if ((arg == LASTMARK) && (mp == stp->sd_mark))
5613 *rvalp = 1;
5614 else
5615 *rvalp = 0;
5616 mutex_exit(QLOCK(q));
5618 mutex_exit(&stp->sd_lock);
5619 return (0);
5622 case I_CANPUT:
5624 char band;
5626 if ((arg < 0) || (arg >= NBAND))
5627 return (EINVAL);
5628 band = (char)arg;
5629 *rvalp = bcanputnext(stp->sd_wrq, band);
5630 return (0);
5633 case I_SETCLTIME:
5635 int closetime;
5637 error = strcopyin((void *)arg, &closetime, sizeof (int),
5638 copyflag);
5639 if (error)
5640 return (error);
5641 if (closetime < 0)
5642 return (EINVAL);
5644 stp->sd_closetime = closetime;
5645 return (0);
5648 case I_GETCLTIME:
5650 int closetime;
5652 closetime = stp->sd_closetime;
5653 return (strcopyout(&closetime, (void *)arg, sizeof (int),
5654 copyflag));
5657 case TIOCGSID:
5659 pid_t sid;
5661 mutex_enter(&stp->sd_lock);
5662 if (stp->sd_sidp == NULL) {
5663 mutex_exit(&stp->sd_lock);
5664 return (ENOTTY);
5666 sid = stp->sd_sidp->pid_id;
5667 mutex_exit(&stp->sd_lock);
5668 return (strcopyout(&sid, (void *)arg, sizeof (pid_t),
5669 copyflag));
5672 case TIOCSPGRP:
5674 pid_t pgrp;
5675 proc_t *q;
5676 pid_t sid, fg_pgid, bg_pgid;
5678 if (error = strcopyin((void *)arg, &pgrp, sizeof (pid_t),
5679 copyflag))
5680 return (error);
5681 mutex_enter(&stp->sd_lock);
5682 mutex_enter(&pidlock);
5683 if (stp->sd_sidp != ttoproc(curthread)->p_sessp->s_sidp) {
5684 mutex_exit(&pidlock);
5685 mutex_exit(&stp->sd_lock);
5686 return (ENOTTY);
5688 if (pgrp == stp->sd_pgidp->pid_id) {
5689 mutex_exit(&pidlock);
5690 mutex_exit(&stp->sd_lock);
5691 return (0);
5693 if (pgrp <= 0 || pgrp >= maxpid) {
5694 mutex_exit(&pidlock);
5695 mutex_exit(&stp->sd_lock);
5696 return (EINVAL);
5698 if ((q = pgfind(pgrp)) == NULL ||
5699 q->p_sessp != ttoproc(curthread)->p_sessp) {
5700 mutex_exit(&pidlock);
5701 mutex_exit(&stp->sd_lock);
5702 return (EPERM);
5704 sid = stp->sd_sidp->pid_id;
5705 fg_pgid = q->p_pgrp;
5706 bg_pgid = stp->sd_pgidp->pid_id;
5707 CL_SET_PROCESS_GROUP(curthread, sid, bg_pgid, fg_pgid);
5708 PID_RELE(stp->sd_pgidp);
5709 ctty_clear_sighuped();
5710 stp->sd_pgidp = q->p_pgidp;
5711 PID_HOLD(stp->sd_pgidp);
5712 mutex_exit(&pidlock);
5713 mutex_exit(&stp->sd_lock);
5714 return (0);
5717 case TIOCGPGRP:
5719 pid_t pgrp;
5721 mutex_enter(&stp->sd_lock);
5722 if (stp->sd_sidp == NULL) {
5723 mutex_exit(&stp->sd_lock);
5724 return (ENOTTY);
5726 pgrp = stp->sd_pgidp->pid_id;
5727 mutex_exit(&stp->sd_lock);
5728 return (strcopyout(&pgrp, (void *)arg, sizeof (pid_t),
5729 copyflag));
5732 case TIOCSCTTY:
5734 return (strctty(stp));
5737 case TIOCNOTTY:
5739 /* freectty() always assumes curproc. */
5740 if (freectty(B_FALSE) != 0)
5741 return (0);
5742 return (ENOTTY);
5745 case FIONBIO:
5746 case FIOASYNC:
5747 return (0); /* handled by the upper layer */
5752 * Custom free routine used for M_PASSFP messages.
5754 static void
5755 free_passfp(struct k_strrecvfd *srf)
5757 (void) closef(srf->fp);
5758 kmem_free(srf, sizeof (struct k_strrecvfd) + sizeof (frtn_t));
5761 /* ARGSUSED */
5763 do_sendfp(struct stdata *stp, struct file *fp, struct cred *cr)
5765 queue_t *qp, *nextqp;
5766 struct k_strrecvfd *srf;
5767 mblk_t *mp;
5768 frtn_t *frtnp;
5769 size_t bufsize;
5770 queue_t *mate = NULL;
5771 syncq_t *sq = NULL;
5772 int retval = 0;
5774 if (stp->sd_flag & STRHUP)
5775 return (ENXIO);
5777 claimstr(stp->sd_wrq);
5779 /* Fastpath, we have a pipe, and we are already mated, use it. */
5780 if (STRMATED(stp)) {
5781 qp = _RD(stp->sd_mate->sd_wrq);
5782 claimstr(qp);
5783 mate = qp;
5784 } else { /* Not already mated. */
5787 * Walk the stream to the end of this one.
5788 * assumes that the claimstr() will prevent
5789 * plumbing between the stream head and the
5790 * driver from changing
5792 qp = stp->sd_wrq;
5795 * Loop until we reach the end of this stream.
5796 * On completion, qp points to the write queue
5797 * at the end of the stream, or the read queue
5798 * at the stream head if this is a fifo.
5800 while (((qp = qp->q_next) != NULL) && _SAMESTR(qp))
5804 * Just in case we get a q_next which is NULL, but
5805 * not at the end of the stream. This is actually
5806 * broken, so we set an assert to catch it in
5807 * debug, and set an error and return if not debug.
5809 ASSERT(qp);
5810 if (qp == NULL) {
5811 releasestr(stp->sd_wrq);
5812 return (EINVAL);
5816 * Enter the syncq for the driver, so (hopefully)
5817 * the queue values will not change on us.
5818 * XXXX - This will only prevent the race IFF only
5819 * the write side modifies the q_next member, and
5820 * the put procedure is protected by at least
5821 * MT_PERQ.
5823 if ((sq = qp->q_syncq) != NULL)
5824 entersq(sq, SQ_PUT);
5826 /* Now get the q_next value from this qp. */
5827 nextqp = qp->q_next;
5830 * If nextqp exists and the other stream is different
5831 * from this one claim the stream, set the mate, and
5832 * get the read queue at the stream head of the other
5833 * stream. Assumes that nextqp was at least valid when
5834 * we got it. Hopefully the entersq of the driver
5835 * will prevent it from changing on us.
5837 if ((nextqp != NULL) && (STREAM(nextqp) != stp)) {
5838 ASSERT(qp->q_qinfo->qi_srvp);
5839 ASSERT(_OTHERQ(qp)->q_qinfo->qi_srvp);
5840 ASSERT(_OTHERQ(qp->q_next)->q_qinfo->qi_srvp);
5841 claimstr(nextqp);
5843 /* Make sure we still have a q_next */
5844 if (nextqp != qp->q_next) {
5845 releasestr(stp->sd_wrq);
5846 releasestr(nextqp);
5847 return (EINVAL);
5850 qp = _RD(STREAM(nextqp)->sd_wrq);
5851 mate = qp;
5853 /* If we entered the synq above, leave it. */
5854 if (sq != NULL)
5855 leavesq(sq, SQ_PUT);
5856 } /* STRMATED(STP) */
5858 /* XXX prevents substitution of the ops vector */
5859 if (qp->q_qinfo != &strdata && qp->q_qinfo != &fifo_strdata) {
5860 retval = EINVAL;
5861 goto out;
5864 if (qp->q_flag & QFULL) {
5865 retval = EAGAIN;
5866 goto out;
5870 * Since M_PASSFP messages include a file descriptor, we use
5871 * esballoc() and specify a custom free routine (free_passfp()) that
5872 * will close the descriptor as part of freeing the message. For
5873 * convenience, we stash the frtn_t right after the data block.
5875 bufsize = sizeof (struct k_strrecvfd) + sizeof (frtn_t);
5876 srf = kmem_alloc(bufsize, KM_NOSLEEP);
5877 if (srf == NULL) {
5878 retval = EAGAIN;
5879 goto out;
5882 frtnp = (frtn_t *)(srf + 1);
5883 frtnp->free_arg = (caddr_t)srf;
5884 frtnp->free_func = free_passfp;
5886 mp = esballoc((uchar_t *)srf, bufsize, BPRI_MED, frtnp);
5887 if (mp == NULL) {
5888 kmem_free(srf, bufsize);
5889 retval = EAGAIN;
5890 goto out;
5892 mp->b_wptr += sizeof (struct k_strrecvfd);
5893 mp->b_datap->db_type = M_PASSFP;
5895 srf->fp = fp;
5896 srf->uid = crgetuid(curthread->t_cred);
5897 srf->gid = crgetgid(curthread->t_cred);
5898 mutex_enter(&fp->f_tlock);
5899 fp->f_count++;
5900 mutex_exit(&fp->f_tlock);
5902 put(qp, mp);
5903 out:
5904 releasestr(stp->sd_wrq);
5905 if (mate)
5906 releasestr(mate);
5907 return (retval);
5911 * Send an ioctl message downstream and wait for acknowledgement.
5912 * flags may be set to either U_TO_K or K_TO_K and a combination
5913 * of STR_NOERROR or STR_NOSIG
5914 * STR_NOSIG: Signals are essentially ignored or held and have
5915 * no effect for the duration of the call.
5916 * STR_NOERROR: Ignores stream head read, write and hup errors.
5917 * Additionally, if an existing ioctl times out, it is assumed
5918 * lost and and this ioctl will continue as if the previous ioctl had
5919 * finished. ETIME may be returned if this ioctl times out (i.e.
5920 * ic_timout is not INFTIM). Non-stream head errors may be returned if
5921 * the ioc_error indicates that the driver/module had problems,
5922 * an EFAULT was found when accessing user data, a lack of
5923 * resources, etc.
5926 strdoioctl(
5927 struct stdata *stp,
5928 struct strioctl *strioc,
5929 int fflags, /* file flags with model info */
5930 int flag,
5931 cred_t *crp,
5932 int *rvalp)
5934 mblk_t *bp;
5935 struct iocblk *iocbp;
5936 struct copyreq *reqp;
5937 struct copyresp *resp;
5938 int id;
5939 int transparent = 0;
5940 int error = 0;
5941 int len = 0;
5942 caddr_t taddr;
5943 int copyflag = (flag & (U_TO_K | K_TO_K));
5944 int sigflag = (flag & STR_NOSIG);
5945 int errs;
5946 uint_t waitflags;
5947 boolean_t set_iocwaitne = B_FALSE;
5949 ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
5950 ASSERT((fflags & FMODELS) != 0);
5952 TRACE_2(TR_FAC_STREAMS_FR,
5953 TR_STRDOIOCTL,
5954 "strdoioctl:stp %p strioc %p", stp, strioc);
5955 if (strioc->ic_len == TRANSPARENT) { /* send arg in M_DATA block */
5956 transparent = 1;
5957 strioc->ic_len = sizeof (intptr_t);
5960 if (strioc->ic_len < 0 || (strmsgsz > 0 && strioc->ic_len > strmsgsz))
5961 return (EINVAL);
5963 if ((bp = allocb_cred_wait(sizeof (union ioctypes), sigflag, &error,
5964 crp, curproc->p_pid)) == NULL)
5965 return (error);
5967 bzero(bp->b_wptr, sizeof (union ioctypes));
5969 iocbp = (struct iocblk *)bp->b_wptr;
5970 iocbp->ioc_count = strioc->ic_len;
5971 iocbp->ioc_cmd = strioc->ic_cmd;
5972 iocbp->ioc_flag = (fflags & FMODELS);
5974 crhold(crp);
5975 iocbp->ioc_cr = crp;
5976 DB_TYPE(bp) = M_IOCTL;
5977 bp->b_wptr += sizeof (struct iocblk);
5979 if (flag & STR_NOERROR)
5980 errs = STPLEX;
5981 else
5982 errs = STRHUP|STRDERR|STWRERR|STPLEX;
5985 * If there is data to copy into ioctl block, do so.
5987 if (iocbp->ioc_count > 0) {
5988 if (transparent)
5990 * Note: STR_NOERROR does not have an effect
5991 * in putiocd()
5993 id = K_TO_K | sigflag;
5994 else
5995 id = flag;
5996 if ((error = putiocd(bp, strioc->ic_dp, id, crp)) != 0) {
5997 freemsg(bp);
5998 crfree(crp);
5999 return (error);
6003 * We could have slept copying in user pages.
6004 * Recheck the stream head state (the other end
6005 * of a pipe could have gone away).
6007 if (stp->sd_flag & errs) {
6008 mutex_enter(&stp->sd_lock);
6009 error = strgeterr(stp, errs, 0);
6010 mutex_exit(&stp->sd_lock);
6011 if (error != 0) {
6012 freemsg(bp);
6013 crfree(crp);
6014 return (error);
6018 if (transparent)
6019 iocbp->ioc_count = TRANSPARENT;
6022 * Block for up to STRTIMOUT milliseconds if there is an outstanding
6023 * ioctl for this stream already running. All processes
6024 * sleeping here will be awakened as a result of an ACK
6025 * or NAK being received for the outstanding ioctl, or
6026 * as a result of the timer expiring on the outstanding
6027 * ioctl (a failure), or as a result of any waiting
6028 * process's timer expiring (also a failure).
6031 error = 0;
6032 mutex_enter(&stp->sd_lock);
6033 while ((stp->sd_flag & IOCWAIT) ||
6034 (!set_iocwaitne && (stp->sd_flag & IOCWAITNE))) {
6035 clock_t cv_rval;
6037 TRACE_0(TR_FAC_STREAMS_FR,
6038 TR_STRDOIOCTL_WAIT,
6039 "strdoioctl sleeps - IOCWAIT");
6040 cv_rval = str_cv_wait(&stp->sd_iocmonitor, &stp->sd_lock,
6041 STRTIMOUT, sigflag);
6042 if (cv_rval <= 0) {
6043 if (cv_rval == 0) {
6044 error = EINTR;
6045 } else {
6046 if (flag & STR_NOERROR) {
6048 * Terminating current ioctl in
6049 * progress -- assume it got lost and
6050 * wake up the other thread so that the
6051 * operation completes.
6053 if (!(stp->sd_flag & IOCWAITNE)) {
6054 set_iocwaitne = B_TRUE;
6055 stp->sd_flag |= IOCWAITNE;
6056 cv_broadcast(&stp->sd_monitor);
6059 * Otherwise, there's a running
6060 * STR_NOERROR -- we have no choice
6061 * here but to wait forever (or until
6062 * interrupted).
6064 } else {
6066 * pending ioctl has caused
6067 * us to time out
6069 error = ETIME;
6072 } else if ((stp->sd_flag & errs)) {
6073 error = strgeterr(stp, errs, 0);
6075 if (error) {
6076 mutex_exit(&stp->sd_lock);
6077 freemsg(bp);
6078 crfree(crp);
6079 return (error);
6084 * Have control of ioctl mechanism.
6085 * Send down ioctl packet and wait for response.
6087 if (stp->sd_iocblk != (mblk_t *)-1) {
6088 freemsg(stp->sd_iocblk);
6090 stp->sd_iocblk = NULL;
6093 * If this is marked with 'noerror' (internal; mostly
6094 * I_{P,}{UN,}LINK), then make sure nobody else is able to get
6095 * in here by setting IOCWAITNE.
6097 waitflags = IOCWAIT;
6098 if (flag & STR_NOERROR)
6099 waitflags |= IOCWAITNE;
6101 stp->sd_flag |= waitflags;
6104 * Assign sequence number.
6106 iocbp->ioc_id = stp->sd_iocid = getiocseqno();
6108 mutex_exit(&stp->sd_lock);
6110 TRACE_1(TR_FAC_STREAMS_FR,
6111 TR_STRDOIOCTL_PUT, "strdoioctl put: stp %p", stp);
6112 stream_willservice(stp);
6113 putnext(stp->sd_wrq, bp);
6114 stream_runservice(stp);
6117 * Timed wait for acknowledgment. The wait time is limited by the
6118 * timeout value, which must be a positive integer (number of
6119 * milliseconds) to wait, or 0 (use default value of STRTIMOUT
6120 * milliseconds), or -1 (wait forever). This will be awakened
6121 * either by an ACK/NAK message arriving, the timer expiring, or
6122 * the timer expiring on another ioctl waiting for control of the
6123 * mechanism.
6125 waitioc:
6126 mutex_enter(&stp->sd_lock);
6130 * If the reply has already arrived, don't sleep. If awakened from
6131 * the sleep, fail only if the reply has not arrived by then.
6132 * Otherwise, process the reply.
6134 while (!stp->sd_iocblk) {
6135 clock_t cv_rval;
6137 if (stp->sd_flag & errs) {
6138 error = strgeterr(stp, errs, 0);
6139 if (error != 0) {
6140 stp->sd_flag &= ~waitflags;
6141 cv_broadcast(&stp->sd_iocmonitor);
6142 mutex_exit(&stp->sd_lock);
6143 crfree(crp);
6144 return (error);
6148 TRACE_0(TR_FAC_STREAMS_FR,
6149 TR_STRDOIOCTL_WAIT2,
6150 "strdoioctl sleeps awaiting reply");
6151 ASSERT(error == 0);
6153 cv_rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock,
6154 (strioc->ic_timout ?
6155 strioc->ic_timout * 1000 : STRTIMOUT), sigflag);
6158 * There are four possible cases here: interrupt, timeout,
6159 * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
6160 * valid M_IOCTL reply).
6162 * If we've been awakened by a STR_NOERROR ioctl on some other
6163 * thread, then sd_iocblk will still be NULL, and IOCWAITNE
6164 * will be set. Pretend as if we just timed out. Note that
6165 * this other thread waited at least STRTIMOUT before trying to
6166 * awaken our thread, so this is indistinguishable (even for
6167 * INFTIM) from the case where we failed with ETIME waiting on
6168 * IOCWAIT in the prior loop.
6170 if (cv_rval > 0 && !(flag & STR_NOERROR) &&
6171 stp->sd_iocblk == NULL && (stp->sd_flag & IOCWAITNE)) {
6172 cv_rval = -1;
6176 * note: STR_NOERROR does not protect
6177 * us here.. use ic_timout < 0
6179 if (cv_rval <= 0) {
6180 if (cv_rval == 0) {
6181 error = EINTR;
6182 } else {
6183 error = ETIME;
6186 * A message could have come in after we were scheduled
6187 * but before we were actually run.
6189 bp = stp->sd_iocblk;
6190 stp->sd_iocblk = NULL;
6191 if (bp != NULL) {
6192 if ((bp->b_datap->db_type == M_COPYIN) ||
6193 (bp->b_datap->db_type == M_COPYOUT)) {
6194 mutex_exit(&stp->sd_lock);
6195 if (bp->b_cont) {
6196 freemsg(bp->b_cont);
6197 bp->b_cont = NULL;
6199 bp->b_datap->db_type = M_IOCDATA;
6200 bp->b_wptr = bp->b_rptr +
6201 sizeof (struct copyresp);
6202 resp = (struct copyresp *)bp->b_rptr;
6203 resp->cp_rval =
6204 (caddr_t)1; /* failure */
6205 stream_willservice(stp);
6206 putnext(stp->sd_wrq, bp);
6207 stream_runservice(stp);
6208 mutex_enter(&stp->sd_lock);
6209 } else {
6210 freemsg(bp);
6213 stp->sd_flag &= ~waitflags;
6214 cv_broadcast(&stp->sd_iocmonitor);
6215 mutex_exit(&stp->sd_lock);
6216 crfree(crp);
6217 return (error);
6220 bp = stp->sd_iocblk;
6222 * Note: it is strictly impossible to get here with sd_iocblk set to
6223 * -1. This is because the initial loop above doesn't allow any new
6224 * ioctls into the fray until all others have passed this point.
6226 ASSERT(bp != NULL && bp != (mblk_t *)-1);
6227 TRACE_1(TR_FAC_STREAMS_FR,
6228 TR_STRDOIOCTL_ACK, "strdoioctl got reply: bp %p", bp);
6229 if ((bp->b_datap->db_type == M_IOCACK) ||
6230 (bp->b_datap->db_type == M_IOCNAK)) {
6231 /* for detection of duplicate ioctl replies */
6232 stp->sd_iocblk = (mblk_t *)-1;
6233 stp->sd_flag &= ~waitflags;
6234 cv_broadcast(&stp->sd_iocmonitor);
6235 mutex_exit(&stp->sd_lock);
6236 } else {
6238 * flags not cleared here because we're still doing
6239 * copy in/out for ioctl.
6241 stp->sd_iocblk = NULL;
6242 mutex_exit(&stp->sd_lock);
6247 * Have received acknowledgment.
6250 switch (bp->b_datap->db_type) {
6251 case M_IOCACK:
6253 * Positive ack.
6255 iocbp = (struct iocblk *)bp->b_rptr;
6258 * Set error if indicated.
6260 if (iocbp->ioc_error) {
6261 error = iocbp->ioc_error;
6262 break;
6266 * Set return value.
6268 *rvalp = iocbp->ioc_rval;
6271 * Data may have been returned in ACK message (ioc_count > 0).
6272 * If so, copy it out to the user's buffer.
6274 if (iocbp->ioc_count && !transparent) {
6275 if (error = getiocd(bp, strioc->ic_dp, copyflag))
6276 break;
6278 if (!transparent) {
6279 if (len) /* an M_COPYOUT was used with I_STR */
6280 strioc->ic_len = len;
6281 else
6282 strioc->ic_len = (int)iocbp->ioc_count;
6284 break;
6286 case M_IOCNAK:
6288 * Negative ack.
6290 * The only thing to do is set error as specified
6291 * in neg ack packet.
6293 iocbp = (struct iocblk *)bp->b_rptr;
6295 error = (iocbp->ioc_error ? iocbp->ioc_error : EINVAL);
6296 break;
6298 case M_COPYIN:
6300 * Driver or module has requested user ioctl data.
6302 reqp = (struct copyreq *)bp->b_rptr;
6305 * M_COPYIN should *never* have a message attached, though
6306 * it's harmless if it does -- thus, panic on a DEBUG
6307 * kernel and just free it on a non-DEBUG build.
6309 ASSERT(bp->b_cont == NULL);
6310 if (bp->b_cont != NULL) {
6311 freemsg(bp->b_cont);
6312 bp->b_cont = NULL;
6315 error = putiocd(bp, reqp->cq_addr, flag, crp);
6316 if (error && bp->b_cont) {
6317 freemsg(bp->b_cont);
6318 bp->b_cont = NULL;
6321 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6322 bp->b_datap->db_type = M_IOCDATA;
6324 mblk_setcred(bp, crp, curproc->p_pid);
6325 resp = (struct copyresp *)bp->b_rptr;
6326 resp->cp_rval = (caddr_t)(uintptr_t)error;
6327 resp->cp_flag = (fflags & FMODELS);
6329 stream_willservice(stp);
6330 putnext(stp->sd_wrq, bp);
6331 stream_runservice(stp);
6333 if (error) {
6334 mutex_enter(&stp->sd_lock);
6335 stp->sd_flag &= ~waitflags;
6336 cv_broadcast(&stp->sd_iocmonitor);
6337 mutex_exit(&stp->sd_lock);
6338 crfree(crp);
6339 return (error);
6342 goto waitioc;
6344 case M_COPYOUT:
6346 * Driver or module has ioctl data for a user.
6348 reqp = (struct copyreq *)bp->b_rptr;
6349 ASSERT(bp->b_cont != NULL);
6352 * Always (transparent or non-transparent )
6353 * use the address specified in the request
6355 taddr = reqp->cq_addr;
6356 if (!transparent)
6357 len = (int)reqp->cq_size;
6359 /* copyout data to the provided address */
6360 error = getiocd(bp, taddr, copyflag);
6362 freemsg(bp->b_cont);
6363 bp->b_cont = NULL;
6365 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6366 bp->b_datap->db_type = M_IOCDATA;
6368 mblk_setcred(bp, crp, curproc->p_pid);
6369 resp = (struct copyresp *)bp->b_rptr;
6370 resp->cp_rval = (caddr_t)(uintptr_t)error;
6371 resp->cp_flag = (fflags & FMODELS);
6373 stream_willservice(stp);
6374 putnext(stp->sd_wrq, bp);
6375 stream_runservice(stp);
6377 if (error) {
6378 mutex_enter(&stp->sd_lock);
6379 stp->sd_flag &= ~waitflags;
6380 cv_broadcast(&stp->sd_iocmonitor);
6381 mutex_exit(&stp->sd_lock);
6382 crfree(crp);
6383 return (error);
6385 goto waitioc;
6387 default:
6388 ASSERT(0);
6389 mutex_enter(&stp->sd_lock);
6390 stp->sd_flag &= ~waitflags;
6391 cv_broadcast(&stp->sd_iocmonitor);
6392 mutex_exit(&stp->sd_lock);
6393 break;
6396 freemsg(bp);
6397 crfree(crp);
6398 return (error);
6402 * Send an M_CMD message downstream and wait for a reply. This is a ptools
6403 * special used to retrieve information from modules/drivers a stream without
6404 * being subjected to flow control or interfering with pending messages on the
6405 * stream (e.g. an ioctl in flight).
6408 strdocmd(struct stdata *stp, struct strcmd *scp, cred_t *crp)
6410 mblk_t *mp;
6411 struct cmdblk *cmdp;
6412 int error = 0;
6413 int errs = STRHUP|STRDERR|STWRERR|STPLEX;
6414 clock_t rval, timeout = STRTIMOUT;
6416 if (scp->sc_len < 0 || scp->sc_len > sizeof (scp->sc_buf) ||
6417 scp->sc_timeout < -1)
6418 return (EINVAL);
6420 if (scp->sc_timeout > 0)
6421 timeout = scp->sc_timeout * MILLISEC;
6423 if ((mp = allocb_cred(sizeof (struct cmdblk), crp,
6424 curproc->p_pid)) == NULL)
6425 return (ENOMEM);
6427 crhold(crp);
6429 cmdp = (struct cmdblk *)mp->b_wptr;
6430 cmdp->cb_cr = crp;
6431 cmdp->cb_cmd = scp->sc_cmd;
6432 cmdp->cb_len = scp->sc_len;
6433 cmdp->cb_error = 0;
6434 mp->b_wptr += sizeof (struct cmdblk);
6436 DB_TYPE(mp) = M_CMD;
6437 DB_CPID(mp) = curproc->p_pid;
6440 * Copy in the payload.
6442 if (cmdp->cb_len > 0) {
6443 mp->b_cont = allocb_cred(sizeof (scp->sc_buf), crp,
6444 curproc->p_pid);
6445 if (mp->b_cont == NULL) {
6446 error = ENOMEM;
6447 goto out;
6450 /* cb_len comes from sc_len, which has already been checked */
6451 ASSERT(cmdp->cb_len <= sizeof (scp->sc_buf));
6452 (void) bcopy(scp->sc_buf, mp->b_cont->b_wptr, cmdp->cb_len);
6453 mp->b_cont->b_wptr += cmdp->cb_len;
6454 DB_CPID(mp->b_cont) = curproc->p_pid;
6458 * Since this mechanism is strictly for ptools, and since only one
6459 * process can be grabbed at a time, we simply fail if there's
6460 * currently an operation pending.
6462 mutex_enter(&stp->sd_lock);
6463 if (stp->sd_flag & STRCMDWAIT) {
6464 mutex_exit(&stp->sd_lock);
6465 error = EBUSY;
6466 goto out;
6468 stp->sd_flag |= STRCMDWAIT;
6469 ASSERT(stp->sd_cmdblk == NULL);
6470 mutex_exit(&stp->sd_lock);
6472 putnext(stp->sd_wrq, mp);
6473 mp = NULL;
6476 * Timed wait for acknowledgment. If the reply has already arrived,
6477 * don't sleep. If awakened from the sleep, fail only if the reply
6478 * has not arrived by then. Otherwise, process the reply.
6480 mutex_enter(&stp->sd_lock);
6481 while (stp->sd_cmdblk == NULL) {
6482 if (stp->sd_flag & errs) {
6483 if ((error = strgeterr(stp, errs, 0)) != 0)
6484 goto waitout;
6487 rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock, timeout, 0);
6488 if (stp->sd_cmdblk != NULL)
6489 break;
6491 if (rval <= 0) {
6492 error = (rval == 0) ? EINTR : ETIME;
6493 goto waitout;
6498 * We received a reply.
6500 mp = stp->sd_cmdblk;
6501 stp->sd_cmdblk = NULL;
6502 ASSERT(mp != NULL && DB_TYPE(mp) == M_CMD);
6503 ASSERT(stp->sd_flag & STRCMDWAIT);
6504 stp->sd_flag &= ~STRCMDWAIT;
6505 mutex_exit(&stp->sd_lock);
6507 cmdp = (struct cmdblk *)mp->b_rptr;
6508 if ((error = cmdp->cb_error) != 0)
6509 goto out;
6512 * Data may have been returned in the reply (cb_len > 0).
6513 * If so, copy it out to the user's buffer.
6515 if (cmdp->cb_len > 0) {
6516 if (mp->b_cont == NULL || MBLKL(mp->b_cont) < cmdp->cb_len) {
6517 error = EPROTO;
6518 goto out;
6521 cmdp->cb_len = MIN(cmdp->cb_len, sizeof (scp->sc_buf));
6522 (void) bcopy(mp->b_cont->b_rptr, scp->sc_buf, cmdp->cb_len);
6524 scp->sc_len = cmdp->cb_len;
6525 out:
6526 freemsg(mp);
6527 crfree(crp);
6528 return (error);
6529 waitout:
6530 ASSERT(stp->sd_cmdblk == NULL);
6531 stp->sd_flag &= ~STRCMDWAIT;
6532 mutex_exit(&stp->sd_lock);
6533 crfree(crp);
6534 return (error);
6538 * For the SunOS keyboard driver.
6539 * Return the next available "ioctl" sequence number.
6540 * Exported, so that streams modules can send "ioctl" messages
6541 * downstream from their open routine.
6544 getiocseqno(void)
6546 int i;
6548 mutex_enter(&strresources);
6549 i = ++ioc_id;
6550 mutex_exit(&strresources);
6551 return (i);
6555 * Get the next message from the read queue. If the message is
6556 * priority, STRPRI will have been set by strrput(). This flag
6557 * should be reset only when the entire message at the front of the
6558 * queue as been consumed.
6560 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
6563 strgetmsg(
6564 struct vnode *vp,
6565 struct strbuf *mctl,
6566 struct strbuf *mdata,
6567 unsigned char *prip,
6568 int *flagsp,
6569 int fmode,
6570 rval_t *rvp)
6572 struct stdata *stp;
6573 mblk_t *bp, *nbp;
6574 mblk_t *savemp = NULL;
6575 mblk_t *savemptail = NULL;
6576 uint_t old_sd_flag;
6577 int flg;
6578 int more = 0;
6579 int error = 0;
6580 char first = 1;
6581 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
6582 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
6583 unsigned char pri = 0;
6584 queue_t *q;
6585 int pr = 0; /* Partial read successful */
6586 struct uio uios;
6587 struct uio *uiop = &uios;
6588 struct iovec iovs;
6589 unsigned char type;
6591 TRACE_1(TR_FAC_STREAMS_FR, TR_STRGETMSG_ENTER,
6592 "strgetmsg:%p", vp);
6594 ASSERT(vp->v_stream);
6595 stp = vp->v_stream;
6596 rvp->r_val1 = 0;
6598 mutex_enter(&stp->sd_lock);
6600 if ((error = i_straccess(stp, JCREAD)) != 0) {
6601 mutex_exit(&stp->sd_lock);
6602 return (error);
6605 if (stp->sd_flag & (STRDERR|STPLEX)) {
6606 error = strgeterr(stp, STRDERR|STPLEX, 0);
6607 if (error != 0) {
6608 mutex_exit(&stp->sd_lock);
6609 return (error);
6612 mutex_exit(&stp->sd_lock);
6614 switch (*flagsp) {
6615 case MSG_HIPRI:
6616 if (*prip != 0)
6617 return (EINVAL);
6618 break;
6620 case MSG_ANY:
6621 case MSG_BAND:
6622 break;
6624 default:
6625 return (EINVAL);
6628 * Setup uio and iov for data part
6630 iovs.iov_base = mdata->buf;
6631 iovs.iov_len = mdata->maxlen;
6632 uios.uio_iov = &iovs;
6633 uios.uio_iovcnt = 1;
6634 uios.uio_loffset = 0;
6635 uios.uio_segflg = UIO_USERSPACE;
6636 uios.uio_fmode = 0;
6637 uios.uio_extflg = UIO_COPY_CACHED;
6638 uios.uio_resid = mdata->maxlen;
6639 uios.uio_offset = 0;
6641 q = _RD(stp->sd_wrq);
6642 mutex_enter(&stp->sd_lock);
6643 old_sd_flag = stp->sd_flag;
6644 mark = 0;
6645 for (;;) {
6646 int done = 0;
6647 mblk_t *q_first = q->q_first;
6650 * Get the next message of appropriate priority
6651 * from the stream head. If the caller is interested
6652 * in band or hipri messages, then they should already
6653 * be enqueued at the stream head. On the other hand
6654 * if the caller wants normal (band 0) messages, they
6655 * might be deferred in a synchronous stream and they
6656 * will need to be pulled up.
6658 * After we have dequeued a message, we might find that
6659 * it was a deferred M_SIG that was enqueued at the
6660 * stream head. It must now be posted as part of the
6661 * read by calling strsignal_nolock().
6663 * Also note that strrput does not enqueue an M_PCSIG,
6664 * and there cannot be more than one hipri message,
6665 * so there was no need to have the M_PCSIG case.
6667 * At some time it might be nice to try and wrap the
6668 * functionality of kstrgetmsg() and strgetmsg() into
6669 * a common routine so to reduce the amount of replicated
6670 * code (since they are extremely similar).
6672 if (!(*flagsp & (MSG_HIPRI|MSG_BAND))) {
6673 /* Asking for normal, band0 data */
6674 bp = strget(stp, q, uiop, first, &error);
6675 ASSERT(MUTEX_HELD(&stp->sd_lock));
6676 if (bp != NULL) {
6677 if (DB_TYPE(bp) == M_SIG) {
6678 strsignal_nolock(stp, *bp->b_rptr,
6679 bp->b_band);
6680 freemsg(bp);
6681 continue;
6682 } else {
6683 break;
6686 if (error != 0)
6687 goto getmout;
6690 * We can't depend on the value of STRPRI here because
6691 * the stream head may be in transit. Therefore, we
6692 * must look at the type of the first message to
6693 * determine if a high priority messages is waiting
6695 } else if ((*flagsp & MSG_HIPRI) && q_first != NULL &&
6696 DB_TYPE(q_first) >= QPCTL &&
6697 (bp = getq_noenab(q, 0)) != NULL) {
6698 /* Asked for HIPRI and got one */
6699 ASSERT(DB_TYPE(bp) >= QPCTL);
6700 break;
6701 } else if ((*flagsp & MSG_BAND) && q_first != NULL &&
6702 ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
6703 (bp = getq_noenab(q, 0)) != NULL) {
6705 * Asked for at least band "prip" and got either at
6706 * least that band or a hipri message.
6708 ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
6709 if (DB_TYPE(bp) == M_SIG) {
6710 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
6711 freemsg(bp);
6712 continue;
6713 } else {
6714 break;
6718 /* No data. Time to sleep? */
6719 qbackenable(q, 0);
6722 * If STRHUP or STREOF, return 0 length control and data.
6723 * If resid is 0, then a read(fd,buf,0) was done. Do not
6724 * sleep to satisfy this request because by default we have
6725 * zero bytes to return.
6727 if ((stp->sd_flag & (STRHUP|STREOF)) || (mctl->maxlen == 0 &&
6728 mdata->maxlen == 0)) {
6729 mctl->len = mdata->len = 0;
6730 *flagsp = 0;
6731 mutex_exit(&stp->sd_lock);
6732 return (0);
6734 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_WAIT,
6735 "strgetmsg calls strwaitq:%p, %p",
6736 vp, uiop);
6737 if (((error = strwaitq(stp, GETWAIT, (ssize_t)0, fmode, -1,
6738 &done)) != 0) || done) {
6739 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_DONE,
6740 "strgetmsg error or done:%p, %p",
6741 vp, uiop);
6742 mutex_exit(&stp->sd_lock);
6743 return (error);
6745 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_AWAKE,
6746 "strgetmsg awakes:%p, %p", vp, uiop);
6747 if ((error = i_straccess(stp, JCREAD)) != 0) {
6748 mutex_exit(&stp->sd_lock);
6749 return (error);
6751 first = 0;
6753 ASSERT(bp != NULL);
6755 * Extract any mark information. If the message is not completely
6756 * consumed this information will be put in the mblk
6757 * that is putback.
6758 * If MSGMARKNEXT is set and the message is completely consumed
6759 * the STRATMARK flag will be set below. Likewise, if
6760 * MSGNOTMARKNEXT is set and the message is
6761 * completely consumed STRNOTATMARK will be set.
6763 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
6764 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
6765 (MSGMARKNEXT|MSGNOTMARKNEXT));
6766 if (mark != 0 && bp == stp->sd_mark) {
6767 mark |= _LASTMARK;
6768 stp->sd_mark = NULL;
6771 * keep track of the original message type and priority
6773 pri = bp->b_band;
6774 type = bp->b_datap->db_type;
6775 if (type == M_PASSFP) {
6776 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
6777 stp->sd_mark = bp;
6778 bp->b_flag |= mark & ~_LASTMARK;
6779 putback(stp, q, bp, pri);
6780 qbackenable(q, pri);
6781 mutex_exit(&stp->sd_lock);
6782 return (EBADMSG);
6784 ASSERT(type != M_SIG);
6787 * Set this flag so strrput will not generate signals. Need to
6788 * make sure this flag is cleared before leaving this routine
6789 * else signals will stop being sent.
6791 stp->sd_flag |= STRGETINPROG;
6792 mutex_exit(&stp->sd_lock);
6794 if (STREAM_NEEDSERVICE(stp))
6795 stream_runservice(stp);
6798 * Set HIPRI flag if message is priority.
6800 if (type >= QPCTL)
6801 flg = MSG_HIPRI;
6802 else
6803 flg = MSG_BAND;
6806 * First process PROTO or PCPROTO blocks, if any.
6808 if (mctl->maxlen >= 0 && type != M_DATA) {
6809 size_t n, bcnt;
6810 char *ubuf;
6812 bcnt = mctl->maxlen;
6813 ubuf = mctl->buf;
6814 while (bp != NULL && bp->b_datap->db_type != M_DATA) {
6815 if ((n = MIN(bcnt, bp->b_wptr - bp->b_rptr)) != 0 &&
6816 copyout(bp->b_rptr, ubuf, n)) {
6817 error = EFAULT;
6818 mutex_enter(&stp->sd_lock);
6820 * clear stream head pri flag based on
6821 * first message type
6823 if (type >= QPCTL) {
6824 ASSERT(type == M_PCPROTO);
6825 stp->sd_flag &= ~STRPRI;
6827 more = 0;
6828 freemsg(bp);
6829 goto getmout;
6831 ubuf += n;
6832 bp->b_rptr += n;
6833 if (bp->b_rptr >= bp->b_wptr) {
6834 nbp = bp;
6835 bp = bp->b_cont;
6836 freeb(nbp);
6838 ASSERT(n <= bcnt);
6839 bcnt -= n;
6840 if (bcnt == 0)
6841 break;
6843 mctl->len = mctl->maxlen - bcnt;
6844 } else
6845 mctl->len = -1;
6847 if (bp && bp->b_datap->db_type != M_DATA) {
6849 * More PROTO blocks in msg.
6851 more |= MORECTL;
6852 savemp = bp;
6853 while (bp && bp->b_datap->db_type != M_DATA) {
6854 savemptail = bp;
6855 bp = bp->b_cont;
6857 savemptail->b_cont = NULL;
6861 * Now process DATA blocks, if any.
6863 if (mdata->maxlen >= 0 && bp) {
6865 * struiocopyout will consume a potential zero-length
6866 * M_DATA even if uio_resid is zero.
6868 size_t oldresid = uiop->uio_resid;
6870 bp = struiocopyout(bp, uiop, &error);
6871 if (error != 0) {
6872 mutex_enter(&stp->sd_lock);
6874 * clear stream head hi pri flag based on
6875 * first message
6877 if (type >= QPCTL) {
6878 ASSERT(type == M_PCPROTO);
6879 stp->sd_flag &= ~STRPRI;
6881 more = 0;
6882 freemsg(savemp);
6883 goto getmout;
6886 * (pr == 1) indicates a partial read.
6888 if (oldresid > uiop->uio_resid)
6889 pr = 1;
6890 mdata->len = mdata->maxlen - uiop->uio_resid;
6891 } else
6892 mdata->len = -1;
6894 if (bp) { /* more data blocks in msg */
6895 more |= MOREDATA;
6896 if (savemp)
6897 savemptail->b_cont = bp;
6898 else
6899 savemp = bp;
6902 mutex_enter(&stp->sd_lock);
6903 if (savemp) {
6904 if (pr && (savemp->b_datap->db_type == M_DATA) &&
6905 msgnodata(savemp)) {
6907 * Avoid queuing a zero-length tail part of
6908 * a message. pr=1 indicates that we read some of
6909 * the message.
6911 freemsg(savemp);
6912 more &= ~MOREDATA;
6914 * clear stream head hi pri flag based on
6915 * first message
6917 if (type >= QPCTL) {
6918 ASSERT(type == M_PCPROTO);
6919 stp->sd_flag &= ~STRPRI;
6921 } else {
6922 savemp->b_band = pri;
6924 * If the first message was HIPRI and the one we're
6925 * putting back isn't, then clear STRPRI, otherwise
6926 * set STRPRI again. Note that we must set STRPRI
6927 * again since the flush logic in strrput_nondata()
6928 * may have cleared it while we had sd_lock dropped.
6930 if (type >= QPCTL) {
6931 ASSERT(type == M_PCPROTO);
6932 if (queclass(savemp) < QPCTL)
6933 stp->sd_flag &= ~STRPRI;
6934 else
6935 stp->sd_flag |= STRPRI;
6936 } else if (queclass(savemp) >= QPCTL) {
6938 * The first message was not a HIPRI message,
6939 * but the one we are about to putback is.
6940 * For simplicitly, we do not allow for HIPRI
6941 * messages to be embedded in the message
6942 * body, so just force it to same type as
6943 * first message.
6945 ASSERT(type == M_DATA || type == M_PROTO);
6946 ASSERT(savemp->b_datap->db_type == M_PCPROTO);
6947 savemp->b_datap->db_type = type;
6949 if (mark != 0) {
6950 savemp->b_flag |= mark & ~_LASTMARK;
6951 if ((mark & _LASTMARK) &&
6952 (stp->sd_mark == NULL)) {
6954 * If another marked message arrived
6955 * while sd_lock was not held sd_mark
6956 * would be non-NULL.
6958 stp->sd_mark = savemp;
6961 putback(stp, q, savemp, pri);
6963 } else {
6965 * The complete message was consumed.
6967 * If another M_PCPROTO arrived while sd_lock was not held
6968 * it would have been discarded since STRPRI was still set.
6970 * Move the MSG*MARKNEXT information
6971 * to the stream head just in case
6972 * the read queue becomes empty.
6973 * clear stream head hi pri flag based on
6974 * first message
6976 * If the stream head was at the mark
6977 * (STRATMARK) before we dropped sd_lock above
6978 * and some data was consumed then we have
6979 * moved past the mark thus STRATMARK is
6980 * cleared. However, if a message arrived in
6981 * strrput during the copyout above causing
6982 * STRATMARK to be set we can not clear that
6983 * flag.
6985 if (type >= QPCTL) {
6986 ASSERT(type == M_PCPROTO);
6987 stp->sd_flag &= ~STRPRI;
6989 if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
6990 if (mark & MSGMARKNEXT) {
6991 stp->sd_flag &= ~STRNOTATMARK;
6992 stp->sd_flag |= STRATMARK;
6993 } else if (mark & MSGNOTMARKNEXT) {
6994 stp->sd_flag &= ~STRATMARK;
6995 stp->sd_flag |= STRNOTATMARK;
6996 } else {
6997 stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
6999 } else if (pr && (old_sd_flag & STRATMARK)) {
7000 stp->sd_flag &= ~STRATMARK;
7004 *flagsp = flg;
7005 *prip = pri;
7008 * Getmsg cleanup processing - if the state of the queue has changed
7009 * some signals may need to be sent and/or poll awakened.
7011 getmout:
7012 qbackenable(q, pri);
7015 * We dropped the stream head lock above. Send all M_SIG messages
7016 * before processing stream head for SIGPOLL messages.
7018 ASSERT(MUTEX_HELD(&stp->sd_lock));
7019 while ((bp = q->q_first) != NULL &&
7020 (bp->b_datap->db_type == M_SIG)) {
7022 * sd_lock is held so the content of the read queue can not
7023 * change.
7025 bp = getq(q);
7026 ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7028 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7029 mutex_exit(&stp->sd_lock);
7030 freemsg(bp);
7031 if (STREAM_NEEDSERVICE(stp))
7032 stream_runservice(stp);
7033 mutex_enter(&stp->sd_lock);
7037 * stream head cannot change while we make the determination
7038 * whether or not to send a signal. Drop the flag to allow strrput
7039 * to send firstmsgsigs again.
7041 stp->sd_flag &= ~STRGETINPROG;
7044 * If the type of message at the front of the queue changed
7045 * due to the receive the appropriate signals and pollwakeup events
7046 * are generated. The type of changes are:
7047 * Processed a hipri message, q_first is not hipri.
7048 * Processed a band X message, and q_first is band Y.
7049 * The generated signals and pollwakeups are identical to what
7050 * strrput() generates should the message that is now on q_first
7051 * arrive to an empty read queue.
7053 * Note: only strrput will send a signal for a hipri message.
7055 if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7056 strsigset_t signals = 0;
7057 strpollset_t pollwakeups = 0;
7059 if (flg & MSG_HIPRI) {
7061 * Removed a hipri message. Regular data at
7062 * the front of the queue.
7064 if (bp->b_band == 0) {
7065 signals = S_INPUT | S_RDNORM;
7066 pollwakeups = POLLIN | POLLRDNORM;
7067 } else {
7068 signals = S_INPUT | S_RDBAND;
7069 pollwakeups = POLLIN | POLLRDBAND;
7071 } else if (pri != bp->b_band) {
7073 * The band is different for the new q_first.
7075 if (bp->b_band == 0) {
7076 signals = S_RDNORM;
7077 pollwakeups = POLLIN | POLLRDNORM;
7078 } else {
7079 signals = S_RDBAND;
7080 pollwakeups = POLLIN | POLLRDBAND;
7084 if (pollwakeups != 0) {
7085 if (pollwakeups == (POLLIN | POLLRDNORM)) {
7086 if (!(stp->sd_rput_opt & SR_POLLIN))
7087 goto no_pollwake;
7088 stp->sd_rput_opt &= ~SR_POLLIN;
7090 mutex_exit(&stp->sd_lock);
7091 pollwakeup(&stp->sd_pollist, pollwakeups);
7092 mutex_enter(&stp->sd_lock);
7094 no_pollwake:
7096 if (stp->sd_sigflags & signals)
7097 strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7099 mutex_exit(&stp->sd_lock);
7101 rvp->r_val1 = more;
7102 return (error);
7103 #undef _LASTMARK
7107 * Get the next message from the read queue. If the message is
7108 * priority, STRPRI will have been set by strrput(). This flag
7109 * should be reset only when the entire message at the front of the
7110 * queue as been consumed.
7112 * If uiop is NULL all data is returned in mctlp.
7113 * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
7114 * not enabled.
7115 * The timeout parameter is in milliseconds; -1 for infinity.
7116 * This routine handles the consolidation private flags:
7117 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7118 * MSG_DELAYERROR Defer the error check until the queue is empty.
7119 * MSG_HOLDSIG Hold signals while waiting for data.
7120 * MSG_IPEEK Only peek at messages.
7121 * MSG_DISCARDTAIL Discard the tail M_DATA part of the message
7122 * that doesn't fit.
7123 * MSG_NOMARK If the message is marked leave it on the queue.
7125 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
7128 kstrgetmsg(
7129 struct vnode *vp,
7130 mblk_t **mctlp,
7131 struct uio *uiop,
7132 unsigned char *prip,
7133 int *flagsp,
7134 clock_t timout,
7135 rval_t *rvp)
7137 struct stdata *stp;
7138 mblk_t *bp, *nbp;
7139 mblk_t *savemp = NULL;
7140 mblk_t *savemptail = NULL;
7141 int flags;
7142 uint_t old_sd_flag;
7143 int flg;
7144 int more = 0;
7145 int error = 0;
7146 char first = 1;
7147 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
7148 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
7149 unsigned char pri = 0;
7150 queue_t *q;
7151 int pr = 0; /* Partial read successful */
7152 unsigned char type;
7154 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_ENTER,
7155 "kstrgetmsg:%p", vp);
7157 ASSERT(vp->v_stream);
7158 stp = vp->v_stream;
7159 rvp->r_val1 = 0;
7161 mutex_enter(&stp->sd_lock);
7163 if ((error = i_straccess(stp, JCREAD)) != 0) {
7164 mutex_exit(&stp->sd_lock);
7165 return (error);
7168 flags = *flagsp;
7169 if (stp->sd_flag & (STRDERR|STPLEX)) {
7170 if ((stp->sd_flag & STPLEX) ||
7171 (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == 0) {
7172 error = strgeterr(stp, STRDERR|STPLEX,
7173 (flags & MSG_IPEEK));
7174 if (error != 0) {
7175 mutex_exit(&stp->sd_lock);
7176 return (error);
7180 mutex_exit(&stp->sd_lock);
7182 switch (flags & (MSG_HIPRI|MSG_ANY|MSG_BAND)) {
7183 case MSG_HIPRI:
7184 if (*prip != 0)
7185 return (EINVAL);
7186 break;
7188 case MSG_ANY:
7189 case MSG_BAND:
7190 break;
7192 default:
7193 return (EINVAL);
7196 retry:
7197 q = _RD(stp->sd_wrq);
7198 mutex_enter(&stp->sd_lock);
7199 old_sd_flag = stp->sd_flag;
7200 mark = 0;
7201 for (;;) {
7202 int done = 0;
7203 int waitflag;
7204 int fmode;
7205 mblk_t *q_first = q->q_first;
7208 * This section of the code operates just like the code
7209 * in strgetmsg(). There is a comment there about what
7210 * is going on here.
7212 if (!(flags & (MSG_HIPRI|MSG_BAND))) {
7213 /* Asking for normal, band0 data */
7214 bp = strget(stp, q, uiop, first, &error);
7215 ASSERT(MUTEX_HELD(&stp->sd_lock));
7216 if (bp != NULL) {
7217 if (DB_TYPE(bp) == M_SIG) {
7218 strsignal_nolock(stp, *bp->b_rptr,
7219 bp->b_band);
7220 freemsg(bp);
7221 continue;
7222 } else {
7223 break;
7226 if (error != 0) {
7227 goto getmout;
7230 * We can't depend on the value of STRPRI here because
7231 * the stream head may be in transit. Therefore, we
7232 * must look at the type of the first message to
7233 * determine if a high priority messages is waiting
7235 } else if ((flags & MSG_HIPRI) && q_first != NULL &&
7236 DB_TYPE(q_first) >= QPCTL &&
7237 (bp = getq_noenab(q, 0)) != NULL) {
7238 ASSERT(DB_TYPE(bp) >= QPCTL);
7239 break;
7240 } else if ((flags & MSG_BAND) && q_first != NULL &&
7241 ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
7242 (bp = getq_noenab(q, 0)) != NULL) {
7244 * Asked for at least band "prip" and got either at
7245 * least that band or a hipri message.
7247 ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
7248 if (DB_TYPE(bp) == M_SIG) {
7249 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7250 freemsg(bp);
7251 continue;
7252 } else {
7253 break;
7257 /* No data. Time to sleep? */
7258 qbackenable(q, 0);
7261 * Delayed error notification?
7263 if ((stp->sd_flag & (STRDERR|STPLEX)) &&
7264 (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == MSG_DELAYERROR) {
7265 error = strgeterr(stp, STRDERR|STPLEX,
7266 (flags & MSG_IPEEK));
7267 if (error != 0) {
7268 mutex_exit(&stp->sd_lock);
7269 return (error);
7274 * If STRHUP or STREOF, return 0 length control and data.
7275 * If a read(fd,buf,0) has been done, do not sleep, just
7276 * return.
7278 * If mctlp == NULL and uiop == NULL, then the code will
7279 * do the strwaitq. This is an understood way of saying
7280 * sleep "polling" until a message is received.
7282 if ((stp->sd_flag & (STRHUP|STREOF)) ||
7283 (uiop != NULL && uiop->uio_resid == 0)) {
7284 if (mctlp != NULL)
7285 *mctlp = NULL;
7286 *flagsp = 0;
7287 mutex_exit(&stp->sd_lock);
7288 return (0);
7291 waitflag = GETWAIT;
7292 if (flags &
7293 (MSG_HOLDSIG|MSG_IGNERROR|MSG_IPEEK|MSG_DELAYERROR)) {
7294 if (flags & MSG_HOLDSIG)
7295 waitflag |= STR_NOSIG;
7296 if (flags & MSG_IGNERROR)
7297 waitflag |= STR_NOERROR;
7298 if (flags & MSG_IPEEK)
7299 waitflag |= STR_PEEK;
7300 if (flags & MSG_DELAYERROR)
7301 waitflag |= STR_DELAYERR;
7303 if (uiop != NULL)
7304 fmode = uiop->uio_fmode;
7305 else
7306 fmode = 0;
7308 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_WAIT,
7309 "kstrgetmsg calls strwaitq:%p, %p",
7310 vp, uiop);
7311 if (((error = strwaitq(stp, waitflag, (ssize_t)0,
7312 fmode, timout, &done))) != 0 || done) {
7313 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_DONE,
7314 "kstrgetmsg error or done:%p, %p",
7315 vp, uiop);
7316 mutex_exit(&stp->sd_lock);
7317 return (error);
7319 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_AWAKE,
7320 "kstrgetmsg awakes:%p, %p", vp, uiop);
7321 if ((error = i_straccess(stp, JCREAD)) != 0) {
7322 mutex_exit(&stp->sd_lock);
7323 return (error);
7325 first = 0;
7327 ASSERT(bp != NULL);
7329 * Extract any mark information. If the message is not completely
7330 * consumed this information will be put in the mblk
7331 * that is putback.
7332 * If MSGMARKNEXT is set and the message is completely consumed
7333 * the STRATMARK flag will be set below. Likewise, if
7334 * MSGNOTMARKNEXT is set and the message is
7335 * completely consumed STRNOTATMARK will be set.
7337 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
7338 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
7339 (MSGMARKNEXT|MSGNOTMARKNEXT));
7340 pri = bp->b_band;
7341 if (mark != 0) {
7343 * If the caller doesn't want the mark return.
7344 * Used to implement MSG_WAITALL in sockets.
7346 if (flags & MSG_NOMARK) {
7347 putback(stp, q, bp, pri);
7348 qbackenable(q, pri);
7349 mutex_exit(&stp->sd_lock);
7350 return (EWOULDBLOCK);
7352 if (bp == stp->sd_mark) {
7353 mark |= _LASTMARK;
7354 stp->sd_mark = NULL;
7359 * keep track of the first message type
7361 type = bp->b_datap->db_type;
7363 if (bp->b_datap->db_type == M_PASSFP) {
7364 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7365 stp->sd_mark = bp;
7366 bp->b_flag |= mark & ~_LASTMARK;
7367 putback(stp, q, bp, pri);
7368 qbackenable(q, pri);
7369 mutex_exit(&stp->sd_lock);
7370 return (EBADMSG);
7372 ASSERT(type != M_SIG);
7374 if (flags & MSG_IPEEK) {
7376 * Clear any struioflag - we do the uiomove over again
7377 * when peeking since it simplifies the code.
7379 * Dup the message and put the original back on the queue.
7380 * If dupmsg() fails, try again with copymsg() to see if
7381 * there is indeed a shortage of memory. dupmsg() may fail
7382 * if db_ref in any of the messages reaches its limit.
7385 if ((nbp = dupmsg(bp)) == NULL && (nbp = copymsg(bp)) == NULL) {
7387 * Restore the state of the stream head since we
7388 * need to drop sd_lock (strwaitbuf is sleeping).
7390 size_t size = msgdsize(bp);
7392 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7393 stp->sd_mark = bp;
7394 bp->b_flag |= mark & ~_LASTMARK;
7395 putback(stp, q, bp, pri);
7396 mutex_exit(&stp->sd_lock);
7397 error = strwaitbuf(size, BPRI_HI);
7398 if (error) {
7400 * There is no net change to the queue thus
7401 * no need to qbackenable.
7403 return (error);
7405 goto retry;
7408 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7409 stp->sd_mark = bp;
7410 bp->b_flag |= mark & ~_LASTMARK;
7411 putback(stp, q, bp, pri);
7412 bp = nbp;
7416 * Set this flag so strrput will not generate signals. Need to
7417 * make sure this flag is cleared before leaving this routine
7418 * else signals will stop being sent.
7420 stp->sd_flag |= STRGETINPROG;
7421 mutex_exit(&stp->sd_lock);
7423 if ((stp->sd_rputdatafunc != NULL) && (DB_TYPE(bp) == M_DATA)) {
7424 mblk_t *tmp, *prevmp;
7427 * Put first non-data mblk back to stream head and
7428 * cut the mblk chain so sd_rputdatafunc only sees
7429 * M_DATA mblks. We can skip the first mblk since it
7430 * is M_DATA according to the condition above.
7432 for (prevmp = bp, tmp = bp->b_cont; tmp != NULL;
7433 prevmp = tmp, tmp = tmp->b_cont) {
7434 if (DB_TYPE(tmp) != M_DATA) {
7435 prevmp->b_cont = NULL;
7436 mutex_enter(&stp->sd_lock);
7437 putback(stp, q, tmp, tmp->b_band);
7438 mutex_exit(&stp->sd_lock);
7439 break;
7443 bp = (stp->sd_rputdatafunc)(stp->sd_vnode, bp,
7444 NULL, NULL, NULL, NULL);
7446 if (bp == NULL)
7447 goto retry;
7450 if (STREAM_NEEDSERVICE(stp))
7451 stream_runservice(stp);
7454 * Set HIPRI flag if message is priority.
7456 if (type >= QPCTL)
7457 flg = MSG_HIPRI;
7458 else
7459 flg = MSG_BAND;
7462 * First process PROTO or PCPROTO blocks, if any.
7464 if (mctlp != NULL && type != M_DATA) {
7465 mblk_t *nbp;
7467 *mctlp = bp;
7468 while (bp->b_cont && bp->b_cont->b_datap->db_type != M_DATA)
7469 bp = bp->b_cont;
7470 nbp = bp->b_cont;
7471 bp->b_cont = NULL;
7472 bp = nbp;
7475 if (bp && bp->b_datap->db_type != M_DATA) {
7477 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
7479 more |= MORECTL;
7480 savemp = bp;
7481 while (bp && bp->b_datap->db_type != M_DATA) {
7482 savemptail = bp;
7483 bp = bp->b_cont;
7485 savemptail->b_cont = NULL;
7489 * Now process DATA blocks, if any.
7491 if (uiop == NULL) {
7492 /* Append data to tail of mctlp */
7494 if (mctlp != NULL) {
7495 mblk_t **mpp = mctlp;
7497 while (*mpp != NULL)
7498 mpp = &((*mpp)->b_cont);
7499 *mpp = bp;
7500 bp = NULL;
7502 } else if (uiop->uio_resid >= 0 && bp) {
7503 size_t oldresid = uiop->uio_resid;
7506 * If a streams message is likely to consist
7507 * of many small mblks, it is pulled up into
7508 * one continuous chunk of memory.
7509 * The size of the first mblk may be bogus because
7510 * successive read() calls on the socket reduce
7511 * the size of this mblk until it is exhausted
7512 * and then the code walks on to the next. Thus
7513 * the size of the mblk may not be the original size
7514 * that was passed up, it's simply a remainder
7515 * and hence can be very small without any
7516 * implication that the packet is badly fragmented.
7517 * So the size of the possible second mblk is
7518 * used to spot a badly fragmented packet.
7519 * see longer comment at top of page
7520 * by mblk_pull_len declaration.
7523 if (bp->b_cont != NULL && MBLKL(bp->b_cont) < mblk_pull_len) {
7524 (void) pullupmsg(bp, -1);
7527 bp = struiocopyout(bp, uiop, &error);
7528 if (error != 0) {
7529 if (mctlp != NULL) {
7530 freemsg(*mctlp);
7531 *mctlp = NULL;
7532 } else
7533 freemsg(savemp);
7534 mutex_enter(&stp->sd_lock);
7536 * clear stream head hi pri flag based on
7537 * first message
7539 if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7540 ASSERT(type == M_PCPROTO);
7541 stp->sd_flag &= ~STRPRI;
7543 more = 0;
7544 goto getmout;
7547 * (pr == 1) indicates a partial read.
7549 if (oldresid > uiop->uio_resid)
7550 pr = 1;
7553 if (bp) { /* more data blocks in msg */
7554 more |= MOREDATA;
7555 if (savemp)
7556 savemptail->b_cont = bp;
7557 else
7558 savemp = bp;
7561 mutex_enter(&stp->sd_lock);
7562 if (savemp) {
7563 if (flags & (MSG_IPEEK|MSG_DISCARDTAIL)) {
7565 * When MSG_DISCARDTAIL is set or
7566 * when peeking discard any tail. When peeking this
7567 * is the tail of the dup that was copied out - the
7568 * message has already been putback on the queue.
7569 * Return MOREDATA to the caller even though the data
7570 * is discarded. This is used by sockets (to
7571 * set MSG_TRUNC).
7573 freemsg(savemp);
7574 if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7575 ASSERT(type == M_PCPROTO);
7576 stp->sd_flag &= ~STRPRI;
7578 } else if (pr && (savemp->b_datap->db_type == M_DATA) &&
7579 msgnodata(savemp)) {
7581 * Avoid queuing a zero-length tail part of
7582 * a message. pr=1 indicates that we read some of
7583 * the message.
7585 freemsg(savemp);
7586 more &= ~MOREDATA;
7587 if (type >= QPCTL) {
7588 ASSERT(type == M_PCPROTO);
7589 stp->sd_flag &= ~STRPRI;
7591 } else {
7592 savemp->b_band = pri;
7594 * If the first message was HIPRI and the one we're
7595 * putting back isn't, then clear STRPRI, otherwise
7596 * set STRPRI again. Note that we must set STRPRI
7597 * again since the flush logic in strrput_nondata()
7598 * may have cleared it while we had sd_lock dropped.
7601 if (type >= QPCTL) {
7602 ASSERT(type == M_PCPROTO);
7603 if (queclass(savemp) < QPCTL)
7604 stp->sd_flag &= ~STRPRI;
7605 else
7606 stp->sd_flag |= STRPRI;
7607 } else if (queclass(savemp) >= QPCTL) {
7609 * The first message was not a HIPRI message,
7610 * but the one we are about to putback is.
7611 * For simplicitly, we do not allow for HIPRI
7612 * messages to be embedded in the message
7613 * body, so just force it to same type as
7614 * first message.
7616 ASSERT(type == M_DATA || type == M_PROTO);
7617 ASSERT(savemp->b_datap->db_type == M_PCPROTO);
7618 savemp->b_datap->db_type = type;
7620 if (mark != 0) {
7621 if ((mark & _LASTMARK) &&
7622 (stp->sd_mark == NULL)) {
7624 * If another marked message arrived
7625 * while sd_lock was not held sd_mark
7626 * would be non-NULL.
7628 stp->sd_mark = savemp;
7630 savemp->b_flag |= mark & ~_LASTMARK;
7632 putback(stp, q, savemp, pri);
7634 } else if (!(flags & MSG_IPEEK)) {
7636 * The complete message was consumed.
7638 * If another M_PCPROTO arrived while sd_lock was not held
7639 * it would have been discarded since STRPRI was still set.
7641 * Move the MSG*MARKNEXT information
7642 * to the stream head just in case
7643 * the read queue becomes empty.
7644 * clear stream head hi pri flag based on
7645 * first message
7647 * If the stream head was at the mark
7648 * (STRATMARK) before we dropped sd_lock above
7649 * and some data was consumed then we have
7650 * moved past the mark thus STRATMARK is
7651 * cleared. However, if a message arrived in
7652 * strrput during the copyout above causing
7653 * STRATMARK to be set we can not clear that
7654 * flag.
7655 * XXX A "perimeter" would help by single-threading strrput,
7656 * strread, strgetmsg and kstrgetmsg.
7658 if (type >= QPCTL) {
7659 ASSERT(type == M_PCPROTO);
7660 stp->sd_flag &= ~STRPRI;
7662 if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
7663 if (mark & MSGMARKNEXT) {
7664 stp->sd_flag &= ~STRNOTATMARK;
7665 stp->sd_flag |= STRATMARK;
7666 } else if (mark & MSGNOTMARKNEXT) {
7667 stp->sd_flag &= ~STRATMARK;
7668 stp->sd_flag |= STRNOTATMARK;
7669 } else {
7670 stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
7672 } else if (pr && (old_sd_flag & STRATMARK)) {
7673 stp->sd_flag &= ~STRATMARK;
7677 *flagsp = flg;
7678 *prip = pri;
7681 * Getmsg cleanup processing - if the state of the queue has changed
7682 * some signals may need to be sent and/or poll awakened.
7684 getmout:
7685 qbackenable(q, pri);
7688 * We dropped the stream head lock above. Send all M_SIG messages
7689 * before processing stream head for SIGPOLL messages.
7691 ASSERT(MUTEX_HELD(&stp->sd_lock));
7692 while ((bp = q->q_first) != NULL &&
7693 (bp->b_datap->db_type == M_SIG)) {
7695 * sd_lock is held so the content of the read queue can not
7696 * change.
7698 bp = getq(q);
7699 ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7701 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7702 mutex_exit(&stp->sd_lock);
7703 freemsg(bp);
7704 if (STREAM_NEEDSERVICE(stp))
7705 stream_runservice(stp);
7706 mutex_enter(&stp->sd_lock);
7710 * stream head cannot change while we make the determination
7711 * whether or not to send a signal. Drop the flag to allow strrput
7712 * to send firstmsgsigs again.
7714 stp->sd_flag &= ~STRGETINPROG;
7717 * If the type of message at the front of the queue changed
7718 * due to the receive the appropriate signals and pollwakeup events
7719 * are generated. The type of changes are:
7720 * Processed a hipri message, q_first is not hipri.
7721 * Processed a band X message, and q_first is band Y.
7722 * The generated signals and pollwakeups are identical to what
7723 * strrput() generates should the message that is now on q_first
7724 * arrive to an empty read queue.
7726 * Note: only strrput will send a signal for a hipri message.
7728 if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7729 strsigset_t signals = 0;
7730 strpollset_t pollwakeups = 0;
7732 if (flg & MSG_HIPRI) {
7734 * Removed a hipri message. Regular data at
7735 * the front of the queue.
7737 if (bp->b_band == 0) {
7738 signals = S_INPUT | S_RDNORM;
7739 pollwakeups = POLLIN | POLLRDNORM;
7740 } else {
7741 signals = S_INPUT | S_RDBAND;
7742 pollwakeups = POLLIN | POLLRDBAND;
7744 } else if (pri != bp->b_band) {
7746 * The band is different for the new q_first.
7748 if (bp->b_band == 0) {
7749 signals = S_RDNORM;
7750 pollwakeups = POLLIN | POLLRDNORM;
7751 } else {
7752 signals = S_RDBAND;
7753 pollwakeups = POLLIN | POLLRDBAND;
7757 if (pollwakeups != 0) {
7758 if (pollwakeups == (POLLIN | POLLRDNORM)) {
7759 if (!(stp->sd_rput_opt & SR_POLLIN))
7760 goto no_pollwake;
7761 stp->sd_rput_opt &= ~SR_POLLIN;
7763 mutex_exit(&stp->sd_lock);
7764 pollwakeup(&stp->sd_pollist, pollwakeups);
7765 mutex_enter(&stp->sd_lock);
7767 no_pollwake:
7769 if (stp->sd_sigflags & signals)
7770 strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7772 mutex_exit(&stp->sd_lock);
7774 rvp->r_val1 = more;
7775 return (error);
7776 #undef _LASTMARK
7780 * Put a message downstream.
7782 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7785 strputmsg(
7786 struct vnode *vp,
7787 struct strbuf *mctl,
7788 struct strbuf *mdata,
7789 unsigned char pri,
7790 int flag,
7791 int fmode)
7793 struct stdata *stp;
7794 queue_t *wqp;
7795 mblk_t *mp;
7796 ssize_t msgsize;
7797 ssize_t rmin, rmax;
7798 int error;
7799 struct uio uios;
7800 struct uio *uiop = &uios;
7801 struct iovec iovs;
7802 int xpg4 = 0;
7804 ASSERT(vp->v_stream);
7805 stp = vp->v_stream;
7806 wqp = stp->sd_wrq;
7809 * If it is an XPG4 application, we need to send
7810 * SIGPIPE below
7813 xpg4 = (flag & MSG_XPG4) ? 1 : 0;
7814 flag &= ~MSG_XPG4;
7816 if (AU_AUDITING())
7817 audit_strputmsg(vp, mctl, mdata, pri, flag, fmode);
7819 mutex_enter(&stp->sd_lock);
7821 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7822 mutex_exit(&stp->sd_lock);
7823 return (error);
7826 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7827 error = strwriteable(stp, B_FALSE, xpg4);
7828 if (error != 0) {
7829 mutex_exit(&stp->sd_lock);
7830 return (error);
7834 mutex_exit(&stp->sd_lock);
7837 * Check for legal flag value.
7839 switch (flag) {
7840 case MSG_HIPRI:
7841 if ((mctl->len < 0) || (pri != 0))
7842 return (EINVAL);
7843 break;
7844 case MSG_BAND:
7845 break;
7847 default:
7848 return (EINVAL);
7851 TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_IN,
7852 "strputmsg in:stp %p", stp);
7854 /* get these values from those cached in the stream head */
7855 rmin = stp->sd_qn_minpsz;
7856 rmax = stp->sd_qn_maxpsz;
7859 * Make sure ctl and data sizes together fall within the
7860 * limits of the max and min receive packet sizes and do
7861 * not exceed system limit.
7863 ASSERT((rmax >= 0) || (rmax == INFPSZ));
7864 if (rmax == 0) {
7865 return (ERANGE);
7868 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
7869 * Needed to prevent partial failures in the strmakedata loop.
7871 if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
7872 rmax = stp->sd_maxblk;
7874 if ((msgsize = mdata->len) < 0) {
7875 msgsize = 0;
7876 rmin = 0; /* no range check for NULL data part */
7878 if ((msgsize < rmin) ||
7879 ((msgsize > rmax) && (rmax != INFPSZ)) ||
7880 (mctl->len > strctlsz)) {
7881 return (ERANGE);
7885 * Setup uio and iov for data part
7887 iovs.iov_base = mdata->buf;
7888 iovs.iov_len = msgsize;
7889 uios.uio_iov = &iovs;
7890 uios.uio_iovcnt = 1;
7891 uios.uio_loffset = 0;
7892 uios.uio_segflg = UIO_USERSPACE;
7893 uios.uio_fmode = fmode;
7894 uios.uio_extflg = UIO_COPY_DEFAULT;
7895 uios.uio_resid = msgsize;
7896 uios.uio_offset = 0;
7898 /* Ignore flow control in strput for HIPRI */
7899 if (flag & MSG_HIPRI)
7900 flag |= MSG_IGNFLOW;
7902 for (;;) {
7903 int done = 0;
7906 * strput will always free the ctl mblk - even when strput
7907 * fails.
7909 if ((error = strmakectl(mctl, flag, fmode, &mp)) != 0) {
7910 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7911 "strputmsg out:stp %p out %d error %d",
7912 stp, 1, error);
7913 return (error);
7916 * Verify that the whole message can be transferred by
7917 * strput.
7919 ASSERT(stp->sd_maxblk == INFPSZ ||
7920 stp->sd_maxblk >= mdata->len);
7922 msgsize = mdata->len;
7923 error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
7924 mdata->len = msgsize;
7926 if (error == 0)
7927 break;
7929 if (error != EWOULDBLOCK)
7930 goto out;
7932 mutex_enter(&stp->sd_lock);
7934 * Check for a missed wakeup.
7935 * Needed since strput did not hold sd_lock across
7936 * the canputnext.
7938 if (bcanputnext(wqp, pri)) {
7939 /* Try again */
7940 mutex_exit(&stp->sd_lock);
7941 continue;
7943 TRACE_2(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAIT,
7944 "strputmsg wait:stp %p waits pri %d", stp, pri);
7945 if (((error = strwaitq(stp, WRITEWAIT, (ssize_t)0, fmode, -1,
7946 &done)) != 0) || done) {
7947 mutex_exit(&stp->sd_lock);
7948 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7949 "strputmsg out:q %p out %d error %d",
7950 stp, 0, error);
7951 return (error);
7953 TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAKE,
7954 "strputmsg wake:stp %p wakes", stp);
7955 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7956 mutex_exit(&stp->sd_lock);
7957 return (error);
7959 mutex_exit(&stp->sd_lock);
7961 out:
7963 * For historic reasons, applications expect EAGAIN
7964 * when data mblk could not be allocated. so change
7965 * ENOMEM back to EAGAIN
7967 if (error == ENOMEM)
7968 error = EAGAIN;
7969 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7970 "strputmsg out:stp %p out %d error %d", stp, 2, error);
7971 return (error);
7975 * Put a message downstream.
7976 * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
7977 * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
7978 * and the fmode parameter.
7980 * This routine handles the consolidation private flags:
7981 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7982 * MSG_HOLDSIG Hold signals while waiting for data.
7983 * MSG_IGNFLOW Don't check streams flow control.
7985 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7988 kstrputmsg(
7989 struct vnode *vp,
7990 mblk_t *mctl,
7991 struct uio *uiop,
7992 ssize_t msgsize,
7993 unsigned char pri,
7994 int flag,
7995 int fmode)
7997 struct stdata *stp;
7998 queue_t *wqp;
7999 ssize_t rmin, rmax;
8000 int error;
8002 ASSERT(vp->v_stream);
8003 stp = vp->v_stream;
8004 wqp = stp->sd_wrq;
8005 if (AU_AUDITING())
8006 audit_strputmsg(vp, NULL, NULL, pri, flag, fmode);
8007 if (mctl == NULL)
8008 return (EINVAL);
8010 mutex_enter(&stp->sd_lock);
8012 if ((error = i_straccess(stp, JCWRITE)) != 0) {
8013 mutex_exit(&stp->sd_lock);
8014 freemsg(mctl);
8015 return (error);
8018 if ((stp->sd_flag & STPLEX) || !(flag & MSG_IGNERROR)) {
8019 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
8020 error = strwriteable(stp, B_FALSE, B_TRUE);
8021 if (error != 0) {
8022 mutex_exit(&stp->sd_lock);
8023 freemsg(mctl);
8024 return (error);
8029 mutex_exit(&stp->sd_lock);
8032 * Check for legal flag value.
8034 switch (flag & (MSG_HIPRI|MSG_BAND|MSG_ANY)) {
8035 case MSG_HIPRI:
8036 if (pri != 0) {
8037 freemsg(mctl);
8038 return (EINVAL);
8040 break;
8041 case MSG_BAND:
8042 break;
8043 default:
8044 freemsg(mctl);
8045 return (EINVAL);
8048 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_IN,
8049 "kstrputmsg in:stp %p", stp);
8051 /* get these values from those cached in the stream head */
8052 rmin = stp->sd_qn_minpsz;
8053 rmax = stp->sd_qn_maxpsz;
8056 * Make sure ctl and data sizes together fall within the
8057 * limits of the max and min receive packet sizes and do
8058 * not exceed system limit.
8060 ASSERT((rmax >= 0) || (rmax == INFPSZ));
8061 if (rmax == 0) {
8062 freemsg(mctl);
8063 return (ERANGE);
8066 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
8067 * Needed to prevent partial failures in the strmakedata loop.
8069 if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
8070 rmax = stp->sd_maxblk;
8072 if (uiop == NULL) {
8073 msgsize = -1;
8074 rmin = -1; /* no range check for NULL data part */
8075 } else {
8076 /* Use uio flags as well as the fmode parameter flags */
8077 fmode |= uiop->uio_fmode;
8079 if ((msgsize < rmin) ||
8080 ((msgsize > rmax) && (rmax != INFPSZ))) {
8081 freemsg(mctl);
8082 return (ERANGE);
8086 /* Ignore flow control in strput for HIPRI */
8087 if (flag & MSG_HIPRI)
8088 flag |= MSG_IGNFLOW;
8090 for (;;) {
8091 int done = 0;
8092 int waitflag;
8093 mblk_t *mp;
8096 * strput will always free the ctl mblk - even when strput
8097 * fails. If MSG_IGNFLOW is set then any error returned
8098 * will cause us to break the loop, so we don't need a copy
8099 * of the message. If MSG_IGNFLOW is not set, then we can
8100 * get hit by flow control and be forced to try again. In
8101 * this case we need to have a copy of the message. We
8102 * do this using copymsg since the message may get modified
8103 * by something below us.
8105 * We've observed that many TPI providers do not check db_ref
8106 * on the control messages but blindly reuse them for the
8107 * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
8108 * friendly to such providers than using dupmsg. Also, note
8109 * that sockfs uses MSG_IGNFLOW for all TPI control messages.
8110 * Only data messages are subject to flow control, hence
8111 * subject to this copymsg.
8113 if (flag & MSG_IGNFLOW) {
8114 mp = mctl;
8115 mctl = NULL;
8116 } else {
8117 do {
8119 * If a message has a free pointer, the message
8120 * must be dupmsg to maintain this pointer.
8121 * Code using this facility must be sure
8122 * that modules below will not change the
8123 * contents of the dblk without checking db_ref
8124 * first. If db_ref is > 1, then the module
8125 * needs to do a copymsg first. Otherwise,
8126 * the contents of the dblk may become
8127 * inconsistent because the freesmg/freeb below
8128 * may end up calling atomic_add_32_nv.
8129 * The atomic_add_32_nv in freeb (accessing
8130 * all of db_ref, db_type, db_flags, and
8131 * db_struioflag) does not prevent other threads
8132 * from concurrently trying to modify e.g.
8133 * db_type.
8135 if (mctl->b_datap->db_frtnp != NULL)
8136 mp = dupmsg(mctl);
8137 else
8138 mp = copymsg(mctl);
8140 if (mp != NULL)
8141 break;
8143 error = strwaitbuf(msgdsize(mctl), BPRI_MED);
8144 if (error) {
8145 freemsg(mctl);
8146 return (error);
8148 } while (mp == NULL);
8151 * Verify that all of msgsize can be transferred by
8152 * strput.
8154 ASSERT(stp->sd_maxblk == INFPSZ || stp->sd_maxblk >= msgsize);
8155 error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
8156 if (error == 0)
8157 break;
8159 if (error != EWOULDBLOCK)
8160 goto out;
8163 * IF MSG_IGNFLOW is set we should have broken out of loop
8164 * above.
8166 ASSERT(!(flag & MSG_IGNFLOW));
8167 mutex_enter(&stp->sd_lock);
8169 * Check for a missed wakeup.
8170 * Needed since strput did not hold sd_lock across
8171 * the canputnext.
8173 if (bcanputnext(wqp, pri)) {
8174 /* Try again */
8175 mutex_exit(&stp->sd_lock);
8176 continue;
8178 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAIT,
8179 "kstrputmsg wait:stp %p waits pri %d", stp, pri);
8181 waitflag = WRITEWAIT;
8182 if (flag & (MSG_HOLDSIG|MSG_IGNERROR)) {
8183 if (flag & MSG_HOLDSIG)
8184 waitflag |= STR_NOSIG;
8185 if (flag & MSG_IGNERROR)
8186 waitflag |= STR_NOERROR;
8188 if (((error = strwaitq(stp, waitflag,
8189 (ssize_t)0, fmode, -1, &done)) != 0) || done) {
8190 mutex_exit(&stp->sd_lock);
8191 TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8192 "kstrputmsg out:stp %p out %d error %d",
8193 stp, 0, error);
8194 freemsg(mctl);
8195 return (error);
8197 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAKE,
8198 "kstrputmsg wake:stp %p wakes", stp);
8199 if ((error = i_straccess(stp, JCWRITE)) != 0) {
8200 mutex_exit(&stp->sd_lock);
8201 freemsg(mctl);
8202 return (error);
8204 mutex_exit(&stp->sd_lock);
8206 out:
8207 freemsg(mctl);
8209 * For historic reasons, applications expect EAGAIN
8210 * when data mblk could not be allocated. so change
8211 * ENOMEM back to EAGAIN
8213 if (error == ENOMEM)
8214 error = EAGAIN;
8215 TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8216 "kstrputmsg out:stp %p out %d error %d", stp, 2, error);
8217 return (error);
8221 * Determines whether the necessary conditions are set on a stream
8222 * for it to be readable, writeable, or have exceptions.
8224 * strpoll handles the consolidation private events:
8225 * POLLNOERR Do not return POLLERR even if there are stream
8226 * head errors.
8227 * Used by sockfs.
8228 * POLLRDDATA Do not return POLLIN unless at least one message on
8229 * the queue contains one or more M_DATA mblks. Thus
8230 * when this flag is set a queue with only
8231 * M_PROTO/M_PCPROTO mblks does not return POLLIN.
8232 * Used by sockfs to ignore T_EXDATA_IND messages.
8234 * Note: POLLRDDATA assumes that synch streams only return messages with
8235 * an M_DATA attached (i.e. not messages consisting of only
8236 * an M_PROTO/M_PCPROTO part).
8239 strpoll(struct stdata *stp, short events_arg, int anyyet, short *reventsp,
8240 struct pollhead **phpp)
8242 int events = (ushort_t)events_arg;
8243 int retevents = 0;
8244 mblk_t *mp;
8245 qband_t *qbp;
8246 long sd_flags = stp->sd_flag;
8247 int headlocked = 0;
8250 * For performance, a single 'if' tests for most possible edge
8251 * conditions in one shot
8253 if (sd_flags & (STPLEX | STRDERR | STWRERR)) {
8254 if (sd_flags & STPLEX) {
8255 *reventsp = POLLNVAL;
8256 return (EINVAL);
8258 if (((events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) &&
8259 (sd_flags & STRDERR)) ||
8260 ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) &&
8261 (sd_flags & STWRERR))) {
8262 if (!(events & POLLNOERR)) {
8263 *reventsp = POLLERR;
8264 return (0);
8268 if (sd_flags & STRHUP) {
8269 retevents |= POLLHUP;
8270 } else if (events & (POLLWRNORM | POLLWRBAND)) {
8271 queue_t *tq;
8272 queue_t *qp = stp->sd_wrq;
8274 claimstr(qp);
8275 /* Find next module forward that has a service procedure */
8276 tq = qp->q_next->q_nfsrv;
8277 ASSERT(tq != NULL);
8279 if (polllock(&stp->sd_pollist, QLOCK(tq)) != 0) {
8280 releasestr(qp);
8281 *reventsp = POLLNVAL;
8282 return (0);
8284 if (events & POLLWRNORM) {
8285 queue_t *sqp;
8287 if (tq->q_flag & QFULL)
8288 /* ensure backq svc procedure runs */
8289 tq->q_flag |= QWANTW;
8290 else if ((sqp = stp->sd_struiowrq) != NULL) {
8291 /* Check sync stream barrier write q */
8292 mutex_exit(QLOCK(tq));
8293 if (polllock(&stp->sd_pollist,
8294 QLOCK(sqp)) != 0) {
8295 releasestr(qp);
8296 *reventsp = POLLNVAL;
8297 return (0);
8299 if (sqp->q_flag & QFULL)
8300 /* ensure pollwakeup() is done */
8301 sqp->q_flag |= QWANTWSYNC;
8302 else
8303 retevents |= POLLOUT;
8304 /* More write events to process ??? */
8305 if (! (events & POLLWRBAND)) {
8306 mutex_exit(QLOCK(sqp));
8307 releasestr(qp);
8308 goto chkrd;
8310 mutex_exit(QLOCK(sqp));
8311 if (polllock(&stp->sd_pollist,
8312 QLOCK(tq)) != 0) {
8313 releasestr(qp);
8314 *reventsp = POLLNVAL;
8315 return (0);
8317 } else
8318 retevents |= POLLOUT;
8320 if (events & POLLWRBAND) {
8321 qbp = tq->q_bandp;
8322 if (qbp) {
8323 while (qbp) {
8324 if (qbp->qb_flag & QB_FULL)
8325 qbp->qb_flag |= QB_WANTW;
8326 else
8327 retevents |= POLLWRBAND;
8328 qbp = qbp->qb_next;
8330 } else {
8331 retevents |= POLLWRBAND;
8334 mutex_exit(QLOCK(tq));
8335 releasestr(qp);
8337 chkrd:
8338 if (sd_flags & STRPRI) {
8339 retevents |= (events & POLLPRI);
8340 } else if (events & (POLLRDNORM | POLLRDBAND | POLLIN)) {
8341 queue_t *qp = _RD(stp->sd_wrq);
8342 int normevents = (events & (POLLIN | POLLRDNORM));
8345 * Note: Need to do polllock() here since ps_lock may be
8346 * held. See bug 4191544.
8348 if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8349 *reventsp = POLLNVAL;
8350 return (0);
8352 headlocked = 1;
8353 mp = qp->q_first;
8354 while (mp) {
8356 * For POLLRDDATA we scan b_cont and b_next until we
8357 * find an M_DATA.
8359 if ((events & POLLRDDATA) &&
8360 mp->b_datap->db_type != M_DATA) {
8361 mblk_t *nmp = mp->b_cont;
8363 while (nmp != NULL &&
8364 nmp->b_datap->db_type != M_DATA)
8365 nmp = nmp->b_cont;
8366 if (nmp == NULL) {
8367 mp = mp->b_next;
8368 continue;
8371 if (mp->b_band == 0)
8372 retevents |= normevents;
8373 else
8374 retevents |= (events & (POLLIN | POLLRDBAND));
8375 break;
8377 if (!(retevents & normevents) && (stp->sd_wakeq & RSLEEP)) {
8379 * Sync stream barrier read queue has data.
8381 retevents |= normevents;
8383 /* Treat eof as normal data */
8384 if (sd_flags & STREOF)
8385 retevents |= normevents;
8389 * Pass back a pollhead if no events are pending or if edge-triggering
8390 * has been configured on this resource.
8392 if ((retevents == 0 && !anyyet) || (events & POLLET)) {
8393 *phpp = &stp->sd_pollist;
8394 if (headlocked == 0) {
8395 if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8396 *reventsp = POLLNVAL;
8397 return (0);
8399 headlocked = 1;
8401 stp->sd_rput_opt |= SR_POLLIN;
8404 *reventsp = (short)retevents;
8405 if (headlocked)
8406 mutex_exit(&stp->sd_lock);
8407 return (0);
8411 * The purpose of putback() is to assure sleeping polls/reads
8412 * are awakened when there are no new messages arriving at the,
8413 * stream head, and a message is placed back on the read queue.
8415 * sd_lock must be held when messages are placed back on stream
8416 * head. (getq() holds sd_lock when it removes messages from
8417 * the queue)
8420 static void
8421 putback(struct stdata *stp, queue_t *q, mblk_t *bp, int band)
8423 mblk_t *qfirst;
8424 ASSERT(MUTEX_HELD(&stp->sd_lock));
8427 * As a result of lock-step ordering around q_lock and sd_lock,
8428 * it's possible for function calls like putnext() and
8429 * canputnext() to get an inaccurate picture of how much
8430 * data is really being processed at the stream head.
8431 * We only consolidate with existing messages on the queue
8432 * if the length of the message we want to put back is smaller
8433 * than the queue hiwater mark.
8435 if ((stp->sd_rput_opt & SR_CONSOL_DATA) &&
8436 (DB_TYPE(bp) == M_DATA) && ((qfirst = q->q_first) != NULL) &&
8437 (DB_TYPE(qfirst) == M_DATA) &&
8438 ((qfirst->b_flag & (MSGMARK|MSGDELIM)) == 0) &&
8439 ((bp->b_flag & (MSGMARK|MSGDELIM|MSGMARKNEXT)) == 0) &&
8440 (mp_cont_len(bp, NULL) < q->q_hiwat)) {
8442 * We use the same logic as defined in strrput()
8443 * but in reverse as we are putting back onto the
8444 * queue and want to retain byte ordering.
8445 * Consolidate M_DATA messages with M_DATA ONLY.
8446 * strrput() allows the consolidation of M_DATA onto
8447 * M_PROTO | M_PCPROTO but not the other way round.
8449 * The consolidation does not take place if the message
8450 * we are returning to the queue is marked with either
8451 * of the marks or the delim flag or if q_first
8452 * is marked with MSGMARK. The MSGMARK check is needed to
8453 * handle the odd semantics of MSGMARK where essentially
8454 * the whole message is to be treated as marked.
8455 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from q_first
8456 * to the front of the b_cont chain.
8458 rmvq_noenab(q, qfirst);
8461 * The first message in the b_cont list
8462 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
8463 * We need to handle the case where we
8464 * are appending:
8466 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
8467 * 2) a MSGMARKNEXT to a plain message.
8468 * 3) a MSGNOTMARKNEXT to a plain message
8469 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
8470 * message.
8472 * Thus we never append a MSGMARKNEXT or
8473 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
8475 if (qfirst->b_flag & MSGMARKNEXT) {
8476 bp->b_flag |= MSGMARKNEXT;
8477 bp->b_flag &= ~MSGNOTMARKNEXT;
8478 qfirst->b_flag &= ~MSGMARKNEXT;
8479 } else if (qfirst->b_flag & MSGNOTMARKNEXT) {
8480 bp->b_flag |= MSGNOTMARKNEXT;
8481 qfirst->b_flag &= ~MSGNOTMARKNEXT;
8484 linkb(bp, qfirst);
8486 (void) putbq(q, bp);
8489 * A message may have come in when the sd_lock was dropped in the
8490 * calling routine. If this is the case and STR*ATMARK info was
8491 * received, need to move that from the stream head to the q_last
8492 * so that SIOCATMARK can return the proper value.
8494 if (stp->sd_flag & (STRATMARK | STRNOTATMARK)) {
8495 unsigned short *flagp = &q->q_last->b_flag;
8496 uint_t b_flag = (uint_t)*flagp;
8498 if (stp->sd_flag & STRATMARK) {
8499 b_flag &= ~MSGNOTMARKNEXT;
8500 b_flag |= MSGMARKNEXT;
8501 stp->sd_flag &= ~STRATMARK;
8502 } else {
8503 b_flag &= ~MSGMARKNEXT;
8504 b_flag |= MSGNOTMARKNEXT;
8505 stp->sd_flag &= ~STRNOTATMARK;
8507 *flagp = (unsigned short) b_flag;
8510 #ifdef DEBUG
8512 * Make sure that the flags are not messed up.
8515 mblk_t *mp;
8516 mp = q->q_last;
8517 while (mp != NULL) {
8518 ASSERT((mp->b_flag & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
8519 (MSGMARKNEXT|MSGNOTMARKNEXT));
8520 mp = mp->b_cont;
8523 #endif
8524 if (q->q_first == bp) {
8525 short pollevents;
8527 if (stp->sd_flag & RSLEEP) {
8528 stp->sd_flag &= ~RSLEEP;
8529 cv_broadcast(&q->q_wait);
8531 if (stp->sd_flag & STRPRI) {
8532 pollevents = POLLPRI;
8533 } else {
8534 if (band == 0) {
8535 if (!(stp->sd_rput_opt & SR_POLLIN))
8536 return;
8537 stp->sd_rput_opt &= ~SR_POLLIN;
8538 pollevents = POLLIN | POLLRDNORM;
8539 } else {
8540 pollevents = POLLIN | POLLRDBAND;
8543 mutex_exit(&stp->sd_lock);
8544 pollwakeup(&stp->sd_pollist, pollevents);
8545 mutex_enter(&stp->sd_lock);
8550 * Return the held vnode attached to the stream head of a
8551 * given queue
8552 * It is the responsibility of the calling routine to ensure
8553 * that the queue does not go away (e.g. pop).
8555 vnode_t *
8556 strq2vp(queue_t *qp)
8558 vnode_t *vp;
8559 vp = STREAM(qp)->sd_vnode;
8560 ASSERT(vp != NULL);
8561 VN_HOLD(vp);
8562 return (vp);
8566 * return the stream head write queue for the given vp
8567 * It is the responsibility of the calling routine to ensure
8568 * that the stream or vnode do not close.
8570 queue_t *
8571 strvp2wq(vnode_t *vp)
8573 ASSERT(vp->v_stream != NULL);
8574 return (vp->v_stream->sd_wrq);
8578 * pollwakeup stream head
8579 * It is the responsibility of the calling routine to ensure
8580 * that the stream or vnode do not close.
8582 void
8583 strpollwakeup(vnode_t *vp, short event)
8585 ASSERT(vp->v_stream);
8586 pollwakeup(&vp->v_stream->sd_pollist, event);
8590 * Mate the stream heads of two vnodes together. If the two vnodes are the
8591 * same, we just make the write-side point at the read-side -- otherwise,
8592 * we do a full mate. Only works on vnodes associated with streams that are
8593 * still being built and thus have only a stream head.
8595 void
8596 strmate(vnode_t *vp1, vnode_t *vp2)
8598 queue_t *wrq1 = strvp2wq(vp1);
8599 queue_t *wrq2 = strvp2wq(vp2);
8602 * Verify that there are no modules on the stream yet. We also
8603 * rely on the stream head always having a service procedure to
8604 * avoid tweaking q_nfsrv.
8606 ASSERT(wrq1->q_next == NULL && wrq2->q_next == NULL);
8607 ASSERT(wrq1->q_qinfo->qi_srvp != NULL);
8608 ASSERT(wrq2->q_qinfo->qi_srvp != NULL);
8611 * If the queues are the same, just twist; otherwise do a full mate.
8613 if (wrq1 == wrq2) {
8614 wrq1->q_next = _RD(wrq1);
8615 } else {
8616 wrq1->q_next = _RD(wrq2);
8617 wrq2->q_next = _RD(wrq1);
8618 STREAM(wrq1)->sd_mate = STREAM(wrq2);
8619 STREAM(wrq1)->sd_flag |= STRMATE;
8620 STREAM(wrq2)->sd_mate = STREAM(wrq1);
8621 STREAM(wrq2)->sd_flag |= STRMATE;
8626 * XXX will go away when console is correctly fixed.
8627 * Clean up the console PIDS, from previous I_SETSIG,
8628 * called only for cnopen which never calls strclean().
8630 void
8631 str_cn_clean(struct vnode *vp)
8633 strsig_t *ssp, *pssp, *tssp;
8634 struct stdata *stp;
8635 struct pid *pidp;
8636 int update = 0;
8638 ASSERT(vp->v_stream);
8639 stp = vp->v_stream;
8640 pssp = NULL;
8641 mutex_enter(&stp->sd_lock);
8642 ssp = stp->sd_siglist;
8643 while (ssp) {
8644 mutex_enter(&pidlock);
8645 pidp = ssp->ss_pidp;
8647 * Get rid of PID if the proc is gone.
8649 if (pidp->pid_prinactive) {
8650 tssp = ssp->ss_next;
8651 if (pssp)
8652 pssp->ss_next = tssp;
8653 else
8654 stp->sd_siglist = tssp;
8655 ASSERT(pidp->pid_ref <= 1);
8656 PID_RELE(ssp->ss_pidp);
8657 mutex_exit(&pidlock);
8658 kmem_free(ssp, sizeof (strsig_t));
8659 update = 1;
8660 ssp = tssp;
8661 continue;
8662 } else
8663 mutex_exit(&pidlock);
8664 pssp = ssp;
8665 ssp = ssp->ss_next;
8667 if (update) {
8668 stp->sd_sigflags = 0;
8669 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
8670 stp->sd_sigflags |= ssp->ss_events;
8672 mutex_exit(&stp->sd_lock);
8676 * Return B_TRUE if there is data in the message, B_FALSE otherwise.
8678 static boolean_t
8679 msghasdata(mblk_t *bp)
8681 for (; bp; bp = bp->b_cont)
8682 if (bp->b_datap->db_type == M_DATA) {
8683 ASSERT(bp->b_wptr >= bp->b_rptr);
8684 if (bp->b_wptr > bp->b_rptr)
8685 return (B_TRUE);
8687 return (B_FALSE);