ns_main.c

来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· C语言 代码 · 共 1,408 行 · 第 1/3 页

C
1,408
字号
#ifndef lintstatic	char	*sccsid = "@(#)ns_main.c	4.3	(ULTRIX)	2/14/91";#endif lint/************************************************************************ *									* *			Copyright (c) 1984-1988 by			* *		Digital Equipment Corporation, Maynard, MA		* *			All rights reserved.				* *									* *   This software is furnished under a license and may be used and	* *   copied  only  in accordance with the terms of such license and	* *   with the  inclusion  of  the  above  copyright  notice.   This	* *   software  or  any  other copies thereof may not be provided or	* *   otherwise made available to any other person.  No title to and	* *   ownership of the software is hereby transferred.			* *									* *   This software is  derived  from  software  received  from  the	* *   University    of   California,   Berkeley,   and   from   Bell	* *   Laboratories.  Use, duplication, or disclosure is  subject  to	* *   restrictions  under  license  agreements  with  University  of	* *   California and with AT&T.						* *									* *   The information in this software is subject to change  without	* *   notice  and should not be construed as a commitment by Digital	* *   Equipment Corporation.						* *									* *   Digital assumes no responsibility for the use  or  reliability	* *   of its software on equipment which is not supplied by Digital.	* *									* ************************************************************************//* * Copyright (c) 1986 Regents of the University of California *	All Rights Reserved * static char sccsid[] = "@(#)ns_main.c	4.30 (Berkeley) 3/7/88"; *//* * Modification History: * * 18-Jan-88	logcher *	Added BIND 4.7.2. * * 26-Jan-88	logcher *	Added BIND 4.7.3. * * 17-May-89	logcher *	Added BIND 4.8. * * 17-Jul-90	sue *	Added mallocs so that all named args can be seen as is on a ps. *//* * Internet Name server (see rfc883 & others). */#include <sys/param.h>#if defined(SYSV)#include <fcntl.h>#endif SYSV#include <sys/file.h>#include <sys/time.h>#if !defined(SYSV)#include <sys/wait.h>#endif !SYSV#ifdef ULTRIXFUNC#include <sys/stat.h>#endif ULTRIXFUNC#include <sys/resource.h>#include <sys/ioctl.h>#include <sys/socket.h>#include <netinet/in.h>#include <net/if.h>#include <stdio.h>#include <syslog.h>#include <errno.h>#include <signal.h>#include <netdb.h>#include <arpa/nameser.h>#include <arpa/inet.h>#include <resolv.h>#undef nsaddr				/* XXX */#include "ns.h"#include "db.h"#ifdef BOOTFILE 			/* default boot file */char	*bootfile = BOOTFILE;#elsechar	*bootfile = "/etc/named.boot";#endif#ifdef DEBUGFILE 			/* default debug output file */char	*debugfile = DEBUGFILE;#elsechar	*debugfile = "/usr/var/tmp/named.run";#endif#ifdef PIDFILE 				/* file to store current named PID */char	*PidFile = PIDFILE;#elsechar	*PidFile = "/etc/named.pid";	#endif#ifndef FD_SET#define	NFDBITS		32#define	FD_SETSIZE	32#define	FD_SET(n, p)	((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))#define	FD_CLR(n, p)	((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))#define	FD_ISSET(n, p)	((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))#define FD_ZERO(p)	bzero((char *)(p), sizeof(*(p)))#endifFILE	*fp;  				/* file descriptor for pid file */#ifdef DEBUGFILE	*ddt;#endifint	debug = 0;			/* debugging flag */int	nofork = 0;			/* dont fork flag */int	ds;				/* datagram socket */int	read_interrupted = 0;		/* flag for read timer */int	needreload = 0;			/* received SIGHUP, need to reload db */int	needmaint = 0;			/* need to call ns_maint()*/int	needzoneload = 0;		/* need to reload secondary zone(s) from						xfer output file */int     needToDoadump = 0;              /* need to dump database */int     needToChkpt = 0;	        /* need to checkpoint cache */int	needStatsDump = 0;		/* need to dump statistics */#ifdef ALLOW_UPDATESint     needToExit = 0;                 /* need to exit (may need to doadump					 * first, if database has changed since					 * it was last dumped/booted). Gets					 * set by shutdown signal handler					 *  (onintr)					 */#endif ALLOW_UPDATES#ifdef NEW_SIGint	need_rereadzones = 0;		/* Need to check to see if the source					 * files have changed for the primary					 * or the zones of a secondary need					 * to be pulled again.					 */#endif NEW_SIG#ifdef ULTRIXFUNCint	continuemaint = 0;		/* If continuemaint == 1, there is at					 * least one zone that is being slowly					 * loaded.  So, keep loading the zone.					 */#endif ULTRIXFUNCint	priming = 0;			/* is cache being primed */#ifdef SO_RCVBUFint	rbufsize = 8 * 1024;		/* UDP recive buffer size */#endifstruct	qstream *streamq = QSTREAM_NULL; /* list of open streams */struct	qdatagram *datagramq = QDATAGRAM_NULL; /* list of datagram interfaces */struct	qdatagram *dqp, *sqp;struct	sockaddr_in nsaddr;struct	timeval tt;struct	netinfo *nettab = NULL;struct	netinfo *fnettab = NULL;struct	netinfo *onettab = NULL;struct	netinfo netloop;short	ns_port;struct	sockaddr_in from_addr;		/* Source addr of last packet */int	from_len;			/* Source addr size of last packet */time_t	boottime, resettime;		/* Used by ns_stats */fd_set	mask;				/* select mask of open descriptors */char	**Argv = NULL;			/* pointer to argument vector */char	*LastArg = NULL;		/* end of argv */#ifdef AUTHENint	defauthentype = AUTH_KRB;int	defauthenver = ONE;int	netsafe = 0;extern map_t m_credval[];extern map_t m_credtype[];extern struct hostent *gethostbyaddr_local();extern struct hostent *gethostbyname_local();struct hostent *temp_ent;u_long local_addr;#endif AUTHENextern int errno;#if defined(SYSV)getdtablesize(){	return(FD_SETSIZE);}#endif SYSVmain(argc, argv, envp)	int argc;	char *argv[], *envp[];{	register int n, udpcnt;	register char *arg;	register struct qstream *sp;	int vs;	int nfds;	int on = 1;	int rfd, size;	u_long lasttime, maxctime;	u_long nnn, nm;	char buf[BUFSIZ];	fd_set tmpmask;	struct timeval t, *tp;	struct qstream *candidate = QSTREAM_NULL;	struct ifconf ifc;	struct ifreq ifreq, *ifr;	struct netinfo *ntp;	struct netinfo *ntip;	struct netinfo *ontp;	struct netinfo *ontip;	extern int onintr(), maint_alarm(), reapchild(), setdumpflg(), onhup();	extern void endxfer();#ifdef NEW_SIG	extern int setIncrDbgFlg(), setNoDbgFlg(), sigprof(), rereadzones();#else NEW_SIG	extern int setIncrDbgFlg(), setNoDbgFlg(), sigprof();#endif NEWSIG	extern int setchkptflg(), setstatsflg();	extern struct qstream *sqadd();	extern struct qinfo *qhead; #ifdef AUTHEN	char *authen_ptr;	char *authentype_c;	char *authenver_c;	int result;	char localhost[MAXDNAME];	char *ptr;	int i;#endif AUTHEN/*	extern char Version[];*/	ns_port = htons(NAMESERVER_PORT);	_res.options &= ~(RES_DEFNAMES | RES_DNSRCH | RES_RECURSE);	/*	**  Save start and extent of argv for setproctitle.	*/	Argv = argv;	if (envp == 0 || *envp == 0)		envp = argv;	while (*envp)		envp++;	LastArg = envp[-1] + strlen(envp[-1]);	(void) umask(022);	while (--argc > 0) {		arg = *++argv;		if (*arg == '-') {			while (*++arg)				switch (*arg) {#ifdef AUTHEN				case 'a':					if (--argc <= 0)						usage();					authen_ptr = *++argv;					for( ptr = authen_ptr, i = 0;					    *ptr != '.' && *ptr != '\0';					    ptr++, i++);					if(*ptr != '.')						usage();					else {						if ((authentype_c = (char *)malloc(i)) == NULL) {#ifdef DEBUG							if (debug >= 5)								fprintf(ddt,"named: malloc error\n");#endif							syslog(LOG_ERR, "named: Out Of Memory");							exit(12);						}						strncpy(authentype_c, authen_ptr, i);						if ((authenver_c = (char *)malloc(strlen(ptr + 1))) == NULL) {#ifdef DEBUG							if (debug >= 5)								fprintf(ddt,"named: malloc error\n");#endif							syslog(LOG_ERR, "named: Out Of Memory");							exit(12);						}						strcpy(authenver_c, ptr + 1);					}					if((defauthentype=m_str_val(m_credtype,						     NUMMAPCREDTYPE,						     authentype_c))== RET_BAD)						usage();					_res.defauthtype = defauthentype;					if((defauthenver=m_str_val(m_credval,						     NUMMAPCREDVAL,						     authenver_c)) == RET_BAD)						usage();					_res.defauthversion= defauthenver;					break;				case 'n':					if (argc <= 0)						usage();					netsafe = 1;					break;#endif AUTHEN				case 'b':					if (--argc <= 0)						usage();					if ((bootfile = (char *)malloc(strlen(*++argv))) == NULL) {#ifdef DEBUG						if (debug >= 5)							fprintf(ddt,"named: malloc error\n");#endif						syslog(LOG_ERR, "named: Out Of Memory");						exit(12);					}					strcpy(bootfile, *argv);					break;  				case 'd': 					++argv; 					if (*argv != 0) { 					    if (**argv == '-') { 						argv--; 						break; 					    } 					    debug = atoi(*argv); 					    --argc; 					}					if (debug <= 0)						debug = 1;					setdebug(1);					break;				case 'f':					if (argc <= 0)						usage();					nofork = 1;					break;				case 'p':					if (--argc <= 0)						usage();					ns_port = htons((u_short)atoi(*++argv));					break;				default:					usage();				}		} else			bootfile = *argv;	}	if (!debug && !nofork) {		if (fork())			exit(0);		for (n = getdtablesize() - 1; n >= 0; n--)			(void) close(n);		(void) open("/dev/null", O_RDONLY);		(void) dup2(0, 1);		(void) dup2(0, 2);		n = open("/dev/tty", O_RDWR);		if (n > 0) {#ifndef SYSV			(void) ioctl(n, TIOCNOTTY, (char *)NULL);#else			setpgrp();#endif			(void) close(n);		}	}#ifdef DEBUG	else if(debug){		fprintf(ddt,"Debug turned ON, Level %d\n",debug);/*		fprintf(ddt,"Version = %s\t",Version);*/		fprintf(ddt,"bootfile = %s\n",bootfile);	}		#endif#ifdef LOG_DAEMON	openlog("named", LOG_PID|LOG_CONS|LOG_NDELAY, LOG_DAEMON);#else	openlog("named", LOG_PID);#endif	/* tuck my process id away */	fp = fopen(PidFile, "w");	if (fp != NULL) {		fprintf(fp, "%d\n", getpid());		(void) fclose(fp);	}	syslog(LOG_NOTICE, "restarted\n");	nsaddr.sin_family = AF_INET;	nsaddr.sin_addr.s_addr = INADDR_ANY;	nsaddr.sin_port = ns_port;#ifdef AUTHEN	if(netsafe) {		if(gethostname(localhost, MAXDNAME)) {			syslog(LOG_ERR, "bad gethostname call\n");			exit(1);		}		if((temp_ent = gethostbyname_local(localhost)) < 0)		{			syslog(LOG_ERR, "bad gethostbyname call %s\n",					localhost);			exit(1);		}		local_addr = *((u_long *)temp_ent->h_addr);		if((ptr = index(localhost, '.')) != (char *)0)			*ptr = '\0';		if((result = krb_svc_init("named", localhost, NULL, 0, NULL,				"/var/dss/kerberos/tkt/tkt.named")) != RET_OK){			syslog(LOG_ERR, "bad krb_svc_int call %d\n", result);			exit(1);		}				}#endif AUTHEN	/*	** Initialize and load database.	*/	gettime(&tt);	buildservicelist();	buildprotolist();	ns_init(bootfile);	time(&boottime);	resettime = boottime;	(void) signal(SIGALRM, maint_alarm);	(void) signal(SIGHUP, onhup);#if defined(SYSV)	(void) signal(SIGCLD, endxfer);#else	(void) signal(SIGCHLD, endxfer);#endif SYSV	(void) signal(SIGPIPE, SIG_IGN);	(void) signal(SIGSYS, sigprof);	(void) signal(SIGINT, setdumpflg);	(void) signal(SIGQUIT, setchkptflg);	(void) signal(SIGIOT, setstatsflg);#ifdef ALLOW_UPDATES        /* Catch SIGTERM so we can dump the database upon shutdown if it           has changed since it was last dumped/booted */        (void) signal(SIGTERM, onintr);#endif ALLOW_UPDATES

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?