Skip to content

Instantly share code, notes, and snippets.

@vidarh
Created August 15, 2015 23:53
Show Gist options
  • Save vidarh/91a110792c86d6c3bb41 to your computer and use it in GitHub Desktop.
Save vidarh/91a110792c86d6c3bb41 to your computer and use it in GitHub Desktop.
Trivial Go Init

Inspired by http://git.suckless.org/sinit

Provides basic reaping of child processes for use in e.g. containers. Note however that this code compiles to a substantially larger binary than sinit. Sinit linked statically against Musl is <14KB on my machine, while this code ends up at 2.7MB due to the Go runtime.

If you must have a license, consider it under the MIT/X Consortium license (though I'm happy to confirm relicensing to whatever license works for you).

package main
import (
"log"
"os"
"os/exec"
"syscall"
)
const initcmd string = "/etc/rc.init"
func main() {
go func() {
var wstatus syscall.WaitStatus
for {
_, err := syscall.Wait4(-1, &wstatus, 0, nil)
if err != nil {
log.Println(err)
}
}
}()
os.Chdir("/")
cmd := exec.Command(initcmd)
cmd.Args = os.Args[1:]
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Run()
if err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment