pkg/pidfile: don't ignore all errors when reading file

It's ok to ignore if the file doesn't exist, or if the file doesn't
have a PID in it, but we should produce an error if the file exists,
but we're unable to read it for other reasons.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2022-10-08 19:13:13 +02:00
parent 3ce2a7d026
commit 4917bcc039
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
1 changed files with 11 additions and 6 deletions

View File

@ -19,12 +19,17 @@ type PIDFile struct {
}
func checkPIDFileAlreadyExists(path string) error {
if pidByte, err := os.ReadFile(path); err == nil {
pidString := strings.TrimSpace(string(pidByte))
if pid, err := strconv.Atoi(pidString); err == nil {
if processExists(pid) {
return fmt.Errorf("pid file found, ensure docker is not running or delete %s", path)
}
pidByte, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
pidString := strings.TrimSpace(string(pidByte))
if pid, err := strconv.Atoi(pidString); err == nil {
if processExists(pid) {
return fmt.Errorf("pid file found, ensure docker is not running or delete %s", path)
}
}
return nil