Handle the lock error and show message to user

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>
This commit is contained in:
dlezcano 2008-11-17 17:55:49 +00:00
parent e2bcd7db5e
commit 1f3da8f86c
15 changed files with 72 additions and 85 deletions

View 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);

View 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;

View 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)) {

View File

@ -22,11 +22,14 @@
*/
#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];
}

View File

@ -24,6 +24,10 @@
#define __lxc_error_h
typedef enum {
LXC_SUCCESS, /* 0 == success ;) */
LXC_ERROR_LOCK,
LXC_ERROR_EMPTY,
LXC_ERROR_BUSY,
LXC_ERROR_ALREADY_EXISTS,

View 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

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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;
}

View 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)) {

View 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;
}

View 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);