hjl / source-git / glibc

Forked from source-git/glibc 3 years ago
Clone

Blame sunrpc/pmap_prot2.c

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