forked from microsoft/hcsshim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
92 lines (78 loc) · 2.89 KB
/
process.go
File metadata and controls
92 lines (78 loc) · 2.89 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
//go:build linux
// +build linux
package runc
import (
"github.com/Microsoft/hcsshim/internal/guest/runtime"
"github.com/Microsoft/hcsshim/internal/guest/stdio"
"github.com/Microsoft/hcsshim/internal/logfields"
"github.com/sirupsen/logrus"
)
// process represents a process running in a container. It can either be a
// container's init process, or an exec process in a container.
type process struct {
c *container
pid int
ttyRelay *stdio.TtyRelay
pipeRelay *stdio.PipeRelay
}
var _ runtime.Process = &process{}
func (p *process) Pid() int {
return p.pid
}
func (p *process) Tty() *stdio.TtyRelay {
return p.ttyRelay
}
func (p *process) PipeRelay() *stdio.PipeRelay {
return p.pipeRelay
}
// Delete deletes any state created for the process by either this wrapper or
// runC itself.
func (p *process) Delete() error {
if err := p.c.r.cleanupProcess(p.c.id, p.pid); err != nil {
return err
}
return nil
}
func (p *process) Wait() (int, error) {
exitCode, err := p.c.r.waitOnProcess(p.pid)
l := logrus.WithField(logfields.ContainerID, p.c.id)
l.WithField(logfields.ContainerID, p.pid).Debug("process wait completed")
// If the init process for the container has exited, kill everything else in
// the container. Runc uses the devices cgroup of the container to determine
// what other processes to kill.
//
// We don't issue the kill if the container owns its own pid namespace,
// because in that case the container kernel will kill everything in the pid
// namespace automatically (as the container init will be the pid namespace
// init). This prevents a potential issue where two containers share cgroups
// but have their own pid namespaces. If we didn't handle this case, runc
// would kill the processes in both containers when trying to kill
// either one of them.
if p == p.c.init && !p.c.ownsPidNamespace {
// If the init process of a pid namespace terminates, the kernel
// terminates all other processes in the namespace with SIGKILL. We
// simulate the same behavior.
if err := p.c.killAll(); err != nil {
l.WithError(err).Error("failed to terminate container after process wait")
}
}
// Wait on the relay to drain any output that was already buffered.
//
// At this point, if this is the init process for the container, everything
// else in the container has been killed, so the write ends of the stdio
// relay will have been closed.
//
// If this is a container exec process instead, then it is possible the
// relay waits will hang waiting for the write ends to close. This can occur
// if the exec spawned any child processes that inherited its stdio.
// Currently we do not do anything to avoid hanging in this case, but in the
// future we could add special handling.
if p.ttyRelay != nil {
p.ttyRelay.Wait()
}
if p.pipeRelay != nil {
p.pipeRelay.Wait()
}
l.WithField(logfields.ProcessID, p.pid).Debug("relay wait completed")
return exitCode, err
}