| 1 | |
| 2 | |
| 3 | |
| 4 | package store |
| 5 | |
| 6 | import ( |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "os" |
| 10 | "path/filepath" |
| 11 | "strings" |
| 12 | |
| 13 | "github.com/sirupsen/logrus" |
| 14 | |
| 15 | "github.com/lima-vm/lima/v2/pkg/limatype/filenames" |
| 16 | "github.com/lima-vm/lima/v2/pkg/osutil" |
| 17 | ) |
| 18 | |
| 19 | |
| 20 | const pidFileSuffix = ".pid" |
| 21 | |
| 22 | |
| 23 | var bootSessionID = osutil.BootSessionID |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | func currentBootSessionID() (string, error) { |
| 29 | id, err := bootSessionID() |
| 30 | if errors.Is(err, osutil.ErrBootSessionNotSupported) { |
| 31 | |
| 32 | return "", nil |
| 33 | } |
| 34 | if err != nil { |
| 35 | return "", fmt.Errorf("failed to determine the boot session ID: %w", err) |
| 36 | } |
| 37 | return id, nil |
| 38 | } |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | func WritePIDFile(path string, pid int) error { |
| 48 | dir := filepath.Dir(path) |
| 49 | current, err := currentBootSessionID() |
| 50 | if err != nil { |
| 51 | return err |
| 52 | } |
| 53 | if current != "" { |
| 54 | stale, err := staleBootSession(dir, current) |
| 55 | if err != nil { |
| 56 | return err |
| 57 | } |
| 58 | if stale { |
| 59 | if err := removePIDFiles(dir); err != nil { |
| 60 | return err |
| 61 | } |
| 62 | } |
| 63 | if err := writeBootSessionMarker(dir, current); err != nil { |
| 64 | return err |
| 65 | } |
| 66 | } |
| 67 | return writePIDFileAtomically(path, pid) |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | func writePIDFileAtomically(path string, pid int) error { |
| 73 | tmpPath := path + ".tmp" |
| 74 | if err := os.WriteFile(tmpPath, fmt.Appendf(nil, "%d\n", pid), 0o644); err != nil { |
| 75 | return err |
| 76 | } |
| 77 | if err := os.Rename(tmpPath, path); err != nil { |
| 78 | _ = os.Remove(tmpPath) |
| 79 | return err |
| 80 | } |
| 81 | return nil |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | func pidFileFromPreviousBoot(path string) (bool, error) { |
| 87 | current, err := currentBootSessionID() |
| 88 | if err != nil || current == "" { |
| 89 | return false, err |
| 90 | } |
| 91 | return staleBootSession(filepath.Dir(path), current) |
| 92 | } |
| 93 | |
| 94 | |
| 95 | |
| 96 | func staleBootSession(dir, current string) (bool, error) { |
| 97 | b, err := os.ReadFile(filepath.Join(dir, filenames.BootSession)) |
| 98 | if err != nil { |
| 99 | if errors.Is(err, os.ErrNotExist) { |
| 100 | |
| 101 | return false, nil |
| 102 | } |
| 103 | return false, err |
| 104 | } |
| 105 | return strings.TrimSpace(string(b)) != current, nil |
| 106 | } |
| 107 | |
| 108 | func writeBootSessionMarker(dir, id string) error { |
| 109 | return os.WriteFile(filepath.Join(dir, filenames.BootSession), []byte(id+"\n"), 0o644) |
| 110 | } |
| 111 | |
| 112 | func removePIDFiles(dir string) error { |
| 113 | entries, err := os.ReadDir(dir) |
| 114 | if err != nil { |
| 115 | return err |
| 116 | } |
| 117 | for _, entry := range entries { |
| 118 | if entry.IsDir() || !strings.HasSuffix(entry.Name(), pidFileSuffix) { |
| 119 | continue |
| 120 | } |
| 121 | path := filepath.Join(dir, entry.Name()) |
| 122 | logrus.Infof("Removing PID file %#q left behind by a previous boot", path) |
| 123 | if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { |
| 124 | return err |
| 125 | } |
| 126 | } |
| 127 | return nil |
| 128 | } |