sprayd.c revision 1.11 1 /* $NetBSD: sprayd.c,v 1.11 1999/01/31 08:51:53 mrg Exp $ */
2
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 #include <sys/cdefs.h>
34 #ifndef lint
35 __RCSID("$NetBSD: sprayd.c,v 1.11 1999/01/31 08:51:53 mrg Exp $");
36 #endif /* not lint */
37
38 #include <stdio.h>
39 #include <signal.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <syslog.h>
43 #include <sys/socket.h>
44 #include <sys/time.h>
45 #include <rpc/rpc.h>
46 #include <rpcsvc/spray.h>
47
48 static void cleanup __P((int));
49 static void die __P((int));
50 static void spray_service __P((struct svc_req *, SVCXPRT *));
51
52 int main __P((int, char *[]));
53
54 static int from_inetd = 1;
55
56 #define TIMEOUT 120
57
58 static void
59 cleanup(n)
60 int n;
61 {
62
63 (void)pmap_unset(SPRAYPROG, SPRAYVERS);
64 exit(0);
65 }
66
67 static void
68 die(n)
69 int n;
70 {
71
72 exit(0);
73 }
74
75 int
76 main(argc, argv)
77 int argc;
78 char *argv[];
79 {
80 SVCXPRT *transp;
81 int sock = 0;
82 int proto = 0;
83 struct sockaddr_in from;
84 int fromlen;
85
86 /*
87 * See if inetd started us
88 */
89 fromlen = sizeof(from);
90 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
91 from_inetd = 0;
92 sock = RPC_ANYSOCK;
93 proto = IPPROTO_UDP;
94 }
95
96 if (!from_inetd) {
97 daemon(0, 0);
98
99 (void)pmap_unset(SPRAYPROG, SPRAYVERS);
100
101 (void)signal(SIGINT, cleanup);
102 (void)signal(SIGTERM, cleanup);
103 (void)signal(SIGHUP, cleanup);
104 } else {
105 (void)signal(SIGALRM, die);
106 alarm(TIMEOUT);
107 }
108
109 openlog("rpc.sprayd", LOG_PID, LOG_DAEMON);
110
111 transp = svcudp_create(sock);
112 if (transp == NULL) {
113 syslog(LOG_ERR, "cannot create udp service.");
114 return 1;
115 }
116 if (!svc_register(transp, SPRAYPROG, SPRAYVERS, spray_service, proto)) {
117 syslog(LOG_ERR,
118 "unable to register (SPRAYPROG, SPRAYVERS, %s).",
119 proto ? "udp" : "(inetd)");
120 return 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(rqstp, transp)
131 struct svc_req *rqstp;
132 SVCXPRT *transp;
133 {
134 static spraycumul scum;
135 static struct timeval clear, get;
136
137 switch (rqstp->rq_proc) {
138 case SPRAYPROC_CLEAR:
139 scum.counter = 0;
140 (void)gettimeofday(&clear, 0);
141 /*FALLTHROUGH*/
142
143 case NULLPROC:
144 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
145 return;
146
147 case SPRAYPROC_SPRAY:
148 scum.counter++;
149 return;
150
151 case SPRAYPROC_GET:
152 (void)gettimeofday(&get, 0);
153 timersub(&get, &clear, &get);
154 scum.clock.sec = get.tv_sec;
155 scum.clock.usec = get.tv_usec;
156 break;
157
158 default:
159 svcerr_noproc(transp);
160 return;
161 }
162
163 if (!svc_sendreply(transp, xdr_spraycumul, (caddr_t)&scum)) {
164 svcerr_systemerr(transp);
165 syslog(LOG_ERR, "bad svc_sendreply");
166 }
167 }
168