Skip to content

Commit

Permalink
Overwrite devstate.json if PId not exists
Browse files Browse the repository at this point in the history
  • Loading branch information
feloy committed Apr 6, 2023
1 parent 3129671 commit 0008627
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
38 changes: 38 additions & 0 deletions pkg/state/process_unix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
// +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos

package state

import (
"fmt"
"os"
"syscall"
)

func pidExists(pid int) (bool, error) {
if pid <= 0 {
return false, fmt.Errorf("invalid pid %v", pid)
}
proc, err := os.FindProcess(pid)
if err != nil {
return false, nil
}
err = proc.Signal(syscall.Signal(0))
if err == nil {
return true, nil
}
if err.Error() == "os: process already finished" {
return false, nil
}
errno, ok := err.(syscall.Errno)
if !ok {
return false, err
}
switch errno {
case syscall.ESRCH:
return false, nil
case syscall.EPERM:
return true, nil
}
return false, err
}
17 changes: 17 additions & 0 deletions pkg/state/process_windows.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package state

import (
"fmt"
"os"
)

func pidExists(pid int) (bool, error) {
if pid <= 0 {
return false, fmt.Errorf("invalid pid %v", pid)
}
_, err := os.FindProcess(pid)
if err != nil {
return false, nil
}
return true, nil
}
10 changes: 10 additions & 0 deletions pkg/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,15 @@ func (o *State) isFreeOrOwnedBy(pid int) (bool, error) {
// File is owned by process
return true, nil
}

exists, err := pidExists(savedContent.PID)
if err != nil {
return false, err
}
if !exists {
// Process already finished
return true, nil
}

return false, nil
}

0 comments on commit 0008627

Please sign in to comment.