/
processmon.go
343 lines (282 loc) · 8.93 KB
/
processmon.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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Package processmon is to manage and monitor remote enforcers.
// When we access the processmanager interface through here it acts as a singleton
// The ProcessMonitor interface is not a singleton and can be used to monitor a list of processes
package processmon
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/exec"
"strconv"
"syscall"
"time"
"go.uber.org/zap"
"github.com/aporeto-inc/trireme/cache"
"github.com/aporeto-inc/trireme/crypto"
"github.com/aporeto-inc/trireme/enforcer/utils/rpcwrapper"
"github.com/kardianos/osext"
)
var (
// GlobalCommandArgs are command args received while invoking this command
GlobalCommandArgs map[string]interface{}
)
//ProcessMon exported
type ProcessMon struct {
activeProcesses *cache.Cache
}
var launcher *ProcessMon
//ProcessInfo exported
type processInfo struct {
contextID string
RPCHdl rpcwrapper.RPCClient
process *os.Process
deleted bool
}
//ExitStatus captures the exit status of a process
//The contextID is optional and is primarily used by remote enforcer processes
//and represents the namespace in which the process was running
type ExitStatus struct {
process int
contextID string
exitStatus error
}
var childExitStatus = make(chan ExitStatus, 100)
var netnspath string
// ErrEnforcerAlreadyRunning Exported
var ErrEnforcerAlreadyRunning = errors.New("Enforcer already running in this context")
// ErrSymLinkFailed Exported
var ErrSymLinkFailed = errors.New("Failed to create symlink for use by ip netns")
// ErrFailedtoLaunch Exported
var ErrFailedtoLaunch = errors.New("Failed to launch enforcer")
// ErrProcessDoesNotExists Exported
var ErrProcessDoesNotExists = errors.New("Process in that context does not exist")
//ErrBinaryNotFound Exported
var ErrBinaryNotFound = errors.New("Enforcer Binary not found")
func init() {
netnspath = "/var/run/netns/"
go collectChildExitStatus()
}
//collectChildExitStatus is an async function which collects status for all launched child processes
func collectChildExitStatus() {
for {
exitStatus := <-childExitStatus
zap.L().Debug("Remote enforcer exited",
zap.String("nativeContextID", exitStatus.contextID),
zap.Int("pid", exitStatus.process),
zap.Error(exitStatus.exitStatus),
)
}
}
// processIOReader will read from a reader and print it on the calling process
func processIOReader(fd io.Reader, contextID string, exited chan int) {
reader := bufio.NewReader(fd)
for {
str, err := reader.ReadString('\n')
if err != nil {
exited <- 1
return
}
fmt.Print("[" + contextID + "]:" + str)
}
}
//SetnsNetPath -- only planned consumer is unit test
//Call this function if you expect network namespace links to be created in a separate path
func (p *ProcessMon) SetnsNetPath(netpath string) {
netnspath = netpath
}
//GetExitStatus reports if the process is marked for deletion or deleted
func (p *ProcessMon) GetExitStatus(contextID string) bool {
s, err := p.activeProcesses.Get(contextID)
if err != nil {
zap.L().Debug("Process already dead", zap.Error(err))
return true
}
return (s.(*processInfo)).deleted
}
//SetExitStatus marks the process for deletion
func (p *ProcessMon) SetExitStatus(contextID string, status bool) error {
s, err := p.activeProcesses.Get(contextID)
if err != nil {
return err
}
val := s.(*processInfo)
val.deleted = status
p.activeProcesses.AddOrUpdate(contextID, val)
return nil
}
//KillProcess sends a rpc to the process to exit failing which it will kill the process
func (p *ProcessMon) KillProcess(contextID string) {
s, err := p.activeProcesses.Get(contextID)
if err != nil {
zap.L().Debug("Process already killed or never launched")
return
}
req := &rpcwrapper.Request{}
resp := &rpcwrapper.Response{}
req.Payload = s.(*processInfo).process.Pid
c := make(chan error, 1)
go func() {
c <- s.(*processInfo).RPCHdl.RemoteCall(contextID, "Server.EnforcerExit", req, resp)
}()
select {
case kerr := <-c:
if kerr != nil {
zap.L().Debug("Failed to stop gracefully",
zap.String("Remote error", kerr.Error()))
}
if perr := s.(*processInfo).process.Kill(); perr != nil {
zap.L().Debug("Process is already dead",
zap.String("Kill error", perr.Error()))
}
case <-time.After(5 * time.Second):
if perr := s.(*processInfo).process.Kill(); perr != nil {
zap.L().Info("Time out while killing process ",
zap.Error(perr))
}
}
s.(*processInfo).RPCHdl.DestroyRPCClient(contextID)
if err := os.Remove(netnspath + contextID); err != nil {
zap.L().Warn("Failed to remote process from netns path", zap.Error(err))
}
if err := p.activeProcesses.Remove(contextID); err != nil {
zap.L().Warn("Failed to remote process from cache", zap.Error(err))
}
}
//LaunchProcess prepares the environment for the new process and launches the process
func (p *ProcessMon) LaunchProcess(contextID string, refPid int, refNSPath string, rpchdl rpcwrapper.RPCClient, arg string, statsServerSecret string, procMountPoint string) error {
secretLength := 32
var cmdName string
_, err := p.activeProcesses.Get(contextID)
if err == nil {
return nil
}
var nsPath string
// We check if the NetNsPath was given as parameter.
// If it was we will use it. Otherwise we will determine it based on the PID.
if refNSPath == "" {
nsPath = procMountPoint + "/" + strconv.Itoa(refPid) + "/ns/net"
} else {
nsPath = refNSPath
}
pidstat, pidstaterr := os.Stat(nsPath)
hoststat, hoststaterr := os.Stat(procMountPoint + "/1/ns/net")
if pidstaterr == nil && hoststaterr == nil {
if pidstat.Sys().(*syscall.Stat_t).Ino == hoststat.Sys().(*syscall.Stat_t).Ino {
zap.L().Error("Refused to launch a remote enforcer in host namespace")
return nil
}
} else {
zap.L().Error("Cannot determine namespace of new container",
zap.Error(hoststaterr),
zap.Error(pidstaterr),
)
}
_, staterr := os.Stat(netnspath)
if staterr != nil {
mkerr := os.MkdirAll(netnspath, os.ModeDir)
if mkerr != nil {
zap.L().Error("Could not create directory", zap.Error(mkerr))
}
}
// A symlink is created from /var/run/netns/<context> to the NetNSPath
if _, lerr := os.Stat(netnspath + contextID); lerr != nil {
linkErr := os.Symlink(nsPath,
netnspath+contextID)
if linkErr != nil {
zap.L().Error(ErrSymLinkFailed.Error(), zap.Error(linkErr))
}
}
namedPipe := "APORETO_ENV_SOCKET_PATH=/var/run/" + contextID + ".sock"
cmdName, _ = osext.Executable()
cmdArgs := []string{arg}
if _, ok := GlobalCommandArgs["--log-level"]; ok {
cmdArgs = append(cmdArgs, "--log-level")
cmdArgs = append(cmdArgs, GlobalCommandArgs["--log-level"].(string))
}
cmd := exec.Command(cmdName, cmdArgs...)
zap.L().Debug("Enforcer executed",
zap.String("command", cmdName),
zap.Strings("args", cmdArgs),
)
stdout, err := cmd.StdoutPipe()
if err != nil {
return err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return err
}
statsChannel := "STATSCHANNEL_PATH=" + rpcwrapper.StatsChannel
randomkeystring, err := crypto.GenerateRandomString(secretLength)
if err != nil {
//This is a more serious failure. We can't reliably control the remote enforcer
return fmt.Errorf("Failed to generate secret: %s", err.Error())
}
mountPoint := "APORETO_ENV_PROC_MOUNTPOINT=" + procMountPoint
rpcClientSecret := "APORETO_ENV_SECRET=" + randomkeystring
envStatsSecret := "STATS_SECRET=" + statsServerSecret
containerPID := "CONTAINER_PID=" + strconv.Itoa(refPid)
newEnvVars := []string{
mountPoint,
namedPipe,
statsChannel,
rpcClientSecret,
envStatsSecret,
containerPID,
}
// If the PURuntime Specified a NSPath, then it is added as a new env var also.
if refNSPath != "" {
nsPath := "APORETO_ENV_NS_PATH=" + refNSPath
newEnvVars = append(newEnvVars, nsPath)
}
cmd.Env = append(os.Environ(), newEnvVars...)
err = cmd.Start()
if err != nil {
//Cleanup resources
if oerr := os.Remove(netnspath + contextID); oerr != nil {
zap.L().Warn("Failed to clean up netns path",
zap.String("command", cmdName),
zap.Error(err),
)
}
return ErrBinaryNotFound
}
exited := make(chan int, 2)
go func() {
pid := cmd.Process.Pid
i := 0
for i < 2 {
<-exited
i++
}
status := cmd.Wait()
childExitStatus <- ExitStatus{process: pid, contextID: contextID, exitStatus: status}
}()
// Stdout/err processing
go processIOReader(stdout, contextID, exited)
go processIOReader(stderr, contextID, exited)
if err := rpchdl.NewRPCClient(contextID, "/var/run/"+contextID+".sock", randomkeystring); err != nil {
return err
}
p.activeProcesses.AddOrUpdate(contextID, &processInfo{contextID: contextID,
process: cmd.Process,
RPCHdl: rpchdl,
deleted: false})
return nil
}
//NewProcessMon is a method to create a new processmon
func newProcessMon() ProcessManager {
launcher = &ProcessMon{activeProcesses: cache.NewCache()}
return launcher
}
//GetProcessManagerHdl will ensure that we return an existing handle if one has been created.
//or return a new one if there is none
//This needs locks
func GetProcessManagerHdl() ProcessManager {
if launcher == nil {
return newProcessMon()
}
return launcher
}