Home | History | Annotate | Line # | Download | only in rpc.sprayd
sprayd.c revision 1.9
      1 /*	$NetBSD: sprayd.c,v 1.9 1997/09/17 20:16:08 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1994 Christos Zoulas
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Christos Zoulas.
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 #include <sys/cdefs.h>
     34 #ifndef lint
     35 __RCSID("$NetBSD: sprayd.c,v 1.9 1997/09/17 20:16:08 christos Exp $");
     36 #endif /* not lint */
     37 
     38 #include <stdio.h>
     39 #include <signal.h>
     40 #include <stdlib.h>
     41 #include <unistd.h>
     42 #include <syslog.h>
     43 #include <sys/socket.h>
     44 #include <sys/time.h>
     45 #include <rpc/rpc.h>
     46 #include <rpcsvc/spray.h>
     47 
     48 static void cleanup __P((int));
     49 static void die __P((int));
     50 static void spray_service __P((struct svc_req *, SVCXPRT *));
     51 
     52 int main __P((int, char *[]));
     53 
     54 static int from_inetd = 1;
     55 
     56 #define TIMEOUT 120
     57 
     58 static void
     59 cleanup(n)
     60 	int n;
     61 {
     62 	(void) pmap_unset(SPRAYPROG, SPRAYVERS);
     63 	exit(0);
     64 }
     65 
     66 static void
     67 die(n)
     68 	int n;
     69 {
     70 	exit(0);
     71 }
     72 
     73 int
     74 main(argc, argv)
     75 	int argc;
     76 	char *argv[];
     77 {
     78 	SVCXPRT *transp;
     79 	int sock = 0;
     80 	int proto = 0;
     81 	struct sockaddr_in from;
     82 	int fromlen;
     83 
     84 	/*
     85 	 * See if inetd started us
     86 	 */
     87 	fromlen = sizeof(from);
     88 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
     89 		from_inetd = 0;
     90 		sock = RPC_ANYSOCK;
     91 		proto = IPPROTO_UDP;
     92 	}
     93 
     94 	if (!from_inetd) {
     95 		daemon(0, 0);
     96 
     97 		(void) pmap_unset(SPRAYPROG, SPRAYVERS);
     98 
     99 		(void) signal(SIGINT, cleanup);
    100 		(void) signal(SIGTERM, cleanup);
    101 		(void) signal(SIGHUP, cleanup);
    102 	} else {
    103 		(void) signal(SIGALRM, die);
    104 		alarm(TIMEOUT);
    105 	}
    106 
    107 	openlog("rpc.sprayd", LOG_CONS|LOG_PID, LOG_DAEMON);
    108 
    109 	transp = svcudp_create(sock);
    110 	if (transp == NULL) {
    111 		syslog(LOG_ERR, "cannot create udp service.");
    112 		return 1;
    113 	}
    114 	if (!svc_register(transp, SPRAYPROG, SPRAYVERS, spray_service, proto)) {
    115 		syslog(LOG_ERR,
    116 		    "unable to register (SPRAYPROG, SPRAYVERS, %s).",
    117 		    proto ? "udp" : "(inetd)");
    118 		return 1;
    119 	}
    120 
    121 	svc_run();
    122 	syslog(LOG_ERR, "svc_run returned");
    123 	return 1;
    124 }
    125 
    126 
    127 static void
    128 spray_service(rqstp, transp)
    129 	struct svc_req *rqstp;
    130 	SVCXPRT *transp;
    131 {
    132 	static spraycumul scum;
    133 	static struct timeval clear, get;
    134 
    135 	switch (rqstp->rq_proc) {
    136 	case SPRAYPROC_CLEAR:
    137 		scum.counter = 0;
    138 		(void) gettimeofday(&clear, 0);
    139 		/*FALLTHROUGH*/
    140 
    141 	case NULLPROC:
    142 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
    143 		return;
    144 
    145 	case SPRAYPROC_SPRAY:
    146 		scum.counter++;
    147 		return;
    148 
    149 	case SPRAYPROC_GET:
    150 		(void) gettimeofday(&get, 0);
    151 		timersub(&get, &clear, &get);
    152 		scum.clock.sec = get.tv_sec;
    153 		scum.clock.usec = get.tv_usec;
    154 		break;
    155 
    156 	default:
    157 		svcerr_noproc(transp);
    158 		return;
    159 	}
    160 
    161 	if (!svc_sendreply(transp, xdr_spraycumul, (caddr_t)&scum)) {
    162 		svcerr_systemerr(transp);
    163 		syslog(LOG_ERR, "bad svc_sendreply");
    164 	}
    165 }
    166