From af636848622c8ad27cace63be5f9dd9aaa565502 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Mon, 23 Jan 2017 20:40:11 -0200 Subject: [PATCH 1/4] Squashed 'modules/minwinsvc/' content from commit cad6b2b git-subtree-dir: modules/minwinsvc git-subtree-split: cad6b2b879b0970e4245a20ebf1a81a756e2bb70 --- LICENSE | 20 +++++++++++++++ README.md | 18 +++++++++++++ minwinsvc.go | 18 +++++++++++++ svc_other.go | 11 ++++++++ svc_windows.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 137 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 minwinsvc.go create mode 100644 svc_other.go create mode 100644 svc_windows.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000000..fce91b4e1a --- /dev/null +++ b/LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2015 Daniel Theophanes + +This software is provided 'as-is', without any express or implied +warranty. In no event will the authors be held liable for any damages +arising from the use of this software. + +Permission is granted to anyone to use this software for any purpose, +including commercial applications, and to alter it and redistribute it +freely, subject to the following restrictions: + + 1. The origin of this software must not be misrepresented; you must not + claim that you wrote the original software. If you use this software + in a product, an acknowledgment in the product documentation would be + appreciated but is not required. + + 2. Altered source versions must be plainly marked as such, and must not be + misrepresented as being the original software. + + 3. This notice may not be removed or altered from any source + distribution. diff --git a/README.md b/README.md new file mode 100644 index 0000000000..260dceeac3 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +### Minimal windows service stub + +Programs designed to run from most *nix style operating systems +can import this package to enable running programs as services without modifying +them. + +``` +import _ "github.com/kardianos/minwinsvc" +``` + +If you need more control over the exit behavior, set +``` +minwinsvc.SetOnExit(func() { + // Do something. + // Within 10 seconds call: + os.Exit(0) +}) +``` diff --git a/minwinsvc.go b/minwinsvc.go new file mode 100644 index 0000000000..057ba7f954 --- /dev/null +++ b/minwinsvc.go @@ -0,0 +1,18 @@ +// Copyright 2015 Daniel Theophanes. +// Use of this source code is governed by a zlib-style +// license that can be found in the LICENSE file.package service + +// Minimal non-invasive windows only service stub. +// +// Import to allow running as a windows service. +// import _ "github.com/kardianos/minwinsvc" +// This will detect if running as a windows service +// and install required callbacks for windows. +package minwinsvc + +// SetOnExit sets the function to be called when the windows service +// requests an exit. If this is not called, or if it is called where +// f == nil, then it defaults to calling "os.Exit(0)". +func SetOnExit(f func()) { + setOnExit(f) +} diff --git a/svc_other.go b/svc_other.go new file mode 100644 index 0000000000..197d30021f --- /dev/null +++ b/svc_other.go @@ -0,0 +1,11 @@ +// Copyright 2015 Daniel Theophanes. +// Use of this source code is governed by a zlib-style +// license that can be found in the LICENSE file.package service + +//+build !windows + +package minwinsvc + +func setOnExit(f func()) { + // Nothing. +} diff --git a/svc_windows.go b/svc_windows.go new file mode 100644 index 0000000000..91e2b6a4dd --- /dev/null +++ b/svc_windows.go @@ -0,0 +1,70 @@ +// Copyright 2015 Daniel Theophanes. +// Use of this source code is governed by a zlib-style +// license that can be found in the LICENSE file.package service + +//+build windows + +package minwinsvc + +import ( + "os" + "sync" + + "golang.org/x/sys/windows/svc" +) + +var ( + onExit func() + guard sync.Mutex +) + +func init() { + interactive, err := svc.IsAnInteractiveSession() + if err != nil { + panic(err) + } + if interactive { + return + } + go func() { + _ = svc.Run("", runner{}) + + guard.Lock() + f := onExit + guard.Unlock() + + // Don't hold this lock in user code. + if f != nil { + f() + } + // Make sure we exit. + os.Exit(0) + }() +} + +func setOnExit(f func()) { + guard.Lock() + onExit = f + guard.Unlock() +} + +type runner struct{} + +func (runner) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) { + const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown + changes <- svc.Status{State: svc.StartPending} + + changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} + for { + c := <-r + switch c.Cmd { + case svc.Interrogate: + changes <- c.CurrentStatus + case svc.Stop, svc.Shutdown: + changes <- svc.Status{State: svc.StopPending} + return false, 0 + } + } + + return false, 0 +} From fda44760612aad10e556f65cd201e94ed568fbb3 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Mon, 23 Jan 2017 20:44:23 -0200 Subject: [PATCH 2/4] Fix SSH server on Windows when running as service Closes #680 --- modules/minwinsvc/svc_windows.go | 9 +++++++-- modules/setting/setting.go | 3 ++- modules/ssh/ssh.go | 6 +++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/modules/minwinsvc/svc_windows.go b/modules/minwinsvc/svc_windows.go index 91e2b6a4dd..c6e87e29df 100644 --- a/modules/minwinsvc/svc_windows.go +++ b/modules/minwinsvc/svc_windows.go @@ -8,17 +8,22 @@ package minwinsvc import ( "os" + "strconv" "sync" "golang.org/x/sys/windows/svc" ) var ( - onExit func() - guard sync.Mutex + onExit func() + guard sync.Mutex + skip, _ = strconv.ParseBool(os.Getenv("SKIP_MINWINSVC")) ) func init() { + if skip { + return + } interactive, err := svc.IsAnInteractiveSession() if err != nil { panic(err) diff --git a/modules/setting/setting.go b/modules/setting/setting.go index 910ec93021..61eac4fb33 100644 --- a/modules/setting/setting.go +++ b/modules/setting/setting.go @@ -22,13 +22,14 @@ import ( "code.gitea.io/git" "code.gitea.io/gitea/modules/log" + _ "code.gitea.io/gitea/modules/minwinsvc" // import minwinsvc for windows services "code.gitea.io/gitea/modules/user" + "github.com/Unknwon/com" _ "github.com/go-macaron/cache/memcache" // memcache plugin for cache _ "github.com/go-macaron/cache/redis" "github.com/go-macaron/session" _ "github.com/go-macaron/session/redis" // redis plugin for store session - _ "github.com/kardianos/minwinsvc" // import minwinsvc for windows services "gopkg.in/ini.v1" "strk.kbt.io/projects/go/libravatar" ) diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index 056ef084ef..18bac8dbf2 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -67,7 +67,11 @@ func handleServerConn(keyID string, chans <-chan ssh.NewChannel) { args := []string{"serv", "key-" + keyID, "--config=" + setting.CustomConf} log.Trace("SSH: Arguments: %v", args) cmd := exec.Command(setting.AppPath, args...) - cmd.Env = append(os.Environ(), "SSH_ORIGINAL_COMMAND="+cmdName) + cmd.Env = append( + os.Environ(), + "SSH_ORIGINAL_COMMAND="+cmdName, + "SKIP_MINWINSVC=1", + ) stdout, err := cmd.StdoutPipe() if err != nil { From bcee9b76dd489fbe1dee9413cea5ddcad92c6705 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Mon, 23 Jan 2017 20:56:25 -0200 Subject: [PATCH 3/4] Fix lint --- modules/minwinsvc/minwinsvc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/minwinsvc/minwinsvc.go b/modules/minwinsvc/minwinsvc.go index 057ba7f954..4cd89ffdb4 100644 --- a/modules/minwinsvc/minwinsvc.go +++ b/modules/minwinsvc/minwinsvc.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a zlib-style // license that can be found in the LICENSE file.package service -// Minimal non-invasive windows only service stub. +// Package minwinsvc is a minimal non-invasive windows only service stub. // // Import to allow running as a windows service. // import _ "github.com/kardianos/minwinsvc" From 2009f4cbdae9095240bd8c5408e097e101499879 Mon Sep 17 00:00:00 2001 From: Andrey Nering Date: Mon, 23 Jan 2017 20:57:34 -0200 Subject: [PATCH 4/4] Remove original minwinsvc from vendor --- vendor/github.com/kardianos/minwinsvc/LICENSE | 20 ------ .../github.com/kardianos/minwinsvc/README.md | 18 ----- .../kardianos/minwinsvc/minwinsvc.go | 18 ----- .../kardianos/minwinsvc/svc_other.go | 11 --- .../kardianos/minwinsvc/svc_windows.go | 70 ------------------- vendor/vendor.json | 6 -- 6 files changed, 143 deletions(-) delete mode 100644 vendor/github.com/kardianos/minwinsvc/LICENSE delete mode 100644 vendor/github.com/kardianos/minwinsvc/README.md delete mode 100644 vendor/github.com/kardianos/minwinsvc/minwinsvc.go delete mode 100644 vendor/github.com/kardianos/minwinsvc/svc_other.go delete mode 100644 vendor/github.com/kardianos/minwinsvc/svc_windows.go diff --git a/vendor/github.com/kardianos/minwinsvc/LICENSE b/vendor/github.com/kardianos/minwinsvc/LICENSE deleted file mode 100644 index fce91b4e1a..0000000000 --- a/vendor/github.com/kardianos/minwinsvc/LICENSE +++ /dev/null @@ -1,20 +0,0 @@ -Copyright (c) 2015 Daniel Theophanes - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. diff --git a/vendor/github.com/kardianos/minwinsvc/README.md b/vendor/github.com/kardianos/minwinsvc/README.md deleted file mode 100644 index 260dceeac3..0000000000 --- a/vendor/github.com/kardianos/minwinsvc/README.md +++ /dev/null @@ -1,18 +0,0 @@ -### Minimal windows service stub - -Programs designed to run from most *nix style operating systems -can import this package to enable running programs as services without modifying -them. - -``` -import _ "github.com/kardianos/minwinsvc" -``` - -If you need more control over the exit behavior, set -``` -minwinsvc.SetOnExit(func() { - // Do something. - // Within 10 seconds call: - os.Exit(0) -}) -``` diff --git a/vendor/github.com/kardianos/minwinsvc/minwinsvc.go b/vendor/github.com/kardianos/minwinsvc/minwinsvc.go deleted file mode 100644 index 057ba7f954..0000000000 --- a/vendor/github.com/kardianos/minwinsvc/minwinsvc.go +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2015 Daniel Theophanes. -// Use of this source code is governed by a zlib-style -// license that can be found in the LICENSE file.package service - -// Minimal non-invasive windows only service stub. -// -// Import to allow running as a windows service. -// import _ "github.com/kardianos/minwinsvc" -// This will detect if running as a windows service -// and install required callbacks for windows. -package minwinsvc - -// SetOnExit sets the function to be called when the windows service -// requests an exit. If this is not called, or if it is called where -// f == nil, then it defaults to calling "os.Exit(0)". -func SetOnExit(f func()) { - setOnExit(f) -} diff --git a/vendor/github.com/kardianos/minwinsvc/svc_other.go b/vendor/github.com/kardianos/minwinsvc/svc_other.go deleted file mode 100644 index 197d30021f..0000000000 --- a/vendor/github.com/kardianos/minwinsvc/svc_other.go +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright 2015 Daniel Theophanes. -// Use of this source code is governed by a zlib-style -// license that can be found in the LICENSE file.package service - -//+build !windows - -package minwinsvc - -func setOnExit(f func()) { - // Nothing. -} diff --git a/vendor/github.com/kardianos/minwinsvc/svc_windows.go b/vendor/github.com/kardianos/minwinsvc/svc_windows.go deleted file mode 100644 index 91e2b6a4dd..0000000000 --- a/vendor/github.com/kardianos/minwinsvc/svc_windows.go +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright 2015 Daniel Theophanes. -// Use of this source code is governed by a zlib-style -// license that can be found in the LICENSE file.package service - -//+build windows - -package minwinsvc - -import ( - "os" - "sync" - - "golang.org/x/sys/windows/svc" -) - -var ( - onExit func() - guard sync.Mutex -) - -func init() { - interactive, err := svc.IsAnInteractiveSession() - if err != nil { - panic(err) - } - if interactive { - return - } - go func() { - _ = svc.Run("", runner{}) - - guard.Lock() - f := onExit - guard.Unlock() - - // Don't hold this lock in user code. - if f != nil { - f() - } - // Make sure we exit. - os.Exit(0) - }() -} - -func setOnExit(f func()) { - guard.Lock() - onExit = f - guard.Unlock() -} - -type runner struct{} - -func (runner) Execute(args []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) { - const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown - changes <- svc.Status{State: svc.StartPending} - - changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} - for { - c := <-r - switch c.Cmd { - case svc.Interrogate: - changes <- c.CurrentStatus - case svc.Stop, svc.Shutdown: - changes <- svc.Status{State: svc.StopPending} - return false, 0 - } - } - - return false, 0 -} diff --git a/vendor/vendor.json b/vendor/vendor.json index 85e0dd968e..cc1b8d5f91 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -302,12 +302,6 @@ "revision": "b2c7a7da5b2995941048f60146e67702a292e468", "revisionTime": "2016-02-12T04:00:40Z" }, - { - "checksumSHA1": "/dBJ2h8Jo359deiC5GQ8ZYzX8M8=", - "path": "github.com/kardianos/minwinsvc", - "revision": "cad6b2b879b0970e4245a20ebf1a81a756e2bb70", - "revisionTime": "2015-11-22T16:33:09Z" - }, { "checksumSHA1": "fh+CcgeUUsnuwSORHYWg0ycbp+4=", "path": "github.com/klauspost/compress/flate",