174462Salfred/*	$NetBSD: svc_run.c,v 1.17 2000/07/06 03:10:35 christos Exp $	*/
274462Salfred
3258578Shrs/*-
4258578Shrs * Copyright (c) 2009, Sun Microsystems, Inc.
5258578Shrs * All rights reserved.
68870Srgrimes *
7258578Shrs * Redistribution and use in source and binary forms, with or without
8258578Shrs * modification, are permitted provided that the following conditions are met:
9258578Shrs * - Redistributions of source code must retain the above copyright notice,
10258578Shrs *   this list of conditions and the following disclaimer.
11258578Shrs * - Redistributions in binary form must reproduce the above copyright notice,
12258578Shrs *   this list of conditions and the following disclaimer in the documentation
13258578Shrs *   and/or other materials provided with the distribution.
14258578Shrs * - Neither the name of Sun Microsystems, Inc. nor the names of its
15258578Shrs *   contributors may be used to endorse or promote products derived
16258578Shrs *   from this software without specific prior written permission.
17258578Shrs *
18258578Shrs * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19258578Shrs * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20258578Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21258578Shrs * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22258578Shrs * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23258578Shrs * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24258578Shrs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25258578Shrs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26258578Shrs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27258578Shrs * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28258578Shrs * POSSIBILITY OF SUCH DAMAGE.
291901Swollman */
301901Swollman
311901Swollman#if defined(LIBC_SCCS) && !defined(lint)
32136581Sobrienstatic char *sccsid2 = "from: @(#)svc_run.c 1.1 87/10/13 Copyr 1984 Sun Micro";
3392990Sobrienstatic char *sccsid = "from: @(#)svc_run.c	2.1 88/07/29 4.0 RPCSRC";
341901Swollman#endif
3592990Sobrien#include <sys/cdefs.h>
3692990Sobrien__FBSDID("$FreeBSD$");
371901Swollman
381901Swollman/*
391901Swollman * This is the rpc server side idle loop
401901Swollman * Wait for input, call server program.
411901Swollman */
4275094Siedowse#include "namespace.h"
4374462Salfred#include "reentrant.h"
4474462Salfred#include <err.h>
4574462Salfred#include <errno.h>
461901Swollman#include <rpc/rpc.h>
4716270Sjraynard#include <stdio.h>
4874462Salfred#include <string.h>
4916270Sjraynard#include <unistd.h>
5071579Sdeischen#include "un-namespace.h"
511901Swollman
5274462Salfred#include <rpc/rpc.h>
53111618Snectar#include "rpc_com.h"
54156090Sdeischen#include "mt_misc.h"
5516270Sjraynard
561901Swollmanvoid
57288113Srodrigcsvc_run(void)
581901Swollman{
59109359Smbr	fd_set readfds, cleanfds;
60109359Smbr	struct timeval timeout;
611901Swollman
62241007Spfg	timeout.tv_sec = 30;
63241007Spfg	timeout.tv_usec = 0;
64241007Spfg
651901Swollman	for (;;) {
6674462Salfred		rwlock_rdlock(&svc_fd_lock);
6774462Salfred		readfds = svc_fdset;
68109359Smbr		cleanfds = svc_fdset;
6974462Salfred		rwlock_unlock(&svc_fd_lock);
70109904Smbr		switch (_select(svc_maxfd+1, &readfds, NULL, NULL, &timeout)) {
711901Swollman		case -1:
7274462Salfred			FD_ZERO(&readfds);
731901Swollman			if (errno == EINTR) {
741901Swollman				continue;
751901Swollman			}
7682496Sbde			_warn("svc_run: - select failed");
771901Swollman			return;
781901Swollman		case 0:
79109359Smbr			__svc_clean_idle(&cleanfds, 30, FALSE);
801901Swollman			continue;
811901Swollman		default:
8274462Salfred			svc_getreqset(&readfds);
831901Swollman		}
841901Swollman	}
851901Swollman}
8674462Salfred
8774462Salfred/*
8874462Salfred *      This function causes svc_run() to exit by telling it that it has no
8974462Salfred *      more work to do.
9074462Salfred */
9174462Salfredvoid
92288113Srodrigcsvc_exit(void)
9374462Salfred{
9474462Salfred	rwlock_wrlock(&svc_fd_lock);
9574462Salfred	FD_ZERO(&svc_fdset);
9674462Salfred	rwlock_unlock(&svc_fd_lock);
9774462Salfred}
98