[zeromq-dev] [PATCH] fix overwriting errno on bind failure

Dhammika Pathirana dhammika at gmail.com
Mon Dec 13 08:14:04 CET 2010


>From 8dd8d580beb9fd84aec99aeb08ead34e3206a080 Mon Sep 17 00:00:00 2001
From: Dhammika Pathirana <dhammika at gmail.com>
Date: Sun, 12 Dec 2010 22:55:55 -0800
Subject: [PATCH 1/2] fix overwriting errno on bind failure


Signed-off-by: Dhammika Pathirana <dhammika at gmail.com>
---
 src/tcp_listener.cpp |    7 +++++--
 src/tcp_listener.hpp |    2 ++
 2 files changed, 7 insertions(+), 2 deletions(-)

diff --git a/src/tcp_listener.cpp b/src/tcp_listener.cpp
index 6d41069..2b60983 100644
--- a/src/tcp_listener.cpp
+++ b/src/tcp_listener.cpp
@@ -30,6 +30,7 @@
 #ifdef ZMQ_HAVE_WINDOWS

 zmq::tcp_listener_t::tcp_listener_t () :
+    addr_file (false),
     s (retired_fd)
 {
     memset (&addr, 0, sizeof (addr));
@@ -151,6 +152,7 @@ zmq::fd_t zmq::tcp_listener_t::accept ()
 #endif

 zmq::tcp_listener_t::tcp_listener_t () :
+    addr_file (false),
     s (retired_fd)
 {
     memset (&addr, 0, sizeof (addr));
@@ -236,11 +238,12 @@ int zmq::tcp_listener_t::set_address (const char
*protocol_, const char *addr_,
         errno_assert (rc != -1);

         //  Bind the socket to the file path.
-        rc = bind (s, (struct sockaddr*) &addr, sizeof (sockaddr_un));
+        rc = bind (s, (struct sockaddr*) &addr, addr_len);
         if (rc != 0) {
             close ();
             return -1;
         }
+        addr_file = true;

         //  Listen for incomming connections.
         rc = listen (s, backlog_);
@@ -270,7 +273,7 @@ int zmq::tcp_listener_t::close ()
     //  If there's an underlying UNIX domain socket, get rid of the file it
     //  is associated with.
     struct sockaddr_un *su = (struct sockaddr_un*) &addr;
-    if (AF_UNIX == su->sun_family) {
+    if (AF_UNIX == su->sun_family && addr_file) {
         rc = ::unlink(su->sun_path);
         if (rc != 0)
             return -1;
diff --git a/src/tcp_listener.hpp b/src/tcp_listener.hpp
index 1490a56..94a6452 100644
--- a/src/tcp_listener.hpp
+++ b/src/tcp_listener.hpp
@@ -56,6 +56,8 @@ namespace zmq
         //  Address to listen on.
         sockaddr_storage addr;
         socklen_t addr_len;
+        //  Unix domain socket bound to a file.
+        bool addr_file;

         //  Underlying socket.
         fd_t s;
-- 
1.7.0.4


Dhammika



More information about the zeromq-dev mailing list