diff options
author | Robert James Kaes <rjkaes@users.sourceforge.net> | 2002-04-18 16:57:06 +0000 |
---|---|---|
committer | Robert James Kaes <rjkaes@users.sourceforge.net> | 2002-04-18 16:57:06 +0000 |
commit | 5822ec3d44aa6ad2261d9e3ce07d589a7e6a3dcb (patch) | |
tree | 6367565772fd4a626c328e7f396113c213bd8eb3 /src | |
parent | 6a588826c1411d3ea259a83f96f6744b20de65d6 (diff) |
Moved the log rotation code out of the signal handler and into it's own
function. The signal handler now simply sets a flag which is monitored
inside the thread_main_loop() function. The log rotation code has also
been tightened to handle any error conditions better. Credit to Petr
Lampa for suggesting that system functions inside of a signal handler is
bad magic.
Diffstat (limited to 'src')
-rw-r--r-- | src/thread.c | 8 | ||||
-rw-r--r-- | src/tinyproxy.c | 54 | ||||
-rw-r--r-- | src/tinyproxy.h | 5 | ||||
-rw-r--r-- | src/utils.c | 71 | ||||
-rw-r--r-- | src/utils.h | 3 |
5 files changed, 86 insertions, 55 deletions
diff --git a/src/thread.c b/src/thread.c index 163f7d3..86c586f 100644 --- a/src/thread.c +++ b/src/thread.c @@ -1,4 +1,4 @@ -/* $Id: thread.c,v 1.24 2002-04-17 20:54:26 rjkaes Exp $ +/* $Id: thread.c,v 1.25 2002-04-18 16:57:06 rjkaes Exp $ * * Handles the creation/destruction of the various threads required for * processing incoming connections. @@ -321,6 +321,12 @@ thread_main_loop(void) SERVER_UNLOCK(); sleep(5); + + /* Handle log rotation if it was requested */ + if (log_rotation_request) { + rotate_log_files(); + log_rotation_request = FALSE; + } } } diff --git a/src/tinyproxy.c b/src/tinyproxy.c index ff498b0..c9966dd 100644 --- a/src/tinyproxy.c +++ b/src/tinyproxy.c @@ -1,4 +1,4 @@ -/* $Id: tinyproxy.c,v 1.24 2002-04-08 21:35:10 rjkaes Exp $ +/* $Id: tinyproxy.c,v 1.25 2002-04-18 16:57:06 rjkaes Exp $ * * The initialise routine. Basically sets up all the initial stuff (logfile, * listening socket, config options, etc.) and then sits there and loops @@ -44,6 +44,7 @@ extern FILE *yyin; */ struct config_s config; float load = 0.00; +bool_t log_rotation_request = FALSE; /* * Handle a signal @@ -53,56 +54,9 @@ takesig(int sig) { switch (sig) { case SIGHUP: - log_message(LOG_NOTICE, "SIGHUP received, cleaning up."); - - if (config.logf) { - char *rename_file; - int log_file_des; - FILE *old_fd; - - rename_file = safemalloc(strlen(config.logf_name) + 5); - if (!rename_file) { - fprintf(stderr, - "Could not allocate memory in signal handler!\n"); - exit(EX_OSERR); - } - - strcpy(rename_file, config.logf_name); - strcat(rename_file, ".rot"); - - rename(config.logf_name, rename_file); - - log_file_des = create_file_safely(config.logf_name); - if (log_file_des < 0) { - fprintf(stderr, - "Could not safely create new log file.\n"); - exit(EX_OSERR); - } - - old_fd = config.logf; - - if (!(config.logf = fdopen(log_file_des, "w"))) { - fprintf(stderr, - "Could not create new log file.\n"); - exit(EX_CANTCREAT); - } - - fclose(old_fd); - - log_message(LOG_NOTICE, "Log file rotated."); - - safefree(rename_file); - } -#ifdef FILTER_ENABLE - if (config.filter) { - filter_destroy(); - filter_init(); - } - log_message(LOG_NOTICE, "Re-reading filter file."); -#endif /* FILTER_ENABLE */ - log_message(LOG_NOTICE, - "Finished cleaning memory/connections."); + log_rotation_request = TRUE; break; + case SIGTERM: #ifdef FILTER_ENABLE if (config.filter) diff --git a/src/tinyproxy.h b/src/tinyproxy.h index dfbceea..8414612 100644 --- a/src/tinyproxy.h +++ b/src/tinyproxy.h @@ -1,4 +1,4 @@ -/* $Id: tinyproxy.h,v 1.27 2002-04-09 22:02:05 rjkaes Exp $ +/* $Id: tinyproxy.h,v 1.28 2002-04-18 16:57:06 rjkaes Exp $ * * See 'tinyproxy.c' for a detailed description. * @@ -184,7 +184,7 @@ /* Make a new type: bool_t */ typedef enum { FALSE = 0, - TRUE = (!FALSE) + TRUE = 1 } bool_t; struct config_s { @@ -218,5 +218,6 @@ struct config_s { /* Global Structures used in the program */ extern struct config_s config; +extern bool_t log_rotation_request; #endif diff --git a/src/utils.c b/src/utils.c index ec6d551..8ad876d 100644 --- a/src/utils.c +++ b/src/utils.c @@ -1,4 +1,4 @@ -/* $Id: utils.c,v 1.25 2002-04-16 03:22:16 rjkaes Exp $ +/* $Id: utils.c,v 1.26 2002-04-18 16:57:06 rjkaes Exp $ * * Misc. routines which are used by the various functions to handle strings * and memory allocation and pretty much anything else we can think of. Also, @@ -23,6 +23,7 @@ #include "buffer.h" #include "conns.h" +#include "filter.h" #include "log.h" #include "sock.h" #include "utils.h" @@ -403,3 +404,71 @@ chomp(char *buffer, size_t length) return chars; } + +/* + * Rotate the current log file. This is performed whenever a SIGHUP is + * received. + */ +void +rotate_log_files(void) +{ + char* rename_file; + int log_file_des; + FILE *old_fd; + FILE* new_fd; + + log_message(LOG_NOTICE, "SIGHUP received, cleaning up."); + +#ifdef FILTER_ENABLE + if (config.filter) { + filter_destroy(); + filter_init(); + } + log_message(LOG_NOTICE, "Re-reading filter file."); +#endif /* FILTER_ENABLE */ + + if (config.logf) { + rename_file = safemalloc(strlen(config.logf_name) + 5); + if (!rename_file) { + log_message(LOG_CRIT, + "Could not allocate memory when trying to rotate the log file; therefore, the log has not been rotated."); + return; + } + + strcpy(rename_file, config.logf_name); + strcat(rename_file, ".rot"); + + rename(config.logf_name, rename_file); + + log_file_des = create_file_safely(config.logf_name); + if (log_file_des < 0) { + log_message(LOG_CRIT, + "Could not properly create the new log file; therefore the log has not been rotated."); + + /* Switch the file name back */ + rename(rename_file, config.logf_name); + safefree(rename_file); + return; + } + + old_fd = config.logf; + if (!(new_fd = fdopen(log_file_des, "w"))) { + log_message(LOG_CRIT, + "Could not create the new log file; therefore, the log has not been rotated."); + + /* Switch the file name back */ + rename(rename_file, config.logf_name); + safefree(rename_file); + } + + config.logf = new_fd; + fclose(old_fd); + + log_message(LOG_NOTICE, "Log file rotated."); + + safefree(rename_file); + } + + log_message(LOG_NOTICE, + "Finished cleaning memory/connections."); +} diff --git a/src/utils.h b/src/utils.h index 1e19bad..a24b23a 100644 --- a/src/utils.h +++ b/src/utils.h @@ -1,4 +1,4 @@ -/* $Id: utils.h,v 1.14 2002-04-15 02:07:27 rjkaes Exp $ +/* $Id: utils.h,v 1.15 2002-04-18 16:57:06 rjkaes Exp $ * * See 'utils.h' for a detailed description. * @@ -49,6 +49,7 @@ extern size_t strlcpy(char *dst, const char *src, size_t size); #endif /* HAVE_STRLCPY */ extern size_t chomp(char *buffer, size_t length); +extern void rotate_log_files(void); /* * The following is to allow for better memory checking. |