|
Packit Service |
4f68e0 |
/*
|
|
Packit Service |
4f68e0 |
* Copyright (c) 2009, Sun Microsystems, Inc.
|
|
Packit Service |
4f68e0 |
* All rights reserved.
|
|
Packit Service |
4f68e0 |
*
|
|
Packit Service |
4f68e0 |
* Redistribution and use in source and binary forms, with or without
|
|
Packit Service |
4f68e0 |
* modification, are permitted provided that the following conditions are met:
|
|
Packit Service |
4f68e0 |
* - Redistributions of source code must retain the above copyright notice,
|
|
Packit Service |
4f68e0 |
* this list of conditions and the following disclaimer.
|
|
Packit Service |
4f68e0 |
* - Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit Service |
4f68e0 |
* this list of conditions and the following disclaimer in the documentation
|
|
Packit Service |
4f68e0 |
* and/or other materials provided with the distribution.
|
|
Packit Service |
4f68e0 |
* - Neither the name of Sun Microsystems, Inc. nor the names of its
|
|
Packit Service |
4f68e0 |
* contributors may be used to endorse or promote products derived
|
|
Packit Service |
4f68e0 |
* from this software without specific prior written permission.
|
|
Packit Service |
4f68e0 |
*
|
|
Packit Service |
4f68e0 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit Service |
4f68e0 |
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit Service |
4f68e0 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit Service |
4f68e0 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
|
Packit Service |
4f68e0 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit Service |
4f68e0 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit Service |
4f68e0 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit Service |
4f68e0 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit Service |
4f68e0 |
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit Service |
4f68e0 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit Service |
4f68e0 |
* POSSIBILITY OF SUCH DAMAGE.
|
|
Packit Service |
4f68e0 |
*/
|
|
Packit Service |
4f68e0 |
/*
|
|
Packit Service |
4f68e0 |
* Copyright (c) 1986-1991 by Sun Microsystems Inc.
|
|
Packit Service |
4f68e0 |
*/
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
/*
|
|
Packit Service |
4f68e0 |
* clnt_simple.c
|
|
Packit Service |
4f68e0 |
* Simplified front end to client rpc.
|
|
Packit Service |
4f68e0 |
*/
|
|
Packit Service |
4f68e0 |
#include <pthread.h>
|
|
Packit Service |
4f68e0 |
#include <reentrant.h>
|
|
Packit Service |
4f68e0 |
#include <sys/param.h>
|
|
Packit Service |
4f68e0 |
#include <stdio.h>
|
|
Packit Service |
4f68e0 |
#include <errno.h>
|
|
Packit Service |
4f68e0 |
#include <rpc/rpc.h>
|
|
Packit Service |
4f68e0 |
#include <string.h>
|
|
Packit Service |
4f68e0 |
#include <stdlib.h>
|
|
Packit Service |
4f68e0 |
#include <fcntl.h>
|
|
Packit Service |
4f68e0 |
#include <unistd.h>
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
#include <rpc/clnt.h>
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
#ifndef MAXHOSTNAMELEN
|
|
Packit Service |
4f68e0 |
#define MAXHOSTNAMELEN 64
|
|
Packit Service |
4f68e0 |
#endif
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
#ifndef NETIDLEN
|
|
Packit Service |
4f68e0 |
#define NETIDLEN 32
|
|
Packit Service |
4f68e0 |
#endif
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
struct rpc_call_private {
|
|
Packit Service |
4f68e0 |
int valid; /* Is this entry valid ? */
|
|
Packit Service |
4f68e0 |
CLIENT *client; /* Client handle */
|
|
Packit Service |
4f68e0 |
pid_t pid; /* process-id at moment of creation */
|
|
Packit Service |
4f68e0 |
rpcprog_t prognum; /* Program */
|
|
Packit Service |
4f68e0 |
rpcvers_t versnum; /* Version */
|
|
Packit Service |
4f68e0 |
char host[MAXHOSTNAMELEN]; /* Servers host */
|
|
Packit Service |
4f68e0 |
char nettype[NETIDLEN]; /* Network type */
|
|
Packit Service |
4f68e0 |
};
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
static void rpc_call_destroy(void *);
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
static void
|
|
Packit Service |
4f68e0 |
rpc_call_destroy(void *vp)
|
|
Packit Service |
4f68e0 |
{
|
|
Packit Service |
4f68e0 |
struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
if (rcp) {
|
|
Packit Service |
4f68e0 |
if (rcp->client)
|
|
Packit Service |
4f68e0 |
CLNT_DESTROY(rcp->client);
|
|
Packit Service |
4f68e0 |
free(rcp);
|
|
Packit Service |
4f68e0 |
}
|
|
Packit Service |
4f68e0 |
}
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
/*
|
|
Packit Service |
4f68e0 |
* This is the simplified interface to the client rpc layer.
|
|
Packit Service |
4f68e0 |
* The client handle is not destroyed here and is reused for
|
|
Packit Service |
4f68e0 |
* the future calls to same prog, vers, host and nettype combination.
|
|
Packit Service |
4f68e0 |
*
|
|
Packit Service |
4f68e0 |
* The total time available is 25 seconds.
|
|
Packit Service |
4f68e0 |
*/
|
|
Packit Service |
4f68e0 |
enum clnt_stat
|
|
Packit Service |
4f68e0 |
rpc_call(host, prognum, versnum, procnum, inproc, in, outproc, out, nettype)
|
|
Packit Service |
4f68e0 |
const char *host; /* host name */
|
|
Packit Service |
4f68e0 |
rpcprog_t prognum; /* program number */
|
|
Packit Service |
4f68e0 |
rpcvers_t versnum; /* version number */
|
|
Packit Service |
4f68e0 |
rpcproc_t procnum; /* procedure number */
|
|
Packit Service |
4f68e0 |
xdrproc_t inproc, outproc; /* in/out XDR procedures */
|
|
Packit Service |
4f68e0 |
const char *in;
|
|
Packit Service |
4f68e0 |
char *out; /* recv/send data */
|
|
Packit Service |
4f68e0 |
const char *nettype; /* nettype */
|
|
Packit Service |
4f68e0 |
{
|
|
Packit Service |
4f68e0 |
struct rpc_call_private *rcp = (struct rpc_call_private *) 0;
|
|
Packit Service |
4f68e0 |
enum clnt_stat clnt_stat;
|
|
Packit Service |
4f68e0 |
struct timeval timeout, tottimeout;
|
|
Packit Service |
4f68e0 |
extern thread_key_t rpc_call_key;
|
|
Packit Service |
4f68e0 |
extern mutex_t tsd_lock;
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
if (rpc_call_key == KEY_INITIALIZER) {
|
|
Packit Service |
4f68e0 |
mutex_lock(&tsd_lock);
|
|
Packit Service |
4f68e0 |
if (rpc_call_key == KEY_INITIALIZER)
|
|
Packit Service |
4f68e0 |
thr_keycreate(&rpc_call_key, rpc_call_destroy);
|
|
Packit Service |
4f68e0 |
mutex_unlock(&tsd_lock);
|
|
Packit Service |
4f68e0 |
}
|
|
Packit Service |
4f68e0 |
rcp = (struct rpc_call_private *)thr_getspecific(rpc_call_key);
|
|
Packit Service |
4f68e0 |
if (rcp == NULL) {
|
|
Packit Service |
4f68e0 |
rcp = malloc(sizeof (*rcp));
|
|
Packit Service |
4f68e0 |
if (rcp == NULL) {
|
|
Packit Service |
4f68e0 |
rpc_createerr.cf_stat = RPC_SYSTEMERROR;
|
|
Packit Service |
4f68e0 |
rpc_createerr.cf_error.re_errno = errno;
|
|
Packit Service |
4f68e0 |
return (rpc_createerr.cf_stat);
|
|
Packit Service |
4f68e0 |
}
|
|
Packit Service |
4f68e0 |
thr_setspecific(rpc_call_key, (void *) rcp);
|
|
Packit Service |
4f68e0 |
rcp->valid = 0;
|
|
Packit Service |
4f68e0 |
rcp->client = NULL;
|
|
Packit Service |
4f68e0 |
}
|
|
Packit Service |
4f68e0 |
if ((nettype == NULL) || (nettype[0] == 0))
|
|
Packit Service |
4f68e0 |
nettype = "netpath";
|
|
Packit Service |
4f68e0 |
if (!(rcp->valid && rcp->pid == getpid() &&
|
|
Packit Service |
4f68e0 |
(rcp->prognum == prognum) &&
|
|
Packit Service |
4f68e0 |
(rcp->versnum == versnum) &&
|
|
Packit Service |
4f68e0 |
(!strcmp(rcp->host, host)) &&
|
|
Packit Service |
4f68e0 |
(!strcmp(rcp->nettype, nettype)))) {
|
|
Packit Service |
4f68e0 |
int fd;
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
rcp->valid = 0;
|
|
Packit Service |
4f68e0 |
if (rcp->client)
|
|
Packit Service |
4f68e0 |
CLNT_DESTROY(rcp->client);
|
|
Packit Service |
4f68e0 |
/*
|
|
Packit Service |
4f68e0 |
* Using the first successful transport for that type
|
|
Packit Service |
4f68e0 |
*/
|
|
Packit Service |
4f68e0 |
rcp->client = clnt_create(host, prognum, versnum, nettype);
|
|
Packit Service |
4f68e0 |
rcp->pid = getpid();
|
|
Packit Service |
4f68e0 |
if (rcp->client == NULL) {
|
|
Packit Service |
4f68e0 |
return (rpc_createerr.cf_stat);
|
|
Packit Service |
4f68e0 |
}
|
|
Packit Service |
4f68e0 |
/*
|
|
Packit Service |
4f68e0 |
* Set time outs for connectionless case. Do it
|
|
Packit Service |
4f68e0 |
* unconditionally. Faster than doing a t_getinfo()
|
|
Packit Service |
4f68e0 |
* and then doing the right thing.
|
|
Packit Service |
4f68e0 |
*/
|
|
Packit Service |
4f68e0 |
timeout.tv_usec = 0;
|
|
Packit Service |
4f68e0 |
timeout.tv_sec = 5;
|
|
Packit Service |
4f68e0 |
(void) CLNT_CONTROL(rcp->client,
|
|
Packit Service |
4f68e0 |
CLSET_RETRY_TIMEOUT, (char *)(void *)&timeout);
|
|
Packit Service |
4f68e0 |
if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)(void *)&fd))
|
|
Packit Service |
4f68e0 |
fcntl(fd, F_SETFD, 1); /* make it "close on exec" */
|
|
Packit Service |
4f68e0 |
rcp->prognum = prognum;
|
|
Packit Service |
4f68e0 |
rcp->versnum = versnum;
|
|
Packit Service |
4f68e0 |
if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
|
|
Packit Service |
4f68e0 |
(strlen(nettype) < (size_t)NETIDLEN)) {
|
|
Packit Service |
4f68e0 |
(void) strcpy(rcp->host, host);
|
|
Packit Service |
4f68e0 |
(void) strcpy(rcp->nettype, nettype);
|
|
Packit Service |
4f68e0 |
rcp->valid = 1;
|
|
Packit Service |
4f68e0 |
} else {
|
|
Packit Service |
4f68e0 |
rcp->valid = 0;
|
|
Packit Service |
4f68e0 |
}
|
|
Packit Service |
4f68e0 |
} /* else reuse old client */
|
|
Packit Service |
4f68e0 |
tottimeout.tv_sec = 25;
|
|
Packit Service |
4f68e0 |
tottimeout.tv_usec = 0;
|
|
Packit Service |
4f68e0 |
|
|
Packit Service |
4f68e0 |
/* LINTED const castaway */
|
|
Packit Service |
4f68e0 |
clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *) in,
|
|
Packit Service |
4f68e0 |
outproc, out, tottimeout);
|
|
Packit Service |
4f68e0 |
/*
|
|
Packit Service |
4f68e0 |
* if call failed, empty cache
|
|
Packit Service |
4f68e0 |
*/
|
|
Packit Service |
4f68e0 |
if (clnt_stat != RPC_SUCCESS)
|
|
Packit Service |
4f68e0 |
rcp->valid = 0;
|
|
Packit Service |
4f68e0 |
return (clnt_stat);
|
|
Packit Service |
4f68e0 |
}
|