diff --git a/src/ipc_listener.cpp b/src/ipc_listener.cpp index 1df2e0a0..4e20f437 100644 --- a/src/ipc_listener.cpp +++ b/src/ipc_listener.cpp @@ -249,11 +249,17 @@ int zmq::ipc_listener_t::close () _s = retired_fd; if (_has_file && options.use_fd == -1) { - rc = 0; + if (!_tmp_socket_dirname.empty ()) { + // TODO review this behaviour, it is inconsistent with the use of + // unlink in open since 656cdb959a7482c45db979c1d08ede585d12e315; + // however, we must at least remove the file before removing the + // directory, otherwise it will always fail + rc = ::unlink (_filename.c_str ()); - if (rc == 0 && !_tmp_socket_dirname.empty ()) { - rc = ::rmdir (_tmp_socket_dirname.c_str ()); - _tmp_socket_dirname.clear (); + if (rc == 0) { + rc = ::rmdir (_tmp_socket_dirname.c_str ()); + _tmp_socket_dirname.clear (); + } } if (rc != 0) { diff --git a/tests/test_rebind_ipc.cpp b/tests/test_rebind_ipc.cpp index b14cb81d..55512c77 100644 --- a/tests/test_rebind_ipc.cpp +++ b/tests/test_rebind_ipc.cpp @@ -44,20 +44,8 @@ void tearDown () void test_rebind_ipc () { - char my_endpoint[32], random_file[16]; - strcpy (random_file, "tmpXXXXXX"); - -#ifdef HAVE_MKDTEMP - TEST_ASSERT_TRUE (mkdtemp (random_file)); - strcat (random_file, "/ipc"); -#else - int fd = mkstemp (random_file); - TEST_ASSERT_TRUE (fd != -1); - close (fd); -#endif - - strcpy (my_endpoint, "ipc://"); - strcat (my_endpoint, random_file); + char my_endpoint[32]; + make_random_ipc_endpoint (my_endpoint); void *sb0 = test_context_socket (ZMQ_PUSH); void *sb1 = test_context_socket (ZMQ_PUSH); diff --git a/tests/test_reconnect_ivl.cpp b/tests/test_reconnect_ivl.cpp index 6dd0e4cd..5d225398 100644 --- a/tests/test_reconnect_ivl.cpp +++ b/tests/test_reconnect_ivl.cpp @@ -72,12 +72,10 @@ void test_reconnect_ivl_against_pair_socket (const char *my_endpoint_, void test_reconnect_ivl_ipc (void) { char my_endpoint[256]; - size_t len = sizeof (my_endpoint); + make_random_ipc_endpoint (my_endpoint); void *sb = test_context_socket (ZMQ_PAIR); - TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, "ipc://*")); - TEST_ASSERT_SUCCESS_ERRNO ( - zmq_getsockopt (sb, ZMQ_LAST_ENDPOINT, my_endpoint, &len)); + TEST_ASSERT_SUCCESS_ERRNO (zmq_bind (sb, my_endpoint)); test_reconnect_ivl_against_pair_socket (my_endpoint, sb); test_context_socket_close (sb); diff --git a/tests/testutil_unity.hpp b/tests/testutil_unity.hpp index 63cb091a..1fe04c0e 100644 --- a/tests/testutil_unity.hpp +++ b/tests/testutil_unity.hpp @@ -326,3 +326,23 @@ void bind_loopback_ipv6 (void *socket_, char *my_endpoint_, size_t len_) { bind_loopback (socket_, true, my_endpoint_, len_); } + +// utility function to create a random IPC endpoint, similar to what a ipc://* +// wildcard binding does, but in a way it can be reused for multiple binds +void make_random_ipc_endpoint (char *out_endpoint_) +{ + char random_file[16]; + strcpy (random_file, "tmpXXXXXX"); + +#ifdef HAVE_MKDTEMP + TEST_ASSERT_TRUE (mkdtemp (random_file)); + strcat (random_file, "/ipc"); +#else + int fd = mkstemp (random_file); + TEST_ASSERT_TRUE (fd != -1); + close (fd); +#endif + + strcpy (out_endpoint_, "ipc://"); + strcat (out_endpoint_, random_file); +}