-
Notifications
You must be signed in to change notification settings - Fork 257
Refactor uninstall on Windows #13106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
f939a54
Switch back to os.RemoveAll
swiatekm 9729fff
Try using os.Rename and a reboot delete
swiatekm 7ecd612
Put the moved file in Temp by default
swiatekm 3a29f94
Merge branch 'main' into fix/bump-golang-1.25.1
swiatekm 50ac81d
Leave the executable in the root dir
swiatekm 6f89d54
Merge branch 'main' into fix/bump-golang-1.25.1
swiatekm 4b703b1
Remove the top path during install
swiatekm d5e4862
Merge branch 'main' into fix/bump-golang-1.25.1
swiatekm ca77d56
Improve logging
swiatekm 43c2f4b
Log a warning about failing to delete a rename
swiatekm 7f0dd9a
Merge branch 'main' into fix/bump-golang-1.25.1
swiatekm 34a931b
Add changelog entry
swiatekm b56cc3a
Merge branch 'main' into fix/bump-golang-1.25.1
swiatekm 2ce5852
Rename the whole root dir
swiatekm 17c06a4
make linter happy
swiatekm c73ade7
Rename the versioned dir instead
swiatekm 93db80f
Merge branch 'main' into fix/bump-golang-1.25.1
swiatekm 258ce7e
Always use the versioned path
swiatekm 3127503
Merge branch 'main' into fix/bump-golang-1.25.1
swiatekm 00e0fe3
Merge branch 'main' into fix/bump-golang-1.25.1
swiatekm c19d0cd
Fix unit test
swiatekm 87e7722
Switch back to moving the file
swiatekm affd03c
Merge branch 'main' into fix/bump-golang-1.25.1
swiatekm 6ddae7e
Minor test fix
swiatekm 4e60991
Make some functions universal
swiatekm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,8 +11,9 @@ import ( | |
| "fmt" | ||
| "io/fs" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "syscall" | ||
| "unsafe" | ||
|
|
||
| "golang.org/x/sys/windows" | ||
| ) | ||
|
|
@@ -39,40 +40,61 @@ func isRetryableError(err error) bool { | |
| return errno == syscall.ERROR_ACCESS_DENIED || errno == windows.ERROR_SHARING_VIOLATION | ||
| } | ||
|
|
||
| func removeBlockingExe(blockingErr error) error { | ||
| // removeBlockingExe moves a locked executable out of the way so that | ||
| // os.RemoveAll can remove the directory it lived in. On Windows a running exe | ||
| // cannot be deleted, but it *can* be renamed/moved (even across directories, | ||
| // as long as it stays on the same volume). After the move we schedule the temp | ||
| // file for deletion on the next reboot via MoveFileEx/MOVEFILE_DELAY_UNTIL_REBOOT. | ||
| // | ||
| // tempDir is the directory where the temp file will be created. It must be on | ||
| // the same volume as the blocked file (cross-volume rename won't work for | ||
| // in-use files). | ||
| func removeBlockingExe(blockingErr error, tempDir string) error { | ||
| path, _ := getPathFromError(blockingErr) | ||
| if path == "" { | ||
| return nil | ||
| } | ||
|
|
||
| // open handle for delete only | ||
| h, err := openDeleteHandle(path) | ||
| tmp, err := os.CreateTemp(tempDir, ".elastic-agent-rm-*.exe") | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open handle for %q: %w", path, err) | ||
| return fmt.Errorf("failed to create temp file in %q: %w", tempDir, err) | ||
| } | ||
| tmpPath := tmp.Name() | ||
| _ = tmp.Close() | ||
|
|
||
| // rename handle | ||
| err = renameHandle(h) | ||
| _ = windows.CloseHandle(h) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to rename handle for %q: %w", path, err) | ||
| // os.Rename uses MoveFileEx(MOVEFILE_REPLACE_EXISTING) under the hood. | ||
| if err := os.Rename(path, tmpPath); err != nil { | ||
| _ = os.Remove(tmpPath) | ||
| return fmt.Errorf("failed to rename %q to %q: %w", path, tmpPath, err) | ||
| } | ||
|
|
||
| // re-open handle | ||
| h, err = openDeleteHandle(path) | ||
| // Schedule the temp file for deletion on next reboot. | ||
| tmpPathPtr, err := windows.UTF16PtrFromString(tmpPath) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to open handle after rename for %q: %w", path, err) | ||
| return fmt.Errorf("failed to convert temp path: %w", err) | ||
| } | ||
|
|
||
| // dispose of the handle | ||
| err = disposeHandle(h) | ||
| _ = windows.CloseHandle(h) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to dispose handle for %q: %w", path, err) | ||
| if err := windows.MoveFileEx(tmpPathPtr, nil, windows.MOVEFILE_DELAY_UNTIL_REBOOT); err != nil { | ||
|
swiatekm marked this conversation as resolved.
Outdated
|
||
| // Non-fatal: the file is already out of the way, it just won't be | ||
| // auto-cleaned. Log-worthy but not worth failing the uninstall. | ||
| return fmt.Errorf("failed to schedule %q for deletion on reboot: %w", tmpPath, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // tempDirOnSameVolume returns os.TempDir() if it is on the same volume as | ||
| // path, otherwise falls back to the parent of path. Rename/move of an in-use | ||
| // file only works within the same volume. | ||
| func tempDirOnSameVolume(path string) string { | ||
| sysTemp := os.TempDir() | ||
| pathVol := filepath.VolumeName(path) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you might need to resolve any symbolic links. NTFS will allow you to link to another drive |
||
| tempVol := filepath.VolumeName(sysTemp) | ||
| if strings.EqualFold(pathVol, tempVol) { | ||
| return sysTemp | ||
| } | ||
| return filepath.Dir(path) | ||
| } | ||
|
|
||
| func getPathFromError(blockingErr error) (string, syscall.Errno) { | ||
| var perr *fs.PathError | ||
| if errors.As(blockingErr, &perr) { | ||
|
|
@@ -84,86 +106,6 @@ func getPathFromError(blockingErr error) (string, syscall.Errno) { | |
| return "", 0 | ||
| } | ||
|
|
||
| func openDeleteHandle(path string) (windows.Handle, error) { | ||
| wPath, err := windows.UTF16PtrFromString(path) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| handle, err := windows.CreateFile( | ||
| wPath, | ||
| windows.DELETE, | ||
| 0, | ||
| nil, | ||
| windows.OPEN_EXISTING, | ||
| windows.FILE_ATTRIBUTE_NORMAL, | ||
| 0, | ||
| ) | ||
| if err != nil { | ||
| return 0, err | ||
| } | ||
| return handle, nil | ||
| } | ||
|
|
||
| func renameHandle(hHandle windows.Handle) error { | ||
| wRename, err := windows.UTF16FromString(":agentrm") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var rename fileRenameInfo | ||
| lpwStream := &wRename[0] | ||
| rename.FileNameLength = uint32(unsafe.Sizeof(lpwStream)) | ||
|
|
||
| // RtlCopyMemory (https://learn.microsoft.com/en-us/windows-hardware/drivers/ddi/wdm/nf-wdm-rtlcopymemory) | ||
| // Return value - None | ||
| _, _, _ = windows.NewLazyDLL("kernel32.dll").NewProc("RtlCopyMemory").Call( | ||
| uintptr(unsafe.Pointer(&rename.FileName[0])), | ||
| uintptr(unsafe.Pointer(lpwStream)), | ||
| unsafe.Sizeof(lpwStream), | ||
| ) | ||
|
|
||
| err = windows.SetFileInformationByHandle( | ||
| hHandle, | ||
| windows.FileRenameInfo, | ||
| (*byte)(unsafe.Pointer(&rename)), | ||
| uint32(unsafe.Sizeof(rename)+unsafe.Sizeof(lpwStream)), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func disposeHandle(hHandle windows.Handle) error { | ||
| var deleteFile fileDispositionInfo | ||
| deleteFile.DeleteFile = true | ||
|
|
||
| err := windows.SetFileInformationByHandle( | ||
| hHandle, | ||
| windows.FileDispositionInfo, | ||
| (*byte)(unsafe.Pointer(&deleteFile)), | ||
| uint32(unsafe.Sizeof(deleteFile)), | ||
| ) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| type fileRenameInfo struct { | ||
| Union struct { | ||
| ReplaceIfExists bool | ||
| Flags uint32 | ||
| } | ||
| RootDirectory windows.Handle | ||
| FileNameLength uint32 | ||
| FileName [1]uint16 | ||
| } | ||
|
|
||
| type fileDispositionInfo struct { | ||
| DeleteFile bool | ||
| } | ||
|
|
||
| // killNoneChildProcess provides a way of killing a process that is not started as a child of this process. | ||
| // | ||
| // On Windows when running in unprivileged mode the internal way that golang uses DuplicateHandle to perform the kill | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe worth a comment that passing a nil as destination will schedule it for deletion.
it is documented in a syscall page, but it would be better to have it duplicated here so not involved person passing by understands on first read