Skip to content

Commit fb772da

Browse files
committed
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".
1 parent 10212d7 commit fb772da

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

daemonize.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,21 @@ func exitOnUsr1() {
2828
// This is a workaround for the missing true fork function in Go.
2929
func forkChild() int {
3030
name := os.Args[0]
31+
// Use the full path to our executable if we can get if from /proc.
32+
buf := make([]byte, syscall.PathMax)
33+
n, err := syscall.Readlink("/proc/self/exe", buf)
34+
if err == nil {
35+
name = string(buf[:n])
36+
tlog.Debug.Printf("forkChild: readlink worked: %q", name)
37+
}
3138
newArgs := []string{"-fg", fmt.Sprintf("-notifypid=%d", os.Getpid())}
3239
newArgs = append(newArgs, os.Args[1:]...)
3340
c := exec.Command(name, newArgs...)
3441
c.Stdout = os.Stdout
3542
c.Stderr = os.Stderr
3643
c.Stdin = os.Stdin
3744
exitOnUsr1()
38-
err := c.Start()
45+
err = c.Start()
3946
if err != nil {
4047
tlog.Fatal.Printf("forkChild: starting %s failed: %v", name, err)
4148
return exitcodes.ForkChild

0 commit comments

Comments
 (0)