44

Is there a command similar to mkfifo but for domain sockets?

benmmurphy
  • 685
  • 1
  • 7
  • 8

4 Answers4

26

There is no exact equivalent of mkfifo for socket, i.e. there is no command that just creates a "hanging" socket. This is for historical reason: server's function bind(), the one that creates a socket name/inode in the filesystem, fails if the name is already used. In other words, server cannot operate on a pre-existing socket.

So if you'd created socket earlier, it would need to be removed by the server anyway first. No benefit. As you see with Gregory's answer, you can create a socket IF you keep a server for it, such as netcat. Once a server is gone, the old socket is gone. A new server has a new socket, and all clients need to re-connect, despite the socket's name being identical.

kubanczyk
  • 13,502
  • 5
  • 40
  • 55
17

Most recent netcat (nc) and similar programs (socat as far as I know) have domain socket options.
Else, you can have a look at ucspi-unix

Gregory MOUSSAT
  • 1,737
  • 2
  • 25
  • 48
16

You can use python:

python -c "import socket as s; sock = s.socket(s.AF_UNIX); sock.bind('/tmp/test.sock')"

Also C, see this answer.

akostadinov
  • 1,118
  • 1
  • 9
  • 18
  • 1
    `mksock() { SOCK="$1" python -c "import os, socket as s; s.socket(s.AF_UNIX).bind(os.environ['SOCK'])"; }` for easy shell use: `mksock /tmp/test.sock` – Tino Jul 21 '19 at 12:39
13

I simply use netcat and stay listening in such a case:

nc -lkU aSocket.sock

you should use netcat-openbsd. netcat-traditional does not have -U switch which is for Unix Domain socket.

Thomas
  • 4,155
  • 5
  • 21
  • 28
Karimai
  • 231
  • 2
  • 2
  • 2
    -k Forces nc to stay listening for another connection after its current connection is completed. It is an error to use this option without the -l option. -U Specifies to use UNIX-domain sockets. – Johan Boulé Aug 29 '19 at 15:01
  • Why, then, doesn't the `-k` option automatically turn on the `-l` option? – Sapphire_Brick Jun 09 '21 at 01:28