Browse Source

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 <git@rickw.nl>
Rick Wieman 10 years ago
parent
commit
772833274f
4 changed files with 80 additions and 38 deletions
  1. 4 2
      daemon/daemon.go
  2. 44 0
      pkg/pidfile/pidfile.go
  3. 32 0
      pkg/pidfile/pidfile_test.go
  4. 0 36
      utils/daemon.go

+ 4 - 2
daemon/daemon.go

@@ -36,6 +36,7 @@ import (
 	"github.com/docker/docker/pkg/namesgenerator"
 	"github.com/docker/docker/pkg/namesgenerator"
 	"github.com/docker/docker/pkg/parsers"
 	"github.com/docker/docker/pkg/parsers"
 	"github.com/docker/docker/pkg/parsers/kernel"
 	"github.com/docker/docker/pkg/parsers/kernel"
+	"github.com/docker/docker/pkg/pidfile"
 	"github.com/docker/docker/pkg/resolvconf"
 	"github.com/docker/docker/pkg/resolvconf"
 	"github.com/docker/docker/pkg/stringid"
 	"github.com/docker/docker/pkg/stringid"
 	"github.com/docker/docker/pkg/sysinfo"
 	"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.
 	// 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.
 	// Some of the init doesn't need a pidfile lock - but let's not try to be smart.
 	if config.Pidfile != "" {
 	if config.Pidfile != "" {
-		if err := utils.CreatePidFile(config.Pidfile); err != nil {
+		file, err := pidfile.New(config.Pidfile)
+		if err != nil {
 			return nil, err
 			return nil, err
 		}
 		}
 		eng.OnShutdown(func() {
 		eng.OnShutdown(func() {
 			// Always release the pidfile last, just in case
 			// Always release the pidfile last, just in case
-			utils.RemovePidFile(config.Pidfile)
+			file.Remove()
 		})
 		})
 	}
 	}
 
 

+ 44 - 0
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
+}

+ 32 - 0
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")
+	}
+}

+ 0 - 36
utils/daemon.go

@@ -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)
-	}
-}