Unverified Commit 4c08bd46 by Christian Brauner Committed by GitHub

Merge pull request #2434 from 2xsec/bugfix

tools: fix quiet option is not working
parents 74fd09a8 097268e1
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/param.h>
#include <lxc/lxccontainer.h> #include <lxc/lxccontainer.h>
struct lxc_arguments; struct lxc_arguments;
...@@ -171,10 +172,47 @@ extern int lxc_arguments_parse(struct lxc_arguments *args, int argc, ...@@ -171,10 +172,47 @@ extern int lxc_arguments_parse(struct lxc_arguments *args, int argc,
extern int lxc_arguments_str_to_int(struct lxc_arguments *args, extern int lxc_arguments_str_to_int(struct lxc_arguments *args,
const char *str); const char *str);
#define lxc_error(arg, fmt, args...) \
if (!(arg)->quiet) \
fprintf(stderr, "%s: " fmt "\n", (arg)->progname, ##args)
extern bool lxc_setup_shared_ns(struct lxc_arguments *args, struct lxc_container *c); extern bool lxc_setup_shared_ns(struct lxc_arguments *args, struct lxc_container *c);
/* Helper macro to define errno string. */
#if (_POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600) && !defined(_GNU_SOURCE) || IS_BIONIC
#define lxc_log_strerror_r \
char errno_buf[MAXPATHLEN / 2] = {"Failed to get errno string"}; \
char *ptr = errno_buf; \
{ \
(void)strerror_r(errno, errno_buf, sizeof(errno_buf)); \
}
#else
#define lxc_log_strerror_r \
char errno_buf[MAXPATHLEN / 2] = {"Failed to get errno string"}; \
char *ptr; \
{ \
ptr = strerror_r(errno, errno_buf, sizeof(errno_buf)); \
if (!ptr) \
ptr = errno_buf; \
}
#endif
#define lxc_info(arg, fmt, args...) \
do { \
if (!(arg)->quiet) { \
fprintf(stdout, "%s: " fmt "\n", (arg)->progname, ##args); \
} \
} while (0)
#define lxc_error(arg, fmt, args...) \
do { \
if (!(arg)->quiet) { \
fprintf(stderr, "%s: " fmt "\n", (arg)->progname, ##args); \
} \
} while (0)
#define lxc_sys_error(arg, fmt, args...) \
do { \
if (!(arg)->quiet) { \
lxc_log_strerror_r \
fprintf(stderr, "%s: %s - " fmt "\n", (arg)->progname, ptr, ##args); \
} \
} while (0)
#endif /* __LXC_ARGUMENTS_H */ #endif /* __LXC_ARGUMENTS_H */
...@@ -92,7 +92,7 @@ static int add_to_simple_array(char ***array, ssize_t *capacity, char *value) ...@@ -92,7 +92,7 @@ static int add_to_simple_array(char ***array, ssize_t *capacity, char *value)
return 0; return 0;
} }
static int my_parser(struct lxc_arguments* args, int c, char* arg) static int my_parser(struct lxc_arguments *args, int c, char *arg)
{ {
char **it; char **it;
char *del; char *del;
...@@ -240,13 +240,13 @@ static bool stdfd_is_pty(void) ...@@ -240,13 +240,13 @@ static bool stdfd_is_pty(void)
return false; return false;
} }
int lxc_attach_create_log_file(const char *log_file) static int lxc_attach_create_log_file(const char *log_file)
{ {
int fd; int fd;
fd = open(log_file, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600); fd = open(log_file, O_CLOEXEC | O_RDWR | O_CREAT | O_APPEND, 0600);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "Failed to open log file \"%s\"\n", log_file); lxc_error(&my_args, "Failed to open log file \"%s\"", log_file);
return -1; return -1;
} }
...@@ -285,8 +285,7 @@ int main(int argc, char *argv[]) ...@@ -285,8 +285,7 @@ int main(int argc, char *argv[])
if (geteuid()) { if (geteuid()) {
if (access(my_args.lxcpath[0], O_RDONLY) < 0) { if (access(my_args.lxcpath[0], O_RDONLY) < 0) {
if (!my_args.quiet) lxc_error(&my_args, "You lack access to %s", my_args.lxcpath[0]);
fprintf(stderr, "You lack access to %s\n", my_args.lxcpath[0]);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
...@@ -298,30 +297,34 @@ int main(int argc, char *argv[]) ...@@ -298,30 +297,34 @@ int main(int argc, char *argv[])
if (my_args.rcfile) { if (my_args.rcfile) {
c->clear_config(c); c->clear_config(c);
if (!c->load_config(c, my_args.rcfile)) { if (!c->load_config(c, my_args.rcfile)) {
fprintf(stderr, "Failed to load rcfile\n"); lxc_error(&my_args, "Failed to load rcfile");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
c->configfile = strdup(my_args.rcfile); c->configfile = strdup(my_args.rcfile);
if (!c->configfile) { if (!c->configfile) {
fprintf(stderr, "Out of memory setting new config filename\n"); lxc_error(&my_args, "Out of memory setting new config filename");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
if (!c->may_control(c)) { if (!c->may_control(c)) {
fprintf(stderr, "Insufficent privileges to control %s\n", c->name); lxc_error(&my_args, "Insufficent privileges to control %s", c->name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
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;
if (elevated_privileges) if (elevated_privileges)
attach_options.attach_flags &= ~(elevated_privileges); attach_options.attach_flags &= ~(elevated_privileges);
if (stdfd_is_pty()) if (stdfd_is_pty())
attach_options.attach_flags |= LXC_ATTACH_TERMINAL; attach_options.attach_flags |= LXC_ATTACH_TERMINAL;
attach_options.namespaces = namespace_flags; attach_options.namespaces = namespace_flags;
attach_options.personality = new_personality; attach_options.personality = new_personality;
attach_options.env_policy = env_policy; attach_options.env_policy = env_policy;
...@@ -343,7 +346,6 @@ int main(int argc, char *argv[]) ...@@ -343,7 +346,6 @@ int main(int argc, char *argv[])
ret = c->attach(c, lxc_attach_run_command, &command, &attach_options, &pid); ret = c->attach(c, lxc_attach_run_command, &command, &attach_options, &pid);
else else
ret = c->attach(c, lxc_attach_run_shell, NULL, &attach_options, &pid); ret = c->attach(c, lxc_attach_run_shell, NULL, &attach_options, &pid);
if (ret < 0) if (ret < 0)
goto out; goto out;
...@@ -357,5 +359,6 @@ out: ...@@ -357,5 +359,6 @@ out:
lxc_container_put(c); lxc_container_put(c);
if (ret >= 0) if (ret >= 0)
exit(wexit); exit(wexit);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -37,7 +37,7 @@ ...@@ -37,7 +37,7 @@
static int my_checker(const struct lxc_arguments* args) static int my_checker(const struct lxc_arguments* args)
{ {
if (!args->argc) { if (!args->argc) {
lxc_error(args, "missing state object"); lxc_error(args, "Missing state object");
return -1; return -1;
} }
...@@ -95,43 +95,47 @@ int main(int argc, char *argv[]) ...@@ -95,43 +95,47 @@ int main(int argc, char *argv[])
if (my_args.rcfile) { if (my_args.rcfile) {
c->clear_config(c); c->clear_config(c);
if (!c->load_config(c, my_args.rcfile)) { if (!c->load_config(c, my_args.rcfile)) {
fprintf(stderr, "Failed to load rcfile\n"); lxc_error(&my_args, "Failed to load rcfile");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
c->configfile = strdup(my_args.rcfile); c->configfile = strdup(my_args.rcfile);
if (!c->configfile) { if (!c->configfile) {
fprintf(stderr, "Out of memory setting new config filename\n"); lxc_error(&my_args, "Out of memory setting new config filename");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
if (!c->may_control(c)) { if (!c->may_control(c)) {
fprintf(stderr, "Insufficent privileges to control %s:%s\n", my_args.lxcpath[0], my_args.name); lxc_error(&my_args, "Insufficent privileges to control %s:%s", my_args.lxcpath[0], my_args.name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!c->is_running(c)) { if (!c->is_running(c)) {
fprintf(stderr, "'%s:%s' is not running\n", my_args.lxcpath[0], my_args.name); lxc_error(&my_args, "'%s:%s' is not running", my_args.lxcpath[0], my_args.name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if ((my_args.argc) > 1) { if ((my_args.argc) > 1) {
value = my_args.argv[1]; value = my_args.argv[1];
if (!c->set_cgroup_item(c, state_object, value)) { if (!c->set_cgroup_item(c, state_object, value)) {
fprintf(stderr, "failed to assign '%s' value to '%s' for '%s'\n", lxc_error(&my_args, "Failed to assign '%s' value to '%s' for '%s'",
value, state_object, my_args.name); value, state_object, my_args.name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} else { } else {
char buffer[TOOL_MAXPATHLEN]; char buffer[TOOL_MAXPATHLEN];
int ret = c->get_cgroup_item(c, state_object, buffer, TOOL_MAXPATHLEN); int ret;
ret = c->get_cgroup_item(c, state_object, buffer, TOOL_MAXPATHLEN);
if (ret < 0) { if (ret < 0) {
fprintf(stderr, "failed to retrieve value of '%s' for '%s:%s'\n", lxc_error(&my_args, "Failed to retrieve value of '%s' for '%s:%s'",
state_object, my_args.lxcpath[0], my_args.name); state_object, my_args.lxcpath[0], my_args.name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
......
...@@ -75,33 +75,33 @@ int main(int argc, char *argv[]) ...@@ -75,33 +75,33 @@ int main(int argc, char *argv[])
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) {
fprintf(stderr, "No such container: %s:%s\n", my_args.lxcpath[0], my_args.name); lxc_error(&my_args, "No such container: %s:%s", my_args.lxcpath[0], my_args.name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (my_args.rcfile) { if (my_args.rcfile) {
c->clear_config(c); c->clear_config(c);
if (!c->load_config(c, my_args.rcfile)) { if (!c->load_config(c, my_args.rcfile)) {
fprintf(stderr, "Failed to load rcfile\n"); lxc_error(&my_args, "Failed to load rcfile");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
c->configfile = strdup(my_args.rcfile); c->configfile = strdup(my_args.rcfile);
if (!c->configfile) { if (!c->configfile) {
fprintf(stderr, "Out of memory setting new config filename\n"); lxc_error(&my_args, "Out of memory setting new config filename");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
if (!c->may_control(c)) { if (!c->may_control(c)) {
fprintf(stderr, "Insufficent privileges to control %s:%s\n", my_args.lxcpath[0], my_args.name); lxc_error(&my_args, "Insufficent privileges to control %s:%s", my_args.lxcpath[0], my_args.name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!c->freeze(c)) { if (!c->freeze(c)) {
fprintf(stderr, "Failed to freeze %s:%s\n", my_args.lxcpath[0], my_args.name); lxc_error(&my_args, "Failed to freeze %s:%s", my_args.lxcpath[0], my_args.name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
......
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
static struct lxc_list defines; static struct lxc_list defines;
static int ensure_path(char **confpath, const char *path) static int ensure_path(struct lxc_arguments *args, char **confpath, const char *path)
{ {
int err = -1, fd; int err = -1, fd;
char *fullpath = NULL; char *fullpath = NULL;
...@@ -56,21 +56,23 @@ static int ensure_path(char **confpath, const char *path) ...@@ -56,21 +56,23 @@ static int ensure_path(char **confpath, const char *path)
if (access(path, W_OK)) { if (access(path, W_OK)) {
fd = creat(path, 0600); fd = creat(path, 0600);
if (fd < 0 && errno != EEXIST) { if (fd < 0 && errno != EEXIST) {
fprintf(stderr, "failed to create '%s'\n", path); lxc_error(args, "Failed to create '%s'", path);
goto err; goto err;
} }
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
} }
fullpath = realpath(path, NULL); fullpath = realpath(path, NULL);
if (!fullpath) { if (!fullpath) {
fprintf(stderr, "failed to get the real path of '%s'\n", path); lxc_error(args, "Failed to get the real path of '%s'", path);
goto err; goto err;
} }
*confpath = fullpath; *confpath = fullpath;
} }
err = EXIT_SUCCESS; err = EXIT_SUCCESS;
err: err:
...@@ -204,8 +206,7 @@ int main(int argc, char *argv[]) ...@@ -204,8 +206,7 @@ int main(int argc, char *argv[])
lxcpath = my_args.lxcpath[0]; lxcpath = my_args.lxcpath[0];
if (access(lxcpath, O_RDONLY) < 0) { if (access(lxcpath, O_RDONLY) < 0) {
if (!my_args.quiet) lxc_error(&my_args, "You lack access to %s", lxcpath);
fprintf(stderr, "You lack access to %s\n", lxcpath);
exit(err); exit(err);
} }
...@@ -218,20 +219,24 @@ int main(int argc, char *argv[]) ...@@ -218,20 +219,24 @@ int main(int argc, char *argv[])
/* rcfile is specified in the cli option */ /* rcfile is specified in the cli option */
if (my_args.rcfile) { if (my_args.rcfile) {
rcfile = (char *)my_args.rcfile; rcfile = (char *)my_args.rcfile;
c = lxc_container_new(my_args.name, lxcpath); c = lxc_container_new(my_args.name, lxcpath);
if (!c) { if (!c) {
fprintf(stderr, "Failed to create lxc_container\n"); lxc_error(&my_args, "Failed to create lxc_container");
exit(err); exit(err);
} }
c->clear_config(c); c->clear_config(c);
if (!c->load_config(c, rcfile)) { if (!c->load_config(c, rcfile)) {
fprintf(stderr, "Failed to load rcfile\n"); lxc_error(&my_args, "Failed to load rcfile");
lxc_container_put(c); lxc_container_put(c);
exit(err); exit(err);
} }
c->configfile = strdup(my_args.rcfile); c->configfile = strdup(my_args.rcfile);
if (!c->configfile) { if (!c->configfile) {
fprintf(stderr, "Out of memory setting new config filename\n"); lxc_error(&my_args, "Out of memory setting new config filename");
goto out; goto out;
} }
} else { } else {
...@@ -239,7 +244,7 @@ int main(int argc, char *argv[]) ...@@ -239,7 +244,7 @@ int main(int argc, char *argv[])
rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name); rc = asprintf(&rcfile, "%s/%s/config", lxcpath, my_args.name);
if (rc == -1) { if (rc == -1) {
fprintf(stderr, "failed to allocate memory\n"); lxc_error(&my_args, "Failed to allocate memory");
exit(err); exit(err);
} }
...@@ -248,9 +253,10 @@ int main(int argc, char *argv[]) ...@@ -248,9 +253,10 @@ int main(int argc, char *argv[])
free(rcfile); free(rcfile);
rcfile = NULL; rcfile = NULL;
} }
c = lxc_container_new(my_args.name, lxcpath); c = lxc_container_new(my_args.name, lxcpath);
if (!c) { if (!c) {
fprintf(stderr, "Failed to create lxc_container\n"); lxc_error(&my_args, "Failed to create lxc_container");
exit(err); exit(err);
} }
} }
...@@ -260,23 +266,23 @@ int main(int argc, char *argv[]) ...@@ -260,23 +266,23 @@ int main(int argc, char *argv[])
* to be created for it to be started. You can just pass a configuration * to be created for it to be started. You can just pass a configuration
* file as argument and start the container right away. * file as argument and start the container right away.
*/ */
if (!c->may_control(c)) { if (!c->may_control(c)) {
fprintf(stderr, "Insufficent privileges to control %s\n", c->name); lxc_error(&my_args, "Insufficent privileges to control %s", c->name);
goto out; goto out;
} }
if (c->is_running(c)) { if (c->is_running(c)) {
fprintf(stderr, "Container is already running.\n"); lxc_error(&my_args, "Container is already running.");
err = EXIT_SUCCESS; err = EXIT_SUCCESS;
goto out; goto out;
} }
/* /*
* We should use set_config_item() over &defines, which would handle * We should use set_config_item() over &defines, which would handle
* unset c->lxc_conf for us and let us not use lxc_config_define_load() * unset c->lxc_conf for us and let us not use lxc_config_define_load()
*/ */
if (!c->lxc_conf) { if (!c->lxc_conf) {
fprintf(stderr, "No container config specified\n"); lxc_error(&my_args, "No container config specified");
goto out; goto out;
} }
...@@ -284,13 +290,13 @@ int main(int argc, char *argv[]) ...@@ -284,13 +290,13 @@ int main(int argc, char *argv[])
goto out; goto out;
if (!rcfile && !strcmp("/sbin/init", args[0])) { if (!rcfile && !strcmp("/sbin/init", args[0])) {
fprintf(stderr, "Executing '/sbin/init' with no configuration file may crash the host\n"); lxc_error(&my_args, "Executing '/sbin/init' with no configuration file may crash the host");
goto out; goto out;
} }
if (my_args.pidfile != NULL) { if (my_args.pidfile != NULL) {
if (ensure_path(&c->pidfile, my_args.pidfile) < 0) { if (ensure_path(&my_args, &c->pidfile, my_args.pidfile) < 0) {
fprintf(stderr, "failed to ensure pidfile '%s'\n", my_args.pidfile); lxc_error(&my_args, "Failed to ensure pidfile '%s'", my_args.pidfile);
goto out; goto out;
} }
} }
...@@ -317,13 +323,15 @@ int main(int argc, char *argv[]) ...@@ -317,13 +323,15 @@ int main(int argc, char *argv[])
err = c->start(c, 0, NULL) ? EXIT_SUCCESS : EXIT_FAILURE; err = c->start(c, 0, NULL) ? EXIT_SUCCESS : EXIT_FAILURE;
else else
err = c->start(c, 0, args) ? EXIT_SUCCESS : EXIT_FAILURE; err = c->start(c, 0, args) ? EXIT_SUCCESS : EXIT_FAILURE;
if (err) { if (err) {
fprintf(stderr, "The container failed to start.\n"); lxc_error(&my_args, "The container failed to start.");
if (my_args.daemonize) if (my_args.daemonize)
fprintf(stderr, "To get more details, run the container in foreground mode.\n"); lxc_error(&my_args, "To get more details, run the container in foreground mode.");
fprintf(stderr, "Additional information can be obtained by setting the "
lxc_error(&my_args, "Additional information can be obtained by setting the "
"--logfile and --logpriority options.\n"); "--logfile and --logpriority options.\n");
err = c->error_num; err = c->error_num;
lxc_container_put(c); lxc_container_put(c);
exit(err); exit(err);
......
...@@ -130,56 +130,59 @@ int main(int argc, char *argv[]) ...@@ -130,56 +130,59 @@ int main(int argc, char *argv[])
/* some checks */ /* some checks */
if (!my_args.hardstop && my_args.timeout < -1) { if (!my_args.hardstop && my_args.timeout < -1) {
fprintf(stderr, "invalid timeout\n"); lxc_error(&my_args, "Invalid timeout");
exit(ret); exit(ret);
} }
if (my_args.hardstop && my_args.nokill) { if (my_args.hardstop && my_args.nokill) {
fprintf(stderr, "-k can't be used with --nokill\n"); lxc_error(&my_args, "-k can't be used with --nokill");
exit(ret); exit(ret);
} }
if (my_args.hardstop && my_args.reboot) { if (my_args.hardstop && my_args.reboot) {
fprintf(stderr, "-k can't be used with -r\n"); lxc_error(&my_args, "-k can't be used with -r");
exit(ret); exit(ret);
} }
if (my_args.hardstop && my_args.timeout) { if (my_args.hardstop && my_args.timeout) {
fprintf(stderr, "-k doesn't allow timeouts\n"); lxc_error(&my_args, "-k doesn't allow timeouts");
exit(ret); exit(ret);
} }
if (my_args.nolock && !my_args.hardstop) { if (my_args.nolock && !my_args.hardstop) {
fprintf(stderr, "--nolock may only be used with -k\n"); lxc_error(&my_args, "--nolock may only be used with -k");
exit(ret); exit(ret);
} }
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) {
fprintf(stderr, "Error opening container\n"); lxc_error(&my_args, "Error opening container");
goto out; goto out;
} }
if (my_args.rcfile) { if (my_args.rcfile) {
c->clear_config(c); c->clear_config(c);
if (!c->load_config(c, my_args.rcfile)) { if (!c->load_config(c, my_args.rcfile)) {
fprintf(stderr, "Failed to load rcfile\n"); lxc_error(&my_args, "Failed to load rcfile");
goto out; goto out;
} }
c->configfile = strdup(my_args.rcfile); c->configfile = strdup(my_args.rcfile);
if (!c->configfile) { if (!c->configfile) {
fprintf(stderr, "Out of memory setting new config filename\n"); lxc_error(&my_args, "Out of memory setting new config filename");
goto out; goto out;
} }
} }
if (!c->may_control(c)) { if (!c->may_control(c)) {
fprintf(stderr, "Insufficent privileges to control %s\n", c->name); lxc_error(&my_args, "Insufficent privileges to control %s", c->name);
goto out; goto out;
} }
if (!c->is_running(c)) { if (!c->is_running(c)) {
fprintf(stderr, "%s is not running\n", c->name); lxc_error(&my_args, "%s is not running", c->name);
/* Per our manpage we need to exit with exit code: /* Per our manpage we need to exit with exit code:
* 2: The specified container exists but was not running. * 2: The specified container exists but was not running.
*/ */
...@@ -200,6 +203,7 @@ int main(int argc, char *argv[]) ...@@ -200,6 +203,7 @@ int main(int argc, char *argv[])
ret = EXIT_FAILURE; ret = EXIT_FAILURE;
else else
ret = EXIT_SUCCESS; ret = EXIT_SUCCESS;
goto out; goto out;
} }
......
...@@ -75,33 +75,35 @@ int main(int argc, char *argv[]) ...@@ -75,33 +75,35 @@ int main(int argc, char *argv[])
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) {
fprintf(stderr, "No such container: %s:%s\n", my_args.lxcpath[0], my_args.name); lxc_error(&my_args, "No such container: %s:%s", my_args.lxcpath[0], my_args.name);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (!c->may_control(c)) { if (!c->may_control(c)) {
fprintf(stderr, "Insufficent privileges to control %s:%s\n", my_args.lxcpath[0], my_args.name); lxc_error(&my_args, "Insufficent privileges to control %s:%s", my_args.lxcpath[0], my_args.name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (my_args.rcfile) { if (my_args.rcfile) {
c->clear_config(c); c->clear_config(c);
if (!c->load_config(c, my_args.rcfile)) { if (!c->load_config(c, my_args.rcfile)) {
fprintf(stderr, "Failed to load rcfile\n"); lxc_error(&my_args, "Failed to load rcfile");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
c->configfile = strdup(my_args.rcfile); c->configfile = strdup(my_args.rcfile);
if (!c->configfile) { if (!c->configfile) {
fprintf(stderr, "Out of memory setting new config filename\n"); lxc_error(&my_args, "Out of memory setting new config filename");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
if (!c->unfreeze(c)) { if (!c->unfreeze(c)) {
fprintf(stderr, "Failed to unfreeze %s:%s\n", my_args.lxcpath[0], my_args.name); lxc_error(&my_args, "Failed to unfreeze %s:%s", my_args.lxcpath[0], my_args.name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
......
...@@ -35,21 +35,23 @@ ...@@ -35,21 +35,23 @@
#include "arguments.h" #include "arguments.h"
#include "tool_utils.h" #include "tool_utils.h"
static int my_checker(const struct lxc_arguments* args) static int my_checker(const struct lxc_arguments *args)
{ {
if (!args->states) { if (!args->states) {
lxc_error(args, "missing state option to wait for."); lxc_error(args, "missing state option to wait for.");
return -1; return -1;
} }
return 0; return 0;
} }
static int my_parser(struct lxc_arguments* args, int c, char* arg) static int my_parser(struct lxc_arguments *args, int c, char *arg)
{ {
switch (c) { switch (c) {
case 's': args->states = optarg; break; case 's': args->states = optarg; break;
case 't': args->timeout = atol(optarg); break; case 't': args->timeout = atol(optarg); break;
} }
return 0; return 0;
} }
...@@ -105,21 +107,23 @@ int main(int argc, char *argv[]) ...@@ -105,21 +107,23 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
if (!c->may_control(c)) { if (!c->may_control(c)) {
fprintf(stderr, "Insufficent privileges to control %s\n", c->name); lxc_error(&my_args, "Insufficent privileges to control %s", c->name);
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (my_args.rcfile) { if (my_args.rcfile) {
c->clear_config(c); c->clear_config(c);
if (!c->load_config(c, my_args.rcfile)) { if (!c->load_config(c, my_args.rcfile)) {
fprintf(stderr, "Failed to load rcfile\n"); lxc_error(&my_args, "Failed to load rcfile");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
c->configfile = strdup(my_args.rcfile); c->configfile = strdup(my_args.rcfile);
if (!c->configfile) { if (!c->configfile) {
fprintf(stderr, "Out of memory setting new config filename\n"); lxc_error(&my_args, "Out of memory setting new config filename");
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
...@@ -129,5 +133,6 @@ int main(int argc, char *argv[]) ...@@ -129,5 +133,6 @@ int main(int argc, char *argv[])
lxc_container_put(c); lxc_container_put(c);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
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