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