2011-07-08 10:28:52 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-07-08 10:28:52 +04:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2012-08-15 01:09:01 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
2012-03-06 06:04:42 +04:00
|
|
|
#include "config.h"
|
2012-08-15 01:09:01 +04:00
|
|
|
#endif
|
2012-03-06 06:04:42 +04:00
|
|
|
|
2011-07-08 10:28:52 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
2012-05-05 03:36:35 +04:00
|
|
|
#include <winpr/windows.h>
|
2012-10-09 07:42:01 +04:00
|
|
|
|
2012-02-10 02:48:30 +04:00
|
|
|
#include <freerdp/utils/file.h>
|
2011-08-16 23:00:25 +04:00
|
|
|
#include <freerdp/utils/print.h>
|
2012-02-10 02:48:30 +04:00
|
|
|
#include <freerdp/utils/memory.h>
|
2011-07-08 10:28:52 +04:00
|
|
|
#include <freerdp/utils/load_plugin.h>
|
2012-02-19 07:04:28 +04:00
|
|
|
|
2011-07-08 10:28:52 +04:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
2011-11-12 09:51:41 +04:00
|
|
|
#define DLOPEN(f) LoadLibraryA(f)
|
2011-07-08 10:28:52 +04:00
|
|
|
#define DLSYM(f, n) GetProcAddress(f, n)
|
|
|
|
#define DLCLOSE(f) FreeLibrary(f)
|
2012-01-11 03:02:39 +04:00
|
|
|
#define DLERROR() ""
|
2011-07-08 10:28:52 +04:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#define DLOPEN(f) dlopen(f, RTLD_LOCAL | RTLD_LAZY)
|
|
|
|
#define DLSYM(f, n) dlsym(f, n)
|
|
|
|
#define DLCLOSE(f) dlclose(f)
|
2012-01-11 03:02:39 +04:00
|
|
|
#define DLERROR() dlerror()
|
2011-07-08 10:28:52 +04:00
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2012-07-16 18:18:50 +04:00
|
|
|
#define MAX_STATIC_PLUGINS 50
|
|
|
|
|
|
|
|
struct static_plugin
|
|
|
|
{
|
2012-10-06 00:01:59 +04:00
|
|
|
const char* name;
|
|
|
|
const char* entry_name;
|
|
|
|
void* entry_addr;
|
2012-07-16 18:18:50 +04:00
|
|
|
};
|
|
|
|
typedef struct static_plugin staticPlugin;
|
|
|
|
|
|
|
|
static staticPlugin g_static_plugins[MAX_STATIC_PLUGINS];
|
|
|
|
static int g_static_plugins_count;
|
|
|
|
|
2012-05-04 17:42:19 +04:00
|
|
|
/**
|
|
|
|
* This function will load the specified library, retrieve the specified symbol in it, and return a pointer to it.
|
|
|
|
* It is used in freerdp_load_plugin() and freerdp_load_channel_plugin().
|
|
|
|
* It seems there is no way to unload the library once this call is made. Now since this is used for plugins,
|
|
|
|
* we probably don't need to take care of unloading them, as it will be done only at shutdown.
|
|
|
|
*
|
|
|
|
* @param file [IN] - library name
|
|
|
|
* @param name [IN] - symbol name to find in the library
|
|
|
|
*
|
|
|
|
* @return pointer to the referenced symbol. NULL if an error occured.
|
|
|
|
*/
|
2012-02-10 04:48:52 +04:00
|
|
|
void* freerdp_load_library_symbol(const char* file, const char* name)
|
|
|
|
{
|
|
|
|
void* library;
|
|
|
|
void* symbol;
|
|
|
|
|
|
|
|
library = DLOPEN(file);
|
|
|
|
|
|
|
|
if (library == NULL)
|
|
|
|
{
|
|
|
|
printf("freerdp_load_library_symbol: failed to open %s: %s\n", file, DLERROR());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
symbol = DLSYM(library, name);
|
|
|
|
|
|
|
|
if (symbol == NULL)
|
|
|
|
{
|
|
|
|
printf("freerdp_load_library_symbol: failed to load %s: %s\n", file, DLERROR());
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return symbol;
|
|
|
|
}
|
|
|
|
|
2012-05-04 17:42:19 +04:00
|
|
|
/**
|
|
|
|
* This function is used to load plugins. It will load the specified library, retrieve the specified symbol,
|
|
|
|
* and return a pointer to it.
|
|
|
|
* There is no way to unload the library once it has been loaded this way - since it is used for plugins only,
|
|
|
|
* maybe it's not a must.
|
|
|
|
*
|
|
|
|
* @param name [IN] - name of the library to load
|
|
|
|
* @param entry_name [IN] - name of the symbol to find and load from the library
|
|
|
|
*
|
|
|
|
* @return Pointer to the loaded symbol. NULL if an error occurred.
|
|
|
|
*/
|
2011-07-08 10:28:52 +04:00
|
|
|
void* freerdp_load_plugin(const char* name, const char* entry_name)
|
|
|
|
{
|
2012-02-10 02:48:30 +04:00
|
|
|
char* path;
|
2011-07-08 10:28:52 +04:00
|
|
|
void* entry;
|
2012-02-10 02:48:30 +04:00
|
|
|
char* suffixed_name;
|
|
|
|
|
2012-07-16 18:18:50 +04:00
|
|
|
/* first attempt to load a static plugin */
|
|
|
|
entry = freerdp_load_static_plugin(name, entry_name);
|
2012-10-06 00:01:59 +04:00
|
|
|
|
|
|
|
if (entry != NULL)
|
|
|
|
return entry;
|
2012-07-16 18:18:50 +04:00
|
|
|
|
2012-02-10 02:48:30 +04:00
|
|
|
suffixed_name = freerdp_append_shared_library_suffix((char*) name);
|
2011-07-08 10:28:52 +04:00
|
|
|
|
2012-02-10 02:48:30 +04:00
|
|
|
if (!freerdp_path_contains_separator(suffixed_name))
|
|
|
|
{
|
|
|
|
/* no explicit path given, use default path */
|
2012-02-19 07:04:28 +04:00
|
|
|
path = freerdp_construct_path(FREERDP_PLUGIN_PATH, suffixed_name);
|
2012-02-10 02:48:30 +04:00
|
|
|
}
|
2011-07-08 10:28:52 +04:00
|
|
|
else
|
2012-02-10 02:48:30 +04:00
|
|
|
{
|
|
|
|
/* explicit path given, use it instead of default path */
|
2012-10-09 07:42:01 +04:00
|
|
|
path = _strdup(suffixed_name);
|
2012-02-10 02:48:30 +04:00
|
|
|
}
|
2011-07-08 10:28:52 +04:00
|
|
|
|
2012-02-10 04:48:52 +04:00
|
|
|
entry = freerdp_load_library_symbol(path, entry_name);
|
2012-02-10 02:48:30 +04:00
|
|
|
|
2012-10-09 07:21:26 +04:00
|
|
|
free(suffixed_name);
|
|
|
|
free(path);
|
2012-02-10 04:48:52 +04:00
|
|
|
|
|
|
|
if (entry == NULL)
|
2011-07-08 10:28:52 +04:00
|
|
|
{
|
2012-02-10 04:48:52 +04:00
|
|
|
printf("freerdp_load_plugin: failed to load %s/%s\n", name, entry_name);
|
2011-07-08 10:28:52 +04:00
|
|
|
return NULL;
|
|
|
|
}
|
2012-02-10 02:48:30 +04:00
|
|
|
|
2012-02-10 04:48:52 +04:00
|
|
|
return entry;
|
|
|
|
}
|
2012-02-10 02:48:30 +04:00
|
|
|
|
2012-07-16 18:18:50 +04:00
|
|
|
/**
|
|
|
|
* This function is used to register a static plugin so that it can be loaded later on using freerdp_load_plugin.
|
|
|
|
*
|
|
|
|
* @param name [IN] - name of the library to load
|
|
|
|
* @param entry_name [IN] - name of the symbol to register for later loading
|
|
|
|
* @param entry [IN] - function pointer to the entry function
|
|
|
|
*
|
2012-10-09 10:31:28 +04:00
|
|
|
* @return TRUE on success, otherwise FALSE.
|
2012-07-16 18:18:50 +04:00
|
|
|
*/
|
2012-10-09 10:38:39 +04:00
|
|
|
BOOL freerdp_register_static_plugin(const char* name, const char* entry_name, void* entry_addr)
|
2012-07-16 18:18:50 +04:00
|
|
|
{
|
|
|
|
staticPlugin* plugin;
|
|
|
|
|
|
|
|
if (g_static_plugins_count >= MAX_STATIC_PLUGINS)
|
|
|
|
{
|
|
|
|
printf("freerdp_register_static_plugin: cannot register %s/%s", name, entry_name);
|
2012-10-09 10:31:28 +04:00
|
|
|
return FALSE;
|
2012-07-16 18:18:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* add the static plugin to the vector */
|
|
|
|
plugin = &g_static_plugins[g_static_plugins_count++];
|
|
|
|
plugin->name = name;
|
|
|
|
plugin->entry_name = entry_name;
|
|
|
|
plugin->entry_addr = entry_addr;
|
|
|
|
|
2012-10-09 10:31:28 +04:00
|
|
|
return TRUE;
|
2012-07-16 18:18:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a helper function, used by freerdp_load_plugin to return a function pointer from the static plugin table
|
|
|
|
*
|
|
|
|
* @param name [IN] - name of the library to load
|
|
|
|
* @param entry_name [IN] - name of the symbol to register for later loading
|
|
|
|
*
|
|
|
|
* @return Pointer to the entry function, NULL if no matching plugin could be found
|
|
|
|
*/
|
2012-10-06 00:01:59 +04:00
|
|
|
|
2012-07-16 18:18:50 +04:00
|
|
|
void* freerdp_load_static_plugin(const char* name, const char* entry_name)
|
|
|
|
{
|
|
|
|
int i;
|
2012-09-23 04:16:38 +04:00
|
|
|
staticPlugin* plugin;
|
2012-10-06 00:01:59 +04:00
|
|
|
|
2012-07-16 18:18:50 +04:00
|
|
|
for (i = 0; i < g_static_plugins_count; i++)
|
|
|
|
{
|
|
|
|
plugin = &g_static_plugins[i];
|
2012-09-23 04:16:38 +04:00
|
|
|
|
2012-07-16 18:18:50 +04:00
|
|
|
if (!strcmp(plugin->name, name) && !strcmp(plugin->entry_name, entry_name))
|
|
|
|
{
|
|
|
|
return plugin->entry_addr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|