xref: /titanic_41/usr/src/lib/libnsl/nsl/t_rcvrel.c (revision 683007910b81ed9ff942db7c464d1b6df524048a)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
24 /*	  All Rights Reserved  	*/
25 
26 /*
27  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.7.3.1 */
32 
33 /*
34  * t_rcvrel.c and t_rcvreldata.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.
37  */
38 #include "mt.h"
39 #include <stdlib.h>
40 #include <errno.h>
41 #include <stropts.h>
42 #include <sys/stream.h>
43 #define	_SUN_TPI_VERSION 2
44 #include <sys/tihdr.h>
45 #include <sys/timod.h>
46 #include <xti.h>
47 #include <signal.h>
48 #include <syslog.h>
49 #include "tx.h"
50 
51 int
52 _tx_rcvrel(int fd, int api_semantics)
53 {
54 	struct strbuf ctlbuf;
55 	struct strbuf databuf;
56 	int retval;
57 	union T_primitives *pptr;
58 	struct _ti_user *tiptr;
59 	int sv_errno;
60 	int didalloc, didralloc;
61 
62 	int flg = 0;
63 
64 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == 0)
65 		return (-1);
66 	sig_mutex_lock(&tiptr->ti_lock);
67 
68 	if (tiptr->ti_servtype != T_COTS_ORD) {
69 		t_errno = TNOTSUPPORT;
70 		sig_mutex_unlock(&tiptr->ti_lock);
71 		return (-1);
72 	}
73 
74 	if (_T_IS_XTI(api_semantics)) {
75 		/*
76 		 * User level state verification only done for XTI
77 		 * because doing for TLI may break existing applications
78 		 */
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);
84 		}
85 	}
86 
87 	if ((retval = _t_look_locked(fd, tiptr, 0, api_semantics)) < 0) {
88 		sv_errno = errno;
89 		sig_mutex_unlock(&tiptr->ti_lock);
90 		errno = sv_errno;
91 		return (-1);
92 	}
93 
94 	if (retval == T_DISCONNECT) {
95 		/*
96 		 * This ensures preference to T_DISCON_IND which is
97 		 * the design model for TPI
98 		 */
99 		t_errno = TLOOK;
100 		sig_mutex_unlock(&tiptr->ti_lock);
101 		return (-1);
102 	}
103 
104 	if ((tiptr->ti_lookcnt > 0) &&
105 	    /* LINTED pointer cast */
106 	    (*((t_scalar_t *)tiptr->ti_lookbufs.tl_lookcbuf) == T_ORDREL_IND)) {
107 		/*
108 		 * Current look buffer event is T_ORDREL_IND.
109 		 * Remove it from look buffer event list.
110 		 */
111 		_t_free_looklist_head(tiptr);
112 		_T_TX_NEXTSTATE(T_RCVREL, tiptr,
113 					"t_rcv: invalid state event T_RCVREL");
114 		sig_mutex_unlock(&tiptr->ti_lock);
115 		return (0);
116 	} else {
117 		if (retval != T_ORDREL) {
118 			t_errno = TNOREL;
119 			sig_mutex_unlock(&tiptr->ti_lock);
120 			return (-1);
121 		}
122 	}
123 
124 	/*
125 	 * get ordrel off read queue.
126 	 * use ctl and rcv buffers
127 	 *
128 	 * Acquire ctlbuf for use in sending/receiving control part
129 	 * of the message.
130 	 */
131 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
132 		sv_errno = errno;
133 		sig_mutex_unlock(&tiptr->ti_lock);
134 		errno = sv_errno;
135 		return (-1);
136 	}
137 
138 	/*
139 	 * Acquire databuf for use in sending/receiving data part
140 	 */
141 	if (_t_acquire_databuf(tiptr, &databuf, &didralloc) < 0) {
142 		sv_errno = errno;
143 		if (didalloc)
144 			free(ctlbuf.buf);
145 		else
146 			tiptr->ti_ctlbuf = ctlbuf.buf;
147 		sig_mutex_unlock(&tiptr->ti_lock);
148 		errno = sv_errno;
149 		return (-1);
150 	}
151 
152 	/*
153 	 * Since we have verified above that an orderly release event
154 	 * is pending on this endpoint, we assume that this getmsg()
155 	 * cannot block forever.
156 	 */
157 	do {
158 		retval = getmsg(fd, &ctlbuf, &databuf, &flg);
159 	} while (retval < 0 && errno == EINTR);
160 
161 	if (retval < 0) {
162 		t_errno = TSYSERR;
163 		goto err_out;
164 	}
165 
166 	/*
167 	 * did I get entire message?
168 	 */
169 	if (retval > 0) {
170 		t_errno = TSYSERR;
171 		errno = EIO;
172 		goto err_out;
173 	}
174 	/* LINTED pointer cast */
175 	pptr = (union T_primitives *)ctlbuf.buf;
176 
177 	if (ctlbuf.len < (int)sizeof (struct T_ordrel_ind)) {
178 		t_errno = TSYSERR;
179 		errno = EPROTO;
180 		goto err_out;
181 	}
182 	if (pptr->type != T_ORDREL_IND) {
183 		if (pptr->type == T_DISCON_IND) {
184 			/*
185 			 * T_DISCON_IND gets priority following
186 			 * TPI design philosphy.
187 			 *
188 			 * Add it to the events in the "look buffer"
189 			 * list of events. This routine may defer signals.
190 			 */
191 			if (_t_register_lookevent(tiptr, databuf.buf,
192 						databuf.len, ctlbuf.buf,
193 						ctlbuf.len) < 0) {
194 				t_errno = TSYSERR;
195 				errno = ENOMEM;
196 				goto err_out;
197 			}
198 			t_errno = TLOOK;
199 			goto err_out;
200 		} else {
201 			t_errno = TSYSERR;
202 			errno = EPROTO;
203 			goto err_out;
204 		}
205 	}
206 
207 	_T_TX_NEXTSTATE(T_RCVREL, tiptr,
208 			"t_rcvrel: invalid state event T_RCVREL");
209 
210 	if (didalloc)
211 		free(ctlbuf.buf);
212 	else
213 		tiptr->ti_ctlbuf = ctlbuf.buf;
214 	if (didralloc)
215 		free(databuf.buf);
216 	else
217 		tiptr->ti_rcvbuf = databuf.buf;
218 	sig_mutex_unlock(&tiptr->ti_lock);
219 	return (0);
220 
221 err_out:
222 	sv_errno = errno;
223 
224 	if (didalloc)
225 		free(ctlbuf.buf);
226 	else
227 		tiptr->ti_ctlbuf = ctlbuf.buf;
228 	if (didralloc)
229 		free(databuf.buf);
230 	else
231 		tiptr->ti_rcvbuf = databuf.buf;
232 	sig_mutex_unlock(&tiptr->ti_lock);
233 	errno = sv_errno;
234 	return (-1);
235 }
236