xref: /netbsd/libexec/rpc.sprayd/sprayd.c (revision 4b81573e)
1 /*	$NetBSD: sprayd.c,v 1.18 2011/09/16 16:13:17 plunky 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: sprayd.c,v 1.18 2011/09/16 16:13:17 plunky Exp $");
31 #endif /* not lint */
32 
33 #include <stdio.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <syslog.h>
38 #include <sys/socket.h>
39 #include <sys/time.h>
40 #include <rpc/rpc.h>
41 #include <rpcsvc/spray.h>
42 
43 __dead static void cleanup(int);
44 __dead static void die(int);
45 static void spray_service(struct svc_req *, SVCXPRT *);
46 
47 static int from_inetd = 1;
48 
49 #define TIMEOUT 120
50 
51 static void
cleanup(int n)52 cleanup(int n)
53 {
54 
55 	(void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
56 	exit(0);
57 }
58 
59 static void
die(int n)60 die(int n)
61 {
62 
63 	exit(0);
64 }
65 
66 int
main(int argc,char * argv[])67 main(int argc, char *argv[])
68 {
69 	SVCXPRT *transp;
70 	struct sockaddr_storage from;
71 	socklen_t fromlen;
72 
73 	/*
74 	 * See if inetd started us
75 	 */
76 	fromlen = sizeof(from);
77 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
78 		from_inetd = 0;
79 
80 	if (!from_inetd) {
81 		daemon(0, 0);
82 
83 		(void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
84 
85 		(void)signal(SIGINT, cleanup);
86 		(void)signal(SIGTERM, cleanup);
87 		(void)signal(SIGHUP, cleanup);
88 	} else {
89 		(void)signal(SIGALRM, die);
90 		alarm(TIMEOUT);
91 	}
92 
93 	openlog("rpc.sprayd", LOG_PID, LOG_DAEMON);
94 
95 	if (from_inetd) {
96 		transp = svc_dg_create(0, 0, 0);
97 		if (transp == NULL) {
98 			syslog(LOG_ERR, "cannot create udp service.");
99 			exit(1);
100 		}
101 		if (!svc_reg(transp, SPRAYPROG, SPRAYVERS, spray_service,
102 		    NULL)) {
103 			syslog(LOG_ERR,
104 			    "unable to register (SPRAYPROG, SPRAYVERS).");
105 			exit(1);
106 		}
107 	} else {
108 		if (!svc_create(spray_service, SPRAYPROG, SPRAYVERS, "udp")) {
109 			syslog(LOG_ERR,
110 			    "unable to register (SPRAYPROG, SPRAYVERS).");
111 			exit(1);
112 		}
113 	}
114 
115 	svc_run();
116 	syslog(LOG_ERR, "svc_run returned");
117 	exit(1);
118 }
119 
120 
121 static void
spray_service(struct svc_req * rqstp,SVCXPRT * transp)122 spray_service(struct svc_req *rqstp, SVCXPRT *transp)
123 {
124 	static spraycumul scum;
125 	static struct timeval clear, get;
126 
127 	switch (rqstp->rq_proc) {
128 	case SPRAYPROC_CLEAR:
129 		scum.counter = 0;
130 		(void)gettimeofday(&clear, 0);
131 		/*FALLTHROUGH*/
132 
133 	case NULLPROC:
134 		(void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
135 		return;
136 
137 	case SPRAYPROC_SPRAY:
138 		scum.counter++;
139 		return;
140 
141 	case SPRAYPROC_GET:
142 		(void)gettimeofday(&get, 0);
143 		timersub(&get, &clear, &get);
144 		scum.clock.sec = get.tv_sec;
145 		scum.clock.usec = get.tv_usec;
146 		break;
147 
148 	default:
149 		svcerr_noproc(transp);
150 		return;
151 	}
152 
153 	if (!svc_sendreply(transp, (xdrproc_t)xdr_spraycumul, (caddr_t)&scum)) {
154 		svcerr_systemerr(transp);
155 		syslog(LOG_WARNING, "bad svc_sendreply");
156 	}
157 }
158