#include <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>

#define LOGFILE "/home/binarym/logfile"
extern unsigned int errno;

int main2(){
	int s_unix,len;
	FILE *fd;
	char buffer[100];
	struct sockaddr_un su;
	struct tm *st;
	/* Ouverture du fichier de log */
	if(!(fd = fopen(LOGFILE,"a"))){
		perror("fopen()");
		exit(-1);
	}
	/* Creation du socket UNIX */
	if((s_unix = socket(PF_UNIX,SOCK_DGRAM,0)) < 0){
		perror("socket()");
		exit(-1);
	}
	su.sun_family = AF_UNIX;
	strcpy(su.sun_path,"/tmp/.log");
	errno = 0;
	if(bind(s_unix,(struct sockaddr*)&su,sizeof(struct sockaddr_un)) < 0){
		perror("bind");
		exit(-1);
	}
	/* Boucle principale d'écoute */
	while(1){
		int current;
		len = 0;
		memset(buffer,0,100);
		if((len = read(s_unix,buffer,100)) > 0){
			current = time(NULL);
			st = localtime((time_t*)&current);
			fprintf(fd,"%.2i/%.2i/%i %.2i:%.2i:%.2i",st->tm_mday,st->tm_mon,st->tm_year+1900,st->tm_hour,st->tm_min,st->tm_sec);
			fprintf(fd," - %s\n",buffer);
			fflush(fd);
		}
	}
}

void main(){
	int pid;
	pid = fork();
	if(pid == 0){
		setsid();
		main2();
	}
	else{
		exit(0);
	}
}
