| 1 | //go:build windows |
| 2 | |
| 3 | package agent |
| 4 | |
| 5 | import ( |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "sync/atomic" |
| 11 | "unsafe" |
| 12 | |
| 13 | "reasonix/internal/store" |
| 14 | |
| 15 | "golang.org/x/sys/windows" |
| 16 | ) |
| 17 | |
| 18 | // tryLockSessionFile attempts the compatibility save lock once without |
| 19 | // blocking. The shared wrapper in save.go supplies the bounded retry window. |
| 20 | func tryLockSessionFile(path string) (func(), error) { |
| 21 | f, err := os.OpenFile(store.SessionLockFile(path), os.O_CREATE|os.O_RDWR, 0o600) |
| 22 | if err != nil { |
| 23 | if errors.Is(err, windows.ERROR_SHARING_VIOLATION) { |
| 24 | return nil, ErrSessionFileLockHeld |
| 25 | } |
| 26 | return nil, err |
| 27 | } |
| 28 | handle := windows.Handle(f.Fd()) |
| 29 | var overlapped windows.Overlapped |
| 30 | flags := uint32(windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY) |
| 31 | if err := windows.LockFileEx(handle, flags, 0, 1, 0, &overlapped); err != nil { |
| 32 | _ = f.Close() |
| 33 | if errors.Is(err, windows.ERROR_LOCK_VIOLATION) || errors.Is(err, windows.ERROR_SHARING_VIOLATION) { |
| 34 | return nil, ErrSessionFileLockHeld |
| 35 | } |
| 36 | return nil, err |
| 37 | } |
| 38 | return func() { |
| 39 | _ = windows.UnlockFileEx(handle, 0, 1, 0, &overlapped) |
| 40 | _ = f.Close() |
| 41 | }, nil |
| 42 | } |
| 43 | |
| 44 | // sessionLockFile is a non-blocking exclusive lock on a lock file itself, |
| 45 | // used by cleanup paths that may need to delete the file they locked. |
| 46 | type sessionLockFile struct { |
| 47 | handle windows.Handle |
| 48 | path string |
| 49 | overlapped windows.Overlapped |
| 50 | } |
| 51 | |
| 52 | // sessionLockDispositionFallbacks counts RemoveAndUnlock calls that could not |
| 53 | // delete through the held handle and fell back to a path-based remove. The |
| 54 | // fallback reopens the cleanup-vs-saver window, so tests pin it at zero. |
| 55 | var sessionLockDispositionFallbacks atomic.Int64 |
| 56 | |
| 57 | // tryTakeSessionLockFile opens lockPath and takes exclusive LockFileEx |
| 58 | // without blocking. The handle requests DELETE so RemoveAndUnlock can mark |
| 59 | // disposition on the same handle that owns the lock. |
| 60 | func tryTakeSessionLockFile(lockPath string) (*sessionLockFile, error) { |
| 61 | return tryTakeSessionLockFileAt(lockPath) |
| 62 | } |
| 63 | |
| 64 | func tryTakeSessionLeaseLockFile(lockPath string) (*sessionLockFile, error) { |
| 65 | return tryTakeSessionLockFileAt(lockPath) |
| 66 | } |
| 67 | |
| 68 | func tryTakeSessionLockFileAt(lockPath string) (*sessionLockFile, error) { |
| 69 | pathp, err := windows.UTF16PtrFromString(lockPath) |
| 70 | if err != nil { |
| 71 | return nil, err |
| 72 | } |
| 73 | handle, err := windows.CreateFile(pathp, |
| 74 | windows.GENERIC_READ|windows.GENERIC_WRITE|windows.DELETE, |
| 75 | windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, |
| 76 | nil, windows.OPEN_ALWAYS, windows.FILE_ATTRIBUTE_NORMAL, 0) |
| 77 | if err != nil { |
| 78 | if errors.Is(err, windows.ERROR_SHARING_VIOLATION) { |
| 79 | return nil, ErrSessionFileLockHeld |
| 80 | } |
| 81 | return nil, err |
| 82 | } |
| 83 | l := &sessionLockFile{handle: handle, path: lockPath} |
| 84 | flags := uint32(windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY) |
| 85 | if err := windows.LockFileEx(handle, flags, 0, 1, 0, &l.overlapped); err != nil { |
| 86 | _ = windows.CloseHandle(handle) |
| 87 | if errors.Is(err, windows.ERROR_LOCK_VIOLATION) { |
| 88 | return nil, ErrSessionFileLockHeld |
| 89 | } |
| 90 | return nil, err |
| 91 | } |
| 92 | return l, nil |
| 93 | } |
| 94 | |
| 95 | func (l *sessionLockFile) Unlock() { |
| 96 | _ = windows.UnlockFileEx(l.handle, 0, 1, 0, &l.overlapped) |
| 97 | _ = windows.CloseHandle(l.handle) |
| 98 | } |
| 99 | |
| 100 | // writeOwnerInfo replaces the lock file contents through the held handle |
| 101 | // so owner identity lives inside .lease.lock and dies with RemoveAndUnlock. |
| 102 | func (l *sessionLockFile) writeOwnerInfo(b []byte) error { |
| 103 | if l == nil || l.handle == 0 { |
| 104 | return errors.New("lease lock not held") |
| 105 | } |
| 106 | // SetFilePointerEx is not in golang.org/x/sys/windows. Truncate via |
| 107 | // seek-to-zero + SetEndOfFile, then write the replacement document. |
| 108 | if _, err := windows.SetFilePointer(l.handle, 0, nil, windows.FILE_BEGIN); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | if err := windows.SetEndOfFile(l.handle); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | payload := sessionLeaseOwnerBytes(b) |
| 115 | var written uint32 |
| 116 | if err := windows.WriteFile(l.handle, payload, &written, nil); err != nil { |
| 117 | return err |
| 118 | } |
| 119 | if int(written) != len(payload) { |
| 120 | return fmt.Errorf("lease owner info: short write %d of %d bytes", written, len(payload)) |
| 121 | } |
| 122 | return windows.FlushFileBuffers(l.handle) |
| 123 | } |
| 124 | |
| 125 | // RemoveAndUnlock marks delete-disposition on the held handle, then unlocks |
| 126 | // and closes so the name dies with the handle. |
| 127 | func (l *sessionLockFile) RemoveAndUnlock() error { |
| 128 | // FILE_DISPOSITION_INFO with its BOOLEAN widened to a full word. |
| 129 | info := struct{ DeleteFile uint32 }{DeleteFile: 1} |
| 130 | dispErr := windows.SetFileInformationByHandle(l.handle, windows.FileDispositionInfo, |
| 131 | (*byte)(unsafe.Pointer(&info)), uint32(unsafe.Sizeof(info))) |
| 132 | l.Unlock() |
| 133 | if dispErr != nil { |
| 134 | // Delete disposition unsupported (exotic filesystem): fall back to a |
| 135 | // path-based remove after the release. A short adoption window beats |
| 136 | // leaving the sidecar behind forever. |
| 137 | sessionLockDispositionFallbacks.Add(1) |
| 138 | if err := os.Remove(l.path); err != nil && !os.IsNotExist(err) { |
| 139 | return err |
| 140 | } |
| 141 | } |
| 142 | return nil |
| 143 | } |
| 144 | |
| 145 | func tryLockSessionLeaseFile(path string) (func(), error) { |
| 146 | lockPath := store.SessionLeaseLock(path) |
| 147 | pathp, err := windows.UTF16PtrFromString(lockPath) |
| 148 | if err != nil { |
| 149 | return nil, err |
| 150 | } |
| 151 | handle, err := windows.CreateFile(pathp, |
| 152 | windows.GENERIC_READ|windows.GENERIC_WRITE, |
| 153 | windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, |
| 154 | nil, windows.OPEN_ALWAYS, windows.FILE_ATTRIBUTE_NORMAL, 0) |
| 155 | if err != nil { |
| 156 | if errors.Is(err, windows.ERROR_SHARING_VIOLATION) { |
| 157 | return nil, ErrSessionLeaseHeld |
| 158 | } |
| 159 | return nil, err |
| 160 | } |
| 161 | var overlapped windows.Overlapped |
| 162 | flags := uint32(windows.LOCKFILE_EXCLUSIVE_LOCK | windows.LOCKFILE_FAIL_IMMEDIATELY) |
| 163 | if err := windows.LockFileEx(handle, flags, 0, 1, 0, &overlapped); err != nil { |
| 164 | _ = windows.CloseHandle(handle) |
| 165 | if errors.Is(err, windows.ERROR_LOCK_VIOLATION) || errors.Is(err, windows.ERROR_SHARING_VIOLATION) { |
| 166 | return nil, ErrSessionLeaseHeld |
| 167 | } |
| 168 | return nil, err |
| 169 | } |
| 170 | return func() { |
| 171 | _ = windows.UnlockFileEx(handle, 0, 1, 0, &overlapped) |
| 172 | _ = windows.CloseHandle(handle) |
| 173 | }, nil |
| 174 | } |
| 175 | |
| 176 | func readSessionLeaseLockFile(path string) ([]byte, error) { |
| 177 | pathp, err := windows.UTF16PtrFromString(path) |
| 178 | if err != nil { |
| 179 | return nil, err |
| 180 | } |
| 181 | handle, err := windows.CreateFile(pathp, |
| 182 | windows.GENERIC_READ, |
| 183 | windows.FILE_SHARE_READ|windows.FILE_SHARE_WRITE|windows.FILE_SHARE_DELETE, |
| 184 | nil, windows.OPEN_EXISTING, windows.FILE_ATTRIBUTE_NORMAL, 0) |
| 185 | if err != nil { |
| 186 | return nil, err |
| 187 | } |
| 188 | f := os.NewFile(uintptr(handle), path) |
| 189 | if f == nil { |
| 190 | _ = windows.CloseHandle(handle) |
| 191 | return nil, os.ErrInvalid |
| 192 | } |
| 193 | defer f.Close() |
| 194 | if _, err := f.Seek(sessionLeaseOwnerOffset, io.SeekStart); err != nil { |
| 195 | return nil, err |
| 196 | } |
| 197 | return io.ReadAll(f) |
| 198 | } |
| 199 |