gokm/procfs

import "gokm/procfs"

Package procfs creates files in /proc backed by Go code.

f, err := procfs.Create("mystats", procfs.ReadFunc(func() ([]byte, error) {
	return []byte("hits: " + strconv.Itoa(hits) + "\n"), nil
}))

Read is called once per open() and its result is what that open file reads (so readers always see a consistent snapshot). Handlers can run concurrently on several CPUs: protect shared state. If the handler also implements Writer, the file is writable and each write() is passed to it. Files are removed automatically at module exit (or earlier with Remove).

Index#

Types#

type File#

type File struct {
	// contains filtered or unexported fields
}

File is a /proc entry.

func Create#

func Create(name string, h Handler) (*File, error)

Create creates /proc/<name>: mode 0444, or 0644 if h implements Writer.

func (*File) Name#

func (f *File) Name() string

Name returns the file name (under /proc).

func (*File) Remove#

func (f *File) Remove()

Remove removes the file, waiting for in-progress operations on it.

type Handler#

type Handler interface {
	Read() ([]byte, error)
}

Handler produces the file’s content.

type ReadFunc#

type ReadFunc func() ([]byte, error)

ReadFunc adapts a function to a read-only Handler.

func (ReadFunc) Read#

func (f ReadFunc) Read() ([]byte, error)

type Writer#

type Writer interface {
	Write(p []byte) error
}

Writer is optionally implemented by handlers of writable files. Each write(2) call becomes one Write (at most 64 KiB).