3 ms·
I don't think the cross-filesystem hardlink is the problem, since the link in proc is a symbolic link. I can do this experimentally by creating a symbolic link
by andrewla 9y ago
I don't think the cross-filesystem hardlink is the problem, since the link in proc is a symbolic link.
I can do this experimentally by creating a symbolic link in /tmp (a different filesystem) to a file in /home, and then creating a hard link (with ln -L) from the symbolic link to another file in /home, and the result is a valid hardlink to the same inode as the original file.
This doesn't work through /proc for an unlinked file, but only because the underlying link call requires a path, not an inode. You can create a hard link out of proc if the file has not been deleted, though, without any cross-filesystem problems.
- dfox 9y agoYou have to somehow increment inode's reference count and write reference to it into some directory. symlink() does not increase reference count of anything and in fact its target does not have to be meaningful filename at all (although in the practical non-POSIX sense there does not exist any string that is not valid filename). One interesting ab-use of this is that you can use symlink()/readlink() as ad-hoc key-value store with atomicity guarantees (that hold true even on NFS). For example emacs uses exactly this for it's file locking mechanism. IIRC the files in /proc/pid/fd are not true symlinks but something that behaves as both file (you can do same IO operations as on the original FD) and symlink (ie. you can readlink() them and get some string) at once.
- zokier 9y agoman 2 open section about O_TMPFILE seems to strongly imply that you can linkat from /proc/<pid>/fd to concrete file. Not sure if there are some special cases for /proc/self/fd vs /proc/<pid>/fd, but that would seem bit odd. http://man7.org/linux/man-pages/man2/open.2.html http://man7.org/linux/man-pages/man2/open.2.html edit: nevermind, seems like O_TMPFILE is the one that has been special-cased here, from man 2 linkat: > This will generally not work if the file has a link count of zero (files created with O_TMPFILE and without O_EXCL are an exception). http://man7.org/linux/man-pages/man2/linkat.2.html http://man7.org/linux/man-pages/man2/linkat.2.html :(