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