Commit 66aeffc7 by Daniel Lezcano

returns the state of the container with the af_unix socket

Like the pid, let's store the state in the handler and modify it at runtime. Return the value of state with a specific command. Signed-off-by: 's avatarDaniel Lezcano <dlezcano@fr.ibm.com>
parent 5379ce78
...@@ -125,17 +125,7 @@ int lxc_create(const char *name, struct lxc_conf *conf) ...@@ -125,17 +125,7 @@ int lxc_create(const char *name, struct lxc_conf *conf)
lock = lxc_get_lock(name); lock = lxc_get_lock(name);
if (lock < 0) if (lock < 0)
return err;
if (lxc_mkstate(name)) {
ERROR("failed to create the state file for %s", name);
goto err; goto err;
}
if (lxc_setstate(name, STOPPED)) {
ERROR("failed to set state for %s", name);
goto err_state;
}
if (lxc_configure(name, conf)) { if (lxc_configure(name, conf)) {
ERROR("failed to set configuration for %s", name); ERROR("failed to set configuration for %s", name);
......
...@@ -74,17 +74,11 @@ static int freeze_unfreeze(const char *name, int freeze) ...@@ -74,17 +74,11 @@ static int freeze_unfreeze(const char *name, int freeze)
int lxc_freeze(const char *name) int lxc_freeze(const char *name)
{ {
if (freeze_unfreeze(name, 1)) return freeze_unfreeze(name, 1);
return -1;
return lxc_setstate(name, FROZEN);
} }
int lxc_unfreeze(const char *name) int lxc_unfreeze(const char *name)
{ {
if (freeze_unfreeze(name, 0)) return freeze_unfreeze(name, 0);
return -1;
return lxc_setstate(name, RUNNING);
} }
...@@ -141,6 +141,13 @@ static int sigchld_handler(int fd, void *data, ...@@ -141,6 +141,13 @@ static int sigchld_handler(int fd, void *data,
return 1; return 1;
} }
static int set_state(const char *name, struct lxc_handler *handler, lxc_state_t state)
{
handler->state = state;
lxc_monitor_send_state(name, state);
return 0;
}
int lxc_poll(const char *name, struct lxc_handler *handler) int lxc_poll(const char *name, struct lxc_handler *handler)
{ {
int sigfd = handler->sigfd; int sigfd = handler->sigfd;
...@@ -245,7 +252,7 @@ struct lxc_handler *lxc_init(const char *name) ...@@ -245,7 +252,7 @@ struct lxc_handler *lxc_init(const char *name)
goto out_free; goto out_free;
/* Begin the set the state to STARTING*/ /* Begin the set the state to STARTING*/
if (lxc_setstate(name, STARTING)) { if (set_state(name, handler, STARTING)) {
ERROR("failed to set state '%s'", lxc_state2str(STARTING)); ERROR("failed to set state '%s'", lxc_state2str(STARTING));
goto out_put_lock; goto out_put_lock;
} }
...@@ -282,7 +289,7 @@ out: ...@@ -282,7 +289,7 @@ out:
out_delete_tty: out_delete_tty:
lxc_delete_tty(&handler->tty_info); lxc_delete_tty(&handler->tty_info);
out_aborting: out_aborting:
lxc_setstate(name, ABORTING); set_state(name, handler, ABORTING);
out_put_lock: out_put_lock:
lxc_put_lock(handler->lock); lxc_put_lock(handler->lock);
out_free: out_free:
...@@ -296,8 +303,8 @@ void lxc_fini(const char *name, struct lxc_handler *handler) ...@@ -296,8 +303,8 @@ void lxc_fini(const char *name, struct lxc_handler *handler)
/* The STOPPING state is there for future cleanup code /* The STOPPING state is there for future cleanup code
* which can take awhile * which can take awhile
*/ */
lxc_setstate(name, STOPPING); set_state(name, handler, STOPPING);
lxc_setstate(name, STOPPED); set_state(name, handler, STOPPED);
lxc_unlink_nsgroup(name); lxc_unlink_nsgroup(name);
if (handler) { if (handler) {
...@@ -313,7 +320,7 @@ void lxc_fini(const char *name, struct lxc_handler *handler) ...@@ -313,7 +320,7 @@ void lxc_fini(const char *name, struct lxc_handler *handler)
void lxc_abort(const char *name, struct lxc_handler *handler) void lxc_abort(const char *name, struct lxc_handler *handler)
{ {
lxc_setstate(name, ABORTING); set_state(name, handler, ABORTING);
kill(handler->pid, SIGKILL); kill(handler->pid, SIGKILL);
} }
...@@ -439,7 +446,7 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[]) ...@@ -439,7 +446,7 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[])
goto out_abort; goto out_abort;
} }
if (lxc_setstate(name, RUNNING)) { if (set_state(name, handler, RUNNING)) {
ERROR("failed to set state to %s", ERROR("failed to set state to %s",
lxc_state2str(RUNNING)); lxc_state2str(RUNNING));
goto out_abort; goto out_abort;
...@@ -469,7 +476,7 @@ int lxc_start(const char *name, char *const argv[]) ...@@ -469,7 +476,7 @@ int lxc_start(const char *name, char *const argv[])
handler = lxc_init(name); handler = lxc_init(name);
if (!handler) { if (!handler) {
ERROR("failed to initialize the container"); ERROR("failed to initialize the container");
goto out; return -1;
} }
err = lxc_spawn(name, handler, argv); err = lxc_spawn(name, handler, argv);
......
...@@ -22,9 +22,12 @@ ...@@ -22,9 +22,12 @@
*/ */
struct lxc_handler { struct lxc_handler {
pid_t pid;
lxc_state_t state;
int sigfd; int sigfd;
int lock; int lock;
pid_t pid;
char tty[MAXPATHLEN]; char tty[MAXPATHLEN];
sigset_t oldmask; sigset_t oldmask;
struct lxc_tty_info tty_info; struct lxc_tty_info tty_info;
......
...@@ -60,64 +60,6 @@ lxc_state_t lxc_str2state(const char *state) ...@@ -60,64 +60,6 @@ lxc_state_t lxc_str2state(const char *state)
return -1; return -1;
} }
int lxc_setstate(const char *name, lxc_state_t state)
{
int fd, err = -1;
char file[MAXPATHLEN];
const char *str = lxc_state2str(state);
if (!str)
return err;
snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
fd = open(file, O_WRONLY);
if (fd < 0) {
SYSERROR("failed to open %s file", file);
return err;
}
if (flock(fd, LOCK_EX)) {
SYSERROR("failed to take the lock to %s", file);
goto out;
}
if (ftruncate(fd, 0)) {
SYSERROR("failed to truncate the file %s", file);
goto out;
}
if (write(fd, str, strlen(str)) < 0) {
SYSERROR("failed to write state to %s", file);
goto out;
}
err = 0;
DEBUG("set state to '%s'", str);
out:
close(fd);
lxc_monitor_send_state(name, state);
return err;
}
int lxc_mkstate(const char *name)
{
int fd;
char file[MAXPATHLEN];
snprintf(file, MAXPATHLEN, LXCPATH "/%s/state", name);
fd = creat(file, S_IRUSR|S_IWUSR);
if (fd < 0) {
SYSERROR("failed to create file %s", file);
return -1;
}
close(fd);
return 0;
}
int lxc_rmstate(const char *name) int lxc_rmstate(const char *name)
{ {
char file[MAXPATHLEN]; char file[MAXPATHLEN];
......
...@@ -28,9 +28,7 @@ typedef enum { ...@@ -28,9 +28,7 @@ typedef enum {
ABORTING, FREEZING, FROZEN, MAX_STATE, ABORTING, FREEZING, FROZEN, MAX_STATE,
} lxc_state_t; } lxc_state_t;
extern int lxc_mkstate(const char *name);
extern int lxc_rmstate(const char *name); extern int lxc_rmstate(const char *name);
extern int lxc_setstate(const char *name, lxc_state_t state);
extern lxc_state_t lxc_getstate(const char *name); extern lxc_state_t lxc_getstate(const char *name);
extern lxc_state_t lxc_str2state(const char *state); extern lxc_state_t lxc_str2state(const char *state);
......
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