]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
common: Add dlfcn_compat.h for win32 32704/head
authorLucian Petrut <lpetrut@cloudbasesolutions.com>
Fri, 4 Oct 2019 14:56:43 +0000 (17:56 +0300)
committerLucian Petrut <lpetrut@cloudbasesolutions.com>
Tue, 14 Apr 2020 11:11:32 +0000 (11:11 +0000)
We'll emulate the dlfcn.h interface using Windows functions, which
will allow loading Windows DLLs.

Signed-off-by: Lucian Petrut <lpetrut@cloudbasesolutions.com>
src/CMakeLists.txt
src/common/CMakeLists.txt
src/common/PluginRegistry.cc
src/common/TracepointProvider.h
src/common/dlfcn_win32.cc [new file with mode: 0644]
src/erasure-code/ErasureCodePlugin.cc
src/include/config-h.in.cmake
src/include/dlfcn_compat.h [new file with mode: 0644]
src/osd/ClassHandler.cc

index 460dde54a5069a014d6f6c9175fd338baf60a13e..23fc2b80d916ba64b88816a1027c850ea2a436ed 100644 (file)
@@ -408,6 +408,7 @@ endif()
 
 if(WIN32)
   list(APPEND ceph_common_deps ws2_32 mswsock)
+  list(APPEND ceph_common_deps dlfcn_win32)
 endif()
 
 if(WITH_BLUESTORE_PMEM OR WITH_RBD_RWL)
index 5e7fb6a78ebe8aa03d8f14a7c82d7e1dfa010b9d..cb6f4faa65e5fab5a5b73e1f4ce6bea63dbced8f 100644 (file)
@@ -7,6 +7,10 @@ add_library(common_texttable_obj OBJECT
 add_library(common_prioritycache_obj OBJECT
   PriorityCache.cc)
 
+if(WIN32)
+  add_library(dlfcn_win32 STATIC dlfcn_win32.cc)
+endif()
+
 set(common_srcs
   AsyncOpTracker.cc
   BackTrace.cc
index c697aaa17f00628b38838847d3b6d1c91987f4d1..1e01528de20229defafe2ccd297f3ac239ff86a8 100644 (file)
 #include "common/ceph_context.h"
 #include "common/errno.h"
 #include "common/debug.h"
-
-#include <dlfcn.h>
+#include "include/dlfcn_compat.h"
 
 #define PLUGIN_PREFIX "libceph_"
-#ifdef __APPLE__
-#define PLUGIN_SUFFIX ".dylib"
-#else
-#define PLUGIN_SUFFIX ".so"
-#endif
+#define PLUGIN_SUFFIX SHARED_LIB_SUFFIX
 #define PLUGIN_INIT_FUNCTION "__ceph_plugin_init"
 #define PLUGIN_VERSION_FUNCTION "__ceph_plugin_version"
 
index 30e290600a5247e452b4eaf8b0e2d100ca9c7bc8..fe447677ccb3104d5f3f74e6e5b8918fe827f4dc 100644 (file)
@@ -7,7 +7,7 @@
 #include "common/ceph_context.h"
 #include "common/config_obs.h"
 #include "common/ceph_mutex.h"
-#include <dlfcn.h>
+#include "include/dlfcn_compat.h"
 
 class TracepointProvider : public md_config_obs_t {
 public:
diff --git a/src/common/dlfcn_win32.cc b/src/common/dlfcn_win32.cc
new file mode 100644 (file)
index 0000000..cfe4427
--- /dev/null
@@ -0,0 +1,57 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2020 SUSE LINUX GmbH
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#include <sstream>
+#include <windows.h>
+
+#include "include/dlfcn_compat.h"
+
+
+void* dlopen(const char *filename, int flags) {
+  return LoadLibrary(filename);
+}
+
+int dlclose(void* handle) {
+  //FreeLibrary returns 0 on error, as opposed to dlclose.
+  return !FreeLibrary(handle);
+}
+
+void* dlsym(void* handle, const char* symbol) {
+  return (void*)GetProcAddress(handle, symbol);
+}
+
+dl_errmsg_t dlerror() {
+  DWORD err_code = ::GetLastError();
+  // As opposed to dlerror messages, this has to be freed.
+  LPSTR msg = NULL;
+  DWORD msg_len = FormatMessageA(
+    FORMAT_MESSAGE_ALLOCATE_BUFFER |
+    FORMAT_MESSAGE_FROM_SYSTEM |
+    FORMAT_MESSAGE_IGNORE_INSERTS,
+    NULL,
+    err_code,
+    0,
+    (LPSTR) &msg,
+    0,
+    NULL);
+  if (!msg_len) {
+    std::ostringstream msg_stream;
+    msg_stream << "Unknown error (" << err_code << ").";
+    return msg_stream.str();
+  }
+  std::string msg_s(msg);
+  LocalFree(msg);
+  return msg_s;
+}
+
index a42bd957a11a806d4e38ac35e49dba056ebc5a8a..f189b91fdfe3598958f476693fed9be91dc90baf 100644 (file)
  */
 
 #include <errno.h>
-#include <dlfcn.h>
 
 #include "ceph_ver.h"
 #include "ErasureCodePlugin.h"
 #include "common/errno.h"
+#include "include/dlfcn_compat.h"
 #include "include/str_list.h"
 #include "include/ceph_assert.h"
 
 using namespace std;
 
 #define PLUGIN_PREFIX "libec_"
-#if defined(__APPLE__)
-#define PLUGIN_SUFFIX ".dylib"
-#else
-#define PLUGIN_SUFFIX ".so"
-#endif
+#define PLUGIN_SUFFIX SHARED_LIB_SUFFIX
 #define PLUGIN_INIT_FUNCTION "__erasure_code_init"
 #define PLUGIN_VERSION_FUNCTION "__erasure_code_version"
 
index 31dfb8983a7f1037d77058928720c0f5b3b5d462..dc213938f5c9c7e4e5a62fd0bb3d45287923f6ab 100644 (file)
 /* Define if RWL is enabled */
 #cmakedefine WITH_RBD_RWL
 
+/* Shared library extension, such as .so, .dll or .dylib */
+#cmakedefine CMAKE_SHARED_LIBRARY_SUFFIX "@CMAKE_SHARED_LIBRARY_SUFFIX@"
+
 #endif /* CONFIG_H */
diff --git a/src/include/dlfcn_compat.h b/src/include/dlfcn_compat.h
new file mode 100644 (file)
index 0000000..95fd64e
--- /dev/null
@@ -0,0 +1,48 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+/*
+ * Ceph - scalable distributed file system
+ *
+ * Copyright (C) 2020 SUSE LINUX GmbH
+ *
+ * This is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License version 2.1, as published by the Free Software
+ * Foundation.  See file COPYING.
+ *
+ */
+
+#ifndef DLFCN_COMPAT_H
+#define DLFCN_COMPAT_H
+
+#include "acconfig.h"
+
+#define SHARED_LIB_SUFFIX CMAKE_SHARED_LIBRARY_SUFFIX
+
+#ifdef _WIN32
+  #include <string>
+
+  using dl_errmsg_t = std::string;
+
+  // The load mode flags will be ignored on Windows. We keep the same
+  // values for debugging purposes though.
+  #define RTLD_LAZY       0x00001
+  #define RTLD_NOW        0x00002
+  #define RTLD_BINDING_MASK   0x3
+  #define RTLD_NOLOAD     0x00004
+  #define RTLD_DEEPBIND   0x00008
+  #define RTLD_GLOBAL     0x00100
+  #define RTLD_LOCAL      0
+  #define RTLD_NODELETE   0x01000
+
+  void* dlopen(const char *filename, int flags);
+  int dlclose(void* handle);
+  dl_errmsg_t dlerror();
+  void* dlsym(void* handle, const char* symbol);
+#else
+  #include <dlfcn.h>
+
+  using dl_errmsg_t = char*;
+#endif /* _WIN32 */
+
+#endif /* DLFCN_H */
index 2bf6a69ad100ddf2d15d684f196fdd25cf1893a7..02c2cb694b1d1511ca2dcf5fc7c4784522a1bc38 100644 (file)
@@ -5,8 +5,7 @@
 #include "ClassHandler.h"
 #include "common/errno.h"
 #include "common/ceph_context.h"
-
-#include <dlfcn.h>
+#include "include/dlfcn_compat.h"
 
 #include <map>
 
@@ -23,7 +22,7 @@
 
 
 #define CLS_PREFIX "libcls_"
-#define CLS_SUFFIX ".so"
+#define CLS_SUFFIX SHARED_LIB_SUFFIX
 
 using std::map;
 using std::set;