Home | History | Annotate | Line # | Download | only in rpc
svc_run.c revision 1.20.2.1
      1  1.20.2.1       tls /*	$NetBSD: svc_run.c,v 1.20.2.1 2013/06/23 06:21:05 tls Exp $	*/
      2       1.6       cgd 
      3       1.1       cgd /*
      4  1.20.2.1       tls  * Copyright (c) 2010, Oracle America, Inc.
      5  1.20.2.1       tls  *
      6  1.20.2.1       tls  * Redistribution and use in source and binary forms, with or without
      7  1.20.2.1       tls  * modification, are permitted provided that the following conditions are
      8  1.20.2.1       tls  * met:
      9  1.20.2.1       tls  *
     10  1.20.2.1       tls  *     * Redistributions of source code must retain the above copyright
     11  1.20.2.1       tls  *       notice, this list of conditions and the following disclaimer.
     12  1.20.2.1       tls  *     * Redistributions in binary form must reproduce the above
     13  1.20.2.1       tls  *       copyright notice, this list of conditions and the following
     14  1.20.2.1       tls  *       disclaimer in the documentation and/or other materials
     15  1.20.2.1       tls  *       provided with the distribution.
     16  1.20.2.1       tls  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  1.20.2.1       tls  *       contributors may be used to endorse or promote products derived
     18  1.20.2.1       tls  *       from this software without specific prior written permission.
     19  1.20.2.1       tls  *
     20  1.20.2.1       tls  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.20.2.1       tls  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.20.2.1       tls  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.20.2.1       tls  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.20.2.1       tls  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  1.20.2.1       tls  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.20.2.1       tls  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  1.20.2.1       tls  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.20.2.1       tls  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.20.2.1       tls  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  1.20.2.1       tls  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  1.20.2.1       tls  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32       1.1       cgd  */
     33       1.1       cgd 
     34       1.8  christos #include <sys/cdefs.h>
     35       1.1       cgd #if defined(LIBC_SCCS) && !defined(lint)
     36       1.8  christos #if 0
     37       1.8  christos static char *sccsid = "@(#)svc_run.c 1.1 87/10/13 Copyr 1984 Sun Micro";
     38       1.8  christos static char *sccsid = "@(#)svc_run.c	2.1 88/07/29 4.0 RPCSRC";
     39       1.8  christos #else
     40  1.20.2.1       tls __RCSID("$NetBSD: svc_run.c,v 1.20.2.1 2013/06/23 06:21:05 tls Exp $");
     41       1.8  christos #endif
     42       1.1       cgd #endif
     43       1.1       cgd 
     44       1.1       cgd /*
     45       1.1       cgd  * This is the rpc server side idle loop
     46       1.1       cgd  * Wait for input, call server program.
     47       1.1       cgd  */
     48       1.9       jtc #include "namespace.h"
     49      1.15      fvdl #include "reentrant.h"
     50      1.12     lukem #include <err.h>
     51      1.12     lukem #include <errno.h>
     52       1.8  christos #include <stdio.h>
     53      1.16   thorpej #include <string.h>
     54      1.12     lukem #include <unistd.h>
     55      1.12     lukem 
     56      1.11     lukem #include <rpc/rpc.h>
     57       1.9       jtc 
     58  1.20.2.1       tls #include "svc_fdset.h"
     59      1.18      fvdl #include "rpc_internal.h"
     60      1.18      fvdl 
     61       1.9       jtc #ifdef __weak_alias
     62      1.14   mycroft __weak_alias(svc_run,_svc_run)
     63      1.15      fvdl __weak_alias(svc_exit,_svc_exit)
     64       1.9       jtc #endif
     65       1.3   deraadt 
     66       1.1       cgd void
     67      1.20  christos svc_run(void)
     68       1.1       cgd {
     69      1.18      fvdl 	fd_set readfds, cleanfds;
     70      1.18      fvdl 	struct timeval timeout;
     71  1.20.2.1       tls 	int maxfd;
     72  1.20.2.1       tls #ifndef RUMP_RPC
     73  1.20.2.1       tls 	int probs = 0;
     74  1.20.2.1       tls #endif
     75      1.19   thorpej #ifdef _REENTRANT
     76      1.15      fvdl 	extern rwlock_t svc_fd_lock;
     77      1.15      fvdl #endif
     78       1.1       cgd 
     79      1.18      fvdl 	timeout.tv_sec = 30;
     80      1.18      fvdl 	timeout.tv_usec = 0;
     81      1.18      fvdl 
     82       1.1       cgd 	for (;;) {
     83      1.15      fvdl 		rwlock_rdlock(&svc_fd_lock);
     84  1.20.2.1       tls 		readfds = *get_fdset();
     85  1.20.2.1       tls 		cleanfds = *get_fdset();
     86  1.20.2.1       tls 		maxfd = *get_fdsetmax();
     87      1.15      fvdl 		rwlock_unlock(&svc_fd_lock);
     88  1.20.2.1       tls 		switch (select(maxfd + 1, &readfds, NULL, NULL, &timeout)) {
     89       1.1       cgd 		case -1:
     90  1.20.2.1       tls #ifndef RUMP_RPC
     91  1.20.2.1       tls 			if ((errno == EINTR || errno == EBADF) && probs < 100) {
     92  1.20.2.1       tls 				probs++;
     93  1.20.2.1       tls 				continue;
     94  1.20.2.1       tls 			}
     95  1.20.2.1       tls #endif
     96       1.1       cgd 			if (errno == EINTR) {
     97       1.1       cgd 				continue;
     98       1.1       cgd 			}
     99  1.20.2.1       tls 			warn("%s: select failed", __func__);
    100       1.1       cgd 			return;
    101       1.1       cgd 		case 0:
    102      1.18      fvdl 			__svc_clean_idle(&cleanfds, 30, FALSE);
    103       1.1       cgd 			continue;
    104       1.1       cgd 		default:
    105       1.1       cgd 			svc_getreqset(&readfds);
    106  1.20.2.1       tls #ifndef RUMP_RPC
    107  1.20.2.1       tls 			probs = 0;
    108  1.20.2.1       tls #endif
    109       1.1       cgd 		}
    110       1.1       cgd 	}
    111      1.15      fvdl }
    112      1.15      fvdl 
    113      1.15      fvdl /*
    114      1.15      fvdl  *      This function causes svc_run() to exit by telling it that it has no
    115      1.15      fvdl  *      more work to do.
    116      1.15      fvdl  */
    117      1.15      fvdl void
    118      1.20  christos svc_exit(void)
    119      1.15      fvdl {
    120      1.19   thorpej #ifdef _REENTRANT
    121      1.15      fvdl 	extern rwlock_t svc_fd_lock;
    122      1.15      fvdl #endif
    123      1.15      fvdl 
    124      1.15      fvdl 	rwlock_wrlock(&svc_fd_lock);
    125  1.20.2.1       tls 	FD_ZERO(get_fdset());
    126      1.15      fvdl 	rwlock_unlock(&svc_fd_lock);
    127       1.1       cgd }
    128