Commit 469b5787 by Serge Hallyn

lxclock: use XDG_RUNTIME_DIR for lock if appropriate (v2)

If we are euid==0 or XDG_RUNTIME_DIR is not set, then use /run/lock/lxc/$lxcpath/$lxcname as before. Otherwise, use $XDG_RUNTIME_DIR/lock/lxc/$lxcpath/$lxcname. Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com> Cc: Stéphane Graber <stephane.graber@canonical.com> Acked-by: 's avatarStéphane Graber <stgraber@ubuntu.com>
parent b60ed720
...@@ -24,6 +24,8 @@ ...@@ -24,6 +24,8 @@
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#define _GNU_SOURCE
#include <stdlib.h>
#include <lxc/utils.h> #include <lxc/utils.h>
#include <lxc/log.h> #include <lxc/log.h>
#include <lxc/lxccontainer.h> #include <lxc/lxccontainer.h>
...@@ -40,14 +42,29 @@ pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER; ...@@ -40,14 +42,29 @@ pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER;
static char *lxclock_name(const char *p, const char *n) static char *lxclock_name(const char *p, const char *n)
{ {
int ret; int ret;
// /run/lock/lxc/$lxcpath/$lxcname + '\0' int len;
int len = strlen(p) + strlen(n) + strlen("/run/lock/lxc/") + 2; char *dest;
char *dest = malloc(len); const char *rundir;
struct stat sb; struct stat sb;
if (!dest) /* lockfile will be:
* "/run" + "/lock/lxc/$lxcpath/$lxcname + '\0' if root
* or
* $XDG_RUNTIME_DIR + "/lock/lxc/$lxcpath/$lxcname + '\0' if non-root
*/
/* length of "/lock/lxc/" + $lxcpath + "/" + $lxcname + '\0' */
len = strlen("/lock/lxc/") + strlen(n) + strlen(p) + 2;
rundir = getenv("XDG_RUNTIME_DIR");
if (geteuid() == 0 || rundir == NULL)
rundir = "/run";
len += strlen(rundir);
if ((dest = malloc(len)) == NULL)
return NULL; return NULL;
ret = snprintf(dest, len, "/run/lock/lxc/%s", p);
ret = snprintf(dest, len, "%s/lock/lxc/%s", rundir, p);
if (ret < 0 || ret >= len) { if (ret < 0 || ret >= len) {
free(dest); free(dest);
return NULL; return NULL;
...@@ -69,7 +86,7 @@ static char *lxclock_name(const char *p, const char *n) ...@@ -69,7 +86,7 @@ static char *lxclock_name(const char *p, const char *n)
ERROR("Failed to set mode for lockdir %s\n", dest); ERROR("Failed to set mode for lockdir %s\n", dest);
} }
ret = snprintf(dest, len, "/run/lock/lxc/%s/%s", p, n); ret = snprintf(dest, len, "%s/lock/lxc/%s/%s", rundir, p, n);
if (ret < 0 || ret >= len) { if (ret < 0 || ret >= len) {
free(dest); free(dest);
return NULL; return NULL;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment