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