yp_main.c revision 43847
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
33#ifndef lint
34static const char rcsid[] =
35	"$Id: yp_main.c,v 1.18 1998/06/04 15:11:14 wpaul Exp $";
36#endif /* not lint */
37
38/*
39 * ypserv startup function.
40 * We need out own main() since we have to do some additional work
41 * that rpcgen won't do for us. Most of this file was generated using
42 * rpcgen.new, and later modified.
43 */
44
45#include "yp.h"
46#include <err.h>
47#include <errno.h>
48#include <memory.h>
49#include <stdio.h>
50#include <signal.h>
51#include <stdlib.h> /* getenv, exit */
52#include <string.h> /* strcmp */
53#include <syslog.h>
54#include <unistd.h>
55#include <rpc/pmap_clnt.h> /* for pmap_unset */
56#include <sys/ttycom.h> /* TIOCNOTTY */
57#ifdef __cplusplus
58#include <sysent.h> /* getdtablesize, open */
59#endif /* __cplusplus */
60#include <sys/socket.h>
61#include <netinet/in.h>
62#include <sys/wait.h>
63#include "yp_extern.h"
64#include <rpc/rpc.h>
65
66#ifndef SIG_PF
67#define	SIG_PF void(*)(int)
68#endif
69
70#define	_RPCSVC_CLOSEDOWN 120
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;
86/*int 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			warnx("%s", 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;
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		timeout.tv_sec = RESOLVER_TIMEOUT;
128		timeout.tv_usec = 0;
129		switch (select(fd_setsize, &readfds, NULL, NULL,
130			       &timeout)) {
131		case -1:
132			if (errno == EINTR) {
133				continue;
134			}
135			warn("svc_run: - select failed");
136			return;
137		case 0:
138			yp_prune_dnsq();
139			break;
140		default:
141			if (FD_ISSET(resfd, &readfds)) {
142				yp_run_dnsq();
143				FD_CLR(resfd, &readfds);
144			}
145			svc_getreqset(&readfds);
146		}
147		if (forked && pid != getpid())
148			_exit(0);
149	}
150}
151
152static void unregister()
153{
154	(void) pmap_unset(YPPROG, YPVERS);
155	(void) pmap_unset(YPPROG, YPOLDVERS);
156}
157
158static void reaper(sig)
159	int sig;
160{
161	int			status;
162	int			saved_errno;
163
164	saved_errno = errno;
165
166	if (sig == SIGHUP) {
167		load_securenets();
168#ifdef DB_CACHE
169		yp_flush_all();
170#endif
171		errno = saved_errno;
172		return;
173	}
174
175	if (sig == SIGCHLD) {
176		while (wait3(&status, WNOHANG, NULL) > 0)
177			children--;
178	} else {
179		unregister();
180		exit(0);
181	}
182	errno = saved_errno;
183	return;
184}
185
186static void usage()
187{
188	fprintf(stderr, "usage: ypserv [-h] [-d] [-n] [-p path]\n");
189	exit(1);
190}
191
192static void
193closedown(int sig)
194{
195	if (_rpcsvcstate == _IDLE) {
196		extern fd_set svc_fdset;
197		static int size;
198		int i, openfd;
199
200		if (_rpcfdtype == SOCK_DGRAM) {
201			unregister();
202			exit(0);
203		}
204		if (size == 0) {
205			size = getdtablesize();
206		}
207		for (i = 0, openfd = 0; i < size && openfd < 2; i++)
208			if (FD_ISSET(i, &svc_fdset))
209				openfd++;
210		if (openfd <= 1) {
211			unregister();
212			exit(0);
213		}
214	}
215	if (_rpcsvcstate == _SERVED)
216		_rpcsvcstate = _IDLE;
217
218	(void) signal(SIGALRM, (SIG_PF) closedown);
219	(void) alarm(_RPCSVC_CLOSEDOWN/2);
220}
221
222int
223main(argc, argv)
224	int argc;
225	char *argv[];
226{
227	register SVCXPRT *transp = NULL;
228	int sock;
229	int proto = 0;
230	struct sockaddr_in saddr;
231	int asize = sizeof (saddr);
232	int ch;
233
234	while ((ch = getopt(argc, argv, "hdnp:")) != -1) {
235		switch(ch) {
236		case 'd':
237			debug = ypdb_debug = 1;
238			break;
239		case 'n':
240			do_dns = 1;
241			break;
242		case 'p':
243			yp_dir = optarg;
244			break;
245		case 'h':
246		default:
247			usage();
248		}
249	}
250
251	load_securenets();
252	yp_init_resolver();
253#ifdef DB_CACHE
254	yp_init_dbs();
255#endif
256	if (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {
257		int ssize = sizeof (int);
258
259		if (saddr.sin_family != AF_INET)
260			exit(1);
261		if (getsockopt(0, SOL_SOCKET, SO_TYPE,
262				(char *)&_rpcfdtype, &ssize) == -1)
263			exit(1);
264		sock = 0;
265		_rpcpmstart = 1;
266		proto = 0;
267		openlog("ypserv", LOG_PID, LOG_DAEMON);
268	} else {
269		if (!debug) {
270			if (daemon(0,0)) {
271				err(1,"cannot fork");
272			}
273			openlog("ypserv", LOG_PID, LOG_DAEMON);
274		}
275		sock = RPC_ANYSOCK;
276		(void) pmap_unset(YPPROG, YPVERS);
277		(void) pmap_unset(YPPROG, 1);
278	}
279
280	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_DGRAM)) {
281		transp = svcudp_create(sock);
282		if (transp == NULL) {
283			_msgout("cannot create udp service");
284			exit(1);
285		}
286		if (!_rpcpmstart)
287			proto = IPPROTO_UDP;
288		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
289			_msgout("unable to register (YPPROG, YPOLDVERS, udp)");
290			exit(1);
291		}
292		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
293			_msgout("unable to register (YPPROG, YPVERS, udp)");
294			exit(1);
295		}
296	}
297
298	if ((_rpcfdtype == 0) || (_rpcfdtype == SOCK_STREAM)) {
299		transp = svctcp_create(sock, 0, 0);
300		if (transp == NULL) {
301			_msgout("cannot create tcp service");
302			exit(1);
303		}
304		if (!_rpcpmstart)
305			proto = IPPROTO_TCP;
306		if (!svc_register(transp, YPPROG, YPOLDVERS, ypprog_1, proto)) {
307			_msgout("unable to register (YPPROG, YPOLDVERS, tcp)");
308			exit(1);
309		}
310		if (!svc_register(transp, YPPROG, YPVERS, ypprog_2, proto)) {
311			_msgout("unable to register (YPPROG, YPVERS, tcp)");
312			exit(1);
313		}
314	}
315
316	if (transp == (SVCXPRT *)NULL) {
317		_msgout("could not create a handle");
318		exit(1);
319	}
320	if (_rpcpmstart) {
321		(void) signal(SIGALRM, (SIG_PF) closedown);
322		(void) alarm(_RPCSVC_CLOSEDOWN/2);
323	}
324/*
325 * Make sure SIGPIPE doesn't blow us away while servicing TCP
326 * connections.
327 */
328	(void) signal(SIGPIPE, SIG_IGN);
329	(void) signal(SIGCHLD, (SIG_PF) reaper);
330	(void) signal(SIGTERM, (SIG_PF) reaper);
331	(void) signal(SIGINT, (SIG_PF) reaper);
332	(void) signal(SIGHUP, (SIG_PF) reaper);
333	yp_svc_run();
334	_msgout("svc_run returned");
335	exit(1);
336	/* NOTREACHED */
337}
338