Skip to content

Commit

Permalink
Moved pidfile from utils to pkg
Browse files Browse the repository at this point in the history
Fixes moby#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 <[email protected]>
  • Loading branch information
Rick Wieman committed Mar 24, 2015
1 parent f3fc857 commit 7728332
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 38 deletions.
6 changes: 4 additions & 2 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
})
}

Expand Down
44 changes: 44 additions & 0 deletions pkg/pidfile/pidfile.go
Original file line number Diff line number Diff line change
@@ -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 changes: 32 additions & 0 deletions pkg/pidfile/pidfile_test.go
Original file line number Diff line number Diff line change
@@ -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")
}
}
36 changes: 0 additions & 36 deletions utils/daemon.go

This file was deleted.

0 comments on commit 7728332

Please sign in to comment.