Home | History | Annotate | Line # | Download | only in rpc
svc_run.c revision 1.19.56.2
      1  1.19.56.2      yamt /*	$NetBSD: svc_run.c,v 1.19.56.2 2014/05/22 11:36:53 yamt Exp $	*/
      2        1.6       cgd 
      3        1.1       cgd /*
      4  1.19.56.2      yamt  * Copyright (c) 2010, Oracle America, Inc.
      5  1.19.56.2      yamt  *
      6  1.19.56.2      yamt  * Redistribution and use in source and binary forms, with or without
      7  1.19.56.2      yamt  * modification, are permitted provided that the following conditions are
      8  1.19.56.2      yamt  * met:
      9  1.19.56.2      yamt  *
     10  1.19.56.2      yamt  *     * Redistributions of source code must retain the above copyright
     11  1.19.56.2      yamt  *       notice, this list of conditions and the following disclaimer.
     12  1.19.56.2      yamt  *     * Redistributions in binary form must reproduce the above
     13  1.19.56.2      yamt  *       copyright notice, this list of conditions and the following
     14  1.19.56.2      yamt  *       disclaimer in the documentation and/or other materials
     15  1.19.56.2      yamt  *       provided with the distribution.
     16  1.19.56.2      yamt  *     * Neither the name of the "Oracle America, Inc." nor the names of its
     17  1.19.56.2      yamt  *       contributors may be used to endorse or promote products derived
     18  1.19.56.2      yamt  *       from this software without specific prior written permission.
     19  1.19.56.2      yamt  *
     20  1.19.56.2      yamt  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     21  1.19.56.2      yamt  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     22  1.19.56.2      yamt  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     23  1.19.56.2      yamt  *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     24  1.19.56.2      yamt  *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
     25  1.19.56.2      yamt  *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  1.19.56.2      yamt  *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     27  1.19.56.2      yamt  *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  1.19.56.2      yamt  *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     29  1.19.56.2      yamt  *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
     30  1.19.56.2      yamt  *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     31  1.19.56.2      yamt  *   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.19.56.2      yamt __RCSID("$NetBSD: svc_run.c,v 1.19.56.2 2014/05/22 11:36:53 yamt 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.19.56.2      yamt #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.19.56.1      yamt svc_run(void)
     68        1.1       cgd {
     69       1.18      fvdl 	fd_set readfds, cleanfds;
     70       1.18      fvdl 	struct timeval timeout;
     71  1.19.56.2      yamt 	int maxfd;
     72  1.19.56.2      yamt #ifndef RUMP_RPC
     73  1.19.56.2      yamt 	int probs = 0;
     74  1.19.56.2      yamt #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.19.56.2      yamt 		readfds = *get_fdset();
     85  1.19.56.2      yamt 		cleanfds = *get_fdset();
     86  1.19.56.2      yamt 		maxfd = *get_fdsetmax();
     87       1.15      fvdl 		rwlock_unlock(&svc_fd_lock);
     88  1.19.56.2      yamt 		switch (select(maxfd + 1, &readfds, NULL, NULL, &timeout)) {
     89        1.1       cgd 		case -1:
     90  1.19.56.2      yamt #ifndef RUMP_RPC
     91  1.19.56.2      yamt 			if ((errno == EINTR || errno == EBADF) && probs < 100) {
     92  1.19.56.2      yamt 				probs++;
     93  1.19.56.2      yamt 				continue;
     94  1.19.56.2      yamt 			}
     95  1.19.56.2      yamt #endif
     96        1.1       cgd 			if (errno == EINTR) {
     97        1.1       cgd 				continue;
     98        1.1       cgd 			}
     99  1.19.56.2      yamt 			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.19.56.2      yamt #ifndef RUMP_RPC
    107  1.19.56.2      yamt 			probs = 0;
    108  1.19.56.2      yamt #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.19.56.1      yamt 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.19.56.2      yamt 	FD_ZERO(get_fdset());
    126       1.15      fvdl 	rwlock_unlock(&svc_fd_lock);
    127        1.1       cgd }
    128