31

"Everything is a file" in the UNIX World.

Above sentence is famous. When I run echo "hello programmer" >> /dev/tty1 , I can watch the given string on TeleType 1 , ....

What and where is file per each socket? Suppose my friend connects to my PC, and its IP is h.h.h.h , how can I access the respective file? Is it possible?

3
  • 3
    The socket API is actually a deviation from the "Unix policy" in this regard, because it originally came from BSD. Note that there's always Plan 9 from Bell Labs which is "more Unix than Unix" – even the network and graphics APIs are files there.
    – ntoskrnl
    Feb 23, 2014 at 21:39
  • sockets and processes are mentioned a fair amount in Plan 9 papers, mostly talking about where the UNIX model went wrong.
    – strugee
    Feb 24, 2014 at 1:31
  • 1
    "Everything is a file" in the UNIX World. TLDR: That's not true. Read that carefully and note how POSIX clearly and consistently differentiates between files, file descriptions, and file descriptors. And note well how the value returned from socket() is a file descriptor that's not associated with any file nor file description. Jul 11, 2021 at 14:11

3 Answers 3

47

A socket is a file. But not all files have names. Here are a few examples of files that don't have names:

  • Any file that used to have a name, and is now deleted, but is still opened by a program.
  • An unnamed pipe, such as one created by the | shell operator.
  • Most sockets: any Internet socket, or a Unix socket which is not in the filesystem namespace (it can be in the abstract namespace or unnamed).

Files such as unnamed pipes or sockets are created by a process and can only be accessed in that process or in subsequently-created child processes. (This is not completely true: a process that has a pipe or socket (or any other file) open can transmit it to other processes via a Unix socket; this is known as file descriptor passing.)

Sockets that have a name (whether in the filesystem or abstract) can be opened using that name. Network sockets can be opened (or more precisely connected to) remotely from any machine that has appropriate connectivity.

7
  • This is the correct answer.
    – jforberg
    Aug 27, 2015 at 23:51
  • 5
    /proc/<pid>/fd/* and /proc/net/* may be interesting
    – n611x007
    Dec 25, 2015 at 3:56
  • Please accept this answer. It is IMHO a lot more accurate. Sep 23, 2016 at 15:05
  • 1
    @user1202136 This answer is not accurate. POSIX specifically and consistently uses file, file descriptor, and file description: "The open() function shall establish the connection between a file and a file descriptor. It shall create an open file description that refers to a file and a file descriptor that refers to that open file description. The file descriptor is used by other I/O functions to refer to that file. The path argument points to a pathname naming the file." Jul 11, 2021 at 14:03
  • 1
    @AndrewHenle If you're going to nitpick like this, “everything is a file descriptor” is also wrong: the correct statement would be “everything can be accessed via a file descriptor”. In the context of this question, there is no meaningful difference between “is a file” and “can be accessed via a file descriptor”. This is a question about operating system design, not about POSIX terminology. Jul 11, 2021 at 22:02
14

What and where is file per each socket?

"Everything" is an exaggeration. It's not a strict policy, it's just a common practice to use the filesystem for interfaces since filesystem access is synonymous with system calls (i.e., the filesystem is really an interface with the kernel, and so provides a convenient format for all kinds of things). Other operating systems do not make as much use of this, so it is considered a distinguishing feature.

As Hauke Laging mentions, "unix local" sockets have a file node as do named pipes (see man fifo). However, internet protocol sockets (used for network communication) do not. Instead, they are associated in userspace with a port number. Note that a server socket on a single port connects multiple clients each with their own individual socket (a single unix local socket file can be also be used this way with a server, meaning, there may be multiple sockets associated with the same file address) and in code they are in fact identified individually via separate numerical file descriptors.

So, in that sense all sockets are much like files, and have a link in /proc/[pid]/fd/. You can even call readlink() on this inode and get a special sort of filename, which is used in command line tools such as lsof, I believe; likewise you can get information about the socket descriptor via fstat().

2
  • You mean "identified in userspace by their inode"? Not every socket has a port number and there can be several sockets for the same port number (doesn't make sense, though). Feb 23, 2014 at 17:29
  • @HaukeLaging : Good point. I've edited to make this clearer starting with the second paragraph.
    – goldilocks
    Feb 24, 2014 at 15:51
13

man 7 unix:

The AF_UNIX (also known as AF_LOCAL) socket family is used to communicate between processes on the same machine efficiently. Traditionally, UNIX domain sockets can be either unnamed, or bound to a file system pathname (marked as being of type socket). Linux also supports an abstract namespace which is independent of the file system.

I.e. not every socket can be seen as a file (in the sense of "no file without a file name").

But there are files with lists of sockets (e.g. /proc/net/tcp); not exactly what "everything is a file" means, though.

1
  • This is the correct answer. See my comments above and on the most-upvoted answer. Jul 11, 2021 at 14:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .