I'm playing around with Linux domain sockets and now I came to the issue about understanding backlog. Here is the server side code:
const char *const hostname = "/tmp/test_local_addr";
int sock_fd = socket(AF_LOCAL, SOCK_STREAM, 0);
struct sockaddr_un server_address;
memset(&server_address, '\0', sizeof(struct sockaddr_un));
server_address.sun_family = AF_LOCAL;
strncpy(server_address.sun_path, hostname, hostname_len + 1);
const socklen_t addrlen = offsetof(struct sockaddr_un, sun_path) + hostname_len + 1;
bind(sock_fd, (struct sockaddr*) &server_address, addrlen);
int listen_result = listen(sock_fd, 0);
I set the backlog size to 0
in the listen and do not call accept
method so I expected that any client would receive Connection refused when trying to connect
. But when I ran strace nc -U /tmp/test_local_addr
from two separate terminals I have the first one prints
connect(3, {sa_family=AF_UNIX, sun_path="/tmp/test_local_addr"}, 110) = 0
poll([{fd=0, events=POLLIN}, {fd=3, events=0}, {fd=3, events=POLLIN}, {fd=1, events=0}], 4, -1
and the second
--- SIGWINCH {si_signo=SIGWINCH, si_code=SI_KERNEL} ---
connect(3, {sa_family=AF_UNIX, sun_path="/tmp/test_local_addr"}, 110
The behavior is not really clear to me. I expected they both immediately terminates with Connection refused
. But the
read(-1, 0x7fffc0db3740, 16384) = -1 EBADF (Bad file descriptor)
happens in both clients only when I terminate the server.