memexec

package module
v0.0.0-...-591a25f Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

go-memexec

Small library that executes binaries from the memory.

Usage

Static Binary

Running static binaries is quite simple, it's only needed to embed it into the app and pass its content to memexec.New:

import (
	_ "embed"
	
	"github.com/KakashiHatake324/go-memexec"
)

// go:embed path-to-binary
var mybinary []byte

exe, err := memexec.New(mybinary)
if err != nil {
	return err
}
defer exe.Close()

cmd := exe.Command(argv...)
cmd.Output() // cmd is a `*exec.Cmd` from the standard library

Dynamic Binary (Linux only)

With dynamic linked binaries things get more complicated, it's needed to embed all dependencies along with the executable.

At the runtime deps are copied to a temp dir and executable receives the corresponding LD_LIBRARY_PATH that forces the dynamic linker to use the copied libraries.

The dynamic linker must be the same on both building and running machines since it's not included in the resulting binary (there's no interoperability between musl and GNU systems).

The following script helps generating packages, say python3:

go install github.com/KakashiHatake324/go-memexec/cmd/memexec-gen@latest
PATH=$(go env GOPATH)/bin:$PATH memexec-gen /usr/bin/python3

It produces python3 directory with binaries are to embed and gen.go file, that is a go package:

import "mypackagename/python3"

exe, err := python3.New()
if err != nil {
	return err
}
defer exe.Close()

b, err := exe.Command("-c", "print('Hello World')").CombinedOutput()
if err != nil {
	return err
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Exec

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

Exec is an in-memory executable code unit.

func New

func New(name string, b []byte, opts ...Option) (*Exec, error)

New creates new memory execution object that can be used for executing commands on a memory based binary.

func (*Exec) Close

func (m *Exec) Close() error

Close closes Exec object.

Any further command will fail, it's client's responsibility to control the flow by using synchronization algorithms.

func (*Exec) Command

func (m *Exec) Command(args ...string) *exec.Cmd

Command is an equivalent of `exec.Command`, except that the path to the executable is being omitted.

func (*Exec) CommandContext

func (m *Exec) CommandContext(ctx context.Context, args ...string) *exec.Cmd

CommandContext is an equivalent of `exec.CommandContext`, except that the path to the executable is being omitted.

type Option

type Option func(e *Exec)

func WithCleanup

func WithCleanup(fn func() error) Option

WithCleanup is executed right after Exec.Close.

func WithPrepare

func WithPrepare(fn func(cmd *exec.Cmd)) Option

WithPrepare configures cmd with default values such as Env, Dir, etc.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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