Skip to content

handle runc command context manually #28

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions command_linux.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package runc

import (
"context"
"os/exec"
"syscall"
)

func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
func (r *Runc) command(args ...string) *exec.Cmd {
command := r.Command
if command == "" {
command = DefaultCommand
}
cmd := exec.CommandContext(context, command, append(r.args(), args...)...)
cmd := exec.Command(command, append(r.args(), args...)...)
cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: r.Setpgid,
}
Expand Down
5 changes: 2 additions & 3 deletions command_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@
package runc

import (
"context"
"os/exec"
)

func (r *Runc) command(context context.Context, args ...string) *exec.Cmd {
func (r *Runc) command(args ...string) *exec.Cmd {
command := r.Command
if command == "" {
command = DefaultCommand
}
return exec.CommandContext(context, command, append(r.args(), args...)...)
return exec.Command(command, append(r.args(), args...)...)
}
43 changes: 40 additions & 3 deletions monitor.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
package runc

import (
"context"
"os"
"os/exec"
"syscall"
"time"

"golang.org/x/sys/unix"
)

var Monitor ProcessMonitor = &defaultMonitor{}
var Monitor ProcessMonitor = DefaultMonitor(unix.SIGTERM, 10*time.Second)

type Exit struct {
Timestamp time.Time
Pid int
Status int
Signal os.Signal
}

// ProcessMonitor is an interface for process monitoring
Expand All @@ -22,34 +27,66 @@ type Exit struct {
// These methods should match the methods exposed by exec.Cmd to provide
// a consistent experience for the caller
type ProcessMonitor interface {
Start(*exec.Cmd) (chan Exit, error)
Start(context.Context, *exec.Cmd) (chan Exit, error)
Wait(*exec.Cmd, chan Exit) (int, error)
}

func DefaultMonitor(defaultSignal os.Signal, killTimeout time.Duration) ProcessMonitor {
return &defaultMonitor{
defaultSignal: defaultSignal,
killTimeout: killTimeout,
}
}

type defaultMonitor struct {
defaultSignal os.Signal
killTimeout time.Duration
}

func (m *defaultMonitor) Start(c *exec.Cmd) (chan Exit, error) {
func (m *defaultMonitor) Start(ctx context.Context, c *exec.Cmd) (chan Exit, error) {
if err := c.Start(); err != nil {
return nil, err
}
ec := make(chan Exit, 1)
waitDone := make(chan struct{}, 1)
go func() {
select {
case <-ctx.Done():
if m.defaultSignal == nil {
c.Process.Signal(unix.SIGKILL)
} else {
c.Process.Signal(m.defaultSignal)
if m.killTimeout > 0 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there's no killtimeout set better use a default value, the process may never die otherwise.

select {
case <-time.After(m.killTimeout):
c.Process.Kill()
case <-waitDone:
}
}
}
case <-waitDone:
}
}()
go func() {
var status int
var signal os.Signal
if err := c.Wait(); err != nil {
status = 255
if exitErr, ok := err.(*exec.ExitError); ok {
if ws, ok := exitErr.Sys().(syscall.WaitStatus); ok {
status = ws.ExitStatus()
signal = ws.Signal()
}
}
}
ec <- Exit{
Timestamp: time.Now(),
Pid: c.Process.Pid,
Status: status,
Signal: signal,
}
close(ec)
close(waitDone)
}()
return ec, nil
}
Expand Down
40 changes: 40 additions & 0 deletions monitor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package runc

import (
"context"
"os/exec"
"testing"
"time"

"golang.org/x/sys/unix"
)

func TestMonitorCustomSignal(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
cmd := exec.Command("sleep", "10")
monitor := DefaultMonitor(unix.SIGTERM, time.Second)
ec, err := monitor.Start(ctx, cmd)
if err != nil {
t.Errorf("Failed to start command: %v", err)
}
e := <-ec
if e.Signal != unix.SIGTERM {
t.Errorf("Got signal (%v), expected (%v)", e.Signal, unix.SIGTERM)
}
}

func TestMonitorKill(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
cmd := exec.Command("sleep", "10")
monitor := &defaultMonitor{}
ec, err := monitor.Start(ctx, cmd)
if err != nil {
t.Errorf("Failed to start command: %v", err)
}
e := <-ec
if e.Signal != unix.SIGKILL {
t.Errorf("Got signal (%v), expected (%v)", e.Signal, unix.SIGTERM)
}
}
Loading