Skip to content

Commit 85e0c77

Browse files
authored
Merge pull request #4045 from norio-nomura/detect-hostagent-process-on-checking-pid
`limactl hostagent`: check process is actually running on detecting `ha.pid`
2 parents aa6312e + 524e924 commit 85e0c77

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

cmd/limactl/hostagent.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"github.com/lima-vm/lima/v2/pkg/hostagent"
2222
"github.com/lima-vm/lima/v2/pkg/hostagent/api/server"
23+
"github.com/lima-vm/lima/v2/pkg/store"
2324
)
2425

2526
func newHostagentCommand() *cobra.Command {
@@ -46,8 +47,10 @@ func hostagentAction(cmd *cobra.Command, args []string) error {
4647
return err
4748
}
4849
if pidfile != "" {
49-
if _, err := os.Stat(pidfile); !errors.Is(err, os.ErrNotExist) {
50-
return fmt.Errorf("pidfile %q already exists", pidfile)
50+
if existingPID, err := store.ReadPIDFile(pidfile); existingPID != 0 {
51+
return fmt.Errorf("another hostagent may already be running with pid %d (pidfile %q)", existingPID, pidfile)
52+
} else if err != nil {
53+
return fmt.Errorf("failed to determine if another hostagent is running: %w", err)
5154
}
5255
if err := os.WriteFile(pidfile, []byte(strconv.Itoa(os.Getpid())+"\n"), 0o644); err != nil {
5356
return err

0 commit comments

Comments
 (0)