// Filename:	errlogd.C
// Contents:	the error logger daemon 
// Author:		Greg Shaw
// Created:		7/11/93

/*
This file 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, or (at your option) any
later version.

In addition to the permissions in the GNU General Public License, the
Free Software Foundation gives you unlimited permission to link the
compiled version of this file with other programs, and to distribute
those programs without any restriction coming from the use of this
file.  (The General Public License restrictions do apply in other
respects; for example, they cover modification of the file, and
distribution when not linked into another program.)

This file 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#ifndef _ERRLOGD_C_
#define _ERRLOGD_C_

#include "bbshdr.h"

#undef DEBUG

// declared global so that the 'getout' function can close the socket
bbsipc	ipcobj;

// Function:	getout
// Purpose:	get out of the program.  Quick and dirty
// Author:	Greg Shaw
// Created:	8/16/93

void getout(int sig)
{
	char	tmpstr[255];	// temporary string
	char	ttystr[20];	// name of tty
	char	*u;		// for strchr
	static	int inalready = 0;	// if already in, don't do again
	time_t	now;			// current time
	struct tm *timeinfo;		// time information (broken down)
	char	filestr[255];	// string to output to file
	FILE	*outfile;		// file to append to
	
	if (!inalready)
	{
		ipcobj.close_sock(1);	// close for exit
		ipcobj.close_sock(0);	// close for exit
		sprintf(tmpstr,"Errlogd: Got signal %d.  Exiting.",sig);
		if (outfile = fopen(ERR_LOG_PATH,"a"), outfile != NULL)
		{
			time(&now);	// get current time
			timeinfo = localtime(&now);
			strftime(filestr,50,"%D %T: ",timeinfo); // get formatted time
			strcat(filestr,tmpstr); // add message
			fprintf(outfile,"%s\n",filestr);// append to file
			fclose(outfile);	// close file so that buffers cleared
		}
		inalready++;
		strcpy(ttystr,ttyname(fileno(stdout)));	// get ttyname
		if (u = strchr(ttystr,'y'), u != NULL)	
		{						// chop off /dev/tty
			strcpy(tmpstr,++u);	// chop after y
			strcpy(ttystr,tmpstr);	// copy back
		}
		exit(0);	// exit to OS
	}
}


// Function:	main
// Purpose:		the main calling routine for the error logger daemon
// Input:		socket connections will connect with program.  
// Output:		all data sent to this daemon will be logged to a generic file
// Author:		Greg Shaw
// Created:		7/11/93

main()		// no arguments
{
        struct timeval waittime;
	FILE	*outfile;		// file to append to
	char 	msg[255];
	time_t	now;			// current time
	int	ret,x;			// return from ipc
	struct tm *timeinfo;		// time information (broken down)
	char	filestr[255];	// string to output to file

#ifdef DEBUG
printf("Init...\r\n");
fflush(stdout);
#endif
        for (x=1; x<15; x++)
                signal(x,&getout);
	if(ipcobj.open_sock (NULL, ERRLOG_PORT) != 0) // create socket
	{
		printf("Unable to open server socket.\r\n");
		return (0);
	}
	ipcobj.do_connect();	// wait for connection
	waittime.tv_usec = 100; // 100msec
	waittime.tv_sec = 0; 
	while(1)	// loop forever
	{
#ifdef DEBUG
printf("Got connect ... waiting for message.\r\n");
fflush(stdout);
#endif
		if (ret = ipcobj.msg_avail(1), ret < 0)
		{
#ifdef DEBUG
printf("Nothing available.  Closing socket\r\n");
fflush(stdout);
#endif
			ipcobj.close_sock(1);	// close for exit
#ifdef DEBUG
printf("Disconnect.\r\n");
fflush(stdout);
#endif
			ipcobj.do_connect();	// wait for connection
		}
		else if (ret > 0)
		{
#ifdef DEBUG
printf("Something available.\r\n");
fflush(stdout);
#endif
			if (ipcobj.receive(msg)< 0)	// connection closed
			{
				ipcobj.close_sock(1);	// close for exit
#ifdef DEBUG
printf("Disconnect.\r\n");
fflush(stdout);
#endif
				ipcobj.do_connect();	// wait for connection
			}
			else	// append to output file
			{
				if (outfile = fopen(ERR_LOG_PATH,"a"), outfile != NULL)
				{
					time(&now);	// get current time
					timeinfo = localtime(&now);
					strftime(filestr,50,"%D %T: ",timeinfo); // get formatted time
					strcat(filestr,msg); // add message
					fprintf(outfile,"%s\n",filestr);// append to file
					fclose(outfile);	// close file so that buffers cleared
#ifdef DEBUG
printf("appended %s.\r\n",filestr);
fflush(stdout);
#endif
				}

				#ifdef DEBUG
				else
				{
					printf("Unable to open file.\r\n");
					fflush(stdout);
				}
				#endif
			}
		}
//		select(0,NULL,NULL,NULL,&waittime);// delay
	}
	ipcobj.close_sock(0);	// close for exit
}
	


#endif 

