Documentation
¶
Overview ¶
Package procfs provides a safe API for operating on /proc on Linux. Note that this is the *internal* procfs API, mainy needed due to Go's restrictions on cyclic dependencies and its incredibly minimal visibility system without making a separate internal/ package.
Index ¶
- Constants
- func CheckProcSelfFdPath(path string, file fd.Fd) error
- func ProcSelfFdReadlink(fd fd.Fd) (string, error)
- func ReopenFd(handle fd.Fd, flags int) (*os.File, error)
- type Handle
- func (proc *Handle) Close() error
- func (proc *Handle) OpenPid(pid int, subpath string) (*os.File, error)
- func (proc *Handle) OpenRoot(subpath string) (*os.File, error)
- func (proc *Handle) OpenSelf(subpath string) (*os.File, error)
- func (proc *Handle) OpenThreadSelf(subpath string) (_ *os.File, _ ProcThreadSelfCloser, Err error)
- func (proc *Handle) Readlink(base procfsBase, subpath string) (string, error)
- type ProcThreadSelfCloser
Constants ¶
const ( // ProcRoot refers to the root of the procfs (i.e., "/proc/<subpath>"). ProcRoot procfsBase = "/proc" // ProcSelf refers to the current process' subdirectory (i.e., // "/proc/self/<subpath>"). ProcSelf procfsBase = "/proc/self" // ProcThreadSelf refers to the current thread's subdirectory (i.e., // "/proc/thread-self/<subpath>"). In multi-threaded programs (i.e., all Go // programs) where one thread has a different CLONE_FS, it is possible for // "/proc/self" to point the wrong thread and so "/proc/thread-self" may be // necessary. Note that on pre-3.17 kernels, "/proc/thread-self" doesn't // exist and so a fallback will be used in that case. ProcThreadSelf procfsBase = "/proc/thread-self" )
Variables ¶
This section is empty.
Functions ¶
func CheckProcSelfFdPath ¶
CheckProcSelfFdPath returns whether the given file handle matches the expected path. (This is inherently racy.)
func ProcSelfFdReadlink ¶
ProcSelfFdReadlink gets the real path of the given file by looking at readlink(/proc/thread-self/fd/$n).
This is just a wrapper around Handle.Readlink.
func ReopenFd ¶
ReopenFd takes an existing file descriptor and "re-opens" it through /proc/thread-self/fd/<fd>. This allows for O_PATH file descriptors to be upgraded to regular file descriptors, as well as changing the open mode of a regular file descriptor. Some filesystems have unique handling of open(2) which make this incredibly useful (such as /dev/ptmx).
Types ¶
type Handle ¶
Handle is a wrapper around an *os.File handle to "/proc", which can be used to do further procfs-related operations in a safe way.
func OpenProcRoot ¶
OpenProcRoot tries to open a "safer" handle to "/proc".
func OpenUnsafeProcRoot ¶
OpenUnsafeProcRoot opens a handle to "/proc" without any overmounts or masked paths (but also without "subset=pid").
func (*Handle) OpenPid ¶
OpenPid returns a handle to /proc/$pid/<subpath> (pid can be a pid or tid). This is mainly intended for usage when operating on other processes.
func (*Handle) OpenThreadSelf ¶
OpenThreadSelf returns a handle to "/proc/thread-self/<subpath>" (or an equivalent handle on older kernels where "/proc/thread-self" doesn't exist). Once finished with the handle, you must call the returned closer function (runtime.UnlockOSThread). You must not pass the returned *os.File to other Go threads or use the handle after calling the closer.
func (*Handle) Readlink ¶
Readlink performs a readlink operation on "/proc/<base>/<subpath>" in a way that should be free from race attacks. This is most commonly used to get the real path of a file by looking at "/proc/self/fd/$n", with the same safety protections as [Open] (as well as some additional checks against overmounts).
type ProcThreadSelfCloser ¶
type ProcThreadSelfCloser func()
ProcThreadSelfCloser is a callback that needs to be called when you are done operating on an os.File fetched using ProcThreadSelf.