From e2bcd7db5e53dc20a25293283ff7c9404b4d0e2c Mon Sep 17 00:00:00 2001 From: dlezcano Date: Mon, 17 Nov 2008 16:01:34 +0000 Subject: [PATCH] Add return error status in the different functions From: Daniel Lezcano 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 --- src/lxc/cgroup.c | 5 +++-- src/lxc/create.c | 7 ++++--- src/lxc/destroy.c | 5 +++-- src/lxc/error.c | 3 +++ src/lxc/error.h | 7 +++++-- src/lxc/freezer.c | 9 +++++---- src/lxc/monitor.c | 5 +++-- src/lxc/start.c | 8 +++++--- src/lxc/stop.c | 7 ++++--- 9 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/lxc/cgroup.c b/src/lxc/cgroup.c index fc0a22200..ebca4917f 100644 --- a/src/lxc/cgroup.c +++ b/src/lxc/cgroup.c @@ -36,6 +36,7 @@ #include #include +#include "error.h" #include #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); diff --git a/src/lxc/create.c b/src/lxc/create.c index bcf661ff6..98ec61afc 100644 --- a/src/lxc/create.c +++ b/src/lxc/create.c @@ -30,6 +30,7 @@ #include #include +#include "error.h" #include 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) { diff --git a/src/lxc/destroy.c b/src/lxc/destroy.c index 9eb206f61..88a47f530 100644 --- a/src/lxc/destroy.c +++ b/src/lxc/destroy.c @@ -29,6 +29,7 @@ #include #include +#include "error.h" #include 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) { diff --git a/src/lxc/error.c b/src/lxc/error.c index a98fdf815..6e36eee8c 100644 --- a/src/lxc/error.c +++ b/src/lxc/error.c @@ -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) diff --git a/src/lxc/error.h b/src/lxc/error.h index 295e8cd31..bd5258f1a 100644 --- a/src/lxc/error.h +++ b/src/lxc/error.h @@ -20,12 +20,13 @@ * 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; diff --git a/src/lxc/freezer.c b/src/lxc/freezer.c index edb77fb2f..9c774cc23 100644 --- a/src/lxc/freezer.c +++ b/src/lxc/freezer.c @@ -31,6 +31,7 @@ #include #include +#include "error.h" #include 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; } diff --git a/src/lxc/monitor.c b/src/lxc/monitor.c index ff5e08ba5..98195c8e8 100644 --- a/src/lxc/monitor.c +++ b/src/lxc/monitor.c @@ -36,6 +36,7 @@ #include #include +#include "error.h" #include #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; diff --git a/src/lxc/start.c b/src/lxc/start.c index bf233cd2e..5c9cccaae 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -38,6 +38,8 @@ #include #include +#include "error.h" + #include 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*/ diff --git a/src/lxc/stop.c b/src/lxc/stop.c index 2d050f3cd..608fd33e2 100644 --- a/src/lxc/stop.c +++ b/src/lxc/stop.c @@ -31,6 +31,7 @@ #include #include +#include "error.h" #include #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); -- 2.39.2