aboutsummaryrefslogtreecommitdiff
path: root/internal/syscallcompat/reopen_linux.go
blob: 3b0766bc4b0045d24228cdb881bf96dab7e18c6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package syscallcompat

import (
	"fmt"
	"syscall"
)

// ReopenFd opens the file referenced by "fd" again, with new "flags".
//
// On Linux, this goes through /proc/self/fd, which always resolves to
// exactly the file "fd" refers to, even if it has been renamed or replaced.
// "dirfd" and "name" are not used. They are needed for the fallback
// implementation on other platforms.
// Retries on EINTR.
func ReopenFd(fd int, dirfd int, name string, flags int) (int, error) {
	procPath := fmt.Sprintf("/proc/self/fd/%d", fd)
	// /proc/self/fd/N is a symlink, so O_NOFOLLOW would make the open fail
	// with ELOOP. Following it is safe because it is a "magic link" that
	// points to the open file description, not to a path.
	flags = flags&^syscall.O_NOFOLLOW | syscall.O_CLOEXEC
	return retryEINTR2(func() (int, error) {
		return syscall.Open(procPath, flags, 0)
	})
}