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 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.5.3.3 */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include "mt.h" 3461961e0fSrobinson #include <stdlib.h> 3561961e0fSrobinson #include <string.h> 3661961e0fSrobinson #include <strings.h> 377c478bd9Sstevel@tonic-gate #include <unistd.h> 387c478bd9Sstevel@tonic-gate #include <errno.h> 397c478bd9Sstevel@tonic-gate #include <stropts.h> 4061961e0fSrobinson #include <sys/stream.h> 417c478bd9Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 4261961e0fSrobinson #include <sys/tihdr.h> 437c478bd9Sstevel@tonic-gate #include <sys/timod.h> 4461961e0fSrobinson #include <sys/stat.h> 457c478bd9Sstevel@tonic-gate #include <xti.h> 4661961e0fSrobinson #include <fcntl.h> 477c478bd9Sstevel@tonic-gate #include <signal.h> 4861961e0fSrobinson #include <assert.h> 497c478bd9Sstevel@tonic-gate #include <syslog.h> 5061961e0fSrobinson #include <limits.h> 517c478bd9Sstevel@tonic-gate #include "tx.h" 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* 547c478bd9Sstevel@tonic-gate * If _tx_open is called for transport that doesn't understand T_CAPABILITY_REQ 557c478bd9Sstevel@tonic-gate * TPI message, call to _t_create may fail the first time it is called with 567c478bd9Sstevel@tonic-gate * given transport (in the rare case when transport shuts down the stream with 577c478bd9Sstevel@tonic-gate * M_ERROR in reply to unknown T_CAPABILITY_REQ). In this case we may reopen the 587c478bd9Sstevel@tonic-gate * stream again since timod will emulate T_CAPABILITY_REQ behaviour. 597c478bd9Sstevel@tonic-gate * 607c478bd9Sstevel@tonic-gate * _t_create sends T_CAPABILITY_REQ through TI_CAPABILITY ioctl. 617c478bd9Sstevel@tonic-gate */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate int 647c478bd9Sstevel@tonic-gate _tx_open(const char *path, int flags, struct t_info *info, int api_semantics) 657c478bd9Sstevel@tonic-gate { 667c478bd9Sstevel@tonic-gate int retval, fd, sv_errno; 677c478bd9Sstevel@tonic-gate int sv_terrno; 687c478bd9Sstevel@tonic-gate int sv_errno_global; 697c478bd9Sstevel@tonic-gate struct _ti_user *tiptr; 707c478bd9Sstevel@tonic-gate sigset_t mask; 717c478bd9Sstevel@tonic-gate int t_create_first_attempt = 1; 727c478bd9Sstevel@tonic-gate int ticap_ioctl_failed = 0; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate if (!(flags & O_RDWR)) { 757c478bd9Sstevel@tonic-gate t_errno = TBADFLAG; 767c478bd9Sstevel@tonic-gate return (-1); 777c478bd9Sstevel@tonic-gate } 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate sv_errno_global = errno; 807c478bd9Sstevel@tonic-gate sv_terrno = t_errno; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate retry: 837c478bd9Sstevel@tonic-gate if ((fd = open(path, flags)) < 0) { 847c478bd9Sstevel@tonic-gate t_errno = TSYSERR; 857c478bd9Sstevel@tonic-gate if (_T_IS_XTI(api_semantics) && errno == ENOENT) 867c478bd9Sstevel@tonic-gate /* XTI only */ 877c478bd9Sstevel@tonic-gate t_errno = TBADNAME; 887c478bd9Sstevel@tonic-gate return (-1); 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate /* 917c478bd9Sstevel@tonic-gate * is module already pushed 927c478bd9Sstevel@tonic-gate */ 937c478bd9Sstevel@tonic-gate do { 94*e8031f0aSraf retval = ioctl(fd, I_FIND, "timod"); 957c478bd9Sstevel@tonic-gate } while (retval < 0 && errno == EINTR); 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate if (retval < 0) { 987c478bd9Sstevel@tonic-gate sv_errno = errno; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate t_errno = TSYSERR; 1017c478bd9Sstevel@tonic-gate (void) close(fd); 1027c478bd9Sstevel@tonic-gate errno = sv_errno; 1037c478bd9Sstevel@tonic-gate return (-1); 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate if (retval == 0) { 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * "timod" not already on stream, then push it 1097c478bd9Sstevel@tonic-gate */ 1107c478bd9Sstevel@tonic-gate do { 1117c478bd9Sstevel@tonic-gate /* 1127c478bd9Sstevel@tonic-gate * Assumes (correctly) that I_PUSH is 1137c478bd9Sstevel@tonic-gate * atomic w.r.t signals (EINTR error) 1147c478bd9Sstevel@tonic-gate */ 115*e8031f0aSraf retval = ioctl(fd, I_PUSH, "timod"); 1167c478bd9Sstevel@tonic-gate } while (retval < 0 && errno == EINTR); 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate if (retval < 0) { 1197c478bd9Sstevel@tonic-gate int sv_errno = errno; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate t_errno = TSYSERR; 1227c478bd9Sstevel@tonic-gate (void) close(fd); 1237c478bd9Sstevel@tonic-gate errno = sv_errno; 1247c478bd9Sstevel@tonic-gate return (-1); 1257c478bd9Sstevel@tonic-gate } 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate /* 1297c478bd9Sstevel@tonic-gate * _t_create() requires that all signals be blocked. 1307c478bd9Sstevel@tonic-gate * Note that sig_mutex_lock() only defers signals, it does not 1317c478bd9Sstevel@tonic-gate * block them, so interruptible syscalls could still get EINTR. 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &fillset, &mask); 1347c478bd9Sstevel@tonic-gate sig_mutex_lock(&_ti_userlock); 1357c478bd9Sstevel@tonic-gate /* 1367c478bd9Sstevel@tonic-gate * Call to _t_create may fail either because transport doesn't 1377c478bd9Sstevel@tonic-gate * understand T_CAPABILITY_REQ or for some other reason. It is nearly 1387c478bd9Sstevel@tonic-gate * impossible to distinguish between these cases so it is implicitly 1397c478bd9Sstevel@tonic-gate * assumed that it is always save to close and reopen the same stream 1407c478bd9Sstevel@tonic-gate * and that open/close doesn't have side effects. _t_create may fail 1417c478bd9Sstevel@tonic-gate * only once if its' failure is caused by unimplemented 1427c478bd9Sstevel@tonic-gate * T_CAPABILITY_REQ. 1437c478bd9Sstevel@tonic-gate */ 1447c478bd9Sstevel@tonic-gate tiptr = _t_create(fd, info, api_semantics, &ticap_ioctl_failed); 1457c478bd9Sstevel@tonic-gate if (tiptr == NULL) { 1467c478bd9Sstevel@tonic-gate /* 1477c478bd9Sstevel@tonic-gate * If _t_create failed due to fail of ti_capability_req we may 1487c478bd9Sstevel@tonic-gate * try to reopen the stream in the hope that timod will emulate 1497c478bd9Sstevel@tonic-gate * TI_CAPABILITY and it will succeed when called again. 1507c478bd9Sstevel@tonic-gate */ 1517c478bd9Sstevel@tonic-gate if (t_create_first_attempt == 1 && ticap_ioctl_failed == 1) { 1527c478bd9Sstevel@tonic-gate t_create_first_attempt = 0; 1537c478bd9Sstevel@tonic-gate (void) close(fd); 1547c478bd9Sstevel@tonic-gate errno = sv_errno_global; 1557c478bd9Sstevel@tonic-gate t_errno = sv_terrno; 1567c478bd9Sstevel@tonic-gate sig_mutex_unlock(&_ti_userlock); 1577c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 1587c478bd9Sstevel@tonic-gate goto retry; 1597c478bd9Sstevel@tonic-gate } else { 1607c478bd9Sstevel@tonic-gate int sv_errno = errno; 1617c478bd9Sstevel@tonic-gate (void) close(fd); 1627c478bd9Sstevel@tonic-gate sig_mutex_unlock(&_ti_userlock); 1637c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 1647c478bd9Sstevel@tonic-gate errno = sv_errno; 1657c478bd9Sstevel@tonic-gate return (-1); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate /* 1707c478bd9Sstevel@tonic-gate * _t_create synchronizes state witk kernel timod and 1717c478bd9Sstevel@tonic-gate * already sets it to T_UNBND - what it needs to be 1727c478bd9Sstevel@tonic-gate * be on T_OPEN event. No _T_TX_NEXTSTATE needed here. 1737c478bd9Sstevel@tonic-gate */ 1747c478bd9Sstevel@tonic-gate sig_mutex_unlock(&_ti_userlock); 1757c478bd9Sstevel@tonic-gate (void) thr_sigsetmask(SIG_SETMASK, &mask, NULL); 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate do { 178*e8031f0aSraf retval = ioctl(fd, I_FLUSH, FLUSHRW); 1797c478bd9Sstevel@tonic-gate } while (retval < 0 && errno == EINTR); 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate /* 1827c478bd9Sstevel@tonic-gate * We ignore other error cases (retval < 0) - assumption is 1837c478bd9Sstevel@tonic-gate * that I_FLUSH failures is temporary (e.g. ENOSR) or 1847c478bd9Sstevel@tonic-gate * otherwise benign failure on a this newly opened file 1857c478bd9Sstevel@tonic-gate * descriptor and not a critical failure. 1867c478bd9Sstevel@tonic-gate */ 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate return (fd); 1897c478bd9Sstevel@tonic-gate } 190