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