[lxc-devel] [PATCH 1/2] command socket: use hash if needed

Stéphane Graber stgraber at ubuntu.com
Mon Aug 18 03:36:43 UTC 2014


On Sat, Aug 09, 2014 at 12:28:18AM +0000, Serge Hallyn wrote:
> The container command socket is an abstract unix socket containing
> the lxcpath and container name.  Those can be too long.  In that case,
> use the hash of the lxcpath and lxcname.  Continue to use the path and
> name if possible to avoid any back compat issues.
> 
> Signed-off-by: Serge Hallyn <serge.hallyn at ubuntu.com>

Acked-by: Stéphane Graber <stgraber at ubuntu.com>

> ---
>  src/lxc/commands.c | 32 +++++++++++++++++++++++++++-----
>  1 file changed, 27 insertions(+), 5 deletions(-)
> 
> diff --git a/src/lxc/commands.c b/src/lxc/commands.c
> index 458d41e..f46efc5 100644
> --- a/src/lxc/commands.c
> +++ b/src/lxc/commands.c
> @@ -28,6 +28,7 @@
>  #include <fcntl.h>
>  #include <poll.h>
>  #include <sys/socket.h>
> +#include <inttypes.h>
>  #include <sys/un.h>
>  #include <sys/param.h>
>  #include <malloc.h>
> @@ -74,24 +75,45 @@
>  lxc_log_define(lxc_commands, lxc);
>  
>  static int fill_sock_name(char *path, int len, const char *name,
> -			  const char *inpath)
> +			  const char *lxcpath)
>  {
> -	const char *lxcpath = NULL;
> +	char *tmppath;
> +	size_t tmplen;
> +	uint64_t hash;
>  	int ret;
>  
> -	if (!inpath) {
> +	if (!lxcpath) {
>  		lxcpath = lxc_global_config_value("lxc.lxcpath");
>  		if (!lxcpath) {
>  			ERROR("Out of memory getting lxcpath");
>  			return -1;
>  		}
>  	}
> -	ret = snprintf(path, len, "%s/%s/command", lxcpath ? lxcpath : inpath, name);
> +		
> +	ret = snprintf(path, len, "%s/%s/command", lxcpath, name);
>  
> +	if (ret < 0) {
> +		ERROR("Error writing to command sock path");
> +		return -1;
> +	}
> +	if (ret < len)
> +		return 0;
> +
> +	/* ret >= len; lxcpath or name is too long.  hash both */
> +	tmplen = strlen(name) + strlen(lxcpath) + 2;
> +	tmppath = alloca(tmplen);
> +	ret = snprintf(tmppath, tmplen, "%s/%s", lxcpath, name);
> +	if (ret < 0 || ret >= tmplen) {
> +		ERROR("memory error");
> +		return -1;
> +	}
> +	hash = fnv_64a_buf(tmppath, ret, FNV1A_64_INIT);
> +	ret = snprintf(path, len, "lxc/%016" PRIx64 "/cmd_sock", hash);
>  	if (ret < 0 || ret >= len) {
> -		ERROR("Name too long");
> +		ERROR("Command socket name too long");
>  		return -1;
>  	}
> +
>  	return 0;
>  }
>  
> -- 
> 2.1.0.rc1
> 
> _______________________________________________
> lxc-devel mailing list
> lxc-devel at lists.linuxcontainers.org
> http://lists.linuxcontainers.org/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: 819 bytes
Desc: Digital signature
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20140817/26782654/attachment.sig>


More information about the lxc-devel mailing list