From 75d98b26b7acb0023b990a7305a2553c6dbc12d4 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 14 Jun 2017 15:38:45 -0700 Subject: [PATCH] libcontainer: Replace GetProcessStartTime with Stat_t.StartTime And convert the various start-time properties from strings to uint64s. This removes all internal consumers of the deprecated GetProcessStartTime function. Signed-off-by: W. Trevor King --- libcontainer/container.go | 2 +- libcontainer/container_linux.go | 10 +++++----- libcontainer/container_linux_test.go | 10 +++++----- libcontainer/process_linux.go | 12 +++++++----- libcontainer/restored_process.go | 12 ++++++------ 5 files changed, 24 insertions(+), 22 deletions(-) diff --git a/libcontainer/container.go b/libcontainer/container.go index 3ddb5ec62..2e31b4d4f 100644 --- a/libcontainer/container.go +++ b/libcontainer/container.go @@ -54,7 +54,7 @@ type BaseState struct { InitProcessPid int `json:"init_process_pid"` // InitProcessStartTime is the init process start time in clock cycles since boot time. - InitProcessStartTime string `json:"init_process_start"` + InitProcessStartTime uint64 `json:"init_process_start"` // Created is the unix timestamp for the creation time of the container in UTC Created time.Time `json:"created"` diff --git a/libcontainer/container_linux.go b/libcontainer/container_linux.go index 79771e41d..130395872 100644 --- a/libcontainer/container_linux.go +++ b/libcontainer/container_linux.go @@ -40,7 +40,7 @@ type linuxContainer struct { cgroupManager cgroups.Manager initArgs []string initProcess parentProcess - initProcessStartTime string + initProcessStartTime uint64 criuPath string m sync.Mutex criuVersion int @@ -1370,11 +1370,11 @@ func (c *linuxContainer) refreshState() error { // and a new process has been created with the same pid, in this case, the // container would already be stopped. func (c *linuxContainer) doesInitProcessExist(initPid int) (bool, error) { - startTime, err := system.GetProcessStartTime(initPid) + stat, err := system.Stat(initPid) if err != nil { - return false, newSystemErrorWithCausef(err, "getting init process %d start time", initPid) + return false, newSystemErrorWithCausef(err, "getting init process %d status", initPid) } - if c.initProcessStartTime != startTime { + if c.initProcessStartTime != stat.StartTime { return false, nil } return true, nil @@ -1427,7 +1427,7 @@ func (c *linuxContainer) isPaused() (bool, error) { func (c *linuxContainer) currentState() (*State, error) { var ( - startTime string + startTime uint64 externalDescriptors []string pid = -1 ) diff --git a/libcontainer/container_linux_test.go b/libcontainer/container_linux_test.go index b7ce552ef..b3b50aa7b 100644 --- a/libcontainer/container_linux_test.go +++ b/libcontainer/container_linux_test.go @@ -52,7 +52,7 @@ func (m *mockCgroupManager) Freeze(state configs.FreezerState) error { type mockProcess struct { _pid int - started string + started uint64 } func (m *mockProcess) terminate() error { @@ -63,7 +63,7 @@ func (m *mockProcess) pid() int { return m._pid } -func (m *mockProcess) startTime() (string, error) { +func (m *mockProcess) startTime() (uint64, error) { return m.started, nil } @@ -150,7 +150,7 @@ func TestGetContainerState(t *testing.T) { }, initProcess: &mockProcess{ _pid: pid, - started: "010", + started: 10, }, cgroupManager: &mockCgroupManager{ pids: []int{1, 2, 3}, @@ -174,8 +174,8 @@ func TestGetContainerState(t *testing.T) { if state.InitProcessPid != pid { t.Fatalf("expected pid %d but received %d", pid, state.InitProcessPid) } - if state.InitProcessStartTime != "010" { - t.Fatalf("expected process start time 010 but received %s", state.InitProcessStartTime) + if state.InitProcessStartTime != 10 { + t.Fatalf("expected process start time 10 but received %d", state.InitProcessStartTime) } paths := state.CgroupPaths if paths == nil { diff --git a/libcontainer/process_linux.go b/libcontainer/process_linux.go index f0f3f8643..171685ccd 100644 --- a/libcontainer/process_linux.go +++ b/libcontainer/process_linux.go @@ -35,7 +35,7 @@ type parentProcess interface { wait() (*os.ProcessState, error) // startTime returns the process start time. - startTime() (string, error) + startTime() (uint64, error) signal(os.Signal) error @@ -55,8 +55,9 @@ type setnsProcess struct { bootstrapData io.Reader } -func (p *setnsProcess) startTime() (string, error) { - return system.GetProcessStartTime(p.pid()) +func (p *setnsProcess) startTime() (uint64, error) { + stat, err := system.Stat(p.pid()) + return stat.StartTime, err } func (p *setnsProcess) signal(sig os.Signal) error { @@ -384,8 +385,9 @@ func (p *initProcess) terminate() error { return err } -func (p *initProcess) startTime() (string, error) { - return system.GetProcessStartTime(p.pid()) +func (p *initProcess) startTime() (uint64, error) { + stat, err := system.Stat(p.pid()) + return stat.StartTime, err } func (p *initProcess) sendConfig() error { diff --git a/libcontainer/restored_process.go b/libcontainer/restored_process.go index a96f4ca5f..408916ad9 100644 --- a/libcontainer/restored_process.go +++ b/libcontainer/restored_process.go @@ -17,20 +17,20 @@ func newRestoredProcess(pid int, fds []string) (*restoredProcess, error) { if err != nil { return nil, err } - started, err := system.GetProcessStartTime(pid) + stat, err := system.Stat(pid) if err != nil { return nil, err } return &restoredProcess{ proc: proc, - processStartTime: started, + processStartTime: stat.StartTime, fds: fds, }, nil } type restoredProcess struct { proc *os.Process - processStartTime string + processStartTime uint64 fds []string } @@ -60,7 +60,7 @@ func (p *restoredProcess) wait() (*os.ProcessState, error) { return st, nil } -func (p *restoredProcess) startTime() (string, error) { +func (p *restoredProcess) startTime() (uint64, error) { return p.processStartTime, nil } @@ -81,7 +81,7 @@ func (p *restoredProcess) setExternalDescriptors(newFds []string) { // a persisted state. type nonChildProcess struct { processPid int - processStartTime string + processStartTime uint64 fds []string } @@ -101,7 +101,7 @@ func (p *nonChildProcess) wait() (*os.ProcessState, error) { return nil, newGenericError(fmt.Errorf("restored process cannot be waited on"), SystemError) } -func (p *nonChildProcess) startTime() (string, error) { +func (p *nonChildProcess) startTime() (uint64, error) { return p.processStartTime, nil }