[PATCH] Move the wrappers to the third_party directory

Andreas Schneider asn at samba.org
Tue Nov 7 15:50:31 UTC 2017


Hi Jeremy,

you requested to move the cwrap libraries to the third_party directory. 
Attached is a patchset to do that and also update socket_wrapper to version 
1.1.8.


Please review the patches and push if you're fine with them.


Thanks,


	Andreas
-------------- next part --------------
>From 09535298a8eecddfb277e5e5d9979509f4fe1595 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Tue, 7 Nov 2017 10:51:11 +0100
Subject: [PATCH 1/6] build: Move socket_wrapper to third_party

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 buildtools/wafsamba/samba_third_party.py             | 5 +++++
 {lib => third_party}/socket_wrapper/socket_wrapper.c | 4 ++--
 {lib => third_party}/socket_wrapper/wscript          | 4 ++--
 third_party/wscript                                  | 4 ++++
 wscript                                              | 8 ++++++--
 wscript_build                                        | 2 --
 6 files changed, 19 insertions(+), 8 deletions(-)
 rename {lib => third_party}/socket_wrapper/socket_wrapper.c (99%)
 rename {lib => third_party}/socket_wrapper/wscript (95%)

diff --git a/buildtools/wafsamba/samba_third_party.py b/buildtools/wafsamba/samba_third_party.py
index ac77be74f5a..170c101099a 100644
--- a/buildtools/wafsamba/samba_third_party.py
+++ b/buildtools/wafsamba/samba_third_party.py
@@ -39,3 +39,8 @@ def CHECK_CMOCKA(conf):
     return conf.CHECK_BUNDLED_SYSTEM_PKG('cmocka', minversion='1.1.1')
 
 Build.BuildContext.CHECK_CMOCKA = CHECK_CMOCKA
+
+ at conf
+def CHECK_SOCKET_WRAPPER(conf):
+    return conf.CHECK_BUNDLED_SYSTEM_PKG('socket_wrapper', minversion='1.1.7')
+Build.BuildContext.CHECK_SOCKET_WRAPPER = CHECK_SOCKET_WRAPPER
diff --git a/lib/socket_wrapper/socket_wrapper.c b/third_party/socket_wrapper/socket_wrapper.c
similarity index 99%
rename from lib/socket_wrapper/socket_wrapper.c
rename to third_party/socket_wrapper/socket_wrapper.c
index ba289e6fbaf..43b92f76f1e 100644
--- a/lib/socket_wrapper/socket_wrapper.c
+++ b/third_party/socket_wrapper/socket_wrapper.c
@@ -1233,7 +1233,7 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
 
 		if (addr == 0) {
 			/* 0.0.0.0 */
-		 	is_bcast = 0;
+			is_bcast = 0;
 			type = d_type;
 			iface = socket_wrapper_default_iface();
 		} else if (a_type && addr == 0xFFFFFFFF) {
@@ -2019,7 +2019,7 @@ static int swrap_pcap_get_fd(const char *fname)
 	if (fd != -1) {
 		struct swrap_file_hdr file_hdr;
 		file_hdr.magic		= 0xA1B2C3D4;
-		file_hdr.version_major	= 0x0002;	
+		file_hdr.version_major	= 0x0002;
 		file_hdr.version_minor	= 0x0004;
 		file_hdr.timezone	= 0x00000000;
 		file_hdr.sigfigs	= 0x00000000;
diff --git a/lib/socket_wrapper/wscript b/third_party/socket_wrapper/wscript
similarity index 95%
rename from lib/socket_wrapper/wscript
rename to third_party/socket_wrapper/wscript
index dbefa83ea33..514265b92b6 100644
--- a/lib/socket_wrapper/wscript
+++ b/third_party/socket_wrapper/wscript
@@ -5,7 +5,7 @@ import os
 VERSION="1.1.7"
 
 def configure(conf):
-    if conf.CHECK_BUNDLED_SYSTEM('socket_wrapper', minversion=VERSION, set_target=False):
+    if conf.CHECK_SOCKET_WRAPPER():
         conf.DEFINE('USING_SYSTEM_SOCKET_WRAPPER', 1)
         libsocket_wrapper_so_path = 'libsocket_wrapper.so'
     else:
@@ -97,7 +97,7 @@ def configure(conf):
 
         # Create full path to socket_wrapper
         blddir = os.path.realpath(conf.blddir)
-        libsocket_wrapper_so_path = blddir + '/default/lib/socket_wrapper/libsocket-wrapper.so'
+        libsocket_wrapper_so_path = blddir + '/default/third_party/socket_wrapper/libsocket-wrapper.so'
 
     conf.DEFINE('LIBSOCKET_WRAPPER_SO_PATH', libsocket_wrapper_so_path)
     conf.DEFINE('SOCKET_WRAPPER', 1)
diff --git a/third_party/wscript b/third_party/wscript
index 7c2923f611e..a5192eb5f1c 100644
--- a/third_party/wscript
+++ b/third_party/wscript
@@ -51,6 +51,8 @@ def configure(conf):
     conf.RECURSE('popt')
     conf.RECURSE('zlib')
     conf.RECURSE('aesni-intel')
+    if conf.CONFIG_GET('ENABLE_SELFTEST'):
+        conf.RECURSE('socket_wrapper')
 
 
 def build(bld):
@@ -75,3 +77,5 @@ def build(bld):
     bld.RECURSE('zlib')
     bld.RECURSE('popt')
     bld.RECURSE('aesni-intel')
+    if bld.CONFIG_GET('SOCKET_WRAPPER'):
+        bld.RECURSE('socket_wrapper')
diff --git a/wscript b/wscript
index 35cb9d1b08d..f7bdb1f3d39 100644
--- a/wscript
+++ b/wscript
@@ -135,6 +135,7 @@ def configure(conf):
         raise Utils.WafError('Python version 3.x is not supported by Samba yet')
 
     conf.RECURSE('dynconfig')
+    conf.RECURSE('selftest')
 
     if conf.CHECK_FOR_THIRD_PARTY():
         conf.RECURSE('third_party')
@@ -154,6 +155,11 @@ def configure(conf):
         else:
             conf.define('USING_SYSTEM_CMOCKA', 1)
 
+        if conf.CONFIG_GET('ENABLE_SELFTEST'):
+            if not conf.CHECK_SOCKET_WRAPPER():
+                raise Utils.WafError('socket_wrapper package has not been found.\nIf third_party is installed, check that it is in the proper place.')
+            else:
+                conf.define('USING_SYSTEM_SOCKET_WRAPPER', 1)
 
     conf.RECURSE('lib/ldb')
 
@@ -182,11 +188,9 @@ def configure(conf):
     conf.RECURSE('libcli/smbreadline')
     conf.RECURSE('lib/crypto')
     conf.RECURSE('pidl')
-    conf.RECURSE('selftest')
     if conf.CONFIG_GET('ENABLE_SELFTEST'):
         conf.RECURSE('lib/nss_wrapper')
         conf.RECURSE('lib/resolv_wrapper')
-        conf.RECURSE('lib/socket_wrapper')
         conf.RECURSE('lib/uid_wrapper')
         if Options.options.with_pam:
             conf.RECURSE('lib/pam_wrapper')
diff --git a/wscript_build b/wscript_build
index 8758b6d5789..45e30160b4c 100644
--- a/wscript_build
+++ b/wscript_build
@@ -71,8 +71,6 @@ if bld.CONFIG_GET('NSS_WRAPPER'):
     bld.RECURSE('lib/nss_wrapper')
 if bld.CONFIG_GET('PAM_WRAPPER') and Options.options.with_pam:
     bld.RECURSE('lib/pam_wrapper')
-if bld.CONFIG_GET('SOCKET_WRAPPER'):
-    bld.RECURSE('lib/socket_wrapper')
 if bld.CONFIG_GET('RESOLV_WRAPPER'):
     bld.RECURSE('lib/resolv_wrapper')
 if bld.CONFIG_GET('UID_WRAPPER'):
-- 
2.13.6


>From a909d572a218acc5e7f7af2973c0d60cb5a725b5 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Tue, 7 Nov 2017 11:40:11 +0100
Subject: [PATCH 2/6] build: Move nss_wrapper to third_party

---
 buildtools/wafsamba/samba_third_party.py        | 5 +++++
 selftest/target/Samba3.pm                       | 2 +-
 {lib => third_party}/nss_wrapper/nss_wrapper.c  | 0
 {lib => third_party}/nss_wrapper/nss_wrapper.pl | 0
 {lib => third_party}/nss_wrapper/wscript        | 4 ++--
 third_party/wscript                             | 3 +++
 wscript                                         | 6 +++++-
 wscript_build                                   | 2 --
 8 files changed, 16 insertions(+), 6 deletions(-)
 rename {lib => third_party}/nss_wrapper/nss_wrapper.c (100%)
 rename {lib => third_party}/nss_wrapper/nss_wrapper.pl (100%)
 rename {lib => third_party}/nss_wrapper/wscript (96%)

diff --git a/buildtools/wafsamba/samba_third_party.py b/buildtools/wafsamba/samba_third_party.py
index 170c101099a..917f8b92956 100644
--- a/buildtools/wafsamba/samba_third_party.py
+++ b/buildtools/wafsamba/samba_third_party.py
@@ -44,3 +44,8 @@ Build.BuildContext.CHECK_CMOCKA = CHECK_CMOCKA
 def CHECK_SOCKET_WRAPPER(conf):
     return conf.CHECK_BUNDLED_SYSTEM_PKG('socket_wrapper', minversion='1.1.7')
 Build.BuildContext.CHECK_SOCKET_WRAPPER = CHECK_SOCKET_WRAPPER
+
+ at conf
+def CHECK_NSS_WRAPPER(conf):
+    return conf.CHECK_BUNDLED_SYSTEM_PKG('nss_wrapper', minversion='1.1.3')
+Build.BuildContext.CHECK_NSS_WRAPPER = CHECK_NSS_WRAPPER
diff --git a/selftest/target/Samba3.pm b/selftest/target/Samba3.pm
index f4d033d0c7a..741d4d1ced4 100755
--- a/selftest/target/Samba3.pm
+++ b/selftest/target/Samba3.pm
@@ -1598,7 +1598,7 @@ sub provision($$$$$$$$$)
 	my $conffile="$libdir/server.conf";
 	my $dfqconffile="$libdir/dfq.conf";
 
-	my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/lib/nss_wrapper/nss_wrapper.pl";
+	my $nss_wrapper_pl = "$ENV{PERL} $self->{srcdir}/third_party/nss_wrapper/nss_wrapper.pl";
 	my $nss_wrapper_passwd = "$privatedir/passwd";
 	my $nss_wrapper_group = "$privatedir/group";
 	my $nss_wrapper_hosts = "$ENV{SELFTEST_PREFIX}/hosts";
diff --git a/lib/nss_wrapper/nss_wrapper.c b/third_party/nss_wrapper/nss_wrapper.c
similarity index 100%
rename from lib/nss_wrapper/nss_wrapper.c
rename to third_party/nss_wrapper/nss_wrapper.c
diff --git a/lib/nss_wrapper/nss_wrapper.pl b/third_party/nss_wrapper/nss_wrapper.pl
similarity index 100%
rename from lib/nss_wrapper/nss_wrapper.pl
rename to third_party/nss_wrapper/nss_wrapper.pl
diff --git a/lib/nss_wrapper/wscript b/third_party/nss_wrapper/wscript
similarity index 96%
rename from lib/nss_wrapper/wscript
rename to third_party/nss_wrapper/wscript
index f35e94e4068..90c6e71d6e9 100644
--- a/lib/nss_wrapper/wscript
+++ b/third_party/nss_wrapper/wscript
@@ -5,7 +5,7 @@ import os
 VERSION="1.1.3"
 
 def configure(conf):
-    if conf.CHECK_BUNDLED_SYSTEM('nss_wrapper', minversion=VERSION, set_target=False):
+    if conf.CHECK_NSS_WRAPPER():
         conf.DEFINE('USING_SYSTEM_NSS_WRAPPER', 1)
         libnss_wrapper_so_path = 'libnss_wrapper.so'
     else:
@@ -102,7 +102,7 @@ def configure(conf):
 
         # Create full path to nss_wrapper
         blddir = os.path.realpath(conf.blddir)
-        libnss_wrapper_so_path = blddir + '/default/lib/nss_wrapper/libnss-wrapper.so'
+        libnss_wrapper_so_path = blddir + '/default/third_party/nss_wrapper/libnss-wrapper.so'
 
     conf.DEFINE('LIBNSS_WRAPPER_SO_PATH', libnss_wrapper_so_path)
     conf.DEFINE('NSS_WRAPPER', 1)
diff --git a/third_party/wscript b/third_party/wscript
index a5192eb5f1c..9c6bccffb61 100644
--- a/third_party/wscript
+++ b/third_party/wscript
@@ -53,6 +53,7 @@ def configure(conf):
     conf.RECURSE('aesni-intel')
     if conf.CONFIG_GET('ENABLE_SELFTEST'):
         conf.RECURSE('socket_wrapper')
+        conf.RECURSE('nss_wrapper')
 
 
 def build(bld):
@@ -79,3 +80,5 @@ def build(bld):
     bld.RECURSE('aesni-intel')
     if bld.CONFIG_GET('SOCKET_WRAPPER'):
         bld.RECURSE('socket_wrapper')
+    if bld.CONFIG_GET('NSS_WRAPPER'):
+        bld.RECURSE('nss_wrapper')
diff --git a/wscript b/wscript
index f7bdb1f3d39..30cf0251738 100644
--- a/wscript
+++ b/wscript
@@ -161,6 +161,11 @@ def configure(conf):
             else:
                 conf.define('USING_SYSTEM_SOCKET_WRAPPER', 1)
 
+            if not conf.CHECK_NSS_WRAPPER():
+                raise Utils.WafError('nss_wrapper package has not been found.\nIf third_party is installed, check that it is in the proper place.')
+            else:
+                conf.define('USING_SYSTEM_NSS_WRAPPER', 1)
+
     conf.RECURSE('lib/ldb')
 
     if not (Options.options.without_ad_dc):
@@ -189,7 +194,6 @@ def configure(conf):
     conf.RECURSE('lib/crypto')
     conf.RECURSE('pidl')
     if conf.CONFIG_GET('ENABLE_SELFTEST'):
-        conf.RECURSE('lib/nss_wrapper')
         conf.RECURSE('lib/resolv_wrapper')
         conf.RECURSE('lib/uid_wrapper')
         if Options.options.with_pam:
diff --git a/wscript_build b/wscript_build
index 45e30160b4c..54532d88a29 100644
--- a/wscript_build
+++ b/wscript_build
@@ -67,8 +67,6 @@ bld.RECURSE('source4/lib/messaging')
 bld.RECURSE('source4/lib/events')
 bld.RECURSE('source4/lib/cmdline')
 bld.RECURSE('source4/lib/http')
-if bld.CONFIG_GET('NSS_WRAPPER'):
-    bld.RECURSE('lib/nss_wrapper')
 if bld.CONFIG_GET('PAM_WRAPPER') and Options.options.with_pam:
     bld.RECURSE('lib/pam_wrapper')
 if bld.CONFIG_GET('RESOLV_WRAPPER'):
-- 
2.13.6


>From 9e200a0050e1941cc23589bf23c48c2b09c88ce4 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Tue, 7 Nov 2017 11:55:04 +0100
Subject: [PATCH 3/6] build: Move resolv_wrapper to third_party

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 buildtools/wafsamba/samba_third_party.py             | 5 +++++
 {lib => third_party}/resolv_wrapper/resolv_wrapper.c | 0
 {lib => third_party}/resolv_wrapper/wscript          | 4 ++--
 third_party/wscript                                  | 3 +++
 wscript                                              | 6 +++++-
 wscript_build                                        | 2 --
 6 files changed, 15 insertions(+), 5 deletions(-)
 rename {lib => third_party}/resolv_wrapper/resolv_wrapper.c (100%)
 rename {lib => third_party}/resolv_wrapper/wscript (94%)

diff --git a/buildtools/wafsamba/samba_third_party.py b/buildtools/wafsamba/samba_third_party.py
index 917f8b92956..99336154def 100644
--- a/buildtools/wafsamba/samba_third_party.py
+++ b/buildtools/wafsamba/samba_third_party.py
@@ -49,3 +49,8 @@ Build.BuildContext.CHECK_SOCKET_WRAPPER = CHECK_SOCKET_WRAPPER
 def CHECK_NSS_WRAPPER(conf):
     return conf.CHECK_BUNDLED_SYSTEM_PKG('nss_wrapper', minversion='1.1.3')
 Build.BuildContext.CHECK_NSS_WRAPPER = CHECK_NSS_WRAPPER
+
+ at conf
+def CHECK_RESOLV_WRAPPER(conf):
+    return conf.CHECK_BUNDLED_SYSTEM_PKG('resolv_wrapper', minversion='1.1.4')
+Build.BuildContext.CHECK_RESOLV_WRAPPER = CHECK_RESOLV_WRAPPER
diff --git a/lib/resolv_wrapper/resolv_wrapper.c b/third_party/resolv_wrapper/resolv_wrapper.c
similarity index 100%
rename from lib/resolv_wrapper/resolv_wrapper.c
rename to third_party/resolv_wrapper/resolv_wrapper.c
diff --git a/lib/resolv_wrapper/wscript b/third_party/resolv_wrapper/wscript
similarity index 94%
rename from lib/resolv_wrapper/wscript
rename to third_party/resolv_wrapper/wscript
index 25371ed80be..be3e82be3ea 100644
--- a/lib/resolv_wrapper/wscript
+++ b/third_party/resolv_wrapper/wscript
@@ -5,7 +5,7 @@ import os
 VERSION="1.1.4"
 
 def configure(conf):
-    if conf.CHECK_BUNDLED_SYSTEM('resolv_wrapper', minversion=VERSION, set_target=False):
+    if conf.CHECK_RESOLV_WRAPPER():
         conf.DEFINE('USING_SYSTEM_RESOLV_WRAPPER', 1)
         libresolv_wrapper_so_path = 'libresolv_wrapper.so'
     else:
@@ -80,7 +80,7 @@ def configure(conf):
 
         # Create full path to resolv_wrapper
         blddir = os.path.realpath(conf.blddir)
-        libresolv_wrapper_so_path = blddir + '/default/lib/resolv_wrapper/libresolv-wrapper.so'
+        libresolv_wrapper_so_path = blddir + '/default/third_party/resolv_wrapper/libresolv-wrapper.so'
 
     conf.DEFINE('LIBRESOLV_WRAPPER_SO_PATH', libresolv_wrapper_so_path)
     conf.DEFINE('RESOLV_WRAPPER', 1)
diff --git a/third_party/wscript b/third_party/wscript
index 9c6bccffb61..a39c1ebdc10 100644
--- a/third_party/wscript
+++ b/third_party/wscript
@@ -54,6 +54,7 @@ def configure(conf):
     if conf.CONFIG_GET('ENABLE_SELFTEST'):
         conf.RECURSE('socket_wrapper')
         conf.RECURSE('nss_wrapper')
+        conf.RECURSE('resolv_wrapper')
 
 
 def build(bld):
@@ -82,3 +83,5 @@ def build(bld):
         bld.RECURSE('socket_wrapper')
     if bld.CONFIG_GET('NSS_WRAPPER'):
         bld.RECURSE('nss_wrapper')
+    if bld.CONFIG_GET('RESOLV_WRAPPER'):
+        bld.RECURSE('resolv_wrapper')
diff --git a/wscript b/wscript
index 30cf0251738..771555a1c04 100644
--- a/wscript
+++ b/wscript
@@ -166,6 +166,11 @@ def configure(conf):
             else:
                 conf.define('USING_SYSTEM_NSS_WRAPPER', 1)
 
+            if not conf.CHECK_RESOLV_WRAPPER():
+                raise Utils.WafError('resolv_wrapper package has not been found.\nIf third_party is installed, check that it is in the proper place.')
+            else:
+                conf.define('USING_SYSTEM_RESOLV_WRAPPER', 1)
+
     conf.RECURSE('lib/ldb')
 
     if not (Options.options.without_ad_dc):
@@ -194,7 +199,6 @@ def configure(conf):
     conf.RECURSE('lib/crypto')
     conf.RECURSE('pidl')
     if conf.CONFIG_GET('ENABLE_SELFTEST'):
-        conf.RECURSE('lib/resolv_wrapper')
         conf.RECURSE('lib/uid_wrapper')
         if Options.options.with_pam:
             conf.RECURSE('lib/pam_wrapper')
diff --git a/wscript_build b/wscript_build
index 54532d88a29..766d61a3132 100644
--- a/wscript_build
+++ b/wscript_build
@@ -69,8 +69,6 @@ bld.RECURSE('source4/lib/cmdline')
 bld.RECURSE('source4/lib/http')
 if bld.CONFIG_GET('PAM_WRAPPER') and Options.options.with_pam:
     bld.RECURSE('lib/pam_wrapper')
-if bld.CONFIG_GET('RESOLV_WRAPPER'):
-    bld.RECURSE('lib/resolv_wrapper')
 if bld.CONFIG_GET('UID_WRAPPER'):
     bld.RECURSE('lib/uid_wrapper')
 if bld.CHECK_FOR_THIRD_PARTY():
-- 
2.13.6


>From fe57a10824e3586323c8d8730981a7ed199ca784 Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Tue, 7 Nov 2017 12:02:19 +0100
Subject: [PATCH 4/6] build: Move uid_wrapper to third_party

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 buildtools/wafsamba/samba_third_party.py            | 5 +++++
 {lib => third_party}/pam_wrapper/libpamtest.c       | 0
 {lib => third_party}/pam_wrapper/libpamtest.h       | 0
 {lib => third_party}/pam_wrapper/pam_wrapper.c      | 2 +-
 {lib => third_party}/pam_wrapper/pwrap_compat.h     | 0
 {lib => third_party}/pam_wrapper/python/pypamtest.c | 0
 {lib => third_party}/pam_wrapper/wscript            | 0
 {lib => third_party}/uid_wrapper/uid_wrapper.c      | 0
 {lib => third_party}/uid_wrapper/wscript            | 5 ++---
 third_party/wscript                                 | 3 +++
 wscript                                             | 6 +++++-
 wscript_build                                       | 2 --
 12 files changed, 16 insertions(+), 7 deletions(-)
 rename {lib => third_party}/pam_wrapper/libpamtest.c (100%)
 rename {lib => third_party}/pam_wrapper/libpamtest.h (100%)
 rename {lib => third_party}/pam_wrapper/pam_wrapper.c (99%)
 rename {lib => third_party}/pam_wrapper/pwrap_compat.h (100%)
 rename {lib => third_party}/pam_wrapper/python/pypamtest.c (100%)
 rename {lib => third_party}/pam_wrapper/wscript (100%)
 rename {lib => third_party}/uid_wrapper/uid_wrapper.c (100%)
 rename {lib => third_party}/uid_wrapper/wscript (95%)

diff --git a/buildtools/wafsamba/samba_third_party.py b/buildtools/wafsamba/samba_third_party.py
index 99336154def..3253f948b8b 100644
--- a/buildtools/wafsamba/samba_third_party.py
+++ b/buildtools/wafsamba/samba_third_party.py
@@ -54,3 +54,8 @@ Build.BuildContext.CHECK_NSS_WRAPPER = CHECK_NSS_WRAPPER
 def CHECK_RESOLV_WRAPPER(conf):
     return conf.CHECK_BUNDLED_SYSTEM_PKG('resolv_wrapper', minversion='1.1.4')
 Build.BuildContext.CHECK_RESOLV_WRAPPER = CHECK_RESOLV_WRAPPER
+
+ at conf
+def CHECK_UID_WRAPPER(conf):
+    return conf.CHECK_BUNDLED_SYSTEM_PKG('uid_wrapper', minversion='1.2.4')
+Build.BuildContext.CHECK_UID_WRAPPER = CHECK_UID_WRAPPER
diff --git a/lib/pam_wrapper/libpamtest.c b/third_party/pam_wrapper/libpamtest.c
similarity index 100%
rename from lib/pam_wrapper/libpamtest.c
rename to third_party/pam_wrapper/libpamtest.c
diff --git a/lib/pam_wrapper/libpamtest.h b/third_party/pam_wrapper/libpamtest.h
similarity index 100%
rename from lib/pam_wrapper/libpamtest.h
rename to third_party/pam_wrapper/libpamtest.h
diff --git a/lib/pam_wrapper/pam_wrapper.c b/third_party/pam_wrapper/pam_wrapper.c
similarity index 99%
rename from lib/pam_wrapper/pam_wrapper.c
rename to third_party/pam_wrapper/pam_wrapper.c
index 4be81460a5b..716d3d8bd22 100644
--- a/lib/pam_wrapper/pam_wrapper.c
+++ b/third_party/pam_wrapper/pam_wrapper.c
@@ -1454,7 +1454,7 @@ int pam_prompt(pam_handle_t *pamh,
 			       args);
 	va_end(args);
 
-	return rv;  
+	return rv;
 }
 
 #ifdef HAVE_PAM_STRERROR_CONST
diff --git a/lib/pam_wrapper/pwrap_compat.h b/third_party/pam_wrapper/pwrap_compat.h
similarity index 100%
rename from lib/pam_wrapper/pwrap_compat.h
rename to third_party/pam_wrapper/pwrap_compat.h
diff --git a/lib/pam_wrapper/python/pypamtest.c b/third_party/pam_wrapper/python/pypamtest.c
similarity index 100%
rename from lib/pam_wrapper/python/pypamtest.c
rename to third_party/pam_wrapper/python/pypamtest.c
diff --git a/lib/pam_wrapper/wscript b/third_party/pam_wrapper/wscript
similarity index 100%
rename from lib/pam_wrapper/wscript
rename to third_party/pam_wrapper/wscript
diff --git a/lib/uid_wrapper/uid_wrapper.c b/third_party/uid_wrapper/uid_wrapper.c
similarity index 100%
rename from lib/uid_wrapper/uid_wrapper.c
rename to third_party/uid_wrapper/uid_wrapper.c
diff --git a/lib/uid_wrapper/wscript b/third_party/uid_wrapper/wscript
similarity index 95%
rename from lib/uid_wrapper/wscript
rename to third_party/uid_wrapper/wscript
index 9c1c0090770..6e1540f108d 100644
--- a/lib/uid_wrapper/wscript
+++ b/third_party/uid_wrapper/wscript
@@ -6,7 +6,7 @@ import os, sys
 VERSION="1.2.4"
 
 def configure(conf):
-    if conf.CHECK_BUNDLED_SYSTEM('uid_wrapper', minversion=VERSION, set_target=False):
+    if conf.CHECK_UID_WRAPPER():
         conf.DEFINE('USING_SYSTEM_UID_WRAPPER', 1)
         libuid_wrapper_so_path = 'libuid_wrapper.so'
     else:
@@ -113,7 +113,7 @@ syscall(SYS_setgroups32, 0, NULL);
 
         # Create full path to uid_wrapper
         blddir = os.path.realpath(conf.blddir)
-        libuid_wrapper_so_path = blddir + '/default/lib/uid_wrapper/libuid-wrapper.so'
+        libuid_wrapper_so_path = blddir + '/default/third_party/uid_wrapper/libuid-wrapper.so'
 
     conf.DEFINE('LIBUID_WRAPPER_SO_PATH', libuid_wrapper_so_path)
     conf.DEFINE('UID_WRAPPER', 1)
@@ -128,4 +128,3 @@ def build(bld):
                           deps='dl',
                           install=False,
                           realname='libuid-wrapper.so')
-
diff --git a/third_party/wscript b/third_party/wscript
index a39c1ebdc10..72f1daabcde 100644
--- a/third_party/wscript
+++ b/third_party/wscript
@@ -55,6 +55,7 @@ def configure(conf):
         conf.RECURSE('socket_wrapper')
         conf.RECURSE('nss_wrapper')
         conf.RECURSE('resolv_wrapper')
+        conf.RECURSE('uid_wrapper')
 
 
 def build(bld):
@@ -85,3 +86,5 @@ def build(bld):
         bld.RECURSE('nss_wrapper')
     if bld.CONFIG_GET('RESOLV_WRAPPER'):
         bld.RECURSE('resolv_wrapper')
+    if bld.CONFIG_GET('UID_WRAPPER'):
+        bld.RECURSE('uid_wrapper')
diff --git a/wscript b/wscript
index 771555a1c04..7dbaa90e773 100644
--- a/wscript
+++ b/wscript
@@ -171,6 +171,11 @@ def configure(conf):
             else:
                 conf.define('USING_SYSTEM_RESOLV_WRAPPER', 1)
 
+            if not conf.CHECK_UID_WRAPPER():
+                raise Utils.WafError('uid_wrapper package has not been found.\nIf third_party is installed, check that it is in the proper place.')
+            else:
+                conf.define('USING_SYSTEM_UID_WRAPPER', 1)
+
     conf.RECURSE('lib/ldb')
 
     if not (Options.options.without_ad_dc):
@@ -199,7 +204,6 @@ def configure(conf):
     conf.RECURSE('lib/crypto')
     conf.RECURSE('pidl')
     if conf.CONFIG_GET('ENABLE_SELFTEST'):
-        conf.RECURSE('lib/uid_wrapper')
         if Options.options.with_pam:
             conf.RECURSE('lib/pam_wrapper')
         if Options.options.with_ntvfs_fileserver != False:
diff --git a/wscript_build b/wscript_build
index 766d61a3132..c28fdd3f675 100644
--- a/wscript_build
+++ b/wscript_build
@@ -69,8 +69,6 @@ bld.RECURSE('source4/lib/cmdline')
 bld.RECURSE('source4/lib/http')
 if bld.CONFIG_GET('PAM_WRAPPER') and Options.options.with_pam:
     bld.RECURSE('lib/pam_wrapper')
-if bld.CONFIG_GET('UID_WRAPPER'):
-    bld.RECURSE('lib/uid_wrapper')
 if bld.CHECK_FOR_THIRD_PARTY():
     bld.RECURSE('third_party')
 bld.RECURSE('source4/lib/stream')
-- 
2.13.6


>From f767f5f4eda0cedd9f8e1cda2aa82014b6de499d Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Tue, 7 Nov 2017 12:08:44 +0100
Subject: [PATCH 5/6] build: Move pam_wrapper to third_party

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 buildtools/wafsamba/samba_third_party.py | 5 +++++
 third_party/pam_wrapper/wscript          | 4 ++--
 third_party/wscript                      | 5 +++++
 wscript                                  | 7 +++++--
 wscript_build                            | 2 --
 5 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/buildtools/wafsamba/samba_third_party.py b/buildtools/wafsamba/samba_third_party.py
index 3253f948b8b..9c894e4aed4 100644
--- a/buildtools/wafsamba/samba_third_party.py
+++ b/buildtools/wafsamba/samba_third_party.py
@@ -59,3 +59,8 @@ Build.BuildContext.CHECK_RESOLV_WRAPPER = CHECK_RESOLV_WRAPPER
 def CHECK_UID_WRAPPER(conf):
     return conf.CHECK_BUNDLED_SYSTEM_PKG('uid_wrapper', minversion='1.2.4')
 Build.BuildContext.CHECK_UID_WRAPPER = CHECK_UID_WRAPPER
+
+ at conf
+def CHECK_PAM_WRAPPER(conf):
+    return conf.CHECK_BUNDLED_SYSTEM_PKG('pam_wrapper', minversion='1.0.4')
+Build.BuildContext.CHECK_PAM_WRAPPER = CHECK_PAM_WRAPPER
diff --git a/third_party/pam_wrapper/wscript b/third_party/pam_wrapper/wscript
index cfd8b4f9baf..509c0a762f8 100644
--- a/third_party/pam_wrapper/wscript
+++ b/third_party/pam_wrapper/wscript
@@ -13,7 +13,7 @@ def find_library(library_names, lookup_paths):
     return ''
 
 def configure(conf):
-    if conf.CHECK_BUNDLED_SYSTEM('pam_wrapper', minversion=VERSION, set_target=False):
+    if conf.CHECK_PAM_WRAPPER():
         conf.DEFINE('USING_SYSTEM_PAM_WRAPPER', 1)
         libpam_wrapper_so_path = 'libpam_wrapper.so'
     else:
@@ -93,7 +93,7 @@ def configure(conf):
 
         # Create full path to pam_wrapper
         blddir = os.path.realpath(conf.blddir)
-        libpam_wrapper_so_path = blddir + '/default/lib/pam_wrapper/libpam-wrapper.so'
+        libpam_wrapper_so_path = blddir + '/default/third_party/pam_wrapper/libpam-wrapper.so'
 
     conf.DEFINE('LIBPAM_WRAPPER_SO_PATH', libpam_wrapper_so_path)
     conf.DEFINE('PAM_WRAPPER', 1)
diff --git a/third_party/wscript b/third_party/wscript
index 72f1daabcde..9a86dfe44e3 100644
--- a/third_party/wscript
+++ b/third_party/wscript
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 
 import samba_git
+import Options
 import Utils
 import os
 import sys
@@ -56,6 +57,8 @@ def configure(conf):
         conf.RECURSE('nss_wrapper')
         conf.RECURSE('resolv_wrapper')
         conf.RECURSE('uid_wrapper')
+        if Options.options.with_pam:
+            conf.RECURSE('pam_wrapper')
 
 
 def build(bld):
@@ -88,3 +91,5 @@ def build(bld):
         bld.RECURSE('resolv_wrapper')
     if bld.CONFIG_GET('UID_WRAPPER'):
         bld.RECURSE('uid_wrapper')
+    if bld.CONFIG_GET('PAM_WRAPPER'):
+        bld.RECURSE('pam_wrapper')
diff --git a/wscript b/wscript
index 7dbaa90e773..b1671022be1 100644
--- a/wscript
+++ b/wscript
@@ -176,6 +176,11 @@ def configure(conf):
             else:
                 conf.define('USING_SYSTEM_UID_WRAPPER', 1)
 
+            if not conf.CHECK_PAM_WRAPPER():
+                raise Utils.WafError('pam_wrapper package has not been found.\nIf third_party is installed, check that it is in the proper place.')
+            else:
+                conf.define('USING_SYSTEM_PAM_WRAPPER', 1)
+
     conf.RECURSE('lib/ldb')
 
     if not (Options.options.without_ad_dc):
@@ -204,8 +209,6 @@ def configure(conf):
     conf.RECURSE('lib/crypto')
     conf.RECURSE('pidl')
     if conf.CONFIG_GET('ENABLE_SELFTEST'):
-        if Options.options.with_pam:
-            conf.RECURSE('lib/pam_wrapper')
         if Options.options.with_ntvfs_fileserver != False:
             if not (Options.options.without_ad_dc):
                 conf.DEFINE('WITH_NTVFS_FILESERVER', 1)
diff --git a/wscript_build b/wscript_build
index c28fdd3f675..c663a015b5b 100644
--- a/wscript_build
+++ b/wscript_build
@@ -67,8 +67,6 @@ bld.RECURSE('source4/lib/messaging')
 bld.RECURSE('source4/lib/events')
 bld.RECURSE('source4/lib/cmdline')
 bld.RECURSE('source4/lib/http')
-if bld.CONFIG_GET('PAM_WRAPPER') and Options.options.with_pam:
-    bld.RECURSE('lib/pam_wrapper')
 if bld.CHECK_FOR_THIRD_PARTY():
     bld.RECURSE('third_party')
 bld.RECURSE('source4/lib/stream')
-- 
2.13.6


>From 98de3288ec380fc48e7c1565607da5513de21c4a Mon Sep 17 00:00:00 2001
From: Andreas Schneider <asn at samba.org>
Date: Tue, 7 Nov 2017 12:29:51 +0100
Subject: [PATCH 6/6] third_party: Update socket_wrapper to version 1.1.8

Signed-off-by: Andreas Schneider <asn at samba.org>
---
 buildtools/wafsamba/samba_third_party.py    |    2 +-
 third_party/socket_wrapper/socket_wrapper.c | 1221 +++++++++++++++++++--------
 third_party/socket_wrapper/wscript          |    2 +-
 3 files changed, 884 insertions(+), 341 deletions(-)

diff --git a/buildtools/wafsamba/samba_third_party.py b/buildtools/wafsamba/samba_third_party.py
index 9c894e4aed4..65c642ecf2e 100644
--- a/buildtools/wafsamba/samba_third_party.py
+++ b/buildtools/wafsamba/samba_third_party.py
@@ -42,7 +42,7 @@ Build.BuildContext.CHECK_CMOCKA = CHECK_CMOCKA
 
 @conf
 def CHECK_SOCKET_WRAPPER(conf):
-    return conf.CHECK_BUNDLED_SYSTEM_PKG('socket_wrapper', minversion='1.1.7')
+    return conf.CHECK_BUNDLED_SYSTEM_PKG('socket_wrapper', minversion='1.1.8')
 Build.BuildContext.CHECK_SOCKET_WRAPPER = CHECK_SOCKET_WRAPPER
 
 @conf
diff --git a/third_party/socket_wrapper/socket_wrapper.c b/third_party/socket_wrapper/socket_wrapper.c
index 43b92f76f1e..8da9d96687d 100644
--- a/third_party/socket_wrapper/socket_wrapper.c
+++ b/third_party/socket_wrapper/socket_wrapper.c
@@ -79,6 +79,7 @@
 #ifdef HAVE_RPC_RPC_H
 #include <rpc/rpc.h>
 #endif
+#include <pthread.h>
 
 enum swrap_dbglvl_e {
 	SWRAP_LOG_ERROR = 0,
@@ -94,12 +95,26 @@ enum swrap_dbglvl_e {
 #define PRINTF_ATTRIBUTE(a,b)
 #endif /* HAVE_FUNCTION_ATTRIBUTE_FORMAT */
 
+#ifdef HAVE_CONSTRUCTOR_ATTRIBUTE
+#define CONSTRUCTOR_ATTRIBUTE __attribute__ ((constructor))
+#else
+#define CONSTRUCTOR_ATTRIBUTE
+#endif /* HAVE_CONSTRUCTOR_ATTRIBUTE */
+
 #ifdef HAVE_DESTRUCTOR_ATTRIBUTE
 #define DESTRUCTOR_ATTRIBUTE __attribute__ ((destructor))
 #else
 #define DESTRUCTOR_ATTRIBUTE
 #endif
 
+#ifndef FALL_THROUGH
+# ifdef HAVE_FALLTHROUGH_ATTRIBUTE
+#  define FALL_THROUGH __attribute__ ((fallthrough))
+# else /* HAVE_FALLTHROUGH_ATTRIBUTE */
+#  define FALL_THROUGH
+# endif /* HAVE_FALLTHROUGH_ATTRIBUTE */
+#endif /* FALL_THROUGH */
+
 #ifdef HAVE_ADDRESS_SANITIZER_ATTRIBUTE
 #define DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE __attribute__((no_sanitize_address))
 #else
@@ -135,6 +150,8 @@ enum swrap_dbglvl_e {
 #define discard_const_p(type, ptr) ((type *)discard_const(ptr))
 #endif
 
+#define UNUSED(x) (void)(x)
+
 #ifdef IPV6_PKTINFO
 # ifndef IPV6_RECVPKTINFO
 #  define IPV6_RECVPKTINFO IPV6_PKTINFO
@@ -152,6 +169,22 @@ enum swrap_dbglvl_e {
 # endif
 #endif
 
+/* Macros for accessing mutexes */
+# define SWRAP_LOCK(m) do { \
+	pthread_mutex_lock(&(m ## _mutex)); \
+} while(0)
+
+# define SWRAP_UNLOCK(m) do { \
+	pthread_mutex_unlock(&(m ## _mutex)); \
+} while(0)
+
+/* Add new global locks here please */
+# define SWRAP_LOCK_ALL \
+	SWRAP_LOCK(libc_symbol_binding); \
+
+# define SWRAP_UNLOCK_ALL \
+	SWRAP_UNLOCK(libc_symbol_binding); \
+
 
 #define SWRAP_DLIST_ADD(list,item) do { \
 	if (!(list)) { \
@@ -184,6 +217,20 @@ enum swrap_dbglvl_e {
 	(item)->next	= NULL; \
 } while (0)
 
+#define SWRAP_DLIST_ADD_AFTER(list, item, el) \
+do { \
+	if ((list) == NULL || (el) == NULL) { \
+		SWRAP_DLIST_ADD(list, item); \
+	} else { \
+		(item)->prev = (el); \
+		(item)->next = (el)->next; \
+		(el)->next = (item); \
+		if ((item)->next != NULL) { \
+			(item)->next->prev = (item); \
+		} \
+	} \
+} while (0)
+
 #if defined(HAVE_GETTIMEOFDAY_TZ) || defined(HAVE_GETTIMEOFDAY_TZ_VOID)
 #define swrapGetTimeOfDay(tval) gettimeofday(tval,NULL)
 #else
@@ -212,10 +259,20 @@ enum swrap_dbglvl_e {
 
 #define SOCKET_MAX_SOCKETS 1024
 
+
+/*
+ * Maximum number of socket_info structures that can
+ * be used. Can be overriden by the environment variable
+ * SOCKET_WRAPPER_MAX_SOCKETS.
+ */
+#define SOCKET_WRAPPER_MAX_SOCKETS_DEFAULT 65535
+
+#define SOCKET_WRAPPER_MAX_SOCKETS_LIMIT 256000
+
 /* This limit is to avoid broadcast sendto() needing to stat too many
  * files.  It may be raised (with a performance cost) to up to 254
  * without changing the format above */
-#define MAX_WRAPPED_INTERFACES 40
+#define MAX_WRAPPED_INTERFACES 64
 
 struct swrap_address {
 	socklen_t sa_socklen;
@@ -233,11 +290,21 @@ struct swrap_address {
 struct socket_info_fd {
 	struct socket_info_fd *prev, *next;
 	int fd;
+
+	/*
+	 * Points to corresponding index in array of
+	 * socket_info structures
+	 */
+	int si_index;
 };
 
+int first_free;
+
 struct socket_info
 {
-	struct socket_info_fd *fds;
+	unsigned int refcount;
+
+	int next_free;
 
 	int family;
 	int type;
@@ -261,24 +328,27 @@ struct socket_info
 		unsigned long pck_snd;
 		unsigned long pck_rcv;
 	} io;
-
-	struct socket_info *prev, *next;
 };
 
+static struct socket_info *sockets;
+static size_t max_sockets = 0;
+
 /*
- * File descriptors are shared between threads so we should share socket
- * information too.
+ * While socket file descriptors are passed among different processes, the
+ * numerical value gets changed. So its better to store it locally to each
+ * process rather than including it within socket_info which will be shared.
  */
-struct socket_info *sockets;
+static struct socket_info_fd *socket_fds;
+
+/* The mutex for accessing the global libc.symbols */
+static pthread_mutex_t libc_symbol_binding_mutex = PTHREAD_MUTEX_INITIALIZER;
 
 /* Function prototypes */
 
 bool socket_wrapper_enabled(void);
-void swrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
 
-#ifdef NDEBUG
-# define SWRAP_LOG(...)
-#else
+void swrap_constructor(void) CONSTRUCTOR_ATTRIBUTE;
+void swrap_destructor(void) DESTRUCTOR_ATTRIBUTE;
 
 static void swrap_log(enum swrap_dbglvl_e dbglvl, const char *func, const char *format, ...) PRINTF_ATTRIBUTE(3, 4);
 # define SWRAP_LOG(dbglvl, ...) swrap_log((dbglvl), __func__, __VA_ARGS__)
@@ -291,42 +361,40 @@ static void swrap_log(enum swrap_dbglvl_e dbglvl,
 	va_list va;
 	const char *d;
 	unsigned int lvl = 0;
+	const char *prefix = "SWRAP";
 
 	d = getenv("SOCKET_WRAPPER_DEBUGLEVEL");
 	if (d != NULL) {
 		lvl = atoi(d);
 	}
 
+	if (lvl < dbglvl) {
+		return;
+	}
+
 	va_start(va, format);
 	vsnprintf(buffer, sizeof(buffer), format, va);
 	va_end(va);
 
-	if (lvl >= dbglvl) {
-		switch (dbglvl) {
-			case SWRAP_LOG_ERROR:
-				fprintf(stderr,
-					"SWRAP_ERROR(%d) - %s: %s\n",
-					(int)getpid(), func, buffer);
-				break;
-			case SWRAP_LOG_WARN:
-				fprintf(stderr,
-					"SWRAP_WARN(%d) - %s: %s\n",
-					(int)getpid(), func, buffer);
-				break;
-			case SWRAP_LOG_DEBUG:
-				fprintf(stderr,
-					"SWRAP_DEBUG(%d) - %s: %s\n",
-					(int)getpid(), func, buffer);
-				break;
-			case SWRAP_LOG_TRACE:
-				fprintf(stderr,
-					"SWRAP_TRACE(%d) - %s: %s\n",
-					(int)getpid(), func, buffer);
-				break;
-		}
+	switch (dbglvl) {
+		case SWRAP_LOG_ERROR:
+			prefix = "SWRAP_ERROR";
+			break;
+		case SWRAP_LOG_WARN:
+			prefix = "SWRAP_WARN";
+			break;
+		case SWRAP_LOG_DEBUG:
+			prefix = "SWRAP_DEBUG";
+			break;
+		case SWRAP_LOG_TRACE:
+			prefix = "SWRAP_TRACE";
+			break;
 	}
+
+	fprintf(stderr,
+		"%s(%d) - %s: %s\n",
+		prefix, (int)getpid(), func, buffer);
 }
-#endif
 
 /*********************************************************
  * SWRAP LOADING LIBC FUNCTIONS
@@ -334,91 +402,149 @@ static void swrap_log(enum swrap_dbglvl_e dbglvl,
 
 #include <dlfcn.h>
 
-struct swrap_libc_fns {
 #ifdef HAVE_ACCEPT4
-	int (*libc_accept4)(int sockfd,
-			   struct sockaddr *addr,
-			   socklen_t *addrlen,
-			   int flags);
+typedef int (*__libc_accept4)(int sockfd,
+			      struct sockaddr *addr,
+			      socklen_t *addrlen,
+			      int flags);
 #else
-	int (*libc_accept)(int sockfd,
-			   struct sockaddr *addr,
-			   socklen_t *addrlen);
+typedef int (*__libc_accept)(int sockfd,
+			     struct sockaddr *addr,
+			     socklen_t *addrlen);
+#endif
+typedef int (*__libc_bind)(int sockfd,
+			   const struct sockaddr *addr,
+			   socklen_t addrlen);
+typedef int (*__libc_close)(int fd);
+typedef int (*__libc_connect)(int sockfd,
+			      const struct sockaddr *addr,
+			      socklen_t addrlen);
+typedef int (*__libc_dup)(int fd);
+typedef int (*__libc_dup2)(int oldfd, int newfd);
+typedef int (*__libc_fcntl)(int fd, int cmd, ...);
+typedef FILE *(*__libc_fopen)(const char *name, const char *mode);
+#ifdef HAVE_FOPEN64
+typedef FILE *(*__libc_fopen64)(const char *name, const char *mode);
 #endif
-	int (*libc_bind)(int sockfd,
-			 const struct sockaddr *addr,
-			 socklen_t addrlen);
-	int (*libc_close)(int fd);
-	int (*libc_connect)(int sockfd,
-			    const struct sockaddr *addr,
-			    socklen_t addrlen);
-	int (*libc_dup)(int fd);
-	int (*libc_dup2)(int oldfd, int newfd);
-	int (*libc_fcntl)(int fd, int cmd, ...);
-	FILE *(*libc_fopen)(const char *name, const char *mode);
 #ifdef HAVE_EVENTFD
-	int (*libc_eventfd)(int count, int flags);
+typedef int (*__libc_eventfd)(int count, int flags);
 #endif
-	int (*libc_getpeername)(int sockfd,
-				struct sockaddr *addr,
-				socklen_t *addrlen);
-	int (*libc_getsockname)(int sockfd,
-				struct sockaddr *addr,
-				socklen_t *addrlen);
-	int (*libc_getsockopt)(int sockfd,
+typedef int (*__libc_getpeername)(int sockfd,
+				  struct sockaddr *addr,
+				  socklen_t *addrlen);
+typedef int (*__libc_getsockname)(int sockfd,
+				  struct sockaddr *addr,
+				  socklen_t *addrlen);
+typedef int (*__libc_getsockopt)(int sockfd,
 			       int level,
 			       int optname,
 			       void *optval,
 			       socklen_t *optlen);
-	int (*libc_ioctl)(int d, unsigned long int request, ...);
-	int (*libc_listen)(int sockfd, int backlog);
-	int (*libc_open)(const char *pathname, int flags, mode_t mode);
-	int (*libc_pipe)(int pipefd[2]);
-	int (*libc_read)(int fd, void *buf, size_t count);
-	ssize_t (*libc_readv)(int fd, const struct iovec *iov, int iovcnt);
-	int (*libc_recv)(int sockfd, void *buf, size_t len, int flags);
-	int (*libc_recvfrom)(int sockfd,
+typedef int (*__libc_ioctl)(int d, unsigned long int request, ...);
+typedef int (*__libc_listen)(int sockfd, int backlog);
+typedef int (*__libc_open)(const char *pathname, int flags, ...);
+#ifdef HAVE_OPEN64
+typedef int (*__libc_open64)(const char *pathname, int flags, ...);
+#endif /* HAVE_OPEN64 */
+typedef int (*__libc_openat)(int dirfd, const char *path, int flags, ...);
+typedef int (*__libc_pipe)(int pipefd[2]);
+typedef int (*__libc_read)(int fd, void *buf, size_t count);
+typedef ssize_t (*__libc_readv)(int fd, const struct iovec *iov, int iovcnt);
+typedef int (*__libc_recv)(int sockfd, void *buf, size_t len, int flags);
+typedef int (*__libc_recvfrom)(int sockfd,
 			     void *buf,
 			     size_t len,
 			     int flags,
 			     struct sockaddr *src_addr,
 			     socklen_t *addrlen);
-	int (*libc_recvmsg)(int sockfd, const struct msghdr *msg, int flags);
-	int (*libc_send)(int sockfd, const void *buf, size_t len, int flags);
-	int (*libc_sendmsg)(int sockfd, const struct msghdr *msg, int flags);
-	int (*libc_sendto)(int sockfd,
+typedef int (*__libc_recvmsg)(int sockfd, const struct msghdr *msg, int flags);
+typedef int (*__libc_send)(int sockfd, const void *buf, size_t len, int flags);
+typedef int (*__libc_sendmsg)(int sockfd, const struct msghdr *msg, int flags);
+typedef int (*__libc_sendto)(int sockfd,
 			   const void *buf,
 			   size_t len,
 			   int flags,
 			   const  struct sockaddr *dst_addr,
 			   socklen_t addrlen);
-	int (*libc_setsockopt)(int sockfd,
+typedef int (*__libc_setsockopt)(int sockfd,
 			       int level,
 			       int optname,
 			       const void *optval,
 			       socklen_t optlen);
 #ifdef HAVE_SIGNALFD
-	int (*libc_signalfd)(int fd, const sigset_t *mask, int flags);
+typedef int (*__libc_signalfd)(int fd, const sigset_t *mask, int flags);
 #endif
-	int (*libc_socket)(int domain, int type, int protocol);
-	int (*libc_socketpair)(int domain, int type, int protocol, int sv[2]);
+typedef int (*__libc_socket)(int domain, int type, int protocol);
+typedef int (*__libc_socketpair)(int domain, int type, int protocol, int sv[2]);
 #ifdef HAVE_TIMERFD_CREATE
-	int (*libc_timerfd_create)(int clockid, int flags);
+typedef int (*__libc_timerfd_create)(int clockid, int flags);
 #endif
-	ssize_t (*libc_write)(int fd, const void *buf, size_t count);
-	ssize_t (*libc_writev)(int fd, const struct iovec *iov, int iovcnt);
-};
-
-struct swrap {
-	void *libc_handle;
-	void *libsocket_handle;
+typedef ssize_t (*__libc_write)(int fd, const void *buf, size_t count);
+typedef ssize_t (*__libc_writev)(int fd, const struct iovec *iov, int iovcnt);
 
-	bool initialised;
-	bool enabled;
+#define SWRAP_SYMBOL_ENTRY(i) \
+	union { \
+		__libc_##i f; \
+		void *obj; \
+	} _libc_##i
 
-	char *socket_dir;
+struct swrap_libc_symbols {
+#ifdef HAVE_ACCEPT4
+	SWRAP_SYMBOL_ENTRY(accept4);
+#else
+	SWRAP_SYMBOL_ENTRY(accept);
+#endif
+	SWRAP_SYMBOL_ENTRY(bind);
+	SWRAP_SYMBOL_ENTRY(close);
+	SWRAP_SYMBOL_ENTRY(connect);
+	SWRAP_SYMBOL_ENTRY(dup);
+	SWRAP_SYMBOL_ENTRY(dup2);
+	SWRAP_SYMBOL_ENTRY(fcntl);
+	SWRAP_SYMBOL_ENTRY(fopen);
+#ifdef HAVE_FOPEN64
+	SWRAP_SYMBOL_ENTRY(fopen64);
+#endif
+#ifdef HAVE_EVENTFD
+	SWRAP_SYMBOL_ENTRY(eventfd);
+#endif
+	SWRAP_SYMBOL_ENTRY(getpeername);
+	SWRAP_SYMBOL_ENTRY(getsockname);
+	SWRAP_SYMBOL_ENTRY(getsockopt);
+	SWRAP_SYMBOL_ENTRY(ioctl);
+	SWRAP_SYMBOL_ENTRY(listen);
+	SWRAP_SYMBOL_ENTRY(open);
+#ifdef HAVE_OPEN64
+	SWRAP_SYMBOL_ENTRY(open64);
+#endif
+	SWRAP_SYMBOL_ENTRY(openat);
+	SWRAP_SYMBOL_ENTRY(pipe);
+	SWRAP_SYMBOL_ENTRY(read);
+	SWRAP_SYMBOL_ENTRY(readv);
+	SWRAP_SYMBOL_ENTRY(recv);
+	SWRAP_SYMBOL_ENTRY(recvfrom);
+	SWRAP_SYMBOL_ENTRY(recvmsg);
+	SWRAP_SYMBOL_ENTRY(send);
+	SWRAP_SYMBOL_ENTRY(sendmsg);
+	SWRAP_SYMBOL_ENTRY(sendto);
+	SWRAP_SYMBOL_ENTRY(setsockopt);
+#ifdef HAVE_SIGNALFD
+	SWRAP_SYMBOL_ENTRY(signalfd);
+#endif
+	SWRAP_SYMBOL_ENTRY(socket);
+	SWRAP_SYMBOL_ENTRY(socketpair);
+#ifdef HAVE_TIMERFD_CREATE
+	SWRAP_SYMBOL_ENTRY(timerfd_create);
+#endif
+	SWRAP_SYMBOL_ENTRY(write);
+	SWRAP_SYMBOL_ENTRY(writev);
+};
 
-	struct swrap_libc_fns fns;
+struct swrap {
+	struct {
+		void *handle;
+		void *socket_handle;
+		struct swrap_libc_symbols symbols;
+	} libc;
 };
 
 static struct swrap swrap;
@@ -434,7 +560,6 @@ enum swrap_lib {
     SWRAP_LIBSOCKET,
 };
 
-#ifndef NDEBUG
 static const char *swrap_str_lib(enum swrap_lib lib)
 {
 	switch (lib) {
@@ -449,7 +574,6 @@ static const char *swrap_str_lib(enum swrap_lib lib)
 	/* Compiler would warn us about unhandled enum value if we get here */
 	return "unknown";
 }
-#endif
 
 static void *swrap_load_lib_handle(enum swrap_lib lib)
 {
@@ -463,10 +587,10 @@ static void *swrap_load_lib_handle(enum swrap_lib lib)
 
 	switch (lib) {
 	case SWRAP_LIBNSL:
-		/* FALL TROUGH */
+		FALL_THROUGH;
 	case SWRAP_LIBSOCKET:
 #ifdef HAVE_LIBSOCKET
-		handle = swrap.libsocket_handle;
+		handle = swrap.libc.socket_handle;
 		if (handle == NULL) {
 			for (i = 10; i >= 0; i--) {
 				char soname[256] = {0};
@@ -478,18 +602,18 @@ static void *swrap_load_lib_handle(enum swrap_lib lib)
 				}
 			}
 
-			swrap.libsocket_handle = handle;
+			swrap.libc.socket_handle = handle;
 		}
 		break;
 #endif
-		/* FALL TROUGH */
+		FALL_THROUGH;
 	case SWRAP_LIBC:
-		handle = swrap.libc_handle;
+		handle = swrap.libc.handle;
 #ifdef LIBC_SO
 		if (handle == NULL) {
 			handle = dlopen(LIBC_SO, flags);
 
-			swrap.libc_handle = handle;
+			swrap.libc.handle = handle;
 		}
 #endif
 		if (handle == NULL) {
@@ -503,14 +627,14 @@ static void *swrap_load_lib_handle(enum swrap_lib lib)
 				}
 			}
 
-			swrap.libc_handle = handle;
+			swrap.libc.handle = handle;
 		}
 		break;
 	}
 
 	if (handle == NULL) {
 #ifdef RTLD_NEXT
-		handle = swrap.libc_handle = swrap.libsocket_handle = RTLD_NEXT;
+		handle = swrap.libc.handle = swrap.libc.socket_handle = RTLD_NEXT;
 #else
 		SWRAP_LOG(SWRAP_LOG_ERROR,
 			  "Failed to dlopen library: %s\n",
@@ -522,7 +646,7 @@ static void *swrap_load_lib_handle(enum swrap_lib lib)
 	return handle;
 }
 
-static void *_swrap_load_lib_function(enum swrap_lib lib, const char *fn_name)
+static void *_swrap_bind_symbol(enum swrap_lib lib, const char *fn_name)
 {
 	void *handle;
 	void *func;
@@ -532,24 +656,43 @@ static void *_swrap_load_lib_function(enum swrap_lib lib, const char *fn_name)
 	func = dlsym(handle, fn_name);
 	if (func == NULL) {
 		SWRAP_LOG(SWRAP_LOG_ERROR,
-				"Failed to find %s: %s\n",
-				fn_name, dlerror());
+			  "Failed to find %s: %s\n",
+			  fn_name,
+			  dlerror());
 		exit(-1);
 	}
 
 	SWRAP_LOG(SWRAP_LOG_TRACE,
-			"Loaded %s from %s",
-			fn_name, swrap_str_lib(lib));
+		  "Loaded %s from %s",
+		  fn_name,
+		  swrap_str_lib(lib));
+
 	return func;
 }
 
-#define swrap_load_lib_function(lib, fn_name) \
-	if (swrap.fns.libc_##fn_name == NULL) { \
-		void *swrap_cast_ptr = _swrap_load_lib_function(lib, #fn_name); \
-		*(void **) (&swrap.fns.libc_##fn_name) = \
-			swrap_cast_ptr; \
-	}
+#define swrap_bind_symbol_libc(sym_name) \
+	SWRAP_LOCK(libc_symbol_binding); \
+	if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
+		swrap.libc.symbols._libc_##sym_name.obj = \
+			_swrap_bind_symbol(SWRAP_LIBC, #sym_name); \
+	} \
+	SWRAP_UNLOCK(libc_symbol_binding)
 
+#define swrap_bind_symbol_libsocket(sym_name) \
+	SWRAP_LOCK(libc_symbol_binding); \
+	if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
+		swrap.libc.symbols._libc_##sym_name.obj = \
+			_swrap_bind_symbol(SWRAP_LIBSOCKET, #sym_name); \
+	} \
+	SWRAP_UNLOCK(libc_symbol_binding)
+
+#define swrap_bind_symbol_libnsl(sym_name) \
+	SWRAP_LOCK(libc_symbol_binding); \
+	if (swrap.libc.symbols._libc_##sym_name.obj == NULL) { \
+		swrap.libc.symbols._libc_##sym_name.obj = \
+			_swrap_bind_symbol(SWRAP_LIBNSL, #sym_name); \
+	} \
+	SWRAP_UNLOCK(libc_symbol_binding)
 
 /*
  * IMPORTANT
@@ -565,18 +708,18 @@ static int libc_accept4(int sockfd,
 			socklen_t *addrlen,
 			int flags)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, accept4);
+	swrap_bind_symbol_libsocket(accept4);
 
-	return swrap.fns.libc_accept4(sockfd, addr, addrlen, flags);
+	return swrap.libc.symbols._libc_accept4.f(sockfd, addr, addrlen, flags);
 }
 
 #else /* HAVE_ACCEPT4 */
 
 static int libc_accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, accept);
+	swrap_bind_symbol_libsocket(accept);
 
-	return swrap.fns.libc_accept(sockfd, addr, addrlen);
+	return swrap.libc.symbols._libc_accept.f(sockfd, addr, addrlen);
 }
 #endif /* HAVE_ACCEPT4 */
 
@@ -584,69 +727,61 @@ static int libc_bind(int sockfd,
 		     const struct sockaddr *addr,
 		     socklen_t addrlen)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, bind);
+	swrap_bind_symbol_libsocket(bind);
 
-	return swrap.fns.libc_bind(sockfd, addr, addrlen);
+	return swrap.libc.symbols._libc_bind.f(sockfd, addr, addrlen);
 }
 
 static int libc_close(int fd)
 {
-	swrap_load_lib_function(SWRAP_LIBC, close);
+	swrap_bind_symbol_libc(close);
 
-	return swrap.fns.libc_close(fd);
+	return swrap.libc.symbols._libc_close.f(fd);
 }
 
 static int libc_connect(int sockfd,
 			const struct sockaddr *addr,
 			socklen_t addrlen)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, connect);
+	swrap_bind_symbol_libsocket(connect);
 
-	return swrap.fns.libc_connect(sockfd, addr, addrlen);
+	return swrap.libc.symbols._libc_connect.f(sockfd, addr, addrlen);
 }
 
 static int libc_dup(int fd)
 {
-	swrap_load_lib_function(SWRAP_LIBC, dup);
+	swrap_bind_symbol_libc(dup);
 
-	return swrap.fns.libc_dup(fd);
+	return swrap.libc.symbols._libc_dup.f(fd);
 }
 
 static int libc_dup2(int oldfd, int newfd)
 {
-	swrap_load_lib_function(SWRAP_LIBC, dup2);
+	swrap_bind_symbol_libc(dup2);
 
-	return swrap.fns.libc_dup2(oldfd, newfd);
+	return swrap.libc.symbols._libc_dup2.f(oldfd, newfd);
 }
 
 #ifdef HAVE_EVENTFD
 static int libc_eventfd(int count, int flags)
 {
-	swrap_load_lib_function(SWRAP_LIBC, eventfd);
+	swrap_bind_symbol_libc(eventfd);
 
-	return swrap.fns.libc_eventfd(count, flags);
+	return swrap.libc.symbols._libc_eventfd.f(count, flags);
 }
 #endif
 
 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
 static int libc_vfcntl(int fd, int cmd, va_list ap)
 {
-	long int args[4];
+	void *arg;
 	int rc;
-	int i;
 
-	swrap_load_lib_function(SWRAP_LIBC, fcntl);
+	swrap_bind_symbol_libc(fcntl);
 
-	for (i = 0; i < 4; i++) {
-		args[i] = va_arg(ap, long int);
-	}
+	arg = va_arg(ap, void *);
 
-	rc = swrap.fns.libc_fcntl(fd,
-				  cmd,
-				  args[0],
-				  args[1],
-				  args[2],
-				  args[3]);
+	rc = swrap.libc.symbols._libc_fcntl.f(fd, cmd, arg);
 
 	return rc;
 }
@@ -655,18 +790,18 @@ static int libc_getpeername(int sockfd,
 			    struct sockaddr *addr,
 			    socklen_t *addrlen)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, getpeername);
+	swrap_bind_symbol_libsocket(getpeername);
 
-	return swrap.fns.libc_getpeername(sockfd, addr, addrlen);
+	return swrap.libc.symbols._libc_getpeername.f(sockfd, addr, addrlen);
 }
 
 static int libc_getsockname(int sockfd,
 			    struct sockaddr *addr,
 			    socklen_t *addrlen)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, getsockname);
+	swrap_bind_symbol_libsocket(getsockname);
 
-	return swrap.fns.libc_getsockname(sockfd, addr, addrlen);
+	return swrap.libc.symbols._libc_getsockname.f(sockfd, addr, addrlen);
 }
 
 static int libc_getsockopt(int sockfd,
@@ -675,58 +810,64 @@ static int libc_getsockopt(int sockfd,
 			   void *optval,
 			   socklen_t *optlen)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, getsockopt);
+	swrap_bind_symbol_libsocket(getsockopt);
 
-	return swrap.fns.libc_getsockopt(sockfd, level, optname, optval, optlen);
+	return swrap.libc.symbols._libc_getsockopt.f(sockfd,
+						     level,
+						     optname,
+						     optval,
+						     optlen);
 }
 
 DO_NOT_SANITIZE_ADDRESS_ATTRIBUTE
 static int libc_vioctl(int d, unsigned long int request, va_list ap)
 {
-	long int args[4];
+	void *arg;
 	int rc;
-	int i;
 
-	swrap_load_lib_function(SWRAP_LIBC, ioctl);
+	swrap_bind_symbol_libc(ioctl);
 
-	for (i = 0; i < 4; i++) {
-		args[i] = va_arg(ap, long int);
-	}
+	arg = va_arg(ap, void *);
 
-	rc = swrap.fns.libc_ioctl(d,
-				  request,
-				  args[0],
-				  args[1],
-				  args[2],
-				  args[3]);
+	rc = swrap.libc.symbols._libc_ioctl.f(d, request, arg);
 
 	return rc;
 }
 
 static int libc_listen(int sockfd, int backlog)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, listen);
+	swrap_bind_symbol_libsocket(listen);
 
-	return swrap.fns.libc_listen(sockfd, backlog);
+	return swrap.libc.symbols._libc_listen.f(sockfd, backlog);
 }
 
 static FILE *libc_fopen(const char *name, const char *mode)
 {
-	swrap_load_lib_function(SWRAP_LIBC, fopen);
+	swrap_bind_symbol_libc(fopen);
 
-	return swrap.fns.libc_fopen(name, mode);
+	return swrap.libc.symbols._libc_fopen.f(name, mode);
 }
 
+#ifdef HAVE_FOPEN64
+static FILE *libc_fopen64(const char *name, const char *mode)
+{
+	swrap_bind_symbol_libc(fopen64);
+
+	return swrap.libc.symbols._libc_fopen64.f(name, mode);
+}
+#endif /* HAVE_FOPEN64 */
+
 static int libc_vopen(const char *pathname, int flags, va_list ap)
 {
-	long int mode = 0;
+	int mode = 0;
 	int fd;
 
-	swrap_load_lib_function(SWRAP_LIBC, open);
+	swrap_bind_symbol_libc(open);
 
-	mode = va_arg(ap, long int);
-
-	fd = swrap.fns.libc_open(pathname, flags, (mode_t)mode);
+	if (flags & O_CREAT) {
+		mode = va_arg(ap, int);
+	}
+	fd = swrap.libc.symbols._libc_open.f(pathname, flags, (mode_t)mode);
 
 	return fd;
 }
@@ -743,32 +884,81 @@ static int libc_open(const char *pathname, int flags, ...)
 	return fd;
 }
 
+#ifdef HAVE_OPEN64
+static int libc_vopen64(const char *pathname, int flags, va_list ap)
+{
+	int mode = 0;
+	int fd;
+
+	swrap_bind_symbol_libc(open64);
+
+	if (flags & O_CREAT) {
+		mode = va_arg(ap, int);
+	}
+	fd = swrap.libc.symbols._libc_open64.f(pathname, flags, (mode_t)mode);
+
+	return fd;
+}
+#endif /* HAVE_OPEN64 */
+
+static int libc_vopenat(int dirfd, const char *path, int flags, va_list ap)
+{
+	int mode = 0;
+	int fd;
+
+	swrap_bind_symbol_libc(openat);
+
+	if (flags & O_CREAT) {
+		mode = va_arg(ap, int);
+	}
+	fd = swrap.libc.symbols._libc_openat.f(dirfd,
+					       path,
+					       flags,
+					       (mode_t)mode);
+
+	return fd;
+}
+
+#if 0
+static int libc_openat(int dirfd, const char *path, int flags, ...)
+{
+	va_list ap;
+	int fd;
+
+	va_start(ap, flags);
+	fd = libc_vopenat(dirfd, path, flags, ap);
+	va_end(ap);
+
+	return fd;
+}
+#endif
+
 static int libc_pipe(int pipefd[2])
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, pipe);
+	swrap_bind_symbol_libsocket(pipe);
 
-	return swrap.fns.libc_pipe(pipefd);
+	return swrap.libc.symbols._libc_pipe.f(pipefd);
 }
 
 static int libc_read(int fd, void *buf, size_t count)
 {
-	swrap_load_lib_function(SWRAP_LIBC, read);
+	swrap_bind_symbol_libc(read);
 
-	return swrap.fns.libc_read(fd, buf, count);
+	return swrap.libc.symbols._libc_read.f(fd, buf, count);
 }
 
 static ssize_t libc_readv(int fd, const struct iovec *iov, int iovcnt)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, readv);
+	swrap_bind_symbol_libsocket(readv);
 
-	return swrap.fns.libc_readv(fd, iov, iovcnt);
+	return swrap.libc.symbols._libc_readv.f(fd, iov, iovcnt);
 }
 
 static int libc_recv(int sockfd, void *buf, size_t len, int flags)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, recv);
+	swrap_bind_symbol_libsocket(recv);
 
-	return swrap.fns.libc_recv(sockfd, buf, len, flags);
+	return swrap.libc.symbols._libc_recv.f(sockfd, buf, len, flags);
 }
 
 static int libc_recvfrom(int sockfd,
@@ -778,30 +968,35 @@ static int libc_recvfrom(int sockfd,
 			 struct sockaddr *src_addr,
 			 socklen_t *addrlen)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, recvfrom);
+	swrap_bind_symbol_libsocket(recvfrom);
 
-	return swrap.fns.libc_recvfrom(sockfd, buf, len, flags, src_addr, addrlen);
+	return swrap.libc.symbols._libc_recvfrom.f(sockfd,
+						   buf,
+						   len,
+						   flags,
+						   src_addr,
+						   addrlen);
 }
 
 static int libc_recvmsg(int sockfd, struct msghdr *msg, int flags)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, recvmsg);
+	swrap_bind_symbol_libsocket(recvmsg);
 
-	return swrap.fns.libc_recvmsg(sockfd, msg, flags);
+	return swrap.libc.symbols._libc_recvmsg.f(sockfd, msg, flags);
 }
 
 static int libc_send(int sockfd, const void *buf, size_t len, int flags)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, send);
+	swrap_bind_symbol_libsocket(send);
 
-	return swrap.fns.libc_send(sockfd, buf, len, flags);
+	return swrap.libc.symbols._libc_send.f(sockfd, buf, len, flags);
 }
 
 static int libc_sendmsg(int sockfd, const struct msghdr *msg, int flags)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, sendmsg);
+	swrap_bind_symbol_libsocket(sendmsg);
 
-	return swrap.fns.libc_sendmsg(sockfd, msg, flags);
+	return swrap.libc.symbols._libc_sendmsg.f(sockfd, msg, flags);
 }
 
 static int libc_sendto(int sockfd,
@@ -811,9 +1006,14 @@ static int libc_sendto(int sockfd,
 		       const  struct sockaddr *dst_addr,
 		       socklen_t addrlen)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, sendto);
+	swrap_bind_symbol_libsocket(sendto);
 
-	return swrap.fns.libc_sendto(sockfd, buf, len, flags, dst_addr, addrlen);
+	return swrap.libc.symbols._libc_sendto.f(sockfd,
+						 buf,
+						 len,
+						 flags,
+						 dst_addr,
+						 addrlen);
 }
 
 static int libc_setsockopt(int sockfd,
@@ -822,55 +1022,59 @@ static int libc_setsockopt(int sockfd,
 			   const void *optval,
 			   socklen_t optlen)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, setsockopt);
+	swrap_bind_symbol_libsocket(setsockopt);
 
-	return swrap.fns.libc_setsockopt(sockfd, level, optname, optval, optlen);
+	return swrap.libc.symbols._libc_setsockopt.f(sockfd,
+						     level,
+						     optname,
+						     optval,
+						     optlen);
 }
 
 #ifdef HAVE_SIGNALFD
 static int libc_signalfd(int fd, const sigset_t *mask, int flags)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, signalfd);
+	swrap_bind_symbol_libsocket(signalfd);
 
-	return swrap.fns.libc_signalfd(fd, mask, flags);
+	return swrap.libc.symbols._libc_signalfd.f(fd, mask, flags);
 }
 #endif
 
 static int libc_socket(int domain, int type, int protocol)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, socket);
+	swrap_bind_symbol_libsocket(socket);
 
-	return swrap.fns.libc_socket(domain, type, protocol);
+	return swrap.libc.symbols._libc_socket.f(domain, type, protocol);
 }
 
 static int libc_socketpair(int domain, int type, int protocol, int sv[2])
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, socketpair);
+	swrap_bind_symbol_libsocket(socketpair);
 
-	return swrap.fns.libc_socketpair(domain, type, protocol, sv);
+	return swrap.libc.symbols._libc_socketpair.f(domain, type, protocol, sv);
 }
 
 #ifdef HAVE_TIMERFD_CREATE
 static int libc_timerfd_create(int clockid, int flags)
 {
-	swrap_load_lib_function(SWRAP_LIBC, timerfd_create);
+	swrap_bind_symbol_libc(timerfd_create);
 
-	return swrap.fns.libc_timerfd_create(clockid, flags);
+	return swrap.libc.symbols._libc_timerfd_create.f(clockid, flags);
 }
 #endif
 
 static ssize_t libc_write(int fd, const void *buf, size_t count)
 {
-	swrap_load_lib_function(SWRAP_LIBC, write);
+	swrap_bind_symbol_libc(write);
 
-	return swrap.fns.libc_write(fd, buf, count);
+	return swrap.libc.symbols._libc_write.f(fd, buf, count);
 }
 
 static ssize_t libc_writev(int fd, const struct iovec *iov, int iovcnt)
 {
-	swrap_load_lib_function(SWRAP_LIBSOCKET, writev);
+	swrap_bind_symbol_libsocket(writev);
 
-	return swrap.fns.libc_writev(fd, iov, iovcnt);
+	return swrap.libc.symbols._libc_writev.f(fd, iov, iovcnt);
 }
 
 /*********************************************************
@@ -975,11 +1179,78 @@ done:
 	return max_mtu;
 }
 
+static size_t socket_wrapper_max_sockets(void)
+{
+	const char *s;
+	unsigned long tmp;
+	char *endp;
+
+	if (max_sockets != 0) {
+		return max_sockets;
+	}
+
+	max_sockets = SOCKET_WRAPPER_MAX_SOCKETS_DEFAULT;
+
+	s = getenv("SOCKET_WRAPPER_MAX_SOCKETS");
+	if (s == NULL || s[0] == '\0') {
+		goto done;
+	}
+
+	tmp = strtoul(s, &endp, 10);
+	if (s == endp) {
+		goto done;
+	}
+	if (tmp == 0 || tmp > SOCKET_WRAPPER_MAX_SOCKETS_LIMIT) {
+		SWRAP_LOG(SWRAP_LOG_ERROR,
+			  "Invalid number of sockets specified, using default.");
+		goto done;
+	}
+
+	max_sockets = tmp;
+
+done:
+	return max_sockets;
+}
+
+static void socket_wrapper_init_sockets(void)
+{
+	size_t i;
+
+	if (sockets != NULL) {
+		return;
+	}
+
+	max_sockets = socket_wrapper_max_sockets();
+
+	sockets = (struct socket_info *)calloc(max_sockets,
+					       sizeof(struct socket_info));
+
+	if (sockets == NULL) {
+		SWRAP_LOG(SWRAP_LOG_ERROR,
+			  "Failed to allocate sockets array.\n");
+		exit(-1);
+	}
+
+	first_free = 0;
+
+	for (i = 0; i < max_sockets; i++) {
+		sockets[i].next_free = i+1;
+	}
+
+	sockets[max_sockets-1].next_free = -1;
+}
+
 bool socket_wrapper_enabled(void)
 {
 	const char *s = socket_wrapper_dir();
 
-	return s != NULL ? true : false;
+	if (s == NULL) {
+		return false;
+	}
+
+	socket_wrapper_init_sockets();
+
+	return true;
 }
 
 static unsigned int socket_wrapper_default_iface(void)
@@ -997,6 +1268,25 @@ static unsigned int socket_wrapper_default_iface(void)
 	return 1;/* 127.0.0.1 */
 }
 
+/*
+ * Return the first free entry (if any) and make
+ * it re-usable again (by nulling it out)
+ */
+static int socket_wrapper_first_free_index(void)
+{
+	int next_free;
+
+	if (first_free == -1) {
+		return -1;
+	}
+
+	next_free = sockets[first_free].next_free;
+	ZERO_STRUCT(sockets[first_free]);
+	sockets[first_free].next_free = next_free;
+
+	return first_free;
+}
+
 static int convert_un_in(const struct sockaddr_un *un, struct sockaddr *in, socklen_t *len)
 {
 	unsigned int iface;
@@ -1364,26 +1654,46 @@ static int convert_in_un_alloc(struct socket_info *si, const struct sockaddr *in
 	return 0;
 }
 
-static struct socket_info *find_socket_info(int fd)
+static struct socket_info_fd *find_socket_info_fd(int fd)
 {
-	struct socket_info *i;
+	struct socket_info_fd *f;
 
-	for (i = sockets; i; i = i->next) {
-		struct socket_info_fd *f;
-		for (f = i->fds; f; f = f->next) {
-			if (f->fd == fd) {
-				return i;
-			}
+	for (f = socket_fds; f; f = f->next) {
+		if (f->fd == fd) {
+			return f;
 		}
 	}
 
 	return NULL;
 }
 
+static int find_socket_info_index(int fd)
+{
+	struct socket_info_fd *fi = find_socket_info_fd(fd);
+
+	if (fi == NULL) {
+		return -1;
+	}
+
+	return fi->si_index;
+}
+
+static struct socket_info *find_socket_info(int fd)
+{
+	int idx = find_socket_info_index(fd);
+
+	if (idx == -1) {
+		return NULL;
+	}
+
+	return &sockets[idx];
+}
+
 #if 0 /* FIXME */
 static bool check_addr_port_in_use(const struct sockaddr *sa, socklen_t len)
 {
-	struct socket_info *s;
+	struct socket_info_fd *f;
+	const struct socket_info *last_s = NULL;
 
 	/* first catch invalid input */
 	switch (sa->sa_family) {
@@ -1404,7 +1714,14 @@ static bool check_addr_port_in_use(const struct sockaddr *sa, socklen_t len)
 		break;
 	}
 
-	for (s = sockets; s != NULL; s = s->next) {
+	for (f = socket_fds; f; f = f->next) {
+		struct socket_info *s = &sockets[f->si_index];
+
+		if (s == last_s) {
+			continue;
+		}
+		last_s = s;
+
 		if (s->myname == NULL) {
 			continue;
 		}
@@ -1466,27 +1783,33 @@ static bool check_addr_port_in_use(const struct sockaddr *sa, socklen_t len)
 
 static void swrap_remove_stale(int fd)
 {
-	struct socket_info *si = find_socket_info(fd);
-	struct socket_info_fd *fi;
+	struct socket_info_fd *fi = find_socket_info_fd(fd);
+	struct socket_info *si;
+	int si_index;
 
-	if (si != NULL) {
-		for (fi = si->fds; fi; fi = fi->next) {
-			if (fi->fd == fd) {
-				SWRAP_LOG(SWRAP_LOG_TRACE, "remove stale wrapper for %d", fd);
-				SWRAP_DLIST_REMOVE(si->fds, fi);
-				free(fi);
-				break;
-			}
-		}
+	if (fi == NULL) {
+		return;
+	}
 
-		if (si->fds == NULL) {
-			SWRAP_DLIST_REMOVE(sockets, si);
-			if (si->un_addr.sun_path[0] != '\0') {
-				unlink(si->un_addr.sun_path);
-			}
-			free(si);
-		}
+	si_index = fi->si_index;
+
+	SWRAP_LOG(SWRAP_LOG_TRACE, "remove stale wrapper for %d", fd);
+	SWRAP_DLIST_REMOVE(socket_fds, fi);
+	free(fi);
+
+	si = &sockets[si_index];
+	si->refcount--;
+
+	if (si->refcount > 0) {
+		return;
 	}
+
+	if (si->un_addr.sun_path[0] != '\0') {
+		unlink(si->un_addr.sun_path);
+	}
+
+	si->next_free = first_free;
+	first_free = si_index;
 }
 
 static int sockaddr_convert_to_un(struct socket_info *si,
@@ -1528,7 +1851,7 @@ static int sockaddr_convert_to_un(struct socket_info *si,
 		 * AF_UNSPEC is mapped to AF_INET and must be treated here.
 		 */
 
-		/* FALL THROUGH */
+		FALL_THROUGH;
 	}
 	case AF_INET:
 #ifdef HAVE_IPV6
@@ -2013,7 +2336,9 @@ static int swrap_pcap_get_fd(const char *fname)
 {
 	static int fd = -1;
 
-	if (fd != -1) return fd;
+	if (fd != -1) {
+		return fd;
+	}
 
 	fd = libc_open(fname, O_WRONLY|O_CREAT|O_EXCL|O_APPEND, 0644);
 	if (fd != -1) {
@@ -2066,7 +2391,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 
 	switch (type) {
 	case SWRAP_CONNECT_SEND:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		src_addr  = &si->myname.sa.s;
 		dest_addr = addr;
@@ -2080,7 +2407,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 		break;
 
 	case SWRAP_CONNECT_RECV:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		dest_addr = &si->myname.sa.s;
 		src_addr = addr;
@@ -2094,7 +2423,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 		break;
 
 	case SWRAP_CONNECT_UNREACH:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		dest_addr = &si->myname.sa.s;
 		src_addr  = addr;
@@ -2108,7 +2439,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 		break;
 
 	case SWRAP_CONNECT_ACK:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		src_addr  = &si->myname.sa.s;
 		dest_addr = addr;
@@ -2120,7 +2453,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 		break;
 
 	case SWRAP_ACCEPT_SEND:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		dest_addr = &si->myname.sa.s;
 		src_addr = addr;
@@ -2134,7 +2469,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 		break;
 
 	case SWRAP_ACCEPT_RECV:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		src_addr = &si->myname.sa.s;
 		dest_addr = addr;
@@ -2148,7 +2485,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 		break;
 
 	case SWRAP_ACCEPT_ACK:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		dest_addr = &si->myname.sa.s;
 		src_addr = addr;
@@ -2255,7 +2594,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 		break;
 
 	case SWRAP_CLOSE_SEND:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		src_addr  = &si->myname.sa.s;
 		dest_addr = &si->peername.sa.s;
@@ -2269,7 +2610,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 		break;
 
 	case SWRAP_CLOSE_RECV:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		dest_addr = &si->myname.sa.s;
 		src_addr  = &si->peername.sa.s;
@@ -2283,7 +2626,9 @@ static uint8_t *swrap_pcap_marshall_packet(struct socket_info *si,
 		break;
 
 	case SWRAP_CLOSE_ACK:
-		if (si->type != SOCK_STREAM) return NULL;
+		if (si->type != SOCK_STREAM) {
+			return NULL;
+		}
 
 		src_addr  = &si->myname.sa.s;
 		dest_addr = &si->peername.sa.s;
@@ -2380,6 +2725,7 @@ static int swrap_socket(int family, int type, int protocol)
 	struct socket_info *si;
 	struct socket_info_fd *fi;
 	int fd;
+	int idx;
 	int real_type = type;
 
 	/*
@@ -2434,12 +2780,12 @@ static int swrap_socket(int family, int type, int protocol)
 		if (real_type == SOCK_STREAM) {
 			break;
 		}
-		/*fall through*/
+		FALL_THROUGH;
 	case 17:
 		if (real_type == SOCK_DGRAM) {
 			break;
 		}
-		/*fall through*/
+		FALL_THROUGH;
 	default:
 		errno = EPROTONOSUPPORT;
 		return -1;
@@ -2456,17 +2802,16 @@ static int swrap_socket(int family, int type, int protocol)
 	}
 
 	/* Check if we have a stale fd and remove it */
-	si = find_socket_info(fd);
-	if (si != NULL) {
-		swrap_remove_stale(fd);
-	}
+	swrap_remove_stale(fd);
 
-	si = (struct socket_info *)calloc(1, sizeof(struct socket_info));
-	if (si == NULL) {
+	idx = socket_wrapper_first_free_index();
+	if (idx == -1) {
 		errno = ENOMEM;
 		return -1;
 	}
 
+	si = &sockets[idx];
+
 	si->family = family;
 
 	/* however, the rest of the socket_wrapper code expects just
@@ -2498,22 +2843,24 @@ static int swrap_socket(int family, int type, int protocol)
 		break;
 	}
 	default:
-		free(si);
 		errno = EINVAL;
 		return -1;
 	}
 
 	fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
 	if (fi == NULL) {
-		free(si);
 		errno = ENOMEM;
 		return -1;
 	}
 
+	si->refcount = 1;
+	first_free = si->next_free;
+	si->next_free = 0;
+
 	fi->fd = fd;
+	fi->si_index = idx;
 
-	SWRAP_DLIST_ADD(si->fds, fi);
-	SWRAP_DLIST_ADD(sockets, si);
+	SWRAP_DLIST_ADD(socket_fds, fi);
 
 	SWRAP_LOG(SWRAP_LOG_TRACE,
 		  "Created %s socket for protocol %s",
@@ -2607,6 +2954,7 @@ static int swrap_accept(int s,
 	struct socket_info *parent_si, *child_si;
 	struct socket_info_fd *child_fi;
 	int fd;
+	int idx;
 	struct swrap_address un_addr = {
 		.sa_socklen = sizeof(struct sockaddr_un),
 	};
@@ -2626,6 +2974,7 @@ static int swrap_accept(int s,
 #ifdef HAVE_ACCEPT4
 		return libc_accept4(s, addr, addrlen, flags);
 #else
+		UNUSED(flags);
 		return libc_accept(s, addr, addrlen);
 #endif
 	}
@@ -2643,6 +2992,7 @@ static int swrap_accept(int s,
 #ifdef HAVE_ACCEPT4
 	ret = libc_accept4(s, &un_addr.sa.s, &un_addr.sa_socklen, flags);
 #else
+	UNUSED(flags);
 	ret = libc_accept(s, &un_addr.sa.s, &un_addr.sa_socklen);
 #endif
 	if (ret == -1) {
@@ -2666,16 +3016,16 @@ static int swrap_accept(int s,
 		return ret;
 	}
 
-	child_si = (struct socket_info *)calloc(1, sizeof(struct socket_info));
-	if (child_si == NULL) {
-		close(fd);
+	idx = socket_wrapper_first_free_index();
+	if (idx == -1) {
 		errno = ENOMEM;
 		return -1;
 	}
 
+	child_si = &sockets[idx];
+
 	child_fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
 	if (child_fi == NULL) {
-		free(child_si);
 		close(fd);
 		errno = ENOMEM;
 		return -1;
@@ -2683,8 +3033,6 @@ static int swrap_accept(int s,
 
 	child_fi->fd = fd;
 
-	SWRAP_DLIST_ADD(child_si->fds, child_fi);
-
 	child_si->family = parent_si->family;
 	child_si->type = parent_si->type;
 	child_si->protocol = parent_si->protocol;
@@ -2710,7 +3058,6 @@ static int swrap_accept(int s,
 			       &un_my_addr.sa_socklen);
 	if (ret == -1) {
 		free(child_fi);
-		free(child_si);
 		close(fd);
 		return ret;
 	}
@@ -2723,7 +3070,6 @@ static int swrap_accept(int s,
 				       &in_my_addr.sa_socklen);
 	if (ret == -1) {
 		free(child_fi);
-		free(child_si);
 		close(fd);
 		return ret;
 	}
@@ -2737,7 +3083,13 @@ static int swrap_accept(int s,
 	};
 	memcpy(&child_si->myname.sa.ss, &in_my_addr.sa.ss, in_my_addr.sa_socklen);
 
-	SWRAP_DLIST_ADD(sockets, child_si);
+	child_si->refcount = 1;
+	first_free = child_si->next_free;
+	child_si->next_free = 0;
+
+	child_fi->si_index = idx;
+
+	SWRAP_DLIST_ADD(socket_fds, child_fi);
 
 	if (addr != NULL) {
 		swrap_pcap_dump_packet(child_si, addr, SWRAP_ACCEPT_SEND, NULL, 0);
@@ -2805,8 +3157,9 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 			type = SOCKET_TYPE_CHAR_UDP;
 			break;
 		default:
-		    errno = ESOCKTNOSUPPORT;
-		    return -1;
+			errno = ESOCKTNOSUPPORT;
+			ret = -1;
+			goto done;
 		}
 
 		memset(&in, 0, sizeof(in));
@@ -2826,7 +3179,8 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 
 		if (si->family != family) {
 			errno = ENETUNREACH;
-			return -1;
+			ret = -1;
+			goto done;
 		}
 
 		switch (si->type) {
@@ -2838,7 +3192,8 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 			break;
 		default:
 			errno = ESOCKTNOSUPPORT;
-			return -1;
+			ret = -1;
+			goto done;
 		}
 
 		memset(&in6, 0, sizeof(in6));
@@ -2855,7 +3210,8 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 #endif
 	default:
 		errno = ESOCKTNOSUPPORT;
-		return -1;
+		ret = -1;
+		goto done;
 	}
 
 	if (autobind_start > 60000) {
@@ -2870,7 +3226,9 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 		if (stat(un_addr.sa.un.sun_path, &st) == 0) continue;
 
 		ret = libc_bind(fd, &un_addr.sa.s, un_addr.sa_socklen);
-		if (ret == -1) return ret;
+		if (ret == -1) {
+			goto done;
+		}
 
 		si->un_addr = un_addr.sa.un;
 
@@ -2886,13 +3244,17 @@ static int swrap_auto_bind(int fd, struct socket_info *si, int family)
 					   socket_wrapper_default_iface(),
 					   0);
 		errno = ENFILE;
-		return -1;
+		ret = -1;
+		goto done;
 	}
 
 	si->family = family;
 	set_port(si->family, port, &si->myname);
 
-	return 0;
+	ret = 0;
+
+done:
+	return ret;
 }
 
 /****************************************************************************
@@ -2915,21 +3277,27 @@ static int swrap_connect(int s, const struct sockaddr *serv_addr,
 
 	if (si->bound == 0) {
 		ret = swrap_auto_bind(s, si, serv_addr->sa_family);
-		if (ret == -1) return -1;
+		if (ret == -1) {
+			goto done;
+		}
 	}
 
 	if (si->family != serv_addr->sa_family) {
 		errno = EINVAL;
-		return -1;
+		ret = -1;
+		goto done;
 	}
 
 	ret = sockaddr_convert_to_un(si, serv_addr,
 				     addrlen, &un_addr.sa.un, 0, &bcast);
-	if (ret == -1) return -1;
+	if (ret == -1) {
+		goto done;
+	}
 
 	if (bcast) {
 		errno = ENETUNREACH;
-		return -1;
+		ret = -1;
+		goto done;
 	}
 
 	if (si->type == SOCK_DGRAM) {
@@ -2989,6 +3357,7 @@ static int swrap_connect(int s, const struct sockaddr *serv_addr,
 		swrap_pcap_dump_packet(si, serv_addr, SWRAP_CONNECT_UNREACH, NULL, 0);
 	}
 
+done:
 	return ret;
 }
 
@@ -3084,7 +3453,9 @@ static int swrap_bind(int s, const struct sockaddr *myaddr, socklen_t addrlen)
 				     &un_addr.sa.un,
 				     1,
 				     &si->bcast);
-	if (ret == -1) return -1;
+	if (ret == -1) {
+		return -1;
+	}
 
 	unlink(un_addr.sa.un.sun_path);
 
@@ -3243,6 +3614,31 @@ FILE *fopen(const char *name, const char *mode)
 }
 
 /****************************************************************************
+ *   FOPEN64
+ ***************************************************************************/
+
+#ifdef HAVE_FOPEN64
+static FILE *swrap_fopen64(const char *name, const char *mode)
+{
+	FILE *fp;
+
+	fp = libc_fopen64(name, mode);
+	if (fp != NULL) {
+		int fd = fileno(fp);
+
+		swrap_remove_stale(fd);
+	}
+
+	return fp;
+}
+
+FILE *fopen64(const char *name, const char *mode)
+{
+	return swrap_fopen64(name, mode);
+}
+#endif /* HAVE_FOPEN64 */
+
+/****************************************************************************
  *   OPEN
  ***************************************************************************/
 
@@ -3276,6 +3672,75 @@ int open(const char *pathname, int flags, ...)
 }
 
 /****************************************************************************
+ *   OPEN64
+ ***************************************************************************/
+
+#ifdef HAVE_OPEN64
+static int swrap_vopen64(const char *pathname, int flags, va_list ap)
+{
+	int ret;
+
+	ret = libc_vopen64(pathname, flags, ap);
+	if (ret != -1) {
+		/*
+		 * There are methods for closing descriptors (libc-internal code
+		 * paths, direct syscalls) which close descriptors in ways that
+		 * we can't intercept, so try to recover when we notice that
+		 * that's happened
+		 */
+		swrap_remove_stale(ret);
+	}
+	return ret;
+}
+
+int open64(const char *pathname, int flags, ...)
+{
+	va_list ap;
+	int fd;
+
+	va_start(ap, flags);
+	fd = swrap_vopen64(pathname, flags, ap);
+	va_end(ap);
+
+	return fd;
+}
+#endif /* HAVE_OPEN64 */
+
+/****************************************************************************
+ *   OPENAT
+ ***************************************************************************/
+
+static int swrap_vopenat(int dirfd, const char *path, int flags, va_list ap)
+{
+	int ret;
+
+	ret = libc_vopenat(dirfd, path, flags, ap);
+	if (ret != -1) {
+		/*
+		 * There are methods for closing descriptors (libc-internal code
+		 * paths, direct syscalls) which close descriptors in ways that
+		 * we can't intercept, so try to recover when we notice that
+		 * that's happened
+		 */
+		swrap_remove_stale(ret);
+	}
+
+	return ret;
+}
+
+int openat(int dirfd, const char *path, int flags, ...)
+{
+	va_list ap;
+	int fd;
+
+	va_start(ap, flags);
+	fd = swrap_vopenat(dirfd, path, flags, ap);
+	va_end(ap);
+
+	return fd;
+}
+
+/****************************************************************************
  *   GETPEERNAME
  ***************************************************************************/
 
@@ -3361,6 +3826,7 @@ static int swrap_getsockopt(int s, int level, int optname,
 			    void *optval, socklen_t *optlen)
 {
 	struct socket_info *si = find_socket_info(s);
+	int ret;
 
 	if (!si) {
 		return libc_getsockopt(s,
@@ -3377,12 +3843,14 @@ static int swrap_getsockopt(int s, int level, int optname,
 			if (optval == NULL || optlen == NULL ||
 			    *optlen < (socklen_t)sizeof(int)) {
 				errno = EINVAL;
-				return -1;
+				ret = -1;
+				goto done;
 			}
 
 			*optlen = sizeof(int);
 			*(int *)optval = si->family;
-			return 0;
+			ret = 0;
+			goto done;
 #endif /* SO_DOMAIN */
 
 #ifdef SO_PROTOCOL
@@ -3390,29 +3858,34 @@ static int swrap_getsockopt(int s, int level, int optname,
 			if (optval == NULL || optlen == NULL ||
 			    *optlen < (socklen_t)sizeof(int)) {
 				errno = EINVAL;
-				return -1;
+				ret = -1;
+				goto done;
 			}
 
 			*optlen = sizeof(int);
 			*(int *)optval = si->protocol;
-			return 0;
+			ret = 0;
+			goto done;
 #endif /* SO_PROTOCOL */
 		case SO_TYPE:
 			if (optval == NULL || optlen == NULL ||
 			    *optlen < (socklen_t)sizeof(int)) {
 				errno = EINVAL;
-				return -1;
+				ret = -1;
+				goto done;
 			}
 
 			*optlen = sizeof(int);
 			*(int *)optval = si->type;
-			return 0;
+			ret = 0;
+			goto done;
 		default:
-			return libc_getsockopt(s,
-					       level,
-					       optname,
-					       optval,
-					       optlen);
+			ret = libc_getsockopt(s,
+					      level,
+					      optname,
+					      optval,
+					      optlen);
+			goto done;
 		}
 	} else if (level == IPPROTO_TCP) {
 		switch (optname) {
@@ -3426,13 +3899,15 @@ static int swrap_getsockopt(int s, int level, int optname,
 			if (optval == NULL || optlen == NULL ||
 			    *optlen < (socklen_t)sizeof(int)) {
 				errno = EINVAL;
-				return -1;
+				ret = -1;
+				goto done;
 			}
 
 			*optlen = sizeof(int);
 			*(int *)optval = si->tcp_nodelay;
 
-			return 0;
+			ret = 0;
+			goto done;
 #endif /* TCP_NODELAY */
 		default:
 			break;
@@ -3440,7 +3915,10 @@ static int swrap_getsockopt(int s, int level, int optname,
 	}
 
 	errno = ENOPROTOOPT;
-	return -1;
+	ret = -1;
+
+done:
+	return ret;
 }
 
 #ifdef HAVE_ACCEPT_PSOCKLEN_T
@@ -3460,6 +3938,7 @@ static int swrap_setsockopt(int s, int level, int optname,
 			    const void *optval, socklen_t optlen)
 {
 	struct socket_info *si = find_socket_info(s);
+	int ret;
 
 	if (!si) {
 		return libc_setsockopt(s,
@@ -3488,17 +3967,20 @@ static int swrap_setsockopt(int s, int level, int optname,
 			if (optval == NULL || optlen == 0 ||
 			    optlen < (socklen_t)sizeof(int)) {
 				errno = EINVAL;
-				return -1;
+				ret = -1;
+				goto done;
 			}
 
 			i = *discard_const_p(int, optval);
 			if (i != 0 && i != 1) {
 				errno = EINVAL;
-				return -1;
+				ret = -1;
+				goto done;
 			}
 			si->tcp_nodelay = i;
 
-			return 0;
+			ret = 0;
+			goto done;
 		}
 #endif /* TCP_NODELAY */
 		default:
@@ -3515,7 +3997,8 @@ static int swrap_setsockopt(int s, int level, int optname,
 			}
 #endif /* IP_PKTINFO */
 		}
-		return 0;
+		ret = 0;
+		goto done;
 #ifdef HAVE_IPV6
 	case AF_INET6:
 		if (level == IPPROTO_IPV6) {
@@ -3525,12 +4008,17 @@ static int swrap_setsockopt(int s, int level, int optname,
 			}
 #endif /* IPV6_PKTINFO */
 		}
-		return 0;
+		ret = 0;
+		goto done;
 #endif
 	default:
 		errno = ENOPROTOOPT;
-		return -1;
+		ret = -1;
+		goto done;
 	}
+
+done:
+	return ret;
 }
 
 int setsockopt(int s, int level, int optname,
@@ -3944,7 +4432,9 @@ static ssize_t swrap_sendmsg_before(int fd,
 
 			ret = sockaddr_convert_to_un(si, msg_name, msg->msg_namelen,
 						     tmp_un, 0, bcast);
-			if (ret == -1) return -1;
+			if (ret == -1) {
+				return -1;
+			}
 
 			if (to_un) {
 				*to_un = tmp_un;
@@ -3979,7 +4469,9 @@ static ssize_t swrap_sendmsg_before(int fd,
 					     tmp_un,
 					     0,
 					     NULL);
-		if (ret == -1) return -1;
+		if (ret == -1) {
+			return -1;
+		}
 
 		ret = libc_connect(fd,
 				   (struct sockaddr *)(void *)tmp_un,
@@ -5085,35 +5577,34 @@ ssize_t writev(int s, const struct iovec *vector, int count)
 
 static int swrap_close(int fd)
 {
-	struct socket_info *si = find_socket_info(fd);
-	struct socket_info_fd *fi;
+	struct socket_info_fd *fi = find_socket_info_fd(fd);
+	struct socket_info *si = NULL;
+	int si_index;
 	int ret;
 
-	if (!si) {
+	if (fi == NULL) {
 		return libc_close(fd);
 	}
 
-	for (fi = si->fds; fi; fi = fi->next) {
-		if (fi->fd == fd) {
-			SWRAP_DLIST_REMOVE(si->fds, fi);
-			free(fi);
-			break;
-		}
-	}
+	si_index = fi->si_index;
+
+	SWRAP_DLIST_REMOVE(socket_fds, fi);
+	free(fi);
 
-	if (si->fds) {
+	ret = libc_close(fd);
+
+	si = &sockets[si_index];
+	si->refcount--;
+
+	if (si->refcount > 0) {
 		/* there are still references left */
-		return libc_close(fd);
+		return ret;
 	}
 
-	SWRAP_DLIST_REMOVE(sockets, si);
-
 	if (si->myname.sa_socklen > 0 && si->peername.sa_socklen > 0) {
 		swrap_pcap_dump_packet(si, NULL, SWRAP_CLOSE_SEND, NULL, 0);
 	}
 
-	ret = libc_close(fd);
-
 	if (si->myname.sa_socklen > 0 && si->peername.sa_socklen > 0) {
 		swrap_pcap_dump_packet(si, NULL, SWRAP_CLOSE_RECV, NULL, 0);
 		swrap_pcap_dump_packet(si, NULL, SWRAP_CLOSE_ACK, NULL, 0);
@@ -5122,7 +5613,9 @@ static int swrap_close(int fd)
 	if (si->un_addr.sun_path[0] != '\0') {
 		unlink(si->un_addr.sun_path);
 	}
-	free(si);
+
+	si->next_free = first_free;
+	first_free = si_index;
 
 	return ret;
 }
@@ -5139,14 +5632,15 @@ int close(int fd)
 static int swrap_dup(int fd)
 {
 	struct socket_info *si;
-	struct socket_info_fd *fi;
-
-	si = find_socket_info(fd);
+	struct socket_info_fd *src_fi, *fi;
 
-	if (!si) {
+	src_fi = find_socket_info_fd(fd);
+	if (src_fi == NULL) {
 		return libc_dup(fd);
 	}
 
+	si = &sockets[src_fi->si_index];
+
 	fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
 	if (fi == NULL) {
 		errno = ENOMEM;
@@ -5161,10 +5655,13 @@ static int swrap_dup(int fd)
 		return -1;
 	}
 
+	si->refcount++;
+	fi->si_index = src_fi->si_index;
+
 	/* Make sure we don't have an entry for the fd */
 	swrap_remove_stale(fi->fd);
 
-	SWRAP_DLIST_ADD(si->fds, fi);
+	SWRAP_DLIST_ADD_AFTER(socket_fds, fi, src_fi);
 	return fi->fd;
 }
 
@@ -5180,14 +5677,25 @@ int dup(int fd)
 static int swrap_dup2(int fd, int newfd)
 {
 	struct socket_info *si;
-	struct socket_info_fd *fi;
-
-	si = find_socket_info(fd);
+	struct socket_info_fd *src_fi, *fi;
 
-	if (!si) {
+	src_fi = find_socket_info_fd(fd);
+	if (src_fi == NULL) {
 		return libc_dup2(fd, newfd);
 	}
 
+	si = &sockets[src_fi->si_index];
+
+	if (fd == newfd) {
+		/*
+		 * According to the manpage:
+		 *
+		 * "If oldfd is a valid file descriptor, and newfd has the same
+		 * value as oldfd, then dup2() does nothing, and returns newfd."
+		 */
+		return newfd;
+	}
+
 	if (find_socket_info(newfd)) {
 		/* dup2() does an implicit close of newfd, which we
 		 * need to emulate */
@@ -5208,10 +5716,13 @@ static int swrap_dup2(int fd, int newfd)
 		return -1;
 	}
 
+	si->refcount++;
+	fi->si_index = src_fi->si_index;
+
 	/* Make sure we don't have an entry for the fd */
 	swrap_remove_stale(fi->fd);
 
-	SWRAP_DLIST_ADD(si->fds, fi);
+	SWRAP_DLIST_ADD_AFTER(socket_fds, fi, src_fi);
 	return fi->fd;
 }
 
@@ -5226,17 +5737,17 @@ int dup2(int fd, int newfd)
 
 static int swrap_vfcntl(int fd, int cmd, va_list va)
 {
-	struct socket_info_fd *fi;
+	struct socket_info_fd *src_fi, *fi;
 	struct socket_info *si;
 	int rc;
 
-	si = find_socket_info(fd);
-	if (si == NULL) {
-		rc = libc_vfcntl(fd, cmd, va);
-
-		return rc;
+	src_fi = find_socket_info_fd(fd);
+	if (src_fi == NULL) {
+		return libc_vfcntl(fd, cmd, va);
 	}
 
+	si = &sockets[src_fi->si_index];
+
 	switch (cmd) {
 	case F_DUPFD:
 		fi = (struct socket_info_fd *)calloc(1, sizeof(struct socket_info_fd));
@@ -5253,10 +5764,13 @@ static int swrap_vfcntl(int fd, int cmd, va_list va)
 			return -1;
 		}
 
+		si->refcount++;
+		fi->si_index = src_fi->si_index;
+
 		/* Make sure we don't have an entry for the fd */
 		swrap_remove_stale(fi->fd);
 
-		SWRAP_DLIST_ADD(si->fds, fi);
+		SWRAP_DLIST_ADD_AFTER(socket_fds, fi, src_fi);
 
 		rc = fi->fd;
 		break;
@@ -5319,6 +5833,36 @@ int pledge(const char *promises, const char *paths[])
 }
 #endif /* HAVE_PLEDGE */
 
+static void swrap_thread_prepare(void)
+{
+	SWRAP_LOCK_ALL;
+}
+
+static void swrap_thread_parent(void)
+{
+	SWRAP_UNLOCK_ALL;
+}
+
+static void swrap_thread_child(void)
+{
+	SWRAP_UNLOCK_ALL;
+}
+
+/****************************
+ * CONSTRUCTOR
+ ***************************/
+void swrap_constructor(void)
+{
+	/*
+	* If we hold a lock and the application forks, then the child
+	* is not able to unlock the mutex and we are in a deadlock.
+	* This should prevent such deadlocks.
+	*/
+	pthread_atfork(&swrap_thread_prepare,
+		       &swrap_thread_parent,
+		       &swrap_thread_child);
+}
+
 /****************************
  * DESTRUCTOR
  ***************************/
@@ -5329,20 +5873,19 @@ int pledge(const char *promises, const char *paths[])
  */
 void swrap_destructor(void)
 {
-	struct socket_info *s = sockets;
+	struct socket_info_fd *s = socket_fds;
 
 	while (s != NULL) {
-		struct socket_info_fd *f = s->fds;
-		if (f != NULL) {
-			swrap_close(f->fd);
-		}
-		s = sockets;
+		swrap_close(s->fd);
+		s = socket_fds;
 	}
 
-	if (swrap.libc_handle != NULL) {
-		dlclose(swrap.libc_handle);
+	free(sockets);
+
+	if (swrap.libc.handle != NULL) {
+		dlclose(swrap.libc.handle);
 	}
-	if (swrap.libsocket_handle) {
-		dlclose(swrap.libsocket_handle);
+	if (swrap.libc.socket_handle) {
+		dlclose(swrap.libc.socket_handle);
 	}
 }
diff --git a/third_party/socket_wrapper/wscript b/third_party/socket_wrapper/wscript
index 514265b92b6..fb3cf779098 100644
--- a/third_party/socket_wrapper/wscript
+++ b/third_party/socket_wrapper/wscript
@@ -2,7 +2,7 @@
 
 import os
 
-VERSION="1.1.7"
+VERSION="1.1.8"
 
 def configure(conf):
     if conf.CHECK_SOCKET_WRAPPER():
-- 
2.13.6



More information about the samba-technical mailing list