db_dump.c

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

C
835
字号
#ifndef lintstatic	char	*sccsid = "@(#)db_dump.c	4.1	(ULTRIX)	7/2/90";#endif lint/************************************************************************ *									* *			Copyright (c) 1984-1990 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 *//* * Modification History: * * 18-Jan-88	logcher *	Added BIND 4.7.2 with ifdef-ed MIT Hesiod support. * * 26-Jan-88	logcher *	Added BIND 4.7.3. * * 08-Jun-88	logcher *	Added the correct MIT Hesiod support. * * 11-Nov-88	logcher *	Updated with V3.0 changes. * * 24-Jun-89	logcher *	Removed trailing HESIOD defines that kept a kill INT from *	displaying TXT records. * * 10-Jan-90	sue *	Incorporated Hesiod changes from MIT to implement multiple *	"character strings" in a TXT record according to RFC1035. */#include <sys/param.h>#include <sys/time.h>#include <sys/stat.h>#include <netinet/in.h>#include <netdb.h>#include <stdio.h>#include <syslog.h>#include <arpa/nameser.h>#include "ns.h"#include "db.h"extern	char *p_type(), *p_class();#ifdef DUMPFILEchar	*dumpfile = DUMPFILE;#elsechar	*dumpfile = "/usr/var/tmp/named_dump.db";#endifextern char *cache_file;/* * Dump current cache in a format similar to RFC 883. * * We try to be careful and determine whether the operation succeeded * so that the new cache file can be installed. */#define DB_ROOT_TIMBUF 3600doachkpt(){    extern int errno;    FILE *fp;    char tmpcheckfile[256];    /* nowhere to checkpoint cache... */    if (cache_file == NULL) {#ifdef DEBUG        if (debug >= 3)            fprintf(ddt,"doachkpt(to where?)\n");#endif        return;    }#ifdef DEBUG    if (debug >= 3)        fprintf(ddt,"doachkpt()\n");#endif    (void) sprintf(tmpcheckfile, "%s.chk", cache_file);    if ((fp = fopen(tmpcheckfile, "w")) == NULL) {#ifdef DEBUG        if (debug >= 3)            fprintf(ddt,"doachkpt(can't open %s for write)\n", tmpcheckfile);#endif        return;    }    (void) gettime(&tt);    fprintf(fp, "; Dumped at %s", ctime(&tt.tv_sec));    fflush(fp);    if (ferror(fp)) {#ifdef DEBUG        if (debug >= 3)            fprintf(ddt,"doachkpt(write to checkpoint file failed)\n");#endif        return;    }    if (fcachetab != NULL) {	int n;	if ((n = scan_root(hashtab)) < MINROOTS) {	    syslog(LOG_ERR, "%d root hints... (too low)", n);	    fprintf(fp, "; ---- Root hint cache dump ----\n");	    (void) db_dump(fcachetab, fp, DB_Z_CACHE, "");	}    }    if (hashtab != NULL) {        fprintf(fp, "; ---- Cache dump ----\n");        if (db_dump(hashtab, fp, DB_Z_CACHE, "") == NODBFILE) {#ifdef DEBUG            if (debug >= 3)                fprintf(ddt,"doachkpt(checkpoint failed)\n");#endif            (void) fclose(fp);            return;        }    }    (void) fsync(fileno(fp));    if (fclose(fp) == EOF) {#ifdef DEBUG        if (debug >= 3)            fprintf(ddt,"doachkpt(close failed)\n");#endif        return;    }    if (rename(tmpcheckfile, cache_file)) {#ifdef DEBUG        if (debug >= 3)            fprintf(ddt,"doachkpt(install %s to %s failed, %d)\n",                    tmpcheckfile,cache_file, errno);#endif    }}/* * What we do is scan the root hint cache to make sure there are at least * MINROOTS root pointers with non-0 TTL's so that the checkpoint will not * lose the root.  Failing this, all pointers are written out w/ TTL ~0 * (root pointers timed out and prime_cache() not done or failed). */#define TIMBUF 300intscan_root(htp)	struct hashbuf *htp;{	register struct databuf *dp;	register struct namebuf *np;	struct timeval soon;	int roots = 0;#ifdef DEBUG	if (debug)		fprintf(ddt,"scan_root(0x%x)\n", htp);#endif	/* metric by which we determine whether a root NS pointer is still */	/* valid (will be written out if we do a dump).  we also add some */	/* time buffer for safety... */	(void) gettime(&soon);	soon.tv_sec += TIMBUF;	for (np = htp->h_tab[0]; np != NULL; np = np->n_next) {		if (np->n_dname[0] == '\0') {			dp = np->n_data;			while (dp != NULL) {				if (dp->d_type == T_NS &&				    dp->d_ttl > soon.tv_sec) {					roots++;					if (roots >= MINROOTS)						return (roots);				}				dp = dp->d_next;			}		}	}	return (roots);}#ifdef notdefmark_cache(htp, ttl)    struct hashbuf *htp;    int ttl;{    register struct databuf *dp;    register struct namebuf *np;    struct namebuf **npp, **nppend;    struct timeval soon;#ifdef DEBUG    if (debug)        fprintf(ddt,"mark_cache()\n");#endif    (void) gettime(&soon);    soon.tv_sec += TIMBUF;    npp = htp->h_tab;    nppend = npp + htp->h_size;    while (npp < nppend) {        for (np = *npp++; np != NULL; np = np->n_next) {            if (np->n_data == NULL)                continue;            for (dp = np->n_data; dp != NULL; dp = dp->d_next) {                if (dp->d_ttl < soon.tv_sec)                    dp->d_ttl = ttl;            }        }    }    npp = htp->h_tab;    nppend = npp + htp->h_size;    while (npp < nppend) {        for (np = *npp++; np != NULL; np = np->n_next) {            if (np->n_hash == NULL)                continue;            mark_cache(np->n_hash, ttl);        }    }}#endif notdef/* * Dump current data base in a format similar to RFC 883. */doadump(){	FILE	*fp;#ifdef DEBUG	if (debug >= 3)		fprintf(ddt,"doadump()\n");#endif	if ((fp = fopen(dumpfile, "w")) == NULL)		return;	gettime(&tt);	fprintf(fp, "; Dumped at %s", ctime(&tt.tv_sec));	fprintf(fp, "; --- Cache & Data ---\n");	if (hashtab != NULL)		(void) db_dump(hashtab, fp, DB_Z_ALL, "");	fprintf(fp, "; --- Hints ---\n");	if (fcachetab != NULL)		(void) db_dump(fcachetab, fp, DB_Z_ALL, "");	(void) fclose(fp);}/* Create a disk database to back up zones  */zonedump(zp)    register struct zoneinfo *zp;{	FILE	*fp;	char *fname;	struct hashbuf *htp;	char *op;	struct stat st;	/* Only dump zone if there is a cache specified */	if (zp->z_source && *(zp->z_source)) {#ifdef DEBUG	    if (debug)		    fprintf(ddt, "zonedump(%s)\n", zp->z_source);#endif	    if ((fp = fopen(zp->z_source, "w")) == NULL)		    return;	    if (op = index(zp->z_origin, '.'))		op++;	    gettime(&tt);	    htp = hashtab;	    if (nlookup(zp->z_origin, &htp, &fname, 0) != NULL) {		    db_dump(htp, fp, zp-zones, (op == NULL ? "" : op));#ifdef ALLOW_UPDATES		    zp->hasChanged = 0;		/* Checkpointed */#endif ALLOW_UPDATES	    }	    (void) fclose(fp);	    if (stat(zp->z_source, &st) == 0)		    zp->z_ftime = st.st_mtime;	}#ifdef DEBUG	    else if (debug)		fprintf(ddt, "zonedump: no zone to dump\n");#endif}intdb_dump(htp, fp, zone, origin)	int zone;	struct hashbuf *htp;	FILE *fp;	char *origin;{	register struct databuf *dp;	register struct namebuf *np;	struct namebuf **npp, **nppend;	char dname[MAXDNAME];	u_long n;	u_long addr;	u_short i;	int j;	register u_char *cp;	char *proto;	extern char *inet_ntoa(), *p_protocal(), *p_service();	int found_data, tab, printed_origin = 0;	npp = htp->h_tab;	nppend = npp + htp->h_size;	while (npp < nppend) {	    for (np = *npp++; np != NULL; np = np->n_next) {		if (np->n_data == NULL)			continue;		/* Blecch - can't tell if there is data here for the		 * right zone, so can't print name yet		 */		found_data = 0;		/* we want a snapshot in time... */		for (dp = np->n_data; dp != NULL; dp = dp->d_next) {			/* Is the data for this zone? */			if (zone != DB_Z_ALL && dp->d_zone != zone)			    continue;			if (dp->d_zone == DB_Z_CACHE &&			    dp->d_ttl <= tt.tv_sec &&			    (dp->d_flags & DB_F_HINT) == 0)				continue;			if (!printed_origin) {			    fprintf(fp, "$ORIGIN %s.\n", origin);			    printed_origin++;			}			tab = 0;			if (!found_data) {			    if (np->n_dname[0] == 0) {				if (origin[0] == 0)				    fprintf(fp, ".\t");				else				    fprintf(fp, ".%s.\t", origin); /* ??? */			    } else				fprintf(fp, "%s\t", np->n_dname);			    if (strlen(np->n_dname) < 8)				tab = 1;			    found_data++;			} else {			    (void) putc('\t', fp);			    tab = 1;			}			if (dp->d_zone == DB_Z_CACHE) {			    if (dp->d_flags & DB_F_HINT &&			        (long)(dp->d_ttl - tt.tv_sec) < DB_ROOT_TIMBUF)				    fprintf(fp, "%d\t", DB_ROOT_TIMBUF);			    else				    fprintf(fp, "%d\t",				        (int)(dp->d_ttl - tt.tv_sec));			} else if (dp->d_ttl != 0 &&			    dp->d_ttl != zones[dp->d_zone].z_minimum)				fprintf(fp, "%d\t", (int)dp->d_ttl);			else if (tab)			    (void) putc('\t', fp);			fprintf(fp, "%s\t%s\t", p_class(dp->d_class),				p_type(dp->d_type));			cp = (u_char *)dp->d_data;			/*			 * Print type specific data			 */			switch (dp->d_type) {			case T_A:				switch (dp->d_class) {				case C_IN:					GETLONG(n, cp);					n = htonl(n);					fprintf(fp, "%s",					   inet_ntoa(*(struct in_addr *)&n));					break;				}				if (dp->d_nstime)					fprintf(fp, "\t; %d", dp->d_nstime);				fprintf(fp, "\n");				break;			case T_CNAME:			case T_MB:			case T_MG:			case T_MR:

⌨️ 快捷键说明

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