main: doMount: call Setsid before starting logger

The logger should be in the new background session together
with the gocryptfs process.

Before:
	$ xfce4-terminal -x gocryptfs a b
	$ ps xao pid,ppid,pgid,sid,comm,args
	    PID    PPID    PGID     SID COMMAND         COMMAND
	 192272    1371  192272  192272 gocryptfs       /ssd2/jakob.donotbackup/go/bin/gocryptfs -fg -notifypid=192265 a b
	 192292  192272  192265  192265 logge <defunct> [logger] <defunct>

After:
	$ xfce4-terminal -x gocryptfs a b
	$ ps xao pid,ppid,pgid,sid,comm,args
	    PID    PPID    PGID     SID COMMAND         COMMAND
	 211714    1371  211714  211714 gocryptfs       /ssd2/jakob.donotbackup/go/bin/gocryptfs -fg -notifypid=211708 a b
	 211776  211714  211714  211714 logger          logger -t gocryptfs-211714-logger

Fixes https://github.com/rfjakob/gocryptfs/issues/660
This commit is contained in:
Jakob Unterwurzacher 2022-12-29 13:52:35 +01:00
parent 7ee4c8e9c3
commit ff32e99791

View File

@ -120,9 +120,18 @@ func doMount(args *argContainer) {
tlog.Info.Println(tlog.ColorGreen + "Filesystem mounted and ready." + tlog.ColorReset) tlog.Info.Println(tlog.ColorGreen + "Filesystem mounted and ready." + tlog.ColorReset)
// We have been forked into the background, as evidenced by the set // We have been forked into the background, as evidenced by the set
// "notifypid". // "notifypid".
// Do what daemons should do: https://man7.org/linux/man-pages/man7/daemon.7.html
if args.notifypid > 0 { if args.notifypid > 0 {
// Chdir to the root directory so we don't block unmounting the CWD // Chdir to the root directory so we don't block unmounting the CWD
os.Chdir("/") os.Chdir("/")
// Disconnect from the controlling terminal by creating a new session.
// This prevents us from getting SIGINT when the user presses Ctrl-C
// to exit a running script that has called gocryptfs, or SIGHUP when
// xfce4-terminal closes itself ( https://github.com/rfjakob/gocryptfs/issues/660 ).
_, err = syscall.Setsid()
if err != nil {
tlog.Warn.Printf("Setsid: %v", err)
}
// Switch to syslog // Switch to syslog
if !args.nosyslog { if !args.nosyslog {
// Switch all of our logs and the generic logger to syslog // Switch all of our logs and the generic logger to syslog
@ -134,13 +143,6 @@ func doMount(args *argContainer) {
// Daemons should redirect stdin, stdout and stderr // Daemons should redirect stdin, stdout and stderr
redirectStdFds() redirectStdFds()
} }
// Disconnect from the controlling terminal by creating a new session.
// This prevents us from getting SIGINT when the user presses Ctrl-C
// to exit a running script that has called gocryptfs.
_, err = syscall.Setsid()
if err != nil {
tlog.Warn.Printf("Setsid: %v", err)
}
// Send SIGUSR1 to our parent // Send SIGUSR1 to our parent
sendUsr1(args.notifypid) sendUsr1(args.notifypid)
} }