From 0c852fd6a4b13dd111806180a8292229b18d7d9d Mon Sep 17 00:00:00 2001 From: Tao He Date: Mon, 14 Aug 2023 09:42:05 +0800 Subject: [PATCH] Support WSL1 support where memfd_create returns `ENOSYS (function not implemented)`. (#1519) Fixes comment https://github.com/v6d-io/v6d/issues/141#issuecomment-1675590455 Signed-off-by: Tao He --- src/server/memory/malloc.cc | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/server/memory/malloc.cc b/src/server/memory/malloc.cc index 7b43de7d..5d8b5101 100644 --- a/src/server/memory/malloc.cc +++ b/src/server/memory/malloc.cc @@ -95,22 +95,30 @@ struct memfd_create_compat { std::string file_template = "vineyard-bulk-XXXXXX"; std::vector file_name(file_template.begin(), file_template.end()); file_name.push_back('\0'); - return memfd_create_fn(&file_name[0], 0); - } else { - std::string file_template = "/dev/shm/vineyard-bulk-XXXXXX"; - std::vector file_name(file_template.begin(), file_template.end()); - file_name.push_back('\0'); - int fd = mkstemp(&file_name[0]); - if (fd < 0) { - return fd; - } - if (unlink(&file_name[0]) != 0) { - LOG(ERROR) << "failed to unlink file " << &file_name[0]; - close(fd); - return -1; + int fd = memfd_create_fn(&file_name[0], 0); + if (fd < 0 && errno == ENOSYS) { + LOG(WARNING) << "Looks like that we are working inside WSL 1 (" + << strerror(errno) + << "), fallback to mkstemp(3) for shared memory"; + // disable memfd_create, and fall through + memfd_create_fn = nullptr; + } else { + return fd; // propogate the error } + } + std::string file_template = "/dev/shm/vineyard-bulk-XXXXXX"; + std::vector file_name(file_template.begin(), file_template.end()); + file_name.push_back('\0'); + int fd = mkstemp(&file_name[0]); + if (fd < 0) { return fd; } + if (unlink(&file_name[0]) != 0) { + LOG(ERROR) << "failed to unlink file " << &file_name[0]; + close(fd); + return -1; + } + return fd; } ~memfd_create_compat() {