1 /* $NetBSD: sprayd.c,v 1.15 2009/10/21 01:07:46 snj Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 1994 Christos Zoulas
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
31 static const char rcsid[] =
36 #include <rpcsvc/spray.h>
41 #include <sys/socket.h>
45 static void spray_service(struct svc_req *, SVCXPRT *);
47 static int from_inetd = 1;
49 #define timersub(tvp, uvp, vvp) \
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) { \
55 (vvp)->tv_usec += 1000000; \
62 cleanup(int sig __unused)
64 (void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
75 main(int argc, char *argv[])
79 struct sockaddr_storage from;
83 * See if inetd started us
85 fromlen = sizeof(from);
86 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
93 (void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
95 (void)signal(SIGINT, cleanup);
96 (void)signal(SIGTERM, cleanup);
97 (void)signal(SIGHUP, cleanup);
99 (void)signal(SIGALRM, die);
103 openlog("rpc.sprayd", LOG_PID, LOG_DAEMON);
106 transp = svc_tli_create(0, NULL, NULL, 0, 0);
107 if (transp == NULL) {
108 syslog(LOG_ERR, "cannot create udp service.");
111 ok = svc_reg(transp, SPRAYPROG, SPRAYVERS,
112 spray_service, NULL);
114 ok = svc_create(spray_service,
115 SPRAYPROG, SPRAYVERS, "udp");
118 "unable to register (SPRAYPROG, SPRAYVERS, %s)",
119 (!from_inetd)?"udp":"(inetd)");
124 syslog(LOG_ERR, "svc_run returned");
130 spray_service(struct svc_req *rqstp, SVCXPRT *transp)
132 static spraycumul scum;
133 static struct timeval clear, get;
135 switch (rqstp->rq_proc) {
136 case SPRAYPROC_CLEAR:
138 (void)gettimeofday(&clear, 0);
142 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
145 case SPRAYPROC_SPRAY:
150 (void)gettimeofday(&get, 0);
151 timersub(&get, &clear, &get);
152 scum.clock.sec = get.tv_sec;
153 scum.clock.usec = get.tv_usec;
157 svcerr_noproc(transp);
161 if (!svc_sendreply(transp, (xdrproc_t)xdr_spraycumul, &scum)) {
162 svcerr_systemerr(transp);
163 syslog(LOG_WARNING, "bad svc_sendreply");