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