fix build

This commit is contained in:
wh1te909 2023-02-27 21:54:48 +00:00
parent 1d00f0ad41
commit 2316271bf9
4 changed files with 49 additions and 45 deletions

View File

@ -167,9 +167,9 @@ func (a *Agent) RunScript(code string, shell string, args []string, timeout int,
code = removeWinNewLines(code)
content := []byte(code)
f, err := createTmpFile()
f, err := createNixTmpFile()
if err != nil {
a.Logger.Errorln("RunScript createTmpFile()", err)
a.Logger.Errorln("RunScript createNixTmpFile()", err)
return "", err.Error(), 85, err
}
defer os.Remove(f.Name())
@ -226,9 +226,9 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
return
}
f, err := createTmpFile()
f, err := createNixTmpFile()
if err != nil {
a.Logger.Errorln("AgentUpdate createTmpFile()", err)
a.Logger.Errorln("AgentUpdate createNixTmpFile()", err)
return
}
defer os.Remove(f.Name())
@ -307,9 +307,9 @@ func (a *Agent) AgentUpdate(url, inno, version string) {
}
func (a *Agent) AgentUninstall(code string) {
f, err := createTmpFile()
f, err := createNixTmpFile()
if err != nil {
a.Logger.Errorln("AgentUninstall createTmpFile():", err)
a.Logger.Errorln("AgentUninstall createNixTmpFile():", err)
return
}
@ -492,6 +492,39 @@ func (a *Agent) GetWMIInfo() map[string]interface{} {
return wmiInfo
}
func tmpNoExec() bool {
var stat syscall.Statfs_t
var noexec bool
tmpdir := os.TempDir()
if err := syscall.Statfs(tmpdir, &stat); err == nil {
if stat.Flags&syscall.MS_NOEXEC != 0 {
noexec = true
}
}
return noexec
}
func createNixTmpFile() (*os.File, error) {
var f *os.File
noexec := tmpNoExec()
f, err := os.CreateTemp("", "trmm")
if err != nil || noexec {
if noexec {
os.Remove(f.Name())
}
cwd, err := os.Getwd()
if err != nil {
return f, err
}
f, err = os.CreateTemp(cwd, "trmm")
if err != nil {
return f, err
}
}
return f, nil
}
// windows only below TODO add into stub file
func (a *Agent) GetAgentCheckInConfig(ret AgentCheckInConfig) AgentCheckInConfig {
return ret

View File

@ -906,3 +906,12 @@ func (a *Agent) GetAgentCheckInConfig(ret AgentCheckInConfig) AgentCheckInConfig
func (a *Agent) NixMeshNodeID() string {
return "not implemented"
}
func tmpNoExec() bool {
return false
}
func createNixTmpFile() (*os.File, error) {
var f *os.File
return f, nil
}

View File

@ -147,7 +147,7 @@ func (a *Agent) Install(i *Installer) {
case "windows":
meshOutput = filepath.Join(a.ProgramDir, a.MeshInstaller)
case "darwin":
tmp, err := createTmpFile()
tmp, err := createNixTmpFile()
if err != nil {
a.Logger.Fatalln("Failed to create mesh temp file", err)
}

View File

@ -26,7 +26,6 @@ import (
goDebug "runtime/debug"
"strconv"
"strings"
"syscall"
"time"
ps "github.com/elastic/go-sysinfo"
@ -310,26 +309,6 @@ func removeWinNewLines(s string) string {
return strings.ReplaceAll(s, "\r\n", "\n")
}
func createTmpFile() (*os.File, error) {
var f *os.File
noexec := tmpNoExec()
f, err := os.CreateTemp("", "trmm")
if err != nil || noexec {
if noexec {
os.Remove(f.Name())
}
cwd, err := os.Getwd()
if err != nil {
return f, err
}
f, err = os.CreateTemp(cwd, "trmm")
if err != nil {
return f, err
}
}
return f, nil
}
func stringInSlice(a string, list []string) bool {
for _, b := range list {
if b == a {
@ -361,20 +340,3 @@ func getCMDExe() string {
}
return cmdExe
}
func tmpNoExec() bool {
if runtime.GOOS == "windows" {
return false
}
var stat syscall.Statfs_t
var noexec bool
tmpdir := os.TempDir()
if err := syscall.Statfs(tmpdir, &stat); err == nil {
if stat.Flags&syscall.MS_NOEXEC != 0 {
noexec = true
}
}
return noexec
}