From: Michel Normand Date: Thu, 14 May 2009 13:52:03 +0000 (+0200) Subject: add support of a lxc log file to cli X-Git-Tag: lxc_0_6_3~99 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=51cab6315f1500af5105cfbf56cc03b2ceefa0d9;p=thirdparty%2Flxc.git add support of a lxc log file to cli this is adding -o and -l options to all cli of lxc Signed-off-by: Michel Normand Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/log.c b/src/lxc/log.c index 496805cb1..a5a43535d 100644 --- a/src/lxc/log.c +++ b/src/lxc/log.c @@ -127,9 +127,21 @@ static int log_open(const char *name) } /*---------------------------------------------------------------------------*/ -extern int lxc_log_init(const char *file, int priority, const char *prefix) +extern int lxc_log_init(const char *file, const char *priority, + const char *prefix) { - lxc_log_category_lxc.priority = priority; + int lxc_priority = LXC_LOG_PRIORITY_ERROR; + + if (priority) { + lxc_priority = lxc_log_priority_to_int(priority); + + if (lxc_priority == LXC_LOG_PRIORITY_NOTSET) { + ERROR("invalid log priority %s", priority); + return -1; + } + } + + lxc_log_category_lxc.priority = lxc_priority; if (prefix) lxc_log_setprefix(prefix); diff --git a/src/lxc/log.h b/src/lxc/log.h index cfe52c054..d36bcece2 100644 --- a/src/lxc/log.h +++ b/src/lxc/log.h @@ -278,4 +278,6 @@ extern struct lxc_log_category lxc_log_category_lxc; ERROR("%s - " format "\n", strerror(errno), ##__VA_ARGS__); \ } while (0) +extern int lxc_log_init(const char *file, const char *priority, + const char *prefix); #endif diff --git a/src/lxc/lxc_cgroup.c b/src/lxc/lxc_cgroup.c index e0cf6354d..98dfe93cd 100644 --- a/src/lxc/lxc_cgroup.c +++ b/src/lxc/lxc_cgroup.c @@ -34,6 +34,8 @@ void usage(char *cmd) { fprintf(stderr, "%s [value]\n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } @@ -41,13 +43,20 @@ int main(int argc, char *argv[]) { int opt; char *name = NULL, *subsystem = NULL, *value = NULL; + const char *log_file = NULL, *log_priority = NULL; int nbargs = 0; - while ((opt = getopt(argc, argv, "n:")) != -1) { + while ((opt = getopt(argc, argv, "n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -56,6 +65,9 @@ int main(int argc, char *argv[]) if (!name || (argc-optind) < 1) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + if ((argc -optind) >= 1) subsystem = argv[optind]; diff --git a/src/lxc/lxc_checkpoint.c b/src/lxc/lxc_checkpoint.c index 287ac3dce..687d7f5cb 100644 --- a/src/lxc/lxc_checkpoint.c +++ b/src/lxc/lxc_checkpoint.c @@ -31,6 +31,8 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } @@ -38,11 +40,12 @@ int main(int argc, char *argv[]) { int opt; char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; int stop = 0; int nbargs = 0; int ret = 1; - while ((opt = getopt(argc, argv, "sn:")) != -1) { + while ((opt = getopt(argc, argv, "sn:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; @@ -50,6 +53,12 @@ int main(int argc, char *argv[]) case 's': stop = 1; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -61,6 +70,9 @@ int main(int argc, char *argv[]) if (!argv[1]) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return -1; + if (lxc_freeze(name)) return -1; diff --git a/src/lxc/lxc_console.c b/src/lxc/lxc_console.c index d095e6c71..dc6e8999e 100644 --- a/src/lxc/lxc_console.c +++ b/src/lxc/lxc_console.c @@ -48,12 +48,15 @@ void usage(char *cmd) fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); fprintf(stderr, "\t [-t ] : tty number\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; int opt; int nbargs = 0; int master = -1; @@ -62,7 +65,7 @@ int main(int argc, char *argv[]) int err = LXC_ERROR_INTERNAL; struct termios tios, oldtios; - while ((opt = getopt(argc, argv, "t:n:")) != -1) { + while ((opt = getopt(argc, argv, "t:n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; @@ -71,6 +74,12 @@ int main(int argc, char *argv[]) case 't': ttynum = atoi(optarg); break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -79,6 +88,9 @@ int main(int argc, char *argv[]) if (!name) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + /* Get current termios */ if (tcgetattr(0, &tios)) { ERROR("failed to get current terminal settings : %s", diff --git a/src/lxc/lxc_create.c b/src/lxc/lxc_create.c index a8a01a3bf..42ac24d54 100644 --- a/src/lxc/lxc_create.c +++ b/src/lxc/lxc_create.c @@ -39,16 +39,19 @@ void usage(char *cmd) fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); fprintf(stderr, "\t -f : path of the configuration file\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { const char *name = NULL, *file = NULL; + const char *log_file = NULL, *log_priority = NULL; struct lxc_conf lxc_conf; int err, opt; - while ((opt = getopt(argc, argv, "f:n:")) != -1) { + while ((opt = getopt(argc, argv, "f:n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; @@ -56,12 +59,23 @@ int main(int argc, char *argv[]) case 'f': file = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } } if (!name) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + + if (lxc_conf_init(&lxc_conf)) + return 1; if (file && lxc_config_read(file, &lxc_conf)) return 1; diff --git a/src/lxc/lxc_destroy.c b/src/lxc/lxc_destroy.c index 0170aab09..0383be1d6 100644 --- a/src/lxc/lxc_destroy.c +++ b/src/lxc/lxc_destroy.c @@ -31,21 +31,30 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; int opt; int nbargs = 0; int err; - while ((opt = getopt(argc, argv, "n:")) != -1) { + while ((opt = getopt(argc, argv, "n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -54,6 +63,9 @@ int main(int argc, char *argv[]) if (!name) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + err = lxc_destroy(name); if (err) return 1; diff --git a/src/lxc/lxc_execute.c b/src/lxc/lxc_execute.c index e46361821..27aee6b47 100644 --- a/src/lxc/lxc_execute.c +++ b/src/lxc/lxc_execute.c @@ -40,12 +40,15 @@ void usage(char *cmd) fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); fprintf(stderr, "\t [-f ] : path of the configuration file\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { - char *name = NULL, *file = NULL; + const char *name = NULL, *file = NULL; + const char *log_file = NULL, *log_priority = NULL; static char **args; char path[MAXPATHLEN]; int opt; @@ -54,7 +57,7 @@ int main(int argc, char *argv[]) int ret = 1; struct lxc_conf lxc_conf; - while ((opt = getopt(argc, argv, "f:n:")) != -1) { + while ((opt = getopt(argc, argv, "f:n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; @@ -62,6 +65,12 @@ int main(int argc, char *argv[]) case 'f': file = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -72,6 +81,9 @@ int main(int argc, char *argv[]) argc -= nbargs; + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + goto out; + if (lxc_conf_init(&lxc_conf)) goto out; diff --git a/src/lxc/lxc_freeze.c b/src/lxc/lxc_freeze.c index b8bdb3096..bf6b58084 100644 --- a/src/lxc/lxc_freeze.c +++ b/src/lxc/lxc_freeze.c @@ -32,20 +32,29 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; int opt; int nbargs = 0; - while ((opt = getopt(argc, argv, "n:")) != -1) { + while ((opt = getopt(argc, argv, "n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -54,6 +63,9 @@ int main(int argc, char *argv[]) if (!name) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + if (lxc_freeze(name)) return 1; diff --git a/src/lxc/lxc_info.c b/src/lxc/lxc_info.c index a88e2a2d8..a960985d4 100644 --- a/src/lxc/lxc_info.c +++ b/src/lxc/lxc_info.c @@ -31,19 +31,28 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; int opt, state, nbargs = 0; - while ((opt = getopt(argc, argv, "n:")) != -1) { + while ((opt = getopt(argc, argv, "n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -52,6 +61,9 @@ int main(int argc, char *argv[]) if (!name) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + state = lxc_getstate(name); if (state < 0) return 1; diff --git a/src/lxc/lxc_monitor.c b/src/lxc/lxc_monitor.c index 52a91e864..472b31930 100644 --- a/src/lxc/lxc_monitor.c +++ b/src/lxc/lxc_monitor.c @@ -35,28 +35,40 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container or regular expression\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; char *regexp; struct lxc_msg msg; regex_t preg; int fd, opt; - while ((opt = getopt(argc, argv, "n:")) != -1) { + while ((opt = getopt(argc, argv, "n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } } if (!name) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + regexp = malloc(strlen(name) + 3); sprintf(regexp, "^%s$", name); diff --git a/src/lxc/lxc_restart.c b/src/lxc/lxc_restart.c index 1b5aab481..58bf40d78 100644 --- a/src/lxc/lxc_restart.c +++ b/src/lxc/lxc_restart.c @@ -33,19 +33,28 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; int opt, nbargs = 0; - while ((opt = getopt(argc, argv, "n:")) != -1) { + while ((opt = getopt(argc, argv, "n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -54,9 +63,12 @@ int main(int argc, char *argv[]) if (!name) usage(argv[0]); - if (!argv[1]) + if (!argv[optind]) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + if (lxc_restart(name, argv[1], 0)) { ERROR("failed to restart %s", name); return 1; diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c index 922732c08..a60d1491d 100644 --- a/src/lxc/lxc_start.c +++ b/src/lxc/lxc_start.c @@ -43,12 +43,15 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; char **args; int opt, err = LXC_ERROR_INTERNAL, nbargs = 0; struct termios tios; @@ -58,11 +61,17 @@ int main(int argc, char *argv[]) '\0', }; - while ((opt = getopt(argc, argv, "n:")) != -1) { + while ((opt = getopt(argc, argv, "n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -78,6 +87,9 @@ int main(int argc, char *argv[]) if (!name) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + if (tcgetattr(0, &tios)) { ERROR("failed to get current terminal settings : %s", strerror(errno)); diff --git a/src/lxc/lxc_stop.c b/src/lxc/lxc_stop.c index 275d27d52..cc0b5edbd 100644 --- a/src/lxc/lxc_stop.c +++ b/src/lxc/lxc_stop.c @@ -31,19 +31,28 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; int opt, err, nbargs = 0; - while ((opt = getopt(argc, argv, "n:")) != -1) { + while ((opt = getopt(argc, argv, "n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -52,6 +61,9 @@ int main(int argc, char *argv[]) if (!name) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + err = lxc_stop(name); if (err) { fprintf(stderr, "%s\n", lxc_strerror(err)); diff --git a/src/lxc/lxc_unfreeze.c b/src/lxc/lxc_unfreeze.c index 7553df538..562bd22ed 100644 --- a/src/lxc/lxc_unfreeze.c +++ b/src/lxc/lxc_unfreeze.c @@ -31,19 +31,28 @@ void usage(char *cmd) { fprintf(stderr, "%s \n", basename(cmd)); fprintf(stderr, "\t -n : name of the container\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } int main(int argc, char *argv[]) { char *name = NULL; + const char *log_file = NULL, *log_priority = NULL; int opt, nbargs = 0; - while ((opt = getopt(argc, argv, "n:")) != -1) { + while ((opt = getopt(argc, argv, "n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } nbargs++; @@ -52,6 +61,9 @@ int main(int argc, char *argv[]) if (!name) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + if (lxc_unfreeze(name)) return 1; diff --git a/src/lxc/lxc_unshare.c b/src/lxc/lxc_unshare.c index f105ef26d..c380422f2 100644 --- a/src/lxc/lxc_unshare.c +++ b/src/lxc/lxc_unshare.c @@ -45,6 +45,8 @@ void usage(char *cmd) "\t MOUNT, PID, UTSNAME, IPC, USER, NETWORK\n"); fprintf(stderr, "\t -u : new id to be set if -s USER is specified\n"); fprintf(stderr, "\t if -f or -s PID is specified, is mandatory)\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } @@ -131,11 +133,12 @@ int main(int argc, char *argv[]) int ret; char *namespaces = NULL; char **args; + const char *log_file = NULL, *log_priority = NULL; long flags = 0; uid_t uid = -1; /* valid only if (flags & CLONE_NEWUSER) */ pid_t pid; - while ((opt = getopt(argc, argv, "fs:u:")) != -1) { + while ((opt = getopt(argc, argv, "fs:u:o:l:")) != -1) { switch (opt) { case 's': namespaces = optarg; @@ -147,11 +150,20 @@ int main(int argc, char *argv[]) case 'f': hastofork = 1; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } } args = &argv[optind]; + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return 1; + ret = lxc_fill_namespace_flags(namespaces, &flags); if (ret) usage(argv[0]); diff --git a/src/lxc/lxc_wait.c b/src/lxc/lxc_wait.c index 16f21e803..711b37738 100644 --- a/src/lxc/lxc_wait.c +++ b/src/lxc/lxc_wait.c @@ -36,6 +36,8 @@ void usage(char *cmd) fprintf(stderr, "\t -n : name of the container\n"); fprintf(stderr, "\t -s : ORed states to wait for STOPPED, " \ "STARTING, RUNNING, STOPPING, ABORTING, FREEZING, FROZEN\n"); + fprintf(stderr, "\t[-o ] : path of the log file\n"); + fprintf(stderr, "\t[-l ]: log level priority\n"); _exit(1); } @@ -61,10 +63,11 @@ static int fillwaitedstates(char *strstates, int *states) int main(int argc, char *argv[]) { char *name = NULL, *states = NULL; + const char *log_file = NULL, *log_priority = NULL; struct lxc_msg msg; int s[MAX_STATE] = { }, fd, opt; - while ((opt = getopt(argc, argv, "s:n:")) != -1) { + while ((opt = getopt(argc, argv, "s:n:o:l:")) != -1) { switch (opt) { case 'n': name = optarg; @@ -72,12 +75,21 @@ int main(int argc, char *argv[]) case 's': states = optarg; break; + case 'o': + log_file = optarg; + break; + case 'l': + log_priority = optarg; + break; } } if (!name || !states) usage(argv[0]); + if (lxc_log_init(log_file, log_priority, basename(argv[0]))) + return -1; + if (fillwaitedstates(states, s)) { usage(argv[0]); }