]> git.proxmox.com Git - mirror_lxc.git/commitdiff
Add return error status in the different functions
authordlezcano <dlezcano>
Mon, 17 Nov 2008 16:01:34 +0000 (16:01 +0000)
committerdlezcano <dlezcano>
Mon, 17 Nov 2008 16:01:34 +0000 (16:01 +0000)
From: Daniel Lezcano <dlezcano@fr.ibm.com>

Add the most known error to the different API to be followed up by the
caller, so we can later show a better message to the user when something
goes wrong. The error catching is coarse grain right now but will be improved,
step by step.

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
src/lxc/cgroup.c
src/lxc/create.c
src/lxc/destroy.c
src/lxc/error.c
src/lxc/error.h
src/lxc/freezer.c
src/lxc/monitor.c
src/lxc/start.c
src/lxc/stop.c

index fc0a22200a80c933df50c8f5519fe1d7b021ae73..ebca4917f7840fc07013753bcead608b744ba7af 100644 (file)
@@ -36,6 +36,7 @@
 #include <netinet/in.h>
 #include <net/if.h>
 
+#include "error.h"
 #include <lxc/lxc.h>
 
 #define MTAB "/etc/mtab"
@@ -98,7 +99,7 @@ int lxc_unlink_nsgroup(const char *name)
 
 int lxc_cgroup_set(const char *name, const char *subsystem, const char *value)
 {
-       int fd, ret = -1;;
+       int fd, ret = -LXC_ERROR_INTERNAL;;
        char path[MAXPATHLEN];
 
         snprintf(path, MAXPATHLEN, LXCPATH "/%s/nsgroup/%s", name, subsystem);
@@ -119,7 +120,7 @@ out:
 int lxc_cgroup_get(const char *name, const char *subsystem,  
                   char *value, size_t len)
 {
-       int fd, ret = -1;;
+       int fd, ret = -LXC_ERROR_INTERNAL;
        char path[MAXPATHLEN];
 
         snprintf(path, MAXPATHLEN, LXCPATH "/%s/nsgroup/%s", name, subsystem);
index bcf661ff669b8d23e9a7b4835387180f247c6ef4..98ec61afc9b5ed4a1d501707a00752df319631b0 100644 (file)
@@ -30,6 +30,7 @@
 #include <sys/stat.h>
 #include <sys/types.h>
 
+#include "error.h"
 #include <lxc/lxc.h>
 
 static int dir_filter(const struct dirent *dirent)
@@ -93,17 +94,17 @@ static int remove_lxc_directory(const char *dirname)
 
 int lxc_create(const char *name, struct lxc_conf *conf)
 {
-       int lock, err = -1;
+       int lock, err = -LXC_ERROR_INTERNAL;
 
        if (create_lxc_directory(name)) {
                lxc_log_error("failed to create %s directory", name);
-               return -1;
+               return -LXC_ERROR_INTERNAL;
        }
 
        lock = lxc_get_lock(name);
        if (!lock) {
                lxc_log_error("'%s' is busy", name);
-               goto err;
+               return -LXC_ERROR_ALREADY_EXISTS;
        }
 
        if (lock < 0) {
index 9eb206f61c51cf64448465cb5aa0405446e0463d..88a47f5306fc8fd8e29ae678fe2fe58ad8afca3c 100644 (file)
@@ -29,6 +29,7 @@
 #include <errno.h>
 #include <sys/param.h>
 
+#include "error.h"
 #include <lxc/lxc.h>
 
 static int remove_lxc_directory(const char *dirname)
@@ -47,13 +48,13 @@ static int remove_lxc_directory(const char *dirname)
 
 int lxc_destroy(const char *name)
 {
-       int ret = -1, lock;
+       int lock, ret = -LXC_ERROR_INTERNAL;
        char path[MAXPATHLEN];
 
        lock = lxc_get_lock(name);
        if (!lock) {
                lxc_log_error("'%s' is busy", name);
-               goto out;
+               return -LXC_ERROR_BUSY;
        }
 
        if (lock < 0) {
index a98fdf815d616ee2eb8fa04217eb076ad1e05f10..6e36eee8cdf27b4dcbe0bab5afa7d2a52c234d22 100644 (file)
@@ -27,6 +27,7 @@
 
 static char *catalogue[] = {
        [LXC_ERROR_EMPTY] = "The container is not running",
+       [LXC_ERROR_ALREADY_EXISTS] = "The container already exists",
        [LXC_ERROR_BUSY] = "The container is busy",
        [LXC_ERROR_NOT_FOUND] = "The container was not found",
        [LXC_ERROR_PERMISSION_DENIED] = "Permission denied",
@@ -43,6 +44,8 @@ static char *catalogue[] = {
        [LXC_ERROR_SETUP_UTSNAME] = "Failed to setup the utsname",
        [LXC_ERROR_SETUP_NETWORK] = "Failed to setup the network",
        [LXC_ERROR_SETUP_ROOTFS] = "Failed to setup the root fs",
+
+       [LXC_ERROR_INTERNAL] = "Internal system error",
 };
 
 const char *const lxc_strerror(int error)
index 295e8cd31edd7b2f1bc088ad2c199c3c0a5332c9..bd5258f1a46098cde138466481ea55212696be57 100644 (file)
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  */
-#ifndef __lxc_h
-#define __lxc_h
+#ifndef __lxc_error_h
+#define __lxc_error_h
 
 typedef enum {
        LXC_ERROR_EMPTY,
        LXC_ERROR_BUSY,
+       LXC_ERROR_ALREADY_EXISTS,
        LXC_ERROR_NOT_FOUND,
        LXC_ERROR_PERMISSION_DENIED,
        LXC_ERROR_WRONG_COMMAND,
@@ -42,6 +43,8 @@ typedef enum {
        LXC_ERROR_SETUP_NETWORK,
        LXC_ERROR_SETUP_ROOTFS,
 
+       LXC_ERROR_INTERNAL,
+
        LXC_LAST_ERROR,
 } lxc_error_t;
 
index edb77fb2f86996c051bfb20f2fcb133b02f67433..9c774cc23e447bedfd0b7261c3f4005e4eedcd89 100644 (file)
@@ -31,6 +31,7 @@
 #include <sys/types.h>
 #include <sys/param.h>
 
+#include "error.h"
 #include <lxc/lxc.h>
 
 static int freeze_unfreeze(const char *name, int freeze)
@@ -58,10 +59,10 @@ static int freeze_unfreeze(const char *name, int freeze)
 int lxc_freeze(const char *name)
 {
        if (freeze_unfreeze(name, 1))
-               return -1;
+               return -LXC_ERROR_INTERNAL;
 
        if (lxc_setstate(name, FROZEN))
-               return -1;
+               return -LXC_ERROR_INTERNAL;
 
        return 0;
 }
@@ -69,10 +70,10 @@ int lxc_freeze(const char *name)
 int lxc_unfreeze(const char *name)
 {
        if (freeze_unfreeze(name, 0))
-               return -1;
+               return -LXC_ERROR_INTERNAL;
 
        if (lxc_setstate(name, RUNNING))
-               return -1;
+               return -LXC_ERROR_INTERNAL;
 
        return 0;
 }
index ff5e08ba5e0f9f02f59353b1a258766a73f58f53..98195c8e861153b0053a9b39fccbb806dd90c6a5 100644 (file)
@@ -36,6 +36,7 @@
 #include <linux/netlink.h>
 #include <linux/rtnetlink.h>
 
+#include "error.h"
 #include <lxc/lxc.h>
 
 #ifndef UNIX_PATH_MAX
@@ -151,7 +152,7 @@ int lxc_monitor_open(void)
        fd = socket(PF_NETLINK, SOCK_RAW, 0);
        if (fd < 0) {
                lxc_log_syserror("failed to create notification socket");
-               return -1;
+               return -LXC_ERROR_INTERNAL;
        }
 
        memset(&addr, 0, sizeof(addr));
@@ -180,7 +181,7 @@ int lxc_monitor_read(int fd, struct lxc_msg *msg)
                       (struct sockaddr *)&from, &len);
        if (ret < 0) {
                lxc_log_syserror("failed to received state");
-               return -1;
+               return -LXC_ERROR_INTERNAL;
        }
 
        return ret;
index bf233cd2efc481dcf875384f9da1d8d4d5b2906f..5c9cccaae0b96e8d69b4a1fef1d5d037a324202a 100644 (file)
@@ -38,6 +38,8 @@
 #include <sys/capability.h>
 #include <sys/wait.h>
 
+#include "error.h"
+
 #include <lxc/lxc.h>
 
 LXC_TTY_HANDLER(SIGINT);
@@ -48,20 +50,20 @@ int lxc_start(const char *name, char *argv[])
        char init[MAXPATHLEN];
        char *val = NULL;
        char ttyname[MAXPATHLEN];
-       int fd, lock, sv[2], sync = 0, err = -1;
+       int fd, lock, sv[2], sync = 0, err = -LXC_ERROR_INTERNAL;
        pid_t pid;
        int clone_flags;
 
        lock = lxc_get_lock(name);
        if (!lock) {
                lxc_log_error("'%s' is busy", name);
-               return -1;
+               return -LXC_ERROR_BUSY;
        }
 
        if (lock < 0) {
                lxc_log_error("failed to acquire lock on '%s':%s",
                              name, strerror(-lock));
-               return -1;
+               return -LXC_ERROR_INTERNAL;
        }
 
        /* Begin the set the state to STARTING*/
index 2d050f3cd09d2d3d65fe94c6856a5e3ee6874d2a..608fd33e2491ff4f96561a2d8188d0bf5e10bea1 100644 (file)
@@ -31,6 +31,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 
+#include "error.h"
 #include <lxc/lxc.h>
 
 #define MAXPIDLEN 20
@@ -39,20 +40,20 @@ int lxc_stop(const char *name)
 {
        char init[MAXPATHLEN];
        char val[MAXPIDLEN];
-       int fd, lock, ret = -1;
+       int fd, lock, ret = -LXC_ERROR_INTERNAL;
        size_t pid;
 
        lock = lxc_get_lock(name);
        if (lock > 0) {
                lxc_log_error("'%s' is not running", name);
                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;
+               return -LXC_ERROR_INTERNAL;
        }
 
        snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);