From 3a9c7749797697fd0943dd27810eb286c4d7a2ed Mon Sep 17 00:00:00 2001 From: Vic Lee Date: Fri, 8 Jul 2011 14:28:52 +0800 Subject: [PATCH] libfreerdp-utils: add load_plugin module. --- CMakeLists.txt | 3 ++ include/freerdp/utils/load_plugin.h | 25 ++++++++++ libfreerdp-utils/CMakeLists.txt | 3 ++ libfreerdp-utils/load_plugin.c | 74 +++++++++++++++++++++++++++++ 4 files changed, 105 insertions(+) create mode 100644 include/freerdp/utils/load_plugin.h create mode 100644 libfreerdp-utils/load_plugin.c diff --git a/CMakeLists.txt b/CMakeLists.txt index de2c6b8d3..464d00503 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -56,6 +56,9 @@ test_big_endian(BIG_ENDIAN) # Path to put keymaps set(FREERDP_KEYMAP_PATH "${CMAKE_INSTALL_PREFIX}/freerdp/keymaps") +# Path to put plugins +set(FREERDP_PLUGIN_PATH "${CMAKE_INSTALL_PREFIX}/lib/freerdp") + # Include directories include_directories(${CMAKE_SOURCE_DIR}) include_directories(${CMAKE_SOURCE_DIR}/include) diff --git a/include/freerdp/utils/load_plugin.h b/include/freerdp/utils/load_plugin.h new file mode 100644 index 000000000..a06c16959 --- /dev/null +++ b/include/freerdp/utils/load_plugin.h @@ -0,0 +1,25 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * Plugin Loading Utils + * + * Copyright 2011 Vic Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __LOAD_PLUGIN_UTILS_H +#define __LOAD_PLUGIN_UTILS_H + +void* freerdp_load_plugin(const char* name, const char* entry_name); + +#endif /* __LOAD_PLUGIN_UTILS_H */ diff --git a/libfreerdp-utils/CMakeLists.txt b/libfreerdp-utils/CMakeLists.txt index ad482dcc7..f8ad0d8c4 100644 --- a/libfreerdp-utils/CMakeLists.txt +++ b/libfreerdp-utils/CMakeLists.txt @@ -20,12 +20,15 @@ set(FREERDP_UTILS_SRCS blob.c hexdump.c + load_plugin.c memory.c mutex.c semaphore.c stream.c unicode.c) +add_definitions(-DPLUGIN_PATH="${FREERDP_PLUGIN_PATH}") + add_library(freerdp-utils SHARED ${FREERDP_UTILS_SRCS}) set_target_properties(freerdp-utils PROPERTIES VERSION ${FREERDP_VERSION_FULL} SOVERSION ${FREERDP_VERSION}) diff --git a/libfreerdp-utils/load_plugin.c b/libfreerdp-utils/load_plugin.c new file mode 100644 index 000000000..350d78841 --- /dev/null +++ b/libfreerdp-utils/load_plugin.c @@ -0,0 +1,74 @@ +/** + * FreeRDP: A Remote Desktop Protocol Client + * Plugin Loading Utils + * + * Copyright 2011 Vic Lee + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include + +#ifdef _WIN32 + +#include +#define DLOPEN(f) LoadLibrary(f) +#define DLSYM(f, n) GetProcAddress(f, n) +#define DLCLOSE(f) FreeLibrary(f) +#define PATH_SEPARATOR '\\' +#define PLUGIN_EXT "dll" + +#else + +#include +#include +#define DLOPEN(f) dlopen(f, RTLD_LOCAL | RTLD_LAZY) +#define DLSYM(f, n) dlsym(f, n) +#define DLCLOSE(f) dlclose(f) +#define PATH_SEPARATOR '/' +#define PLUGIN_EXT "so" + +#endif + +void* freerdp_load_plugin(const char* name, const char* entry_name) +{ + char path[255]; + void* module; + void* entry; + + if (strchr(name, PATH_SEPARATOR) == NULL) + { + snprintf(path, sizeof(path), PLUGIN_PATH "%c%s." PLUGIN_EXT, PATH_SEPARATOR, name); + } + else + { + strncpy(path, name, sizeof(path)); + } + + module = DLOPEN(path); + if (module == NULL) + { + printf("freerdp_load_plugin: failed to open %s.\n", path); + return NULL; + } + entry = DLSYM(module, entry_name); + if (entry == NULL) + { + printf("freerdp_load_plugin: failed to load %s.\n", path); + return NULL; + } + return entry; +}