2020-05-06 01:06:46 +00:00
|
|
|
// Copyright 2018 The containerd Authors.
|
|
|
|
// Copyright 2018 The gVisor Authors.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-12-04 09:55:39 +00:00
|
|
|
|
|
|
|
package proc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-05-06 05:00:14 +00:00
|
|
|
"fmt"
|
2018-12-04 09:55:39 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/errdefs"
|
2020-07-13 23:10:58 +00:00
|
|
|
"github.com/containerd/containerd/pkg/process"
|
2021-06-22 00:16:19 +00:00
|
|
|
runc "github.com/containerd/go-runc"
|
|
|
|
"golang.org/x/sys/unix"
|
2021-06-29 17:53:10 +00:00
|
|
|
"gvisor.dev/gvisor/pkg/shim/utils"
|
2018-12-04 09:55:39 +00:00
|
|
|
)
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
type stateTransition int
|
|
|
|
|
|
|
|
const (
|
|
|
|
running stateTransition = iota
|
|
|
|
stopped
|
|
|
|
deleted
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s stateTransition) String() string {
|
|
|
|
switch s {
|
|
|
|
case running:
|
|
|
|
return "running"
|
|
|
|
case stopped:
|
|
|
|
return "stopped"
|
|
|
|
case deleted:
|
|
|
|
return "deleted"
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown state: %d", s))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 09:55:39 +00:00
|
|
|
type initState interface {
|
2019-01-30 17:50:10 +00:00
|
|
|
Start(context.Context) error
|
|
|
|
Delete(context.Context) error
|
2020-07-13 23:10:58 +00:00
|
|
|
Exec(context.Context, string, *ExecConfig) (process.Process, error)
|
2021-06-22 00:16:19 +00:00
|
|
|
State(ctx context.Context) (string, error)
|
|
|
|
Stats(context.Context, string) (*runc.Stats, error)
|
2019-01-30 17:50:10 +00:00
|
|
|
Kill(context.Context, uint32, bool) error
|
|
|
|
SetExited(int)
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type createdState struct {
|
|
|
|
p *Init
|
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *createdState) name() string {
|
|
|
|
return "created"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *createdState) transition(transition stateTransition) {
|
|
|
|
switch transition {
|
|
|
|
case running:
|
2018-12-04 09:55:39 +00:00
|
|
|
s.p.initState = &runningState{p: s.p}
|
2021-06-22 00:16:19 +00:00
|
|
|
case stopped:
|
|
|
|
s.p.initState = &stoppedState{process: s.p}
|
|
|
|
case deleted:
|
2018-12-04 09:55:39 +00:00
|
|
|
s.p.initState = &deletedState{}
|
|
|
|
default:
|
2021-06-22 00:16:19 +00:00
|
|
|
panic(fmt.Sprintf("invalid state transition %q to %q", s.name(), transition))
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *createdState) Start(ctx context.Context) error {
|
|
|
|
if err := s.p.start(ctx); err != nil {
|
|
|
|
// Containerd doesn't allow deleting container in created state.
|
|
|
|
// However, for gvisor, a non-root container in created state can
|
|
|
|
// only go to running state. If the container can't be started,
|
|
|
|
// it can only stay in created state, and never be deleted.
|
|
|
|
// To work around that, we treat non-root container in start failure
|
|
|
|
// state as stopped.
|
|
|
|
if !s.p.Sandbox {
|
|
|
|
s.p.io.Close()
|
|
|
|
s.p.setExited(internalErrorCode)
|
2021-06-22 00:16:19 +00:00
|
|
|
s.transition(stopped)
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2021-06-22 00:16:19 +00:00
|
|
|
s.transition(running)
|
|
|
|
return nil
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *createdState) Delete(ctx context.Context) error {
|
|
|
|
if err := s.p.delete(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-22 00:16:19 +00:00
|
|
|
s.transition(deleted)
|
|
|
|
return nil
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *createdState) Kill(ctx context.Context, sig uint32, all bool) error {
|
|
|
|
return s.p.kill(ctx, sig, all)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *createdState) SetExited(status int) {
|
|
|
|
s.p.setExited(status)
|
2021-06-22 00:16:19 +00:00
|
|
|
s.transition(stopped)
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 23:10:58 +00:00
|
|
|
func (s *createdState) Exec(ctx context.Context, path string, r *ExecConfig) (process.Process, error) {
|
2020-11-13 03:09:43 +00:00
|
|
|
return s.p.exec(path, r)
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *createdState) State(ctx context.Context) (string, error) {
|
|
|
|
state, err := s.p.state(ctx)
|
|
|
|
if err == nil && state == statusStopped {
|
|
|
|
s.transition(stopped)
|
|
|
|
}
|
|
|
|
return state, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *createdState) Stats(ctx context.Context, id string) (*runc.Stats, error) {
|
|
|
|
return s.p.stats(ctx, id)
|
|
|
|
}
|
|
|
|
|
2018-12-04 09:55:39 +00:00
|
|
|
type runningState struct {
|
|
|
|
p *Init
|
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *runningState) name() string {
|
|
|
|
return "running"
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *runningState) transition(transition stateTransition) {
|
|
|
|
switch transition {
|
|
|
|
case stopped:
|
|
|
|
s.p.initState = &stoppedState{process: s.p}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid state transition %q to %q", s.name(), transition))
|
|
|
|
}
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runningState) Start(ctx context.Context) error {
|
2021-06-22 00:16:19 +00:00
|
|
|
return fmt.Errorf("cannot start a running container")
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runningState) Delete(ctx context.Context) error {
|
2021-06-22 00:16:19 +00:00
|
|
|
return fmt.Errorf("cannot delete a running container")
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runningState) Kill(ctx context.Context, sig uint32, all bool) error {
|
|
|
|
return s.p.kill(ctx, sig, all)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runningState) SetExited(status int) {
|
|
|
|
s.p.setExited(status)
|
2021-06-22 00:16:19 +00:00
|
|
|
s.transition(stopped)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *runningState) Exec(_ context.Context, path string, r *ExecConfig) (process.Process, error) {
|
|
|
|
return s.p.exec(path, r)
|
|
|
|
}
|
2018-12-04 09:55:39 +00:00
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *runningState) State(ctx context.Context) (string, error) {
|
|
|
|
state, err := s.p.state(ctx)
|
|
|
|
if err == nil && state == "stopped" {
|
|
|
|
s.transition(stopped)
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
2021-06-22 00:16:19 +00:00
|
|
|
return state, err
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *runningState) Stats(ctx context.Context, id string) (*runc.Stats, error) {
|
|
|
|
return s.p.stats(ctx, id)
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type stoppedState struct {
|
2021-06-22 00:16:19 +00:00
|
|
|
process *Init
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *stoppedState) name() string {
|
|
|
|
return "stopped"
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *stoppedState) transition(transition stateTransition) {
|
|
|
|
switch transition {
|
|
|
|
case deleted:
|
|
|
|
s.process.initState = &deletedState{}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid state transition %q to %q", s.name(), transition))
|
|
|
|
}
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *stoppedState) Start(context.Context) error {
|
|
|
|
return fmt.Errorf("cannot start a stopped container")
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stoppedState) Delete(ctx context.Context) error {
|
2021-06-22 00:16:19 +00:00
|
|
|
if err := s.process.delete(ctx); err != nil {
|
2018-12-04 09:55:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-06-22 00:16:19 +00:00
|
|
|
s.transition(deleted)
|
|
|
|
return nil
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *stoppedState) Kill(_ context.Context, signal uint32, _ bool) error {
|
|
|
|
return handleStoppedKill(signal)
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stoppedState) SetExited(status int) {
|
2021-06-22 00:16:19 +00:00
|
|
|
s.process.setExited(status)
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
2021-06-22 00:16:19 +00:00
|
|
|
func (s *stoppedState) Exec(context.Context, string, *ExecConfig) (process.Process, error) {
|
2020-05-06 05:00:14 +00:00
|
|
|
return nil, fmt.Errorf("cannot exec in a stopped state")
|
2018-12-04 09:55:39 +00:00
|
|
|
}
|
2021-06-22 00:16:19 +00:00
|
|
|
|
|
|
|
func (s *stoppedState) State(context.Context) (string, error) {
|
|
|
|
return "stopped", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *stoppedState) Stats(context.Context, string) (*runc.Stats, error) {
|
|
|
|
return nil, fmt.Errorf("cannot stat a stopped container")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleStoppedKill(signal uint32) error {
|
|
|
|
switch unix.Signal(signal) {
|
|
|
|
case unix.SIGTERM, unix.SIGKILL:
|
|
|
|
// Container is already stopped, so everything inside the container has
|
|
|
|
// already been killed.
|
|
|
|
return nil
|
|
|
|
default:
|
2021-06-29 17:53:10 +00:00
|
|
|
return utils.ErrToGRPCf(errdefs.ErrNotFound, "process not found")
|
2021-06-22 00:16:19 +00:00
|
|
|
}
|
|
|
|
}
|