procfs

package
v0.6.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2025 License: BSD-3-Clause, MPL-2.0 Imports: 16 Imported by: 0

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

View Source
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

func CheckProcSelfFdPath(path string, file fd.Fd) error

CheckProcSelfFdPath returns whether the given file handle matches the expected path. (This is inherently racy.)

func ProcSelfFdReadlink(fd fd.Fd) (string, error)

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

func ReopenFd(handle fd.Fd, flags int) (*os.File, error)

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

type Handle struct {
	Inner fd.Fd
	// contains filtered or unexported fields
}

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

func OpenProcRoot() (*Handle, error)

OpenProcRoot tries to open a "safer" handle to "/proc".

func OpenUnsafeProcRoot

func OpenUnsafeProcRoot() (*Handle, error)

OpenUnsafeProcRoot opens a handle to "/proc" without any overmounts or masked paths (but also without "subset=pid").

func (*Handle) Close

func (proc *Handle) Close() error

Close closes the underlying file for the Handle.

func (*Handle) OpenPid

func (proc *Handle) OpenPid(pid int, subpath string) (*os.File, error)

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) OpenRoot

func (proc *Handle) OpenRoot(subpath string) (*os.File, error)

OpenRoot returns a handle to /proc/<subpath>.

func (*Handle) OpenSelf

func (proc *Handle) OpenSelf(subpath string) (*os.File, error)

OpenSelf returns a handle to /proc/self/<subpath>.

func (*Handle) OpenThreadSelf

func (proc *Handle) OpenThreadSelf(subpath string) (_ *os.File, _ ProcThreadSelfCloser, Err error)

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 (proc *Handle) Readlink(base procfsBase, subpath string) (string, error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL