Blame src/svc_run.c

Packit 00408a
/*
Packit 00408a
 * Copyright (c) 2009, Sun Microsystems, Inc.
Packit 00408a
 * All rights reserved.
Packit 00408a
 *
Packit 00408a
 * Redistribution and use in source and binary forms, with or without
Packit 00408a
 * modification, are permitted provided that the following conditions are met:
Packit 00408a
 * - Redistributions of source code must retain the above copyright notice,
Packit 00408a
 *   this list of conditions and the following disclaimer.
Packit 00408a
 * - Redistributions in binary form must reproduce the above copyright notice,
Packit 00408a
 *   this list of conditions and the following disclaimer in the documentation
Packit 00408a
 *   and/or other materials provided with the distribution.
Packit 00408a
 * - Neither the name of Sun Microsystems, Inc. nor the names of its
Packit 00408a
 *   contributors may be used to endorse or promote products derived
Packit 00408a
 *   from this software without specific prior written permission.
Packit 00408a
 *
Packit 00408a
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 00408a
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit 00408a
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 00408a
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
Packit 00408a
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit 00408a
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit 00408a
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit 00408a
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit 00408a
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit 00408a
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit 00408a
 * POSSIBILITY OF SUCH DAMAGE.
Packit 00408a
 */
Packit 00408a
Packit 00408a
/*
Packit 00408a
 * This is the rpc server side idle loop
Packit 00408a
 * Wait for input, call server program.
Packit 00408a
 */
Packit 00408a
#include <pthread.h>
Packit 00408a
#include <reentrant.h>
Packit 00408a
#include <err.h>
Packit 00408a
#include <errno.h>
Packit 00408a
#include <stdio.h>
Packit 00408a
#include <string.h>
Packit 00408a
#include <unistd.h>
Packit 00408a
#include <sys/poll.h>
Packit 00408a
Packit 00408a
Packit 00408a
#include <rpc/rpc.h>
Packit 00408a
#include "rpc_com.h"
Packit 00408a
#include <sys/select.h>
Packit 00408a
Packit 00408a
void
Packit 00408a
svc_run()
Packit 00408a
{
Packit 00408a
  int i;
Packit 00408a
  struct pollfd *my_pollfd = NULL;
Packit 00408a
  int last_max_pollfd = 0;
Packit 00408a
Packit 00408a
  for (;;) {
Packit 00408a
    int max_pollfd = svc_max_pollfd;
Packit 00408a
    if (max_pollfd == 0 && svc_pollfd == NULL)
Packit 00408a
        break;
Packit 00408a
Packit 00408a
      if (last_max_pollfd != max_pollfd)
Packit 00408a
        {
Packit 00408a
          struct pollfd *new_pollfd
Packit 00408a
            = realloc (my_pollfd, sizeof (struct pollfd) * max_pollfd);
Packit 00408a
Packit 00408a
          if (new_pollfd == NULL)
Packit 00408a
            {
Packit 00408a
              warn ("svc_run: - out of memory");
Packit 00408a
              break;
Packit 00408a
            }
Packit 00408a
Packit 00408a
          my_pollfd = new_pollfd;
Packit 00408a
          last_max_pollfd = max_pollfd;
Packit 00408a
        }
Packit 00408a
Packit 00408a
      for (i = 0; i < max_pollfd; ++i)
Packit 00408a
        {
Packit 00408a
          my_pollfd[i].fd = svc_pollfd[i].fd;
Packit 00408a
          my_pollfd[i].events = svc_pollfd[i].events;
Packit 00408a
          my_pollfd[i].revents = 0;
Packit 00408a
        }
Packit 00408a
Packit 00408a
      switch (i = poll (my_pollfd, max_pollfd, -1))
Packit 00408a
        {
Packit 00408a
        case -1:
Packit 00408a
          if (errno == EINTR)
Packit 00408a
            continue;
Packit 00408a
          warn ("svc_run: - poll failed");
Packit 00408a
          break;
Packit 00408a
        case 0:
Packit 00408a
          continue;
Packit 00408a
        default:
Packit 00408a
          svc_getreq_poll (my_pollfd, i);
Packit 00408a
          continue;
Packit 00408a
        }
Packit 00408a
      break;
Packit 00408a
    }
Packit 00408a
Packit 00408a
  free (my_pollfd);
Packit 00408a
}
Packit 00408a
Packit 00408a
/*
Packit 00408a
 *      This function causes svc_run() to exit by telling it that it has no
Packit 00408a
 *      more work to do.
Packit 00408a
 */
Packit 00408a
void
Packit 00408a
svc_exit()
Packit 00408a
{
Packit 00408a
	extern rwlock_t svc_fd_lock;
Packit 00408a
Packit 00408a
	rwlock_wrlock(&svc_fd_lock);
Packit 00408a
	free (svc_pollfd);
Packit 00408a
	svc_pollfd = NULL;
Packit 00408a
	svc_max_pollfd = 0;
Packit 00408a
	rwlock_unlock(&svc_fd_lock);
Packit 00408a
}