[Win32utils-devel] Need help with win32-service, failure actions
Heesob Park
phasis at gmail.com
Mon Nov 19 20:46:34 EST 2007
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
More information about the win32utils-devel
mailing list