Skip to content

Commit

Permalink
cleanup stderr redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
pjstevns committed Aug 11, 2013
1 parent 197347f commit 0fae0bc
Show file tree
Hide file tree
Showing 22 changed files with 50 additions and 60 deletions.
3 changes: 2 additions & 1 deletion src/dm_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ char configFile[PATH_MAX];
static GKeyFile *config_dict = NULL;
static int configured = 0;


void config_get_file(void)
{
strncpy(configFile, DEFAULT_CONFIG_FILE, sizeof(configFile)-1);
Expand Down Expand Up @@ -283,7 +284,7 @@ void SetTraceLevel(const char *service_name)
}
}

configure_debug(trace_syslog_int, trace_stderr_int);
configure_debug(service_name, trace_syslog_int, trace_stderr_int);
}

void GetDBParams(void)
Expand Down
19 changes: 18 additions & 1 deletion src/dm_debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ char *__progname = NULL;

char hostname[16];

#define THIS_MODULE "debug"

/* the debug variables */
static Trace_T TRACE_SYSLOG = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING; /* default: emerg, alert, crit, err, warning */
static Trace_T TRACE_STDERR = TRACE_EMERG | TRACE_ALERT | TRACE_CRIT | TRACE_ERR | TRACE_WARNING; /* default: emerg, alert, crit, err, warning */
Expand All @@ -46,10 +48,25 @@ void TabortHandler(const char *error)
/*
* configure the debug settings
*/
void configure_debug(Trace_T trace_syslog, Trace_T trace_stderr)
FILE *fstderr = NULL;

static void configure_stderr(const char *service_name)
{
Field_T error_log;
config_get_value("errorlog", service_name, error_log);
if (! (fstderr = freopen(error_log, "a", stderr))) {
int serr = errno;
TRACE(TRACE_ERR, "freopen failed on [%s] [%s]", error_log, strerror(serr));
}
}

void configure_debug(const char *service_name, Trace_T trace_syslog, Trace_T trace_stderr)
{
TRACE_SYSLOG = trace_syslog;
TRACE_STDERR = trace_stderr;

configure_stderr(service_name?service_name:"DBMAIL");

}

/* Make sure that these match Trace_T. */
Expand Down
2 changes: 1 addition & 1 deletion src/dm_debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ typedef enum {
void TabortHandler(const char *error);
void trace(Trace_T level, const char * module, const char * function, int line, const char *formatstring, ...) PRINTF_ARGS(5, 6);

void configure_debug(Trace_T trace_syslog, Trace_T trace_stderr);
void configure_debug(const char *service_name, Trace_T trace_syslog, Trace_T trace_stderr);

void null_logger(const char UNUSED *log_domain, GLogLevelFlags UNUSED log_level, const char UNUSED *message, gpointer UNUSED data);
#endif
37 changes: 8 additions & 29 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct event *pev = NULL;
SSL_CTX *tls_context;

FILE *fstdout = NULL;
FILE *fstderr = NULL;
extern FILE *fstderr;
FILE *fnull = NULL;
/*
* self-pipe event
Expand Down Expand Up @@ -310,7 +310,7 @@ int StartCliServer(ServerConfig_T * conf)
/* Should be called after a HUP to allow for log rotation,
* as the filesystem may want to give us new inodes and/or
* the user may have changed the log file configs. */
static void reopen_logs(ServerConfig_T *conf)
static void reopen_logs_level(ServerConfig_T *conf, Trace_T level)
{
int serr;

Expand All @@ -325,44 +325,23 @@ static void reopen_logs(ServerConfig_T *conf)

if (! (fstdout = freopen(conf->log, "a", stdout))) {
serr = errno;
TRACE(TRACE_ERR, "freopen failed on [%s] [%s]", conf->log, strerror(serr));
TRACE(level, "freopen failed on [%s] [%s]", conf->log, strerror(serr));
}

if (! (fstderr = freopen(conf->error_log, "a", stderr))) {
serr = errno;
TRACE(TRACE_ERR, "freopen failed on [%s] [%s]", conf->error_log, strerror(serr));
TRACE(level, "freopen failed on [%s] [%s]", conf->error_log, strerror(serr));
}

if (! (fnull = freopen("/dev/null", "r", stdin))) {
serr = errno;
TRACE(TRACE_ERR, "freopen failed on stdin [%s]", strerror(serr));
TRACE(level, "freopen failed on stdin [%s]", strerror(serr));
}

}

/* Should be called once to initially close the actual std{in,out,err}
* and open the redirection files. */
static void reopen_logs_fatal(ServerConfig_T *conf)
{
int serr;

if (fstdout) fclose(fstdout);
if (fstderr) fclose(fstderr);
if (fnull) fclose(fnull);

if (! (fstdout = freopen(conf->log, "a", stdout))) {
serr = errno;
TRACE(TRACE_EMERG, "freopen failed on [%s] [%s]", conf->log, strerror(serr));
}
if (! (fstdout = freopen(conf->error_log, "a", stderr))) {
serr = errno;
TRACE(TRACE_EMERG, "freopen failed on [%s] [%s]", conf->error_log, strerror(serr));
}
if (! (fstdout = freopen("/dev/null", "r", stdin))) {
serr = errno;
TRACE(TRACE_EMERG, "freopen failed on stdin [%s]", strerror(serr));
}
}
#define reopen_logs(a) reopen_logs_level(a, TRACE_ERR)
#define reopen_logs_fatal(a) reopen_logs_level(a, TRACE_EMERG)

pid_t server_daemonize(ServerConfig_T *conf)
{
Expand Down Expand Up @@ -989,7 +968,7 @@ void server_config_load(ServerConfig_T * config, const char * const service)
SetTraceLevel(service);
/* Override SetTraceLevel. */
if (config->log_verbose) {
configure_debug(5,5);
configure_debug(service,5,5);
}

config_get_value("max_db_connections", service, val);
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ static void ci_delete(ClientBase_T *ci)
void setup(void)
{
reallyquiet = 1;
configure_debug(511,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,511,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_capa.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ Mempool_T pool;

void setup(void)
{
configure_debug(255,31);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,31);
pool = mempool_open();
A = Capa_new(pool);
}
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ extern char *raw_lmtp_data;

void setup(void)
{
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
GetDBParams();
db_connect();
}
Expand Down
7 changes: 2 additions & 5 deletions test/check_dbmail_db.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,9 @@ char *domain_catchall = "@nonexistantdomain";
*/
void setup(void)
{
configure_debug(511,0);
config_get_file();
if (config_read(configFile) < 0) {
printf( "Config file not found: %s\n", configFile );
exit(1);
}
config_read(configFile);
configure_debug(NULL,511,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_deliver.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ void init_testuser1(void)

void setup(void)
{
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down
7 changes: 2 additions & 5 deletions test/check_dbmail_dsn.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,9 @@ char *domain_catchall = "@nonexistantdomain";
*/
void setup(void)
{
configure_debug(255,0);
config_get_file();
if (config_read(configFile) < 0) {
printf( "Config file not found: %s\n", configFile );
exit(1);
}
config_read(configFile);
configure_debug(NULL,255,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down
5 changes: 2 additions & 3 deletions test/check_dbmail_imapd.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ void init_testuser1(void)

void setup(void)
{
queue_pool = mempool_open();
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
GetDBParams();
db_connect();
auth_connect();
init_testuser1();
queue_pool = mempool_open();
}

void teardown(void)
Expand Down Expand Up @@ -184,7 +184,6 @@ START_TEST(test_imap_session_new)
s = dbmail_imap_session_new(pool);
fail_unless(s!=NULL, "Failed to initialize imapsession");
dbmail_imap_session_delete(&s);
mempool_close(&pool);
}
END_TEST

Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ Mempool_T pool;
void setup(void)
{
//reallyquiet = 1;
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
pool = mempool_open();
}

Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_mailbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ static uint64_t get_mailbox_id(const char *name)

void setup(void)
{
configure_debug(511,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,511,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_mailboxstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ static void insert_message(void)

void setup(void)
{
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_mempool.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ extern char configFile[PATH_MAX];
*/
void setup(void)
{
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
}

void teardown(void)
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ extern char *raw_lmtp_data;

void setup(void)
{
configure_debug(511,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,511,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ extern int reallyquiet;
void setup(void)
{
reallyquiet = 1;
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ int imap_before_smtp = 0;

void setup(void)
{
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
GetDBParams();
db_connect();
g_mime_init(GMIME_ENABLE_RFC2047_WORKAROUNDS);
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_sset.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ static int compare(const void *a, const void *b)

void setup(void)
{
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
V = Sset_new(compare, sizeof(struct item), NULL);
W = Sset_new(compare, sizeof(struct item), NULL);
}
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ Mempool_T pool;

void setup(void)
{
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
pool = mempool_open();
}

Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ extern int reallyquiet;
void setup(void)
{
reallyquiet = 1;
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down
2 changes: 1 addition & 1 deletion test/check_dbmail_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ extern char configFile[PATH_MAX];
*/
void setup(void)
{
configure_debug(255,0);
config_get_file();
config_read(configFile);
configure_debug(NULL,255,0);
GetDBParams();
db_connect();
auth_connect();
Expand Down

0 comments on commit 0fae0bc

Please sign in to comment.