Daniel Berger
2007-Nov-18 18:06 UTC
[Win32utils-devel] Need help with win32-service, failure actions
Hi all, I''m trying to get failure action support working. I''ve modified the Service.configure method to accept the following options: failure_reset_period failure_command failure_reboot_message failure_actions failure_delay I''ve got the failure_command and failure_reboot message working, but the rest aren''t working properly. I''m mostly focused on failure_reboot_actions. Here''s an example: Service.configure(''some_service'', nil, :failure_reset_period => 10, :failure_command => "notepad.exe", :failure_delay => 5000, :failure_actions => [ Service::ACTION_RESTART, Service::ACTION_RUN_COMMAND, Service::ACTION_REBOOT ] ) I''ve probably just packed the data structures wrong, but I couldn''t see how. Please take a look at Service.configure in CVS. Thanks, Dan
Heesob Park
2007-Nov-19 05:41 UTC
[Win32utils-devel] Need help with win32-service, failure actions
Hi, 2007/11/19, Daniel Berger <djberg96 at gmail.com>:> > Hi all, > > I''m trying to get failure action support working. I''ve modified the > Service.configure method to accept the following options: > > failure_reset_period > failure_command > failure_reboot_message > failure_actions > failure_delay > > I''ve got the failure_command and failure_reboot message working, but the > rest aren''t working properly. I''m mostly focused on > failure_reboot_actions. > > Here''s an example: > > Service.configure(''some_service'', nil, > :failure_reset_period => 10, > :failure_command => "notepad.exe", > :failure_delay => 5000, > :failure_actions => [ > Service::ACTION_RESTART, > Service::ACTION_RUN_COMMAND, > Service::ACTION_REBOOT > ] > ) > > I''ve probably just packed the data structures wrong, but I couldn''t see > how. Please take a look at Service.configure in CVS. > > Thanks, > > DanThe main reason of not working is the wrong definition of SC_ACTION_RESTART and SC_ACTION_REBOOT and the lack of privileges. 1. Modify SC_ACTION_RESTART and SC_ACTION_REBOOT constant value. windows-pr/lib/windows/service.rb #23 SC_ACTION_NONE = 0 SC_ACTION_RESTART = 1 SC_ACTION_REBOOT = 2 SC_ACTION_RUN_COMMAND = 3 2. Define some constants and API functions. windows-pr/lib/windows/process.rb #53 TOKEN_ADJUST_PRIVILEGES = 0x20 TOKEN_QUERY = 0x8 SE_PRIVILEGE_ENABLED = 0x2 API.new(''OpenProcessToken'', ''LLP'', ''L'', ''advapi32'') API.new(''LookupPrivilegeValue'', ''PPP'', ''L'', ''advapi32'') API.new(''AdjustTokenPrivileges'', ''LLPLPP'', ''L'', ''advapi32'') 3. Modify service.rb configure method. Modify line # 443 handle_scs = OpenService(handle_scm, service, SERVICE_CHANGE_CONFIG) to handle_scs = OpenService(handle_scm, service, SERVICE_CHANGE_CONFIG | SERVICE_START) Insert following line after line #503 hdlTokenHandle = 0.chr * 4 OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, hdlTokenHandle) hdlTokenHandle = hdlTokenHandle.unpack(''L'').first #Get the LUID for shutdown privilege. tmpLuid = 0.chr * 8 LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid) tkp = [1].pack(''L'') + tmpLuid + [SE_PRIVILEGE_ENABLED].pack(''L'') # Enable the shutdown privilege in the access token of this process. AdjustTokenPrivileges(hdlTokenHandle, 0,tkp, tkp.length , nil, nil) Regards, Park Heesob -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/win32utils-devel/attachments/20071119/1763f96b/attachment-0001.html
Daniel Berger
2007-Nov-20 01:12 UTC
[Win32utils-devel] Need help with win32-service, failure actions
Heesob Park wrote: <snip>> The main reason of not working is the wrong definition of > SC_ACTION_RESTART and SC_ACTION_REBOOT and the lack of privileges. > > 1. Modify SC_ACTION_RESTART and SC_ACTION_REBOOT constant value. > windows-pr/lib/windows/service.rb #23 > > SC_ACTION_NONE = 0 > SC_ACTION_RESTART = 1 > SC_ACTION_REBOOT = 2 > SC_ACTION_RUN_COMMAND = 3Whoops, they reversed the value order on the doc page. Fixed, thanks.> 2. Define some constants and API functions. > windows-pr/lib/windows/process.rb #53 > > TOKEN_ADJUST_PRIVILEGES = 0x20 > TOKEN_QUERY = 0x8 > SE_PRIVILEGE_ENABLED = 0x2 > > API.new(''OpenProcessToken'', ''LLP'', ''L'', ''advapi32'') > API.new(''LookupPrivilegeValue'', ''PPP'', ''L'', ''advapi32'') > API.new(''AdjustTokenPrivileges'', ''LLPLPP'', ''L'', ''advapi32'')Ok, I''ve added these along with several other token related methods and constants.> 3. Modify service.rb configure method. > > Modify line # 443 > handle_scs = OpenService(handle_scm, service, > SERVICE_CHANGE_CONFIG) > to > handle_scs = OpenService(handle_scm, service, > SERVICE_CHANGE_CONFIG | SERVICE_START)Ok, I added SERVICE_START to the desired_access if I detect a SC_ACTION_REBOOT in the opts[''failure_actions''].> Insert following line after line #503 > > hdlTokenHandle = 0.chr * 4 > OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES > | TOKEN_QUERY, hdlTokenHandle) > hdlTokenHandle = hdlTokenHandle.unpack(''L'').first > > #Get the LUID for shutdown privilege. > tmpLuid = 0.chr * 8 > LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid) > tkp = [1].pack(''L'') + tmpLuid + [SE_PRIVILEGE_ENABLED].pack(''L'') > > # Enable the shutdown privilege in the access token of this > process. > AdjustTokenPrivileges(hdlTokenHandle, 0,tkp, tkp.length , > nil, nil)Awesome, thanks! The only tweak I made is that I only do this if the opts[''failure_actions''] option is set. I still don''t think the opts[''failure_reset_period''] or if opts[''failure_reset_period''] are working right, though. Not sure why. Thanks, Dan
Heesob Park
2007-Nov-20 01:46 UTC
[Win32utils-devel] Need help with win32-service, failure actions
Hi, 2007/11/20, Daniel Berger <djberg96 at gmail.com>:> > Heesob Park wrote: > > <snip> > > > The main reason of not working is the wrong definition of > > SC_ACTION_RESTART and SC_ACTION_REBOOT and the lack of privileges. > > > > 1. Modify SC_ACTION_RESTART and SC_ACTION_REBOOT constant value. > > windows-pr/lib/windows/service.rb #23 > > > > SC_ACTION_NONE = 0 > > SC_ACTION_RESTART = 1 > > SC_ACTION_REBOOT = 2 > > SC_ACTION_RUN_COMMAND = 3 > > Whoops, they reversed the value order on the doc page. Fixed, thanks. > > > 2. Define some constants and API functions. > > windows-pr/lib/windows/process.rb #53 > > > > TOKEN_ADJUST_PRIVILEGES = 0x20 > > TOKEN_QUERY = 0x8 > > SE_PRIVILEGE_ENABLED = 0x2 > > > > API.new(''OpenProcessToken'', ''LLP'', ''L'', ''advapi32'') > > API.new(''LookupPrivilegeValue'', ''PPP'', ''L'', ''advapi32'') > > API.new(''AdjustTokenPrivileges'', ''LLPLPP'', ''L'', ''advapi32'') > > Ok, I''ve added these along with several other token related methods and > constants. > > > 3. Modify service.rb configure method. > > > > Modify line # 443 > > handle_scs = OpenService(handle_scm, service, > > SERVICE_CHANGE_CONFIG) > > to > > handle_scs = OpenService(handle_scm, service, > > SERVICE_CHANGE_CONFIG | SERVICE_START) > > Ok, I added SERVICE_START to the desired_access if I detect a > SC_ACTION_REBOOT in the opts[''failure_actions'']. > > > Insert following line after line #503 > > > > hdlTokenHandle = 0.chr * 4 > > OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES > > | TOKEN_QUERY, hdlTokenHandle) > > hdlTokenHandle = hdlTokenHandle.unpack(''L'').first > > > > #Get the LUID for shutdown privilege. > > tmpLuid = 0.chr * 8 > > LookupPrivilegeValue("", "SeShutdownPrivilege", tmpLuid) > > tkp = [1].pack(''L'') + tmpLuid + > [SE_PRIVILEGE_ENABLED].pack(''L'') > > > > # Enable the shutdown privilege in the access token of this > > process. > > AdjustTokenPrivileges(hdlTokenHandle, 0,tkp, tkp.length , > > nil, nil) > > Awesome, thanks! The only tweak I made is that I only do this if the > opts[''failure_actions''] option is set. > > I still don''t think the opts[''failure_reset_period''] or if > opts[''failure_reset_period''] are working right, though. Not sure why.Be careful of the UI display unit. The following code works fine in my test. Service.configure(''Service_Desktop'', nil, :failure_reset_period => 172800, # 3600 * 24 * 2 seconds -> 2 days :failure_command => "notepad.exe", :failure_delay => 180000, # 1000 * 60 * 3 milliseconds -> 3 minutes :failure_reboot_message => "This is a test of the Notepad service!", :failure_actions => [ Service::ACTION_RESTART, Service::ACTION_RUN_COMMAND, Service::ACTION_REBOOT ] ) Regards, Park Heesob -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/win32utils-devel/attachments/20071120/9c42894f/attachment.html
Daniel Berger
2007-Nov-20 05:36 UTC
[Win32utils-devel] Need help with win32-service, failure actions
Heesob Park wrote: <snip>> Be careful of the UI display unit. > > The following code works fine in my test. > > Service.configure(''Service_Desktop'', nil, > :failure_reset_period => 172800, # 3600 > * 24 * 2 seconds -> 2 days > :failure_command => "notepad.exe ", > :failure_delay => 180000, > # 1000 * 60 * 3 milliseconds -> 3 minutes > :failure_reboot_message => "This is a test of the Notepad service!", > :failure_actions => [ > Service::ACTION_RESTART, > Service::ACTION_RUN_COMMAND, > Service::ACTION_REBOOT > ] > )Aha, you''re correct. I just hadn''t set the values high enough. :) With that done (and rubygems 0.9.5 released), things are looking good for an 0.6.0 release soon. I just want to add some more tests, and tidy up the documentation. Regards, Dan