Running all commands in the same context

This commit is contained in:
Lilleman auf Larv 2024-02-03 10:40:54 +01:00
parent a4a2e15541
commit 0fa444ae74

36
main.go
View File

@ -156,6 +156,7 @@ func runCmd(log go_log.Log, workDir string, cmdStr string, uid int, gid int) (in
}
// Create the command
response += "Running command: " + cmdStr + "\n"
cmd := exec.Command("/bin/sh", "-c", cmdStr)
cmd.Dir = workDir
cmd.SysProcAttr = &syscall.SysProcAttr{}
@ -344,22 +345,25 @@ func main() {
res.Header().Set("Cache-Control", "no-cache")
res.Header().Set("Connection", "keep-alive")
for idx, command := range payload.Commands {
response += "Running command #" + fmt.Sprint(idx) + ": " + command + "\n"
exitCode, cmdRes, err := runCmd(log, workDir, command, uid, gid)
response += cmdRes
if err != nil {
response += "Command failed, err: " + err.Error() + "\n"
res.WriteHeader(400)
res.Write([]byte(response))
return
}
if exitCode != 0 {
response += "Command failed, non-zero exit code: " + fmt.Sprint(exitCode) + "\n"
res.WriteHeader(400)
res.Write([]byte(response))
return
}
allCommands := "true"
for _, command := range payload.Commands {
allCommands += " && " + command
}
exitCode, cmdRes, err := runCmd(log, workDir, allCommands, uid, gid)
response += cmdRes
if err != nil {
response += "Command failed, err: " + err.Error() + "\n"
res.WriteHeader(400)
res.Write([]byte(response))
return
}
if exitCode != 0 {
response += "Command failed, non-zero exit code: " + fmt.Sprint(exitCode) + "\n"
res.WriteHeader(400)
res.Write([]byte(response))
return
}
// If we get here, nothing have broken down, so 200 OK and send the full response