main: forkChild: try to read /proc/self/exe

On Linux, where /proc exists, this makes sure that we are
executing ourselves again, and not some other copy of the
gocryptfs executable.

This usually does not matter, but mount(1) unsets $PATH
and sets argv[0] to just "gocryptfs".
This commit is contained in:
Jakob Unterwurzacher 2018-06-07 23:06:03 +02:00
parent 10212d791a
commit fb772da697
1 changed files with 8 additions and 1 deletions

View File

@ -28,6 +28,13 @@ func exitOnUsr1() {
// This is a workaround for the missing true fork function in Go.
func forkChild() int {
name := os.Args[0]
// Use the full path to our executable if we can get if from /proc.
buf := make([]byte, syscall.PathMax)
n, err := syscall.Readlink("/proc/self/exe", buf)
if err == nil {
name = string(buf[:n])
tlog.Debug.Printf("forkChild: readlink worked: %q", name)
}
newArgs := []string{"-fg", fmt.Sprintf("-notifypid=%d", os.Getpid())}
newArgs = append(newArgs, os.Args[1:]...)
c := exec.Command(name, newArgs...)
@ -35,7 +42,7 @@ func forkChild() int {
c.Stderr = os.Stderr
c.Stdin = os.Stdin
exitOnUsr1()
err := c.Start()
err = c.Start()
if err != nil {
tlog.Fatal.Printf("forkChild: starting %s failed: %v", name, err)
return exitcodes.ForkChild