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