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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
240Sstevel@tonic-gate /* All Rights Reserved */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
27*132Srobinson * Copyright 2005 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.3.1 */
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * t_rcvrel.c and t_rcvreldata.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 <stropts.h>
420Sstevel@tonic-gate #include <sys/stream.h>
430Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
440Sstevel@tonic-gate #include <sys/tihdr.h>
450Sstevel@tonic-gate #include <sys/timod.h>
460Sstevel@tonic-gate #include <xti.h>
470Sstevel@tonic-gate #include <signal.h>
480Sstevel@tonic-gate #include <syslog.h>
490Sstevel@tonic-gate #include "tx.h"
500Sstevel@tonic-gate
510Sstevel@tonic-gate int
_tx_rcvrel(int fd,int api_semantics)520Sstevel@tonic-gate _tx_rcvrel(int fd, int api_semantics)
530Sstevel@tonic-gate {
540Sstevel@tonic-gate struct strbuf ctlbuf;
550Sstevel@tonic-gate struct strbuf databuf;
560Sstevel@tonic-gate int retval;
570Sstevel@tonic-gate union T_primitives *pptr;
580Sstevel@tonic-gate struct _ti_user *tiptr;
590Sstevel@tonic-gate int sv_errno;
600Sstevel@tonic-gate int didalloc, didralloc;
610Sstevel@tonic-gate
620Sstevel@tonic-gate int flg = 0;
630Sstevel@tonic-gate
64*132Srobinson if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0)
650Sstevel@tonic-gate return (-1);
660Sstevel@tonic-gate sig_mutex_lock(&tiptr->ti_lock);
670Sstevel@tonic-gate
680Sstevel@tonic-gate if (tiptr->ti_servtype != T_COTS_ORD) {
690Sstevel@tonic-gate t_errno = TNOTSUPPORT;
700Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
710Sstevel@tonic-gate return (-1);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate
740Sstevel@tonic-gate if (_T_IS_XTI(api_semantics)) {
750Sstevel@tonic-gate /*
760Sstevel@tonic-gate * User level state verification only done for XTI
770Sstevel@tonic-gate * because doing for TLI may break existing applications
780Sstevel@tonic-gate */
79*132Srobinson if (!(tiptr->ti_state == T_DATAXFER ||
800Sstevel@tonic-gate tiptr->ti_state == T_OUTREL)) {
810Sstevel@tonic-gate t_errno = TOUTSTATE;
820Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
830Sstevel@tonic-gate return (-1);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate }
860Sstevel@tonic-gate
870Sstevel@tonic-gate if ((retval = _t_look_locked(fd, tiptr, 0, api_semantics)) < 0) {
880Sstevel@tonic-gate sv_errno = errno;
890Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
900Sstevel@tonic-gate errno = sv_errno;
910Sstevel@tonic-gate return (-1);
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate if (retval == T_DISCONNECT) {
950Sstevel@tonic-gate /*
960Sstevel@tonic-gate * This ensures preference to T_DISCON_IND which is
970Sstevel@tonic-gate * the design model for TPI
980Sstevel@tonic-gate */
990Sstevel@tonic-gate t_errno = TLOOK;
1000Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1010Sstevel@tonic-gate return (-1);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
1040Sstevel@tonic-gate if ((tiptr->ti_lookcnt > 0) &&
105*132Srobinson /* LINTED pointer cast */
1060Sstevel@tonic-gate (*((t_scalar_t *)tiptr->ti_lookbufs.tl_lookcbuf) == T_ORDREL_IND)) {
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate * Current look buffer event is T_ORDREL_IND.
1090Sstevel@tonic-gate * Remove it from look buffer event list.
1100Sstevel@tonic-gate */
1110Sstevel@tonic-gate _t_free_looklist_head(tiptr);
1120Sstevel@tonic-gate _T_TX_NEXTSTATE(T_RCVREL, tiptr,
1130Sstevel@tonic-gate "t_rcv: invalid state event T_RCVREL");
1140Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1150Sstevel@tonic-gate return (0);
1160Sstevel@tonic-gate } else {
1170Sstevel@tonic-gate if (retval != T_ORDREL) {
1180Sstevel@tonic-gate t_errno = TNOREL;
1190Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1200Sstevel@tonic-gate return (-1);
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate }
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate * get ordrel off read queue.
1260Sstevel@tonic-gate * use ctl and rcv buffers
1270Sstevel@tonic-gate *
1280Sstevel@tonic-gate * Acquire ctlbuf for use in sending/receiving control part
1290Sstevel@tonic-gate * of the message.
1300Sstevel@tonic-gate */
1310Sstevel@tonic-gate if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
1320Sstevel@tonic-gate sv_errno = errno;
1330Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1340Sstevel@tonic-gate errno = sv_errno;
1350Sstevel@tonic-gate return (-1);
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate /*
1390Sstevel@tonic-gate * Acquire databuf for use in sending/receiving data part
1400Sstevel@tonic-gate */
1410Sstevel@tonic-gate if (_t_acquire_databuf(tiptr, &databuf, &didralloc) < 0) {
1420Sstevel@tonic-gate sv_errno = errno;
1430Sstevel@tonic-gate if (didalloc)
1440Sstevel@tonic-gate free(ctlbuf.buf);
1450Sstevel@tonic-gate else
1460Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
1470Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
1480Sstevel@tonic-gate errno = sv_errno;
1490Sstevel@tonic-gate return (-1);
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate /*
1530Sstevel@tonic-gate * Since we have verified above that an orderly release event
1540Sstevel@tonic-gate * is pending on this endpoint, we assume that this getmsg()
1550Sstevel@tonic-gate * cannot block forever.
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate do {
1580Sstevel@tonic-gate retval = getmsg(fd, &ctlbuf, &databuf, &flg);
1590Sstevel@tonic-gate } while (retval < 0 && errno == EINTR);
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate if (retval < 0) {
1620Sstevel@tonic-gate t_errno = TSYSERR;
1630Sstevel@tonic-gate goto err_out;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate * did I get entire message?
1680Sstevel@tonic-gate */
1690Sstevel@tonic-gate if (retval > 0) {
1700Sstevel@tonic-gate t_errno = TSYSERR;
1710Sstevel@tonic-gate errno = EIO;
1720Sstevel@tonic-gate goto err_out;
1730Sstevel@tonic-gate }
174*132Srobinson /* LINTED pointer cast */
1750Sstevel@tonic-gate pptr = (union T_primitives *)ctlbuf.buf;
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate if (ctlbuf.len < (int)sizeof (struct T_ordrel_ind)) {
1780Sstevel@tonic-gate t_errno = TSYSERR;
1790Sstevel@tonic-gate errno = EPROTO;
1800Sstevel@tonic-gate goto err_out;
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate if (pptr->type != T_ORDREL_IND) {
1830Sstevel@tonic-gate if (pptr->type == T_DISCON_IND) {
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate * T_DISCON_IND gets priority following
1860Sstevel@tonic-gate * TPI design philosphy.
1870Sstevel@tonic-gate *
1880Sstevel@tonic-gate * Add it to the events in the "look buffer"
1890Sstevel@tonic-gate * list of events. This routine may defer signals.
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate if (_t_register_lookevent(tiptr, databuf.buf,
1920Sstevel@tonic-gate databuf.len, ctlbuf.buf,
1930Sstevel@tonic-gate ctlbuf.len) < 0) {
1940Sstevel@tonic-gate t_errno = TSYSERR;
1950Sstevel@tonic-gate errno = ENOMEM;
1960Sstevel@tonic-gate goto err_out;
1970Sstevel@tonic-gate }
1980Sstevel@tonic-gate t_errno = TLOOK;
1990Sstevel@tonic-gate goto err_out;
2000Sstevel@tonic-gate } else {
2010Sstevel@tonic-gate t_errno = TSYSERR;
2020Sstevel@tonic-gate errno = EPROTO;
2030Sstevel@tonic-gate goto err_out;
2040Sstevel@tonic-gate }
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate _T_TX_NEXTSTATE(T_RCVREL, tiptr,
2080Sstevel@tonic-gate "t_rcvrel: invalid state event T_RCVREL");
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate if (didalloc)
2110Sstevel@tonic-gate free(ctlbuf.buf);
2120Sstevel@tonic-gate else
2130Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
2140Sstevel@tonic-gate if (didralloc)
2150Sstevel@tonic-gate free(databuf.buf);
2160Sstevel@tonic-gate else
2170Sstevel@tonic-gate tiptr->ti_rcvbuf = databuf.buf;
2180Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2190Sstevel@tonic-gate return (0);
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate err_out:
2220Sstevel@tonic-gate sv_errno = errno;
2230Sstevel@tonic-gate
2240Sstevel@tonic-gate if (didalloc)
2250Sstevel@tonic-gate free(ctlbuf.buf);
2260Sstevel@tonic-gate else
2270Sstevel@tonic-gate tiptr->ti_ctlbuf = ctlbuf.buf;
2280Sstevel@tonic-gate if (didralloc)
2290Sstevel@tonic-gate free(databuf.buf);
2300Sstevel@tonic-gate else
2310Sstevel@tonic-gate tiptr->ti_rcvbuf = databuf.buf;
2320Sstevel@tonic-gate sig_mutex_unlock(&tiptr->ti_lock);
2330Sstevel@tonic-gate errno = sv_errno;
2340Sstevel@tonic-gate return (-1);
2350Sstevel@tonic-gate }
236