Browse code

build: libdl usage

1. properly detect.
2. Link only required components.
3. No way we don't have LoadLibrary on Windows.
4. ENABLE_PLUGIN should be controlled in autoconf.

Signed-off-by: Alon Bar-Lev <alon.barlev@gmail.com>
Acked-by: Adriaan de Jong <dejong@fox-it.com>
Acked-by: David Sommerseth <davids@redhat.com>
Signed-off-by: David Sommerseth <davids@redhat.com>

Alon Bar-Lev authored on 2012/03/01 05:12:02
Showing 5 changed files
... ...
@@ -395,7 +395,7 @@ AC_CHECK_HEADERS([ \
395 395
 	stdio.h stdarg.h \
396 396
 	time.h errno.h fcntl.h io.h direct.h \
397 397
 	ctype.h sys/types.h sys/socket.h \
398
-	signal.h unistd.h \
398
+	signal.h unistd.h dlfcn.h \
399 399
 	netinet/in.h netinet/in_systm.h \
400 400
 	netinet/tcp.h arpa/inet.h netdb.h \
401 401
 	windows.h winsock2.h ws2tcpip.h \
... ...
@@ -606,6 +606,13 @@ else
606 606
 	AC_CHECK_FUNCS(SOCKET_OPT_FUNCS)
607 607
 fi
608 608
 
609
+AC_CHECK_LIB(
610
+	[dl],
611
+	[dlopen],
612
+	[DL_LIBS="-ldl"]
613
+)
614
+AC_SUBST([DL_LIBS])
615
+
609 616
 case "${with_mem_check}" in
610 617
 	valgrind)
611 618
 		AC_CHECK_HEADER(
... ...
@@ -658,38 +665,6 @@ case "${with_mem_check}" in
658 658
 esac
659 659
 
660 660
 dnl
661
-dnl Check for dlopen -- first try libc then libdl.
662
-dnl
663
-if test "${WIN32}" != "yes" -a "${enable_plugins}" = "yes"; then
664
-	AC_CHECK_HEADER(
665
-		[dlfcn.h],
666
-		[AC_CHECK_FUNC(
667
-			[dlopen],
668
-			[AC_DEFINE(USE_LIBDL, 1, [Use libdl for dynamic library loading])],
669
-			[AC_CHECK_LIB(
670
-				[dl],
671
-				[dlopen],
672
-				[
673
-					LIBS="${LIBS} -ldl"
674
-					AC_DEFINE(USE_LIBDL, 1, [Use libdl for dynamic library loading])
675
-				],
676
-				[AC_MSG_RESULT([libdl library not found.])]
677
-			)],
678
-		)],
679
-	)
680
-	if test "${enable_eurephia}" = "yes"; then
681
-		AC_DEFINE([ENABLE_EUREPHIA], [1], [Enable support for the eurephia plug-in])
682
-	fi
683
-fi
684
-
685
-dnl
686
-dnl Check if LoadLibrary exists on Windows
687
-dnl
688
-if test "${WIN32}" = "yes"; then
689
-	AC_DEFINE(USE_LOAD_LIBRARY, 1, [Use LoadLibrary to load DLLs on Windows])
690
-fi
691
-
692
-dnl
693 661
 dnl check for LZO library
694 662
 dnl
695 663
 if test "${enable_lzo}" = "yes" && test "${enable_lzo_stub}" = "no"; then
... ...
@@ -898,6 +873,13 @@ test "${enable_strict_options}" = "yes" && AC_DEFINE([ENABLE_STRICT_OPTIONS_CHEC
898 898
 test "${enable_password_save}" = "yes" && AC_DEFINE([ENABLE_PASSWORD_SAVE], [1], [Allow --askpass and --auth-user-pass passwords to be read from a file])
899 899
 test "${enable_systemd}" = "yes" && AC_DEFINE([ENABLE_SYSTEMD], [1], [Enable systemd support])
900 900
 
901
+if test "${enable_plugins}" = "yes"; then
902
+	test "${WIN32}" != "yes" -a -z "${DL_LIBS}" && AC_MSG_ERROR([libdl is required for plugins])
903
+	OPTIONAL_DL_LIBS="${DL_LIBS}"
904
+	AC_DEFINE([ENABLE_PLUGIN], [1], [Enable systemd support])
905
+	test "${enable_eurephia}" = "yes" && AC_DEFINE([ENABLE_EUREPHIA], [1], [Enable support for the eurephia plug-in])
906
+fi
907
+
901 908
 if test "${enable_iproute2}" = "yes"; then
902 909
 	test -z "${IPROUTE}" && AC_MSG_ERROR([ip utility is required but missing])
903 910
 	AC_DEFINE([ENABLE_IPROUTE], [1], [enable iproute2 support])
... ...
@@ -933,6 +915,8 @@ AC_SUBST([TAP_WIN_COMPONENT_ID])
933 933
 AC_SUBST([TAP_WIN_MIN_MAJOR])
934 934
 AC_SUBST([TAP_WIN_MIN_MINOR])
935 935
 
936
+AC_SUBST([OPTIONAL_DL_LIBS])
937
+
936 938
 AM_CONDITIONAL([WIN32], [test "${WIN32}" = "yes"])
937 939
 
938 940
 AC_CONFIG_FILES([
... ...
@@ -95,7 +95,9 @@ openvpn_SOURCES = \
95 95
 	tun.c tun.h \
96 96
 	win32.h win32.c \
97 97
 	cryptoapi.h cryptoapi.c
98
+openvpn_LDADD = \
99
+	$(OPTIONAL_DL_LIBS)
98 100
 if WIN32
99 101
 openvpn_SOURCES += openvpn_win32_resources.rc
100
-openvpn_LDADD = -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lshell32
102
+openvpn_LDADD += -lgdi32 -lws2_32 -lwininet -lcrypt32 -liphlpapi -lwinmm -lshell32
101 103
 endif
... ...
@@ -26,6 +26,10 @@
26 26
 
27 27
 #ifdef ENABLE_PLUGIN
28 28
 
29
+#ifdef HAVE_DLFCN_H
30
+#include <dlfcn.h>
31
+#endif
32
+
29 33
 #include "buffer.h"
30 34
 #include "error.h"
31 35
 #include "misc.h"
... ...
@@ -160,7 +164,7 @@ plugin_option_list_print (const struct plugin_option_list *list, int msglevel)
160 160
 }
161 161
 #endif
162 162
 
163
-#if defined(USE_LIBDL)
163
+#ifndef WIN32
164 164
 
165 165
 static void
166 166
 libdl_resolve_symbol (void *handle, void **dest, const char *symbol, const char *plugin_name, const unsigned int flags)
... ...
@@ -170,7 +174,7 @@ libdl_resolve_symbol (void *handle, void **dest, const char *symbol, const char
170 170
     msg (M_FATAL, "PLUGIN: could not find required symbol '%s' in plugin shared object %s: %s", symbol, plugin_name, dlerror());
171 171
 }
172 172
 
173
-#elif defined(USE_LOAD_LIBRARY)
173
+#else
174 174
 
175 175
 static void
176 176
 dll_resolve_symbol (HMODULE module, void **dest, const char *symbol, const char *plugin_name, const unsigned int flags)
... ...
@@ -191,7 +195,7 @@ plugin_init_item (struct plugin *p, const struct plugin_option *o)
191 191
   p->so_pathname = o->so_pathname;
192 192
   p->plugin_type_mask = plugin_supported_types ();
193 193
 
194
-#if defined(USE_LIBDL)
194
+#ifndef WIN32
195 195
 
196 196
   p->handle = NULL;
197 197
 #if defined(PLUGIN_LIBDIR)
... ...
@@ -220,7 +224,7 @@ plugin_init_item (struct plugin *p, const struct plugin_option *o)
220 220
 
221 221
 # define PLUGIN_SYM(var, name, flags) libdl_resolve_symbol (p->handle, (void*)&p->var, name, p->so_pathname, flags)
222 222
 
223
-#elif defined(USE_LOAD_LIBRARY)
223
+#else
224 224
 
225 225
   rel = !absolute_pathname (p->so_pathname);
226 226
   p->module = LoadLibraryW (wide_string (p->so_pathname, &gc));
... ...
@@ -427,10 +431,10 @@ plugin_close_item (struct plugin *p)
427 427
       if (p->plugin_handle)
428 428
 	(*p->close)(p->plugin_handle);
429 429
 
430
-#if defined(USE_LIBDL)
430
+#ifndef WIN32
431 431
       if (dlclose (p->handle))
432 432
 	msg (M_WARN, "PLUGIN_CLOSE: dlclose() failed on plugin: %s", p->so_pathname);
433
-#elif defined(USE_LOAD_LIBRARY)
433
+#elif defined(WIN32)
434 434
       if (!FreeLibrary (p->module))
435 435
 	msg (M_WARN, "PLUGIN_CLOSE: FreeLibrary() failed on plugin: %s", p->so_pathname);
436 436
 #endif
... ...
@@ -59,9 +59,9 @@ struct plugin {
59 59
   unsigned int plugin_type_mask;
60 60
   int requested_initialization_point;
61 61
 
62
-#if defined(USE_LIBDL)
62
+#ifndef WIN32
63 63
   void *handle;
64
-#elif defined(USE_LOAD_LIBRARY)
64
+#else
65 65
   HMODULE module;
66 66
 #endif
67 67
 
... ...
@@ -156,10 +156,6 @@
156 156
 #include <grp.h>
157 157
 #endif
158 158
 
159
-#ifdef USE_LIBDL
160
-#include <dlfcn.h>
161
-#endif
162
-
163 159
 #ifdef HAVE_NETDB_H
164 160
 #include <netdb.h>
165 161
 #endif
... ...
@@ -507,13 +503,6 @@ socket_defined (const socket_descriptor_t sd)
507 507
 #endif
508 508
 
509 509
 /*
510
- * Do we have a plug-in capability?
511
- */
512
-#if defined(USE_LIBDL) || defined(USE_LOAD_LIBRARY)
513
-#define ENABLE_PLUGIN
514
-#endif
515
-
516
-/*
517 510
  * Enable deferred authentication?
518 511
  */
519 512
 #if defined(ENABLE_DEF_AUTH) && P2MP_SERVER && defined(ENABLE_PLUGIN)