]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
Handle the lock error and show message to user
authordlezcano <dlezcano>
Mon, 17 Nov 2008 17:55:49 +0000 (17:55 +0000)
committerdlezcano <dlezcano>
Mon, 17 Nov 2008 17:55:49 +0000 (17:55 +0000)
From: Daniel Lezcano <dlezcano@fr.ibm.com>

Handle the lock error and show message to user.

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
15 files changed:
src/lxc/checkpoint.c
src/lxc/create.c
src/lxc/destroy.c
src/lxc/error.c
src/lxc/error.h
src/lxc/lxc.h
src/lxc/lxc_create.c
src/lxc/lxc_destroy.c
src/lxc/lxc_execute.c
src/lxc/lxc_lock.c
src/lxc/lxc_start.c
src/lxc/lxc_stop.c
src/lxc/restart.c
src/lxc/start.c
src/lxc/stop.c

index 744801c62124829395ce8029cb2f38a6615a54a0..cec69b5ad31131d374d51eedbdc300741f534710 100644 (file)
@@ -36,6 +36,7 @@
 #include <netinet/in.h>
 #include <net/if.h>
 
+#include "error.h"
 #include <lxc.h>
 
 #define MAXPIDLEN 20
@@ -70,17 +71,13 @@ int lxc_checkpoint(const char *name, int cfd, unsigned long flags)
        size_t pid;
 
        lock = lxc_get_lock(name);
-       if (lock > 0) {
-               lxc_log_error("'%s' is not running", name);
+       if (lock >= 0) {
                lxc_put_lock(lock);
-               return -1;
+               return -LXC_ERROR_EMPTY;
        }
 
-       if (lock < 0) {
-               lxc_log_error("failed to acquire the lock on '%s':%s", 
-                             name, strerror(-lock));
-               return -1;
-       }
+       if (lock < 0 && lock != -EWOULDBLOCK)
+               return -LXC_ERROR_LOCK;
 
        snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
        fd = open(init, O_RDONLY);
index 98ec61afc9b5ed4a1d501707a00752df319631b0..ef7fa996d04b10542927244d86aabab5dc6ac38d 100644 (file)
@@ -56,17 +56,13 @@ static int create_lxc_directory(const char *dirname)
 {
        char path[MAXPATHLEN];
 
-       if (mkdir(LXCPATH, 0755) && errno != EEXIST) {
-               lxc_log_syserror("failed to created %s directory", LXCPATH);
-               return -1;
-       }
+       if (mkdir(LXCPATH, 0755) && errno != EEXIST)
+               return -errno;
 
        sprintf(path, LXCPATH "/%s", dirname);
 
-       if (mkdir(path, 0755)) {
-               lxc_log_syserror("failed to created %s directory", path);
-               return -1;
-       }
+       if (mkdir(path, 0755))
+               return -errno;
 
        return 0;
 }
@@ -94,25 +90,18 @@ static int remove_lxc_directory(const char *dirname)
 
 int lxc_create(const char *name, struct lxc_conf *conf)
 {
-       int lock, err = -LXC_ERROR_INTERNAL;
-
-       if (create_lxc_directory(name)) {
-               lxc_log_error("failed to create %s directory", name);
-               return -LXC_ERROR_INTERNAL;
-       }
+       int lock, err;
 
+       err = create_lxc_directory(name);
+       if (err < 0)
+               return err == -EEXIST ?
+                       -LXC_ERROR_ALREADY_EXISTS:LXC_ERROR_INTERNAL;
+       
        lock = lxc_get_lock(name);
-       if (!lock) {
-               lxc_log_error("'%s' is busy", name);
-               return -LXC_ERROR_ALREADY_EXISTS;
-       }
-
-       if (lock < 0) {
-               lxc_log_error("failed to acquire lock on '%s':%s",
-                             name, strerror(-lock));
-               goto err;
-       }
+       if (lock < 0)
+               return -LXC_ERROR_LOCK;
 
+       err = LXC_ERROR_INTERNAL;
        if (lxc_mkstate(name)) {
                lxc_log_error("failed to create the state file for %s", name);
                goto err;
index 88a47f5306fc8fd8e29ae678fe2fe58ad8afca3c..74584e41b381a36a9a8efd510a1053957e1a4a20 100644 (file)
@@ -52,15 +52,12 @@ int lxc_destroy(const char *name)
        char path[MAXPATHLEN];
 
        lock = lxc_get_lock(name);
-       if (!lock) {
-               lxc_log_error("'%s' is busy", name);
-               return -LXC_ERROR_BUSY;
-       }
-
        if (lock < 0) {
-               lxc_log_error("failed to acquire the lock for '%s':%s", 
-                             name, strerror(-lock));
-               goto out;
+               if (lock == -EWOULDBLOCK)
+                       return -LXC_ERROR_BUSY;
+               if (lock == -ENOENT)
+                       return -LXC_ERROR_NOT_FOUND;
+               return -LXC_ERROR_INTERNAL;
        }
 
        if (lxc_rmstate(name)) {
index 6e36eee8cdf27b4dcbe0bab5afa7d2a52c234d22..5673da09f465f89980d588a05511a29b286ae5d1 100644 (file)
  */
 
 #include <stdio.h>
+#include <stdlib.h>
 #include "error.h"
 
+static const char *const catalogue[] = {
 
-static char *catalogue[] = {
-       [LXC_ERROR_EMPTY] = "The container is not running",
+       [LXC_ERROR_LOCK] = "Failed to lock the container",
+
+       [LXC_ERROR_EMPTY] = "The container is empty",
        [LXC_ERROR_ALREADY_EXISTS] = "The container already exists",
        [LXC_ERROR_BUSY] = "The container is busy",
        [LXC_ERROR_NOT_FOUND] = "The container was not found",
@@ -50,7 +53,10 @@ static char *catalogue[] = {
 
 const char *const lxc_strerror(int error)
 {
-       if (error < 0 || error >= LXC_LAST_ERROR)
+       error = abs(error);
+
+       if (error >= LXC_LAST_ERROR)
                return NULL;
+
        return catalogue[error];
 }
index bd5258f1a46098cde138466481ea55212696be57..5f6b25e03f0a6262401766d9f581a528f7f89259 100644 (file)
 #define __lxc_error_h
 
 typedef enum {
+       LXC_SUCCESS, /* 0 == success ;) */
+
+       LXC_ERROR_LOCK,
+
        LXC_ERROR_EMPTY,
        LXC_ERROR_BUSY,
        LXC_ERROR_ALREADY_EXISTS,
index fa040589bd1800c93041c5fbe8a842e6352057f2..012ab3cc0885692b944d2523dc24c5882a6a8b85 100644 (file)
@@ -171,7 +171,7 @@ extern int lxc_cgroup_get(const char *name, const char *subsystem,
  * @error : the value of the error
  * Returns a string on success or NULL otherwise.
  */
-extern int lxc_strerror(int error);
+extern const char *const lxc_strerror(int error);
 
 /*
  * Checkpoint a container previously frozen
index 383972c508c97c930fb844a12e7496115727d8ea..dccdccfad47f64f9e5409dd2aa4eaa817b758aae 100644 (file)
@@ -46,7 +46,7 @@ int main(int argc, char *argv[])
 {
        const char *name = NULL, *file = NULL;
        struct lxc_conf lxc_conf;
-       int opt;
+       int err, opt;
 
        while ((opt = getopt(argc, argv, "f:n:")) != -1) {
                switch (opt) {
@@ -72,8 +72,9 @@ int main(int argc, char *argv[])
                return 1;
        }
 
-       if (lxc_create(name, &lxc_conf)) {
-               fprintf(stderr, "failed to create the container '%s'\n", name);
+       err = lxc_create(name, &lxc_conf);
+       if (err) {
+               fprintf(stderr, "%s\n", lxc_strerror(err));
                return 1;
        }
 
index 0d3908ab42f5d84e0d79c1ec50c46b9381e42c9b..012c0a8aa5fad932677e332c2939f0a716342f6b 100644 (file)
@@ -39,6 +39,7 @@ int main(int argc, char *argv[])
        char opt;
        char *name = NULL;
        int nbargs = 0;
+       int err;
 
        while ((opt = getopt(argc, argv, "n:")) != -1) {
                switch (opt) {
@@ -53,8 +54,9 @@ int main(int argc, char *argv[])
        if (!name)
                usage(argv[0]);
 
-       if (lxc_destroy(name)) {
-               fprintf(stderr, "failed to destroy '%s'\n", name);
+       err = lxc_destroy(name);
+       if (err) {
+               fprintf(stderr, "%s\n", lxc_strerror(err));
                return 1;
        }
 
index dc3cec1a1c874c2ea0207b346ba31e31cd862cf0..12d15c34dbd9144b8d188d5303a5f632e439efaa 100644 (file)
@@ -104,8 +104,9 @@ int main(int argc, char *argv[])
        for (opt = 0; opt < argc; opt++)
                args[nbargs++] = argv[optind++];
 
-       if (lxc_start(name, args)) {
-               fprintf(stderr, "failed to execute '%s'\n", name);
+       ret = lxc_start(name, args);
+       if (ret) {
+               fprintf(stderr, "%s\n", lxc_strerror(ret));
                goto out;
        }
 
index 2742f8445874d91b5fbe69fd5b7f8f705c095987..0eadfabc24dcfe90fcb9f8a797308a446a841198 100644 (file)
@@ -47,7 +47,7 @@ int lxc_get_lock(const char *name)
         fcntl(fd, F_SETFD, FD_CLOEXEC);
 
        if (flock(fd, LOCK_EX|LOCK_NB)) {
-               ret = errno == EWOULDBLOCK ? 0 : -errno;
+               ret = -errno;
                close(fd);
                goto out;
        }
index 1af2b0245b2d7aa58b1007b60ddb62af913d23c3..a4f84d1efadc3ca2cc09dca48b84a65b6bca32e5 100644 (file)
@@ -46,7 +46,7 @@ int main(int argc, char *argv[])
        char opt;
        char *name = NULL;
        char **args;
-       int nbargs = 0;
+       int err, nbargs = 0;
        char *default_args[] = {
                "/sbin/init",
                '\0',
@@ -72,8 +72,9 @@ int main(int argc, char *argv[])
        if (!name)
                usage(argv[0]);
 
-       if (lxc_start(name, args)) {
-               fprintf(stderr, "failed to start %s\n", name);
+       err = lxc_start(name, args);
+       if (err) {
+               fprintf(stderr, "%s\n", lxc_strerror(err));
                return 1;
        }
 
index 9d7de2a47f7da5c999831cc9d0ab3be1b2c17641..a63f800c104c3829f1cfc13499a0b58c298765b0 100644 (file)
@@ -38,7 +38,7 @@ int main(int argc, char *argv[])
 {
        char opt;
        char *name = NULL;
-       int nbargs = 0;
+       int err, nbargs = 0;
 
        while ((opt = getopt(argc, argv, "n:")) != -1) {
                switch (opt) {
@@ -53,8 +53,9 @@ int main(int argc, char *argv[])
        if (!name)
                usage(argv[0]);
 
-       if (lxc_stop(name)) {
-               fprintf(stderr, "failed to stop %s\n", name);
+       err = lxc_stop(name);
+       if (err) {
+               fprintf(stderr, "%s\n", lxc_strerror(err));
                return 1;
        }
 
index e78b6264882f593ffb743ce9ca4ac83bdd323a05..d1e9bf2831f23831abf3783920947ac37f6a8ced 100644 (file)
@@ -37,6 +37,7 @@
 #include <sys/wait.h>
 #include <sys/mount.h>
 
+#include "error.h"
 #include <lxc/lxc.h>
 
 LXC_TTY_HANDLER(SIGINT);
@@ -73,16 +74,10 @@ int lxc_restart(const char *name, int cfd, unsigned long flags)
        int clone_flags;
 
        lock = lxc_get_lock(name);
-       if (!lock) {
-               lxc_log_error("'%s' is busy", name);
-               return -1;
-       }
-
-       if (lock < 0) {
-               lxc_log_error("failed to acquire lock on '%s':%s",
-                             name, strerror(-lock));
-               return -1;
-       }
+       if (lock < 0)
+               return lock == -EWOULDBLOCK ? 
+                       -LXC_ERROR_BUSY : 
+                       -LXC_ERROR_LOCK;
 
        /* Begin the set the state to STARTING*/
        if (lxc_setstate(name, STARTING)) {
index 5c9cccaae0b96e8d69b4a1fef1d5d037a324202a..8d056fecf5d8a719255ea4017ed0629d380caaab 100644 (file)
@@ -55,20 +55,14 @@ int lxc_start(const char *name, char *argv[])
        int clone_flags;
 
        lock = lxc_get_lock(name);
-       if (!lock) {
-               lxc_log_error("'%s' is busy", name);
-               return -LXC_ERROR_BUSY;
-       }
-
-       if (lock < 0) {
-               lxc_log_error("failed to acquire lock on '%s':%s",
-                             name, strerror(-lock));
-               return -LXC_ERROR_INTERNAL;
-       }
+       if (lock < 0)
+               return lock == -EWOULDBLOCK ? 
+                       -LXC_ERROR_BUSY : 
+                       -LXC_ERROR_LOCK;
 
        /* Begin the set the state to STARTING*/
        if (lxc_setstate(name, STARTING)) {
-               lxc_log_error("failed to set state %s", lxc_state2str(STARTING));
+               lxc_log_error("failed to set state '%s'", lxc_state2str(STARTING));
                goto out;
        }
 
index 608fd33e2491ff4f96561a2d8188d0bf5e10bea1..4b359152ccb1a4c96af1918e224f1c2217a199da 100644 (file)
@@ -44,16 +44,15 @@ int lxc_stop(const char *name)
        size_t pid;
 
        lock = lxc_get_lock(name);
-       if (lock > 0) {
-               lxc_log_error("'%s' is not running", name);
+       if (lock >= 0) {
                lxc_put_lock(lock);
                return -LXC_ERROR_EMPTY;
        }
 
-       if (lock < 0) {
-               lxc_log_error("failed to acquire the lock on '%s':%s", 
-                             name, strerror(-lock));
-               return -LXC_ERROR_INTERNAL;
+       if (lock < 0 && lock != -EWOULDBLOCK) {
+               if (lock == -ENOENT)
+                       return -LXC_ERROR_NOT_FOUND;
+               return -LXC_ERROR_LOCK;
        }
 
        snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);