From: Natanael Copa Date: Mon, 26 Nov 2012 19:04:32 +0000 (+0100) Subject: lxc-info: add option -t, --state-is=STATE to test for a given test X-Git-Tag: lxc-0.9.0.alpha1~1^2~61 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b917ef75223480f0fa86d47eed5951787b86a99f;p=thirdparty%2Flxc.git lxc-info: add option -t, --state-is=STATE to test for a given test 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 Acked-by: Serge E. Hallyn --- diff --git a/src/lxc/lxc_info.c b/src/lxc/lxc_info.c index 809769e4d..1a1cc22df 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)); }