4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
23 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
24 /* All Rights Reserved */
27 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.7 */
34 * t_rcv.c and t_rcvv.c are very similar and contain common code.
35 * Any changes to either of them should be reviewed to see whether they
36 * are applicable to the other file.
43 #include <sys/stream.h>
44 #define _SUN_TPI_VERSION 2
45 #include <sys/tihdr.h>
46 #include <sys/timod.h>
53 _tx_rcv(int fd
, char *buf
, unsigned nbytes
, int *flags
, int api_semantics
)
55 struct strbuf ctlbuf
, databuf
;
58 union T_primitives
*pptr
;
59 struct _ti_user
*tiptr
;
63 if ((tiptr
= _t_checkfd(fd
, 0, api_semantics
)) == NULL
)
65 sig_mutex_lock(&tiptr
->ti_lock
);
67 if (tiptr
->ti_servtype
== T_CLTS
) {
68 t_errno
= TNOTSUPPORT
;
69 sig_mutex_unlock(&tiptr
->ti_lock
);
73 if (_T_IS_XTI(api_semantics
)) {
75 * User level state verification only done for XTI
76 * because doing for TLI may break existing applications
78 if (!(tiptr
->ti_state
== T_DATAXFER
||
79 tiptr
->ti_state
== T_OUTREL
)) {
81 sig_mutex_unlock(&tiptr
->ti_lock
);
87 * Check in lookbuf for stuff
89 if (tiptr
->ti_lookcnt
> 0) {
91 * Implied preference rules give priority to
92 * T_DISCON_IND over T_ORDREL_IND. Also certain errors like
93 * data received after T_ORDREL_IND or a duplicate T_ORDREL_IND
94 * after a T_ORDRELING have priority over TLOOK.
95 * This manifests in following code behavior.
97 * (1) If something in lookbuf then check
98 * the stream head also. This may result
99 * in retuning a TLOOK error but only if there are
100 * - message at stream head but look buffer
101 * has a T_DISCON_IND event.
102 * - no messages are on the stream head
104 * (2) If there are messages on the stream head and
105 * all of them are T_ORDREL_IND(i.e. no message in
106 * look buffer is T_DISCON_IND), there
107 * could be data on stream head to be picked up and
108 * we work on the stream head and not return TLOOK.
109 * We remove the event on the stream head and queue it.
113 retval
= ioctl(fd
, I_NREAD
, &msglen
);
114 } while (retval
< 0 && errno
== EINTR
);
120 sig_mutex_unlock(&tiptr
->ti_lock
);
127 * If any T_DISCON_IND event in look buffer
128 * list then return TLOOK. Else continue
129 * processing as what could be on the stream
130 * head might be a possible T_DISCON_IND (which
131 * would have priority over the T_ORDREL_INDs
132 * on the look buffer.)
134 struct _ti_lookbufs
*tlbs
;
136 tlbs
= &tiptr
->ti_lookbufs
;
138 /* LINTED pointer cast */
139 if (*((t_scalar_t
*)tlbs
->tl_lookcbuf
)
142 sig_mutex_unlock(&tiptr
->ti_lock
);
145 } while ((tlbs
= tlbs
->tl_next
) != NULL
);
147 } else { /* retval == 0 */
149 * Nothing on stream head so whatever in
150 * look buffer has nothing that might override
154 sig_mutex_unlock(&tiptr
->ti_lock
);
160 * Acquire ctlbuf for use in sending/receiving control part
163 if (_t_acquire_ctlbuf(tiptr
, &ctlbuf
, &didalloc
) < 0) {
165 sig_mutex_unlock(&tiptr
->ti_lock
);
170 databuf
.maxlen
= nbytes
;
177 * This is a call that may block indefinitely so we drop the
178 * lock and allow signals in MT case here and reacquire it.
179 * Error case should roll back state changes done above
180 * (happens to be no state change here)
182 sig_mutex_unlock(&tiptr
->ti_lock
);
183 if ((retval
= getmsg(fd
, &ctlbuf
, &databuf
, &flg
)) < 0) {
189 sig_mutex_lock(&tiptr
->ti_lock
);
193 sig_mutex_lock(&tiptr
->ti_lock
);
195 assert((retval
& MORECTL
) == 0); /* MORECTL should not be on */
197 if (databuf
.len
== -1) databuf
.len
= 0;
199 if (ctlbuf
.len
> 0) {
200 if (ctlbuf
.len
< (int)sizeof (t_scalar_t
)) {
206 /* LINTED pointer cast */
207 pptr
= (union T_primitives
*)ctlbuf
.buf
;
209 switch (pptr
->type
) {
212 *flags
|= T_EXPEDITED
;
214 tiptr
->ti_flags
|= EXPEDITED
;
218 * Uses the fact T_DATA_IND and T_EXDATA_IND
221 if ((ctlbuf
.len
< (int)sizeof (struct T_data_ind
)) ||
222 (tiptr
->ti_lookcnt
> 0)) {
224 * ti_lookcnt > 0 implies data
225 * received after T_DISCON_IND or
226 * T_ORDREL_IND hence error
233 if ((pptr
->data_ind
.MORE_flag
) || retval
)
235 if ((pptr
->data_ind
.MORE_flag
) && retval
)
236 tiptr
->ti_flags
|= MORE
;
238 * No real state change on T_RCV event (noop)
240 * We invoke the macro only for error logging
241 * part of its capabilities when in a bad state.
243 _T_TX_NEXTSTATE(T_RCV
, tiptr
,
244 "t_rcv: invalid state event T_RCV");
248 tiptr
->ti_ctlbuf
= ctlbuf
.buf
;
249 sig_mutex_unlock(&tiptr
->ti_lock
);
250 return (databuf
.len
);
253 if (tiptr
->ti_lookcnt
> 0) {
255 * ti_lookcnt > 0 implies T_ORDREL_IND
256 * received after T_DISCON_IND or
257 * another T_ORDREL_IND hence error.
266 * Post event (T_ORDREL_IND/T_DISCON_IND) to
267 * the lookbuffer list.
270 if (_t_register_lookevent(tiptr
, databuf
.buf
,
272 ctlbuf
.buf
, ctlbuf
.len
) < 0) {
278 * We know that T_DISCON_IND is stored in
279 * last look buffer. If there is more data
280 * that follows, we try to append it to
281 * the same look buffer
283 if (retval
& MOREDATA
) {
284 ctlbuf
.maxlen
= 0; /* XXX why ? */
288 * XXX Will break (-ve maxlen) for
289 * transport provider with unbounded
290 * T_DISCON_IND data part (-1).
293 tiptr
->ti_rcvsize
- databuf
.len
;
297 tiptr
->ti_lookbufs
.tl_lookdbuf
+
298 tiptr
->ti_lookbufs
.tl_lookdlen
;
302 * Since MOREDATA was set, we assume
303 * that this getmsg will not block
307 retval
= getmsg(fd
, &ctlbuf
,
309 } while (retval
< 0 && errno
== EINTR
);
315 if (databuf
.len
== -1) databuf
.len
= 0;
317 /* MORECTL should not be on */
318 assert((retval
& MORECTL
) == 0);
321 * No support for unbounded data
328 tiptr
->ti_lookbufs
.tl_lookdlen
+=
343 } else { /* else for "if (ctlbuf.len > 0)" */
344 if (!retval
&& (tiptr
->ti_flags
& MORE
)) {
346 tiptr
->ti_flags
&= ~MORE
;
348 if (retval
& MOREDATA
)
352 * If inside an ETSDU, set expedited flag and turn
353 * of internal version when reach end of "ETIDU".
355 if (tiptr
->ti_flags
& EXPEDITED
) {
356 *flags
|= T_EXPEDITED
;
358 tiptr
->ti_flags
&= ~EXPEDITED
;
362 * No real state change on T_RCV events (It is a NOOP)
364 * We invoke the macro only for error logging
365 * part of its capabilities when in a bad state.
367 _T_TX_NEXTSTATE(T_RCV
, tiptr
,
368 "t_rcv: state invalid T_RCV event");
372 tiptr
->ti_ctlbuf
= ctlbuf
.buf
;
373 sig_mutex_unlock(&tiptr
->ti_lock
);
374 return (databuf
.len
);
383 tiptr
->ti_ctlbuf
= ctlbuf
.buf
;
384 sig_mutex_unlock(&tiptr
->ti_lock
);