[Lxc-users] [RFC 1/2] lxc-monitor: support --quit-on-stop

Serge E. Hallyn serge.hallyn at canonical.com
Fri Aug 5 13:55:08 UTC 2011


Hi, do you have any objections to this patch?  It adds a '-Q' option
to lxc-monitor to have lxc-monitor quit when the monitored container
stops.

This is used by lxc-start-ephemeral to track a container's death.

Index: ephemeral/src/lxc/lxc_monitor.c
===================================================================
--- ephemeral.orig/src/lxc/lxc_monitor.c	2011-08-03 23:00:42.925819531 -0500
+++ ephemeral/src/lxc/lxc_monitor.c	2011-08-03 23:43:43.396909384 -0500
@@ -36,9 +36,18 @@
 lxc_log_define(lxc_monitor_ui, lxc_monitor);
 
 static const struct option my_longopts[] = {
+	{"quit-on-stop", no_argument, 0, 'Q'},
 	LXC_COMMON_OPTIONS
 };
 
+static int my_parser(struct lxc_arguments* args, int c, char* arg)
+{
+	switch (c) {
+	case 'Q': args->quit_on_stop = 1; break;
+	}
+	return 0;
+}
+
 static struct lxc_arguments my_args = {
 	.progname = "lxc-monitor",
 	.help     = "\
@@ -47,11 +56,13 @@
 lxc-monitor monitors the state of the NAME container\n\
 \n\
 Options :\n\
-  -n, --name=NAME   NAME for name of the container\n\
-                    NAME may be a regular expression",
+  -Q, --quit-on-stop quit when the container stops\n\
+  -n, --name=NAME    NAME for name of the container\n\
+                     NAME may be a regular expression",
 	.options  = my_longopts,
-	.parser   = NULL,
+	.parser   = my_parser,
 	.checker  = NULL,
+	.quit_on_stop = 0,
 };
 
 int main(int argc, char *argv[])
@@ -95,6 +106,8 @@
 		case lxc_msg_state:
 			printf("'%s' changed state to [%s]\n", 
 			       msg.name, lxc_state2str(msg.value));
+			if (msg.value == STOPPED && my_args.quit_on_stop)
+				goto quit;
 			break;
 		default:
 			/* ignore garbage */
@@ -102,6 +115,7 @@
 		}
 	}
 
+quit:
 	regfree(&preg);
 
 	return 0;
Index: ephemeral/src/lxc/arguments.h
===================================================================
--- ephemeral.orig/src/lxc/arguments.h	2011-08-03 23:32:20.905831229 -0500
+++ ephemeral/src/lxc/arguments.h	2011-08-03 23:32:37.625831332 -0500
@@ -58,6 +58,9 @@
 	/* for lxc-wait */
 	char *states;
 
+	/* for lxc-shutdown*/
+	int quit_on_stop;
+
 	/* remaining arguments */
 	char *const *argv;
 	int argc;




More information about the lxc-users mailing list