Commit b486346a by Jan Kiszka Committed by Stéphane Graber

lxc-wait: Add timeout option

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: 's avatarJan Kiszka <jan.kiszka@siemens.com> Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com>
parent 4aa7ac35
...@@ -79,6 +79,17 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ...@@ -79,6 +79,17 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
</listitem> </listitem>
</varlistentry> </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> </variablelist>
</refsect1> </refsect1>
......
...@@ -57,6 +57,7 @@ struct lxc_arguments { ...@@ -57,6 +57,7 @@ struct lxc_arguments {
/* for lxc-wait */ /* for lxc-wait */
char *states; char *states;
long timeout;
/* close fds from parent? */ /* close fds from parent? */
int close_all_fds; int close_all_fds;
......
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
#include <string.h> #include <string.h>
#include <libgen.h> #include <libgen.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/types.h> #include <sys/types.h>
#include <lxc/lxc.h> #include <lxc/lxc.h>
...@@ -46,12 +48,14 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg) ...@@ -46,12 +48,14 @@ static int my_parser(struct lxc_arguments* args, int c, char* arg)
{ {
switch (c) { switch (c) {
case 's': args->states = optarg; break; case 's': args->states = optarg; break;
case 't': args->timeout = atol(optarg); break;
} }
return 0; return 0;
} }
static const struct option my_longopts[] = { static const struct option my_longopts[] = {
{"state", required_argument, 0, 's'}, {"state", required_argument, 0, 's'},
{"timeout", required_argument, 0, 't'},
LXC_COMMON_OPTIONS LXC_COMMON_OPTIONS
}; };
...@@ -66,7 +70,8 @@ Options :\n\ ...@@ -66,7 +70,8 @@ Options :\n\
-n, --name=NAME NAME for name of the container\n\ -n, --name=NAME NAME for name of the container\n\
-s, --state=STATE ORed states to wait for\n\ -s, --state=STATE ORed states to wait for\n\
STOPPED, STARTING, RUNNING, STOPPING,\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, .options = my_longopts,
.parser = my_parser, .parser = my_parser,
.checker = my_checker, .checker = my_checker,
...@@ -91,6 +96,11 @@ static int fillwaitedstates(char *strstates, int *states) ...@@ -91,6 +96,11 @@ static int fillwaitedstates(char *strstates, int *states)
return 0; return 0;
} }
static void timeout_handler(int signal)
{
exit(-1);
}
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
struct lxc_msg msg; struct lxc_msg msg;
...@@ -124,6 +134,9 @@ int main(int argc, char *argv[]) ...@@ -124,6 +134,9 @@ int main(int argc, char *argv[])
goto out_close; goto out_close;
} }
signal(SIGALRM, timeout_handler);
alarm(my_args.timeout);
for (;;) { for (;;) {
if (lxc_monitor_read(fd, &msg) < 0) if (lxc_monitor_read(fd, &msg) < 0)
goto out_close; goto out_close;
...@@ -140,6 +153,7 @@ int main(int argc, char *argv[]) ...@@ -140,6 +153,7 @@ int main(int argc, char *argv[])
} }
if (s[msg.value]) { if (s[msg.value]) {
alarm(0);
ret = 0; ret = 0;
goto out_close; goto out_close;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment