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