xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_unbind.c (revision e3ae4b35c024af1196582063ecee3ab79367227d)
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 2006 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 #include "mt.h"
32 #include <stdlib.h>
33 #include <errno.h>
34 #include <unistd.h>
35 #include <sys/stropts.h>
36 #include <sys/stream.h>
37 #define	_SUN_TPI_VERSION 2
38 #include <sys/tihdr.h>
39 #include <sys/timod.h>
40 #include <xti.h>
41 #include <signal.h>
42 #include <syslog.h>
43 #include "tx.h"
44 
45 
46 int
47 _tx_unbind(int fd, int api_semantics)
48 {
49 	struct _ti_user *tiptr;
50 	sigset_t mask;
51 	int sv_errno, retval, didalloc;
52 	struct strbuf ctlbuf;
53 
54 	if ((tiptr = _t_checkfd(fd, 0, api_semantics)) == NULL)
55 		return (-1);
56 
57 	if (_T_IS_XTI(api_semantics)) {
58 		/*
59 		 * User level state verification only done for XTI
60 		 * because doing for TLI may break existing applications
61 		 */
62 		if (tiptr->ti_state != T_IDLE) {
63 			t_errno = TOUTSTATE;
64 			return (-1);
65 		}
66 	}
67 
68 	/*
69 	 * Since unbind is not an idempotent operation, we
70 	 * block signals around the call.
71 	 * Note that sig_mutex_lock() only defers signals, it does not
72 	 * block them, so interruptible syscalls could still get EINTR.
73 	 */
74 	(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
75 	sig_mutex_lock(&tiptr->ti_lock);
76 	/*
77 	 * Acquire buffer for use in sending/receiving the message.
78 	 * Note: assumes (correctly) that ti_ctlsize is large enough
79 	 * to hold sizeof (struct T_unbind_req/ack)
80 	 */
81 	if (_t_acquire_ctlbuf(tiptr, &ctlbuf, &didalloc) < 0) {
82 		sv_errno = errno;
83 		sig_mutex_unlock(&tiptr->ti_lock);
84 		(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
85 		errno = sv_errno;
86 		return (-1);
87 	}
88 
89 	retval = _tx_unbind_locked(fd, tiptr, &ctlbuf);
90 
91 	sv_errno = errno;
92 	if (didalloc)
93 		free(ctlbuf.buf);
94 	else
95 		tiptr->ti_ctlbuf = ctlbuf.buf;
96 	sig_mutex_unlock(&tiptr->ti_lock);
97 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
98 	errno = sv_errno;
99 	return (retval);
100 }
101 
102 int
103 _tx_unbind_locked(int fd, struct _ti_user *tiptr, struct strbuf *ctlbufp)
104 {
105 	struct T_unbind_req *unbind_reqp;
106 	int retlen;
107 
108 	if (_t_is_event(fd, tiptr) < 0)
109 		return (-1);
110 
111 	/* LINTED pointer cast */
112 	unbind_reqp = (struct T_unbind_req *)ctlbufp->buf;
113 	unbind_reqp->PRIM_type = T_UNBIND_REQ;
114 
115 	if (_t_do_ioctl(fd, (char *)unbind_reqp,
116 	    (int)sizeof (struct T_unbind_req), TI_UNBIND, &retlen) < 0) {
117 		goto err_out;
118 	}
119 
120 	if (ioctl(fd, I_FLUSH, FLUSHRW) < 0) {
121 		t_errno = TSYSERR;
122 		goto err_out;
123 	}
124 
125 	/*
126 	 * clear more and expedited data bits
127 	 */
128 	tiptr->ti_flags &= ~(MORE|EXPEDITED);
129 
130 	_T_TX_NEXTSTATE(T_UNBIND, tiptr,
131 			"t_unbind: invalid state event T_UNBIND");
132 
133 	return (0);
134 
135 err_out:
136 	return (-1);
137 }
138