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