From: Michel Normand Date: Thu, 28 May 2009 13:32:29 +0000 (+0200) Subject: lxc-wait to check if container already in requested state X-Git-Tag: lxc_0_6_3~58 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=549153a7cf44b9c3da201ac9b6a6be56f88991e8;p=thirdparty%2Flxc.git lxc-wait to check if container already in requested state 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 Signed-off-by: Daniel Lezcano --- diff --git a/src/lxc/lxc_wait.c b/src/lxc/lxc_wait.c index 6e01a056b..2d0f93cdc 100644 --- a/src/lxc/lxc_wait.c +++ b/src/lxc/lxc_wait.c @@ -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; }