Skip to content

Instantly share code, notes, and snippets.

@developer-guy
Last active March 14, 2024 17:57
Show Gist options
  • Save developer-guy/b9a49597862e7f2fc3cc636cee4e5914 to your computer and use it in GitHub Desktop.
Save developer-guy/b9a49597862e7f2fc3cc636cee4e5914 to your computer and use it in GitHub Desktop.
execve syscall in go
package main
import (
_ "embed"
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"os/signal"
"path/filepath"
"syscall"
"github.com/mitchellh/go-ps"
)
//go:embed in.tpl
var tpl string
func main()
err := ioutil.WriteFile("/consul-template/in.tpl", []byte(tpl), 0644)
if err != nil {
log.Fatalf("could not write file, error: %v", err)
}
err = filepath.Walk("/consul-template",
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
fmt.Println(path, info.Size())
return nil
})
if err != nil {
log.Fatalf("could not walk filepath, error: %v", err)
}
ctExecPath, err := exec.LookPath("ct")
if err != nil {
log.Fatalf("could not found binary, error: %v", err)
}
log.Printf("Starting %s process by uid: %d and gid: %d", ctExecPath, uid, gid)
argv := []string{ctExecPath, "-template=in.tpl:./data/out.txt", "-consul-addr=http://host.docker.internal:8500"}
log.Println("Starting argv:", argv)
err = syscall.Exec(ctExecPath, argv, os.Environ())
if err != nil {
log.Fatalf("could not execve ct, error: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment