mirror of
git://github.com/lxc/lxc
synced 2025-08-30 18:29:33 +00:00
Add return error status in the different functions
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>
This commit is contained in:
@@ -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);
|
||||
|
@@ -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) {
|
||||
|
@@ -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) {
|
||||
|
@@ -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)
|
||||
|
@@ -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;
|
||||
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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;
|
||||
|
@@ -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*/
|
||||
|
@@ -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);
|
||||
|
Reference in New Issue
Block a user