From: Daniel Lezcano Date: Wed, 7 Oct 2009 14:06:09 +0000 (+0200) Subject: returns the state of the container with the af_unix socket X-Git-Tag: lxc-2.1.1~3175 X-Git-Url: https://git.proxmox.com/?a=commitdiff_plain;h=66aeffc7218618890f02bb962b46fbd435443b55;p=mirror_lxc.git 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: Daniel Lezcano --- diff --git a/src/lxc/create.c b/src/lxc/create.c index 41ba5734a..afb920c1c 100644 --- a/src/lxc/create.c +++ b/src/lxc/create.c @@ -125,17 +125,7 @@ int lxc_create(const char *name, struct lxc_conf *conf) lock = lxc_get_lock(name); if (lock < 0) - return err; - - if (lxc_mkstate(name)) { - ERROR("failed to create the state file for %s", name); goto err; - } - - if (lxc_setstate(name, STOPPED)) { - ERROR("failed to set state for %s", name); - goto err_state; - } if (lxc_configure(name, conf)) { ERROR("failed to set configuration for %s", name); diff --git a/src/lxc/freezer.c b/src/lxc/freezer.c index c99a315fa..0c0f4059f 100644 --- a/src/lxc/freezer.c +++ b/src/lxc/freezer.c @@ -74,17 +74,11 @@ static int freeze_unfreeze(const char *name, int freeze) int lxc_freeze(const char *name) { - if (freeze_unfreeze(name, 1)) - return -1; - - return lxc_setstate(name, FROZEN); + return freeze_unfreeze(name, 1); } int lxc_unfreeze(const char *name) { - if (freeze_unfreeze(name, 0)) - return -1; - - return lxc_setstate(name, RUNNING); + return freeze_unfreeze(name, 0); } diff --git a/src/lxc/start.c b/src/lxc/start.c index 580b46047..b7c8903a6 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -141,6 +141,13 @@ static int sigchld_handler(int fd, void *data, 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 sigfd = handler->sigfd; @@ -245,7 +252,7 @@ struct lxc_handler *lxc_init(const char *name) goto out_free; /* 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)); goto out_put_lock; } @@ -282,7 +289,7 @@ out: out_delete_tty: lxc_delete_tty(&handler->tty_info); out_aborting: - lxc_setstate(name, ABORTING); + set_state(name, handler, ABORTING); out_put_lock: lxc_put_lock(handler->lock); out_free: @@ -296,8 +303,8 @@ void lxc_fini(const char *name, struct lxc_handler *handler) /* The STOPPING state is there for future cleanup code * which can take awhile */ - lxc_setstate(name, STOPPING); - lxc_setstate(name, STOPPED); + set_state(name, handler, STOPPING); + set_state(name, handler, STOPPED); lxc_unlink_nsgroup(name); if (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) { - lxc_setstate(name, ABORTING); + set_state(name, handler, ABORTING); kill(handler->pid, SIGKILL); } @@ -439,7 +446,7 @@ int lxc_spawn(const char *name, struct lxc_handler *handler, char *const argv[]) goto out_abort; } - if (lxc_setstate(name, RUNNING)) { + if (set_state(name, handler, RUNNING)) { ERROR("failed to set state to %s", lxc_state2str(RUNNING)); goto out_abort; @@ -469,7 +476,7 @@ int lxc_start(const char *name, char *const argv[]) handler = lxc_init(name); if (!handler) { ERROR("failed to initialize the container"); - goto out; + return -1; } err = lxc_spawn(name, handler, argv); diff --git a/src/lxc/start.h b/src/lxc/start.h index 5206f8e18..a2ab31377 100644 --- a/src/lxc/start.h +++ b/src/lxc/start.h @@ -22,9 +22,12 @@ */ struct lxc_handler { + + pid_t pid; + lxc_state_t state; + int sigfd; int lock; - pid_t pid; char tty[MAXPATHLEN]; sigset_t oldmask; struct lxc_tty_info tty_info; diff --git a/src/lxc/state.c b/src/lxc/state.c index e164e0e52..1f338c81f 100644 --- a/src/lxc/state.c +++ b/src/lxc/state.c @@ -60,64 +60,6 @@ lxc_state_t lxc_str2state(const char *state) 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) { char file[MAXPATHLEN]; diff --git a/src/lxc/state.h b/src/lxc/state.h index 871d1a8d5..c56860d70 100644 --- a/src/lxc/state.h +++ b/src/lxc/state.h @@ -28,9 +28,7 @@ typedef enum { ABORTING, FREEZING, FROZEN, MAX_STATE, } lxc_state_t; -extern int lxc_mkstate(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_str2state(const char *state);