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