Skip to content

Commit

Permalink
Release logging related resources when mce makes an orderly exit
Browse files Browse the repository at this point in the history
While closing logging is not necessary, doing it reduces noise during
memory leak debugging. In the code the logging cleanup was made on
abnormal exit paths, but omitted when making an orderly exit.

Remove unnecessary mce_log_close() calls from abnormal exit paths.

Perform logging cleanup actions on normal exit.
  • Loading branch information
spiiroin committed Aug 31, 2014
1 parent deac54f commit 7e7ac38
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 18 deletions.
18 changes: 14 additions & 4 deletions mce-log.c
Expand Up @@ -36,6 +36,15 @@ static unsigned int logverbosity = LL_WARN; /**< Log verbosity */
static int logtype = MCE_LOG_STDERR; /**< Output for log messages */
static char *logname = NULL;

/** Get process identity to use for logging
*
* Will default to "mce" before mce_log_open() and after mce_log_close().
*/
static const char *mce_log_name(void)
{
return logname ?: "mce";
}

/** Get monotonic time as struct timeval */
static void monotime(struct timeval *tv)
{
Expand All @@ -54,7 +63,7 @@ static void timestamp(struct timeval *tv)
if( diff.tv_sec >= 4 ) {
timersub(tv, &start, &diff);
fprintf(stderr, "%s: T+%ld.%03ld %s\n\n",
logname,
mce_log_name(),
(long)diff.tv_sec, (long)(diff.tv_usec/1000),
"END OF BURST");
start = *tv;
Expand Down Expand Up @@ -179,7 +188,7 @@ void mce_log_file(loglevel_t loglevel, const char *const file,
struct timeval tv;
timestamp(&tv);
fprintf(stderr, "%s: T+%ld.%03ld %s: %s\n",
logname,
mce_log_name(),
(long)tv.tv_sec, (long)(tv.tv_usec/1000),
mce_log_level_tag(loglevel),
msg);
Expand Down Expand Up @@ -214,20 +223,21 @@ void mce_log_set_verbosity(const int verbosity)
void mce_log_open(const char *const name, const int facility, const int type)
{
logtype = type;
logname = g_strdup(name);

if (logtype == MCE_LOG_SYSLOG)
openlog(name, LOG_PID | LOG_NDELAY, facility);
else
logname = g_strdup(name);
}

/**
* Close log
*/
void mce_log_close(void)
{
/* Logging (to stderr) after this will use default identity */
g_free(logname), logname = 0;

/* Any further syslog() calls will automatically reopen */
if (logtype == MCE_LOG_SYSLOG)
closelog();
}
Expand Down
16 changes: 2 additions & 14 deletions mce.c
Expand Up @@ -443,7 +443,6 @@ static gboolean daemonize(void)
/* Parent - Failure */
mce_log(LL_CRIT, "daemonize: fork failed: %s",
g_strerror(errno));
mce_log_close();
exit(EXIT_FAILURE);

case 0:
Expand Down Expand Up @@ -471,7 +470,6 @@ static gboolean daemonize(void)
mce_log(LL_CRIT,
"close() was interrupted more than "
"10 times. Exiting.");
mce_log_close();
exit(EXIT_FAILURE);
}

Expand All @@ -489,7 +487,6 @@ static gboolean daemonize(void)
"Failed to close() fd %d; %s. "
"Exiting.",
i + 1, g_strerror(errno));
mce_log_close();
exit(EXIT_FAILURE);
}
} else {
Expand All @@ -501,23 +498,20 @@ static gboolean daemonize(void)
mce_log(LL_CRIT,
"Cannot open `/dev/null'; %s. Exiting.",
g_strerror(errno));
mce_log_close();
exit(EXIT_FAILURE);
}

if ((dup(i) == -1)) {
mce_log(LL_CRIT,
"Failed to dup() `/dev/null'; %s. Exiting.",
g_strerror(errno));
mce_log_close();
exit(EXIT_FAILURE);
}

if ((dup(i) == -1)) {
mce_log(LL_CRIT,
"Failed to dup() `/dev/null'; %s. Exiting.",
g_strerror(errno));
mce_log_close();
exit(EXIT_FAILURE);
}

Expand All @@ -529,7 +523,6 @@ static gboolean daemonize(void)
mce_log(LL_CRIT,
"Failed to chdir() to `/tmp'; %s. Exiting.",
g_strerror(errno));
mce_log_close();
exit(EXIT_FAILURE);
}

Expand All @@ -538,13 +531,11 @@ static gboolean daemonize(void)
mce_log(LL_CRIT,
"Cannot open lockfile; %s. Exiting.",
g_strerror(errno));
mce_log_close();
exit(EXIT_FAILURE);
}

if (lockf(i, F_TLOCK, 0) == -1) {
mce_log(LL_CRIT, "Already running. Exiting.");
mce_log_close();
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -955,7 +946,6 @@ int main(int argc, char **argv)
if( !mce_dbus_init(mce_args.systembus) ) {
mce_log(LL_CRIT,
"Failed to initialise D-Bus");
mce_log_close();
exit(EXIT_FAILURE);
}

Expand All @@ -965,7 +955,6 @@ int main(int argc, char **argv)
if (mce_gconf_init() == FALSE) {
mce_log(LL_CRIT,
"Cannot connect to default GConf engine");
mce_log_close();
exit(EXIT_FAILURE);
}

Expand Down Expand Up @@ -1076,9 +1065,8 @@ int main(int argc, char **argv)
/* Log a farewell message and close the log */
mce_log(LL_INFO, "Exiting...");

/* We do not need to explicitly close the log and doing so
* would not allow logging from atexit handlers */
//mce_log_close();
/* No more logging expected */
mce_log_close();

return status;
}

0 comments on commit 7e7ac38

Please sign in to comment.