diff --git a/daemon/commit.go b/daemon/commit.go index d92c34ba5d..fd11fc4683 100644 --- a/daemon/commit.go +++ b/daemon/commit.go @@ -22,8 +22,8 @@ type ContainerCommitConfig struct { // The image can optionally be tagged into a repository. func (daemon *Daemon) Commit(container *Container, c *ContainerCommitConfig) (*image.Image, error) { if c.Pause && !container.isPaused() { - container.pause() - defer container.unpause() + daemon.containerPause(container) + defer daemon.containerUnpause(container) } rwTar, err := daemon.exportContainerRw(container) diff --git a/daemon/container.go b/daemon/container.go index f90dde799e..b17429682a 100644 --- a/daemon/container.go +++ b/daemon/container.go @@ -343,50 +343,6 @@ func (container *Container) ExitOnNext() { container.monitor.ExitOnNext() } -func (container *Container) pause() error { - container.Lock() - defer container.Unlock() - - // We cannot Pause the container which is not running - if !container.Running { - return derr.ErrorCodeNotRunning.WithArgs(container.ID) - } - - // We cannot Pause the container which is already paused - if container.Paused { - return derr.ErrorCodeAlreadyPaused.WithArgs(container.ID) - } - - if err := container.daemon.execDriver.Pause(container.command); err != nil { - return err - } - container.Paused = true - container.logEvent("pause") - return nil -} - -func (container *Container) unpause() error { - container.Lock() - defer container.Unlock() - - // We cannot unpause the container which is not running - if !container.Running { - return derr.ErrorCodeNotRunning.WithArgs(container.ID) - } - - // We cannot unpause the container which is not paused - if !container.Paused { - return derr.ErrorCodeNotPaused.WithArgs(container.ID) - } - - if err := container.daemon.execDriver.Unpause(container.command); err != nil { - return err - } - container.Paused = false - container.logEvent("unpause") - return nil -} - // Resize changes the TTY of the process running inside the container // to the given height and width. The container must be running. func (container *Container) Resize(h, w int) error { diff --git a/daemon/daemon.go b/daemon/daemon.go index 138a56b7a3..ad1b2063fd 100644 --- a/daemon/daemon.go +++ b/daemon/daemon.go @@ -849,10 +849,10 @@ func (daemon *Daemon) shutdownContainer(c *Container) error { if !ok { return fmt.Errorf("System doesn not support SIGTERM") } - if err := c.daemon.kill(c, int(sig)); err != nil { + if err := daemon.kill(c, int(sig)); err != nil { return fmt.Errorf("sending SIGTERM to container %s with error: %v", c.ID, err) } - if err := c.unpause(); err != nil { + if err := daemon.containerUnpause(c); err != nil { return fmt.Errorf("Failed to unpause container %s with error: %v", c.ID, err) } if _, err := c.WaitStop(10 * time.Second); err != nil { @@ -861,7 +861,7 @@ func (daemon *Daemon) shutdownContainer(c *Container) error { if !ok { return fmt.Errorf("System does not support SIGKILL") } - if err := c.daemon.kill(c, int(sig)); err != nil { + if err := daemon.kill(c, int(sig)); err != nil { logrus.Errorf("Failed to SIGKILL container %s", c.ID) } c.WaitStop(-1 * time.Second) diff --git a/daemon/pause.go b/daemon/pause.go index 385e93f9f9..4182e92e2f 100644 --- a/daemon/pause.go +++ b/daemon/pause.go @@ -11,9 +11,33 @@ func (daemon *Daemon) ContainerPause(name string) error { return err } - if err := container.pause(); err != nil { + if err := daemon.containerPause(container); err != nil { return derr.ErrorCodePauseError.WithArgs(name, err) } return nil } + +// containerPause pauses the container execution without stopping the process. +// The execution can be resumed by calling containerUnpause. +func (daemon *Daemon) containerPause(container *Container) error { + container.Lock() + defer container.Unlock() + + // We cannot Pause the container which is not running + if !container.Running { + return derr.ErrorCodeNotRunning.WithArgs(container.ID) + } + + // We cannot Pause the container which is already paused + if container.Paused { + return derr.ErrorCodeAlreadyPaused.WithArgs(container.ID) + } + + if err := daemon.execDriver.Pause(container.command); err != nil { + return err + } + container.Paused = true + daemon.logContainerEvent(container, "pause") + return nil +} diff --git a/daemon/unpause.go b/daemon/unpause.go index 6dd41c511c..4112494fc4 100644 --- a/daemon/unpause.go +++ b/daemon/unpause.go @@ -11,9 +11,33 @@ func (daemon *Daemon) ContainerUnpause(name string) error { return err } - if err := container.unpause(); err != nil { + if err := daemon.containerUnpause(container); err != nil { return derr.ErrorCodeCantUnpause.WithArgs(name, err) } return nil } + +// containerUnpause resumes the container execution after the container is paused. +func (daemon *Daemon) containerUnpause(container *Container) error { + container.Lock() + defer container.Unlock() + + // We cannot unpause the container which is not running + if !container.Running { + return derr.ErrorCodeNotRunning.WithArgs(container.ID) + } + + // We cannot unpause the container which is not paused + if !container.Paused { + return derr.ErrorCodeNotPaused.WithArgs(container.ID) + } + + if err := daemon.execDriver.Unpause(container.command); err != nil { + return err + } + + container.Paused = false + daemon.logContainerEvent(container, "unpause") + return nil +}