sys: Index | Files | Directories

package svc

import ""

Package svc provides everything required to build Windows service.


Package Files

event.go go13.go security.go service.go


const (
    Stopped         = State(windows.SERVICE_STOPPED)
    StartPending    = State(windows.SERVICE_START_PENDING)
    StopPending     = State(windows.SERVICE_STOP_PENDING)
    Running         = State(windows.SERVICE_RUNNING)
    ContinuePending = State(windows.SERVICE_CONTINUE_PENDING)
    PausePending    = State(windows.SERVICE_PAUSE_PENDING)
    Paused          = State(windows.SERVICE_PAUSED)
const (
    Stop                  = Cmd(windows.SERVICE_CONTROL_STOP)
    Pause                 = Cmd(windows.SERVICE_CONTROL_PAUSE)
    Continue              = Cmd(windows.SERVICE_CONTROL_CONTINUE)
    Interrogate           = Cmd(windows.SERVICE_CONTROL_INTERROGATE)
    Shutdown              = Cmd(windows.SERVICE_CONTROL_SHUTDOWN)
    ParamChange           = Cmd(windows.SERVICE_CONTROL_PARAMCHANGE)
    NetBindAdd            = Cmd(windows.SERVICE_CONTROL_NETBINDADD)
    NetBindRemove         = Cmd(windows.SERVICE_CONTROL_NETBINDREMOVE)
    NetBindEnable         = Cmd(windows.SERVICE_CONTROL_NETBINDENABLE)
    NetBindDisable        = Cmd(windows.SERVICE_CONTROL_NETBINDDISABLE)
    DeviceEvent           = Cmd(windows.SERVICE_CONTROL_DEVICEEVENT)
    HardwareProfileChange = Cmd(windows.SERVICE_CONTROL_HARDWAREPROFILECHANGE)
    PowerEvent            = Cmd(windows.SERVICE_CONTROL_POWEREVENT)
    SessionChange         = Cmd(windows.SERVICE_CONTROL_SESSIONCHANGE)
    PreShutdown           = Cmd(windows.SERVICE_CONTROL_PRESHUTDOWN)
const (
    AcceptStop                  = Accepted(windows.SERVICE_ACCEPT_STOP)
    AcceptShutdown              = Accepted(windows.SERVICE_ACCEPT_SHUTDOWN)
    AcceptPauseAndContinue      = Accepted(windows.SERVICE_ACCEPT_PAUSE_CONTINUE)
    AcceptParamChange           = Accepted(windows.SERVICE_ACCEPT_PARAMCHANGE)
    AcceptNetBindChange         = Accepted(windows.SERVICE_ACCEPT_NETBINDCHANGE)
    AcceptHardwareProfileChange = Accepted(windows.SERVICE_ACCEPT_HARDWAREPROFILECHANGE)
    AcceptPowerEvent            = Accepted(windows.SERVICE_ACCEPT_POWEREVENT)
    AcceptSessionChange         = Accepted(windows.SERVICE_ACCEPT_SESSIONCHANGE)
    AcceptPreShutdown           = Accepted(windows.SERVICE_ACCEPT_PRESHUTDOWN)

func IsAnInteractiveSession Uses

func IsAnInteractiveSession() (bool, error)

IsAnInteractiveSession determines if calling process is running interactively. It queries the process token for membership in the Interactive group.

Deprecated: Use IsWindowsService instead.

func IsWindowsService Uses

func IsWindowsService() (bool, error)

IsWindowsService reports whether the process is currently executing as a Windows service.

func Run Uses

func Run(name string, handler Handler) error

Run executes service name by calling appropriate handler function.

func StatusHandle Uses

func StatusHandle() windows.Handle

StatusHandle returns service status handle. It is safe to call this function from inside the Handler.Execute because then it is guaranteed to be set. This code will have to change once multiple services are possible per process.

type Accepted Uses

type Accepted uint32

Accepted is used to describe commands accepted by the service. Note that Interrogate is always accepted.

type ChangeRequest Uses

type ChangeRequest struct {
    Cmd           Cmd
    EventType     uint32
    EventData     uintptr
    CurrentStatus Status
    Context       uintptr

ChangeRequest is sent to the service Handler to request service status change.

type Cmd Uses

type Cmd uint32

Cmd represents service state change request. It is sent to a service by the service manager, and should be actioned upon by the service.

type Handler Uses

type Handler interface {

    // Execute will be called by the package code at the start of
    // the service, and the service will exit once Execute completes.
    // Inside Execute you must read service change requests from r and
    // act accordingly. You must keep service control manager up to date
    // about state of your service by writing into s as required.
    // args contains service name followed by argument strings passed
    // to the service.
    // You can provide service exit code in exitCode return parameter,
    // with 0 being "no error". You can also indicate if exit code,
    // if any, is service specific or not by using svcSpecificEC
    // parameter.
    Execute(args []string, r <-chan ChangeRequest, s chan<- Status) (svcSpecificEC bool, exitCode uint32)

Handler is the interface that must be implemented to build Windows service.

type State Uses

type State uint32

State describes service execution state (Stopped, Running and so on).

type Status Uses

type Status struct {
    State                   State
    Accepts                 Accepted
    CheckPoint              uint32 // used to report progress during a lengthy operation
    WaitHint                uint32 // estimated time required for a pending operation, in milliseconds
    ProcessId               uint32 // if the service is running, the process identifier of it, and otherwise zero
    Win32ExitCode           uint32 // set if the service has exited with a win32 exit code
    ServiceSpecificExitCode uint32 // set if the service has exited with a service-specific exit code

Status combines State and Accepted commands to fully describe running service.


There is no mechanism to run multiple services inside one single executable. Perhaps, it can be overcome by using RegisterServiceCtrlHandlerEx Windows api.


debugPackage debug provides facilities to execute svc.Handler on console.
eventlogPackage eventlog implements access to Windows event log.
exampleExample service program that beeps.
mgrPackage mgr can be used to manage Windows service programs.

Package svc imports 6 packages (graph) and is imported by 77 packages. Updated 2021-01-24 with GOOS=windows. Refresh now. Tools for package owners.