[lxc-devel] [PATCH] remove logfile and loglevel from struct lxc_conf

Serge Hallyn serge.hallyn at canonical.com
Fri Jan 11 18:47:06 UTC 2013


The options are still supported in the lxc configuration file.
However they are stored only in local variables in src/lxc/log.c,
which can be read using two new functions:
	int lxc_log_get_level(void);
	const char *lxc_log_get_file(void);

Per discussions with Michael, this seems like a cleaner way
to approach this.   The goal is (as it was) to allow lxc-start
and lxc-execute to set these very early (before reading the
rcfile), to allow the rcfile to set them if not already set,
and to make sure what is specified on the command line
overrides anything in a configuration file.

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/conf.c    |  3 ---
 src/lxc/conf.h    |  2 --
 src/lxc/confile.c | 36 ++++++++++++++++++------------------
 src/lxc/log.c     | 24 +++++++++++++++++++++---
 src/lxc/log.h     |  2 ++
 5 files changed, 41 insertions(+), 26 deletions(-)

diff --git a/src/lxc/conf.c b/src/lxc/conf.c
index e5e522c..ea0fcf6 100644
--- a/src/lxc/conf.c
+++ b/src/lxc/conf.c
@@ -2077,7 +2077,6 @@ struct lxc_conf *lxc_conf_init(void)
 	new->console.name[0] = '\0';
 	new->maincmd_fd = -1;
 	new->rootfs.mount = default_rootfs_mount;
-	new->loglevel = LXC_LOG_PRIORITY_NOTSET;
 	lxc_list_init(&new->cgroup);
 	lxc_list_init(&new->network);
 	lxc_list_init(&new->mount_list);
@@ -2938,8 +2937,6 @@ void lxc_conf_free(struct lxc_conf *conf)
 		free(conf->ttydir);
 	if (conf->fstab)
 		free(conf->fstab);
-	if (conf->logfile)
-		free(conf->logfile);
 	lxc_clear_config_network(conf);
 #if HAVE_APPARMOR
 	if (conf->aa_profile)
diff --git a/src/lxc/conf.h b/src/lxc/conf.h
index 1f9b861..83de84a 100644
--- a/src/lxc/conf.h
+++ b/src/lxc/conf.h
@@ -246,8 +246,6 @@ struct lxc_conf {
 #if HAVE_APPARMOR
 	char *aa_profile;
 #endif
-	char *logfile;
-	int loglevel;
 
 #if HAVE_APPARMOR /* || HAVE_SELINUX || HAVE_SMACK */
 	int lsm_umount_proc;
diff --git a/src/lxc/confile.c b/src/lxc/confile.c
index 6b75b6a..8b4789f 100644
--- a/src/lxc/confile.c
+++ b/src/lxc/confile.c
@@ -924,12 +924,8 @@ static int config_logfile(const char *key, const char *value,
 {
 	char *path;
 
-	// if given a blank entry, null out any previous entries.
-	if (!value || strlen(value) == 0) {
-		if (lxc_conf->logfile) {
-			free(lxc_conf->logfile);
-			lxc_conf->logfile = NULL;
-		}
+	if (lxc_log_get_file()) {
+		DEBUG("Log file already specified - ignoring new value");
 		return 0;
 	}
 
@@ -944,9 +940,7 @@ static int config_logfile(const char *key, const char *value,
 		return -1;
 	}
 
-	if (lxc_conf->logfile)
-		free(lxc_conf->logfile);
-	lxc_conf->logfile = path;
+	// log.c's log_path points to path, don't free it here.
 
 	return 0;
 }
@@ -954,14 +948,20 @@ static int config_logfile(const char *key, const char *value,
 static int config_loglevel(const char *key, const char *value,
 			     struct lxc_conf *lxc_conf)
 {
+	int newlevel;
+
 	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')
-		lxc_conf->loglevel = atoi(value);
+		newlevel = atoi(value);
 	else
-		lxc_conf->loglevel = lxc_log_priority_to_int(value);
-	return lxc_log_set_level(lxc_conf->loglevel);
+		newlevel = lxc_log_priority_to_int(value);
+	return lxc_log_set_level(newlevel);
 }
 
 static int config_autodev(const char *key, const char *value,
@@ -1612,9 +1612,9 @@ int lxc_get_config_item(struct lxc_conf *c, const char *key, char *retv,
 		v = c->aa_profile;
 #endif
 	else if (strcmp(key, "lxc.logfile") == 0)
-		v = c->logfile;
+		v = lxc_log_get_file();
 	else if (strcmp(key, "lxc.loglevel") == 0)
-		v = lxc_log_priority_to_string(c->loglevel);
+		v = lxc_log_priority_to_string(lxc_log_get_level());
 	else if (strcmp(key, "lxc.cgroup") == 0) // all cgroup info
 		return lxc_get_cgroup_entry(c, retv, inlen, "all");
 	else if (strncmp(key, "lxc.cgroup.", 11) == 0) // specific cgroup info
@@ -1694,10 +1694,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 (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);
+	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());
 	lxc_list_for_each(it, &c->cgroup) {
 		struct lxc_cgroup *cg = it->elem;
 		fprintf(fout, "lxc.cgroup.%s = %s\n", cg->subsystem, cg->value);
diff --git a/src/lxc/log.c b/src/lxc/log.c
index 0354d8d..c3aaf79 100644
--- a/src/lxc/log.c
+++ b/src/lxc/log.c
@@ -41,7 +41,7 @@
 
 int lxc_log_fd = -1;
 static char log_prefix[LXC_LOG_PREFIX_SIZE] = "lxc";
-int lxc_loglevel_specified = 0;
+static int lxc_loglevel_specified = 0;
 
 lxc_log_define(lxc_log, lxc);
 
@@ -208,6 +208,8 @@ extern int lxc_log_set_level(int level)
 	return 0;
 }
 
+char *log_fname;  // default to NULL, set in lxc_log_set_file.
+
 /*
  * This is called when we read a lxc.logfile entry in a lxc.conf file.  This
  * happens after processing command line arguments, which override the .conf
@@ -216,13 +218,29 @@ extern int lxc_log_set_level(int level)
 extern int lxc_log_set_file(char *fname)
 {
 	if (lxc_log_fd != -1) {
-		INFO("Configuration file was specified on command line, configuration file entry being ignored");
-		return 0;
+		// this should've been caught at config_logfile.
+		ERROR("Race in setting logfile?");
+		return -1;
 	}
+
 	lxc_log_fd = log_open(fname);
 	if (lxc_log_fd == -1) {
 		ERROR("failed to open log file %s\n", fname);
 		return -1;
 	}
+
+	log_fname = fname;
 	return 0;
 }
+
+extern int lxc_log_get_level(void)
+{
+	if (!lxc_loglevel_specified)
+		return LXC_LOG_PRIORITY_NOTSET;
+	return lxc_log_category_lxc.priority;
+}
+
+extern const char *lxc_log_get_file(void)
+{
+	return log_fname;
+}
diff --git a/src/lxc/log.h b/src/lxc/log.h
index 340a3ab..03a0961 100644
--- a/src/lxc/log.h
+++ b/src/lxc/log.h
@@ -293,4 +293,6 @@ extern int lxc_log_init(const char *file, const char *priority,
 extern void lxc_log_setprefix(const char *a_prefix);
 extern int lxc_log_set_level(int level);
 extern int lxc_log_set_file(char *fname);
+extern int lxc_log_get_level(void);
+extern const char *lxc_log_get_file(void);
 #endif
-- 
1.8.0





More information about the lxc-devel mailing list