| 1 |
//go:build !windows |
| 2 |
|
| 3 |
/* |
| 4 |
Copyright The containerd Authors. |
| 5 |
|
| 6 |
Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 |
you may not use this file except in compliance with the License. |
| 8 |
You may obtain a copy of the License at |
| 9 |
|
| 10 |
http://www.apache.org/licenses/LICENSE-2.0 |
| 11 |
|
| 12 |
Unless required by applicable law or agreed to in writing, software |
| 13 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 14 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 |
See the License for the specific language governing permissions and |
| 16 |
limitations under the License. |
| 17 |
*/ |
| 18 |
|
| 19 |
package process |
| 20 |
|
| 21 |
import ( |
| 22 |
"context" |
| 23 |
"errors" |
| 24 |
"fmt" |
| 25 |
|
| 26 |
"github.com/containerd/console" |
| 27 |
) |
| 28 |
|
| 29 |
type execState interface { |
| 30 |
Resize(console.WinSize) error |
| 31 |
Start(context.Context) error |
| 32 |
Delete(context.Context) error |
| 33 |
Kill(context.Context, uint32, bool) error |
| 34 |
SetExited(int) |
| 35 |
Status(context.Context) (string, error) |
| 36 |
} |
| 37 |
|
| 38 |
type execCreatedState struct { |
| 39 |
p *execProcess |
| 40 |
} |
| 41 |
|
| 42 |
func (s *execCreatedState) transition(name string) error { |
| 43 |
switch name { |
| 44 |
case "running": |
| 45 |
s.p.execState = &execRunningState{p: s.p} |
| 46 |
case "stopped": |
| 47 |
s.p.execState = &execStoppedState{p: s.p} |
| 48 |
case "deleted": |
| 49 |
s.p.execState = &deletedState{} |
| 50 |
default: |
| 51 |
return fmt.Errorf("invalid state transition %q to %q", stateName(s), name) |
| 52 |
} |
| 53 |
return nil |
| 54 |
} |
| 55 |
|
| 56 |
func (s *execCreatedState) Resize(ws console.WinSize) error { |
| 57 |
return s.p.resize(ws) |
| 58 |
} |
| 59 |
|
| 60 |
func (s *execCreatedState) Start(ctx context.Context) error { |
| 61 |
if err := s.p.start(ctx); err != nil { |
| 62 |
return err |
| 63 |
} |
| 64 |
return s.transition("running") |
| 65 |
} |
| 66 |
|
| 67 |
func (s *execCreatedState) Delete(ctx context.Context) error { |
| 68 |
if err := s.p.delete(ctx); err != nil { |
| 69 |
return err |
| 70 |
} |
| 71 |
|
| 72 |
return s.transition("deleted") |
| 73 |
} |
| 74 |
|
| 75 |
func (s *execCreatedState) Kill(ctx context.Context, sig uint32, all bool) error { |
| 76 |
return s.p.kill(ctx, sig, all) |
| 77 |
} |
| 78 |
|
| 79 |
func (s *execCreatedState) SetExited(status int) { |
| 80 |
s.p.setExited(status) |
| 81 |
|
| 82 |
if err := s.transition("stopped"); err != nil { |
| 83 |
panic(err) |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func (s *execCreatedState) Status(ctx context.Context) (string, error) { |
| 88 |
return "created", nil |
| 89 |
} |
| 90 |
|
| 91 |
type execRunningState struct { |
| 92 |
p *execProcess |
| 93 |
} |
| 94 |
|
| 95 |
func (s *execRunningState) transition(name string) error { |
| 96 |
switch name { |
| 97 |
case "stopped": |
| 98 |
s.p.execState = &execStoppedState{p: s.p} |
| 99 |
default: |
| 100 |
return fmt.Errorf("invalid state transition %q to %q", stateName(s), name) |
| 101 |
} |
| 102 |
return nil |
| 103 |
} |
| 104 |
|
| 105 |
func (s *execRunningState) Resize(ws console.WinSize) error { |
| 106 |
return s.p.resize(ws) |
| 107 |
} |
| 108 |
|
| 109 |
func (s *execRunningState) Start(ctx context.Context) error { |
| 110 |
return errors.New("cannot start a running process") |
| 111 |
} |
| 112 |
|
| 113 |
func (s *execRunningState) Delete(ctx context.Context) error { |
| 114 |
return errors.New("cannot delete a running process") |
| 115 |
} |
| 116 |
|
| 117 |
func (s *execRunningState) Kill(ctx context.Context, sig uint32, all bool) error { |
| 118 |
return s.p.kill(ctx, sig, all) |
| 119 |
} |
| 120 |
|
| 121 |
func (s *execRunningState) SetExited(status int) { |
| 122 |
s.p.setExited(status) |
| 123 |
|
| 124 |
if err := s.transition("stopped"); err != nil { |
| 125 |
panic(err) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func (s *execRunningState) Status(ctx context.Context) (string, error) { |
| 130 |
return "running", nil |
| 131 |
} |
| 132 |
|
| 133 |
type execStoppedState struct { |
| 134 |
p *execProcess |
| 135 |
} |
| 136 |
|
| 137 |
func (s *execStoppedState) transition(name string) error { |
| 138 |
switch name { |
| 139 |
case "deleted": |
| 140 |
s.p.execState = &deletedState{} |
| 141 |
default: |
| 142 |
return fmt.Errorf("invalid state transition %q to %q", stateName(s), name) |
| 143 |
} |
| 144 |
return nil |
| 145 |
} |
| 146 |
|
| 147 |
func (s *execStoppedState) Resize(ws console.WinSize) error { |
| 148 |
return errors.New("cannot resize a stopped container") |
| 149 |
} |
| 150 |
|
| 151 |
func (s *execStoppedState) Start(ctx context.Context) error { |
| 152 |
return errors.New("cannot start a stopped process") |
| 153 |
} |
| 154 |
|
| 155 |
func (s *execStoppedState) Delete(ctx context.Context) error { |
| 156 |
if err := s.p.delete(ctx); err != nil { |
| 157 |
return err |
| 158 |
} |
| 159 |
|
| 160 |
return s.transition("deleted") |
| 161 |
} |
| 162 |
|
| 163 |
func (s *execStoppedState) Kill(ctx context.Context, sig uint32, all bool) error { |
| 164 |
return s.p.kill(ctx, sig, all) |
| 165 |
} |
| 166 |
|
| 167 |
func (s *execStoppedState) SetExited(status int) { |
| 168 |
// no op |
| 169 |
} |
| 170 |
|
| 171 |
func (s *execStoppedState) Status(ctx context.Context) (string, error) { |
| 172 |
return "stopped", nil |
| 173 |
} |