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.
This commit is contained in:
Robert James Kaes 2002-04-18 16:57:06 +00:00
parent 6a588826c1
commit 5822ec3d44
6 changed files with 95 additions and 55 deletions

View File

@ -1,3 +1,12 @@
2002-04-18 Robert James Kaes <rjkaes@flarenet.com>
* src/utils.c (rotate_log_files): Moved the log rotation code out
of the signal handler and into it's own function. Also improved
the robustness of the code. Credit to Petr Lampa for suggesting
that system calls in a signal handler is bad magic. Now the
signal handler sets a flag which is responded to inside of
thread_main_loop().
2002-04-17 Robert James Kaes <rjkaes@flarenet.com>
* configure.ac: Added test for pthread_cancel() since it doesn't

View File

@ -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;
}
}
}

View File

@ -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)

View File

@ -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

View File

@ -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.");
}

View File

@ -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.