1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #11018 from delftswa2014/10958-pidfile

Moves pid file operations to /pkg/
This commit is contained in:
Michael Crosby 2015-03-24 17:20:52 -07:00
commit d214e64032
4 changed files with 80 additions and 38 deletions

View file

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

44
pkg/pidfile/pidfile.go Normal file
View file

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

View file

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

View file

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