windowsservicegoadvapi32

How do I configure failure actions of a Windows service written in Go?


I'm writing a Windows Service in Go using the golang.org/x/sys/windows/svc package.

So far, it's all working great and so easy to get started with, I love it.

I've written in some auto update functionality and I'm wanting the service to restart itself when it has completed an update.

I've tried spawning a process that will restart the service using the SCM, but it logs an error message which seems to be to do with trying to control the service while running as Local System.

The service process could not connect to the service controller. 

A better/easier way seems to be to os.Exit(1) and have the service Failure Actions set to Restart on Failure, which works brilliantly!

The only trouble is, there doesn't seem to be the functionality to configure those options programatically using Go.

I've done some digging and it looks like they are configured by passing a struct to ChangeServiceConfig2 in advapi32.dll - How to create service which restarts on crash

In golang/sys/blob/master/windows/svc/mgr/config.go - func updateDescription(handle windows.Handle, desc string) error

The code already calls windows.ChangeServiceConfig2 which is a link to the DLL call.

And the Microsoft docs for the SERVICE_FAILURE_ACTIONS struct are here.

I'm having trouble figuring out how to build and pass that struct using Go - does anyone have any insights?


Solution

  • After some guidance from here, plus reading through the source code for the existing Go Windows Service interface, I came up with my own answer, which I'll try to document below.

    For type reference when working with the Windows DLLs, the MSDN docs are here.

    My code looks like this:

    import (
        "unsafe"
        "golang.org/x/sys/windows"
    )
    
    const (
        SC_ACTION_NONE                      = 0
        SC_ACTION_RESTART                   = 1
        SC_ACTION_REBOOT                    = 2
        SC_ACTION_RUN_COMMAND               = 3
    
        SERVICE_CONFIG_FAILURE_ACTIONS      = 2
    )
    
    type SERVICE_FAILURE_ACTIONS struct {
        ResetPeriod     uint32
        RebootMsg       *uint16
        Command         *uint16
        ActionsCount    uint32
        Actions         uintptr
    }
    
    type SC_ACTION struct {
        Type            uint32
        Delay           uint32
    }
    
    func setServiceFailureActions(handle windows.Handle) error {
        t := []SC_ACTION{
            { Type: SC_ACTION_RESTART, Delay: uint32(1000) },
            { Type: SC_ACTION_RESTART, Delay: uint32(10000) },
            { Type: SC_ACTION_RESTART, Delay: uint32(60000) },
        }
        m := SERVICE_FAILURE_ACTIONS{ ResetPeriod: uint32(60), ActionsCount: uint32(3), Actions: uintptr(unsafe.Pointer(&t[0])) }
    
        return windows.ChangeServiceConfig2(handle, SERVICE_CONFIG_FAILURE_ACTIONS, (*byte)(unsafe.Pointer(&m)))
    }
    

    In my basic example, you need to pass a Service Handle, then it'll set the failure actions to a hard coded default of:

    1. Restart the first time after 1 second.
    2. Restart the second time after 10 seconds.
    3. Restart the third time and any subsequent times after 60 seconds.
    4. Reset the failure counter after 60 seconds.

    I've just tested and it seems to work ok.