Skip to content

Instantly share code, notes, and snippets.

@samj1912
Created July 12, 2023 15:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samj1912/d3c26bee1009db030bc70a1bc6fbd9c9 to your computer and use it in GitHub Desktop.
Save samj1912/d3c26bee1009db030bc70a1bc6fbd9c9 to your computer and use it in GitHub Desktop.
package main
import (
"os"
"os/signal"
"syscall"
cmd "github.com/buildpacks/lifecycle/cmd"
launcher "github.com/buildpacks/lifecycle/cmd/launcher/cli"
reaper "github.com/ramr/go-reaper"
)
func main() {
if os.Getpid() == 1 {
// Start background reaping of orphaned child processes.
go reaper.Reap()
signalsBuffer := make(chan os.Signal, 3)
signal.Notify(signalsBuffer)
go func() {
for {
signalReceived := <-signalsBuffer
if signalReceived == syscall.SIGCHLD {
// Ignore SIGCHLD and let go-reaper handle them as per.
continue
}
signalId := signalReceived.(syscall.Signal)
// `man 2 kill`
// If pid equals -1, then sig is sent to every process for which the calling
// process has permission to send signals, except for process 1 (init)
_ = syscall.Kill(-1, signalId)
}
}()
pwd, err := os.Getwd()
if err != nil {
panic(err)
}
var wstatus syscall.WaitStatus
pattrs := &syscall.ProcAttr{
Dir: pwd,
Env: os.Environ(),
Sys: &syscall.SysProcAttr{Setsid: true},
Files: []uintptr{
uintptr(syscall.Stdin),
uintptr(syscall.Stdout),
uintptr(syscall.Stderr),
},
}
args := os.Args
pid, _ := syscall.ForkExec(args[0], args, pattrs)
_, err = syscall.Wait4(pid, &wstatus, 0, nil)
for syscall.EINTR == err {
_, err = syscall.Wait4(pid, &wstatus, 0, nil)
}
os.Exit(0)
return
}
cmd.Exit(launcher.RunLaunch())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment