[lxc-devel] [PATCH] lxclock: use XDG_RUNTIME_DIR for lock if appropriate (v2)

Serge Hallyn serge.hallyn at ubuntu.com
Tue Jul 23 13:07:23 UTC 2013


Quoting Stéphane Graber (stgraber at ubuntu.com):
> On Mon, Jul 22, 2013 at 02:09:19PM -0500, Serge Hallyn wrote:
> > 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: Serge Hallyn <serge.hallyn at ubuntu.com>
> > Cc: Stéphane Graber <stephane.graber at canonical.com>
> 
> Acked-by: Stéphane Graber <stgraber at ubuntu.com>
> 
> > ---
> >  src/lxc/lxclock.c | 29 +++++++++++++++++++++++------
> >  1 file changed, 23 insertions(+), 6 deletions(-)
> > 
> > diff --git a/src/lxc/lxclock.c b/src/lxc/lxclock.c
> > index 31bedd8..43e76fa 100644
> > --- a/src/lxc/lxclock.c
> > +++ b/src/lxc/lxclock.c
> > @@ -24,6 +24,8 @@
> >  #include <errno.h>
> >  #include <unistd.h>
> >  #include <fcntl.h>
> > +#define _GNU_SOURCE
> > +#include <stdlib.h>
> >  #include <lxc/utils.h>
> >  #include <lxc/log.h>
> >  #include <lxc/lxccontainer.h>
> > @@ -40,14 +42,29 @@ pthread_mutex_t thread_mutex = PTHREAD_MUTEX_INITIALIZER;
> >  static char *lxclock_name(const char *p, const char *n)
> >  {
> >  	int ret;
> > -	// /run/lock/lxc/$lxcpath/$lxcname + '\0'
> > -	int len = strlen(p) + strlen(n) + strlen("/run/lock/lxc/") + 2;
> > -	char *dest = malloc(len);
> > +	int len;
> > +	char *dest;
> > +	const char *rundir;
> >  	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;
> > -	ret = snprintf(dest, len, "/run/lock/lxc/%s", p);
> > +
> > +	ret = snprintf(dest, len, "%s/lock/lxc/%s", rundir, p);
> 
> The standard path appears to be /run/user/<uid>/<software>/<whatever> so
> in our case, /run/user/<uid>/lxc/... but there's no clear spec on that,
> so I guess /run/user/<uid>/lock/lxc/... works too.

I do prefer to keep the code for euid == 0 and !=0 as much the same as
possible, but a trivial patch to switch the order of "lxc" and "lock"
would be fine with me.  I'll apply this patch as is may silently update
it (or accept a patch to do so).

thanks,
-serge




More information about the lxc-devel mailing list