⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 db.c

📁 DHCP服务器源码
💻 C
📖 第 1 页 / 共 2 页
字号:
	errno = 0;	fputs ("\n}\n", db_file);	if (errno) {		++errors;	}	if (errors) {		log_info ("write_host: unable to write host %s",			  host -> name);		lease_file_is_corrupt = 1;	}	return !errors;}int write_group (group)	struct group_object *group;{	int errors = 0;	int i;	/* If the lease file is corrupt, don't try to write any more leases	   until we've written a good lease file. */	if (lease_file_is_corrupt)		if (!new_lease_file ())			return 0;	if (!db_printable (group -> name))		return 0;	if (counting)		++count;	errno = 0;	fprintf (db_file, "group %s {", group -> name);	if (errno) {		++errors;	}	if (group -> flags & GROUP_OBJECT_DYNAMIC) {		errno = 0;		fprintf (db_file, "\n  dynamic;");		if (errno)			++errors;	}	if (group -> flags & GROUP_OBJECT_STATIC) {		errno = 0;		fprintf (db_file, "\n  static;");		if (errno)			++errors;	}	if (group -> flags & GROUP_OBJECT_DELETED) {		errno = 0;		fprintf (db_file, "\n  deleted;");		if (errno)			++errors;	} else {		if (group -> group) {			errno = 0;			write_statements (db_file,					  group -> group -> statements, 8);			if (errno) {				++errors;			}		}	}	errno = 0;	fputs ("\n}\n", db_file);	if (errno) {		++errors;	}	if (errors) {		log_info ("write_group: unable to write group %s",			  group -> name);		lease_file_is_corrupt = 1;	}	return !errors;}#if defined (FAILOVER_PROTOCOL)int write_failover_state (dhcp_failover_state_t *state){	struct tm *t;	int errors = 0;	if (lease_file_is_corrupt)		if (!new_lease_file ())			return 0;	errno = 0;	fprintf (db_file, "\nfailover peer \"%s\" state {", state -> name);	if (errno)		++errors;	t = gmtime (&state -> me.stos);	errno = 0;	fprintf (db_file, "\n  my state %s at %d %d/%02d/%02d %02d:%02d:%02d;",		 /* Never record our state as "startup"! */		 (state -> me.state == startup		  ? dhcp_failover_state_name_print (state -> saved_state)		  : dhcp_failover_state_name_print (state -> me.state)),		 t -> tm_wday, t -> tm_year + 1900,		 t -> tm_mon + 1, t -> tm_mday,		 t -> tm_hour, t -> tm_min, t -> tm_sec);	if (errno)		++errors;	t = gmtime (&state -> partner.stos);	errno = 0;	fprintf (db_file,		 "\n  partner state %s at %d %d/%02d/%02d %02d:%02d:%02d;",		 dhcp_failover_state_name_print (state -> partner.state),		 t -> tm_wday, t -> tm_year + 1900,		 t -> tm_mon + 1, t -> tm_mday,		 t -> tm_hour, t -> tm_min, t -> tm_sec);	if (errno)		++errors;	if (state -> i_am == secondary) {		errno = 0;		fprintf (db_file, "\n  mclt %ld;",			 (unsigned long)state -> mclt);		if (errno)			++errors;	}	fprintf (db_file, "\n}\n");	if (errno)		++errors;	if (errors) {		log_info ("write_failover_state: unable to write state %s",			  state -> name);		lease_file_is_corrupt = 1;		return 0;	}	return 1;}#endifint db_printable (s)	const char *s;{	int i;	for (i = 0; s [i]; i++)		if (!isascii (s [i]) || !isprint (s [i])		    || s [i] == '"' || s [i] == '\\')			return 0;	return 1;}int db_printable_len (s, len)	const unsigned char *s;	unsigned len;{	int i;	for (i = 0; i < len; i++)		if (!isascii (s [i]) || !isprint (s [i]) ||		    s [i] == '"' || s [i] == '\\')			return 0;	return 1;}void write_named_billing_class (const char *name, unsigned len,				struct class *class){	/* XXX billing classes that are modified by OMAPI need	   XXX to be detected and written out here. */}void write_billing_classes (){	struct collection *lp;	struct class *cp;	struct hash_bucket *bp;	int i;	for (lp = collections; lp; lp = lp -> next) {	    for (cp = lp -> classes; cp; cp = cp -> nic) {		if (cp -> spawning && cp -> hash) {		    class_hash_foreach (cp -> hash, write_named_billing_class);		}	    }	}}/* Write a spawned class to the database file. */int write_billing_class (class)	struct class *class;{	int errors = 0;	int i;	if (lease_file_is_corrupt)		if (!new_lease_file ())			return 0;	if (!class -> superclass) {		errno = 0;		fprintf (db_file, "\n  billing class \"%s\";", class -> name);		return !errno;	}	errno = 0;	fprintf (db_file, "\n  billing subclass \"%s\"",		 class -> superclass -> name);	if (errno)		++errors;	for (i = 0; i < class -> hash_string.len; i++)		if (!isascii (class -> hash_string.data [i]) ||		    !isprint (class -> hash_string.data [i]))			break;	if (i == class -> hash_string.len) {		errno = 0;		fprintf (db_file, " \"%.*s\";",			 (int)class -> hash_string.len,			 class -> hash_string.data);		if (errno)			++errors;	} else {		errno = 0;		fprintf (db_file, " %2.2x", class -> hash_string.data [0]);		if (errno)			++errors;		for (i = 1; i < class -> hash_string.len; i++) {			errno = 0;			fprintf (db_file, ":%2.2x",				 class -> hash_string.data [i]);			if (errno)				++errors;		}		errno = 0;		fprintf (db_file, ";");		if (errno)			++errors;	}	class -> dirty = !errors;	if (errors)		lease_file_is_corrupt = 1;	return !errors;}/* Commit leases after a timeout. */void commit_leases_timeout (void *foo){	commit_leases ();}/* Commit any leases that have been written out... */int commit_leases (){	/* Commit any outstanding writes to the lease database file.	   We need to do this even if we're rewriting the file below,	   just in case the rewrite fails. */	if (fflush (db_file) == EOF) {		log_info ("commit_leases: unable to commit: %m");		return 0;	}	if (fsync (fileno (db_file)) < 0) {		log_info ("commit_leases: unable to commit: %m");		return 0;	}	/* If we haven't rewritten the lease database in over an	   hour, rewrite it now.  (The length of time should probably	   be configurable. */	if (count && cur_time - write_time > 3600) {		count = 0;		write_time = cur_time;		new_lease_file ();	}	return 1;}void db_startup (testp)	int testp;{	isc_result_t status;#if defined (TRACING)	if (!trace_playback ()) {#endif		/* Read in the existing lease file... */		status = read_conf_file (path_dhcpd_db,					 (struct group *)0, 0, 1);		/* XXX ignore status? */#if defined (TRACING)	}#endif#if defined (TRACING)	/* If we're playing back, there is no lease file, so we can't	   append it, so we create one immediately (maybe this isn't	   the best solution... */	if (trace_playback ()) {		new_lease_file ();	}#endif	if (!testp) {		db_file = fopen (path_dhcpd_db, "a");		if (!db_file)			log_fatal ("Can't open %s for append.", path_dhcpd_db);		expire_all_pools ();#if defined (TRACING)		if (trace_playback ())			write_time = cur_time;		else#endif			GET_TIME (&write_time);		new_lease_file ();	}}int new_lease_file (){	char newfname [512];	char backfname [512];	TIME t;	int db_fd;	/* If we already have an open database, close it. */	if (db_file) {		fclose (db_file);		db_file = (FILE *)0;	}	/* Make a temporary lease file... */	GET_TIME (&t);	/* %Audit% Truncated filename causes panic. %2004.06.17,Safe%	 * This should never happen since the path is a configuration	 * variable from build-time or command-line.  But if it should,	 * either by malice or ignorance, we panic, since the potential	 * for havoc is high.	 */	if (snprintf (newfname, sizeof newfname, "%s.%d",		     path_dhcpd_db, (int)t) >= sizeof newfname)		log_fatal("new_lease_file: lease file path too long");	db_fd = open (newfname, O_WRONLY | O_TRUNC | O_CREAT, 0664);	if (db_fd < 0) {		log_error ("Can't create new lease file: %m");		return 0;	}	if ((db_file = fdopen (db_fd, "w")) == NULL) {		log_error ("Can't fdopen new lease file!");		goto fail;	}	/* Write an introduction so people don't complain about time	   being off. */	errno = 0;	fprintf (db_file, "# All times in this file are in UTC (GMT), not %s",		 "your local timezone.   This is\n");	if (errno != 0)		goto fail;	fprintf (db_file, "# not a bug, so please don't ask about it.   %s",		 "There is no portable way to\n");	if (errno != 0)		goto fail;	fprintf (db_file, "# store leases in the local timezone, so please %s",		 "don't request this as a\n");	if (errno != 0)		goto fail;	fprintf (db_file, "# feature.   If this is inconvenient or %s",		 "confusing to you, we sincerely\n");	if (errno != 0)		goto fail;	fprintf (db_file, "# apologize.   Seriously, though - don't ask.\n");	if (errno != 0)		goto fail;	fprintf (db_file, "# The format of this file is documented in the %s",		 "dhcpd.leases(5) manual page.\n");	if (errno != 0)		goto fail;	fprintf (db_file, "# This lease file was written by isc-dhcp-%s\n\n",		 DHCP_VERSION);	if (errno != 0)		goto fail;	/* Write out all the leases that we know of... */	counting = 0;	if (!write_leases ())		goto fail;#if defined (TRACING)	if (!trace_playback ()) {#endif	    /* %Audit% Truncated filename causes panic. %2004.06.17,Safe%	     * This should never happen since the path is a configuration	     * variable from build-time or command-line.  But if it should,	     * either by malice or ignorance, we panic, since the potential	     * for havoc is too high.	     */	    if (snprintf (backfname, sizeof backfname, "%s~", path_dhcpd_db)			>= sizeof backfname)		log_fatal("new_lease_file: backup lease file path too long");	    /* Get the old database out of the way... */	    if (unlink (backfname) < 0 && errno != ENOENT) {		log_error ("Can't remove old lease database backup %s: %m",			   backfname);		goto fail;	    }	    if (link (path_dhcpd_db, backfname) < 0) {		log_error ("Can't backup lease database %s to %s: %m",			   path_dhcpd_db, backfname);		goto fail;	    }#if defined (TRACING)	}#endif		/* Move in the new file... */	if (rename (newfname, path_dhcpd_db) < 0) {		log_error ("Can't install new lease database %s to %s: %m",			   newfname, path_dhcpd_db);		goto fail;	}	counting = 1;	lease_file_is_corrupt = 0;	return 1;      fail:	unlink (newfname);	lease_file_is_corrupt = 1;	return 0;}int group_writer (struct group_object *group){	if (!write_group (group))		return 0;	if (!commit_leases ())		return 0;	return 1;}

⌨️ 快捷键说明

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