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