Skip to content

Instantly share code, notes, and snippets.

@xiaq
Created January 16, 2017 23:26
Show Gist options
  • Select an option

  • Save xiaq/b33cc92cf126ef2740e7f07aebd6ba63 to your computer and use it in GitHub Desktop.

Select an option

Save xiaq/b33cc92cf126ef2740e7f07aebd6ba63 to your computer and use it in GitHub Desktop.
package main
import (
"log"
"os"
"os/signal"
"syscall"
"github.com/elves/elvish/sys"
)
func main() {
signal.Ignore(syscall.SIGTTOU)
sysattr := syscall.SysProcAttr{Setpgid: true}
attr := syscall.ProcAttr{Env: os.Environ(), Files: []uintptr{0, 1, 2}, Sys: &sysattr}
path := "/bin/sleep"
args := []string{path, "5"}
pid, err := syscall.ForkExec(path, args, &attr)
if err != nil {
log.Fatal(err)
}
log.Println("spawned sleep, pid", pid)
err = sys.Tcsetpgrp(0, pid)
if err != nil {
log.Fatal(err)
}
log.Println("put sleep in fg")
var ws syscall.WaitStatus
_, err = syscall.Wait4(pid, &ws, syscall.WUNTRACED, nil)
if err != nil {
log.Fatal(err)
}
log.Println("sleep terminated with", ws)
err = sys.Tcsetpgrp(0, syscall.Getpgrp())
if err != nil {
log.Fatal(err)
}
log.Println("put my own pgrp back in fg")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment