2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A/*
2N/A * Portions of this source code were derived from Berkeley
2N/A * 4.3 BSD under license from the Regents of the University of
2N/A * California.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A/*
2N/A * Simplified front end to client rpc.
2N/A */
2N/A
2N/A#include "mt.h"
2N/A#include "rpc_mt.h"
2N/A#include <stdio.h>
2N/A#include <errno.h>
2N/A#include <rpc/rpc.h>
2N/A#include <string.h>
2N/A#include <sys/param.h>
2N/A#include <stdlib.h>
2N/A#include <unistd.h>
2N/A
2N/A#ifndef MAXHOSTNAMELEN
2N/A#define MAXHOSTNAMELEN 64
2N/A#endif
2N/A
2N/A#ifndef NETIDLEN
2N/A#define NETIDLEN 32
2N/A#endif
2N/A
2N/Astruct rpc_call_private {
2N/A int valid; /* Is this entry valid ? */
2N/A CLIENT *client; /* Client handle */
2N/A pid_t pid; /* process-id at moment of creation */
2N/A rpcprog_t prognum; /* Program */
2N/A rpcvers_t versnum; /* version */
2N/A char host[MAXHOSTNAMELEN]; /* Servers host */
2N/A char nettype[NETIDLEN]; /* Network type */
2N/A};
2N/A
2N/Astatic void
2N/Arpc_call_destroy(void *vp)
2N/A{
2N/A struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
2N/A
2N/A if (rcp) {
2N/A if (rcp->client)
2N/A CLNT_DESTROY(rcp->client);
2N/A free(rcp);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * This is the simplified interface to the client rpc layer.
2N/A * The client handle is not destroyed here and is reused for
2N/A * the future calls to same prog, vers, host and nettype combination.
2N/A *
2N/A * The total time available is 25 seconds.
2N/A */
2N/Aenum clnt_stat
2N/Arpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum,
2N/A const rpcproc_t procnum, const xdrproc_t inproc, const char *in,
2N/A const xdrproc_t outproc, char *out, const char *netclass)
2N/A{
2N/A struct rpc_call_private *rcp;
2N/A enum clnt_stat clnt_stat;
2N/A struct timeval timeout, tottimeout;
2N/A static pthread_key_t rpc_call_key = PTHREAD_ONCE_KEY_NP;
2N/A char nettype_array[NETIDLEN];
2N/A char *nettype = &nettype_array[0];
2N/A
2N/A if (netclass == NULL)
2N/A nettype = NULL;
2N/A else {
2N/A size_t len = strlen(netclass);
2N/A if (len >= sizeof (nettype_array)) {
2N/A rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2N/A return (rpc_createerr.cf_stat);
2N/A }
2N/A (void) strcpy(nettype, netclass);
2N/A }
2N/A
2N/A rcp = thr_get_storage(&rpc_call_key, sizeof (*rcp), rpc_call_destroy);
2N/A if (rcp == NULL) {
2N/A rpc_createerr.cf_stat = RPC_SYSTEMERROR;
2N/A rpc_createerr.cf_error.re_errno = errno;
2N/A return (rpc_createerr.cf_stat);
2N/A }
2N/A
2N/A if ((nettype == NULL) || (nettype[0] == NULL))
2N/A nettype = "netpath";
2N/A if (!(rcp->valid &&
2N/A rcp->pid == getpid() &&
2N/A rcp->prognum == prognum &&
2N/A rcp->versnum == versnum &&
2N/A strcmp(rcp->host, host) == 0 &&
2N/A strcmp(rcp->nettype, nettype) == 0)) {
2N/A int fd;
2N/A
2N/A rcp->valid = 0;
2N/A if (rcp->client)
2N/A CLNT_DESTROY(rcp->client);
2N/A /*
2N/A * Using the first successful transport for that type
2N/A */
2N/A rcp->client = clnt_create(host, prognum, versnum, nettype);
2N/A rcp->pid = getpid();
2N/A if (rcp->client == NULL)
2N/A return (rpc_createerr.cf_stat);
2N/A /*
2N/A * Set time outs for connectionless case. Do it
2N/A * unconditionally. Faster than doing a t_getinfo()
2N/A * and then doing the right thing.
2N/A */
2N/A timeout.tv_usec = 0;
2N/A timeout.tv_sec = 5;
2N/A (void) CLNT_CONTROL(rcp->client,
2N/A CLSET_RETRY_TIMEOUT, (char *)&timeout);
2N/A if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)&fd))
2N/A (void) fcntl(fd, F_SETFD, 1); /* close on exec */
2N/A rcp->prognum = prognum;
2N/A rcp->versnum = versnum;
2N/A if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
2N/A (strlen(nettype) < (size_t)NETIDLEN)) {
2N/A (void) strcpy(rcp->host, host);
2N/A (void) strcpy(rcp->nettype, nettype);
2N/A rcp->valid = 1;
2N/A } else {
2N/A rcp->valid = 0;
2N/A }
2N/A } /* else reuse old client */
2N/A tottimeout.tv_sec = 25;
2N/A tottimeout.tv_usec = 0;
2N/A clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *)in,
2N/A outproc, out, tottimeout);
2N/A /*
2N/A * if call failed, empty cache
2N/A */
2N/A if (clnt_stat != RPC_SUCCESS)
2N/A rcp->valid = 0;
2N/A return (clnt_stat);
2N/A}