...

Text file src/github.com/amenzhinsky/go-memexec/README.md

Documentation: github.com/amenzhinsky/go-memexec

     1# go-memexec
     2
     3Small library that executes binaries from the memory.
     4
     5## Usage
     6
     7## Static Binary
     8
     9Running static binaries is quite simple, it's only needed to embed it into the app and pass its content to `memexec.New`:
    10
    11```go
    12import (
    13	_ "embed"
    14	
    15	"github.com/amenzhinsky/go-memexec"
    16)
    17
    18// go:embed path-to-binary
    19var mybinary []byte
    20
    21exe, err := memexec.New(mybinary)
    22if err != nil {
    23	return err
    24}
    25defer exe.Close()
    26
    27cmd := exe.Command(argv...)
    28cmd.Output() // cmd is a `*exec.Cmd` from the standard library
    29```
    30
    31## Dynamic Binary (Linux only)
    32
    33With dynamic linked binaries things get more complicated, it's needed to embed all dependencies along with the executable.
    34
    35At 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.
    36
    37The 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).
    38
    39The following script helps generating packages, say `python3`:
    40
    41```sh
    42go install github.com/amenzhinsky/go-memexec/cmd/memexec-gen@latest
    43PATH=$(go env GOPATH)/bin:$PATH memexec-gen /usr/bin/python3
    44```
    45
    46It produces `python3` directory with binaries are to embed and `gen.go` file, that is a go package:
    47
    48```go
    49import "mypackagename/python3"
    50
    51exe, err := python3.New()
    52if err != nil {
    53	return err
    54}
    55defer exe.Close()
    56
    57b, err := exe.Command("-c", "print('Hello World')").CombinedOutput()
    58if err != nil {
    59	return err
    60}
    61```

View as plain text