5

I have general question about Linux. Will the inode be created if I create a fifo? pipe? socket?

Kara
  • 6,115
  • 16
  • 50
  • 57
macindows
  • 4,303
  • 4
  • 18
  • 9

2 Answers2

8

On Linux the answer can be obtained from /proc/<PID>/fd directory. To quote /proc documentation ( man 5 proc ):

For file descriptors for pipes and sockets, the entries will be symbolic links whose content is the file type with the inode. A readlink(2) call on this file returns a string in the format:

    type:[inode]

For example, socket:[2248868] will be a socket and its inode is 2248868. For sockets, that inode can be used to find more information in one of the files under /proc/net/.

Let's verify that:

$ bash -c 'true | ls -l /proc/self/fd/0'
lr-x------ 1 user user 64 Sep 13 03:58 /proc/self/fd/0 -> 'pipe:[54741]'

So will pipes and sockets have an inode ? Yes ! What about FIFOs ? We can guess that since they have a filename, they do have inode ( and I don't think directory entries without inode can exist ). But lets verify:

$ mkfifo foobar.fifo
$ ls -i foobar.fifo
1093642 foobar.fifo

The answer is "yes, FIFOs have inodes,too".

However, this raises an important question: inodes are properties of filesystems, and inodes aren't unique accross filesystems, so which filesystem is being referenced when we see a pipe inode ? Well, turns out there exists pipefs virtual filesystem which is mounted in Kernel space, rather than userspace. It manages both pipes and FIFOs, so the inode number you see is the /proc example is the property of those filesystems, rather than the filesystem you have on disk. And yes, anonymous pipes and anonymous sockets won't have inode on disk filesystem, because there's no filename and no bytes on disk (although there may be caching of data, and in fact old Unixes cached pipes to disk). FIFOs and Unix-domain sockets, however, have filename on the filesystem, so in foobar.fifo example that inode belongs on the disk filesystem.

See also:

Sergiy Kolodyazhnyy
  • 938
  • 1
  • 13
  • 41
5

No inode will be created for an anonymous pipe or a socket, as an inode is a property of a filesystem and neither of these two lives as a filesystem entity (they don't have a file path). They only have file descriptors.

However, for named pipes (aka fifo) an inode is created as it lives as an filesystem entity.

DarkDust
  • 90,870
  • 19
  • 190
  • 224
  • 1
    Unix domain sockets can have names in the filesystem too. See `unix(7)` for details. – sarnold Jun 29 '11 at 11:16
  • @sarnold: Nice catch, I forgot about them. Luckily they are the only sockets that do touch the filesystem. – DarkDust Jun 29 '11 at 11:20
  • It's important to note, that in this answer "filesystem" is referring to the filesystem on disk. There's multiple filesystems in Linux, including virtual filesystems like `/proc/`, `/dev/`, `/sys` in userspace, and then there's filesystems in kernel-space, which actually inode numbers as well. See [my answer](https://stackoverflow.com/a/52302763/3701431) on the topic. – Sergiy Kolodyazhnyy Sep 12 '18 at 20:22