[lxc-devel] [PATCH] lxclock: fall back to /tmp if xdg_rundir is not writeable

Serge Hallyn serge.hallyn at ubuntu.com
Tue Jan 7 05:39:02 UTC 2014


This happens for instance if you run a test under sudo which
then runs lxc commands under 'su - <someuser>'

Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>
---
 src/lxc/lxclock.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)

diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c
index 69d3d15..c6f2448 100644
--- a/src/lxc/lxclock.c
+++ b/src/lxc/lxclock.c
@@ -123,11 +123,27 @@ static char *lxclock_name(const char *p, const char *n)
 	}
 	ret = mkdir_p(dest, 0755);
 	if (ret < 0) {
-		free(dest);
-		return NULL;
-	}
+		/* fall back to "/tmp/" $(id -u) "/lxc/" $lxcpath / $lxcname + '\0' */
+		int l2 = 33 + strlen(n) + strlen(p);
+		char *d;
+		if (l2 > len) {
+			d = realloc(dest, l2);
+			if (!d) {
+				free(dest);
+				return NULL;
+			}
+			len = l2;
+		}
+		dest = d;
+		ret = snprintf(dest, len, "/tmp/%d/lxc/%s", geteuid(), p);
+		if (ret < 0 || ret >= len) {
+			free(dest);
+			return NULL;
+		}
+		ret = snprintf(dest, len, "/tmp/%d/lxc/%s/%s", geteuid(), p, n);
+	} else
+		ret = snprintf(dest, len, "%s/lock/lxc/%s/%s", rundir, p, n);
 
-	ret = snprintf(dest, len, "%s/lock/lxc/%s/%s", rundir, p, n);
 	if (ret < 0 || ret >= len) {
 		free(dest);
 		return NULL;
-- 
1.8.5.2



More information about the lxc-devel mailing list