Back to index

glibc  2.9
svc_run.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user.
00008  *
00009  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00010  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00011  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00012  *
00013  * Sun RPC is provided with no support and without any obligation on the
00014  * part of Sun Microsystems, Inc. to assist in its use, correction,
00015  * modification or enhancement.
00016  *
00017  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00018  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00019  * OR ANY PART THEREOF.
00020  *
00021  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00022  * or profits or other special, indirect and consequential damages, even if
00023  * Sun has been advised of the possibility of such damages.
00024  *
00025  * Sun Microsystems, Inc.
00026  * 2550 Garcia Avenue
00027  * Mountain View, California  94043
00028  */
00029 /*
00030  * This is the rpc server side idle loop
00031  * Wait for input, call server program.
00032  */
00033 
00034 #include <errno.h>
00035 #include <unistd.h>
00036 #include <libintl.h>
00037 #include <sys/poll.h>
00038 #include <rpc/rpc.h>
00039 
00040 /* This function can be used as a signal handler to terminate the
00041    server loop.  */
00042 void
00043 svc_exit (void)
00044 {
00045   free (svc_pollfd);
00046   svc_pollfd = NULL;
00047   svc_max_pollfd = 0;
00048 }
00049 
00050 void
00051 svc_run (void)
00052 {
00053   int i;
00054   struct pollfd *my_pollfd = NULL;
00055   int last_max_pollfd = 0;
00056 
00057   for (;;)
00058     {
00059       int max_pollfd = svc_max_pollfd;
00060       if (max_pollfd == 0 && svc_pollfd == NULL)
00061        break;
00062 
00063       if (last_max_pollfd != max_pollfd)
00064        {
00065          struct pollfd *new_pollfd
00066            = realloc (my_pollfd, sizeof (struct pollfd) * max_pollfd);
00067 
00068          if (new_pollfd == NULL)
00069            {
00070              perror (_("svc_run: - out of memory"));
00071              break;
00072            }
00073 
00074          my_pollfd = new_pollfd;
00075          last_max_pollfd = max_pollfd;
00076        }
00077 
00078       for (i = 0; i < max_pollfd; ++i)
00079        {
00080          my_pollfd[i].fd = svc_pollfd[i].fd;
00081          my_pollfd[i].events = svc_pollfd[i].events;
00082          my_pollfd[i].revents = 0;
00083        }
00084 
00085       switch (i = __poll (my_pollfd, max_pollfd, -1))
00086        {
00087        case -1:
00088          if (errno == EINTR)
00089            continue;
00090          perror (_("svc_run: - poll failed"));
00091          break;
00092        case 0:
00093          continue;
00094        default:
00095          INTUSE(svc_getreq_poll) (my_pollfd, i);
00096          continue;
00097        }
00098       break;
00099     }
00100 
00101   free (my_pollfd);
00102 }