From c1dc0db682436da7b077e9cc35c5544c346c25e0 Mon Sep 17 00:00:00 2001 From: miker Date: Wed, 3 Aug 2005 16:49:11 +0000 Subject: [PATCH] daemonizing and hushing warnings; logger reopens files instead of writing blindly to an open FH git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@458 9efc2488-bf62-4759-914b-345cdb29e865 --- src/jserver/jserver-c_main.c | 13 ++-- src/jserver/jserver-c_session.h | 4 +- src/router/router.c | 4 ++ src/utils/logging.c | 140 +++++++++++++++++++++++++++------------- src/utils/utils.c | 21 +++++- src/utils/utils.h | 2 + 6 files changed, 132 insertions(+), 52 deletions(-) diff --git a/src/jserver/jserver-c_main.c b/src/jserver/jserver-c_main.c index ff97017..9bcd8cd 100644 --- a/src/jserver/jserver-c_main.c +++ b/src/jserver/jserver-c_main.c @@ -39,10 +39,6 @@ void sig_int_handler( int a ) { /* loads the command line settings and launches the server */ int main(int argc, char* argv[]) { - signal(SIGHUP, &sig_hup_handler); - signal(SIGINT, &sig_int_handler); - signal(SIGTERM, &sig_int_handler); - char* prog = argv[0]; char* sport = argv[1]; unix_sock_file = argv[2]; @@ -74,6 +70,15 @@ int main(int argc, char* argv[]) { fprintf(stderr, "Launching with port %d, unix sock %s, log level %d, log file %s\n", port, unix_sock_file, log_level, log_file ); + if (daemonize() == -1) { + fprintf(stderr, "!!! Error forking the daemon! Going away now... :(\n"); + exit(2); + } + + signal(SIGHUP, &sig_hup_handler); + signal(SIGINT, &sig_int_handler); + signal(SIGTERM, &sig_int_handler); + launch_server(); return 0; } diff --git a/src/jserver/jserver-c_session.h b/src/jserver/jserver-c_session.h index c4abea2..9761339 100644 --- a/src/jserver/jserver-c_session.h +++ b/src/jserver/jserver-c_session.h @@ -1,8 +1,10 @@ +#define _GNU_SOURCE + #include "utils.h" #include "logging.h" -#include #include +#include #include #include diff --git a/src/router/router.c b/src/router/router.c index c1ba8a9..9311670 100644 --- a/src/router/router.c +++ b/src/router/router.c @@ -80,7 +80,11 @@ int main( int argc, char* argv[] ) { free(con_timeout); free(max_retries); + daemonize(); + signal(SIGHUP,sig_hup_handler); + signal(SIGINT,sig_hup_handler); + signal(SIGTERM,sig_hup_handler); int counter = 0; diff --git a/src/utils/logging.c b/src/utils/logging.c index ebaaf25..53105b0 100644 --- a/src/utils/logging.c +++ b/src/utils/logging.c @@ -1,5 +1,7 @@ +#include #include "logging.h" + void get_timestamp( char buf_36chars[]) { struct timeb tb; @@ -14,14 +16,16 @@ void get_timestamp( char buf_36chars[]) { free(localtime); } -static FILE* log_file = NULL; +static char* lf = NULL; static int log_level = -1; static int logging = 0; -void log_free() { if( log_file != NULL ) fclose(log_file ); } +void log_free() { if( lf != NULL ) free(lf); } int fatal_handler( char* msg, ... ) { - + + FILE * log_file; + char buf[36]; memset( buf, 0, 36 ); get_timestamp( buf ); @@ -29,19 +33,25 @@ int fatal_handler( char* msg, ... ) { va_list args; if( logging ) { - if( log_level < LOG_ERROR ) return -1; - fprintf( log_file, "[%s %d] [%s] ", buf, pid, "ERR " ); + log_file = fopen( lf, "a" ); + if( log_file == NULL ) { + perror( "Unable to open log file for appending\n" ); + } else { + + fprintf( log_file, "[%s %d] [%s] ", buf, pid, "ERR " ); - va_start(args, msg); - vfprintf(log_file, msg, args); - va_end(args); + va_start(args, msg); + vfprintf(log_file, msg, args); + va_end(args); - fprintf(log_file, "\n"); - fflush( log_file ); + fprintf(log_file, "\n"); + fflush( log_file ); + fclose(log_file); + } } /* also log to stderr for ERRORS*/ @@ -57,26 +67,40 @@ int fatal_handler( char* msg, ... ) { int warning_handler( char* msg, ... ) { + FILE * log_file; + char buf[36]; memset( buf, 0, 36 ); get_timestamp( buf ); pid_t pid = getpid(); va_list args; - if(logging) { + if( log_level < LOG_WARNING ) + return -1; - if( log_level < LOG_WARNING ) - return -1; + if(logging) { - fprintf( log_file, "[%s %d] [%s] ", buf, pid, "WARN" ); + log_file = fopen( lf, "a" ); + if( log_file == NULL ) { + perror( "Unable to open log file for appending\n" ); + fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" ); + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + fprintf( stderr, "\n" ); + } else { + + fprintf( log_file, "[%s %d] [%s] ", buf, pid, "WARN" ); - va_start(args, msg); - vfprintf(log_file, msg, args); - va_end(args); + va_start(args, msg); + vfprintf(log_file, msg, args); + va_end(args); - fprintf(log_file, "\n"); - fflush( log_file ); + fprintf(log_file, "\n"); + fflush( log_file ); + fclose(log_file); + } } else { fprintf( stderr, "[%s %d] [%s] ", buf, pid, "WARN" ); @@ -91,25 +115,40 @@ int warning_handler( char* msg, ... ) { int info_handler( char* msg, ... ) { + FILE * log_file; + char buf[36]; memset( buf, 0, 36 ); get_timestamp( buf ); pid_t pid = getpid(); va_list args; - if(logging) { + if( log_level < LOG_INFO ) + return -1; - if( log_level < LOG_INFO ) - return -1; - fprintf( log_file, "[%s %d] [%s] ", buf, pid, "INFO" ); + if(logging) { - va_start(args, msg); - vfprintf(log_file, msg, args); - va_end(args); + log_file = fopen( lf, "a" ); + if( log_file == NULL ) { + perror( "Unable to open log file for appending\n" ); + fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" ); + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + fprintf( stderr, "\n" ); + fflush(stderr); + } else { + + fprintf( log_file, "[%s %d] [%s] ", buf, pid, "INFO" ); + + va_start(args, msg); + vfprintf(log_file, msg, args); + va_end(args); - fprintf(log_file, "\n"); - fflush( log_file ); - + fprintf(log_file, "\n"); + fflush( log_file ); + fclose(log_file); + } } else { fprintf( stderr, "[%s %d] [%s] ", buf, pid, "INFO" ); @@ -126,26 +165,39 @@ int info_handler( char* msg, ... ) { int debug_handler( char* msg, ... ) { + FILE * log_file; + char buf[36]; memset( buf, 0, 36 ); get_timestamp( buf ); pid_t pid = getpid(); va_list args; - if(logging) { + if( log_level < LOG_DEBUG ) + return -1; - if( log_level < LOG_DEBUG ) - return -1; + if(logging) { - fprintf( log_file, "[%s %d] [%s] ", buf, pid, "DEBG" ); + log_file = fopen( lf, "a" ); + if( log_file == NULL ) { + perror( "Unable to open log file for appending\n" ); + fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" ); + va_start(args, msg); + vfprintf(stderr, msg, args); + va_end(args); + fprintf( stderr, "\n" ); + } else { + fprintf( log_file, "[%s %d] [%s] ", buf, pid, "DEBG" ); - va_start(args, msg); - vfprintf(log_file, msg, args); - va_end(args); + va_start(args, msg); + vfprintf(log_file, msg, args); + va_end(args); - fprintf(log_file, "\n"); - fflush( log_file ); - + fprintf(log_file, "\n"); + fflush( log_file ); + + fclose(log_file); + } } else { fprintf( stderr, "[%s %d] [%s] ", buf, pid, "DEBG" ); @@ -167,16 +219,12 @@ int log_init( int llevel, char* lfile ) { return 0; } - log_level = llevel; - /* log to stderr */ if(lfile == NULL) return 0; - log_file = fopen( lfile, "a" ); - if( log_file == NULL ) { - fprintf( stderr, "Unable to open log file %s for appending\n", lfile ); - return 0; - } + log_level = llevel; + lf = strdup(lfile); + logging = 1; return 1; diff --git a/src/utils/utils.c b/src/utils/utils.c index 830861a..d4cb133 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -15,6 +15,8 @@ GNU General Public License for more details. */ #include + +#include #include #include #include @@ -300,4 +302,21 @@ char* uescape( const char* string, int size, int full_escape ) { buffer_free(buf); return d; } - + +// A function to turn a process into a daemon and set it's process name in ps/top +int daemonize() { + int f = fork(); + if (f == -1) { + perror("Failed to fork!"); + return -1; + } else if (f == 0) { + // We're in the child now... + setsid(); + return 0; + } else { + // We're in the parent... + exit(0); + } +} + + diff --git a/src/utils/utils.h b/src/utils/utils.h index 1a205fd..35c15d9 100644 --- a/src/utils/utils.h +++ b/src/utils/utils.h @@ -26,6 +26,8 @@ GNU General Public License for more details. #define BUFFER_MAX_SIZE 10485760 +int daemonize(); + void* safe_malloc(int size); // --------------------------------------------------------------------------------- -- 2.11.0