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