From 3f53c691d248434fdb817e615495d9c419af07d1 Mon Sep 17 00:00:00 2001 From: Serge Hallyn Date: Mon, 15 Feb 2016 12:15:10 -0800 Subject: [PATCH 1/2] log.c:__lxc_log_set_file: fname cannot be null fname cannot be passed in as NULL by any of its current callers. If it could, then build_dir() would crash as it doesn't check for it. So make sure we are warned if in the future we pass in NULL. Signed-off-by: Serge Hallyn --- src/lxc/log.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lxc/log.c b/src/lxc/log.c index 20be5ac07..babaebc84 100644 --- a/src/lxc/log.c +++ b/src/lxc/log.c @@ -20,6 +20,7 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include #include @@ -268,7 +269,9 @@ static int __lxc_log_set_file(const char *fname, int create_dirs) free(log_fname); } - if (!fname || strlen(fname) == 0) { + assert(fname != NULL); + + if (strlen(fname) == 0) { log_fname = NULL; return 0; } From d9c9b1808af250ec37379c7f0242941a450aa3af Mon Sep 17 00:00:00 2001 From: Serge Hallyn Date: Mon, 15 Feb 2016 12:18:18 -0800 Subject: [PATCH 2/2] log.c:__lxc_log_set_file: completely close log file when overriding Otherwise after a shortcut on error we could end up trying to write to the closed log fd. Signed-off-by: Serge Hallyn --- src/lxc/log.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/lxc/log.c b/src/lxc/log.c index babaebc84..55fa7f5b9 100644 --- a/src/lxc/log.c +++ b/src/lxc/log.c @@ -253,6 +253,16 @@ static char *build_log_path(const char *name, const char *lxcpath) return p; } +extern void lxc_log_close(void) +{ + if (lxc_log_fd == -1) + return; + close(lxc_log_fd); + lxc_log_fd = -1; + free(log_fname); + log_fname = NULL; +} + /* * This can be called: * 1. when a program calls lxc_log_init with no logfile parameter (in which @@ -265,8 +275,7 @@ static int __lxc_log_set_file(const char *fname, int create_dirs) { if (lxc_log_fd != -1) { // we are overriding the default. - close(lxc_log_fd); - free(log_fname); + lxc_log_close(); } assert(fname != NULL); @@ -383,16 +392,6 @@ extern int lxc_log_init(const char *name, const char *file, return ret; } -extern void lxc_log_close(void) -{ - if (lxc_log_fd == -1) - return; - close(lxc_log_fd); - lxc_log_fd = -1; - free(log_fname); - log_fname = NULL; -} - /* * This is called when we read a lxc.loglevel entry in a lxc.conf file. This * happens after processing command line arguments, which override the .conf