diff --git a/btcd.go b/btcd.go index 5b5121cd..4aad4b51 100644 --- a/btcd.go +++ b/btcd.go @@ -19,6 +19,10 @@ var ( shutdownChannel = make(chan bool) ) +// winServiceMain is only invoked on Windows. It detect when btcd is running as +// a service and reacts accordingly. +var winServiceMain func() (bool, error) + // btcdMain is the real main function for btcd. It is necessary to work around // the fact that deferred functions do not run when os.Exit() is called. The // optional serverChan parameter is mainly used by the service code to be @@ -126,7 +130,7 @@ func main() { // the return isService flag is true, exit now since we ran as a // service. Otherwise, just fall through to normal operation. if runtime.GOOS == "windows" { - isService, err := serviceMain() + isService, err := winServiceMain() if err != nil { fmt.Println(err) os.Exit(1) diff --git a/config.go b/config.go index 79aa7891..4aeed6b1 100644 --- a/config.go +++ b/config.go @@ -43,6 +43,10 @@ var ( defaultLogFile = filepath.Join(btcdHomeDir, "logs", "btcd.log") ) +// runServiceCommand is only set to a real function on Windows. It is used +// to parse and execute service commands specified via the -s flag. +var runServiceCommand func(string) error + // config defines the configuration options for btcd. // // See loadConfig for details on the configuration load process. @@ -271,10 +275,6 @@ func loadConfig() (*config, []string, error) { // Service options which are only added on Windows. serviceOpts := serviceOptions{} - var runServiceCommand func(string) error - if runtime.GOOS == "windows" { - runServiceCommand = performServiceCommand - } // Create the home directory if it doesn't already exist. err := os.MkdirAll(btcdHomeDir, 0700) diff --git a/service_windows.go b/service_windows.go index f81920e7..a0b9d3ba 100644 --- a/service_windows.go +++ b/service_windows.go @@ -318,3 +318,9 @@ func serviceMain() (bool, error) { return true, nil } + +// Set windows specific functions to real functions. +func init() { + runServiceCommand = performServiceCommand + winServiceMain = serviceMain +}