From bbf5cf35a35c2876181e70d729f6817f0f271dd8 Mon Sep 17 00:00:00 2001 From: Christian Brauner Date: Mon, 3 Jul 2017 23:31:04 +0200 Subject: [PATCH] commands: mv lxc_make_abstract_socket_name() Signed-off-by: Christian Brauner --- src/lxc/commands.c | 1 + src/lxc/commands_utils.c | 64 ++++++++++++++++++++++++++++++++++++++++ src/lxc/commands_utils.h | 5 ++++ src/lxc/start.c | 1 + src/lxc/utils.c | 59 ------------------------------------ src/lxc/utils.h | 4 --- 6 files changed, 71 insertions(+), 63 deletions(-) diff --git a/src/lxc/commands.c b/src/lxc/commands.c index 61ac6e708..bda9d7e81 100644 --- a/src/lxc/commands.c +++ b/src/lxc/commands.c @@ -40,6 +40,7 @@ #include "utils.h" #include "cgroup.h" #include "commands.h" +#include "commands_utils.h" #include "console.h" #include "confile.h" #include "lxclock.h" diff --git a/src/lxc/commands_utils.c b/src/lxc/commands_utils.c index 1d5e9a51b..d82e20ffe 100644 --- a/src/lxc/commands_utils.c +++ b/src/lxc/commands_utils.c @@ -18,8 +18,11 @@ */ #define _GNU_SOURCE +#define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */ #include +#include #include +#include #include #include #include @@ -30,6 +33,7 @@ #include "log.h" #include "monitor.h" #include "state.h" +#include "utils.h" lxc_log_define(lxc_commands_utils, lxc); @@ -89,3 +93,63 @@ int lxc_cmd_sock_get_state(const char *name, const char *lxcpath, close(state_client_fd); return ret; } + +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 || (size_t)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/commands_utils.h b/src/lxc/commands_utils.h index c1acdb5f1..40307b6ab 100644 --- a/src/lxc/commands_utils.h +++ b/src/lxc/commands_utils.h @@ -24,6 +24,11 @@ #include "state.h" +int lxc_make_abstract_socket_name(char *path, int len, const char *lxcname, + const char *lxcpath, + const char *hashed_sock_name, + const char *suffix); + /* lxc_cmd_sock_get_state Register a new state client fd in the container's * in-memory handler and retrieve the requested * states. diff --git a/src/lxc/start.c b/src/lxc/start.c index 8291fda12..cd9dc18be 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -67,6 +67,7 @@ #include "caps.h" #include "cgroup.h" #include "commands.h" +#include "commands_utils.h" #include "conf.h" #include "console.h" #include "error.h" diff --git a/src/lxc/utils.c b/src/lxc/utils.c index c82cc6943..2b30c056e 100644 --- a/src/lxc/utils.c +++ b/src/lxc/utils.c @@ -2338,62 +2338,3 @@ 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 7d81a3270..916ee56a6 100644 --- a/src/lxc/utils.h +++ b/src/lxc/utils.h @@ -374,9 +374,5 @@ 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.39.5