xref: /minix3/lib/libc/rpc/clnt_generic.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: clnt_generic.c,v 1.33 2014/05/28 14:45:19 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
484d9c625SLionel Sambuc  * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras  *
684d9c625SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
784d9c625SLionel Sambuc  * modification, are permitted provided that the following conditions are
884d9c625SLionel Sambuc  * met:
92fe8fb19SBen Gras  *
1084d9c625SLionel Sambuc  *     * Redistributions of source code must retain the above copyright
1184d9c625SLionel Sambuc  *       notice, this list of conditions and the following disclaimer.
1284d9c625SLionel Sambuc  *     * Redistributions in binary form must reproduce the above
1384d9c625SLionel Sambuc  *       copyright notice, this list of conditions and the following
1484d9c625SLionel Sambuc  *       disclaimer in the documentation and/or other materials
1584d9c625SLionel Sambuc  *       provided with the distribution.
1684d9c625SLionel Sambuc  *     * Neither the name of the "Oracle America, Inc." nor the names of its
1784d9c625SLionel Sambuc  *       contributors may be used to endorse or promote products derived
1884d9c625SLionel Sambuc  *       from this software without specific prior written permission.
192fe8fb19SBen Gras  *
2084d9c625SLionel Sambuc  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2184d9c625SLionel Sambuc  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2284d9c625SLionel Sambuc  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
2384d9c625SLionel Sambuc  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
2484d9c625SLionel Sambuc  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
2584d9c625SLionel Sambuc  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2684d9c625SLionel Sambuc  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2784d9c625SLionel Sambuc  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2884d9c625SLionel Sambuc  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
2984d9c625SLionel Sambuc  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3084d9c625SLionel Sambuc  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
3184d9c625SLionel Sambuc  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras  */
332fe8fb19SBen Gras /*
342fe8fb19SBen Gras  * Copyright (c) 1986-1991 by Sun Microsystems Inc.
352fe8fb19SBen Gras  */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras /* #ident	"@(#)clnt_generic.c	1.20	94/05/03 SMI" */
382fe8fb19SBen Gras 
392fe8fb19SBen Gras #include <sys/cdefs.h>
402fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
412fe8fb19SBen Gras #if 0
422fe8fb19SBen Gras static char sccsid[] = "@(#)clnt_generic.c 1.32 89/03/16 Copyr 1988 Sun Micro";
432fe8fb19SBen Gras #else
44*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: clnt_generic.c,v 1.33 2014/05/28 14:45:19 christos Exp $");
452fe8fb19SBen Gras #endif
462fe8fb19SBen Gras #endif
472fe8fb19SBen Gras 
482fe8fb19SBen Gras #include "namespace.h"
492fe8fb19SBen Gras #include "reentrant.h"
502fe8fb19SBen Gras #include <sys/types.h>
512fe8fb19SBen Gras #include <sys/socket.h>
522fe8fb19SBen Gras #include <netinet/in.h>
532fe8fb19SBen Gras #include <assert.h>
542fe8fb19SBen Gras #include <stdio.h>
552fe8fb19SBen Gras #include <errno.h>
562fe8fb19SBen Gras #include <rpc/rpc.h>
572fe8fb19SBen Gras #include <rpc/nettype.h>
582fe8fb19SBen Gras #include <string.h>
592fe8fb19SBen Gras #include <stdlib.h>
602fe8fb19SBen Gras #include <unistd.h>
6184d9c625SLionel Sambuc 
6284d9c625SLionel Sambuc #include "svc_fdset.h"
632fe8fb19SBen Gras #include "rpc_internal.h"
642fe8fb19SBen Gras 
652fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(clnt_create_vers,_clnt_create_vers)662fe8fb19SBen Gras __weak_alias(clnt_create_vers,_clnt_create_vers)
672fe8fb19SBen Gras __weak_alias(clnt_create,_clnt_create)
682fe8fb19SBen Gras __weak_alias(clnt_tp_create,_clnt_tp_create)
692fe8fb19SBen Gras __weak_alias(clnt_tli_create,_clnt_tli_create)
702fe8fb19SBen Gras #endif
712fe8fb19SBen Gras 
722fe8fb19SBen Gras /*
732fe8fb19SBen Gras  * Generic client creation with version checking the value of
742fe8fb19SBen Gras  * vers_out is set to the highest server supported value
752fe8fb19SBen Gras  * vers_low <= vers_out <= vers_high  AND an error results
762fe8fb19SBen Gras  * if this can not be done.
772fe8fb19SBen Gras  */
782fe8fb19SBen Gras CLIENT *
79f14fb602SLionel Sambuc clnt_create_vers(
80f14fb602SLionel Sambuc 	const char *	hostname,
81f14fb602SLionel Sambuc 	rpcprog_t	prog,
82f14fb602SLionel Sambuc 	rpcvers_t *	vers_out,
83f14fb602SLionel Sambuc 	rpcvers_t	vers_low,
84f14fb602SLionel Sambuc 	rpcvers_t	vers_high,
85f14fb602SLionel Sambuc 	const char *	nettype)
862fe8fb19SBen Gras {
872fe8fb19SBen Gras 	CLIENT *clnt;
882fe8fb19SBen Gras 	struct timeval to;
892fe8fb19SBen Gras 	enum clnt_stat rpc_stat;
902fe8fb19SBen Gras 	struct rpc_err rpcerr;
912fe8fb19SBen Gras 
922fe8fb19SBen Gras 	_DIAGASSERT(hostname != NULL);
932fe8fb19SBen Gras 	_DIAGASSERT(vers_out != NULL);
942fe8fb19SBen Gras 	/* XXX: nettype appears to support being NULL */
952fe8fb19SBen Gras 
962fe8fb19SBen Gras 	clnt = clnt_create(hostname, prog, vers_high, nettype);
972fe8fb19SBen Gras 	if (clnt == NULL) {
982fe8fb19SBen Gras 		return (NULL);
992fe8fb19SBen Gras 	}
1002fe8fb19SBen Gras 	to.tv_sec = 10;
1012fe8fb19SBen Gras 	to.tv_usec = 0;
1022fe8fb19SBen Gras 	rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void,
1032fe8fb19SBen Gras 	    NULL, (xdrproc_t) xdr_void, NULL, to);
1042fe8fb19SBen Gras 	if (rpc_stat == RPC_SUCCESS) {
1052fe8fb19SBen Gras 		*vers_out = vers_high;
1062fe8fb19SBen Gras 		return (clnt);
1072fe8fb19SBen Gras 	}
1082fe8fb19SBen Gras 	if (rpc_stat == RPC_PROGVERSMISMATCH) {
1092fe8fb19SBen Gras 		unsigned long minvers, maxvers;
1102fe8fb19SBen Gras 
1112fe8fb19SBen Gras 		clnt_geterr(clnt, &rpcerr);
1122fe8fb19SBen Gras 		minvers = rpcerr.re_vers.low;
1132fe8fb19SBen Gras 		maxvers = rpcerr.re_vers.high;
1142fe8fb19SBen Gras 		if (maxvers < vers_high)
1152fe8fb19SBen Gras 			vers_high = (rpcvers_t)maxvers;
1162fe8fb19SBen Gras 		if (minvers > vers_low)
1172fe8fb19SBen Gras 			vers_low = (rpcvers_t)minvers;
1182fe8fb19SBen Gras 		if (vers_low > vers_high) {
1192fe8fb19SBen Gras 			goto error;
1202fe8fb19SBen Gras 		}
1212fe8fb19SBen Gras 		CLNT_CONTROL(clnt, CLSET_VERS, (char *)(void *)&vers_high);
1222fe8fb19SBen Gras 		rpc_stat = clnt_call(clnt, NULLPROC, (xdrproc_t) xdr_void,
1232fe8fb19SBen Gras 		    NULL, (xdrproc_t) xdr_void, NULL, to);
1242fe8fb19SBen Gras 		if (rpc_stat == RPC_SUCCESS) {
1252fe8fb19SBen Gras 			*vers_out = vers_high;
1262fe8fb19SBen Gras 			return (clnt);
1272fe8fb19SBen Gras 		}
1282fe8fb19SBen Gras 	}
1292fe8fb19SBen Gras 	clnt_geterr(clnt, &rpcerr);
1302fe8fb19SBen Gras 
1312fe8fb19SBen Gras error:
1322fe8fb19SBen Gras 	rpc_createerr.cf_stat = rpc_stat;
1332fe8fb19SBen Gras 	rpc_createerr.cf_error = rpcerr;
1342fe8fb19SBen Gras 	clnt_destroy(clnt);
1352fe8fb19SBen Gras 	return (NULL);
1362fe8fb19SBen Gras }
1372fe8fb19SBen Gras 
1382fe8fb19SBen Gras /*
1392fe8fb19SBen Gras  * Top level client creation routine.
1402fe8fb19SBen Gras  * Generic client creation: takes (servers name, program-number, nettype) and
1412fe8fb19SBen Gras  * returns client handle. Default options are set, which the user can
1422fe8fb19SBen Gras  * change using the rpc equivalent of ioctl()'s.
1432fe8fb19SBen Gras  *
1442fe8fb19SBen Gras  * It tries for all the netids in that particular class of netid until
1452fe8fb19SBen Gras  * it succeeds.
1462fe8fb19SBen Gras  * XXX The error message in the case of failure will be the one
1472fe8fb19SBen Gras  * pertaining to the last create error.
1482fe8fb19SBen Gras  *
1492fe8fb19SBen Gras  * It calls clnt_tp_create();
1502fe8fb19SBen Gras  */
1512fe8fb19SBen Gras CLIENT *
clnt_create(const char * hostname,rpcprog_t prog,rpcvers_t vers,const char * nettype)152f14fb602SLionel Sambuc clnt_create(
153f14fb602SLionel Sambuc 	const char *	hostname,			/* server name */
154f14fb602SLionel Sambuc 	rpcprog_t	prog,				/* program number */
155f14fb602SLionel Sambuc 	rpcvers_t	vers,				/* version number */
156f14fb602SLionel Sambuc 	const char *	nettype)			/* net type */
1572fe8fb19SBen Gras {
1582fe8fb19SBen Gras 	struct netconfig *nconf;
1592fe8fb19SBen Gras 	CLIENT *clnt = NULL;
1602fe8fb19SBen Gras 	void *handle;
1612fe8fb19SBen Gras 	enum clnt_stat	save_cf_stat = RPC_SUCCESS;
1622fe8fb19SBen Gras 	struct rpc_err	save_cf_error;
1632fe8fb19SBen Gras 
1642fe8fb19SBen Gras 	_DIAGASSERT(hostname != NULL);
1652fe8fb19SBen Gras 	/* XXX: nettype appears to support being NULL */
1662fe8fb19SBen Gras 
1672fe8fb19SBen Gras 	if ((handle = __rpc_setconf(nettype)) == NULL) {
1682fe8fb19SBen Gras 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1692fe8fb19SBen Gras 		return (NULL);
1702fe8fb19SBen Gras 	}
1712fe8fb19SBen Gras 	rpc_createerr.cf_stat = RPC_SUCCESS;
1722fe8fb19SBen Gras 	while (clnt == NULL) {
1732fe8fb19SBen Gras 		if ((nconf = __rpc_getconf(handle)) == NULL) {
1742fe8fb19SBen Gras 			if (rpc_createerr.cf_stat == RPC_SUCCESS)
1752fe8fb19SBen Gras 				rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
1762fe8fb19SBen Gras 			break;
1772fe8fb19SBen Gras 		}
1782fe8fb19SBen Gras #ifdef CLNT_DEBUG
1792fe8fb19SBen Gras 		printf("trying netid %s\n", nconf->nc_netid);
1802fe8fb19SBen Gras #endif
1812fe8fb19SBen Gras 		clnt = clnt_tp_create(hostname, prog, vers, nconf);
1822fe8fb19SBen Gras 		if (clnt)
1832fe8fb19SBen Gras 			break;
1842fe8fb19SBen Gras 		else
1852fe8fb19SBen Gras 			/*
1862fe8fb19SBen Gras 			 *	Since we didn't get a name-to-address
1872fe8fb19SBen Gras 			 *	translation failure here, we remember
1882fe8fb19SBen Gras 			 *	this particular error.  The object of
1892fe8fb19SBen Gras 			 *	this is to enable us to return to the
1902fe8fb19SBen Gras 			 *	caller a more-specific error than the
1912fe8fb19SBen Gras 			 *	unhelpful ``Name to address translation
1922fe8fb19SBen Gras 			 *	failed'' which might well occur if we
1932fe8fb19SBen Gras 			 *	merely returned the last error (because
1942fe8fb19SBen Gras 			 *	the local loopbacks are typically the
1952fe8fb19SBen Gras 			 *	last ones in /etc/netconfig and the most
1962fe8fb19SBen Gras 			 *	likely to be unable to translate a host
1972fe8fb19SBen Gras 			 *	name).
1982fe8fb19SBen Gras 			 */
1992fe8fb19SBen Gras 			if (rpc_createerr.cf_stat != RPC_N2AXLATEFAILURE) {
2002fe8fb19SBen Gras 				save_cf_stat = rpc_createerr.cf_stat;
2012fe8fb19SBen Gras 				save_cf_error = rpc_createerr.cf_error;
2022fe8fb19SBen Gras 			}
2032fe8fb19SBen Gras 	}
2042fe8fb19SBen Gras 
2052fe8fb19SBen Gras 	/*
2062fe8fb19SBen Gras 	 *	Attempt to return an error more specific than ``Name to address
2072fe8fb19SBen Gras 	 *	translation failed''
2082fe8fb19SBen Gras 	 */
2092fe8fb19SBen Gras 	if ((rpc_createerr.cf_stat == RPC_N2AXLATEFAILURE) &&
2102fe8fb19SBen Gras 		(save_cf_stat != RPC_SUCCESS)) {
2112fe8fb19SBen Gras 		rpc_createerr.cf_stat = save_cf_stat;
2122fe8fb19SBen Gras 		rpc_createerr.cf_error = save_cf_error;
2132fe8fb19SBen Gras 	}
2142fe8fb19SBen Gras 	__rpc_endconf(handle);
2152fe8fb19SBen Gras 	return (clnt);
2162fe8fb19SBen Gras }
2172fe8fb19SBen Gras 
2182fe8fb19SBen Gras /*
2192fe8fb19SBen Gras  * Generic client creation: takes (servers name, program-number, netconf) and
2202fe8fb19SBen Gras  * returns client handle. Default options are set, which the user can
2212fe8fb19SBen Gras  * change using the rpc equivalent of ioctl()'s : clnt_control()
2222fe8fb19SBen Gras  * It finds out the server address from rpcbind and calls clnt_tli_create()
2232fe8fb19SBen Gras  */
2242fe8fb19SBen Gras CLIENT *
clnt_tp_create(const char * hostname,rpcprog_t prog,rpcvers_t vers,const struct netconfig * nconf)225f14fb602SLionel Sambuc clnt_tp_create(
226f14fb602SLionel Sambuc 	const char *		hostname,	/* server name */
227f14fb602SLionel Sambuc 	rpcprog_t		prog,		/* program number */
228f14fb602SLionel Sambuc 	rpcvers_t		vers,		/* version number */
229f14fb602SLionel Sambuc 	const struct netconfig *nconf)		/* net config struct */
2302fe8fb19SBen Gras {
2312fe8fb19SBen Gras 	struct netbuf *svcaddr;			/* servers address */
2322fe8fb19SBen Gras 	CLIENT *cl = NULL;			/* client handle */
2332fe8fb19SBen Gras 
2342fe8fb19SBen Gras 	_DIAGASSERT(hostname != NULL);
2352fe8fb19SBen Gras 	/* nconf is handled below */
2362fe8fb19SBen Gras 
2372fe8fb19SBen Gras 	if (nconf == NULL) {
2382fe8fb19SBen Gras 		rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
2392fe8fb19SBen Gras 		return (NULL);
2402fe8fb19SBen Gras 	}
2412fe8fb19SBen Gras 
2422fe8fb19SBen Gras 	/*
2432fe8fb19SBen Gras 	 * Get the address of the server
2442fe8fb19SBen Gras 	 */
2452fe8fb19SBen Gras 	if ((svcaddr = __rpcb_findaddr(prog, vers, nconf, hostname,
2462fe8fb19SBen Gras 		&cl)) == NULL) {
2472fe8fb19SBen Gras 		/* appropriate error number is set by rpcbind libraries */
2482fe8fb19SBen Gras 		return (NULL);
2492fe8fb19SBen Gras 	}
2502fe8fb19SBen Gras 	if (cl == NULL) {
2512fe8fb19SBen Gras 		cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
2522fe8fb19SBen Gras 					prog, vers, 0, 0);
2532fe8fb19SBen Gras 	} else {
2542fe8fb19SBen Gras 		/* Reuse the CLIENT handle and change the appropriate fields */
2552fe8fb19SBen Gras 		if (CLNT_CONTROL(cl, CLSET_SVC_ADDR, (void *)svcaddr) == TRUE) {
2562fe8fb19SBen Gras 			if (cl->cl_netid == NULL) {
2572fe8fb19SBen Gras 				cl->cl_netid = strdup(nconf->nc_netid);
2582fe8fb19SBen Gras 				if (cl->cl_netid == NULL)
2592fe8fb19SBen Gras 					goto out;
2602fe8fb19SBen Gras 			}
2612fe8fb19SBen Gras 			if (cl->cl_tp == NULL) {
2622fe8fb19SBen Gras 				cl->cl_tp = strdup(nconf->nc_device);
2632fe8fb19SBen Gras 				if (cl->cl_tp == NULL)
2642fe8fb19SBen Gras 					goto out;
2652fe8fb19SBen Gras 			}
2662fe8fb19SBen Gras 			(void) CLNT_CONTROL(cl, CLSET_PROG, (void *)&prog);
2672fe8fb19SBen Gras 			(void) CLNT_CONTROL(cl, CLSET_VERS, (void *)&vers);
2682fe8fb19SBen Gras 		} else {
2692fe8fb19SBen Gras 			CLNT_DESTROY(cl);
2702fe8fb19SBen Gras 			cl = clnt_tli_create(RPC_ANYFD, nconf, svcaddr,
2712fe8fb19SBen Gras 					prog, vers, 0, 0);
2722fe8fb19SBen Gras 		}
2732fe8fb19SBen Gras 	}
2742fe8fb19SBen Gras 	free(svcaddr->buf);
2752fe8fb19SBen Gras 	free(svcaddr);
2762fe8fb19SBen Gras 	return (cl);
2772fe8fb19SBen Gras out:
2782fe8fb19SBen Gras 	clnt_destroy(cl);
2792fe8fb19SBen Gras 	return NULL;
2802fe8fb19SBen Gras }
2812fe8fb19SBen Gras 
2822fe8fb19SBen Gras /*
2832fe8fb19SBen Gras  * Generic client creation:  returns client handle.
2842fe8fb19SBen Gras  * Default options are set, which the user can
2852fe8fb19SBen Gras  * change using the rpc equivalent of ioctl()'s : clnt_control().
2862fe8fb19SBen Gras  * If fd is RPC_ANYFD, it will be opened using nconf.
2872fe8fb19SBen Gras  * It will be bound if not so.
2882fe8fb19SBen Gras  * If sizes are 0; appropriate defaults will be chosen.
2892fe8fb19SBen Gras  */
2902fe8fb19SBen Gras CLIENT *
clnt_tli_create(int fd,const struct netconfig * nconf,const struct netbuf * svcaddr,rpcprog_t prog,rpcvers_t vers,u_int sendsz,u_int recvsz)291f14fb602SLionel Sambuc clnt_tli_create(
292f14fb602SLionel Sambuc 	int fd,				/* fd */
293f14fb602SLionel Sambuc 	const struct netconfig *nconf,	/* netconfig structure */
294f14fb602SLionel Sambuc 	const struct netbuf *svcaddr,	/* servers address */
295f14fb602SLionel Sambuc 	rpcprog_t prog,			/* program number */
296f14fb602SLionel Sambuc 	rpcvers_t vers,			/* version number */
297f14fb602SLionel Sambuc 	u_int sendsz,			/* send size */
298f14fb602SLionel Sambuc 	u_int recvsz)			/* recv size */
2992fe8fb19SBen Gras {
3002fe8fb19SBen Gras 	CLIENT *cl;			/* client handle */
3012fe8fb19SBen Gras 	bool_t madefd = FALSE;		/* whether fd opened here */
3022fe8fb19SBen Gras 	long servtype;
3032fe8fb19SBen Gras 	struct __rpc_sockinfo si;
3042fe8fb19SBen Gras 
3052fe8fb19SBen Gras 	/* nconf is handled below */
3062fe8fb19SBen Gras 	_DIAGASSERT(svcaddr != NULL);
3072fe8fb19SBen Gras 
3082fe8fb19SBen Gras 	if (fd == RPC_ANYFD) {
3092fe8fb19SBen Gras 		if (nconf == NULL) {
3102fe8fb19SBen Gras 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
3112fe8fb19SBen Gras 			return (NULL);
3122fe8fb19SBen Gras 		}
3132fe8fb19SBen Gras 
3142fe8fb19SBen Gras 		fd = __rpc_nconf2fd(nconf);
3152fe8fb19SBen Gras 
3162fe8fb19SBen Gras 		if (fd == -1)
3172fe8fb19SBen Gras 			goto err;
3182fe8fb19SBen Gras 
3192fe8fb19SBen Gras 		madefd = TRUE;
3202fe8fb19SBen Gras 		servtype = nconf->nc_semantics;
3212fe8fb19SBen Gras 		if (!__rpc_fd2sockinfo(fd, &si))
3222fe8fb19SBen Gras 			goto err;
3232fe8fb19SBen Gras 
324*0a6a1f1dSLionel Sambuc 		(void)bindresvport(fd, NULL);
3252fe8fb19SBen Gras 	} else {
3262fe8fb19SBen Gras 		if (!__rpc_fd2sockinfo(fd, &si))
3272fe8fb19SBen Gras 			goto err;
3282fe8fb19SBen Gras 		servtype = __rpc_socktype2seman(si.si_socktype);
3292fe8fb19SBen Gras 		if (servtype == -1) {
3302fe8fb19SBen Gras 			rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
3312fe8fb19SBen Gras 			return NULL;
3322fe8fb19SBen Gras 		}
3332fe8fb19SBen Gras 
3342fe8fb19SBen Gras 	}
3352fe8fb19SBen Gras 
3362fe8fb19SBen Gras 	if (si.si_af != ((struct sockaddr *)svcaddr->buf)->sa_family) {
3372fe8fb19SBen Gras 		rpc_createerr.cf_stat = RPC_UNKNOWNHOST;	/* XXX */
3382fe8fb19SBen Gras 		goto err1;
3392fe8fb19SBen Gras 	}
3402fe8fb19SBen Gras 
3412fe8fb19SBen Gras 	switch (servtype) {
3422fe8fb19SBen Gras 	case NC_TPI_COTS_ORD:
3432fe8fb19SBen Gras 		cl = clnt_vc_create(fd, svcaddr, prog, vers, sendsz, recvsz);
3442fe8fb19SBen Gras 		if (!nconf || !cl)
3452fe8fb19SBen Gras 			break;
346*0a6a1f1dSLionel Sambuc 		(void)__rpc_setnodelay(fd, &si);
3472fe8fb19SBen Gras 		break;
3482fe8fb19SBen Gras 	case NC_TPI_CLTS:
3492fe8fb19SBen Gras 		cl = clnt_dg_create(fd, svcaddr, prog, vers, sendsz, recvsz);
3502fe8fb19SBen Gras 		break;
3512fe8fb19SBen Gras 	default:
3522fe8fb19SBen Gras 		goto err;
3532fe8fb19SBen Gras 	}
3542fe8fb19SBen Gras 
3552fe8fb19SBen Gras 	if (cl == NULL)
3562fe8fb19SBen Gras 		goto err1; /* borrow errors from clnt_dg/vc creates */
3572fe8fb19SBen Gras 	if (nconf) {
3582fe8fb19SBen Gras 		cl->cl_netid = strdup(nconf->nc_netid);
3592fe8fb19SBen Gras 		if (cl->cl_netid == NULL)
3602fe8fb19SBen Gras 			goto err0;
3612fe8fb19SBen Gras 		cl->cl_tp = strdup(nconf->nc_device);
3622fe8fb19SBen Gras 		if (cl->cl_tp == NULL)
3632fe8fb19SBen Gras 			goto err0;
3642fe8fb19SBen Gras 	} else {
3652fe8fb19SBen Gras 		cl->cl_netid = __UNCONST("");
3662fe8fb19SBen Gras 		cl->cl_tp = __UNCONST("");
3672fe8fb19SBen Gras 	}
3682fe8fb19SBen Gras 	if (madefd) {
3692fe8fb19SBen Gras 		(void) CLNT_CONTROL(cl, CLSET_FD_CLOSE, NULL);
3702fe8fb19SBen Gras /*		(void) CLNT_CONTROL(cl, CLSET_POP_TIMOD, NULL);  */
3712fe8fb19SBen Gras 	};
3722fe8fb19SBen Gras 
3732fe8fb19SBen Gras 	return (cl);
3742fe8fb19SBen Gras 
3752fe8fb19SBen Gras err0:
3762fe8fb19SBen Gras 	clnt_destroy(cl);
3772fe8fb19SBen Gras err:
3782fe8fb19SBen Gras 	rpc_createerr.cf_stat = RPC_SYSTEMERROR;
3792fe8fb19SBen Gras 	rpc_createerr.cf_error.re_errno = errno;
3802fe8fb19SBen Gras err1:	if (madefd)
3812fe8fb19SBen Gras 		(void) close(fd);
3822fe8fb19SBen Gras 	return (NULL);
3832fe8fb19SBen Gras }
38484d9c625SLionel Sambuc 
38584d9c625SLionel Sambuc /*
38684d9c625SLionel Sambuc  * Don't block thse so interactive programs don't get stuck in lalaland.
38784d9c625SLionel Sambuc  * (easier to do this than making connect(2) non-blocking..)
38884d9c625SLionel Sambuc  */
38984d9c625SLionel Sambuc int
__clnt_sigfillset(sigset_t * ss)39084d9c625SLionel Sambuc __clnt_sigfillset(sigset_t *ss) {
39184d9c625SLionel Sambuc 	static const int usersig[] = {
39284d9c625SLionel Sambuc 	    SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGTSTP
39384d9c625SLionel Sambuc 	};
39484d9c625SLionel Sambuc 	if (sigfillset(ss) == -1)
39584d9c625SLionel Sambuc 		return -1;
39684d9c625SLionel Sambuc 	for (size_t i = 0; i < __arraycount(usersig); i++)
39784d9c625SLionel Sambuc 		if (sigdelset(ss, usersig[i]) == -1)
39884d9c625SLionel Sambuc 			return -1;
39984d9c625SLionel Sambuc 	return 0;
40084d9c625SLionel Sambuc }
401