Print this page
12306 XPG4v2 slave pty behaviour should generally be disabled
Reviewed by: Robert Mustacchi <rm@fingolfin.org>
Change-ID: I7ccd399c22866f34dd20c6bb9d28e77ba4e24c67
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/os/streamio.c
+++ new/usr/src/uts/common/os/streamio.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
22 22 /* All Rights Reserved */
23 23
24 24
25 25 /*
26 26 * Copyright (c) 1988, 2010, Oracle and/or its affiliates. All rights reserved.
27 27 * Copyright 2017 Joyent, Inc.
28 - * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
28 + * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
29 29 */
30 30
31 31 #include <sys/types.h>
32 32 #include <sys/sysmacros.h>
33 33 #include <sys/param.h>
34 34 #include <sys/errno.h>
35 35 #include <sys/signal.h>
36 36 #include <sys/stat.h>
37 37 #include <sys/proc.h>
38 38 #include <sys/cred.h>
39 39 #include <sys/user.h>
40 40 #include <sys/vnode.h>
41 41 #include <sys/file.h>
42 42 #include <sys/stream.h>
43 43 #include <sys/strsubr.h>
44 44 #include <sys/stropts.h>
45 45 #include <sys/tihdr.h>
46 46 #include <sys/var.h>
47 47 #include <sys/poll.h>
48 48 #include <sys/termio.h>
49 49 #include <sys/ttold.h>
50 50 #include <sys/systm.h>
51 51 #include <sys/uio.h>
52 52 #include <sys/cmn_err.h>
53 53 #include <sys/sad.h>
54 54 #include <sys/netstack.h>
55 55 #include <sys/priocntl.h>
56 56 #include <sys/jioctl.h>
57 57 #include <sys/procset.h>
58 58 #include <sys/session.h>
59 59 #include <sys/kmem.h>
60 60 #include <sys/filio.h>
61 61 #include <sys/vtrace.h>
62 62 #include <sys/debug.h>
63 63 #include <sys/strredir.h>
64 64 #include <sys/fs/fifonode.h>
65 65 #include <sys/fs/snode.h>
66 66 #include <sys/strlog.h>
67 67 #include <sys/strsun.h>
68 68 #include <sys/project.h>
69 69 #include <sys/kbio.h>
70 70 #include <sys/msio.h>
↓ open down ↓ |
32 lines elided |
↑ open up ↑ |
71 71 #include <sys/tty.h>
72 72 #include <sys/ptyvar.h>
73 73 #include <sys/vuid_event.h>
74 74 #include <sys/modctl.h>
75 75 #include <sys/sunddi.h>
76 76 #include <sys/sunldi_impl.h>
77 77 #include <sys/autoconf.h>
78 78 #include <sys/policy.h>
79 79 #include <sys/dld.h>
80 80 #include <sys/zone.h>
81 +#include <sys/ptms.h>
81 82 #include <c2/audit.h>
82 83
83 84 /*
84 85 * This define helps improve the readability of streams code while
85 86 * still maintaining a very old streams performance enhancement. The
86 87 * performance enhancement basically involved having all callers
87 88 * of straccess() perform the first check that straccess() will do
88 89 * locally before actually calling straccess(). (There by reducing
89 90 * the number of unnecessary calls to straccess().)
90 91 */
91 92 #define i_straccess(x, y) ((stp->sd_sidp == NULL) ? 0 : \
92 93 (stp->sd_vnode->v_type == VFIFO) ? 0 : \
93 94 straccess((x), (y)))
94 95
95 96 /*
96 97 * what is mblk_pull_len?
97 98 *
98 99 * If a streams message consists of many short messages,
99 100 * a performance degradation occurs from copyout overhead.
100 101 * To decrease the per mblk overhead, messages that are
101 102 * likely to consist of many small mblks are pulled up into
102 103 * one continuous chunk of memory.
103 104 *
104 105 * To avoid the processing overhead of examining every
105 106 * mblk, a quick heuristic is used. If the first mblk in
106 107 * the message is shorter than mblk_pull_len, it is likely
107 108 * that the rest of the mblk will be short.
108 109 *
109 110 * This heuristic was decided upon after performance tests
110 111 * indicated that anything more complex slowed down the main
111 112 * code path.
112 113 */
113 114 #define MBLK_PULL_LEN 64
114 115 uint32_t mblk_pull_len = MBLK_PULL_LEN;
115 116
116 117 /*
117 118 * The sgttyb_handling flag controls the handling of the old BSD
118 119 * TIOCGETP, TIOCSETP, and TIOCSETN ioctls as follows:
119 120 *
120 121 * 0 - Emit no warnings at all and retain old, broken behavior.
121 122 * 1 - Emit no warnings and silently handle new semantics.
122 123 * 2 - Send cmn_err(CE_NOTE) when either TIOCSETP or TIOCSETN is used
123 124 * (once per system invocation). Handle with new semantics.
124 125 * 3 - Send SIGSYS when any TIOCGETP, TIOCSETP, or TIOCSETN call is
125 126 * made (so that offenders drop core and are easy to debug).
126 127 *
127 128 * The "new semantics" are that TIOCGETP returns B38400 for
128 129 * sg_[io]speed if the corresponding value is over B38400, and that
129 130 * TIOCSET[PN] accept B38400 in these cases to mean "retain current
130 131 * bit rate."
131 132 */
132 133 int sgttyb_handling = 1;
133 134 static boolean_t sgttyb_complaint;
134 135
135 136 /* don't push drcompat module by default on Style-2 streams */
136 137 static int push_drcompat = 0;
137 138
138 139 /*
139 140 * id value used to distinguish between different ioctl messages
140 141 */
141 142 static uint32_t ioc_id;
142 143
143 144 static void putback(struct stdata *, queue_t *, mblk_t *, int);
144 145 static void strcleanall(struct vnode *);
145 146 static int strwsrv(queue_t *);
146 147 static int strdocmd(struct stdata *, struct strcmd *, cred_t *);
147 148
148 149 /*
149 150 * qinit and module_info structures for stream head read and write queues
150 151 */
151 152 struct module_info strm_info = { 0, "strrhead", 0, INFPSZ, STRHIGH, STRLOW };
152 153 struct module_info stwm_info = { 0, "strwhead", 0, 0, 0, 0 };
153 154 struct qinit strdata = { strrput, NULL, NULL, NULL, NULL, &strm_info };
154 155 struct qinit stwdata = { NULL, strwsrv, NULL, NULL, NULL, &stwm_info };
155 156 struct module_info fiform_info = { 0, "fifostrrhead", 0, PIPE_BUF, FIFOHIWAT,
156 157 FIFOLOWAT };
157 158 struct module_info fifowm_info = { 0, "fifostrwhead", 0, 0, 0, 0 };
158 159 struct qinit fifo_strdata = { strrput, NULL, NULL, NULL, NULL, &fiform_info };
159 160 struct qinit fifo_stwdata = { NULL, strwsrv, NULL, NULL, NULL, &fifowm_info };
160 161
161 162 extern kmutex_t strresources; /* protects global resources */
162 163 extern kmutex_t muxifier; /* single-threads multiplexor creation */
163 164
164 165 static boolean_t msghasdata(mblk_t *bp);
165 166 #define msgnodata(bp) (!msghasdata(bp))
166 167
167 168 /*
168 169 * Stream head locking notes:
169 170 * There are four monitors associated with the stream head:
170 171 * 1. v_stream monitor: in stropen() and strclose() v_lock
171 172 * is held while the association of vnode and stream
172 173 * head is established or tested for.
173 174 * 2. open/close/push/pop monitor: sd_lock is held while each
174 175 * thread bids for exclusive access to this monitor
175 176 * for opening or closing a stream. In addition, this
176 177 * monitor is entered during pushes and pops. This
177 178 * guarantees that during plumbing operations there
178 179 * is only one thread trying to change the plumbing.
179 180 * Any other threads present in the stream are only
180 181 * using the plumbing.
181 182 * 3. read/write monitor: in the case of read, a thread holds
182 183 * sd_lock while trying to get data from the stream
183 184 * head queue. if there is none to fulfill a read
184 185 * request, it sets RSLEEP and calls cv_wait_sig() down
185 186 * in strwaitq() to await the arrival of new data.
186 187 * when new data arrives in strrput(), sd_lock is acquired
187 188 * before testing for RSLEEP and calling cv_broadcast().
188 189 * the behavior of strwrite(), strwsrv(), and WSLEEP
189 190 * mirror this.
190 191 * 4. ioctl monitor: sd_lock is gotten to ensure that only one
191 192 * thread is doing an ioctl at a time.
192 193 */
193 194
194 195 static int
195 196 push_mod(queue_t *qp, dev_t *devp, struct stdata *stp, const char *name,
196 197 int anchor, cred_t *crp, uint_t anchor_zoneid)
197 198 {
198 199 int error;
199 200 fmodsw_impl_t *fp;
200 201
201 202 if (stp->sd_flag & (STRHUP|STRDERR|STWRERR)) {
202 203 error = (stp->sd_flag & STRHUP) ? ENXIO : EIO;
203 204 return (error);
204 205 }
205 206 if (stp->sd_pushcnt >= nstrpush) {
206 207 return (EINVAL);
207 208 }
208 209
209 210 if ((fp = fmodsw_find(name, FMODSW_HOLD | FMODSW_LOAD)) == NULL) {
210 211 stp->sd_flag |= STREOPENFAIL;
211 212 return (EINVAL);
212 213 }
213 214
214 215 /*
215 216 * push new module and call its open routine via qattach
216 217 */
217 218 if ((error = qattach(qp, devp, 0, crp, fp, B_FALSE)) != 0)
218 219 return (error);
219 220
220 221 /*
221 222 * Check to see if caller wants a STREAMS anchor
222 223 * put at this place in the stream, and add if so.
223 224 */
↓ open down ↓ |
133 lines elided |
↑ open up ↑ |
224 225 mutex_enter(&stp->sd_lock);
225 226 if (anchor == stp->sd_pushcnt) {
226 227 stp->sd_anchor = stp->sd_pushcnt;
227 228 stp->sd_anchorzone = anchor_zoneid;
228 229 }
229 230 mutex_exit(&stp->sd_lock);
230 231
231 232 return (0);
232 233 }
233 234
235 +static int
236 +xpg4_fixup(queue_t *qp, dev_t *devp, struct stdata *stp, cred_t *crp)
237 +{
238 + static const char *ptsmods[] = {
239 + "ptem", "ldterm", "ttcompat"
240 + };
241 + dev_t dummydev = *devp;
242 + struct strioctl strioc;
243 + zoneid_t zoneid;
244 + int32_t rval;
245 + uint_t i;
246 +
247 + /*
248 + * Push modules required for the slave PTY to have terminal
249 + * semantics out of the box; this is required by XPG4v2.
250 + * These three modules are flagged as single-instance so that
251 + * the system will never end up with duplicate copies pushed
252 + * onto a stream.
253 + */
254 +
255 + zoneid = crgetzoneid(crp);
256 + for (i = 0; i < ARRAY_SIZE(ptsmods); i++) {
257 + int error;
258 +
259 + error = push_mod(qp, &dummydev, stp, ptsmods[i], 0,
260 + crp, zoneid);
261 + if (error != 0)
262 + return (error);
263 + }
264 +
265 + /*
266 + * Send PTSSTTY down the stream
267 + */
268 +
269 + strioc.ic_cmd = PTSSTTY;
270 + strioc.ic_timout = 0;
271 + strioc.ic_len = 0;
272 + strioc.ic_dp = NULL;
273 +
274 + (void) strdoioctl(stp, &strioc, FNATIVE, K_TO_K, crp, &rval);
275 +
276 + return (0);
277 +}
278 +
234 279 /*
235 280 * Open a stream device.
236 281 */
237 282 int
238 283 stropen(vnode_t *vp, dev_t *devp, int flag, cred_t *crp)
239 284 {
240 285 struct stdata *stp;
241 286 queue_t *qp;
242 287 int s;
243 288 dev_t dummydev, savedev;
244 289 struct autopush *ap;
245 290 struct dlautopush dlap;
246 291 int error = 0;
247 292 ssize_t rmin, rmax;
248 293 int cloneopen;
249 294 queue_t *brq;
250 295 major_t major;
251 296 str_stack_t *ss;
252 297 zoneid_t zoneid;
253 298 uint_t anchor;
254 299
255 300 /*
256 301 * If the stream already exists, wait for any open in progress
257 302 * to complete, then call the open function of each module and
258 303 * driver in the stream. Otherwise create the stream.
259 304 */
260 305 TRACE_1(TR_FAC_STREAMS_FR, TR_STROPEN, "stropen:%p", vp);
261 306 retry:
262 307 mutex_enter(&vp->v_lock);
263 308 if ((stp = vp->v_stream) != NULL) {
264 309
265 310 /*
266 311 * Waiting for stream to be created to device
267 312 * due to another open.
268 313 */
269 314 mutex_exit(&vp->v_lock);
270 315
271 316 if (STRMATED(stp)) {
272 317 struct stdata *strmatep = stp->sd_mate;
273 318
274 319 STRLOCKMATES(stp);
275 320 if (strmatep->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
276 321 if (flag & (FNDELAY|FNONBLOCK)) {
277 322 error = EAGAIN;
278 323 mutex_exit(&strmatep->sd_lock);
279 324 goto ckreturn;
280 325 }
281 326 mutex_exit(&stp->sd_lock);
282 327 if (!cv_wait_sig(&strmatep->sd_monitor,
283 328 &strmatep->sd_lock)) {
284 329 error = EINTR;
285 330 mutex_exit(&strmatep->sd_lock);
286 331 mutex_enter(&stp->sd_lock);
287 332 goto ckreturn;
288 333 }
289 334 mutex_exit(&strmatep->sd_lock);
290 335 goto retry;
291 336 }
292 337 if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
293 338 if (flag & (FNDELAY|FNONBLOCK)) {
294 339 error = EAGAIN;
295 340 mutex_exit(&strmatep->sd_lock);
296 341 goto ckreturn;
297 342 }
298 343 mutex_exit(&strmatep->sd_lock);
299 344 if (!cv_wait_sig(&stp->sd_monitor,
300 345 &stp->sd_lock)) {
301 346 error = EINTR;
302 347 goto ckreturn;
303 348 }
304 349 mutex_exit(&stp->sd_lock);
305 350 goto retry;
306 351 }
307 352
308 353 if (stp->sd_flag & (STRDERR|STWRERR)) {
309 354 error = EIO;
310 355 mutex_exit(&strmatep->sd_lock);
311 356 goto ckreturn;
312 357 }
313 358
314 359 stp->sd_flag |= STWOPEN;
315 360 STRUNLOCKMATES(stp);
316 361 } else {
317 362 mutex_enter(&stp->sd_lock);
318 363 if (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
319 364 if (flag & (FNDELAY|FNONBLOCK)) {
320 365 error = EAGAIN;
321 366 goto ckreturn;
322 367 }
323 368 if (!cv_wait_sig(&stp->sd_monitor,
324 369 &stp->sd_lock)) {
325 370 error = EINTR;
326 371 goto ckreturn;
327 372 }
328 373 mutex_exit(&stp->sd_lock);
329 374 goto retry; /* could be clone! */
330 375 }
331 376
332 377 if (stp->sd_flag & (STRDERR|STWRERR)) {
333 378 error = EIO;
334 379 goto ckreturn;
335 380 }
336 381
337 382 stp->sd_flag |= STWOPEN;
338 383 mutex_exit(&stp->sd_lock);
339 384 }
340 385
341 386 /*
342 387 * Open all modules and devices down stream to notify
343 388 * that another user is streaming. For modules, set the
344 389 * last argument to MODOPEN and do not pass any open flags.
345 390 * Ignore dummydev since this is not the first open.
346 391 */
347 392 claimstr(stp->sd_wrq);
348 393 qp = stp->sd_wrq;
349 394 while (_SAMESTR(qp)) {
350 395 qp = qp->q_next;
351 396 if ((error = qreopen(_RD(qp), devp, flag, crp)) != 0)
352 397 break;
353 398 }
354 399 releasestr(stp->sd_wrq);
355 400 mutex_enter(&stp->sd_lock);
356 401 stp->sd_flag &= ~(STRHUP|STWOPEN|STRDERR|STWRERR);
357 402 stp->sd_rerror = 0;
358 403 stp->sd_werror = 0;
359 404 ckreturn:
360 405 cv_broadcast(&stp->sd_monitor);
361 406 mutex_exit(&stp->sd_lock);
362 407 return (error);
363 408 }
364 409
365 410 /*
366 411 * This vnode isn't streaming. SPECFS already
367 412 * checked for multiple vnodes pointing to the
368 413 * same stream, so create a stream to the driver.
369 414 */
370 415 qp = allocq();
371 416 stp = shalloc(qp);
372 417
373 418 /*
374 419 * Initialize stream head. shalloc() has given us
375 420 * exclusive access, and we have the vnode locked;
376 421 * we can do whatever we want with stp.
377 422 */
378 423 stp->sd_flag = STWOPEN;
379 424 stp->sd_siglist = NULL;
380 425 stp->sd_pollist.ph_list = NULL;
381 426 stp->sd_sigflags = 0;
382 427 stp->sd_mark = NULL;
383 428 stp->sd_closetime = STRTIMOUT;
384 429 stp->sd_sidp = NULL;
385 430 stp->sd_pgidp = NULL;
386 431 stp->sd_vnode = vp;
387 432 stp->sd_pvnode = NULL;
388 433 stp->sd_rerror = 0;
389 434 stp->sd_werror = 0;
390 435 stp->sd_wroff = 0;
391 436 stp->sd_tail = 0;
392 437 stp->sd_iocblk = NULL;
393 438 stp->sd_cmdblk = NULL;
394 439 stp->sd_pushcnt = 0;
395 440 stp->sd_qn_minpsz = 0;
396 441 stp->sd_qn_maxpsz = INFPSZ - 1; /* used to check for initialization */
397 442 stp->sd_maxblk = INFPSZ;
398 443 qp->q_ptr = _WR(qp)->q_ptr = stp;
399 444 STREAM(qp) = STREAM(_WR(qp)) = stp;
400 445 vp->v_stream = stp;
401 446 mutex_exit(&vp->v_lock);
402 447 if (vp->v_type == VFIFO) {
403 448 stp->sd_flag |= OLDNDELAY;
404 449 /*
405 450 * This means, both for pipes and fifos
406 451 * strwrite will send SIGPIPE if the other
407 452 * end is closed. For putmsg it depends
408 453 * on whether it is a XPG4_2 application
409 454 * or not
410 455 */
411 456 stp->sd_wput_opt = SW_SIGPIPE;
412 457
413 458 /* setq might sleep in kmem_alloc - avoid holding locks. */
414 459 setq(qp, &fifo_strdata, &fifo_stwdata, NULL, QMTSAFE,
415 460 SQ_CI|SQ_CO, B_FALSE);
416 461
417 462 set_qend(qp);
418 463 stp->sd_strtab = fifo_getinfo();
419 464 _WR(qp)->q_nfsrv = _WR(qp);
420 465 qp->q_nfsrv = qp;
421 466 /*
422 467 * Wake up others that are waiting for stream to be created.
423 468 */
424 469 mutex_enter(&stp->sd_lock);
425 470 /*
426 471 * nothing is be pushed on stream yet, so
427 472 * optimized stream head packetsizes are just that
428 473 * of the read queue
429 474 */
430 475 stp->sd_qn_minpsz = qp->q_minpsz;
431 476 stp->sd_qn_maxpsz = qp->q_maxpsz;
432 477 stp->sd_flag &= ~STWOPEN;
433 478 goto fifo_opendone;
434 479 }
435 480 /* setq might sleep in kmem_alloc - avoid holding locks. */
436 481 setq(qp, &strdata, &stwdata, NULL, QMTSAFE, SQ_CI|SQ_CO, B_FALSE);
437 482
438 483 set_qend(qp);
439 484
440 485 /*
441 486 * Open driver and create stream to it (via qattach).
442 487 */
443 488 savedev = *devp;
444 489 cloneopen = (getmajor(*devp) == clone_major);
445 490 if ((error = qattach(qp, devp, flag, crp, NULL, B_FALSE)) != 0) {
446 491 mutex_enter(&vp->v_lock);
447 492 vp->v_stream = NULL;
448 493 mutex_exit(&vp->v_lock);
449 494 mutex_enter(&stp->sd_lock);
450 495 cv_broadcast(&stp->sd_monitor);
451 496 mutex_exit(&stp->sd_lock);
452 497 freeq(_RD(qp));
453 498 shfree(stp);
454 499 return (error);
455 500 }
456 501 /*
457 502 * Set sd_strtab after open in order to handle clonable drivers
458 503 */
459 504 stp->sd_strtab = STREAMSTAB(getmajor(*devp));
460 505
461 506 /*
462 507 * Historical note: dummydev used to be be prior to the initial
463 508 * open (via qattach above), which made the value seen
464 509 * inconsistent between an I_PUSH and an autopush of a module.
465 510 */
466 511 dummydev = *devp;
467 512
468 513 /*
469 514 * For clone open of old style (Q not associated) network driver,
470 515 * push DRMODNAME module to handle DL_ATTACH/DL_DETACH
471 516 */
472 517 brq = _RD(_WR(qp)->q_next);
473 518 major = getmajor(*devp);
474 519 if (push_drcompat && cloneopen && NETWORK_DRV(major) &&
475 520 ((brq->q_flag & _QASSOCIATED) == 0)) {
476 521 if (push_mod(qp, &dummydev, stp, DRMODNAME, 0, crp, 0) != 0)
477 522 cmn_err(CE_WARN, "cannot push " DRMODNAME
478 523 " streams module");
479 524 }
480 525
481 526 if (!NETWORK_DRV(major)) {
482 527 savedev = *devp;
483 528 } else {
484 529 /*
485 530 * For network devices, process differently based on the
486 531 * return value from dld_autopush():
487 532 *
488 533 * 0: the passed-in device points to a GLDv3 datalink with
489 534 * per-link autopush configuration; use that configuration
490 535 * and ignore any per-driver autopush configuration.
491 536 *
492 537 * 1: the passed-in device points to a physical GLDv3
493 538 * datalink without per-link autopush configuration. The
494 539 * passed in device was changed to refer to the actual
495 540 * physical device (if it's not already); we use that new
496 541 * device to look up any per-driver autopush configuration.
497 542 *
498 543 * -1: neither of the above cases applied; use the initial
499 544 * device to look up any per-driver autopush configuration.
500 545 */
501 546 switch (dld_autopush(&savedev, &dlap)) {
502 547 case 0:
503 548 zoneid = crgetzoneid(crp);
504 549 for (s = 0; s < dlap.dap_npush; s++) {
505 550 error = push_mod(qp, &dummydev, stp,
506 551 dlap.dap_aplist[s], dlap.dap_anchor, crp,
507 552 zoneid);
508 553 if (error != 0)
509 554 break;
510 555 }
511 556 goto opendone;
512 557 case 1:
513 558 break;
514 559 case -1:
515 560 savedev = *devp;
516 561 break;
517 562 }
518 563 }
519 564 /*
520 565 * Find the autopush configuration based on "savedev". Start with the
521 566 * global zone. If not found check in the local zone.
522 567 */
523 568 zoneid = GLOBAL_ZONEID;
524 569 retryap:
525 570 ss = netstack_find_by_stackid(zoneid_to_netstackid(zoneid))->
526 571 netstack_str;
527 572 if ((ap = sad_ap_find_by_dev(savedev, ss)) == NULL) {
528 573 netstack_rele(ss->ss_netstack);
529 574 if (zoneid == GLOBAL_ZONEID) {
530 575 /*
531 576 * None found. Also look in the zone's autopush table.
532 577 */
533 578 zoneid = crgetzoneid(crp);
534 579 if (zoneid != GLOBAL_ZONEID)
535 580 goto retryap;
536 581 }
537 582 goto opendone;
538 583 }
539 584 anchor = ap->ap_anchor;
540 585 zoneid = crgetzoneid(crp);
541 586 for (s = 0; s < ap->ap_npush; s++) {
↓ open down ↓ |
298 lines elided |
↑ open up ↑ |
542 587 error = push_mod(qp, &dummydev, stp, ap->ap_list[s],
543 588 anchor, crp, zoneid);
544 589 if (error != 0)
545 590 break;
546 591 }
547 592 sad_ap_rele(ap, ss);
548 593 netstack_rele(ss->ss_netstack);
549 594
550 595 opendone:
551 596
597 + if (error == 0 &&
598 + (stp->sd_flag & (STRISTTY|STRXPG4TTY)) == (STRISTTY|STRXPG4TTY)) {
599 + error = xpg4_fixup(qp, devp, stp, crp);
600 + }
601 +
552 602 /*
553 603 * let specfs know that open failed part way through
554 604 */
555 - if (error) {
605 + if (error != 0) {
556 606 mutex_enter(&stp->sd_lock);
557 607 stp->sd_flag |= STREOPENFAIL;
558 608 mutex_exit(&stp->sd_lock);
559 609 }
560 610
561 611 /*
562 612 * Wake up others that are waiting for stream to be created.
563 613 */
564 614 mutex_enter(&stp->sd_lock);
565 615 stp->sd_flag &= ~STWOPEN;
566 616
567 617 /*
568 618 * As a performance concern we are caching the values of
569 619 * q_minpsz and q_maxpsz of the module below the stream
570 620 * head in the stream head.
571 621 */
572 622 mutex_enter(QLOCK(stp->sd_wrq->q_next));
573 623 rmin = stp->sd_wrq->q_next->q_minpsz;
574 624 rmax = stp->sd_wrq->q_next->q_maxpsz;
575 625 mutex_exit(QLOCK(stp->sd_wrq->q_next));
576 626
577 627 /* do this processing here as a performance concern */
578 628 if (strmsgsz != 0) {
579 629 if (rmax == INFPSZ)
580 630 rmax = strmsgsz;
581 631 else
582 632 rmax = MIN(strmsgsz, rmax);
583 633 }
584 634
585 635 mutex_enter(QLOCK(stp->sd_wrq));
586 636 stp->sd_qn_minpsz = rmin;
587 637 stp->sd_qn_maxpsz = rmax;
588 638 mutex_exit(QLOCK(stp->sd_wrq));
589 639
590 640 fifo_opendone:
591 641 cv_broadcast(&stp->sd_monitor);
592 642 mutex_exit(&stp->sd_lock);
593 643 return (error);
594 644 }
595 645
596 646 static int strsink(queue_t *, mblk_t *);
597 647 static struct qinit deadrend = {
598 648 strsink, NULL, NULL, NULL, NULL, &strm_info, NULL
599 649 };
600 650 static struct qinit deadwend = {
601 651 NULL, NULL, NULL, NULL, NULL, &stwm_info, NULL
602 652 };
603 653
604 654 /*
605 655 * Close a stream.
606 656 * This is called from closef() on the last close of an open stream.
607 657 * Strclean() will already have removed the siglist and pollist
608 658 * information, so all that remains is to remove all multiplexor links
609 659 * for the stream, pop all the modules (and the driver), and free the
610 660 * stream structure.
611 661 */
612 662
613 663 int
614 664 strclose(struct vnode *vp, int flag, cred_t *crp)
615 665 {
616 666 struct stdata *stp;
617 667 queue_t *qp;
618 668 int rval;
619 669 int freestp = 1;
620 670 queue_t *rmq;
621 671
622 672 TRACE_1(TR_FAC_STREAMS_FR,
623 673 TR_STRCLOSE, "strclose:%p", vp);
624 674 ASSERT(vp->v_stream);
625 675
626 676 stp = vp->v_stream;
627 677 ASSERT(!(stp->sd_flag & STPLEX));
628 678 qp = stp->sd_wrq;
629 679
630 680 /*
631 681 * Needed so that strpoll will return non-zero for this fd.
632 682 * Note that with POLLNOERR STRHUP does still cause POLLHUP.
633 683 */
634 684 mutex_enter(&stp->sd_lock);
635 685 stp->sd_flag |= STRHUP;
636 686 mutex_exit(&stp->sd_lock);
637 687
638 688 /*
639 689 * If the registered process or process group did not have an
640 690 * open instance of this stream then strclean would not be
641 691 * called. Thus at the time of closing all remaining siglist entries
642 692 * are removed.
643 693 */
644 694 if (stp->sd_siglist != NULL)
645 695 strcleanall(vp);
646 696
647 697 ASSERT(stp->sd_siglist == NULL);
648 698 ASSERT(stp->sd_sigflags == 0);
649 699
650 700 if (STRMATED(stp)) {
651 701 struct stdata *strmatep = stp->sd_mate;
652 702 int waited = 1;
653 703
654 704 STRLOCKMATES(stp);
655 705 while (waited) {
656 706 waited = 0;
657 707 while (stp->sd_flag & (STWOPEN|STRCLOSE|STRPLUMB)) {
658 708 mutex_exit(&strmatep->sd_lock);
659 709 cv_wait(&stp->sd_monitor, &stp->sd_lock);
660 710 mutex_exit(&stp->sd_lock);
661 711 STRLOCKMATES(stp);
662 712 waited = 1;
663 713 }
664 714 while (strmatep->sd_flag &
665 715 (STWOPEN|STRCLOSE|STRPLUMB)) {
666 716 mutex_exit(&stp->sd_lock);
667 717 cv_wait(&strmatep->sd_monitor,
668 718 &strmatep->sd_lock);
669 719 mutex_exit(&strmatep->sd_lock);
670 720 STRLOCKMATES(stp);
671 721 waited = 1;
672 722 }
673 723 }
674 724 stp->sd_flag |= STRCLOSE;
675 725 STRUNLOCKMATES(stp);
676 726 } else {
677 727 mutex_enter(&stp->sd_lock);
678 728 stp->sd_flag |= STRCLOSE;
679 729 mutex_exit(&stp->sd_lock);
680 730 }
681 731
682 732 ASSERT(qp->q_first == NULL); /* No more delayed write */
683 733
684 734 /* Check if an I_LINK was ever done on this stream */
685 735 if (stp->sd_flag & STRHASLINKS) {
686 736 netstack_t *ns;
687 737 str_stack_t *ss;
688 738
689 739 ns = netstack_find_by_cred(crp);
690 740 ASSERT(ns != NULL);
691 741 ss = ns->netstack_str;
692 742 ASSERT(ss != NULL);
693 743
694 744 (void) munlinkall(stp, LINKCLOSE|LINKNORMAL, crp, &rval, ss);
695 745 netstack_rele(ss->ss_netstack);
696 746 }
697 747
698 748 while (_SAMESTR(qp)) {
699 749 /*
700 750 * Holding sd_lock prevents q_next from changing in
701 751 * this stream.
702 752 */
703 753 mutex_enter(&stp->sd_lock);
704 754 if (!(flag & (FNDELAY|FNONBLOCK)) && (stp->sd_closetime > 0)) {
705 755
706 756 /*
707 757 * sleep until awakened by strwsrv() or timeout
708 758 */
709 759 for (;;) {
710 760 mutex_enter(QLOCK(qp->q_next));
711 761 if (!(qp->q_next->q_mblkcnt)) {
712 762 mutex_exit(QLOCK(qp->q_next));
713 763 break;
714 764 }
715 765 stp->sd_flag |= WSLEEP;
716 766
717 767 /* ensure strwsrv gets enabled */
718 768 qp->q_next->q_flag |= QWANTW;
719 769 mutex_exit(QLOCK(qp->q_next));
720 770 /* get out if we timed out or recv'd a signal */
721 771 if (str_cv_wait(&qp->q_wait, &stp->sd_lock,
722 772 stp->sd_closetime, 0) <= 0) {
723 773 break;
724 774 }
725 775 }
726 776 stp->sd_flag &= ~WSLEEP;
727 777 }
728 778 mutex_exit(&stp->sd_lock);
729 779
730 780 rmq = qp->q_next;
731 781 if (rmq->q_flag & QISDRV) {
732 782 ASSERT(!_SAMESTR(rmq));
733 783 wait_sq_svc(_RD(qp)->q_syncq);
734 784 }
735 785
736 786 qdetach(_RD(rmq), 1, flag, crp, B_FALSE);
737 787 }
738 788
739 789 /*
740 790 * Since we call pollwakeup in close() now, the poll list should
741 791 * be empty in most cases. The only exception is the layered devices
742 792 * (e.g. the console drivers with redirection modules pushed on top
743 793 * of it). We have to do this after calling qdetach() because
744 794 * the redirection module won't have torn down the console
745 795 * redirection until after qdetach() has been invoked.
746 796 */
747 797 if (stp->sd_pollist.ph_list != NULL) {
748 798 pollwakeup(&stp->sd_pollist, POLLERR);
749 799 pollhead_clean(&stp->sd_pollist);
750 800 }
751 801 ASSERT(stp->sd_pollist.ph_list == NULL);
752 802 ASSERT(stp->sd_sidp == NULL);
753 803 ASSERT(stp->sd_pgidp == NULL);
754 804
755 805 /* Prevent qenable from re-enabling the stream head queue */
756 806 disable_svc(_RD(qp));
757 807
758 808 /*
759 809 * Wait until service procedure of each queue is
760 810 * run, if QINSERVICE is set.
761 811 */
762 812 wait_svc(_RD(qp));
763 813
764 814 /*
765 815 * Now, flush both queues.
766 816 */
767 817 flushq(_RD(qp), FLUSHALL);
768 818 flushq(qp, FLUSHALL);
769 819
770 820 /*
771 821 * If the write queue of the stream head is pointing to a
772 822 * read queue, we have a twisted stream. If the read queue
773 823 * is alive, convert the stream head queues into a dead end.
774 824 * If the read queue is dead, free the dead pair.
775 825 */
776 826 if (qp->q_next && !_SAMESTR(qp)) {
777 827 if (qp->q_next->q_qinfo == &deadrend) { /* half-closed pipe */
778 828 flushq(qp->q_next, FLUSHALL); /* ensure no message */
779 829 shfree(qp->q_next->q_stream);
780 830 freeq(qp->q_next);
781 831 freeq(_RD(qp));
782 832 } else if (qp->q_next == _RD(qp)) { /* fifo */
783 833 freeq(_RD(qp));
784 834 } else { /* pipe */
785 835 freestp = 0;
786 836 /*
787 837 * The q_info pointers are never accessed when
788 838 * SQLOCK is held.
789 839 */
790 840 ASSERT(qp->q_syncq == _RD(qp)->q_syncq);
791 841 mutex_enter(SQLOCK(qp->q_syncq));
792 842 qp->q_qinfo = &deadwend;
793 843 _RD(qp)->q_qinfo = &deadrend;
794 844 mutex_exit(SQLOCK(qp->q_syncq));
795 845 }
796 846 } else {
797 847 freeq(_RD(qp)); /* free stream head queue pair */
798 848 }
799 849
800 850 mutex_enter(&vp->v_lock);
801 851 if (stp->sd_iocblk) {
802 852 if (stp->sd_iocblk != (mblk_t *)-1) {
803 853 freemsg(stp->sd_iocblk);
804 854 }
805 855 stp->sd_iocblk = NULL;
806 856 }
807 857 stp->sd_vnode = stp->sd_pvnode = NULL;
808 858 vp->v_stream = NULL;
809 859 mutex_exit(&vp->v_lock);
810 860 mutex_enter(&stp->sd_lock);
811 861 freemsg(stp->sd_cmdblk);
812 862 stp->sd_cmdblk = NULL;
813 863 stp->sd_flag &= ~STRCLOSE;
814 864 cv_broadcast(&stp->sd_monitor);
815 865 mutex_exit(&stp->sd_lock);
816 866
817 867 if (freestp)
818 868 shfree(stp);
819 869 return (0);
820 870 }
821 871
822 872 static int
823 873 strsink(queue_t *q, mblk_t *bp)
824 874 {
825 875 struct copyresp *resp;
826 876
827 877 switch (bp->b_datap->db_type) {
828 878 case M_FLUSH:
829 879 if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
830 880 *bp->b_rptr &= ~FLUSHR;
831 881 bp->b_flag |= MSGNOLOOP;
832 882 /*
833 883 * Protect against the driver passing up
834 884 * messages after it has done a qprocsoff.
835 885 */
836 886 if (_OTHERQ(q)->q_next == NULL)
837 887 freemsg(bp);
838 888 else
839 889 qreply(q, bp);
840 890 } else {
841 891 freemsg(bp);
842 892 }
843 893 break;
844 894
845 895 case M_COPYIN:
846 896 case M_COPYOUT:
847 897 if (bp->b_cont) {
848 898 freemsg(bp->b_cont);
849 899 bp->b_cont = NULL;
850 900 }
851 901 bp->b_datap->db_type = M_IOCDATA;
852 902 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
853 903 resp = (struct copyresp *)bp->b_rptr;
854 904 resp->cp_rval = (caddr_t)1; /* failure */
855 905 /*
856 906 * Protect against the driver passing up
857 907 * messages after it has done a qprocsoff.
858 908 */
859 909 if (_OTHERQ(q)->q_next == NULL)
860 910 freemsg(bp);
861 911 else
862 912 qreply(q, bp);
863 913 break;
864 914
865 915 case M_IOCTL:
866 916 if (bp->b_cont) {
867 917 freemsg(bp->b_cont);
868 918 bp->b_cont = NULL;
869 919 }
870 920 bp->b_datap->db_type = M_IOCNAK;
871 921 /*
872 922 * Protect against the driver passing up
873 923 * messages after it has done a qprocsoff.
874 924 */
875 925 if (_OTHERQ(q)->q_next == NULL)
876 926 freemsg(bp);
877 927 else
878 928 qreply(q, bp);
879 929 break;
880 930
881 931 default:
882 932 freemsg(bp);
883 933 break;
884 934 }
885 935
886 936 return (0);
887 937 }
888 938
889 939 /*
890 940 * Clean up after a process when it closes a stream. This is called
891 941 * from closef for all closes, whereas strclose is called only for the
892 942 * last close on a stream. The siglist is scanned for entries for the
893 943 * current process, and these are removed.
894 944 */
895 945 void
896 946 strclean(struct vnode *vp)
897 947 {
898 948 strsig_t *ssp, *pssp, *tssp;
899 949 stdata_t *stp;
900 950 int update = 0;
901 951
902 952 TRACE_1(TR_FAC_STREAMS_FR,
903 953 TR_STRCLEAN, "strclean:%p", vp);
904 954 stp = vp->v_stream;
905 955 pssp = NULL;
906 956 mutex_enter(&stp->sd_lock);
907 957 ssp = stp->sd_siglist;
908 958 while (ssp) {
909 959 if (ssp->ss_pidp == curproc->p_pidp) {
910 960 tssp = ssp->ss_next;
911 961 if (pssp)
912 962 pssp->ss_next = tssp;
913 963 else
914 964 stp->sd_siglist = tssp;
915 965 mutex_enter(&pidlock);
916 966 PID_RELE(ssp->ss_pidp);
917 967 mutex_exit(&pidlock);
918 968 kmem_free(ssp, sizeof (strsig_t));
919 969 update = 1;
920 970 ssp = tssp;
921 971 } else {
922 972 pssp = ssp;
923 973 ssp = ssp->ss_next;
924 974 }
925 975 }
926 976 if (update) {
927 977 stp->sd_sigflags = 0;
928 978 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
929 979 stp->sd_sigflags |= ssp->ss_events;
930 980 }
931 981 mutex_exit(&stp->sd_lock);
932 982 }
933 983
934 984 /*
935 985 * Used on the last close to remove any remaining items on the siglist.
936 986 * These could be present on the siglist due to I_ESETSIG calls that
937 987 * use process groups or processed that do not have an open file descriptor
938 988 * for this stream (Such entries would not be removed by strclean).
939 989 */
940 990 static void
941 991 strcleanall(struct vnode *vp)
942 992 {
943 993 strsig_t *ssp, *nssp;
944 994 stdata_t *stp;
945 995
946 996 stp = vp->v_stream;
947 997 mutex_enter(&stp->sd_lock);
948 998 ssp = stp->sd_siglist;
949 999 stp->sd_siglist = NULL;
950 1000 while (ssp) {
951 1001 nssp = ssp->ss_next;
952 1002 mutex_enter(&pidlock);
953 1003 PID_RELE(ssp->ss_pidp);
954 1004 mutex_exit(&pidlock);
955 1005 kmem_free(ssp, sizeof (strsig_t));
956 1006 ssp = nssp;
957 1007 }
958 1008 stp->sd_sigflags = 0;
959 1009 mutex_exit(&stp->sd_lock);
960 1010 }
961 1011
962 1012 /*
963 1013 * Retrieve the next message from the logical stream head read queue
964 1014 * using either rwnext (if sync stream) or getq_noenab.
965 1015 * It is the callers responsibility to call qbackenable after
966 1016 * it is finished with the message. The caller should not call
967 1017 * qbackenable until after any putback calls to avoid spurious backenabling.
968 1018 */
969 1019 mblk_t *
970 1020 strget(struct stdata *stp, queue_t *q, struct uio *uiop, int first,
971 1021 int *errorp)
972 1022 {
973 1023 mblk_t *bp;
974 1024 int error;
975 1025 ssize_t rbytes = 0;
976 1026
977 1027 /* Holding sd_lock prevents the read queue from changing */
978 1028 ASSERT(MUTEX_HELD(&stp->sd_lock));
979 1029
980 1030 if (uiop != NULL && stp->sd_struiordq != NULL &&
981 1031 q->q_first == NULL &&
982 1032 (!first || (stp->sd_wakeq & RSLEEP))) {
983 1033 /*
984 1034 * Stream supports rwnext() for the read side.
985 1035 * If this is the first time we're called by e.g. strread
986 1036 * only do the downcall if there is a deferred wakeup
987 1037 * (registered in sd_wakeq).
988 1038 */
989 1039 struiod_t uiod;
990 1040
991 1041 if (first)
992 1042 stp->sd_wakeq &= ~RSLEEP;
993 1043
994 1044 (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
995 1045 sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
996 1046 uiod.d_mp = 0;
997 1047 /*
998 1048 * Mark that a thread is in rwnext on the read side
999 1049 * to prevent strrput from nacking ioctls immediately.
1000 1050 * When the last concurrent rwnext returns
1001 1051 * the ioctls are nack'ed.
1002 1052 */
1003 1053 ASSERT(MUTEX_HELD(&stp->sd_lock));
1004 1054 stp->sd_struiodnak++;
1005 1055 /*
1006 1056 * Note: rwnext will drop sd_lock.
1007 1057 */
1008 1058 error = rwnext(q, &uiod);
1009 1059 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
1010 1060 mutex_enter(&stp->sd_lock);
1011 1061 stp->sd_struiodnak--;
1012 1062 while (stp->sd_struiodnak == 0 &&
1013 1063 ((bp = stp->sd_struionak) != NULL)) {
1014 1064 stp->sd_struionak = bp->b_next;
1015 1065 bp->b_next = NULL;
1016 1066 bp->b_datap->db_type = M_IOCNAK;
1017 1067 /*
1018 1068 * Protect against the driver passing up
1019 1069 * messages after it has done a qprocsoff.
1020 1070 */
1021 1071 if (_OTHERQ(q)->q_next == NULL)
1022 1072 freemsg(bp);
1023 1073 else {
1024 1074 mutex_exit(&stp->sd_lock);
1025 1075 qreply(q, bp);
1026 1076 mutex_enter(&stp->sd_lock);
1027 1077 }
1028 1078 }
1029 1079 ASSERT(MUTEX_HELD(&stp->sd_lock));
1030 1080 if (error == 0 || error == EWOULDBLOCK) {
1031 1081 if ((bp = uiod.d_mp) != NULL) {
1032 1082 *errorp = 0;
1033 1083 ASSERT(MUTEX_HELD(&stp->sd_lock));
1034 1084 return (bp);
1035 1085 }
1036 1086 error = 0;
1037 1087 } else if (error == EINVAL) {
1038 1088 /*
1039 1089 * The stream plumbing must have
1040 1090 * changed while we were away, so
1041 1091 * just turn off rwnext()s.
1042 1092 */
1043 1093 error = 0;
1044 1094 } else if (error == EBUSY) {
1045 1095 /*
1046 1096 * The module might have data in transit using putnext
1047 1097 * Fall back on waiting + getq.
1048 1098 */
1049 1099 error = 0;
1050 1100 } else {
1051 1101 *errorp = error;
1052 1102 ASSERT(MUTEX_HELD(&stp->sd_lock));
1053 1103 return (NULL);
1054 1104 }
1055 1105 /*
1056 1106 * Try a getq in case a rwnext() generated mblk
1057 1107 * has bubbled up via strrput().
1058 1108 */
1059 1109 }
1060 1110 *errorp = 0;
1061 1111 ASSERT(MUTEX_HELD(&stp->sd_lock));
1062 1112
1063 1113 /*
1064 1114 * If we have a valid uio, try and use this as a guide for how
1065 1115 * many bytes to retrieve from the queue via getq_noenab().
1066 1116 * Doing this can avoid unneccesary counting of overlong
1067 1117 * messages in putback(). We currently only do this for sockets
1068 1118 * and only if there is no sd_rputdatafunc hook.
1069 1119 *
1070 1120 * The sd_rputdatafunc hook transforms the entire message
1071 1121 * before any bytes in it can be given to a client. So, rbytes
1072 1122 * must be 0 if there is a hook.
1073 1123 */
1074 1124 if ((uiop != NULL) && (stp->sd_vnode->v_type == VSOCK) &&
1075 1125 (stp->sd_rputdatafunc == NULL))
1076 1126 rbytes = uiop->uio_resid;
1077 1127
1078 1128 return (getq_noenab(q, rbytes));
1079 1129 }
1080 1130
1081 1131 /*
1082 1132 * Copy out the message pointed to by `bp' into the uio pointed to by `uiop'.
1083 1133 * If the message does not fit in the uio the remainder of it is returned;
1084 1134 * otherwise NULL is returned. Any embedded zero-length mblk_t's are
1085 1135 * consumed, even if uio_resid reaches zero. On error, `*errorp' is set to
1086 1136 * the error code, the message is consumed, and NULL is returned.
1087 1137 */
1088 1138 static mblk_t *
1089 1139 struiocopyout(mblk_t *bp, struct uio *uiop, int *errorp)
1090 1140 {
1091 1141 int error;
1092 1142 ptrdiff_t n;
1093 1143 mblk_t *nbp;
1094 1144
1095 1145 ASSERT(bp->b_wptr >= bp->b_rptr);
1096 1146
1097 1147 do {
1098 1148 if ((n = MIN(uiop->uio_resid, MBLKL(bp))) != 0) {
1099 1149 ASSERT(n > 0);
1100 1150
1101 1151 error = uiomove(bp->b_rptr, n, UIO_READ, uiop);
1102 1152 if (error != 0) {
1103 1153 freemsg(bp);
1104 1154 *errorp = error;
1105 1155 return (NULL);
1106 1156 }
1107 1157 }
1108 1158
1109 1159 bp->b_rptr += n;
1110 1160 while (bp != NULL && (bp->b_rptr >= bp->b_wptr)) {
1111 1161 nbp = bp;
1112 1162 bp = bp->b_cont;
1113 1163 freeb(nbp);
1114 1164 }
1115 1165 } while (bp != NULL && uiop->uio_resid > 0);
1116 1166
1117 1167 *errorp = 0;
1118 1168 return (bp);
1119 1169 }
1120 1170
1121 1171 /*
1122 1172 * Read a stream according to the mode flags in sd_flag:
1123 1173 *
1124 1174 * (default mode) - Byte stream, msg boundaries are ignored
1125 1175 * RD_MSGDIS (msg discard) - Read on msg boundaries and throw away
1126 1176 * any data remaining in msg
1127 1177 * RD_MSGNODIS (msg non-discard) - Read on msg boundaries and put back
1128 1178 * any remaining data on head of read queue
1129 1179 *
1130 1180 * Consume readable messages on the front of the queue until
1131 1181 * ttolwp(curthread)->lwp_count
1132 1182 * is satisfied, the readable messages are exhausted, or a message
1133 1183 * boundary is reached in a message mode. If no data was read and
1134 1184 * the stream was not opened with the NDELAY flag, block until data arrives.
1135 1185 * Otherwise return the data read and update the count.
1136 1186 *
1137 1187 * In default mode a 0 length message signifies end-of-file and terminates
1138 1188 * a read in progress. The 0 length message is removed from the queue
1139 1189 * only if it is the only message read (no data is read).
1140 1190 *
1141 1191 * An attempt to read an M_PROTO or M_PCPROTO message results in an
1142 1192 * EBADMSG error return, unless either RD_PROTDAT or RD_PROTDIS are set.
1143 1193 * If RD_PROTDAT is set, M_PROTO and M_PCPROTO messages are read as data.
1144 1194 * If RD_PROTDIS is set, the M_PROTO and M_PCPROTO parts of the message
1145 1195 * are unlinked from and M_DATA blocks in the message, the protos are
1146 1196 * thrown away, and the data is read.
1147 1197 */
1148 1198 /* ARGSUSED */
1149 1199 int
1150 1200 strread(struct vnode *vp, struct uio *uiop, cred_t *crp)
1151 1201 {
1152 1202 struct stdata *stp;
1153 1203 mblk_t *bp, *nbp;
1154 1204 queue_t *q;
1155 1205 int error = 0;
1156 1206 uint_t old_sd_flag;
1157 1207 int first;
1158 1208 char rflg;
1159 1209 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
1160 1210 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
1161 1211 short delim;
1162 1212 unsigned char pri = 0;
1163 1213 char waitflag;
1164 1214 unsigned char type;
1165 1215
1166 1216 TRACE_1(TR_FAC_STREAMS_FR,
1167 1217 TR_STRREAD_ENTER, "strread:%p", vp);
1168 1218 ASSERT(vp->v_stream);
1169 1219 stp = vp->v_stream;
1170 1220
1171 1221 mutex_enter(&stp->sd_lock);
1172 1222
1173 1223 if ((error = i_straccess(stp, JCREAD)) != 0) {
1174 1224 mutex_exit(&stp->sd_lock);
1175 1225 return (error);
1176 1226 }
1177 1227
1178 1228 if (stp->sd_flag & (STRDERR|STPLEX)) {
1179 1229 error = strgeterr(stp, STRDERR|STPLEX, 0);
1180 1230 if (error != 0) {
1181 1231 mutex_exit(&stp->sd_lock);
1182 1232 return (error);
1183 1233 }
1184 1234 }
1185 1235
1186 1236 /*
1187 1237 * Loop terminates when uiop->uio_resid == 0.
1188 1238 */
1189 1239 rflg = 0;
1190 1240 waitflag = READWAIT;
1191 1241 q = _RD(stp->sd_wrq);
1192 1242 for (;;) {
1193 1243 ASSERT(MUTEX_HELD(&stp->sd_lock));
1194 1244 old_sd_flag = stp->sd_flag;
1195 1245 mark = 0;
1196 1246 delim = 0;
1197 1247 first = 1;
1198 1248 while ((bp = strget(stp, q, uiop, first, &error)) == NULL) {
1199 1249 int done = 0;
1200 1250
1201 1251 ASSERT(MUTEX_HELD(&stp->sd_lock));
1202 1252
1203 1253 if (error != 0)
1204 1254 goto oops;
1205 1255
1206 1256 if (stp->sd_flag & (STRHUP|STREOF)) {
1207 1257 goto oops;
1208 1258 }
1209 1259 if (rflg && !(stp->sd_flag & STRDELIM)) {
1210 1260 goto oops;
1211 1261 }
1212 1262 /*
1213 1263 * If a read(fd,buf,0) has been done, there is no
1214 1264 * need to sleep. We always have zero bytes to
1215 1265 * return.
1216 1266 */
1217 1267 if (uiop->uio_resid == 0) {
1218 1268 goto oops;
1219 1269 }
1220 1270
1221 1271 qbackenable(q, 0);
1222 1272
1223 1273 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_WAIT,
1224 1274 "strread calls strwaitq:%p, %p, %p",
1225 1275 vp, uiop, crp);
1226 1276 if ((error = strwaitq(stp, waitflag, uiop->uio_resid,
1227 1277 uiop->uio_fmode, -1, &done)) != 0 || done) {
1228 1278 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_DONE,
1229 1279 "strread error or done:%p, %p, %p",
1230 1280 vp, uiop, crp);
1231 1281 if ((uiop->uio_fmode & FNDELAY) &&
1232 1282 (stp->sd_flag & OLDNDELAY) &&
1233 1283 (error == EAGAIN))
1234 1284 error = 0;
1235 1285 goto oops;
1236 1286 }
1237 1287 TRACE_3(TR_FAC_STREAMS_FR, TR_STRREAD_AWAKE,
1238 1288 "strread awakes:%p, %p, %p", vp, uiop, crp);
1239 1289 if ((error = i_straccess(stp, JCREAD)) != 0) {
1240 1290 goto oops;
1241 1291 }
1242 1292 first = 0;
1243 1293 }
1244 1294
1245 1295 ASSERT(MUTEX_HELD(&stp->sd_lock));
1246 1296 ASSERT(bp);
1247 1297 pri = bp->b_band;
1248 1298 /*
1249 1299 * Extract any mark information. If the message is not
1250 1300 * completely consumed this information will be put in the mblk
1251 1301 * that is putback.
1252 1302 * If MSGMARKNEXT is set and the message is completely consumed
1253 1303 * the STRATMARK flag will be set below. Likewise, if
1254 1304 * MSGNOTMARKNEXT is set and the message is
1255 1305 * completely consumed STRNOTATMARK will be set.
1256 1306 *
1257 1307 * For some unknown reason strread only breaks the read at the
1258 1308 * last mark.
1259 1309 */
1260 1310 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
1261 1311 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
1262 1312 (MSGMARKNEXT|MSGNOTMARKNEXT));
1263 1313 if (mark != 0 && bp == stp->sd_mark) {
1264 1314 if (rflg) {
1265 1315 putback(stp, q, bp, pri);
1266 1316 goto oops;
1267 1317 }
1268 1318 mark |= _LASTMARK;
1269 1319 stp->sd_mark = NULL;
1270 1320 }
1271 1321 if ((stp->sd_flag & STRDELIM) && (bp->b_flag & MSGDELIM))
1272 1322 delim = 1;
1273 1323 mutex_exit(&stp->sd_lock);
1274 1324
1275 1325 if (STREAM_NEEDSERVICE(stp))
1276 1326 stream_runservice(stp);
1277 1327
1278 1328 type = bp->b_datap->db_type;
1279 1329
1280 1330 switch (type) {
1281 1331
1282 1332 case M_DATA:
1283 1333 ismdata:
1284 1334 if (msgnodata(bp)) {
1285 1335 if (mark || delim) {
1286 1336 freemsg(bp);
1287 1337 } else if (rflg) {
1288 1338
1289 1339 /*
1290 1340 * If already read data put zero
1291 1341 * length message back on queue else
1292 1342 * free msg and return 0.
1293 1343 */
1294 1344 bp->b_band = pri;
1295 1345 mutex_enter(&stp->sd_lock);
1296 1346 putback(stp, q, bp, pri);
1297 1347 mutex_exit(&stp->sd_lock);
1298 1348 } else {
1299 1349 freemsg(bp);
1300 1350 }
1301 1351 error = 0;
1302 1352 goto oops1;
1303 1353 }
1304 1354
1305 1355 rflg = 1;
1306 1356 waitflag |= NOINTR;
1307 1357 bp = struiocopyout(bp, uiop, &error);
1308 1358 if (error != 0)
1309 1359 goto oops1;
1310 1360
1311 1361 mutex_enter(&stp->sd_lock);
1312 1362 if (bp) {
1313 1363 /*
1314 1364 * Have remaining data in message.
1315 1365 * Free msg if in discard mode.
1316 1366 */
1317 1367 if (stp->sd_read_opt & RD_MSGDIS) {
1318 1368 freemsg(bp);
1319 1369 } else {
1320 1370 bp->b_band = pri;
1321 1371 if ((mark & _LASTMARK) &&
1322 1372 (stp->sd_mark == NULL))
1323 1373 stp->sd_mark = bp;
1324 1374 bp->b_flag |= mark & ~_LASTMARK;
1325 1375 if (delim)
1326 1376 bp->b_flag |= MSGDELIM;
1327 1377 if (msgnodata(bp))
1328 1378 freemsg(bp);
1329 1379 else
1330 1380 putback(stp, q, bp, pri);
1331 1381 }
1332 1382 } else {
1333 1383 /*
1334 1384 * Consumed the complete message.
1335 1385 * Move the MSG*MARKNEXT information
1336 1386 * to the stream head just in case
1337 1387 * the read queue becomes empty.
1338 1388 *
1339 1389 * If the stream head was at the mark
1340 1390 * (STRATMARK) before we dropped sd_lock above
1341 1391 * and some data was consumed then we have
1342 1392 * moved past the mark thus STRATMARK is
1343 1393 * cleared. However, if a message arrived in
1344 1394 * strrput during the copyout above causing
1345 1395 * STRATMARK to be set we can not clear that
1346 1396 * flag.
1347 1397 */
1348 1398 if (mark &
1349 1399 (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
1350 1400 if (mark & MSGMARKNEXT) {
1351 1401 stp->sd_flag &= ~STRNOTATMARK;
1352 1402 stp->sd_flag |= STRATMARK;
1353 1403 } else if (mark & MSGNOTMARKNEXT) {
1354 1404 stp->sd_flag &= ~STRATMARK;
1355 1405 stp->sd_flag |= STRNOTATMARK;
1356 1406 } else {
1357 1407 stp->sd_flag &=
1358 1408 ~(STRATMARK|STRNOTATMARK);
1359 1409 }
1360 1410 } else if (rflg && (old_sd_flag & STRATMARK)) {
1361 1411 stp->sd_flag &= ~STRATMARK;
1362 1412 }
1363 1413 }
1364 1414
1365 1415 /*
1366 1416 * Check for signal messages at the front of the read
1367 1417 * queue and generate the signal(s) if appropriate.
1368 1418 * The only signal that can be on queue is M_SIG at
1369 1419 * this point.
1370 1420 */
1371 1421 while ((((bp = q->q_first)) != NULL) &&
1372 1422 (bp->b_datap->db_type == M_SIG)) {
1373 1423 bp = getq_noenab(q, 0);
1374 1424 /*
1375 1425 * sd_lock is held so the content of the
1376 1426 * read queue can not change.
1377 1427 */
1378 1428 ASSERT(bp != NULL && DB_TYPE(bp) == M_SIG);
1379 1429 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
1380 1430 mutex_exit(&stp->sd_lock);
1381 1431 freemsg(bp);
1382 1432 if (STREAM_NEEDSERVICE(stp))
1383 1433 stream_runservice(stp);
1384 1434 mutex_enter(&stp->sd_lock);
1385 1435 }
1386 1436
1387 1437 if ((uiop->uio_resid == 0) || (mark & _LASTMARK) ||
1388 1438 delim ||
1389 1439 (stp->sd_read_opt & (RD_MSGDIS|RD_MSGNODIS))) {
1390 1440 goto oops;
1391 1441 }
1392 1442 continue;
1393 1443
1394 1444 case M_SIG:
1395 1445 strsignal(stp, *bp->b_rptr, (int32_t)bp->b_band);
1396 1446 freemsg(bp);
1397 1447 mutex_enter(&stp->sd_lock);
1398 1448 continue;
1399 1449
1400 1450 case M_PROTO:
1401 1451 case M_PCPROTO:
1402 1452 /*
1403 1453 * Only data messages are readable.
1404 1454 * Any others generate an error, unless
1405 1455 * RD_PROTDIS or RD_PROTDAT is set.
1406 1456 */
1407 1457 if (stp->sd_read_opt & RD_PROTDAT) {
1408 1458 for (nbp = bp; nbp; nbp = nbp->b_next) {
1409 1459 if ((nbp->b_datap->db_type ==
1410 1460 M_PROTO) ||
1411 1461 (nbp->b_datap->db_type ==
1412 1462 M_PCPROTO)) {
1413 1463 nbp->b_datap->db_type = M_DATA;
1414 1464 } else {
1415 1465 break;
1416 1466 }
1417 1467 }
1418 1468 /*
1419 1469 * clear stream head hi pri flag based on
1420 1470 * first message
1421 1471 */
1422 1472 if (type == M_PCPROTO) {
1423 1473 mutex_enter(&stp->sd_lock);
1424 1474 stp->sd_flag &= ~STRPRI;
1425 1475 mutex_exit(&stp->sd_lock);
1426 1476 }
1427 1477 goto ismdata;
1428 1478 } else if (stp->sd_read_opt & RD_PROTDIS) {
1429 1479 /*
1430 1480 * discard non-data messages
1431 1481 */
1432 1482 while (bp &&
1433 1483 ((bp->b_datap->db_type == M_PROTO) ||
1434 1484 (bp->b_datap->db_type == M_PCPROTO))) {
1435 1485 nbp = unlinkb(bp);
1436 1486 freeb(bp);
1437 1487 bp = nbp;
1438 1488 }
1439 1489 /*
1440 1490 * clear stream head hi pri flag based on
1441 1491 * first message
1442 1492 */
1443 1493 if (type == M_PCPROTO) {
1444 1494 mutex_enter(&stp->sd_lock);
1445 1495 stp->sd_flag &= ~STRPRI;
1446 1496 mutex_exit(&stp->sd_lock);
1447 1497 }
1448 1498 if (bp) {
1449 1499 bp->b_band = pri;
1450 1500 goto ismdata;
1451 1501 } else {
1452 1502 break;
1453 1503 }
1454 1504 }
1455 1505 /* FALLTHRU */
1456 1506 case M_PASSFP:
1457 1507 if ((bp->b_datap->db_type == M_PASSFP) &&
1458 1508 (stp->sd_read_opt & RD_PROTDIS)) {
1459 1509 freemsg(bp);
1460 1510 break;
1461 1511 }
1462 1512 mutex_enter(&stp->sd_lock);
1463 1513 putback(stp, q, bp, pri);
1464 1514 mutex_exit(&stp->sd_lock);
1465 1515 if (rflg == 0)
1466 1516 error = EBADMSG;
1467 1517 goto oops1;
1468 1518
1469 1519 default:
1470 1520 /*
1471 1521 * Garbage on stream head read queue.
1472 1522 */
1473 1523 cmn_err(CE_WARN, "bad %x found at stream head\n",
1474 1524 bp->b_datap->db_type);
1475 1525 freemsg(bp);
1476 1526 goto oops1;
1477 1527 }
1478 1528 mutex_enter(&stp->sd_lock);
1479 1529 }
1480 1530 oops:
1481 1531 mutex_exit(&stp->sd_lock);
1482 1532 oops1:
1483 1533 qbackenable(q, pri);
1484 1534 return (error);
1485 1535 #undef _LASTMARK
1486 1536 }
1487 1537
1488 1538 /*
1489 1539 * Default processing of M_PROTO/M_PCPROTO messages.
1490 1540 * Determine which wakeups and signals are needed.
1491 1541 * This can be replaced by a user-specified procedure for kernel users
1492 1542 * of STREAMS.
1493 1543 */
1494 1544 /* ARGSUSED */
1495 1545 mblk_t *
1496 1546 strrput_proto(vnode_t *vp, mblk_t *mp,
1497 1547 strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1498 1548 strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1499 1549 {
1500 1550 *wakeups = RSLEEP;
1501 1551 *allmsgsigs = 0;
1502 1552
1503 1553 switch (mp->b_datap->db_type) {
1504 1554 case M_PROTO:
1505 1555 if (mp->b_band == 0) {
1506 1556 *firstmsgsigs = S_INPUT | S_RDNORM;
1507 1557 *pollwakeups = POLLIN | POLLRDNORM;
1508 1558 } else {
1509 1559 *firstmsgsigs = S_INPUT | S_RDBAND;
1510 1560 *pollwakeups = POLLIN | POLLRDBAND;
1511 1561 }
1512 1562 break;
1513 1563 case M_PCPROTO:
1514 1564 *firstmsgsigs = S_HIPRI;
1515 1565 *pollwakeups = POLLPRI;
1516 1566 break;
1517 1567 }
1518 1568 return (mp);
1519 1569 }
1520 1570
1521 1571 /*
1522 1572 * Default processing of everything but M_DATA, M_PROTO, M_PCPROTO and
1523 1573 * M_PASSFP messages.
1524 1574 * Determine which wakeups and signals are needed.
1525 1575 * This can be replaced by a user-specified procedure for kernel users
1526 1576 * of STREAMS.
1527 1577 */
1528 1578 /* ARGSUSED */
1529 1579 mblk_t *
1530 1580 strrput_misc(vnode_t *vp, mblk_t *mp,
1531 1581 strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1532 1582 strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
1533 1583 {
1534 1584 *wakeups = 0;
1535 1585 *firstmsgsigs = 0;
1536 1586 *allmsgsigs = 0;
1537 1587 *pollwakeups = 0;
1538 1588 return (mp);
1539 1589 }
1540 1590
1541 1591 /*
1542 1592 * Stream read put procedure. Called from downstream driver/module
1543 1593 * with messages for the stream head. Data, protocol, and in-stream
1544 1594 * signal messages are placed on the queue, others are handled directly.
1545 1595 */
1546 1596 int
1547 1597 strrput(queue_t *q, mblk_t *bp)
1548 1598 {
1549 1599 struct stdata *stp;
1550 1600 ulong_t rput_opt;
1551 1601 strwakeup_t wakeups;
1552 1602 strsigset_t firstmsgsigs; /* Signals if first message on queue */
1553 1603 strsigset_t allmsgsigs; /* Signals for all messages */
1554 1604 strsigset_t signals; /* Signals events to generate */
1555 1605 strpollset_t pollwakeups;
1556 1606 mblk_t *nextbp;
1557 1607 uchar_t band = 0;
1558 1608 int hipri_sig;
1559 1609
1560 1610 stp = (struct stdata *)q->q_ptr;
1561 1611 /*
1562 1612 * Use rput_opt for optimized access to the SR_ flags except
1563 1613 * SR_POLLIN. That flag has to be checked under sd_lock since it
1564 1614 * is modified by strpoll().
1565 1615 */
1566 1616 rput_opt = stp->sd_rput_opt;
1567 1617
1568 1618 ASSERT(qclaimed(q));
1569 1619 TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_ENTER,
1570 1620 "strrput called with message type:q %p bp %p", q, bp);
1571 1621
1572 1622 /*
1573 1623 * Perform initial processing and pass to the parameterized functions.
1574 1624 */
1575 1625 ASSERT(bp->b_next == NULL);
1576 1626
1577 1627 switch (bp->b_datap->db_type) {
1578 1628 case M_DATA:
1579 1629 /*
1580 1630 * sockfs is the only consumer of STREOF and when it is set,
1581 1631 * it implies that the receiver is not interested in receiving
1582 1632 * any more data, hence the mblk is freed to prevent unnecessary
1583 1633 * message queueing at the stream head.
1584 1634 */
1585 1635 if (stp->sd_flag == STREOF) {
1586 1636 freemsg(bp);
1587 1637 return (0);
1588 1638 }
1589 1639 if ((rput_opt & SR_IGN_ZEROLEN) &&
1590 1640 bp->b_rptr == bp->b_wptr && msgnodata(bp)) {
1591 1641 /*
1592 1642 * Ignore zero-length M_DATA messages. These might be
1593 1643 * generated by some transports.
1594 1644 * The zero-length M_DATA messages, even if they
1595 1645 * are ignored, should effect the atmark tracking and
1596 1646 * should wake up a thread sleeping in strwaitmark.
1597 1647 */
1598 1648 mutex_enter(&stp->sd_lock);
1599 1649 if (bp->b_flag & MSGMARKNEXT) {
1600 1650 /*
1601 1651 * Record the position of the mark either
1602 1652 * in q_last or in STRATMARK.
1603 1653 */
1604 1654 if (q->q_last != NULL) {
1605 1655 q->q_last->b_flag &= ~MSGNOTMARKNEXT;
1606 1656 q->q_last->b_flag |= MSGMARKNEXT;
1607 1657 } else {
1608 1658 stp->sd_flag &= ~STRNOTATMARK;
1609 1659 stp->sd_flag |= STRATMARK;
1610 1660 }
1611 1661 } else if (bp->b_flag & MSGNOTMARKNEXT) {
1612 1662 /*
1613 1663 * Record that this is not the position of
1614 1664 * the mark either in q_last or in
1615 1665 * STRNOTATMARK.
1616 1666 */
1617 1667 if (q->q_last != NULL) {
1618 1668 q->q_last->b_flag &= ~MSGMARKNEXT;
1619 1669 q->q_last->b_flag |= MSGNOTMARKNEXT;
1620 1670 } else {
1621 1671 stp->sd_flag &= ~STRATMARK;
1622 1672 stp->sd_flag |= STRNOTATMARK;
1623 1673 }
1624 1674 }
1625 1675 if (stp->sd_flag & RSLEEP) {
1626 1676 stp->sd_flag &= ~RSLEEP;
1627 1677 cv_broadcast(&q->q_wait);
1628 1678 }
1629 1679 mutex_exit(&stp->sd_lock);
1630 1680 freemsg(bp);
1631 1681 return (0);
1632 1682 }
1633 1683 wakeups = RSLEEP;
1634 1684 if (bp->b_band == 0) {
1635 1685 firstmsgsigs = S_INPUT | S_RDNORM;
1636 1686 pollwakeups = POLLIN | POLLRDNORM;
1637 1687 } else {
1638 1688 firstmsgsigs = S_INPUT | S_RDBAND;
1639 1689 pollwakeups = POLLIN | POLLRDBAND;
1640 1690 }
1641 1691 if (rput_opt & SR_SIGALLDATA)
1642 1692 allmsgsigs = firstmsgsigs;
1643 1693 else
1644 1694 allmsgsigs = 0;
1645 1695
1646 1696 mutex_enter(&stp->sd_lock);
1647 1697 if ((rput_opt & SR_CONSOL_DATA) &&
1648 1698 (q->q_last != NULL) &&
1649 1699 (bp->b_flag & (MSGMARK|MSGDELIM)) == 0) {
1650 1700 /*
1651 1701 * Consolidate an M_DATA message onto an M_DATA,
1652 1702 * M_PROTO, or M_PCPROTO by merging it with q_last.
1653 1703 * The consolidation does not take place if
1654 1704 * the old message is marked with either of the
1655 1705 * marks or the delim flag or if the new
1656 1706 * message is marked with MSGMARK. The MSGMARK
1657 1707 * check is needed to handle the odd semantics of
1658 1708 * MSGMARK where essentially the whole message
1659 1709 * is to be treated as marked.
1660 1710 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from the
1661 1711 * new message to the front of the b_cont chain.
1662 1712 */
1663 1713 mblk_t *lbp = q->q_last;
1664 1714 unsigned char db_type = lbp->b_datap->db_type;
1665 1715
1666 1716 if ((db_type == M_DATA || db_type == M_PROTO ||
1667 1717 db_type == M_PCPROTO) &&
1668 1718 !(lbp->b_flag & (MSGDELIM|MSGMARK|MSGMARKNEXT))) {
1669 1719 rmvq_noenab(q, lbp);
1670 1720 /*
1671 1721 * The first message in the b_cont list
1672 1722 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
1673 1723 * We need to handle the case where we
1674 1724 * are appending:
1675 1725 *
1676 1726 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
1677 1727 * 2) a MSGMARKNEXT to a plain message.
1678 1728 * 3) a MSGNOTMARKNEXT to a plain message
1679 1729 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
1680 1730 * message.
1681 1731 *
1682 1732 * Thus we never append a MSGMARKNEXT or
1683 1733 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
1684 1734 */
1685 1735 if (bp->b_flag & MSGMARKNEXT) {
1686 1736 lbp->b_flag |= MSGMARKNEXT;
1687 1737 lbp->b_flag &= ~MSGNOTMARKNEXT;
1688 1738 bp->b_flag &= ~MSGMARKNEXT;
1689 1739 } else if (bp->b_flag & MSGNOTMARKNEXT) {
1690 1740 lbp->b_flag |= MSGNOTMARKNEXT;
1691 1741 bp->b_flag &= ~MSGNOTMARKNEXT;
1692 1742 }
1693 1743
1694 1744 linkb(lbp, bp);
1695 1745 bp = lbp;
1696 1746 /*
1697 1747 * The new message logically isn't the first
1698 1748 * even though the q_first check below thinks
1699 1749 * it is. Clear the firstmsgsigs to make it
1700 1750 * not appear to be first.
1701 1751 */
1702 1752 firstmsgsigs = 0;
1703 1753 }
1704 1754 }
1705 1755 break;
1706 1756
1707 1757 case M_PASSFP:
1708 1758 wakeups = RSLEEP;
1709 1759 allmsgsigs = 0;
1710 1760 if (bp->b_band == 0) {
1711 1761 firstmsgsigs = S_INPUT | S_RDNORM;
1712 1762 pollwakeups = POLLIN | POLLRDNORM;
1713 1763 } else {
1714 1764 firstmsgsigs = S_INPUT | S_RDBAND;
1715 1765 pollwakeups = POLLIN | POLLRDBAND;
1716 1766 }
1717 1767 mutex_enter(&stp->sd_lock);
1718 1768 break;
1719 1769
1720 1770 case M_PROTO:
1721 1771 case M_PCPROTO:
1722 1772 ASSERT(stp->sd_rprotofunc != NULL);
1723 1773 bp = (stp->sd_rprotofunc)(stp->sd_vnode, bp,
1724 1774 &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1725 1775 #define ALLSIG (S_INPUT|S_HIPRI|S_OUTPUT|S_MSG|S_ERROR|S_HANGUP|S_RDNORM|\
1726 1776 S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)
1727 1777 #define ALLPOLL (POLLIN|POLLPRI|POLLOUT|POLLRDNORM|POLLWRNORM|POLLRDBAND|\
1728 1778 POLLWRBAND)
1729 1779
1730 1780 ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1731 1781 ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1732 1782 ASSERT((allmsgsigs & ~ALLSIG) == 0);
1733 1783 ASSERT((pollwakeups & ~ALLPOLL) == 0);
1734 1784
1735 1785 mutex_enter(&stp->sd_lock);
1736 1786 break;
1737 1787
1738 1788 default:
1739 1789 ASSERT(stp->sd_rmiscfunc != NULL);
1740 1790 bp = (stp->sd_rmiscfunc)(stp->sd_vnode, bp,
1741 1791 &wakeups, &firstmsgsigs, &allmsgsigs, &pollwakeups);
1742 1792 ASSERT((wakeups & ~(RSLEEP|WSLEEP)) == 0);
1743 1793 ASSERT((firstmsgsigs & ~ALLSIG) == 0);
1744 1794 ASSERT((allmsgsigs & ~ALLSIG) == 0);
1745 1795 ASSERT((pollwakeups & ~ALLPOLL) == 0);
1746 1796 #undef ALLSIG
1747 1797 #undef ALLPOLL
1748 1798 mutex_enter(&stp->sd_lock);
1749 1799 break;
1750 1800 }
1751 1801 ASSERT(MUTEX_HELD(&stp->sd_lock));
1752 1802
1753 1803 /* By default generate superset of signals */
1754 1804 signals = (firstmsgsigs | allmsgsigs);
1755 1805
1756 1806 /*
1757 1807 * The proto and misc functions can return multiple messages
1758 1808 * as a b_next chain. Such messages are processed separately.
1759 1809 */
1760 1810 one_more:
1761 1811 hipri_sig = 0;
1762 1812 if (bp == NULL) {
1763 1813 nextbp = NULL;
1764 1814 } else {
1765 1815 nextbp = bp->b_next;
1766 1816 bp->b_next = NULL;
1767 1817
1768 1818 switch (bp->b_datap->db_type) {
1769 1819 case M_PCPROTO:
1770 1820 /*
1771 1821 * Only one priority protocol message is allowed at the
1772 1822 * stream head at a time.
1773 1823 */
1774 1824 if (stp->sd_flag & STRPRI) {
1775 1825 TRACE_0(TR_FAC_STREAMS_FR, TR_STRRPUT_PROTERR,
1776 1826 "M_PCPROTO already at head");
1777 1827 freemsg(bp);
1778 1828 mutex_exit(&stp->sd_lock);
1779 1829 goto done;
1780 1830 }
1781 1831 stp->sd_flag |= STRPRI;
1782 1832 hipri_sig = 1;
1783 1833 /* FALLTHRU */
1784 1834 case M_DATA:
1785 1835 case M_PROTO:
1786 1836 case M_PASSFP:
1787 1837 band = bp->b_band;
1788 1838 /*
1789 1839 * Marking doesn't work well when messages
1790 1840 * are marked in more than one band. We only
1791 1841 * remember the last message received, even if
1792 1842 * it is placed on the queue ahead of other
1793 1843 * marked messages.
1794 1844 */
1795 1845 if (bp->b_flag & MSGMARK)
1796 1846 stp->sd_mark = bp;
1797 1847 (void) putq(q, bp);
1798 1848
1799 1849 /*
1800 1850 * If message is a PCPROTO message, always use
1801 1851 * firstmsgsigs to determine if a signal should be
1802 1852 * sent as strrput is the only place to send
1803 1853 * signals for PCPROTO. Other messages are based on
1804 1854 * the STRGETINPROG flag. The flag determines if
1805 1855 * strrput or (k)strgetmsg will be responsible for
1806 1856 * sending the signals, in the firstmsgsigs case.
1807 1857 */
1808 1858 if ((hipri_sig == 1) ||
1809 1859 (((stp->sd_flag & STRGETINPROG) == 0) &&
1810 1860 (q->q_first == bp)))
1811 1861 signals = (firstmsgsigs | allmsgsigs);
1812 1862 else
1813 1863 signals = allmsgsigs;
1814 1864 break;
1815 1865
1816 1866 default:
1817 1867 mutex_exit(&stp->sd_lock);
1818 1868 (void) strrput_nondata(q, bp);
1819 1869 mutex_enter(&stp->sd_lock);
1820 1870 break;
1821 1871 }
1822 1872 }
1823 1873 ASSERT(MUTEX_HELD(&stp->sd_lock));
1824 1874 /*
1825 1875 * Wake sleeping read/getmsg and cancel deferred wakeup
1826 1876 */
1827 1877 if (wakeups & RSLEEP)
1828 1878 stp->sd_wakeq &= ~RSLEEP;
1829 1879
1830 1880 wakeups &= stp->sd_flag;
1831 1881 if (wakeups & RSLEEP) {
1832 1882 stp->sd_flag &= ~RSLEEP;
1833 1883 cv_broadcast(&q->q_wait);
1834 1884 }
1835 1885 if (wakeups & WSLEEP) {
1836 1886 stp->sd_flag &= ~WSLEEP;
1837 1887 cv_broadcast(&_WR(q)->q_wait);
1838 1888 }
1839 1889
1840 1890 if (pollwakeups != 0) {
1841 1891 if (pollwakeups == (POLLIN | POLLRDNORM)) {
1842 1892 /*
1843 1893 * Can't use rput_opt since it was not
1844 1894 * read when sd_lock was held and SR_POLLIN is changed
1845 1895 * by strpoll() under sd_lock.
1846 1896 */
1847 1897 if (!(stp->sd_rput_opt & SR_POLLIN))
1848 1898 goto no_pollwake;
1849 1899 stp->sd_rput_opt &= ~SR_POLLIN;
1850 1900 }
1851 1901 mutex_exit(&stp->sd_lock);
1852 1902 pollwakeup(&stp->sd_pollist, pollwakeups);
1853 1903 mutex_enter(&stp->sd_lock);
1854 1904 }
1855 1905 no_pollwake:
1856 1906
1857 1907 /*
1858 1908 * strsendsig can handle multiple signals with a
1859 1909 * single call.
1860 1910 */
1861 1911 if (stp->sd_sigflags & signals)
1862 1912 strsendsig(stp->sd_siglist, signals, band, 0);
1863 1913 mutex_exit(&stp->sd_lock);
1864 1914
1865 1915
1866 1916 done:
1867 1917 if (nextbp == NULL)
1868 1918 return (0);
1869 1919
1870 1920 /*
1871 1921 * Any signals were handled the first time.
1872 1922 * Wakeups and pollwakeups are redone to avoid any race
1873 1923 * conditions - all the messages are not queued until the
1874 1924 * last message has been processed by strrput.
1875 1925 */
1876 1926 bp = nextbp;
1877 1927 signals = firstmsgsigs = allmsgsigs = 0;
1878 1928 mutex_enter(&stp->sd_lock);
1879 1929 goto one_more;
1880 1930 }
1881 1931
1882 1932 static void
1883 1933 log_dupioc(queue_t *rq, mblk_t *bp)
1884 1934 {
1885 1935 queue_t *wq, *qp;
1886 1936 char *modnames, *mnp, *dname;
1887 1937 size_t maxmodstr;
1888 1938 boolean_t islast;
1889 1939
1890 1940 /*
1891 1941 * Allocate a buffer large enough to hold the names of nstrpush modules
1892 1942 * and one driver, with spaces between and NUL terminator. If we can't
1893 1943 * get memory, then we'll just log the driver name.
1894 1944 */
1895 1945 maxmodstr = nstrpush * (FMNAMESZ + 1);
1896 1946 mnp = modnames = kmem_alloc(maxmodstr, KM_NOSLEEP);
1897 1947
1898 1948 /* march down write side to print log message down to the driver */
1899 1949 wq = WR(rq);
1900 1950
1901 1951 /* make sure q_next doesn't shift around while we're grabbing data */
1902 1952 claimstr(wq);
1903 1953 qp = wq->q_next;
1904 1954 do {
1905 1955 dname = Q2NAME(qp);
1906 1956 islast = !SAMESTR(qp) || qp->q_next == NULL;
1907 1957 if (modnames == NULL) {
1908 1958 /*
1909 1959 * If we don't have memory, then get the driver name in
1910 1960 * the log where we can see it. Note that memory
1911 1961 * pressure is a possible cause of these sorts of bugs.
1912 1962 */
1913 1963 if (islast) {
1914 1964 modnames = dname;
1915 1965 maxmodstr = 0;
1916 1966 }
1917 1967 } else {
1918 1968 mnp += snprintf(mnp, FMNAMESZ + 1, "%s", dname);
1919 1969 if (!islast)
1920 1970 *mnp++ = ' ';
1921 1971 }
1922 1972 qp = qp->q_next;
1923 1973 } while (!islast);
1924 1974 releasestr(wq);
1925 1975 /* Cannot happen unless stream head is corrupt. */
1926 1976 ASSERT(modnames != NULL);
1927 1977 (void) strlog(rq->q_qinfo->qi_minfo->mi_idnum, 0, 1,
1928 1978 SL_CONSOLE|SL_TRACE|SL_ERROR,
1929 1979 "Warning: stream %p received duplicate %X M_IOC%s; module list: %s",
1930 1980 rq->q_ptr, ((struct iocblk *)bp->b_rptr)->ioc_cmd,
1931 1981 (DB_TYPE(bp) == M_IOCACK ? "ACK" : "NAK"), modnames);
1932 1982 if (maxmodstr != 0)
1933 1983 kmem_free(modnames, maxmodstr);
1934 1984 }
1935 1985
1936 1986 int
1937 1987 strrput_nondata(queue_t *q, mblk_t *bp)
1938 1988 {
1939 1989 struct stdata *stp;
1940 1990 struct iocblk *iocbp;
1941 1991 struct stroptions *sop;
1942 1992 struct copyreq *reqp;
1943 1993 struct copyresp *resp;
1944 1994 unsigned char bpri;
1945 1995 unsigned char flushed_already = 0;
1946 1996
1947 1997 stp = (struct stdata *)q->q_ptr;
1948 1998
1949 1999 ASSERT(!(stp->sd_flag & STPLEX));
1950 2000 ASSERT(qclaimed(q));
1951 2001
1952 2002 switch (bp->b_datap->db_type) {
1953 2003 case M_ERROR:
1954 2004 /*
1955 2005 * An error has occurred downstream, the errno is in the first
1956 2006 * bytes of the message.
1957 2007 */
1958 2008 if ((bp->b_wptr - bp->b_rptr) == 2) { /* New flavor */
1959 2009 unsigned char rw = 0;
1960 2010
1961 2011 mutex_enter(&stp->sd_lock);
1962 2012 if (*bp->b_rptr != NOERROR) { /* read error */
1963 2013 if (*bp->b_rptr != 0) {
1964 2014 if (stp->sd_flag & STRDERR)
1965 2015 flushed_already |= FLUSHR;
1966 2016 stp->sd_flag |= STRDERR;
1967 2017 rw |= FLUSHR;
1968 2018 } else {
1969 2019 stp->sd_flag &= ~STRDERR;
1970 2020 }
1971 2021 stp->sd_rerror = *bp->b_rptr;
1972 2022 }
1973 2023 bp->b_rptr++;
1974 2024 if (*bp->b_rptr != NOERROR) { /* write error */
1975 2025 if (*bp->b_rptr != 0) {
1976 2026 if (stp->sd_flag & STWRERR)
1977 2027 flushed_already |= FLUSHW;
1978 2028 stp->sd_flag |= STWRERR;
1979 2029 rw |= FLUSHW;
1980 2030 } else {
1981 2031 stp->sd_flag &= ~STWRERR;
1982 2032 }
1983 2033 stp->sd_werror = *bp->b_rptr;
1984 2034 }
1985 2035 if (rw) {
1986 2036 TRACE_2(TR_FAC_STREAMS_FR, TR_STRRPUT_WAKE,
1987 2037 "strrput cv_broadcast:q %p, bp %p",
1988 2038 q, bp);
1989 2039 cv_broadcast(&q->q_wait); /* readers */
1990 2040 cv_broadcast(&_WR(q)->q_wait); /* writers */
1991 2041 cv_broadcast(&stp->sd_monitor); /* ioctllers */
1992 2042
1993 2043 mutex_exit(&stp->sd_lock);
1994 2044 pollwakeup(&stp->sd_pollist, POLLERR);
1995 2045 mutex_enter(&stp->sd_lock);
1996 2046
1997 2047 if (stp->sd_sigflags & S_ERROR)
1998 2048 strsendsig(stp->sd_siglist, S_ERROR, 0,
1999 2049 ((rw & FLUSHR) ? stp->sd_rerror :
2000 2050 stp->sd_werror));
2001 2051 mutex_exit(&stp->sd_lock);
2002 2052 /*
2003 2053 * Send the M_FLUSH only
2004 2054 * for the first M_ERROR
2005 2055 * message on the stream
2006 2056 */
2007 2057 if (flushed_already == rw) {
2008 2058 freemsg(bp);
2009 2059 return (0);
2010 2060 }
2011 2061
2012 2062 bp->b_datap->db_type = M_FLUSH;
2013 2063 *bp->b_rptr = rw;
2014 2064 bp->b_wptr = bp->b_rptr + 1;
2015 2065 /*
2016 2066 * Protect against the driver
2017 2067 * passing up messages after
2018 2068 * it has done a qprocsoff
2019 2069 */
2020 2070 if (_OTHERQ(q)->q_next == NULL)
2021 2071 freemsg(bp);
2022 2072 else
2023 2073 qreply(q, bp);
2024 2074 return (0);
2025 2075 } else
2026 2076 mutex_exit(&stp->sd_lock);
2027 2077 } else if (*bp->b_rptr != 0) { /* Old flavor */
2028 2078 if (stp->sd_flag & (STRDERR|STWRERR))
2029 2079 flushed_already = FLUSHRW;
2030 2080 mutex_enter(&stp->sd_lock);
2031 2081 stp->sd_flag |= (STRDERR|STWRERR);
2032 2082 stp->sd_rerror = *bp->b_rptr;
2033 2083 stp->sd_werror = *bp->b_rptr;
2034 2084 TRACE_2(TR_FAC_STREAMS_FR,
2035 2085 TR_STRRPUT_WAKE2,
2036 2086 "strrput wakeup #2:q %p, bp %p", q, bp);
2037 2087 cv_broadcast(&q->q_wait); /* the readers */
2038 2088 cv_broadcast(&_WR(q)->q_wait); /* the writers */
2039 2089 cv_broadcast(&stp->sd_monitor); /* ioctllers */
2040 2090
2041 2091 mutex_exit(&stp->sd_lock);
2042 2092 pollwakeup(&stp->sd_pollist, POLLERR);
2043 2093 mutex_enter(&stp->sd_lock);
2044 2094
2045 2095 if (stp->sd_sigflags & S_ERROR)
2046 2096 strsendsig(stp->sd_siglist, S_ERROR, 0,
2047 2097 (stp->sd_werror ? stp->sd_werror :
2048 2098 stp->sd_rerror));
2049 2099 mutex_exit(&stp->sd_lock);
2050 2100
2051 2101 /*
2052 2102 * Send the M_FLUSH only
2053 2103 * for the first M_ERROR
2054 2104 * message on the stream
2055 2105 */
2056 2106 if (flushed_already != FLUSHRW) {
2057 2107 bp->b_datap->db_type = M_FLUSH;
2058 2108 *bp->b_rptr = FLUSHRW;
2059 2109 /*
2060 2110 * Protect against the driver passing up
2061 2111 * messages after it has done a
2062 2112 * qprocsoff.
2063 2113 */
2064 2114 if (_OTHERQ(q)->q_next == NULL)
2065 2115 freemsg(bp);
2066 2116 else
2067 2117 qreply(q, bp);
2068 2118 return (0);
2069 2119 }
2070 2120 }
2071 2121 freemsg(bp);
2072 2122 return (0);
2073 2123
2074 2124 case M_HANGUP:
2075 2125
2076 2126 freemsg(bp);
2077 2127 mutex_enter(&stp->sd_lock);
2078 2128 stp->sd_werror = ENXIO;
2079 2129 stp->sd_flag |= STRHUP;
2080 2130 stp->sd_flag &= ~(WSLEEP|RSLEEP);
2081 2131
2082 2132 /*
2083 2133 * send signal if controlling tty
2084 2134 */
2085 2135
2086 2136 if (stp->sd_sidp) {
2087 2137 prsignal(stp->sd_sidp, SIGHUP);
2088 2138 if (stp->sd_sidp != stp->sd_pgidp)
2089 2139 pgsignal(stp->sd_pgidp, SIGTSTP);
2090 2140 }
2091 2141
2092 2142 /*
2093 2143 * wake up read, write, and exception pollers and
2094 2144 * reset wakeup mechanism.
2095 2145 */
2096 2146 cv_broadcast(&q->q_wait); /* the readers */
2097 2147 cv_broadcast(&_WR(q)->q_wait); /* the writers */
2098 2148 cv_broadcast(&stp->sd_monitor); /* the ioctllers */
2099 2149 strhup(stp);
2100 2150 mutex_exit(&stp->sd_lock);
2101 2151 return (0);
2102 2152
2103 2153 case M_UNHANGUP:
2104 2154 freemsg(bp);
2105 2155 mutex_enter(&stp->sd_lock);
2106 2156 stp->sd_werror = 0;
2107 2157 stp->sd_flag &= ~STRHUP;
2108 2158 mutex_exit(&stp->sd_lock);
2109 2159 return (0);
2110 2160
2111 2161 case M_SIG:
2112 2162 /*
2113 2163 * Someone downstream wants to post a signal. The
2114 2164 * signal to post is contained in the first byte of the
2115 2165 * message. If the message would go on the front of
2116 2166 * the queue, send a signal to the process group
2117 2167 * (if not SIGPOLL) or to the siglist processes
2118 2168 * (SIGPOLL). If something is already on the queue,
2119 2169 * OR if we are delivering a delayed suspend (*sigh*
2120 2170 * another "tty" hack) and there's no one sleeping already,
2121 2171 * just enqueue the message.
2122 2172 */
2123 2173 mutex_enter(&stp->sd_lock);
2124 2174 if (q->q_first || (*bp->b_rptr == SIGTSTP &&
2125 2175 !(stp->sd_flag & RSLEEP))) {
2126 2176 (void) putq(q, bp);
2127 2177 mutex_exit(&stp->sd_lock);
2128 2178 return (0);
2129 2179 }
2130 2180 mutex_exit(&stp->sd_lock);
2131 2181 /* FALLTHRU */
2132 2182
2133 2183 case M_PCSIG:
2134 2184 /*
2135 2185 * Don't enqueue, just post the signal.
2136 2186 */
2137 2187 strsignal(stp, *bp->b_rptr, 0L);
2138 2188 freemsg(bp);
2139 2189 return (0);
2140 2190
2141 2191 case M_CMD:
2142 2192 if (MBLKL(bp) != sizeof (cmdblk_t)) {
2143 2193 freemsg(bp);
2144 2194 return (0);
2145 2195 }
2146 2196
2147 2197 mutex_enter(&stp->sd_lock);
2148 2198 if (stp->sd_flag & STRCMDWAIT) {
2149 2199 ASSERT(stp->sd_cmdblk == NULL);
2150 2200 stp->sd_cmdblk = bp;
2151 2201 cv_broadcast(&stp->sd_monitor);
2152 2202 mutex_exit(&stp->sd_lock);
2153 2203 } else {
2154 2204 mutex_exit(&stp->sd_lock);
2155 2205 freemsg(bp);
2156 2206 }
2157 2207 return (0);
2158 2208
2159 2209 case M_FLUSH:
2160 2210 /*
2161 2211 * Flush queues. The indication of which queues to flush
2162 2212 * is in the first byte of the message. If the read queue
2163 2213 * is specified, then flush it. If FLUSHBAND is set, just
2164 2214 * flush the band specified by the second byte of the message.
2165 2215 *
2166 2216 * If a module has issued a M_SETOPT to not flush hi
2167 2217 * priority messages off of the stream head, then pass this
2168 2218 * flag into the flushq code to preserve such messages.
2169 2219 */
2170 2220
2171 2221 if (*bp->b_rptr & FLUSHR) {
2172 2222 mutex_enter(&stp->sd_lock);
2173 2223 if (*bp->b_rptr & FLUSHBAND) {
2174 2224 ASSERT((bp->b_wptr - bp->b_rptr) >= 2);
2175 2225 flushband(q, *(bp->b_rptr + 1), FLUSHALL);
2176 2226 } else
2177 2227 flushq_common(q, FLUSHALL,
2178 2228 stp->sd_read_opt & RFLUSHPCPROT);
2179 2229 if ((q->q_first == NULL) ||
2180 2230 (q->q_first->b_datap->db_type < QPCTL))
2181 2231 stp->sd_flag &= ~STRPRI;
2182 2232 else {
2183 2233 ASSERT(stp->sd_flag & STRPRI);
2184 2234 }
2185 2235 mutex_exit(&stp->sd_lock);
2186 2236 }
2187 2237 if ((*bp->b_rptr & FLUSHW) && !(bp->b_flag & MSGNOLOOP)) {
2188 2238 *bp->b_rptr &= ~FLUSHR;
2189 2239 bp->b_flag |= MSGNOLOOP;
2190 2240 /*
2191 2241 * Protect against the driver passing up
2192 2242 * messages after it has done a qprocsoff.
2193 2243 */
2194 2244 if (_OTHERQ(q)->q_next == NULL)
2195 2245 freemsg(bp);
2196 2246 else
2197 2247 qreply(q, bp);
2198 2248 return (0);
2199 2249 }
2200 2250 freemsg(bp);
2201 2251 return (0);
2202 2252
2203 2253 case M_IOCACK:
2204 2254 case M_IOCNAK:
2205 2255 iocbp = (struct iocblk *)bp->b_rptr;
2206 2256 /*
2207 2257 * If not waiting for ACK or NAK then just free msg.
2208 2258 * If incorrect id sequence number then just free msg.
2209 2259 * If already have ACK or NAK for user then this is a
2210 2260 * duplicate, display a warning and free the msg.
2211 2261 */
2212 2262 mutex_enter(&stp->sd_lock);
2213 2263 if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2214 2264 (stp->sd_iocid != iocbp->ioc_id)) {
2215 2265 /*
2216 2266 * If the ACK/NAK is a dup, display a message
2217 2267 * Dup is when sd_iocid == ioc_id, and
2218 2268 * sd_iocblk == <valid ptr> or -1 (the former
2219 2269 * is when an ioctl has been put on the stream
2220 2270 * head, but has not yet been consumed, the
2221 2271 * later is when it has been consumed).
2222 2272 */
2223 2273 if ((stp->sd_iocid == iocbp->ioc_id) &&
2224 2274 (stp->sd_iocblk != NULL)) {
2225 2275 log_dupioc(q, bp);
2226 2276 }
2227 2277 freemsg(bp);
2228 2278 mutex_exit(&stp->sd_lock);
2229 2279 return (0);
2230 2280 }
2231 2281
2232 2282 /*
2233 2283 * Assign ACK or NAK to user and wake up.
2234 2284 */
2235 2285 stp->sd_iocblk = bp;
2236 2286 cv_broadcast(&stp->sd_monitor);
2237 2287 mutex_exit(&stp->sd_lock);
2238 2288 return (0);
2239 2289
2240 2290 case M_COPYIN:
2241 2291 case M_COPYOUT:
2242 2292 reqp = (struct copyreq *)bp->b_rptr;
2243 2293
2244 2294 /*
2245 2295 * If not waiting for ACK or NAK then just fail request.
2246 2296 * If already have ACK, NAK, or copy request, then just
2247 2297 * fail request.
2248 2298 * If incorrect id sequence number then just fail request.
2249 2299 */
2250 2300 mutex_enter(&stp->sd_lock);
2251 2301 if ((stp->sd_flag & IOCWAIT) == 0 || stp->sd_iocblk ||
2252 2302 (stp->sd_iocid != reqp->cq_id)) {
2253 2303 if (bp->b_cont) {
2254 2304 freemsg(bp->b_cont);
2255 2305 bp->b_cont = NULL;
2256 2306 }
2257 2307 bp->b_datap->db_type = M_IOCDATA;
2258 2308 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
2259 2309 resp = (struct copyresp *)bp->b_rptr;
2260 2310 resp->cp_rval = (caddr_t)1; /* failure */
2261 2311 mutex_exit(&stp->sd_lock);
2262 2312 putnext(stp->sd_wrq, bp);
2263 2313 return (0);
2264 2314 }
2265 2315
2266 2316 /*
2267 2317 * Assign copy request to user and wake up.
2268 2318 */
2269 2319 stp->sd_iocblk = bp;
2270 2320 cv_broadcast(&stp->sd_monitor);
2271 2321 mutex_exit(&stp->sd_lock);
2272 2322 return (0);
2273 2323
2274 2324 case M_SETOPTS:
2275 2325 /*
2276 2326 * Set stream head options (read option, write offset,
2277 2327 * min/max packet size, and/or high/low water marks for
2278 2328 * the read side only).
2279 2329 */
2280 2330
2281 2331 bpri = 0;
2282 2332 sop = (struct stroptions *)bp->b_rptr;
2283 2333 mutex_enter(&stp->sd_lock);
2284 2334 if (sop->so_flags & SO_READOPT) {
2285 2335 switch (sop->so_readopt & RMODEMASK) {
2286 2336 case RNORM:
2287 2337 stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
2288 2338 break;
2289 2339
2290 2340 case RMSGD:
2291 2341 stp->sd_read_opt =
2292 2342 ((stp->sd_read_opt & ~RD_MSGNODIS) |
2293 2343 RD_MSGDIS);
2294 2344 break;
2295 2345
2296 2346 case RMSGN:
2297 2347 stp->sd_read_opt =
2298 2348 ((stp->sd_read_opt & ~RD_MSGDIS) |
2299 2349 RD_MSGNODIS);
2300 2350 break;
2301 2351 }
2302 2352 switch (sop->so_readopt & RPROTMASK) {
2303 2353 case RPROTNORM:
2304 2354 stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
2305 2355 break;
2306 2356
2307 2357 case RPROTDAT:
2308 2358 stp->sd_read_opt =
2309 2359 ((stp->sd_read_opt & ~RD_PROTDIS) |
2310 2360 RD_PROTDAT);
2311 2361 break;
2312 2362
2313 2363 case RPROTDIS:
2314 2364 stp->sd_read_opt =
2315 2365 ((stp->sd_read_opt & ~RD_PROTDAT) |
2316 2366 RD_PROTDIS);
2317 2367 break;
2318 2368 }
2319 2369 switch (sop->so_readopt & RFLUSHMASK) {
2320 2370 case RFLUSHPCPROT:
2321 2371 /*
2322 2372 * This sets the stream head to NOT flush
2323 2373 * M_PCPROTO messages.
2324 2374 */
2325 2375 stp->sd_read_opt |= RFLUSHPCPROT;
2326 2376 break;
2327 2377 }
2328 2378 }
2329 2379 if (sop->so_flags & SO_ERROPT) {
2330 2380 switch (sop->so_erropt & RERRMASK) {
2331 2381 case RERRNORM:
2332 2382 stp->sd_flag &= ~STRDERRNONPERSIST;
2333 2383 break;
2334 2384 case RERRNONPERSIST:
2335 2385 stp->sd_flag |= STRDERRNONPERSIST;
2336 2386 break;
2337 2387 }
2338 2388 switch (sop->so_erropt & WERRMASK) {
2339 2389 case WERRNORM:
2340 2390 stp->sd_flag &= ~STWRERRNONPERSIST;
2341 2391 break;
2342 2392 case WERRNONPERSIST:
2343 2393 stp->sd_flag |= STWRERRNONPERSIST;
2344 2394 break;
2345 2395 }
2346 2396 }
2347 2397 if (sop->so_flags & SO_COPYOPT) {
2348 2398 if (sop->so_copyopt & ZCVMSAFE) {
2349 2399 stp->sd_copyflag |= STZCVMSAFE;
2350 2400 stp->sd_copyflag &= ~STZCVMUNSAFE;
2351 2401 } else if (sop->so_copyopt & ZCVMUNSAFE) {
2352 2402 stp->sd_copyflag |= STZCVMUNSAFE;
2353 2403 stp->sd_copyflag &= ~STZCVMSAFE;
2354 2404 }
2355 2405
2356 2406 if (sop->so_copyopt & COPYCACHED) {
2357 2407 stp->sd_copyflag |= STRCOPYCACHED;
2358 2408 }
2359 2409 }
2360 2410 if (sop->so_flags & SO_WROFF)
2361 2411 stp->sd_wroff = sop->so_wroff;
2362 2412 if (sop->so_flags & SO_TAIL)
2363 2413 stp->sd_tail = sop->so_tail;
2364 2414 if (sop->so_flags & SO_MINPSZ)
2365 2415 q->q_minpsz = sop->so_minpsz;
2366 2416 if (sop->so_flags & SO_MAXPSZ)
2367 2417 q->q_maxpsz = sop->so_maxpsz;
2368 2418 if (sop->so_flags & SO_MAXBLK)
2369 2419 stp->sd_maxblk = sop->so_maxblk;
2370 2420 if (sop->so_flags & SO_HIWAT) {
2371 2421 if (sop->so_flags & SO_BAND) {
2372 2422 if (strqset(q, QHIWAT,
2373 2423 sop->so_band, sop->so_hiwat)) {
2374 2424 cmn_err(CE_WARN, "strrput: could not "
2375 2425 "allocate qband\n");
2376 2426 } else {
2377 2427 bpri = sop->so_band;
2378 2428 }
2379 2429 } else {
2380 2430 q->q_hiwat = sop->so_hiwat;
2381 2431 }
2382 2432 }
2383 2433 if (sop->so_flags & SO_LOWAT) {
2384 2434 if (sop->so_flags & SO_BAND) {
2385 2435 if (strqset(q, QLOWAT,
2386 2436 sop->so_band, sop->so_lowat)) {
2387 2437 cmn_err(CE_WARN, "strrput: could not "
2388 2438 "allocate qband\n");
2389 2439 } else {
2390 2440 bpri = sop->so_band;
2391 2441 }
2392 2442 } else {
2393 2443 q->q_lowat = sop->so_lowat;
2394 2444 }
2395 2445 }
2396 2446 if (sop->so_flags & SO_MREADON)
2397 2447 stp->sd_flag |= SNDMREAD;
2398 2448 if (sop->so_flags & SO_MREADOFF)
2399 2449 stp->sd_flag &= ~SNDMREAD;
2400 2450 if (sop->so_flags & SO_NDELON)
2401 2451 stp->sd_flag |= OLDNDELAY;
2402 2452 if (sop->so_flags & SO_NDELOFF)
2403 2453 stp->sd_flag &= ~OLDNDELAY;
2404 2454 if (sop->so_flags & SO_ISTTY)
2405 2455 stp->sd_flag |= STRISTTY;
2406 2456 if (sop->so_flags & SO_ISNTTY)
2407 2457 stp->sd_flag &= ~STRISTTY;
2408 2458 if (sop->so_flags & SO_TOSTOP)
2409 2459 stp->sd_flag |= STRTOSTOP;
2410 2460 if (sop->so_flags & SO_TONSTOP)
2411 2461 stp->sd_flag &= ~STRTOSTOP;
2412 2462 if (sop->so_flags & SO_DELIM)
2413 2463 stp->sd_flag |= STRDELIM;
2414 2464 if (sop->so_flags & SO_NODELIM)
2415 2465 stp->sd_flag &= ~STRDELIM;
2416 2466
2417 2467 mutex_exit(&stp->sd_lock);
2418 2468 freemsg(bp);
2419 2469
2420 2470 /* Check backenable in case the water marks changed */
2421 2471 qbackenable(q, bpri);
2422 2472 return (0);
2423 2473
2424 2474 /*
2425 2475 * The following set of cases deal with situations where two stream
2426 2476 * heads are connected to each other (twisted streams). These messages
2427 2477 * have no meaning at the stream head.
2428 2478 */
2429 2479 case M_BREAK:
2430 2480 case M_CTL:
2431 2481 case M_DELAY:
2432 2482 case M_START:
2433 2483 case M_STOP:
2434 2484 case M_IOCDATA:
2435 2485 case M_STARTI:
2436 2486 case M_STOPI:
2437 2487 freemsg(bp);
2438 2488 return (0);
2439 2489
2440 2490 case M_IOCTL:
2441 2491 /*
2442 2492 * Always NAK this condition
2443 2493 * (makes no sense)
2444 2494 * If there is one or more threads in the read side
2445 2495 * rwnext we have to defer the nacking until that thread
2446 2496 * returns (in strget).
2447 2497 */
2448 2498 mutex_enter(&stp->sd_lock);
2449 2499 if (stp->sd_struiodnak != 0) {
2450 2500 /*
2451 2501 * Defer NAK to the streamhead. Queue at the end
2452 2502 * the list.
2453 2503 */
2454 2504 mblk_t *mp = stp->sd_struionak;
2455 2505
2456 2506 while (mp && mp->b_next)
2457 2507 mp = mp->b_next;
2458 2508 if (mp)
2459 2509 mp->b_next = bp;
2460 2510 else
2461 2511 stp->sd_struionak = bp;
2462 2512 bp->b_next = NULL;
2463 2513 mutex_exit(&stp->sd_lock);
2464 2514 return (0);
2465 2515 }
2466 2516 mutex_exit(&stp->sd_lock);
2467 2517
2468 2518 bp->b_datap->db_type = M_IOCNAK;
2469 2519 /*
2470 2520 * Protect against the driver passing up
2471 2521 * messages after it has done a qprocsoff.
2472 2522 */
2473 2523 if (_OTHERQ(q)->q_next == NULL)
2474 2524 freemsg(bp);
2475 2525 else
2476 2526 qreply(q, bp);
2477 2527 return (0);
2478 2528
2479 2529 default:
2480 2530 #ifdef DEBUG
2481 2531 cmn_err(CE_WARN,
2482 2532 "bad message type %x received at stream head\n",
2483 2533 bp->b_datap->db_type);
2484 2534 #endif
2485 2535 freemsg(bp);
2486 2536 return (0);
2487 2537 }
2488 2538
2489 2539 /* NOTREACHED */
2490 2540 }
2491 2541
2492 2542 /*
2493 2543 * Check if the stream pointed to by `stp' can be written to, and return an
2494 2544 * error code if not. If `eiohup' is set, then return EIO if STRHUP is set.
2495 2545 * If `sigpipeok' is set and the SW_SIGPIPE option is enabled on the stream,
2496 2546 * then always return EPIPE and send a SIGPIPE to the invoking thread.
2497 2547 */
2498 2548 static int
2499 2549 strwriteable(struct stdata *stp, boolean_t eiohup, boolean_t sigpipeok)
2500 2550 {
2501 2551 int error;
2502 2552
2503 2553 ASSERT(MUTEX_HELD(&stp->sd_lock));
2504 2554
2505 2555 /*
2506 2556 * For modem support, POSIX states that on writes, EIO should
2507 2557 * be returned if the stream has been hung up.
2508 2558 */
2509 2559 if (eiohup && (stp->sd_flag & (STPLEX|STRHUP)) == STRHUP)
2510 2560 error = EIO;
2511 2561 else
2512 2562 error = strgeterr(stp, STRHUP|STPLEX|STWRERR, 0);
2513 2563
2514 2564 if (error != 0) {
2515 2565 if (!(stp->sd_flag & STPLEX) &&
2516 2566 (stp->sd_wput_opt & SW_SIGPIPE) && sigpipeok) {
2517 2567 tsignal(curthread, SIGPIPE);
2518 2568 error = EPIPE;
2519 2569 }
2520 2570 }
2521 2571
2522 2572 return (error);
2523 2573 }
2524 2574
2525 2575 /*
2526 2576 * Copyin and send data down a stream.
2527 2577 * The caller will allocate and copyin any control part that precedes the
2528 2578 * message and pass that in as mctl.
2529 2579 *
2530 2580 * Caller should *not* hold sd_lock.
2531 2581 * When EWOULDBLOCK is returned the caller has to redo the canputnext
2532 2582 * under sd_lock in order to avoid missing a backenabling wakeup.
2533 2583 *
2534 2584 * Use iosize = -1 to not send any M_DATA. iosize = 0 sends zero-length M_DATA.
2535 2585 *
2536 2586 * Set MSG_IGNFLOW in flags to ignore flow control for hipri messages.
2537 2587 * For sync streams we can only ignore flow control by reverting to using
2538 2588 * putnext.
2539 2589 *
2540 2590 * If sd_maxblk is less than *iosize this routine might return without
2541 2591 * transferring all of *iosize. In all cases, on return *iosize will contain
2542 2592 * the amount of data that was transferred.
2543 2593 */
2544 2594 static int
2545 2595 strput(struct stdata *stp, mblk_t *mctl, struct uio *uiop, ssize_t *iosize,
2546 2596 int b_flag, int pri, int flags)
2547 2597 {
2548 2598 struiod_t uiod;
2549 2599 mblk_t *mp;
2550 2600 queue_t *wqp = stp->sd_wrq;
2551 2601 int error = 0;
2552 2602 ssize_t count = *iosize;
2553 2603
2554 2604 ASSERT(MUTEX_NOT_HELD(&stp->sd_lock));
2555 2605
2556 2606 if (uiop != NULL && count >= 0)
2557 2607 flags |= stp->sd_struiowrq ? STRUIO_POSTPONE : 0;
2558 2608
2559 2609 if (!(flags & STRUIO_POSTPONE)) {
2560 2610 /*
2561 2611 * Use regular canputnext, strmakedata, putnext sequence.
2562 2612 */
2563 2613 if (pri == 0) {
2564 2614 if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2565 2615 freemsg(mctl);
2566 2616 return (EWOULDBLOCK);
2567 2617 }
2568 2618 } else {
2569 2619 if (!(flags & MSG_IGNFLOW) && !bcanputnext(wqp, pri)) {
2570 2620 freemsg(mctl);
2571 2621 return (EWOULDBLOCK);
2572 2622 }
2573 2623 }
2574 2624
2575 2625 if ((error = strmakedata(iosize, uiop, stp, flags,
2576 2626 &mp)) != 0) {
2577 2627 freemsg(mctl);
2578 2628 /*
2579 2629 * need to change return code to ENOMEM
2580 2630 * so that this is not confused with
2581 2631 * flow control, EAGAIN.
2582 2632 */
2583 2633
2584 2634 if (error == EAGAIN)
2585 2635 return (ENOMEM);
2586 2636 else
2587 2637 return (error);
2588 2638 }
2589 2639 if (mctl != NULL) {
2590 2640 if (mctl->b_cont == NULL)
2591 2641 mctl->b_cont = mp;
2592 2642 else if (mp != NULL)
2593 2643 linkb(mctl, mp);
2594 2644 mp = mctl;
2595 2645 } else if (mp == NULL)
2596 2646 return (0);
2597 2647
2598 2648 mp->b_flag |= b_flag;
2599 2649 mp->b_band = (uchar_t)pri;
2600 2650
2601 2651 if (flags & MSG_IGNFLOW) {
2602 2652 /*
2603 2653 * XXX Hack: Don't get stuck running service
2604 2654 * procedures. This is needed for sockfs when
2605 2655 * sending the unbind message out of the rput
2606 2656 * procedure - we don't want a put procedure
2607 2657 * to run service procedures.
2608 2658 */
2609 2659 putnext(wqp, mp);
2610 2660 } else {
2611 2661 stream_willservice(stp);
2612 2662 putnext(wqp, mp);
2613 2663 stream_runservice(stp);
2614 2664 }
2615 2665 return (0);
2616 2666 }
2617 2667 /*
2618 2668 * Stream supports rwnext() for the write side.
2619 2669 */
2620 2670 if ((error = strmakedata(iosize, uiop, stp, flags, &mp)) != 0) {
2621 2671 freemsg(mctl);
2622 2672 /*
2623 2673 * map EAGAIN to ENOMEM since EAGAIN means "flow controlled".
2624 2674 */
2625 2675 return (error == EAGAIN ? ENOMEM : error);
2626 2676 }
2627 2677 if (mctl != NULL) {
2628 2678 if (mctl->b_cont == NULL)
2629 2679 mctl->b_cont = mp;
2630 2680 else if (mp != NULL)
2631 2681 linkb(mctl, mp);
2632 2682 mp = mctl;
2633 2683 } else if (mp == NULL) {
2634 2684 return (0);
2635 2685 }
2636 2686
2637 2687 mp->b_flag |= b_flag;
2638 2688 mp->b_band = (uchar_t)pri;
2639 2689
2640 2690 (void) uiodup(uiop, &uiod.d_uio, uiod.d_iov,
2641 2691 sizeof (uiod.d_iov) / sizeof (*uiod.d_iov));
2642 2692 uiod.d_uio.uio_offset = 0;
2643 2693 uiod.d_mp = mp;
2644 2694 error = rwnext(wqp, &uiod);
2645 2695 if (! uiod.d_mp) {
2646 2696 uioskip(uiop, *iosize);
2647 2697 return (error);
2648 2698 }
2649 2699 ASSERT(mp == uiod.d_mp);
2650 2700 if (error == EINVAL) {
2651 2701 /*
2652 2702 * The stream plumbing must have changed while
2653 2703 * we were away, so just turn off rwnext()s.
2654 2704 */
2655 2705 error = 0;
2656 2706 } else if (error == EBUSY || error == EWOULDBLOCK) {
2657 2707 /*
2658 2708 * Couldn't enter a perimeter or took a page fault,
2659 2709 * so fall-back to putnext().
2660 2710 */
2661 2711 error = 0;
2662 2712 } else {
2663 2713 freemsg(mp);
2664 2714 return (error);
2665 2715 }
2666 2716 /* Have to check canput before consuming data from the uio */
2667 2717 if (pri == 0) {
2668 2718 if (!canputnext(wqp) && !(flags & MSG_IGNFLOW)) {
2669 2719 freemsg(mp);
2670 2720 return (EWOULDBLOCK);
2671 2721 }
2672 2722 } else {
2673 2723 if (!bcanputnext(wqp, pri) && !(flags & MSG_IGNFLOW)) {
2674 2724 freemsg(mp);
2675 2725 return (EWOULDBLOCK);
2676 2726 }
2677 2727 }
2678 2728 ASSERT(mp == uiod.d_mp);
2679 2729 /* Copyin data from the uio */
2680 2730 if ((error = struioget(wqp, mp, &uiod, 0)) != 0) {
2681 2731 freemsg(mp);
2682 2732 return (error);
2683 2733 }
2684 2734 uioskip(uiop, *iosize);
2685 2735 if (flags & MSG_IGNFLOW) {
2686 2736 /*
2687 2737 * XXX Hack: Don't get stuck running service procedures.
2688 2738 * This is needed for sockfs when sending the unbind message
2689 2739 * out of the rput procedure - we don't want a put procedure
2690 2740 * to run service procedures.
2691 2741 */
2692 2742 putnext(wqp, mp);
2693 2743 } else {
2694 2744 stream_willservice(stp);
2695 2745 putnext(wqp, mp);
2696 2746 stream_runservice(stp);
2697 2747 }
2698 2748 return (0);
2699 2749 }
2700 2750
2701 2751 /*
2702 2752 * Write attempts to break the write request into messages conforming
2703 2753 * with the minimum and maximum packet sizes set downstream.
2704 2754 *
2705 2755 * Write will not block if downstream queue is full and
2706 2756 * O_NDELAY is set, otherwise it will block waiting for the queue to get room.
2707 2757 *
2708 2758 * A write of zero bytes gets packaged into a zero length message and sent
2709 2759 * downstream like any other message.
2710 2760 *
2711 2761 * If buffers of the requested sizes are not available, the write will
2712 2762 * sleep until the buffers become available.
2713 2763 *
2714 2764 * Write (if specified) will supply a write offset in a message if it
2715 2765 * makes sense. This can be specified by downstream modules as part of
2716 2766 * a M_SETOPTS message. Write will not supply the write offset if it
2717 2767 * cannot supply any data in a buffer. In other words, write will never
2718 2768 * send down an empty packet due to a write offset.
2719 2769 */
2720 2770 /* ARGSUSED2 */
2721 2771 int
2722 2772 strwrite(struct vnode *vp, struct uio *uiop, cred_t *crp)
2723 2773 {
2724 2774 return (strwrite_common(vp, uiop, crp, 0));
2725 2775 }
2726 2776
2727 2777 /* ARGSUSED2 */
2728 2778 int
2729 2779 strwrite_common(struct vnode *vp, struct uio *uiop, cred_t *crp, int wflag)
2730 2780 {
2731 2781 struct stdata *stp;
2732 2782 struct queue *wqp;
2733 2783 ssize_t rmin, rmax;
2734 2784 ssize_t iosize;
2735 2785 int waitflag;
2736 2786 int tempmode;
2737 2787 int error = 0;
2738 2788 int b_flag;
2739 2789
2740 2790 ASSERT(vp->v_stream);
2741 2791 stp = vp->v_stream;
2742 2792
2743 2793 mutex_enter(&stp->sd_lock);
2744 2794
2745 2795 if ((error = i_straccess(stp, JCWRITE)) != 0) {
2746 2796 mutex_exit(&stp->sd_lock);
2747 2797 return (error);
2748 2798 }
2749 2799
2750 2800 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
2751 2801 error = strwriteable(stp, B_TRUE, B_TRUE);
2752 2802 if (error != 0) {
2753 2803 mutex_exit(&stp->sd_lock);
2754 2804 return (error);
2755 2805 }
2756 2806 }
2757 2807
2758 2808 mutex_exit(&stp->sd_lock);
2759 2809
2760 2810 wqp = stp->sd_wrq;
2761 2811
2762 2812 /* get these values from them cached in the stream head */
2763 2813 rmin = stp->sd_qn_minpsz;
2764 2814 rmax = stp->sd_qn_maxpsz;
2765 2815
2766 2816 /*
2767 2817 * Check the min/max packet size constraints. If min packet size
2768 2818 * is non-zero, the write cannot be split into multiple messages
2769 2819 * and still guarantee the size constraints.
2770 2820 */
2771 2821 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_IN, "strwrite in:q %p", wqp);
2772 2822
2773 2823 ASSERT((rmax >= 0) || (rmax == INFPSZ));
2774 2824 if (rmax == 0) {
2775 2825 return (0);
2776 2826 }
2777 2827 if (rmin > 0) {
2778 2828 if (uiop->uio_resid < rmin) {
2779 2829 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2780 2830 "strwrite out:q %p out %d error %d",
2781 2831 wqp, 0, ERANGE);
2782 2832 return (ERANGE);
2783 2833 }
2784 2834 if ((rmax != INFPSZ) && (uiop->uio_resid > rmax)) {
2785 2835 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2786 2836 "strwrite out:q %p out %d error %d",
2787 2837 wqp, 1, ERANGE);
2788 2838 return (ERANGE);
2789 2839 }
2790 2840 }
2791 2841
2792 2842 /*
2793 2843 * Do until count satisfied or error.
2794 2844 */
2795 2845 waitflag = WRITEWAIT | wflag;
2796 2846 if (stp->sd_flag & OLDNDELAY)
2797 2847 tempmode = uiop->uio_fmode & ~FNDELAY;
2798 2848 else
2799 2849 tempmode = uiop->uio_fmode;
2800 2850
2801 2851 if (rmax == INFPSZ)
2802 2852 rmax = uiop->uio_resid;
2803 2853
2804 2854 /*
2805 2855 * Note that tempmode does not get used in strput/strmakedata
2806 2856 * but only in strwaitq. The other routines use uio_fmode
2807 2857 * unmodified.
2808 2858 */
2809 2859
2810 2860 /* LINTED: constant in conditional context */
2811 2861 while (1) { /* breaks when uio_resid reaches zero */
2812 2862 /*
2813 2863 * Determine the size of the next message to be
2814 2864 * packaged. May have to break write into several
2815 2865 * messages based on max packet size.
2816 2866 */
2817 2867 iosize = MIN(uiop->uio_resid, rmax);
2818 2868
2819 2869 /*
2820 2870 * Put block downstream when flow control allows it.
2821 2871 */
2822 2872 if ((stp->sd_flag & STRDELIM) && (uiop->uio_resid == iosize))
2823 2873 b_flag = MSGDELIM;
2824 2874 else
2825 2875 b_flag = 0;
2826 2876
2827 2877 for (;;) {
2828 2878 int done = 0;
2829 2879
2830 2880 error = strput(stp, NULL, uiop, &iosize, b_flag, 0, 0);
2831 2881 if (error == 0)
2832 2882 break;
2833 2883 if (error != EWOULDBLOCK)
2834 2884 goto out;
2835 2885
2836 2886 mutex_enter(&stp->sd_lock);
2837 2887 /*
2838 2888 * Check for a missed wakeup.
2839 2889 * Needed since strput did not hold sd_lock across
2840 2890 * the canputnext.
2841 2891 */
2842 2892 if (canputnext(wqp)) {
2843 2893 /* Try again */
2844 2894 mutex_exit(&stp->sd_lock);
2845 2895 continue;
2846 2896 }
2847 2897 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAIT,
2848 2898 "strwrite wait:q %p wait", wqp);
2849 2899 if ((error = strwaitq(stp, waitflag, (ssize_t)0,
2850 2900 tempmode, -1, &done)) != 0 || done) {
2851 2901 mutex_exit(&stp->sd_lock);
2852 2902 if ((vp->v_type == VFIFO) &&
2853 2903 (uiop->uio_fmode & FNDELAY) &&
2854 2904 (error == EAGAIN))
2855 2905 error = 0;
2856 2906 goto out;
2857 2907 }
2858 2908 TRACE_1(TR_FAC_STREAMS_FR, TR_STRWRITE_WAKE,
2859 2909 "strwrite wake:q %p awakes", wqp);
2860 2910 if ((error = i_straccess(stp, JCWRITE)) != 0) {
2861 2911 mutex_exit(&stp->sd_lock);
2862 2912 goto out;
2863 2913 }
2864 2914 mutex_exit(&stp->sd_lock);
2865 2915 }
2866 2916 waitflag |= NOINTR;
2867 2917 TRACE_2(TR_FAC_STREAMS_FR, TR_STRWRITE_RESID,
2868 2918 "strwrite resid:q %p uiop %p", wqp, uiop);
2869 2919 if (uiop->uio_resid) {
2870 2920 /* Recheck for errors - needed for sockets */
2871 2921 if ((stp->sd_wput_opt & SW_RECHECK_ERR) &&
2872 2922 (stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
2873 2923 mutex_enter(&stp->sd_lock);
2874 2924 error = strwriteable(stp, B_FALSE, B_TRUE);
2875 2925 mutex_exit(&stp->sd_lock);
2876 2926 if (error != 0)
2877 2927 return (error);
2878 2928 }
2879 2929 continue;
2880 2930 }
2881 2931 break;
2882 2932 }
2883 2933 out:
2884 2934 /*
2885 2935 * For historical reasons, applications expect EAGAIN when a data
2886 2936 * mblk_t cannot be allocated, so change ENOMEM back to EAGAIN.
2887 2937 */
2888 2938 if (error == ENOMEM)
2889 2939 error = EAGAIN;
2890 2940 TRACE_3(TR_FAC_STREAMS_FR, TR_STRWRITE_OUT,
2891 2941 "strwrite out:q %p out %d error %d", wqp, 2, error);
2892 2942 return (error);
2893 2943 }
2894 2944
2895 2945 /*
2896 2946 * Stream head write service routine.
2897 2947 * Its job is to wake up any sleeping writers when a queue
2898 2948 * downstream needs data (part of the flow control in putq and getq).
2899 2949 * It also must wake anyone sleeping on a poll().
2900 2950 * For stream head right below mux module, it must also invoke put procedure
2901 2951 * of next downstream module.
2902 2952 */
2903 2953 int
2904 2954 strwsrv(queue_t *q)
2905 2955 {
2906 2956 struct stdata *stp;
2907 2957 queue_t *tq;
2908 2958 qband_t *qbp;
2909 2959 int i;
2910 2960 qband_t *myqbp;
2911 2961 int isevent;
2912 2962 unsigned char qbf[NBAND]; /* band flushing backenable flags */
2913 2963
2914 2964 TRACE_1(TR_FAC_STREAMS_FR,
2915 2965 TR_STRWSRV, "strwsrv:q %p", q);
2916 2966 stp = (struct stdata *)q->q_ptr;
2917 2967 ASSERT(qclaimed(q));
2918 2968 mutex_enter(&stp->sd_lock);
2919 2969 ASSERT(!(stp->sd_flag & STPLEX));
2920 2970
2921 2971 if (stp->sd_flag & WSLEEP) {
2922 2972 stp->sd_flag &= ~WSLEEP;
2923 2973 cv_broadcast(&q->q_wait);
2924 2974 }
2925 2975 mutex_exit(&stp->sd_lock);
2926 2976
2927 2977 /* The other end of a stream pipe went away. */
2928 2978 if ((tq = q->q_next) == NULL) {
2929 2979 return (0);
2930 2980 }
2931 2981
2932 2982 /* Find the next module forward that has a service procedure */
2933 2983 claimstr(q);
2934 2984 tq = q->q_nfsrv;
2935 2985 ASSERT(tq != NULL);
2936 2986
2937 2987 if ((q->q_flag & QBACK)) {
2938 2988 if ((tq->q_flag & QFULL)) {
2939 2989 mutex_enter(QLOCK(tq));
2940 2990 if (!(tq->q_flag & QFULL)) {
2941 2991 mutex_exit(QLOCK(tq));
2942 2992 goto wakeup;
2943 2993 }
2944 2994 /*
2945 2995 * The queue must have become full again. Set QWANTW
2946 2996 * again so strwsrv will be back enabled when
2947 2997 * the queue becomes non-full next time.
2948 2998 */
2949 2999 tq->q_flag |= QWANTW;
2950 3000 mutex_exit(QLOCK(tq));
2951 3001 } else {
2952 3002 wakeup:
2953 3003 pollwakeup(&stp->sd_pollist, POLLWRNORM);
2954 3004 mutex_enter(&stp->sd_lock);
2955 3005 if (stp->sd_sigflags & S_WRNORM)
2956 3006 strsendsig(stp->sd_siglist, S_WRNORM, 0, 0);
2957 3007 mutex_exit(&stp->sd_lock);
2958 3008 }
2959 3009 }
2960 3010
2961 3011 isevent = 0;
2962 3012 i = 1;
2963 3013 bzero((caddr_t)qbf, NBAND);
2964 3014 mutex_enter(QLOCK(tq));
2965 3015 if ((myqbp = q->q_bandp) != NULL)
2966 3016 for (qbp = tq->q_bandp; qbp && myqbp; qbp = qbp->qb_next) {
2967 3017 ASSERT(myqbp);
2968 3018 if ((myqbp->qb_flag & QB_BACK)) {
2969 3019 if (qbp->qb_flag & QB_FULL) {
2970 3020 /*
2971 3021 * The band must have become full again.
2972 3022 * Set QB_WANTW again so strwsrv will
2973 3023 * be back enabled when the band becomes
2974 3024 * non-full next time.
2975 3025 */
2976 3026 qbp->qb_flag |= QB_WANTW;
2977 3027 } else {
2978 3028 isevent = 1;
2979 3029 qbf[i] = 1;
2980 3030 }
2981 3031 }
2982 3032 myqbp = myqbp->qb_next;
2983 3033 i++;
2984 3034 }
2985 3035 mutex_exit(QLOCK(tq));
2986 3036
2987 3037 if (isevent) {
2988 3038 for (i = tq->q_nband; i; i--) {
2989 3039 if (qbf[i]) {
2990 3040 pollwakeup(&stp->sd_pollist, POLLWRBAND);
2991 3041 mutex_enter(&stp->sd_lock);
2992 3042 if (stp->sd_sigflags & S_WRBAND)
2993 3043 strsendsig(stp->sd_siglist, S_WRBAND,
2994 3044 (uchar_t)i, 0);
2995 3045 mutex_exit(&stp->sd_lock);
2996 3046 }
2997 3047 }
2998 3048 }
2999 3049
3000 3050 releasestr(q);
3001 3051 return (0);
3002 3052 }
3003 3053
3004 3054 /*
3005 3055 * Special case of strcopyin/strcopyout for copying
3006 3056 * struct strioctl that can deal with both data
3007 3057 * models.
3008 3058 */
3009 3059
3010 3060 #ifdef _LP64
3011 3061
3012 3062 static int
3013 3063 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3014 3064 {
3015 3065 struct strioctl32 strioc32;
3016 3066 struct strioctl *striocp;
3017 3067
3018 3068 if (copyflag & U_TO_K) {
3019 3069 ASSERT((copyflag & K_TO_K) == 0);
3020 3070
3021 3071 if ((flag & FMODELS) == DATAMODEL_ILP32) {
3022 3072 if (copyin(from, &strioc32, sizeof (strioc32)))
3023 3073 return (EFAULT);
3024 3074
3025 3075 striocp = (struct strioctl *)to;
3026 3076 striocp->ic_cmd = strioc32.ic_cmd;
3027 3077 striocp->ic_timout = strioc32.ic_timout;
3028 3078 striocp->ic_len = strioc32.ic_len;
3029 3079 striocp->ic_dp = (char *)(uintptr_t)strioc32.ic_dp;
3030 3080
3031 3081 } else { /* NATIVE data model */
3032 3082 if (copyin(from, to, sizeof (struct strioctl))) {
3033 3083 return (EFAULT);
3034 3084 } else {
3035 3085 return (0);
3036 3086 }
3037 3087 }
3038 3088 } else {
3039 3089 ASSERT(copyflag & K_TO_K);
3040 3090 bcopy(from, to, sizeof (struct strioctl));
3041 3091 }
3042 3092 return (0);
3043 3093 }
3044 3094
3045 3095 static int
3046 3096 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3047 3097 {
3048 3098 struct strioctl32 strioc32;
3049 3099 struct strioctl *striocp;
3050 3100
3051 3101 if (copyflag & U_TO_K) {
3052 3102 ASSERT((copyflag & K_TO_K) == 0);
3053 3103
3054 3104 if ((flag & FMODELS) == DATAMODEL_ILP32) {
3055 3105 striocp = (struct strioctl *)from;
3056 3106 strioc32.ic_cmd = striocp->ic_cmd;
3057 3107 strioc32.ic_timout = striocp->ic_timout;
3058 3108 strioc32.ic_len = striocp->ic_len;
3059 3109 strioc32.ic_dp = (caddr32_t)(uintptr_t)striocp->ic_dp;
3060 3110 ASSERT((char *)(uintptr_t)strioc32.ic_dp ==
3061 3111 striocp->ic_dp);
3062 3112
3063 3113 if (copyout(&strioc32, to, sizeof (strioc32)))
3064 3114 return (EFAULT);
3065 3115
3066 3116 } else { /* NATIVE data model */
3067 3117 if (copyout(from, to, sizeof (struct strioctl))) {
3068 3118 return (EFAULT);
3069 3119 } else {
3070 3120 return (0);
3071 3121 }
3072 3122 }
3073 3123 } else {
3074 3124 ASSERT(copyflag & K_TO_K);
3075 3125 bcopy(from, to, sizeof (struct strioctl));
3076 3126 }
3077 3127 return (0);
3078 3128 }
3079 3129
3080 3130 #else /* ! _LP64 */
3081 3131
3082 3132 /* ARGSUSED2 */
3083 3133 static int
3084 3134 strcopyin_strioctl(void *from, void *to, int flag, int copyflag)
3085 3135 {
3086 3136 return (strcopyin(from, to, sizeof (struct strioctl), copyflag));
3087 3137 }
3088 3138
3089 3139 /* ARGSUSED2 */
3090 3140 static int
3091 3141 strcopyout_strioctl(void *from, void *to, int flag, int copyflag)
3092 3142 {
3093 3143 return (strcopyout(from, to, sizeof (struct strioctl), copyflag));
3094 3144 }
3095 3145
3096 3146 #endif /* _LP64 */
3097 3147
3098 3148 /*
3099 3149 * Determine type of job control semantics expected by user. The
3100 3150 * possibilities are:
3101 3151 * JCREAD - Behaves like read() on fd; send SIGTTIN
3102 3152 * JCWRITE - Behaves like write() on fd; send SIGTTOU if TOSTOP set
3103 3153 * JCSETP - Sets a value in the stream; send SIGTTOU, ignore TOSTOP
3104 3154 * JCGETP - Gets a value in the stream; no signals.
3105 3155 * See straccess in strsubr.c for usage of these values.
3106 3156 *
3107 3157 * This routine also returns -1 for I_STR as a special case; the
3108 3158 * caller must call again with the real ioctl number for
3109 3159 * classification.
3110 3160 */
3111 3161 static int
3112 3162 job_control_type(int cmd)
3113 3163 {
3114 3164 switch (cmd) {
3115 3165 case I_STR:
3116 3166 return (-1);
3117 3167
3118 3168 case I_RECVFD:
3119 3169 case I_E_RECVFD:
3120 3170 return (JCREAD);
3121 3171
3122 3172 case I_FDINSERT:
3123 3173 case I_SENDFD:
3124 3174 return (JCWRITE);
3125 3175
3126 3176 case TCSETA:
3127 3177 case TCSETAW:
3128 3178 case TCSETAF:
3129 3179 case TCSBRK:
3130 3180 case TCXONC:
3131 3181 case TCFLSH:
3132 3182 case TCDSET: /* Obsolete */
3133 3183 case TIOCSWINSZ:
3134 3184 case TCSETS:
3135 3185 case TCSETSW:
3136 3186 case TCSETSF:
3137 3187 case TIOCSETD:
3138 3188 case TIOCHPCL:
3139 3189 case TIOCSETP:
3140 3190 case TIOCSETN:
3141 3191 case TIOCEXCL:
3142 3192 case TIOCNXCL:
3143 3193 case TIOCFLUSH:
3144 3194 case TIOCSETC:
3145 3195 case TIOCLBIS:
3146 3196 case TIOCLBIC:
3147 3197 case TIOCLSET:
3148 3198 case TIOCSBRK:
3149 3199 case TIOCCBRK:
3150 3200 case TIOCSDTR:
3151 3201 case TIOCCDTR:
3152 3202 case TIOCSLTC:
3153 3203 case TIOCSTOP:
3154 3204 case TIOCSTART:
3155 3205 case TIOCSTI:
3156 3206 case TIOCSPGRP:
3157 3207 case TIOCMSET:
3158 3208 case TIOCMBIS:
3159 3209 case TIOCMBIC:
3160 3210 case TIOCREMOTE:
3161 3211 case TIOCSIGNAL:
3162 3212 case LDSETT:
3163 3213 case LDSMAP: /* Obsolete */
3164 3214 case DIOCSETP:
3165 3215 case I_FLUSH:
3166 3216 case I_SRDOPT:
3167 3217 case I_SETSIG:
3168 3218 case I_SWROPT:
3169 3219 case I_FLUSHBAND:
3170 3220 case I_SETCLTIME:
3171 3221 case I_SERROPT:
3172 3222 case I_ESETSIG:
3173 3223 case FIONBIO:
3174 3224 case FIOASYNC:
3175 3225 case FIOSETOWN:
3176 3226 case JBOOT: /* Obsolete */
3177 3227 case JTERM: /* Obsolete */
3178 3228 case JTIMOM: /* Obsolete */
3179 3229 case JZOMBOOT: /* Obsolete */
3180 3230 case JAGENT: /* Obsolete */
3181 3231 case JTRUN: /* Obsolete */
3182 3232 case JXTPROTO: /* Obsolete */
3183 3233 return (JCSETP);
3184 3234 }
3185 3235
3186 3236 return (JCGETP);
3187 3237 }
3188 3238
3189 3239 /*
3190 3240 * ioctl for streams
3191 3241 */
3192 3242 int
3193 3243 strioctl(struct vnode *vp, int cmd, intptr_t arg, int flag, int copyflag,
3194 3244 cred_t *crp, int *rvalp)
3195 3245 {
3196 3246 struct stdata *stp;
3197 3247 struct strcmd *scp;
3198 3248 struct strioctl strioc;
3199 3249 struct uio uio;
3200 3250 struct iovec iov;
3201 3251 int access;
3202 3252 mblk_t *mp;
3203 3253 int error = 0;
3204 3254 int done = 0;
3205 3255 ssize_t rmin, rmax;
3206 3256 queue_t *wrq;
3207 3257 queue_t *rdq;
3208 3258 boolean_t kioctl = B_FALSE;
3209 3259 uint32_t auditing = AU_AUDITING();
3210 3260
3211 3261 if (flag & FKIOCTL) {
3212 3262 copyflag = K_TO_K;
3213 3263 kioctl = B_TRUE;
3214 3264 }
3215 3265 ASSERT(vp->v_stream);
3216 3266 ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
3217 3267 stp = vp->v_stream;
3218 3268
3219 3269 TRACE_3(TR_FAC_STREAMS_FR, TR_IOCTL_ENTER,
3220 3270 "strioctl:stp %p cmd %X arg %lX", stp, cmd, arg);
3221 3271
3222 3272 /*
3223 3273 * If the copy is kernel to kernel, make sure that the FNATIVE
3224 3274 * flag is set. After this it would be a serious error to have
3225 3275 * no model flag.
3226 3276 */
3227 3277 if (copyflag == K_TO_K)
3228 3278 flag = (flag & ~FMODELS) | FNATIVE;
3229 3279
3230 3280 ASSERT((flag & FMODELS) != 0);
3231 3281
3232 3282 wrq = stp->sd_wrq;
3233 3283 rdq = _RD(wrq);
3234 3284
3235 3285 access = job_control_type(cmd);
3236 3286
3237 3287 /* We should never see these here, should be handled by iwscn */
3238 3288 if (cmd == SRIOCSREDIR || cmd == SRIOCISREDIR)
3239 3289 return (EINVAL);
3240 3290
3241 3291 mutex_enter(&stp->sd_lock);
3242 3292 if ((access != -1) && ((error = i_straccess(stp, access)) != 0)) {
3243 3293 mutex_exit(&stp->sd_lock);
3244 3294 return (error);
3245 3295 }
3246 3296 mutex_exit(&stp->sd_lock);
3247 3297
3248 3298 /*
3249 3299 * Check for sgttyb-related ioctls first, and complain as
3250 3300 * necessary.
3251 3301 */
3252 3302 switch (cmd) {
3253 3303 case TIOCGETP:
3254 3304 case TIOCSETP:
3255 3305 case TIOCSETN:
3256 3306 if (sgttyb_handling >= 2 && !sgttyb_complaint) {
3257 3307 sgttyb_complaint = B_TRUE;
3258 3308 cmn_err(CE_NOTE,
3259 3309 "application used obsolete TIOC[GS]ET");
3260 3310 }
3261 3311 if (sgttyb_handling >= 3) {
3262 3312 tsignal(curthread, SIGSYS);
3263 3313 return (EIO);
3264 3314 }
3265 3315 break;
3266 3316 }
3267 3317
3268 3318 mutex_enter(&stp->sd_lock);
3269 3319
3270 3320 switch (cmd) {
3271 3321 case I_RECVFD:
3272 3322 case I_E_RECVFD:
3273 3323 case I_PEEK:
3274 3324 case I_NREAD:
3275 3325 case FIONREAD:
3276 3326 case FIORDCHK:
3277 3327 case I_ATMARK:
3278 3328 case FIONBIO:
3279 3329 case FIOASYNC:
3280 3330 if (stp->sd_flag & (STRDERR|STPLEX)) {
3281 3331 error = strgeterr(stp, STRDERR|STPLEX, 0);
3282 3332 if (error != 0) {
3283 3333 mutex_exit(&stp->sd_lock);
3284 3334 return (error);
3285 3335 }
3286 3336 }
3287 3337 break;
3288 3338
3289 3339 default:
3290 3340 if (stp->sd_flag & (STRDERR|STWRERR|STPLEX)) {
3291 3341 error = strgeterr(stp, STRDERR|STWRERR|STPLEX, 0);
3292 3342 if (error != 0) {
3293 3343 mutex_exit(&stp->sd_lock);
3294 3344 return (error);
3295 3345 }
3296 3346 }
3297 3347 }
3298 3348
3299 3349 mutex_exit(&stp->sd_lock);
3300 3350
3301 3351 switch (cmd) {
3302 3352 default:
3303 3353 /*
3304 3354 * The stream head has hardcoded knowledge of a
3305 3355 * miscellaneous collection of terminal-, keyboard- and
3306 3356 * mouse-related ioctls, enumerated below. This hardcoded
3307 3357 * knowledge allows the stream head to automatically
3308 3358 * convert transparent ioctl requests made by userland
3309 3359 * programs into I_STR ioctls which many old STREAMS
3310 3360 * modules and drivers require.
3311 3361 *
3312 3362 * No new ioctls should ever be added to this list.
3313 3363 * Instead, the STREAMS module or driver should be written
3314 3364 * to either handle transparent ioctls or require any
3315 3365 * userland programs to use I_STR ioctls (by returning
3316 3366 * EINVAL to any transparent ioctl requests).
3317 3367 *
3318 3368 * More importantly, removing ioctls from this list should
3319 3369 * be done with the utmost care, since our STREAMS modules
3320 3370 * and drivers *count* on the stream head performing this
3321 3371 * conversion, and thus may panic while processing
3322 3372 * transparent ioctl request for one of these ioctls (keep
3323 3373 * in mind that third party modules and drivers may have
3324 3374 * similar problems).
3325 3375 */
3326 3376 if (((cmd & IOCTYPE) == LDIOC) ||
3327 3377 ((cmd & IOCTYPE) == tIOC) ||
3328 3378 ((cmd & IOCTYPE) == TIOC) ||
3329 3379 ((cmd & IOCTYPE) == KIOC) ||
3330 3380 ((cmd & IOCTYPE) == MSIOC) ||
3331 3381 ((cmd & IOCTYPE) == VUIOC)) {
3332 3382 /*
3333 3383 * The ioctl is a tty ioctl - set up strioc buffer
3334 3384 * and call strdoioctl() to do the work.
3335 3385 */
3336 3386 if (stp->sd_flag & STRHUP)
3337 3387 return (ENXIO);
3338 3388 strioc.ic_cmd = cmd;
3339 3389 strioc.ic_timout = INFTIM;
3340 3390
3341 3391 switch (cmd) {
3342 3392
3343 3393 case TCXONC:
3344 3394 case TCSBRK:
3345 3395 case TCFLSH:
3346 3396 case TCDSET:
3347 3397 {
3348 3398 int native_arg = (int)arg;
3349 3399 strioc.ic_len = sizeof (int);
3350 3400 strioc.ic_dp = (char *)&native_arg;
3351 3401 return (strdoioctl(stp, &strioc, flag,
3352 3402 K_TO_K, crp, rvalp));
3353 3403 }
3354 3404
3355 3405 case TCSETA:
3356 3406 case TCSETAW:
3357 3407 case TCSETAF:
3358 3408 strioc.ic_len = sizeof (struct termio);
3359 3409 strioc.ic_dp = (char *)arg;
3360 3410 return (strdoioctl(stp, &strioc, flag,
3361 3411 copyflag, crp, rvalp));
3362 3412
3363 3413 case TCSETS:
3364 3414 case TCSETSW:
3365 3415 case TCSETSF:
3366 3416 strioc.ic_len = sizeof (struct termios);
3367 3417 strioc.ic_dp = (char *)arg;
3368 3418 return (strdoioctl(stp, &strioc, flag,
3369 3419 copyflag, crp, rvalp));
3370 3420
3371 3421 case LDSETT:
3372 3422 strioc.ic_len = sizeof (struct termcb);
3373 3423 strioc.ic_dp = (char *)arg;
3374 3424 return (strdoioctl(stp, &strioc, flag,
3375 3425 copyflag, crp, rvalp));
3376 3426
3377 3427 case TIOCSETP:
3378 3428 strioc.ic_len = sizeof (struct sgttyb);
3379 3429 strioc.ic_dp = (char *)arg;
3380 3430 return (strdoioctl(stp, &strioc, flag,
3381 3431 copyflag, crp, rvalp));
3382 3432
3383 3433 case TIOCSTI:
3384 3434 if ((flag & FREAD) == 0 &&
3385 3435 secpolicy_sti(crp) != 0) {
3386 3436 return (EPERM);
3387 3437 }
3388 3438 mutex_enter(&stp->sd_lock);
3389 3439 mutex_enter(&curproc->p_splock);
3390 3440 if (stp->sd_sidp != curproc->p_sessp->s_sidp &&
3391 3441 secpolicy_sti(crp) != 0) {
3392 3442 mutex_exit(&curproc->p_splock);
3393 3443 mutex_exit(&stp->sd_lock);
3394 3444 return (EACCES);
3395 3445 }
3396 3446 mutex_exit(&curproc->p_splock);
3397 3447 mutex_exit(&stp->sd_lock);
3398 3448
3399 3449 strioc.ic_len = sizeof (char);
3400 3450 strioc.ic_dp = (char *)arg;
3401 3451 return (strdoioctl(stp, &strioc, flag,
3402 3452 copyflag, crp, rvalp));
3403 3453
3404 3454 case TIOCSWINSZ:
3405 3455 strioc.ic_len = sizeof (struct winsize);
3406 3456 strioc.ic_dp = (char *)arg;
3407 3457 return (strdoioctl(stp, &strioc, flag,
3408 3458 copyflag, crp, rvalp));
3409 3459
3410 3460 case TIOCSSIZE:
3411 3461 strioc.ic_len = sizeof (struct ttysize);
3412 3462 strioc.ic_dp = (char *)arg;
3413 3463 return (strdoioctl(stp, &strioc, flag,
3414 3464 copyflag, crp, rvalp));
3415 3465
3416 3466 case TIOCSSOFTCAR:
3417 3467 case KIOCTRANS:
3418 3468 case KIOCTRANSABLE:
3419 3469 case KIOCCMD:
3420 3470 case KIOCSDIRECT:
3421 3471 case KIOCSCOMPAT:
3422 3472 case KIOCSKABORTEN:
3423 3473 case KIOCSRPTCOUNT:
3424 3474 case KIOCSRPTDELAY:
3425 3475 case KIOCSRPTRATE:
3426 3476 case VUIDSFORMAT:
3427 3477 case TIOCSPPS:
3428 3478 strioc.ic_len = sizeof (int);
3429 3479 strioc.ic_dp = (char *)arg;
3430 3480 return (strdoioctl(stp, &strioc, flag,
3431 3481 copyflag, crp, rvalp));
3432 3482
3433 3483 case KIOCSETKEY:
3434 3484 case KIOCGETKEY:
3435 3485 strioc.ic_len = sizeof (struct kiockey);
3436 3486 strioc.ic_dp = (char *)arg;
3437 3487 return (strdoioctl(stp, &strioc, flag,
3438 3488 copyflag, crp, rvalp));
3439 3489
3440 3490 case KIOCSKEY:
3441 3491 case KIOCGKEY:
3442 3492 strioc.ic_len = sizeof (struct kiockeymap);
3443 3493 strioc.ic_dp = (char *)arg;
3444 3494 return (strdoioctl(stp, &strioc, flag,
3445 3495 copyflag, crp, rvalp));
3446 3496
3447 3497 case KIOCSLED:
3448 3498 /* arg is a pointer to char */
3449 3499 strioc.ic_len = sizeof (char);
3450 3500 strioc.ic_dp = (char *)arg;
3451 3501 return (strdoioctl(stp, &strioc, flag,
3452 3502 copyflag, crp, rvalp));
3453 3503
3454 3504 case MSIOSETPARMS:
3455 3505 strioc.ic_len = sizeof (Ms_parms);
3456 3506 strioc.ic_dp = (char *)arg;
3457 3507 return (strdoioctl(stp, &strioc, flag,
3458 3508 copyflag, crp, rvalp));
3459 3509
3460 3510 case VUIDSADDR:
3461 3511 case VUIDGADDR:
3462 3512 strioc.ic_len = sizeof (struct vuid_addr_probe);
3463 3513 strioc.ic_dp = (char *)arg;
3464 3514 return (strdoioctl(stp, &strioc, flag,
3465 3515 copyflag, crp, rvalp));
3466 3516
3467 3517 /*
3468 3518 * These M_IOCTL's don't require any data to be sent
3469 3519 * downstream, and the driver will allocate and link
3470 3520 * on its own mblk_t upon M_IOCACK -- thus we set
3471 3521 * ic_len to zero and set ic_dp to arg so we know
3472 3522 * where to copyout to later.
3473 3523 */
3474 3524 case TIOCGSOFTCAR:
3475 3525 case TIOCGWINSZ:
3476 3526 case TIOCGSIZE:
3477 3527 case KIOCGTRANS:
3478 3528 case KIOCGTRANSABLE:
3479 3529 case KIOCTYPE:
3480 3530 case KIOCGDIRECT:
3481 3531 case KIOCGCOMPAT:
3482 3532 case KIOCLAYOUT:
3483 3533 case KIOCGLED:
3484 3534 case MSIOGETPARMS:
3485 3535 case MSIOBUTTONS:
3486 3536 case VUIDGFORMAT:
3487 3537 case TIOCGPPS:
3488 3538 case TIOCGPPSEV:
3489 3539 case TCGETA:
3490 3540 case TCGETS:
3491 3541 case LDGETT:
3492 3542 case TIOCGETP:
3493 3543 case KIOCGRPTCOUNT:
3494 3544 case KIOCGRPTDELAY:
3495 3545 case KIOCGRPTRATE:
3496 3546 strioc.ic_len = 0;
3497 3547 strioc.ic_dp = (char *)arg;
3498 3548 return (strdoioctl(stp, &strioc, flag,
3499 3549 copyflag, crp, rvalp));
3500 3550 }
3501 3551 }
3502 3552
3503 3553 /*
3504 3554 * Unknown cmd - send it down as a transparent ioctl.
3505 3555 */
3506 3556 strioc.ic_cmd = cmd;
3507 3557 strioc.ic_timout = INFTIM;
3508 3558 strioc.ic_len = TRANSPARENT;
3509 3559 strioc.ic_dp = (char *)&arg;
3510 3560
3511 3561 return (strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp));
3512 3562
3513 3563 case I_STR:
3514 3564 /*
3515 3565 * Stream ioctl. Read in an strioctl buffer from the user
3516 3566 * along with any data specified and send it downstream.
3517 3567 * Strdoioctl will wait allow only one ioctl message at
3518 3568 * a time, and waits for the acknowledgement.
3519 3569 */
3520 3570
3521 3571 if (stp->sd_flag & STRHUP)
3522 3572 return (ENXIO);
3523 3573
3524 3574 error = strcopyin_strioctl((void *)arg, &strioc, flag,
3525 3575 copyflag);
3526 3576 if (error != 0)
3527 3577 return (error);
3528 3578
3529 3579 if ((strioc.ic_len < 0) || (strioc.ic_timout < -1))
3530 3580 return (EINVAL);
3531 3581
3532 3582 access = job_control_type(strioc.ic_cmd);
3533 3583 mutex_enter(&stp->sd_lock);
3534 3584 if ((access != -1) &&
3535 3585 ((error = i_straccess(stp, access)) != 0)) {
3536 3586 mutex_exit(&stp->sd_lock);
3537 3587 return (error);
3538 3588 }
3539 3589 mutex_exit(&stp->sd_lock);
3540 3590
3541 3591 /*
3542 3592 * The I_STR facility provides a trap door for malicious
3543 3593 * code to send down bogus streamio(7I) ioctl commands to
3544 3594 * unsuspecting STREAMS modules and drivers which expect to
3545 3595 * only get these messages from the stream head.
3546 3596 * Explicitly prohibit any streamio ioctls which can be
3547 3597 * passed downstream by the stream head. Note that we do
3548 3598 * not block all streamio ioctls because the ioctl
3549 3599 * numberspace is not well managed and thus it's possible
3550 3600 * that a module or driver's ioctl numbers may accidentally
3551 3601 * collide with them.
3552 3602 */
3553 3603 switch (strioc.ic_cmd) {
3554 3604 case I_LINK:
3555 3605 case I_PLINK:
3556 3606 case I_UNLINK:
3557 3607 case I_PUNLINK:
3558 3608 case _I_GETPEERCRED:
3559 3609 case _I_PLINK_LH:
3560 3610 return (EINVAL);
3561 3611 }
3562 3612
3563 3613 error = strdoioctl(stp, &strioc, flag, copyflag, crp, rvalp);
3564 3614 if (error == 0) {
3565 3615 error = strcopyout_strioctl(&strioc, (void *)arg,
3566 3616 flag, copyflag);
3567 3617 }
3568 3618 return (error);
3569 3619
3570 3620 case _I_CMD:
3571 3621 /*
3572 3622 * Like I_STR, but without using M_IOC* messages and without
3573 3623 * copyins/copyouts beyond the passed-in argument.
3574 3624 */
3575 3625 if (stp->sd_flag & STRHUP)
3576 3626 return (ENXIO);
3577 3627
3578 3628 if (copyflag == U_TO_K) {
3579 3629 if ((scp = kmem_alloc(sizeof (strcmd_t),
3580 3630 KM_NOSLEEP)) == NULL) {
3581 3631 return (ENOMEM);
3582 3632 }
3583 3633
3584 3634 if (copyin((void *)arg, scp, sizeof (strcmd_t))) {
3585 3635 kmem_free(scp, sizeof (strcmd_t));
3586 3636 return (EFAULT);
3587 3637 }
3588 3638 } else {
3589 3639 scp = (strcmd_t *)arg;
3590 3640 }
3591 3641
3592 3642 access = job_control_type(scp->sc_cmd);
3593 3643 mutex_enter(&stp->sd_lock);
3594 3644 if (access != -1 && (error = i_straccess(stp, access)) != 0) {
3595 3645 mutex_exit(&stp->sd_lock);
3596 3646 if (copyflag == U_TO_K)
3597 3647 kmem_free(scp, sizeof (strcmd_t));
3598 3648 return (error);
3599 3649 }
3600 3650 mutex_exit(&stp->sd_lock);
3601 3651
3602 3652 *rvalp = 0;
3603 3653 if ((error = strdocmd(stp, scp, crp)) == 0) {
3604 3654 if (copyflag == U_TO_K &&
3605 3655 copyout(scp, (void *)arg, sizeof (strcmd_t))) {
3606 3656 error = EFAULT;
3607 3657 }
3608 3658 }
3609 3659 if (copyflag == U_TO_K)
3610 3660 kmem_free(scp, sizeof (strcmd_t));
3611 3661 return (error);
3612 3662
3613 3663 case I_NREAD:
3614 3664 /*
3615 3665 * Return number of bytes of data in first message
3616 3666 * in queue in "arg" and return the number of messages
3617 3667 * in queue in return value.
3618 3668 */
3619 3669 {
3620 3670 size_t size;
3621 3671 int retval;
3622 3672 int count = 0;
3623 3673
3624 3674 mutex_enter(QLOCK(rdq));
3625 3675
3626 3676 size = msgdsize(rdq->q_first);
3627 3677 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3628 3678 count++;
3629 3679
3630 3680 mutex_exit(QLOCK(rdq));
3631 3681 if (stp->sd_struiordq) {
3632 3682 infod_t infod;
3633 3683
3634 3684 infod.d_cmd = INFOD_COUNT;
3635 3685 infod.d_count = 0;
3636 3686 if (count == 0) {
3637 3687 infod.d_cmd |= INFOD_FIRSTBYTES;
3638 3688 infod.d_bytes = 0;
3639 3689 }
3640 3690 infod.d_res = 0;
3641 3691 (void) infonext(rdq, &infod);
3642 3692 count += infod.d_count;
3643 3693 if (infod.d_res & INFOD_FIRSTBYTES)
3644 3694 size = infod.d_bytes;
3645 3695 }
3646 3696
3647 3697 /*
3648 3698 * Drop down from size_t to the "int" required by the
3649 3699 * interface. Cap at INT_MAX.
3650 3700 */
3651 3701 retval = MIN(size, INT_MAX);
3652 3702 error = strcopyout(&retval, (void *)arg, sizeof (retval),
3653 3703 copyflag);
3654 3704 if (!error)
3655 3705 *rvalp = count;
3656 3706 return (error);
3657 3707 }
3658 3708
3659 3709 case FIONREAD:
3660 3710 /*
3661 3711 * Return number of bytes of data in all data messages
3662 3712 * in queue in "arg".
3663 3713 */
3664 3714 {
3665 3715 size_t size = 0;
3666 3716 int retval;
3667 3717
3668 3718 mutex_enter(QLOCK(rdq));
3669 3719 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3670 3720 size += msgdsize(mp);
3671 3721 mutex_exit(QLOCK(rdq));
3672 3722
3673 3723 if (stp->sd_struiordq) {
3674 3724 infod_t infod;
3675 3725
3676 3726 infod.d_cmd = INFOD_BYTES;
3677 3727 infod.d_res = 0;
3678 3728 infod.d_bytes = 0;
3679 3729 (void) infonext(rdq, &infod);
3680 3730 size += infod.d_bytes;
3681 3731 }
3682 3732
3683 3733 /*
3684 3734 * Drop down from size_t to the "int" required by the
3685 3735 * interface. Cap at INT_MAX.
3686 3736 */
3687 3737 retval = MIN(size, INT_MAX);
3688 3738 error = strcopyout(&retval, (void *)arg, sizeof (retval),
3689 3739 copyflag);
3690 3740
3691 3741 *rvalp = 0;
3692 3742 return (error);
3693 3743 }
3694 3744 case FIORDCHK:
3695 3745 /*
3696 3746 * FIORDCHK does not use arg value (like FIONREAD),
3697 3747 * instead a count is returned. I_NREAD value may
3698 3748 * not be accurate but safe. The real thing to do is
3699 3749 * to add the msgdsizes of all data messages until
3700 3750 * a non-data message.
3701 3751 */
3702 3752 {
3703 3753 size_t size = 0;
3704 3754
3705 3755 mutex_enter(QLOCK(rdq));
3706 3756 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
3707 3757 size += msgdsize(mp);
3708 3758 mutex_exit(QLOCK(rdq));
3709 3759
3710 3760 if (stp->sd_struiordq) {
3711 3761 infod_t infod;
3712 3762
3713 3763 infod.d_cmd = INFOD_BYTES;
3714 3764 infod.d_res = 0;
3715 3765 infod.d_bytes = 0;
3716 3766 (void) infonext(rdq, &infod);
3717 3767 size += infod.d_bytes;
3718 3768 }
3719 3769
3720 3770 /*
3721 3771 * Since ioctl returns an int, and memory sizes under
3722 3772 * LP64 may not fit, we return INT_MAX if the count was
3723 3773 * actually greater.
3724 3774 */
3725 3775 *rvalp = MIN(size, INT_MAX);
3726 3776 return (0);
3727 3777 }
3728 3778
3729 3779 case I_FIND:
3730 3780 /*
3731 3781 * Get module name.
3732 3782 */
3733 3783 {
3734 3784 char mname[FMNAMESZ + 1];
3735 3785 queue_t *q;
3736 3786
3737 3787 error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3738 3788 mname, FMNAMESZ + 1, NULL);
3739 3789 if (error)
3740 3790 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3741 3791
3742 3792 /*
3743 3793 * Return EINVAL if we're handed a bogus module name.
3744 3794 */
3745 3795 if (fmodsw_find(mname, FMODSW_LOAD) == NULL) {
3746 3796 TRACE_0(TR_FAC_STREAMS_FR,
3747 3797 TR_I_CANT_FIND, "couldn't I_FIND");
3748 3798 return (EINVAL);
3749 3799 }
3750 3800
3751 3801 *rvalp = 0;
3752 3802
3753 3803 /* Look downstream to see if module is there. */
3754 3804 claimstr(stp->sd_wrq);
3755 3805 for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
3756 3806 if (q->q_flag & QREADR) {
3757 3807 q = NULL;
3758 3808 break;
3759 3809 }
3760 3810 if (strcmp(mname, Q2NAME(q)) == 0)
3761 3811 break;
3762 3812 }
3763 3813 releasestr(stp->sd_wrq);
3764 3814
3765 3815 *rvalp = (q ? 1 : 0);
3766 3816 return (error);
3767 3817 }
3768 3818
3769 3819 case I_PUSH:
3770 3820 case __I_PUSH_NOCTTY:
3771 3821 /*
3772 3822 * Push a module.
3773 3823 * For the case __I_PUSH_NOCTTY push a module but
3774 3824 * do not allocate controlling tty. See bugid 4025044
3775 3825 */
3776 3826
3777 3827 {
3778 3828 char mname[FMNAMESZ + 1];
3779 3829 fmodsw_impl_t *fp;
3780 3830 dev_t dummydev;
3781 3831
3782 3832 if (stp->sd_flag & STRHUP)
3783 3833 return (ENXIO);
3784 3834
3785 3835 /*
3786 3836 * Get module name and look up in fmodsw.
3787 3837 */
3788 3838 error = (copyflag & U_TO_K ? copyinstr : copystr)((void *)arg,
3789 3839 mname, FMNAMESZ + 1, NULL);
3790 3840 if (error)
3791 3841 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
3792 3842
3793 3843 if ((fp = fmodsw_find(mname, FMODSW_HOLD | FMODSW_LOAD)) ==
3794 3844 NULL)
3795 3845 return (EINVAL);
3796 3846
3797 3847 TRACE_2(TR_FAC_STREAMS_FR, TR_I_PUSH,
3798 3848 "I_PUSH:fp %p stp %p", fp, stp);
3799 3849
3800 3850 /*
3801 3851 * If the module is flagged as single-instance, then check
3802 3852 * to see if the module is already pushed. If it is, return
3803 3853 * as if the push was successful.
3804 3854 */
3805 3855 if (fp->f_qflag & _QSINGLE_INSTANCE) {
3806 3856 queue_t *q;
3807 3857
3808 3858 claimstr(stp->sd_wrq);
3809 3859 for (q = stp->sd_wrq->q_next; q; q = q->q_next) {
3810 3860 if (q->q_flag & QREADR) {
3811 3861 q = NULL;
3812 3862 break;
3813 3863 }
3814 3864 if (strcmp(mname, Q2NAME(q)) == 0)
3815 3865 break;
3816 3866 }
3817 3867 releasestr(stp->sd_wrq);
3818 3868 if (q != NULL) {
3819 3869 fmodsw_rele(fp);
3820 3870 return (0);
3821 3871 }
3822 3872 }
3823 3873
3824 3874 if (error = strstartplumb(stp, flag, cmd)) {
3825 3875 fmodsw_rele(fp);
3826 3876 return (error);
3827 3877 }
3828 3878
3829 3879 /*
3830 3880 * See if any more modules can be pushed on this stream.
3831 3881 * Note that this check must be done after strstartplumb()
3832 3882 * since otherwise multiple threads issuing I_PUSHes on
3833 3883 * the same stream will be able to exceed nstrpush.
3834 3884 */
3835 3885 mutex_enter(&stp->sd_lock);
3836 3886 if (stp->sd_pushcnt >= nstrpush) {
3837 3887 fmodsw_rele(fp);
3838 3888 strendplumb(stp);
3839 3889 mutex_exit(&stp->sd_lock);
3840 3890 return (EINVAL);
3841 3891 }
3842 3892 mutex_exit(&stp->sd_lock);
3843 3893
3844 3894 /*
3845 3895 * Push new module and call its open routine
3846 3896 * via qattach(). Modules don't change device
3847 3897 * numbers, so just ignore dummydev here.
3848 3898 */
3849 3899 dummydev = vp->v_rdev;
3850 3900 if ((error = qattach(rdq, &dummydev, 0, crp, fp,
3851 3901 B_FALSE)) == 0) {
3852 3902 if (vp->v_type == VCHR && /* sorry, no pipes allowed */
3853 3903 (cmd == I_PUSH) && (stp->sd_flag & STRISTTY)) {
3854 3904 /*
3855 3905 * try to allocate it as a controlling terminal
3856 3906 */
3857 3907 (void) strctty(stp);
3858 3908 }
3859 3909 }
3860 3910
3861 3911 mutex_enter(&stp->sd_lock);
3862 3912
3863 3913 /*
3864 3914 * As a performance concern we are caching the values of
3865 3915 * q_minpsz and q_maxpsz of the module below the stream
3866 3916 * head in the stream head.
3867 3917 */
3868 3918 mutex_enter(QLOCK(stp->sd_wrq->q_next));
3869 3919 rmin = stp->sd_wrq->q_next->q_minpsz;
3870 3920 rmax = stp->sd_wrq->q_next->q_maxpsz;
3871 3921 mutex_exit(QLOCK(stp->sd_wrq->q_next));
3872 3922
3873 3923 /* Do this processing here as a performance concern */
3874 3924 if (strmsgsz != 0) {
3875 3925 if (rmax == INFPSZ)
3876 3926 rmax = strmsgsz;
3877 3927 else {
3878 3928 if (vp->v_type == VFIFO)
3879 3929 rmax = MIN(PIPE_BUF, rmax);
3880 3930 else rmax = MIN(strmsgsz, rmax);
3881 3931 }
3882 3932 }
3883 3933
3884 3934 mutex_enter(QLOCK(wrq));
3885 3935 stp->sd_qn_minpsz = rmin;
3886 3936 stp->sd_qn_maxpsz = rmax;
3887 3937 mutex_exit(QLOCK(wrq));
3888 3938
3889 3939 strendplumb(stp);
3890 3940 mutex_exit(&stp->sd_lock);
3891 3941 return (error);
3892 3942 }
3893 3943
3894 3944 case I_POP:
3895 3945 {
3896 3946 queue_t *q;
3897 3947
3898 3948 if (stp->sd_flag & STRHUP)
3899 3949 return (ENXIO);
3900 3950 if (!wrq->q_next) /* for broken pipes */
3901 3951 return (EINVAL);
3902 3952
3903 3953 if (error = strstartplumb(stp, flag, cmd))
3904 3954 return (error);
3905 3955
3906 3956 /*
3907 3957 * If there is an anchor on this stream and popping
3908 3958 * the current module would attempt to pop through the
3909 3959 * anchor, then disallow the pop unless we have sufficient
3910 3960 * privileges; take the cheapest (non-locking) check
3911 3961 * first.
3912 3962 */
3913 3963 if (secpolicy_ip_config(crp, B_TRUE) != 0 ||
3914 3964 (stp->sd_anchorzone != crgetzoneid(crp))) {
3915 3965 mutex_enter(&stp->sd_lock);
3916 3966 /*
3917 3967 * Anchors only apply if there's at least one
3918 3968 * module on the stream (sd_pushcnt > 0).
3919 3969 */
3920 3970 if (stp->sd_pushcnt > 0 &&
3921 3971 stp->sd_pushcnt == stp->sd_anchor &&
3922 3972 stp->sd_vnode->v_type != VFIFO) {
3923 3973 strendplumb(stp);
3924 3974 mutex_exit(&stp->sd_lock);
3925 3975 if (stp->sd_anchorzone != crgetzoneid(crp))
3926 3976 return (EINVAL);
3927 3977 /* Audit and report error */
3928 3978 return (secpolicy_ip_config(crp, B_FALSE));
3929 3979 }
3930 3980 mutex_exit(&stp->sd_lock);
3931 3981 }
3932 3982
3933 3983 q = wrq->q_next;
3934 3984 TRACE_2(TR_FAC_STREAMS_FR, TR_I_POP,
3935 3985 "I_POP:%p from %p", q, stp);
3936 3986 if (q->q_next == NULL || (q->q_flag & (QREADR|QISDRV))) {
3937 3987 error = EINVAL;
3938 3988 } else {
3939 3989 qdetach(_RD(q), 1, flag, crp, B_FALSE);
3940 3990 error = 0;
3941 3991 }
3942 3992 mutex_enter(&stp->sd_lock);
3943 3993
3944 3994 /*
3945 3995 * As a performance concern we are caching the values of
3946 3996 * q_minpsz and q_maxpsz of the module below the stream
3947 3997 * head in the stream head.
3948 3998 */
3949 3999 mutex_enter(QLOCK(wrq->q_next));
3950 4000 rmin = wrq->q_next->q_minpsz;
3951 4001 rmax = wrq->q_next->q_maxpsz;
3952 4002 mutex_exit(QLOCK(wrq->q_next));
3953 4003
3954 4004 /* Do this processing here as a performance concern */
3955 4005 if (strmsgsz != 0) {
3956 4006 if (rmax == INFPSZ)
3957 4007 rmax = strmsgsz;
3958 4008 else {
3959 4009 if (vp->v_type == VFIFO)
3960 4010 rmax = MIN(PIPE_BUF, rmax);
3961 4011 else rmax = MIN(strmsgsz, rmax);
3962 4012 }
3963 4013 }
3964 4014
3965 4015 mutex_enter(QLOCK(wrq));
3966 4016 stp->sd_qn_minpsz = rmin;
3967 4017 stp->sd_qn_maxpsz = rmax;
3968 4018 mutex_exit(QLOCK(wrq));
3969 4019
3970 4020 /* If we popped through the anchor, then reset the anchor. */
3971 4021 if (stp->sd_pushcnt < stp->sd_anchor) {
3972 4022 stp->sd_anchor = 0;
3973 4023 stp->sd_anchorzone = 0;
3974 4024 }
3975 4025 strendplumb(stp);
3976 4026 mutex_exit(&stp->sd_lock);
3977 4027 return (error);
3978 4028 }
3979 4029
3980 4030 case _I_MUXID2FD:
3981 4031 {
3982 4032 /*
3983 4033 * Create a fd for a I_PLINK'ed lower stream with a given
3984 4034 * muxid. With the fd, application can send down ioctls,
3985 4035 * like I_LIST, to the previously I_PLINK'ed stream. Note
3986 4036 * that after getting the fd, the application has to do an
3987 4037 * I_PUNLINK on the muxid before it can do any operation
3988 4038 * on the lower stream. This is required by spec1170.
3989 4039 *
3990 4040 * The fd used to do this ioctl should point to the same
3991 4041 * controlling device used to do the I_PLINK. If it uses
3992 4042 * a different stream or an invalid muxid, I_MUXID2FD will
3993 4043 * fail. The error code is set to EINVAL.
3994 4044 *
3995 4045 * The intended use of this interface is the following.
3996 4046 * An application I_PLINK'ed a stream and exits. The fd
3997 4047 * to the lower stream is gone. Another application
3998 4048 * wants to get a fd to the lower stream, it uses I_MUXID2FD.
3999 4049 */
4000 4050 int muxid = (int)arg;
4001 4051 int fd;
4002 4052 linkinfo_t *linkp;
4003 4053 struct file *fp;
4004 4054 netstack_t *ns;
4005 4055 str_stack_t *ss;
4006 4056
4007 4057 /*
4008 4058 * Do not allow the wildcard muxid. This ioctl is not
4009 4059 * intended to find arbitrary link.
4010 4060 */
4011 4061 if (muxid == 0) {
4012 4062 return (EINVAL);
4013 4063 }
4014 4064
4015 4065 ns = netstack_find_by_cred(crp);
4016 4066 ASSERT(ns != NULL);
4017 4067 ss = ns->netstack_str;
4018 4068 ASSERT(ss != NULL);
4019 4069
4020 4070 mutex_enter(&muxifier);
4021 4071 linkp = findlinks(vp->v_stream, muxid, LINKPERSIST, ss);
4022 4072 if (linkp == NULL) {
4023 4073 mutex_exit(&muxifier);
4024 4074 netstack_rele(ss->ss_netstack);
4025 4075 return (EINVAL);
4026 4076 }
4027 4077
4028 4078 if ((fd = ufalloc(0)) == -1) {
4029 4079 mutex_exit(&muxifier);
4030 4080 netstack_rele(ss->ss_netstack);
4031 4081 return (EMFILE);
4032 4082 }
4033 4083 fp = linkp->li_fpdown;
4034 4084 mutex_enter(&fp->f_tlock);
4035 4085 fp->f_count++;
4036 4086 mutex_exit(&fp->f_tlock);
4037 4087 mutex_exit(&muxifier);
4038 4088 setf(fd, fp);
4039 4089 *rvalp = fd;
4040 4090 netstack_rele(ss->ss_netstack);
4041 4091 return (0);
4042 4092 }
4043 4093
4044 4094 case _I_INSERT:
4045 4095 {
4046 4096 /*
4047 4097 * To insert a module to a given position in a stream.
4048 4098 * In the first release, only allow privileged user
4049 4099 * to use this ioctl. Furthermore, the insert is only allowed
4050 4100 * below an anchor if the zoneid is the same as the zoneid
4051 4101 * which created the anchor.
4052 4102 *
4053 4103 * Note that we do not plan to support this ioctl
4054 4104 * on pipes in the first release. We want to learn more
4055 4105 * about the implications of these ioctls before extending
4056 4106 * their support. And we do not think these features are
4057 4107 * valuable for pipes.
4058 4108 */
4059 4109 STRUCT_DECL(strmodconf, strmodinsert);
4060 4110 char mod_name[FMNAMESZ + 1];
4061 4111 fmodsw_impl_t *fp;
4062 4112 dev_t dummydev;
4063 4113 queue_t *tmp_wrq;
4064 4114 int pos;
4065 4115 boolean_t is_insert;
4066 4116
4067 4117 STRUCT_INIT(strmodinsert, flag);
4068 4118 if (stp->sd_flag & STRHUP)
4069 4119 return (ENXIO);
4070 4120 if (STRMATED(stp))
4071 4121 return (EINVAL);
4072 4122 if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4073 4123 return (error);
4074 4124 if (stp->sd_anchor != 0 &&
4075 4125 stp->sd_anchorzone != crgetzoneid(crp))
4076 4126 return (EINVAL);
4077 4127
4078 4128 error = strcopyin((void *)arg, STRUCT_BUF(strmodinsert),
4079 4129 STRUCT_SIZE(strmodinsert), copyflag);
4080 4130 if (error)
4081 4131 return (error);
4082 4132
4083 4133 /*
4084 4134 * Get module name and look up in fmodsw.
4085 4135 */
4086 4136 error = (copyflag & U_TO_K ? copyinstr :
4087 4137 copystr)(STRUCT_FGETP(strmodinsert, mod_name),
4088 4138 mod_name, FMNAMESZ + 1, NULL);
4089 4139 if (error)
4090 4140 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4091 4141
4092 4142 if ((fp = fmodsw_find(mod_name, FMODSW_HOLD | FMODSW_LOAD)) ==
4093 4143 NULL)
4094 4144 return (EINVAL);
4095 4145
4096 4146 if (error = strstartplumb(stp, flag, cmd)) {
4097 4147 fmodsw_rele(fp);
4098 4148 return (error);
4099 4149 }
4100 4150
4101 4151 /*
4102 4152 * Is this _I_INSERT just like an I_PUSH? We need to know
4103 4153 * this because we do some optimizations if this is a
4104 4154 * module being pushed.
4105 4155 */
4106 4156 pos = STRUCT_FGET(strmodinsert, pos);
4107 4157 is_insert = (pos != 0);
4108 4158
4109 4159 /*
4110 4160 * Make sure pos is valid. Even though it is not an I_PUSH,
4111 4161 * we impose the same limit on the number of modules in a
4112 4162 * stream.
4113 4163 */
4114 4164 mutex_enter(&stp->sd_lock);
4115 4165 if (stp->sd_pushcnt >= nstrpush || pos < 0 ||
4116 4166 pos > stp->sd_pushcnt) {
4117 4167 fmodsw_rele(fp);
4118 4168 strendplumb(stp);
4119 4169 mutex_exit(&stp->sd_lock);
4120 4170 return (EINVAL);
4121 4171 }
4122 4172 if (stp->sd_anchor != 0) {
4123 4173 /*
4124 4174 * Is this insert below the anchor?
4125 4175 * Pushcnt hasn't been increased yet hence
4126 4176 * we test for greater than here, and greater or
4127 4177 * equal after qattach.
4128 4178 */
4129 4179 if (pos > (stp->sd_pushcnt - stp->sd_anchor) &&
4130 4180 stp->sd_anchorzone != crgetzoneid(crp)) {
4131 4181 fmodsw_rele(fp);
4132 4182 strendplumb(stp);
4133 4183 mutex_exit(&stp->sd_lock);
4134 4184 return (EPERM);
4135 4185 }
4136 4186 }
4137 4187
4138 4188 mutex_exit(&stp->sd_lock);
4139 4189
4140 4190 /*
4141 4191 * First find the correct position this module to
4142 4192 * be inserted. We don't need to call claimstr()
4143 4193 * as the stream should not be changing at this point.
4144 4194 *
4145 4195 * Insert new module and call its open routine
4146 4196 * via qattach(). Modules don't change device
4147 4197 * numbers, so just ignore dummydev here.
4148 4198 */
4149 4199 for (tmp_wrq = stp->sd_wrq; pos > 0;
4150 4200 tmp_wrq = tmp_wrq->q_next, pos--) {
4151 4201 ASSERT(SAMESTR(tmp_wrq));
4152 4202 }
4153 4203 dummydev = vp->v_rdev;
4154 4204 if ((error = qattach(_RD(tmp_wrq), &dummydev, 0, crp,
4155 4205 fp, is_insert)) != 0) {
4156 4206 mutex_enter(&stp->sd_lock);
4157 4207 strendplumb(stp);
4158 4208 mutex_exit(&stp->sd_lock);
4159 4209 return (error);
4160 4210 }
4161 4211
4162 4212 mutex_enter(&stp->sd_lock);
4163 4213
4164 4214 /*
4165 4215 * As a performance concern we are caching the values of
4166 4216 * q_minpsz and q_maxpsz of the module below the stream
4167 4217 * head in the stream head.
4168 4218 */
4169 4219 if (!is_insert) {
4170 4220 mutex_enter(QLOCK(stp->sd_wrq->q_next));
4171 4221 rmin = stp->sd_wrq->q_next->q_minpsz;
4172 4222 rmax = stp->sd_wrq->q_next->q_maxpsz;
4173 4223 mutex_exit(QLOCK(stp->sd_wrq->q_next));
4174 4224
4175 4225 /* Do this processing here as a performance concern */
4176 4226 if (strmsgsz != 0) {
4177 4227 if (rmax == INFPSZ) {
4178 4228 rmax = strmsgsz;
4179 4229 } else {
4180 4230 rmax = MIN(strmsgsz, rmax);
4181 4231 }
4182 4232 }
4183 4233
4184 4234 mutex_enter(QLOCK(wrq));
4185 4235 stp->sd_qn_minpsz = rmin;
4186 4236 stp->sd_qn_maxpsz = rmax;
4187 4237 mutex_exit(QLOCK(wrq));
4188 4238 }
4189 4239
4190 4240 /*
4191 4241 * Need to update the anchor value if this module is
4192 4242 * inserted below the anchor point.
4193 4243 */
4194 4244 if (stp->sd_anchor != 0) {
4195 4245 pos = STRUCT_FGET(strmodinsert, pos);
4196 4246 if (pos >= (stp->sd_pushcnt - stp->sd_anchor))
4197 4247 stp->sd_anchor++;
4198 4248 }
4199 4249
4200 4250 strendplumb(stp);
4201 4251 mutex_exit(&stp->sd_lock);
4202 4252 return (0);
4203 4253 }
4204 4254
4205 4255 case _I_REMOVE:
4206 4256 {
4207 4257 /*
4208 4258 * To remove a module with a given name in a stream. The
4209 4259 * caller of this ioctl needs to provide both the name and
4210 4260 * the position of the module to be removed. This eliminates
4211 4261 * the ambiguity of removal if a module is inserted/pushed
4212 4262 * multiple times in a stream. In the first release, only
4213 4263 * allow privileged user to use this ioctl.
4214 4264 * Furthermore, the remove is only allowed
4215 4265 * below an anchor if the zoneid is the same as the zoneid
4216 4266 * which created the anchor.
4217 4267 *
4218 4268 * Note that we do not plan to support this ioctl
4219 4269 * on pipes in the first release. We want to learn more
4220 4270 * about the implications of these ioctls before extending
4221 4271 * their support. And we do not think these features are
4222 4272 * valuable for pipes.
4223 4273 *
4224 4274 * Also note that _I_REMOVE cannot be used to remove a
4225 4275 * driver or the stream head.
4226 4276 */
4227 4277 STRUCT_DECL(strmodconf, strmodremove);
4228 4278 queue_t *q;
4229 4279 int pos;
4230 4280 char mod_name[FMNAMESZ + 1];
4231 4281 boolean_t is_remove;
4232 4282
4233 4283 STRUCT_INIT(strmodremove, flag);
4234 4284 if (stp->sd_flag & STRHUP)
4235 4285 return (ENXIO);
4236 4286 if (STRMATED(stp))
4237 4287 return (EINVAL);
4238 4288 if ((error = secpolicy_net_config(crp, B_FALSE)) != 0)
4239 4289 return (error);
4240 4290 if (stp->sd_anchor != 0 &&
4241 4291 stp->sd_anchorzone != crgetzoneid(crp))
4242 4292 return (EINVAL);
4243 4293
4244 4294 error = strcopyin((void *)arg, STRUCT_BUF(strmodremove),
4245 4295 STRUCT_SIZE(strmodremove), copyflag);
4246 4296 if (error)
4247 4297 return (error);
4248 4298
4249 4299 error = (copyflag & U_TO_K ? copyinstr :
4250 4300 copystr)(STRUCT_FGETP(strmodremove, mod_name),
4251 4301 mod_name, FMNAMESZ + 1, NULL);
4252 4302 if (error)
4253 4303 return ((error == ENAMETOOLONG) ? EINVAL : EFAULT);
4254 4304
4255 4305 if ((error = strstartplumb(stp, flag, cmd)) != 0)
4256 4306 return (error);
4257 4307
4258 4308 /*
4259 4309 * Match the name of given module to the name of module at
4260 4310 * the given position.
4261 4311 */
4262 4312 pos = STRUCT_FGET(strmodremove, pos);
4263 4313
4264 4314 is_remove = (pos != 0);
4265 4315 for (q = stp->sd_wrq->q_next; SAMESTR(q) && pos > 0;
4266 4316 q = q->q_next, pos--)
4267 4317 ;
4268 4318 if (pos > 0 || !SAMESTR(q) ||
4269 4319 strcmp(Q2NAME(q), mod_name) != 0) {
4270 4320 mutex_enter(&stp->sd_lock);
4271 4321 strendplumb(stp);
4272 4322 mutex_exit(&stp->sd_lock);
4273 4323 return (EINVAL);
4274 4324 }
4275 4325
4276 4326 /*
4277 4327 * If the position is at or below an anchor, then the zoneid
4278 4328 * must match the zoneid that created the anchor.
4279 4329 */
4280 4330 if (stp->sd_anchor != 0) {
4281 4331 pos = STRUCT_FGET(strmodremove, pos);
4282 4332 if (pos >= (stp->sd_pushcnt - stp->sd_anchor) &&
4283 4333 stp->sd_anchorzone != crgetzoneid(crp)) {
4284 4334 mutex_enter(&stp->sd_lock);
4285 4335 strendplumb(stp);
4286 4336 mutex_exit(&stp->sd_lock);
4287 4337 return (EPERM);
4288 4338 }
4289 4339 }
4290 4340
4291 4341
4292 4342 ASSERT(!(q->q_flag & QREADR));
4293 4343 qdetach(_RD(q), 1, flag, crp, is_remove);
4294 4344
4295 4345 mutex_enter(&stp->sd_lock);
4296 4346
4297 4347 /*
4298 4348 * As a performance concern we are caching the values of
4299 4349 * q_minpsz and q_maxpsz of the module below the stream
4300 4350 * head in the stream head.
4301 4351 */
4302 4352 if (!is_remove) {
4303 4353 mutex_enter(QLOCK(wrq->q_next));
4304 4354 rmin = wrq->q_next->q_minpsz;
4305 4355 rmax = wrq->q_next->q_maxpsz;
4306 4356 mutex_exit(QLOCK(wrq->q_next));
4307 4357
4308 4358 /* Do this processing here as a performance concern */
4309 4359 if (strmsgsz != 0) {
4310 4360 if (rmax == INFPSZ)
4311 4361 rmax = strmsgsz;
4312 4362 else {
4313 4363 if (vp->v_type == VFIFO)
4314 4364 rmax = MIN(PIPE_BUF, rmax);
4315 4365 else rmax = MIN(strmsgsz, rmax);
4316 4366 }
4317 4367 }
4318 4368
4319 4369 mutex_enter(QLOCK(wrq));
4320 4370 stp->sd_qn_minpsz = rmin;
4321 4371 stp->sd_qn_maxpsz = rmax;
4322 4372 mutex_exit(QLOCK(wrq));
4323 4373 }
4324 4374
4325 4375 /*
4326 4376 * Need to update the anchor value if this module is removed
4327 4377 * at or below the anchor point. If the removed module is at
4328 4378 * the anchor point, remove the anchor for this stream if
4329 4379 * there is no module above the anchor point. Otherwise, if
4330 4380 * the removed module is below the anchor point, decrement the
4331 4381 * anchor point by 1.
4332 4382 */
4333 4383 if (stp->sd_anchor != 0) {
4334 4384 pos = STRUCT_FGET(strmodremove, pos);
4335 4385 if (pos == stp->sd_pushcnt - stp->sd_anchor + 1)
4336 4386 stp->sd_anchor = 0;
4337 4387 else if (pos > (stp->sd_pushcnt - stp->sd_anchor + 1))
4338 4388 stp->sd_anchor--;
4339 4389 }
4340 4390
4341 4391 strendplumb(stp);
4342 4392 mutex_exit(&stp->sd_lock);
4343 4393 return (0);
4344 4394 }
4345 4395
4346 4396 case I_ANCHOR:
4347 4397 /*
4348 4398 * Set the anchor position on the stream to reside at
4349 4399 * the top module (in other words, the top module
4350 4400 * cannot be popped). Anchors with a FIFO make no
4351 4401 * obvious sense, so they're not allowed.
4352 4402 */
4353 4403 mutex_enter(&stp->sd_lock);
4354 4404
4355 4405 if (stp->sd_vnode->v_type == VFIFO) {
4356 4406 mutex_exit(&stp->sd_lock);
4357 4407 return (EINVAL);
4358 4408 }
4359 4409 /* Only allow the same zoneid to update the anchor */
4360 4410 if (stp->sd_anchor != 0 &&
4361 4411 stp->sd_anchorzone != crgetzoneid(crp)) {
4362 4412 mutex_exit(&stp->sd_lock);
4363 4413 return (EINVAL);
4364 4414 }
4365 4415 stp->sd_anchor = stp->sd_pushcnt;
4366 4416 stp->sd_anchorzone = crgetzoneid(crp);
4367 4417 mutex_exit(&stp->sd_lock);
4368 4418 return (0);
4369 4419
4370 4420 case I_LOOK:
4371 4421 /*
4372 4422 * Get name of first module downstream.
4373 4423 * If no module, return an error.
4374 4424 */
4375 4425 claimstr(wrq);
4376 4426 if (_SAMESTR(wrq) && wrq->q_next->q_next != NULL) {
4377 4427 char *name = Q2NAME(wrq->q_next);
4378 4428
4379 4429 error = strcopyout(name, (void *)arg, strlen(name) + 1,
4380 4430 copyflag);
4381 4431 releasestr(wrq);
4382 4432 return (error);
4383 4433 }
4384 4434 releasestr(wrq);
4385 4435 return (EINVAL);
4386 4436
4387 4437 case I_LINK:
4388 4438 case I_PLINK:
4389 4439 /*
4390 4440 * Link a multiplexor.
4391 4441 */
4392 4442 return (mlink(vp, cmd, (int)arg, crp, rvalp, 0));
4393 4443
4394 4444 case _I_PLINK_LH:
4395 4445 /*
4396 4446 * Link a multiplexor: Call must originate from kernel.
4397 4447 */
4398 4448 if (kioctl)
4399 4449 return (ldi_mlink_lh(vp, cmd, arg, crp, rvalp));
4400 4450
4401 4451 return (EINVAL);
4402 4452 case I_UNLINK:
4403 4453 case I_PUNLINK:
4404 4454 /*
4405 4455 * Unlink a multiplexor.
4406 4456 * If arg is -1, unlink all links for which this is the
4407 4457 * controlling stream. Otherwise, arg is an index number
4408 4458 * for a link to be removed.
4409 4459 */
4410 4460 {
4411 4461 struct linkinfo *linkp;
4412 4462 int native_arg = (int)arg;
4413 4463 int type;
4414 4464 netstack_t *ns;
4415 4465 str_stack_t *ss;
4416 4466
4417 4467 TRACE_1(TR_FAC_STREAMS_FR,
4418 4468 TR_I_UNLINK, "I_UNLINK/I_PUNLINK:%p", stp);
4419 4469 if (vp->v_type == VFIFO) {
4420 4470 return (EINVAL);
4421 4471 }
4422 4472 if (cmd == I_UNLINK)
4423 4473 type = LINKNORMAL;
4424 4474 else /* I_PUNLINK */
4425 4475 type = LINKPERSIST;
4426 4476 if (native_arg == 0) {
4427 4477 return (EINVAL);
4428 4478 }
4429 4479 ns = netstack_find_by_cred(crp);
4430 4480 ASSERT(ns != NULL);
4431 4481 ss = ns->netstack_str;
4432 4482 ASSERT(ss != NULL);
4433 4483
4434 4484 if (native_arg == MUXID_ALL)
4435 4485 error = munlinkall(stp, type, crp, rvalp, ss);
4436 4486 else {
4437 4487 mutex_enter(&muxifier);
4438 4488 if (!(linkp = findlinks(stp, (int)arg, type, ss))) {
4439 4489 /* invalid user supplied index number */
4440 4490 mutex_exit(&muxifier);
4441 4491 netstack_rele(ss->ss_netstack);
4442 4492 return (EINVAL);
4443 4493 }
4444 4494 /* munlink drops the muxifier lock */
4445 4495 error = munlink(stp, linkp, type, crp, rvalp, ss);
4446 4496 }
4447 4497 netstack_rele(ss->ss_netstack);
4448 4498 return (error);
4449 4499 }
4450 4500
4451 4501 case I_FLUSH:
4452 4502 /*
4453 4503 * send a flush message downstream
4454 4504 * flush message can indicate
4455 4505 * FLUSHR - flush read queue
4456 4506 * FLUSHW - flush write queue
4457 4507 * FLUSHRW - flush read/write queue
4458 4508 */
4459 4509 if (stp->sd_flag & STRHUP)
4460 4510 return (ENXIO);
4461 4511 if (arg & ~FLUSHRW)
4462 4512 return (EINVAL);
4463 4513
4464 4514 for (;;) {
4465 4515 if (putnextctl1(stp->sd_wrq, M_FLUSH, (int)arg)) {
4466 4516 break;
4467 4517 }
4468 4518 if (error = strwaitbuf(1, BPRI_HI)) {
4469 4519 return (error);
4470 4520 }
4471 4521 }
4472 4522
4473 4523 /*
4474 4524 * Send down an unsupported ioctl and wait for the nack
4475 4525 * in order to allow the M_FLUSH to propagate back
4476 4526 * up to the stream head.
4477 4527 * Replaces if (qready()) runqueues();
4478 4528 */
4479 4529 strioc.ic_cmd = -1; /* The unsupported ioctl */
4480 4530 strioc.ic_timout = 0;
4481 4531 strioc.ic_len = 0;
4482 4532 strioc.ic_dp = NULL;
4483 4533 (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4484 4534 *rvalp = 0;
4485 4535 return (0);
4486 4536
4487 4537 case I_FLUSHBAND:
4488 4538 {
4489 4539 struct bandinfo binfo;
4490 4540
4491 4541 error = strcopyin((void *)arg, &binfo, sizeof (binfo),
4492 4542 copyflag);
4493 4543 if (error)
4494 4544 return (error);
4495 4545 if (stp->sd_flag & STRHUP)
4496 4546 return (ENXIO);
4497 4547 if (binfo.bi_flag & ~FLUSHRW)
4498 4548 return (EINVAL);
4499 4549 while (!(mp = allocb(2, BPRI_HI))) {
4500 4550 if (error = strwaitbuf(2, BPRI_HI))
4501 4551 return (error);
4502 4552 }
4503 4553 mp->b_datap->db_type = M_FLUSH;
4504 4554 *mp->b_wptr++ = binfo.bi_flag | FLUSHBAND;
4505 4555 *mp->b_wptr++ = binfo.bi_pri;
4506 4556 putnext(stp->sd_wrq, mp);
4507 4557 /*
4508 4558 * Send down an unsupported ioctl and wait for the nack
4509 4559 * in order to allow the M_FLUSH to propagate back
4510 4560 * up to the stream head.
4511 4561 * Replaces if (qready()) runqueues();
4512 4562 */
4513 4563 strioc.ic_cmd = -1; /* The unsupported ioctl */
4514 4564 strioc.ic_timout = 0;
4515 4565 strioc.ic_len = 0;
4516 4566 strioc.ic_dp = NULL;
4517 4567 (void) strdoioctl(stp, &strioc, flag, K_TO_K, crp, rvalp);
4518 4568 *rvalp = 0;
4519 4569 return (0);
4520 4570 }
4521 4571
4522 4572 case I_SRDOPT:
4523 4573 /*
4524 4574 * Set read options
4525 4575 *
4526 4576 * RNORM - default stream mode
4527 4577 * RMSGN - message no discard
4528 4578 * RMSGD - message discard
4529 4579 * RPROTNORM - fail read with EBADMSG for M_[PC]PROTOs
4530 4580 * RPROTDAT - convert M_[PC]PROTOs to M_DATAs
4531 4581 * RPROTDIS - discard M_[PC]PROTOs and retain M_DATAs
4532 4582 */
4533 4583 if (arg & ~(RMODEMASK | RPROTMASK))
4534 4584 return (EINVAL);
4535 4585
4536 4586 if ((arg & (RMSGD|RMSGN)) == (RMSGD|RMSGN))
4537 4587 return (EINVAL);
4538 4588
4539 4589 mutex_enter(&stp->sd_lock);
4540 4590 switch (arg & RMODEMASK) {
4541 4591 case RNORM:
4542 4592 stp->sd_read_opt &= ~(RD_MSGDIS | RD_MSGNODIS);
4543 4593 break;
4544 4594 case RMSGD:
4545 4595 stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGNODIS) |
4546 4596 RD_MSGDIS;
4547 4597 break;
4548 4598 case RMSGN:
4549 4599 stp->sd_read_opt = (stp->sd_read_opt & ~RD_MSGDIS) |
4550 4600 RD_MSGNODIS;
4551 4601 break;
4552 4602 }
4553 4603
4554 4604 switch (arg & RPROTMASK) {
4555 4605 case RPROTNORM:
4556 4606 stp->sd_read_opt &= ~(RD_PROTDAT | RD_PROTDIS);
4557 4607 break;
4558 4608
4559 4609 case RPROTDAT:
4560 4610 stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDIS) |
4561 4611 RD_PROTDAT);
4562 4612 break;
4563 4613
4564 4614 case RPROTDIS:
4565 4615 stp->sd_read_opt = ((stp->sd_read_opt & ~RD_PROTDAT) |
4566 4616 RD_PROTDIS);
4567 4617 break;
4568 4618 }
4569 4619 mutex_exit(&stp->sd_lock);
4570 4620 return (0);
4571 4621
4572 4622 case I_GRDOPT:
4573 4623 /*
4574 4624 * Get read option and return the value
4575 4625 * to spot pointed to by arg
4576 4626 */
4577 4627 {
4578 4628 int rdopt;
4579 4629
4580 4630 rdopt = ((stp->sd_read_opt & RD_MSGDIS) ? RMSGD :
4581 4631 ((stp->sd_read_opt & RD_MSGNODIS) ? RMSGN : RNORM));
4582 4632 rdopt |= ((stp->sd_read_opt & RD_PROTDAT) ? RPROTDAT :
4583 4633 ((stp->sd_read_opt & RD_PROTDIS) ? RPROTDIS : RPROTNORM));
4584 4634
4585 4635 return (strcopyout(&rdopt, (void *)arg, sizeof (int),
4586 4636 copyflag));
4587 4637 }
4588 4638
4589 4639 case I_SERROPT:
4590 4640 /*
4591 4641 * Set error options
4592 4642 *
4593 4643 * RERRNORM - persistent read errors
4594 4644 * RERRNONPERSIST - non-persistent read errors
4595 4645 * WERRNORM - persistent write errors
4596 4646 * WERRNONPERSIST - non-persistent write errors
4597 4647 */
4598 4648 if (arg & ~(RERRMASK | WERRMASK))
4599 4649 return (EINVAL);
4600 4650
4601 4651 mutex_enter(&stp->sd_lock);
4602 4652 switch (arg & RERRMASK) {
4603 4653 case RERRNORM:
4604 4654 stp->sd_flag &= ~STRDERRNONPERSIST;
4605 4655 break;
4606 4656 case RERRNONPERSIST:
4607 4657 stp->sd_flag |= STRDERRNONPERSIST;
4608 4658 break;
4609 4659 }
4610 4660 switch (arg & WERRMASK) {
4611 4661 case WERRNORM:
4612 4662 stp->sd_flag &= ~STWRERRNONPERSIST;
4613 4663 break;
4614 4664 case WERRNONPERSIST:
4615 4665 stp->sd_flag |= STWRERRNONPERSIST;
4616 4666 break;
4617 4667 }
4618 4668 mutex_exit(&stp->sd_lock);
4619 4669 return (0);
4620 4670
4621 4671 case I_GERROPT:
4622 4672 /*
4623 4673 * Get error option and return the value
4624 4674 * to spot pointed to by arg
4625 4675 */
4626 4676 {
4627 4677 int erropt = 0;
4628 4678
4629 4679 erropt |= (stp->sd_flag & STRDERRNONPERSIST) ? RERRNONPERSIST :
4630 4680 RERRNORM;
4631 4681 erropt |= (stp->sd_flag & STWRERRNONPERSIST) ? WERRNONPERSIST :
4632 4682 WERRNORM;
4633 4683 return (strcopyout(&erropt, (void *)arg, sizeof (int),
4634 4684 copyflag));
4635 4685 }
4636 4686
4637 4687 case I_SETSIG:
4638 4688 /*
4639 4689 * Register the calling proc to receive the SIGPOLL
4640 4690 * signal based on the events given in arg. If
4641 4691 * arg is zero, remove the proc from register list.
4642 4692 */
4643 4693 {
4644 4694 strsig_t *ssp, *pssp;
4645 4695 struct pid *pidp;
4646 4696
4647 4697 pssp = NULL;
4648 4698 pidp = curproc->p_pidp;
4649 4699 /*
4650 4700 * Hold sd_lock to prevent traversal of sd_siglist while
4651 4701 * it is modified.
4652 4702 */
4653 4703 mutex_enter(&stp->sd_lock);
4654 4704 for (ssp = stp->sd_siglist; ssp && (ssp->ss_pidp != pidp);
4655 4705 pssp = ssp, ssp = ssp->ss_next)
4656 4706 ;
4657 4707
4658 4708 if (arg) {
4659 4709 if (arg & ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4660 4710 S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4661 4711 mutex_exit(&stp->sd_lock);
4662 4712 return (EINVAL);
4663 4713 }
4664 4714 if ((arg & S_BANDURG) && !(arg & S_RDBAND)) {
4665 4715 mutex_exit(&stp->sd_lock);
4666 4716 return (EINVAL);
4667 4717 }
4668 4718
4669 4719 /*
4670 4720 * If proc not already registered, add it
4671 4721 * to list.
4672 4722 */
4673 4723 if (!ssp) {
4674 4724 ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4675 4725 ssp->ss_pidp = pidp;
4676 4726 ssp->ss_pid = pidp->pid_id;
4677 4727 ssp->ss_next = NULL;
4678 4728 if (pssp)
4679 4729 pssp->ss_next = ssp;
4680 4730 else
4681 4731 stp->sd_siglist = ssp;
4682 4732 mutex_enter(&pidlock);
4683 4733 PID_HOLD(pidp);
4684 4734 mutex_exit(&pidlock);
4685 4735 }
4686 4736
4687 4737 /*
4688 4738 * Set events.
4689 4739 */
4690 4740 ssp->ss_events = (int)arg;
4691 4741 } else {
4692 4742 /*
4693 4743 * Remove proc from register list.
4694 4744 */
4695 4745 if (ssp) {
4696 4746 mutex_enter(&pidlock);
4697 4747 PID_RELE(pidp);
4698 4748 mutex_exit(&pidlock);
4699 4749 if (pssp)
4700 4750 pssp->ss_next = ssp->ss_next;
4701 4751 else
4702 4752 stp->sd_siglist = ssp->ss_next;
4703 4753 kmem_free(ssp, sizeof (strsig_t));
4704 4754 } else {
4705 4755 mutex_exit(&stp->sd_lock);
4706 4756 return (EINVAL);
4707 4757 }
4708 4758 }
4709 4759
4710 4760 /*
4711 4761 * Recalculate OR of sig events.
4712 4762 */
4713 4763 stp->sd_sigflags = 0;
4714 4764 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4715 4765 stp->sd_sigflags |= ssp->ss_events;
4716 4766 mutex_exit(&stp->sd_lock);
4717 4767 return (0);
4718 4768 }
4719 4769
4720 4770 case I_GETSIG:
4721 4771 /*
4722 4772 * Return (in arg) the current registration of events
4723 4773 * for which the calling proc is to be signaled.
4724 4774 */
4725 4775 {
4726 4776 struct strsig *ssp;
4727 4777 struct pid *pidp;
4728 4778
4729 4779 pidp = curproc->p_pidp;
4730 4780 mutex_enter(&stp->sd_lock);
4731 4781 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4732 4782 if (ssp->ss_pidp == pidp) {
4733 4783 error = strcopyout(&ssp->ss_events, (void *)arg,
4734 4784 sizeof (int), copyflag);
4735 4785 mutex_exit(&stp->sd_lock);
4736 4786 return (error);
4737 4787 }
4738 4788 mutex_exit(&stp->sd_lock);
4739 4789 return (EINVAL);
4740 4790 }
4741 4791
4742 4792 case I_ESETSIG:
4743 4793 /*
4744 4794 * Register the ss_pid to receive the SIGPOLL
4745 4795 * signal based on the events is ss_events arg. If
4746 4796 * ss_events is zero, remove the proc from register list.
4747 4797 */
4748 4798 {
4749 4799 struct strsig *ssp, *pssp;
4750 4800 struct proc *proc;
4751 4801 struct pid *pidp;
4752 4802 pid_t pid;
4753 4803 struct strsigset ss;
4754 4804
4755 4805 error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4756 4806 if (error)
4757 4807 return (error);
4758 4808
4759 4809 pid = ss.ss_pid;
4760 4810
4761 4811 if (ss.ss_events != 0) {
4762 4812 /*
4763 4813 * Permissions check by sending signal 0.
4764 4814 * Note that when kill fails it does a set_errno
4765 4815 * causing the system call to fail.
4766 4816 */
4767 4817 error = kill(pid, 0);
4768 4818 if (error) {
4769 4819 return (error);
4770 4820 }
4771 4821 }
4772 4822 mutex_enter(&pidlock);
4773 4823 if (pid == 0)
4774 4824 proc = curproc;
4775 4825 else if (pid < 0)
4776 4826 proc = pgfind(-pid);
4777 4827 else
4778 4828 proc = prfind(pid);
4779 4829 if (proc == NULL) {
4780 4830 mutex_exit(&pidlock);
4781 4831 return (ESRCH);
4782 4832 }
4783 4833 if (pid < 0)
4784 4834 pidp = proc->p_pgidp;
4785 4835 else
4786 4836 pidp = proc->p_pidp;
4787 4837 ASSERT(pidp);
4788 4838 /*
4789 4839 * Get a hold on the pid structure while referencing it.
4790 4840 * There is a separate PID_HOLD should it be inserted
4791 4841 * in the list below.
4792 4842 */
4793 4843 PID_HOLD(pidp);
4794 4844 mutex_exit(&pidlock);
4795 4845
4796 4846 pssp = NULL;
4797 4847 /*
4798 4848 * Hold sd_lock to prevent traversal of sd_siglist while
4799 4849 * it is modified.
4800 4850 */
4801 4851 mutex_enter(&stp->sd_lock);
4802 4852 for (ssp = stp->sd_siglist; ssp && (ssp->ss_pid != pid);
4803 4853 pssp = ssp, ssp = ssp->ss_next)
4804 4854 ;
4805 4855
4806 4856 if (ss.ss_events) {
4807 4857 if (ss.ss_events &
4808 4858 ~(S_INPUT|S_HIPRI|S_MSG|S_HANGUP|S_ERROR|
4809 4859 S_RDNORM|S_WRNORM|S_RDBAND|S_WRBAND|S_BANDURG)) {
4810 4860 mutex_exit(&stp->sd_lock);
4811 4861 mutex_enter(&pidlock);
4812 4862 PID_RELE(pidp);
4813 4863 mutex_exit(&pidlock);
4814 4864 return (EINVAL);
4815 4865 }
4816 4866 if ((ss.ss_events & S_BANDURG) &&
4817 4867 !(ss.ss_events & S_RDBAND)) {
4818 4868 mutex_exit(&stp->sd_lock);
4819 4869 mutex_enter(&pidlock);
4820 4870 PID_RELE(pidp);
4821 4871 mutex_exit(&pidlock);
4822 4872 return (EINVAL);
4823 4873 }
4824 4874
4825 4875 /*
4826 4876 * If proc not already registered, add it
4827 4877 * to list.
4828 4878 */
4829 4879 if (!ssp) {
4830 4880 ssp = kmem_alloc(sizeof (strsig_t), KM_SLEEP);
4831 4881 ssp->ss_pidp = pidp;
4832 4882 ssp->ss_pid = pid;
4833 4883 ssp->ss_next = NULL;
4834 4884 if (pssp)
4835 4885 pssp->ss_next = ssp;
4836 4886 else
4837 4887 stp->sd_siglist = ssp;
4838 4888 mutex_enter(&pidlock);
4839 4889 PID_HOLD(pidp);
4840 4890 mutex_exit(&pidlock);
4841 4891 }
4842 4892
4843 4893 /*
4844 4894 * Set events.
4845 4895 */
4846 4896 ssp->ss_events = ss.ss_events;
4847 4897 } else {
4848 4898 /*
4849 4899 * Remove proc from register list.
4850 4900 */
4851 4901 if (ssp) {
4852 4902 mutex_enter(&pidlock);
4853 4903 PID_RELE(pidp);
4854 4904 mutex_exit(&pidlock);
4855 4905 if (pssp)
4856 4906 pssp->ss_next = ssp->ss_next;
4857 4907 else
4858 4908 stp->sd_siglist = ssp->ss_next;
4859 4909 kmem_free(ssp, sizeof (strsig_t));
4860 4910 } else {
4861 4911 mutex_exit(&stp->sd_lock);
4862 4912 mutex_enter(&pidlock);
4863 4913 PID_RELE(pidp);
4864 4914 mutex_exit(&pidlock);
4865 4915 return (EINVAL);
4866 4916 }
4867 4917 }
4868 4918
4869 4919 /*
4870 4920 * Recalculate OR of sig events.
4871 4921 */
4872 4922 stp->sd_sigflags = 0;
4873 4923 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4874 4924 stp->sd_sigflags |= ssp->ss_events;
4875 4925 mutex_exit(&stp->sd_lock);
4876 4926 mutex_enter(&pidlock);
4877 4927 PID_RELE(pidp);
4878 4928 mutex_exit(&pidlock);
4879 4929 return (0);
4880 4930 }
4881 4931
4882 4932 case I_EGETSIG:
4883 4933 /*
4884 4934 * Return (in arg) the current registration of events
4885 4935 * for which the calling proc is to be signaled.
4886 4936 */
4887 4937 {
4888 4938 struct strsig *ssp;
4889 4939 struct proc *proc;
4890 4940 pid_t pid;
4891 4941 struct pid *pidp;
4892 4942 struct strsigset ss;
4893 4943
4894 4944 error = strcopyin((void *)arg, &ss, sizeof (ss), copyflag);
4895 4945 if (error)
4896 4946 return (error);
4897 4947
4898 4948 pid = ss.ss_pid;
4899 4949 mutex_enter(&pidlock);
4900 4950 if (pid == 0)
4901 4951 proc = curproc;
4902 4952 else if (pid < 0)
4903 4953 proc = pgfind(-pid);
4904 4954 else
4905 4955 proc = prfind(pid);
4906 4956 if (proc == NULL) {
4907 4957 mutex_exit(&pidlock);
4908 4958 return (ESRCH);
4909 4959 }
4910 4960 if (pid < 0)
4911 4961 pidp = proc->p_pgidp;
4912 4962 else
4913 4963 pidp = proc->p_pidp;
4914 4964
4915 4965 /* Prevent the pidp from being reassigned */
4916 4966 PID_HOLD(pidp);
4917 4967 mutex_exit(&pidlock);
4918 4968
4919 4969 mutex_enter(&stp->sd_lock);
4920 4970 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
4921 4971 if (ssp->ss_pid == pid) {
4922 4972 ss.ss_pid = ssp->ss_pid;
4923 4973 ss.ss_events = ssp->ss_events;
4924 4974 error = strcopyout(&ss, (void *)arg,
4925 4975 sizeof (struct strsigset), copyflag);
4926 4976 mutex_exit(&stp->sd_lock);
4927 4977 mutex_enter(&pidlock);
4928 4978 PID_RELE(pidp);
4929 4979 mutex_exit(&pidlock);
4930 4980 return (error);
4931 4981 }
4932 4982 mutex_exit(&stp->sd_lock);
4933 4983 mutex_enter(&pidlock);
4934 4984 PID_RELE(pidp);
4935 4985 mutex_exit(&pidlock);
4936 4986 return (EINVAL);
4937 4987 }
4938 4988
4939 4989 case I_PEEK:
4940 4990 {
4941 4991 STRUCT_DECL(strpeek, strpeek);
4942 4992 size_t n;
4943 4993 mblk_t *fmp, *tmp_mp = NULL;
4944 4994
4945 4995 STRUCT_INIT(strpeek, flag);
4946 4996
4947 4997 error = strcopyin((void *)arg, STRUCT_BUF(strpeek),
4948 4998 STRUCT_SIZE(strpeek), copyflag);
4949 4999 if (error)
4950 5000 return (error);
4951 5001
4952 5002 mutex_enter(QLOCK(rdq));
4953 5003 /*
4954 5004 * Skip the invalid messages
4955 5005 */
4956 5006 for (mp = rdq->q_first; mp != NULL; mp = mp->b_next)
4957 5007 if (mp->b_datap->db_type != M_SIG)
4958 5008 break;
4959 5009
4960 5010 /*
4961 5011 * If user has requested to peek at a high priority message
4962 5012 * and first message is not, return 0
4963 5013 */
4964 5014 if (mp != NULL) {
4965 5015 if ((STRUCT_FGET(strpeek, flags) & RS_HIPRI) &&
4966 5016 queclass(mp) == QNORM) {
4967 5017 *rvalp = 0;
4968 5018 mutex_exit(QLOCK(rdq));
4969 5019 return (0);
4970 5020 }
4971 5021 } else if (stp->sd_struiordq == NULL ||
4972 5022 (STRUCT_FGET(strpeek, flags) & RS_HIPRI)) {
4973 5023 /*
4974 5024 * No mblks to look at at the streamhead and
4975 5025 * 1). This isn't a synch stream or
4976 5026 * 2). This is a synch stream but caller wants high
4977 5027 * priority messages which is not supported by
4978 5028 * the synch stream. (it only supports QNORM)
4979 5029 */
4980 5030 *rvalp = 0;
4981 5031 mutex_exit(QLOCK(rdq));
4982 5032 return (0);
4983 5033 }
4984 5034
4985 5035 fmp = mp;
4986 5036
4987 5037 if (mp && mp->b_datap->db_type == M_PASSFP) {
4988 5038 mutex_exit(QLOCK(rdq));
4989 5039 return (EBADMSG);
4990 5040 }
4991 5041
4992 5042 ASSERT(mp == NULL || mp->b_datap->db_type == M_PCPROTO ||
4993 5043 mp->b_datap->db_type == M_PROTO ||
4994 5044 mp->b_datap->db_type == M_DATA);
4995 5045
4996 5046 if (mp && mp->b_datap->db_type == M_PCPROTO) {
4997 5047 STRUCT_FSET(strpeek, flags, RS_HIPRI);
4998 5048 } else {
4999 5049 STRUCT_FSET(strpeek, flags, 0);
5000 5050 }
5001 5051
5002 5052
5003 5053 if (mp && ((tmp_mp = dupmsg(mp)) == NULL)) {
5004 5054 mutex_exit(QLOCK(rdq));
5005 5055 return (ENOSR);
5006 5056 }
5007 5057 mutex_exit(QLOCK(rdq));
5008 5058
5009 5059 /*
5010 5060 * set mp = tmp_mp, so that I_PEEK processing can continue.
5011 5061 * tmp_mp is used to free the dup'd message.
5012 5062 */
5013 5063 mp = tmp_mp;
5014 5064
5015 5065 uio.uio_fmode = 0;
5016 5066 uio.uio_extflg = UIO_COPY_CACHED;
5017 5067 uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5018 5068 UIO_SYSSPACE;
5019 5069 uio.uio_limit = 0;
5020 5070 /*
5021 5071 * First process PROTO blocks, if any.
5022 5072 * If user doesn't want to get ctl info by setting maxlen <= 0,
5023 5073 * then set len to -1/0 and skip control blocks part.
5024 5074 */
5025 5075 if (STRUCT_FGET(strpeek, ctlbuf.maxlen) < 0)
5026 5076 STRUCT_FSET(strpeek, ctlbuf.len, -1);
5027 5077 else if (STRUCT_FGET(strpeek, ctlbuf.maxlen) == 0)
5028 5078 STRUCT_FSET(strpeek, ctlbuf.len, 0);
5029 5079 else {
5030 5080 int ctl_part = 0;
5031 5081
5032 5082 iov.iov_base = STRUCT_FGETP(strpeek, ctlbuf.buf);
5033 5083 iov.iov_len = STRUCT_FGET(strpeek, ctlbuf.maxlen);
5034 5084 uio.uio_iov = &iov;
5035 5085 uio.uio_resid = iov.iov_len;
5036 5086 uio.uio_loffset = 0;
5037 5087 uio.uio_iovcnt = 1;
5038 5088 while (mp && mp->b_datap->db_type != M_DATA &&
5039 5089 uio.uio_resid >= 0) {
5040 5090 ASSERT(STRUCT_FGET(strpeek, flags) == 0 ?
5041 5091 mp->b_datap->db_type == M_PROTO :
5042 5092 mp->b_datap->db_type == M_PCPROTO);
5043 5093
5044 5094 if ((n = MIN(uio.uio_resid,
5045 5095 mp->b_wptr - mp->b_rptr)) != 0 &&
5046 5096 (error = uiomove((char *)mp->b_rptr, n,
5047 5097 UIO_READ, &uio)) != 0) {
5048 5098 freemsg(tmp_mp);
5049 5099 return (error);
5050 5100 }
5051 5101 ctl_part = 1;
5052 5102 mp = mp->b_cont;
5053 5103 }
5054 5104 /* No ctl message */
5055 5105 if (ctl_part == 0)
5056 5106 STRUCT_FSET(strpeek, ctlbuf.len, -1);
5057 5107 else
5058 5108 STRUCT_FSET(strpeek, ctlbuf.len,
5059 5109 STRUCT_FGET(strpeek, ctlbuf.maxlen) -
5060 5110 uio.uio_resid);
5061 5111 }
5062 5112
5063 5113 /*
5064 5114 * Now process DATA blocks, if any.
5065 5115 * If user doesn't want to get data info by setting maxlen <= 0,
5066 5116 * then set len to -1/0 and skip data blocks part.
5067 5117 */
5068 5118 if (STRUCT_FGET(strpeek, databuf.maxlen) < 0)
5069 5119 STRUCT_FSET(strpeek, databuf.len, -1);
5070 5120 else if (STRUCT_FGET(strpeek, databuf.maxlen) == 0)
5071 5121 STRUCT_FSET(strpeek, databuf.len, 0);
5072 5122 else {
5073 5123 int data_part = 0;
5074 5124
5075 5125 iov.iov_base = STRUCT_FGETP(strpeek, databuf.buf);
5076 5126 iov.iov_len = STRUCT_FGET(strpeek, databuf.maxlen);
5077 5127 uio.uio_iov = &iov;
5078 5128 uio.uio_resid = iov.iov_len;
5079 5129 uio.uio_loffset = 0;
5080 5130 uio.uio_iovcnt = 1;
5081 5131 while (mp && uio.uio_resid) {
5082 5132 if (mp->b_datap->db_type == M_DATA) {
5083 5133 if ((n = MIN(uio.uio_resid,
5084 5134 mp->b_wptr - mp->b_rptr)) != 0 &&
5085 5135 (error = uiomove((char *)mp->b_rptr,
5086 5136 n, UIO_READ, &uio)) != 0) {
5087 5137 freemsg(tmp_mp);
5088 5138 return (error);
5089 5139 }
5090 5140 data_part = 1;
5091 5141 }
5092 5142 ASSERT(data_part == 0 ||
5093 5143 mp->b_datap->db_type == M_DATA);
5094 5144 mp = mp->b_cont;
5095 5145 }
5096 5146 /* No data message */
5097 5147 if (data_part == 0)
5098 5148 STRUCT_FSET(strpeek, databuf.len, -1);
5099 5149 else
5100 5150 STRUCT_FSET(strpeek, databuf.len,
5101 5151 STRUCT_FGET(strpeek, databuf.maxlen) -
5102 5152 uio.uio_resid);
5103 5153 }
5104 5154 freemsg(tmp_mp);
5105 5155
5106 5156 /*
5107 5157 * It is a synch stream and user wants to get
5108 5158 * data (maxlen > 0).
5109 5159 * uio setup is done by the codes that process DATA
5110 5160 * blocks above.
5111 5161 */
5112 5162 if ((fmp == NULL) && STRUCT_FGET(strpeek, databuf.maxlen) > 0) {
5113 5163 infod_t infod;
5114 5164
5115 5165 infod.d_cmd = INFOD_COPYOUT;
5116 5166 infod.d_res = 0;
5117 5167 infod.d_uiop = &uio;
5118 5168 error = infonext(rdq, &infod);
5119 5169 if (error == EINVAL || error == EBUSY)
5120 5170 error = 0;
5121 5171 if (error)
5122 5172 return (error);
5123 5173 STRUCT_FSET(strpeek, databuf.len, STRUCT_FGET(strpeek,
5124 5174 databuf.maxlen) - uio.uio_resid);
5125 5175 if (STRUCT_FGET(strpeek, databuf.len) == 0) {
5126 5176 /*
5127 5177 * No data found by the infonext().
5128 5178 */
5129 5179 STRUCT_FSET(strpeek, databuf.len, -1);
5130 5180 }
5131 5181 }
5132 5182 error = strcopyout(STRUCT_BUF(strpeek), (void *)arg,
5133 5183 STRUCT_SIZE(strpeek), copyflag);
5134 5184 if (error) {
5135 5185 return (error);
5136 5186 }
5137 5187 /*
5138 5188 * If there is no message retrieved, set return code to 0
5139 5189 * otherwise, set it to 1.
5140 5190 */
5141 5191 if (STRUCT_FGET(strpeek, ctlbuf.len) == -1 &&
5142 5192 STRUCT_FGET(strpeek, databuf.len) == -1)
5143 5193 *rvalp = 0;
5144 5194 else
5145 5195 *rvalp = 1;
5146 5196 return (0);
5147 5197 }
5148 5198
5149 5199 case I_FDINSERT:
5150 5200 {
5151 5201 STRUCT_DECL(strfdinsert, strfdinsert);
5152 5202 struct file *resftp;
5153 5203 struct stdata *resstp;
5154 5204 t_uscalar_t ival;
5155 5205 ssize_t msgsize;
5156 5206 struct strbuf mctl;
5157 5207
5158 5208 STRUCT_INIT(strfdinsert, flag);
5159 5209 if (stp->sd_flag & STRHUP)
5160 5210 return (ENXIO);
5161 5211 /*
5162 5212 * STRDERR, STWRERR and STPLEX tested above.
5163 5213 */
5164 5214 error = strcopyin((void *)arg, STRUCT_BUF(strfdinsert),
5165 5215 STRUCT_SIZE(strfdinsert), copyflag);
5166 5216 if (error)
5167 5217 return (error);
5168 5218
5169 5219 if (STRUCT_FGET(strfdinsert, offset) < 0 ||
5170 5220 (STRUCT_FGET(strfdinsert, offset) %
5171 5221 sizeof (t_uscalar_t)) != 0)
5172 5222 return (EINVAL);
5173 5223 if ((resftp = getf(STRUCT_FGET(strfdinsert, fildes))) != NULL) {
5174 5224 if ((resstp = resftp->f_vnode->v_stream) == NULL) {
5175 5225 releasef(STRUCT_FGET(strfdinsert, fildes));
5176 5226 return (EINVAL);
5177 5227 }
5178 5228 } else
5179 5229 return (EINVAL);
5180 5230
5181 5231 mutex_enter(&resstp->sd_lock);
5182 5232 if (resstp->sd_flag & (STRDERR|STWRERR|STRHUP|STPLEX)) {
5183 5233 error = strgeterr(resstp,
5184 5234 STRDERR|STWRERR|STRHUP|STPLEX, 0);
5185 5235 if (error != 0) {
5186 5236 mutex_exit(&resstp->sd_lock);
5187 5237 releasef(STRUCT_FGET(strfdinsert, fildes));
5188 5238 return (error);
5189 5239 }
5190 5240 }
5191 5241 mutex_exit(&resstp->sd_lock);
5192 5242
5193 5243 #ifdef _ILP32
5194 5244 {
5195 5245 queue_t *q;
5196 5246 queue_t *mate = NULL;
5197 5247
5198 5248 /* get read queue of stream terminus */
5199 5249 claimstr(resstp->sd_wrq);
5200 5250 for (q = resstp->sd_wrq->q_next; q->q_next != NULL;
5201 5251 q = q->q_next)
5202 5252 if (!STRMATED(resstp) && STREAM(q) != resstp &&
5203 5253 mate == NULL) {
5204 5254 ASSERT(q->q_qinfo->qi_srvp);
5205 5255 ASSERT(_OTHERQ(q)->q_qinfo->qi_srvp);
5206 5256 claimstr(q);
5207 5257 mate = q;
5208 5258 }
5209 5259 q = _RD(q);
5210 5260 if (mate)
5211 5261 releasestr(mate);
5212 5262 releasestr(resstp->sd_wrq);
5213 5263 ival = (t_uscalar_t)q;
5214 5264 }
5215 5265 #else
5216 5266 ival = (t_uscalar_t)getminor(resftp->f_vnode->v_rdev);
5217 5267 #endif /* _ILP32 */
5218 5268
5219 5269 if (STRUCT_FGET(strfdinsert, ctlbuf.len) <
5220 5270 STRUCT_FGET(strfdinsert, offset) + sizeof (t_uscalar_t)) {
5221 5271 releasef(STRUCT_FGET(strfdinsert, fildes));
5222 5272 return (EINVAL);
5223 5273 }
5224 5274
5225 5275 /*
5226 5276 * Check for legal flag value.
5227 5277 */
5228 5278 if (STRUCT_FGET(strfdinsert, flags) & ~RS_HIPRI) {
5229 5279 releasef(STRUCT_FGET(strfdinsert, fildes));
5230 5280 return (EINVAL);
5231 5281 }
5232 5282
5233 5283 /* get these values from those cached in the stream head */
5234 5284 mutex_enter(QLOCK(stp->sd_wrq));
5235 5285 rmin = stp->sd_qn_minpsz;
5236 5286 rmax = stp->sd_qn_maxpsz;
5237 5287 mutex_exit(QLOCK(stp->sd_wrq));
5238 5288
5239 5289 /*
5240 5290 * Make sure ctl and data sizes together fall within
5241 5291 * the limits of the max and min receive packet sizes
5242 5292 * and do not exceed system limit. A negative data
5243 5293 * length means that no data part is to be sent.
5244 5294 */
5245 5295 ASSERT((rmax >= 0) || (rmax == INFPSZ));
5246 5296 if (rmax == 0) {
5247 5297 releasef(STRUCT_FGET(strfdinsert, fildes));
5248 5298 return (ERANGE);
5249 5299 }
5250 5300 if ((msgsize = STRUCT_FGET(strfdinsert, databuf.len)) < 0)
5251 5301 msgsize = 0;
5252 5302 if ((msgsize < rmin) ||
5253 5303 ((msgsize > rmax) && (rmax != INFPSZ)) ||
5254 5304 (STRUCT_FGET(strfdinsert, ctlbuf.len) > strctlsz)) {
5255 5305 releasef(STRUCT_FGET(strfdinsert, fildes));
5256 5306 return (ERANGE);
5257 5307 }
5258 5308
5259 5309 mutex_enter(&stp->sd_lock);
5260 5310 while (!(STRUCT_FGET(strfdinsert, flags) & RS_HIPRI) &&
5261 5311 !canputnext(stp->sd_wrq)) {
5262 5312 if ((error = strwaitq(stp, WRITEWAIT, (ssize_t)0,
5263 5313 flag, -1, &done)) != 0 || done) {
5264 5314 mutex_exit(&stp->sd_lock);
5265 5315 releasef(STRUCT_FGET(strfdinsert, fildes));
5266 5316 return (error);
5267 5317 }
5268 5318 if ((error = i_straccess(stp, access)) != 0) {
5269 5319 mutex_exit(&stp->sd_lock);
5270 5320 releasef(
5271 5321 STRUCT_FGET(strfdinsert, fildes));
5272 5322 return (error);
5273 5323 }
5274 5324 }
5275 5325 mutex_exit(&stp->sd_lock);
5276 5326
5277 5327 /*
5278 5328 * Copy strfdinsert.ctlbuf into native form of
5279 5329 * ctlbuf to pass down into strmakemsg().
5280 5330 */
5281 5331 mctl.maxlen = STRUCT_FGET(strfdinsert, ctlbuf.maxlen);
5282 5332 mctl.len = STRUCT_FGET(strfdinsert, ctlbuf.len);
5283 5333 mctl.buf = STRUCT_FGETP(strfdinsert, ctlbuf.buf);
5284 5334
5285 5335 iov.iov_base = STRUCT_FGETP(strfdinsert, databuf.buf);
5286 5336 iov.iov_len = STRUCT_FGET(strfdinsert, databuf.len);
5287 5337 uio.uio_iov = &iov;
5288 5338 uio.uio_iovcnt = 1;
5289 5339 uio.uio_loffset = 0;
5290 5340 uio.uio_segflg = (copyflag == U_TO_K) ? UIO_USERSPACE :
5291 5341 UIO_SYSSPACE;
5292 5342 uio.uio_fmode = 0;
5293 5343 uio.uio_extflg = UIO_COPY_CACHED;
5294 5344 uio.uio_resid = iov.iov_len;
5295 5345 if ((error = strmakemsg(&mctl,
5296 5346 &msgsize, &uio, stp,
5297 5347 STRUCT_FGET(strfdinsert, flags), &mp)) != 0 || !mp) {
5298 5348 STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5299 5349 releasef(STRUCT_FGET(strfdinsert, fildes));
5300 5350 return (error);
5301 5351 }
5302 5352
5303 5353 STRUCT_FSET(strfdinsert, databuf.len, msgsize);
5304 5354
5305 5355 /*
5306 5356 * Place the possibly reencoded queue pointer 'offset' bytes
5307 5357 * from the start of the control portion of the message.
5308 5358 */
5309 5359 *((t_uscalar_t *)(mp->b_rptr +
5310 5360 STRUCT_FGET(strfdinsert, offset))) = ival;
5311 5361
5312 5362 /*
5313 5363 * Put message downstream.
5314 5364 */
5315 5365 stream_willservice(stp);
5316 5366 putnext(stp->sd_wrq, mp);
5317 5367 stream_runservice(stp);
5318 5368 releasef(STRUCT_FGET(strfdinsert, fildes));
5319 5369 return (error);
5320 5370 }
5321 5371
5322 5372 case I_SENDFD:
5323 5373 {
5324 5374 struct file *fp;
5325 5375
5326 5376 if ((fp = getf((int)arg)) == NULL)
5327 5377 return (EBADF);
5328 5378 error = do_sendfp(stp, fp, crp);
5329 5379 if (auditing) {
5330 5380 audit_fdsend((int)arg, fp, error);
5331 5381 }
5332 5382 releasef((int)arg);
5333 5383 return (error);
5334 5384 }
5335 5385
5336 5386 case I_RECVFD:
5337 5387 case I_E_RECVFD:
5338 5388 {
5339 5389 struct k_strrecvfd *srf;
5340 5390 int i, fd;
5341 5391
5342 5392 mutex_enter(&stp->sd_lock);
5343 5393 while (!(mp = getq(rdq))) {
5344 5394 if (stp->sd_flag & (STRHUP|STREOF)) {
5345 5395 mutex_exit(&stp->sd_lock);
5346 5396 return (ENXIO);
5347 5397 }
5348 5398 if ((error = strwaitq(stp, GETWAIT, (ssize_t)0,
5349 5399 flag, -1, &done)) != 0 || done) {
5350 5400 mutex_exit(&stp->sd_lock);
5351 5401 return (error);
5352 5402 }
5353 5403 if ((error = i_straccess(stp, access)) != 0) {
5354 5404 mutex_exit(&stp->sd_lock);
5355 5405 return (error);
5356 5406 }
5357 5407 }
5358 5408 if (mp->b_datap->db_type != M_PASSFP) {
5359 5409 putback(stp, rdq, mp, mp->b_band);
5360 5410 mutex_exit(&stp->sd_lock);
5361 5411 return (EBADMSG);
5362 5412 }
5363 5413 mutex_exit(&stp->sd_lock);
5364 5414
5365 5415 srf = (struct k_strrecvfd *)mp->b_rptr;
5366 5416 if ((fd = ufalloc(0)) == -1) {
5367 5417 mutex_enter(&stp->sd_lock);
5368 5418 putback(stp, rdq, mp, mp->b_band);
5369 5419 mutex_exit(&stp->sd_lock);
5370 5420 return (EMFILE);
5371 5421 }
5372 5422 if (cmd == I_RECVFD) {
5373 5423 struct o_strrecvfd ostrfd;
5374 5424
5375 5425 /* check to see if uid/gid values are too large. */
5376 5426
5377 5427 if (srf->uid > (o_uid_t)USHRT_MAX ||
5378 5428 srf->gid > (o_gid_t)USHRT_MAX) {
5379 5429 mutex_enter(&stp->sd_lock);
5380 5430 putback(stp, rdq, mp, mp->b_band);
5381 5431 mutex_exit(&stp->sd_lock);
5382 5432 setf(fd, NULL); /* release fd entry */
5383 5433 return (EOVERFLOW);
5384 5434 }
5385 5435
5386 5436 ostrfd.fd = fd;
5387 5437 ostrfd.uid = (o_uid_t)srf->uid;
5388 5438 ostrfd.gid = (o_gid_t)srf->gid;
5389 5439
5390 5440 /* Null the filler bits */
5391 5441 for (i = 0; i < 8; i++)
5392 5442 ostrfd.fill[i] = 0;
5393 5443
5394 5444 error = strcopyout(&ostrfd, (void *)arg,
5395 5445 sizeof (struct o_strrecvfd), copyflag);
5396 5446 } else { /* I_E_RECVFD */
5397 5447 struct strrecvfd strfd;
5398 5448
5399 5449 strfd.fd = fd;
5400 5450 strfd.uid = srf->uid;
5401 5451 strfd.gid = srf->gid;
5402 5452
5403 5453 /* null the filler bits */
5404 5454 for (i = 0; i < 8; i++)
5405 5455 strfd.fill[i] = 0;
5406 5456
5407 5457 error = strcopyout(&strfd, (void *)arg,
5408 5458 sizeof (struct strrecvfd), copyflag);
5409 5459 }
5410 5460
5411 5461 if (error) {
5412 5462 setf(fd, NULL); /* release fd entry */
5413 5463 mutex_enter(&stp->sd_lock);
5414 5464 putback(stp, rdq, mp, mp->b_band);
5415 5465 mutex_exit(&stp->sd_lock);
5416 5466 return (error);
5417 5467 }
5418 5468 if (auditing) {
5419 5469 audit_fdrecv(fd, srf->fp);
5420 5470 }
5421 5471
5422 5472 /*
5423 5473 * Always increment f_count since the freemsg() below will
5424 5474 * always call free_passfp() which performs a closef().
5425 5475 */
5426 5476 mutex_enter(&srf->fp->f_tlock);
5427 5477 srf->fp->f_count++;
5428 5478 mutex_exit(&srf->fp->f_tlock);
5429 5479 setf(fd, srf->fp);
5430 5480 freemsg(mp);
5431 5481 return (0);
5432 5482 }
5433 5483
5434 5484 case I_SWROPT:
5435 5485 /*
5436 5486 * Set/clear the write options. arg is a bit
5437 5487 * mask with any of the following bits set...
5438 5488 * SNDZERO - send zero length message
5439 5489 * SNDPIPE - send sigpipe to process if
5440 5490 * sd_werror is set and process is
5441 5491 * doing a write or putmsg.
5442 5492 * The new stream head write options should reflect
5443 5493 * what is in arg.
5444 5494 */
5445 5495 if (arg & ~(SNDZERO|SNDPIPE))
5446 5496 return (EINVAL);
5447 5497
5448 5498 mutex_enter(&stp->sd_lock);
5449 5499 stp->sd_wput_opt &= ~(SW_SIGPIPE|SW_SNDZERO);
5450 5500 if (arg & SNDZERO)
5451 5501 stp->sd_wput_opt |= SW_SNDZERO;
5452 5502 if (arg & SNDPIPE)
5453 5503 stp->sd_wput_opt |= SW_SIGPIPE;
5454 5504 mutex_exit(&stp->sd_lock);
5455 5505 return (0);
5456 5506
5457 5507 case I_GWROPT:
5458 5508 {
5459 5509 int wropt = 0;
5460 5510
5461 5511 if (stp->sd_wput_opt & SW_SNDZERO)
5462 5512 wropt |= SNDZERO;
5463 5513 if (stp->sd_wput_opt & SW_SIGPIPE)
5464 5514 wropt |= SNDPIPE;
5465 5515 return (strcopyout(&wropt, (void *)arg, sizeof (wropt),
5466 5516 copyflag));
5467 5517 }
5468 5518
5469 5519 case I_LIST:
5470 5520 /*
5471 5521 * Returns all the modules found on this stream,
5472 5522 * upto the driver. If argument is NULL, return the
5473 5523 * number of modules (including driver). If argument
5474 5524 * is not NULL, copy the names into the structure
5475 5525 * provided.
5476 5526 */
5477 5527
5478 5528 {
5479 5529 queue_t *q;
5480 5530 char *qname;
5481 5531 int i, nmods;
5482 5532 struct str_mlist *mlist;
5483 5533 STRUCT_DECL(str_list, strlist);
5484 5534
5485 5535 if (arg == 0) { /* Return number of modules plus driver */
5486 5536 if (stp->sd_vnode->v_type == VFIFO)
5487 5537 *rvalp = stp->sd_pushcnt;
5488 5538 else
5489 5539 *rvalp = stp->sd_pushcnt + 1;
5490 5540 return (0);
5491 5541 }
5492 5542
5493 5543 STRUCT_INIT(strlist, flag);
5494 5544
5495 5545 error = strcopyin((void *)arg, STRUCT_BUF(strlist),
5496 5546 STRUCT_SIZE(strlist), copyflag);
5497 5547 if (error != 0)
5498 5548 return (error);
5499 5549
5500 5550 mlist = STRUCT_FGETP(strlist, sl_modlist);
5501 5551 nmods = STRUCT_FGET(strlist, sl_nmods);
5502 5552 if (nmods <= 0)
5503 5553 return (EINVAL);
5504 5554
5505 5555 claimstr(stp->sd_wrq);
5506 5556 q = stp->sd_wrq;
5507 5557 for (i = 0; i < nmods && _SAMESTR(q); i++, q = q->q_next) {
5508 5558 qname = Q2NAME(q->q_next);
5509 5559 error = strcopyout(qname, &mlist[i], strlen(qname) + 1,
5510 5560 copyflag);
5511 5561 if (error != 0) {
5512 5562 releasestr(stp->sd_wrq);
5513 5563 return (error);
5514 5564 }
5515 5565 }
5516 5566 releasestr(stp->sd_wrq);
5517 5567 return (strcopyout(&i, (void *)arg, sizeof (int), copyflag));
5518 5568 }
5519 5569
5520 5570 case I_CKBAND:
5521 5571 {
5522 5572 queue_t *q;
5523 5573 qband_t *qbp;
5524 5574
5525 5575 if ((arg < 0) || (arg >= NBAND))
5526 5576 return (EINVAL);
5527 5577 q = _RD(stp->sd_wrq);
5528 5578 mutex_enter(QLOCK(q));
5529 5579 if (arg > (int)q->q_nband) {
5530 5580 *rvalp = 0;
5531 5581 } else {
5532 5582 if (arg == 0) {
5533 5583 if (q->q_first)
5534 5584 *rvalp = 1;
5535 5585 else
5536 5586 *rvalp = 0;
5537 5587 } else {
5538 5588 qbp = q->q_bandp;
5539 5589 while (--arg > 0)
5540 5590 qbp = qbp->qb_next;
5541 5591 if (qbp->qb_first)
5542 5592 *rvalp = 1;
5543 5593 else
5544 5594 *rvalp = 0;
5545 5595 }
5546 5596 }
5547 5597 mutex_exit(QLOCK(q));
5548 5598 return (0);
5549 5599 }
5550 5600
5551 5601 case I_GETBAND:
5552 5602 {
5553 5603 int intpri;
5554 5604 queue_t *q;
5555 5605
5556 5606 q = _RD(stp->sd_wrq);
5557 5607 mutex_enter(QLOCK(q));
5558 5608 mp = q->q_first;
5559 5609 if (!mp) {
5560 5610 mutex_exit(QLOCK(q));
5561 5611 return (ENODATA);
5562 5612 }
5563 5613 intpri = (int)mp->b_band;
5564 5614 error = strcopyout(&intpri, (void *)arg, sizeof (int),
5565 5615 copyflag);
5566 5616 mutex_exit(QLOCK(q));
5567 5617 return (error);
5568 5618 }
5569 5619
5570 5620 case I_ATMARK:
5571 5621 {
5572 5622 queue_t *q;
5573 5623
5574 5624 if (arg & ~(ANYMARK|LASTMARK))
5575 5625 return (EINVAL);
5576 5626 q = _RD(stp->sd_wrq);
5577 5627 mutex_enter(&stp->sd_lock);
5578 5628 if ((stp->sd_flag & STRATMARK) && (arg == ANYMARK)) {
5579 5629 *rvalp = 1;
5580 5630 } else {
5581 5631 mutex_enter(QLOCK(q));
5582 5632 mp = q->q_first;
5583 5633
5584 5634 if (mp == NULL)
5585 5635 *rvalp = 0;
5586 5636 else if ((arg == ANYMARK) && (mp->b_flag & MSGMARK))
5587 5637 *rvalp = 1;
5588 5638 else if ((arg == LASTMARK) && (mp == stp->sd_mark))
5589 5639 *rvalp = 1;
5590 5640 else
5591 5641 *rvalp = 0;
5592 5642 mutex_exit(QLOCK(q));
5593 5643 }
5594 5644 mutex_exit(&stp->sd_lock);
5595 5645 return (0);
5596 5646 }
5597 5647
5598 5648 case I_CANPUT:
5599 5649 {
5600 5650 char band;
5601 5651
5602 5652 if ((arg < 0) || (arg >= NBAND))
5603 5653 return (EINVAL);
5604 5654 band = (char)arg;
5605 5655 *rvalp = bcanputnext(stp->sd_wrq, band);
5606 5656 return (0);
5607 5657 }
5608 5658
5609 5659 case I_SETCLTIME:
5610 5660 {
5611 5661 int closetime;
5612 5662
5613 5663 error = strcopyin((void *)arg, &closetime, sizeof (int),
5614 5664 copyflag);
5615 5665 if (error)
5616 5666 return (error);
5617 5667 if (closetime < 0)
5618 5668 return (EINVAL);
5619 5669
5620 5670 stp->sd_closetime = closetime;
5621 5671 return (0);
5622 5672 }
5623 5673
5624 5674 case I_GETCLTIME:
5625 5675 {
5626 5676 int closetime;
5627 5677
5628 5678 closetime = stp->sd_closetime;
5629 5679 return (strcopyout(&closetime, (void *)arg, sizeof (int),
5630 5680 copyflag));
5631 5681 }
5632 5682
5633 5683 case TIOCGSID:
5634 5684 {
5635 5685 pid_t sid;
5636 5686
5637 5687 mutex_enter(&stp->sd_lock);
5638 5688 if (stp->sd_sidp == NULL) {
5639 5689 mutex_exit(&stp->sd_lock);
5640 5690 return (ENOTTY);
5641 5691 }
5642 5692 sid = stp->sd_sidp->pid_id;
5643 5693 mutex_exit(&stp->sd_lock);
5644 5694 return (strcopyout(&sid, (void *)arg, sizeof (pid_t),
5645 5695 copyflag));
5646 5696 }
5647 5697
5648 5698 case TIOCSPGRP:
5649 5699 {
5650 5700 pid_t pgrp;
5651 5701 proc_t *q;
5652 5702 pid_t sid, fg_pgid, bg_pgid;
5653 5703
5654 5704 if (error = strcopyin((void *)arg, &pgrp, sizeof (pid_t),
5655 5705 copyflag))
5656 5706 return (error);
5657 5707 mutex_enter(&stp->sd_lock);
5658 5708 mutex_enter(&pidlock);
5659 5709 if (stp->sd_sidp != ttoproc(curthread)->p_sessp->s_sidp) {
5660 5710 mutex_exit(&pidlock);
5661 5711 mutex_exit(&stp->sd_lock);
5662 5712 return (ENOTTY);
5663 5713 }
5664 5714 if (pgrp == stp->sd_pgidp->pid_id) {
5665 5715 mutex_exit(&pidlock);
5666 5716 mutex_exit(&stp->sd_lock);
5667 5717 return (0);
5668 5718 }
5669 5719 if (pgrp <= 0 || pgrp >= maxpid) {
5670 5720 mutex_exit(&pidlock);
5671 5721 mutex_exit(&stp->sd_lock);
5672 5722 return (EINVAL);
5673 5723 }
5674 5724 if ((q = pgfind(pgrp)) == NULL ||
5675 5725 q->p_sessp != ttoproc(curthread)->p_sessp) {
5676 5726 mutex_exit(&pidlock);
5677 5727 mutex_exit(&stp->sd_lock);
5678 5728 return (EPERM);
5679 5729 }
5680 5730 sid = stp->sd_sidp->pid_id;
5681 5731 fg_pgid = q->p_pgrp;
5682 5732 bg_pgid = stp->sd_pgidp->pid_id;
5683 5733 CL_SET_PROCESS_GROUP(curthread, sid, bg_pgid, fg_pgid);
5684 5734 PID_RELE(stp->sd_pgidp);
5685 5735 ctty_clear_sighuped();
5686 5736 stp->sd_pgidp = q->p_pgidp;
5687 5737 PID_HOLD(stp->sd_pgidp);
5688 5738 mutex_exit(&pidlock);
5689 5739 mutex_exit(&stp->sd_lock);
5690 5740 return (0);
5691 5741 }
5692 5742
5693 5743 case TIOCGPGRP:
5694 5744 {
5695 5745 pid_t pgrp;
5696 5746
5697 5747 mutex_enter(&stp->sd_lock);
5698 5748 if (stp->sd_sidp == NULL) {
5699 5749 mutex_exit(&stp->sd_lock);
5700 5750 return (ENOTTY);
5701 5751 }
5702 5752 pgrp = stp->sd_pgidp->pid_id;
5703 5753 mutex_exit(&stp->sd_lock);
5704 5754 return (strcopyout(&pgrp, (void *)arg, sizeof (pid_t),
5705 5755 copyflag));
5706 5756 }
5707 5757
5708 5758 case TIOCSCTTY:
5709 5759 {
5710 5760 return (strctty(stp));
5711 5761 }
5712 5762
5713 5763 case TIOCNOTTY:
5714 5764 {
5715 5765 /* freectty() always assumes curproc. */
5716 5766 if (freectty(B_FALSE) != 0)
5717 5767 return (0);
5718 5768 return (ENOTTY);
5719 5769 }
5720 5770
5721 5771 case FIONBIO:
5722 5772 case FIOASYNC:
5723 5773 return (0); /* handled by the upper layer */
5724 5774 }
5725 5775 }
5726 5776
5727 5777 /*
5728 5778 * Custom free routine used for M_PASSFP messages.
5729 5779 */
5730 5780 static void
5731 5781 free_passfp(struct k_strrecvfd *srf)
5732 5782 {
5733 5783 (void) closef(srf->fp);
5734 5784 kmem_free(srf, sizeof (struct k_strrecvfd) + sizeof (frtn_t));
5735 5785 }
5736 5786
5737 5787 /* ARGSUSED */
5738 5788 int
5739 5789 do_sendfp(struct stdata *stp, struct file *fp, struct cred *cr)
5740 5790 {
5741 5791 queue_t *qp, *nextqp;
5742 5792 struct k_strrecvfd *srf;
5743 5793 mblk_t *mp;
5744 5794 frtn_t *frtnp;
5745 5795 size_t bufsize;
5746 5796 queue_t *mate = NULL;
5747 5797 syncq_t *sq = NULL;
5748 5798 int retval = 0;
5749 5799
5750 5800 if (stp->sd_flag & STRHUP)
5751 5801 return (ENXIO);
5752 5802
5753 5803 claimstr(stp->sd_wrq);
5754 5804
5755 5805 /* Fastpath, we have a pipe, and we are already mated, use it. */
5756 5806 if (STRMATED(stp)) {
5757 5807 qp = _RD(stp->sd_mate->sd_wrq);
5758 5808 claimstr(qp);
5759 5809 mate = qp;
5760 5810 } else { /* Not already mated. */
5761 5811
5762 5812 /*
5763 5813 * Walk the stream to the end of this one.
5764 5814 * assumes that the claimstr() will prevent
5765 5815 * plumbing between the stream head and the
5766 5816 * driver from changing
5767 5817 */
5768 5818 qp = stp->sd_wrq;
5769 5819
5770 5820 /*
5771 5821 * Loop until we reach the end of this stream.
5772 5822 * On completion, qp points to the write queue
5773 5823 * at the end of the stream, or the read queue
5774 5824 * at the stream head if this is a fifo.
5775 5825 */
5776 5826 while (((qp = qp->q_next) != NULL) && _SAMESTR(qp))
5777 5827 ;
5778 5828
5779 5829 /*
5780 5830 * Just in case we get a q_next which is NULL, but
5781 5831 * not at the end of the stream. This is actually
5782 5832 * broken, so we set an assert to catch it in
5783 5833 * debug, and set an error and return if not debug.
5784 5834 */
5785 5835 ASSERT(qp);
5786 5836 if (qp == NULL) {
5787 5837 releasestr(stp->sd_wrq);
5788 5838 return (EINVAL);
5789 5839 }
5790 5840
5791 5841 /*
5792 5842 * Enter the syncq for the driver, so (hopefully)
5793 5843 * the queue values will not change on us.
5794 5844 * XXXX - This will only prevent the race IFF only
5795 5845 * the write side modifies the q_next member, and
5796 5846 * the put procedure is protected by at least
5797 5847 * MT_PERQ.
5798 5848 */
5799 5849 if ((sq = qp->q_syncq) != NULL)
5800 5850 entersq(sq, SQ_PUT);
5801 5851
5802 5852 /* Now get the q_next value from this qp. */
5803 5853 nextqp = qp->q_next;
5804 5854
5805 5855 /*
5806 5856 * If nextqp exists and the other stream is different
5807 5857 * from this one claim the stream, set the mate, and
5808 5858 * get the read queue at the stream head of the other
5809 5859 * stream. Assumes that nextqp was at least valid when
5810 5860 * we got it. Hopefully the entersq of the driver
5811 5861 * will prevent it from changing on us.
5812 5862 */
5813 5863 if ((nextqp != NULL) && (STREAM(nextqp) != stp)) {
5814 5864 ASSERT(qp->q_qinfo->qi_srvp);
5815 5865 ASSERT(_OTHERQ(qp)->q_qinfo->qi_srvp);
5816 5866 ASSERT(_OTHERQ(qp->q_next)->q_qinfo->qi_srvp);
5817 5867 claimstr(nextqp);
5818 5868
5819 5869 /* Make sure we still have a q_next */
5820 5870 if (nextqp != qp->q_next) {
5821 5871 releasestr(stp->sd_wrq);
5822 5872 releasestr(nextqp);
5823 5873 return (EINVAL);
5824 5874 }
5825 5875
5826 5876 qp = _RD(STREAM(nextqp)->sd_wrq);
5827 5877 mate = qp;
5828 5878 }
5829 5879 /* If we entered the synq above, leave it. */
5830 5880 if (sq != NULL)
5831 5881 leavesq(sq, SQ_PUT);
5832 5882 } /* STRMATED(STP) */
5833 5883
5834 5884 /* XXX prevents substitution of the ops vector */
5835 5885 if (qp->q_qinfo != &strdata && qp->q_qinfo != &fifo_strdata) {
5836 5886 retval = EINVAL;
5837 5887 goto out;
5838 5888 }
5839 5889
5840 5890 if (qp->q_flag & QFULL) {
5841 5891 retval = EAGAIN;
5842 5892 goto out;
5843 5893 }
5844 5894
5845 5895 /*
5846 5896 * Since M_PASSFP messages include a file descriptor, we use
5847 5897 * esballoc() and specify a custom free routine (free_passfp()) that
5848 5898 * will close the descriptor as part of freeing the message. For
5849 5899 * convenience, we stash the frtn_t right after the data block.
5850 5900 */
5851 5901 bufsize = sizeof (struct k_strrecvfd) + sizeof (frtn_t);
5852 5902 srf = kmem_alloc(bufsize, KM_NOSLEEP);
5853 5903 if (srf == NULL) {
5854 5904 retval = EAGAIN;
5855 5905 goto out;
5856 5906 }
5857 5907
5858 5908 frtnp = (frtn_t *)(srf + 1);
5859 5909 frtnp->free_arg = (caddr_t)srf;
5860 5910 frtnp->free_func = free_passfp;
5861 5911
5862 5912 mp = esballoc((uchar_t *)srf, bufsize, BPRI_MED, frtnp);
5863 5913 if (mp == NULL) {
5864 5914 kmem_free(srf, bufsize);
5865 5915 retval = EAGAIN;
5866 5916 goto out;
5867 5917 }
5868 5918 mp->b_wptr += sizeof (struct k_strrecvfd);
5869 5919 mp->b_datap->db_type = M_PASSFP;
5870 5920
5871 5921 srf->fp = fp;
5872 5922 srf->uid = crgetuid(curthread->t_cred);
5873 5923 srf->gid = crgetgid(curthread->t_cred);
5874 5924 mutex_enter(&fp->f_tlock);
5875 5925 fp->f_count++;
5876 5926 mutex_exit(&fp->f_tlock);
5877 5927
5878 5928 put(qp, mp);
5879 5929 out:
5880 5930 releasestr(stp->sd_wrq);
5881 5931 if (mate)
5882 5932 releasestr(mate);
5883 5933 return (retval);
5884 5934 }
5885 5935
5886 5936 /*
5887 5937 * Send an ioctl message downstream and wait for acknowledgement.
5888 5938 * flags may be set to either U_TO_K or K_TO_K and a combination
5889 5939 * of STR_NOERROR or STR_NOSIG
5890 5940 * STR_NOSIG: Signals are essentially ignored or held and have
5891 5941 * no effect for the duration of the call.
5892 5942 * STR_NOERROR: Ignores stream head read, write and hup errors.
5893 5943 * Additionally, if an existing ioctl times out, it is assumed
5894 5944 * lost and and this ioctl will continue as if the previous ioctl had
5895 5945 * finished. ETIME may be returned if this ioctl times out (i.e.
5896 5946 * ic_timout is not INFTIM). Non-stream head errors may be returned if
5897 5947 * the ioc_error indicates that the driver/module had problems,
5898 5948 * an EFAULT was found when accessing user data, a lack of
5899 5949 * resources, etc.
5900 5950 */
5901 5951 int
5902 5952 strdoioctl(
5903 5953 struct stdata *stp,
5904 5954 struct strioctl *strioc,
5905 5955 int fflags, /* file flags with model info */
5906 5956 int flag,
5907 5957 cred_t *crp,
5908 5958 int *rvalp)
5909 5959 {
5910 5960 mblk_t *bp;
5911 5961 struct iocblk *iocbp;
5912 5962 struct copyreq *reqp;
5913 5963 struct copyresp *resp;
5914 5964 int id;
5915 5965 int transparent = 0;
5916 5966 int error = 0;
5917 5967 int len = 0;
5918 5968 caddr_t taddr;
5919 5969 int copyflag = (flag & (U_TO_K | K_TO_K));
5920 5970 int sigflag = (flag & STR_NOSIG);
5921 5971 int errs;
5922 5972 uint_t waitflags;
5923 5973 boolean_t set_iocwaitne = B_FALSE;
5924 5974
5925 5975 ASSERT(copyflag == U_TO_K || copyflag == K_TO_K);
5926 5976 ASSERT((fflags & FMODELS) != 0);
5927 5977
5928 5978 TRACE_2(TR_FAC_STREAMS_FR,
5929 5979 TR_STRDOIOCTL,
5930 5980 "strdoioctl:stp %p strioc %p", stp, strioc);
5931 5981 if (strioc->ic_len == TRANSPARENT) { /* send arg in M_DATA block */
5932 5982 transparent = 1;
5933 5983 strioc->ic_len = sizeof (intptr_t);
5934 5984 }
5935 5985
5936 5986 if (strioc->ic_len < 0 || (strmsgsz > 0 && strioc->ic_len > strmsgsz))
5937 5987 return (EINVAL);
5938 5988
5939 5989 if ((bp = allocb_cred_wait(sizeof (union ioctypes), sigflag, &error,
5940 5990 crp, curproc->p_pid)) == NULL)
5941 5991 return (error);
5942 5992
5943 5993 bzero(bp->b_wptr, sizeof (union ioctypes));
5944 5994
5945 5995 iocbp = (struct iocblk *)bp->b_wptr;
5946 5996 iocbp->ioc_count = strioc->ic_len;
5947 5997 iocbp->ioc_cmd = strioc->ic_cmd;
5948 5998 iocbp->ioc_flag = (fflags & FMODELS);
5949 5999
5950 6000 crhold(crp);
5951 6001 iocbp->ioc_cr = crp;
5952 6002 DB_TYPE(bp) = M_IOCTL;
5953 6003 bp->b_wptr += sizeof (struct iocblk);
5954 6004
5955 6005 if (flag & STR_NOERROR)
5956 6006 errs = STPLEX;
5957 6007 else
5958 6008 errs = STRHUP|STRDERR|STWRERR|STPLEX;
5959 6009
5960 6010 /*
5961 6011 * If there is data to copy into ioctl block, do so.
5962 6012 */
5963 6013 if (iocbp->ioc_count > 0) {
5964 6014 if (transparent)
5965 6015 /*
5966 6016 * Note: STR_NOERROR does not have an effect
5967 6017 * in putiocd()
5968 6018 */
5969 6019 id = K_TO_K | sigflag;
5970 6020 else
5971 6021 id = flag;
5972 6022 if ((error = putiocd(bp, strioc->ic_dp, id, crp)) != 0) {
5973 6023 freemsg(bp);
5974 6024 crfree(crp);
5975 6025 return (error);
5976 6026 }
5977 6027
5978 6028 /*
5979 6029 * We could have slept copying in user pages.
5980 6030 * Recheck the stream head state (the other end
5981 6031 * of a pipe could have gone away).
5982 6032 */
5983 6033 if (stp->sd_flag & errs) {
5984 6034 mutex_enter(&stp->sd_lock);
5985 6035 error = strgeterr(stp, errs, 0);
5986 6036 mutex_exit(&stp->sd_lock);
5987 6037 if (error != 0) {
5988 6038 freemsg(bp);
5989 6039 crfree(crp);
5990 6040 return (error);
5991 6041 }
5992 6042 }
5993 6043 }
5994 6044 if (transparent)
5995 6045 iocbp->ioc_count = TRANSPARENT;
5996 6046
5997 6047 /*
5998 6048 * Block for up to STRTIMOUT milliseconds if there is an outstanding
5999 6049 * ioctl for this stream already running. All processes
6000 6050 * sleeping here will be awakened as a result of an ACK
6001 6051 * or NAK being received for the outstanding ioctl, or
6002 6052 * as a result of the timer expiring on the outstanding
6003 6053 * ioctl (a failure), or as a result of any waiting
6004 6054 * process's timer expiring (also a failure).
6005 6055 */
6006 6056
6007 6057 error = 0;
6008 6058 mutex_enter(&stp->sd_lock);
6009 6059 while ((stp->sd_flag & IOCWAIT) ||
6010 6060 (!set_iocwaitne && (stp->sd_flag & IOCWAITNE))) {
6011 6061 clock_t cv_rval;
6012 6062
6013 6063 TRACE_0(TR_FAC_STREAMS_FR,
6014 6064 TR_STRDOIOCTL_WAIT,
6015 6065 "strdoioctl sleeps - IOCWAIT");
6016 6066 cv_rval = str_cv_wait(&stp->sd_iocmonitor, &stp->sd_lock,
6017 6067 STRTIMOUT, sigflag);
6018 6068 if (cv_rval <= 0) {
6019 6069 if (cv_rval == 0) {
6020 6070 error = EINTR;
6021 6071 } else {
6022 6072 if (flag & STR_NOERROR) {
6023 6073 /*
6024 6074 * Terminating current ioctl in
6025 6075 * progress -- assume it got lost and
6026 6076 * wake up the other thread so that the
6027 6077 * operation completes.
6028 6078 */
6029 6079 if (!(stp->sd_flag & IOCWAITNE)) {
6030 6080 set_iocwaitne = B_TRUE;
6031 6081 stp->sd_flag |= IOCWAITNE;
6032 6082 cv_broadcast(&stp->sd_monitor);
6033 6083 }
6034 6084 /*
6035 6085 * Otherwise, there's a running
6036 6086 * STR_NOERROR -- we have no choice
6037 6087 * here but to wait forever (or until
6038 6088 * interrupted).
6039 6089 */
6040 6090 } else {
6041 6091 /*
6042 6092 * pending ioctl has caused
6043 6093 * us to time out
6044 6094 */
6045 6095 error = ETIME;
6046 6096 }
6047 6097 }
6048 6098 } else if ((stp->sd_flag & errs)) {
6049 6099 error = strgeterr(stp, errs, 0);
6050 6100 }
6051 6101 if (error) {
6052 6102 mutex_exit(&stp->sd_lock);
6053 6103 freemsg(bp);
6054 6104 crfree(crp);
6055 6105 return (error);
6056 6106 }
6057 6107 }
6058 6108
6059 6109 /*
6060 6110 * Have control of ioctl mechanism.
6061 6111 * Send down ioctl packet and wait for response.
6062 6112 */
6063 6113 if (stp->sd_iocblk != (mblk_t *)-1) {
6064 6114 freemsg(stp->sd_iocblk);
6065 6115 }
6066 6116 stp->sd_iocblk = NULL;
6067 6117
6068 6118 /*
6069 6119 * If this is marked with 'noerror' (internal; mostly
6070 6120 * I_{P,}{UN,}LINK), then make sure nobody else is able to get
6071 6121 * in here by setting IOCWAITNE.
6072 6122 */
6073 6123 waitflags = IOCWAIT;
6074 6124 if (flag & STR_NOERROR)
6075 6125 waitflags |= IOCWAITNE;
6076 6126
6077 6127 stp->sd_flag |= waitflags;
6078 6128
6079 6129 /*
6080 6130 * Assign sequence number.
6081 6131 */
6082 6132 iocbp->ioc_id = stp->sd_iocid = getiocseqno();
6083 6133
6084 6134 mutex_exit(&stp->sd_lock);
6085 6135
6086 6136 TRACE_1(TR_FAC_STREAMS_FR,
6087 6137 TR_STRDOIOCTL_PUT, "strdoioctl put: stp %p", stp);
6088 6138 stream_willservice(stp);
6089 6139 putnext(stp->sd_wrq, bp);
6090 6140 stream_runservice(stp);
6091 6141
6092 6142 /*
6093 6143 * Timed wait for acknowledgment. The wait time is limited by the
6094 6144 * timeout value, which must be a positive integer (number of
6095 6145 * milliseconds) to wait, or 0 (use default value of STRTIMOUT
6096 6146 * milliseconds), or -1 (wait forever). This will be awakened
6097 6147 * either by an ACK/NAK message arriving, the timer expiring, or
6098 6148 * the timer expiring on another ioctl waiting for control of the
6099 6149 * mechanism.
6100 6150 */
6101 6151 waitioc:
6102 6152 mutex_enter(&stp->sd_lock);
6103 6153
6104 6154
6105 6155 /*
6106 6156 * If the reply has already arrived, don't sleep. If awakened from
6107 6157 * the sleep, fail only if the reply has not arrived by then.
6108 6158 * Otherwise, process the reply.
6109 6159 */
6110 6160 while (!stp->sd_iocblk) {
6111 6161 clock_t cv_rval;
6112 6162
6113 6163 if (stp->sd_flag & errs) {
6114 6164 error = strgeterr(stp, errs, 0);
6115 6165 if (error != 0) {
6116 6166 stp->sd_flag &= ~waitflags;
6117 6167 cv_broadcast(&stp->sd_iocmonitor);
6118 6168 mutex_exit(&stp->sd_lock);
6119 6169 crfree(crp);
6120 6170 return (error);
6121 6171 }
6122 6172 }
6123 6173
6124 6174 TRACE_0(TR_FAC_STREAMS_FR,
6125 6175 TR_STRDOIOCTL_WAIT2,
6126 6176 "strdoioctl sleeps awaiting reply");
6127 6177 ASSERT(error == 0);
6128 6178
6129 6179 cv_rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock,
6130 6180 (strioc->ic_timout ?
6131 6181 strioc->ic_timout * 1000 : STRTIMOUT), sigflag);
6132 6182
6133 6183 /*
6134 6184 * There are four possible cases here: interrupt, timeout,
6135 6185 * wakeup by IOCWAITNE (above), or wakeup by strrput_nondata (a
6136 6186 * valid M_IOCTL reply).
6137 6187 *
6138 6188 * If we've been awakened by a STR_NOERROR ioctl on some other
6139 6189 * thread, then sd_iocblk will still be NULL, and IOCWAITNE
6140 6190 * will be set. Pretend as if we just timed out. Note that
6141 6191 * this other thread waited at least STRTIMOUT before trying to
6142 6192 * awaken our thread, so this is indistinguishable (even for
6143 6193 * INFTIM) from the case where we failed with ETIME waiting on
6144 6194 * IOCWAIT in the prior loop.
6145 6195 */
6146 6196 if (cv_rval > 0 && !(flag & STR_NOERROR) &&
6147 6197 stp->sd_iocblk == NULL && (stp->sd_flag & IOCWAITNE)) {
6148 6198 cv_rval = -1;
6149 6199 }
6150 6200
6151 6201 /*
6152 6202 * note: STR_NOERROR does not protect
6153 6203 * us here.. use ic_timout < 0
6154 6204 */
6155 6205 if (cv_rval <= 0) {
6156 6206 if (cv_rval == 0) {
6157 6207 error = EINTR;
6158 6208 } else {
6159 6209 error = ETIME;
6160 6210 }
6161 6211 /*
6162 6212 * A message could have come in after we were scheduled
6163 6213 * but before we were actually run.
6164 6214 */
6165 6215 bp = stp->sd_iocblk;
6166 6216 stp->sd_iocblk = NULL;
6167 6217 if (bp != NULL) {
6168 6218 if ((bp->b_datap->db_type == M_COPYIN) ||
6169 6219 (bp->b_datap->db_type == M_COPYOUT)) {
6170 6220 mutex_exit(&stp->sd_lock);
6171 6221 if (bp->b_cont) {
6172 6222 freemsg(bp->b_cont);
6173 6223 bp->b_cont = NULL;
6174 6224 }
6175 6225 bp->b_datap->db_type = M_IOCDATA;
6176 6226 bp->b_wptr = bp->b_rptr +
6177 6227 sizeof (struct copyresp);
6178 6228 resp = (struct copyresp *)bp->b_rptr;
6179 6229 resp->cp_rval =
6180 6230 (caddr_t)1; /* failure */
6181 6231 stream_willservice(stp);
6182 6232 putnext(stp->sd_wrq, bp);
6183 6233 stream_runservice(stp);
6184 6234 mutex_enter(&stp->sd_lock);
6185 6235 } else {
6186 6236 freemsg(bp);
6187 6237 }
6188 6238 }
6189 6239 stp->sd_flag &= ~waitflags;
6190 6240 cv_broadcast(&stp->sd_iocmonitor);
6191 6241 mutex_exit(&stp->sd_lock);
6192 6242 crfree(crp);
6193 6243 return (error);
6194 6244 }
6195 6245 }
6196 6246 bp = stp->sd_iocblk;
6197 6247 /*
6198 6248 * Note: it is strictly impossible to get here with sd_iocblk set to
6199 6249 * -1. This is because the initial loop above doesn't allow any new
6200 6250 * ioctls into the fray until all others have passed this point.
6201 6251 */
6202 6252 ASSERT(bp != NULL && bp != (mblk_t *)-1);
6203 6253 TRACE_1(TR_FAC_STREAMS_FR,
6204 6254 TR_STRDOIOCTL_ACK, "strdoioctl got reply: bp %p", bp);
6205 6255 if ((bp->b_datap->db_type == M_IOCACK) ||
6206 6256 (bp->b_datap->db_type == M_IOCNAK)) {
6207 6257 /* for detection of duplicate ioctl replies */
6208 6258 stp->sd_iocblk = (mblk_t *)-1;
6209 6259 stp->sd_flag &= ~waitflags;
6210 6260 cv_broadcast(&stp->sd_iocmonitor);
6211 6261 mutex_exit(&stp->sd_lock);
6212 6262 } else {
6213 6263 /*
6214 6264 * flags not cleared here because we're still doing
6215 6265 * copy in/out for ioctl.
6216 6266 */
6217 6267 stp->sd_iocblk = NULL;
6218 6268 mutex_exit(&stp->sd_lock);
6219 6269 }
6220 6270
6221 6271
6222 6272 /*
6223 6273 * Have received acknowledgment.
6224 6274 */
6225 6275
6226 6276 switch (bp->b_datap->db_type) {
6227 6277 case M_IOCACK:
6228 6278 /*
6229 6279 * Positive ack.
6230 6280 */
6231 6281 iocbp = (struct iocblk *)bp->b_rptr;
6232 6282
6233 6283 /*
6234 6284 * Set error if indicated.
6235 6285 */
6236 6286 if (iocbp->ioc_error) {
6237 6287 error = iocbp->ioc_error;
6238 6288 break;
6239 6289 }
6240 6290
6241 6291 /*
6242 6292 * Set return value.
6243 6293 */
6244 6294 *rvalp = iocbp->ioc_rval;
6245 6295
6246 6296 /*
6247 6297 * Data may have been returned in ACK message (ioc_count > 0).
6248 6298 * If so, copy it out to the user's buffer.
6249 6299 */
6250 6300 if (iocbp->ioc_count && !transparent) {
6251 6301 if (error = getiocd(bp, strioc->ic_dp, copyflag))
6252 6302 break;
6253 6303 }
6254 6304 if (!transparent) {
6255 6305 if (len) /* an M_COPYOUT was used with I_STR */
6256 6306 strioc->ic_len = len;
6257 6307 else
6258 6308 strioc->ic_len = (int)iocbp->ioc_count;
6259 6309 }
6260 6310 break;
6261 6311
6262 6312 case M_IOCNAK:
6263 6313 /*
6264 6314 * Negative ack.
6265 6315 *
6266 6316 * The only thing to do is set error as specified
6267 6317 * in neg ack packet.
6268 6318 */
6269 6319 iocbp = (struct iocblk *)bp->b_rptr;
6270 6320
6271 6321 error = (iocbp->ioc_error ? iocbp->ioc_error : EINVAL);
6272 6322 break;
6273 6323
6274 6324 case M_COPYIN:
6275 6325 /*
6276 6326 * Driver or module has requested user ioctl data.
6277 6327 */
6278 6328 reqp = (struct copyreq *)bp->b_rptr;
6279 6329
6280 6330 /*
6281 6331 * M_COPYIN should *never* have a message attached, though
6282 6332 * it's harmless if it does -- thus, panic on a DEBUG
6283 6333 * kernel and just free it on a non-DEBUG build.
6284 6334 */
6285 6335 ASSERT(bp->b_cont == NULL);
6286 6336 if (bp->b_cont != NULL) {
6287 6337 freemsg(bp->b_cont);
6288 6338 bp->b_cont = NULL;
6289 6339 }
6290 6340
6291 6341 error = putiocd(bp, reqp->cq_addr, flag, crp);
6292 6342 if (error && bp->b_cont) {
6293 6343 freemsg(bp->b_cont);
6294 6344 bp->b_cont = NULL;
6295 6345 }
6296 6346
6297 6347 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6298 6348 bp->b_datap->db_type = M_IOCDATA;
6299 6349
6300 6350 mblk_setcred(bp, crp, curproc->p_pid);
6301 6351 resp = (struct copyresp *)bp->b_rptr;
6302 6352 resp->cp_rval = (caddr_t)(uintptr_t)error;
6303 6353 resp->cp_flag = (fflags & FMODELS);
6304 6354
6305 6355 stream_willservice(stp);
6306 6356 putnext(stp->sd_wrq, bp);
6307 6357 stream_runservice(stp);
6308 6358
6309 6359 if (error) {
6310 6360 mutex_enter(&stp->sd_lock);
6311 6361 stp->sd_flag &= ~waitflags;
6312 6362 cv_broadcast(&stp->sd_iocmonitor);
6313 6363 mutex_exit(&stp->sd_lock);
6314 6364 crfree(crp);
6315 6365 return (error);
6316 6366 }
6317 6367
6318 6368 goto waitioc;
6319 6369
6320 6370 case M_COPYOUT:
6321 6371 /*
6322 6372 * Driver or module has ioctl data for a user.
6323 6373 */
6324 6374 reqp = (struct copyreq *)bp->b_rptr;
6325 6375 ASSERT(bp->b_cont != NULL);
6326 6376
6327 6377 /*
6328 6378 * Always (transparent or non-transparent )
6329 6379 * use the address specified in the request
6330 6380 */
6331 6381 taddr = reqp->cq_addr;
6332 6382 if (!transparent)
6333 6383 len = (int)reqp->cq_size;
6334 6384
6335 6385 /* copyout data to the provided address */
6336 6386 error = getiocd(bp, taddr, copyflag);
6337 6387
6338 6388 freemsg(bp->b_cont);
6339 6389 bp->b_cont = NULL;
6340 6390
6341 6391 bp->b_wptr = bp->b_rptr + sizeof (struct copyresp);
6342 6392 bp->b_datap->db_type = M_IOCDATA;
6343 6393
6344 6394 mblk_setcred(bp, crp, curproc->p_pid);
6345 6395 resp = (struct copyresp *)bp->b_rptr;
6346 6396 resp->cp_rval = (caddr_t)(uintptr_t)error;
6347 6397 resp->cp_flag = (fflags & FMODELS);
6348 6398
6349 6399 stream_willservice(stp);
6350 6400 putnext(stp->sd_wrq, bp);
6351 6401 stream_runservice(stp);
6352 6402
6353 6403 if (error) {
6354 6404 mutex_enter(&stp->sd_lock);
6355 6405 stp->sd_flag &= ~waitflags;
6356 6406 cv_broadcast(&stp->sd_iocmonitor);
6357 6407 mutex_exit(&stp->sd_lock);
6358 6408 crfree(crp);
6359 6409 return (error);
6360 6410 }
6361 6411 goto waitioc;
6362 6412
6363 6413 default:
6364 6414 ASSERT(0);
6365 6415 mutex_enter(&stp->sd_lock);
6366 6416 stp->sd_flag &= ~waitflags;
6367 6417 cv_broadcast(&stp->sd_iocmonitor);
6368 6418 mutex_exit(&stp->sd_lock);
6369 6419 break;
6370 6420 }
6371 6421
6372 6422 freemsg(bp);
6373 6423 crfree(crp);
6374 6424 return (error);
6375 6425 }
6376 6426
6377 6427 /*
6378 6428 * Send an M_CMD message downstream and wait for a reply. This is a ptools
6379 6429 * special used to retrieve information from modules/drivers a stream without
6380 6430 * being subjected to flow control or interfering with pending messages on the
6381 6431 * stream (e.g. an ioctl in flight).
6382 6432 */
6383 6433 int
6384 6434 strdocmd(struct stdata *stp, struct strcmd *scp, cred_t *crp)
6385 6435 {
6386 6436 mblk_t *mp;
6387 6437 struct cmdblk *cmdp;
6388 6438 int error = 0;
6389 6439 int errs = STRHUP|STRDERR|STWRERR|STPLEX;
6390 6440 clock_t rval, timeout = STRTIMOUT;
6391 6441
6392 6442 if (scp->sc_len < 0 || scp->sc_len > sizeof (scp->sc_buf) ||
6393 6443 scp->sc_timeout < -1)
6394 6444 return (EINVAL);
6395 6445
6396 6446 if (scp->sc_timeout > 0)
6397 6447 timeout = scp->sc_timeout * MILLISEC;
6398 6448
6399 6449 if ((mp = allocb_cred(sizeof (struct cmdblk), crp,
6400 6450 curproc->p_pid)) == NULL)
6401 6451 return (ENOMEM);
6402 6452
6403 6453 crhold(crp);
6404 6454
6405 6455 cmdp = (struct cmdblk *)mp->b_wptr;
6406 6456 cmdp->cb_cr = crp;
6407 6457 cmdp->cb_cmd = scp->sc_cmd;
6408 6458 cmdp->cb_len = scp->sc_len;
6409 6459 cmdp->cb_error = 0;
6410 6460 mp->b_wptr += sizeof (struct cmdblk);
6411 6461
6412 6462 DB_TYPE(mp) = M_CMD;
6413 6463 DB_CPID(mp) = curproc->p_pid;
6414 6464
6415 6465 /*
6416 6466 * Copy in the payload.
6417 6467 */
6418 6468 if (cmdp->cb_len > 0) {
6419 6469 mp->b_cont = allocb_cred(sizeof (scp->sc_buf), crp,
6420 6470 curproc->p_pid);
6421 6471 if (mp->b_cont == NULL) {
6422 6472 error = ENOMEM;
6423 6473 goto out;
6424 6474 }
6425 6475
6426 6476 /* cb_len comes from sc_len, which has already been checked */
6427 6477 ASSERT(cmdp->cb_len <= sizeof (scp->sc_buf));
6428 6478 (void) bcopy(scp->sc_buf, mp->b_cont->b_wptr, cmdp->cb_len);
6429 6479 mp->b_cont->b_wptr += cmdp->cb_len;
6430 6480 DB_CPID(mp->b_cont) = curproc->p_pid;
6431 6481 }
6432 6482
6433 6483 /*
6434 6484 * Since this mechanism is strictly for ptools, and since only one
6435 6485 * process can be grabbed at a time, we simply fail if there's
6436 6486 * currently an operation pending.
6437 6487 */
6438 6488 mutex_enter(&stp->sd_lock);
6439 6489 if (stp->sd_flag & STRCMDWAIT) {
6440 6490 mutex_exit(&stp->sd_lock);
6441 6491 error = EBUSY;
6442 6492 goto out;
6443 6493 }
6444 6494 stp->sd_flag |= STRCMDWAIT;
6445 6495 ASSERT(stp->sd_cmdblk == NULL);
6446 6496 mutex_exit(&stp->sd_lock);
6447 6497
6448 6498 putnext(stp->sd_wrq, mp);
6449 6499 mp = NULL;
6450 6500
6451 6501 /*
6452 6502 * Timed wait for acknowledgment. If the reply has already arrived,
6453 6503 * don't sleep. If awakened from the sleep, fail only if the reply
6454 6504 * has not arrived by then. Otherwise, process the reply.
6455 6505 */
6456 6506 mutex_enter(&stp->sd_lock);
6457 6507 while (stp->sd_cmdblk == NULL) {
6458 6508 if (stp->sd_flag & errs) {
6459 6509 if ((error = strgeterr(stp, errs, 0)) != 0)
6460 6510 goto waitout;
6461 6511 }
6462 6512
6463 6513 rval = str_cv_wait(&stp->sd_monitor, &stp->sd_lock, timeout, 0);
6464 6514 if (stp->sd_cmdblk != NULL)
6465 6515 break;
6466 6516
6467 6517 if (rval <= 0) {
6468 6518 error = (rval == 0) ? EINTR : ETIME;
6469 6519 goto waitout;
6470 6520 }
6471 6521 }
6472 6522
6473 6523 /*
6474 6524 * We received a reply.
6475 6525 */
6476 6526 mp = stp->sd_cmdblk;
6477 6527 stp->sd_cmdblk = NULL;
6478 6528 ASSERT(mp != NULL && DB_TYPE(mp) == M_CMD);
6479 6529 ASSERT(stp->sd_flag & STRCMDWAIT);
6480 6530 stp->sd_flag &= ~STRCMDWAIT;
6481 6531 mutex_exit(&stp->sd_lock);
6482 6532
6483 6533 cmdp = (struct cmdblk *)mp->b_rptr;
6484 6534 if ((error = cmdp->cb_error) != 0)
6485 6535 goto out;
6486 6536
6487 6537 /*
6488 6538 * Data may have been returned in the reply (cb_len > 0).
6489 6539 * If so, copy it out to the user's buffer.
6490 6540 */
6491 6541 if (cmdp->cb_len > 0) {
6492 6542 if (mp->b_cont == NULL || MBLKL(mp->b_cont) < cmdp->cb_len) {
6493 6543 error = EPROTO;
6494 6544 goto out;
6495 6545 }
6496 6546
6497 6547 cmdp->cb_len = MIN(cmdp->cb_len, sizeof (scp->sc_buf));
6498 6548 (void) bcopy(mp->b_cont->b_rptr, scp->sc_buf, cmdp->cb_len);
6499 6549 }
6500 6550 scp->sc_len = cmdp->cb_len;
6501 6551 out:
6502 6552 freemsg(mp);
6503 6553 crfree(crp);
6504 6554 return (error);
6505 6555 waitout:
6506 6556 ASSERT(stp->sd_cmdblk == NULL);
6507 6557 stp->sd_flag &= ~STRCMDWAIT;
6508 6558 mutex_exit(&stp->sd_lock);
6509 6559 crfree(crp);
6510 6560 return (error);
6511 6561 }
6512 6562
6513 6563 /*
6514 6564 * For the SunOS keyboard driver.
6515 6565 * Return the next available "ioctl" sequence number.
6516 6566 * Exported, so that streams modules can send "ioctl" messages
6517 6567 * downstream from their open routine.
6518 6568 */
6519 6569 int
6520 6570 getiocseqno(void)
6521 6571 {
6522 6572 int i;
6523 6573
6524 6574 mutex_enter(&strresources);
6525 6575 i = ++ioc_id;
6526 6576 mutex_exit(&strresources);
6527 6577 return (i);
6528 6578 }
6529 6579
6530 6580 /*
6531 6581 * Get the next message from the read queue. If the message is
6532 6582 * priority, STRPRI will have been set by strrput(). This flag
6533 6583 * should be reset only when the entire message at the front of the
6534 6584 * queue as been consumed.
6535 6585 *
6536 6586 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
6537 6587 */
6538 6588 int
6539 6589 strgetmsg(
6540 6590 struct vnode *vp,
6541 6591 struct strbuf *mctl,
6542 6592 struct strbuf *mdata,
6543 6593 unsigned char *prip,
6544 6594 int *flagsp,
6545 6595 int fmode,
6546 6596 rval_t *rvp)
6547 6597 {
6548 6598 struct stdata *stp;
6549 6599 mblk_t *bp, *nbp;
6550 6600 mblk_t *savemp = NULL;
6551 6601 mblk_t *savemptail = NULL;
6552 6602 uint_t old_sd_flag;
6553 6603 int flg = MSG_BAND;
6554 6604 int more = 0;
6555 6605 int error = 0;
6556 6606 char first = 1;
6557 6607 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
6558 6608 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
6559 6609 unsigned char pri = 0;
6560 6610 queue_t *q;
6561 6611 int pr = 0; /* Partial read successful */
6562 6612 struct uio uios;
6563 6613 struct uio *uiop = &uios;
6564 6614 struct iovec iovs;
6565 6615 unsigned char type;
6566 6616
6567 6617 TRACE_1(TR_FAC_STREAMS_FR, TR_STRGETMSG_ENTER,
6568 6618 "strgetmsg:%p", vp);
6569 6619
6570 6620 ASSERT(vp->v_stream);
6571 6621 stp = vp->v_stream;
6572 6622 rvp->r_val1 = 0;
6573 6623
6574 6624 mutex_enter(&stp->sd_lock);
6575 6625
6576 6626 if ((error = i_straccess(stp, JCREAD)) != 0) {
6577 6627 mutex_exit(&stp->sd_lock);
6578 6628 return (error);
6579 6629 }
6580 6630
6581 6631 if (stp->sd_flag & (STRDERR|STPLEX)) {
6582 6632 error = strgeterr(stp, STRDERR|STPLEX, 0);
6583 6633 if (error != 0) {
6584 6634 mutex_exit(&stp->sd_lock);
6585 6635 return (error);
6586 6636 }
6587 6637 }
6588 6638 mutex_exit(&stp->sd_lock);
6589 6639
6590 6640 switch (*flagsp) {
6591 6641 case MSG_HIPRI:
6592 6642 if (*prip != 0)
6593 6643 return (EINVAL);
6594 6644 break;
6595 6645
6596 6646 case MSG_ANY:
6597 6647 case MSG_BAND:
6598 6648 break;
6599 6649
6600 6650 default:
6601 6651 return (EINVAL);
6602 6652 }
6603 6653 /*
6604 6654 * Setup uio and iov for data part
6605 6655 */
6606 6656 iovs.iov_base = mdata->buf;
6607 6657 iovs.iov_len = mdata->maxlen;
6608 6658 uios.uio_iov = &iovs;
6609 6659 uios.uio_iovcnt = 1;
6610 6660 uios.uio_loffset = 0;
6611 6661 uios.uio_segflg = UIO_USERSPACE;
6612 6662 uios.uio_fmode = 0;
6613 6663 uios.uio_extflg = UIO_COPY_CACHED;
6614 6664 uios.uio_resid = mdata->maxlen;
6615 6665 uios.uio_offset = 0;
6616 6666
6617 6667 q = _RD(stp->sd_wrq);
6618 6668 mutex_enter(&stp->sd_lock);
6619 6669 old_sd_flag = stp->sd_flag;
6620 6670 mark = 0;
6621 6671 for (;;) {
6622 6672 int done = 0;
6623 6673 mblk_t *q_first = q->q_first;
6624 6674
6625 6675 /*
6626 6676 * Get the next message of appropriate priority
6627 6677 * from the stream head. If the caller is interested
6628 6678 * in band or hipri messages, then they should already
6629 6679 * be enqueued at the stream head. On the other hand
6630 6680 * if the caller wants normal (band 0) messages, they
6631 6681 * might be deferred in a synchronous stream and they
6632 6682 * will need to be pulled up.
6633 6683 *
6634 6684 * After we have dequeued a message, we might find that
6635 6685 * it was a deferred M_SIG that was enqueued at the
6636 6686 * stream head. It must now be posted as part of the
6637 6687 * read by calling strsignal_nolock().
6638 6688 *
6639 6689 * Also note that strrput does not enqueue an M_PCSIG,
6640 6690 * and there cannot be more than one hipri message,
6641 6691 * so there was no need to have the M_PCSIG case.
6642 6692 *
6643 6693 * At some time it might be nice to try and wrap the
6644 6694 * functionality of kstrgetmsg() and strgetmsg() into
6645 6695 * a common routine so to reduce the amount of replicated
6646 6696 * code (since they are extremely similar).
6647 6697 */
6648 6698 if (!(*flagsp & (MSG_HIPRI|MSG_BAND))) {
6649 6699 /* Asking for normal, band0 data */
6650 6700 bp = strget(stp, q, uiop, first, &error);
6651 6701 ASSERT(MUTEX_HELD(&stp->sd_lock));
6652 6702 if (bp != NULL) {
6653 6703 if (DB_TYPE(bp) == M_SIG) {
6654 6704 strsignal_nolock(stp, *bp->b_rptr,
6655 6705 bp->b_band);
6656 6706 freemsg(bp);
6657 6707 continue;
6658 6708 } else {
6659 6709 break;
6660 6710 }
6661 6711 }
6662 6712 if (error != 0)
6663 6713 goto getmout;
6664 6714
6665 6715 /*
6666 6716 * We can't depend on the value of STRPRI here because
6667 6717 * the stream head may be in transit. Therefore, we
6668 6718 * must look at the type of the first message to
6669 6719 * determine if a high priority messages is waiting
6670 6720 */
6671 6721 } else if ((*flagsp & MSG_HIPRI) && q_first != NULL &&
6672 6722 DB_TYPE(q_first) >= QPCTL &&
6673 6723 (bp = getq_noenab(q, 0)) != NULL) {
6674 6724 /* Asked for HIPRI and got one */
6675 6725 ASSERT(DB_TYPE(bp) >= QPCTL);
6676 6726 break;
6677 6727 } else if ((*flagsp & MSG_BAND) && q_first != NULL &&
6678 6728 ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
6679 6729 (bp = getq_noenab(q, 0)) != NULL) {
6680 6730 /*
6681 6731 * Asked for at least band "prip" and got either at
6682 6732 * least that band or a hipri message.
6683 6733 */
6684 6734 ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
6685 6735 if (DB_TYPE(bp) == M_SIG) {
6686 6736 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
6687 6737 freemsg(bp);
6688 6738 continue;
6689 6739 } else {
6690 6740 break;
6691 6741 }
6692 6742 }
6693 6743
6694 6744 /* No data. Time to sleep? */
6695 6745 qbackenable(q, 0);
6696 6746
6697 6747 /*
6698 6748 * If STRHUP or STREOF, return 0 length control and data.
6699 6749 * If resid is 0, then a read(fd,buf,0) was done. Do not
6700 6750 * sleep to satisfy this request because by default we have
6701 6751 * zero bytes to return.
6702 6752 */
6703 6753 if ((stp->sd_flag & (STRHUP|STREOF)) || (mctl->maxlen == 0 &&
6704 6754 mdata->maxlen == 0)) {
6705 6755 mctl->len = mdata->len = 0;
6706 6756 *flagsp = 0;
6707 6757 mutex_exit(&stp->sd_lock);
6708 6758 return (0);
6709 6759 }
6710 6760 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_WAIT,
6711 6761 "strgetmsg calls strwaitq:%p, %p",
6712 6762 vp, uiop);
6713 6763 if (((error = strwaitq(stp, GETWAIT, (ssize_t)0, fmode, -1,
6714 6764 &done)) != 0) || done) {
6715 6765 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_DONE,
6716 6766 "strgetmsg error or done:%p, %p",
6717 6767 vp, uiop);
6718 6768 mutex_exit(&stp->sd_lock);
6719 6769 return (error);
6720 6770 }
6721 6771 TRACE_2(TR_FAC_STREAMS_FR, TR_STRGETMSG_AWAKE,
6722 6772 "strgetmsg awakes:%p, %p", vp, uiop);
6723 6773 if ((error = i_straccess(stp, JCREAD)) != 0) {
6724 6774 mutex_exit(&stp->sd_lock);
6725 6775 return (error);
6726 6776 }
6727 6777 first = 0;
6728 6778 }
6729 6779 ASSERT(bp != NULL);
6730 6780 /*
6731 6781 * Extract any mark information. If the message is not completely
6732 6782 * consumed this information will be put in the mblk
6733 6783 * that is putback.
6734 6784 * If MSGMARKNEXT is set and the message is completely consumed
6735 6785 * the STRATMARK flag will be set below. Likewise, if
6736 6786 * MSGNOTMARKNEXT is set and the message is
6737 6787 * completely consumed STRNOTATMARK will be set.
6738 6788 */
6739 6789 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
6740 6790 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
6741 6791 (MSGMARKNEXT|MSGNOTMARKNEXT));
6742 6792 if (mark != 0 && bp == stp->sd_mark) {
6743 6793 mark |= _LASTMARK;
6744 6794 stp->sd_mark = NULL;
6745 6795 }
6746 6796 /*
6747 6797 * keep track of the original message type and priority
6748 6798 */
6749 6799 pri = bp->b_band;
6750 6800 type = bp->b_datap->db_type;
6751 6801 if (type == M_PASSFP) {
6752 6802 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
6753 6803 stp->sd_mark = bp;
6754 6804 bp->b_flag |= mark & ~_LASTMARK;
6755 6805 putback(stp, q, bp, pri);
6756 6806 qbackenable(q, pri);
6757 6807 mutex_exit(&stp->sd_lock);
6758 6808 return (EBADMSG);
6759 6809 }
6760 6810 ASSERT(type != M_SIG);
6761 6811
6762 6812 /*
6763 6813 * Set this flag so strrput will not generate signals. Need to
6764 6814 * make sure this flag is cleared before leaving this routine
6765 6815 * else signals will stop being sent.
6766 6816 */
6767 6817 stp->sd_flag |= STRGETINPROG;
6768 6818 mutex_exit(&stp->sd_lock);
6769 6819
6770 6820 if (STREAM_NEEDSERVICE(stp))
6771 6821 stream_runservice(stp);
6772 6822
6773 6823 /*
6774 6824 * Set HIPRI flag if message is priority.
6775 6825 */
6776 6826 if (type >= QPCTL)
6777 6827 flg = MSG_HIPRI;
6778 6828 else
6779 6829 flg = MSG_BAND;
6780 6830
6781 6831 /*
6782 6832 * First process PROTO or PCPROTO blocks, if any.
6783 6833 */
6784 6834 if (mctl->maxlen >= 0 && type != M_DATA) {
6785 6835 size_t n, bcnt;
6786 6836 char *ubuf;
6787 6837
6788 6838 bcnt = mctl->maxlen;
6789 6839 ubuf = mctl->buf;
6790 6840 while (bp != NULL && bp->b_datap->db_type != M_DATA) {
6791 6841 if ((n = MIN(bcnt, bp->b_wptr - bp->b_rptr)) != 0 &&
6792 6842 copyout(bp->b_rptr, ubuf, n)) {
6793 6843 error = EFAULT;
6794 6844 mutex_enter(&stp->sd_lock);
6795 6845 /*
6796 6846 * clear stream head pri flag based on
6797 6847 * first message type
6798 6848 */
6799 6849 if (type >= QPCTL) {
6800 6850 ASSERT(type == M_PCPROTO);
6801 6851 stp->sd_flag &= ~STRPRI;
6802 6852 }
6803 6853 more = 0;
6804 6854 freemsg(bp);
6805 6855 goto getmout;
6806 6856 }
6807 6857 ubuf += n;
6808 6858 bp->b_rptr += n;
6809 6859 if (bp->b_rptr >= bp->b_wptr) {
6810 6860 nbp = bp;
6811 6861 bp = bp->b_cont;
6812 6862 freeb(nbp);
6813 6863 }
6814 6864 ASSERT(n <= bcnt);
6815 6865 bcnt -= n;
6816 6866 if (bcnt == 0)
6817 6867 break;
6818 6868 }
6819 6869 mctl->len = mctl->maxlen - bcnt;
6820 6870 } else
6821 6871 mctl->len = -1;
6822 6872
6823 6873 if (bp && bp->b_datap->db_type != M_DATA) {
6824 6874 /*
6825 6875 * More PROTO blocks in msg.
6826 6876 */
6827 6877 more |= MORECTL;
6828 6878 savemp = bp;
6829 6879 while (bp && bp->b_datap->db_type != M_DATA) {
6830 6880 savemptail = bp;
6831 6881 bp = bp->b_cont;
6832 6882 }
6833 6883 savemptail->b_cont = NULL;
6834 6884 }
6835 6885
6836 6886 /*
6837 6887 * Now process DATA blocks, if any.
6838 6888 */
6839 6889 if (mdata->maxlen >= 0 && bp) {
6840 6890 /*
6841 6891 * struiocopyout will consume a potential zero-length
6842 6892 * M_DATA even if uio_resid is zero.
6843 6893 */
6844 6894 size_t oldresid = uiop->uio_resid;
6845 6895
6846 6896 bp = struiocopyout(bp, uiop, &error);
6847 6897 if (error != 0) {
6848 6898 mutex_enter(&stp->sd_lock);
6849 6899 /*
6850 6900 * clear stream head hi pri flag based on
6851 6901 * first message
6852 6902 */
6853 6903 if (type >= QPCTL) {
6854 6904 ASSERT(type == M_PCPROTO);
6855 6905 stp->sd_flag &= ~STRPRI;
6856 6906 }
6857 6907 more = 0;
6858 6908 freemsg(savemp);
6859 6909 goto getmout;
6860 6910 }
6861 6911 /*
6862 6912 * (pr == 1) indicates a partial read.
6863 6913 */
6864 6914 if (oldresid > uiop->uio_resid)
6865 6915 pr = 1;
6866 6916 mdata->len = mdata->maxlen - uiop->uio_resid;
6867 6917 } else
6868 6918 mdata->len = -1;
6869 6919
6870 6920 if (bp) { /* more data blocks in msg */
6871 6921 more |= MOREDATA;
6872 6922 if (savemp)
6873 6923 savemptail->b_cont = bp;
6874 6924 else
6875 6925 savemp = bp;
6876 6926 }
6877 6927
6878 6928 mutex_enter(&stp->sd_lock);
6879 6929 if (savemp) {
6880 6930 if (pr && (savemp->b_datap->db_type == M_DATA) &&
6881 6931 msgnodata(savemp)) {
6882 6932 /*
6883 6933 * Avoid queuing a zero-length tail part of
6884 6934 * a message. pr=1 indicates that we read some of
6885 6935 * the message.
6886 6936 */
6887 6937 freemsg(savemp);
6888 6938 more &= ~MOREDATA;
6889 6939 /*
6890 6940 * clear stream head hi pri flag based on
6891 6941 * first message
6892 6942 */
6893 6943 if (type >= QPCTL) {
6894 6944 ASSERT(type == M_PCPROTO);
6895 6945 stp->sd_flag &= ~STRPRI;
6896 6946 }
6897 6947 } else {
6898 6948 savemp->b_band = pri;
6899 6949 /*
6900 6950 * If the first message was HIPRI and the one we're
6901 6951 * putting back isn't, then clear STRPRI, otherwise
6902 6952 * set STRPRI again. Note that we must set STRPRI
6903 6953 * again since the flush logic in strrput_nondata()
6904 6954 * may have cleared it while we had sd_lock dropped.
6905 6955 */
6906 6956 if (type >= QPCTL) {
6907 6957 ASSERT(type == M_PCPROTO);
6908 6958 if (queclass(savemp) < QPCTL)
6909 6959 stp->sd_flag &= ~STRPRI;
6910 6960 else
6911 6961 stp->sd_flag |= STRPRI;
6912 6962 } else if (queclass(savemp) >= QPCTL) {
6913 6963 /*
6914 6964 * The first message was not a HIPRI message,
6915 6965 * but the one we are about to putback is.
6916 6966 * For simplicitly, we do not allow for HIPRI
6917 6967 * messages to be embedded in the message
6918 6968 * body, so just force it to same type as
6919 6969 * first message.
6920 6970 */
6921 6971 ASSERT(type == M_DATA || type == M_PROTO);
6922 6972 ASSERT(savemp->b_datap->db_type == M_PCPROTO);
6923 6973 savemp->b_datap->db_type = type;
6924 6974 }
6925 6975 if (mark != 0) {
6926 6976 savemp->b_flag |= mark & ~_LASTMARK;
6927 6977 if ((mark & _LASTMARK) &&
6928 6978 (stp->sd_mark == NULL)) {
6929 6979 /*
6930 6980 * If another marked message arrived
6931 6981 * while sd_lock was not held sd_mark
6932 6982 * would be non-NULL.
6933 6983 */
6934 6984 stp->sd_mark = savemp;
6935 6985 }
6936 6986 }
6937 6987 putback(stp, q, savemp, pri);
6938 6988 }
6939 6989 } else {
6940 6990 /*
6941 6991 * The complete message was consumed.
6942 6992 *
6943 6993 * If another M_PCPROTO arrived while sd_lock was not held
6944 6994 * it would have been discarded since STRPRI was still set.
6945 6995 *
6946 6996 * Move the MSG*MARKNEXT information
6947 6997 * to the stream head just in case
6948 6998 * the read queue becomes empty.
6949 6999 * clear stream head hi pri flag based on
6950 7000 * first message
6951 7001 *
6952 7002 * If the stream head was at the mark
6953 7003 * (STRATMARK) before we dropped sd_lock above
6954 7004 * and some data was consumed then we have
6955 7005 * moved past the mark thus STRATMARK is
6956 7006 * cleared. However, if a message arrived in
6957 7007 * strrput during the copyout above causing
6958 7008 * STRATMARK to be set we can not clear that
6959 7009 * flag.
6960 7010 */
6961 7011 if (type >= QPCTL) {
6962 7012 ASSERT(type == M_PCPROTO);
6963 7013 stp->sd_flag &= ~STRPRI;
6964 7014 }
6965 7015 if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
6966 7016 if (mark & MSGMARKNEXT) {
6967 7017 stp->sd_flag &= ~STRNOTATMARK;
6968 7018 stp->sd_flag |= STRATMARK;
6969 7019 } else if (mark & MSGNOTMARKNEXT) {
6970 7020 stp->sd_flag &= ~STRATMARK;
6971 7021 stp->sd_flag |= STRNOTATMARK;
6972 7022 } else {
6973 7023 stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
6974 7024 }
6975 7025 } else if (pr && (old_sd_flag & STRATMARK)) {
6976 7026 stp->sd_flag &= ~STRATMARK;
6977 7027 }
6978 7028 }
6979 7029
6980 7030 *flagsp = flg;
6981 7031 *prip = pri;
6982 7032
6983 7033 /*
6984 7034 * Getmsg cleanup processing - if the state of the queue has changed
6985 7035 * some signals may need to be sent and/or poll awakened.
6986 7036 */
6987 7037 getmout:
6988 7038 qbackenable(q, pri);
6989 7039
6990 7040 /*
6991 7041 * We dropped the stream head lock above. Send all M_SIG messages
6992 7042 * before processing stream head for SIGPOLL messages.
6993 7043 */
6994 7044 ASSERT(MUTEX_HELD(&stp->sd_lock));
6995 7045 while ((bp = q->q_first) != NULL &&
6996 7046 (bp->b_datap->db_type == M_SIG)) {
6997 7047 /*
6998 7048 * sd_lock is held so the content of the read queue can not
6999 7049 * change.
7000 7050 */
7001 7051 bp = getq(q);
7002 7052 ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7003 7053
7004 7054 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7005 7055 mutex_exit(&stp->sd_lock);
7006 7056 freemsg(bp);
7007 7057 if (STREAM_NEEDSERVICE(stp))
7008 7058 stream_runservice(stp);
7009 7059 mutex_enter(&stp->sd_lock);
7010 7060 }
7011 7061
7012 7062 /*
7013 7063 * stream head cannot change while we make the determination
7014 7064 * whether or not to send a signal. Drop the flag to allow strrput
7015 7065 * to send firstmsgsigs again.
7016 7066 */
7017 7067 stp->sd_flag &= ~STRGETINPROG;
7018 7068
7019 7069 /*
7020 7070 * If the type of message at the front of the queue changed
7021 7071 * due to the receive the appropriate signals and pollwakeup events
7022 7072 * are generated. The type of changes are:
7023 7073 * Processed a hipri message, q_first is not hipri.
7024 7074 * Processed a band X message, and q_first is band Y.
7025 7075 * The generated signals and pollwakeups are identical to what
7026 7076 * strrput() generates should the message that is now on q_first
7027 7077 * arrive to an empty read queue.
7028 7078 *
7029 7079 * Note: only strrput will send a signal for a hipri message.
7030 7080 */
7031 7081 if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7032 7082 strsigset_t signals = 0;
7033 7083 strpollset_t pollwakeups = 0;
7034 7084
7035 7085 if (flg & MSG_HIPRI) {
7036 7086 /*
7037 7087 * Removed a hipri message. Regular data at
7038 7088 * the front of the queue.
7039 7089 */
7040 7090 if (bp->b_band == 0) {
7041 7091 signals = S_INPUT | S_RDNORM;
7042 7092 pollwakeups = POLLIN | POLLRDNORM;
7043 7093 } else {
7044 7094 signals = S_INPUT | S_RDBAND;
7045 7095 pollwakeups = POLLIN | POLLRDBAND;
7046 7096 }
7047 7097 } else if (pri != bp->b_band) {
7048 7098 /*
7049 7099 * The band is different for the new q_first.
7050 7100 */
7051 7101 if (bp->b_band == 0) {
7052 7102 signals = S_RDNORM;
7053 7103 pollwakeups = POLLIN | POLLRDNORM;
7054 7104 } else {
7055 7105 signals = S_RDBAND;
7056 7106 pollwakeups = POLLIN | POLLRDBAND;
7057 7107 }
7058 7108 }
7059 7109
7060 7110 if (pollwakeups != 0) {
7061 7111 if (pollwakeups == (POLLIN | POLLRDNORM)) {
7062 7112 if (!(stp->sd_rput_opt & SR_POLLIN))
7063 7113 goto no_pollwake;
7064 7114 stp->sd_rput_opt &= ~SR_POLLIN;
7065 7115 }
7066 7116 mutex_exit(&stp->sd_lock);
7067 7117 pollwakeup(&stp->sd_pollist, pollwakeups);
7068 7118 mutex_enter(&stp->sd_lock);
7069 7119 }
7070 7120 no_pollwake:
7071 7121
7072 7122 if (stp->sd_sigflags & signals)
7073 7123 strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7074 7124 }
7075 7125 mutex_exit(&stp->sd_lock);
7076 7126
7077 7127 rvp->r_val1 = more;
7078 7128 return (error);
7079 7129 #undef _LASTMARK
7080 7130 }
7081 7131
7082 7132 /*
7083 7133 * Get the next message from the read queue. If the message is
7084 7134 * priority, STRPRI will have been set by strrput(). This flag
7085 7135 * should be reset only when the entire message at the front of the
7086 7136 * queue as been consumed.
7087 7137 *
7088 7138 * If uiop is NULL all data is returned in mctlp.
7089 7139 * Note that a NULL uiop implies that FNDELAY and FNONBLOCK are assumed
7090 7140 * not enabled.
7091 7141 * The timeout parameter is in milliseconds; -1 for infinity.
7092 7142 * This routine handles the consolidation private flags:
7093 7143 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7094 7144 * MSG_DELAYERROR Defer the error check until the queue is empty.
7095 7145 * MSG_HOLDSIG Hold signals while waiting for data.
7096 7146 * MSG_IPEEK Only peek at messages.
7097 7147 * MSG_DISCARDTAIL Discard the tail M_DATA part of the message
7098 7148 * that doesn't fit.
7099 7149 * MSG_NOMARK If the message is marked leave it on the queue.
7100 7150 *
7101 7151 * NOTE: strgetmsg and kstrgetmsg have much of the logic in common.
7102 7152 */
7103 7153 int
7104 7154 kstrgetmsg(
7105 7155 struct vnode *vp,
7106 7156 mblk_t **mctlp,
7107 7157 struct uio *uiop,
7108 7158 unsigned char *prip,
7109 7159 int *flagsp,
7110 7160 clock_t timout,
7111 7161 rval_t *rvp)
7112 7162 {
7113 7163 struct stdata *stp;
7114 7164 mblk_t *bp, *nbp;
7115 7165 mblk_t *savemp = NULL;
7116 7166 mblk_t *savemptail = NULL;
7117 7167 int flags;
7118 7168 uint_t old_sd_flag;
7119 7169 int flg = MSG_BAND;
7120 7170 int more = 0;
7121 7171 int error = 0;
7122 7172 char first = 1;
7123 7173 uint_t mark; /* Contains MSG*MARK and _LASTMARK */
7124 7174 #define _LASTMARK 0x8000 /* Distinct from MSG*MARK */
7125 7175 unsigned char pri = 0;
7126 7176 queue_t *q;
7127 7177 int pr = 0; /* Partial read successful */
7128 7178 unsigned char type;
7129 7179
7130 7180 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_ENTER,
7131 7181 "kstrgetmsg:%p", vp);
7132 7182
7133 7183 ASSERT(vp->v_stream);
7134 7184 stp = vp->v_stream;
7135 7185 rvp->r_val1 = 0;
7136 7186
7137 7187 mutex_enter(&stp->sd_lock);
7138 7188
7139 7189 if ((error = i_straccess(stp, JCREAD)) != 0) {
7140 7190 mutex_exit(&stp->sd_lock);
7141 7191 return (error);
7142 7192 }
7143 7193
7144 7194 flags = *flagsp;
7145 7195 if (stp->sd_flag & (STRDERR|STPLEX)) {
7146 7196 if ((stp->sd_flag & STPLEX) ||
7147 7197 (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == 0) {
7148 7198 error = strgeterr(stp, STRDERR|STPLEX,
7149 7199 (flags & MSG_IPEEK));
7150 7200 if (error != 0) {
7151 7201 mutex_exit(&stp->sd_lock);
7152 7202 return (error);
7153 7203 }
7154 7204 }
7155 7205 }
7156 7206 mutex_exit(&stp->sd_lock);
7157 7207
7158 7208 switch (flags & (MSG_HIPRI|MSG_ANY|MSG_BAND)) {
7159 7209 case MSG_HIPRI:
7160 7210 if (*prip != 0)
7161 7211 return (EINVAL);
7162 7212 break;
7163 7213
7164 7214 case MSG_ANY:
7165 7215 case MSG_BAND:
7166 7216 break;
7167 7217
7168 7218 default:
7169 7219 return (EINVAL);
7170 7220 }
7171 7221
7172 7222 retry:
7173 7223 q = _RD(stp->sd_wrq);
7174 7224 mutex_enter(&stp->sd_lock);
7175 7225 old_sd_flag = stp->sd_flag;
7176 7226 mark = 0;
7177 7227 for (;;) {
7178 7228 int done = 0;
7179 7229 int waitflag;
7180 7230 int fmode;
7181 7231 mblk_t *q_first = q->q_first;
7182 7232
7183 7233 /*
7184 7234 * This section of the code operates just like the code
7185 7235 * in strgetmsg(). There is a comment there about what
7186 7236 * is going on here.
7187 7237 */
7188 7238 if (!(flags & (MSG_HIPRI|MSG_BAND))) {
7189 7239 /* Asking for normal, band0 data */
7190 7240 bp = strget(stp, q, uiop, first, &error);
7191 7241 ASSERT(MUTEX_HELD(&stp->sd_lock));
7192 7242 if (bp != NULL) {
7193 7243 if (DB_TYPE(bp) == M_SIG) {
7194 7244 strsignal_nolock(stp, *bp->b_rptr,
7195 7245 bp->b_band);
7196 7246 freemsg(bp);
7197 7247 continue;
7198 7248 } else {
7199 7249 break;
7200 7250 }
7201 7251 }
7202 7252 if (error != 0) {
7203 7253 goto getmout;
7204 7254 }
7205 7255 /*
7206 7256 * We can't depend on the value of STRPRI here because
7207 7257 * the stream head may be in transit. Therefore, we
7208 7258 * must look at the type of the first message to
7209 7259 * determine if a high priority messages is waiting
7210 7260 */
7211 7261 } else if ((flags & MSG_HIPRI) && q_first != NULL &&
7212 7262 DB_TYPE(q_first) >= QPCTL &&
7213 7263 (bp = getq_noenab(q, 0)) != NULL) {
7214 7264 ASSERT(DB_TYPE(bp) >= QPCTL);
7215 7265 break;
7216 7266 } else if ((flags & MSG_BAND) && q_first != NULL &&
7217 7267 ((q_first->b_band >= *prip) || DB_TYPE(q_first) >= QPCTL) &&
7218 7268 (bp = getq_noenab(q, 0)) != NULL) {
7219 7269 /*
7220 7270 * Asked for at least band "prip" and got either at
7221 7271 * least that band or a hipri message.
7222 7272 */
7223 7273 ASSERT(bp->b_band >= *prip || DB_TYPE(bp) >= QPCTL);
7224 7274 if (DB_TYPE(bp) == M_SIG) {
7225 7275 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7226 7276 freemsg(bp);
7227 7277 continue;
7228 7278 } else {
7229 7279 break;
7230 7280 }
7231 7281 }
7232 7282
7233 7283 /* No data. Time to sleep? */
7234 7284 qbackenable(q, 0);
7235 7285
7236 7286 /*
7237 7287 * Delayed error notification?
7238 7288 */
7239 7289 if ((stp->sd_flag & (STRDERR|STPLEX)) &&
7240 7290 (flags & (MSG_IGNERROR|MSG_DELAYERROR)) == MSG_DELAYERROR) {
7241 7291 error = strgeterr(stp, STRDERR|STPLEX,
7242 7292 (flags & MSG_IPEEK));
7243 7293 if (error != 0) {
7244 7294 mutex_exit(&stp->sd_lock);
7245 7295 return (error);
7246 7296 }
7247 7297 }
7248 7298
7249 7299 /*
7250 7300 * If STRHUP or STREOF, return 0 length control and data.
7251 7301 * If a read(fd,buf,0) has been done, do not sleep, just
7252 7302 * return.
7253 7303 *
7254 7304 * If mctlp == NULL and uiop == NULL, then the code will
7255 7305 * do the strwaitq. This is an understood way of saying
7256 7306 * sleep "polling" until a message is received.
7257 7307 */
7258 7308 if ((stp->sd_flag & (STRHUP|STREOF)) ||
7259 7309 (uiop != NULL && uiop->uio_resid == 0)) {
7260 7310 if (mctlp != NULL)
7261 7311 *mctlp = NULL;
7262 7312 *flagsp = 0;
7263 7313 mutex_exit(&stp->sd_lock);
7264 7314 return (0);
7265 7315 }
7266 7316
7267 7317 waitflag = GETWAIT;
7268 7318 if (flags &
7269 7319 (MSG_HOLDSIG|MSG_IGNERROR|MSG_IPEEK|MSG_DELAYERROR)) {
7270 7320 if (flags & MSG_HOLDSIG)
7271 7321 waitflag |= STR_NOSIG;
7272 7322 if (flags & MSG_IGNERROR)
7273 7323 waitflag |= STR_NOERROR;
7274 7324 if (flags & MSG_IPEEK)
7275 7325 waitflag |= STR_PEEK;
7276 7326 if (flags & MSG_DELAYERROR)
7277 7327 waitflag |= STR_DELAYERR;
7278 7328 }
7279 7329 if (uiop != NULL)
7280 7330 fmode = uiop->uio_fmode;
7281 7331 else
7282 7332 fmode = 0;
7283 7333
7284 7334 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_WAIT,
7285 7335 "kstrgetmsg calls strwaitq:%p, %p",
7286 7336 vp, uiop);
7287 7337 if (((error = strwaitq(stp, waitflag, (ssize_t)0,
7288 7338 fmode, timout, &done))) != 0 || done) {
7289 7339 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_DONE,
7290 7340 "kstrgetmsg error or done:%p, %p",
7291 7341 vp, uiop);
7292 7342 mutex_exit(&stp->sd_lock);
7293 7343 return (error);
7294 7344 }
7295 7345 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRGETMSG_AWAKE,
7296 7346 "kstrgetmsg awakes:%p, %p", vp, uiop);
7297 7347 if ((error = i_straccess(stp, JCREAD)) != 0) {
7298 7348 mutex_exit(&stp->sd_lock);
7299 7349 return (error);
7300 7350 }
7301 7351 first = 0;
7302 7352 }
7303 7353 ASSERT(bp != NULL);
7304 7354 /*
7305 7355 * Extract any mark information. If the message is not completely
7306 7356 * consumed this information will be put in the mblk
7307 7357 * that is putback.
7308 7358 * If MSGMARKNEXT is set and the message is completely consumed
7309 7359 * the STRATMARK flag will be set below. Likewise, if
7310 7360 * MSGNOTMARKNEXT is set and the message is
7311 7361 * completely consumed STRNOTATMARK will be set.
7312 7362 */
7313 7363 mark = bp->b_flag & (MSGMARK | MSGMARKNEXT | MSGNOTMARKNEXT);
7314 7364 ASSERT((mark & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
7315 7365 (MSGMARKNEXT|MSGNOTMARKNEXT));
7316 7366 pri = bp->b_band;
7317 7367 if (mark != 0) {
7318 7368 /*
7319 7369 * If the caller doesn't want the mark return.
7320 7370 * Used to implement MSG_WAITALL in sockets.
7321 7371 */
7322 7372 if (flags & MSG_NOMARK) {
7323 7373 putback(stp, q, bp, pri);
7324 7374 qbackenable(q, pri);
7325 7375 mutex_exit(&stp->sd_lock);
7326 7376 return (EWOULDBLOCK);
7327 7377 }
7328 7378 if (bp == stp->sd_mark) {
7329 7379 mark |= _LASTMARK;
7330 7380 stp->sd_mark = NULL;
7331 7381 }
7332 7382 }
7333 7383
7334 7384 /*
7335 7385 * keep track of the first message type
7336 7386 */
7337 7387 type = bp->b_datap->db_type;
7338 7388
7339 7389 if (bp->b_datap->db_type == M_PASSFP) {
7340 7390 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7341 7391 stp->sd_mark = bp;
7342 7392 bp->b_flag |= mark & ~_LASTMARK;
7343 7393 putback(stp, q, bp, pri);
7344 7394 qbackenable(q, pri);
7345 7395 mutex_exit(&stp->sd_lock);
7346 7396 return (EBADMSG);
7347 7397 }
7348 7398 ASSERT(type != M_SIG);
7349 7399
7350 7400 if (flags & MSG_IPEEK) {
7351 7401 /*
7352 7402 * Clear any struioflag - we do the uiomove over again
7353 7403 * when peeking since it simplifies the code.
7354 7404 *
7355 7405 * Dup the message and put the original back on the queue.
7356 7406 * If dupmsg() fails, try again with copymsg() to see if
7357 7407 * there is indeed a shortage of memory. dupmsg() may fail
7358 7408 * if db_ref in any of the messages reaches its limit.
7359 7409 */
7360 7410
7361 7411 if ((nbp = dupmsg(bp)) == NULL && (nbp = copymsg(bp)) == NULL) {
7362 7412 /*
7363 7413 * Restore the state of the stream head since we
7364 7414 * need to drop sd_lock (strwaitbuf is sleeping).
7365 7415 */
7366 7416 size_t size = msgdsize(bp);
7367 7417
7368 7418 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7369 7419 stp->sd_mark = bp;
7370 7420 bp->b_flag |= mark & ~_LASTMARK;
7371 7421 putback(stp, q, bp, pri);
7372 7422 mutex_exit(&stp->sd_lock);
7373 7423 error = strwaitbuf(size, BPRI_HI);
7374 7424 if (error) {
7375 7425 /*
7376 7426 * There is no net change to the queue thus
7377 7427 * no need to qbackenable.
7378 7428 */
7379 7429 return (error);
7380 7430 }
7381 7431 goto retry;
7382 7432 }
7383 7433
7384 7434 if ((mark & _LASTMARK) && (stp->sd_mark == NULL))
7385 7435 stp->sd_mark = bp;
7386 7436 bp->b_flag |= mark & ~_LASTMARK;
7387 7437 putback(stp, q, bp, pri);
7388 7438 bp = nbp;
7389 7439 }
7390 7440
7391 7441 /*
7392 7442 * Set this flag so strrput will not generate signals. Need to
7393 7443 * make sure this flag is cleared before leaving this routine
7394 7444 * else signals will stop being sent.
7395 7445 */
7396 7446 stp->sd_flag |= STRGETINPROG;
7397 7447 mutex_exit(&stp->sd_lock);
7398 7448
7399 7449 if ((stp->sd_rputdatafunc != NULL) && (DB_TYPE(bp) == M_DATA)) {
7400 7450 mblk_t *tmp, *prevmp;
7401 7451
7402 7452 /*
7403 7453 * Put first non-data mblk back to stream head and
7404 7454 * cut the mblk chain so sd_rputdatafunc only sees
7405 7455 * M_DATA mblks. We can skip the first mblk since it
7406 7456 * is M_DATA according to the condition above.
7407 7457 */
7408 7458 for (prevmp = bp, tmp = bp->b_cont; tmp != NULL;
7409 7459 prevmp = tmp, tmp = tmp->b_cont) {
7410 7460 if (DB_TYPE(tmp) != M_DATA) {
7411 7461 prevmp->b_cont = NULL;
7412 7462 mutex_enter(&stp->sd_lock);
7413 7463 putback(stp, q, tmp, tmp->b_band);
7414 7464 mutex_exit(&stp->sd_lock);
7415 7465 break;
7416 7466 }
7417 7467 }
7418 7468
7419 7469 bp = (stp->sd_rputdatafunc)(stp->sd_vnode, bp,
7420 7470 NULL, NULL, NULL, NULL);
7421 7471
7422 7472 if (bp == NULL)
7423 7473 goto retry;
7424 7474 }
7425 7475
7426 7476 if (STREAM_NEEDSERVICE(stp))
7427 7477 stream_runservice(stp);
7428 7478
7429 7479 /*
7430 7480 * Set HIPRI flag if message is priority.
7431 7481 */
7432 7482 if (type >= QPCTL)
7433 7483 flg = MSG_HIPRI;
7434 7484 else
7435 7485 flg = MSG_BAND;
7436 7486
7437 7487 /*
7438 7488 * First process PROTO or PCPROTO blocks, if any.
7439 7489 */
7440 7490 if (mctlp != NULL && type != M_DATA) {
7441 7491 mblk_t *nbp;
7442 7492
7443 7493 *mctlp = bp;
7444 7494 while (bp->b_cont && bp->b_cont->b_datap->db_type != M_DATA)
7445 7495 bp = bp->b_cont;
7446 7496 nbp = bp->b_cont;
7447 7497 bp->b_cont = NULL;
7448 7498 bp = nbp;
7449 7499 }
7450 7500
7451 7501 if (bp && bp->b_datap->db_type != M_DATA) {
7452 7502 /*
7453 7503 * More PROTO blocks in msg. Will only happen if mctlp is NULL.
7454 7504 */
7455 7505 more |= MORECTL;
7456 7506 savemp = bp;
7457 7507 while (bp && bp->b_datap->db_type != M_DATA) {
7458 7508 savemptail = bp;
7459 7509 bp = bp->b_cont;
7460 7510 }
7461 7511 savemptail->b_cont = NULL;
7462 7512 }
7463 7513
7464 7514 /*
7465 7515 * Now process DATA blocks, if any.
7466 7516 */
7467 7517 if (uiop == NULL) {
7468 7518 /* Append data to tail of mctlp */
7469 7519
7470 7520 if (mctlp != NULL) {
7471 7521 mblk_t **mpp = mctlp;
7472 7522
7473 7523 while (*mpp != NULL)
7474 7524 mpp = &((*mpp)->b_cont);
7475 7525 *mpp = bp;
7476 7526 bp = NULL;
7477 7527 }
7478 7528 } else if (uiop->uio_resid >= 0 && bp) {
7479 7529 size_t oldresid = uiop->uio_resid;
7480 7530
7481 7531 /*
7482 7532 * If a streams message is likely to consist
7483 7533 * of many small mblks, it is pulled up into
7484 7534 * one continuous chunk of memory.
7485 7535 * The size of the first mblk may be bogus because
7486 7536 * successive read() calls on the socket reduce
7487 7537 * the size of this mblk until it is exhausted
7488 7538 * and then the code walks on to the next. Thus
7489 7539 * the size of the mblk may not be the original size
7490 7540 * that was passed up, it's simply a remainder
7491 7541 * and hence can be very small without any
7492 7542 * implication that the packet is badly fragmented.
7493 7543 * So the size of the possible second mblk is
7494 7544 * used to spot a badly fragmented packet.
7495 7545 * see longer comment at top of page
7496 7546 * by mblk_pull_len declaration.
7497 7547 */
7498 7548
7499 7549 if (bp->b_cont != NULL && MBLKL(bp->b_cont) < mblk_pull_len) {
7500 7550 (void) pullupmsg(bp, -1);
7501 7551 }
7502 7552
7503 7553 bp = struiocopyout(bp, uiop, &error);
7504 7554 if (error != 0) {
7505 7555 if (mctlp != NULL) {
7506 7556 freemsg(*mctlp);
7507 7557 *mctlp = NULL;
7508 7558 } else
7509 7559 freemsg(savemp);
7510 7560 mutex_enter(&stp->sd_lock);
7511 7561 /*
7512 7562 * clear stream head hi pri flag based on
7513 7563 * first message
7514 7564 */
7515 7565 if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7516 7566 ASSERT(type == M_PCPROTO);
7517 7567 stp->sd_flag &= ~STRPRI;
7518 7568 }
7519 7569 more = 0;
7520 7570 goto getmout;
7521 7571 }
7522 7572 /*
7523 7573 * (pr == 1) indicates a partial read.
7524 7574 */
7525 7575 if (oldresid > uiop->uio_resid)
7526 7576 pr = 1;
7527 7577 }
7528 7578
7529 7579 if (bp) { /* more data blocks in msg */
7530 7580 more |= MOREDATA;
7531 7581 if (savemp)
7532 7582 savemptail->b_cont = bp;
7533 7583 else
7534 7584 savemp = bp;
7535 7585 }
7536 7586
7537 7587 mutex_enter(&stp->sd_lock);
7538 7588 if (savemp) {
7539 7589 if (flags & (MSG_IPEEK|MSG_DISCARDTAIL)) {
7540 7590 /*
7541 7591 * When MSG_DISCARDTAIL is set or
7542 7592 * when peeking discard any tail. When peeking this
7543 7593 * is the tail of the dup that was copied out - the
7544 7594 * message has already been putback on the queue.
7545 7595 * Return MOREDATA to the caller even though the data
7546 7596 * is discarded. This is used by sockets (to
7547 7597 * set MSG_TRUNC).
7548 7598 */
7549 7599 freemsg(savemp);
7550 7600 if (!(flags & MSG_IPEEK) && (type >= QPCTL)) {
7551 7601 ASSERT(type == M_PCPROTO);
7552 7602 stp->sd_flag &= ~STRPRI;
7553 7603 }
7554 7604 } else if (pr && (savemp->b_datap->db_type == M_DATA) &&
7555 7605 msgnodata(savemp)) {
7556 7606 /*
7557 7607 * Avoid queuing a zero-length tail part of
7558 7608 * a message. pr=1 indicates that we read some of
7559 7609 * the message.
7560 7610 */
7561 7611 freemsg(savemp);
7562 7612 more &= ~MOREDATA;
7563 7613 if (type >= QPCTL) {
7564 7614 ASSERT(type == M_PCPROTO);
7565 7615 stp->sd_flag &= ~STRPRI;
7566 7616 }
7567 7617 } else {
7568 7618 savemp->b_band = pri;
7569 7619 /*
7570 7620 * If the first message was HIPRI and the one we're
7571 7621 * putting back isn't, then clear STRPRI, otherwise
7572 7622 * set STRPRI again. Note that we must set STRPRI
7573 7623 * again since the flush logic in strrput_nondata()
7574 7624 * may have cleared it while we had sd_lock dropped.
7575 7625 */
7576 7626
7577 7627 if (type >= QPCTL) {
7578 7628 ASSERT(type == M_PCPROTO);
7579 7629 if (queclass(savemp) < QPCTL)
7580 7630 stp->sd_flag &= ~STRPRI;
7581 7631 else
7582 7632 stp->sd_flag |= STRPRI;
7583 7633 } else if (queclass(savemp) >= QPCTL) {
7584 7634 /*
7585 7635 * The first message was not a HIPRI message,
7586 7636 * but the one we are about to putback is.
7587 7637 * For simplicitly, we do not allow for HIPRI
7588 7638 * messages to be embedded in the message
7589 7639 * body, so just force it to same type as
7590 7640 * first message.
7591 7641 */
7592 7642 ASSERT(type == M_DATA || type == M_PROTO);
7593 7643 ASSERT(savemp->b_datap->db_type == M_PCPROTO);
7594 7644 savemp->b_datap->db_type = type;
7595 7645 }
7596 7646 if (mark != 0) {
7597 7647 if ((mark & _LASTMARK) &&
7598 7648 (stp->sd_mark == NULL)) {
7599 7649 /*
7600 7650 * If another marked message arrived
7601 7651 * while sd_lock was not held sd_mark
7602 7652 * would be non-NULL.
7603 7653 */
7604 7654 stp->sd_mark = savemp;
7605 7655 }
7606 7656 savemp->b_flag |= mark & ~_LASTMARK;
7607 7657 }
7608 7658 putback(stp, q, savemp, pri);
7609 7659 }
7610 7660 } else if (!(flags & MSG_IPEEK)) {
7611 7661 /*
7612 7662 * The complete message was consumed.
7613 7663 *
7614 7664 * If another M_PCPROTO arrived while sd_lock was not held
7615 7665 * it would have been discarded since STRPRI was still set.
7616 7666 *
7617 7667 * Move the MSG*MARKNEXT information
7618 7668 * to the stream head just in case
7619 7669 * the read queue becomes empty.
7620 7670 * clear stream head hi pri flag based on
7621 7671 * first message
7622 7672 *
7623 7673 * If the stream head was at the mark
7624 7674 * (STRATMARK) before we dropped sd_lock above
7625 7675 * and some data was consumed then we have
7626 7676 * moved past the mark thus STRATMARK is
7627 7677 * cleared. However, if a message arrived in
7628 7678 * strrput during the copyout above causing
7629 7679 * STRATMARK to be set we can not clear that
7630 7680 * flag.
7631 7681 * XXX A "perimeter" would help by single-threading strrput,
7632 7682 * strread, strgetmsg and kstrgetmsg.
7633 7683 */
7634 7684 if (type >= QPCTL) {
7635 7685 ASSERT(type == M_PCPROTO);
7636 7686 stp->sd_flag &= ~STRPRI;
7637 7687 }
7638 7688 if (mark & (MSGMARKNEXT|MSGNOTMARKNEXT|MSGMARK)) {
7639 7689 if (mark & MSGMARKNEXT) {
7640 7690 stp->sd_flag &= ~STRNOTATMARK;
7641 7691 stp->sd_flag |= STRATMARK;
7642 7692 } else if (mark & MSGNOTMARKNEXT) {
7643 7693 stp->sd_flag &= ~STRATMARK;
7644 7694 stp->sd_flag |= STRNOTATMARK;
7645 7695 } else {
7646 7696 stp->sd_flag &= ~(STRATMARK|STRNOTATMARK);
7647 7697 }
7648 7698 } else if (pr && (old_sd_flag & STRATMARK)) {
7649 7699 stp->sd_flag &= ~STRATMARK;
7650 7700 }
7651 7701 }
7652 7702
7653 7703 *flagsp = flg;
7654 7704 *prip = pri;
7655 7705
7656 7706 /*
7657 7707 * Getmsg cleanup processing - if the state of the queue has changed
7658 7708 * some signals may need to be sent and/or poll awakened.
7659 7709 */
7660 7710 getmout:
7661 7711 qbackenable(q, pri);
7662 7712
7663 7713 /*
7664 7714 * We dropped the stream head lock above. Send all M_SIG messages
7665 7715 * before processing stream head for SIGPOLL messages.
7666 7716 */
7667 7717 ASSERT(MUTEX_HELD(&stp->sd_lock));
7668 7718 while ((bp = q->q_first) != NULL &&
7669 7719 (bp->b_datap->db_type == M_SIG)) {
7670 7720 /*
7671 7721 * sd_lock is held so the content of the read queue can not
7672 7722 * change.
7673 7723 */
7674 7724 bp = getq(q);
7675 7725 ASSERT(bp != NULL && bp->b_datap->db_type == M_SIG);
7676 7726
7677 7727 strsignal_nolock(stp, *bp->b_rptr, bp->b_band);
7678 7728 mutex_exit(&stp->sd_lock);
7679 7729 freemsg(bp);
7680 7730 if (STREAM_NEEDSERVICE(stp))
7681 7731 stream_runservice(stp);
7682 7732 mutex_enter(&stp->sd_lock);
7683 7733 }
7684 7734
7685 7735 /*
7686 7736 * stream head cannot change while we make the determination
7687 7737 * whether or not to send a signal. Drop the flag to allow strrput
7688 7738 * to send firstmsgsigs again.
7689 7739 */
7690 7740 stp->sd_flag &= ~STRGETINPROG;
7691 7741
7692 7742 /*
7693 7743 * If the type of message at the front of the queue changed
7694 7744 * due to the receive the appropriate signals and pollwakeup events
7695 7745 * are generated. The type of changes are:
7696 7746 * Processed a hipri message, q_first is not hipri.
7697 7747 * Processed a band X message, and q_first is band Y.
7698 7748 * The generated signals and pollwakeups are identical to what
7699 7749 * strrput() generates should the message that is now on q_first
7700 7750 * arrive to an empty read queue.
7701 7751 *
7702 7752 * Note: only strrput will send a signal for a hipri message.
7703 7753 */
7704 7754 if ((bp = q->q_first) != NULL && !(stp->sd_flag & STRPRI)) {
7705 7755 strsigset_t signals = 0;
7706 7756 strpollset_t pollwakeups = 0;
7707 7757
7708 7758 if (flg & MSG_HIPRI) {
7709 7759 /*
7710 7760 * Removed a hipri message. Regular data at
7711 7761 * the front of the queue.
7712 7762 */
7713 7763 if (bp->b_band == 0) {
7714 7764 signals = S_INPUT | S_RDNORM;
7715 7765 pollwakeups = POLLIN | POLLRDNORM;
7716 7766 } else {
7717 7767 signals = S_INPUT | S_RDBAND;
7718 7768 pollwakeups = POLLIN | POLLRDBAND;
7719 7769 }
7720 7770 } else if (pri != bp->b_band) {
7721 7771 /*
7722 7772 * The band is different for the new q_first.
7723 7773 */
7724 7774 if (bp->b_band == 0) {
7725 7775 signals = S_RDNORM;
7726 7776 pollwakeups = POLLIN | POLLRDNORM;
7727 7777 } else {
7728 7778 signals = S_RDBAND;
7729 7779 pollwakeups = POLLIN | POLLRDBAND;
7730 7780 }
7731 7781 }
7732 7782
7733 7783 if (pollwakeups != 0) {
7734 7784 if (pollwakeups == (POLLIN | POLLRDNORM)) {
7735 7785 if (!(stp->sd_rput_opt & SR_POLLIN))
7736 7786 goto no_pollwake;
7737 7787 stp->sd_rput_opt &= ~SR_POLLIN;
7738 7788 }
7739 7789 mutex_exit(&stp->sd_lock);
7740 7790 pollwakeup(&stp->sd_pollist, pollwakeups);
7741 7791 mutex_enter(&stp->sd_lock);
7742 7792 }
7743 7793 no_pollwake:
7744 7794
7745 7795 if (stp->sd_sigflags & signals)
7746 7796 strsendsig(stp->sd_siglist, signals, bp->b_band, 0);
7747 7797 }
7748 7798 mutex_exit(&stp->sd_lock);
7749 7799
7750 7800 rvp->r_val1 = more;
7751 7801 return (error);
7752 7802 #undef _LASTMARK
7753 7803 }
7754 7804
7755 7805 /*
7756 7806 * Put a message downstream.
7757 7807 *
7758 7808 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7759 7809 */
7760 7810 int
7761 7811 strputmsg(
7762 7812 struct vnode *vp,
7763 7813 struct strbuf *mctl,
7764 7814 struct strbuf *mdata,
7765 7815 unsigned char pri,
7766 7816 int flag,
7767 7817 int fmode)
7768 7818 {
7769 7819 struct stdata *stp;
7770 7820 queue_t *wqp;
7771 7821 mblk_t *mp;
7772 7822 ssize_t msgsize;
7773 7823 ssize_t rmin, rmax;
7774 7824 int error;
7775 7825 struct uio uios;
7776 7826 struct uio *uiop = &uios;
7777 7827 struct iovec iovs;
7778 7828 int xpg4 = 0;
7779 7829
7780 7830 ASSERT(vp->v_stream);
7781 7831 stp = vp->v_stream;
7782 7832 wqp = stp->sd_wrq;
7783 7833
7784 7834 /*
7785 7835 * If it is an XPG4 application, we need to send
7786 7836 * SIGPIPE below
7787 7837 */
7788 7838
7789 7839 xpg4 = (flag & MSG_XPG4) ? 1 : 0;
7790 7840 flag &= ~MSG_XPG4;
7791 7841
7792 7842 if (AU_AUDITING())
7793 7843 audit_strputmsg(vp, mctl, mdata, pri, flag, fmode);
7794 7844
7795 7845 mutex_enter(&stp->sd_lock);
7796 7846
7797 7847 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7798 7848 mutex_exit(&stp->sd_lock);
7799 7849 return (error);
7800 7850 }
7801 7851
7802 7852 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7803 7853 error = strwriteable(stp, B_FALSE, xpg4);
7804 7854 if (error != 0) {
7805 7855 mutex_exit(&stp->sd_lock);
7806 7856 return (error);
7807 7857 }
7808 7858 }
7809 7859
7810 7860 mutex_exit(&stp->sd_lock);
7811 7861
7812 7862 /*
7813 7863 * Check for legal flag value.
7814 7864 */
7815 7865 switch (flag) {
7816 7866 case MSG_HIPRI:
7817 7867 if ((mctl->len < 0) || (pri != 0))
7818 7868 return (EINVAL);
7819 7869 break;
7820 7870 case MSG_BAND:
7821 7871 break;
7822 7872
7823 7873 default:
7824 7874 return (EINVAL);
7825 7875 }
7826 7876
7827 7877 TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_IN,
7828 7878 "strputmsg in:stp %p", stp);
7829 7879
7830 7880 /* get these values from those cached in the stream head */
7831 7881 rmin = stp->sd_qn_minpsz;
7832 7882 rmax = stp->sd_qn_maxpsz;
7833 7883
7834 7884 /*
7835 7885 * Make sure ctl and data sizes together fall within the
7836 7886 * limits of the max and min receive packet sizes and do
7837 7887 * not exceed system limit.
7838 7888 */
7839 7889 ASSERT((rmax >= 0) || (rmax == INFPSZ));
7840 7890 if (rmax == 0) {
7841 7891 return (ERANGE);
7842 7892 }
7843 7893 /*
7844 7894 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
7845 7895 * Needed to prevent partial failures in the strmakedata loop.
7846 7896 */
7847 7897 if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
7848 7898 rmax = stp->sd_maxblk;
7849 7899
7850 7900 if ((msgsize = mdata->len) < 0) {
7851 7901 msgsize = 0;
7852 7902 rmin = 0; /* no range check for NULL data part */
7853 7903 }
7854 7904 if ((msgsize < rmin) ||
7855 7905 ((msgsize > rmax) && (rmax != INFPSZ)) ||
7856 7906 (mctl->len > strctlsz)) {
7857 7907 return (ERANGE);
7858 7908 }
7859 7909
7860 7910 /*
7861 7911 * Setup uio and iov for data part
7862 7912 */
7863 7913 iovs.iov_base = mdata->buf;
7864 7914 iovs.iov_len = msgsize;
7865 7915 uios.uio_iov = &iovs;
7866 7916 uios.uio_iovcnt = 1;
7867 7917 uios.uio_loffset = 0;
7868 7918 uios.uio_segflg = UIO_USERSPACE;
7869 7919 uios.uio_fmode = fmode;
7870 7920 uios.uio_extflg = UIO_COPY_DEFAULT;
7871 7921 uios.uio_resid = msgsize;
7872 7922 uios.uio_offset = 0;
7873 7923
7874 7924 /* Ignore flow control in strput for HIPRI */
7875 7925 if (flag & MSG_HIPRI)
7876 7926 flag |= MSG_IGNFLOW;
7877 7927
7878 7928 for (;;) {
7879 7929 int done = 0;
7880 7930
7881 7931 /*
7882 7932 * strput will always free the ctl mblk - even when strput
7883 7933 * fails.
7884 7934 */
7885 7935 if ((error = strmakectl(mctl, flag, fmode, &mp)) != 0) {
7886 7936 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7887 7937 "strputmsg out:stp %p out %d error %d",
7888 7938 stp, 1, error);
7889 7939 return (error);
7890 7940 }
7891 7941 /*
7892 7942 * Verify that the whole message can be transferred by
7893 7943 * strput.
7894 7944 */
7895 7945 ASSERT(stp->sd_maxblk == INFPSZ ||
7896 7946 stp->sd_maxblk >= mdata->len);
7897 7947
7898 7948 msgsize = mdata->len;
7899 7949 error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
7900 7950 mdata->len = msgsize;
7901 7951
7902 7952 if (error == 0)
7903 7953 break;
7904 7954
7905 7955 if (error != EWOULDBLOCK)
7906 7956 goto out;
7907 7957
7908 7958 mutex_enter(&stp->sd_lock);
7909 7959 /*
7910 7960 * Check for a missed wakeup.
7911 7961 * Needed since strput did not hold sd_lock across
7912 7962 * the canputnext.
7913 7963 */
7914 7964 if (bcanputnext(wqp, pri)) {
7915 7965 /* Try again */
7916 7966 mutex_exit(&stp->sd_lock);
7917 7967 continue;
7918 7968 }
7919 7969 TRACE_2(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAIT,
7920 7970 "strputmsg wait:stp %p waits pri %d", stp, pri);
7921 7971 if (((error = strwaitq(stp, WRITEWAIT, (ssize_t)0, fmode, -1,
7922 7972 &done)) != 0) || done) {
7923 7973 mutex_exit(&stp->sd_lock);
7924 7974 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7925 7975 "strputmsg out:q %p out %d error %d",
7926 7976 stp, 0, error);
7927 7977 return (error);
7928 7978 }
7929 7979 TRACE_1(TR_FAC_STREAMS_FR, TR_STRPUTMSG_WAKE,
7930 7980 "strputmsg wake:stp %p wakes", stp);
7931 7981 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7932 7982 mutex_exit(&stp->sd_lock);
7933 7983 return (error);
7934 7984 }
7935 7985 mutex_exit(&stp->sd_lock);
7936 7986 }
7937 7987 out:
7938 7988 /*
7939 7989 * For historic reasons, applications expect EAGAIN
7940 7990 * when data mblk could not be allocated. so change
7941 7991 * ENOMEM back to EAGAIN
7942 7992 */
7943 7993 if (error == ENOMEM)
7944 7994 error = EAGAIN;
7945 7995 TRACE_3(TR_FAC_STREAMS_FR, TR_STRPUTMSG_OUT,
7946 7996 "strputmsg out:stp %p out %d error %d", stp, 2, error);
7947 7997 return (error);
7948 7998 }
7949 7999
7950 8000 /*
7951 8001 * Put a message downstream.
7952 8002 * Can send only an M_PROTO/M_PCPROTO by passing in a NULL uiop.
7953 8003 * The fmode flag (NDELAY, NONBLOCK) is the or of the flags in the uio
7954 8004 * and the fmode parameter.
7955 8005 *
7956 8006 * This routine handles the consolidation private flags:
7957 8007 * MSG_IGNERROR Ignore any stream head error except STPLEX.
7958 8008 * MSG_HOLDSIG Hold signals while waiting for data.
7959 8009 * MSG_IGNFLOW Don't check streams flow control.
7960 8010 *
7961 8011 * NOTE: strputmsg and kstrputmsg have much of the logic in common.
7962 8012 */
7963 8013 int
7964 8014 kstrputmsg(
7965 8015 struct vnode *vp,
7966 8016 mblk_t *mctl,
7967 8017 struct uio *uiop,
7968 8018 ssize_t msgsize,
7969 8019 unsigned char pri,
7970 8020 int flag,
7971 8021 int fmode)
7972 8022 {
7973 8023 struct stdata *stp;
7974 8024 queue_t *wqp;
7975 8025 ssize_t rmin, rmax;
7976 8026 int error;
7977 8027
7978 8028 ASSERT(vp->v_stream);
7979 8029 stp = vp->v_stream;
7980 8030 wqp = stp->sd_wrq;
7981 8031 if (AU_AUDITING())
7982 8032 audit_strputmsg(vp, NULL, NULL, pri, flag, fmode);
7983 8033 if (mctl == NULL)
7984 8034 return (EINVAL);
7985 8035
7986 8036 mutex_enter(&stp->sd_lock);
7987 8037
7988 8038 if ((error = i_straccess(stp, JCWRITE)) != 0) {
7989 8039 mutex_exit(&stp->sd_lock);
7990 8040 freemsg(mctl);
7991 8041 return (error);
7992 8042 }
7993 8043
7994 8044 if ((stp->sd_flag & STPLEX) || !(flag & MSG_IGNERROR)) {
7995 8045 if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
7996 8046 error = strwriteable(stp, B_FALSE, B_TRUE);
7997 8047 if (error != 0) {
7998 8048 mutex_exit(&stp->sd_lock);
7999 8049 freemsg(mctl);
8000 8050 return (error);
8001 8051 }
8002 8052 }
8003 8053 }
8004 8054
8005 8055 mutex_exit(&stp->sd_lock);
8006 8056
8007 8057 /*
8008 8058 * Check for legal flag value.
8009 8059 */
8010 8060 switch (flag & (MSG_HIPRI|MSG_BAND|MSG_ANY)) {
8011 8061 case MSG_HIPRI:
8012 8062 if (pri != 0) {
8013 8063 freemsg(mctl);
8014 8064 return (EINVAL);
8015 8065 }
8016 8066 break;
8017 8067 case MSG_BAND:
8018 8068 break;
8019 8069 default:
8020 8070 freemsg(mctl);
8021 8071 return (EINVAL);
8022 8072 }
8023 8073
8024 8074 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_IN,
8025 8075 "kstrputmsg in:stp %p", stp);
8026 8076
8027 8077 /* get these values from those cached in the stream head */
8028 8078 rmin = stp->sd_qn_minpsz;
8029 8079 rmax = stp->sd_qn_maxpsz;
8030 8080
8031 8081 /*
8032 8082 * Make sure ctl and data sizes together fall within the
8033 8083 * limits of the max and min receive packet sizes and do
8034 8084 * not exceed system limit.
8035 8085 */
8036 8086 ASSERT((rmax >= 0) || (rmax == INFPSZ));
8037 8087 if (rmax == 0) {
8038 8088 freemsg(mctl);
8039 8089 return (ERANGE);
8040 8090 }
8041 8091 /*
8042 8092 * Use the MAXIMUM of sd_maxblk and q_maxpsz.
8043 8093 * Needed to prevent partial failures in the strmakedata loop.
8044 8094 */
8045 8095 if (stp->sd_maxblk != INFPSZ && rmax != INFPSZ && rmax < stp->sd_maxblk)
8046 8096 rmax = stp->sd_maxblk;
8047 8097
8048 8098 if (uiop == NULL) {
8049 8099 msgsize = -1;
8050 8100 rmin = -1; /* no range check for NULL data part */
8051 8101 } else {
8052 8102 /* Use uio flags as well as the fmode parameter flags */
8053 8103 fmode |= uiop->uio_fmode;
8054 8104
8055 8105 if ((msgsize < rmin) ||
8056 8106 ((msgsize > rmax) && (rmax != INFPSZ))) {
8057 8107 freemsg(mctl);
8058 8108 return (ERANGE);
8059 8109 }
8060 8110 }
8061 8111
8062 8112 /* Ignore flow control in strput for HIPRI */
8063 8113 if (flag & MSG_HIPRI)
8064 8114 flag |= MSG_IGNFLOW;
8065 8115
8066 8116 for (;;) {
8067 8117 int done = 0;
8068 8118 int waitflag;
8069 8119 mblk_t *mp;
8070 8120
8071 8121 /*
8072 8122 * strput will always free the ctl mblk - even when strput
8073 8123 * fails. If MSG_IGNFLOW is set then any error returned
8074 8124 * will cause us to break the loop, so we don't need a copy
8075 8125 * of the message. If MSG_IGNFLOW is not set, then we can
8076 8126 * get hit by flow control and be forced to try again. In
8077 8127 * this case we need to have a copy of the message. We
8078 8128 * do this using copymsg since the message may get modified
8079 8129 * by something below us.
8080 8130 *
8081 8131 * We've observed that many TPI providers do not check db_ref
8082 8132 * on the control messages but blindly reuse them for the
8083 8133 * T_OK_ACK/T_ERROR_ACK. Thus using copymsg is more
8084 8134 * friendly to such providers than using dupmsg. Also, note
8085 8135 * that sockfs uses MSG_IGNFLOW for all TPI control messages.
8086 8136 * Only data messages are subject to flow control, hence
8087 8137 * subject to this copymsg.
8088 8138 */
8089 8139 if (flag & MSG_IGNFLOW) {
8090 8140 mp = mctl;
8091 8141 mctl = NULL;
8092 8142 } else {
8093 8143 do {
8094 8144 /*
8095 8145 * If a message has a free pointer, the message
8096 8146 * must be dupmsg to maintain this pointer.
8097 8147 * Code using this facility must be sure
8098 8148 * that modules below will not change the
8099 8149 * contents of the dblk without checking db_ref
8100 8150 * first. If db_ref is > 1, then the module
8101 8151 * needs to do a copymsg first. Otherwise,
8102 8152 * the contents of the dblk may become
8103 8153 * inconsistent because the freesmg/freeb below
8104 8154 * may end up calling atomic_add_32_nv.
8105 8155 * The atomic_add_32_nv in freeb (accessing
8106 8156 * all of db_ref, db_type, db_flags, and
8107 8157 * db_struioflag) does not prevent other threads
8108 8158 * from concurrently trying to modify e.g.
8109 8159 * db_type.
8110 8160 */
8111 8161 if (mctl->b_datap->db_frtnp != NULL)
8112 8162 mp = dupmsg(mctl);
8113 8163 else
8114 8164 mp = copymsg(mctl);
8115 8165
8116 8166 if (mp != NULL)
8117 8167 break;
8118 8168
8119 8169 error = strwaitbuf(msgdsize(mctl), BPRI_MED);
8120 8170 if (error) {
8121 8171 freemsg(mctl);
8122 8172 return (error);
8123 8173 }
8124 8174 } while (mp == NULL);
8125 8175 }
8126 8176 /*
8127 8177 * Verify that all of msgsize can be transferred by
8128 8178 * strput.
8129 8179 */
8130 8180 ASSERT(stp->sd_maxblk == INFPSZ || stp->sd_maxblk >= msgsize);
8131 8181 error = strput(stp, mp, uiop, &msgsize, 0, pri, flag);
8132 8182 if (error == 0)
8133 8183 break;
8134 8184
8135 8185 if (error != EWOULDBLOCK)
8136 8186 goto out;
8137 8187
8138 8188 /*
8139 8189 * IF MSG_IGNFLOW is set we should have broken out of loop
8140 8190 * above.
8141 8191 */
8142 8192 ASSERT(!(flag & MSG_IGNFLOW));
8143 8193 mutex_enter(&stp->sd_lock);
8144 8194 /*
8145 8195 * Check for a missed wakeup.
8146 8196 * Needed since strput did not hold sd_lock across
8147 8197 * the canputnext.
8148 8198 */
8149 8199 if (bcanputnext(wqp, pri)) {
8150 8200 /* Try again */
8151 8201 mutex_exit(&stp->sd_lock);
8152 8202 continue;
8153 8203 }
8154 8204 TRACE_2(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAIT,
8155 8205 "kstrputmsg wait:stp %p waits pri %d", stp, pri);
8156 8206
8157 8207 waitflag = WRITEWAIT;
8158 8208 if (flag & (MSG_HOLDSIG|MSG_IGNERROR)) {
8159 8209 if (flag & MSG_HOLDSIG)
8160 8210 waitflag |= STR_NOSIG;
8161 8211 if (flag & MSG_IGNERROR)
8162 8212 waitflag |= STR_NOERROR;
8163 8213 }
8164 8214 if (((error = strwaitq(stp, waitflag,
8165 8215 (ssize_t)0, fmode, -1, &done)) != 0) || done) {
8166 8216 mutex_exit(&stp->sd_lock);
8167 8217 TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8168 8218 "kstrputmsg out:stp %p out %d error %d",
8169 8219 stp, 0, error);
8170 8220 freemsg(mctl);
8171 8221 return (error);
8172 8222 }
8173 8223 TRACE_1(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_WAKE,
8174 8224 "kstrputmsg wake:stp %p wakes", stp);
8175 8225 if ((error = i_straccess(stp, JCWRITE)) != 0) {
8176 8226 mutex_exit(&stp->sd_lock);
8177 8227 freemsg(mctl);
8178 8228 return (error);
8179 8229 }
8180 8230 mutex_exit(&stp->sd_lock);
8181 8231 }
8182 8232 out:
8183 8233 freemsg(mctl);
8184 8234 /*
8185 8235 * For historic reasons, applications expect EAGAIN
8186 8236 * when data mblk could not be allocated. so change
8187 8237 * ENOMEM back to EAGAIN
8188 8238 */
8189 8239 if (error == ENOMEM)
8190 8240 error = EAGAIN;
8191 8241 TRACE_3(TR_FAC_STREAMS_FR, TR_KSTRPUTMSG_OUT,
8192 8242 "kstrputmsg out:stp %p out %d error %d", stp, 2, error);
8193 8243 return (error);
8194 8244 }
8195 8245
8196 8246 /*
8197 8247 * Determines whether the necessary conditions are set on a stream
8198 8248 * for it to be readable, writeable, or have exceptions.
8199 8249 *
8200 8250 * strpoll handles the consolidation private events:
8201 8251 * POLLNOERR Do not return POLLERR even if there are stream
8202 8252 * head errors.
8203 8253 * Used by sockfs.
8204 8254 * POLLRDDATA Do not return POLLIN unless at least one message on
8205 8255 * the queue contains one or more M_DATA mblks. Thus
8206 8256 * when this flag is set a queue with only
8207 8257 * M_PROTO/M_PCPROTO mblks does not return POLLIN.
8208 8258 * Used by sockfs to ignore T_EXDATA_IND messages.
8209 8259 *
8210 8260 * Note: POLLRDDATA assumes that synch streams only return messages with
8211 8261 * an M_DATA attached (i.e. not messages consisting of only
8212 8262 * an M_PROTO/M_PCPROTO part).
8213 8263 */
8214 8264 int
8215 8265 strpoll(struct stdata *stp, short events_arg, int anyyet, short *reventsp,
8216 8266 struct pollhead **phpp)
8217 8267 {
8218 8268 int events = (ushort_t)events_arg;
8219 8269 int retevents = 0;
8220 8270 mblk_t *mp;
8221 8271 qband_t *qbp;
8222 8272 long sd_flags = stp->sd_flag;
8223 8273 int headlocked = 0;
8224 8274
8225 8275 /*
8226 8276 * For performance, a single 'if' tests for most possible edge
8227 8277 * conditions in one shot
8228 8278 */
8229 8279 if (sd_flags & (STPLEX | STRDERR | STWRERR)) {
8230 8280 if (sd_flags & STPLEX) {
8231 8281 *reventsp = POLLNVAL;
8232 8282 return (EINVAL);
8233 8283 }
8234 8284 if (((events & (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)) &&
8235 8285 (sd_flags & STRDERR)) ||
8236 8286 ((events & (POLLOUT | POLLWRNORM | POLLWRBAND)) &&
8237 8287 (sd_flags & STWRERR))) {
8238 8288 if (!(events & POLLNOERR)) {
8239 8289 *reventsp = POLLERR;
8240 8290 return (0);
8241 8291 }
8242 8292 }
8243 8293 }
8244 8294 if (sd_flags & STRHUP) {
8245 8295 retevents |= POLLHUP;
8246 8296 } else if (events & (POLLWRNORM | POLLWRBAND)) {
8247 8297 queue_t *tq;
8248 8298 queue_t *qp = stp->sd_wrq;
8249 8299
8250 8300 claimstr(qp);
8251 8301 /* Find next module forward that has a service procedure */
8252 8302 tq = qp->q_next->q_nfsrv;
8253 8303 ASSERT(tq != NULL);
8254 8304
8255 8305 if (polllock(&stp->sd_pollist, QLOCK(tq)) != 0) {
8256 8306 releasestr(qp);
8257 8307 *reventsp = POLLNVAL;
8258 8308 return (0);
8259 8309 }
8260 8310 if (events & POLLWRNORM) {
8261 8311 queue_t *sqp;
8262 8312
8263 8313 if (tq->q_flag & QFULL)
8264 8314 /* ensure backq svc procedure runs */
8265 8315 tq->q_flag |= QWANTW;
8266 8316 else if ((sqp = stp->sd_struiowrq) != NULL) {
8267 8317 /* Check sync stream barrier write q */
8268 8318 mutex_exit(QLOCK(tq));
8269 8319 if (polllock(&stp->sd_pollist,
8270 8320 QLOCK(sqp)) != 0) {
8271 8321 releasestr(qp);
8272 8322 *reventsp = POLLNVAL;
8273 8323 return (0);
8274 8324 }
8275 8325 if (sqp->q_flag & QFULL)
8276 8326 /* ensure pollwakeup() is done */
8277 8327 sqp->q_flag |= QWANTWSYNC;
8278 8328 else
8279 8329 retevents |= POLLOUT;
8280 8330 /* More write events to process ??? */
8281 8331 if (! (events & POLLWRBAND)) {
8282 8332 mutex_exit(QLOCK(sqp));
8283 8333 releasestr(qp);
8284 8334 goto chkrd;
8285 8335 }
8286 8336 mutex_exit(QLOCK(sqp));
8287 8337 if (polllock(&stp->sd_pollist,
8288 8338 QLOCK(tq)) != 0) {
8289 8339 releasestr(qp);
8290 8340 *reventsp = POLLNVAL;
8291 8341 return (0);
8292 8342 }
8293 8343 } else
8294 8344 retevents |= POLLOUT;
8295 8345 }
8296 8346 if (events & POLLWRBAND) {
8297 8347 qbp = tq->q_bandp;
8298 8348 if (qbp) {
8299 8349 while (qbp) {
8300 8350 if (qbp->qb_flag & QB_FULL)
8301 8351 qbp->qb_flag |= QB_WANTW;
8302 8352 else
8303 8353 retevents |= POLLWRBAND;
8304 8354 qbp = qbp->qb_next;
8305 8355 }
8306 8356 } else {
8307 8357 retevents |= POLLWRBAND;
8308 8358 }
8309 8359 }
8310 8360 mutex_exit(QLOCK(tq));
8311 8361 releasestr(qp);
8312 8362 }
8313 8363 chkrd:
8314 8364 if (sd_flags & STRPRI) {
8315 8365 retevents |= (events & POLLPRI);
8316 8366 } else if (events & (POLLRDNORM | POLLRDBAND | POLLIN)) {
8317 8367 queue_t *qp = _RD(stp->sd_wrq);
8318 8368 int normevents = (events & (POLLIN | POLLRDNORM));
8319 8369
8320 8370 /*
8321 8371 * Note: Need to do polllock() here since ps_lock may be
8322 8372 * held. See bug 4191544.
8323 8373 */
8324 8374 if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8325 8375 *reventsp = POLLNVAL;
8326 8376 return (0);
8327 8377 }
8328 8378 headlocked = 1;
8329 8379 mp = qp->q_first;
8330 8380 while (mp) {
8331 8381 /*
8332 8382 * For POLLRDDATA we scan b_cont and b_next until we
8333 8383 * find an M_DATA.
8334 8384 */
8335 8385 if ((events & POLLRDDATA) &&
8336 8386 mp->b_datap->db_type != M_DATA) {
8337 8387 mblk_t *nmp = mp->b_cont;
8338 8388
8339 8389 while (nmp != NULL &&
8340 8390 nmp->b_datap->db_type != M_DATA)
8341 8391 nmp = nmp->b_cont;
8342 8392 if (nmp == NULL) {
8343 8393 mp = mp->b_next;
8344 8394 continue;
8345 8395 }
8346 8396 }
8347 8397 if (mp->b_band == 0)
8348 8398 retevents |= normevents;
8349 8399 else
8350 8400 retevents |= (events & (POLLIN | POLLRDBAND));
8351 8401 break;
8352 8402 }
8353 8403 if (!(retevents & normevents) && (stp->sd_wakeq & RSLEEP)) {
8354 8404 /*
8355 8405 * Sync stream barrier read queue has data.
8356 8406 */
8357 8407 retevents |= normevents;
8358 8408 }
8359 8409 /* Treat eof as normal data */
8360 8410 if (sd_flags & STREOF)
8361 8411 retevents |= normevents;
8362 8412 }
8363 8413
8364 8414 /*
8365 8415 * Pass back a pollhead if no events are pending or if edge-triggering
8366 8416 * has been configured on this resource.
8367 8417 */
8368 8418 if ((retevents == 0 && !anyyet) || (events & POLLET)) {
8369 8419 *phpp = &stp->sd_pollist;
8370 8420 if (headlocked == 0) {
8371 8421 if (polllock(&stp->sd_pollist, &stp->sd_lock) != 0) {
8372 8422 *reventsp = POLLNVAL;
8373 8423 return (0);
8374 8424 }
8375 8425 headlocked = 1;
8376 8426 }
8377 8427 stp->sd_rput_opt |= SR_POLLIN;
8378 8428 }
8379 8429
8380 8430 *reventsp = (short)retevents;
8381 8431 if (headlocked)
8382 8432 mutex_exit(&stp->sd_lock);
8383 8433 return (0);
8384 8434 }
8385 8435
8386 8436 /*
8387 8437 * The purpose of putback() is to assure sleeping polls/reads
8388 8438 * are awakened when there are no new messages arriving at the,
8389 8439 * stream head, and a message is placed back on the read queue.
8390 8440 *
8391 8441 * sd_lock must be held when messages are placed back on stream
8392 8442 * head. (getq() holds sd_lock when it removes messages from
8393 8443 * the queue)
8394 8444 */
8395 8445
8396 8446 static void
8397 8447 putback(struct stdata *stp, queue_t *q, mblk_t *bp, int band)
8398 8448 {
8399 8449 mblk_t *qfirst;
8400 8450 ASSERT(MUTEX_HELD(&stp->sd_lock));
8401 8451
8402 8452 /*
8403 8453 * As a result of lock-step ordering around q_lock and sd_lock,
8404 8454 * it's possible for function calls like putnext() and
8405 8455 * canputnext() to get an inaccurate picture of how much
8406 8456 * data is really being processed at the stream head.
8407 8457 * We only consolidate with existing messages on the queue
8408 8458 * if the length of the message we want to put back is smaller
8409 8459 * than the queue hiwater mark.
8410 8460 */
8411 8461 if ((stp->sd_rput_opt & SR_CONSOL_DATA) &&
8412 8462 (DB_TYPE(bp) == M_DATA) && ((qfirst = q->q_first) != NULL) &&
8413 8463 (DB_TYPE(qfirst) == M_DATA) &&
8414 8464 ((qfirst->b_flag & (MSGMARK|MSGDELIM)) == 0) &&
8415 8465 ((bp->b_flag & (MSGMARK|MSGDELIM|MSGMARKNEXT)) == 0) &&
8416 8466 (mp_cont_len(bp, NULL) < q->q_hiwat)) {
8417 8467 /*
8418 8468 * We use the same logic as defined in strrput()
8419 8469 * but in reverse as we are putting back onto the
8420 8470 * queue and want to retain byte ordering.
8421 8471 * Consolidate M_DATA messages with M_DATA ONLY.
8422 8472 * strrput() allows the consolidation of M_DATA onto
8423 8473 * M_PROTO | M_PCPROTO but not the other way round.
8424 8474 *
8425 8475 * The consolidation does not take place if the message
8426 8476 * we are returning to the queue is marked with either
8427 8477 * of the marks or the delim flag or if q_first
8428 8478 * is marked with MSGMARK. The MSGMARK check is needed to
8429 8479 * handle the odd semantics of MSGMARK where essentially
8430 8480 * the whole message is to be treated as marked.
8431 8481 * Carry any MSGMARKNEXT and MSGNOTMARKNEXT from q_first
8432 8482 * to the front of the b_cont chain.
8433 8483 */
8434 8484 rmvq_noenab(q, qfirst);
8435 8485
8436 8486 /*
8437 8487 * The first message in the b_cont list
8438 8488 * tracks MSGMARKNEXT and MSGNOTMARKNEXT.
8439 8489 * We need to handle the case where we
8440 8490 * are appending:
8441 8491 *
8442 8492 * 1) a MSGMARKNEXT to a MSGNOTMARKNEXT.
8443 8493 * 2) a MSGMARKNEXT to a plain message.
8444 8494 * 3) a MSGNOTMARKNEXT to a plain message
8445 8495 * 4) a MSGNOTMARKNEXT to a MSGNOTMARKNEXT
8446 8496 * message.
8447 8497 *
8448 8498 * Thus we never append a MSGMARKNEXT or
8449 8499 * MSGNOTMARKNEXT to a MSGMARKNEXT message.
8450 8500 */
8451 8501 if (qfirst->b_flag & MSGMARKNEXT) {
8452 8502 bp->b_flag |= MSGMARKNEXT;
8453 8503 bp->b_flag &= ~MSGNOTMARKNEXT;
8454 8504 qfirst->b_flag &= ~MSGMARKNEXT;
8455 8505 } else if (qfirst->b_flag & MSGNOTMARKNEXT) {
8456 8506 bp->b_flag |= MSGNOTMARKNEXT;
8457 8507 qfirst->b_flag &= ~MSGNOTMARKNEXT;
8458 8508 }
8459 8509
8460 8510 linkb(bp, qfirst);
8461 8511 }
8462 8512 (void) putbq(q, bp);
8463 8513
8464 8514 /*
8465 8515 * A message may have come in when the sd_lock was dropped in the
8466 8516 * calling routine. If this is the case and STR*ATMARK info was
8467 8517 * received, need to move that from the stream head to the q_last
8468 8518 * so that SIOCATMARK can return the proper value.
8469 8519 */
8470 8520 if (stp->sd_flag & (STRATMARK | STRNOTATMARK)) {
8471 8521 unsigned short *flagp = &q->q_last->b_flag;
8472 8522 uint_t b_flag = (uint_t)*flagp;
8473 8523
8474 8524 if (stp->sd_flag & STRATMARK) {
8475 8525 b_flag &= ~MSGNOTMARKNEXT;
8476 8526 b_flag |= MSGMARKNEXT;
8477 8527 stp->sd_flag &= ~STRATMARK;
8478 8528 } else {
8479 8529 b_flag &= ~MSGMARKNEXT;
8480 8530 b_flag |= MSGNOTMARKNEXT;
8481 8531 stp->sd_flag &= ~STRNOTATMARK;
8482 8532 }
8483 8533 *flagp = (unsigned short) b_flag;
8484 8534 }
8485 8535
8486 8536 #ifdef DEBUG
8487 8537 /*
8488 8538 * Make sure that the flags are not messed up.
8489 8539 */
8490 8540 {
8491 8541 mblk_t *mp;
8492 8542 mp = q->q_last;
8493 8543 while (mp != NULL) {
8494 8544 ASSERT((mp->b_flag & (MSGMARKNEXT|MSGNOTMARKNEXT)) !=
8495 8545 (MSGMARKNEXT|MSGNOTMARKNEXT));
8496 8546 mp = mp->b_cont;
8497 8547 }
8498 8548 }
8499 8549 #endif
8500 8550 if (q->q_first == bp) {
8501 8551 short pollevents;
8502 8552
8503 8553 if (stp->sd_flag & RSLEEP) {
8504 8554 stp->sd_flag &= ~RSLEEP;
8505 8555 cv_broadcast(&q->q_wait);
8506 8556 }
8507 8557 if (stp->sd_flag & STRPRI) {
8508 8558 pollevents = POLLPRI;
8509 8559 } else {
8510 8560 if (band == 0) {
8511 8561 if (!(stp->sd_rput_opt & SR_POLLIN))
8512 8562 return;
8513 8563 stp->sd_rput_opt &= ~SR_POLLIN;
8514 8564 pollevents = POLLIN | POLLRDNORM;
8515 8565 } else {
8516 8566 pollevents = POLLIN | POLLRDBAND;
8517 8567 }
8518 8568 }
8519 8569 mutex_exit(&stp->sd_lock);
8520 8570 pollwakeup(&stp->sd_pollist, pollevents);
8521 8571 mutex_enter(&stp->sd_lock);
8522 8572 }
8523 8573 }
8524 8574
8525 8575 /*
8526 8576 * Return the held vnode attached to the stream head of a
8527 8577 * given queue
8528 8578 * It is the responsibility of the calling routine to ensure
8529 8579 * that the queue does not go away (e.g. pop).
8530 8580 */
8531 8581 vnode_t *
8532 8582 strq2vp(queue_t *qp)
8533 8583 {
8534 8584 vnode_t *vp;
8535 8585 vp = STREAM(qp)->sd_vnode;
8536 8586 ASSERT(vp != NULL);
8537 8587 VN_HOLD(vp);
8538 8588 return (vp);
8539 8589 }
8540 8590
8541 8591 /*
8542 8592 * return the stream head write queue for the given vp
8543 8593 * It is the responsibility of the calling routine to ensure
8544 8594 * that the stream or vnode do not close.
8545 8595 */
8546 8596 queue_t *
8547 8597 strvp2wq(vnode_t *vp)
8548 8598 {
8549 8599 ASSERT(vp->v_stream != NULL);
8550 8600 return (vp->v_stream->sd_wrq);
8551 8601 }
8552 8602
8553 8603 /*
8554 8604 * pollwakeup stream head
8555 8605 * It is the responsibility of the calling routine to ensure
8556 8606 * that the stream or vnode do not close.
8557 8607 */
8558 8608 void
8559 8609 strpollwakeup(vnode_t *vp, short event)
8560 8610 {
8561 8611 ASSERT(vp->v_stream);
8562 8612 pollwakeup(&vp->v_stream->sd_pollist, event);
8563 8613 }
8564 8614
8565 8615 /*
8566 8616 * Mate the stream heads of two vnodes together. If the two vnodes are the
8567 8617 * same, we just make the write-side point at the read-side -- otherwise,
8568 8618 * we do a full mate. Only works on vnodes associated with streams that are
8569 8619 * still being built and thus have only a stream head.
8570 8620 */
8571 8621 void
8572 8622 strmate(vnode_t *vp1, vnode_t *vp2)
8573 8623 {
8574 8624 queue_t *wrq1 = strvp2wq(vp1);
8575 8625 queue_t *wrq2 = strvp2wq(vp2);
8576 8626
8577 8627 /*
8578 8628 * Verify that there are no modules on the stream yet. We also
8579 8629 * rely on the stream head always having a service procedure to
8580 8630 * avoid tweaking q_nfsrv.
8581 8631 */
8582 8632 ASSERT(wrq1->q_next == NULL && wrq2->q_next == NULL);
8583 8633 ASSERT(wrq1->q_qinfo->qi_srvp != NULL);
8584 8634 ASSERT(wrq2->q_qinfo->qi_srvp != NULL);
8585 8635
8586 8636 /*
8587 8637 * If the queues are the same, just twist; otherwise do a full mate.
8588 8638 */
8589 8639 if (wrq1 == wrq2) {
8590 8640 wrq1->q_next = _RD(wrq1);
8591 8641 } else {
8592 8642 wrq1->q_next = _RD(wrq2);
8593 8643 wrq2->q_next = _RD(wrq1);
8594 8644 STREAM(wrq1)->sd_mate = STREAM(wrq2);
8595 8645 STREAM(wrq1)->sd_flag |= STRMATE;
8596 8646 STREAM(wrq2)->sd_mate = STREAM(wrq1);
8597 8647 STREAM(wrq2)->sd_flag |= STRMATE;
8598 8648 }
8599 8649 }
8600 8650
8601 8651 /*
8602 8652 * XXX will go away when console is correctly fixed.
8603 8653 * Clean up the console PIDS, from previous I_SETSIG,
8604 8654 * called only for cnopen which never calls strclean().
8605 8655 */
8606 8656 void
8607 8657 str_cn_clean(struct vnode *vp)
8608 8658 {
8609 8659 strsig_t *ssp, *pssp, *tssp;
8610 8660 struct stdata *stp;
8611 8661 struct pid *pidp;
8612 8662 int update = 0;
8613 8663
8614 8664 ASSERT(vp->v_stream);
8615 8665 stp = vp->v_stream;
8616 8666 pssp = NULL;
8617 8667 mutex_enter(&stp->sd_lock);
8618 8668 ssp = stp->sd_siglist;
8619 8669 while (ssp) {
8620 8670 mutex_enter(&pidlock);
8621 8671 pidp = ssp->ss_pidp;
8622 8672 /*
8623 8673 * Get rid of PID if the proc is gone.
8624 8674 */
8625 8675 if (pidp->pid_prinactive) {
8626 8676 tssp = ssp->ss_next;
8627 8677 if (pssp)
8628 8678 pssp->ss_next = tssp;
8629 8679 else
8630 8680 stp->sd_siglist = tssp;
8631 8681 ASSERT(pidp->pid_ref <= 1);
8632 8682 PID_RELE(ssp->ss_pidp);
8633 8683 mutex_exit(&pidlock);
8634 8684 kmem_free(ssp, sizeof (strsig_t));
8635 8685 update = 1;
8636 8686 ssp = tssp;
8637 8687 continue;
8638 8688 } else
8639 8689 mutex_exit(&pidlock);
8640 8690 pssp = ssp;
8641 8691 ssp = ssp->ss_next;
8642 8692 }
8643 8693 if (update) {
8644 8694 stp->sd_sigflags = 0;
8645 8695 for (ssp = stp->sd_siglist; ssp; ssp = ssp->ss_next)
8646 8696 stp->sd_sigflags |= ssp->ss_events;
8647 8697 }
8648 8698 mutex_exit(&stp->sd_lock);
8649 8699 }
8650 8700
8651 8701 /*
8652 8702 * Return B_TRUE if there is data in the message, B_FALSE otherwise.
8653 8703 */
8654 8704 static boolean_t
8655 8705 msghasdata(mblk_t *bp)
8656 8706 {
8657 8707 for (; bp; bp = bp->b_cont)
8658 8708 if (bp->b_datap->db_type == M_DATA) {
8659 8709 ASSERT(bp->b_wptr >= bp->b_rptr);
8660 8710 if (bp->b_wptr > bp->b_rptr)
8661 8711 return (B_TRUE);
8662 8712 }
8663 8713 return (B_FALSE);
8664 8714 }
↓ open down ↓ |
8099 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX