From d97b36f8f87775b81009a39fbbdd7b491ed4f605 Mon Sep 17 00:00:00 2001 From: Daniel Lezcano Date: Wed, 7 Oct 2009 16:06:09 +0200 Subject: [PATCH] returns a specific info when we have ECONNREFUSED When a command can not be send because the connection is refused, that means the container is stopped. Let's report this specific case instead of raising an error. Signed-off-by: Daniel Lezcano Signed-off-by: Michel Normand --- src/lxc/commands.c | 14 ++++++++++---- src/lxc/commands.h | 3 ++- src/lxc/console.c | 9 +++++++-- src/lxc/state.c | 7 +++++-- src/lxc/stop.c | 11 ++++++++--- 5 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/lxc/commands.c b/src/lxc/commands.c index 51d318eb5..1ae9d24e1 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -52,7 +52,8 @@ static int receive_answer(int sock, struct lxc_answer *answer) return ret; } -extern int lxc_command(const char *name, struct lxc_command *command) +extern int lxc_command(const char *name, struct lxc_command *command, + int *stopped) { int sock, ret = -1; char path[sizeof(((struct sockaddr_un *)0)->sun_path)] = { 0 }; @@ -61,20 +62,25 @@ extern int lxc_command(const char *name, struct lxc_command *command) sprintf(offset, "/var/run/lxc/%s/command", name); sock = lxc_af_unix_connect(path); + if (sock < 0 && errno == ECONNREFUSED) { + *stopped = 1; + return -1; + } + if (sock < 0) { - WARN("failed to connect to '@%s': %s", offset, strerror(errno)); + SYSERROR("failed to connect to '@%s'", offset); return -1; } ret = lxc_af_unix_send_credential(sock, &command->request, sizeof(command->request)); if (ret < 0) { - SYSERROR("failed to send credentials"); + SYSERROR("failed to send request to '@%s'", offset); goto out_close; } if (ret != sizeof(command->request)) { - SYSERROR("message only partially sent to '@%s'", offset); + SYSERROR("message partially sent to '@%s'", offset); goto out_close; } diff --git a/src/lxc/commands.h b/src/lxc/commands.h index 925eb446d..3c6c08271 100644 --- a/src/lxc/commands.h +++ b/src/lxc/commands.h @@ -45,7 +45,8 @@ struct lxc_command { struct lxc_answer answer; }; -extern int lxc_command(const char *name, struct lxc_command *command); +extern int lxc_command(const char *name, struct lxc_command *command, + int *stopped); struct lxc_epoll_descr; struct lxc_handler; diff --git a/src/lxc/console.c b/src/lxc/console.c index 92bbd47c6..05f06309b 100644 --- a/src/lxc/console.c +++ b/src/lxc/console.c @@ -35,12 +35,17 @@ lxc_log_define(lxc_console, lxc); extern int lxc_console(const char *name, int ttynum, int *fd) { - int ret; + int ret, stopped = 0; struct lxc_command command = { .request = { .type = LXC_COMMAND_TTY, .data = ttynum }, }; - ret = lxc_command(name, &command); + ret = lxc_command(name, &command, &stopped); + if (ret < 0 && stopped) { + ERROR("'%s' is stopped", name); + return -1; + } + if (ret < 0) { ERROR("failed to send command"); return -1; diff --git a/src/lxc/state.c b/src/lxc/state.c index 06b7b632f..1e8b8e159 100644 --- a/src/lxc/state.c +++ b/src/lxc/state.c @@ -100,9 +100,12 @@ lxc_state_t lxc_getstate(const char *name) .request = { .type = LXC_COMMAND_STATE }, }; - int ret; + int ret, stopped = 0; + + ret = lxc_command(name, &command, &stopped); + if (ret < 0 && stopped) + return STOPPED; - ret = lxc_command(name, &command); if (ret < 0) { ERROR("failed to send command"); return -1; diff --git a/src/lxc/stop.c b/src/lxc/stop.c index a88cb4e9d..504d27e58 100644 --- a/src/lxc/stop.c +++ b/src/lxc/stop.c @@ -43,9 +43,14 @@ int lxc_stop(const char *name) .request = { .type = LXC_COMMAND_STOP }, }; - int ret; + int ret, stopped = 0; + + ret = lxc_command(name, &command,&stopped); + if (ret < 0 && stopped) { + INFO("'%s' is already stopped", name); + return 0; + } - ret = lxc_command(name, &command); if (ret < 0) { ERROR("failed to send command"); return -1; @@ -55,7 +60,7 @@ int lxc_stop(const char *name) * closed */ if (ret > 0) { - ERROR("stop request rejected for '%s': %s", + ERROR("failed to stop '%s': %s", name, strerror(-command.answer.ret)); return -1; }