diff --git a/src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.cpp b/src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.cpp index b0da601fe6..dc3a4a19fa 100644 --- a/src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.cpp +++ b/src/add-ons/kernel/network/protocols/tcp/TCPEndpoint.cpp @@ -728,18 +728,20 @@ TCPEndpoint::Listen(int count) MutexLocker _(fLock); - if (fState != CLOSED) + if (fState != CLOSED && fState != LISTEN) return B_BAD_VALUE; - fAcceptSemaphore = create_sem(0, "tcp accept"); - if (fAcceptSemaphore < B_OK) - return ENOBUFS; - - status_t status = fManager->SetPassive(this); - if (status != B_OK) { - delete_sem(fAcceptSemaphore); - fAcceptSemaphore = -1; - return status; + if (fState == CLOSED) { + fAcceptSemaphore = create_sem(0, "tcp accept"); + if (fAcceptSemaphore < B_OK) + return ENOBUFS; + + status_t status = fManager->SetPassive(this); + if (status != B_OK) { + delete_sem(fAcceptSemaphore); + fAcceptSemaphore = -1; + return status; + } } gSocketModule->set_max_backlog(socket, count); diff --git a/src/add-ons/kernel/network/protocols/unix/UnixEndpoint.cpp b/src/add-ons/kernel/network/protocols/unix/UnixEndpoint.cpp index b2d12b5d56..59524a4cd5 100644 --- a/src/add-ons/kernel/network/protocols/unix/UnixEndpoint.cpp +++ b/src/add-ons/kernel/network/protocols/unix/UnixEndpoint.cpp @@ -239,22 +239,25 @@ UnixEndpoint::Listen(int backlog) if (!IsBound()) RETURN_ERROR(EDESTADDRREQ); - if (fState != UNIX_ENDPOINT_NOT_CONNECTED) + if (fState != UNIX_ENDPOINT_NOT_CONNECTED + && fState != UNIX_ENDPOINT_LISTENING) RETURN_ERROR(EINVAL); gSocketModule->set_max_backlog(socket, backlog); - fAcceptSemaphore = create_sem(0, "unix accept"); - if (fAcceptSemaphore < 0) - RETURN_ERROR(ENOBUFS); - - _UnsetReceiveFifo(); - - fCredentials.pid = getpid(); - fCredentials.uid = geteuid(); - fCredentials.gid = getegid(); - - fState = UNIX_ENDPOINT_LISTENING; + if (fState == UNIX_ENDPOINT_NOT_CONNECTED) { + fAcceptSemaphore = create_sem(0, "unix accept"); + if (fAcceptSemaphore < 0) + RETURN_ERROR(ENOBUFS); + + _UnsetReceiveFifo(); + + fCredentials.pid = getpid(); + fCredentials.uid = geteuid(); + fCredentials.gid = getegid(); + + fState = UNIX_ENDPOINT_LISTENING; + } RETURN_ERROR(B_OK); }