@@ -189,29 +189,27 @@ func (c *linuxContainer) Start(process *Process) error {
189
189
}
190
190
return newSystemError (err )
191
191
}
192
+ c .state = & runningState {
193
+ c : c ,
194
+ }
192
195
if doInit {
193
196
if err := c .updateState (parent ); err != nil {
194
197
return err
195
198
}
196
- } else {
197
- c .state .transition (& nullState {
198
- c : c ,
199
- s : Running ,
200
- })
201
- }
202
- if c .config .Hooks != nil {
203
- s := configs.HookState {
204
- Version : c .config .Version ,
205
- ID : c .id ,
206
- Pid : parent .pid (),
207
- Root : c .config .Rootfs ,
208
- }
209
- for _ , hook := range c .config .Hooks .Poststart {
210
- if err := hook .Run (s ); err != nil {
211
- if err := parent .terminate (); err != nil {
212
- logrus .Warn (err )
199
+ if c .config .Hooks != nil {
200
+ s := configs.HookState {
201
+ Version : c .config .Version ,
202
+ ID : c .id ,
203
+ Pid : parent .pid (),
204
+ Root : c .config .Rootfs ,
205
+ }
206
+ for _ , hook := range c .config .Hooks .Poststart {
207
+ if err := hook .Run (s ); err != nil {
208
+ if err := parent .terminate (); err != nil {
209
+ logrus .Warn (err )
210
+ }
211
+ return newSystemError (err )
213
212
}
214
- return newSystemError (err )
215
213
}
216
214
}
217
215
}
@@ -340,6 +338,13 @@ func (c *linuxContainer) Destroy() error {
340
338
func (c * linuxContainer ) Pause () error {
341
339
c .m .Lock ()
342
340
defer c .m .Unlock ()
341
+ status , err := c .currentStatus ()
342
+ if err != nil {
343
+ return err
344
+ }
345
+ if status != Running {
346
+ return newGenericError (fmt .Errorf ("container not running" ), ContainerNotRunning )
347
+ }
343
348
if err := c .cgroupManager .Freeze (configs .Frozen ); err != nil {
344
349
return err
345
350
}
@@ -351,6 +356,13 @@ func (c *linuxContainer) Pause() error {
351
356
func (c * linuxContainer ) Resume () error {
352
357
c .m .Lock ()
353
358
defer c .m .Unlock ()
359
+ status , err := c .currentStatus ()
360
+ if err != nil {
361
+ return err
362
+ }
363
+ if status != Paused {
364
+ return newGenericError (fmt .Errorf ("container not paused" ), ContainerNotPaused )
365
+ }
354
366
if err := c .cgroupManager .Freeze (configs .Thawed ); err != nil {
355
367
return err
356
368
}
@@ -939,9 +951,6 @@ func (c *linuxContainer) criuNotifications(resp *criurpc.CriuResp, process *Proc
939
951
940
952
func (c * linuxContainer ) updateState (process parentProcess ) error {
941
953
c .initProcess = process
942
- if err := c .refreshState (); err != nil {
943
- return err
944
- }
945
954
state , err := c .currentState ()
946
955
if err != nil {
947
956
return err
@@ -1017,35 +1026,36 @@ func (c *linuxContainer) isPaused() (bool, error) {
1017
1026
}
1018
1027
1019
1028
func (c * linuxContainer ) currentState () (* State , error ) {
1020
- status , err := c .currentStatus ()
1021
- if err != nil {
1022
- return nil , err
1023
- }
1024
- if status == Destroyed {
1025
- return nil , newGenericError (fmt .Errorf ("container destroyed" ), ContainerNotExists )
1026
- }
1027
- startTime , err := c .initProcess .startTime ()
1028
- if err != nil {
1029
- return nil , newSystemError (err )
1029
+ var (
1030
+ startTime string
1031
+ externalDescriptors []string
1032
+ pid = - 1
1033
+ )
1034
+ if c .initProcess != nil {
1035
+ pid = c .initProcess .pid ()
1036
+ startTime , _ = c .initProcess .startTime ()
1037
+ externalDescriptors = c .initProcess .externalDescriptors ()
1030
1038
}
1031
1039
state := & State {
1032
1040
BaseState : BaseState {
1033
1041
ID : c .ID (),
1034
1042
Config : * c .config ,
1035
- InitProcessPid : c . initProcess . pid () ,
1043
+ InitProcessPid : pid ,
1036
1044
InitProcessStartTime : startTime ,
1037
1045
},
1038
1046
CgroupPaths : c .cgroupManager .GetPaths (),
1039
1047
NamespacePaths : make (map [configs.NamespaceType ]string ),
1040
- ExternalDescriptors : c .initProcess .externalDescriptors (),
1041
- }
1042
- for _ , ns := range c .config .Namespaces {
1043
- state .NamespacePaths [ns .Type ] = ns .GetPath (c .initProcess .pid ())
1048
+ ExternalDescriptors : externalDescriptors ,
1044
1049
}
1045
- for _ , nsType := range configs .NamespaceTypes () {
1046
- if _ , ok := state .NamespacePaths [nsType ]; ! ok {
1047
- ns := configs.Namespace {Type : nsType }
1048
- state .NamespacePaths [ns .Type ] = ns .GetPath (c .initProcess .pid ())
1050
+ if pid > 0 {
1051
+ for _ , ns := range c .config .Namespaces {
1052
+ state .NamespacePaths [ns .Type ] = ns .GetPath (pid )
1053
+ }
1054
+ for _ , nsType := range configs .NamespaceTypes () {
1055
+ if _ , ok := state .NamespacePaths [nsType ]; ! ok {
1056
+ ns := configs.Namespace {Type : nsType }
1057
+ state .NamespacePaths [ns .Type ] = ns .GetPath (pid )
1058
+ }
1049
1059
}
1050
1060
}
1051
1061
return state , nil
0 commit comments