9

Under OS-X, I've got process named 'listener' that is waiting on 'accept' to read data from local unix socket named listener_socket. unfortunately, any attempt to connect that socket terminate in 'connection refused' error.

Using lsof, to make sure that the 'listener' actually listen to this socket :

sudo lsof -p 570
COMMAND PID USER   FD     TYPE             DEVICE   SIZE/OFF   NODE NAME
...
listener  570 root    3u    unix 0x48a2751a1bad61ef        0t0        /private/var/run/my_sockets/listener_socket

Notice that the file is, in fact, a valid unix socket :

file /private/var/run/my_sockets/listener_socket /private/var/run/my_sockets/listener_socket: socket

However, it still fail to connect, even when i'm using an alternative way from command like (using socat command)

sudo socat LOCAL:/private/var/run/my_sockets/listener_socket,interval=1 EXEC:'aaaaaaaaaaaaaaaaa',nofork

2015/11/23 00:57:33 socat[928] E connect(3, LEN=49 AF=1 "/private/var/run/my_sockets/listener_socket", 49): Connection refused

perhaps there are more i can do to figure out why i cannot send data to the socket, even-though it's obvious that 'listener' waiting for this data on the other side ?

here's the relevant part of my code :

sender:

sockfd = socket(PF_UNIX, SOCK_STREAM, 0);
address.sun_family = AF_UNIX;
snprintf(address.sun_path, UNIX_PATH_MAX, "%s", LISTENER_SOCKET_PATH);
connect(sockfd, (struct sockaddr *) &address, sizeof(struct sockaddr_un)
write ...

receiver:

fd = socket(PF_UNIX, SOCK_STREAM, 0);
unlink(sock_name); // in case the socket is used before
listen(server->fd, 5); // we don't reach 5 listeners for sure ...
chmod(sock_name, mode); // giving root privilages
accept(server->fd, (struct sockaddr *) &server->address, &server->address_length);
read ...

thanks

8
  • 5
    Please post the source code, preferably a "minimum, complete, verifiable example". See stackoverflow.com/help/mcve for guidelines.
    – keithmo
    Commented Nov 23, 2015 at 9:23
  • 1
    Have you checked the permissions on the socket file? Commented Nov 23, 2015 at 9:26
  • @TheDarkKnight, notice that 'ls -ltr' shows : srw------- 1 root daemon 0 Nov 22 07:09 /private/var/run/my_sockets/listener_socket and the parent process that runs listener has pid 0, so listener is also having root privilages 0 49 1 0 6:36AM ?? 3:24.06 /Library/Application Support/PaloAltoNetworks/Traps/bin/trapsd
    – Zohar81
    Commented Nov 23, 2015 at 9:40
  • 1
    @Zohar this isn't enough - you need to post a complete, compilable (but still minimal) example.
    – davmac
    Commented Nov 23, 2015 at 10:00
  • 1
    Two things: On the receiving side, you're missing a call to bind() (probably accidentally removed when you prepared the code for posting here), and 2) in accept(server->fd, (struct sockaddr *) &server->address, &server->address_length); are you setting server->address_length before calling accept()?
    – keithmo
    Commented Nov 23, 2015 at 10:05

1 Answer 1

5

The server seems to miss calling bind() on the listening socket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.