From 21bcf31c43fe87766e97abf531c74053fa245b1b Mon Sep 17 00:00:00 2001 From: Bernhard Miklautz Date: Mon, 28 Apr 2014 16:46:07 +0200 Subject: [PATCH 1/3] wtsapi: consolidate lib initialization and load Move duplicated code to LoadAndInitialize. --- winpr/libwinpr/wtsapi/wtsapi.c | 56 ++++++++++++++++------------------ 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/winpr/libwinpr/wtsapi/wtsapi.c b/winpr/libwinpr/wtsapi/wtsapi.c index 151bcded9..3f8cadf35 100644 --- a/winpr/libwinpr/wtsapi/wtsapi.c +++ b/winpr/libwinpr/wtsapi/wtsapi.c @@ -397,35 +397,43 @@ BOOL WTSRegisterWtsApiFunctionTable(PWtsApiFunctionTable table) return TRUE; } +static BOOL LoadAndInitialize(char *library) +{ + INIT_WTSAPI_FN pInitWtsApi; + g_WtsApiModule = LoadLibraryA(library); + + if (!g_WtsApiModule) + return FALSE; + + pInitWtsApi = (INIT_WTSAPI_FN) GetProcAddress(g_WtsApiModule, "InitWtsApi"); + + if (!pInitWtsApi) + { + return FALSE; + } + g_WtsApi = pInitWtsApi(); + return TRUE; +} + void InitializeWtsApiStubs_Env() { DWORD nSize; char* env = NULL; - INIT_WTSAPI_FN pInitWtsApi; if (g_WtsApi) return; nSize = GetEnvironmentVariableA("WTSAPI_LIBRARY", NULL, 0); - if (nSize) + if (!nSize) { - env = (LPSTR) malloc(nSize); - nSize = GetEnvironmentVariableA("WTSAPI_LIBRARY", env, nSize); - } - - if (env) - g_WtsApiModule = LoadLibraryA(env); - - if (!g_WtsApiModule) return; - - pInitWtsApi = (INIT_WTSAPI_FN) GetProcAddress(g_WtsApiModule, "InitWtsApi"); - - if (pInitWtsApi) - { - g_WtsApi = pInitWtsApi(); } + + env = (LPSTR) malloc(nSize); + nSize = GetEnvironmentVariableA("WTSAPI_LIBRARY", env, nSize); + if (env) + LoadAndInitialize(env); } void InitializeWtsApiStubs_FreeRDS() @@ -459,19 +467,7 @@ void InitializeWtsApiStubs_FreeRDS() if (wtsapi_library) { - INIT_WTSAPI_FN pInitWtsApi; - - g_WtsApiModule = LoadLibraryA(wtsapi_library); - - if (g_WtsApiModule) - { - pInitWtsApi = (INIT_WTSAPI_FN) GetProcAddress(g_WtsApiModule, "InitWtsApi"); - - if (pInitWtsApi) - { - g_WtsApi = pInitWtsApi(); - } - } + LoadAndInitialize(wtsapi_library); } free(prefix_libdir); @@ -492,4 +488,6 @@ void InitializeWtsApiStubs(void) if (!g_WtsApi) InitializeWtsApiStubs_FreeRDS(); + + return; } From bc73a554fccbfee02d9c6561d4675c25d50a57ca Mon Sep 17 00:00:00 2001 From: Bernhard Miklautz Date: Mon, 28 Apr 2014 16:48:24 +0200 Subject: [PATCH 2/3] wtsapi: replaced printf with fprintf --- winpr/libwinpr/wtsapi/wtsapi.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/winpr/libwinpr/wtsapi/wtsapi.c b/winpr/libwinpr/wtsapi/wtsapi.c index 3f8cadf35..2d71595ce 100644 --- a/winpr/libwinpr/wtsapi/wtsapi.c +++ b/winpr/libwinpr/wtsapi/wtsapi.c @@ -449,13 +449,13 @@ void InitializeWtsApiStubs_FreeRDS() if (IniFile_Parse(ini, "/var/run/freerds.instance") < 0) { - printf("failed to parse freerds.instance\n"); + fprintf(stderr, "failed to parse freerds.instance\n"); } prefix = IniFile_GetKeyValueString(ini, "FreeRDS", "prefix"); libdir = IniFile_GetKeyValueString(ini, "FreeRDS", "libdir"); - printf("FreeRDS: %s / %s\n", prefix, libdir); + fprintf(stderr, "FreeRDS (prefix / libdir): %s / %s\n", prefix, libdir); if (prefix && libdir) { From 0bcd933e704b0365e89c3b6f5ae314e188225bae Mon Sep 17 00:00:00 2001 From: Bernhard Miklautz Date: Mon, 28 Apr 2014 16:54:58 +0200 Subject: [PATCH 3/3] wtsapi: add fallback to load freerds lib Try to load libfreerds-fdsapi.so directly if the freerds ini file isn't found. This adds a fallback that uses the systems library loading mechanisms (e.g LD_LIBRARY_PATH). Might also be required for older version of FreeRDS. --- winpr/libwinpr/wtsapi/wtsapi.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/winpr/libwinpr/wtsapi/wtsapi.c b/winpr/libwinpr/wtsapi/wtsapi.c index 2d71595ce..02cf985cb 100644 --- a/winpr/libwinpr/wtsapi/wtsapi.c +++ b/winpr/libwinpr/wtsapi/wtsapi.c @@ -436,6 +436,7 @@ void InitializeWtsApiStubs_Env() LoadAndInitialize(env); } +#define FREERDS_LIBRARY_NAME "libfreerds-fdsapi.so" void InitializeWtsApiStubs_FreeRDS() { char* prefix; @@ -449,7 +450,10 @@ void InitializeWtsApiStubs_FreeRDS() if (IniFile_Parse(ini, "/var/run/freerds.instance") < 0) { + IniFile_Free(ini); fprintf(stderr, "failed to parse freerds.instance\n"); + LoadAndInitialize(FREERDS_LIBRARY_NAME); + return; } prefix = IniFile_GetKeyValueString(ini, "FreeRDS", "prefix"); @@ -463,7 +467,7 @@ void InitializeWtsApiStubs_FreeRDS() char* wtsapi_library; prefix_libdir = GetCombinedPath(prefix, libdir); - wtsapi_library = GetCombinedPath(prefix_libdir, "libfreerds-fdsapi.so"); + wtsapi_library = GetCombinedPath(prefix_libdir, FREERDS_LIBRARY_NAME); if (wtsapi_library) {