Commit 154ba520 by Serge Hallyn Committed by Stéphane Graber

monitor: fix sockname calculation for long lxcpaths

A long enough lxcpath (and small PATH_MAX through crappy defines) can cause the creation of the string to be hashed to fail. So just use alloca to get the size string we need. More importantly, while I can't explain it, if lxcpath is too long, setting sockname[sizeof(addr->sun_path)-2] to \0 simply doesn't seem to work. So set sockname[sizeof(addr->sun_path)-3] to \0, which does work. With this, and with lxc.lxcpath = /opt/lxc0123456789/lxc0123456789/lxc0123456789/lxc0123456789/lxc0123456789/lxc0123456789/lxc0123456789/lxc0123456789/lxc0123456789/lxc0123456789 in /etc/lxc/lxc.conf, I can run lxc-wait just fine. Without it, it fails (as does lxc-start -d, which uses lxc_wait to verify the container started) Signed-off-by: 's avatarSerge Hallyn <serge.hallyn@ubuntu.com> Acked-by: 's avatarStéphane Graber <stgraber@ubuntu.com>
parent 408d0479
...@@ -142,7 +142,7 @@ int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) { ...@@ -142,7 +142,7 @@ int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
size_t len; size_t len;
int ret; int ret;
char *sockname = &addr->sun_path[1]; char *sockname = &addr->sun_path[1];
char path[PATH_MAX+18]; char *path;
uint64_t hash; uint64_t hash;
/* addr.sun_path is only 108 bytes, so we hash the full name and /* addr.sun_path is only 108 bytes, so we hash the full name and
...@@ -150,18 +150,20 @@ int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) { ...@@ -150,18 +150,20 @@ int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
*/ */
memset(addr, 0, sizeof(*addr)); memset(addr, 0, sizeof(*addr));
addr->sun_family = AF_UNIX; addr->sun_family = AF_UNIX;
len = sizeof(addr->sun_path) - 1; len = strlen(lxcpath) + 18;
ret = snprintf(path, sizeof(path), "lxc/%s/monitor-sock", lxcpath); path = alloca(len);
if (ret < 0 || ret >= sizeof(path)) { ret = snprintf(path, len, "lxc/%s/monitor-sock", lxcpath);
ERROR("lxcpath %s too long for monitor unix socket", lxcpath); if (ret < 0 || ret >= len) {
ERROR("memory error creating monitor path");
return -1; return -1;
} }
len = sizeof(addr->sun_path) - 1;
hash = fnv_64a_buf(path, ret, FNV1A_64_INIT); hash = fnv_64a_buf(path, ret, FNV1A_64_INIT);
ret = snprintf(sockname, len, "lxc/%016" PRIx64 "/%s", hash, lxcpath); ret = snprintf(sockname, len, "lxc/%016" PRIx64 "/%s", hash, lxcpath);
if (ret < 0) if (ret < 0)
return -1; return -1;
sockname[sizeof(addr->sun_path)-2] = '\0'; sockname[sizeof(addr->sun_path)-3] = '\0';
INFO("using monitor sock name %s", sockname); INFO("using monitor sock name %s", sockname);
return 0; return 0;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment