Avoid mutex lock wait in socket close failure
authorAnoop C S <anoopcs@redhat.com>
Fri, 10 Feb 2017 16:08:11 +0000 (21:38 +0530)
committerMichael Adam <obnox@samba.org>
Fri, 10 Feb 2017 17:31:24 +0000 (18:31 +0100)
In case of absence to close a socket fd during an exit from
application we try to close the same by traversing the
socket_fds in swrap_destructor. But the early lock taken on
libc_symbol_binding_mutex inside the destructor blocks the
subsequent request for locking the same while loading
libc_close within swrap_close.

Also added a test case to verify this flaw in destructor.

Signed-off-by: Anoop C S <anoopcs@redhat.com>
Reviewed-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
src/socket_wrapper.c
tests/CMakeLists.txt
tests/test_close_failure.c [new file with mode: 0644]

index 186b696a4fe265780ef109cc3aab08a0c8391f7e..3d468c38a47eae19ffdfff83b578708190814507 100644 (file)
@@ -5656,8 +5656,6 @@ void swrap_destructor(void)
 {
        struct socket_info_fd *s = socket_fds;
 
-       SWRAP_LOCK_ALL;
-
        while (s != NULL) {
                swrap_close(s->fd);
                s = socket_fds;
@@ -5671,6 +5669,4 @@ void swrap_destructor(void)
        if (swrap.libc.socket_handle) {
                dlclose(swrap.libc.socket_handle);
        }
-
-       SWRAP_UNLOCK_ALL;
 }
index 9292b4029f52e5174cdaf1da2d3a4061e2ec34db..c2bd79974bcc9fe1025729c3e9c6be44d717aa4d 100644 (file)
@@ -35,7 +35,8 @@ set(SWRAP_TESTS
     test_echo_udp_send_recv
     test_echo_udp_sendmsg_recvmsg
     test_swrap_unit
-    test_max_sockets)
+    test_max_sockets
+    test_close_failure)
 
 if (HAVE_STRUCT_MSGHDR_MSG_CONTROL)
     set(SWRAP_TESTS ${SWRAP_TESTS} test_sendmsg_recvmsg_fd)
diff --git a/tests/test_close_failure.c b/tests/test_close_failure.c
new file mode 100644 (file)
index 0000000..0c9d6a7
--- /dev/null
@@ -0,0 +1,50 @@
+#include "torture.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <cmocka.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+static int setup(void **state)
+{
+       torture_setup_socket_dir(state);
+
+       return 0;
+}
+
+static int teardown(void **state)
+{
+       torture_teardown_socket_dir(state);
+
+       return 0;
+}
+
+static void test_close_failure(void **state)
+{
+       int s;
+       int rc;
+
+       (void) state; /* unused */
+       (void) s; /*set but not used */
+
+       s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
+
+       assert_int_not_equal(rc, -1);
+
+       /* Do not close the socket here so that destructor
+        * handles it and no hang should be observed.*/
+}
+
+int main(void) {
+       int rc;
+
+       const struct CMUnitTest close_failure_tests[] = {
+               cmocka_unit_test_setup_teardown(test_close_failure,
+                                               setup, teardown),
+       };
+
+       rc = cmocka_run_group_tests(close_failure_tests, NULL, NULL);
+
+       return rc;
+}