Skip to content

Commit

Permalink
Merge branch 'hcc-tools' of https://github.com/TianyouLi/PerfSpect in…
Browse files Browse the repository at this point in the history
…to hcc-tools
  • Loading branch information
TianyouLi committed Dec 6, 2024
2 parents 1a1ef4d + 031737b commit 5c97900
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 17 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ replace (

require (
github.com/Knetic/govaluate v3.0.0+incompatible
github.com/deckarep/golang-set/v2 v2.6.0
github.com/deckarep/golang-set/v2 v2.7.0
github.com/spf13/cobra v1.8.1
github.com/spf13/pflag v1.0.5
github.com/xuri/excelize/v2 v2.9.0
golang.org/x/exp v0.0.0-20241004190924-225e2abe05e6
golang.org/x/term v0.26.0
golang.org/x/text v0.20.0
golang.org/x/term v0.27.0
golang.org/x/text v0.21.0
gopkg.in/yaml.v2 v2.4.0
)

Expand All @@ -33,5 +33,5 @@ require (
github.com/xuri/nfp v0.0.0-20240318013403-ab9948c2c4a7 // indirect
golang.org/x/crypto v0.28.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/sys v0.28.0 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/Knetic/govaluate v3.0.0+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8L
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/deckarep/golang-set/v2 v2.7.0 h1:gIloKvD7yH2oip4VLhsv3JyLLFnC0Y2mlusgcvJYW5k=
github.com/deckarep/golang-set/v2 v2.7.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw=
Expand Down Expand Up @@ -37,12 +37,12 @@ golang.org/x/image v0.18.0 h1:jGzIakQa/ZXI1I0Fxvaa9W7yP25TqT6cHIHn+6CqvSQ=
golang.org/x/image v0.18.0/go.mod h1:4yyo5vMFQjVjUcVk4jEQcU9MGy/rulF5WvUILseCM2E=
golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.26.0 h1:WEQa6V3Gja/BhNxg540hBip/kkaYtRg3cxg4oXSw4AU=
golang.org/x/term v0.26.0/go.mod h1:Si5m1o57C5nBNQo5z1iq+XDijt21BDBDp2bK0QI8e3E=
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.27.0 h1:WP60Sv1nlK1T6SupCHbXzSaN0b9wUmsPoRS9b61A23Q=
golang.org/x/term v0.27.0/go.mod h1:iMsnZpn0cago0GOrHO2+Y7u7JPn5AylBrcoWkElMTSM=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
Expand Down
10 changes: 7 additions & 3 deletions internal/script/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func RunScripts(myTarget target.Target, scripts []ScriptDefinition, ignoreScript
continue
}
if script.Superuser && !canElevate {
slog.Info("skipping script because it requires superuser privileges and the target cannot elevate privileges", slog.String("script", script.Name))
slog.Info("skipping script because it requires superuser privileges and the user cannot elevate privileges on target", slog.String("script", script.Name))
continue
}
if script.Sequential {
Expand Down Expand Up @@ -163,8 +163,12 @@ func RunScripts(myTarget target.Target, scripts []ScriptDefinition, ignoreScript
// instigates a known bug in the terminal that corrupts the tty settings:
// https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1043320
var cmd *exec.Cmd
if needsElevatedPrivileges {
// run master script with sudo, "-S" to read password from stdin
if needsElevatedPrivileges && !canElevate {
// this shouldn't happen because we already filtered out the scripts that require elevated privileges if the user cannot elevate privileges on the target
err = fmt.Errorf("master script requires elevated privileges but the user cannot elevate privileges on target")
return nil, err
} else if needsElevatedPrivileges && !myTarget.IsSuperUser() {
// run master script with sudo, "-S" to read password from stdin. Note: password won't be asked for if password-less sudo is configured.
cmd = exec.Command("sudo", "-S", "bash", path.Join(myTarget.GetTempDirectory(), masterScriptName))
} else {
cmd = exec.Command("bash", path.Join(myTarget.GetTempDirectory(), masterScriptName))
Expand Down
18 changes: 16 additions & 2 deletions internal/target/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type Target interface {
// It returns true if the user can elevate privileges, false otherwise.
CanElevatePrivileges() bool

// IsSuperUser checks if the current user is a superuser.
// It returns true if the user is a superuser, false otherwise.
IsSuperUser() bool

// GetArchitecture returns the architecture of the target system.
// It returns a string representing the architecture and any error that occurred.
GetArchitecture() (arch string, err error)
Expand Down Expand Up @@ -370,7 +374,7 @@ func (t *LocalTarget) CanElevatePrivileges() bool {
if t.canElevate != 0 {
return t.canElevate == 1
}
if os.Geteuid() == 0 {
if t.IsSuperUser() {
t.canElevate = 1
return true // user is root
}
Expand Down Expand Up @@ -406,7 +410,7 @@ func (t *RemoteTarget) CanElevatePrivileges() bool {
if t.canElevate != 0 {
return t.canElevate == 1
}
if t.user == "root" {
if t.IsSuperUser() {
t.canElevate = 1
return true
}
Expand All @@ -420,6 +424,16 @@ func (t *RemoteTarget) CanElevatePrivileges() bool {
return false
}

// IsSuperUser checks if the current user is a superuser.
// It returns true if the user is a superuser, false otherwise.
func (t *LocalTarget) IsSuperUser() bool {
return os.Geteuid() == 0
}

func (t *RemoteTarget) IsSuperUser() bool {
return t.user == "root"
}

// InstallLkms installs the specified LKMs (Loadable Kernel Modules) on the target.
// It returns the list of installed LKMs and any error encountered during the installation process.
func (t *LocalTarget) InstallLkms(lkms []string) (installedLkms []string, err error) {
Expand Down

0 comments on commit 5c97900

Please sign in to comment.