1 /* $NetBSD: clnt_simple.c,v 1.21 2000/07/06 03:10:34 christos Exp $ */ 2 3 /* 4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 5 * unrestricted use provided that this legend is included on all tape 6 * media and as a part of the software program in whole or part. Users 7 * may copy or modify Sun RPC without charge, but are not authorized 8 * to license or distribute it to anyone else except as part of a product or 9 * program developed by the user. 10 * 11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 14 * 15 * Sun RPC is provided with no support and without any obligation on the 16 * part of Sun Microsystems, Inc. to assist in its use, correction, 17 * modification or enhancement. 18 * 19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 21 * OR ANY PART THEREOF. 22 * 23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 24 * or profits or other special, indirect and consequential damages, even if 25 * Sun has been advised of the possibility of such damages. 26 * 27 * Sun Microsystems, Inc. 28 * 2550 Garcia Avenue 29 * Mountain View, California 94043 30 */ 31 /* 32 * Copyright (c) 1986-1991 by Sun Microsystems Inc. 33 */ 34 35 /* #ident "@(#)clnt_simple.c 1.17 94/04/24 SMI" */ 36 37 #if 0 38 #if !defined(lint) && defined(SCCSIDS) 39 static char sccsid[] = "@(#)clnt_simple.c 1.49 89/01/31 Copyr 1984 Sun Micro"; 40 #endif 41 #endif 42 43 /* 44 * clnt_simple.c 45 * Simplified front end to client rpc. 46 * 47 */ 48 49 #include "namespace.h" 50 #include "reentrant.h" 51 #include <sys/param.h> 52 #include <stdio.h> 53 #include <errno.h> 54 #include <rpc/rpc.h> 55 #include <string.h> 56 #include <stdlib.h> 57 #include <fcntl.h> 58 #include <unistd.h> 59 60 #ifdef __weak_alias 61 __weak_alias(rpc_call,_rpc_call) 62 #endif 63 64 #ifndef MAXHOSTNAMELEN 65 #define MAXHOSTNAMELEN 64 66 #endif 67 68 #ifndef NETIDLEN 69 #define NETIDLEN 32 70 #endif 71 72 struct rpc_call_private { 73 int valid; /* Is this entry valid ? */ 74 CLIENT *client; /* Client handle */ 75 pid_t pid; /* process-id at moment of creation */ 76 rpcprog_t prognum; /* Program */ 77 rpcvers_t versnum; /* Version */ 78 char host[MAXHOSTNAMELEN]; /* Servers host */ 79 char nettype[NETIDLEN]; /* Network type */ 80 }; 81 static struct rpc_call_private *rpc_call_private_main; 82 83 #ifdef __REENT 84 static void rpc_call_destroy __P((void *)); 85 86 static void 87 rpc_call_destroy(void *vp) 88 { 89 struct rpc_call_private *rcp = (struct rpc_call_private *)vp; 90 91 if (rcp) { 92 if (rcp->client) 93 CLNT_DESTROY(rcp->client); 94 free(rcp); 95 } 96 } 97 #endif 98 99 /* 100 * This is the simplified interface to the client rpc layer. 101 * The client handle is not destroyed here and is reused for 102 * the future calls to same prog, vers, host and nettype combination. 103 * 104 * The total time available is 25 seconds. 105 */ 106 enum clnt_stat 107 rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype) 108 const char *host; /* host name */ 109 rpcprog_t prognum; /* program number */ 110 rpcvers_t versnum; /* version number */ 111 rpcproc_t procnum; /* procedure number */ 112 xdrproc_t inproc, outproc; /* in/out XDR procedures */ 113 const char *in; 114 char *out; /* recv/send data */ 115 const char *nettype; /* nettype */ 116 { 117 struct rpc_call_private *rcp = (struct rpc_call_private *) 0; 118 enum clnt_stat clnt_stat; 119 struct timeval timeout, tottimeout; 120 #ifdef __REENT 121 static thread_key_t rpc_call_key; 122 extern mutex_t tsd_lock; 123 #endif 124 int main_thread = 1; 125 126 #ifdef __REENT 127 if ((main_thread = _thr_main())) { 128 rcp = rpc_call_private_main; 129 } else { 130 if (rpc_call_key == 0) { 131 mutex_lock(&tsd_lock); 132 if (rpc_call_key == 0) 133 thr_keycreate(&rpc_call_key, rpc_call_destroy); 134 mutex_unlock(&tsd_lock); 135 } 136 thr_getspecific(rpc_call_key, (void **) &rcp); 137 } 138 #else 139 rcp = rpc_call_private_main; 140 #endif 141 if (rcp == NULL) { 142 rcp = malloc(sizeof (*rcp)); 143 if (rcp == NULL) { 144 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 145 rpc_createerr.cf_error.re_errno = errno; 146 return (rpc_createerr.cf_stat); 147 } 148 if (main_thread) 149 rpc_call_private_main = rcp; 150 else 151 thr_setspecific(rpc_call_key, (void *) rcp); 152 rcp->valid = 0; 153 rcp->client = NULL; 154 } 155 if ((nettype == NULL) || (nettype[0] == NULL)) 156 nettype = "netpath"; 157 if (!(rcp->valid && rcp->pid == getpid() && 158 (rcp->prognum == prognum) && 159 (rcp->versnum == versnum) && 160 (!strcmp(rcp->host, host)) && 161 (!strcmp(rcp->nettype, nettype)))) { 162 int fd; 163 164 rcp->valid = 0; 165 if (rcp->client) 166 CLNT_DESTROY(rcp->client); 167 /* 168 * Using the first successful transport for that type 169 */ 170 rcp->client = clnt_create(host, prognum, versnum, nettype); 171 rcp->pid = getpid(); 172 if (rcp->client == NULL) { 173 return (rpc_createerr.cf_stat); 174 } 175 /* 176 * Set time outs for connectionless case. Do it 177 * unconditionally. Faster than doing a t_getinfo() 178 * and then doing the right thing. 179 */ 180 timeout.tv_usec = 0; 181 timeout.tv_sec = 5; 182 (void) CLNT_CONTROL(rcp->client, 183 CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout); 184 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd)) 185 fcntl(fd, F_SETFD, 1); /* make it "close on exec" */ 186 rcp->prognum = prognum; 187 rcp->versnum = versnum; 188 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) && 189 (strlen(nettype) < (size_t)NETIDLEN)) { 190 (void) strcpy(rcp->host, host); 191 (void) strcpy(rcp->nettype, nettype); 192 rcp->valid = 1; 193 } else { 194 rcp->valid = 0; 195 } 196 } /* else reuse old client */ 197 tottimeout.tv_sec = 25; 198 tottimeout.tv_usec = 0; 199 /*LINTED const castaway*/ 200 clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in, 201 outproc, out, tottimeout); 202 /* 203 * if call failed, empty cache 204 */ 205 if (clnt_stat != RPC_SUCCESS) 206 rcp->valid = 0; 207 return (clnt_stat); 208 } 209