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 <dlezcano@fr.ibm.com>
---
 src/lxc/create.c  |   10 ---------
 src/lxc/freezer.c |   10 +--------
 src/lxc/start.c   |   21 +++++++++++++------
 src/lxc/start.h   |    5 +++-
 src/lxc/state.c   |   58 ------------------------------------------------------
 src/lxc/state.h   |    2 -
 6 files changed, 20 insertions(+), 86 deletions(-)

Index: lxc/src/lxc/create.c
===================================================================
--- lxc.orig/src/lxc/create.c
+++ lxc/src/lxc/create.c
@@ -125,17 +125,7 @@ int lxc_create(const char *name, struct 
 	
 	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);
Index: lxc/src/lxc/freezer.c
===================================================================
--- lxc.orig/src/lxc/freezer.c
+++ lxc/src/lxc/freezer.c
@@ -74,17 +74,11 @@ static int freeze_unfreeze(const char *n
 
 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);
 }
 
Index: lxc/src/lxc/start.c
===================================================================
--- lxc.orig/src/lxc/start.c
+++ lxc/src/lxc/start.c
@@ -141,6 +141,13 @@ static int sigchld_handler(int fd, void 
 	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 
 		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 l
 	/* 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 l
 
 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 l
 		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 *co
 	handler = lxc_init(name);
 	if (!handler) {
 		ERROR("failed to initialize the container");
-		goto out;
+		return -1;
 	}
 
 	err = lxc_spawn(name, handler, argv);
Index: lxc/src/lxc/start.h
===================================================================
--- lxc.orig/src/lxc/start.h
+++ lxc/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;
Index: lxc/src/lxc/state.c
===================================================================
--- lxc.orig/src/lxc/state.c
+++ lxc/src/lxc/state.c
@@ -60,64 +60,6 @@ lxc_state_t lxc_str2state(const char *st
 	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];
Index: lxc/src/lxc/state.h
===================================================================
--- lxc.orig/src/lxc/state.h
+++ lxc/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);



