1 /* $NetBSD: svc_run.c,v 1.29 2024/01/23 17:24:38 christos Exp $ */
2
3 /*
4 * Copyright (c) 2010, Oracle America, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of the "Oracle America, Inc." nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #if defined(LIBC_SCCS) && !defined(lint)
36 #if 0
37 static char *sccsid = "@(#)svc_run.c 1.1 87/10/13 Copyr 1984 Sun Micro";
38 static char *sccsid = "@(#)svc_run.c 2.1 88/07/29 4.0 RPCSRC";
39 #else
40 __RCSID("$NetBSD: svc_run.c,v 1.29 2024/01/23 17:24:38 christos Exp $");
41 #endif
42 #endif
43
44 /*
45 * This is the rpc server side idle loop
46 * Wait for input, call server program.
47 */
48 #include "namespace.h"
49 #include "reentrant.h"
50 #include <err.h>
51 #include <errno.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <unistd.h>
56 #include <poll.h>
57
58 #include <rpc/rpc.h>
59
60 #include "svc_fdset.h"
61 #include "rpc_internal.h"
62
63 #ifdef __weak_alias
__weak_alias(svc_run,_svc_run)64 __weak_alias(svc_run,_svc_run)
65 __weak_alias(svc_exit,_svc_exit)
66 #endif
67
68 static void
69 svc_run_select(void)
70 {
71 fd_set *readfds;
72 struct timeval timeout;
73 int *maxfd, fdsize;
74 #ifndef RUMP_RPC
75 int probs = 0;
76 #endif
77
78 readfds = NULL;
79 fdsize = 0;
80 timeout.tv_sec = 30;
81 timeout.tv_usec = 0;
82
83 for (;;) {
84 rwlock_rdlock(&svc_fd_lock);
85
86 maxfd = svc_fdset_getmax();
87 if (maxfd == NULL) {
88 warn("%s: can't get maxfd", __func__);
89 goto out;
90 }
91
92 if (fdsize != svc_fdset_getsize(0)) {
93 fdsize = svc_fdset_getsize(0);
94 free(readfds);
95 readfds = svc_fdset_copy(svc_fdset_get());
96 if (readfds == NULL) {
97 warn("%s: can't copy fdset", __func__);
98 goto out;
99 }
100 } else
101 memcpy(readfds, svc_fdset_get(), __NFD_BYTES(fdsize));
102
103 rwlock_unlock(&svc_fd_lock);
104
105 switch (select(*maxfd + 1, readfds, NULL, NULL, &timeout)) {
106 case -1:
107 #ifndef RUMP_RPC
108 if ((errno == EINTR || errno == EBADF) && probs < 100) {
109 probs++;
110 continue;
111 }
112 #endif
113 if (errno == EINTR) {
114 continue;
115 }
116 warn("%s: select failed", __func__);
117 goto out;
118 case 0:
119 __svc_clean_idle(NULL, 30, FALSE);
120 continue;
121 default:
122 svc_getreqset2(readfds, fdsize);
123 #ifndef RUMP_RPC
124 probs = 0;
125 #endif
126 }
127 }
128 out:
129 free(readfds);
130 }
131
132 static void
svc_run_poll(void)133 svc_run_poll(void)
134 {
135 struct pollfd *pfd;
136 int *maxfd, fdsize, i;
137 #ifndef RUMP_RPC
138 int probs = 0;
139 #endif
140
141 fdsize = 0;
142 pfd = NULL;
143
144 for (;;) {
145 rwlock_rdlock(&svc_fd_lock);
146
147 maxfd = svc_pollfd_getmax();
148 if (maxfd == NULL) {
149 warn("can't get maxfd");
150 goto out;
151 }
152
153 if (pfd == NULL || fdsize != svc_pollfd_getsize(0)) {
154 fdsize = svc_fdset_getsize(0);
155 free(pfd);
156 pfd = svc_pollfd_copy(svc_pollfd_get());
157 if (pfd == NULL) {
158 warn("can't get pollfd");
159 goto out;
160 }
161 } else
162 memcpy(pfd, svc_pollfd_get(), *maxfd * sizeof(*pfd));
163
164 rwlock_unlock(&svc_fd_lock);
165
166 switch ((i = poll(pfd, (nfds_t)*maxfd, 30 * 1000))) {
167 case -1:
168 #ifndef RUMP_RPC
169 if ((errno == EINTR || errno == EBADF) && probs < 100) {
170 probs++;
171 continue;
172 }
173 #endif
174 if (errno == EINTR) {
175 continue;
176 }
177 warn("%s: poll failed", __func__);
178 goto out;
179 case 0:
180 __svc_clean_idle(NULL, 30, FALSE);
181 continue;
182 default:
183 svc_getreq_poll(pfd, i);
184 #ifndef RUMP_RPC
185 probs = 0;
186 #endif
187 }
188 }
189 out:
190 free(pfd);
191 }
192
193 void
svc_run(void)194 svc_run(void)
195 {
196 (__svc_flags & SVC_FDSET_POLL) ? svc_run_poll() : svc_run_select();
197 }
198
199 /*
200 * This function causes svc_run() to exit by telling it that it has no
201 * more work to do.
202 */
203 void
svc_exit(void)204 svc_exit(void)
205 {
206
207 rwlock_wrlock(&svc_fd_lock);
208 svc_fdset_zero();
209 rwlock_unlock(&svc_fd_lock);
210 }
211