[lxc-devel] [PATCH] lxc-info: add option -t, --state-is=STATE to test for a given test

Natanael Copa ncopa at alpinelinux.org
Mon Nov 26 19:04:32 UTC 2012


Add an option to test for a give state. This is useful for scripts.
It lets us you do thing like:

 if lxc-info --name myname --state-is RUNNING; then
   ...

Signed-off-by: Natanael Copa <ncopa at alpinelinux.org>
---
 src/lxc/lxc_info.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/src/lxc/lxc_info.c b/src/lxc/lxc_info.c
index 809769e..1a1cc22 100644
--- a/src/lxc/lxc_info.c
+++ b/src/lxc/lxc_info.c
@@ -34,12 +34,14 @@
 
 static bool state;
 static bool pid;
+static char *test_state = NULL;
 
 static int my_parser(struct lxc_arguments* args, int c, char* arg)
 {
 	switch (c) {
 	case 's': state = true; break;
 	case 'p': pid = true; break;
+	case 't': test_state = arg; break;
 	}
 	return 0;
 }
@@ -47,6 +49,7 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
 static const struct option my_longopts[] = {
 	{"state", no_argument, 0, 's'},
 	{"pid", no_argument, 0, 'p'},
+	{"state-is", required_argument, 0, 't'},
 	LXC_COMMON_OPTIONS,
 };
 
@@ -58,9 +61,11 @@ static struct lxc_arguments my_args = {
 lxc-info display some information about a container with the identifier NAME\n\
 \n\
 Options :\n\
-  -n, --name=NAME   NAME for name of the container\n\
-  -s, --state       shows the state of the container\n\
-  -p, --pid         shows the process id of the init container\n",
+  -n, --name=NAME       NAME for name of the container\n\
+  -s, --state           shows the state of the container\n\
+  -p, --pid             shows the process id of the init container\n\
+  -t, --state-is=STATE  test if current state is STATE\n\
+                        returns success if it matches, false otherwise\n",
 	.options  = my_longopts,
 	.parser   = my_parser,
 	.checker  = NULL,
@@ -81,10 +86,12 @@ int main(int argc, char *argv[])
 	if (!state && !pid)
 		state = pid = true;
 
-	if (state) {
+	if (state || test_state) {
 		ret = lxc_getstate(my_args.name);
 		if (ret < 0)
 			return 1;
+		if (test_state)
+			return strcmp(lxc_state2str(ret), test_state) != 0;
 
 		printf("state:%10s\n", lxc_state2str(ret));
 	}
-- 
1.8.0





More information about the lxc-devel mailing list