mirror of
				https://github.com/caddyserver/caddy.git
				synced 2025-11-04 03:27:23 -05:00 
			
		
		
		
	core: Windows service integration (#4790)
Co-authored-by: Matthew Holt <mholt@users.noreply.github.com>
This commit is contained in:
		
							parent
							
								
									2f43aa0629
								
							
						
					
					
						commit
						1e0cdc54f8
					
				@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build gofuzz
 | 
					//go:build gofuzz
 | 
				
			||||||
// +build gofuzz
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package caddyfile
 | 
					package caddyfile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build gofuzz
 | 
					//go:build gofuzz
 | 
				
			||||||
// +build gofuzz
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package caddyfile
 | 
					package caddyfile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build gofuzz
 | 
					//go:build gofuzz
 | 
				
			||||||
// +build gofuzz
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package httpcaddyfile
 | 
					package httpcaddyfile
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build !windows
 | 
					//go:build !windows
 | 
				
			||||||
// +build !windows
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package caddycmd
 | 
					package caddycmd
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build gofuzz
 | 
					//go:build gofuzz
 | 
				
			||||||
// +build gofuzz
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package caddy
 | 
					package caddy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										2
									
								
								go.mod
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								go.mod
									
									
									
									
									
								
							@ -121,7 +121,7 @@ require (
 | 
				
			|||||||
	go.uber.org/atomic v1.9.0 // indirect
 | 
						go.uber.org/atomic v1.9.0 // indirect
 | 
				
			||||||
	go.uber.org/multierr v1.6.0 // indirect
 | 
						go.uber.org/multierr v1.6.0 // indirect
 | 
				
			||||||
	golang.org/x/mod v0.4.2 // indirect
 | 
						golang.org/x/mod v0.4.2 // indirect
 | 
				
			||||||
	golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e // indirect
 | 
						golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e
 | 
				
			||||||
	golang.org/x/text v0.3.8-0.20211004125949-5bd84dd9b33b // indirect
 | 
						golang.org/x/text v0.3.8-0.20211004125949-5bd84dd9b33b // indirect
 | 
				
			||||||
	golang.org/x/tools v0.1.7 // indirect
 | 
						golang.org/x/tools v0.1.7 // indirect
 | 
				
			||||||
	golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
 | 
						golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build gofuzz
 | 
					//go:build gofuzz
 | 
				
			||||||
// +build gofuzz
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package caddy
 | 
					package caddy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build gofuzz
 | 
					//go:build gofuzz
 | 
				
			||||||
// +build gofuzz
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package templates
 | 
					package templates
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -12,6 +12,8 @@
 | 
				
			|||||||
// See the License for the specific language governing permissions and
 | 
					// See the License for the specific language governing permissions and
 | 
				
			||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Package notify provides facilities for notifying process managers
 | 
				
			||||||
 | 
					// of state changes, mainly for when running as a system service.
 | 
				
			||||||
package notify
 | 
					package notify
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
 | 
				
			|||||||
@ -12,19 +12,10 @@
 | 
				
			|||||||
// See the License for the specific language governing permissions and
 | 
					// See the License for the specific language governing permissions and
 | 
				
			||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build !linux
 | 
					//go:build !linux && !windows
 | 
				
			||||||
// +build !linux
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package notify
 | 
					package notify
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func notifyReadiness() error {
 | 
					func notifyReadiness() error { return nil }
 | 
				
			||||||
	return nil
 | 
					func notifyReloading() error { return nil }
 | 
				
			||||||
}
 | 
					func notifyStopping() error  { return nil }
 | 
				
			||||||
 | 
					 | 
				
			||||||
func notifyReloading() error {
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func notifyStopping() error {
 | 
					 | 
				
			||||||
	return nil
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										49
									
								
								notify/notify_windows.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								notify/notify_windows.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,49 @@
 | 
				
			|||||||
 | 
					// Copyright 2015 Matthew Holt and The Caddy Authors
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					// you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					// You may obtain a copy of the License at
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//     http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					// distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					// See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package notify
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import "golang.org/x/sys/windows/svc"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// globalStatus store windows service status, it can be
 | 
				
			||||||
 | 
					// use to notify caddy status.
 | 
				
			||||||
 | 
					var globalStatus chan<- svc.Status
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func SetGlobalStatus(status chan<- svc.Status) {
 | 
				
			||||||
 | 
						globalStatus = status
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func notifyReadiness() error {
 | 
				
			||||||
 | 
						if globalStatus != nil {
 | 
				
			||||||
 | 
							globalStatus <- svc.Status{
 | 
				
			||||||
 | 
								State:   svc.Running,
 | 
				
			||||||
 | 
								Accepts: svc.AcceptStop | svc.AcceptShutdown,
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func notifyReloading() error {
 | 
				
			||||||
 | 
						if globalStatus != nil {
 | 
				
			||||||
 | 
							globalStatus <- svc.Status{State: svc.StartPending}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func notifyStopping() error {
 | 
				
			||||||
 | 
						if globalStatus != nil {
 | 
				
			||||||
 | 
							globalStatus <- svc.Status{State: svc.StopPending}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build gofuzz
 | 
					//go:build gofuzz
 | 
				
			||||||
// +build gofuzz
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package caddy
 | 
					package caddy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										49
									
								
								service_windows.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								service_windows.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,49 @@
 | 
				
			|||||||
 | 
					// Copyright 2015 Matthew Holt and The Caddy Authors
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Licensed under the Apache License, Version 2.0 (the "License");
 | 
				
			||||||
 | 
					// you may not use this file except in compliance with the License.
 | 
				
			||||||
 | 
					// You may obtain a copy of the License at
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					//     http://www.apache.org/licenses/LICENSE-2.0
 | 
				
			||||||
 | 
					//
 | 
				
			||||||
 | 
					// Unless required by applicable law or agreed to in writing, software
 | 
				
			||||||
 | 
					// distributed under the License is distributed on an "AS IS" BASIS,
 | 
				
			||||||
 | 
					// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 | 
				
			||||||
 | 
					// See the License for the specific language governing permissions and
 | 
				
			||||||
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					package caddy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"github.com/caddyserver/caddy/v2/notify"
 | 
				
			||||||
 | 
						"golang.org/x/sys/windows/svc"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func init() {
 | 
				
			||||||
 | 
						isService, err := svc.IsWindowsService()
 | 
				
			||||||
 | 
						if err != nil || isService {
 | 
				
			||||||
 | 
							return
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						go func() {
 | 
				
			||||||
 | 
							_ = svc.Run("", runner{})
 | 
				
			||||||
 | 
						}()
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					type runner struct{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (runner) Execute(args []string, request <-chan svc.ChangeRequest, status chan<- svc.Status) (bool, uint32) {
 | 
				
			||||||
 | 
						notify.SetGlobalStatus(status)
 | 
				
			||||||
 | 
						status <- svc.Status{State: svc.StartPending}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for {
 | 
				
			||||||
 | 
							req := <-request
 | 
				
			||||||
 | 
							switch req.Cmd {
 | 
				
			||||||
 | 
							case svc.Interrogate:
 | 
				
			||||||
 | 
								status <- req.CurrentStatus
 | 
				
			||||||
 | 
							case svc.Stop, svc.Shutdown:
 | 
				
			||||||
 | 
								status <- svc.Status{State: svc.StopPending}
 | 
				
			||||||
 | 
								exitProcessFromSignal("SIGINT")
 | 
				
			||||||
 | 
								return false, 0
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build windows || plan9 || nacl || js
 | 
					//go:build windows || plan9 || nacl || js
 | 
				
			||||||
// +build windows plan9 nacl js
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package caddy
 | 
					package caddy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -13,7 +13,6 @@
 | 
				
			|||||||
// limitations under the License.
 | 
					// limitations under the License.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
//go:build !windows && !plan9 && !nacl && !js
 | 
					//go:build !windows && !plan9 && !nacl && !js
 | 
				
			||||||
// +build !windows,!plan9,!nacl,!js
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
package caddy
 | 
					package caddy
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user