1*84d9c625SLionel Sambuc /* $NetBSD: svc_run.c,v 1.22 2013/03/11 20:19:29 tron Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
4*84d9c625SLionel Sambuc * Copyright (c) 2010, Oracle America, Inc.
52fe8fb19SBen Gras *
6*84d9c625SLionel Sambuc * Redistribution and use in source and binary forms, with or without
7*84d9c625SLionel Sambuc * modification, are permitted provided that the following conditions are
8*84d9c625SLionel Sambuc * met:
92fe8fb19SBen Gras *
10*84d9c625SLionel Sambuc * * Redistributions of source code must retain the above copyright
11*84d9c625SLionel Sambuc * notice, this list of conditions and the following disclaimer.
12*84d9c625SLionel Sambuc * * Redistributions in binary form must reproduce the above
13*84d9c625SLionel Sambuc * copyright notice, this list of conditions and the following
14*84d9c625SLionel Sambuc * disclaimer in the documentation and/or other materials
15*84d9c625SLionel Sambuc * provided with the distribution.
16*84d9c625SLionel Sambuc * * Neither the name of the "Oracle America, Inc." nor the names of its
17*84d9c625SLionel Sambuc * contributors may be used to endorse or promote products derived
18*84d9c625SLionel Sambuc * from this software without specific prior written permission.
192fe8fb19SBen Gras *
20*84d9c625SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*84d9c625SLionel Sambuc * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*84d9c625SLionel Sambuc * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*84d9c625SLionel Sambuc * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24*84d9c625SLionel Sambuc * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25*84d9c625SLionel Sambuc * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26*84d9c625SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27*84d9c625SLionel Sambuc * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28*84d9c625SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29*84d9c625SLionel Sambuc * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30*84d9c625SLionel Sambuc * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*84d9c625SLionel Sambuc * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras */
332fe8fb19SBen Gras
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
362fe8fb19SBen Gras #if 0
372fe8fb19SBen Gras static char *sccsid = "@(#)svc_run.c 1.1 87/10/13 Copyr 1984 Sun Micro";
382fe8fb19SBen Gras static char *sccsid = "@(#)svc_run.c 2.1 88/07/29 4.0 RPCSRC";
392fe8fb19SBen Gras #else
40*84d9c625SLionel Sambuc __RCSID("$NetBSD: svc_run.c,v 1.22 2013/03/11 20:19:29 tron Exp $");
412fe8fb19SBen Gras #endif
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras * This is the rpc server side idle loop
462fe8fb19SBen Gras * Wait for input, call server program.
472fe8fb19SBen Gras */
482fe8fb19SBen Gras #include "namespace.h"
492fe8fb19SBen Gras #include "reentrant.h"
502fe8fb19SBen Gras #include <err.h>
512fe8fb19SBen Gras #include <errno.h>
522fe8fb19SBen Gras #include <stdio.h>
532fe8fb19SBen Gras #include <string.h>
542fe8fb19SBen Gras #include <unistd.h>
552fe8fb19SBen Gras
562fe8fb19SBen Gras #include <rpc/rpc.h>
572fe8fb19SBen Gras
58*84d9c625SLionel Sambuc #include "svc_fdset.h"
592fe8fb19SBen Gras #include "rpc_internal.h"
602fe8fb19SBen Gras
612fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(svc_run,_svc_run)622fe8fb19SBen Gras __weak_alias(svc_run,_svc_run)
632fe8fb19SBen Gras __weak_alias(svc_exit,_svc_exit)
642fe8fb19SBen Gras #endif
652fe8fb19SBen Gras
662fe8fb19SBen Gras void
67f14fb602SLionel Sambuc svc_run(void)
682fe8fb19SBen Gras {
692fe8fb19SBen Gras fd_set readfds, cleanfds;
702fe8fb19SBen Gras struct timeval timeout;
71*84d9c625SLionel Sambuc int maxfd;
72*84d9c625SLionel Sambuc #ifndef RUMP_RPC
73*84d9c625SLionel Sambuc int probs = 0;
74*84d9c625SLionel Sambuc #endif
752fe8fb19SBen Gras #ifdef _REENTRANT
762fe8fb19SBen Gras extern rwlock_t svc_fd_lock;
772fe8fb19SBen Gras #endif
782fe8fb19SBen Gras
792fe8fb19SBen Gras timeout.tv_sec = 30;
802fe8fb19SBen Gras timeout.tv_usec = 0;
812fe8fb19SBen Gras
822fe8fb19SBen Gras for (;;) {
832fe8fb19SBen Gras rwlock_rdlock(&svc_fd_lock);
84*84d9c625SLionel Sambuc readfds = *get_fdset();
85*84d9c625SLionel Sambuc cleanfds = *get_fdset();
86*84d9c625SLionel Sambuc maxfd = *get_fdsetmax();
872fe8fb19SBen Gras rwlock_unlock(&svc_fd_lock);
88*84d9c625SLionel Sambuc switch (select(maxfd + 1, &readfds, NULL, NULL, &timeout)) {
892fe8fb19SBen Gras case -1:
90*84d9c625SLionel Sambuc #ifndef RUMP_RPC
91*84d9c625SLionel Sambuc if ((errno == EINTR || errno == EBADF) && probs < 100) {
92*84d9c625SLionel Sambuc probs++;
93*84d9c625SLionel Sambuc continue;
94*84d9c625SLionel Sambuc }
95*84d9c625SLionel Sambuc #endif
962fe8fb19SBen Gras if (errno == EINTR) {
972fe8fb19SBen Gras continue;
982fe8fb19SBen Gras }
99*84d9c625SLionel Sambuc warn("%s: select failed", __func__);
1002fe8fb19SBen Gras return;
1012fe8fb19SBen Gras case 0:
1022fe8fb19SBen Gras __svc_clean_idle(&cleanfds, 30, FALSE);
1032fe8fb19SBen Gras continue;
1042fe8fb19SBen Gras default:
1052fe8fb19SBen Gras svc_getreqset(&readfds);
106*84d9c625SLionel Sambuc #ifndef RUMP_RPC
107*84d9c625SLionel Sambuc probs = 0;
108*84d9c625SLionel Sambuc #endif
1092fe8fb19SBen Gras }
1102fe8fb19SBen Gras }
1112fe8fb19SBen Gras }
1122fe8fb19SBen Gras
1132fe8fb19SBen Gras /*
1142fe8fb19SBen Gras * This function causes svc_run() to exit by telling it that it has no
1152fe8fb19SBen Gras * more work to do.
1162fe8fb19SBen Gras */
1172fe8fb19SBen Gras void
svc_exit(void)118f14fb602SLionel Sambuc svc_exit(void)
1192fe8fb19SBen Gras {
1202fe8fb19SBen Gras #ifdef _REENTRANT
1212fe8fb19SBen Gras extern rwlock_t svc_fd_lock;
1222fe8fb19SBen Gras #endif
1232fe8fb19SBen Gras
1242fe8fb19SBen Gras rwlock_wrlock(&svc_fd_lock);
125*84d9c625SLionel Sambuc FD_ZERO(get_fdset());
1262fe8fb19SBen Gras rwlock_unlock(&svc_fd_lock);
1272fe8fb19SBen Gras }
128