#include <fcntl.h>
#include <poll.h>
#include <sys/socket.h>
-#include <inttypes.h>
#include <sys/un.h>
#include <sys/param.h>
#include <malloc.h>
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] = {
* 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);
* 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);
#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>
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;
+}