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);
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);
}
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;
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;
}
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:
/* 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) {
void lxc_abort(const char *name, struct lxc_handler *handler)
{
- lxc_setstate(name, ABORTING);
+ set_state(name, handler, ABORTING);
kill(handler->pid, SIGKILL);
}
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;
handler = lxc_init(name);
if (!handler) {
ERROR("failed to initialize the container");
- goto out;
+ return -1;
}
err = lxc_spawn(name, handler, argv);
*/
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;
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];
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);