Sync usage with man page.
[netbsd-mini2440.git] / libexec / rpc.sprayd / sprayd.c
blobc5fbbec06b4e58e9bb2a12060195d960574a69f5
1 /* $NetBSD: sprayd.c,v 1.14 2006/05/09 20:18:07 mrg Exp $ */
3 /*
4 * Copyright (c) 1994 Christos Zoulas
5 * All rights reserved.
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.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: sprayd.c,v 1.14 2006/05/09 20:18:07 mrg Exp $");
31 #endif /* not lint */
33 #include <stdio.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <syslog.h>
38 #include <sys/socket.h>
39 #include <sys/time.h>
40 #include <rpc/rpc.h>
41 #include <rpcsvc/spray.h>
43 static void cleanup(int);
44 static void die(int);
45 static void spray_service(struct svc_req *, SVCXPRT *);
47 int main(int, char *[]);
49 static int from_inetd = 1;
51 #define TIMEOUT 120
53 static void
54 cleanup(int n)
57 (void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
58 exit(0);
61 static void
62 die(int n)
65 exit(0);
68 int
69 main(int argc, char *argv[])
71 SVCXPRT *transp;
72 struct sockaddr_storage from;
73 socklen_t fromlen;
76 * See if inetd started us
78 fromlen = sizeof(from);
79 if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
80 from_inetd = 0;
82 if (!from_inetd) {
83 daemon(0, 0);
85 (void)rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
87 (void)signal(SIGINT, cleanup);
88 (void)signal(SIGTERM, cleanup);
89 (void)signal(SIGHUP, cleanup);
90 } else {
91 (void)signal(SIGALRM, die);
92 alarm(TIMEOUT);
95 openlog("rpc.sprayd", LOG_PID, LOG_DAEMON);
97 if (from_inetd) {
98 transp = svc_dg_create(0, 0, 0);
99 if (transp == NULL) {
100 syslog(LOG_ERR, "cannot create udp service.");
101 exit(1);
103 if (!svc_reg(transp, SPRAYPROG, SPRAYVERS, spray_service,
104 NULL)) {
105 syslog(LOG_ERR,
106 "unable to register (SPRAYPROG, SPRAYVERS).");
107 exit(1);
109 } else {
110 if (!svc_create(spray_service, SPRAYPROG, SPRAYVERS, "udp")) {
111 syslog(LOG_ERR,
112 "unable to register (SPRAYPROG, SPRAYVERS).");
113 exit(1);
117 svc_run();
118 syslog(LOG_ERR, "svc_run returned");
119 exit(1);
123 static void
124 spray_service(struct svc_req *rqstp, SVCXPRT *transp)
126 static spraycumul scum;
127 static struct timeval clear, get;
129 switch (rqstp->rq_proc) {
130 case SPRAYPROC_CLEAR:
131 scum.counter = 0;
132 (void)gettimeofday(&clear, 0);
133 /*FALLTHROUGH*/
135 case NULLPROC:
136 (void)svc_sendreply(transp, xdr_void, (char *)NULL);
137 return;
139 case SPRAYPROC_SPRAY:
140 scum.counter++;
141 return;
143 case SPRAYPROC_GET:
144 (void)gettimeofday(&get, 0);
145 timersub(&get, &clear, &get);
146 scum.clock.sec = get.tv_sec;
147 scum.clock.usec = get.tv_usec;
148 break;
150 default:
151 svcerr_noproc(transp);
152 return;
155 if (!svc_sendreply(transp, xdr_spraycumul, (caddr_t)&scum)) {
156 svcerr_systemerr(transp);
157 syslog(LOG_WARNING, "bad svc_sendreply");