[lxc-devel] [PATCH] logging: Add lxc_log_options_no_override function

Stéphane Graber stgraber at ubuntu.com
Tue Feb 4 19:46:41 UTC 2014


In current LXC, loglevel and logfile are write-once functions.
That behaviour was appropriate when those two were first introduced
(pre-API) but with current API, one would expect to be able to
set_config_item those multiple times.

So instead, introduce lxc_log_options_no_override which when called
turns those two config keys read-only and have all existing binaries
which use log_init call that function once they're done setting the
value requested by the user.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 src/lxc/confile.c              |  4 ----
 src/lxc/log.c                  | 20 ++++++++++++++------
 src/lxc/log.h                  |  1 +
 src/lxc/lxc_attach.c           |  1 +
 src/lxc/lxc_cgroup.c           |  1 +
 src/lxc/lxc_console.c          |  1 +
 src/lxc/lxc_create.c           |  1 +
 src/lxc/lxc_destroy.c          |  1 +
 src/lxc/lxc_execute.c          |  1 +
 src/lxc/lxc_freeze.c           |  1 +
 src/lxc/lxc_info.c             |  1 +
 src/lxc/lxc_init.c             |  1 +
 src/lxc/lxc_monitor.c          |  1 +
 src/lxc/lxc_monitord.c         |  1 +
 src/lxc/lxc_snapshot.c         |  1 +
 src/lxc/lxc_start.c            |  1 +
 src/lxc/lxc_stop.c             |  1 +
 src/lxc/lxc_unfreeze.c         |  1 +
 src/lxc/lxc_wait.c             |  1 +
 src/python-lxc/lxc/__init__.py |  4 ++++
 20 files changed, 35 insertions(+), 10 deletions(-)

diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index ec8b07f..546df53 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -1106,10 +1106,6 @@ static int config_loglevel(const char *key, const char *value,
 	if (!value || strlen(value) == 0)
 		return 0;
 
-	if (lxc_log_get_level() != LXC_LOG_PRIORITY_NOTSET) {
-		DEBUG("Log level already set - ignoring new value");
-		return 0;
-	}
 	if (value[0] >= '0' && value[0] <= '9')
 		newlevel = atoi(value);
 	else
diff --git a/src/lxc/log.c b/src/lxc/log.c
index 4a2b7eb..6706f90 100644
--- a/src/lxc/log.c
+++ b/src/lxc/log.c
@@ -247,6 +247,11 @@ static int __lxc_log_set_file(const char *fname, int create_dirs)
 		free(log_fname);
 	}
 
+	if (!fname || strlen(fname) == 0) {
+		log_fname = NULL;
+		return 0;
+	}
+
 #if USE_CONFIGPATH_LOGS
 	// we don't build_dir for the default if the default is
 	// i.e. /var/lib/lxc/$container/$container.log
@@ -299,7 +304,6 @@ extern int lxc_log_init(const char *name, const char *file,
 			return -1;
 		}
 
-		lxc_loglevel_specified = 1;
 		lxc_priority = lxc_log_priority_to_int(priority);
 	}
 
@@ -315,7 +319,6 @@ extern int lxc_log_init(const char *name, const char *file,
 	if (file) {
 		if (strcmp(file, "none") == 0)
 			return 0;
-		lxc_logfile_specified = 1;
 		ret = __lxc_log_set_file(file, 1);
 	} else {
 
@@ -366,15 +369,12 @@ extern int lxc_log_set_level(int level)
 		ERROR("invalid log priority %d", level);
 		return -1;
 	}
-	lxc_loglevel_specified = 1;
 	lxc_log_category_lxc.priority = level;
 	return 0;
 }
 
 extern int lxc_log_get_level(void)
 {
-	if (!lxc_loglevel_specified)
-		return LXC_LOG_PRIORITY_NOTSET;
 	return lxc_log_category_lxc.priority;
 }
 
@@ -395,7 +395,6 @@ extern int lxc_log_set_file(const char *fname)
 {
 	if (lxc_logfile_specified)
 		return 0;
-	lxc_logfile_specified = 1;
 	return __lxc_log_set_file(fname, 0);
 }
 
@@ -414,3 +413,12 @@ extern const char *lxc_log_get_prefix(void)
 {
 	return log_prefix;
 }
+
+extern void lxc_log_options_no_override()
+{
+	if (lxc_log_get_file())
+		lxc_logfile_specified = 1;
+
+	if (lxc_log_get_level() != LXC_LOG_PRIORITY_NOTSET)
+		lxc_loglevel_specified = 1;
+}
diff --git a/src/lxc/log.h b/src/lxc/log.h
index 4a9714a..ff8e47d 100644
--- a/src/lxc/log.h
+++ b/src/lxc/log.h
@@ -298,4 +298,5 @@ extern const char *lxc_log_get_file(void);
 extern int lxc_log_get_level(void);
 extern bool lxc_log_has_valid_level(void);
 extern const char *lxc_log_get_prefix(void);
+extern void lxc_log_options_no_override();
 #endif
diff --git a/src/lxc/lxc_attach.c b/src/lxc/lxc_attach.c
index 6744c05..e3e89c4 100644
--- a/src/lxc/lxc_attach.c
+++ b/src/lxc/lxc_attach.c
@@ -203,6 +203,7 @@ int main(int argc, char *argv[])
 			   my_args.progname, my_args.quiet, my_args.lxcpath[0]);
 	if (ret)
 		return ret;
+	lxc_log_options_no_override();
 
 	if (remount_sys_proc)
 		attach_options.attach_flags |= LXC_ATTACH_REMOUNT_PROC_SYS;
diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c
index 7998f1c..b7fc621 100644
--- a/src/lxc/lxc_cgroup.c
+++ b/src/lxc/lxc_cgroup.c
@@ -76,6 +76,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		return -1;
+	lxc_log_options_no_override();
 
 	state_object = my_args.argv[0];
 
diff --git a/src/lxc/lxc_console.c b/src/lxc/lxc_console.c
index 03aa158..c262ada 100644
--- a/src/lxc/lxc_console.c
+++ b/src/lxc/lxc_console.c
@@ -105,6 +105,7 @@ int main(int argc, char *argv[])
 			   my_args.progname, my_args.quiet, my_args.lxcpath[0]);
 	if (ret)
 		return EXIT_FAILURE;
+	lxc_log_options_no_override();
 
 	c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
 	if (!c) {
diff --git a/src/lxc/lxc_create.c b/src/lxc/lxc_create.c
index 058dc43..87845b4 100644
--- a/src/lxc/lxc_create.c
+++ b/src/lxc/lxc_create.c
@@ -201,6 +201,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		exit(1);
+	lxc_log_options_no_override();
 
 	memset(&spec, 0, sizeof(spec));
 	if (!my_args.bdevtype)
diff --git a/src/lxc/lxc_destroy.c b/src/lxc/lxc_destroy.c
index 729d352..cd56f08 100644
--- a/src/lxc/lxc_destroy.c
+++ b/src/lxc/lxc_destroy.c
@@ -72,6 +72,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		exit(1);
+	lxc_log_options_no_override();
 
 	c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
 	if (!c) {
diff --git a/src/lxc/lxc_execute.c b/src/lxc/lxc_execute.c
index 6a54bf6..18baa06 100644
--- a/src/lxc/lxc_execute.c
+++ b/src/lxc/lxc_execute.c
@@ -103,6 +103,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		return -1;
+	lxc_log_options_no_override();
 
 	/* rcfile is specified in the cli option */
 	if (my_args.rcfile)
diff --git a/src/lxc/lxc_freeze.c b/src/lxc/lxc_freeze.c
index 086c81c..bb01a3a 100644
--- a/src/lxc/lxc_freeze.c
+++ b/src/lxc/lxc_freeze.c
@@ -66,6 +66,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		exit(1);
+	lxc_log_options_no_override();
 
 	c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
 	if (!c) {
diff --git a/src/lxc/lxc_info.c b/src/lxc/lxc_info.c
index 4243cf3..b556c2c 100644
--- a/src/lxc/lxc_info.c
+++ b/src/lxc/lxc_info.c
@@ -371,6 +371,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		return ret;
+	lxc_log_options_no_override();
 
 	if (print_info(my_args.name, my_args.lxcpath[0]) == 0)
 		ret = EXIT_SUCCESS;
diff --git a/src/lxc/lxc_init.c b/src/lxc/lxc_init.c
index 3e51c00..91ed08d 100644
--- a/src/lxc/lxc_init.c
+++ b/src/lxc/lxc_init.c
@@ -111,6 +111,7 @@ int main(int argc, char *argv[])
 			   basename(argv[0]), quiet, lxcpath);
 	if (err < 0)
 		exit(EXIT_FAILURE);
+	lxc_log_options_no_override();
 
 	if (!argv[optind]) {
 		ERROR("missing command to launch");
diff --git a/src/lxc/lxc_monitor.c b/src/lxc/lxc_monitor.c
index 52f4c98..fa954dc 100644
--- a/src/lxc/lxc_monitor.c
+++ b/src/lxc/lxc_monitor.c
@@ -86,6 +86,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		return -1;
+	lxc_log_options_no_override();
 
 	if (quit_monitord) {
 		int ret = EXIT_SUCCESS;
diff --git a/src/lxc/lxc_monitord.c b/src/lxc/lxc_monitord.c
index 84e0239..f6d99d5 100644
--- a/src/lxc/lxc_monitord.c
+++ b/src/lxc/lxc_monitord.c
@@ -355,6 +355,7 @@ int main(int argc, char *argv[])
 	ret = lxc_log_init(NULL, logpath, "NOTICE", "lxc-monitord", 0, lxcpath);
 	if (ret)
 		INFO("Failed to open log file %s, log will be lost", lxcpath);
+	lxc_log_options_no_override();
 
 	pipefd = atoi(argv[2]);
 
diff --git a/src/lxc/lxc_snapshot.c b/src/lxc/lxc_snapshot.c
index a49e8d7..a8d4e7f 100644
--- a/src/lxc/lxc_snapshot.c
+++ b/src/lxc/lxc_snapshot.c
@@ -187,6 +187,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		exit(1);
+	lxc_log_options_no_override();
 
 	if (geteuid()) {
 		if (access(my_args.lxcpath[0], O_RDWR) < 0) {
diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c
index 19ebea0..9517fe6 100644
--- a/src/lxc/lxc_start.c
+++ b/src/lxc/lxc_start.c
@@ -228,6 +228,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		return err;
+	lxc_log_options_no_override();
 
 	const char *lxcpath = my_args.lxcpath[0];
 
diff --git a/src/lxc/lxc_stop.c b/src/lxc/lxc_stop.c
index 99621a2..fc9d70a 100644
--- a/src/lxc/lxc_stop.c
+++ b/src/lxc/lxc_stop.c
@@ -144,6 +144,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		return 1;
+	lxc_log_options_no_override();
 
 	/* Set default timeout */
 	if (my_args.timeout == -2) {
diff --git a/src/lxc/lxc_unfreeze.c b/src/lxc/lxc_unfreeze.c
index ccabada..e66d165 100644
--- a/src/lxc/lxc_unfreeze.c
+++ b/src/lxc/lxc_unfreeze.c
@@ -64,6 +64,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		exit(1);
+	lxc_log_options_no_override();
 
 	c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
 	if (!c) {
diff --git a/src/lxc/lxc_wait.c b/src/lxc/lxc_wait.c
index 269acfd..d34c5e7 100644
--- a/src/lxc/lxc_wait.c
+++ b/src/lxc/lxc_wait.c
@@ -92,6 +92,7 @@ int main(int argc, char *argv[])
 	if (lxc_log_init(my_args.name, my_args.log_file, my_args.log_priority,
 			 my_args.progname, my_args.quiet, my_args.lxcpath[0]))
 		return -1;
+	lxc_log_options_no_override();
 
 	c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
 	if (!c)
diff --git a/src/python-lxc/lxc/__init__.py b/src/python-lxc/lxc/__init__.py
index 43fb07d..4973dee 100644
--- a/src/python-lxc/lxc/__init__.py
+++ b/src/python-lxc/lxc/__init__.py
@@ -362,6 +362,10 @@ class Container(_lxc.Container):
         set_key(key, value)
         new_value = self.get_config_item(key)
 
+        # loglevel is special and won't match the string we set
+        if key == "lxc.loglevel":
+            new_value = value
+
         if (isinstance(value, str) and isinstance(new_value, str) and
                 value == new_value):
             return True
-- 
1.9.rc1



More information about the lxc-devel mailing list