I only have access to one POSIX specification document, which is System Interfaces, so I'm going to do my best from here.
Our specification spelunking adventure must of course start in 2.10.6 Use of Options, which defines the SO_REUSEADDR
option as follows:
The SO_REUSEADDR option indicates that the rules used in validating addresses supplied in a bind() should allow reuse of local addresses. Operation of this option is protocol-specific. The default value for SO_REUSEADDR is off; that is, reuse of local addresses is not permitted.
This paragraph basically disclaims any specification of what this option truly does, by delegating it to the specification of the underlying protocol.
Section 2.10.17 Use of Sockets for Local UNIX Connections describes the mechanics of creating UNIX domain sockets, but really the only thing it tells us is what socket type constant to use and which struct to use for addresses. The documentation for the sockaddr_un
struct tells us only about its format, not about its behavior on bind
.
The documentation for bind
itself is understandably protocol-agnostic, telling us only what happens when the address is already in use, not the circumstances under which re-binding the same socket is valid.
Although it's not a POSIX standard document, Fujitsu has a document on the POSIX sockets API which is interesting if only because it's not specifically about Linux or BSD. Section 2.6.4 of this document has the following to say about the behavior of bind
on UNIX sockets:
The path name, which must be specified in the sun.sun_path
component, is created as a file in the file system using bind()
. The process that calls bind()
must therefore have write rights to the directory in which the file is to be written. The system does not delete the file. It should therefore be deleted by the process when it is no longer required.
Although this says nothing about SO_REUSEADDR
in particular, it does state that the behavior of bind
is to create a file, and that it is the responsibility of the binding process to remove it when it's no longer being used.
Finally, this document's description of setsockopt
has the following to say about SO_REUSEADDR
:
Specifies that the rules for the validity check on the addresses specified for bind() should permit the reuse of local addresses provided this is supported by the protocol.
So while this makes no specific mention of AF_UNIX
, it does acknowledge that this option doesn't apply to all protocols.
I also found a (non-authoritative) summary describing the intended purpose of SO_REUSEADDR
:
This socket option tells the kernel that even if this port is busy (in the TIME_WAIT
state), go ahead and reuse it anyway. If it is busy, but with another state, you will still get an address already in use error.
The TIME_WAIT
state exists primarily for Internet-facing sockets to avoid a new program binding to a port that was recently being used by another program and inadvertently receiving packets relating to the old program. This problem is arguably not applicable to UNIX domain sockets because it's very unlikely that two programs would attempt to create a socket at the same path, so if TIME_WAIT
is not implemented for UNIX sockets then that could be one (approximation of an) explanation for why SO_REUSEADDR
does not apply to AF_UNIX
.