xref: /onnv-gate/usr/src/lib/libnsl/nsl/t_rcvreldata.c (revision 132:e3f7eaf7dde4)
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  */
22*132Srobinson 
230Sstevel@tonic-gate /*
24*132Srobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
250Sstevel@tonic-gate  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * t_rcvrel.c and t_rcvreldata.c are very similar and contain common code.
320Sstevel@tonic-gate  * Any changes to either of them should be reviewed to see whether they
330Sstevel@tonic-gate  * are applicable to the other file.
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate #include "mt.h"
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <stropts.h>
390Sstevel@tonic-gate #include <sys/stream.h>
400Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
410Sstevel@tonic-gate #include <sys/tihdr.h>
420Sstevel@tonic-gate #include <sys/timod.h>
430Sstevel@tonic-gate #include <xti.h>
440Sstevel@tonic-gate #include <signal.h>
450Sstevel@tonic-gate #include <syslog.h>
460Sstevel@tonic-gate #include <assert.h>
470Sstevel@tonic-gate #include "tx.h"
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /* ARGSUSED */
500Sstevel@tonic-gate int
_tx_rcvreldata(int fd,struct t_discon * discon,int api_semantics)510Sstevel@tonic-gate _tx_rcvreldata(int fd, struct t_discon *discon, int api_semantics)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	struct strbuf ctlbuf;
540Sstevel@tonic-gate 	struct strbuf databuf;
550Sstevel@tonic-gate 	int retval;
560Sstevel@tonic-gate 	union T_primitives *pptr;
570Sstevel@tonic-gate 	struct _ti_user *tiptr;
580Sstevel@tonic-gate 	int sv_errno;
590Sstevel@tonic-gate 	int didalloc, didralloc;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	int flg = 0;
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 
640Sstevel@tonic-gate 	assert(api_semantics == TX_XTI_XNS5_API);
65*132Srobinson 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0)
660Sstevel@tonic-gate 		return (-1);
670Sstevel@tonic-gate 	sig_mutex_lock(&tiptr->ti_lock);
680Sstevel@tonic-gate 
690Sstevel@tonic-gate 	if (tiptr->ti_servtype != T_COTS_ORD) {
700Sstevel@tonic-gate 		t_errno = TNOTSUPPORT;
710Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
720Sstevel@tonic-gate 		return (-1);
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 
75*132Srobinson 	if (!(tiptr->ti_state == T_DATAXFER ||
760Sstevel@tonic-gate 	    tiptr->ti_state == T_OUTREL)) {
770Sstevel@tonic-gate 		t_errno = TOUTSTATE;
780Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
790Sstevel@tonic-gate 		return (-1);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	if ((retval = _t_look_locked(fd, tiptr, 0, api_semantics)) < 0) {
830Sstevel@tonic-gate 		sv_errno = errno;
840Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
850Sstevel@tonic-gate 		errno = sv_errno;
860Sstevel@tonic-gate 		return (-1);
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (retval == T_DISCONNECT) {
900Sstevel@tonic-gate 		/*
910Sstevel@tonic-gate 		 * This ensures preference to T_DISCON_IND which is
920Sstevel@tonic-gate 		 * the design model for TPI
930Sstevel@tonic-gate 		 */
940Sstevel@tonic-gate 		t_errno = TLOOK;
950Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
960Sstevel@tonic-gate 		return (-1);
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	/*
1000Sstevel@tonic-gate 	 * Someday there could be transport providers that support T_ORDRELDATA
1010Sstevel@tonic-gate 	 * Until then this function behaves the same as t_rcvrel()
1020Sstevel@tonic-gate 	 * Note: Currently only mOSI ("minimal OSI") provider is specified
1030Sstevel@tonic-gate 	 * to use T_ORDRELDATA so probability of needing it is minimal.
1040Sstevel@tonic-gate 	 */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	if ((tiptr->ti_lookcnt > 0) &&
107*132Srobinson 	    /* LINTED pointer cast */
1080Sstevel@tonic-gate 	    (*((t_scalar_t *)tiptr->ti_lookbufs.tl_lookcbuf) == T_ORDREL_IND)) {
1090Sstevel@tonic-gate 		/*
1100Sstevel@tonic-gate 		 * Current look buffer event is T_ORDREL_IND.
1110Sstevel@tonic-gate 		 * Remove it from look buffer event list.
1120Sstevel@tonic-gate 		 */
1130Sstevel@tonic-gate 		_t_free_looklist_head(tiptr);
1140Sstevel@tonic-gate 		_T_TX_NEXTSTATE(T_RCVREL, tiptr,
1150Sstevel@tonic-gate 			"t_rcvreldata: invalid state event T_RCVREL");
1160Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1170Sstevel@tonic-gate 		return (0);
1180Sstevel@tonic-gate 	} else {
1190Sstevel@tonic-gate 		if (retval != T_ORDREL) {
1200Sstevel@tonic-gate 			t_errno = TNOREL;
1210Sstevel@tonic-gate 			sig_mutex_unlock(&tiptr->ti_lock);
1220Sstevel@tonic-gate 			return (-1);
1230Sstevel@tonic-gate 		}
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * get ordrel off read queue.
1280Sstevel@tonic-gate 	 * use ctl and rcv buffers
1290Sstevel@tonic-gate 	 *
1300Sstevel@tonic-gate 	 * Acquire ctlbuf for use in sending/receiving control part
1310Sstevel@tonic-gate 	 * of the message.
1320Sstevel@tonic-gate 	 */
1330Sstevel@tonic-gate 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
1340Sstevel@tonic-gate 		sv_errno = errno;
1350Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1360Sstevel@tonic-gate 		errno = sv_errno;
1370Sstevel@tonic-gate 		return (-1);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/*
1410Sstevel@tonic-gate 	 * Acquire databuf for use in sending/receiving data part
1420Sstevel@tonic-gate 	 */
1430Sstevel@tonic-gate 	if (_t_acquire_databuf(tiptr, &databuf, &didralloc) < 0) {
1440Sstevel@tonic-gate 		sv_errno = errno;
1450Sstevel@tonic-gate 		if (didalloc)
1460Sstevel@tonic-gate 			free(ctlbuf.buf);
1470Sstevel@tonic-gate 		else
1480Sstevel@tonic-gate 			tiptr->ti_ctlbuf = ctlbuf.buf;
1490Sstevel@tonic-gate 		sig_mutex_unlock(&tiptr->ti_lock);
1500Sstevel@tonic-gate 		errno = sv_errno;
1510Sstevel@tonic-gate 		return (-1);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/*
1550Sstevel@tonic-gate 	 * Since we have verified above that an orderly release event
1560Sstevel@tonic-gate 	 * is pending on this endpoint, we assume that this getmsg()
1570Sstevel@tonic-gate 	 * cannot block forever.
1580Sstevel@tonic-gate 	 */
1590Sstevel@tonic-gate 	do {
1600Sstevel@tonic-gate 		retval = getmsg(fd, &ctlbuf, &databuf, &flg);
1610Sstevel@tonic-gate 	} while (retval < 0 && errno == EINTR);
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if (retval < 0) {
1640Sstevel@tonic-gate 		t_errno = TSYSERR;
1650Sstevel@tonic-gate 		goto err_out;
1660Sstevel@tonic-gate 	}
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	/*
1690Sstevel@tonic-gate 	 * did I get entire message?
1700Sstevel@tonic-gate 	 */
1710Sstevel@tonic-gate 	if (retval > 0) {
1720Sstevel@tonic-gate 		t_errno = TSYSERR;
1730Sstevel@tonic-gate 		errno = EIO;
1740Sstevel@tonic-gate 		goto err_out;
1750Sstevel@tonic-gate 	}
176*132Srobinson 	/* LINTED pointer cast */
1770Sstevel@tonic-gate 	pptr = (union T_primitives *)ctlbuf.buf;
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	if (ctlbuf.len < (int)sizeof (struct T_ordrel_ind)) {
1800Sstevel@tonic-gate 		t_errno = TSYSERR;
1810Sstevel@tonic-gate 		errno = EPROTO;
1820Sstevel@tonic-gate 		goto err_out;
1830Sstevel@tonic-gate 	}
1840Sstevel@tonic-gate 	if (pptr->type != T_ORDREL_IND) {
1850Sstevel@tonic-gate 		if (pptr->type == T_DISCON_IND) {
1860Sstevel@tonic-gate 			/*
1870Sstevel@tonic-gate 			 * T_DISCON_IND gets priority following
1880Sstevel@tonic-gate 			 * TPI design philosphy.
1890Sstevel@tonic-gate 			 *
1900Sstevel@tonic-gate 			 * Add it to the events in the "look buffer"
1910Sstevel@tonic-gate 			 * list of events. This routine may defer signals.
1920Sstevel@tonic-gate 			 */
1930Sstevel@tonic-gate 			if (_t_register_lookevent(tiptr, databuf.buf,
1940Sstevel@tonic-gate 						databuf.len, ctlbuf.buf,
1950Sstevel@tonic-gate 						ctlbuf.len) < 0) {
1960Sstevel@tonic-gate 				t_errno = TSYSERR;
1970Sstevel@tonic-gate 				errno = ENOMEM;
1980Sstevel@tonic-gate 				goto err_out;
1990Sstevel@tonic-gate 			}
2000Sstevel@tonic-gate 			t_errno = TLOOK;
2010Sstevel@tonic-gate 			goto err_out;
2020Sstevel@tonic-gate 		} else {
2030Sstevel@tonic-gate 			t_errno = TSYSERR;
2040Sstevel@tonic-gate 			errno = EPROTO;
2050Sstevel@tonic-gate 			goto err_out;
2060Sstevel@tonic-gate 		}
2070Sstevel@tonic-gate 	}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 	_T_TX_NEXTSTATE(T_RCVREL, tiptr,
2100Sstevel@tonic-gate 		"t_rcvreldata: invalid state event T_RCVREL");
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	if (didalloc)
2130Sstevel@tonic-gate 		free(ctlbuf.buf);
2140Sstevel@tonic-gate 	else
2150Sstevel@tonic-gate 		tiptr->ti_ctlbuf = ctlbuf.buf;
2160Sstevel@tonic-gate 	if (didralloc)
2170Sstevel@tonic-gate 		free(databuf.buf);
2180Sstevel@tonic-gate 	else
2190Sstevel@tonic-gate 		tiptr->ti_rcvbuf = databuf.buf;
2200Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
2210Sstevel@tonic-gate 	return (0);
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate err_out:
2240Sstevel@tonic-gate 	sv_errno = errno;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if (didalloc)
2270Sstevel@tonic-gate 		free(ctlbuf.buf);
2280Sstevel@tonic-gate 	else
2290Sstevel@tonic-gate 		tiptr->ti_ctlbuf = ctlbuf.buf;
2300Sstevel@tonic-gate 	if (didralloc)
2310Sstevel@tonic-gate 		free(databuf.buf);
2320Sstevel@tonic-gate 	else
2330Sstevel@tonic-gate 		tiptr->ti_rcvbuf = databuf.buf;
2340Sstevel@tonic-gate 	sig_mutex_unlock(&tiptr->ti_lock);
2350Sstevel@tonic-gate 	errno = sv_errno;
2360Sstevel@tonic-gate 	return (-1);
2370Sstevel@tonic-gate }
238