xref: /illumos-gate/usr/src/lib/libnsl/nsl/t_open.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
2261961e0fSrobinson 
237c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
247c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
27*e8031f0aSraf  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "mt.h"
3261961e0fSrobinson #include <stdlib.h>
3361961e0fSrobinson #include <string.h>
3461961e0fSrobinson #include <strings.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <stropts.h>
3861961e0fSrobinson #include <sys/stream.h>
397c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
4061961e0fSrobinson #include <sys/tihdr.h>
417c478bd9Sstevel@tonic-gate #include <sys/timod.h>
4261961e0fSrobinson #include <sys/stat.h>
437c478bd9Sstevel@tonic-gate #include <xti.h>
4461961e0fSrobinson #include <fcntl.h>
457c478bd9Sstevel@tonic-gate #include <signal.h>
4661961e0fSrobinson #include <assert.h>
477c478bd9Sstevel@tonic-gate #include <syslog.h>
4861961e0fSrobinson #include <limits.h>
497c478bd9Sstevel@tonic-gate #include "tx.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * If _tx_open is called for transport that doesn't understand T_CAPABILITY_REQ
537c478bd9Sstevel@tonic-gate  * TPI message, call to _t_create may fail the first time it is called with
547c478bd9Sstevel@tonic-gate  * given transport (in the rare case when transport shuts down the stream with
557c478bd9Sstevel@tonic-gate  * M_ERROR in reply to unknown T_CAPABILITY_REQ). In this case we may reopen the
567c478bd9Sstevel@tonic-gate  * stream again since timod will emulate T_CAPABILITY_REQ behaviour.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * _t_create sends T_CAPABILITY_REQ through TI_CAPABILITY ioctl.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate int
_tx_open(const char * path,int flags,struct t_info * info,int api_semantics)627c478bd9Sstevel@tonic-gate _tx_open(const char *path, int flags, struct t_info *info, int api_semantics)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	int retval, fd, sv_errno;
657c478bd9Sstevel@tonic-gate 	int sv_terrno;
667c478bd9Sstevel@tonic-gate 	int sv_errno_global;
677c478bd9Sstevel@tonic-gate 	struct _ti_user *tiptr;
687c478bd9Sstevel@tonic-gate 	sigset_t mask;
697c478bd9Sstevel@tonic-gate 	int t_create_first_attempt = 1;
707c478bd9Sstevel@tonic-gate 	int ticap_ioctl_failed = 0;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	if (!(flags & O_RDWR)) {
737c478bd9Sstevel@tonic-gate 		t_errno = TBADFLAG;
747c478bd9Sstevel@tonic-gate 		return (-1);
757c478bd9Sstevel@tonic-gate 	}
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	sv_errno_global = errno;
787c478bd9Sstevel@tonic-gate 	sv_terrno = t_errno;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate retry:
817c478bd9Sstevel@tonic-gate 	if ((fd = open(path, flags)) < 0) {
827c478bd9Sstevel@tonic-gate 		t_errno = TSYSERR;
837c478bd9Sstevel@tonic-gate 		if (_T_IS_XTI(api_semantics) && errno == ENOENT)
847c478bd9Sstevel@tonic-gate 			/* XTI only */
857c478bd9Sstevel@tonic-gate 			t_errno = TBADNAME;
867c478bd9Sstevel@tonic-gate 		return (-1);
877c478bd9Sstevel@tonic-gate 	}
887c478bd9Sstevel@tonic-gate 	/*
897c478bd9Sstevel@tonic-gate 	 * is module already pushed
907c478bd9Sstevel@tonic-gate 	 */
917c478bd9Sstevel@tonic-gate 	do {
92*e8031f0aSraf 		retval = ioctl(fd, I_FIND, "timod");
937c478bd9Sstevel@tonic-gate 	} while (retval < 0 && errno == EINTR);
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 	if (retval < 0) {
967c478bd9Sstevel@tonic-gate 		sv_errno = errno;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 		t_errno = TSYSERR;
997c478bd9Sstevel@tonic-gate 		(void) close(fd);
1007c478bd9Sstevel@tonic-gate 		errno = sv_errno;
1017c478bd9Sstevel@tonic-gate 		return (-1);
1027c478bd9Sstevel@tonic-gate 	}
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (retval == 0) {
1057c478bd9Sstevel@tonic-gate 		/*
1067c478bd9Sstevel@tonic-gate 		 * "timod" not already on stream, then push it
1077c478bd9Sstevel@tonic-gate 		 */
1087c478bd9Sstevel@tonic-gate 		do {
1097c478bd9Sstevel@tonic-gate 			/*
1107c478bd9Sstevel@tonic-gate 			 * Assumes (correctly) that I_PUSH  is
1117c478bd9Sstevel@tonic-gate 			 * atomic w.r.t signals (EINTR error)
1127c478bd9Sstevel@tonic-gate 			 */
113*e8031f0aSraf 			retval = ioctl(fd, I_PUSH, "timod");
1147c478bd9Sstevel@tonic-gate 		} while (retval < 0 && errno == EINTR);
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 		if (retval < 0) {
1177c478bd9Sstevel@tonic-gate 			int sv_errno = errno;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 			t_errno = TSYSERR;
1207c478bd9Sstevel@tonic-gate 			(void) close(fd);
1217c478bd9Sstevel@tonic-gate 			errno = sv_errno;
1227c478bd9Sstevel@tonic-gate 			return (-1);
1237c478bd9Sstevel@tonic-gate 		}
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/*
1277c478bd9Sstevel@tonic-gate 	 * _t_create() requires that all signals be blocked.
1287c478bd9Sstevel@tonic-gate 	 * Note that sig_mutex_lock() only defers signals, it does not
1297c478bd9Sstevel@tonic-gate 	 * block them, so interruptible syscalls could still get EINTR.
1307c478bd9Sstevel@tonic-gate 	 */
1317c478bd9Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask);
1327c478bd9Sstevel@tonic-gate 	sig_mutex_lock(&_ti_userlock);
1337c478bd9Sstevel@tonic-gate 	/*
1347c478bd9Sstevel@tonic-gate 	 * Call to _t_create may fail either because transport doesn't
1357c478bd9Sstevel@tonic-gate 	 * understand T_CAPABILITY_REQ or for some other reason. It is nearly
1367c478bd9Sstevel@tonic-gate 	 * impossible to distinguish between these cases so it is implicitly
1377c478bd9Sstevel@tonic-gate 	 * assumed that it is always save to close and reopen the same stream
1387c478bd9Sstevel@tonic-gate 	 * and that open/close doesn't have side effects. _t_create may fail
1397c478bd9Sstevel@tonic-gate 	 * only once if its' failure is caused by unimplemented
1407c478bd9Sstevel@tonic-gate 	 * T_CAPABILITY_REQ.
1417c478bd9Sstevel@tonic-gate 	 */
1427c478bd9Sstevel@tonic-gate 	tiptr = _t_create(fd, info, api_semantics, &ticap_ioctl_failed);
1437c478bd9Sstevel@tonic-gate 	if (tiptr == NULL) {
1447c478bd9Sstevel@tonic-gate 		/*
1457c478bd9Sstevel@tonic-gate 		 * If _t_create failed due to fail of ti_capability_req we may
1467c478bd9Sstevel@tonic-gate 		 * try to reopen the stream in the hope that timod will emulate
1477c478bd9Sstevel@tonic-gate 		 * TI_CAPABILITY and it will succeed when called again.
1487c478bd9Sstevel@tonic-gate 		 */
1497c478bd9Sstevel@tonic-gate 		if (t_create_first_attempt == 1 && ticap_ioctl_failed == 1) {
1507c478bd9Sstevel@tonic-gate 			t_create_first_attempt = 0;
1517c478bd9Sstevel@tonic-gate 			(void) close(fd);
1527c478bd9Sstevel@tonic-gate 			errno = sv_errno_global;
1537c478bd9Sstevel@tonic-gate 			t_errno = sv_terrno;
1547c478bd9Sstevel@tonic-gate 			sig_mutex_unlock(&_ti_userlock);
1557c478bd9Sstevel@tonic-gate 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1567c478bd9Sstevel@tonic-gate 			goto retry;
1577c478bd9Sstevel@tonic-gate 		} else {
1587c478bd9Sstevel@tonic-gate 			int sv_errno = errno;
1597c478bd9Sstevel@tonic-gate 			(void) close(fd);
1607c478bd9Sstevel@tonic-gate 			sig_mutex_unlock(&_ti_userlock);
1617c478bd9Sstevel@tonic-gate 			(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1627c478bd9Sstevel@tonic-gate 			errno = sv_errno;
1637c478bd9Sstevel@tonic-gate 			return (-1);
1647c478bd9Sstevel@tonic-gate 		}
1657c478bd9Sstevel@tonic-gate 	}
1667c478bd9Sstevel@tonic-gate 
1677c478bd9Sstevel@tonic-gate 	/*
1687c478bd9Sstevel@tonic-gate 	 * _t_create synchronizes state witk kernel timod and
1697c478bd9Sstevel@tonic-gate 	 * already sets it to T_UNBND - what it needs to be
1707c478bd9Sstevel@tonic-gate 	 * be on T_OPEN event. No _T_TX_NEXTSTATE needed here.
1717c478bd9Sstevel@tonic-gate 	 */
1727c478bd9Sstevel@tonic-gate 	sig_mutex_unlock(&_ti_userlock);
1737c478bd9Sstevel@tonic-gate 	(void) thr_sigsetmask(SIG_SETMASK, &mask, NULL);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	do {
176*e8031f0aSraf 		retval = ioctl(fd, I_FLUSH, FLUSHRW);
1777c478bd9Sstevel@tonic-gate 	} while (retval < 0 && errno == EINTR);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/*
1807c478bd9Sstevel@tonic-gate 	 * We ignore other error cases (retval < 0) - assumption is
1817c478bd9Sstevel@tonic-gate 	 * that I_FLUSH failures is temporary (e.g. ENOSR) or
1827c478bd9Sstevel@tonic-gate 	 * otherwise benign failure on a this newly opened file
1837c478bd9Sstevel@tonic-gate 	 * descriptor and not a critical failure.
1847c478bd9Sstevel@tonic-gate 	 */
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	return (fd);
1877c478bd9Sstevel@tonic-gate }
188