Home | History | Annotate | Line # | Download | only in rpc.sprayd
sprayd.c revision 1.3
      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.3 1995/01/13 06:14:37 mycroft Exp $
     31  */
     32 
     33 #ifndef lint
     34 static char rcsid[] = "$Id: sprayd.c,v 1.3 1995/01/13 06:14:37 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 	}
     88 
     89 	openlog("rpc.sprayd", LOG_CONS|LOG_PID, LOG_DAEMON);
     90 
     91 	transp = svcudp_create(sock);
     92 	if (transp == NULL) {
     93 		syslog(LOG_ERR, "cannot create udp service.");
     94 		return 1;
     95 	}
     96 	if (!svc_register(transp, SPRAYPROG, SPRAYVERS, spray_service, proto)) {
     97 		syslog(LOG_ERR,
     98 		    "unable to register (SPRAYPROG, SPRAYVERS, %s).",
     99 		    proto ? "udp" : "(inetd)");
    100 		return 1;
    101 	}
    102 
    103 	alarm(TIMEOUT);
    104 	svc_run();
    105 	syslog(LOG_ERR, "svc_run returned");
    106 	return 1;
    107 }
    108 
    109 
    110 static void
    111 spray_service(rqstp, transp)
    112 	struct svc_req *rqstp;
    113 	SVCXPRT *transp;
    114 {
    115 	static spraycumul scum;
    116 	static struct timeval clear;
    117 
    118 	switch (rqstp->rq_proc) {
    119 	case SPRAYPROC_CLEAR:
    120 		scum.counter = 0;
    121 		(void) gettimeofday(&clear, 0);
    122 		/*FALLTHROUGH*/
    123 
    124 	case NULLPROC:
    125 		if (!svc_sendreply(transp, xdr_void, (char *)NULL)) {
    126 			svcerr_systemerr(transp);
    127 			syslog(LOG_ERR, "bad svc_sendreply");
    128 		}
    129 		return;
    130 
    131 	case SPRAYPROC_SPRAY:
    132 		scum.counter++;
    133 		return;
    134 
    135 	case SPRAYPROC_GET:
    136 		(void) gettimeofday((struct timeval *)&scum.clock, 0);
    137 		if (scum.clock.usec < clear.tv_usec) {
    138 			scum.clock.sec--;
    139 			scum.clock.usec += 1000000;
    140 		}
    141 		scum.clock.sec -= clear.tv_sec;
    142 		scum.clock.usec -= clear.tv_usec;
    143 		break;
    144 
    145 	default:
    146 		svcerr_noproc(transp);
    147 		return;
    148 	}
    149 
    150 	if (!svc_sendreply(transp, xdr_spraycumul, (caddr_t)&scum)) {
    151 		svcerr_systemerr(transp);
    152 		syslog(LOG_ERR, "bad svc_sendreply");
    153 	}
    154 }
    155