Skip to content

Commit db9aed5

Browse files
committed
Use uint32 as Event's Status type everywhere
Signed-off-by: Kenfe-Mickael Laventure <mickael.laventure@gmail.com>
1 parent 4eb3147 commit db9aed5

File tree

6 files changed

+26
-20
lines changed

6 files changed

+26
-20
lines changed

runtime/process.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Process interface {
3636
ExitFD() int
3737
// ExitStatus returns the exit status of the process or an error if it
3838
// has not exited
39-
ExitStatus() (int, error)
39+
ExitStatus() (uint32, error)
4040
// Spec returns the process spec that created the process
4141
Spec() specs.ProcessSpec
4242
// Signal sends the provided signal to the process
@@ -228,31 +228,31 @@ func (p *process) Resize(w, h int) error {
228228
return err
229229
}
230230

231-
func (p *process) updateExitStatusFile(status int) (int, error) {
231+
func (p *process) updateExitStatusFile(status uint32) (uint32, error) {
232232
p.stateLock.Lock()
233233
p.state = Stopped
234234
p.stateLock.Unlock()
235-
err := ioutil.WriteFile(filepath.Join(p.root, ExitStatusFile), []byte(fmt.Sprintf("%d", status)), 0644)
235+
err := ioutil.WriteFile(filepath.Join(p.root, ExitStatusFile), []byte(fmt.Sprintf("%u", status)), 0644)
236236
return status, err
237237
}
238238

239-
func (p *process) handleSigkilledShim(rst int, rerr error) (int, error) {
239+
func (p *process) handleSigkilledShim(rst uint32, rerr error) (uint32, error) {
240240
if p.cmd == nil || p.cmd.Process == nil {
241241
e := unix.Kill(p.pid, 0)
242242
if e == syscall.ESRCH {
243243
logrus.Warnf("containerd: %s:%s (pid %d) does not exist", p.container.id, p.id, p.pid)
244244
// The process died while containerd was down (probably of
245245
// SIGKILL, but no way to be sure)
246-
return p.updateExitStatusFile(255)
246+
return p.updateExitStatusFile(UnknownStatus)
247247
}
248248

249249
// If it's not the same process, just mark it stopped and set
250-
// the status to 255
250+
// the status to the UnknownStatus value (i.e. 255)
251251
if same, err := p.isSameProcess(); !same {
252252
logrus.Warnf("containerd: %s:%s (pid %d) is not the same process anymore (%v)", p.container.id, p.id, p.pid, err)
253253
// Create the file so we get the exit event generated once monitor kicks in
254254
// without having to go through all this process again
255-
return p.updateExitStatusFile(255)
255+
return p.updateExitStatusFile(UnknownStatus)
256256
}
257257

258258
ppid, err := readProcStatField(p.pid, 4)
@@ -263,7 +263,7 @@ func (p *process) handleSigkilledShim(rst int, rerr error) (int, error) {
263263
logrus.Warnf("containerd: %s:%s shim died, killing associated process", p.container.id, p.id)
264264
unix.Kill(p.pid, syscall.SIGKILL)
265265
if err != nil && err != syscall.ESRCH {
266-
return 255, fmt.Errorf("containerd: unable to SIGKILL %s:%s (pid %v): %v", p.container.id, p.id, p.pid, err)
266+
return UnknownStatus, fmt.Errorf("containerd: unable to SIGKILL %s:%s (pid %v): %v", p.container.id, p.id, p.pid, err)
267267
}
268268

269269
// wait for the process to die
@@ -276,7 +276,7 @@ func (p *process) handleSigkilledShim(rst int, rerr error) (int, error) {
276276
}
277277
// Create the file so we get the exit event generated once monitor kicks in
278278
// without having to go through all this process again
279-
return p.updateExitStatusFile(128 + int(syscall.SIGKILL))
279+
return p.updateExitStatusFile(128 + uint32(syscall.SIGKILL))
280280
}
281281

282282
return rst, rerr
@@ -296,7 +296,7 @@ func (p *process) handleSigkilledShim(rst int, rerr error) (int, error) {
296296
logrus.Debugf("containerd: ExitStatus(container: %s, process: %s): shim was SIGKILL'ed reaping its child with pid %d", p.container.id, p.id, p.pid)
297297

298298
rerr = nil
299-
rst = 128 + int(shimStatus.Signal())
299+
rst = 128 + uint32(shimStatus.Signal())
300300

301301
p.stateLock.Lock()
302302
p.state = Stopped
@@ -306,7 +306,7 @@ func (p *process) handleSigkilledShim(rst int, rerr error) (int, error) {
306306
return rst, rerr
307307
}
308308

309-
func (p *process) ExitStatus() (rst int, rerr error) {
309+
func (p *process) ExitStatus() (rst uint32, rerr error) {
310310
data, err := ioutil.ReadFile(filepath.Join(p.root, ExitStatusFile))
311311
defer func() {
312312
if rerr != nil {
@@ -315,17 +315,19 @@ func (p *process) ExitStatus() (rst int, rerr error) {
315315
}()
316316
if err != nil {
317317
if os.IsNotExist(err) {
318-
return -1, ErrProcessNotExited
318+
return UnknownStatus, ErrProcessNotExited
319319
}
320-
return -1, err
320+
return UnknownStatus, err
321321
}
322322
if len(data) == 0 {
323-
return -1, ErrProcessNotExited
323+
return UnknownStatus, ErrProcessNotExited
324324
}
325325
p.stateLock.Lock()
326326
p.state = Stopped
327327
p.stateLock.Unlock()
328-
return strconv.Atoi(string(data))
328+
329+
i, err := strconv.ParseUint(string(data), 10, 32)
330+
return uint32(i), err
329331
}
330332

331333
func (p *process) Spec() specs.ProcessSpec {

runtime/runtime.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ const (
4747
// StartTimeFile holds the name of the file in which the process
4848
// start time is saved
4949
StartTimeFile = "starttime"
50+
51+
// UnknownStatus is the value returned when a process exit
52+
// status cannot be determined
53+
UnknownStatus = 255
5054
)
5155

5256
// Checkpoint holds information regarding a container checkpoint

supervisor/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
type DeleteTask struct {
1212
baseTask
1313
ID string
14-
Status int
14+
Status uint32
1515
PID string
1616
NoEvent bool
1717
Process runtime.Process

supervisor/exit.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type ExecExitTask struct {
6363
baseTask
6464
ID string
6565
PID string
66-
Status int
66+
Status uint32
6767
Process runtime.Process
6868
}
6969

supervisor/sort_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ func (p *testProcess) ExitFD() int {
4646
return -1
4747
}
4848

49-
func (p *testProcess) ExitStatus() (int, error) {
50-
return -1, nil
49+
func (p *testProcess) ExitStatus() (uint32, error) {
50+
return runtime.UnknownStatus, nil
5151
}
5252

5353
func (p *testProcess) Container() runtime.Container {

supervisor/supervisor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ type Event struct {
186186
Type string `json:"type"`
187187
Timestamp time.Time `json:"timestamp"`
188188
PID string `json:"pid,omitempty"`
189-
Status int `json:"status,omitempty"`
189+
Status uint32 `json:"status,omitempty"`
190190
}
191191

192192
// Events returns an event channel that external consumers can use to receive updates

0 commit comments

Comments
 (0)