1/*	$NetBSD: rstatd.c,v 1.14 2006/05/09 20:18:07 mrg Exp $	*/
2
3/*-
4 * Copyright (c) 1993, John Brezak
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 *    must display the following acknowledgement:
17 *	This product includes software developed by the University of
18 *	California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <sys/cdefs.h>
37#ifndef lint
38__RCSID("$NetBSD: rstatd.c,v 1.14 2006/05/09 20:18:07 mrg Exp $");
39#endif /* not lint */
40
41#include <sys/types.h>
42#include <sys/socket.h>
43
44#include <stdio.h>
45#include <rpc/rpc.h>
46#include <signal.h>
47#include <syslog.h>
48#include <string.h>
49#include <stdlib.h>
50#include <rpcsvc/rstat.h>
51
52extern void rstat_service(struct svc_req *, SVCXPRT *);
53__dead static void cleanup(int);
54
55int from_inetd = 1;     /* started from inetd ? */
56int closedown = 20;	/* how long to wait before going dormant */
57
58static void
59cleanup(int dummy)
60{
61        (void) rpcb_unset(RSTATPROG, RSTATVERS_TIME, NULL);
62        (void) rpcb_unset(RSTATPROG, RSTATVERS_SWTCH, NULL);
63        (void) rpcb_unset(RSTATPROG, RSTATVERS_ORIG, NULL);
64        exit(0);
65}
66
67int
68main(int argc, char *argv[])
69{
70	SVCXPRT *transp;
71	struct sockaddr_storage from;
72	socklen_t fromlen;
73
74        if (argc == 2)
75                closedown = atoi(argv[1]);
76        if (closedown <= 0)
77                closedown = 20;
78
79        /*
80         * See if inetd started us
81         */
82	fromlen = sizeof(from);
83        if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0)
84                from_inetd = 0;
85
86        if (!from_inetd) {
87                /* daemon(0, 0); */
88
89                (void)rpcb_unset(RSTATPROG, RSTATVERS_TIME, NULL);
90                (void)rpcb_unset(RSTATPROG, RSTATVERS_SWTCH, NULL);
91                (void)rpcb_unset(RSTATPROG, RSTATVERS_ORIG, NULL);
92
93		(void) signal(SIGINT, cleanup);
94		(void) signal(SIGTERM, cleanup);
95		(void) signal(SIGHUP, cleanup);
96        }
97
98        openlog("rpc.rstatd", LOG_PID, LOG_DAEMON);
99
100	if (from_inetd) {
101		transp = svc_dg_create(0, 0, 0);
102		if (transp == NULL) {
103			syslog(LOG_ERR, "cannot create udp service.");
104			exit(1);
105		}
106
107		if (!svc_reg(transp, RSTATPROG, RSTATVERS_TIME, rstat_service,
108		    NULL)) {
109			syslog(LOG_ERR, "unable to register (RSTATPROG,"
110			    "RSTATVERS_TIME)");
111			exit(1);
112		}
113
114		if (!svc_reg(transp, RSTATPROG, RSTATVERS_SWTCH, rstat_service,
115		    NULL)) {
116			syslog(LOG_ERR, "unable to register (RSTATPROG,"
117			    "RSTATVERS_TIME)");
118			exit(1);
119		}
120
121		if (!svc_reg(transp, RSTATPROG, RSTATVERS_ORIG, rstat_service,
122		    NULL)) {
123			syslog(LOG_ERR, "unable to register (RSTATPROG,"
124			    "RSTATVERS_ORIG)");
125			exit(1);
126		}
127	} else {
128		if (!svc_create(rstat_service, RSTATPROG, RSTATVERS_TIME,
129		    "udp")) {
130			syslog(LOG_ERR,
131			    "unable to create (RSTATPROG, RSTATVERS_TIME).");
132			exit(1);
133		}
134		if (!svc_create(rstat_service, RSTATPROG, RSTATVERS_SWTCH,
135		    "udp")) {
136			syslog(LOG_ERR,
137			    "unable to create (RSTATPROG, RSTATVERS_SWTCH).");
138			exit(1);
139		}
140		if (!svc_create(rstat_service, RSTATPROG, RSTATVERS_ORIG,
141		    "udp")) {
142			syslog(LOG_ERR,
143			    "unable to register (RSTATPROG, RSTATVERS_ORIG).");
144			exit(1);
145		}
146	}
147
148        svc_run();
149	syslog(LOG_ERR, "svc_run returned");
150	exit(1);
151}
152