Skip to content

Commit

Permalink
remove watchdog service on remote uninstall (#1825)
Browse files Browse the repository at this point in the history
  • Loading branch information
zackattack01 authored Aug 8, 2024
1 parent 1055416 commit ec0bdc5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
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

0 comments on commit ec0bdc5

Please sign in to comment.