From b389d0eb9c390bd218198f5ddf11941765bc621d Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Thu, 6 Jun 2024 18:54:40 +0300 Subject: [PATCH 1/3] fix(app): handle daemon process correctly on Unix systems --- internal/app/app.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index 1d63a2c8..ab4b6c94 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -7,6 +7,7 @@ import ( "os/exec" "runtime" "runtime/debug" + "syscall" ) var ( @@ -45,20 +46,23 @@ func Init() { os.Exit(0) } + if os.Getppid() == 1 || syscall.Getppid() == 1 { + daemon = false + } else { + parent, err := os.FindProcess(os.Getppid()) + if err != nil || parent.Pid < 1 { + daemon = false + } + } + if daemon { if runtime.GOOS == "windows" { fmt.Println("Daemon not supported on Windows") os.Exit(1) } - args := os.Args[1:] - for i, arg := range args { - if arg == "-daemon" || arg == "-d" { - args[i] = "" - } - } // Re-run the program in background and exit - cmd := exec.Command(os.Args[0], args...) + cmd := exec.Command(os.Args[0], os.Args[1:]...) if err := cmd.Start(); err != nil { fmt.Println(err) os.Exit(1) From 72d7e8aaaa5bcbaf9e788522c0bd22fb47d44cdb Mon Sep 17 00:00:00 2001 From: Sergey Krashevich Date: Sat, 8 Jun 2024 15:05:26 +0300 Subject: [PATCH 2/3] refactor(app): remove syscall import and improve error messages --- internal/app/app.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index ab4b6c94..cdbb870b 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -7,7 +7,6 @@ import ( "os/exec" "runtime" "runtime/debug" - "syscall" ) var ( @@ -46,10 +45,11 @@ func Init() { os.Exit(0) } - if os.Getppid() == 1 || syscall.Getppid() == 1 { + ppid := os.Getppid() + if ppid == 1 { daemon = false } else { - parent, err := os.FindProcess(os.Getppid()) + parent, err := os.FindProcess(ppid) if err != nil || parent.Pid < 1 { daemon = false } @@ -57,14 +57,14 @@ func Init() { if daemon { if runtime.GOOS == "windows" { - fmt.Println("Daemon not supported on Windows") + fmt.Println("Daemon mode is not supported on Windows") os.Exit(1) } // Re-run the program in background and exit cmd := exec.Command(os.Args[0], os.Args[1:]...) if err := cmd.Start(); err != nil { - fmt.Println(err) + fmt.Println("Failed to start daemon:", err) os.Exit(1) } fmt.Println("Running in daemon mode with PID:", cmd.Process.Pid) From db6745e8ff034552ef87d4a45220428b87823e67 Mon Sep 17 00:00:00 2001 From: Alex X Date: Tue, 18 Jun 2024 20:35:17 +0300 Subject: [PATCH 3/3] Code refactoring after #1168 --- internal/app/app.go | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/internal/app/app.go b/internal/app/app.go index cdbb870b..eb803584 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -45,17 +45,7 @@ func Init() { os.Exit(0) } - ppid := os.Getppid() - if ppid == 1 { - daemon = false - } else { - parent, err := os.FindProcess(ppid) - if err != nil || parent.Pid < 1 { - daemon = false - } - } - - if daemon { + if daemon && os.Getppid() != 1 { if runtime.GOOS == "windows" { fmt.Println("Daemon mode is not supported on Windows") os.Exit(1)