xref: /freebsd/lib/libc/rpc/pmap_prot2.c (revision 2e322d379631c3bd6107e295f3e5d814c7845fc7)
18360efbdSAlfred Perlstein /*	$NetBSD: pmap_prot2.c,v 1.14 2000/07/06 03:10:34 christos Exp $	*/
28360efbdSAlfred Perlstein 
3*2e322d37SHiroki Sato /*-
4*2e322d37SHiroki Sato  * Copyright (c) 2009, Sun Microsystems, Inc.
5*2e322d37SHiroki Sato  * All rights reserved.
699064799SGarrett Wollman  *
7*2e322d37SHiroki Sato  * Redistribution and use in source and binary forms, with or without
8*2e322d37SHiroki Sato  * modification, are permitted provided that the following conditions are met:
9*2e322d37SHiroki Sato  * - Redistributions of source code must retain the above copyright notice,
10*2e322d37SHiroki Sato  *   this list of conditions and the following disclaimer.
11*2e322d37SHiroki Sato  * - Redistributions in binary form must reproduce the above copyright notice,
12*2e322d37SHiroki Sato  *   this list of conditions and the following disclaimer in the documentation
13*2e322d37SHiroki Sato  *   and/or other materials provided with the distribution.
14*2e322d37SHiroki Sato  * - Neither the name of Sun Microsystems, Inc. nor the names of its
15*2e322d37SHiroki Sato  *   contributors may be used to endorse or promote products derived
16*2e322d37SHiroki Sato  *   from this software without specific prior written permission.
1799064799SGarrett Wollman  *
18*2e322d37SHiroki Sato  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19*2e322d37SHiroki Sato  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*2e322d37SHiroki Sato  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*2e322d37SHiroki Sato  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22*2e322d37SHiroki Sato  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23*2e322d37SHiroki Sato  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24*2e322d37SHiroki Sato  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25*2e322d37SHiroki Sato  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26*2e322d37SHiroki Sato  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27*2e322d37SHiroki Sato  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28*2e322d37SHiroki Sato  * POSSIBILITY OF SUCH DAMAGE.
2999064799SGarrett Wollman  */
3099064799SGarrett Wollman 
3199064799SGarrett Wollman #if defined(LIBC_SCCS) && !defined(lint)
32a986ef57SDavid E. O'Brien static char *sccsid2 = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro";
338360efbdSAlfred Perlstein static char *sccsid = "@(#)pmap_prot2.c	2.1 88/07/29 4.0 RPCSRC";
3499064799SGarrett Wollman #endif
35d3d20c82SDavid E. O'Brien #include <sys/cdefs.h>
36d3d20c82SDavid E. O'Brien __FBSDID("$FreeBSD$");
3799064799SGarrett Wollman 
3899064799SGarrett Wollman /*
3999064799SGarrett Wollman  * pmap_prot2.c
4099064799SGarrett Wollman  * Protocol for the local binder service, or pmap.
4199064799SGarrett Wollman  *
4299064799SGarrett Wollman  * Copyright (C) 1984, Sun Microsystems, Inc.
4399064799SGarrett Wollman  */
4499064799SGarrett Wollman 
458360efbdSAlfred Perlstein #include "namespace.h"
468360efbdSAlfred Perlstein #include <assert.h>
478360efbdSAlfred Perlstein 
4899064799SGarrett Wollman #include <rpc/types.h>
4999064799SGarrett Wollman #include <rpc/xdr.h>
5099064799SGarrett Wollman #include <rpc/pmap_prot.h>
518360efbdSAlfred Perlstein #include "un-namespace.h"
5299064799SGarrett Wollman 
5399064799SGarrett Wollman 
5499064799SGarrett Wollman /*
5599064799SGarrett Wollman  * What is going on with linked lists? (!)
5699064799SGarrett Wollman  * First recall the link list declaration from pmap_prot.h:
5799064799SGarrett Wollman  *
5899064799SGarrett Wollman  * struct pmaplist {
5999064799SGarrett Wollman  *	struct pmap pml_map;
6099064799SGarrett Wollman  *	struct pmaplist *pml_map;
6199064799SGarrett Wollman  * };
6299064799SGarrett Wollman  *
6399064799SGarrett Wollman  * Compare that declaration with a corresponding xdr declaration that
6499064799SGarrett Wollman  * is (a) pointer-less, and (b) recursive:
6599064799SGarrett Wollman  *
6699064799SGarrett Wollman  * typedef union switch (bool_t) {
6799064799SGarrett Wollman  *
6899064799SGarrett Wollman  *	case TRUE: struct {
6999064799SGarrett Wollman  *		struct pmap;
7099064799SGarrett Wollman  * 		pmaplist_t foo;
7199064799SGarrett Wollman  *	};
7299064799SGarrett Wollman  *
7399064799SGarrett Wollman  *	case FALSE: struct {};
7499064799SGarrett Wollman  * } pmaplist_t;
7599064799SGarrett Wollman  *
7699064799SGarrett Wollman  * Notice that the xdr declaration has no nxt pointer while
7799064799SGarrett Wollman  * the C declaration has no bool_t variable.  The bool_t can be
7899064799SGarrett Wollman  * interpreted as ``more data follows me''; if FALSE then nothing
7999064799SGarrett Wollman  * follows this bool_t; if TRUE then the bool_t is followed by
8099064799SGarrett Wollman  * an actual struct pmap, and then (recursively) by the
8199064799SGarrett Wollman  * xdr union, pamplist_t.
8299064799SGarrett Wollman  *
8399064799SGarrett Wollman  * This could be implemented via the xdr_union primitive, though this
8499064799SGarrett Wollman  * would cause a one recursive call per element in the list.  Rather than do
8599064799SGarrett Wollman  * that we can ``unwind'' the recursion
8699064799SGarrett Wollman  * into a while loop and do the union arms in-place.
8799064799SGarrett Wollman  *
8899064799SGarrett Wollman  * The head of the list is what the C programmer wishes to past around
8999064799SGarrett Wollman  * the net, yet is the data that the pointer points to which is interesting;
9099064799SGarrett Wollman  * this sounds like a job for xdr_reference!
9199064799SGarrett Wollman  */
9299064799SGarrett Wollman bool_t
9399064799SGarrett Wollman xdr_pmaplist(xdrs, rp)
948360efbdSAlfred Perlstein 	XDR *xdrs;
958360efbdSAlfred Perlstein 	struct pmaplist **rp;
9699064799SGarrett Wollman {
9799064799SGarrett Wollman 	/*
9899064799SGarrett Wollman 	 * more_elements is pre-computed in case the direction is
9999064799SGarrett Wollman 	 * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
10099064799SGarrett Wollman 	 * xdr_bool when the direction is XDR_DECODE.
10199064799SGarrett Wollman 	 */
10299064799SGarrett Wollman 	bool_t more_elements;
1038360efbdSAlfred Perlstein 	int freeing;
1048360efbdSAlfred Perlstein 	struct pmaplist **next	= NULL; /* pacify gcc */
10599064799SGarrett Wollman 
1068360efbdSAlfred Perlstein 	assert(xdrs != NULL);
1078360efbdSAlfred Perlstein 	assert(rp != NULL);
1088360efbdSAlfred Perlstein 
1098360efbdSAlfred Perlstein 	freeing = (xdrs->x_op == XDR_FREE);
1108360efbdSAlfred Perlstein 
1118360efbdSAlfred Perlstein 	for (;;) {
11299064799SGarrett Wollman 		more_elements = (bool_t)(*rp != NULL);
11399064799SGarrett Wollman 		if (! xdr_bool(xdrs, &more_elements))
11499064799SGarrett Wollman 			return (FALSE);
11599064799SGarrett Wollman 		if (! more_elements)
11699064799SGarrett Wollman 			return (TRUE);  /* we are done */
11799064799SGarrett Wollman 		/*
11899064799SGarrett Wollman 		 * the unfortunate side effect of non-recursion is that in
11999064799SGarrett Wollman 		 * the case of freeing we must remember the next object
12099064799SGarrett Wollman 		 * before we free the current object ...
12199064799SGarrett Wollman 		 */
12299064799SGarrett Wollman 		if (freeing)
12399064799SGarrett Wollman 			next = &((*rp)->pml_next);
12499064799SGarrett Wollman 		if (! xdr_reference(xdrs, (caddr_t *)rp,
1258360efbdSAlfred Perlstein 		    (u_int)sizeof(struct pmaplist), (xdrproc_t)xdr_pmap))
12699064799SGarrett Wollman 			return (FALSE);
12799064799SGarrett Wollman 		rp = (freeing) ? next : &((*rp)->pml_next);
12899064799SGarrett Wollman 	}
12999064799SGarrett Wollman }
1308360efbdSAlfred Perlstein 
1318360efbdSAlfred Perlstein 
1328360efbdSAlfred Perlstein /*
1338360efbdSAlfred Perlstein  * xdr_pmaplist_ptr() is specified to take a PMAPLIST *, but is identical in
1348360efbdSAlfred Perlstein  * functionality to xdr_pmaplist().
1358360efbdSAlfred Perlstein  */
1368360efbdSAlfred Perlstein bool_t
1378360efbdSAlfred Perlstein xdr_pmaplist_ptr(xdrs, rp)
1388360efbdSAlfred Perlstein 	XDR *xdrs;
1398360efbdSAlfred Perlstein 	struct pmaplist *rp;
1408360efbdSAlfred Perlstein {
1418360efbdSAlfred Perlstein 	return xdr_pmaplist(xdrs, (struct pmaplist **)(void *)rp);
1428360efbdSAlfred Perlstein }
143