Commit 6edbfc86 by Stéphane Graber

logging: Add lxc_log_options_no_override function

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: 's avatarStéphane Graber <stgraber@ubuntu.com> Acked-by: 's avatarSerge E. Hallyn <serge.hallyn@ubuntu.com>
parent 2133f58c
...@@ -1106,10 +1106,6 @@ static int config_loglevel(const char *key, const char *value, ...@@ -1106,10 +1106,6 @@ static int config_loglevel(const char *key, const char *value,
if (!value || strlen(value) == 0) if (!value || strlen(value) == 0)
return 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') if (value[0] >= '0' && value[0] <= '9')
newlevel = atoi(value); newlevel = atoi(value);
else else
......
...@@ -247,6 +247,11 @@ static int __lxc_log_set_file(const char *fname, int create_dirs) ...@@ -247,6 +247,11 @@ static int __lxc_log_set_file(const char *fname, int create_dirs)
free(log_fname); free(log_fname);
} }
if (!fname || strlen(fname) == 0) {
log_fname = NULL;
return 0;
}
#if USE_CONFIGPATH_LOGS #if USE_CONFIGPATH_LOGS
// we don't build_dir for the default if the default is // we don't build_dir for the default if the default is
// i.e. /var/lib/lxc/$container/$container.log // i.e. /var/lib/lxc/$container/$container.log
...@@ -299,7 +304,6 @@ extern int lxc_log_init(const char *name, const char *file, ...@@ -299,7 +304,6 @@ extern int lxc_log_init(const char *name, const char *file,
return -1; return -1;
} }
lxc_loglevel_specified = 1;
lxc_priority = lxc_log_priority_to_int(priority); lxc_priority = lxc_log_priority_to_int(priority);
} }
...@@ -315,7 +319,6 @@ extern int lxc_log_init(const char *name, const char *file, ...@@ -315,7 +319,6 @@ extern int lxc_log_init(const char *name, const char *file,
if (file) { if (file) {
if (strcmp(file, "none") == 0) if (strcmp(file, "none") == 0)
return 0; return 0;
lxc_logfile_specified = 1;
ret = __lxc_log_set_file(file, 1); ret = __lxc_log_set_file(file, 1);
} else { } else {
...@@ -366,15 +369,12 @@ extern int lxc_log_set_level(int level) ...@@ -366,15 +369,12 @@ extern int lxc_log_set_level(int level)
ERROR("invalid log priority %d", level); ERROR("invalid log priority %d", level);
return -1; return -1;
} }
lxc_loglevel_specified = 1;
lxc_log_category_lxc.priority = level; lxc_log_category_lxc.priority = level;
return 0; return 0;
} }
extern int lxc_log_get_level(void) extern int lxc_log_get_level(void)
{ {
if (!lxc_loglevel_specified)
return LXC_LOG_PRIORITY_NOTSET;
return lxc_log_category_lxc.priority; return lxc_log_category_lxc.priority;
} }
...@@ -395,7 +395,6 @@ extern int lxc_log_set_file(const char *fname) ...@@ -395,7 +395,6 @@ extern int lxc_log_set_file(const char *fname)
{ {
if (lxc_logfile_specified) if (lxc_logfile_specified)
return 0; return 0;
lxc_logfile_specified = 1;
return __lxc_log_set_file(fname, 0); return __lxc_log_set_file(fname, 0);
} }
...@@ -414,3 +413,12 @@ extern const char *lxc_log_get_prefix(void) ...@@ -414,3 +413,12 @@ extern const char *lxc_log_get_prefix(void)
{ {
return log_prefix; 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;
}
...@@ -298,4 +298,5 @@ extern const char *lxc_log_get_file(void); ...@@ -298,4 +298,5 @@ extern const char *lxc_log_get_file(void);
extern int lxc_log_get_level(void); extern int lxc_log_get_level(void);
extern bool lxc_log_has_valid_level(void); extern bool lxc_log_has_valid_level(void);
extern const char *lxc_log_get_prefix(void); extern const char *lxc_log_get_prefix(void);
extern void lxc_log_options_no_override();
#endif #endif
...@@ -203,6 +203,7 @@ int main(int argc, char *argv[]) ...@@ -203,6 +203,7 @@ int main(int argc, char *argv[])
my_args.progname, my_args.quiet, my_args.lxcpath[0]); my_args.progname, my_args.quiet, my_args.lxcpath[0]);
if (ret) if (ret)
return ret; return ret;
lxc_log_options_no_override();
if (remount_sys_proc) if (remount_sys_proc)
attach_options.attach_flags |= LXC_ATTACH_REMOUNT_PROC_SYS; attach_options.attach_flags |= LXC_ATTACH_REMOUNT_PROC_SYS;
......
...@@ -76,6 +76,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
return -1; return -1;
lxc_log_options_no_override();
state_object = my_args.argv[0]; state_object = my_args.argv[0];
......
...@@ -105,6 +105,7 @@ int main(int argc, char *argv[]) ...@@ -105,6 +105,7 @@ int main(int argc, char *argv[])
my_args.progname, my_args.quiet, my_args.lxcpath[0]); my_args.progname, my_args.quiet, my_args.lxcpath[0]);
if (ret) if (ret)
return EXIT_FAILURE; return EXIT_FAILURE;
lxc_log_options_no_override();
c = lxc_container_new(my_args.name, my_args.lxcpath[0]); c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
if (!c) { if (!c) {
......
...@@ -201,6 +201,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
exit(1); exit(1);
lxc_log_options_no_override();
memset(&spec, 0, sizeof(spec)); memset(&spec, 0, sizeof(spec));
if (!my_args.bdevtype) if (!my_args.bdevtype)
......
...@@ -72,6 +72,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
exit(1); exit(1);
lxc_log_options_no_override();
c = lxc_container_new(my_args.name, my_args.lxcpath[0]); c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
if (!c) { if (!c) {
......
...@@ -103,6 +103,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
return -1; return -1;
lxc_log_options_no_override();
/* rcfile is specified in the cli option */ /* rcfile is specified in the cli option */
if (my_args.rcfile) if (my_args.rcfile)
......
...@@ -66,6 +66,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
exit(1); exit(1);
lxc_log_options_no_override();
c = lxc_container_new(my_args.name, my_args.lxcpath[0]); c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
if (!c) { if (!c) {
......
...@@ -371,6 +371,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
return ret; return ret;
lxc_log_options_no_override();
if (print_info(my_args.name, my_args.lxcpath[0]) == 0) if (print_info(my_args.name, my_args.lxcpath[0]) == 0)
ret = EXIT_SUCCESS; ret = EXIT_SUCCESS;
......
...@@ -111,6 +111,7 @@ int main(int argc, char *argv[]) ...@@ -111,6 +111,7 @@ int main(int argc, char *argv[])
basename(argv[0]), quiet, lxcpath); basename(argv[0]), quiet, lxcpath);
if (err < 0) if (err < 0)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
lxc_log_options_no_override();
if (!argv[optind]) { if (!argv[optind]) {
ERROR("missing command to launch"); ERROR("missing command to launch");
......
...@@ -86,6 +86,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
return -1; return -1;
lxc_log_options_no_override();
if (quit_monitord) { if (quit_monitord) {
int ret = EXIT_SUCCESS; int ret = EXIT_SUCCESS;
......
...@@ -355,6 +355,7 @@ int main(int argc, char *argv[]) ...@@ -355,6 +355,7 @@ int main(int argc, char *argv[])
ret = lxc_log_init(NULL, logpath, "NOTICE", "lxc-monitord", 0, lxcpath); ret = lxc_log_init(NULL, logpath, "NOTICE", "lxc-monitord", 0, lxcpath);
if (ret) if (ret)
INFO("Failed to open log file %s, log will be lost", lxcpath); INFO("Failed to open log file %s, log will be lost", lxcpath);
lxc_log_options_no_override();
pipefd = atoi(argv[2]); pipefd = atoi(argv[2]);
......
...@@ -187,6 +187,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
exit(1); exit(1);
lxc_log_options_no_override();
if (geteuid()) { if (geteuid()) {
if (access(my_args.lxcpath[0], O_RDWR) < 0) { if (access(my_args.lxcpath[0], O_RDWR) < 0) {
......
...@@ -228,6 +228,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
return err; return err;
lxc_log_options_no_override();
const char *lxcpath = my_args.lxcpath[0]; const char *lxcpath = my_args.lxcpath[0];
......
...@@ -144,6 +144,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
return 1; return 1;
lxc_log_options_no_override();
/* Set default timeout */ /* Set default timeout */
if (my_args.timeout == -2) { if (my_args.timeout == -2) {
......
...@@ -64,6 +64,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
exit(1); exit(1);
lxc_log_options_no_override();
c = lxc_container_new(my_args.name, my_args.lxcpath[0]); c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
if (!c) { if (!c) {
......
...@@ -92,6 +92,7 @@ int main(int argc, char *argv[]) ...@@ -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, 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])) my_args.progname, my_args.quiet, my_args.lxcpath[0]))
return -1; return -1;
lxc_log_options_no_override();
c = lxc_container_new(my_args.name, my_args.lxcpath[0]); c = lxc_container_new(my_args.name, my_args.lxcpath[0]);
if (!c) if (!c)
......
...@@ -362,6 +362,10 @@ class Container(_lxc.Container): ...@@ -362,6 +362,10 @@ class Container(_lxc.Container):
set_key(key, value) set_key(key, value)
new_value = self.get_config_item(key) 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 if (isinstance(value, str) and isinstance(new_value, str) and
value == new_value): value == new_value):
return True return True
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment