@@ -195,7 +195,6 @@ func (c *linuxContainer) Start(process *Process) error {
195195 }
196196 // generate a timestamp indicating when the container was started
197197 c .created = time .Now ().UTC ()
198-
199198 c .state = & runningState {
200199 c : c ,
201200 }
@@ -1034,31 +1033,47 @@ func (c *linuxContainer) refreshState() error {
10341033 if paused {
10351034 return c .state .transition (& pausedState {c : c })
10361035 }
1037- running , err := c .isRunning ()
1036+ t , err := c .runType ()
10381037 if err != nil {
10391038 return err
10401039 }
1041- if running {
1040+ switch t {
1041+ case Initialized :
1042+ return c .state .transition (& initializedState {c : c })
1043+ case Running :
10421044 return c .state .transition (& runningState {c : c })
10431045 }
10441046 return c .state .transition (& stoppedState {c : c })
10451047}
10461048
1047- func (c * linuxContainer ) isRunning () (bool , error ) {
1049+ func (c * linuxContainer ) runType () (Status , error ) {
10481050 if c .initProcess == nil {
1049- return false , nil
1051+ return Stopped , nil
10501052 }
1053+ pid := c .initProcess .pid ()
10511054 // return Running if the init process is alive
1052- if err := syscall .Kill (c . initProcess . pid () , 0 ); err != nil {
1055+ if err := syscall .Kill (pid , 0 ); err != nil {
10531056 if err == syscall .ESRCH {
10541057 // It means the process does not exist anymore, could happen when the
10551058 // process exited just when we call the function, we should not return
10561059 // error in this case.
1057- return false , nil
1060+ return Stopped , nil
1061+ }
1062+ return Stopped , newSystemErrorWithCausef (err , "sending signal 0 to pid %d" , pid )
1063+ }
1064+ // check if the process that is running is the init process or the user's process.
1065+ // this is the difference between the container Running and Created.
1066+ environ , err := ioutil .ReadFile (fmt .Sprintf ("/proc/%d/environ" , pid ))
1067+ if err != nil {
1068+ return Stopped , newSystemErrorWithCausef (err , "reading /proc/%d/environ" , pid )
1069+ }
1070+ check := []byte ("_LIBCONTAINER" )
1071+ for _ , v := range bytes .Split (environ , []byte ("\x00 " )) {
1072+ if bytes .Contains (v , check ) {
1073+ return Initialized , nil
10581074 }
1059- return false , newSystemErrorWithCausef (err , "sending signal 0 to pid %d" , c .initProcess .pid ())
10601075 }
1061- return true , nil
1076+ return Running , nil
10621077}
10631078
10641079func (c * linuxContainer ) isPaused () (bool , error ) {
0 commit comments