ypxfrd_main.c revision 50479
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 50479 1999-08-28 01:35:59Z peter $";
36#endif /* not lint */
37
38#include "ypxfrd.h"
39#include <err.h>
40#include <fcntl.h>
41#include <stdio.h>
42#include <stdlib.h> /* getenv, exit */
43#include <unistd.h>
44#include <rpc/pmap_clnt.h> /* for pmap_unset */
45#include <string.h> /* strcmp */
46#include <signal.h>
47#include <sys/ttycom.h> /* TIOCNOTTY */
48#ifdef __cplusplus
49#include <sysent.h> /* getdtablesize, open */
50#endif /* __cplusplus */
51#include <memory.h>
52#include <sys/socket.h>
53#include <netinet/in.h>
54#include <syslog.h>
55#include "ypxfrd_extern.h"
56#include <sys/wait.h>
57#include <errno.h>
58
59#ifndef SIG_PF
60#define	SIG_PF void(*)(int)
61#endif
62
63#ifdef DEBUG
64#define	RPC_SVC_FG
65#endif
66
67#define	_RPCSVC_CLOSEDOWN 120
68int _rpcpmstart;		/* Started by a port monitor ? */
69static int _rpcfdtype;
70		 /* Whether Stream or Datagram ? */
71	/* States a server can be in wrt request */
72
73#define	_IDLE 0
74#define	_SERVED 1
75#define	_SERVING 2
76
77extern int _rpcsvcstate;	 /* Set when a request is serviced */
78
79char *progname = "rpc.ypxfrd";
80char *yp_dir = "/var/yp/";
81
82static
83void _msgout(char* msg)
84{
85#ifdef RPC_SVC_FG
86	if (_rpcpmstart)
87		syslog(LOG_ERR, msg);
88	else
89		warnx("%s", msg);
90#else
91	syslog(LOG_ERR, msg);
92#endif
93}
94
95static void
96closedown(int sig)
97{
98	if (_rpcsvcstate == _IDLE) {
99		extern fd_set svc_fdset;
100		static int size;
101		int i, openfd;
102
103		if (_rpcfdtype == SOCK_DGRAM)
104			exit(0);
105		if (size == 0) {
106			size = getdtablesize();
107		}
108		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
109			if (FD_ISSET(i, &svc_fdset))
110				openfd++;
111		if (openfd <= 1)
112			exit(0);
113	}
114	if (_rpcsvcstate == _SERVED)
115		_rpcsvcstate = _IDLE;
116
117	(void) signal(SIGALRM, (SIG_PF) closedown);
118	(void) alarm(_RPCSVC_CLOSEDOWN/2);
119}
120
121
122static void
123ypxfrd_svc_run()
124{
125#ifdef FD_SETSIZE
126	fd_set readfds;
127#else
128	int readfds;
129#endif /* def FD_SETSIZE */
130	extern int forked;
131	int pid;
132	int fd_setsize = _rpc_dtablesize();
133
134	/* Establish the identity of the parent ypserv process. */
135	pid = getpid();
136
137	for (;;) {
138#ifdef FD_SETSIZE
139		readfds = svc_fdset;
140#else
141		readfds = svc_fds;
142#endif /* def FD_SETSIZE */
143		switch (select(fd_setsize, &readfds, NULL, NULL,
144			       (struct timeval *)0)) {
145		case -1:
146			if (errno == EINTR) {
147				continue;
148			}
149			warn("svc_run: - select failed");
150			return;
151		case 0:
152			continue;
153		default:
154			svc_getreqset(&readfds);
155			if (forked && pid != getpid())
156				exit(0);
157		}
158	}
159}
160
161static void reaper(sig)
162	int sig;
163{
164	int			status;
165	int			saved_errno;
166
167	saved_errno = errno;
168
169	if (sig == SIGHUP) {
170		load_securenets();
171		errno = saved_errno;
172		return;
173	}
174
175	if (sig == SIGCHLD) {
176		while (wait3(&status, WNOHANG, NULL) > 0)
177			children--;
178	} else {
179		(void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
180		exit(0);
181	}
182
183	errno = saved_errno;
184	return;
185}
186
187void usage()
188{
189	fprintf(stderr, "usage: rpc.ypxfrd [-p path]\n");
190	exit(0);
191}
192
193int
194main(argc, argv)
195	int argc;
196	char *argv[];
197{
198	register SVCXPRT *transp = NULL;
199	int sock;
200	int proto = 0;
201	struct sockaddr_in saddr;
202	int asize = sizeof (saddr);
203	int ch;
204
205	while ((ch = getopt(argc, argv, "p:h")) != -1) {
206		switch(ch) {
207		case 'p':
208			yp_dir = optarg;
209			break;
210		default:
211			usage();
212			break;
213		}
214	}
215
216	load_securenets();
217
218	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
219		int ssize = sizeof (int);
220
221		if (saddr.sin_family != AF_INET)
222			exit(1);
223		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
224				(char *)&_rpcfdtype, &ssize) == -1)
225			exit(1);
226		sock = 0;
227		_rpcpmstart = 1;
228		proto = 0;
229		openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON);
230	} else {
231#ifndef RPC_SVC_FG
232		int size;
233		int pid, i;
234
235		pid = fork();
236		if (pid < 0)
237			err(1, "fork");
238		if (pid)
239			exit(0);
240		size = getdtablesize();
241		for (i = 0; i < size; i++)
242			(void) close(i);
243		i = open("/dev/console", 2);
244		(void) dup2(i, 1);
245		(void) dup2(i, 2);
246		i = open("/dev/tty", 2);
247		if (i >= 0) {
248			(void) ioctl(i, TIOCNOTTY, (char *)NULL);
249			(void) close(i);
250		}
251		openlog("rpc.ypxfrd", LOG_PID, LOG_DAEMON);
252#endif
253		sock = RPC_ANYSOCK;
254		(void) pmap_unset(YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS);
255	}
256
257	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
258		transp = svcudp_create(sock);
259		if (transp == NULL) {
260			_msgout("cannot create udp service.");
261			exit(1);
262		}
263		if (!_rpcpmstart)
264			proto = IPPROTO_UDP;
265		if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) {
266			_msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, udp).");
267			exit(1);
268		}
269	}
270
271	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
272		transp = svctcp_create(sock, 0, 0);
273		if (transp == NULL) {
274			_msgout("cannot create tcp service.");
275			exit(1);
276		}
277		if (!_rpcpmstart)
278			proto = IPPROTO_TCP;
279		if (!svc_register(transp, YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, ypxfrd_freebsd_prog_1, proto)) {
280			_msgout("unable to register (YPXFRD_FREEBSD_PROG, YPXFRD_FREEBSD_VERS, tcp).");
281			exit(1);
282		}
283	}
284
285	if (transp == (SVCXPRT *)NULL) {
286		_msgout("could not create a handle");
287		exit(1);
288	}
289	if (_rpcpmstart) {
290		(void) signal(SIGALRM, (SIG_PF) closedown);
291		(void) alarm(_RPCSVC_CLOSEDOWN/2);
292	}
293
294	(void) signal(SIGPIPE, SIG_IGN);
295	(void) signal(SIGCHLD, (SIG_PF) reaper);
296	(void) signal(SIGTERM, (SIG_PF) reaper);
297	(void) signal(SIGINT, (SIG_PF) reaper);
298	(void) signal(SIGHUP, (SIG_PF) reaper);
299
300	ypxfrd_svc_run();
301	_msgout("svc_run returned");
302	exit(1);
303	/* NOTREACHED */
304}
305