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

Serge Hallyn serge.hallyn at canonical.com
Mon Jul 9 17:28:05 UTC 2012


Quoting Jan Kiszka (jan.kiszka at siemens.com):
> 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.

Hi,

in our lxc-api tree (https://code.launchpad.net/~ubuntu-lxc/ubuntu/quantal/lxc/lxc-api-and-python/)
I've added changed src/lxc/state.c:lxc_wait() to take an optional
timeout value (0 means don't wait, -1 means wait forever), and use
select, to implement wait with timeout.  It is only used by our
lxc_container->wait, not by src/lxc/lxc_wait.c which in our tree
passes in -1 to wait forever.  Ideally our patches would be combined.

> 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
> 
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and 
> threat landscape has changed and how IT managers can respond. Discussions 
> will include endpoint security, mobile security and the latest in malware 
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel




More information about the lxc-devel mailing list