Blame sunrpc/svc_run.c

Packit Service 82fcde
/*
Packit Service 82fcde
 * Copyright (c) 2010, Oracle America, Inc.
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
 * This is the rpc server side idle loop
Packit Service 82fcde
 * Wait for input, call server program.
Packit Service 82fcde
 */
Packit Service 82fcde
Packit Service 82fcde
#include <errno.h>
Packit Service 82fcde
#include <unistd.h>
Packit Service 82fcde
#include <libintl.h>
Packit Service 82fcde
#include <sys/poll.h>
Packit Service 82fcde
#include <rpc/rpc.h>
Packit Service 82fcde
#include <shlib-compat.h>
Packit Service 82fcde
Packit Service 82fcde
/* This function can be used as a signal handler to terminate the
Packit Service 82fcde
   server loop.  */
Packit Service 82fcde
void
Packit Service 82fcde
svc_exit (void)
Packit Service 82fcde
{
Packit Service 82fcde
  free (svc_pollfd);
Packit Service 82fcde
  svc_pollfd = NULL;
Packit Service 82fcde
  svc_max_pollfd = 0;
Packit Service 82fcde
}
Packit Service 82fcde
libc_hidden_nolink_sunrpc (svc_exit, GLIBC_2_0)
Packit Service 82fcde
Packit Service 82fcde
void
Packit Service 82fcde
svc_run (void)
Packit Service 82fcde
{
Packit Service 82fcde
  int i;
Packit Service 82fcde
  struct pollfd *my_pollfd = NULL;
Packit Service 82fcde
  int last_max_pollfd = 0;
Packit Service 82fcde
Packit Service 82fcde
  for (;;)
Packit Service 82fcde
    {
Packit Service 82fcde
      int max_pollfd = svc_max_pollfd;
Packit Service 82fcde
      if (max_pollfd == 0 && svc_pollfd == NULL)
Packit Service 82fcde
	break;
Packit Service 82fcde
Packit Service 82fcde
      if (last_max_pollfd != max_pollfd)
Packit Service 82fcde
	{
Packit Service 82fcde
	  struct pollfd *new_pollfd
Packit Service 82fcde
	    = realloc (my_pollfd, sizeof (struct pollfd) * max_pollfd);
Packit Service 82fcde
Packit Service 82fcde
	  if (new_pollfd == NULL)
Packit Service 82fcde
	    {
Packit Service 82fcde
	      perror (_("svc_run: - out of memory"));
Packit Service 82fcde
	      break;
Packit Service 82fcde
	    }
Packit Service 82fcde
Packit Service 82fcde
	  my_pollfd = new_pollfd;
Packit Service 82fcde
	  last_max_pollfd = max_pollfd;
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
      for (i = 0; i < max_pollfd; ++i)
Packit Service 82fcde
	{
Packit Service 82fcde
	  my_pollfd[i].fd = svc_pollfd[i].fd;
Packit Service 82fcde
	  my_pollfd[i].events = svc_pollfd[i].events;
Packit Service 82fcde
	  my_pollfd[i].revents = 0;
Packit Service 82fcde
	}
Packit Service 82fcde
Packit Service 82fcde
      switch (i = __poll (my_pollfd, max_pollfd, -1))
Packit Service 82fcde
	{
Packit Service 82fcde
	case -1:
Packit Service 82fcde
	  if (errno == EINTR)
Packit Service 82fcde
	    continue;
Packit Service 82fcde
	  perror (_("svc_run: - poll failed"));
Packit Service 82fcde
	  break;
Packit Service 82fcde
	case 0:
Packit Service 82fcde
	  continue;
Packit Service 82fcde
	default:
Packit Service 82fcde
	  svc_getreq_poll (my_pollfd, i);
Packit Service 82fcde
	  continue;
Packit Service 82fcde
	}
Packit Service 82fcde
      break;
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  free (my_pollfd);
Packit Service 82fcde
}
Packit Service 82fcde
#ifdef EXPORT_RPC_SYMBOLS
Packit Service 82fcde
libc_hidden_def (svc_run)
Packit Service 82fcde
#else
Packit Service 82fcde
libc_hidden_nolink_sunrpc (svc_run, GLIBC_2_0)
Packit Service 82fcde
#endif