From e0fdaa87e8fb44eb34c7b37ce535a3278fead416 Mon Sep 17 00:00:00 2001 From: Rick Wieman Date: Wed, 25 Feb 2015 19:33:01 +0100 Subject: [PATCH] Moved pidfile from utils to pkg Fixes #10958 by moving utils.daemon to pkg.pidfile. Test cases were also added. Updated the daemon to use the new pidfile. Signed-off-by: Rick Wieman Upstream-commit: 772833274fd84b3c960ccab14258b7e5a00b18cd Component: engine --- components/engine/daemon/daemon.go | 6 ++- components/engine/pkg/pidfile/pidfile.go | 44 +++++++++++++++++++ components/engine/pkg/pidfile/pidfile_test.go | 32 ++++++++++++++ components/engine/utils/daemon.go | 36 --------------- 4 files changed, 80 insertions(+), 38 deletions(-) create mode 100644 components/engine/pkg/pidfile/pidfile.go create mode 100644 components/engine/pkg/pidfile/pidfile_test.go delete mode 100644 components/engine/utils/daemon.go diff --git a/components/engine/daemon/daemon.go b/components/engine/daemon/daemon.go index a437fac1b5..434b78a339 100644 --- a/components/engine/daemon/daemon.go +++ b/components/engine/daemon/daemon.go @@ -36,6 +36,7 @@ import ( "github.com/docker/docker/pkg/namesgenerator" "github.com/docker/docker/pkg/parsers" "github.com/docker/docker/pkg/parsers/kernel" + "github.com/docker/docker/pkg/pidfile" "github.com/docker/docker/pkg/resolvconf" "github.com/docker/docker/pkg/stringid" "github.com/docker/docker/pkg/sysinfo" @@ -836,12 +837,13 @@ func NewDaemonFromDirectory(config *Config, eng *engine.Engine) (*Daemon, error) // Claim the pidfile first, to avoid any and all unexpected race conditions. // Some of the init doesn't need a pidfile lock - but let's not try to be smart. if config.Pidfile != "" { - if err := utils.CreatePidFile(config.Pidfile); err != nil { + file, err := pidfile.New(config.Pidfile) + if err != nil { return nil, err } eng.OnShutdown(func() { // Always release the pidfile last, just in case - utils.RemovePidFile(config.Pidfile) + file.Remove() }) } diff --git a/components/engine/pkg/pidfile/pidfile.go b/components/engine/pkg/pidfile/pidfile.go new file mode 100644 index 0000000000..21a5438799 --- /dev/null +++ b/components/engine/pkg/pidfile/pidfile.go @@ -0,0 +1,44 @@ +package pidfile + +import ( + "fmt" + "io/ioutil" + "log" + "os" + "path/filepath" + "strconv" +) + +type PidFile struct { + path string +} + +func checkPidFileAlreadyExists(path string) error { + if pidString, err := ioutil.ReadFile(path); err == nil { + if pid, err := strconv.Atoi(string(pidString)); err == nil { + if _, err := os.Stat(filepath.Join("/proc", string(pid))); err == nil { + return fmt.Errorf("pid file found, ensure docker is not running or delete %s", path) + } + } + } + return nil +} + +func New(path string) (file *PidFile, err error) { + if err := checkPidFileAlreadyExists(path); err != nil { + return nil, err + } + + file = &PidFile{path: path} + err = ioutil.WriteFile(path, []byte(fmt.Sprintf("%d", os.Getpid())), 0644) + + return file, err +} + +func (file PidFile) Remove() error { + if err := os.Remove(file.path); err != nil { + log.Printf("Error removing %s: %s", file.path, err) + return err + } + return nil +} diff --git a/components/engine/pkg/pidfile/pidfile_test.go b/components/engine/pkg/pidfile/pidfile_test.go new file mode 100644 index 0000000000..6ed9cfc385 --- /dev/null +++ b/components/engine/pkg/pidfile/pidfile_test.go @@ -0,0 +1,32 @@ +package pidfile + +import ( + "io/ioutil" + "os" + "path/filepath" + "testing" +) + +func TestNewAndRemove(t *testing.T) { + dir, err := ioutil.TempDir(os.TempDir(), "test-pidfile") + if err != nil { + t.Fatal("Could not create test directory") + } + + file, err := New(filepath.Join(dir, "testfile")) + if err != nil { + t.Fatal("Could not create test file", err) + } + + if err := file.Remove(); err != nil { + t.Fatal("Could not delete created test file") + } +} + +func TestRemoveInvalidPath(t *testing.T) { + file := PidFile{path: filepath.Join("foo", "bar")} + + if err := file.Remove(); err == nil { + t.Fatal("Non-existing file doesn't give an error on delete") + } +} diff --git a/components/engine/utils/daemon.go b/components/engine/utils/daemon.go deleted file mode 100644 index 871122ed59..0000000000 --- a/components/engine/utils/daemon.go +++ /dev/null @@ -1,36 +0,0 @@ -package utils - -import ( - "fmt" - "io/ioutil" - "log" - "os" - "strconv" -) - -func CreatePidFile(pidfile string) error { - if pidString, err := ioutil.ReadFile(pidfile); err == nil { - pid, err := strconv.Atoi(string(pidString)) - if err == nil { - if _, err := os.Stat(fmt.Sprintf("/proc/%d/", pid)); err == nil { - return fmt.Errorf("pid file found, ensure docker is not running or delete %s", pidfile) - } - } - } - - file, err := os.Create(pidfile) - if err != nil { - return err - } - - defer file.Close() - - _, err = fmt.Fprintf(file, "%d", os.Getpid()) - return err -} - -func RemovePidFile(pidfile string) { - if err := os.Remove(pidfile); err != nil { - log.Printf("Error removing %s: %s", pidfile, err) - } -}