]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
lxc-info: add option -t, --state-is=STATE to test for a given test
authorNatanael Copa <ncopa@alpinelinux.org>
Mon, 26 Nov 2012 19:04:32 +0000 (20:04 +0100)
committerStéphane Graber <stgraber@ubuntu.com>
Mon, 26 Nov 2012 19:29:43 +0000 (14:29 -0500)
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@alpinelinux.org>
Acked-by: Serge E. Hallyn <serge.hallyn@ubuntu.com>
src/lxc/lxc_info.c

index 809769e4d9aa4aa3d274f7238f3e7e8478708e62..1a1cc22df36550cf16dcb3661b1a76605ff89229 100644 (file)
 
 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));
        }