From f2c727c414ef312bd0bf981c1cbd44dda733bf81 Mon Sep 17 00:00:00 2001 From: Alexandre Macabies <81353+zopieux@users.noreply.github.com> Date: Sat, 5 Nov 2022 18:38:53 +0100 Subject: [PATCH 1/3] Do not hard-fail on process owner check `ps` might be missing in $PATH, or fan2go might be running as a non-root user with write access to the subsystem. I don't think it's reasonable to crash the program for a simple uid check that might actually be wrong. This will eventually crash later when trying to modify paths as non-root anyway. --- internal/backend.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index 95593b9..9f39c07 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -28,7 +28,7 @@ import ( ) func RunDaemon() { - if getProcessOwner() != "root" { + if owner, err := getProcessOwner(); err != nil && owner != "root" { ui.Fatal("Fan control requires root permissions to be able to modify fan speeds, please run fan2go as root") } @@ -322,11 +322,10 @@ func initializeFans(controllers []*hwmon.HwMonController) map[configuration.FanC return result } -func getProcessOwner() string { +func getProcessOwner() (string, error) { stdout, err := exec.Command("ps", "-o", "user=", "-p", strconv.Itoa(os.Getpid())).Output() if err != nil { - ui.Fatal("Error checking process owner: %v", err) - os.Exit(1) + return "", err } - return strings.TrimSpace(string(stdout)) + return strings.TrimSpace(string(stdout)), nil } From 47131e80758a75abf5ee6689587aef792d098f5c Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Sun, 6 Nov 2022 20:13:13 +0100 Subject: [PATCH 2/3] use os.User.Current() instead of executing `ps`, remove Fatal log to allow running fan2go as non-root user --- internal/backend.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index 9f39c07..c8f47db 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -18,18 +18,20 @@ import ( "github.com/oklog/run" "net/http" "os" - "os/exec" "os/signal" + "os/user" "regexp" - "strconv" - "strings" "syscall" "time" ) func RunDaemon() { - if owner, err := getProcessOwner(); err != nil && owner != "root" { - ui.Fatal("Fan control requires root permissions to be able to modify fan speeds, please run fan2go as root") + owner, err := getProcessOwner() + if err != nil { + ui.Warning("Unable to verify process owner: %v", err) + } + if owner != "root" { + ui.Info("fan2go is running as a non-root user '%s'. If you encounter errors, make sure to give this user the required permissions.", owner) } pers := persistence.NewPersistence(configuration.CurrentConfig.DbPath) @@ -323,9 +325,10 @@ func initializeFans(controllers []*hwmon.HwMonController) map[configuration.FanC } func getProcessOwner() (string, error) { - stdout, err := exec.Command("ps", "-o", "user=", "-p", strconv.Itoa(os.Getpid())).Output() + currentUser, err := user.Current() if err != nil { return "", err } - return strings.TrimSpace(string(stdout)), nil + + return currentUser.Username, nil } From 84d4b53d982ef68a14a0214e03469191e62d95ba Mon Sep 17 00:00:00 2001 From: Markus Ressel Date: Sun, 6 Nov 2022 20:15:11 +0100 Subject: [PATCH 3/3] ignore owner if there was an error retrieving it --- internal/backend.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/backend.go b/internal/backend.go index c8f47db..f4bd38b 100644 --- a/internal/backend.go +++ b/internal/backend.go @@ -29,8 +29,7 @@ func RunDaemon() { owner, err := getProcessOwner() if err != nil { ui.Warning("Unable to verify process owner: %v", err) - } - if owner != "root" { + } else if owner != "root" { ui.Info("fan2go is running as a non-root user '%s'. If you encounter errors, make sure to give this user the required permissions.", owner) }