Unix – Wait for a Unix Domain socket to be bound

ipcunix-socket

I am writing a client app which connects to a server process through a Unix domain socket. If the server process is not running, I want the client to wait until the server has started and is listening for connections on the socket.

Currently I have a retry loop in the client which calls connect() every second until it successfully connects to the socket.

Is there any function I can call which will simply block until a particular named socket (e.g. "/var/mysock") is created and bound to a server process?

Best Answer

Not a full answer, but...

If you're on Linux, the inotify interface will let you trap a the first few useful steps of the operation:

  • unlink'ing a leftover former socket will trigger an IN_DELETE_SELF on it.
  • bind'ing will trigger an IN_CREATE on the parent directory.

Unfortunately, the server isn't connect'able until it listen's. Though this is the next logical step, there's really no guarantee it will do so right away. It doesn't look as though inotify provides an interface to that.

Related Topic