Skip to content
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

remove watchdog service on remote uninstall #1825

Merged
merged 2 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions ee/uninstall/uninstall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"

"github.com/kolide/launcher/ee/watchdog"
"golang.org/x/sys/windows/svc/mgr"
)

Expand All @@ -30,5 +31,10 @@ func disableAutoStart(ctx context.Context) error {
return fmt.Errorf("updating launcher service config: %w", err)
}

// attempt to remove watchdog service in case it is installed to prevent startups later on
if err := watchdog.RemoveService(svcMgr); err != nil {
return fmt.Errorf("removing watchdog service, error may be expected if not enabled: %w", err)
}

return nil
}
14 changes: 9 additions & 5 deletions ee/watchdog/controller_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (wc *WatchdogController) ServiceEnabledChanged(enabled bool) {
defer serviceManager.Disconnect()

if !enabled {
err := removeService(serviceManager, launcherWatchdogServiceName)
err := RemoveService(serviceManager)
if err != nil {
if err.Error() == serviceDoesNotExistError {
wc.slogger.Log(ctx, slog.LevelDebug, "watchdog service was not previously installed")
Expand Down Expand Up @@ -344,16 +344,20 @@ func (wc *WatchdogController) installService(serviceManager *mgr.Mgr) error {
return nil
}

// removeService utilizes the passed serviceManager to remove the existing service
// after looking up the handle from serviceName
func removeService(serviceManager *mgr.Mgr, serviceName string) error {
existingService, err := serviceManager.OpenService(serviceName)
// RemoveService utilizes the passed serviceManager to remove any existing watchdog service if it exists
func RemoveService(serviceManager *mgr.Mgr) error {
existingService, err := serviceManager.OpenService(launcherWatchdogServiceName)
if err != nil {
return err
}

defer existingService.Close()

// attempt to stop the service first, we don't care if this fails because we are going to
// remove the service next anyway (the removal happens faster if stopped first, but will
// happen eventually regardless)
existingService.Control(svc.Stop)

if err := backoff.WaitFor(func() error {
if err = existingService.Delete(); err != nil {
return err
Expand Down
Loading