Skip to content

Commit bbc2a99

Browse files
committed
use state machine management for exec.Pid()
Signed-off-by: Lifubang <[email protected]>
1 parent 32aa0cd commit bbc2a99

3 files changed

Lines changed: 25 additions & 2 deletions

File tree

runtime/v1/linux/proc/deleted_state.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,7 @@ func (s *deletedState) SetExited(status int) {
6969
func (s *deletedState) Exec(ctx context.Context, path string, r *ExecConfig) (proc.Process, error) {
7070
return nil, errors.Errorf("cannot exec in a deleted state")
7171
}
72+
73+
func (s *deletedState) Pid() int {
74+
return -1
75+
}

runtime/v1/linux/proc/exec.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ func (e *execProcess) ID() string {
6969
}
7070

7171
func (e *execProcess) Pid() int {
72-
e.mu.Lock()
73-
defer e.mu.Unlock()
72+
return e.execState.Pid()
73+
}
74+
75+
func (e *execProcess) pidv() int {
7476
return e.pid
7577
}
7678

runtime/v1/linux/proc/exec_state.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type execState interface {
3131
Delete(context.Context) error
3232
Kill(context.Context, uint32, bool) error
3333
SetExited(int)
34+
Pid() int
3435
}
3536

3637
type execCreatedState struct {
@@ -82,6 +83,12 @@ func (s *execCreatedState) SetExited(status int) {
8283
}
8384
}
8485

86+
func (s *execCreatedState) Pid() int {
87+
s.p.mu.Lock()
88+
defer s.p.mu.Unlock()
89+
return s.p.pidv()
90+
}
91+
8592
type execRunningState struct {
8693
p *execProcess
8794
}
@@ -120,6 +127,12 @@ func (s *execRunningState) SetExited(status int) {
120127
}
121128
}
122129

130+
func (s *execRunningState) Pid() int {
131+
s.p.mu.Lock()
132+
defer s.p.mu.Unlock()
133+
return s.p.pidv()
134+
}
135+
123136
type execStoppedState struct {
124137
p *execProcess
125138
}
@@ -157,3 +170,7 @@ func (s *execStoppedState) Kill(ctx context.Context, sig uint32, all bool) error
157170
func (s *execStoppedState) SetExited(status int) {
158171
// no op
159172
}
173+
174+
func (s *execStoppedState) Pid() int {
175+
return s.p.pidv()
176+
}

0 commit comments

Comments
 (0)