Skip to content

Commit

Permalink
monitor: fix sockname calculation for long lxcpaths
Browse files Browse the repository at this point in the history
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: Serge Hallyn <serge.hallyn@ubuntu.com>
Acked-by: Stéphane Graber <stgraber@ubuntu.com>
  • Loading branch information
hallyn authored and stgraber committed Aug 18, 2014
1 parent e858984 commit 073135b
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions src/lxc/monitor.c
Expand Up @@ -142,26 +142,28 @@ int lxc_monitor_sock_name(const char *lxcpath, struct sockaddr_un *addr) {
size_t len;
int ret;
char *sockname = &addr->sun_path[1];
char path[PATH_MAX+18];
char *path;
uint64_t hash;

/* addr.sun_path is only 108 bytes, so we hash the full name and
* then append as much of the name as we can fit.
*/
memset(addr, 0, sizeof(*addr));
addr->sun_family = AF_UNIX;
len = sizeof(addr->sun_path) - 1;
ret = snprintf(path, sizeof(path), "lxc/%s/monitor-sock", lxcpath);
if (ret < 0 || ret >= sizeof(path)) {
ERROR("lxcpath %s too long for monitor unix socket", lxcpath);
len = strlen(lxcpath) + 18;
path = alloca(len);
ret = snprintf(path, len, "lxc/%s/monitor-sock", lxcpath);
if (ret < 0 || ret >= len) {
ERROR("memory error creating monitor path");
return -1;
}

len = sizeof(addr->sun_path) - 1;
hash = fnv_64a_buf(path, ret, FNV1A_64_INIT);
ret = snprintf(sockname, len, "lxc/%016" PRIx64 "/%s", hash, lxcpath);
if (ret < 0)
return -1;
sockname[sizeof(addr->sun_path)-2] = '\0';
sockname[sizeof(addr->sun_path)-3] = '\0';
INFO("using monitor sock name %s", sockname);
return 0;
}
Expand Down

0 comments on commit 073135b

Please sign in to comment.