[lxc-devel] [PATCHv2] Watch utmp if /var/run is not shared

David Ward david.ward at ll.mit.edu
Tue Mar 1 06:50:29 UTC 2011


In order to stop or restart a container that runs "init" as its top-level
process, lxc must watch for changes to the "utmp" file (which stores init's
current and previous runlevel) located in /var/run in the container. Because
lxc should only react to the container runlevel (if one exists) and not the
system runlevel, lxc must first check that utmp is not shared between the
container and the system.

Presently, lxc will only watch utmp if the "lxc.rootfs" parameter is set in
the container configuration. However, lxc should also watch utmp if the
filesystem root is shared but "/var/run" has been re-mounted from another
location. (In this scenario, Upstart could be used to control the container
if "/etc/init" has also been re-mounted to a directory that holds Upstart
scripts specifically written for the container.)

With this change, lxc checks to see if "/var/run" is shared between the
container and the system by comparing the device and inode numbers. If not,
lxc will watch utmp. This replaces the check for "lxc.rootfs".

Signed-off-by: David Ward <david.ward at ll.mit.edu>
---
 src/lxc/utmp.c |   21 ++++++++++++++++-----
 1 files changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/lxc/utmp.c b/src/lxc/utmp.c
index 691c3ef..48998ca 100644
--- a/src/lxc/utmp.c
+++ b/src/lxc/utmp.c
@@ -30,6 +30,7 @@
 #include <sys/inotify.h>
 #include <sys/ioctl.h>
 #include <sys/timerfd.h>
+#include <sys/stat.h>
 
 #include "conf.h"
 #include "cgroup.h"
@@ -221,10 +222,7 @@ int lxc_utmp_mainloop_add(struct lxc_epoll_descr *descr,
 	char path[MAXPATHLEN];
 	int fd, wd;
 	struct lxc_utmp *utmp_data;
-	struct lxc_conf *conf = handler->conf;
-
-	if (!conf->rootfs.path)
-		return 0;
+	struct stat container_stat, system_stat;
 
 	/* We set up a watch for the /var/run directory. We're only interested
 	 * in utmp at the moment, but want to watch for delete and create
@@ -236,11 +234,24 @@ int lxc_utmp_mainloop_add(struct lxc_epoll_descr *descr,
 		return -1;
 	}
 
-	if (access(path, F_OK)) {
+	if (stat(path, &container_stat)) {
 		WARN("'%s' not found", path);
 		return 0;
 	}
 
+	if (stat("/var/run", &system_stat)) {
+		WARN("'/var/run' not found");
+		return 0;
+	}
+
+	/* Do not watch the /var/run directory if the container shares it with
+	 * the system.
+	 */
+	if ((container_stat.st_dev == system_stat.st_dev)
+	    && (container_stat.st_ino == system_stat.st_ino)) {
+		return 0;
+	}
+
 	utmp_data = (struct lxc_utmp *)malloc(sizeof(struct lxc_utmp));
 
 	if (NULL == utmp_data) {
-- 
1.7.4





More information about the lxc-devel mailing list