rstatd.c revision 31421
12334Scsgr/*- 22334Scsgr * Copyright (c) 1993, John Brezak 32334Scsgr * All rights reserved. 42334Scsgr * 52334Scsgr * Redistribution and use in source and binary forms, with or without 62334Scsgr * modification, are permitted provided that the following conditions 72334Scsgr * are met: 82334Scsgr * 1. Redistributions of source code must retain the above copyright 92334Scsgr * notice, this list of conditions and the following disclaimer. 102334Scsgr * 2. Redistributions in binary form must reproduce the above copyright 112334Scsgr * notice, this list of conditions and the following disclaimer in the 122334Scsgr * documentation and/or other materials provided with the distribution. 132334Scsgr * 3. All advertising materials mentioning features or use of this software 142334Scsgr * must display the following acknowledgement: 152334Scsgr * This product includes software developed by the University of 162334Scsgr * California, Berkeley and its contributors. 172334Scsgr * 4. Neither the name of the University nor the names of its contributors 182334Scsgr * may be used to endorse or promote products derived from this software 192334Scsgr * without specific prior written permission. 202334Scsgr * 212334Scsgr * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 222334Scsgr * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 232334Scsgr * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 242334Scsgr * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 252334Scsgr * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 262334Scsgr * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 272334Scsgr * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 282334Scsgr * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 292334Scsgr * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 302334Scsgr * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 312334Scsgr * SUCH DAMAGE. 322334Scsgr */ 332334Scsgr 342334Scsgr#ifndef lint 3531421Scharnierstatic const char rcsid[] = 3631421Scharnier "$Id$"; 372334Scsgr#endif /* not lint */ 382334Scsgr 392334Scsgr#include <stdio.h> 4031421Scharnier#include <stdlib.h> 412334Scsgr#include <rpc/rpc.h> 422334Scsgr#include <signal.h> 432334Scsgr#include <syslog.h> 4431421Scharnier#include <rpc/pmap_clnt.h> 452334Scsgr#include <rpcsvc/rstat.h> 462334Scsgr 472334Scsgrextern void rstat_service(); 482334Scsgr 492334Scsgrint from_inetd = 1; /* started from inetd ? */ 502334Scsgrint closedown = 20; /* how long to wait before going dormant */ 512334Scsgr 522334Scsgrvoid 532334Scsgrcleanup() 542334Scsgr{ 552334Scsgr (void) pmap_unset(RSTATPROG, RSTATVERS_TIME); 562334Scsgr (void) pmap_unset(RSTATPROG, RSTATVERS_SWTCH); 572334Scsgr (void) pmap_unset(RSTATPROG, RSTATVERS_ORIG); 582334Scsgr exit(0); 592334Scsgr} 602334Scsgr 6131421Scharnierint 622334Scsgrmain(argc, argv) 632334Scsgr int argc; 642334Scsgr char *argv[]; 652334Scsgr{ 662334Scsgr SVCXPRT *transp; 672334Scsgr int sock = 0; 682334Scsgr int proto = 0; 692334Scsgr struct sockaddr_in from; 702334Scsgr int fromlen; 718870Srgrimes 722334Scsgr if (argc == 2) 732334Scsgr closedown = atoi(argv[1]); 742334Scsgr if (closedown <= 0) 752334Scsgr closedown = 20; 762334Scsgr 772334Scsgr /* 782334Scsgr * See if inetd started us 792334Scsgr */ 804656Sats fromlen = sizeof(from); 812334Scsgr if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) { 822334Scsgr from_inetd = 0; 832334Scsgr sock = RPC_ANYSOCK; 842334Scsgr proto = IPPROTO_UDP; 852334Scsgr } 862334Scsgr 872334Scsgr if (!from_inetd) { 882334Scsgr daemon(0, 0); 892334Scsgr 902334Scsgr (void)pmap_unset(RSTATPROG, RSTATVERS_TIME); 912334Scsgr (void)pmap_unset(RSTATPROG, RSTATVERS_SWTCH); 922334Scsgr (void)pmap_unset(RSTATPROG, RSTATVERS_ORIG); 932334Scsgr 942334Scsgr (void) signal(SIGINT, cleanup); 952334Scsgr (void) signal(SIGTERM, cleanup); 962334Scsgr (void) signal(SIGHUP, cleanup); 972334Scsgr } 988870Srgrimes 992334Scsgr openlog("rpc.rstatd", LOG_CONS|LOG_PID, LOG_DAEMON); 1002334Scsgr 1012334Scsgr transp = svcudp_create(sock); 1022334Scsgr if (transp == NULL) { 10331421Scharnier syslog(LOG_ERR, "cannot create udp service"); 1042334Scsgr exit(1); 1052334Scsgr } 1062334Scsgr if (!svc_register(transp, RSTATPROG, RSTATVERS_TIME, rstat_service, proto)) { 10731421Scharnier syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_TIME, udp)"); 1082334Scsgr exit(1); 1092334Scsgr } 1102334Scsgr if (!svc_register(transp, RSTATPROG, RSTATVERS_SWTCH, rstat_service, proto)) { 11131421Scharnier syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_SWTCH, udp)"); 1122334Scsgr exit(1); 1132334Scsgr } 1142334Scsgr if (!svc_register(transp, RSTATPROG, RSTATVERS_ORIG, rstat_service, proto)) { 11531421Scharnier syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_ORIG, udp)"); 1162334Scsgr exit(1); 1172334Scsgr } 1182334Scsgr 1192334Scsgr svc_run(); 1202334Scsgr syslog(LOG_ERR, "svc_run returned"); 1212334Scsgr exit(1); 1222334Scsgr} 123