Search code examples
linuxipczeromqunix-socket

ZeroMQ: Check if someone is listening behind Unix domain socket


Context: Linux (Ubuntu), C, ZeroMQ

I have a server which listens on ipc:// SUB ZeroMQ socket (which physically is a Unix domain socket).

I have a client which should connect to the socket, publish its message and disconnect.

The problem: If server is killed (or otherwise dies unnaturally), socket file stays in place. If client attempts to connect to this stale socket, it blocks in zmq_term().

I need to prevent client from blocking if server is not there, but guarantee delivery if server is alive but busy.

Assume that I can not track server lifetime by some external magic (e.g. by checking a PID file).

Any hints?


Solution

  • Non-portable solution seems to be to read /proc/net/unix and search there for a socket name.