[lxc-devel] [PATCH] lxc-wait: Add timeout option

Jan Kiszka jan.kiszka at siemens.com
Mon Jul 9 17:15:50 UTC 2012


Allow to specify a timeout for waiting on state changes via lxc-wait.
Helpful for scripts that need to handle errors or excessive delays in
state changing procedures.

Signed-off-by: Jan Kiszka <jan.kiszka at siemens.com>
---
 doc/lxc-wait.sgml.in |   11 +++++++++++
 src/lxc/arguments.h  |    1 +
 src/lxc/lxc_wait.c   |   16 +++++++++++++++-
 3 files changed, 27 insertions(+), 1 deletions(-)

diff --git a/doc/lxc-wait.sgml.in b/doc/lxc-wait.sgml.in
index 97a4c39..f30e0c3 100644
--- a/doc/lxc-wait.sgml.in
+++ b/doc/lxc-wait.sgml.in
@@ -79,6 +79,17 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 	</listitem>
       </varlistentry>
 
+      <varlistentry>
+	<term>
+	  <option>-t <replaceable>timeout</replaceable></option>
+	</term>
+	<listitem>
+	  <para>
+	    Wait timeout seconds for desired state to be reached.
+	  </para>
+	</listitem>
+      </varlistentry>
+
     </variablelist>
 
   </refsect1>
diff --git a/src/lxc/arguments.h b/src/lxc/arguments.h
index 40f0d6c..3c9d28f 100644
--- a/src/lxc/arguments.h
+++ b/src/lxc/arguments.h
@@ -57,6 +57,7 @@ struct lxc_arguments {
 
 	/* for lxc-wait */
 	char *states;
+	long timeout;
 
 	/* close fds from parent? */
 	int close_all_fds;
diff --git a/src/lxc/lxc_wait.c b/src/lxc/lxc_wait.c
index a58e0c8..d7a69bc 100644
--- a/src/lxc/lxc_wait.c
+++ b/src/lxc/lxc_wait.c
@@ -24,6 +24,8 @@
 #include <string.h>
 #include <libgen.h>
 #include <unistd.h>
+#include <stdlib.h>
+#include <signal.h>
 #include <sys/types.h>
 
 #include <lxc/lxc.h>
@@ -46,12 +48,14 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
 {
 	switch (c) {
 	case 's': args->states = optarg; break;
+	case 't': args->timeout = atol(optarg); break;
 	}
 	return 0;
 }
 
 static const struct option my_longopts[] = {
 	{"state", required_argument, 0, 's'},
+	{"timeout", required_argument, 0, 't'},
 	LXC_COMMON_OPTIONS
 };
 
@@ -66,7 +70,8 @@ Options :\n\
   -n, --name=NAME   NAME for name of the container\n\
   -s, --state=STATE ORed states to wait for\n\
                     STOPPED, STARTING, RUNNING, STOPPING,\n\
-                    ABORTING, FREEZING, FROZEN\n",
+                    ABORTING, FREEZING, FROZEN\n\
+  -t, --timeout=TMO Seconds to wait for state changes\n",
 	.options  = my_longopts,
 	.parser   = my_parser,
 	.checker  = my_checker,
@@ -91,6 +96,11 @@ static int fillwaitedstates(char *strstates, int *states)
 	return 0;
 }
 
+static void timeout_handler(int signal)
+{
+	exit(-1);
+}
+
 int main(int argc, char *argv[])
 {
 	struct lxc_msg msg;
@@ -124,6 +134,9 @@ int main(int argc, char *argv[])
 		goto out_close;
 	}
 
+	signal(SIGALRM, timeout_handler);
+	alarm(my_args.timeout);
+
 	for (;;) {
 		if (lxc_monitor_read(fd, &msg) < 0)
 			goto out_close;
@@ -140,6 +153,7 @@ int main(int argc, char *argv[])
 			}
 
 			if (s[msg.value]) {
+				alarm(0);
 				ret = 0;
 				goto out_close;
 			}
-- 
1.7.3.4




More information about the lxc-devel mailing list