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 <netinet/in.h>
#include <net/if.h> #include <net/if.h>
#include "error.h"
#include <lxc.h> #include <lxc.h>
#define MAXPIDLEN 20 #define MAXPIDLEN 20
@ -70,17 +71,13 @@ int lxc_checkpoint(const char *name, int cfd, unsigned long flags)
size_t pid; size_t pid;
lock = lxc_get_lock(name); lock = lxc_get_lock(name);
if (lock > 0) { if (lock >= 0) {
lxc_log_error("'%s' is not running", name);
lxc_put_lock(lock); lxc_put_lock(lock);
return -1; return -LXC_ERROR_EMPTY;
} }
if (lock < 0) { if (lock < 0 && lock != -EWOULDBLOCK)
lxc_log_error("failed to acquire the lock on '%s':%s", return -LXC_ERROR_LOCK;
name, strerror(-lock));
return -1;
}
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name); snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);
fd = open(init, O_RDONLY); fd = open(init, O_RDONLY);

View File

@ -56,17 +56,13 @@ static int create_lxc_directory(const char *dirname)
{ {
char path[MAXPATHLEN]; char path[MAXPATHLEN];
if (mkdir(LXCPATH, 0755) && errno != EEXIST) { if (mkdir(LXCPATH, 0755) && errno != EEXIST)
lxc_log_syserror("failed to created %s directory", LXCPATH); return -errno;
return -1;
}
sprintf(path, LXCPATH "/%s", dirname); sprintf(path, LXCPATH "/%s", dirname);
if (mkdir(path, 0755)) { if (mkdir(path, 0755))
lxc_log_syserror("failed to created %s directory", path); return -errno;
return -1;
}
return 0; 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 lxc_create(const char *name, struct lxc_conf *conf)
{ {
int lock, err = -LXC_ERROR_INTERNAL; int lock, err;
if (create_lxc_directory(name)) { err = create_lxc_directory(name);
lxc_log_error("failed to create %s directory", name); if (err < 0)
return -LXC_ERROR_INTERNAL; return err == -EEXIST ?
} -LXC_ERROR_ALREADY_EXISTS:LXC_ERROR_INTERNAL;
lock = lxc_get_lock(name); lock = lxc_get_lock(name);
if (!lock) { if (lock < 0)
lxc_log_error("'%s' is busy", name); return -LXC_ERROR_LOCK;
return -LXC_ERROR_ALREADY_EXISTS;
}
if (lock < 0) {
lxc_log_error("failed to acquire lock on '%s':%s",
name, strerror(-lock));
goto err;
}
err = LXC_ERROR_INTERNAL;
if (lxc_mkstate(name)) { if (lxc_mkstate(name)) {
lxc_log_error("failed to create the state file for %s", name); lxc_log_error("failed to create the state file for %s", name);
goto err; goto err;

View File

@ -52,15 +52,12 @@ int lxc_destroy(const char *name)
char path[MAXPATHLEN]; char path[MAXPATHLEN];
lock = lxc_get_lock(name); lock = lxc_get_lock(name);
if (!lock) {
lxc_log_error("'%s' is busy", name);
return -LXC_ERROR_BUSY;
}
if (lock < 0) { if (lock < 0) {
lxc_log_error("failed to acquire the lock for '%s':%s", if (lock == -EWOULDBLOCK)
name, strerror(-lock)); return -LXC_ERROR_BUSY;
goto out; if (lock == -ENOENT)
return -LXC_ERROR_NOT_FOUND;
return -LXC_ERROR_INTERNAL;
} }
if (lxc_rmstate(name)) { if (lxc_rmstate(name)) {

View File

@ -22,11 +22,14 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "error.h" #include "error.h"
static const char *const catalogue[] = {
static char *catalogue[] = { [LXC_ERROR_LOCK] = "Failed to lock the container",
[LXC_ERROR_EMPTY] = "The container is not running",
[LXC_ERROR_EMPTY] = "The container is empty",
[LXC_ERROR_ALREADY_EXISTS] = "The container already exists", [LXC_ERROR_ALREADY_EXISTS] = "The container already exists",
[LXC_ERROR_BUSY] = "The container is busy", [LXC_ERROR_BUSY] = "The container is busy",
[LXC_ERROR_NOT_FOUND] = "The container was not found", [LXC_ERROR_NOT_FOUND] = "The container was not found",
@ -50,7 +53,10 @@ static char *catalogue[] = {
const char *const lxc_strerror(int error) 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 NULL;
return catalogue[error]; return catalogue[error];
} }

View File

@ -24,6 +24,10 @@
#define __lxc_error_h #define __lxc_error_h
typedef enum { typedef enum {
LXC_SUCCESS, /* 0 == success ;) */
LXC_ERROR_LOCK,
LXC_ERROR_EMPTY, LXC_ERROR_EMPTY,
LXC_ERROR_BUSY, LXC_ERROR_BUSY,
LXC_ERROR_ALREADY_EXISTS, 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 * @error : the value of the error
* Returns a string on success or NULL otherwise. * 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 * Checkpoint a container previously frozen

View File

@ -46,7 +46,7 @@ int main(int argc, char *argv[])
{ {
const char *name = NULL, *file = NULL; const char *name = NULL, *file = NULL;
struct lxc_conf lxc_conf; struct lxc_conf lxc_conf;
int opt; int err, opt;
while ((opt = getopt(argc, argv, "f:n:")) != -1) { while ((opt = getopt(argc, argv, "f:n:")) != -1) {
switch (opt) { switch (opt) {
@ -72,8 +72,9 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
if (lxc_create(name, &lxc_conf)) { err = lxc_create(name, &lxc_conf);
fprintf(stderr, "failed to create the container '%s'\n", name); if (err) {
fprintf(stderr, "%s\n", lxc_strerror(err));
return 1; return 1;
} }

View File

@ -39,6 +39,7 @@ int main(int argc, char *argv[])
char opt; char opt;
char *name = NULL; char *name = NULL;
int nbargs = 0; int nbargs = 0;
int err;
while ((opt = getopt(argc, argv, "n:")) != -1) { while ((opt = getopt(argc, argv, "n:")) != -1) {
switch (opt) { switch (opt) {
@ -53,8 +54,9 @@ int main(int argc, char *argv[])
if (!name) if (!name)
usage(argv[0]); usage(argv[0]);
if (lxc_destroy(name)) { err = lxc_destroy(name);
fprintf(stderr, "failed to destroy '%s'\n", name); if (err) {
fprintf(stderr, "%s\n", lxc_strerror(err));
return 1; return 1;
} }

View File

@ -104,8 +104,9 @@ int main(int argc, char *argv[])
for (opt = 0; opt < argc; opt++) for (opt = 0; opt < argc; opt++)
args[nbargs++] = argv[optind++]; args[nbargs++] = argv[optind++];
if (lxc_start(name, args)) { ret = lxc_start(name, args);
fprintf(stderr, "failed to execute '%s'\n", name); if (ret) {
fprintf(stderr, "%s\n", lxc_strerror(ret));
goto out; goto out;
} }

View File

@ -47,7 +47,7 @@ int lxc_get_lock(const char *name)
fcntl(fd, F_SETFD, FD_CLOEXEC); fcntl(fd, F_SETFD, FD_CLOEXEC);
if (flock(fd, LOCK_EX|LOCK_NB)) { if (flock(fd, LOCK_EX|LOCK_NB)) {
ret = errno == EWOULDBLOCK ? 0 : -errno; ret = -errno;
close(fd); close(fd);
goto out; goto out;
} }

View File

@ -46,7 +46,7 @@ int main(int argc, char *argv[])
char opt; char opt;
char *name = NULL; char *name = NULL;
char **args; char **args;
int nbargs = 0; int err, nbargs = 0;
char *default_args[] = { char *default_args[] = {
"/sbin/init", "/sbin/init",
'\0', '\0',
@ -72,8 +72,9 @@ int main(int argc, char *argv[])
if (!name) if (!name)
usage(argv[0]); usage(argv[0]);
if (lxc_start(name, args)) { err = lxc_start(name, args);
fprintf(stderr, "failed to start %s\n", name); if (err) {
fprintf(stderr, "%s\n", lxc_strerror(err));
return 1; return 1;
} }

View File

@ -38,7 +38,7 @@ int main(int argc, char *argv[])
{ {
char opt; char opt;
char *name = NULL; char *name = NULL;
int nbargs = 0; int err, nbargs = 0;
while ((opt = getopt(argc, argv, "n:")) != -1) { while ((opt = getopt(argc, argv, "n:")) != -1) {
switch (opt) { switch (opt) {
@ -53,8 +53,9 @@ int main(int argc, char *argv[])
if (!name) if (!name)
usage(argv[0]); usage(argv[0]);
if (lxc_stop(name)) { err = lxc_stop(name);
fprintf(stderr, "failed to stop %s\n", name); if (err) {
fprintf(stderr, "%s\n", lxc_strerror(err));
return 1; return 1;
} }

View File

@ -37,6 +37,7 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <sys/mount.h> #include <sys/mount.h>
#include "error.h"
#include <lxc/lxc.h> #include <lxc/lxc.h>
LXC_TTY_HANDLER(SIGINT); LXC_TTY_HANDLER(SIGINT);
@ -73,16 +74,10 @@ int lxc_restart(const char *name, int cfd, unsigned long flags)
int clone_flags; int clone_flags;
lock = lxc_get_lock(name); lock = lxc_get_lock(name);
if (!lock) { if (lock < 0)
lxc_log_error("'%s' is busy", name); return lock == -EWOULDBLOCK ?
return -1; -LXC_ERROR_BUSY :
} -LXC_ERROR_LOCK;
if (lock < 0) {
lxc_log_error("failed to acquire lock on '%s':%s",
name, strerror(-lock));
return -1;
}
/* Begin the set the state to STARTING*/ /* Begin the set the state to STARTING*/
if (lxc_setstate(name, STARTING)) { if (lxc_setstate(name, STARTING)) {

View File

@ -55,20 +55,14 @@ int lxc_start(const char *name, char *argv[])
int clone_flags; int clone_flags;
lock = lxc_get_lock(name); lock = lxc_get_lock(name);
if (!lock) { if (lock < 0)
lxc_log_error("'%s' is busy", name); return lock == -EWOULDBLOCK ?
return -LXC_ERROR_BUSY; -LXC_ERROR_BUSY :
} -LXC_ERROR_LOCK;
if (lock < 0) {
lxc_log_error("failed to acquire lock on '%s':%s",
name, strerror(-lock));
return -LXC_ERROR_INTERNAL;
}
/* Begin the set the state to STARTING*/ /* Begin the set the state to STARTING*/
if (lxc_setstate(name, 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; goto out;
} }

View File

@ -44,16 +44,15 @@ int lxc_stop(const char *name)
size_t pid; size_t pid;
lock = lxc_get_lock(name); lock = lxc_get_lock(name);
if (lock > 0) { if (lock >= 0) {
lxc_log_error("'%s' is not running", name);
lxc_put_lock(lock); lxc_put_lock(lock);
return -LXC_ERROR_EMPTY; return -LXC_ERROR_EMPTY;
} }
if (lock < 0) { if (lock < 0 && lock != -EWOULDBLOCK) {
lxc_log_error("failed to acquire the lock on '%s':%s", if (lock == -ENOENT)
name, strerror(-lock)); return -LXC_ERROR_NOT_FOUND;
return -LXC_ERROR_INTERNAL; return -LXC_ERROR_LOCK;
} }
snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name); snprintf(init, MAXPATHLEN, LXCPATH "/%s/init", name);