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 #if defined(LIBC_SCCS) && !defined(lint) 36 /*static char *sccsid = "from: @(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";*/ 37 /*static char *sccsid = "from: @(#)clnt_simple.c 2.2 88/08/01 4.0 RPCSRC";*/ 38 static char *rcsid = "$FreeBSD$"; 39 #endif 40 41 /* 42 * clnt_simple.c 43 * Simplified front end to client rpc. 44 * 45 */ 46 47 #include "namespace.h" 48 #include "reentrant.h" 49 #include <sys/param.h> 50 #include <stdio.h> 51 #include <errno.h> 52 #include <rpc/rpc.h> 53 #include <string.h> 54 #include <stdlib.h> 55 #include <fcntl.h> 56 #include <unistd.h> 57 #include "un-namespace.h" 58 59 #ifndef MAXHOSTNAMELEN 60 #define MAXHOSTNAMELEN 64 61 #endif 62 63 #ifndef NETIDLEN 64 #define NETIDLEN 32 65 #endif 66 67 struct rpc_call_private { 68 int valid; /* Is this entry valid ? */ 69 CLIENT *client; /* Client handle */ 70 pid_t pid; /* process-id at moment of creation */ 71 rpcprog_t prognum; /* Program */ 72 rpcvers_t versnum; /* Version */ 73 char host[MAXHOSTNAMELEN]; /* Servers host */ 74 char nettype[NETIDLEN]; /* Network type */ 75 }; 76 static struct rpc_call_private *rpc_call_private_main; 77 78 static void rpc_call_destroy __P((void *)); 79 80 static void 81 rpc_call_destroy(void *vp) 82 { 83 struct rpc_call_private *rcp = (struct rpc_call_private *)vp; 84 85 if (rcp) { 86 if (rcp->client) 87 CLNT_DESTROY(rcp->client); 88 free(rcp); 89 } 90 } 91 92 /* 93 * This is the simplified interface to the client rpc layer. 94 * The client handle is not destroyed here and is reused for 95 * the future calls to same prog, vers, host and nettype combination. 96 * 97 * The total time available is 25 seconds. 98 */ 99 enum clnt_stat 100 rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype) 101 const char *host; /* host name */ 102 rpcprog_t prognum; /* program number */ 103 rpcvers_t versnum; /* version number */ 104 rpcproc_t procnum; /* procedure number */ 105 xdrproc_t inproc, outproc; /* in/out XDR procedures */ 106 const char *in; 107 char *out; /* recv/send data */ 108 const char *nettype; /* nettype */ 109 { 110 struct rpc_call_private *rcp = (struct rpc_call_private *) 0; 111 enum clnt_stat clnt_stat; 112 struct timeval timeout, tottimeout; 113 static thread_key_t rpc_call_key; 114 extern mutex_t tsd_lock; 115 int main_thread = 1; 116 117 if ((main_thread = thr_main())) { 118 rcp = rpc_call_private_main; 119 } else { 120 if (rpc_call_key == 0) { 121 mutex_lock(&tsd_lock); 122 if (rpc_call_key == 0) 123 thr_keycreate(&rpc_call_key, rpc_call_destroy); 124 mutex_unlock(&tsd_lock); 125 } 126 rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key); 127 } 128 if (rcp == NULL) { 129 rcp = malloc(sizeof (*rcp)); 130 if (rcp == NULL) { 131 rpc_createerr.cf_stat = RPC_SYSTEMERROR; 132 rpc_createerr.cf_error.re_errno = errno; 133 return (rpc_createerr.cf_stat); 134 } 135 if (main_thread) 136 rpc_call_private_main = rcp; 137 else 138 thr_setspecific(rpc_call_key, (void *) rcp); 139 rcp->valid = 0; 140 rcp->client = NULL; 141 } 142 if ((nettype == NULL) || (nettype[0] == NULL)) 143 nettype = "netpath"; 144 if (!(rcp->valid && rcp->pid == getpid() && 145 (rcp->prognum == prognum) && 146 (rcp->versnum == versnum) && 147 (!strcmp(rcp->host, host)) && 148 (!strcmp(rcp->nettype, nettype)))) { 149 int fd; 150 151 rcp->valid = 0; 152 if (rcp->client) 153 CLNT_DESTROY(rcp->client); 154 /* 155 * Using the first successful transport for that type 156 */ 157 rcp->client = clnt_create(host, prognum, versnum, nettype); 158 rcp->pid = getpid(); 159 if (rcp->client == NULL) { 160 return (rpc_createerr.cf_stat); 161 } 162 /* 163 * Set time outs for connectionless case. Do it 164 * unconditionally. Faster than doing a t_getinfo() 165 * and then doing the right thing. 166 */ 167 timeout.tv_usec = 0; 168 timeout.tv_sec = 5; 169 (void) CLNT_CONTROL(rcp->client, 170 CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout); 171 if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd)) 172 _fcntl(fd, F_SETFD, 1); /* make it "close on exec" */ 173 rcp->prognum = prognum; 174 rcp->versnum = versnum; 175 if ((strlen(host) < (size_t)MAXHOSTNAMELEN) && 176 (strlen(nettype) < (size_t)NETIDLEN)) { 177 (void) strcpy(rcp->host, host); 178 (void) strcpy(rcp->nettype, nettype); 179 rcp->valid = 1; 180 } else { 181 rcp->valid = 0; 182 } 183 } /* else reuse old client */ 184 tottimeout.tv_sec = 25; 185 tottimeout.tv_usec = 0; 186 /*LINTED const castaway*/ 187 clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in, 188 outproc, out, tottimeout); 189 /* 190 * if call failed, empty cache 191 */ 192 if (clnt_stat != RPC_SUCCESS) 193 rcp->valid = 0; 194 return (clnt_stat); 195 } 196