/* ***********************IMPORTANT *********************************************
 *  Copyright (C) 2004-2007 Thierry Lagarde <tlagarde@users.sourceforge.net>
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License.
 *
 *
 *                      ********************
 *
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Library General Public License for more details.
 *
 *
 *                      ********************
 *
 *
 * You are highly encouraged to send your changes to <tlagarde@users.sourceforge.net>
 * for possible incorporation into the main distribution.
 * By sending these changes to <tlagarde@users.sourceforge.net>,
 * it is assumed that you are offering <tlagarde@users.sourceforge.net>
 * the unlimited, non-exclusive right to reuse, modify, and relicense the code.
 *
 *
 *                      ********************
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 **************************************************************************************/
#ifdef __LOG_ENABLE
#include <errno.h>
#include <stdlib.h>
#include <sys/types.h>
#include <fcntl.h>
#include <signal.h>
#ifndef         __WINDOWS_ENABLE
#include <dirent.h>  
#endif
#endif
#include "Quit.h"
#include "Info.h"
#include "Debug.h"
#include "Thread_Detect.h"
#include "Thread.h"
#include "Declaration_Constante.h"
#include "Com/Initialisation_Com.h"
#include "Liste_Equipement.h"
#include "Notification_Equipement.h"
#include "Notification_Port.h"
#include "Scan_Signature.h"
#include "Com/Communication.h"
#include "Scan_Port.h"
#include "Snmp.h"
#include "Nessus.h"
#include "Liste_Sous_Reseau.h"
#include "Notification_Equipement.h"
#include "Scan_Equipement.h"
#include "Ip_Dynamic.h"
#include "Snmp.h"
#include "Semaphore.h"
#include "Macro.h"

void Arret_Immediat(){
	printf("Arret_Immediat : FAILED\n");
	exit(-1);
}



void Exit(){
	if (client_id!=-1){
		printf("Close Socket\n");
		shutdown (client_id, 0);
  		close (client_id);
	}
#ifdef __LOG_ENABLE
	Close_Log_Main();	
	Ckeck_Fermeture_Log(); 
#endif
	printf ("Stopping %s (http://autoscan-network.com)\n", VERSION_AGENT);
	exit(EXIT_SUCCESS);	
}


void Arret_Demande (gpointer * Message)
{
#ifndef         __WINDOWS_ENABLE
	if(!pthread_equal( pthread_self(), mainThreadId )){
		pthread_kill(mainThreadId,SIGINT);
		printf("Stopping ...\n");
		sleep(1);  
	
	
	
		printf("__1\n");
		sleep(1); 
		printf("__2\n");
		sleep(1); 
		printf("__3\n");
		sleep(1); 
		Exit();
	}
	signal (SIGSEGV, Arret_Immediat);

	printf("******** Stopping Thread *************\n");
	envoie_Signal_All_Thread(SIGUSR1);
	while (Nb_Thead!=0)
		sleep(1);
	printf("***************************************\n");
#endif
	Exit();
}
