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