/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1993, John Brezak4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11* 2. Redistributions in binary form must reproduce the above copyright12* notice, this list of conditions and the following disclaimer in the13* documentation and/or other materials provided with the distribution.14* 3. Neither the name of the University nor the names of its contributors15* may be used to endorse or promote products derived from this software16* without specific prior written permission.17*18* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND19* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE20* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE21* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE22* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL23* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS24* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)25* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT26* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY27* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF28* SUCH DAMAGE.29*/3031#include <stdlib.h>32#include <rpc/rpc.h>33#include <signal.h>34#include <syslog.h>35#include <rpcsvc/rstat.h>3637extern void rstat_service(struct svc_req *, SVCXPRT *);3839int from_inetd = 1; /* started from inetd ? */40int closedown = 20; /* how long to wait before going dormant */4142void43cleanup(int sig __unused)44{45(void) rpcb_unset(RSTATPROG, RSTATVERS_TIME, NULL);46(void) rpcb_unset(RSTATPROG, RSTATVERS_SWTCH, NULL);47(void) rpcb_unset(RSTATPROG, RSTATVERS_ORIG, NULL);48exit(0);49}5051int52main(int argc, char *argv[])53{54SVCXPRT *transp;55int ok;56struct sockaddr_storage from;57socklen_t fromlen;5859if (argc == 2)60closedown = atoi(argv[1]);61if (closedown <= 0)62closedown = 20;6364/*65* See if inetd started us66*/67fromlen = sizeof(from);68if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {69from_inetd = 0;70}7172if (!from_inetd) {73daemon(0, 0);7475(void)rpcb_unset(RSTATPROG, RSTATVERS_TIME, NULL);76(void)rpcb_unset(RSTATPROG, RSTATVERS_SWTCH, NULL);77(void)rpcb_unset(RSTATPROG, RSTATVERS_ORIG, NULL);7879(void) signal(SIGINT, cleanup);80(void) signal(SIGTERM, cleanup);81(void) signal(SIGHUP, cleanup);82}8384openlog("rpc.rstatd", LOG_CONS|LOG_PID, LOG_DAEMON);8586if (from_inetd) {87transp = svc_tli_create(0, NULL, NULL, 0, 0);88if (transp == NULL) {89syslog(LOG_ERR, "cannot create udp service.");90exit(1);91}92ok = svc_reg(transp, RSTATPROG, RSTATVERS_TIME,93rstat_service, NULL);94} else95ok = svc_create(rstat_service,96RSTATPROG, RSTATVERS_TIME, "udp");97if (!ok) {98syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_TIME, %s)", (!from_inetd)?"udp":"(inetd)");99exit(1);100}101if (from_inetd)102ok = svc_reg(transp, RSTATPROG, RSTATVERS_SWTCH,103rstat_service, NULL);104else105ok = svc_create(rstat_service,106RSTATPROG, RSTATVERS_SWTCH, "udp");107if (!ok) {108syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_SWTCH, %s)", (!from_inetd)?"udp":"(inetd)");109exit(1);110}111if (from_inetd)112ok = svc_reg(transp, RSTATPROG, RSTATVERS_ORIG,113rstat_service, NULL);114else115ok = svc_create(rstat_service,116RSTATPROG, RSTATVERS_ORIG, "udp");117if (!ok) {118syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_ORIG, %s)", (!from_inetd)?"udp":"(inetd)");119exit(1);120}121122svc_run();123syslog(LOG_ERR, "svc_run returned");124exit(1);125}126127128