4

The Linux kernel creates a file for a Unix domain socket bound to a pathname.

Does the Linux kernel create a file for an Internet domain socket?

0

2 Answers 2

6

No, not in the sense of a file on hard-drive or other block device.

If you look at socket.c in Linux source code, you will see that it creates an inode for sockets, however data is in sockfs filesystem, which is a virtual filesystem within kernel itself, and space is allocated via kmalloc type of function. In that sense, sockets are anonymous files residing in memory.

This brings back to the concept of "everything is a file in Unix", which is a design pattern that focuses on having common utilities to perform same functions similar to files on real physical media. As Linus Torvalds stated:

The whole point with "everything is a file" is not that you have some random filename (indeed, sockets and pipes show that "file" and "filename" have nothing to do with each other), but the fact that you can use common tools to operate on different things.

Thus sockets have inode to have file-like access, but not present on physical filesystem.

However, note, there exist Unix domain sockets, which are IPC type of object intended for process networking, and do reside on disk filesystems.

4
  • 1
    @Tim I'd say yes. If you look at this they've referenced lsof output: "0xffff8803e256d9c0. That number is actually the address of the relevant in-kernel memory structure or type struct unix_sock". So there has to be an inode in kernel's virtual filesystem corresponding to that Commented Jan 6, 2019 at 2:44
  • 1
    And according to Gilles's answer " Unix socket which is not in the filesystem namespace (it can be in the abstract namespace or unnamed)" Commented Jan 6, 2019 at 2:47
  • Thanks. Is it correct that an open file descriptor is exposed as a file under /proc/<pid>/fd/? If that is true, isn't it that both an internet domain socket and a Unix domain socket are exposed as a file?
    – Tim
    Commented Feb 15, 2019 at 13:38
  • "Not In the sense of a file on hard-drive or other block device". @Tim have you ever notice this? Of course, there are very extreme corner cases like you use FUSE or write your own kernel module, in case you're extremely nitpicking. Commented Feb 15, 2019 at 14:58
1

The Linux kernel creates a file for a Unix domain socket bound to a pathname.

OK.

Does the Linux kernel create a file for an Internet domain socket?

No. Binding a socket to an IP address+port does not synthesize a pathname. It does not create a file somewhere you can see.

bind() on a AF_INET / AF_INET6 socket does not create any file on any physical filesystem. The bind() call will not generate a file on any of the builtin virtual filesystems. (Of course you can write your own FUSE filesystem, which mimics netstat -46 in some way, so that it creates a new file).

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