]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - libexec/rpc.sprayd/sprayd.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / libexec / rpc.sprayd / sprayd.c
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.
29  */
30
31 #ifndef lint
32 static const char rcsid[] =
33   "$FreeBSD$";
34 #endif /* not lint */
35
36 #include <rpc/rpc.h>
37 #include <rpcsvc/spray.h>
38 #include <signal.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <sys/time.h>
42 #include <sys/socket.h>
43 #include <syslog.h>
44 #include <unistd.h>
45
46 static void spray_service(struct svc_req *, SVCXPRT *);
47
48 static int from_inetd = 1;
49
50 #define timersub(tvp, uvp, vvp)                                         \
51         do {                                                            \
52                 (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec;          \
53                 (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec;       \
54                 if ((vvp)->tv_usec < 0) {                               \
55                         (vvp)->tv_sec--;                                \
56                         (vvp)->tv_usec += 1000000;                      \
57                 }                                                       \
58         } while (0)
59
60 #define TIMEOUT 120
61
62 void
63 cleanup(int sig __unused)
64 {
65         (void) rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
66         exit(0);
67 }
68
69 void
70 die(int sig __unused)
71 {
72         exit(0);
73 }
74
75 int
76 main(int argc, char *argv[])
77 {
78         SVCXPRT *transp;
79         int ok;
80         struct sockaddr_storage from;
81         socklen_t fromlen;
82
83         /*
84          * See if inetd started us
85          */
86         fromlen = sizeof(from);
87         if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
88                 from_inetd = 0;
89         }
90
91         if (!from_inetd) {
92                 daemon(0, 0);
93
94                 (void) rpcb_unset(SPRAYPROG, SPRAYVERS, NULL);
95
96                 (void) signal(SIGINT, cleanup);
97                 (void) signal(SIGTERM, cleanup);
98                 (void) signal(SIGHUP, cleanup);
99         } else {
100                 (void) signal(SIGALRM, die);
101                 alarm(TIMEOUT);
102         }
103
104         openlog("rpc.sprayd", LOG_CONS|LOG_PID, LOG_DAEMON);
105
106         if (from_inetd) {
107                 transp = svc_tli_create(0, NULL, NULL, 0, 0);
108                 if (transp == NULL) {
109                         syslog(LOG_ERR, "cannot create udp service.");
110                         exit(1);
111                 }
112                 ok = svc_reg(transp, SPRAYPROG, SPRAYVERS,
113                              spray_service, NULL);
114         } else
115                 ok = svc_create(spray_service,
116                                 SPRAYPROG, SPRAYVERS, "udp");
117         if (!ok) {
118                 syslog(LOG_ERR,
119                     "unable to register (SPRAYPROG, SPRAYVERS, %s)",
120                     (!from_inetd)?"udp":"(inetd)");
121                 return 1;
122         }
123
124         svc_run();
125         syslog(LOG_ERR, "svc_run returned");
126         return 1;
127 }
128
129
130 static void
131 spray_service(struct svc_req *rqstp, SVCXPRT *transp)
132 {
133         static spraycumul scum;
134         static struct timeval clear, get;
135
136         switch (rqstp->rq_proc) {
137         case SPRAYPROC_CLEAR:
138                 scum.counter = 0;
139                 (void) gettimeofday(&clear, 0);
140                 /*FALLTHROUGH*/
141
142         case NULLPROC:
143                 (void)svc_sendreply(transp, (xdrproc_t)xdr_void, NULL);
144                 return;
145
146         case SPRAYPROC_SPRAY:
147                 scum.counter++;
148                 return;
149
150         case SPRAYPROC_GET:
151                 (void) gettimeofday(&get, 0);
152                 timersub(&get, &clear, &get);
153                 scum.clock.sec = get.tv_sec;
154                 scum.clock.usec = get.tv_usec;
155                 break;
156
157         default:
158                 svcerr_noproc(transp);
159                 return;
160         }
161
162         if (!svc_sendreply(transp, (xdrproc_t)xdr_spraycumul, &scum)) {
163                 svcerr_systemerr(transp);
164                 syslog(LOG_ERR, "bad svc_sendreply");
165         }
166 }