From 7935833c235e80c07ffc593abcbb5cda689e37f4 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Thu, 29 Jun 2017 00:50:19 +0200 Subject: [PATCH] utils: lxc_make_abstract_socket_name() Signed-off-by: Christian Brauner --- src/lxc/commands.c | 63 +++------------------------------------------- src/lxc/utils.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ src/lxc/utils.h | 4 +++ 3 files changed, 69 insertions(+), 59 deletions(-) diff --git a/src/lxc/commands.c b/src/lxc/commands.c index d7f1ccdc8..1333b8c57 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -28,7 +28,6 @@ #include #include #include -#include #include #include #include @@ -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); diff --git a/src/lxc/utils.c b/src/lxc/utils.c index 81f6987aa..c82cc6943 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -23,11 +23,13 @@ #include "config.h" +#define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */ #include #include #include #include #include +#include #include #include #include @@ -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; +} diff --git a/src/lxc/utils.h b/src/lxc/utils.h index 916ee56a6..7d81a3270 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -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 */ -- 2.47.3