Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
unix: don't unlink UNIX socket on EADDRINUSE
Browse files Browse the repository at this point in the history
It was a bad idea to start with...
  • Loading branch information
bnoordhuis committed May 23, 2012
1 parent 2e3e658 commit 3604b8d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
20 changes: 4 additions & 16 deletions src/unix/pipe.c
Expand Up @@ -81,22 +81,10 @@ int uv_pipe_bind(uv_pipe_t* handle, const char* name) {
uv_strlcpy(saddr.sun_path, pipe_fname, sizeof(saddr.sun_path));
saddr.sun_family = AF_UNIX;

if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr) == -1) {
/* On EADDRINUSE:
*
* We hold the file lock so there is no other process listening
* on the socket. Ergo, it's stale - remove it.
*
* This assumes that the other process uses locking too
* but that's a good enough assumption for now.
*/
if (errno != EADDRINUSE
|| unlink(pipe_fname) == -1
|| bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr) == -1) {
/* Convert ENOENT to EACCES for compatibility with Windows. */
uv__set_sys_error(handle->loop, (errno == ENOENT) ? EACCES : errno);
goto out;
}
if (bind(sockfd, (struct sockaddr*)&saddr, sizeof saddr)) {
/* Convert ENOENT to EACCES for compatibility with Windows. */
uv__set_sys_error(handle->loop, (errno == ENOENT) ? EACCES : errno);
goto out;
}
bound = 1;

Expand Down
4 changes: 4 additions & 0 deletions test/test-pipe-bind-error.c
Expand Up @@ -27,8 +27,10 @@

#ifdef _WIN32
# define BAD_PIPENAME "bad-pipe"
# define UNLINK_PIPE(name)
#else
# define BAD_PIPENAME "/path/to/unix/socket/that/really/should/not/be/there"
# define UNLINK_PIPE(name) remove(name)
#endif


Expand All @@ -45,6 +47,8 @@ TEST_IMPL(pipe_bind_error_addrinuse) {
uv_pipe_t server1, server2;
int r;

UNLINK_PIPE(TEST_PIPENAME);

r = uv_pipe_init(uv_default_loop(), &server1, 0);
ASSERT(r == 0);
r = uv_pipe_bind(&server1, TEST_PIPENAME);
Expand Down

0 comments on commit 3604b8d

Please sign in to comment.