1 /* $NetBSD: sprayd.c,v 1.14 2006/05/09 20:18:07 mrg Exp $ */
4 * Copyright (c) 1994 Christos Zoulas
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
34 static const char rcsid[] =
39 #include <rpcsvc/spray.h>
44 #include <sys/socket.h>
48 static void spray_service(struct svc_req *, SVCXPRT *);
50 static int from_inetd = 1;
52 #define timersub(tvp, uvp, vvp) \
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) { \
58 (vvp)->tv_usec += 1000000; \
65 cleanup(int sig __unused)
67 (void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
78 main(int argc, char *argv[])
82 struct sockaddr_storage from;
86 * See if inetd started us
88 fromlen = sizeof(from);
89 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
96 (void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
98 (void)signal(SIGINT, cleanup);
99 (void)signal(SIGTERM, cleanup);
100 (void)signal(SIGHUP, cleanup);
102 (void)signal(SIGALRM, die);
106 openlog("rpc.sprayd", LOG_PID, LOG_DAEMON);
109 transp = svc_tli_create(0, NULL, NULL, 0, 0);
110 if (transp == NULL) {
111 syslog(LOG_ERR, "cannot create udp service.");
114 ok = svc_reg(transp, SPRAYPROG, SPRAYVERS,
115 spray_service, NULL);
117 ok = svc_create(spray_service,
118 SPRAYPROG, SPRAYVERS, "udp");
121 "unable to register (SPRAYPROG, SPRAYVERS, %s)",
122 (!from_inetd)?"udp":"(inetd)");
127 syslog(LOG_ERR, "svc_run returned");
133 spray_service(struct svc_req *rqstp, SVCXPRT *transp)
135 static spraycumul scum;
136 static struct timeval clear, get;
138 switch (rqstp->rq_proc) {
139 case SPRAYPROC_CLEAR:
141 (void)gettimeofday(&clear, 0);
145 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
148 case SPRAYPROC_SPRAY:
153 (void)gettimeofday(&get, 0);
154 timersub(&get, &clear, &get);
155 scum.clock.sec = get.tv_sec;
156 scum.clock.usec = get.tv_usec;
160 svcerr_noproc(transp);
164 if (!svc_sendreply(transp, (xdrproc_t)xdr_spraycumul, &scum)) {
165 svcerr_systemerr(transp);
166 syslog(LOG_WARNING, "bad svc_sendreply");