From 2a730264aaa3dd237540cfc480a384460bd6b931 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Thu, 18 Jan 2018 17:05:15 -0500 Subject: [PATCH] Add Exec to process states Signed-off-by: Michael Crosby --- linux/proc/deleted_state.go | 4 ++++ linux/proc/init.go | 4 ++-- linux/proc/init_state.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/linux/proc/deleted_state.go b/linux/proc/deleted_state.go index fb2587804..b47ae3830 100644 --- a/linux/proc/deleted_state.go +++ b/linux/proc/deleted_state.go @@ -48,3 +48,7 @@ func (s *deletedState) Kill(ctx context.Context, sig uint32, all bool) error { func (s *deletedState) SetExited(status int) { // no op } + +func (s *deletedState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) { + return nil, errors.Errorf("cannot exec in a deleted state") +} diff --git a/linux/proc/init.go b/linux/proc/init.go index f24f92f7d..29306ce58 100644 --- a/linux/proc/init.go +++ b/linux/proc/init.go @@ -346,8 +346,8 @@ func (p *Init) Runtime() *runc.Runc { return p.runtime } -// Exec returns a new exec'd process -func (p *Init) Exec(context context.Context, path string, r *ExecConfig) (Process, error) { +// exec returns a new exec'd process +func (p *Init) exec(context context.Context, path string, r *ExecConfig) (Process, error) { // process exec request var spec specs.Process if err := json.Unmarshal(r.Spec.Value, &spec); err != nil { diff --git a/linux/proc/init_state.go b/linux/proc/init_state.go index b5b398ec3..945e16fc2 100644 --- a/linux/proc/init_state.go +++ b/linux/proc/init_state.go @@ -22,6 +22,7 @@ type initState interface { Resume(context.Context) error Update(context.Context, *google_protobuf.Any) error Checkpoint(context.Context, *CheckpointConfig) error + Exec(context.Context, string, *ExecConfig) (Process, error) } type createdState struct { @@ -113,6 +114,12 @@ func (s *createdState) SetExited(status int) { } } +func (s *createdState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) { + s.p.mu.Lock() + defer s.p.mu.Unlock() + return s.p.exec(ctx, path, r) +} + type createdCheckpointState struct { p *Init opts *runc.RestoreOpts @@ -227,6 +234,13 @@ func (s *createdCheckpointState) SetExited(status int) { } } +func (s *createdCheckpointState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) { + s.p.mu.Lock() + defer s.p.mu.Unlock() + + return nil, errors.Errorf("cannot exec in a created state") +} + type runningState struct { p *Init } @@ -312,6 +326,12 @@ func (s *runningState) SetExited(status int) { } } +func (s *runningState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) { + s.p.mu.Lock() + defer s.p.mu.Unlock() + return s.p.exec(ctx, path, r) +} + type pausedState struct { p *Init } @@ -396,7 +416,13 @@ func (s *pausedState) SetExited(status int) { if err := s.transition("stopped"); err != nil { panic(err) } +} +func (s *pausedState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) { + s.p.mu.Lock() + defer s.p.mu.Unlock() + + return nil, errors.Errorf("cannot exec in a paused state") } type stoppedState struct { @@ -471,3 +497,10 @@ func (s *stoppedState) Kill(ctx context.Context, sig uint32, all bool) error { func (s *stoppedState) SetExited(status int) { // no op } + +func (s *stoppedState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) { + s.p.mu.Lock() + defer s.p.mu.Unlock() + + return nil, errors.Errorf("cannot exec in a stopped state") +}