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