-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.go
More file actions
133 lines (111 loc) · 2.75 KB
/
exec.go
File metadata and controls
133 lines (111 loc) · 2.75 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"errors"
"fmt"
"os/exec"
"syscall"
"time"
logger "github.com/sirupsen/logrus"
)
type ExecResult struct {
command string
output string
err error
succeed bool
}
func (c *ExecResult) String() string {
if c.succeed {
return fmt.Sprintf("[Succeed] %s", c.output)
} else {
if c.output != "" {
return fmt.Sprintf("[Failed] %s", c.output)
} else {
return fmt.Sprintf("[Failed] %s", c.err)
}
}
}
func execRealCommand(realCommand *RealCommand) *ExecResult {
if realCommand.command.Type == "shell" {
return execCommand(realCommand.command)
} else {
pcapPath, err := realCommand.pcap.new()
if err != nil {
return errResult(err)
}
f := File{
path: pcapPath,
finder: realCommand.pcap.file.finder,
}
f.parse()
defer f.delete()
pcapContext := &PcapContext{
WorkingDirectory: config.workingDirectory,
FinderDirectory: realCommand.pcap.file.finder.workingDirectory,
PcapDirectory: realCommand.pcap.workingDirectory,
RelativeDirectory: f.relativeDirectory,
RelativePath: f.relativePath,
Path: pcapPath,
BaseName: f.baseName,
Name: f.name,
Ext: f.ext,
HasIpv6: realCommand.pcap.hasIPv6,
PacketCount: realCommand.pcap.info.packetCount,
}
renderedCommand, err := pcapContext.render(realCommand.command)
if err != nil {
return errResult(err)
}
result := execShellCommand(renderedCommand, realCommand.command.Timeout)
return result
}
}
func execCommand(command *Command) *ExecResult {
result := execShellCommand(command.Command, command.Timeout)
return result
}
func execShellCommand(command string, timeout time.Duration) *ExecResult {
cmd := exec.Command(pcapTool.Bash, "-c", command)
sysAttr := &syscall.SysProcAttr{
Setpgid: true,
}
cmd.SysProcAttr = sysAttr
if config.ShowCommand {
logger.Debugln(fmt.Sprintf("executing: %s (with timeout %s)", command, timeout))
}
reaper := time.After(timeout)
processFinished := make(chan struct{})
isTimeout := false
go func() {
select {
case <-reaper:
// timeout
isTimeout = true
// kill process use pkill
_ = syscall.Kill(-cmd.Process.Pid, 9)
case <-processFinished:
}
}()
bytes, err := cmd.CombinedOutput()
close(processFinished)
output := string(bytes)
if isTimeout {
err = errors.New("timeout")
}
if config.ShowCommandStdout {
logger.Debugln(fmt.Sprintf("executing: %s (with timeout %s)\n----------------------- output is: --------------------\n%s", command, timeout, output))
}
return &ExecResult{
command: command,
output: output,
err: err,
succeed: err == nil,
}
}
func errResult(err error) *ExecResult {
return &ExecResult{
command: "",
output: "",
err: err,
succeed: false,
}
}