xref: /netbsd-src/libexec/rpc.sprayd/sprayd.c (revision ce0bb6e8d2e560ecacbe865a848624f94498063b)
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.7 1995/03/26 23:36:44 mycroft Exp $
31  */
32 
33 #ifndef lint
34 static char rcsid[] = "$Id: sprayd.c,v 1.7 1995/03/26 23:36:44 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 void
51 cleanup()
52 {
53 	(void) pmap_unset(SPRAYPROG, SPRAYVERS);
54 	exit(0);
55 }
56 
57 void
58 die()
59 {
60 	exit(0);
61 }
62 
63 int
64 main(argc, argv)
65 	int argc;
66 	char *argv[];
67 {
68 	SVCXPRT *transp;
69 	int sock = 0;
70 	int proto = 0;
71 	struct sockaddr_in from;
72 	int fromlen;
73 
74 	/*
75 	 * See if inetd started us
76 	 */
77 	fromlen = sizeof(from);
78 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
79 		from_inetd = 0;
80 		sock = RPC_ANYSOCK;
81 		proto = IPPROTO_UDP;
82 	}
83 
84 	if (!from_inetd) {
85 		daemon(0, 0);
86 
87 		(void) pmap_unset(SPRAYPROG, SPRAYVERS);
88 
89 		(void) signal(SIGINT, cleanup);
90 		(void) signal(SIGTERM, cleanup);
91 		(void) signal(SIGHUP, cleanup);
92 	} else {
93 		(void) signal(SIGALRM, die);
94 		alarm(TIMEOUT);
95 	}
96 
97 	openlog("rpc.sprayd", LOG_CONS|LOG_PID, LOG_DAEMON);
98 
99 	transp = svcudp_create(sock);
100 	if (transp == NULL) {
101 		syslog(LOG_ERR, "cannot create udp service.");
102 		return 1;
103 	}
104 	if (!svc_register(transp, SPRAYPROG, SPRAYVERS, spray_service, proto)) {
105 		syslog(LOG_ERR,
106 		    "unable to register (SPRAYPROG, SPRAYVERS, %s).",
107 		    proto ? "udp" : "(inetd)");
108 		return 1;
109 	}
110 
111 	svc_run();
112 	syslog(LOG_ERR, "svc_run returned");
113 	return 1;
114 }
115 
116 
117 static void
118 spray_service(rqstp, transp)
119 	struct svc_req *rqstp;
120 	SVCXPRT *transp;
121 {
122 	static spraycumul scum;
123 	static struct timeval clear, get;
124 
125 	switch (rqstp->rq_proc) {
126 	case SPRAYPROC_CLEAR:
127 		scum.counter = 0;
128 		(void) gettimeofday(&clear, 0);
129 		/*FALLTHROUGH*/
130 
131 	case NULLPROC:
132 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
133 		return;
134 
135 	case SPRAYPROC_SPRAY:
136 		scum.counter++;
137 		return;
138 
139 	case SPRAYPROC_GET:
140 		(void) gettimeofday(&get, 0);
141 		timersub(&get, &clear, &get);
142 		scum.clock.sec = get.tv_sec;
143 		scum.clock.usec = get.tv_usec;
144 		break;
145 
146 	default:
147 		svcerr_noproc(transp);
148 		return;
149 	}
150 
151 	if (!svc_sendreply(transp, xdr_spraycumul, (caddr_t)&scum)) {
152 		svcerr_systemerr(transp);
153 		syslog(LOG_ERR, "bad svc_sendreply");
154 	}
155 }
156