xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_snddis.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright 1993-2003 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.4.3.1 */
33 
34 #include "mt.h"
35 #include <rpc/trace.h>
36 #include <errno.h>
37 #include <unistd.h>
38 #include <stropts.h>
39 #include <sys/stream.h>
40 #define	_SUN_TPI_VERSION 2
41 #include <sys/tihdr.h>
42 #include <sys/timod.h>
43 #include <xti.h>
44 #include <signal.h>
45 #include <syslog.h>
46 #include "tx.h"
47 
48 int
49 _tx_snddis(int fd, const struct t_call *call, int api_semantics)
50 {
51 	struct T_discon_req dreq;
52 	struct strbuf ctlbuf;
53 	struct strbuf databuf;
54 	struct _ti_user *tiptr;
55 	int sv_errno;
56 	int retval;
57 
58 	trace2(TR_t_snddis, 0, fd);
59 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL) {
60 		sv_errno = errno;
61 		trace2(TR_t_snddis, 1, fd);
62 		errno = sv_errno;
63 		return (-1);
64 	}
65 	sig_mutex_lock(&tiptr->ti_lock);
66 
67 	if (tiptr->ti_servtype == T_CLTS) {
68 		t_errno = TNOTSUPPORT;
69 		sig_mutex_unlock(&tiptr->ti_lock);
70 		trace2(TR_t_snddis, 1, fd);
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 		 * Note: This is documented in TLI man page but never
79 		 * done.
80 		 */
81 		if (! (tiptr->ti_state == T_DATAXFER ||
82 		    tiptr->ti_state == T_OUTCON ||
83 		    tiptr->ti_state == T_OUTREL ||
84 		    tiptr->ti_state == T_INREL ||
85 		    (tiptr->ti_state == T_INCON && tiptr->ti_ocnt > 0))) {
86 			t_errno = TOUTSTATE;
87 			sig_mutex_unlock(&tiptr->ti_lock);
88 			trace2(TR_t_snddis, 1, fd);
89 			return (-1);
90 		}
91 
92 		/*
93 		 * Following check only done for XTI as it may be a risk
94 		 * to existing buggy TLI applications.
95 		 */
96 	}
97 
98 	if (call != NULL && call->udata.len) {
99 		if ((tiptr->ti_ddatasize == T_INVALID /* -2 */) ||
100 		    ((tiptr->ti_ddatasize != T_INFINITE /* -1*/) &&
101 			(call->udata.len >
102 			    (uint32_t)tiptr->ti_ddatasize))) {
103 			/*
104 			 * user data not valid with disconnect or it
105 			 * exceeds the limits specified by the
106 			 * transport provider
107 			 */
108 			t_errno = TBADDATA;
109 			sig_mutex_unlock(&tiptr->ti_lock);
110 			trace2(TR_t_snddis, 1, fd);
111 			return (-1);
112 		}
113 	}
114 
115 	/*
116 	 * If disconnect is done on a listener, the 'call' parameter
117 	 * must be non-null
118 	 */
119 	if ((tiptr->ti_state == T_INCON) &&
120 	    (call == NULL)) {
121 		t_errno = TBADSEQ;
122 		sig_mutex_unlock(&tiptr->ti_lock);
123 		trace2(TR_t_snddis, 1, fd);
124 		return (-1);
125 	}
126 
127 	/*
128 	 * look at look buffer to see if there is a discon there
129 	 */
130 
131 	if (_t_look_locked(fd, tiptr, 0, api_semantics) == T_DISCONNECT) {
132 		t_errno = TLOOK;
133 		sig_mutex_unlock(&tiptr->ti_lock);
134 		trace2(TR_t_snddis, 1, fd);
135 		return (-1);
136 	}
137 
138 	if ((tiptr->ti_lookcnt > 0) && (call == 0))
139 		_t_flush_lookevents(tiptr); /* flush but not on listener */
140 
141 	do {
142 		retval = _ioctl(fd, I_FLUSH, FLUSHW);
143 	} while (retval < 0 && errno == EINTR);
144 	if (retval < 0) {
145 		sv_errno = errno;
146 		t_errno = TSYSERR;
147 		sig_mutex_unlock(&tiptr->ti_lock);
148 		trace2(TR_t_snddis, 1, fd);
149 		errno = sv_errno;
150 		return (-1);
151 	}
152 
153 	ctlbuf.len = (int)sizeof (struct T_discon_req);
154 	ctlbuf.maxlen = (int)sizeof (struct T_discon_req);
155 	ctlbuf.buf = (char *)&dreq;
156 
157 	dreq.PRIM_type = T_DISCON_REQ;
158 	dreq.SEQ_number = (call? call->sequence: -1);
159 
160 	databuf.maxlen = (call? call->udata.len: 0);
161 	databuf.len = (call? call->udata.len: 0);
162 	databuf.buf = (call? call->udata.buf: NULL);
163 
164 	/*
165 	 * Calls to send data (write or putmsg) can potentially
166 	 * block, for MT case, we drop the lock and enable signals here
167 	 * and acquire it back
168 	 */
169 	sig_mutex_unlock(&tiptr->ti_lock);
170 	if (putmsg(fd, &ctlbuf, (databuf.len? &databuf: NULL), 0) < 0) {
171 		sv_errno = errno;
172 
173 		t_errno = TSYSERR;
174 		trace2(TR_t_snddis, 1, fd);
175 		errno = sv_errno;
176 		return (-1);
177 	}
178 	sig_mutex_lock(&tiptr->ti_lock);
179 
180 	if (_t_is_ok(fd, tiptr, T_DISCON_REQ) < 0) {
181 		sv_errno = errno;
182 		sig_mutex_unlock(&tiptr->ti_lock);
183 		trace2(TR_t_snddis, 1, fd);
184 		errno = sv_errno;
185 		return (-1);
186 	}
187 
188 	tiptr->ti_flags &= ~(MORE|EXPEDITED);
189 
190 	if (tiptr->ti_ocnt <= 1) {
191 		if (tiptr->ti_state == T_INCON) {
192 			tiptr->ti_ocnt--;
193 			tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
194 		}
195 		_T_TX_NEXTSTATE(T_SNDDIS1, tiptr,
196 				"t_snddis: invalid state event T_SNDDIS1");
197 	} else {
198 		if (tiptr->ti_state == T_INCON) {
199 			tiptr->ti_ocnt--;
200 			tiptr->ti_flags &= ~TX_TQFULL_NOTIFIED;
201 		}
202 		_T_TX_NEXTSTATE(T_SNDDIS2, tiptr,
203 				"t_snddis: invalid state event T_SNDDIS2");
204 	}
205 
206 	sig_mutex_unlock(&tiptr->ti_lock);
207 	trace2(TR_t_snddis, 1, fd);
208 	return (0);
209 }
210