libgocryptfs/daemonize.go

51 lines
1.2 KiB
Go
Raw Normal View History

2015-10-11 18:02:48 +02:00
package main
import (
"fmt"
2015-10-11 18:51:56 +02:00
"os"
"os/exec"
2015-10-11 18:02:48 +02:00
"os/signal"
2015-10-11 18:51:56 +02:00
"syscall"
"github.com/rfjakob/gocryptfs/internal/tlog"
2015-10-11 18:02:48 +02:00
)
// The child sends us USR1 if the mount was successful
func exitOnUsr1() {
2015-10-11 18:02:48 +02:00
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGUSR1)
<-c
os.Exit(0)
}
// forkChild - execute ourselves once again, this time with the "-f" flag, and
// wait for SIGUSR1 or child exit.
// This is a workaround for the missing true fork function in Go.
func forkChild() int {
go exitOnUsr1()
2015-10-11 18:02:48 +02:00
name := os.Args[0]
newArgs := []string{"-f", fmt.Sprintf("-notifypid=%d", os.Getpid())}
2015-10-11 18:02:48 +02:00
newArgs = append(newArgs, os.Args[1:]...)
c := exec.Command(name, newArgs...)
c.Stdout = os.Stdout
c.Stderr = os.Stderr
c.Stdin = os.Stdin
err := c.Start()
if err != nil {
tlog.Fatal.Printf("forkChild: starting %s failed: %v\n", name, err)
return 1
2015-10-11 18:02:48 +02:00
}
err = c.Wait()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if waitstat, ok := exiterr.Sys().(syscall.WaitStatus); ok {
2015-10-11 18:51:56 +02:00
os.Exit(waitstat.ExitStatus())
}
2015-10-11 18:02:48 +02:00
}
tlog.Fatal.Printf("forkChild: wait returned an unknown error: %v\n", err)
return 1
2015-10-11 18:02:48 +02:00
}
// The child exited with 0 - let's do the same.
return 0
2015-10-11 18:02:48 +02:00
}