1 /*	$OpenBSD: sprayd.c,v 1.9 2004/06/02 02:21:15 brad 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 
34 #ifndef lint
35 static char rcsid[] = "$OpenBSD: sprayd.c,v 1.9 2004/06/02 02:21:15 brad Exp $";
36 #endif /* not lint */
37 
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <sys/time.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <signal.h>
44 #include <unistd.h>
45 #include <syslog.h>
46 #include <rpc/rpc.h>
47 #include <rpcsvc/spray.h>
48 
49 static void spray_service(struct svc_req *, SVCXPRT *);
50 
51 static int from_inetd = 1;
52 
53 #define TIMEOUT 120
54 
55 static void
cleanup(int signo)56 cleanup(int signo)
57 {
58 	(void) pmap_unset(SPRAYPROG, SPRAYVERS);	/* XXX signal race */
59 	_exit(0);
60 }
61 
62 static void
die(int signo)63 die(int signo)
64 {
65 	_exit(0);
66 }
67 
68 int
main(int argc,char * argv[])69 main(int argc, char *argv[])
70 {
71 	SVCXPRT *transp;
72 	int sock = 0;
73 	int proto = 0;
74 	struct sockaddr_storage from;
75 	socklen_t fromlen;
76 
77 	/*
78 	 * See if inetd started us
79 	 */
80 	fromlen = sizeof(from);
81 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
82 		from_inetd = 0;
83 		sock = RPC_ANYSOCK;
84 		proto = IPPROTO_UDP;
85 	}
86 
87 	if (!from_inetd) {
88 		daemon(0, 0);
89 
90 		(void) pmap_unset(SPRAYPROG, SPRAYVERS);
91 
92 		(void) signal(SIGINT, cleanup);
93 		(void) signal(SIGTERM, cleanup);
94 		(void) signal(SIGHUP, cleanup);
95 	} else {
96 		(void) signal(SIGALRM, die);
97 		alarm(TIMEOUT);
98 	}
99 
100 	openlog("rpc.sprayd", LOG_CONS|LOG_PID, LOG_DAEMON);
101 
102 	transp = svcudp_create(sock);
103 	if (transp == NULL) {
104 		syslog(LOG_ERR, "cannot create udp service.");
105 		return 1;
106 	}
107 	if (!svc_register(transp, SPRAYPROG, SPRAYVERS, spray_service, proto)) {
108 		syslog(LOG_ERR,
109 		    "unable to register (SPRAYPROG, SPRAYVERS, %s).",
110 		    proto ? "udp" : "(inetd)");
111 		return 1;
112 	}
113 
114 	svc_run();
115 	syslog(LOG_ERR, "svc_run returned");
116 	return 1;
117 }
118 
119 
120 static void
spray_service(struct svc_req * rqstp,SVCXPRT * transp)121 spray_service(struct svc_req *rqstp, SVCXPRT *transp)
122 {
123 	static struct timeval clear, get;
124 	static spraycumul scum;
125 
126 	switch (rqstp->rq_proc) {
127 	case SPRAYPROC_CLEAR:
128 		scum.counter = 0;
129 		(void) gettimeofday(&clear, 0);
130 		/*FALLTHROUGH*/
131 
132 	case NULLPROC:
133 		(void)svc_sendreply(transp, xdr_void, (char *)NULL);
134 		return;
135 
136 	case SPRAYPROC_SPRAY:
137 		scum.counter++;
138 		return;
139 
140 	case SPRAYPROC_GET:
141 		(void) gettimeofday(&get, 0);
142 		timersub(&get, &clear, &get);
143 		scum.clock.sec = get.tv_sec;
144 		scum.clock.usec = get.tv_usec;
145 		break;
146 
147 	default:
148 		svcerr_noproc(transp);
149 		return;
150 	}
151 
152 	if (!svc_sendreply(transp, xdr_spraycumul, (caddr_t)&scum)) {
153 		svcerr_systemerr(transp);
154 		syslog(LOG_ERR, "bad svc_sendreply");
155 	}
156 }
157