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