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

Stéphane Graber stgraber at ubuntu.com
Tue Jul 23 08:26:32 UTC 2013


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.

>  	if (ret < 0 || ret >= len) {
>  		free(dest);
>  		return NULL;
> @@ -69,7 +86,7 @@ static char *lxclock_name(const char *p, const char *n)
>  			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) {
>  		free(dest);
>  		return NULL;
> -- 
> 1.8.3.2
> 
> 
> ------------------------------------------------------------------------------
> See everything from the browser to the database with AppDynamics
> Get end-to-end visibility with application monitoring from AppDynamics
> Isolate bottlenecks and diagnose root cause in seconds.
> Start your free trial of AppDynamics Pro today!
> http://pubads.g.doubleclick.net/gampad/clk?id=48808831&iu=/4140/ostg.clktrk
> _______________________________________________
> Lxc-devel mailing list
> Lxc-devel at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/lxc-devel

-- 
Stéphane Graber
Ubuntu developer
http://www.ubuntu.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20130723/c3dc1f09/attachment.pgp>


More information about the lxc-devel mailing list