[Win32utils-devel] Need help with win32-service, failure actions
Daniel Berger
djberg96 at gmail.com
Mon Nov 19 20:12:50 EST 2007
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.
Thanks,
Dan
More information about the win32utils-devel
mailing list