ypxfrd_main.c revision 90297
1/*
2 * Copyright (c) 1995, 1996
3 *	Bill Paul <wpaul@ctr.columbia.edu>.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34static const char rcsid[] =
35  "$FreeBSD: head/usr.sbin/rpc.ypxfrd/ypxfrd_main.c 90297 2002-02-06 13:30:31Z des $";
36#endif /* not lint */
37
38#include "ypxfrd.h"
39#include <err.h>
40#include <fcntl.h>
41#include <paths.h>
42#include <stdio.h>
43#include <stdlib.h> /* getenv, exit */
44#include <unistd.h>
45#include <rpc/pmap_clnt.h> /* for pmap_unset */
46#include <string.h> /* strcmp */
47#include <signal.h>
48#include <sys/ttycom.h> /* TIOCNOTTY */
49#ifdef __cplusplus
50#include <sysent.h> /* getdtablesize, open */
51#endif /* __cplusplus */
52#include <memory.h>
53#include <sys/socket.h>
54#include <netinet/in.h>
55#include <syslog.h>
56#include "ypxfrd_extern.h"
57#include <sys/wait.h>
58#include <errno.h>
59
60#ifndef SIG_PF
61#define	SIG_PF void(*)(int)
62#endif
63
64#ifdef DEBUG
65#define	RPC_SVC_FG
66#endif
67
68#define	_RPCSVC_CLOSEDOWN 120
69int _rpcpmstart;		/* Started by a port monitor ? */
70static int _rpcfdtype;
71		 /* Whether Stream or Datagram ? */
72	/* States a server can be in wrt request */
73
74#define	_IDLE 0
75#define	_SERVED 1
76#define	_SERVING 2
77
78extern int _rpcsvcstate;	 /* Set when a request is serviced */
79
80char *progname = "rpc.ypxfrd";
81char *yp_dir = "/var/yp/";
82
83static
84void _msgout(char* msg)
85{
86#ifdef RPC_SVC_FG
87	if (_rpcpmstart)
88		syslog(LOG_ERR, "%s", msg);
89	else
90		warnx("%s", msg);
91#else
92	syslog(LOG_ERR, "%s", msg);
93#endif
94}
95
96static void
97closedown(int sig)
98{
99	if (_rpcsvcstate == _IDLE) {
100		extern fd_set svc_fdset;
101		static int size;
102		int i, openfd;
103
104		if (_rpcfdtype == SOCK_DGRAM)
105			exit(0);
106		if (size == 0) {
107			size = getdtablesize();
108		}
109		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
110			if (FD_ISSET(i, &svc_fdset))
111				openfd++;
112		if (openfd <= 1)
113			exit(0);
114	}
115	if (_rpcsvcstate == _SERVED)
116		_rpcsvcstate = _IDLE;
117
118	(void) signal(SIGALRM, (SIG_PF) closedown);
119	(void) alarm(_RPCSVC_CLOSEDOWN/2);
120}
121
122
123static void
124ypxfrd_svc_run()
125{
126#ifdef FD_SETSIZE
127	fd_set readfds;
128#else
129	int readfds;
130#endif /* def FD_SETSIZE */
131	extern int forked;
132	int pid;
133	int fd_setsize = _rpc_dtablesize();
134
135	/* Establish the identity of the parent ypserv process. */
136	pid = getpid();
137
138	for (;;) {
139#ifdef FD_SETSIZE
140		readfds = svc_fdset;
141#else
142		readfds = svc_fds;
143#endif /* def FD_SETSIZE */
144		switch (select(fd_setsize, &readfds, NULL, NULL,
145			       (struct timeval *)0)) {
146		case -1:
147			if (errno == EINTR) {
148				continue;
149			}
150			warn("svc_run: - select failed");
151			return;
152		case 0:
153			continue;
154		default:
155			svc_getreqset(&readfds);
156			if (forked && pid != getpid())
157				exit(0);
158		}
159	}
160}
161
162static void reaper(sig)
163	int sig;
164{
165	int			status;
166	int			saved_errno;
167
168	saved_errno = errno;
169
170	if (sig == SIGHUP) {
171		load_securenets();
172		errno = saved_errno;
173		return;
174	}
175
176	if (sig == SIGCHLD) {
177		while (wait3(&status, WNOHANG, NULL) > 0)
178			children--;
179	} else {
180		(void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
181		exit(0);
182	}
183
184	errno = saved_errno;
185	return;
186}
187
188void usage()
189{
190	fprintf(stderr, "usage: rpc.ypxfrd [-p path]\n");
191	exit(0);
192}
193
194int
195main(argc, argv)
196	int argc;
197	char *argv[];
198{
199	register SVCXPRT *transp = NULL;
200	int sock;
201	int proto = 0;
202	struct sockaddr_in saddr;
203	int asize = sizeof (saddr);
204	int ch;
205
206	while ((ch = getopt(argc, argv, "p:h")) != -1) {
207		switch (ch) {
208		case 'p':
209			yp_dir = optarg;
210			break;
211		default:
212			usage();
213			break;
214		}
215	}
216
217	load_securenets();
218
219	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
220		int ssize = sizeof (int);
221
222		if (saddr.sin_family != AF_INET)
223			exit(1);
224		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
225				(char *)&_rpcfdtype, &ssize) == -1)
226			exit(1);
227		sock = 0;
228		_rpcpmstart = 1;
229		proto = 0;
230		openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON);
231	} else {
232#ifndef RPC_SVC_FG
233		int size;
234		int pid, i;
235
236		pid = fork();
237		if (pid < 0)
238			err(1, "fork");
239		if (pid)
240			exit(0);
241		size = getdtablesize();
242		for (i = 0; i < size; i++)
243			(void) close(i);
244		i = open(_PATH_CONSOLE, 2);
245		(void) dup2(i, 1);
246		(void) dup2(i, 2);
247		i = open(_PATH_TTY, 2);
248		if (i >= 0) {
249			(void) ioctl(i, TIOCNOTTY, (char *)NULL);
250			(void) close(i);
251		}
252		openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON);
253#endif
254		sock = RPC_ANYSOCK;
255		(void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
256	}
257
258	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
259		transp = svcudp_create(sock);
260		if (transp == NULL) {
261			_msgout("cannot create udp service.");
262			exit(1);
263		}
264		if (!_rpcpmstart)
265			proto = IPPROTO_UDP;
266		if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) {
267			_msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp).");
268			exit(1);
269		}
270	}
271
272	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
273		transp = svctcp_create(sock, 0, 0);
274		if (transp == NULL) {
275			_msgout("cannot create tcp service.");
276			exit(1);
277		}
278		if (!_rpcpmstart)
279			proto = IPPROTO_TCP;
280		if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) {
281			_msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp).");
282			exit(1);
283		}
284	}
285
286	if (transp == (SVCXPRT *)NULL) {
287		_msgout("could not create a handle");
288		exit(1);
289	}
290	if (_rpcpmstart) {
291		(void) signal(SIGALRM, (SIG_PF) closedown);
292		(void) alarm(_RPCSVC_CLOSEDOWN/2);
293	}
294
295	(void) signal(SIGPIPE, SIG_IGN);
296	(void) signal(SIGCHLD, (SIG_PF) reaper);
297	(void) signal(SIGTERM, (SIG_PF) reaper);
298	(void) signal(SIGINT, (SIG_PF) reaper);
299	(void) signal(SIGHUP, (SIG_PF) reaper);
300
301	ypxfrd_svc_run();
302	_msgout("svc_run returned");
303	exit(1);
304	/* NOTREACHED */
305}
306