-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
80 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.