[lxc-devel] [PATCH] hash command unix socket name

S.Çağlar Onur caglar at 10ur.org
Wed Feb 19 21:31:38 UTC 2014


Signed-off-by: S.Çağlar Onur <caglar at 10ur.org>
---
 src/lxc/commands.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/lxc/commands.c b/src/lxc/commands.c
index 6b46c2c..dbfa23e 100644
--- a/src/lxc/commands.c
+++ b/src/lxc/commands.c
@@ -32,6 +32,8 @@
 #include <sys/param.h>
 #include <malloc.h>
 #include <stdlib.h>
+#include <stdbool.h>
+#include <inttypes.h>
 
 #include "log.h"
 #include "lxc.h"
@@ -74,10 +76,12 @@
 lxc_log_define(lxc_commands, lxc);
 
 static int fill_sock_name(char *path, int len, const char *name,
-			  const char *inpath)
+			  const char *inpath, bool init)
 {
+	char *offset = &path[1];
 	const char *lxcpath = NULL;
-	int ret;
+	uint64_t hash;
+	int ret, sock;
 
 	if (!inpath) {
 		lxcpath = lxc_global_config_value("lxc.lxcpath");
@@ -86,12 +90,29 @@ static int fill_sock_name(char *path, int len, const char *name,
 			return -1;
 		}
 	}
-	ret = snprintf(path, len, "%s/%s/command", lxcpath ? lxcpath : inpath, name);
-
+	ret = snprintf(offset, len, "%s/%s/command", lxcpath ? lxcpath : inpath, name);
 	if (ret < 0 || ret >= len) {
 		ERROR("Name too long");
 		return -1;
 	}
+	/* addr.sun_path is only 108 bytes, so we hash the full name */
+	hash = fnv_64a_buf(path, ret, FNV1A_64_INIT);
+	ret = snprintf(offset, len, "lxc/command/%016" PRIx64, hash);
+	if (ret < 0 || ret >= len)
+		return -1;
+
+	/* if caller is not lxc_cmd_init then try connecting to UDS,
+	 * fallback to the old naming scheme if that fails */
+	if (!init) {
+		sock = lxc_abstract_unix_connect(path);
+		if (sock < 0 && errno == ECONNREFUSED) {
+			ret = snprintf(offset, len, "%s/%s/command", lxcpath ? lxcpath : inpath, name);
+			if (ret < 0 || ret >= len) {
+				return -1;
+			}
+		}
+		close(sock);
+	}
 	return 0;
 }
 
@@ -248,7 +269,7 @@ static int lxc_cmd(const char *name, struct lxc_cmd_rr *cmd, int *stopped,
 	*stopped = 0;
 
 	len = sizeof(path)-1;
-	if (fill_sock_name(offset, len, name, lxcpath))
+	if (fill_sock_name(path, len, name, lxcpath, false))
 		return -1;
 
 	sock = lxc_abstract_unix_connect(path);
@@ -856,7 +877,7 @@ int lxc_cmd_init(const char *name, struct lxc_handler *handler,
 	int len;
 
 	len = sizeof(path)-1;
-	if (fill_sock_name(offset, len, name, lxcpath))
+	if (fill_sock_name(path, len, name, lxcpath, true))
 		return -1;
 
 	fd = lxc_abstract_unix_open(path, SOCK_STREAM, 0);
-- 
1.8.3.2



More information about the lxc-devel mailing list