17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5cb620785Sraf * Common Development and Distribution License (the "License").
6cb620785Sraf * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
2061961e0fSrobinson */
2161961e0fSrobinson
2261961e0fSrobinson /*
23cb620785Sraf * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
26e8031f0aSraf
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate * Portions of this source code were derived from Berkeley
317c478bd9Sstevel@tonic-gate * 4.3 BSD under license from the Regents of the University of
327c478bd9Sstevel@tonic-gate * California.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate /*
367c478bd9Sstevel@tonic-gate * Simplified front end to client rpc.
377c478bd9Sstevel@tonic-gate */
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate #include "mt.h"
407c478bd9Sstevel@tonic-gate #include "rpc_mt.h"
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <errno.h>
437c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
447c478bd9Sstevel@tonic-gate #include <string.h>
457c478bd9Sstevel@tonic-gate #include <sys/param.h>
467c478bd9Sstevel@tonic-gate #include <stdlib.h>
477c478bd9Sstevel@tonic-gate #include <unistd.h>
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #ifndef MAXHOSTNAMELEN
507c478bd9Sstevel@tonic-gate #define MAXHOSTNAMELEN 64
517c478bd9Sstevel@tonic-gate #endif
527c478bd9Sstevel@tonic-gate
537c478bd9Sstevel@tonic-gate #ifndef NETIDLEN
547c478bd9Sstevel@tonic-gate #define NETIDLEN 32
557c478bd9Sstevel@tonic-gate #endif
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate struct rpc_call_private {
587c478bd9Sstevel@tonic-gate int valid; /* Is this entry valid ? */
597c478bd9Sstevel@tonic-gate CLIENT *client; /* Client handle */
607c478bd9Sstevel@tonic-gate pid_t pid; /* process-id at moment of creation */
617c478bd9Sstevel@tonic-gate rpcprog_t prognum; /* Program */
627c478bd9Sstevel@tonic-gate rpcvers_t versnum; /* version */
637c478bd9Sstevel@tonic-gate char host[MAXHOSTNAMELEN]; /* Servers host */
647c478bd9Sstevel@tonic-gate char nettype[NETIDLEN]; /* Network type */
657c478bd9Sstevel@tonic-gate };
667c478bd9Sstevel@tonic-gate
677c478bd9Sstevel@tonic-gate static void
rpc_call_destroy(void * vp)687c478bd9Sstevel@tonic-gate rpc_call_destroy(void *vp)
697c478bd9Sstevel@tonic-gate {
707c478bd9Sstevel@tonic-gate struct rpc_call_private *rcp = (struct rpc_call_private *)vp;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate if (rcp) {
737c478bd9Sstevel@tonic-gate if (rcp->client)
747c478bd9Sstevel@tonic-gate CLNT_DESTROY(rcp->client);
757c478bd9Sstevel@tonic-gate free(rcp);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * This is the simplified interface to the client rpc layer.
817c478bd9Sstevel@tonic-gate * The client handle is not destroyed here and is reused for
827c478bd9Sstevel@tonic-gate * the future calls to same prog, vers, host and nettype combination.
837c478bd9Sstevel@tonic-gate *
847c478bd9Sstevel@tonic-gate * The total time available is 25 seconds.
857c478bd9Sstevel@tonic-gate */
867c478bd9Sstevel@tonic-gate enum clnt_stat
rpc_call(const char * host,const rpcprog_t prognum,const rpcvers_t versnum,const rpcproc_t procnum,const xdrproc_t inproc,const char * in,const xdrproc_t outproc,char * out,const char * netclass)8761961e0fSrobinson rpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum,
8861961e0fSrobinson const rpcproc_t procnum, const xdrproc_t inproc, const char *in,
8961961e0fSrobinson const xdrproc_t outproc, char *out, const char *netclass)
907c478bd9Sstevel@tonic-gate {
917c478bd9Sstevel@tonic-gate struct rpc_call_private *rcp;
927c478bd9Sstevel@tonic-gate enum clnt_stat clnt_stat;
937c478bd9Sstevel@tonic-gate struct timeval timeout, tottimeout;
94cb620785Sraf static pthread_key_t rpc_call_key = PTHREAD_ONCE_KEY_NP;
957c478bd9Sstevel@tonic-gate char nettype_array[NETIDLEN];
967c478bd9Sstevel@tonic-gate char *nettype = &nettype_array[0];
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate if (netclass == NULL)
997c478bd9Sstevel@tonic-gate nettype = NULL;
1007c478bd9Sstevel@tonic-gate else {
1017c478bd9Sstevel@tonic-gate size_t len = strlen(netclass);
1027c478bd9Sstevel@tonic-gate if (len >= sizeof (nettype_array)) {
1037c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1047c478bd9Sstevel@tonic-gate return (rpc_createerr.cf_stat);
1057c478bd9Sstevel@tonic-gate }
10661961e0fSrobinson (void) strcpy(nettype, netclass);
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate
1097c478bd9Sstevel@tonic-gate rcp = thr_get_storage(&rpc_call_key, sizeof (*rcp), rpc_call_destroy);
1107c478bd9Sstevel@tonic-gate if (rcp == NULL) {
1117c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_SYSTEMERROR;
1127c478bd9Sstevel@tonic-gate rpc_createerr.cf_error.re_errno = errno;
1137c478bd9Sstevel@tonic-gate return (rpc_createerr.cf_stat);
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
116*344db6f4SToomas Soome if ((nettype == NULL) || (nettype[0] == '\0'))
1177c478bd9Sstevel@tonic-gate nettype = "netpath";
1187c478bd9Sstevel@tonic-gate if (!(rcp->valid &&
1197c478bd9Sstevel@tonic-gate rcp->pid == getpid() &&
1207c478bd9Sstevel@tonic-gate rcp->prognum == prognum &&
1217c478bd9Sstevel@tonic-gate rcp->versnum == versnum &&
1227c478bd9Sstevel@tonic-gate strcmp(rcp->host, host) == 0 &&
1237c478bd9Sstevel@tonic-gate strcmp(rcp->nettype, nettype) == 0)) {
1247c478bd9Sstevel@tonic-gate int fd;
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate rcp->valid = 0;
1277c478bd9Sstevel@tonic-gate if (rcp->client)
1287c478bd9Sstevel@tonic-gate CLNT_DESTROY(rcp->client);
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate * Using the first successful transport for that type
1317c478bd9Sstevel@tonic-gate */
1327c478bd9Sstevel@tonic-gate rcp->client = clnt_create(host, prognum, versnum, nettype);
1337c478bd9Sstevel@tonic-gate rcp->pid = getpid();
13461961e0fSrobinson if (rcp->client == NULL)
1357c478bd9Sstevel@tonic-gate return (rpc_createerr.cf_stat);
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate * Set time outs for connectionless case. Do it
1387c478bd9Sstevel@tonic-gate * unconditionally. Faster than doing a t_getinfo()
1397c478bd9Sstevel@tonic-gate * and then doing the right thing.
1407c478bd9Sstevel@tonic-gate */
1417c478bd9Sstevel@tonic-gate timeout.tv_usec = 0;
1427c478bd9Sstevel@tonic-gate timeout.tv_sec = 5;
1437c478bd9Sstevel@tonic-gate (void) CLNT_CONTROL(rcp->client,
1447c478bd9Sstevel@tonic-gate CLSET_RETRY_TIMEOUT, (char *)&timeout);
1457c478bd9Sstevel@tonic-gate if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)&fd))
146e8031f0aSraf (void) fcntl(fd, F_SETFD, 1); /* close on exec */
1477c478bd9Sstevel@tonic-gate rcp->prognum = prognum;
1487c478bd9Sstevel@tonic-gate rcp->versnum = versnum;
1497c478bd9Sstevel@tonic-gate if ((strlen(host) < (size_t)MAXHOSTNAMELEN) &&
1507c478bd9Sstevel@tonic-gate (strlen(nettype) < (size_t)NETIDLEN)) {
1517c478bd9Sstevel@tonic-gate (void) strcpy(rcp->host, host);
1527c478bd9Sstevel@tonic-gate (void) strcpy(rcp->nettype, nettype);
1537c478bd9Sstevel@tonic-gate rcp->valid = 1;
1547c478bd9Sstevel@tonic-gate } else {
1557c478bd9Sstevel@tonic-gate rcp->valid = 0;
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate } /* else reuse old client */
1587c478bd9Sstevel@tonic-gate tottimeout.tv_sec = 25;
1597c478bd9Sstevel@tonic-gate tottimeout.tv_usec = 0;
1607c478bd9Sstevel@tonic-gate clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *)in,
1617c478bd9Sstevel@tonic-gate outproc, out, tottimeout);
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate * if call failed, empty cache
1647c478bd9Sstevel@tonic-gate */
1657c478bd9Sstevel@tonic-gate if (clnt_stat != RPC_SUCCESS)
1667c478bd9Sstevel@tonic-gate rcp->valid = 0;
1677c478bd9Sstevel@tonic-gate return (clnt_stat);
1687c478bd9Sstevel@tonic-gate }
169