]> git.proxmox.com Git - mirror_lxc.git/commitdiff
returns the state of the container with the af_unix socket
authorDaniel Lezcano <dlezcano@fr.ibm.com>
Wed, 7 Oct 2009 14:06:09 +0000 (16:06 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Wed, 7 Oct 2009 14:06:09 +0000 (16:06 +0200)
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
src/lxc/freezer.c
src/lxc/start.c
src/lxc/start.h
src/lxc/state.c
src/lxc/state.h

index 41ba5734ab494647361dcf625524fc4ea683b6c4..afb920c1c17d423b66cfc5a85a1372b4f0c9d629 100644 (file)
@@ -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);
index c99a315fa85cad4150f498eff8568da7312a33a8..0c0f4059ff4338277efa081ddf2be6774b2b5b6f 100644 (file)
@@ -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);
 }
 
index 580b46047fa8d3322f1f682589a9e297496260f2..b7c8903a62b5c5d3f86589af5cfe875a893b0f4f 100644 (file)
@@ -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);
index 5206f8e18c67ec5346f1301848857792178429b1..a2ab313774a0a30fd34c77dfaaeff83be96ef2ed 100644 (file)
  */
 
 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 e164e0e52a7cd6d6f59fffc723aba1f520f81ce2..1f338c81f136079c918ce814c6843d96f733e7ab 100644 (file)
@@ -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];
index 871d1a8d58c12556a73c3eda30ea97982c26cc6e..c56860d70793e8352a4213094fb4d53223c2e079 100644 (file)
@@ -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);