Package gimel provides functionality for in-memory execution on Linux. It can also load arbitrary files into memory, which (in certain cases) can be shared with other Linux processes via file path or by passing the file descriptor via exec.
This library is based on example code by @magisterquis. Please give their excellent blog post on the subject a read.
The memfd_create(2) system call copies data into memory. The library offers several functions for executing the system call:
MemfdCreate()
- Executesmemfd_create(2)
, returning a file descriptor representing the in-memory fileMemfdCreateOSFile()
- Executesmemfd_create(2)
, returning a *os.File representing the in-memory fileMemfdCreateFromExe()
- Executesmemfd_create(2)
and copies the specified executable into the in-memory file, returning a *os.File and *exec.CmdMemfdCreateFromFile()
- Executesmemfd_create(2)
and copies the specified file into the in-memory file, returning a *os.FileMemfdCreateFromReader()
- Executesmemfd_create(2)
and copies the contents of the specified io.Reader into the in-memory file, returning a *os.File
Several helper functions are also available:
InMemoryFileToCmd()
- Converts an existing *os.File into a *exec.CmdCopyDataIntoMemFile()
- Copies the contents of an io.ReadCloser into the specified *os.File and closes the io.ReadCloser
The following examples can be found in the examples/ directory:
- examples/runonce/ - Run an application from memory only one time
- examples/multirun/ - Run an application from memory several times in a row, reusing the existing in-memory file
- examples/sharefile/ - Load an arbitrary file into memory, print its "/proc" file path so that other programs can access it using the file path