diff options
Diffstat (limited to 'libpod/container.go')
-rw-r--r-- | libpod/container.go | 34 |
1 files changed, 20 insertions, 14 deletions
diff --git a/libpod/container.go b/libpod/container.go index 70011963d..fce64b0dd 100644 --- a/libpod/container.go +++ b/libpod/container.go @@ -8,7 +8,6 @@ import ( "os" "path/filepath" "strconv" - "sync" "syscall" "time" @@ -64,9 +63,8 @@ type Container struct { state *containerRuntimeInfo - // TODO move to storage.Locker from sync.Mutex valid bool - lock sync.Mutex + lock storage.Locker runtime *Runtime } @@ -319,6 +317,8 @@ func (c *Container) attachSocketPath() string { // Sync this container with on-disk state and runc status // Should only be called with container lock held +// This function should suffice to ensure a container's state is accurate and +// it is valid for use. func (c *Container) syncContainer() error { if err := c.runtime.state.UpdateContainer(c); err != nil { return err @@ -343,7 +343,7 @@ func (c *Container) syncContainer() error { } // Make a new container -func newContainer(rspec *spec.Spec) (*Container, error) { +func newContainer(rspec *spec.Spec, lockDir string) (*Container, error) { if rspec == nil { return nil, errors.Wrapf(ErrInvalidArg, "must provide a valid runtime spec to create container") } @@ -360,6 +360,20 @@ func newContainer(rspec *spec.Spec) (*Container, error) { ctr.config.CreatedTime = time.Now() + // Path our lock file will reside at + lockPath := filepath.Join(lockDir, ctr.config.ID) + // Ensure there is no conflict - file does not exist + _, err := os.Stat(lockPath) + if err == nil || !os.IsNotExist(err) { + return nil, errors.Wrapf(ErrCtrExists, "lockfile for container ID %s already exists", ctr.config.ID) + } + // Grab a lockfile at the given path + lock, err := storage.GetLockfile(lockPath) + if err != nil { + return nil, errors.Wrapf(err, "error creating lockfile for new container") + } + ctr.lock = lock + return ctr, nil } @@ -904,11 +918,7 @@ func (c *Container) mountStorage() (err error) { } }() - if err := c.runtime.state.SaveContainer(c); err != nil { - return errors.Wrapf(err, "error saving container %s state", c.ID()) - } - - return nil + return c.save() } // CleanupStorage unmounts all mount points in container and cleans up container storage @@ -944,9 +954,5 @@ func (c *Container) cleanupStorage() error { c.state.Mountpoint = "" c.state.Mounted = false - if err := c.runtime.state.SaveContainer(c); err != nil { - return errors.Wrapf(err, "error saving container %s state", c.ID()) - } - - return nil + return c.save() } |