Debian Codegraph @

cmd/containerd-shim-runc-v2/process/init_state.go

raw
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 google_protobuf "github.com/containerd/containerd/v2/pkg/protobuf/types"
27 runc "github.com/containerd/go-runc"
28 "github.com/containerd/log"
29 )
30
31 type initState interface {
32 Start(context.Context) error
33 Delete(context.Context) error
34 Pause(context.Context) error
35 Resume(context.Context) error
36 Update(context.Context, *google_protobuf.Any) error
37 Checkpoint(context.Context, *CheckpointConfig) error
38 Exec(context.Context, string, *ExecConfig) (Process, error)
39 Kill(context.Context, uint32, bool) error
40 SetExited(int)
41 Status(context.Context) (string, error)
42 }
43
44 type createdState struct {
45 p *Init
46 }
47
48 func (s *createdState) transition(name string) error {
49 switch name {
50 case "running":
51 s.p.initState = &runningState{p: s.p}
52 case "stopped":
53 s.p.initState = &stoppedState{p: s.p}
54 case "deleted":
55 s.p.initState = &deletedState{}
56 default:
57 return fmt.Errorf("invalid state transition %q to %q", stateName(s), name)
58 }
59 return nil
60 }
61
62 func (s *createdState) Pause(ctx context.Context) error {
63 return errors.New("cannot pause task in created state")
64 }
65
66 func (s *createdState) Resume(ctx context.Context) error {
67 return errors.New("cannot resume task in created state")
68 }
69
70 func (s *createdState) Update(ctx context.Context, r *google_protobuf.Any) error {
71 return s.p.update(ctx, r)
72 }
73
74 func (s *createdState) Checkpoint(ctx context.Context, r *CheckpointConfig) error {
75 return errors.New("cannot checkpoint a task in created state")
76 }
77
78 func (s *createdState) Start(ctx context.Context) error {
79 if err := s.p.start(ctx); err != nil {
80 return err
81 }
82 return s.transition("running")
83 }
84
85 func (s *createdState) Delete(ctx context.Context) error {
86 if err := s.p.delete(ctx); err != nil {
87 return err
88 }
89 return s.transition("deleted")
90 }
91
92 func (s *createdState) Kill(ctx context.Context, sig uint32, all bool) error {
93 return s.p.kill(ctx, sig, all)
94 }
95
96 func (s *createdState) SetExited(status int) {
97 s.p.setExited(status)
98
99 if err := s.transition("stopped"); err != nil {
100 panic(err)
101 }
102 }
103
104 func (s *createdState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
105 return s.p.exec(ctx, path, r)
106 }
107
108 func (s *createdState) Status(ctx context.Context) (string, error) {
109 return "created", nil
110 }
111
112 type createdCheckpointState struct {
113 p *Init
114 opts *runc.RestoreOpts
115 }
116
117 func (s *createdCheckpointState) transition(name string) error {
118 switch name {
119 case "running":
120 s.p.initState = &runningState{p: s.p}
121 case "stopped":
122 s.p.initState = &stoppedState{p: s.p}
123 case "deleted":
124 s.p.initState = &deletedState{}
125 default:
126 return fmt.Errorf("invalid state transition %q to %q", stateName(s), name)
127 }
128 return nil
129 }
130
131 func (s *createdCheckpointState) Pause(ctx context.Context) error {
132 return errors.New("cannot pause task in created state")
133 }
134
135 func (s *createdCheckpointState) Resume(ctx context.Context) error {
136 return errors.New("cannot resume task in created state")
137 }
138
139 func (s *createdCheckpointState) Update(ctx context.Context, r *google_protobuf.Any) error {
140 return s.p.update(ctx, r)
141 }
142
143 func (s *createdCheckpointState) Checkpoint(ctx context.Context, r *CheckpointConfig) error {
144 return errors.New("cannot checkpoint a task in created state")
145 }
146
147 func (s *createdCheckpointState) Start(ctx context.Context) error {
148 p := s.p
149 sio := p.stdio
150
151 var (
152 err error
153 socket *runc.Socket
154 )
155 if sio.Terminal {
156 if socket, err = runc.NewTempConsoleSocket(); err != nil {
157 return fmt.Errorf("failed to create OCI runtime console socket: %w", err)
158 }
159 defer socket.Close()
160 s.opts.ConsoleSocket = socket
161 }
162
163 if _, err := s.p.runtime.Restore(ctx, p.id, p.Bundle, s.opts); err != nil {
164 return p.runtimeError(err, "OCI runtime restore failed")
165 }
166 if sio.Stdin != "" {
167 if err := p.openStdin(sio.Stdin); err != nil {
168 return fmt.Errorf("failed to open stdin fifo %s: %w", sio.Stdin, err)
169 }
170 }
171 if socket != nil {
172 console, err := socket.ReceiveMaster()
173 if err != nil {
174 return fmt.Errorf("failed to retrieve console master: %w", err)
175 }
176 console, err = p.Platform.CopyConsole(ctx, console, p.id, sio.Stdin, sio.Stdout, sio.Stderr, &p.wg)
177 if err != nil {
178 return fmt.Errorf("failed to start console copy: %w", err)
179 }
180 p.console = console
181 } else {
182 if err := p.io.Copy(ctx, &p.wg); err != nil {
183 return fmt.Errorf("failed to start io pipe copy: %w", err)
184 }
185 }
186 pid, err := runc.ReadPidFile(s.opts.PidFile)
187 if err != nil {
188 return fmt.Errorf("failed to retrieve OCI runtime container pid: %w", err)
189 }
190 p.pid = pid
191 return s.transition("running")
192 }
193
194 func (s *createdCheckpointState) Delete(ctx context.Context) error {
195 if err := s.p.delete(ctx); err != nil {
196 return err
197 }
198 return s.transition("deleted")
199 }
200
201 func (s *createdCheckpointState) Kill(ctx context.Context, sig uint32, all bool) error {
202 return s.p.kill(ctx, sig, all)
203 }
204
205 func (s *createdCheckpointState) SetExited(status int) {
206 s.p.setExited(status)
207
208 if err := s.transition("stopped"); err != nil {
209 panic(err)
210 }
211 }
212
213 func (s *createdCheckpointState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
214 return nil, errors.New("cannot exec in a created state")
215 }
216
217 func (s *createdCheckpointState) Status(ctx context.Context) (string, error) {
218 return "created", nil
219 }
220
221 type runningState struct {
222 p *Init
223 }
224
225 func (s *runningState) transition(name string) error {
226 switch name {
227 case "stopped":
228 s.p.initState = &stoppedState{p: s.p}
229 case "paused":
230 s.p.initState = &pausedState{p: s.p}
231 default:
232 return fmt.Errorf("invalid state transition %q to %q", stateName(s), name)
233 }
234 return nil
235 }
236
237 func (s *runningState) Pause(ctx context.Context) error {
238 s.p.pausing.Store(true)
239 // NOTE "pausing" will be returned in the short window
240 // after `transition("paused")`, before `pausing` is reset
241 // to false. That doesn't break the state machine, just
242 // delays the "paused" state a little bit.
243 defer s.p.pausing.Store(false)
244
245 if err := s.p.runtime.Pause(ctx, s.p.id); err != nil {
246 return s.p.runtimeError(err, "OCI runtime pause failed")
247 }
248
249 return s.transition("paused")
250 }
251
252 func (s *runningState) Resume(ctx context.Context) error {
253 return errors.New("cannot resume a running process")
254 }
255
256 func (s *runningState) Update(ctx context.Context, r *google_protobuf.Any) error {
257 return s.p.update(ctx, r)
258 }
259
260 func (s *runningState) Checkpoint(ctx context.Context, r *CheckpointConfig) error {
261 return s.p.checkpoint(ctx, r)
262 }
263
264 func (s *runningState) Start(ctx context.Context) error {
265 return errors.New("cannot start a running process")
266 }
267
268 func (s *runningState) Delete(ctx context.Context) error {
269 return errors.New("cannot delete a running process")
270 }
271
272 func (s *runningState) Kill(ctx context.Context, sig uint32, all bool) error {
273 return s.p.kill(ctx, sig, all)
274 }
275
276 func (s *runningState) SetExited(status int) {
277 s.p.setExited(status)
278
279 if err := s.transition("stopped"); err != nil {
280 panic(err)
281 }
282 }
283
284 func (s *runningState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
285 return s.p.exec(ctx, path, r)
286 }
287
288 func (s *runningState) Status(ctx context.Context) (string, error) {
289 return "running", nil
290 }
291
292 type pausedState struct {
293 p *Init
294 }
295
296 func (s *pausedState) transition(name string) error {
297 switch name {
298 case "running":
299 s.p.initState = &runningState{p: s.p}
300 case "stopped":
301 s.p.initState = &stoppedState{p: s.p}
302 default:
303 return fmt.Errorf("invalid state transition %q to %q", stateName(s), name)
304 }
305 return nil
306 }
307
308 func (s *pausedState) Pause(ctx context.Context) error {
309 return errors.New("cannot pause a paused container")
310 }
311
312 func (s *pausedState) Resume(ctx context.Context) error {
313 if err := s.p.runtime.Resume(ctx, s.p.id); err != nil {
314 return s.p.runtimeError(err, "OCI runtime resume failed")
315 }
316
317 return s.transition("running")
318 }
319
320 func (s *pausedState) Update(ctx context.Context, r *google_protobuf.Any) error {
321 return s.p.update(ctx, r)
322 }
323
324 func (s *pausedState) Checkpoint(ctx context.Context, r *CheckpointConfig) error {
325 return s.p.checkpoint(ctx, r)
326 }
327
328 func (s *pausedState) Start(ctx context.Context) error {
329 return errors.New("cannot start a paused process")
330 }
331
332 func (s *pausedState) Delete(ctx context.Context) error {
333 return errors.New("cannot delete a paused process")
334 }
335
336 func (s *pausedState) Kill(ctx context.Context, sig uint32, all bool) error {
337 return s.p.kill(ctx, sig, all)
338 }
339
340 func (s *pausedState) SetExited(status int) {
341 s.p.setExited(status)
342
343 if err := s.p.runtime.Resume(context.Background(), s.p.id); err != nil {
344 log.L.WithError(err).Error("resuming exited container from paused state")
345 }
346
347 if err := s.transition("stopped"); err != nil {
348 panic(err)
349 }
350 }
351
352 func (s *pausedState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
353 return nil, errors.New("cannot exec in a paused state")
354 }
355
356 func (s *pausedState) Status(ctx context.Context) (string, error) {
357 return "paused", nil
358 }
359
360 type stoppedState struct {
361 p *Init
362 }
363
364 func (s *stoppedState) transition(name string) error {
365 switch name {
366 case "deleted":
367 s.p.initState = &deletedState{}
368 default:
369 return fmt.Errorf("invalid state transition %q to %q", stateName(s), name)
370 }
371 return nil
372 }
373
374 func (s *stoppedState) Pause(ctx context.Context) error {
375 return errors.New("cannot pause a stopped container")
376 }
377
378 func (s *stoppedState) Resume(ctx context.Context) error {
379 return errors.New("cannot resume a stopped container")
380 }
381
382 func (s *stoppedState) Update(ctx context.Context, r *google_protobuf.Any) error {
383 return errors.New("cannot update a stopped container")
384 }
385
386 func (s *stoppedState) Checkpoint(ctx context.Context, r *CheckpointConfig) error {
387 return errors.New("cannot checkpoint a stopped container")
388 }
389
390 func (s *stoppedState) Start(ctx context.Context) error {
391 return errors.New("cannot start a stopped process")
392 }
393
394 func (s *stoppedState) Delete(ctx context.Context) error {
395 if err := s.p.delete(ctx); err != nil {
396 return err
397 }
398 return s.transition("deleted")
399 }
400
401 func (s *stoppedState) Kill(ctx context.Context, sig uint32, all bool) error {
402 return s.p.kill(ctx, sig, all)
403 }
404
405 func (s *stoppedState) SetExited(status int) {
406 // no op
407 }
408
409 func (s *stoppedState) Exec(ctx context.Context, path string, r *ExecConfig) (Process, error) {
410 return nil, errors.New("cannot exec in a stopped state")
411 }
412
413 func (s *stoppedState) Status(ctx context.Context) (string, error) {
414 return "stopped", nil
415 }