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