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 5*cb620785Sraf * Common Development and Distribution License (the "License"). 6*cb620785Sraf * 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 /* 23*cb620785Sraf * 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 #pragma ident "%Z%%M% %I% %E% SMI" 367c478bd9Sstevel@tonic-gate 377c478bd9Sstevel@tonic-gate /* 387c478bd9Sstevel@tonic-gate * Simplified front end to client rpc. 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate #include "mt.h" 427c478bd9Sstevel@tonic-gate #include "rpc_mt.h" 437c478bd9Sstevel@tonic-gate #include <stdio.h> 447c478bd9Sstevel@tonic-gate #include <errno.h> 457c478bd9Sstevel@tonic-gate #include <rpc/rpc.h> 467c478bd9Sstevel@tonic-gate #include <string.h> 477c478bd9Sstevel@tonic-gate #include <sys/param.h> 487c478bd9Sstevel@tonic-gate #include <stdlib.h> 497c478bd9Sstevel@tonic-gate #include <unistd.h> 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #ifndef MAXHOSTNAMELEN 527c478bd9Sstevel@tonic-gate #define MAXHOSTNAMELEN 64 537c478bd9Sstevel@tonic-gate #endif 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #ifndef NETIDLEN 567c478bd9Sstevel@tonic-gate #define NETIDLEN 32 577c478bd9Sstevel@tonic-gate #endif 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate struct rpc_call_private { 607c478bd9Sstevel@tonic-gate int valid; /* Is this entry valid ? */ 617c478bd9Sstevel@tonic-gate CLIENT *client; /* Client handle */ 627c478bd9Sstevel@tonic-gate pid_t pid; /* process-id at moment of creation */ 637c478bd9Sstevel@tonic-gate rpcprog_t prognum; /* Program */ 647c478bd9Sstevel@tonic-gate rpcvers_t versnum; /* version */ 657c478bd9Sstevel@tonic-gate char host[MAXHOSTNAMELEN]; /* Servers host */ 667c478bd9Sstevel@tonic-gate char nettype[NETIDLEN]; /* Network type */ 677c478bd9Sstevel@tonic-gate }; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate static void 707c478bd9Sstevel@tonic-gate rpc_call_destroy(void *vp) 717c478bd9Sstevel@tonic-gate { 727c478bd9Sstevel@tonic-gate struct rpc_call_private *rcp = (struct rpc_call_private *)vp; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate if (rcp) { 757c478bd9Sstevel@tonic-gate if (rcp->client) 767c478bd9Sstevel@tonic-gate CLNT_DESTROY(rcp->client); 777c478bd9Sstevel@tonic-gate free(rcp); 787c478bd9Sstevel@tonic-gate } 797c478bd9Sstevel@tonic-gate } 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate /* 827c478bd9Sstevel@tonic-gate * This is the simplified interface to the client rpc layer. 837c478bd9Sstevel@tonic-gate * The client handle is not destroyed here and is reused for 847c478bd9Sstevel@tonic-gate * the future calls to same prog, vers, host and nettype combination. 857c478bd9Sstevel@tonic-gate * 867c478bd9Sstevel@tonic-gate * The total time available is 25 seconds. 877c478bd9Sstevel@tonic-gate */ 887c478bd9Sstevel@tonic-gate enum clnt_stat 8961961e0fSrobinson rpc_call(const char *host, const rpcprog_t prognum, const rpcvers_t versnum, 9061961e0fSrobinson const rpcproc_t procnum, const xdrproc_t inproc, const char *in, 9161961e0fSrobinson const xdrproc_t outproc, char *out, const char *netclass) 927c478bd9Sstevel@tonic-gate { 937c478bd9Sstevel@tonic-gate struct rpc_call_private *rcp; 947c478bd9Sstevel@tonic-gate enum clnt_stat clnt_stat; 957c478bd9Sstevel@tonic-gate struct timeval timeout, tottimeout; 96*cb620785Sraf static pthread_key_t rpc_call_key = PTHREAD_ONCE_KEY_NP; 977c478bd9Sstevel@tonic-gate char nettype_array[NETIDLEN]; 987c478bd9Sstevel@tonic-gate char *nettype = &nettype_array[0]; 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate if (netclass == NULL) 1017c478bd9Sstevel@tonic-gate nettype = NULL; 1027c478bd9Sstevel@tonic-gate else { 1037c478bd9Sstevel@tonic-gate size_t len = strlen(netclass); 1047c478bd9Sstevel@tonic-gate if (len >= sizeof (nettype_array)) { 1057c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; 1067c478bd9Sstevel@tonic-gate return (rpc_createerr.cf_stat); 1077c478bd9Sstevel@tonic-gate } 10861961e0fSrobinson (void) strcpy(nettype, netclass); 1097c478bd9Sstevel@tonic-gate } 1107c478bd9Sstevel@tonic-gate 1117c478bd9Sstevel@tonic-gate rcp = thr_get_storage(&rpc_call_key, sizeof (*rcp), rpc_call_destroy); 1127c478bd9Sstevel@tonic-gate if (rcp == NULL) { 1137c478bd9Sstevel@tonic-gate rpc_createerr.cf_stat = RPC_SYSTEMERROR; 1147c478bd9Sstevel@tonic-gate rpc_createerr.cf_error.re_errno = errno; 1157c478bd9Sstevel@tonic-gate return (rpc_createerr.cf_stat); 1167c478bd9Sstevel@tonic-gate } 1177c478bd9Sstevel@tonic-gate 1187c478bd9Sstevel@tonic-gate if ((nettype == NULL) || (nettype[0] == NULL)) 1197c478bd9Sstevel@tonic-gate nettype = "netpath"; 1207c478bd9Sstevel@tonic-gate if (!(rcp->valid && 1217c478bd9Sstevel@tonic-gate rcp->pid == getpid() && 1227c478bd9Sstevel@tonic-gate rcp->prognum == prognum && 1237c478bd9Sstevel@tonic-gate rcp->versnum == versnum && 1247c478bd9Sstevel@tonic-gate strcmp(rcp->host, host) == 0 && 1257c478bd9Sstevel@tonic-gate strcmp(rcp->nettype, nettype) == 0)) { 1267c478bd9Sstevel@tonic-gate int fd; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate rcp->valid = 0; 1297c478bd9Sstevel@tonic-gate if (rcp->client) 1307c478bd9Sstevel@tonic-gate CLNT_DESTROY(rcp->client); 1317c478bd9Sstevel@tonic-gate /* 1327c478bd9Sstevel@tonic-gate * Using the first successful transport for that type 1337c478bd9Sstevel@tonic-gate */ 1347c478bd9Sstevel@tonic-gate rcp->client = clnt_create(host, prognum, versnum, nettype); 1357c478bd9Sstevel@tonic-gate rcp->pid = getpid(); 13661961e0fSrobinson if (rcp->client == NULL) 1377c478bd9Sstevel@tonic-gate return (rpc_createerr.cf_stat); 1387c478bd9Sstevel@tonic-gate /* 1397c478bd9Sstevel@tonic-gate * Set time outs for connectionless case. Do it 1407c478bd9Sstevel@tonic-gate * unconditionally. Faster than doing a t_getinfo() 1417c478bd9Sstevel@tonic-gate * and then doing the right thing. 1427c478bd9Sstevel@tonic-gate */ 1437c478bd9Sstevel@tonic-gate timeout.tv_usec = 0; 1447c478bd9Sstevel@tonic-gate timeout.tv_sec = 5; 1457c478bd9Sstevel@tonic-gate (void) CLNT_CONTROL(rcp->client, 1467c478bd9Sstevel@tonic-gate CLSET_RETRY_TIMEOUT, (char *)&timeout); 1477c478bd9Sstevel@tonic-gate if (CLNT_CONTROL(rcp->client, CLGET_FD, (char *)&fd)) 148e8031f0aSraf (void) fcntl(fd, F_SETFD, 1); /* close on exec */ 1497c478bd9Sstevel@tonic-gate rcp->prognum = prognum; 1507c478bd9Sstevel@tonic-gate rcp->versnum = versnum; 1517c478bd9Sstevel@tonic-gate if ((strlen(host) < (size_t)MAXHOSTNAMELEN) && 1527c478bd9Sstevel@tonic-gate (strlen(nettype) < (size_t)NETIDLEN)) { 1537c478bd9Sstevel@tonic-gate (void) strcpy(rcp->host, host); 1547c478bd9Sstevel@tonic-gate (void) strcpy(rcp->nettype, nettype); 1557c478bd9Sstevel@tonic-gate rcp->valid = 1; 1567c478bd9Sstevel@tonic-gate } else { 1577c478bd9Sstevel@tonic-gate rcp->valid = 0; 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate } /* else reuse old client */ 1607c478bd9Sstevel@tonic-gate tottimeout.tv_sec = 25; 1617c478bd9Sstevel@tonic-gate tottimeout.tv_usec = 0; 1627c478bd9Sstevel@tonic-gate clnt_stat = CLNT_CALL(rcp->client, procnum, inproc, (char *)in, 1637c478bd9Sstevel@tonic-gate outproc, out, tottimeout); 1647c478bd9Sstevel@tonic-gate /* 1657c478bd9Sstevel@tonic-gate * if call failed, empty cache 1667c478bd9Sstevel@tonic-gate */ 1677c478bd9Sstevel@tonic-gate if (clnt_stat != RPC_SUCCESS) 1687c478bd9Sstevel@tonic-gate rcp->valid = 0; 1697c478bd9Sstevel@tonic-gate return (clnt_stat); 1707c478bd9Sstevel@tonic-gate } 171