[lxc-devel] [PATCH] hash command unix socket name (v2)

S.Çağlar Onur caglar at 10ur.org
Wed Feb 19 22:09:19 UTC 2014


Changes since v1;
  * Address Stéphane's comments

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

diff --git a/src/lxc/commands.c b/src/lxc/commands.c
index 6b46c2c..e5d8a8e 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], sock_name[PATH_MAX];
 	const char *lxcpath = NULL;
-	int ret;
+	uint64_t hash;
+	int ret, sock;
 
 	if (!inpath) {
 		lxcpath = lxc_global_config_value("lxc.lxcpath");
@@ -86,12 +90,32 @@ 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(sock_name, PATH_MAX, "%s/%s/command", lxcpath ? lxcpath : inpath, name);
+	if (ret < 0 || ret >= PATH_MAX) {
+		return -1;
+	}
 
+	/* addr.sun_path is only 108 bytes, so we hash the full name */
+	hash = fnv_64a_buf(sock_name, ret, FNV1A_64_INIT);
+	ret = snprintf(offset, len, "lxc/command/%016" PRIx64, hash);
 	if (ret < 0 || ret >= len) {
 		ERROR("Name too long");
 		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) {
+				ERROR("Name too long");
+				return -1;
+			}
+		}
+		close(sock);
+	}
 	return 0;
 }
 
@@ -248,7 +272,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 +880,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