diff options
author | OpenShift Merge Robot <openshift-merge-robot@users.noreply.github.com> | 2018-10-23 14:45:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-10-23 14:45:33 -0700 |
commit | 37e9e5667e2ed6b790d5d8271509ef93d2935083 (patch) | |
tree | a4d1d7bad134c2fa77a30ea2ee019f460a4745b2 /cmd/podman/create.go | |
parent | 28279ce6bda0e5a5acd06b98f7d8f52c8fc8a393 (diff) | |
parent | dfc689efc9a5746b0c31147562c5051c45874002 (diff) | |
download | podman-37e9e5667e2ed6b790d5d8271509ef93d2935083.tar.gz podman-37e9e5667e2ed6b790d5d8271509ef93d2935083.tar.bz2 podman-37e9e5667e2ed6b790d5d8271509ef93d2935083.zip |
Merge pull request #1706 from giuseppe/fix-cidfile-rootless
create: fix writing cidfile when using rootless
Diffstat (limited to 'cmd/podman/create.go')
-rw-r--r-- | cmd/podman/create.go | 29 |
1 files changed, 18 insertions, 11 deletions
diff --git a/cmd/podman/create.go b/cmd/podman/create.go index 248ff1b7d..9f6825c95 100644 --- a/cmd/podman/create.go +++ b/cmd/podman/create.go @@ -95,15 +95,6 @@ func createInit(c *cli.Context) error { return err } - if c.String("cidfile") != "" { - if _, err := os.Stat(c.String("cidfile")); err == nil { - return errors.Errorf("container id file exists. ensure another container is not using it or delete %s", c.String("cidfile")) - } - if err := libpod.WriteFile("", c.String("cidfile")); err != nil { - return errors.Wrapf(err, "unable to write cidfile %s", c.String("cidfile")) - } - } - if len(c.Args()) < 1 { return errors.Errorf("image name or ID is required") } @@ -119,6 +110,20 @@ func createContainer(c *cli.Context, runtime *libpod.Runtime) (*libpod.Container rootfs = c.Args()[0] } + var err error + var cidFile *os.File + if c.IsSet("cidfile") && os.Geteuid() == 0 { + cidFile, err = libpod.OpenExclusiveFile(c.String("cidfile")) + if err != nil && os.IsExist(err) { + return nil, nil, errors.Errorf("container id file exists. Ensure another container is not using it or delete %s", c.String("cidfile")) + } + if err != nil { + return nil, nil, errors.Errorf("error opening cidfile %s", c.String("cidfile")) + } + defer cidFile.Close() + defer cidFile.Sync() + } + imageName := "" var data *inspect.ImageData = nil @@ -171,12 +176,14 @@ func createContainer(c *cli.Context, runtime *libpod.Runtime) (*libpod.Container return nil, nil, err } - if c.String("cidfile") != "" { - err := libpod.WriteFile(ctr.ID(), c.String("cidfile")) + if cidFile != nil { + _, err = cidFile.WriteString(ctr.ID()) if err != nil { logrus.Error(err) } + } + logrus.Debugf("New container created %q", ctr.ID()) return ctr, createConfig, nil } |