Skip to content

Commit d9f2a24

Browse files
committed
libct: replace runType with hasInit
The semantics of runType is slightly complicated, and the only place where we need to distinguish between Created and Running is refreshState. Replace runType with simpler hasInit, simplifying its users (except the refreshState, which now figures out on its own whether the container is Created or Running). Signed-off-by: Kir Kolyshkin <[email protected]>
1 parent 95a93c1 commit d9f2a24

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

libcontainer/container_linux.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ func (c *Container) ignoreCgroupError(err error) error {
115115
if err == nil {
116116
return nil
117117
}
118-
if errors.Is(err, os.ErrNotExist) && c.runType() == Stopped && !c.cgroupManager.Exists() {
118+
if errors.Is(err, os.ErrNotExist) && !c.hasInit() && !c.cgroupManager.Exists() {
119119
return nil
120120
}
121121
return err
@@ -1006,34 +1006,31 @@ func (c *Container) refreshState() error {
10061006
if paused {
10071007
return c.state.transition(&pausedState{c: c})
10081008
}
1009-
t := c.runType()
1010-
switch t {
1011-
case Created:
1009+
if !c.hasInit() {
1010+
return c.state.transition(&stoppedState{c: c})
1011+
}
1012+
// The presence of exec fifo helps to distinguish between
1013+
// the created and the running states.
1014+
if _, err := os.Stat(filepath.Join(c.stateDir, execFifoFilename)); err == nil {
10121015
return c.state.transition(&createdState{c: c})
1013-
case Running:
1014-
return c.state.transition(&runningState{c: c})
10151016
}
1016-
return c.state.transition(&stoppedState{c: c})
1017+
return c.state.transition(&runningState{c: c})
10171018
}
10181019

1019-
func (c *Container) runType() Status {
1020+
// hasInit tells whether the container init process exists.
1021+
func (c *Container) hasInit() bool {
10201022
if c.initProcess == nil {
1021-
return Stopped
1023+
return false
10221024
}
10231025
pid := c.initProcess.pid()
10241026
stat, err := system.Stat(pid)
10251027
if err != nil {
1026-
return Stopped
1028+
return false
10271029
}
10281030
if stat.StartTime != c.initProcessStartTime || stat.State == system.Zombie || stat.State == system.Dead {
1029-
return Stopped
1030-
}
1031-
// We'll create exec fifo and blocking on it after container is created,
1032-
// and delete it after start container.
1033-
if _, err := os.Stat(filepath.Join(c.stateDir, execFifoFilename)); err == nil {
1034-
return Created
1031+
return false
10351032
}
1036-
return Running
1033+
return true
10371034
}
10381035

10391036
func (c *Container) isPaused() (bool, error) {

libcontainer/state_linux.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (r *runningState) status() Status {
103103
func (r *runningState) transition(s containerState) error {
104104
switch s.(type) {
105105
case *stoppedState:
106-
if r.c.runType() == Running {
106+
if r.c.hasInit() {
107107
return ErrRunning
108108
}
109109
r.c.state = s
@@ -118,7 +118,7 @@ func (r *runningState) transition(s containerState) error {
118118
}
119119

120120
func (r *runningState) destroy() error {
121-
if r.c.runType() == Running {
121+
if r.c.hasInit() {
122122
return ErrRunning
123123
}
124124
return destroy(r.c)
@@ -170,14 +170,13 @@ func (p *pausedState) transition(s containerState) error {
170170
}
171171

172172
func (p *pausedState) destroy() error {
173-
t := p.c.runType()
174-
if t != Running && t != Created {
175-
if err := p.c.cgroupManager.Freeze(configs.Thawed); err != nil {
176-
return err
177-
}
178-
return destroy(p.c)
173+
if p.c.hasInit() {
174+
return ErrPaused
175+
}
176+
if err := p.c.cgroupManager.Freeze(configs.Thawed); err != nil {
177+
return err
179178
}
180-
return ErrPaused
179+
return destroy(p.c)
181180
}
182181

183182
// restoredState is the same as the running state but also has associated checkpoint

0 commit comments

Comments
 (0)