18360efbdSAlfred Perlstein /* $NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $ */ 28360efbdSAlfred Perlstein 3*2e322d37SHiroki Sato /*- 4*2e322d37SHiroki Sato * Copyright (c) 2009, Sun Microsystems, Inc. 5*2e322d37SHiroki Sato * All rights reserved. 699064799SGarrett Wollman * 7*2e322d37SHiroki Sato * Redistribution and use in source and binary forms, with or without 8*2e322d37SHiroki Sato * modification, are permitted provided that the following conditions are met: 9*2e322d37SHiroki Sato * - Redistributions of source code must retain the above copyright notice, 10*2e322d37SHiroki Sato * this list of conditions and the following disclaimer. 11*2e322d37SHiroki Sato * - Redistributions in binary form must reproduce the above copyright notice, 12*2e322d37SHiroki Sato * this list of conditions and the following disclaimer in the documentation 13*2e322d37SHiroki Sato * and/or other materials provided with the distribution. 14*2e322d37SHiroki Sato * - Neither the name of Sun Microsystems, Inc. nor the names of its 15*2e322d37SHiroki Sato * contributors may be used to endorse or promote products derived 16*2e322d37SHiroki Sato * from this software without specific prior written permission. 1799064799SGarrett Wollman * 18*2e322d37SHiroki Sato * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 19*2e322d37SHiroki Sato * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20*2e322d37SHiroki Sato * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21*2e322d37SHiroki Sato * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 22*2e322d37SHiroki Sato * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23*2e322d37SHiroki Sato * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24*2e322d37SHiroki Sato * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25*2e322d37SHiroki Sato * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26*2e322d37SHiroki Sato * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27*2e322d37SHiroki Sato * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28*2e322d37SHiroki Sato * POSSIBILITY OF SUCH DAMAGE. 2999064799SGarrett Wollman */ 308360efbdSAlfred Perlstein /* 318360efbdSAlfred Perlstein * Copyright (c) 1986-1991 by Sun Microsystems Inc. 328360efbdSAlfred Perlstein */ 3399064799SGarrett Wollman 3499064799SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint) 35a986ef57SDavid E. O'Brien static char *sccsid2 = "from: @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro"; 36d3d20c82SDavid E. O'Brien static char *sccsid = "from: @(#)clnt_simple.c 2.2 88/08/01 4.0 RPCSRC"; 3799064799SGarrett Wollman #endif 38d3d20c82SDavid E. O'Brien #include <sys/cdefs.h> 39d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$"); 4099064799SGarrett Wollman 4199064799SGarrett Wollman /* 4299064799SGarrett Wollman * clnt_simple.c 438360efbdSAlfred Perlstein * Simplified front end to client rpc. 4499064799SGarrett Wollman * 4599064799SGarrett Wollman */ 4699064799SGarrett Wollman 47d201fe46SDaniel Eischen #include "namespace.h" 489f5afc13SIan Dowse #include "reentrant.h" 490ab74c6fSPeter Wemm #include <sys/param.h> 5099064799SGarrett Wollman #include <stdio.h> 518360efbdSAlfred Perlstein #include <errno.h> 5299064799SGarrett Wollman #include <rpc/rpc.h> 538360efbdSAlfred Perlstein #include <string.h> 548360efbdSAlfred Perlstein #include <stdlib.h> 558360efbdSAlfred Perlstein #include <fcntl.h> 568360efbdSAlfred Perlstein #include <unistd.h> 57d201fe46SDaniel Eischen #include "un-namespace.h" 58235baf26SDaniel Eischen #include "mt_misc.h" 5999064799SGarrett Wollman 608360efbdSAlfred Perlstein #ifndef MAXHOSTNAMELEN 618360efbdSAlfred Perlstein #define MAXHOSTNAMELEN 64 628360efbdSAlfred Perlstein #endif 6399064799SGarrett Wollman 648360efbdSAlfred Perlstein #ifndef NETIDLEN 658360efbdSAlfred Perlstein #define NETIDLEN 32 668360efbdSAlfred Perlstein #endif 678360efbdSAlfred Perlstein 688360efbdSAlfred Perlstein struct rpc_call_private { 698360efbdSAlfred Perlstein int valid; /* Is this entry valid ? */ 708360efbdSAlfred Perlstein CLIENT *client; /* Client handle */ 718360efbdSAlfred Perlstein pid_t pid; /* process-id at moment of creation */ 728360efbdSAlfred Perlstein rpcprog_t prognum; /* Program */ 738360efbdSAlfred Perlstein rpcvers_t versnum; /* Version */ 748360efbdSAlfred Perlstein char host[MAXHOSTNAMELEN]; /* Servers host */ 758360efbdSAlfred Perlstein char nettype[NETIDLEN]; /* Network type */ 768360efbdSAlfred Perlstein }; 778360efbdSAlfred Perlstein static struct rpc_call_private *rpc_call_private_main; 786f88d2a8SJohn Baldwin static thread_key_t rpc_call_key; 796f88d2a8SJohn Baldwin static once_t rpc_call_once = ONCE_INITIALIZER; 806f88d2a8SJohn Baldwin static int rpc_call_key_error; 818360efbdSAlfred Perlstein 826f88d2a8SJohn Baldwin static void rpc_call_key_init(void); 83c05ac53bSDavid E. O'Brien static void rpc_call_destroy(void *); 848360efbdSAlfred Perlstein 858360efbdSAlfred Perlstein static void 868360efbdSAlfred Perlstein rpc_call_destroy(void *vp) 8799064799SGarrett Wollman { 888360efbdSAlfred Perlstein struct rpc_call_private *rcp = (struct rpc_call_private *)vp; 8999064799SGarrett Wollman 908360efbdSAlfred Perlstein if (rcp) { 918360efbdSAlfred Perlstein if (rcp->client) 928360efbdSAlfred Perlstein CLNT_DESTROY(rcp->client); 938360efbdSAlfred Perlstein free(rcp); 9499064799SGarrett Wollman } 9599064799SGarrett Wollman } 968360efbdSAlfred Perlstein 976f88d2a8SJohn Baldwin static void 986f88d2a8SJohn Baldwin rpc_call_key_init(void) 996f88d2a8SJohn Baldwin { 1006f88d2a8SJohn Baldwin 1016f88d2a8SJohn Baldwin rpc_call_key_error = thr_keycreate(&rpc_call_key, rpc_call_destroy); 1026f88d2a8SJohn Baldwin } 1036f88d2a8SJohn Baldwin 1048360efbdSAlfred Perlstein /* 1058360efbdSAlfred Perlstein * This is the simplified interface to the client rpc layer. 1068360efbdSAlfred Perlstein * The client handle is not destroyed here and is reused for 1078360efbdSAlfred Perlstein * the future calls to same prog, vers, host and nettype combination. 1088360efbdSAlfred Perlstein * 1098360efbdSAlfred Perlstein * The total time available is 25 seconds. 1108360efbdSAlfred Perlstein */ 1118360efbdSAlfred Perlstein enum clnt_stat 1128360efbdSAlfred Perlstein rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype) 1138360efbdSAlfred Perlstein const char *host; /* host name */ 1148360efbdSAlfred Perlstein rpcprog_t prognum; /* program number */ 1158360efbdSAlfred Perlstein rpcvers_t versnum; /* version number */ 1168360efbdSAlfred Perlstein rpcproc_t procnum; /* procedure number */ 1178360efbdSAlfred Perlstein xdrproc_t inproc, outproc; /* in/out XDR procedures */ 1188360efbdSAlfred Perlstein const char *in; 1198360efbdSAlfred Perlstein char *out; /* recv/send data */ 1208360efbdSAlfred Perlstein const char *nettype; /* nettype */ 1218360efbdSAlfred Perlstein { 1228360efbdSAlfred Perlstein struct rpc_call_private *rcp = (struct rpc_call_private *) 0; 1238360efbdSAlfred Perlstein enum clnt_stat clnt_stat; 1248360efbdSAlfred Perlstein struct timeval timeout, tottimeout; 1258360efbdSAlfred Perlstein int main_thread = 1; 1268360efbdSAlfred Perlstein 1278360efbdSAlfred Perlstein if ((main_thread = thr_main())) { 1288360efbdSAlfred Perlstein rcp = rpc_call_private_main; 12999064799SGarrett Wollman } else { 1306f88d2a8SJohn Baldwin if (thr_once(&rpc_call_once, rpc_call_key_init) != 0 || 1316f88d2a8SJohn Baldwin rpc_call_key_error != 0) { 1326f88d2a8SJohn Baldwin rpc_createerr.cf_stat = RPC_SYSTEMERROR; 1336f88d2a8SJohn Baldwin rpc_createerr.cf_error.re_errno = rpc_call_key_error; 1346f88d2a8SJohn Baldwin return (rpc_createerr.cf_stat); 13599064799SGarrett Wollman } 1368360efbdSAlfred Perlstein rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key); 1378360efbdSAlfred Perlstein } 1388360efbdSAlfred Perlstein if (rcp == NULL) { 1398360efbdSAlfred Perlstein rcp = malloc(sizeof (*rcp)); 1408360efbdSAlfred Perlstein if (rcp == NULL) { 1418360efbdSAlfred Perlstein rpc_createerr.cf_stat = RPC_SYSTEMERROR; 1428360efbdSAlfred Perlstein rpc_createerr.cf_error.re_errno = errno; 1438360efbdSAlfred Perlstein return (rpc_createerr.cf_stat); 1448360efbdSAlfred Perlstein } 1458360efbdSAlfred Perlstein if (main_thread) 1468360efbdSAlfred Perlstein rpc_call_private_main = rcp; 1478360efbdSAlfred Perlstein else 1488360efbdSAlfred Perlstein thr_setspecific(rpc_call_key, (void *) rcp); 1498360efbdSAlfred Perlstein rcp->valid = 0; 1508360efbdSAlfred Perlstein rcp->client = NULL; 1518360efbdSAlfred Perlstein } 1522ad99b72SMartin Blapp if ((nettype == NULL) || (nettype[0] == 0)) 1538360efbdSAlfred Perlstein nettype = "netpath"; 1548360efbdSAlfred Perlstein if (!(rcp->valid && rcp->pid == getpid() && 1558360efbdSAlfred Perlstein (rcp->prognum == prognum) && 1568360efbdSAlfred Perlstein (rcp->versnum == versnum) && 1578360efbdSAlfred Perlstein (!strcmp(rcp->host, host)) && 1588360efbdSAlfred Perlstein (!strcmp(rcp->nettype, nettype)))) { 1598360efbdSAlfred Perlstein int fd; 1608360efbdSAlfred Perlstein 1618360efbdSAlfred Perlstein rcp->valid = 0; 1628360efbdSAlfred Perlstein if (rcp->client) 1638360efbdSAlfred Perlstein CLNT_DESTROY(rcp->client); 1648360efbdSAlfred Perlstein /* 1658360efbdSAlfred Perlstein * Using the first successful transport for that type 1668360efbdSAlfred Perlstein */ 1678360efbdSAlfred Perlstein rcp->client = clnt_create(host, prognum, versnum, nettype); 1688360efbdSAlfred Perlstein rcp->pid = getpid(); 1698360efbdSAlfred Perlstein if (rcp->client == NULL) { 1708360efbdSAlfred Perlstein return (rpc_createerr.cf_stat); 1718360efbdSAlfred Perlstein } 1728360efbdSAlfred Perlstein /* 1738360efbdSAlfred Perlstein * Set time outs for connectionless case. Do it 1748360efbdSAlfred Perlstein * unconditionally. Faster than doing a t_getinfo() 1758360efbdSAlfred Perlstein * and then doing the right thing. 1768360efbdSAlfred Perlstein */ 17799064799SGarrett Wollman timeout.tv_usec = 0; 17899064799SGarrett Wollman timeout.tv_sec = 5; 1798360efbdSAlfred Perlstein (void) CLNT_CONTROL(rcp->client, 1808360efbdSAlfred Perlstein CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout); 1818360efbdSAlfred Perlstein if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd)) 1828360efbdSAlfred Perlstein _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */ 1838360efbdSAlfred Perlstein rcp->prognum = prognum; 1848360efbdSAlfred Perlstein rcp->versnum = versnum; 1858360efbdSAlfred Perlstein if ((strlen(host) < (size_t)MAXHOSTNAMELEN) && 1868360efbdSAlfred Perlstein (strlen(nettype) < (size_t)NETIDLEN)) { 1878360efbdSAlfred Perlstein (void) strcpy(rcp->host, host); 1888360efbdSAlfred Perlstein (void) strcpy(rcp->nettype, nettype); 1898360efbdSAlfred Perlstein rcp->valid = 1; 1908360efbdSAlfred Perlstein } else { 1918360efbdSAlfred Perlstein rcp->valid = 0; 19299064799SGarrett Wollman } 1938360efbdSAlfred Perlstein } /* else reuse old client */ 19499064799SGarrett Wollman tottimeout.tv_sec = 25; 19599064799SGarrett Wollman tottimeout.tv_usec = 0; 1968360efbdSAlfred Perlstein /*LINTED const castaway*/ 1978360efbdSAlfred Perlstein clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in, 19899064799SGarrett Wollman outproc, out, tottimeout); 19999064799SGarrett Wollman /* 20099064799SGarrett Wollman * if call failed, empty cache 20199064799SGarrett Wollman */ 20299064799SGarrett Wollman if (clnt_stat != RPC_SUCCESS) 2038360efbdSAlfred Perlstein rcp->valid = 0; 2048360efbdSAlfred Perlstein return (clnt_stat); 20599064799SGarrett Wollman } 206