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: Jan Kiszka <jan.kiszka@siemens.com>
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com>
This commit is contained in:
Jan Kiszka 2012-08-09 17:54:48 -05:00 committed by Stéphane Graber
parent 4aa7ac3569
commit b486346aa2
3 changed files with 27 additions and 1 deletions

View File

@ -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>

View File

@ -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;

View File

@ -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)
{ {
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\
-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)
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[])
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[])
} }
if (s[msg.value]) { if (s[msg.value]) {
alarm(0);
ret = 0; ret = 0;
goto out_close; goto out_close;
} }