cleanup.c

来自「Firestorm NIDS是一个性能非常高的网络入侵检测系统 (NIDS)。目」· C语言 代码 · 共 131 行

C
131
字号
/** This file is part of Firestorm NIDS* Copyright (c) 2002 Gianni Tedesco* This program is released under the terms of the GNU GPL version 2**    This is the cleanup subsystem. You can register cleanup hooks*    which are called in reverse order when firestorm exits. Basically*    the whole exit procedure is managed from here.*/#include <stdarg.h>#include <stdio.h>#include <firestorm.h>#include <cleanup.h>#define MAX_CLEANUP 4096/* global cleanup list */static struct clean_list *cl_list=NULL;static unsigned int num_handlers=0;static unsigned int clean_warned=0;/* actual exit function to call */static proc_exit do_exit=exit;/* Track re-entrancy of cleanup(), totally illegal */static int in_cleanup=0;/* Final cleanup handler */static proc_cleanup cleanup_hack=NULL;static void *cleanup_hack_priv=NULL;static char cleanup_err[1024];void cleanup_exit_func(proc_exit e){	if ( e ) {		do_exit=e;	}else{		do_exit=exit;	}}/* exit the application, calling all cleanup handlers * as we go */void cleanup(int code, const char *format, ...){	struct clean_list *cl;	va_list va;	unsigned char ecode=M_INFO;	/* check for illegal reentrancy condition */	if ( in_cleanup ) {		mesg(M_ERR,"exit: cleanup() reentry!");		do_exit(EXIT_DBG);	}else in_cleanup=1;	/* Format and print final error message */	if ( format ) {		va_start(va, format);		vsnprintf(cleanup_err, 1024, format, va);		va_end(va);		if ( code == EXIT_ERR ) ecode=M_ERR;	}else{		sprintf(cleanup_err, "No reason specified");	}	mesg(ecode,"exit: %s", cleanup_err);		/* Call cleanup handlers in reverse order */	for(cl=cl_list; cl; cl=cl->next) {		cl->cleanup(code, cl->priv);	}	/* Call final cleanup handler */	if ( cleanup_hack )		cleanup_hack(code, cleanup_hack_priv);	mesg(M_DEBUG,"cleanup: exit with code %i", code);	/* prevent runaway interactions */	if ( code==EXIT_ERR ) {		usleep(500000);	}		do_exit(code);}/* Add a cleanup handler to be called in reverse  * order when the application exits */void cleanup_add(proc_cleanup handler, void *priv){	struct clean_list *cl;	if ( !handler )		cleanup(EXIT_ERR, "Called cleanup_add() with NULL handler");	if ( num_handlers >= MAX_CLEANUP ) {		if ( !clean_warned ) {			clean_warned++;			mesg(M_WARN,"cleanup: MAX_CLEANUP exceeded.");		}		return;	}	if ( !(cl=malloc(sizeof(struct clean_list))) ) {		cperror("malloc");	}	cl->cleanup=handler;	cl->priv=priv;		cl->next=cl_list;	cl_list=cl;	num_handlers++;}/* Add a cleanup handler that will always be called last * regardless of when it was added */void cleanup_final(proc_cleanup handler, void *priv){	if ( !handler )		cleanup(EXIT_ERR, "Called cleanup_final() with NULL handler");	if ( cleanup_hack )		cleanup(EXIT_ERR, "Called cleanup_final() twice");		cleanup_hack=handler;	cleanup_hack_priv=priv;}

⌨️ 快捷键说明

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