4 ms·
You have to somehow increment inode's reference count and write reference to it into some directory. symlink() does not increase reference count of anything an
by dfox 9y ago
You 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 :(