import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / nsl / t_rcvv.c
blob0bdc0a18d1ffff93e9bbaa408b3220700606b402
1 /*
2 * CDDL HEADER START
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
7 * with the License.
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]
20 * CDDL HEADER END
24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
29 * t_rcv.c and t_rcvv.c are very similar and contain common code.
30 * Any changes to either of them should be reviewed to see whether they
31 * are applicable to the other file.
33 #include "mt.h"
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <stropts.h>
38 #include <sys/stream.h>
39 #define _SUN_TPI_VERSION 2
40 #include <sys/tihdr.h>
41 #include <sys/timod.h>
42 #include <xti.h>
43 #include <syslog.h>
44 #include <assert.h>
45 #include "tx.h"
47 int
48 _tx_rcvv(int fd, struct t_iovec *tiov, unsigned int tiovcount, int *flags,
49 int api_semantics)
51 struct strbuf ctlbuf, databuf;
52 int retval, flg = 0;
53 int msglen;
54 union T_primitives *pptr;
55 struct _ti_user *tiptr;
56 int sv_errno;
57 int didalloc;
58 unsigned int nbytes;
59 char *dataptr;
61 if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
62 return (-1);
63 sig_mutex_lock(&tiptr->ti_lock);
65 if (tiptr->ti_servtype == T_CLTS) {
66 t_errno = TNOTSUPPORT;
67 sig_mutex_unlock(&tiptr->ti_lock);
68 return (-1);
71 assert(api_semantics == TX_XTI_XNS5_API);
73 if (tiovcount == 0 || tiovcount > T_IOV_MAX) {
74 t_errno = TBADDATA;
75 sig_mutex_unlock(&tiptr->ti_lock);
76 return (-1);
79 if (!(tiptr->ti_state == T_DATAXFER ||
80 tiptr->ti_state == T_OUTREL)) {
81 t_errno = TOUTSTATE;
82 sig_mutex_unlock(&tiptr->ti_lock);
83 return (-1);
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.
112 do {
113 retval = ioctl(fd, I_NREAD, &msglen);
114 } while (retval < 0 && errno == EINTR);
116 if (retval < 0) {
117 sv_errno = errno;
118 t_errno = TSYSERR;
119 sig_mutex_unlock(&tiptr->ti_lock);
120 errno = sv_errno;
121 return (-1);
124 if (retval > 0) {
126 * If any T_DISCON_IND event in look buffer
127 * list then return TLOOK. Else continue
128 * processing as what could be on the stream
129 * head might be a possible T_DISCON_IND (which
130 * would have priority over the T_ORDREL_INDs
131 * on the look buffer.)
133 struct _ti_lookbufs *tlbs;
135 tlbs = &tiptr->ti_lookbufs;
136 do {
137 /* LINTED pointer cast */
138 if (*((t_scalar_t *)tlbs->tl_lookcbuf)
139 == T_DISCON_IND) {
140 t_errno = TLOOK;
141 sig_mutex_unlock(&tiptr->ti_lock);
142 return (-1);
144 } while ((tlbs = tlbs->tl_next) != NULL);
146 } else { /* retval == 0 */
148 * Nothing on stream head so whatever in
149 * look buffer has nothing that might override
150 * it.
152 t_errno = TLOOK;
153 sig_mutex_unlock(&tiptr->ti_lock);
154 return (-1);
159 * Acquire ctlbuf for use in sending/receiving control part
160 * of the message.
162 if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
163 sv_errno = errno;
164 sig_mutex_unlock(&tiptr->ti_lock);
165 errno = sv_errno;
166 return (-1);
169 nbytes = _t_bytecount_upto_intmax(tiov, tiovcount);
170 dataptr = NULL;
171 if (nbytes != 0 && ((dataptr = malloc(nbytes)) == NULL)) {
172 sv_errno = errno;
173 t_errno = TSYSERR;
174 if (didalloc)
175 free(ctlbuf.buf);
176 else
177 tiptr->ti_ctlbuf = ctlbuf.buf;
178 sig_mutex_unlock(&tiptr->ti_lock);
179 errno = sv_errno;
180 return (-1);
183 databuf.maxlen = (int)nbytes;
184 databuf.len = 0;
185 databuf.buf = dataptr;
187 *flags = 0;
190 * This is a call that may block indefinitely so we drop the
191 * lock and allow signals in MT case here and reacquire it.
192 * Error case should roll back state changes done above
193 * (happens to be no state change here)
195 sig_mutex_unlock(&tiptr->ti_lock);
196 if ((retval = getmsg(fd, &ctlbuf, &databuf, &flg)) < 0) {
197 if (errno == EAGAIN)
198 t_errno = TNODATA;
199 else
200 t_errno = TSYSERR;
201 sv_errno = errno;
202 sig_mutex_lock(&tiptr->ti_lock);
203 errno = sv_errno;
204 goto err_out;
206 sig_mutex_lock(&tiptr->ti_lock);
208 assert((retval & MORECTL) == 0); /* MORECTL should not be on */
210 if (databuf.len == -1) databuf.len = 0;
212 if (ctlbuf.len > 0) {
213 if (ctlbuf.len < (int)sizeof (t_scalar_t)) {
214 t_errno = TSYSERR;
215 errno = EPROTO;
216 goto err_out;
219 /* LINTED pointer cast */
220 pptr = (union T_primitives *)ctlbuf.buf;
222 switch (pptr->type) {
224 case T_EXDATA_IND:
225 *flags |= T_EXPEDITED;
226 if (retval > 0)
227 tiptr->ti_flags |= EXPEDITED;
228 /* FALLTHROUGH */
229 case T_DATA_IND:
231 * Uses the fact T_DATA_IND and T_EXDATA_IND
232 * are same in size
234 if ((ctlbuf.len < (int)sizeof (struct T_data_ind)) ||
235 (tiptr->ti_lookcnt > 0)) {
237 * ti_lookcnt > 0 implies data
238 * received after T_DISCON_IND or
239 * T_ORDREL_IND hence error
241 t_errno = TSYSERR;
242 errno = EPROTO;
243 goto err_out;
246 if ((pptr->data_ind.MORE_flag) || retval)
247 *flags |= T_MORE;
248 if ((pptr->data_ind.MORE_flag) && retval)
249 tiptr->ti_flags |= MORE;
251 * No real state change on T_RCV event (noop)
253 * We invoke the macro only for error logging
254 * part of its capabilities when in a bad state.
256 _T_TX_NEXTSTATE(T_RCV, tiptr,
257 "t_rcvv: invalid state event T_RCV");
258 if (didalloc)
259 free(ctlbuf.buf);
260 else
261 tiptr->ti_ctlbuf = ctlbuf.buf;
262 _t_scatter(&databuf, tiov, tiovcount);
263 free(dataptr);
264 sig_mutex_unlock(&tiptr->ti_lock);
265 return (databuf.len);
267 case T_ORDREL_IND:
268 if (tiptr->ti_lookcnt > 0) {
270 * ti_lookcnt > 0 implies T_ORDREL_IND
271 * received after T_DISCON_IND or
272 * another T_ORDREL_IND hence error.
274 t_errno = TSYSERR;
275 errno = EPROTO;
276 goto err_out;
278 /* FALLTHROUGH */
279 case T_DISCON_IND:
281 * Post event (T_ORDREL_IND/T_DISCON_IND) to
282 * the lookbuffer list.
285 if (_t_register_lookevent(tiptr, databuf.buf,
286 databuf.len,
287 ctlbuf.buf, ctlbuf.len) < 0) {
288 t_errno = TSYSERR;
289 errno = ENOMEM;
290 goto err_out;
293 * We know that T_DISCON_IND is stored in
294 * last look buffer. If there is more data
295 * that follows, we try to append it to
296 * the same look buffer
298 if (retval & MOREDATA) {
299 ctlbuf.maxlen = 0; /* XXX why ? */
300 ctlbuf.len = 0;
303 * XXX Will break (-ve maxlen) for
304 * transport provider with unbounded
305 * T_DISCON_IND data part (-1).
307 databuf.maxlen =
308 tiptr->ti_rcvsize - databuf.len;
310 databuf.len = 0;
311 databuf.buf =
312 tiptr->ti_lookbufs.tl_lookdbuf +
313 tiptr->ti_lookbufs.tl_lookdlen;
314 *flags = 0;
317 * Since MOREDATA was set, we assume
318 * that this getmsg will not block
319 * indefinitely
321 do {
322 retval = getmsg(fd, &ctlbuf,
323 &databuf, &flg);
324 } while (retval < 0 && errno == EINTR);
326 if (retval < 0) {
327 t_errno = TSYSERR;
328 goto err_out;
330 if (databuf.len == -1) databuf.len = 0;
331 if (retval > 0) {
332 /* MORECTL should not be on */
333 assert((retval & MORECTL) == 0);
335 * XXX - Why ?
336 * No support for unbounded data
337 * on T_DISCON_IND ?
339 t_errno = TSYSERR;
340 errno = EPROTO;
341 goto err_out;
343 tiptr->ti_lookbufs.tl_lookdlen +=
344 databuf.len;
347 t_errno = TLOOK;
348 goto err_out;
350 default:
351 break;
354 t_errno = TSYSERR;
355 errno = EPROTO;
356 goto err_out;
358 } else { /* else for "if (ctlbuf.len > 0)" */
359 if (!retval && (tiptr->ti_flags & MORE)) {
360 *flags |= T_MORE;
361 tiptr->ti_flags &= ~MORE;
363 if (retval & MOREDATA)
364 *flags |= T_MORE;
367 * If inside an ETSDU, set expedited flag and turn
368 * of internal version when reach end of "ETIDU".
370 if (tiptr->ti_flags & EXPEDITED) {
371 *flags |= T_EXPEDITED;
372 if (!retval)
373 tiptr->ti_flags &= ~EXPEDITED;
377 * No real state change on T_RCV events (It is a NOOP)
379 * We invoke the macro only for error logging
380 * part of its capabilities when in a bad state.
382 _T_TX_NEXTSTATE(T_RCV, tiptr,
383 "t_rcvv: state invalid T_RCV event");
384 if (didalloc)
385 free(ctlbuf.buf);
386 else
387 tiptr->ti_ctlbuf = ctlbuf.buf;
388 _t_scatter(&databuf, tiov, tiovcount);
389 free(dataptr);
390 sig_mutex_unlock(&tiptr->ti_lock);
391 return (databuf.len);
393 /* NOTREACHED */
395 err_out:
396 sv_errno = errno;
397 if (didalloc)
398 free(ctlbuf.buf);
399 else
400 tiptr->ti_ctlbuf = ctlbuf.buf;
401 free(dataptr);
402 sig_mutex_unlock(&tiptr->ti_lock);
404 errno = sv_errno;
405 return (-1);