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 <dlezcano@fr.ibm.com>
Signed-off-by: Michel Normand <normand@fr.ibm.com>
---
 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(-)

Index: lxc/src/lxc/commands.c
===================================================================
--- lxc.orig/src/lxc/commands.c
+++ lxc/src/lxc/commands.c
@@ -52,7 +52,8 @@ static int receive_answer(int sock, stru
 	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,
 	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;
 	}
 
Index: lxc/src/lxc/commands.h
===================================================================
--- lxc.orig/src/lxc/commands.h
+++ lxc/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;
Index: lxc/src/lxc/console.c
===================================================================
--- lxc.orig/src/lxc/console.c
+++ lxc/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;
Index: lxc/src/lxc/state.c
===================================================================
--- lxc.orig/src/lxc/state.c
+++ lxc/src/lxc/state.c
@@ -100,9 +100,12 @@ lxc_state_t lxc_getstate(const char *nam
 		.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;
Index: lxc/src/lxc/stop.c
===================================================================
--- lxc.orig/src/lxc/stop.c
+++ lxc/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;
 	}



