diff --git a/src/lxc/commands.c b/src/lxc/commands.c index 2c4d6036d..2776f032f 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -59,14 +59,23 @@ lxc_log_define(lxc_commands, lxc); -static int fill_sock_name(char *path, int len, const char *name) { - char *lxcpath = default_lxc_path(); +static int fill_sock_name(char *path, int len, const char *name, + const char *inpath) +{ + char *lxcpath = NULL; int ret; - if (!lxcpath) { - ERROR("Out of memory getting lxcpath"); - return -1; + + if (!inpath) { + lxcpath = default_lxc_path(); + if (!lxcpath) { + ERROR("Out of memory getting lxcpath"); + return -1; + } } - ret = snprintf(path, len, "%s/%s/command", lxcpath, name); + ret = snprintf(path, len, "%s/%s/command", lxcpath ? lxcpath : inpath, name); + if (lxcpath) + free(lxcpath); + if (ret < 0 || ret >= len) { ERROR("Name too long"); return -1; @@ -86,7 +95,7 @@ static int receive_answer(int sock, struct lxc_answer *answer) } static int __lxc_command(const char *name, struct lxc_command *command, - int *stopped, int stay_connected) + int *stopped, int stay_connected, const char *lxcpath) { int sock, ret = -1; char path[sizeof(((struct sockaddr_un *)0)->sun_path)] = { 0 }; @@ -94,7 +103,7 @@ static int __lxc_command(const char *name, struct lxc_command *command, int len; len = sizeof(path)-1; - if (fill_sock_name(offset, len, name)) + if (fill_sock_name(offset, len, name, lxcpath)) return -1; sock = lxc_af_unix_connect(path); @@ -129,19 +138,21 @@ out: } extern int lxc_command(const char *name, - struct lxc_command *command, int *stopped) + struct lxc_command *command, int *stopped, + const char *lxcpath) { - return __lxc_command(name, command, stopped, 0); + return __lxc_command(name, command, stopped, 0, lxcpath); } extern int lxc_command_connected(const char *name, - struct lxc_command *command, int *stopped) + struct lxc_command *command, int *stopped, + const char *lxcpath) { - return __lxc_command(name, command, stopped, 1); + return __lxc_command(name, command, stopped, 1, lxcpath); } -pid_t get_init_pid(const char *name) +pid_t get_init_pid(const char *name, const char *lxcpath) { struct lxc_command command = { .request = { .type = LXC_COMMAND_PID }, @@ -149,7 +160,7 @@ pid_t get_init_pid(const char *name) int ret, stopped = 0; - ret = lxc_command(name, &command, &stopped); + ret = lxc_command(name, &command, &stopped, lxcpath); if (ret < 0 && stopped) return -1; @@ -167,7 +178,7 @@ pid_t get_init_pid(const char *name) return command.answer.pid; } -int lxc_get_clone_flags(const char *name) +int lxc_get_clone_flags(const char *name, const char *lxcpath) { struct lxc_command command = { .request = { .type = LXC_COMMAND_CLONE_FLAGS }, @@ -175,7 +186,7 @@ int lxc_get_clone_flags(const char *name) int ret, stopped = 0; - ret = lxc_command(name, &command, &stopped); + ret = lxc_command(name, &command, &stopped, lxcpath); if (ret < 0 && stopped) return -1; @@ -300,7 +311,8 @@ out_close: goto out; } -extern int lxc_command_init(const char *name, struct lxc_handler *handler) +extern int lxc_command_init(const char *name, struct lxc_handler *handler, + const char *lxcpath) { int fd; char path[sizeof(((struct sockaddr_un *)0)->sun_path)] = { 0 }; @@ -308,7 +320,7 @@ extern int lxc_command_init(const char *name, struct lxc_handler *handler) int len; len = sizeof(path)-1; - if (fill_sock_name(offset, len, name)) + if (fill_sock_name(offset, len, name, lxcpath)) return -1; fd = lxc_af_unix_open(path, SOCK_STREAM, 0); diff --git a/src/lxc/commands.h b/src/lxc/commands.h index 0e1c8f96c..0b72cf13b 100644 --- a/src/lxc/commands.h +++ b/src/lxc/commands.h @@ -48,19 +48,20 @@ struct lxc_command { struct lxc_answer answer; }; -extern pid_t get_init_pid(const char *name); -extern int lxc_get_clone_flags(const char *name); +extern pid_t get_init_pid(const char *name, const char *lxcpath); +extern int lxc_get_clone_flags(const char *name, const char *lxcpath); extern int lxc_command(const char *name, struct lxc_command *command, - int *stopped); + int *stopped, const char *lxcpath); extern int lxc_command_connected(const char *name, struct lxc_command *command, - int *stopped); + int *stopped, const char *lxcpath); struct lxc_epoll_descr; struct lxc_handler; -extern int lxc_command_init(const char *name, struct lxc_handler *handler); +extern int lxc_command_init(const char *name, struct lxc_handler *handler, + const char *lxcpath); extern int lxc_command_mainloop_add(const char *name, struct lxc_epoll_descr *descr, struct lxc_handler *handler); diff --git a/src/lxc/console.c b/src/lxc/console.c index 88aac8462..cff7a92ef 100644 --- a/src/lxc/console.c +++ b/src/lxc/console.c @@ -46,14 +46,14 @@ lxc_log_define(lxc_console, lxc); -extern int lxc_console(const char *name, int ttynum, int *fd) +extern int lxc_console(const char *name, int ttynum, int *fd, const char *lxcpath) { int ret, stopped = 0; struct lxc_command command = { .request = { .type = LXC_COMMAND_TTY, .data = ttynum }, }; - ret = lxc_command_connected(name, &command, &stopped); + ret = lxc_command_connected(name, &command, &stopped, lxcpath); if (ret < 0 && stopped) { ERROR("'%s' is stopped", name); return -1; diff --git a/src/lxc/execute.c b/src/lxc/execute.c index 99800d084..18d2fb43a 100644 --- a/src/lxc/execute.c +++ b/src/lxc/execute.c @@ -127,7 +127,7 @@ static struct lxc_operations execute_start_ops = { }; int lxc_execute(const char *name, char *const argv[], int quiet, - struct lxc_conf *conf) + struct lxc_conf *conf, const char *lxcpath) { struct execute_args args = { .argv = argv, @@ -137,5 +137,5 @@ int lxc_execute(const char *name, char *const argv[], int quiet, if (lxc_check_inherited(conf, -1)) return -1; - return __lxc_start(name, conf, &execute_start_ops, &args); + return __lxc_start(name, conf, &execute_start_ops, &args, lxcpath); } diff --git a/src/lxc/lxc.h b/src/lxc/lxc.h index a651d0421..0e1ce632f 100644 --- a/src/lxc/lxc.h +++ b/src/lxc/lxc.h @@ -47,7 +47,8 @@ struct lxc_arguments; * @conf : configuration * Returns 0 on sucess, < 0 otherwise */ -extern int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf); +extern int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf, + const char *lxcpath); /* * Stop the container previously started with lxc_start, all @@ -55,7 +56,7 @@ extern int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf * @name : the name of the container * Returns 0 on success, < 0 otherwise */ -extern int lxc_stop(const char *name); +extern int lxc_stop(const char *name, const char *lxcpath); /* * Start the specified command inside an application container @@ -66,7 +67,7 @@ extern int lxc_stop(const char *name); * Returns 0 on sucess, < 0 otherwise */ extern int lxc_execute(const char *name, char *const argv[], int quiet, - struct lxc_conf *conf); + struct lxc_conf *conf, const char *lxcpath); /* * Open the monitoring mechanism for a specific container @@ -100,7 +101,7 @@ extern int lxc_monitor_close(int fd); * @fd : a pointer to a tty file descriptor * Returns 0 on sucess, < 0 otherwise */ -extern int lxc_console(const char *name, int ttynum, int *fd); +extern int lxc_console(const char *name, int ttynum, int *fd, const char *lxcpath); /* * Freeze all the tasks running inside the container @@ -121,7 +122,7 @@ extern int lxc_unfreeze(const char *name); * @name : the name of the container * Returns the state of the container on success, < 0 otherwise */ -extern lxc_state_t lxc_state(const char *name); +extern lxc_state_t lxc_state(const char *name, const char *lxcpath); /* * Set a specified value for a specified subsystem. The specified diff --git a/src/lxc/lxc_attach.c b/src/lxc/lxc_attach.c index 437a40080..b4ccf44fd 100644 --- a/src/lxc/lxc_attach.c +++ b/src/lxc/lxc_attach.c @@ -130,6 +130,8 @@ int main(int argc, char *argv[]) void *cgroup_data = NULL; uid_t uid; char *curdir; + /* TODO: add cmdline arg to set lxcpath */ + const char *lxcpath = NULL; ret = lxc_caps_init(); if (ret) @@ -144,7 +146,7 @@ int main(int argc, char *argv[]) if (ret) return ret; - init_pid = get_init_pid(my_args.name); + init_pid = get_init_pid(my_args.name, lxcpath); if (init_pid < 0) { ERROR("failed to get the init pid"); return -1; @@ -174,7 +176,7 @@ int main(int argc, char *argv[]) * by asking lxc-start */ if (namespace_flags == -1) { - namespace_flags = lxc_get_clone_flags(my_args.name); + namespace_flags = lxc_get_clone_flags(my_args.name, lxcpath); /* call failed */ if (namespace_flags == -1) { ERROR("failed to automatically determine the " diff --git a/src/lxc/lxc_console.c b/src/lxc/lxc_console.c index c263d0f81..8ff3f5ac3 100644 --- a/src/lxc/lxc_console.c +++ b/src/lxc/lxc_console.c @@ -182,6 +182,8 @@ int main(int argc, char *argv[]) int err, std_in = 1; struct lxc_epoll_descr descr; struct termios newtios, oldtios; + /* TODO: add cmdline arg to specify lxcpath */ + char *lxcpath = NULL; err = lxc_arguments_parse(&my_args, argc, argv); if (err) @@ -198,7 +200,7 @@ int main(int argc, char *argv[]) return -1; } - err = lxc_console(my_args.name, my_args.ttynum, &master); + err = lxc_console(my_args.name, my_args.ttynum, &master, lxcpath); if (err) goto out; diff --git a/src/lxc/lxc_execute.c b/src/lxc/lxc_execute.c index 7a926a274..3c76e2e93 100644 --- a/src/lxc/lxc_execute.c +++ b/src/lxc/lxc_execute.c @@ -143,5 +143,5 @@ int main(int argc, char *argv[]) if (lxc_config_define_load(&defines, conf)) return -1; - return lxc_execute(my_args.name, my_args.argv, my_args.quiet, conf); + return lxc_execute(my_args.name, my_args.argv, my_args.quiet, conf, NULL); } diff --git a/src/lxc/lxc_info.c b/src/lxc/lxc_info.c index 48c1370de..fb37b2f1b 100644 --- a/src/lxc/lxc_info.c +++ b/src/lxc/lxc_info.c @@ -74,6 +74,8 @@ Options :\n\ int main(int argc, char *argv[]) { int ret; + /* TODO: add lxcpath cmdline arg */ + const char *lxcpath = NULL; ret = lxc_arguments_parse(&my_args, argc, argv); if (ret) @@ -87,7 +89,7 @@ int main(int argc, char *argv[]) state = pid = true; if (state || test_state) { - ret = lxc_getstate(my_args.name); + ret = lxc_getstate(my_args.name, lxcpath); if (ret < 0) return 1; if (test_state) @@ -97,7 +99,7 @@ int main(int argc, char *argv[]) } if (pid) - printf("pid:%10d\n", get_init_pid(my_args.name)); + printf("pid:%10d\n", get_init_pid(my_args.name, lxcpath)); return 0; } diff --git a/src/lxc/lxc_kill.c b/src/lxc/lxc_kill.c index f9bfe34b6..669f469ff 100644 --- a/src/lxc/lxc_kill.c +++ b/src/lxc/lxc_kill.c @@ -56,6 +56,8 @@ int main(int argc, char *argv[], char *envp[]) int ret; pid_t pid; int sig; + /* TODO: add lxcpath cmdline arg */ + const char *lxcpath = NULL; ret = lxc_arguments_parse(&my_args, argc, argv); if (ret) @@ -76,7 +78,7 @@ int main(int argc, char *argv[], char *envp[]) } else sig=SIGKILL; - pid = get_init_pid(my_args.name); + pid = get_init_pid(my_args.name, lxcpath); if (pid < 0) { ERROR("failed to get the init pid"); return -1; diff --git a/src/lxc/lxc_start.c b/src/lxc/lxc_start.c index c50c36b7c..aac7fe781 100644 --- a/src/lxc/lxc_start.c +++ b/src/lxc/lxc_start.c @@ -150,6 +150,8 @@ int main(int argc, char *argv[]) '\0', }; FILE *pid_fp = NULL; + /* TODO: add cmdline arg to specify lxcpath */ + char *lxcpath = NULL; lxc_list_init(&defines); @@ -258,7 +260,7 @@ int main(int argc, char *argv[]) if (my_args.close_all_fds) conf->close_all_fds = 1; - err = lxc_start(my_args.name, args, conf); + err = lxc_start(my_args.name, args, conf, lxcpath); /* * exec ourself, that requires to have all opened fd diff --git a/src/lxc/lxc_stop.c b/src/lxc/lxc_stop.c index 749d78a65..703726a2d 100644 --- a/src/lxc/lxc_stop.c +++ b/src/lxc/lxc_stop.c @@ -50,6 +50,9 @@ Options :\n\ int main(int argc, char *argv[]) { + /* TODO - make lxcpath a cmdline arg */ + const char *lxcpath = NULL; + if (lxc_arguments_parse(&my_args, argc, argv)) return -1; @@ -57,5 +60,5 @@ int main(int argc, char *argv[]) my_args.progname, my_args.quiet)) return -1; - return lxc_stop(my_args.name); + return lxc_stop(my_args.name, lxcpath); } diff --git a/src/lxc/lxccontainer.c b/src/lxc/lxccontainer.c index 733cbb68c..3b816e5d0 100644 --- a/src/lxc/lxccontainer.c +++ b/src/lxc/lxccontainer.c @@ -161,7 +161,7 @@ static const char *lxcapi_state(struct lxc_container *c) return NULL; if (lxclock(c->slock, 0)) return NULL; - s = lxc_getstate(c->name); + s = lxc_getstate(c->name, c->config_path); ret = lxc_state2str(s); lxcunlock(c->slock); @@ -171,7 +171,7 @@ static const char *lxcapi_state(struct lxc_container *c) static bool is_stopped_nolock(struct lxc_container *c) { lxc_state_t s; - s = lxc_getstate(c->name); + s = lxc_getstate(c->name, c->config_path); return (s == STOPPED); } @@ -225,7 +225,7 @@ static pid_t lxcapi_init_pid(struct lxc_container *c) if (lxclock(c->slock, 0)) return -1; - ret = get_init_pid(c->name); + ret = get_init_pid(c->name, c->config_path); lxcunlock(c->slock); return ret; } @@ -324,7 +324,7 @@ static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv lxcunlock(c->privlock); if (useinit) { - ret = lxc_execute(c->name, argv, 1, conf); + ret = lxc_execute(c->name, argv, 1, conf, c->config_path); return ret == 0 ? true : false; } @@ -386,7 +386,7 @@ static bool lxcapi_start(struct lxc_container *c, int useinit, char * const argv reboot: conf->reboot = 0; - ret = lxc_start(c->name, argv, conf); + ret = lxc_start(c->name, argv, conf, c->config_path); if (conf->reboot) { INFO("container requested reboot"); @@ -464,7 +464,7 @@ static bool lxcapi_stop(struct lxc_container *c) if (!c) return false; - ret = lxc_stop(c->name); + ret = lxc_stop(c->name, c->config_path); return ret == 0; } diff --git a/src/lxc/restart.c b/src/lxc/restart.c index a05483864..d0b8fa8d4 100644 --- a/src/lxc/restart.c +++ b/src/lxc/restart.c @@ -70,9 +70,11 @@ int lxc_restart(const char *name, int sfd, struct lxc_conf *conf, int flags) .sfd = sfd, .flags = flags }; + /* TODO - make lxcpath a cmdline arg */ + const char *lxcpath = NULL; if (lxc_check_inherited(conf, sfd)) return -1; - return __lxc_start(name, conf, &restart_ops, &restart_arg); + return __lxc_start(name, conf, &restart_ops, &restart_arg, lxcpath); } diff --git a/src/lxc/start.c b/src/lxc/start.c index 5083b24c9..139be0883 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -363,7 +363,7 @@ out_sigfd: extern int lxc_caps_check(void); -struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf) +struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf, const char *lxcpath) { struct lxc_handler *handler; @@ -387,7 +387,7 @@ struct lxc_handler *lxc_init(const char *name, struct lxc_conf *conf) goto out_free; } - if (lxc_command_init(name, handler)) + if (lxc_command_init(name, handler, lxcpath)) goto out_free_name; if (lxc_read_seccomp_config(conf) != 0) { @@ -835,13 +835,13 @@ out_abort: } int __lxc_start(const char *name, struct lxc_conf *conf, - struct lxc_operations* ops, void *data) + struct lxc_operations* ops, void *data, const char *lxcpath) { struct lxc_handler *handler; int err = -1; int status; - handler = lxc_init(name, conf); + handler = lxc_init(name, conf, lxcpath); if (!handler) { ERROR("failed to initialize the container"); return -1; @@ -940,7 +940,8 @@ static struct lxc_operations start_ops = { .post_start = post_start }; -int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf) +int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf, + const char *lxcpath) { struct start_args start_arg = { .argv = argv, @@ -950,5 +951,5 @@ int lxc_start(const char *name, char *const argv[], struct lxc_conf *conf) return -1; conf->need_utmp_watch = 1; - return __lxc_start(name, conf, &start_ops, &start_arg); + return __lxc_start(name, conf, &start_ops, &start_arg, lxcpath); } diff --git a/src/lxc/start.h b/src/lxc/start.h index 27688f386..8c8cbafff 100644 --- a/src/lxc/start.h +++ b/src/lxc/start.h @@ -52,7 +52,7 @@ struct lxc_handler { int pinfd; }; -extern struct lxc_handler *lxc_init(const char *name, struct lxc_conf *); +extern struct lxc_handler *lxc_init(const char *name, struct lxc_conf *, const char *); extern int lxc_spawn(struct lxc_handler *); extern int lxc_poll(const char *name, struct lxc_handler *handler); @@ -61,7 +61,7 @@ extern void lxc_fini(const char *name, struct lxc_handler *handler); extern int lxc_set_state(const char *, struct lxc_handler *, lxc_state_t); extern int lxc_check_inherited(struct lxc_conf *conf, int fd_to_ignore); int __lxc_start(const char *, struct lxc_conf *, struct lxc_operations *, - void *); + void *, const char *); #endif diff --git a/src/lxc/state.c b/src/lxc/state.c index 7ce4b533c..8552522dd 100644 --- a/src/lxc/state.c +++ b/src/lxc/state.c @@ -97,7 +97,7 @@ static int freezer_state(const char *name) return lxc_str2state(status); } -static lxc_state_t __lxc_getstate(const char *name) +static lxc_state_t __lxc_getstate(const char *name, const char *lxcpath) { struct lxc_command command = { .request = { .type = LXC_COMMAND_STATE }, @@ -105,7 +105,7 @@ static lxc_state_t __lxc_getstate(const char *name) int ret, stopped = 0; - ret = lxc_command(name, &command, &stopped); + ret = lxc_command(name, &command, &stopped, lxcpath); if (ret < 0 && stopped) return STOPPED; @@ -130,11 +130,11 @@ static lxc_state_t __lxc_getstate(const char *name) return command.answer.ret; } -lxc_state_t lxc_getstate(const char *name) +lxc_state_t lxc_getstate(const char *name, const char *lxcpath) { int state = freezer_state(name); if (state != FROZEN && state != FREEZING) - state = __lxc_getstate(name); + state = __lxc_getstate(name, lxcpath); return state; } @@ -196,6 +196,8 @@ extern int lxc_wait(const char *lxcname, const char *states, int timeout) struct lxc_msg msg; int state, ret; int s[MAX_STATE] = { }, fd; + /* TODO: add cmdline arg to specify lxcpath */ + char *lxcpath = NULL; if (fillwaitedstates(states, s)) return -1; @@ -209,7 +211,7 @@ extern int lxc_wait(const char *lxcname, const char *states, int timeout) * then check if already in requested state */ ret = -1; - state = lxc_getstate(lxcname); + state = lxc_getstate(lxcname, lxcpath); if (state < 0) { goto out_close; } else if ((state >= 0) && (s[state])) { diff --git a/src/lxc/state.h b/src/lxc/state.h index df8070ac2..c995e55a8 100644 --- a/src/lxc/state.h +++ b/src/lxc/state.h @@ -29,7 +29,7 @@ typedef enum { } lxc_state_t; extern int lxc_rmstate(const char *name); -extern lxc_state_t lxc_getstate(const char *name); +extern lxc_state_t lxc_getstate(const char *name, const char *lxcpath); extern lxc_state_t lxc_str2state(const char *state); extern const char *lxc_state2str(lxc_state_t state); diff --git a/src/lxc/stop.c b/src/lxc/stop.c index 1cacdca18..fa1e37520 100644 --- a/src/lxc/stop.c +++ b/src/lxc/stop.c @@ -40,7 +40,7 @@ lxc_log_define(lxc_stop, lxc); -int lxc_stop(const char *name) +int lxc_stop(const char *name, const char *lxcpath) { struct lxc_command command = { .request = { .type = LXC_COMMAND_STOP }, @@ -48,7 +48,7 @@ int lxc_stop(const char *name) int ret, stopped = 0; - ret = lxc_command(name, &command,&stopped); + ret = lxc_command(name, &command,&stopped, lxcpath); if (ret < 0 && stopped) { INFO("'%s' is already stopped", name); return 0;