xref: /freebsd/libexec/rpc.sprayd/sprayd.c (revision 4f52dfbb8d6c4d446500c5b097e3806ec219fbd4)
1 /*	$NetBSD: sprayd.c,v 1.15 2009/10/21 01:07:46 snj Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-2-Clause-NetBSD
5  *
6  * Copyright (c) 1994 Christos Zoulas
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 static const char rcsid[] =
32   "$FreeBSD$";
33 #endif /* not lint */
34 
35 #include <rpc/rpc.h>
36 #include <rpcsvc/spray.h>
37 #include <signal.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <sys/time.h>
41 #include <sys/socket.h>
42 #include <syslog.h>
43 #include <unistd.h>
44 
45 static void spray_service(struct svc_req *, SVCXPRT *);
46 
47 static int from_inetd = 1;
48 
49 #define	timersub(tvp, uvp, vvp)						\
50 	do {								\
51 		(vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;		\
52 		(vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;	\
53 		if ((vvp)->tv_usec < 0) {				\
54 			(vvp)->tv_sec--;				\
55 			(vvp)->tv_usec += 1000000;			\
56 		}							\
57 	} while (0)
58 
59 #define TIMEOUT 120
60 
61 static void
62 cleanup(int sig __unused)
63 {
64 	(void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
65 	exit(0);
66 }
67 
68 static void
69 die(int sig __unused)
70 {
71 	exit(0);
72 }
73 
74 int
75 main(int argc __unused, char *argv[] __unused)
76 {
77 	SVCXPRT *transp;
78 	int ok;
79 	struct sockaddr_storage from;
80 	socklen_t fromlen;
81 
82 	/*
83 	 * See if inetd started us
84 	 */
85 	fromlen = sizeof(from);
86 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
87 		from_inetd = 0;
88 	}
89 
90 	if (!from_inetd) {
91 		daemon(0, 0);
92 
93 		(void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
94 
95 		(void)signal(SIGINT, cleanup);
96 		(void)signal(SIGTERM, cleanup);
97 		(void)signal(SIGHUP, cleanup);
98 	} else {
99 		(void)signal(SIGALRM, die);
100 		alarm(TIMEOUT);
101 	}
102 
103 	openlog("rpc.sprayd", LOG_PID, LOG_DAEMON);
104 
105 	if (from_inetd) {
106 		transp = svc_tli_create(0, NULL, NULL, 0, 0);
107 		if (transp == NULL) {
108 			syslog(LOG_ERR, "cannot create udp service.");
109 			exit(1);
110 		}
111 		ok = svc_reg(transp, SPRAYPROG, SPRAYVERS,
112 			     spray_service, NULL);
113 	} else
114 		ok = svc_create(spray_service,
115 				SPRAYPROG, SPRAYVERS, "udp");
116 	if (!ok) {
117 		syslog(LOG_ERR,
118 		    "unable to register (SPRAYPROG, SPRAYVERS, %s)",
119 		    (!from_inetd)?"udp":"(inetd)");
120 		exit(1);
121 	}
122 
123 	svc_run();
124 	syslog(LOG_ERR, "svc_run returned");
125 	return 1;
126 }
127 
128 
129 static void
130 spray_service(struct svc_req *rqstp, SVCXPRT *transp)
131 {
132 	static spraycumul scum;
133 	static struct timeval clear, get;
134 
135 	switch (rqstp->rq_proc) {
136 	case SPRAYPROC_CLEAR:
137 		scum.counter = 0;
138 		(void)gettimeofday(&clear, 0);
139 		/*FALLTHROUGH*/
140 
141 	case NULLPROC:
142 		(void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
143 		return;
144 
145 	case SPRAYPROC_SPRAY:
146 		scum.counter++;
147 		return;
148 
149 	case SPRAYPROC_GET:
150 		(void)gettimeofday(&get, 0);
151 		timersub(&get, &clear, &get);
152 		scum.clock.sec = get.tv_sec;
153 		scum.clock.usec = get.tv_usec;
154 		break;
155 
156 	default:
157 		svcerr_noproc(transp);
158 		return;
159 	}
160 
161 	if (!svc_sendreply(transp, (xdrproc_t)xdr_spraycumul, &scum)) {
162 		svcerr_systemerr(transp);
163 		syslog(LOG_WARNING, "bad svc_sendreply");
164 	}
165 }
166