From bd7757077e643eafae3a1f9fb3d8faca09ea2b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafa=C5=82=20=C5=81asocha?= Date: Wed, 25 Jan 2023 16:51:23 +0100 Subject: [PATCH] Adds possibility to configure failure flag --- lib/win32/service.rb | 20 ++++++++++++++++++-- lib/win32/windows/structs.rb | 6 ++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/win32/service.rb b/lib/win32/service.rb index af8c5aa..514609d 100644 --- a/lib/win32/service.rb +++ b/lib/win32/service.rb @@ -272,6 +272,7 @@ class Service # * failure_command => nil, # * failure_actions => nil, # * failure_delay => 0 + # * failure_flag => nil # # Example: # @@ -317,6 +318,7 @@ def initialize(options = {}) "failure_command" => nil, "failure_actions" => nil, "failure_delay" => 0, + "failure_flag" => nil, "host" => nil, "service_name" => nil, } @@ -396,7 +398,7 @@ def initialize(options = {}) end if opts["failure_reset_period"] || opts["failure_reboot_message"] || - opts["failure_command"] || opts["failure_actions"] + opts["failure_command"] || opts["failure_actions"] || !opts["failure_flag"].nil? self.class.configure_failure_actions(handle_scs, opts) end ensure @@ -425,6 +427,7 @@ def initialize(options = {}) # * failure_reboot_message # * failure_command # * failure_actions + # * failure_flag # * failure_delay # # Examples: @@ -475,6 +478,7 @@ def self.configure(options = {}) "failure_command" => nil, "failure_actions" => nil, "failure_delay" => 0, + "failure_flag" => nil, "service_name" => nil, "host" => nil, "delayed_start" => false, @@ -579,7 +583,7 @@ def self.configure(options = {}) end if opts["failure_reset_period"] || opts["failure_reboot_message"] || - opts["failure_command"] || opts["failure_actions"] + opts["failure_command"] || opts["failure_actions"] || !opts["failure_flag"].nil? configure_failure_actions(handle_scs, opts) end ensure @@ -1397,6 +1401,18 @@ def self.configure_failure_actions(handle_scs, opts) close_service_handle(handle_scs) raise SystemCallError.new("ChangeServiceConfig2", error) end + + unless opts["failure_flag"].nil? + sfaf = SERVICE_FAILURE_ACTIONS_FLAG.new + sfaf[:fFailureActionsOnNonCrashFailures] = opts["failure_flag"] ? 1 : 0 + bool = ChangeServiceConfig2( + handle_scs, + SERVICE_CONFIG_FAILURE_ACTIONS_FLAG, + sfaf + ) + + FFI.raise_windows_error("ChangeServiceConfig2") unless bool + end end # Returns a human readable string indicating the action type. diff --git a/lib/win32/windows/structs.rb b/lib/win32/windows/structs.rb index 48ceaf1..c29d7e8 100644 --- a/lib/win32/windows/structs.rb +++ b/lib/win32/windows/structs.rb @@ -90,6 +90,12 @@ class SERVICE_FAILURE_ACTIONS < FFI::Struct ) end + class SERVICE_FAILURE_ACTIONS_FLAG < FFI::Struct + layout( + :fFailureActionsOnNonCrashFailures, :int + ) + end + class SERVICE_TABLE_ENTRY < FFI::Struct layout( :lpServiceName, :pointer,