diff options
Diffstat (limited to 'daemon/syslogd.c')
-rw-r--r-- | daemon/syslogd.c | 132 |
1 files changed, 71 insertions, 61 deletions
diff --git a/daemon/syslogd.c b/daemon/syslogd.c index dc947b3..da53e60 100644 --- a/daemon/syslogd.c +++ b/daemon/syslogd.c @@ -38,7 +38,7 @@ static struct source** internal_source_references = NULL; struct hostname { struct sockaddr_in addr; - gchar *host; + struct string *host; time_t top_age; /* zero prevents aging */ }; static GList *hostnames = NULL; @@ -55,35 +55,37 @@ char *str_month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", * create_message * * Create a new message with refcount=1. - * The caller should allocate sender, hostname, program and message strings. - * The function simply copies these pointers to the structure and the caller - * should not free allocated strings. */ struct message* create_message( struct source* source, - gchar* sender, + struct string* sender, int facility, int priority, LPSYSTEMTIME timestamp, - gchar* hostname, - gchar* program, - gchar* message ) + struct string* hostname, + struct string* program, + struct string* message ) { struct message *msg; + gchar *ts; TRACE_ENTER( "\n" ); msg = g_malloc( sizeof(struct message) ); msg->refcount = 1; msg->source = source; - msg->sender = sender; + msg->sender = string_addref( sender ); msg->facility = facility; msg->priority = priority; - msg->timestamp = g_strdup_printf( "%s %2d %02d:%02d:%02d", - str_month[ timestamp->wMonth - 1 ], - timestamp->wDay, timestamp->wHour, - timestamp->wMinute, timestamp->wSecond ); - msg->hostname = hostname; - msg->program = program; - msg->message = message; + + ts = g_strdup_printf( "%s %2d %02d:%02d:%02d", + str_month[ timestamp->wMonth - 1 ], + timestamp->wDay, timestamp->wHour, + timestamp->wMinute, timestamp->wSecond ); + msg->timestamp = string_new( ts ); + g_free( ts ); + + msg->hostname = string_addref( hostname ); + msg->program = string_addref( program ); + msg->message = string_addref( message ); TRACE_LEAVE( "message=%p\n", msg ); return msg; @@ -101,15 +103,15 @@ struct message* duplicate_message( struct message* msg ) TRACE_ENTER( "message=%p\n", msg ); new_msg = g_malloc( sizeof(struct message) ); - new_msg->refcount = 1; - new_msg->source = msg->source; - new_msg->sender = g_strdup( msg->sender ); - new_msg->facility = msg->facility; - new_msg->priority = msg->priority; - new_msg->timestamp = g_strdup( msg->timestamp ); - new_msg->hostname = g_strdup( msg->hostname ); - new_msg->program = g_strdup( msg->program ); - new_msg->message = g_strdup( msg->message ); + new_msg->refcount = 1; + new_msg->source = msg->source; + new_msg->sender = string_addref( msg->sender ); + new_msg->facility = msg->facility; + new_msg->priority = msg->priority; + new_msg->timestamp = string_addref( msg->timestamp ); + new_msg->hostname = string_addref( msg->hostname ); + new_msg->program = string_addref( msg->program ); + new_msg->message = string_addref( msg->message ); TRACE_LEAVE( "new message=%p\n", new_msg ); return new_msg; @@ -139,11 +141,11 @@ void release_message( struct message* msg ) TRACE_LEAVE( "done; still referenced\n" ); return; } - g_free( msg->sender ); - g_free( msg->timestamp ); - g_free( msg->hostname ); - g_free( msg->program ); - g_free( msg->message ); + string_release( msg->sender ); + string_release( msg->timestamp ); + string_release( msg->hostname ); + string_release( msg->program ); + string_release( msg->message ); g_free( msg ); TRACE_LEAVE( "done\n" ); } @@ -163,7 +165,7 @@ static void convert_message_encoding( struct message* msg ) return; } - converted_msg = g_convert_with_iconv( msg->message, -1, + converted_msg = g_convert_with_iconv( msg->message->gstr->str, -1, conversion_descriptor, NULL, NULL, NULL ); if( !converted_msg ) { @@ -171,10 +173,11 @@ static void convert_message_encoding( struct message* msg ) return; } - g_free( msg->message ); - msg->message = converted_msg; + string_release( msg->message ); + msg->message = string_new( converted_msg ); + g_free( converted_msg ); - TRACE_LEAVE( "done; %s\n", msg->message ); + TRACE_LEAVE( "done; %s\n", msg->message->gstr->str ); } /****************************************************************************** @@ -243,14 +246,16 @@ static void mux_message( struct message* msg ) /****************************************************************************** * get_hostname * - * convert addr to string and return it + * convert addr to string and return it; + * string should be released after use */ -static gchar* get_hostname( struct sockaddr_in* addr ) +static struct string* get_hostname( struct sockaddr_in* addr ) { - gchar *ret; + struct string *ret; time_t current_time; GList *item; struct hostname *new_hostname; + char buf[16]; TRACE_ENTER( "%d.%d.%d.%d\n", addr->sin_addr.S_un.S_un_b.s_b1, addr->sin_addr.S_un.S_un_b.s_b2, @@ -268,7 +273,7 @@ static gchar* get_hostname( struct sockaddr_in* addr ) GList *next_item = item->next; TRACE_2( "delete old entry %s\n", h->host ); - g_free( h->host ); + string_release( h->host ); g_free( h ); hostnames = g_list_delete_link( hostnames, item ); item = next_item; @@ -277,7 +282,7 @@ static gchar* get_hostname( struct sockaddr_in* addr ) if( h->addr.sin_addr.S_un.S_addr == addr->sin_addr.S_un.S_addr ) { /* found in cache */ - ret = g_strdup( h->host ); + ret = string_addref( h->host ); /* move entry to the beginning of the list */ item->data = hostnames->data; hostnames->data = h; @@ -296,19 +301,19 @@ static gchar* get_hostname( struct sockaddr_in* addr ) new_hostname->top_age = time(NULL) + HOSTNAME_LIFETIME; if( !he ) goto use_addr; - new_hostname->host = g_strdup( he->h_name ); + new_hostname->host = string_new( he->h_name ); } else { new_hostname->top_age = 0; use_addr: - new_hostname->host = g_malloc( 16 ); - sprintf( new_hostname->host, "%d.%d.%d.%d", + sprintf( buf, "%d.%d.%d.%d", addr->sin_addr.S_un.S_un_b.s_b1, addr->sin_addr.S_un.S_un_b.s_b2, addr->sin_addr.S_un.S_un_b.s_b3, addr->sin_addr.S_un.S_un_b.s_b4 ); + new_hostname->host = string_new( buf ); } hostnames = g_list_prepend( hostnames, new_hostname ); - ret = g_strdup( new_hostname->host ); + ret = string_addref( new_hostname->host ); TRACE_LEAVE( "done; ret=%s\n", ret ); return ret; } @@ -322,7 +327,7 @@ static void free_hostnames() for( item = hostnames; item; item = item->next ) { struct hostname *h = item->data; - g_free( h->host ); + string_release( h->host ); g_free( h ); } g_list_free( hostnames ); @@ -455,7 +460,7 @@ done: static struct message* parse_raw_message( struct raw_message* raw_msg ) { gchar *current_part, *next_part; - gchar *sender, *hostname, *program, *message; + struct string *sender, *hostname, *program, *message; int facility, priority; SYSTEMTIME timestamp; struct message *msg; @@ -474,8 +479,8 @@ static struct message* parse_raw_message( struct raw_message* raw_msg ) { /* no valid timestamp */ TRACE_2( "no valid timestamp: msg=%s\n", current_part ); - hostname = g_strdup( sender ); - message = g_strdup( current_part ); + hostname = string_addref( sender ); + message = string_new( current_part ); } else { @@ -486,33 +491,38 @@ static struct message* parse_raw_message( struct raw_message* raw_msg ) if( *next_part != ' ' ) { /* invalid hostname */ - hostname = g_strdup( sender ); - message = g_strdup( current_part ); - TRACE_2( "invalid hostname; set sender (%s); msg=%s\n", hostname, message ); + hostname = string_addref( sender ); + message = string_new( current_part ); + TRACE_2( "invalid hostname; set sender (%s); msg=%s\n", + hostname->gstr->str, message->gstr->str ); } else { - hostname = g_strndup( current_part, next_part - current_part ); + hostname = string_new_len( current_part, next_part - current_part ); while( *next_part == ' ' && *next_part != 0 ) next_part++; - message = g_strdup( next_part ); - TRACE_2( "hostname=%s; msg=%s\n", hostname, message ); + message = string_new( next_part ); + TRACE_2( "hostname=%s; msg=%s\n", hostname->gstr->str, message->gstr->str ); } } /* try to find program name */ - current_part = message; + current_part = message->gstr->str; next_part = current_part; while( *next_part != ' ' && *next_part != ':' && *next_part != '[' && *next_part != 0 ) next_part++; if( *next_part == ' ' || *next_part == 0 ) - program = g_strdup(""); + program = string_new(""); else - program = g_strndup( current_part, next_part - current_part ); + program = string_new_len( current_part, next_part - current_part ); /* create message */ msg = create_message( raw_msg->source, sender, facility, priority, ×tamp, hostname, program, message ); + string_release( sender ); + string_release( hostname ); + string_release( program ); + string_release( message ); /* destroy raw message */ g_free( raw_msg->msg ); @@ -551,7 +561,7 @@ unsigned number_of_sources( enum source_type type ) */ void log_internal( int pri, char* fmt, ... ) { - gchar *sender, *hostname, *program, *msg; + struct string *sender, *hostname, *program, *msg; va_list args; SYSTEMTIME stm; int i; @@ -562,12 +572,12 @@ void log_internal( int pri, char* fmt, ... ) if( 0 == internal_source_count ) goto done; - sender = g_strdup( local_hostname ); - hostname = g_strdup( local_hostname ); - program = g_strdup( "syslog" ); + sender = string_addref( local_hostname ); + hostname = string_addref( local_hostname ); + program = string_addref( self_program_name ); va_start( args, fmt ); - msg = g_strdup_vprintf( fmt, args ); + msg = string_vprintf( fmt, args ); va_end( args ); GetLocalTime( &stm ); |