Skip to content

Commit

Permalink
Add flag to run ipmi-sel with sudo, because syscall.Setuid() does not…
Browse files Browse the repository at this point in the history
… work on Linux
  • Loading branch information
Herbert Fischer committed Dec 4, 2017
1 parent 0f000d2 commit a13e816
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"os/exec"
"path"
"strings"
"syscall"

"github.com/sirupsen/logrus"
logSys "github.com/sirupsen/logrus/hooks/syslog"
Expand All @@ -22,6 +23,7 @@ var (
debug = app.Flag("debug", "if set, enables debug logging").Default("false").Bool()
syslogHook = app.Flag("syslog", "if set, enables logging to syslog").Default("false").Bool()
deadman = app.Flag("deadman", "if set, this program will always print something").Default("false").Bool()
useSudo = app.Flag("sudo", "if set, this program will use sudo to run ipmi-sel").Default("true").Bool()
eventTime = app.Flag("event-time", "if set, the event time will be used instead of current time (Use --no-event-time to set to false)").Default("true").Bool()
ipmiSel = app.Flag("ipmi-sel", "Path of ipmi-sel").Default("/usr/sbin/ipmi-sel").String()
)
Expand All @@ -47,12 +49,23 @@ func main() {
log.SetFlags(log.Lshortfile)
}

if uid := syscall.Getuid(); !*useSudo && uid != 0 {
logrus.Fatal("Needs to be executed with UID 0, but it is running with UID ", uid)
}

hostname, err := os.Hostname()
if err != nil {
logrus.Fatal(err)
}

out, err := exec.Command(*ipmiSel, "--debug", "--output-event-state", "--comma-separated-output", "--no-header-output").Output()
args := []string{"--debug", "--output-event-state", "--comma-separated-output", "--no-header-output"}
cmd := exec.Command(*ipmiSel, args...)
if *useSudo {
args = append([]string{*ipmiSel}, args...)
cmd = exec.Command("sudo", args...)
}

out, err := cmd.Output()
outStr := string(out)
if err != nil {
logrus.Errorf("Got error running `%s`: `%s` : `%s`", *ipmiSel, err, outStr) // purposely do not quit
Expand Down

0 comments on commit a13e816

Please sign in to comment.