From 0fa444ae747593ea72f0b17374e18827ba83524b Mon Sep 17 00:00:00 2001 From: lilleman Date: Sat, 3 Feb 2024 10:40:54 +0100 Subject: [PATCH] Running all commands in the same context --- main.go | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index d69027c..dcbcc3c 100644 --- a/main.go +++ b/main.go @@ -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