[lxc-devel] [PATCH] Set a reasonable fallback for get_rundir

Stéphane Graber stgraber at ubuntu.com
Tue Feb 18 22:56:45 UTC 2014


If get_rundir can't find XDG_RUNTIME_DIR in the environment, it'll
attempt to build a path using ~/.cache/lxc/run/. Should that fail
because of missing $HOME in the environment, it'll then return NULL an
all callers will fail in that case.

Signed-off-by: Stéphane Graber <stgraber at ubuntu.com>
---
 src/lxc/lxclock.c |  2 ++
 src/lxc/monitor.c |  3 +++
 src/lxc/utils.c   | 20 +++++++++++++++++---
 3 files changed, 22 insertions(+), 3 deletions(-)

diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c
index 4f433ca..598d6c0 100644
--- a/src/lxc/lxclock.c
+++ b/src/lxc/lxclock.c
@@ -109,6 +109,8 @@ static char *lxclock_name(const char *p, const char *n)
 	/* length of "/lock/lxc/" + $lxcpath + "/" + $lxcname + '\0' */
 	len = strlen("/lock/lxc/") + strlen(n) + strlen(p) + 2;
 	rundir = get_rundir();
+	if (!rundir)
+		return NULL;
 	len += strlen(rundir);
 
 	if ((dest = malloc(len)) == NULL)
diff --git a/src/lxc/monitor.c b/src/lxc/monitor.c
index ef658f5..704cc22 100644
--- a/src/lxc/monitor.c
+++ b/src/lxc/monitor.c
@@ -57,6 +57,9 @@ int lxc_monitor_fifo_name(const char *lxcpath, char *fifo_path, size_t fifo_path
 	const char *rundir;
 
 	rundir = get_rundir();
+	if (!rundir)
+		return -1;
+
 	if (do_mkdirp) {
 		ret = snprintf(fifo_path, fifo_path_sz, "%s/lxc/%s", rundir, lxcpath);
 		if (ret < 0 || ret >= fifo_path_sz) {
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
index 3dff104..1f868a4 100644
--- a/src/lxc/utils.c
+++ b/src/lxc/utils.c
@@ -378,11 +378,25 @@ out:
 
 const char *get_rundir()
 {
-	const char *rundir;
+	char *rundir;
+	const char *homedir;
 
-	rundir = getenv("XDG_RUNTIME_DIR");
-	if (geteuid() == 0 || rundir == NULL)
+	if (geteuid() == 0)
 		rundir = RUNTIME_PATH;
+
+	rundir = getenv("XDG_RUNTIME_DIR");
+	if (!rundir) {
+		INFO("XDG_RUNTIME_DIR isn't set in the environment.");
+		homedir = getenv("HOME");
+		if (!homedir) {
+			ERROR("HOME isn't set in the environment.");
+			return NULL;
+		}
+
+		rundir = malloc(sizeof(char) * (17 + strlen(homedir)));
+		sprintf(rundir, "%s/.cache/lxc/run/", homedir);
+	}
+
 	return rundir;
 }
 
-- 
1.9.rc1



More information about the lxc-devel mailing list