Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not hard-fail on process owner check #177

Merged
merged 3 commits into from
Nov 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions internal/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,19 @@ import (
"github.com/oklog/run"
"net/http"
"os"
"os/exec"
"os/signal"
"os/user"
"regexp"
"strconv"
"strings"
"syscall"
"time"
)

func RunDaemon() {
if getProcessOwner() != "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)
} 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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Making it non-fatal makes a lot of sense actually.

}

pers := persistence.NewPersistence(configuration.CurrentConfig.DbPath)
Expand Down Expand Up @@ -322,11 +323,11 @@ func initializeFans(controllers []*hwmon.HwMonController) map[configuration.FanC
return result
}

func getProcessOwner() string {
stdout, err := exec.Command("ps", "-o", "user=", "-p", strconv.Itoa(os.Getpid())).Output()
func getProcessOwner() (string, error) {
currentUser, err := user.Current()
if err != nil {
ui.Fatal("Error checking process owner: %v", err)
os.Exit(1)
return "", err
}
return strings.TrimSpace(string(stdout))

return currentUser.Username, nil
}