diff options
author | Gabriele Svelto <gabriele.svelto@gmail.com> | 2012-05-08 20:17:01 +0200 |
---|---|---|
committer | Martin Sustrik <sustrik@250bpm.com> | 2012-05-12 12:13:19 +0200 |
commit | e1dff473a625cdbecbaddb89d48fc675efea09f0 (patch) | |
tree | 3bbed0ac0a82c3b719fdeaa71c04c66966abfa8a /src | |
parent | 0b1bfac99a01db54396875ca49987f4117061990 (diff) |
Enabled plug-in loading on all hosts with support for dlopen()
Previously the code used for loading plug-ins would be built only on
Linux hosts. Now the required functions are checked for and plug-in
loading code is enabled if those are present on all platforms. Using
lstat() to check for file types also increases portability to hosts that
do not have the dirent.d_type field. If shared libraries support is
disabled plug-ins will also be automatically disabled.
Signed-off-by: Gabriele Svelto <gabriele.svelto@gmail.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/ctx.cpp | 45 | ||||
-rw-r--r-- | src/ctx.hpp | 7 |
2 files changed, 35 insertions, 17 deletions
diff --git a/src/ctx.cpp b/src/ctx.cpp index f5d9719..063f96c 100644 --- a/src/ctx.cpp +++ b/src/ctx.cpp @@ -23,12 +23,27 @@ #if defined XS_HAVE_WINDOWS #include "windows.hpp" #else -#include <unistd.h> -#endif - -#if defined XS_HAVE_LINUX -#include <dlfcn.h> -#include <dirent.h> +# if HAVE_UNISTD_H +# include <unistd.h> +# endif +# if HAVE_SYS_TYPES_H +# include <sys/types.h> +# endif +# if HAVE_SYS_STAT_H +# include <sys/stat.h> +# endif +# if HAVE_DLFCN_H +# include <dlfcn.h> +# endif +# if HAVE_LINK_H +# include <link.h> +# endif +# if HAVE_FCNTL_H +# include <fcntl.h> +# endif +# if HAVE_DIRENT_H +# include <dirent.h> +# endif #endif #include <new> @@ -114,7 +129,7 @@ next: BOOL brc = FindClose (fh); win_assert (brc != 0); -#elif defined XS_HAVE_LINUX +#elif XS_HAVE_PLUGINS // Load all the installed plug-ins. std::string path (XS_PREFIX_PATH); @@ -126,12 +141,17 @@ next: errno_assert (dp); dirent dir, *dirp; + struct stat stats; + while (true) { rc = readdir_r (dp, &dir, &dirp); assert (rc == 0); if (!dirp) break; - if (dir.d_type == DT_REG) { + + rc = lstat (dirp->d_name, &stats); + + if ((rc == 0) && S_ISREG (stats.st_mode)) { // Ignore the files without .xsp extension. std::string file = dir.d_name; @@ -139,7 +159,7 @@ next: continue; if (file.substr (file.size () - 4) != ".xsp") continue; - + // Open the specified dynamic library. std::string filename = path + "/" + file; void *dl = dlopen (filename.c_str (), RTLD_LOCAL | RTLD_NOW); @@ -260,7 +280,7 @@ int xs::ctx_t::terminate () it != plugins.end (); ++it) FreeLibrary (*it); opt_sync.unlock (); -#elif defined XS_HAVE_LINUX +#elif XS_HAVE_PLUGINS opt_sync.lock (); for (plugins_t::iterator it = plugins.begin (); it != plugins.end (); ++it) @@ -412,7 +432,7 @@ void xs::ctx_t::destroy_socket (class socket_base_t *socket_) // Free the associared thread slot. uint32_t tid = socket_->get_tid (); empty_slots.push_back (tid); - slots [tid] = NULL; + slots [tid] = NULL; // Remove the socket from the list of sockets. sockets.erase (socket_); @@ -497,7 +517,7 @@ void xs::ctx_t::unregister_endpoints (socket_base_t *socket_) } ++it; } - + endpoints_sync.unlock (); } @@ -528,4 +548,3 @@ xs::endpoint_t xs::ctx_t::find_endpoint (const char *addr_) // is a global variable. Thus, even sockets created in different contexts have // unique IDs. xs::atomic_counter_t xs::ctx_t::max_socket_id; - diff --git a/src/ctx.hpp b/src/ctx.hpp index 7b0b74b..8e2fd03 100644 --- a/src/ctx.hpp +++ b/src/ctx.hpp @@ -54,7 +54,7 @@ namespace xs // Context object encapsulates all the global state associated with // the library. - + class ctx_t { public: @@ -173,7 +173,7 @@ namespace xs #if defined XS_HAVE_WINDOWS typedef std::vector <HMODULE> plugins_t; plugins_t plugins; -#elif defined XS_HAVE_LINUX +#elif defined XS_HAVE_PLUGINS typedef std::vector <void*> plugins_t; plugins_t plugins; #endif @@ -185,8 +185,7 @@ namespace xs ctx_t (const ctx_t&); const ctx_t &operator = (const ctx_t&); }; - + } #endif - |