Skip to content

Instantly share code, notes, and snippets.

@thinkhy
Created October 12, 2016 04:49
Show Gist options
  • Save thinkhy/141ffc353ab2fd162c5db3c01f83c1cc to your computer and use it in GitHub Desktop.
Save thinkhy/141ffc353ab2fd162c5db3c01f83c1cc to your computer and use it in GitHub Desktop.
fork.go
package main
import (
"syscall"
"os"
"time"
"fmt"
)
func daemon (nochdir, noclose int) int {
var ret uintptr
//var err uintptr
ret,ret2,e := syscall.Syscall(syscall.SYS_FORK, 0, 0, 0)
if e != 0 { return -1 }
fmt.Printf("Ret %v %v \n", ret, ret2)
time.Sleep(10*time.Second)
switch (ret) {
case 0:
break
default:
fmt.Printf("Ret %v %v \n", ret, ret2)
os.Exit (0)
}
_, r := syscall.Setsid ()
if r != nil { return -1 }
if (nochdir == 0) { os.Chdir("/") }
if noclose == 0 {
f, e := os.Open ("/dev/null")
if e == nil {
fd := f.Fd ()
syscall.Dup2 (int(fd), (int)(os.Stdin.Fd ()))
syscall.Dup2 (int(fd), (int)(os.Stdout.Fd ()))
syscall.Dup2 (int(fd), (int)(os.Stderr.Fd ()))
}
}
return 0
}
func main() {
daemon(0, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment