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