From dfbb9446c44d39eb0045fb786eca773341b14c0f Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Mon, 25 Nov 2013 20:58:18 -0600 Subject: [PATCH] Ensure Win service bits only compile on Windows. --- btcd.go | 6 +++++- config.go | 8 ++++---- service_windows.go | 6 ++++++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/btcd.go b/btcd.go index 5b5121cdf..4aad4b510 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 79aa78910..4aeed6b14 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 f81920e7d..a0b9d3bad 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 +}