Skip to content

Commit 5b791ec

Browse files
committed
Add the creating state to runc
Signed-off-by: jianghao65536 <[email protected]>
1 parent cfb643e commit 5b791ec

File tree

4 files changed

+31
-1
lines changed

4 files changed

+31
-1
lines changed

libcontainer/container.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ const (
2222
Paused
2323
// Stopped is the status that denotes the container does not have a created or running process.
2424
Stopped
25+
// Creating is the status that denotes the container is creating.
26+
Creating
2527
)
2628

2729
func (s Status) String() string {
@@ -34,6 +36,8 @@ func (s Status) String() string {
3436
return "paused"
3537
case Stopped:
3638
return "stopped"
39+
case Creating:
40+
return "creating"
3741
default:
3842
return "unknown"
3943
}

libcontainer/container_linux.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,6 @@ func (c *Container) newInitProcess(p *Process, cmd *exec.Cmd, comm *processComm)
628628
},
629629
intelRdtManager: c.intelRdtManager,
630630
}
631-
c.initProcess = init
632631
return init, nil
633632
}
634633

@@ -879,6 +878,9 @@ func (c *Container) currentStatus() (Status, error) {
879878
// out of process we need to verify the container's status based on runtime
880879
// information and not rely on our in process info.
881880
func (c *Container) refreshState() error {
881+
if c.hasInit() && c.initProcess.pid() == -1 {
882+
return c.state.transition(&creatingState{c: c})
883+
}
882884
paused, err := c.isPaused()
883885
if err != nil {
884886
return err

libcontainer/process_linux.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,13 @@ func (p *initProcess) start() (retErr error) {
511511
return fmt.Errorf("unable to start init: %w", err)
512512
}
513513

514+
// SIGKILL may happen at any time, so generate state.json here
515+
_, err = p.container.updateState(nil)
516+
if err != nil {
517+
return fmt.Errorf("unable to store init state before creating cgroup: %w", err)
518+
}
519+
p.container.initProcess = p
520+
514521
defer func() {
515522
if retErr != nil {
516523
// Find out if init is killed by the kernel's OOM killer.

libcontainer/state_linux.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,20 @@ func (n *loadedState) destroy() error {
242242
}
243243
return n.c.state.destroy()
244244
}
245+
246+
type creatingState struct {
247+
c *Container
248+
}
249+
250+
func (i *creatingState) status() Status {
251+
return Creating
252+
}
253+
254+
func (i *creatingState) transition(s containerState) error {
255+
return newStateTransitionError(i, s)
256+
}
257+
258+
func (i *creatingState) destroy() error {
259+
_ = signalAllProcesses(i.c.cgroupManager, unix.SIGKILL)
260+
return destroy(i.c)
261+
}

0 commit comments

Comments
 (0)