/*
	Demonio tipo UNIX, codigo de ejemplo.
	UNIX Daemon, example code rules.
	By:
	Por:
		Oscar Medina Duarte
		www.medina-web.com
	
*/

#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>

void damncode();
void nomolestar(int);
void setduplicate(int);

main(){
pid_t pid;

signal(SIGINT,nomolestar);
signal(SIGUSR1, setduplicate);

pid = fork();

if (pid <0){
	printf("Forking Error : )\n");
	exit(-1);
}else if (pid !=0 ){
	printf("\nThis is a Father 1\n");
}else{
	pid = fork();

	if (pid <0){
		printf("Forking error : )\n");
		exit(-1);
	}else if (pid !=0 ){
		printf("\nThis is a father 2\n");
		}else{
	
		/* Poner en modo daemon */
		/* Set Daemon mode */
		setsid();
		umask(0);
		chdir("/");
		damncode(); /* Y ejecutar el daemon, run the daemon */
	
	}
}

}
	






void damncode(){

	printf("Daemon :\n PID %d\tPPID %d\n",getpid(),getppid());
	for(;;);
}

void nomolestar(int signo){

	printf("Signal : %d",signo);
	exit(1);
}

void setduplicate(int signo){
	main();
}

