1 /*
2  * Copyright (c) 2010, Oracle America, Inc.
3  * Redistribution and use in source and binary forms, with or without
4  * modification, are permitted provided that the following conditions are
5  * met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above
10  *       copyright notice, this list of conditions and the following
11  *       disclaimer in the documentation and/or other materials
12  *       provided with the distribution.
13  *     * Neither the name of the "Oracle America, Inc." nor the names of its
14  *       contributors may be used to endorse or promote products derived
15  *       from this software without specific prior written permission.
16  *
17  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  *
30  * This is the rpc server side idle loop
31  * Wait for input, call server program.
32  */
33 
34 #include <errno.h>
35 #include <unistd.h>
36 #include <libintl.h>
37 #include <sys/poll.h>
38 #include <rpc/rpc.h>
39 #include <shlib-compat.h>
40 
41 /* This function can be used as a signal handler to terminate the
42    server loop.  */
43 void
svc_exit(void)44 svc_exit (void)
45 {
46   free (svc_pollfd);
47   svc_pollfd = NULL;
48   svc_max_pollfd = 0;
49 }
libc_hidden_nolink_sunrpc(svc_exit,GLIBC_2_0)50 libc_hidden_nolink_sunrpc (svc_exit, GLIBC_2_0)
51 
52 void
53 svc_run (void)
54 {
55   int i;
56   struct pollfd *my_pollfd = NULL;
57   int last_max_pollfd = 0;
58 
59   for (;;)
60     {
61       int max_pollfd = svc_max_pollfd;
62       if (max_pollfd == 0 && svc_pollfd == NULL)
63 	break;
64 
65       if (last_max_pollfd != max_pollfd)
66 	{
67 	  struct pollfd *new_pollfd
68 	    = realloc (my_pollfd, sizeof (struct pollfd) * max_pollfd);
69 
70 	  if (new_pollfd == NULL)
71 	    {
72 	      perror (_("svc_run: - out of memory"));
73 	      break;
74 	    }
75 
76 	  my_pollfd = new_pollfd;
77 	  last_max_pollfd = max_pollfd;
78 	}
79 
80       for (i = 0; i < max_pollfd; ++i)
81 	{
82 	  my_pollfd[i].fd = svc_pollfd[i].fd;
83 	  my_pollfd[i].events = svc_pollfd[i].events;
84 	  my_pollfd[i].revents = 0;
85 	}
86 
87       switch (i = __poll (my_pollfd, max_pollfd, -1))
88 	{
89 	case -1:
90 	  if (errno == EINTR)
91 	    continue;
92 	  perror (_("svc_run: - poll failed"));
93 	  break;
94 	case 0:
95 	  continue;
96 	default:
97 	  svc_getreq_poll (my_pollfd, i);
98 	  continue;
99 	}
100       break;
101     }
102 
103   free (my_pollfd);
104 }
105 #ifdef EXPORT_RPC_SYMBOLS
106 libc_hidden_def (svc_run)
107 #else
108 libc_hidden_nolink_sunrpc (svc_run, GLIBC_2_0)
109 #endif
110