-
Notifications
You must be signed in to change notification settings - Fork 8
/
command_wrapper.go
189 lines (168 loc) · 4.04 KB
/
command_wrapper.go
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
/*
* Copyright (C) 2020-2022 Arm Limited or its affiliates and Contributors. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package subprocess
import (
"context"
"fmt"
"os/exec"
"time"
"github.com/sasha-s/go-deadlock"
"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 fmt.Errorf("%w:undefined command", commonerrors.ErrUndefined)
}
return ConvertCommandError(c.cmd.Start())
}
func (c *cmdWrapper) Run() error {
c.mu.RLock()
defer c.mu.RUnlock()
if c.cmd == nil {
return fmt.Errorf("%w:undefined command", commonerrors.ErrUndefined)
}
return ConvertCommandError(c.cmd.Run())
}
func (c *cmdWrapper) Stop() error {
c.mu.RLock()
defer c.mu.RUnlock()
if c.cmd == nil {
return fmt.Errorf("%w:undefined command", commonerrors.ErrUndefined)
}
subprocess := c.cmd.Process
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
if subprocess != nil {
pid := subprocess.Pid
parallelisation.ScheduleAfter(ctx, 10*time.Millisecond, func(time.Time) {
process, err := proc.FindProcess(ctx, pid)
if process == nil || err != nil {
return
}
_ = process.KillWithChildren(ctx)
})
}
_ = c.cmd.Wait()
return nil
}
func (c *cmdWrapper) Pid() (pid int, err error) {
c.mu.RLock()
defer c.mu.RUnlock()
if c.cmd == nil {
err = fmt.Errorf("%w:undefined command", commonerrors.ErrUndefined)
return
}
subprocess := c.cmd.Process
if subprocess == nil {
err = fmt.Errorf("%w:undefined subprocess", commonerrors.ErrUndefined)
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
}
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
cmd.Stdout = newOutStreamer(cmdCtx, c.loggers)
cmd.Stderr = newErrLogStreamer(cmdCtx, c.loggers)
cmd.Env = cmd.Environ()
cmd.Env = append(cmd.Env, c.env...)
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 = fmt.Errorf("missing command: %w", commonerrors.ErrUndefined)
return
}
if c.as == nil {
err = fmt.Errorf("missing command translator: %w", commonerrors.ErrUndefined)
return
}
if c.loggers == nil {
err = commonerrors.ErrNoLogger
return
}
return
}
func newCommand(loggers logs.Loggers, as *commandUtils.CommandAsDifferentUser, env []string, cmd string, args ...string) (osCmd *command) {
osCmd = &command{
cmd: cmd,
args: args,
env: env,
as: as,
loggers: loggers,
cmdWrapper: cmdWrapper{},
}
return
}
func ConvertCommandError(err error) error {
return proc.ConvertProcessError(err)
}
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
} else {
p, subErr := proc.FindProcess(ctx, thisP.Pid)
if subErr != nil {
err = subErr
return
}
err = p.KillWithChildren(ctx)
}
return
}