]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
command socket: use hash if needed
authorSerge Hallyn <serge.hallyn@ubuntu.com>
Sat, 9 Aug 2014 00:28:18 +0000 (00:28 +0000)
committerStéphane Graber <stgraber@ubuntu.com>
Mon, 18 Aug 2014 03:38:56 +0000 (23:38 -0400)
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@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
src/lxc/commands.c

index 458d41e1f7c9c7ead1e83dda410773de1c102f3d..f46efc55fb1b6f0bec447df93b4bfde8e38e8492 100644 (file)
@@ -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>
 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;
 }