From: Stéphane Graber Date: Tue, 18 Feb 2014 22:33:51 +0000 (-0500) Subject: Set a reasonable fallback for get_rundir X-Git-Tag: lxc-1.0.0.rc4~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=97a696c6e5ac8b82ecd10a90ca69427af685bd1f;p=thirdparty%2Flxc.git Set a reasonable fallback for get_rundir 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 Acked-by: Serge E. Hallyn --- diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c index 4f433ca6a..598d6c0d0 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 ef658f5c4..704cc225f 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 3dff104df..1f868a4ce 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; }