Skip to content

Commit

Permalink
fix overwriting errno on bind failure
Browse files Browse the repository at this point in the history
Signed-off-by: Dhammika Pathirana <dhammika@gmail.com>
  • Loading branch information
dhammika authored and sustrik committed Dec 13, 2010
1 parent a9d969a commit 22b2b9a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/tcp_listener.cpp
Expand Up @@ -30,6 +30,7 @@
#ifdef ZMQ_HAVE_WINDOWS

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

zmq::tcp_listener_t::tcp_listener_t () :
has_file (false),
s (retired_fd)
{
memset (&addr, 0, sizeof (addr));
Expand Down Expand Up @@ -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;
}
has_file = true;

// Listen for incomming connections.
rc = listen (s, backlog_);
Expand Down Expand Up @@ -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 && has_file) {
rc = ::unlink(su->sun_path);
if (rc != 0)
return -1;
Expand Down
3 changes: 3 additions & 0 deletions src/tcp_listener.hpp
Expand Up @@ -57,6 +57,9 @@ namespace zmq
sockaddr_storage addr;
socklen_t addr_len;

// True, if the undelying file for UNIX domain socket exists.
bool has_file;

// Underlying socket.
fd_t s;

Expand Down

0 comments on commit 22b2b9a

Please sign in to comment.