[lxc-devel] [PATCH 1/1] Logging: don't confuse command line and config file specified values

Serge Hallyn serge.hallyn at ubuntu.com
Tue Aug 6 21:26:57 UTC 2013


Currently if loglevel/logfile are specified on command line in a
program using LXC api, and that program does any
container->save_config(), then the new config will be saved with the
loglevel/logfile specified on command line.  This is wrong, especially
in the case of

cat > lxc.conf << EOF
lxc.logfile=a
EOF

lxc-create -t cirros -n c1 -o b

which will result in a container config with lxc.logfile=b.

So store the configfile-specified logfile and loglevel in the
lxc_conf.

Note - this addresses my concern from yesterday, so barring any
objections to this patch, I will push both this patch and the one I
posted yesterday soon.

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/conf.c    |  1 +
 src/lxc/conf.h    |  8 ++++++++
 src/lxc/confile.c | 24 ++++++++++++++++--------
 3 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index 46320dd..53c05e6 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -2123,6 +2123,7 @@ struct lxc_conf *lxc_conf_init(void)
 	}
 	memset(new, 0, sizeof(*new));
 
+	new->loglevel = LXC_LOG_PRIORITY_NOTSET;
 	new->personality = -1;
 	new->console.log_path = NULL;
 	new->console.log_fd = -1;
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index ed3240d..d6f85c8 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -288,6 +288,14 @@ struct lxc_conf {
 	int stopsignal; // signal used to stop container
 	int kmsg;  // if 1, create /dev/kmsg symlink
 	char *rcfile;	// Copy of the top level rcfile we read
+
+	// Logfile and logleve can be set in a container config file.
+	// Those function as defaults.  The defaults can be overriden
+	// by command line.  However we don't want the command line
+	// specified values to be saved on c->save_config().  So we
+	// store the config file specified values here.
+	char *logfile;  // the logfile as specifed in config
+	int loglevel;   // loglevel as specifed in config (if any)
 };
 
 int run_lxc_hooks(const char *name, char *hook, struct lxc_conf *conf,
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index bb02e1c..e75221f 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -990,6 +990,11 @@ static int config_aa_profile(const char *key, const char *value,
 static int config_logfile(const char *key, const char *value,
 			     struct lxc_conf *lxc_conf)
 {
+	// store these values in the lxc_conf, and then try to set for
+	// actual current logging.
+	if (lxc_conf->logfile)
+		free(lxc_conf->logfile);
+	lxc_conf->logfile = strdup(value);
 	return lxc_log_set_file(value);
 }
 
@@ -1001,14 +1006,17 @@ 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
 		newlevel = lxc_log_priority_to_int(value);
+
+	lxc_conf->loglevel = newlevel;
+
+	if (lxc_log_get_level() != LXC_LOG_PRIORITY_NOTSET) {
+		DEBUG("Log level already set - ignoring new value");
+		return 0;
+	}
 	return lxc_log_set_level(newlevel);
 }
 
@@ -1873,10 +1881,10 @@ void write_config(FILE *fout, struct lxc_conf *c)
 	if (c->aa_profile)
 		fprintf(fout, "lxc.aa_profile = %s\n", c->aa_profile);
 #endif
-	if (lxc_log_get_level() != LXC_LOG_PRIORITY_NOTSET)
-		fprintf(fout, "lxc.loglevel = %s\n", lxc_log_priority_to_string(lxc_log_get_level()));
-	if (lxc_log_get_file())
-		fprintf(fout, "lxc.logfile = %s\n", lxc_log_get_file());
+	if (c->loglevel != LXC_LOG_PRIORITY_NOTSET)
+		fprintf(fout, "lxc.loglevel = %s\n", lxc_log_priority_to_string(c->loglevel));
+	if (c->logfile)
+		fprintf(fout, "lxc.logfile = %s\n", c->logfile);
 	lxc_list_for_each(it, &c->cgroup) {
 		struct lxc_cgroup *cg = it->elem;
 		fprintf(fout, "lxc.cgroup.%s = %s\n", cg->subsystem, cg->value);
-- 
1.8.1.2





More information about the lxc-devel mailing list