Skip to content
This repository has been archived by the owner on May 12, 2021. It is now read-only.

Commit

Permalink
reaper: implement reaper interface
Browse files Browse the repository at this point in the history
All reaper implementations must override its functions.
This new interface allows the creation of mock reapers that
are useful for unit testing.

Signed-off-by: Julio Montes <julio.montes@intel.com>
  • Loading branch information
Julio Montes committed Apr 3, 2018
1 parent b13dffe commit 8461df3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
9 changes: 5 additions & 4 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ type sandbox struct {
grpcListener net.Listener
sharedPidNs namespace
mounts []string
subreaper *reaper
subreaper reaper
}

type namespace struct {
Expand Down Expand Up @@ -575,16 +575,17 @@ func main() {
os.Exit(exitSuccess)
}()

r := &agentReaper{}
r.init()

// Initialize unique sandbox structure.
s := &sandbox{
containers: make(map[string]*container),
running: false,
// pivot_root won't work for init, see
// Documention/filesystem/ramfs-rootfs-initramfs.txt
noPivotRoot: os.Getpid() == 1,
subreaper: &reaper{
exitCodeChans: make(map[int]chan<- int),
},
subreaper: r,
}

if err = s.initLogger(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ func (a *agentGRPC) execProcess(ctr *container, proc *process, createContainer b
// miss the opportunity to get the exit code, leading WaitProcess() to
// wait forever on the new channel.
// This lock has to be taken before we run the new process.
a.sandbox.subreaper.RLock()
defer a.sandbox.subreaper.RUnlock()
a.sandbox.subreaper.lock()
defer a.sandbox.subreaper.unlock()

if createContainer {
err = ctr.container.Start(&proc.process)
Expand Down
38 changes: 31 additions & 7 deletions reaper.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ import (
grpcStatus "google.golang.org/grpc/status"
)

type reaper struct {
type reaper interface {
init()
getExitCodeCh(pid int) (chan<- int, error)
setExitCodeCh(pid int, exitCodeCh chan<- int)
deleteExitCodeCh(pid int)
reap() error
start(c *exec.Cmd) (<-chan int, error)
wait(exitCodeCh <-chan int, proc waitProcess) (int, error)
lock()
unlock()
}

type agentReaper struct {
sync.RWMutex

chansLock sync.RWMutex
Expand All @@ -33,7 +45,19 @@ func exitStatus(status unix.WaitStatus) int {
return status.ExitStatus()
}

func (r *reaper) getExitCodeCh(pid int) (chan<- int, error) {
func (r *agentReaper) init() {
r.exitCodeChans = make(map[int]chan<- int)
}

func (r *agentReaper) lock() {
r.RLock()
}

func (r *agentReaper) unlock() {
r.RUnlock()
}

func (r *agentReaper) getExitCodeCh(pid int) (chan<- int, error) {
r.chansLock.RLock()
defer r.chansLock.RUnlock()

Expand All @@ -45,21 +69,21 @@ func (r *reaper) getExitCodeCh(pid int) (chan<- int, error) {
return exitCodeCh, nil
}

func (r *reaper) setExitCodeCh(pid int, exitCodeCh chan<- int) {
func (r *agentReaper) setExitCodeCh(pid int, exitCodeCh chan<- int) {
r.chansLock.Lock()
defer r.chansLock.Unlock()

r.exitCodeChans[pid] = exitCodeCh
}

func (r *reaper) deleteExitCodeCh(pid int) {
func (r *agentReaper) deleteExitCodeCh(pid int) {
r.chansLock.Lock()
defer r.chansLock.Unlock()

delete(r.exitCodeChans, pid)
}

func (r *reaper) reap() error {
func (r *agentReaper) reap() error {
var (
ws unix.WaitStatus
rus unix.Rusage
Expand Down Expand Up @@ -119,7 +143,7 @@ func (r *reaper) reap() error {
// start starts the exec command and registers the process to the reaper.
// This function is a helper for exec.Cmd.Start() since this needs to be
// in sync with exec.Cmd.Wait().
func (r *reaper) start(c *exec.Cmd) (<-chan int, error) {
func (r *agentReaper) start(c *exec.Cmd) (<-chan int, error) {
// This lock is very important to avoid any race with reaper.reap().
// We don't want the reaper to reap a process before we have added
// it to the exit code channel list.
Expand All @@ -143,7 +167,7 @@ func (r *reaper) start(c *exec.Cmd) (<-chan int, error) {
// from the subreaper, the exit code is sent through the provided channel.
// This function is a helper for exec.Cmd.Wait() and os.Process.Wait() since
// both cannot be used directly, because of the subreaper.
func (r *reaper) wait(exitCodeCh <-chan int, proc waitProcess) (int, error) {
func (r *agentReaper) wait(exitCodeCh <-chan int, proc waitProcess) (int, error) {
// Wait for the subreaper to receive the SIGCHLD signal. Once it gets
// it, this channel will be notified by receiving the exit code of the
// corresponding process.
Expand Down

0 comments on commit 8461df3

Please sign in to comment.