Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AF_UNIX domain - why use local file names only?

Tags:

unix

sockets

When using socket in the UNIX domain, it is advisable to use path name for the directory directory mounted on the local disk. The UNIX domain only allows interprocess communication for process working on same machine.

Can you please explain the above line? It is about a socket in the UNIX DOMAIN.

Thanks!

like image 609
mawia Avatar asked Apr 23 '09 19:04

mawia


3 Answers

A Unix domain socket or IPC socket (inter-process communication socket) is a data communications endpoint that is similar to an Internet socket, but does not use a network protocol for communication. It is used in POSIX operating systems for inter-process communication. The correct standard POSIX term is POSIX Local IPC Sockets.

Unix domain connections appear as byte streams, much like network connections, but all data remains within the local computer. UNIX domain sockets use the file system as address name space, i.e. they are referenced by processes as inodes in the file system. This allows two distinct processes to open the same socket in order to communicate. However, the actual communication (the data exchange) does not use the file system, but buffers in kernel memory.

In addition to sending data, processes can send file descriptors across a Unix domain socket connection using the sendmsg() and recvmsg() system calls.

like image 120
user39113 Avatar answered Sep 18 '22 13:09

user39113


The end-points of UNIX domain sockets are represented by files in the file system (instead of by host / port).

However the communication between processes is done within the local system and does not result in a seekable file getting stored anywhere.

The advantage of using the file system as the namespace for the end-points is that normal file permissions and ACLs can be applied - if you can't open the end-point you can't connect. IP sockets have no such mechanism.

like image 21
Alnitak Avatar answered Sep 20 '22 13:09

Alnitak


It means that if you create a AF_UNIX socket on a NFS disk which is shared between two machines A and B, you cannot have a process on A writing data to the unix socket and a process on B reading data from that socket.

The communication happens at kernel level, and you can only transfer data among processes sitting in the same kernel.

like image 42
Stefano Borini Avatar answered Sep 22 '22 13:09

Stefano Borini