xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_open.c (revision 63f91fbc3c024870d86dc3332a4a0080fb29bc40)
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 <string.h>
34 #include <strings.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <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 <sys/stat.h>
43 #include <xti.h>
44 #include <fcntl.h>
45 #include <signal.h>
46 #include <assert.h>
47 #include <syslog.h>
48 #include <limits.h>
49 #include "tx.h"
50 
51 /*
52  * If _tx_open is called for transport that doesn't understand T_CAPABILITY_REQ
53  * TPI message, call to _t_create may fail the first time it is called with
54  * given transport (in the rare case when transport shuts down the stream with
55  * M_ERROR in reply to unknown T_CAPABILITY_REQ). In this case we may reopen the
56  * stream again since timod will emulate T_CAPABILITY_REQ behaviour.
57  *
58  * _t_create sends T_CAPABILITY_REQ through TI_CAPABILITY ioctl.
59  */
60 
61 int
62 _tx_open(const char *path, int flags, struct t_info *info, int api_semantics)
63 {
64 	int retval, fd, sv_errno;
65 	int sv_terrno;
66 	int sv_errno_global;
67 	struct _ti_user *tiptr;
68 	sigset_t mask;
69 	int t_create_first_attempt = 1;
70 	int ticap_ioctl_failed = 0;
71 
72 	if (!(flags & O_RDWR)) {
73 		t_errno = TBADFLAG;
74 		return (-1);
75 	}
76 
77 	sv_errno_global = errno;
78 	sv_terrno = t_errno;
79 
80 retry:
81 	if ((fd = open(path, flags)) < 0) {
82 		t_errno = TSYSERR;
83 		if (_T_IS_XTI(api_semantics) && errno == ENOENT)
84 			/* XTI only */
85 			t_errno = TBADNAME;
86 		return (-1);
87 	}
88 	/*
89 	 * is module already pushed
90 	 */
91 	do {
92 		retval = ioctl(fd, I_FIND, "timod");
93 	} while (retval < 0 && errno == EINTR);
94 
95 	if (retval < 0) {
96 		sv_errno = errno;
97 
98 		t_errno = TSYSERR;
99 		(void) close(fd);
100 		errno = sv_errno;
101 		return (-1);
102 	}
103 
104 	if (retval == 0) {
105 		/*
106 		 * "timod" not already on stream, then push it
107 		 */
108 		do {
109 			/*
110 			 * Assumes (correctly) that I_PUSH  is
111 			 * atomic w.r.t signals (EINTR error)
112 			 */
113 			retval = ioctl(fd, I_PUSH, "timod");
114 		} while (retval < 0 && errno == EINTR);
115 
116 		if (retval < 0) {
117 			int sv_errno = errno;
118 
119 			t_errno = TSYSERR;
120 			(void) close(fd);
121 			errno = sv_errno;
122 			return (-1);
123 		}
124 	}
125 
126 	/*
127 	 * _t_create() requires that all signals be blocked.
128 	 * Note that sig_mutex_lock() only defers signals, it does not
129 	 * block them, so interruptible syscalls could still get EINTR.
130 	 */
131 	(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
132 	sig_mutex_lock(&_ti_userlock);
133 	/*
134 	 * Call to _t_create may fail either because transport doesn't
135 	 * understand T_CAPABILITY_REQ or for some other reason. It is nearly
136 	 * impossible to distinguish between these cases so it is implicitly
137 	 * assumed that it is always save to close and reopen the same stream
138 	 * and that open/close doesn't have side effects. _t_create may fail
139 	 * only once if its' failure is caused by unimplemented
140 	 * T_CAPABILITY_REQ.
141 	 */
142 	tiptr = _t_create(fd, info, api_semantics, &ticap_ioctl_failed);
143 	if (tiptr == NULL) {
144 		/*
145 		 * If _t_create failed due to fail of ti_capability_req we may
146 		 * try to reopen the stream in the hope that timod will emulate
147 		 * TI_CAPABILITY and it will succeed when called again.
148 		 */
149 		if (t_create_first_attempt == 1 && ticap_ioctl_failed == 1) {
150 			t_create_first_attempt = 0;
151 			(void) close(fd);
152 			errno = sv_errno_global;
153 			t_errno = sv_terrno;
154 			sig_mutex_unlock(&_ti_userlock);
155 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
156 			goto retry;
157 		} else {
158 			int sv_errno = errno;
159 			(void) close(fd);
160 			sig_mutex_unlock(&_ti_userlock);
161 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
162 			errno = sv_errno;
163 			return (-1);
164 		}
165 	}
166 
167 	/*
168 	 * _t_create synchronizes state witk kernel timod and
169 	 * already sets it to T_UNBND - what it needs to be
170 	 * be on T_OPEN event. No _T_TX_NEXTSTATE needed here.
171 	 */
172 	sig_mutex_unlock(&_ti_userlock);
173 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
174 
175 	do {
176 		retval = ioctl(fd, I_FLUSH, FLUSHRW);
177 	} while (retval < 0 && errno == EINTR);
178 
179 	/*
180 	 * We ignore other error cases (retval < 0) - assumption is
181 	 * that I_FLUSH failures is temporary (e.g. ENOSR) or
182 	 * otherwise benign failure on a this newly opened file
183 	 * descriptor and not a critical failure.
184 	 */
185 
186 	return (fd);
187 }
188