xref: /openbsd/libexec/rpc.rstatd/rstatd.c (revision 404b540a)
1 /*	$OpenBSD: rstatd.c,v 1.22 2009/05/20 20:37:43 thib 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. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef lint
32 static char rcsid[] = "$OpenBSD: rstatd.c,v 1.22 2009/05/20 20:37:43 thib Exp $";
33 #endif /* not lint */
34 
35 #include <sys/types.h>
36 #include <sys/socket.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <signal.h>
42 #include <pwd.h>
43 #include <syslog.h>
44 #include <errno.h>
45 #include <stdlib.h>
46 #include <rpc/rpc.h>
47 #include <rpcsvc/rstat.h>
48 
49 extern int __svc_fdsetsize;
50 extern fd_set *__svc_fdset;
51 extern void svc_getreqset2(fd_set *, int);
52 extern void rstat_service(struct svc_req *, SVCXPRT *);
53 
54 void my_svc_run(void);
55 
56 int from_inetd = 1;	/* started from inetd ? */
57 int closedown = 20;	/* how long to wait before going dormant */
58 
59 volatile sig_atomic_t gotsig;
60 
61 /* ARGSUSED */
62 static void
63 getsig(int signo)
64 {
65 	gotsig = 1;
66 }
67 
68 
69 int
70 main(int argc, char *argv[])
71 {
72 	int sock = 0, proto = 0;
73 	socklen_t fromlen;
74 	struct passwd *pw;
75 	struct sockaddr_storage from;
76 	SVCXPRT *transp;
77 
78 	openlog("rpc.rstatd", LOG_NDELAY|LOG_CONS|LOG_PID, LOG_DAEMON);
79 
80 	if ((pw = getpwnam("_rstatd")) == NULL) {
81 		syslog(LOG_ERR, "no such user _rstatd");
82 		exit(1);
83 	}
84 	if (chroot("/var/empty") == -1) {
85 		syslog(LOG_ERR, "cannot chdir to /var/empty.");
86 		exit(1);
87 	}
88 	chdir("/");
89 
90 	if (pw) {
91 		setgroups(1, &pw->pw_gid);
92 		setegid(pw->pw_gid);
93 		setgid(pw->pw_gid);
94 		seteuid(pw->pw_uid);
95 		setuid(pw->pw_uid);
96 	}
97 
98 	if (argc == 2)
99 		closedown = atoi(argv[1]);
100 	if (closedown <= 0)
101 		closedown = 20;
102 
103 	/*
104 	 * See if inetd started us
105 	 */
106 	fromlen = sizeof(from);
107 	if (getsockname(0, (struct sockaddr *)&from, &fromlen) < 0) {
108 		from_inetd = 0;
109 		sock = RPC_ANYSOCK;
110 		proto = IPPROTO_UDP;
111 	}
112 
113 	if (!from_inetd) {
114 		daemon(0, 0);
115 
116 		(void)pmap_unset(RSTATPROG, RSTATVERS_TIME);
117 		(void)pmap_unset(RSTATPROG, RSTATVERS_SWTCH);
118 		(void)pmap_unset(RSTATPROG, RSTATVERS_ORIG);
119 
120 		(void) signal(SIGINT, getsig);
121 		(void) signal(SIGTERM, getsig);
122 		(void) signal(SIGHUP, getsig);
123 	}
124 
125 	transp = svcudp_create(sock);
126 	if (transp == NULL) {
127 		syslog(LOG_ERR, "cannot create udp service.");
128 		exit(1);
129 	}
130 	if (!svc_register(transp, RSTATPROG, RSTATVERS_TIME, rstat_service, proto)) {
131 		syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_TIME, udp).");
132 		exit(1);
133 	}
134 	if (!svc_register(transp, RSTATPROG, RSTATVERS_SWTCH, rstat_service, proto)) {
135 		syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_SWTCH, udp).");
136 		exit(1);
137 	}
138 	if (!svc_register(transp, RSTATPROG, RSTATVERS_ORIG, rstat_service, proto)) {
139 		syslog(LOG_ERR, "unable to register (RSTATPROG, RSTATVERS_ORIG, udp).");
140 		exit(1);
141 	}
142 
143 	my_svc_run();
144 	syslog(LOG_ERR, "svc_run returned");
145 	exit(1);
146 }
147 
148 void
149 my_svc_run(void)
150 {
151 	extern volatile sig_atomic_t wantupdatestat;
152 	extern void updatestat(void);
153 	struct pollfd *pfd = NULL, *newp;
154 	int nready, saved_max_pollfd = 0;
155 
156 	for (;;) {
157 		if (wantupdatestat) {
158 			wantupdatestat = 0;
159 			updatestat();
160 		}
161 		if (gotsig) {
162 			(void) pmap_unset(RSTATPROG, RSTATVERS_TIME);
163 			(void) pmap_unset(RSTATPROG, RSTATVERS_SWTCH);
164 			(void) pmap_unset(RSTATPROG, RSTATVERS_ORIG);
165 			exit(0);
166 		}
167 		if (svc_max_pollfd > saved_max_pollfd) {
168 			newp = realloc(pfd, sizeof(*pfd) * svc_max_pollfd);
169 			if (newp == NULL) {
170 				free(pfd);
171 				perror("svc_run: - realloc failed");
172 				return;
173 			}
174 			pfd = newp;
175 			saved_max_pollfd = svc_max_pollfd;
176 		}
177 		memcpy(pfd, svc_pollfd, sizeof(*pfd) * svc_max_pollfd);
178 
179 		nready = poll(pfd, svc_max_pollfd, INFTIM);
180 		switch (nready) {
181 		case -1:
182 			if (errno == EINTR)
183 				continue;
184 			perror("svc_run: - poll failed");
185 			free(pfd);
186 			return;
187 		case 0:
188 			continue;
189 		default:
190 			svc_getreq_poll(pfd, nready);
191 		}
192 	}
193 }
194