-
Notifications
You must be signed in to change notification settings - Fork 1
/
system_linux.go
95 lines (78 loc) · 2.42 KB
/
system_linux.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// +build linux
package pixelproxy
import (
"context"
"os/exec"
"syscall"
"github.com/danjacques/pixelproxy/util"
"github.com/danjacques/pixelproxy/util/logging"
"github.com/pkg/errors"
)
func getSudoShutdownCommands() (sudo, shutdown string, err error) {
if sudo, err = exec.LookPath("sudo"); err != nil {
err = errors.Wrap(err, "could not find 'sudo'")
return
}
if shutdown, err = exec.LookPath("shutdown"); err != nil {
err = errors.Wrap(err, "could not find 'shutdown'")
return
}
return
}
// DefaultSystemControl implements SystemControl, returning an error for each
// command.
var DefaultSystemControl = &SystemControl{
ValidateAccess: func(c context.Context) error {
sudo, shutdown, err := getSudoShutdownCommands()
if err != nil {
return err
}
err = runCommand(c, false, sudo, "--non-interactive", "--list", "--", shutdown)
if err != nil {
return errors.Wrap(err, "user does not have permission")
}
return nil
},
Shutdown: func(c context.Context) error {
sudo, shutdown, err := getSudoShutdownCommands()
if err != nil {
return err
}
return runCommand(c, true, sudo, "--non-interactive", "--", shutdown, "--poweroff", "now")
},
Restart: func(c context.Context) error {
sudo, shutdown, err := getSudoShutdownCommands()
if err != nil {
return err
}
err = runCommand(c, true, sudo, "--non-interactive", "--", shutdown, "--reboot", "now")
if err != nil {
return errors.Wrap(err, "user does not have permission")
}
return nil
},
}
func runCommand(c context.Context, realCommand bool, name string, args ...string) error {
logInfo, logError := logging.S(c).Infof, logging.S(c).Errorf
if !realCommand {
// If this is a probe command, don't log significance.
logError, logInfo = logInfo, logging.S(c).Debugf
}
argSlice := &util.StringSlice{S: args, Delim: " "}
logInfo("Running system command: %s %s", name, argSlice)
cmd := exec.CommandContext(c, name, args...)
output, err := cmd.CombinedOutput()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
logError("Command (%s %s) failed with exit code %d and output:\n%s",
name, argSlice, status.ExitStatus(), output)
return err
}
}
logError("Command (%s %s) failed to execute, output:\n%s", name, argSlice, output)
return err
}
logInfo("Command (%s %s) finished successfully with output:\n%s", name, argSlice, output)
return nil
}