Logging through syslog, preliminary.

This commit is contained in:
simon 1997-10-15 11:26:48 +00:00
parent 7b796e8c92
commit efa714241a
3 changed files with 47 additions and 17 deletions

28
gild.c
View file

@ -15,6 +15,8 @@
* * * *
\***********************************************************************/ \***********************************************************************/
/* $Header$ */
#include "gild.h" #include "gild.h"
int port = DEFAULT_PORT_NO; /* the port I shall listen on */ int port = DEFAULT_PORT_NO; /* the port I shall listen on */
@ -27,11 +29,11 @@ FILE * logstream = stderr; /* where I stick log messages */
void error( int severity) void error( int severity)
/* log the current contents of errorBuff and then if severity is bad die */ /* log the current contents of errorBuff and then if severity is bad die */
{ {
fprintf( logstream, "%s\n", errorBuff); log( severity, errorBuff);
switch ( severity) switch ( severity)
{ {
case FATAL_ERROR: case LOG_ERR:
exit( 1); exit( 1);
break; break;
} }
@ -48,7 +50,7 @@ int main( int argc, char * argv[])
/* the address I bind the keyhole to */ /* the address I bind the keyhole to */
sprintf( errorBuff, "GILD starting..."); sprintf( errorBuff, "GILD starting...");
error( NOTICE); error( LOG_NOTICE);
for ( arg = 1; argc > arg; arg ++) for ( arg = 1; argc > arg; arg ++)
{ /* process arguments */ { /* process arguments */
@ -69,7 +71,7 @@ int main( int argc, char * argv[])
sprintf( errorBuff, sprintf( errorBuff,
"unrecognised command line switch [-%c]", "unrecognised command line switch [-%c]",
argv[ arg][ 1]); argv[ arg][ 1]);
error( FATAL_ERROR); error( LOG_ERR);
break; break;
} }
default: default:
@ -81,14 +83,14 @@ int main( int argc, char * argv[])
if ( parse_config( configPath) == 0) if ( parse_config( configPath) == 0)
{ {
sprintf( errorBuff, "failed to load any handlers"); sprintf( errorBuff, "failed to load any handlers");
error( FATAL_ERROR); error( LOG_ERR);
} }
keyhole = socket( AF_INET, SOCK_STREAM, 0); keyhole = socket( AF_INET, SOCK_STREAM, 0);
if ( keyhole == -1) if ( keyhole == -1)
{ {
sprintf( errorBuff, "failed to intialise socket"); sprintf( errorBuff, "failed to intialise socket");
error( FATAL_ERROR); error( LOG_ERR);
} }
memset( address, 0, sizeof( address)); memset( address, 0, sizeof( address));
@ -107,7 +109,7 @@ int main( int argc, char * argv[])
sizeof( struct sockaddr_in)) == -1) sizeof( struct sockaddr_in)) == -1)
{ /* attempt to bind keyhole to address */ { /* attempt to bind keyhole to address */
sprintf( errorBuff, "failed to bind socket"); sprintf( errorBuff, "failed to bind socket");
error( FATAL_ERROR); error( LOG_ERR);
} }
#ifndef DEBUG #ifndef DEBUG
@ -120,10 +122,10 @@ int main( int argc, char * argv[])
if ( listen( keyhole, MAX_PENDING_REQUESTS) == -1) if ( listen( keyhole, MAX_PENDING_REQUESTS) == -1)
{ {
sprintf( errorBuff, "failed in listen()?"); sprintf( errorBuff, "failed in listen()?");
error( FATAL_ERROR); error( LOG_ERR);
} }
sprintf( errorBuff, "GILD: awaiting requests on port %d", port); sprintf( errorBuff, "GILD: awaiting requests on port %d", port);
error( NOTICE); error( LOG_NOTICE);
for ever for ever
{ {
@ -137,7 +139,7 @@ int main( int argc, char * argv[])
if ( client == 0) if ( client == 0)
{ /* unlikely, but might as well check... */ { /* unlikely, but might as well check... */
sprintf( errorBuff, "Out of memory?"); sprintf( errorBuff, "Out of memory?");
error( FATAL_ERROR); error( LOG_ERR);
} }
memset( client, 0, sizeof( client)); memset( client, 0, sizeof( client));
/* sanitation */ /* sanitation */
@ -149,17 +151,17 @@ int main( int argc, char * argv[])
{ /* again, check we set it up OK */ { /* again, check we set it up OK */
sprintf( errorBuff, "Could not establish conversation [%d]", sprintf( errorBuff, "Could not establish conversation [%d]",
errno); errno);
error( FATAL_ERROR); error( LOG_ERR);
} }
sprintf( errorBuff, "Connection received"); sprintf( errorBuff, "Connection received");
error( NOTICE); error( LOG_NOTICE);
switch( fork()) switch( fork())
{ {
case -1: /* Blew it: whinge and die */ case -1: /* Blew it: whinge and die */
sprintf( errorBuff, "failed to fork?"); sprintf( errorBuff, "failed to fork?");
error( FATAL_ERROR); error( LOG_ERR);
break; break;
case 0: /* I'm the child */ case 0: /* I'm the child */
close( keyhole); close( keyhole);

28
log.c Normal file
View file

@ -0,0 +1,28 @@
/***********************************************************************\
* *
* Project: Gild *
* log.c *
* *
* Purpose: Interface to syslogd. Why do our own logging, *
* which only adds to sysadmin hassle and confusion, *
* when we can use the system log? *
* *
* Author : Simon Brooke *
* Copyright: (c) Simon Brooke 1997 *
* Version : 0.1 *
* Created : 15th October 1997 *
* *
\***********************************************************************/
/* $Header$ */
#include "gild.h"
int log( int level, char *message)
/* hand this message over to the syslog daemon for recording */
{
openlog( GILD_ID, 0, LOG_DAEMON);
syslog( level, message);
closelog);
}

View file

@ -42,7 +42,7 @@ void wrapper( int conversation)
sprintf( errorBuff, sprintf( errorBuff,
"failed to duplicate conversation [%d] onto stdin: %s", "failed to duplicate conversation [%d] onto stdin: %s",
conversation, strerror( errno)); conversation, strerror( errno));
error( FATAL_ERROR); error( LOG_ERR);
} }
if ( dup2( conversation, STDOUT_FILENO) == -1) if ( dup2( conversation, STDOUT_FILENO) == -1)
@ -50,15 +50,15 @@ void wrapper( int conversation)
sprintf( errorBuff, sprintf( errorBuff,
"failed to duplicate conversation [%d] onto stdout: %s", "failed to duplicate conversation [%d] onto stdout: %s",
conversation, strerror( errno)); conversation, strerror( errno));
error( FATAL_ERROR); error( LOG_ERR);
} }
command = get_handler_command( firstln); command = get_handler_command( firstln);
/* and find the appropriate handler */ /* and find the appropriate handler */
if ( ! command) /* didn't find one */ if ( ! command) /* didn't find one */
{ {
sprintf( errorBuff, "no handler registered for %s", firstln); sprintf( errorBuff, "no handler registered for %s", firstln);
error( FATAL_ERROR); error( LOG_ERR);
} }
else /* did find one */ else /* did find one */
{ {