-
Notifications
You must be signed in to change notification settings - Fork 51
/
processmon.go
387 lines (317 loc) · 9.63 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Package processmon is to manage and monitor remote enforcers.
package processmon
import (
"bufio"
"errors"
"fmt"
"io"
"os"
"os/exec"
"path/filepath"
"strconv"
"syscall"
"time"
"go.uber.org/zap"
"github.com/aporeto-inc/trireme-lib/controller/constants"
"github.com/aporeto-inc/trireme-lib/controller/internal/enforcer/utils/rpcwrapper"
"github.com/aporeto-inc/trireme-lib/controller/pkg/remoteenforcer"
"github.com/aporeto-inc/trireme-lib/utils/cache"
"github.com/aporeto-inc/trireme-lib/utils/crypto"
"github.com/kardianos/osext"
)
var (
// launcher supports only a global processMon instance
launcher *processMon
)
const (
// netNSPath holds the directory to ensure ip netns command works
netNSPath = "/var/run/netns/"
processMonitorCacheName = "ProcessMonitorCache"
secretLength = 32
)
// processMon is an instance of processMonitor
type processMon struct {
// netNSPath made configurable to enable running tests
netNSPath string
activeProcesses *cache.Cache
childExitStatus chan exitStatus
// logToConsole stores if we should log to console.
logToConsole bool
// logWithID is the ID for for log files if logging to file.
logWithID bool
// logLevel is the level of logs for remote command.
logLevel string
logFormat string
}
// processInfo stores per process information
type processInfo struct {
contextID string
RPCHdl rpcwrapper.RPCClient
process *os.Process
}
// exitStatus captures the exit status of a process
type exitStatus struct {
process int
// The contextID is optional and is primarily used by remote enforcer
// processes to represent the namespace in which the process was running
contextID string
exitStatus error
}
func init() {
// Setup new launcher
newProcessMon(netNSPath)
}
// contextID2SocketPath returns the socket path to use for a givent context
func contextID2SocketPath(contextID string) string {
if contextID == "" {
panic("contextID is empty")
}
return filepath.Join("/var/run/", contextID+".sock")
}
// 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)
}
}
// newProcessMon is a method to create a new processmon
func newProcessMon(netns string) ProcessManager {
launcher = &processMon{
netNSPath: netns,
activeProcesses: cache.NewCache(processMonitorCacheName),
childExitStatus: make(chan exitStatus, 100),
}
go launcher.collectChildExitStatus()
return launcher
}
// GetProcessManagerHdl returns a process manager handle.
func GetProcessManagerHdl() ProcessManager {
return launcher
}
// collectChildExitStatus is an async function which collects status for all launched child processes
func (p *processMon) collectChildExitStatus() {
for {
es := <-p.childExitStatus
zap.L().Debug("Remote enforcer exited",
zap.String("nativeContextID", es.contextID),
zap.Int("pid", es.process),
zap.Error(es.exitStatus),
)
}
}
// SetLogParameters setups args that should be propagated to child processes
func (p *processMon) SetLogParameters(logToConsole, logWithID bool, logLevel string, logFormat string) {
p.logToConsole = logToConsole
p.logWithID = logWithID
p.logLevel = logLevel
p.logFormat = logFormat
}
// 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
}
procInfo, ok := s.(*processInfo)
if !ok {
return
}
req := &rpcwrapper.Request{}
resp := &rpcwrapper.Response{}
req.Payload = procInfo.process.Pid
c := make(chan error, 1)
go func() {
c <- procInfo.RPCHdl.RemoteCall(contextID, remoteenforcer.EnforcerExit, req, resp)
}()
select {
case err := <-c:
if err != nil {
zap.L().Debug("Failed to stop gracefully",
zap.String("Remote error", err.Error()))
}
if err := procInfo.process.Kill(); err != nil {
zap.L().Debug("Process is already dead",
zap.String("Kill error", err.Error()))
}
case <-time.After(5 * time.Second):
if err := procInfo.process.Kill(); err != nil {
zap.L().Info("Time out while killing process ",
zap.Error(err))
}
}
procInfo.RPCHdl.DestroyRPCClient(contextID)
if err := os.Remove(filepath.Join(p.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))
}
}
// pollStdOutAndErr polls std out and err
func (p *processMon) pollStdOutAndErr(
cmd *exec.Cmd,
exited chan int,
contextID string,
) (initializedCount int, err error) {
stdout, err := cmd.StdoutPipe()
if err != nil {
return initializedCount, err
}
initializedCount++
stderr, err := cmd.StderrPipe()
if err != nil {
return initializedCount, err
}
initializedCount++
// Stdout/err processing
go processIOReader(stdout, contextID, exited)
go processIOReader(stderr, contextID, exited)
return initializedCount, nil
}
// getLaunchProcessCmd returns the command used to launch the enforcerd
func (p *processMon) getLaunchProcessCmd(arg string) (*exec.Cmd, error) {
cmdName, err := osext.Executable()
if err != nil {
return nil, err
}
cmdArgs := []string{arg}
zap.L().Debug("Enforcer executed",
zap.String("command", cmdName),
zap.Strings("args", cmdArgs),
)
return exec.Command(cmdName, cmdArgs...), nil
}
// getLaunchProcessEnvVars returns a slice of env variable strings where each string is in the form of key=value
func (p *processMon) getLaunchProcessEnvVars(
procMountPoint string,
contextID string,
randomkeystring string,
statsServerSecret string,
refPid int,
refNSPath string,
) []string {
newEnvVars := []string{
constants.EnvMountPoint + "=" + procMountPoint,
constants.EnvContextSocket + "=" + contextID2SocketPath(contextID),
constants.EnvStatsChannel + "=" + rpcwrapper.StatsChannel,
constants.EnvRPCClientSecret + "=" + randomkeystring,
constants.EnvStatsSecret + "=" + statsServerSecret,
constants.EnvContainerPID + "=" + strconv.Itoa(refPid),
constants.EnvLogLevel + "=" + p.logLevel,
constants.EnvLogFormat + "=" + p.logFormat,
}
if p.logToConsole {
newEnvVars = append(newEnvVars, constants.EnvLogToConsole+"="+constants.EnvLogToConsoleEnable)
}
if p.logWithID {
newEnvVars = append(newEnvVars, constants.EnvLogID+"="+contextID)
}
// If the PURuntime Specified a NSPath, then it is added as a new env var also.
if refNSPath != "" {
newEnvVars = append(newEnvVars, constants.EnvNSPath+"="+refNSPath)
}
return newEnvVars
}
// LaunchProcess prepares the environment and launches the process
func (p *processMon) LaunchProcess(
contextID string,
refPid int,
refNSPath string,
rpchdl rpcwrapper.RPCClient,
arg string,
statsServerSecret string,
procMountPoint string,
) error {
if _, err := p.activeProcesses.Get(contextID); err == nil {
return nil
}
// 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.
nsPath := refNSPath
if refNSPath == "" {
nsPath = filepath.Join(procMountPoint, strconv.Itoa(refPid), "ns/net")
}
hoststat, err := os.Stat(filepath.Join(procMountPoint, "1/ns/net"))
if err != nil {
return err
}
pidstat, err := os.Stat(nsPath)
if err != nil {
return fmt.Errorf("container pid %d not found: %s", refPid, err)
}
if pidstat.Sys().(*syscall.Stat_t).Ino == hoststat.Sys().(*syscall.Stat_t).Ino {
return errors.New("refused to launch a remote enforcer in host namespace")
}
if _, err = os.Stat(p.netNSPath); err != nil {
err = os.MkdirAll(p.netNSPath, os.ModeDir)
if err != nil {
zap.L().Warn("could not create directory", zap.Error(err))
}
}
// A symlink is created from /var/run/netns/<context> to the NetNSPath
contextFile := filepath.Join(p.netNSPath, contextID)
if _, err = os.Stat(contextFile); err != nil {
if err = os.Symlink(nsPath, contextFile); err != nil {
zap.L().Warn("Failed to create symlink for use by ip netns", zap.Error(err))
}
}
cmd, err := p.getLaunchProcessCmd(arg)
if err != nil {
return fmt.Errorf("enforcer binary not found: %s", err)
}
exited := make(chan int, 2)
waitForExitCount := 0
if p.logToConsole {
if waitForExitCount, err = p.pollStdOutAndErr(cmd, exited, contextID); err != nil {
return err
}
}
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("unable to generate secret: %s", err)
}
// Start command
newEnvVars := p.getLaunchProcessEnvVars(
procMountPoint,
contextID,
randomkeystring,
statsServerSecret,
refPid,
refNSPath,
)
cmd.Env = append(os.Environ(), newEnvVars...)
if err = cmd.Start(); err != nil {
// Cleanup resources
if err1 := os.Remove(contextFile); err1 != nil {
zap.L().Warn("Failed to clean up netns path", zap.Error(err1))
}
return fmt.Errorf("unable to start enforcer binary: %s", err)
}
go func() {
for i := 0; i < waitForExitCount; i++ {
<-exited
}
status := cmd.Wait()
p.childExitStatus <- exitStatus{
process: cmd.Process.Pid,
contextID: contextID,
exitStatus: status,
}
}()
if err := rpchdl.NewRPCClient(contextID, contextID2SocketPath(contextID), randomkeystring); err != nil {
return err
}
p.activeProcesses.AddOrUpdate(contextID, &processInfo{
contextID: contextID,
process: cmd.Process,
RPCHdl: rpchdl})
return nil
}