]> git.proxmox.com Git - mirror_lxc.git/commitdiff
lxc-wait to check if container already in requested state
authorMichel Normand <normand@fr.ibm.com>
Thu, 28 May 2009 13:32:29 +0000 (15:32 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Thu, 28 May 2009 13:32:29 +0000 (15:32 +0200)
Without this patch the lxc_wait may wait forever
if container is already in requested state.

Note that this patch avoids also to be hang if container do not exist yet.

Signed-off-by: Michel Normand <normand@fr.ibm.com>
Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
src/lxc/lxc_wait.c

index 6e01a056b77440fdd5011c7e6e9ff687d99b88ba..2d0f93cdc09f5336e34bb39b121b86ea73d293ce 100644 (file)
@@ -86,7 +86,6 @@ static int fillwaitedstates(char *strstates, int *states)
                states[state] = 1;
 
                token = strtok_r(NULL, "|", &saveptr);
-               
        }
        return 0;
 }
@@ -95,6 +94,7 @@ int main(int argc, char *argv[])
 {
        struct lxc_msg msg;
        int s[MAX_STATE] = { }, fd;
+       int state, ret;
 
        if (lxc_arguments_parse(&my_args, argc, argv))
                return -1;
@@ -110,9 +110,22 @@ int main(int argc, char *argv[])
        if (fd < 0)
                return -1;
 
+       /*
+        * if container present,
+        * then check if already in requested state
+        */
+       ret = -1;
+       state = lxc_getstate(my_args.name);
+       if (state < 0) {
+               goto out_close;
+       } else if ((state >= 0) && (s[state])) {
+               ret = 0;
+               goto out_close;
+       }
+
        for (;;) {
                if (lxc_monitor_read(fd, &msg) < 0)
-                       return -1;
+                       goto out_close;
 
                if (strcmp(my_args.name, msg.name))
                        continue;
@@ -122,11 +135,13 @@ int main(int argc, char *argv[])
                        if (msg.value < 0 || msg.value >= MAX_STATE) {
                                ERROR("Receive an invalid state number '%d'",
                                        msg.value);
-                               return -1;
+                               goto out_close;
                        }
 
-                       if (s[msg.value])
-                               return 0;
+                       if (s[msg.value]) {
+                               ret = 0;
+                               goto out_close;
+                       }
                        break;
                default:
                        /* just ignore garbage */
@@ -134,5 +149,7 @@ int main(int argc, char *argv[])
                }
        }
 
-       return 0;
+out_close:
+       lxc_monitor_close(fd);
+       return ret;
 }