yp_main.c revision 20818
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.4 1996/12/22 15:54:03 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.4 1996/12/22 15:54:03 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		if (yp_dnsq_pending())
126			FD_SET(resfd, &readfds);
127
128		switch (select(fd_setsize, &readfds, NULL, NULL,
129			       &timeout)) {
130		case -1:
131			if (errno == EINTR) {
132				continue;
133			}
134			perror("svc_run: - select failed");
135			return;
136		case 0:
137			yp_prune_dnsq();
138			break;
139		default:
140			if (FD_ISSET(resfd, &readfds)) {
141				yp_run_dnsq();
142				FD_CLR(resfd, &readfds);
143			}
144			svc_getreqset(&readfds);
145			if (forked && pid != getpid())
146				exit(0);
147		}
148	}
149}
150
151static void unregister()
152{
153	(void) pmap_unset(YPPROG, YPVERS);
154	(void) pmap_unset(YPPROG, YPOLDVERS);
155}
156
157static void reaper(sig)
158	int sig;
159{
160	int status;
161
162	if (sig == SIGHUP) {
163		load_securenets();
164#ifdef DB_CACHE
165		yp_flush_all();
166#endif
167		return;
168	}
169
170	if (sig == SIGCHLD) {
171		while (wait3(&status, WNOHANG, NULL) > 0)
172			children--;
173	} else {
174		unregister();
175		exit(0);
176	}
177}
178
179static void usage()
180{
181	fprintf(stderr, "Usage: %s [-h] [-d] [-n] [-p path]\n", progname);
182	exit(1);
183}
184
185static void
186closedown(int sig)
187{
188	if (_rpcsvcstate == _IDLE) {
189		extern fd_set svc_fdset;
190		static int size;
191		int i, openfd;
192
193		if (_rpcfdtype == SOCK_DGRAM) {
194			unregister();
195			exit(0);
196		}
197		if (size == 0) {
198			size = getdtablesize();
199		}
200		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
201			if (FD_ISSET(i, &svc_fdset))
202				openfd++;
203		if (openfd <= 1) {
204			unregister();
205			exit(0);
206		}
207	}
208	if (_rpcsvcstate == _SERVED)
209		_rpcsvcstate = _IDLE;
210
211	(void) signal(SIGALRM, (SIG_PF) closedown);
212	(void) alarm(_RPCSVC_CLOSEDOWN/2);
213}
214
215main(argc, argv)
216	int argc;
217	char *argv[];
218{
219	register SVCXPRT *transp = NULL;
220	int sock;
221	int proto = 0;
222	struct sockaddr_in saddr;
223	int asize = sizeof (saddr);
224	int ch;
225
226	while ((ch = getopt(argc, argv, "hdnp:")) != EOF) {
227		switch(ch) {
228		case 'd':
229			debug = ypdb_debug = 1;
230			break;
231		case 'n':
232			do_dns = 1;
233			break;
234		case 'p':
235			yp_dir = optarg;
236			break;
237		case 'h':
238		default:
239			usage();
240		}
241	}
242
243	load_securenets();
244	yp_init_resolver();
245#ifdef DB_CACHE
246	yp_init_dbs();
247#endif
248	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
249		int ssize = sizeof (int);
250
251		if (saddr.sin_family != AF_INET)
252			exit(1);
253		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
254				(char *)&_rpcfdtype, &ssize) == -1)
255			exit(1);
256		sock = 0;
257		_rpcpmstart = 1;
258		proto = 0;
259		openlog(progname, LOG_PID, LOG_DAEMON);
260	} else {
261		if (!debug) {
262			if (daemon(0,0)) {
263				err(1,"cannot fork");
264			}
265			openlog(progname, LOG_PID, LOG_DAEMON);
266		}
267		sock = RPC_ANYSOCK;
268		(void) pmap_unset(YPPROG, YPVERS);
269		(void) pmap_unset(YPPROG, 1);
270	}
271
272	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
273		transp = svcudp_create(sock);
274		if (transp == NULL) {
275			_msgout("cannot create udp service.");
276			exit(1);
277		}
278		if (!_rpcpmstart)
279			proto = IPPROTO_UDP;
280		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
281			_msgout("unable to register (YPPROG, YPOLDVERS, udp).");
282			exit(1);
283		}
284		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
285			_msgout("unable to register (YPPROG, YPVERS, udp).");
286			exit(1);
287		}
288	}
289
290	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
291		transp = svctcp_create(sock, 0, 0);
292		if (transp == NULL) {
293			_msgout("cannot create tcp service.");
294			exit(1);
295		}
296		if (!_rpcpmstart)
297			proto = IPPROTO_TCP;
298		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
299			_msgout("unable to register (YPPROG, YPOLDVERS, tcp).");
300			exit(1);
301		}
302		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
303			_msgout("unable to register (YPPROG, YPVERS, tcp).");
304			exit(1);
305		}
306	}
307
308	if (transp == (SVCXPRT *)NULL) {
309		_msgout("could not create a handle");
310		exit(1);
311	}
312	if (_rpcpmstart) {
313		(void) signal(SIGALRM, (SIG_PF) closedown);
314		(void) alarm(_RPCSVC_CLOSEDOWN/2);
315	}
316/*
317 * Make sure SIGPIPE doesn't blow us away while servicing TCP
318 * connections.
319 */
320	(void) signal(SIGPIPE, SIG_IGN);
321	(void) signal(SIGCHLD, (SIG_PF) reaper);
322	(void) signal(SIGTERM, (SIG_PF) reaper);
323	(void) signal(SIGINT, (SIG_PF) reaper);
324	(void) signal(SIGHUP, (SIG_PF) reaper);
325	yp_svc_run();
326	_msgout("svc_run returned");
327	exit(1);
328	/* NOTREACHED */
329}
330