xref: /netbsd-src/libexec/rpc.rstatd/rstatd.c (revision 76dfffe33547c37f8bdd446e3e4ab0f3c16cea4b)
1 /*	$NetBSD: rstatd.c,v 1.7 1996/08/30 20:17:44 thorpej 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 #ifndef lint
37 static char rcsid[] = "$NetBSD: rstatd.c,v 1.7 1996/08/30 20:17:44 thorpej Exp $";
38 #endif /* not lint */
39 
40 #include <stdio.h>
41 #include <rpc/rpc.h>
42 #include <signal.h>
43 #include <syslog.h>
44 #include <rpcsvc/rstat.h>
45 
46 extern void rstat_service();
47 
48 int from_inetd = 1;     /* started from inetd ? */
49 int closedown = 20;	/* how long to wait before going dormant */
50 
51 void
52 cleanup()
53 {
54         (void) pmap_unset(RSTATPROG, RSTATVERS_TIME);
55         (void) pmap_unset(RSTATPROG, RSTATVERS_SWTCH);
56         (void) pmap_unset(RSTATPROG, RSTATVERS_ORIG);
57         exit(0);
58 }
59 
60 main(argc, argv)
61         int argc;
62         char *argv[];
63 {
64 	SVCXPRT *transp;
65         int sock = 0;
66         int proto = 0;
67 	struct sockaddr_in from;
68 	int fromlen;
69 
70         if (argc == 2)
71                 closedown = atoi(argv[1]);
72         if (closedown <= 0)
73                 closedown = 20;
74 
75         /*
76          * See if inetd started us
77          */
78 	fromlen = sizeof(from);
79         if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
80                 from_inetd = 0;
81                 sock = RPC_ANYSOCK;
82                 proto = IPPROTO_UDP;
83         }
84 
85         if (!from_inetd) {
86                 daemon(0, 0);
87 
88                 (void)pmap_unset(RSTATPROG, RSTATVERS_TIME);
89                 (void)pmap_unset(RSTATPROG, RSTATVERS_SWTCH);
90                 (void)pmap_unset(RSTATPROG, RSTATVERS_ORIG);
91 
92 		(void) signal(SIGINT, cleanup);
93 		(void) signal(SIGTERM, cleanup);
94 		(void) signal(SIGHUP, cleanup);
95         }
96 
97         openlog("rpc.rstatd", LOG_CONS|LOG_PID, LOG_DAEMON);
98 
99 	transp = svcudp_create(sock);
100 	if (transp == NULL) {
101 		syslog(LOG_ERR, "cannot create udp service.");
102 		exit(1);
103 	}
104 	if (!svc_register(transp, RSTATPROG, RSTATVERS_TIME, rstat_service, proto)) {
105 		syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_TIME, udp).");
106 		exit(1);
107 	}
108 	if (!svc_register(transp, RSTATPROG, RSTATVERS_SWTCH, rstat_service, proto)) {
109 		syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_SWTCH, udp).");
110 		exit(1);
111 	}
112 	if (!svc_register(transp, RSTATPROG, RSTATVERS_ORIG, rstat_service, proto)) {
113 		syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_ORIG, udp).");
114 		exit(1);
115 	}
116 
117         svc_run();
118 	syslog(LOG_ERR, "svc_run returned");
119 	exit(1);
120 }
121