10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
22132Srobinson
230Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
240Sstevel@tonic-gate /* All Rights Reserved */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
27*1219Sraf * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
280Sstevel@tonic-gate * Use is subject to license terms.
290Sstevel@tonic-gate */
300Sstevel@tonic-gate
310Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.7 */
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * t_rcv.c and t_rcvv.c are very similar and contain common code.
350Sstevel@tonic-gate * Any changes to either of them should be reviewed to see whether they
360Sstevel@tonic-gate * are applicable to the other file.
370Sstevel@tonic-gate */
380Sstevel@tonic-gate #include "mt.h"
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <errno.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <stropts.h>
430Sstevel@tonic-gate #include <sys/stream.h>
440Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
450Sstevel@tonic-gate #include <sys/tihdr.h>
460Sstevel@tonic-gate #include <sys/timod.h>
470Sstevel@tonic-gate #include <xti.h>
480Sstevel@tonic-gate #include <syslog.h>
490Sstevel@tonic-gate #include <assert.h>
500Sstevel@tonic-gate #include "tx.h"
510Sstevel@tonic-gate
520Sstevel@tonic-gate int
_tx_rcv(int fd,char * buf,unsigned nbytes,int * flags,int api_semantics)530Sstevel@tonic-gate _tx_rcv(int fd, char *buf, unsigned nbytes, int *flags, int api_semantics)
540Sstevel@tonic-gate {
550Sstevel@tonic-gate struct strbuf ctlbuf, databuf;
560Sstevel@tonic-gate int retval, flg = 0;
570Sstevel@tonic-gate int msglen;
580Sstevel@tonic-gate union T_primitives *pptr;
590Sstevel@tonic-gate struct _ti_user *tiptr;
600Sstevel@tonic-gate int sv_errno;
610Sstevel@tonic-gate int didalloc;
620Sstevel@tonic-gate
63132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
640Sstevel@tonic-gate return (-1);
650Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
660Sstevel@tonic-gate
670Sstevel@tonic-gate if (tiptr->ti_servtype == T_CLTS) {
680Sstevel@tonic-gate t_errno = TNOTSUPPORT;
690Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
700Sstevel@tonic-gate return (-1);
710Sstevel@tonic-gate }
720Sstevel@tonic-gate
730Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
740Sstevel@tonic-gate /*
750Sstevel@tonic-gate * User level state verification only done for XTI
760Sstevel@tonic-gate * because doing for TLI may break existing applications
770Sstevel@tonic-gate */
78132Srobinson if (!(tiptr->ti_state == T_DATAXFER ||
790Sstevel@tonic-gate tiptr->ti_state == T_OUTREL)) {
800Sstevel@tonic-gate t_errno = TOUTSTATE;
810Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
820Sstevel@tonic-gate return (-1);
830Sstevel@tonic-gate }
840Sstevel@tonic-gate }
850Sstevel@tonic-gate
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate * Check in lookbuf for stuff
880Sstevel@tonic-gate */
890Sstevel@tonic-gate if (tiptr->ti_lookcnt > 0) {
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate * Implied preference rules give priority to
920Sstevel@tonic-gate * T_DISCON_IND over T_ORDREL_IND. Also certain errors like
930Sstevel@tonic-gate * data received after T_ORDREL_IND or a duplicate T_ORDREL_IND
940Sstevel@tonic-gate * after a T_ORDRELING have priority over TLOOK.
950Sstevel@tonic-gate * This manifests in following code behavior.
960Sstevel@tonic-gate *
970Sstevel@tonic-gate * (1) If something in lookbuf then check
980Sstevel@tonic-gate * the stream head also. This may result
990Sstevel@tonic-gate * in retuning a TLOOK error but only if there are
1000Sstevel@tonic-gate * - message at stream head but look buffer
1010Sstevel@tonic-gate * has a T_DISCON_IND event.
1020Sstevel@tonic-gate * - no messages are on the stream head
1030Sstevel@tonic-gate *
1040Sstevel@tonic-gate * (2) If there are messages on the stream head and
1050Sstevel@tonic-gate * all of them are T_ORDREL_IND(i.e. no message in
1060Sstevel@tonic-gate * look buffer is T_DISCON_IND), there
1070Sstevel@tonic-gate * could be data on stream head to be picked up and
1080Sstevel@tonic-gate * we work on the stream head and not return TLOOK.
1090Sstevel@tonic-gate * We remove the event on the stream head and queue it.
1100Sstevel@tonic-gate *
1110Sstevel@tonic-gate */
1120Sstevel@tonic-gate do {
113*1219Sraf retval = ioctl(fd, I_NREAD, &msglen);
1140Sstevel@tonic-gate } while (retval < 0 && errno == EINTR);
1150Sstevel@tonic-gate
1160Sstevel@tonic-gate if (retval < 0) {
1170Sstevel@tonic-gate sv_errno = errno;
1180Sstevel@tonic-gate
1190Sstevel@tonic-gate t_errno = TSYSERR;
1200Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1210Sstevel@tonic-gate errno = sv_errno;
1220Sstevel@tonic-gate return (-1);
1230Sstevel@tonic-gate }
1240Sstevel@tonic-gate
1250Sstevel@tonic-gate if (retval > 0) {
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate * If any T_DISCON_IND event in look buffer
1280Sstevel@tonic-gate * list then return TLOOK. Else continue
1290Sstevel@tonic-gate * processing as what could be on the stream
1300Sstevel@tonic-gate * head might be a possible T_DISCON_IND (which
1310Sstevel@tonic-gate * would have priority over the T_ORDREL_INDs
1320Sstevel@tonic-gate * on the look buffer.)
1330Sstevel@tonic-gate */
1340Sstevel@tonic-gate struct _ti_lookbufs *tlbs;
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate tlbs = &tiptr->ti_lookbufs;
1370Sstevel@tonic-gate do {
138132Srobinson /* LINTED pointer cast */
1390Sstevel@tonic-gate if (*((t_scalar_t *)tlbs->tl_lookcbuf)
1400Sstevel@tonic-gate == T_DISCON_IND) {
1410Sstevel@tonic-gate t_errno = TLOOK;
1420Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1430Sstevel@tonic-gate return (-1);
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate } while ((tlbs = tlbs->tl_next) != NULL);
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate } else { /* retval == 0 */
1480Sstevel@tonic-gate /*
1490Sstevel@tonic-gate * Nothing on stream head so whatever in
1500Sstevel@tonic-gate * look buffer has nothing that might override
1510Sstevel@tonic-gate * it.
1520Sstevel@tonic-gate */
1530Sstevel@tonic-gate t_errno = TLOOK;
1540Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1550Sstevel@tonic-gate return (-1);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate /*
1600Sstevel@tonic-gate * Acquire ctlbuf for use in sending/receiving control part
1610Sstevel@tonic-gate * of the message.
1620Sstevel@tonic-gate */
1630Sstevel@tonic-gate if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
1640Sstevel@tonic-gate sv_errno = errno;
1650Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1660Sstevel@tonic-gate errno = sv_errno;
1670Sstevel@tonic-gate return (-1);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate databuf.maxlen = nbytes;
1710Sstevel@tonic-gate databuf.len = 0;
1720Sstevel@tonic-gate databuf.buf = buf;
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate *flags = 0;
1750Sstevel@tonic-gate
1760Sstevel@tonic-gate /*
1770Sstevel@tonic-gate * This is a call that may block indefinitely so we drop the
1780Sstevel@tonic-gate * lock and allow signals in MT case here and reacquire it.
1790Sstevel@tonic-gate * Error case should roll back state changes done above
1800Sstevel@tonic-gate * (happens to be no state change here)
1810Sstevel@tonic-gate */
1820Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1830Sstevel@tonic-gate if ((retval = getmsg(fd, &ctlbuf, &databuf, &flg)) < 0) {
1840Sstevel@tonic-gate if (errno == EAGAIN)
1850Sstevel@tonic-gate t_errno = TNODATA;
1860Sstevel@tonic-gate else
1870Sstevel@tonic-gate t_errno = TSYSERR;
1880Sstevel@tonic-gate sv_errno = errno;
1890Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
1900Sstevel@tonic-gate errno = sv_errno;
1910Sstevel@tonic-gate goto err_out;
1920Sstevel@tonic-gate }
1930Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
1940Sstevel@tonic-gate
1950Sstevel@tonic-gate assert((retval & MORECTL) == 0); /* MORECTL should not be on */
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if (databuf.len == -1) databuf.len = 0;
1980Sstevel@tonic-gate
1990Sstevel@tonic-gate if (ctlbuf.len > 0) {
2000Sstevel@tonic-gate if (ctlbuf.len < (int)sizeof (t_scalar_t)) {
2010Sstevel@tonic-gate t_errno = TSYSERR;
2020Sstevel@tonic-gate errno = EPROTO;
2030Sstevel@tonic-gate goto err_out;
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate
206132Srobinson /* LINTED pointer cast */
2070Sstevel@tonic-gate pptr = (union T_primitives *)ctlbuf.buf;
2080Sstevel@tonic-gate
2090Sstevel@tonic-gate switch (pptr->type) {
2100Sstevel@tonic-gate
2110Sstevel@tonic-gate case T_EXDATA_IND:
2120Sstevel@tonic-gate *flags |= T_EXPEDITED;
2130Sstevel@tonic-gate if (retval > 0)
2140Sstevel@tonic-gate tiptr->ti_flags |= EXPEDITED;
2150Sstevel@tonic-gate /* FALLTHROUGH */
2160Sstevel@tonic-gate case T_DATA_IND:
2170Sstevel@tonic-gate /*
2180Sstevel@tonic-gate * Uses the fact T_DATA_IND and T_EXDATA_IND
2190Sstevel@tonic-gate * are same in size
2200Sstevel@tonic-gate */
2210Sstevel@tonic-gate if ((ctlbuf.len < (int)sizeof (struct T_data_ind)) ||
2220Sstevel@tonic-gate (tiptr->ti_lookcnt > 0)) {
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate * ti_lookcnt > 0 implies data
2250Sstevel@tonic-gate * received after T_DISCON_IND or
2260Sstevel@tonic-gate * T_ORDREL_IND hence error
2270Sstevel@tonic-gate */
2280Sstevel@tonic-gate t_errno = TSYSERR;
2290Sstevel@tonic-gate errno = EPROTO;
2300Sstevel@tonic-gate goto err_out;
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate
2330Sstevel@tonic-gate if ((pptr->data_ind.MORE_flag) || retval)
2340Sstevel@tonic-gate *flags |= T_MORE;
2350Sstevel@tonic-gate if ((pptr->data_ind.MORE_flag) && retval)
2360Sstevel@tonic-gate tiptr->ti_flags |= MORE;
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate * No real state change on T_RCV event (noop)
2390Sstevel@tonic-gate *
2400Sstevel@tonic-gate * We invoke the macro only for error logging
2410Sstevel@tonic-gate * part of its capabilities when in a bad state.
2420Sstevel@tonic-gate */
2430Sstevel@tonic-gate _T_TX_NEXTSTATE(T_RCV, tiptr,
2440Sstevel@tonic-gate "t_rcv: invalid state event T_RCV");
2450Sstevel@tonic-gate if (didalloc)
2460Sstevel@tonic-gate free(ctlbuf.buf);
2470Sstevel@tonic-gate else
2480Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
2490Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2500Sstevel@tonic-gate return (databuf.len);
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate case T_ORDREL_IND:
2530Sstevel@tonic-gate if (tiptr->ti_lookcnt > 0) {
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate * ti_lookcnt > 0 implies T_ORDREL_IND
2560Sstevel@tonic-gate * received after T_DISCON_IND or
2570Sstevel@tonic-gate * another T_ORDREL_IND hence error.
2580Sstevel@tonic-gate */
2590Sstevel@tonic-gate t_errno = TSYSERR;
2600Sstevel@tonic-gate errno = EPROTO;
2610Sstevel@tonic-gate goto err_out;
2620Sstevel@tonic-gate }
2630Sstevel@tonic-gate /* FALLTHROUGH */
2640Sstevel@tonic-gate case T_DISCON_IND:
2650Sstevel@tonic-gate /*
2660Sstevel@tonic-gate * Post event (T_ORDREL_IND/T_DISCON_IND) to
2670Sstevel@tonic-gate * the lookbuffer list.
2680Sstevel@tonic-gate */
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate if (_t_register_lookevent(tiptr, databuf.buf,
2710Sstevel@tonic-gate databuf.len,
2720Sstevel@tonic-gate ctlbuf.buf, ctlbuf.len) < 0) {
2730Sstevel@tonic-gate t_errno = TSYSERR;
2740Sstevel@tonic-gate errno = ENOMEM;
2750Sstevel@tonic-gate goto err_out;
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate /*
2780Sstevel@tonic-gate * We know that T_DISCON_IND is stored in
2790Sstevel@tonic-gate * last look buffer. If there is more data
2800Sstevel@tonic-gate * that follows, we try to append it to
2810Sstevel@tonic-gate * the same look buffer
2820Sstevel@tonic-gate */
2830Sstevel@tonic-gate if (retval & MOREDATA) {
2840Sstevel@tonic-gate ctlbuf.maxlen = 0; /* XXX why ? */
2850Sstevel@tonic-gate ctlbuf.len = 0;
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate * XXX Will break (-ve maxlen) for
2890Sstevel@tonic-gate * transport provider with unbounded
2900Sstevel@tonic-gate * T_DISCON_IND data part (-1).
2910Sstevel@tonic-gate */
2920Sstevel@tonic-gate databuf.maxlen =
2930Sstevel@tonic-gate tiptr->ti_rcvsize - databuf.len;
2940Sstevel@tonic-gate
2950Sstevel@tonic-gate databuf.len = 0;
2960Sstevel@tonic-gate databuf.buf =
2970Sstevel@tonic-gate tiptr->ti_lookbufs.tl_lookdbuf +
2980Sstevel@tonic-gate tiptr->ti_lookbufs.tl_lookdlen;
2990Sstevel@tonic-gate *flags = 0;
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate /*
3020Sstevel@tonic-gate * Since MOREDATA was set, we assume
3030Sstevel@tonic-gate * that this getmsg will not block
3040Sstevel@tonic-gate * indefinitely
3050Sstevel@tonic-gate */
3060Sstevel@tonic-gate do {
3070Sstevel@tonic-gate retval = getmsg(fd, &ctlbuf,
3080Sstevel@tonic-gate &databuf, &flg);
3090Sstevel@tonic-gate } while (retval < 0 && errno == EINTR);
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate if (retval < 0) {
3120Sstevel@tonic-gate t_errno = TSYSERR;
3130Sstevel@tonic-gate goto err_out;
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate if (databuf.len == -1) databuf.len = 0;
3160Sstevel@tonic-gate if (retval > 0) {
3170Sstevel@tonic-gate /* MORECTL should not be on */
3180Sstevel@tonic-gate assert((retval & MORECTL) == 0);
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate * XXX - Why ?
3210Sstevel@tonic-gate * No support for unbounded data
3220Sstevel@tonic-gate * on T_DISCON_IND ?
3230Sstevel@tonic-gate */
3240Sstevel@tonic-gate t_errno = TSYSERR;
3250Sstevel@tonic-gate errno = EPROTO;
3260Sstevel@tonic-gate goto err_out;
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate tiptr->ti_lookbufs.tl_lookdlen +=
3290Sstevel@tonic-gate databuf.len;
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate
3320Sstevel@tonic-gate t_errno = TLOOK;
3330Sstevel@tonic-gate goto err_out;
3340Sstevel@tonic-gate
3350Sstevel@tonic-gate default:
3360Sstevel@tonic-gate break;
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate
3390Sstevel@tonic-gate t_errno = TSYSERR;
3400Sstevel@tonic-gate errno = EPROTO;
3410Sstevel@tonic-gate goto err_out;
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate } else { /* else for "if (ctlbuf.len > 0)" */
3440Sstevel@tonic-gate if (!retval && (tiptr->ti_flags & MORE)) {
3450Sstevel@tonic-gate *flags |= T_MORE;
3460Sstevel@tonic-gate tiptr->ti_flags &= ~MORE;
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate if (retval & MOREDATA)
3490Sstevel@tonic-gate *flags |= T_MORE;
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate * If inside an ETSDU, set expedited flag and turn
3530Sstevel@tonic-gate * of internal version when reach end of "ETIDU".
3540Sstevel@tonic-gate */
3550Sstevel@tonic-gate if (tiptr->ti_flags & EXPEDITED) {
3560Sstevel@tonic-gate *flags |= T_EXPEDITED;
3570Sstevel@tonic-gate if (!retval)
3580Sstevel@tonic-gate tiptr->ti_flags &= ~EXPEDITED;
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate /*
3620Sstevel@tonic-gate * No real state change on T_RCV events (It is a NOOP)
3630Sstevel@tonic-gate *
3640Sstevel@tonic-gate * We invoke the macro only for error logging
3650Sstevel@tonic-gate * part of its capabilities when in a bad state.
3660Sstevel@tonic-gate */
3670Sstevel@tonic-gate _T_TX_NEXTSTATE(T_RCV, tiptr,
3680Sstevel@tonic-gate "t_rcv: state invalid T_RCV event");
3690Sstevel@tonic-gate if (didalloc)
3700Sstevel@tonic-gate free(ctlbuf.buf);
3710Sstevel@tonic-gate else
3720Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
3730Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
3740Sstevel@tonic-gate return (databuf.len);
3750Sstevel@tonic-gate }
3760Sstevel@tonic-gate /* NOTREACHED */
3770Sstevel@tonic-gate
3780Sstevel@tonic-gate err_out:
3790Sstevel@tonic-gate sv_errno = errno;
3800Sstevel@tonic-gate if (didalloc)
3810Sstevel@tonic-gate free(ctlbuf.buf);
3820Sstevel@tonic-gate else
3830Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
3840Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate errno = sv_errno;
3870Sstevel@tonic-gate return (-1);
3880Sstevel@tonic-gate }
389