Skip to content

Commit

Permalink
Check windows service manager settings prior to setting them (#1859)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael <60191460+lurky@users.noreply.github.com>
Co-authored-by: seph <seph@kolide.co>
Co-authored-by: Rebecca Mahany-Horton <rebeccamahany@gmail.com>
  • Loading branch information
4 people committed Sep 12, 2024
1 parent 70114e3 commit 766d939
Showing 1 changed file with 35 additions and 3 deletions.
38 changes: 35 additions & 3 deletions cmd/launcher/svc_config_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func checkServiceConfiguration(logger *slog.Logger, opts *launcher.Options) {

checkRestartActions(logger, launcherService)

setRecoveryActions(context.TODO(), logger, launcherService)
checkRecoveryActions(context.TODO(), logger, launcherService)
}

// checkDelayedAutostart checks the current value of `DelayedAutostart` (whether to wait ~2 minutes
Expand Down Expand Up @@ -192,9 +192,20 @@ func checkRestartActions(logger *slog.Logger, service *mgr.Service) {
logger.Log(context.TODO(), slog.LevelInfo, "successfully set RecoveryActionsOnNonCrashFailures flag")
}

// setRecoveryActions sets the recovery actions for the launcher service.
// checkRecoveryActions checks if the recovery actions for the launcher service are set.
// sets if one or more of the recovery actions are not set.
// previously defined via wix ServicConfig Element (Util Extension) https://wixtoolset.org/docs/v3/xsd/util/serviceconfig/
func setRecoveryActions(ctx context.Context, logger *slog.Logger, service *mgr.Service) {
func checkRecoveryActions(ctx context.Context, logger *slog.Logger, service *mgr.Service) {
curRecoveryActions, err := service.RecoveryActions()
if err != nil {
logger.Log(context.TODO(), slog.LevelError,
"querying for current RecoveryActions",
"err", err,
)

return
}

recoveryActions := []mgr.RecoveryAction{
{
// first failure
Expand All @@ -213,10 +224,31 @@ func setRecoveryActions(ctx context.Context, logger *slog.Logger, service *mgr.S
},
}

// If the recovery actions are already set, we don't need to do anything
if recoveryActionsAreSet(curRecoveryActions, recoveryActions) {
return
}

if err := service.SetRecoveryActions(recoveryActions, 24*60*60); err != nil { // 24 hours
logger.Log(ctx, slog.LevelError,
"setting RecoveryActions",
"err", err,
)
}
}

// recoveryActionsAreSet checks if the current recovery actions are set to the desired recovery actions
func recoveryActionsAreSet(curRecoveryActions, recoveryActions []mgr.RecoveryAction) bool {
if curRecoveryActions == nil || len(curRecoveryActions) != len(recoveryActions) {
return false
}
for i := range curRecoveryActions {
if curRecoveryActions[i].Type != recoveryActions[i].Type {
return false
}
if curRecoveryActions[i].Delay != recoveryActions[i].Delay {
return false
}
}
return true
}

0 comments on commit 766d939

Please sign in to comment.