yp_main.c revision 21097
1/*
2 * Copyright (c) 1995
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 *	$Id: yp_main.c,v 1.10 1996/12/25 18:10:34 wpaul Exp $
33 */
34
35/*
36 * ypserv startup function.
37 * We need out own main() since we have to do some additional work
38 * that rpcgen won't do for us. Most of this file was generated using
39 * rpcgen.new, and later modified.
40 */
41
42#include "yp.h"
43#include <stdio.h>
44#include <stdlib.h> /* getenv, exit */
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 <sys/wait.h>
57#include "yp_extern.h"
58#include <unistd.h>
59#include <rpc/rpc.h>
60#include <errno.h>
61#include <err.h>
62
63#ifndef SIG_PF
64#define	SIG_PF void(*)(int)
65#endif
66
67#define	_RPCSVC_CLOSEDOWN 120
68#ifndef lint
69static const char rcsid[] = "$Id: yp_main.c,v 1.10 1996/12/25 18:10:34 wpaul Exp $";
70#endif /* not lint */
71int _rpcpmstart;		/* Started by a port monitor ? */
72static int _rpcfdtype;
73		 /* Whether Stream or Datagram ? */
74	/* States a server can be in wrt request */
75
76#define	_IDLE 0
77#define	_SERVED 1
78#define	_SERVING 2
79
80extern void ypprog_1 __P((struct svc_req *, register SVCXPRT *));
81extern void ypprog_2 __P((struct svc_req *, register SVCXPRT *));
82extern int _rpc_dtablesize __P((void));
83extern int _rpcsvcstate;	 /* Set when a request is serviced */
84char *progname = "ypserv";
85char *yp_dir = _PATH_YP;
86int debug = 0;
87int do_dns = 0;
88int resfd;
89
90static
91void _msgout(char* msg)
92{
93	if (debug) {
94		if (_rpcpmstart)
95			syslog(LOG_ERR, msg);
96		else
97			(void) fprintf(stderr, "%s\n", msg);
98	} else
99		syslog(LOG_ERR, msg);
100}
101
102static void
103yp_svc_run()
104{
105#ifdef FD_SETSIZE
106	fd_set readfds;
107#else
108	int readfds;
109#endif /* def FD_SETSIZE */
110	extern int forked;
111	int pid;
112	int fd_setsize = _rpc_dtablesize();
113	struct timeval timeout = { RESOLVER_TIMEOUT, 0 };
114
115	/* Establish the identity of the parent ypserv process. */
116	pid = getpid();
117
118	for (;;) {
119#ifdef FD_SETSIZE
120		readfds = svc_fdset;
121#else
122		readfds = svc_fds;
123#endif /* def FD_SETSIZE */
124
125		FD_SET(resfd, &readfds);
126
127		switch (select(fd_setsize, &readfds, NULL, NULL,
128			       &timeout)) {
129		case -1:
130			if (errno == EINTR) {
131				continue;
132			}
133			perror("svc_run: - select failed");
134			return;
135		case 0:
136			yp_prune_dnsq();
137			break;
138		default:
139			if (FD_ISSET(resfd, &readfds)) {
140				yp_run_dnsq();
141				FD_CLR(resfd, &readfds);
142			}
143			svc_getreqset(&readfds);
144			if (forked && pid != getpid())
145				exit(0);
146		}
147	}
148}
149
150static void unregister()
151{
152	(void) pmap_unset(YPPROG, YPVERS);
153	(void) pmap_unset(YPPROG, YPOLDVERS);
154}
155
156static void reaper(sig)
157	int sig;
158{
159	int status;
160
161	if (sig == SIGHUP) {
162		load_securenets();
163#ifdef DB_CACHE
164		yp_flush_all();
165#endif
166		return;
167	}
168
169	if (sig == SIGCHLD) {
170		while (wait3(&status, WNOHANG, NULL) > 0)
171			children--;
172	} else {
173		unregister();
174		exit(0);
175	}
176}
177
178static void usage()
179{
180	fprintf(stderr, "Usage: %s [-h] [-d] [-n] [-p path]\n", progname);
181	exit(1);
182}
183
184static void
185closedown(int sig)
186{
187	if (_rpcsvcstate == _IDLE) {
188		extern fd_set svc_fdset;
189		static int size;
190		int i, openfd;
191
192		if (_rpcfdtype == SOCK_DGRAM) {
193			unregister();
194			exit(0);
195		}
196		if (size == 0) {
197			size = getdtablesize();
198		}
199		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
200			if (FD_ISSET(i, &svc_fdset))
201				openfd++;
202		if (openfd <= 1) {
203			unregister();
204			exit(0);
205		}
206	}
207	if (_rpcsvcstate == _SERVED)
208		_rpcsvcstate = _IDLE;
209
210	(void) signal(SIGALRM, (SIG_PF) closedown);
211	(void) alarm(_RPCSVC_CLOSEDOWN/2);
212}
213
214main(argc, argv)
215	int argc;
216	char *argv[];
217{
218	register SVCXPRT *transp = NULL;
219	int sock;
220	int proto = 0;
221	struct sockaddr_in saddr;
222	int asize = sizeof (saddr);
223	int ch;
224
225	while ((ch = getopt(argc, argv, "hdnp:")) != EOF) {
226		switch(ch) {
227		case 'd':
228			debug = ypdb_debug = 1;
229			break;
230		case 'n':
231			do_dns = 1;
232			break;
233		case 'p':
234			yp_dir = optarg;
235			break;
236		case 'h':
237		default:
238			usage();
239		}
240	}
241
242	load_securenets();
243	yp_init_resolver();
244#ifdef DB_CACHE
245	yp_init_dbs();
246#endif
247	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
248		int ssize = sizeof (int);
249
250		if (saddr.sin_family != AF_INET)
251			exit(1);
252		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
253				(char *)&_rpcfdtype, &ssize) == -1)
254			exit(1);
255		sock = 0;
256		_rpcpmstart = 1;
257		proto = 0;
258		openlog(progname, LOG_PID, LOG_DAEMON);
259	} else {
260		if (!debug) {
261			if (daemon(0,0)) {
262				err(1,"cannot fork");
263			}
264			openlog(progname, LOG_PID, LOG_DAEMON);
265		}
266		sock = RPC_ANYSOCK;
267		(void) pmap_unset(YPPROG, YPVERS);
268		(void) pmap_unset(YPPROG, 1);
269	}
270
271	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
272		transp = svcudp_create(sock);
273		if (transp == NULL) {
274			_msgout("cannot create udp service.");
275			exit(1);
276		}
277		if (!_rpcpmstart)
278			proto = IPPROTO_UDP;
279		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
280			_msgout("unable to register (YPPROG, YPOLDVERS, udp).");
281			exit(1);
282		}
283		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
284			_msgout("unable to register (YPPROG, YPVERS, udp).");
285			exit(1);
286		}
287	}
288
289	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
290		transp = svctcp_create(sock, 0, 0);
291		if (transp == NULL) {
292			_msgout("cannot create tcp service.");
293			exit(1);
294		}
295		if (!_rpcpmstart)
296			proto = IPPROTO_TCP;
297		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
298			_msgout("unable to register (YPPROG, YPOLDVERS, tcp).");
299			exit(1);
300		}
301		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
302			_msgout("unable to register (YPPROG, YPVERS, tcp).");
303			exit(1);
304		}
305	}
306
307	if (transp == (SVCXPRT *)NULL) {
308		_msgout("could not create a handle");
309		exit(1);
310	}
311	if (_rpcpmstart) {
312		(void) signal(SIGALRM, (SIG_PF) closedown);
313		(void) alarm(_RPCSVC_CLOSEDOWN/2);
314	}
315/*
316 * Make sure SIGPIPE doesn't blow us away while servicing TCP
317 * connections.
318 */
319	(void) signal(SIGPIPE, SIG_IGN);
320	(void) signal(SIGCHLD, (SIG_PF) reaper);
321	(void) signal(SIGTERM, (SIG_PF) reaper);
322	(void) signal(SIGINT, (SIG_PF) reaper);
323	(void) signal(SIGHUP, (SIG_PF) reaper);
324	yp_svc_run();
325	_msgout("svc_run returned");
326	exit(1);
327	/* NOTREACHED */
328}
329