Unverified Commit 1dc4a3b5 by Christian Brauner Committed by Stéphane Graber

utils: lxc_make_abstract_socket_name()

parent f6aa6929
......@@ -28,7 +28,6 @@
#include <fcntl.h>
#include <poll.h>
#include <sys/socket.h>
#include <inttypes.h>
#include <sys/un.h>
#include <sys/param.h>
#include <malloc.h>
......@@ -76,62 +75,6 @@
lxc_log_define(lxc_commands, lxc);
static int fill_sock_name(char *path, int len, const char *lxcname,
const char *lxcpath, const char *hashed_sock_name)
{
const char *name;
char *tmppath;
size_t tmplen;
uint64_t hash;
int ret;
name = lxcname;
if (!name)
name = "";
if (hashed_sock_name != NULL) {
ret = snprintf(path, len, "lxc/%s/command", hashed_sock_name);
if (ret < 0 || ret >= len) {
ERROR("Error writing to command sock path");
return -1;
}
return 0;
}
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, 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 "/command", hash);
if (ret < 0 || ret >= len) {
ERROR("Command socket name too long");
return -1;
}
return 0;
}
static const char *lxc_cmd_str(lxc_cmd_t cmd)
{
static const char * const cmdname[LXC_CMD_MAX] = {
......@@ -300,7 +243,8 @@ static int lxc_cmd(const char *name, struct lxc_cmd_rr *cmd, int *stopped,
* because we print the sockname out sometimes.
*/
len = sizeof(path)-2;
if (fill_sock_name(offset, len, name, lxcpath, hashed_sock_name))
if (lxc_make_abstract_socket_name(offset, len, name, lxcpath,
hashed_sock_name, "command"))
return -1;
sock = lxc_abstract_unix_connect(path);
......@@ -1156,7 +1100,8 @@ int lxc_cmd_init(const char *name, struct lxc_handler *handler,
* because we print the sockname out sometimes.
*/
len = sizeof(path) - 2;
if (fill_sock_name(offset, len, name, lxcpath, NULL))
if (lxc_make_abstract_socket_name(offset, len, name, lxcpath, NULL,
"command"))
return -1;
fd = lxc_abstract_unix_open(path, SOCK_STREAM, 0);
......
......@@ -23,11 +23,13 @@
#include "config.h"
#define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <inttypes.h>
#include <libgen.h>
#include <stddef.h>
#include <stdio.h>
......@@ -2336,3 +2338,62 @@ int run_command(char *buf, size_t buf_size, int (*child_fn)(void *), void *args)
return fret;
}
int lxc_make_abstract_socket_name(char *path, int len, const char *lxcname,
const char *lxcpath,
const char *hashed_sock_name,
const char *suffix)
{
const char *name;
char *tmppath;
size_t tmplen;
uint64_t hash;
int ret;
name = lxcname;
if (!name)
name = "";
if (hashed_sock_name != NULL) {
ret =
snprintf(path, len, "lxc/%s/%s", hashed_sock_name, suffix);
if (ret < 0 || ret >= len) {
ERROR("Failed to create abstract socket name");
return -1;
}
return 0;
}
if (!lxcpath) {
lxcpath = lxc_global_config_value("lxc.lxcpath");
if (!lxcpath) {
ERROR("Failed to allocate memory");
return -1;
}
}
ret = snprintf(path, len, "%s/%s/%s", lxcpath, name, suffix);
if (ret < 0) {
ERROR("Failed to create abstract socket name");
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("Failed to create abstract socket name");
return -1;
}
hash = fnv_64a_buf(tmppath, ret, FNV1A_64_INIT);
ret = snprintf(path, len, "lxc/%016" PRIx64 "/%s", hash, suffix);
if (ret < 0 || ret >= len) {
ERROR("Failed to create abstract socket name");
return -1;
}
return 0;
}
......@@ -374,5 +374,9 @@ int lxc_unstack_mountpoint(const char *path, bool lazy);
* @param[in] args Arguments to be passed to child_fn.
*/
int run_command(char *buf, size_t buf_size, int (*child_fn)(void *), void *args);
int lxc_make_abstract_socket_name(char *path, int len, const char *lxcname,
const char *lxcpath,
const char *hashed_sock_name,
const char *suffix);
#endif /* __LXC_UTILS_H */
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