1 1.17 tron /* $NetBSD: pmap_prot2.c,v 1.17 2013/03/11 20:19:29 tron Exp $ */ 2 1.2 cgd 3 1.1 cgd /* 4 1.17 tron * Copyright (c) 2010, Oracle America, Inc. 5 1.17 tron * 6 1.17 tron * Redistribution and use in source and binary forms, with or without 7 1.17 tron * modification, are permitted provided that the following conditions are 8 1.17 tron * met: 9 1.17 tron * 10 1.17 tron * * Redistributions of source code must retain the above copyright 11 1.17 tron * notice, this list of conditions and the following disclaimer. 12 1.17 tron * * Redistributions in binary form must reproduce the above 13 1.17 tron * copyright notice, this list of conditions and the following 14 1.17 tron * disclaimer in the documentation and/or other materials 15 1.17 tron * provided with the distribution. 16 1.17 tron * * Neither the name of the "Oracle America, Inc." nor the names of its 17 1.17 tron * contributors may be used to endorse or promote products derived 18 1.17 tron * from this software without specific prior written permission. 19 1.17 tron * 20 1.17 tron * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 1.17 tron * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 1.17 tron * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 23 1.17 tron * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 24 1.17 tron * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 25 1.17 tron * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 1.17 tron * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 27 1.17 tron * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 28 1.17 tron * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 1.17 tron * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 1.17 tron * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 1.17 tron * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 1.1 cgd */ 33 1.1 cgd 34 1.3 christos #include <sys/cdefs.h> 35 1.1 cgd #if defined(LIBC_SCCS) && !defined(lint) 36 1.3 christos #if 0 37 1.3 christos static char *sccsid = "@(#)pmap_prot2.c 1.3 87/08/11 Copyr 1984 Sun Micro"; 38 1.3 christos static char *sccsid = "@(#)pmap_prot2.c 2.1 88/07/29 4.0 RPCSRC"; 39 1.3 christos #else 40 1.17 tron __RCSID("$NetBSD: pmap_prot2.c,v 1.17 2013/03/11 20:19:29 tron Exp $"); 41 1.3 christos #endif 42 1.1 cgd #endif 43 1.1 cgd 44 1.1 cgd /* 45 1.1 cgd * pmap_prot2.c 46 1.1 cgd * Protocol for the local binder service, or pmap. 47 1.1 cgd * 48 1.1 cgd * Copyright (C) 1984, Sun Microsystems, Inc. 49 1.1 cgd */ 50 1.1 cgd 51 1.4 jtc #include "namespace.h" 52 1.7 lukem 53 1.10 lukem #include <assert.h> 54 1.10 lukem 55 1.1 cgd #include <rpc/types.h> 56 1.1 cgd #include <rpc/xdr.h> 57 1.1 cgd #include <rpc/pmap_prot.h> 58 1.1 cgd 59 1.4 jtc #ifdef __weak_alias 60 1.12 mycroft __weak_alias(xdr_pmaplist,_xdr_pmaplist) 61 1.4 jtc #endif 62 1.1 cgd 63 1.1 cgd /* 64 1.1 cgd * What is going on with linked lists? (!) 65 1.1 cgd * First recall the link list declaration from pmap_prot.h: 66 1.1 cgd * 67 1.1 cgd * struct pmaplist { 68 1.1 cgd * struct pmap pml_map; 69 1.1 cgd * struct pmaplist *pml_map; 70 1.1 cgd * }; 71 1.1 cgd * 72 1.1 cgd * Compare that declaration with a corresponding xdr declaration that 73 1.1 cgd * is (a) pointer-less, and (b) recursive: 74 1.1 cgd * 75 1.1 cgd * typedef union switch (bool_t) { 76 1.1 cgd * 77 1.1 cgd * case TRUE: struct { 78 1.1 cgd * struct pmap; 79 1.1 cgd * pmaplist_t foo; 80 1.1 cgd * }; 81 1.1 cgd * 82 1.1 cgd * case FALSE: struct {}; 83 1.1 cgd * } pmaplist_t; 84 1.1 cgd * 85 1.1 cgd * Notice that the xdr declaration has no nxt pointer while 86 1.1 cgd * the C declaration has no bool_t variable. The bool_t can be 87 1.1 cgd * interpreted as ``more data follows me''; if FALSE then nothing 88 1.1 cgd * follows this bool_t; if TRUE then the bool_t is followed by 89 1.1 cgd * an actual struct pmap, and then (recursively) by the 90 1.1 cgd * xdr union, pamplist_t. 91 1.1 cgd * 92 1.1 cgd * This could be implemented via the xdr_union primitive, though this 93 1.1 cgd * would cause a one recursive call per element in the list. Rather than do 94 1.1 cgd * that we can ``unwind'' the recursion 95 1.1 cgd * into a while loop and do the union arms in-place. 96 1.1 cgd * 97 1.1 cgd * The head of the list is what the C programmer wishes to past around 98 1.1 cgd * the net, yet is the data that the pointer points to which is interesting; 99 1.1 cgd * this sounds like a job for xdr_reference! 100 1.1 cgd */ 101 1.1 cgd bool_t 102 1.16 matt xdr_pmaplist(XDR *xdrs, struct pmaplist **rp) 103 1.1 cgd { 104 1.1 cgd /* 105 1.1 cgd * more_elements is pre-computed in case the direction is 106 1.1 cgd * XDR_ENCODE or XDR_FREE. more_elements is overwritten by 107 1.1 cgd * xdr_bool when the direction is XDR_DECODE. 108 1.1 cgd */ 109 1.1 cgd bool_t more_elements; 110 1.10 lukem int freeing; 111 1.7 lukem struct pmaplist **next = NULL; /* pacify gcc */ 112 1.10 lukem 113 1.10 lukem _DIAGASSERT(xdrs != NULL); 114 1.10 lukem _DIAGASSERT(rp != NULL); 115 1.10 lukem 116 1.10 lukem freeing = (xdrs->x_op == XDR_FREE); 117 1.1 cgd 118 1.8 christos for (;;) { 119 1.1 cgd more_elements = (bool_t)(*rp != NULL); 120 1.1 cgd if (! xdr_bool(xdrs, &more_elements)) 121 1.1 cgd return (FALSE); 122 1.1 cgd if (! more_elements) 123 1.1 cgd return (TRUE); /* we are done */ 124 1.1 cgd /* 125 1.1 cgd * the unfortunate side effect of non-recursion is that in 126 1.1 cgd * the case of freeing we must remember the next object 127 1.1 cgd * before we free the current object ... 128 1.1 cgd */ 129 1.1 cgd if (freeing) 130 1.1 cgd next = &((*rp)->pml_next); 131 1.1 cgd if (! xdr_reference(xdrs, (caddr_t *)rp, 132 1.9 christos (u_int)sizeof(struct pmaplist), (xdrproc_t)xdr_pmap)) 133 1.1 cgd return (FALSE); 134 1.1 cgd rp = (freeing) ? next : &((*rp)->pml_next); 135 1.1 cgd } 136 1.13 fvdl } 137 1.13 fvdl 138 1.13 fvdl 139 1.13 fvdl /* 140 1.13 fvdl * xdr_pmaplist_ptr() is specified to take a PMAPLIST *, but is identical in 141 1.13 fvdl * functionality to xdr_pmaplist(). 142 1.13 fvdl */ 143 1.13 fvdl bool_t 144 1.16 matt xdr_pmaplist_ptr(XDR *xdrs, struct pmaplist *rp) 145 1.13 fvdl { 146 1.15 lukem 147 1.15 lukem _DIAGASSERT(xdrs != NULL); 148 1.15 lukem _DIAGASSERT(rp != NULL); 149 1.15 lukem 150 1.14 christos return xdr_pmaplist(xdrs, (struct pmaplist **)(void *)rp); 151 1.1 cgd } 152