1 /* $NetBSD: clnt_simple.c,v 1.33 2015/01/20 18:31:25 christos Exp $ */ 2 3 /* 4 * Copyright (c) 2010, Oracle America, Inc. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are 8 * met: 9 * 10 * * Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * * Redistributions in binary form must reproduce the above 13 * copyright notice, this list of conditions and the following 14 * disclaimer in the documentation and/or other materials 15 * provided with the distribution. 16 * * Neither the name of the "Oracle America, Inc." nor the names of its 17 * contributors may be used to endorse or promote products derived 18 * from this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 /* 34 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 35 */ 36 37 /* #ident "@(#)clnt_simple.c 1.17 94/04/24 SMI" */ 38 39 #include <sys/cdefs.h> 40 #if defined(LIBC_SCCS) && !defined(lint) 41 #if 0 42 static char sccsid[] = "@(#)clnt_simple.c 1.49 89/01/31 Copyr 1984 Sun Micro"; 43 #else 44 __RCSID("$NetBSD: clnt_simple.c,v 1.33 2015/01/20 18:31:25 christos Exp $"); 45 #endif 46 #endif 47 48 /* 49 * clnt_simple.c 50 * Simplified front end to client rpc. 51 * 52 */ 53 54 #include "namespace.h" 55 #include "reentrant.h" 56 #include <sys/param.h> 57 #include <stdio.h> 58 #include <assert.h> 59 #include <errno.h> 60 #include <rpc/rpc.h> 61 #include <string.h> 62 #include <stdlib.h> 63 #include <fcntl.h> 64 #include <unistd.h> 65 66 #ifdef __weak_alias 67 __weak_alias(rpc_call,_rpc_call) 68 #endif 69 70 #ifndef MAXHOSTNAMELEN 71 #define MAXHOSTNAMELEN 64 72 #endif 73 74 #ifndef NETIDLEN 75 #define NETIDLEN 32 76 #endif 77 78 struct rpc_call_private { 79 int valid; /* Is this entry valid ? */ 80 CLIENT *client; /* Client handle */ 81 pid_t pid; /* process-id at moment of creation */ 82 rpcprog_t prognum; /* Program */ 83 rpcvers_t versnum; /* Version */ 84 char host[MAXHOSTNAMELEN]; /* Servers host */ 85 char nettype[NETIDLEN]; /* Network type */ 86 }; 87 static struct rpc_call_private *rpc_call_private_main; 88 89 #ifdef _REENTRANT 90 static void rpc_call_destroy(void *); 91 92 static void 93 rpc_call_destroy(void *vp) 94 { 95 struct rpc_call_private *rcp = (struct rpc_call_private *)vp; 96 97 if (rcp) { 98 if (rcp->client) 99 CLNT_DESTROY(rcp->client); 100 free(rcp); 101 } 102 } 103 static thread_key_t rpc_call_key; 104 static once_t rpc_call_once = ONCE_INITIALIZER; 105 106 static void 107 rpc_call_setup(void) 108 { 109 110 thr_keycreate(&rpc_call_key, rpc_call_destroy); 111 } 112 #endif 113 114 115 /* 116 * This is the simplified interface to the client rpc layer. 117 * The client handle is not destroyed here and is reused for 118 * the future calls to same prog, vers, host and nettype combination. 119 * 120 * The total time available is 25 seconds. 121 */ 122 enum clnt_stat 123 rpc_call( 124 const char * host, /* host name */ 125 rpcprog_t prognum, /* program number */ 126 rpcvers_t versnum, /* version number */ 127 rpcproc_t procnum, /* procedure number */ 128 xdrproc_t inproc, /* in XDR procedures */ 129 const char * in, /* recv data */ 130 xdrproc_t outproc, /* out XDR procedures */ 131 char * out, /* send data */ 132 const char * nettype) /* nettype */ 133 { 134 struct rpc_call_private *rcp = (struct rpc_call_private *) 0; 135 enum clnt_stat clnt_stat; 136 struct timeval timeout, tottimeout; 137 138 _DIAGASSERT(host != NULL); 139 /* XXX: in may be NULL ??? */ 140 /* XXX: out may be NULL ??? */ 141 /* XXX: nettype may be NULL ??? */ 142 143 #ifdef _REENTRANT 144 if (__isthreaded) { 145 thr_once(&rpc_call_once, rpc_call_setup); 146 rcp = thr_getspecific(rpc_call_key); 147 } else 148 #endif 149 rcp = rpc_call_private_main; 150 if (rcp == NULL) { 151 rcp = malloc(sizeof (*rcp)); 152 if (rcp == NULL) { 153 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 154 rpc_createerr.cf_error.re_errno = errno; 155 return (rpc_createerr.cf_stat); 156 } 157 #ifdef _REENTRANT 158 if (__isthreaded) 159 thr_setspecific(rpc_call_key, (void *) rcp); 160 else 161 #endif 162 rpc_call_private_main = rcp; 163 rcp->valid = 0; 164 rcp->client = NULL; 165 } 166 if ((nettype == NULL) || (nettype[0] == 0)) 167 nettype = "netpath"; 168 if (!(rcp->valid && rcp->pid == getpid() && 169 (rcp->prognum == prognum) && 170 (rcp->versnum == versnum) && 171 (!strcmp(rcp->host, host)) && 172 (!strcmp(rcp->nettype, nettype)))) { 173 int fd; 174 175 rcp->valid = 0; 176 if (rcp->client) 177 CLNT_DESTROY(rcp->client); 178 /* 179 * Using the first successful transport for that type 180 */ 181 rcp->client = clnt_create(host, prognum, versnum, nettype); 182 rcp->pid = getpid(); 183 if (rcp->client == NULL) { 184 return (rpc_createerr.cf_stat); 185 } 186 /* 187 * Set time outs for connectionless case. Do it 188 * unconditionally. Faster than doing a t_getinfo() 189 * and then doing the right thing. 190 */ 191 timeout.tv_usec = 0; 192 timeout.tv_sec = 5; 193 (void) CLNT_CONTROL(rcp->client, 194 CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout); 195 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd)) 196 (void)fcntl(fd, F_SETFD, FD_CLOEXEC); 197 rcp->prognum = prognum; 198 rcp->versnum = versnum; 199 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) && 200 (strlen(nettype) < (size_t)NETIDLEN)) { 201 (void) strcpy(rcp->host, host); 202 (void) strcpy(rcp->nettype, nettype); 203 rcp->valid = 1; 204 } else { 205 rcp->valid = 0; 206 } 207 } /* else reuse old client */ 208 tottimeout.tv_sec = 25; 209 tottimeout.tv_usec = 0; 210 clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, in, 211 outproc, out, tottimeout); 212 /* 213 * if call failed, empty cache 214 */ 215 if (clnt_stat != RPC_SUCCESS) 216 rcp->valid = 0; 217 return (clnt_stat); 218 } 219