Blame sunrpc/pmap_prot2.c

Packit Service 82fcde
/*
Packit Service 82fcde
 * pmap_prot2.c
Packit Service 82fcde
 * Protocol for the local binder service, or pmap.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Copyright (c) 2010, Oracle America, Inc.
Packit Service 82fcde
 *
Packit Service 82fcde
 * Redistribution and use in source and binary forms, with or without
Packit Service 82fcde
 * modification, are permitted provided that the following conditions are
Packit Service 82fcde
 * met:
Packit Service 82fcde
 *
Packit Service 82fcde
 *     * Redistributions of source code must retain the above copyright
Packit Service 82fcde
 *       notice, this list of conditions and the following disclaimer.
Packit Service 82fcde
 *     * Redistributions in binary form must reproduce the above
Packit Service 82fcde
 *       copyright notice, this list of conditions and the following
Packit Service 82fcde
 *       disclaimer in the documentation and/or other materials
Packit Service 82fcde
 *       provided with the distribution.
Packit Service 82fcde
 *     * Neither the name of the "Oracle America, Inc." nor the names of its
Packit Service 82fcde
 *       contributors may be used to endorse or promote products derived
Packit Service 82fcde
 *       from this software without specific prior written permission.
Packit Service 82fcde
 *
Packit Service 82fcde
 *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Packit Service 82fcde
 *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Packit Service 82fcde
 *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
Packit Service 82fcde
 *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
Packit Service 82fcde
 *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
Packit Service 82fcde
 *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
Packit Service 82fcde
 *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
Packit Service 82fcde
 *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit Service 82fcde
 *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit Service 82fcde
 *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
Packit Service 82fcde
 *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Packit Service 82fcde
 *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 82fcde
 */
Packit Service 82fcde
Packit Service 82fcde
#include <rpc/types.h>
Packit Service 82fcde
#include <rpc/xdr.h>
Packit Service 82fcde
#include <rpc/pmap_prot.h>
Packit Service 82fcde
#include <shlib-compat.h>
Packit Service 82fcde
Packit Service 82fcde
Packit Service 82fcde
/*
Packit Service 82fcde
 * What is going on with linked lists? (!)
Packit Service 82fcde
 * First recall the link list declaration from pmap_prot.h:
Packit Service 82fcde
 *
Packit Service 82fcde
 * struct pmaplist {
Packit Service 82fcde
 *      struct pmap pml_map;
Packit Service 82fcde
 *      struct pmaplist *pml_map;
Packit Service 82fcde
 * };
Packit Service 82fcde
 *
Packit Service 82fcde
 * Compare that declaration with a corresponding xdr declaration that
Packit Service 82fcde
 * is (a) pointer-less, and (b) recursive:
Packit Service 82fcde
 *
Packit Service 82fcde
 * typedef union switch (bool_t) {
Packit Service 82fcde
 *
Packit Service 82fcde
 *      case TRUE: struct {
Packit Service 82fcde
 *              struct pmap;
Packit Service 82fcde
 *              pmaplist_t foo;
Packit Service 82fcde
 *      };
Packit Service 82fcde
 *
Packit Service 82fcde
 *      case FALSE: struct {};
Packit Service 82fcde
 * } pmaplist_t;
Packit Service 82fcde
 *
Packit Service 82fcde
 * Notice that the xdr declaration has no nxt pointer while
Packit Service 82fcde
 * the C declaration has no bool_t variable.  The bool_t can be
Packit Service 82fcde
 * interpreted as ``more data follows me''; if FALSE then nothing
Packit Service 82fcde
 * follows this bool_t; if TRUE then the bool_t is followed by
Packit Service 82fcde
 * an actual struct pmap, and then (recursively) by the
Packit Service 82fcde
 * xdr union, pamplist_t.
Packit Service 82fcde
 *
Packit Service 82fcde
 * This could be implemented via the xdr_union primitive, though this
Packit Service 82fcde
 * would cause a one recursive call per element in the list.  Rather than do
Packit Service 82fcde
 * that we can ``unwind'' the recursion
Packit Service 82fcde
 * into a while loop and do the union arms in-place.
Packit Service 82fcde
 *
Packit Service 82fcde
 * The head of the list is what the C programmer wishes to past around
Packit Service 82fcde
 * the net, yet is the data that the pointer points to which is interesting;
Packit Service 82fcde
 * this sounds like a job for xdr_reference!
Packit Service 82fcde
 */
Packit Service 82fcde
bool_t
Packit Service 82fcde
xdr_pmaplist (XDR *xdrs, struct pmaplist **rp)
Packit Service 82fcde
{
Packit Service 82fcde
  /*
Packit Service 82fcde
   * more_elements is pre-computed in case the direction is
Packit Service 82fcde
   * XDR_ENCODE or XDR_FREE.  more_elements is overwritten by
Packit Service 82fcde
   * xdr_bool when the direction is XDR_DECODE.
Packit Service 82fcde
   */
Packit Service 82fcde
  bool_t more_elements;
Packit Service 82fcde
  int freeing = (xdrs->x_op == XDR_FREE);
Packit Service 82fcde
  struct pmaplist *next = NULL;
Packit Service 82fcde
Packit Service 82fcde
  while (TRUE)
Packit Service 82fcde
    {
Packit Service 82fcde
      more_elements = (bool_t) (*rp != NULL);
Packit Service 82fcde
      if (!xdr_bool (xdrs, &more_elements))
Packit Service 82fcde
	return FALSE;
Packit Service 82fcde
      if (!more_elements)
Packit Service 82fcde
	return TRUE;		/* we are done */
Packit Service 82fcde
      /*
Packit Service 82fcde
       * the unfortunate side effect of non-recursion is that in
Packit Service 82fcde
       * the case of freeing we must remember the next object
Packit Service 82fcde
       * before we free the current object ...
Packit Service 82fcde
       */
Packit Service 82fcde
      if (freeing)
Packit Service 82fcde
	next = (*rp)->pml_next;
Packit Service 82fcde
      if (!xdr_reference (xdrs, (caddr_t *) rp,
Packit Service 82fcde
			  (u_int) sizeof (struct pmaplist),
Packit Service 82fcde
			  (xdrproc_t) xdr_pmap))
Packit Service 82fcde
	  return FALSE;
Packit Service 82fcde
      rp = freeing ? &next : &((*rp)->pml_next);
Packit Service 82fcde
    }
Packit Service 82fcde
}
Packit Service 82fcde
libc_hidden_nolink_sunrpc (xdr_pmaplist, GLIBC_2_0)