-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcommand_wrapper.go
More file actions
232 lines (204 loc) · 5.54 KB
/
Copy pathcommand_wrapper.go
File metadata and controls
232 lines (204 loc) · 5.54 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package subprocess
import (
"context"
"os/exec"
"time"
"github.com/sasha-s/go-deadlock"
"go.uber.org/atomic"
"github.com/ARM-software/golang-utils/utils/commonerrors"
"github.com/ARM-software/golang-utils/utils/logs"
"github.com/ARM-software/golang-utils/utils/parallelisation"
"github.com/ARM-software/golang-utils/utils/proc"
commandUtils "github.com/ARM-software/golang-utils/utils/subprocess/command"
)
// INTERNAL
// wrapper over an exec cmd.
type cmdWrapper struct {
mu deadlock.RWMutex
cmd *exec.Cmd
}
func (c *cmdWrapper) Set(cmd *exec.Cmd) {
c.mu.Lock()
defer c.mu.Unlock()
if c.cmd == nil {
c.cmd = cmd
}
}
func (c *cmdWrapper) Reset() {
c.mu.Lock()
defer c.mu.Unlock()
c.cmd = nil
}
func (c *cmdWrapper) Start() error {
c.mu.RLock()
defer c.mu.RUnlock()
if c.cmd == nil {
return commonerrors.UndefinedVariable("command")
}
return ConvertCommandError(c.cmd.Start())
}
func (c *cmdWrapper) Run() error {
c.mu.RLock()
defer c.mu.RUnlock()
if c.cmd == nil {
return commonerrors.UndefinedVariable("command")
}
return ConvertCommandError(c.cmd.Run())
}
func (c *cmdWrapper) interruptWithContext(ctx context.Context, interrupt proc.InterruptType) error {
c.mu.RLock()
defer c.mu.RUnlock()
if c.cmd == nil {
return commonerrors.UndefinedVariable("command")
}
subprocess := c.cmd.Process
ctx, cancel := context.WithCancel(ctx)
defer cancel()
stopErr := atomic.NewError(nil)
if subprocess != nil {
pid := subprocess.Pid
parallelisation.ScheduleAfter(ctx, proc.SubprocessTerminationGracePeriod, func(time.Time) {
process, sErr := proc.FindProcess(ctx, pid)
if process == nil || sErr != nil {
return
}
sErr = proc.InterruptProcess(ctx, pid, interrupt)
if commonerrors.Any(sErr, commonerrors.ErrInvalid, commonerrors.ErrCancelled, commonerrors.ErrTimeout) {
stopErr.Store(sErr)
}
})
}
err := parallelisation.WaitWithContextAndError(ctx, c.cmd)
if commonerrors.Any(err, commonerrors.ErrCancelled, commonerrors.ErrTimeout) {
return err
}
return stopErr.Load()
}
func (c *cmdWrapper) interrupt(interrupt proc.InterruptType) error {
return c.interruptWithContext(context.Background(), interrupt)
}
func (c *cmdWrapper) Stop() error {
return c.interrupt(proc.SigKill)
}
func (c *cmdWrapper) Interrupt(ctx context.Context) error {
return c.interruptWithContext(ctx, proc.SigInt)
}
func (c *cmdWrapper) Pid() (pid int, err error) {
c.mu.RLock()
defer c.mu.RUnlock()
if c.cmd == nil {
err = commonerrors.UndefinedVariable("command")
return
}
subprocess := c.cmd.Process
if subprocess == nil {
err = commonerrors.UndefinedVariable("subprocess")
return
}
pid = subprocess.Pid
return
}
// Definition of a command
type command struct {
cmd string
args []string
env []string
as *commandUtils.CommandAsDifferentUser
loggers logs.Loggers
cmdWrapper cmdWrapper
io ICommandIO
dir string
}
func (c *command) createCommand(cmdCtx context.Context) *exec.Cmd {
newCmd, newArgs := c.as.Redefine(c.cmd, c.args...)
cmd := exec.CommandContext(cmdCtx, newCmd, newArgs...) //nolint:gosec
cancellableCmd, err := proc.DefineCmdCancel(cmd)
if err == nil {
cmd = cancellableCmd
}
cmd.Stdin, cmd.Stdout, cmd.Stderr = c.io.Register(cmdCtx)
cmd.Env = cmd.Environ()
cmd.Env = append(cmd.Env, c.env...)
cmd.Dir = c.dir
// for any of our wait checks to work we need to set the group ID to the pid, otherwise the
// group ID will be the code that launched it (i.e. the code that calls exec.Cmd.Start). This
// causes issues in any checks for running processes as the parent PID (the go program) will
// be the one the waited applies to, not the sub process that was created.
proc.SetGroupAttrToCmd(cmd)
return cmd
}
func (c *command) GetPath() string {
return c.cmd
}
func (c *command) GetCmd(cmdCtx context.Context) *cmdWrapper {
c.cmdWrapper.Set(c.createCommand(cmdCtx))
return &c.cmdWrapper
}
func (c *command) Reset() {
c.cmdWrapper.Reset()
}
func (c *command) Check() (err error) {
if c.cmd == "" {
err = commonerrors.UndefinedVariable("command")
return
}
if c.as == nil {
err = commonerrors.UndefinedVariable("command translator")
return
}
if c.loggers == nil {
err = commonerrors.ErrNoLogger
return
}
return
}
func newCommand(loggers logs.Loggers, as *commandUtils.CommandAsDifferentUser, env []string, dir string, cmd string, args ...string) (osCmd *command) {
osCmd = &command{
cmd: cmd,
args: args,
env: env,
as: as,
loggers: loggers,
cmdWrapper: cmdWrapper{},
io: NewIOFromLoggers(loggers),
dir: dir,
}
return
}
func newCommandWithCustomIO(loggers logs.Loggers, io ICommandIO, as *commandUtils.CommandAsDifferentUser, env []string, dir string, cmd string, args ...string) (osCmd *command) {
osCmd = &command{
cmd: cmd,
args: args,
env: env,
as: as,
loggers: loggers,
cmdWrapper: cmdWrapper{},
io: io,
dir: dir,
}
return
}
func ConvertCommandError(err error) error {
return proc.ConvertProcessError(err)
}
// CleanKillOfCommand tries to terminate a command gracefully.
func CleanKillOfCommand(ctx context.Context, cmd *exec.Cmd) (err error) {
if cmd == nil {
return
}
defer func() {
if cmd.Process != nil {
_ = cmd.Process.Kill()
}
}()
thisP := cmd.Process
if thisP == nil {
return
}
err = proc.TerminateGracefully(ctx, thisP.Pid, proc.SubprocessTerminationGracePeriod)
return
}