svc_run.c revision 1.29.2.1 1 /* $NetBSD: svc_run.c,v 1.29.2.1 2025/08/02 05:54:40 perseant 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.2.1 2025/08/02 05:54:40 perseant 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
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 rwlock_unlock(&svc_fd_lock);
89 warn("%s: can't get maxfd", __func__);
90 goto out;
91 }
92
93 if (fdsize != svc_fdset_getsize(0)) {
94 fdsize = svc_fdset_getsize(0);
95 free(readfds);
96 readfds = svc_fdset_copy(svc_fdset_get());
97 if (readfds == NULL) {
98 rwlock_unlock(&svc_fd_lock);
99 warn("%s: can't copy fdset", __func__);
100 goto out;
101 }
102 } else
103 memcpy(readfds, svc_fdset_get(), __NFD_BYTES(fdsize));
104
105 rwlock_unlock(&svc_fd_lock);
106
107 switch (select(*maxfd + 1, readfds, NULL, NULL, &timeout)) {
108 case -1:
109 #ifndef RUMP_RPC
110 if ((errno == EINTR || errno == EBADF) && probs < 100) {
111 probs++;
112 continue;
113 }
114 #endif
115 if (errno == EINTR) {
116 continue;
117 }
118 warn("%s: select failed", __func__);
119 goto out;
120 case 0:
121 __svc_clean_idle(NULL, 30, FALSE);
122 continue;
123 default:
124 svc_getreqset2(readfds, fdsize);
125 #ifndef RUMP_RPC
126 probs = 0;
127 #endif
128 }
129 }
130 out:
131 free(readfds);
132 }
133
134 static void
135 svc_run_poll(void)
136 {
137 struct pollfd *pfd;
138 int *maxfd, fdsize, i;
139 #ifndef RUMP_RPC
140 int probs = 0;
141 #endif
142
143 fdsize = 0;
144 pfd = NULL;
145
146 for (;;) {
147 rwlock_rdlock(&svc_fd_lock);
148
149 maxfd = svc_pollfd_getmax();
150 if (maxfd == NULL) {
151 rwlock_unlock(&svc_fd_lock);
152 warn("can't get maxfd");
153 goto out;
154 }
155
156 if (pfd == NULL || fdsize != svc_pollfd_getsize(0)) {
157 fdsize = svc_fdset_getsize(0);
158 free(pfd);
159 pfd = svc_pollfd_copy(svc_pollfd_get());
160 if (pfd == NULL) {
161 rwlock_unlock(&svc_fd_lock);
162 warn("can't get pollfd");
163 goto out;
164 }
165 } else
166 memcpy(pfd, svc_pollfd_get(), *maxfd * sizeof(*pfd));
167
168 rwlock_unlock(&svc_fd_lock);
169
170 switch ((i = poll(pfd, (nfds_t)*maxfd, 30 * 1000))) {
171 case -1:
172 #ifndef RUMP_RPC
173 if ((errno == EINTR || errno == EBADF) && probs < 100) {
174 probs++;
175 continue;
176 }
177 #endif
178 if (errno == EINTR) {
179 continue;
180 }
181 warn("%s: poll failed", __func__);
182 goto out;
183 case 0:
184 __svc_clean_idle(NULL, 30, FALSE);
185 continue;
186 default:
187 svc_getreq_poll(pfd, i);
188 #ifndef RUMP_RPC
189 probs = 0;
190 #endif
191 }
192 }
193 out:
194 free(pfd);
195 }
196
197 void
198 svc_run(void)
199 {
200 (__svc_flags & SVC_FDSET_POLL) ? svc_run_poll() : svc_run_select();
201 }
202
203 /*
204 * This function causes svc_run() to exit by telling it that it has no
205 * more work to do.
206 */
207 void
208 svc_exit(void)
209 {
210
211 rwlock_wrlock(&svc_fd_lock);
212 svc_fdset_zero();
213 rwlock_unlock(&svc_fd_lock);
214 }
215