Skip to content

Commit

Permalink
Merge pull request #177 from zopieux/nonfatal-owner-check
Browse files Browse the repository at this point in the history
Do not hard-fail on process owner check
  • Loading branch information
markusressel authored Nov 6, 2022
2 parents 237502d + 84d4b53 commit ae82dec
Showing 1 changed file with 11 additions and 10 deletions.
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)
}

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
}

0 comments on commit ae82dec

Please sign in to comment.