2016-04-23 00:16:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-06-23 00:33:17 +00:00
|
|
|
"context"
|
2016-04-23 00:16:14 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-08-24 10:10:50 +00:00
|
|
|
"io"
|
2016-04-23 00:16:14 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2016-10-24 22:34:20 +00:00
|
|
|
"time"
|
2016-04-23 00:16:14 +00:00
|
|
|
|
2023-09-13 15:41:45 +00:00
|
|
|
"github.com/containerd/log"
|
2016-06-22 22:36:51 +00:00
|
|
|
"github.com/spf13/pflag"
|
2016-04-23 00:16:14 +00:00
|
|
|
"golang.org/x/sys/windows"
|
|
|
|
"golang.org/x/sys/windows/svc"
|
|
|
|
"golang.org/x/sys/windows/svc/debug"
|
|
|
|
"golang.org/x/sys/windows/svc/eventlog"
|
|
|
|
"golang.org/x/sys/windows/svc/mgr"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-06-22 22:36:51 +00:00
|
|
|
flServiceName *string
|
|
|
|
flRegisterService *bool
|
|
|
|
flUnregisterService *bool
|
|
|
|
flRunService *bool
|
2016-04-23 00:16:14 +00:00
|
|
|
|
2022-10-11 23:28:42 +00:00
|
|
|
oldStderr windows.Handle
|
|
|
|
panicFile *os.File
|
2016-04-23 00:16:14 +00:00
|
|
|
|
|
|
|
service *handler
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// These should match the values in event_messages.mc.
|
|
|
|
eventInfo = 1
|
|
|
|
eventWarn = 1
|
|
|
|
eventError = 1
|
|
|
|
eventDebug = 2
|
|
|
|
eventPanic = 3
|
|
|
|
eventFatal = 4
|
|
|
|
|
|
|
|
eventExtraOffset = 10 // Add this to any event to get a string that supports extended data
|
|
|
|
)
|
|
|
|
|
2016-06-22 22:36:51 +00:00
|
|
|
func installServiceFlags(flags *pflag.FlagSet) {
|
|
|
|
flServiceName = flags.String("service-name", "docker", "Set the Windows service name")
|
|
|
|
flRegisterService = flags.Bool("register-service", false, "Register the service and exit")
|
|
|
|
flUnregisterService = flags.Bool("unregister-service", false, "Unregister the service and exit")
|
|
|
|
flRunService = flags.Bool("run-service", false, "")
|
2019-08-28 23:51:55 +00:00
|
|
|
_ = flags.MarkHidden("run-service")
|
2016-06-22 22:36:51 +00:00
|
|
|
}
|
|
|
|
|
2016-04-23 00:16:14 +00:00
|
|
|
type handler struct {
|
2016-08-03 16:20:46 +00:00
|
|
|
tosvc chan bool
|
|
|
|
fromsvc chan error
|
|
|
|
daemonCli *DaemonCli
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type etwHook struct {
|
|
|
|
log *eventlog.Log
|
|
|
|
}
|
|
|
|
|
2023-09-15 11:51:51 +00:00
|
|
|
func (h *etwHook) Levels() []log.Level {
|
|
|
|
return []log.Level{
|
|
|
|
log.PanicLevel,
|
|
|
|
log.FatalLevel,
|
|
|
|
log.ErrorLevel,
|
|
|
|
log.WarnLevel,
|
|
|
|
log.InfoLevel,
|
|
|
|
log.DebugLevel,
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 11:51:51 +00:00
|
|
|
func (h *etwHook) Fire(e *log.Entry) error {
|
2016-04-23 00:16:14 +00:00
|
|
|
var (
|
|
|
|
etype uint16
|
|
|
|
eid uint32
|
|
|
|
)
|
|
|
|
|
|
|
|
switch e.Level {
|
2023-09-15 11:51:51 +00:00
|
|
|
case log.PanicLevel:
|
2016-04-23 00:16:14 +00:00
|
|
|
etype = windows.EVENTLOG_ERROR_TYPE
|
|
|
|
eid = eventPanic
|
2023-09-15 11:51:51 +00:00
|
|
|
case log.FatalLevel:
|
2016-04-23 00:16:14 +00:00
|
|
|
etype = windows.EVENTLOG_ERROR_TYPE
|
|
|
|
eid = eventFatal
|
2023-09-15 11:51:51 +00:00
|
|
|
case log.ErrorLevel:
|
2016-04-23 00:16:14 +00:00
|
|
|
etype = windows.EVENTLOG_ERROR_TYPE
|
|
|
|
eid = eventError
|
2023-09-15 11:51:51 +00:00
|
|
|
case log.WarnLevel:
|
2016-04-23 00:16:14 +00:00
|
|
|
etype = windows.EVENTLOG_WARNING_TYPE
|
|
|
|
eid = eventWarn
|
2023-09-15 11:51:51 +00:00
|
|
|
case log.InfoLevel:
|
2016-04-23 00:16:14 +00:00
|
|
|
etype = windows.EVENTLOG_INFORMATION_TYPE
|
|
|
|
eid = eventInfo
|
2023-09-15 11:51:51 +00:00
|
|
|
case log.DebugLevel:
|
2016-04-23 00:16:14 +00:00
|
|
|
etype = windows.EVENTLOG_INFORMATION_TYPE
|
|
|
|
eid = eventDebug
|
|
|
|
default:
|
|
|
|
return errors.New("unknown level")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is additional data, include it as a second string.
|
|
|
|
exts := ""
|
|
|
|
if len(e.Data) > 0 {
|
|
|
|
fs := bytes.Buffer{}
|
|
|
|
for k, v := range e.Data {
|
|
|
|
fs.WriteString(k)
|
|
|
|
fs.WriteByte('=')
|
|
|
|
fmt.Fprint(&fs, v)
|
|
|
|
fs.WriteByte(' ')
|
|
|
|
}
|
|
|
|
|
|
|
|
exts = fs.String()[:fs.Len()-1]
|
|
|
|
eid += eventExtraOffset
|
|
|
|
}
|
|
|
|
|
|
|
|
if h.log == nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "%s [%s]\n", e.Message, exts)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
ss [2]*uint16
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
2017-05-23 14:22:32 +00:00
|
|
|
ss[0], err = windows.UTF16PtrFromString(e.Message)
|
2016-04-23 00:16:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
count := uint16(1)
|
|
|
|
if exts != "" {
|
2017-05-23 14:22:32 +00:00
|
|
|
ss[1], err = windows.UTF16PtrFromString(exts)
|
2016-04-23 00:16:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
count++
|
|
|
|
}
|
|
|
|
|
|
|
|
return windows.ReportEvent(h.log.Handle, etype, 0, eid, 0, count, 0, &ss[0], nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerService() error {
|
windows: fix --register-service when executed from within binary directory
Go 1.15.7 contained a security fix for CVE-2021-3115, which allowed arbitrary
code to be executed at build time when using cgo on Windows.
This issue was not limited to the go command itself, and could also affect binaries
that use `os.Command`, `os.LookPath`, etc.
From the related blogpost (https://blog.golang.org/path-security):
> Are your own programs affected?
>
> If you use exec.LookPath or exec.Command in your own programs, you only need to
> be concerned if you (or your users) run your program in a directory with untrusted
> contents. If so, then a subprocess could be started using an executable from dot
> instead of from a system directory. (Again, using an executable from dot happens
> always on Windows and only with uncommon PATH settings on Unix.)
>
> If you are concerned, then we’ve published the more restricted variant of os/exec
> as golang.org/x/sys/execabs. You can use it in your program by simply replacing
At time of the go1.15 release, the Go team considered changing the behavior of
`os.LookPath()` and `exec.LookPath()` to be a breaking change, and made the
behavior "opt-in" by providing the `golang.org/x/sys/execabs` package as a
replacement.
However, for the go1.19 release, this changed, and the default behavior of
`os.LookPath()` and `exec.LookPath()` was changed. From the release notes:
https://go.dev/doc/go1.19#os-exec-path
> Command and LookPath no longer allow results from a PATH search to be found
> relative to the current directory. This removes a common source of security
> problems but may also break existing programs that depend on using, say,
> exec.Command("prog") to run a binary named prog (or, on Windows, prog.exe)
> in the current directory. See the os/exec package documentation for information
> about how best to update such programs.
>
> On Windows, Command and LookPath now respect the NoDefaultCurrentDirectoryInExePath
> environment variable, making it possible to disable the default implicit search
> of “.” in PATH lookups on Windows systems.
A result of this change was that registering the daemon as a Windows service
no longer worked when done from within the directory of the binary itself:
C:\> cd "Program Files\Docker\Docker\resources"
C:\Program Files\Docker\Docker\resources> dockerd --register-service
exec: "dockerd": cannot run executable found relative to current directory
Note that using an absolute path would work around the issue:
C:\Program Files\Docker\Docker>resources\dockerd.exe --register-service
This patch changes `registerService()` to use `os.Executable()`, instead of
depending on `os.Args[0]` and `exec.LookPath()` for resolving the absolute
path of the binary.
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2023-07-27 14:20:49 +00:00
|
|
|
p, err := os.Executable()
|
2016-04-23 00:16:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m, err := mgr.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer m.Disconnect()
|
2017-01-12 22:00:56 +00:00
|
|
|
|
2016-04-23 00:16:14 +00:00
|
|
|
c := mgr.Config{
|
|
|
|
ServiceType: windows.SERVICE_WIN32_OWN_PROCESS,
|
|
|
|
StartType: mgr.StartAutomatic,
|
|
|
|
ErrorControl: mgr.ErrorNormal,
|
2022-02-17 16:08:46 +00:00
|
|
|
Dependencies: []string{},
|
2016-04-23 00:16:14 +00:00
|
|
|
DisplayName: "Docker Engine",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the service to launch with the arguments that were just passed.
|
|
|
|
args := []string{"--run-service"}
|
|
|
|
for _, a := range os.Args[1:] {
|
|
|
|
if a != "--register-service" && a != "--unregister-service" {
|
|
|
|
args = append(args, a)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := m.CreateService(*flServiceName, p, c, args...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
2016-10-24 22:34:20 +00:00
|
|
|
|
2022-10-11 23:58:50 +00:00
|
|
|
err = s.SetRecoveryActions(
|
|
|
|
[]mgr.RecoveryAction{
|
2022-10-12 00:01:33 +00:00
|
|
|
{Type: mgr.ServiceRestart, Delay: 15 * time.Second},
|
|
|
|
{Type: mgr.ServiceRestart, Delay: 15 * time.Second},
|
2022-10-11 23:58:50 +00:00
|
|
|
{Type: mgr.NoAction},
|
|
|
|
},
|
|
|
|
uint32(24*time.Hour/time.Second),
|
2016-10-24 22:34:20 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-14 21:16:12 +00:00
|
|
|
return eventlog.Install(*flServiceName, p, false, eventlog.Info|eventlog.Warning|eventlog.Error)
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unregisterService() error {
|
|
|
|
m, err := mgr.Connect()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer m.Disconnect()
|
|
|
|
|
|
|
|
s, err := m.OpenService(*flServiceName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer s.Close()
|
|
|
|
|
|
|
|
eventlog.Remove(*flServiceName)
|
|
|
|
err = s.Delete()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-08 23:18:30 +00:00
|
|
|
// initService is the entry point for running the daemon as a Windows
|
|
|
|
// service. It returns an indication to stop (if registering/un-registering);
|
|
|
|
// an indication of whether it is running as a service; and an error.
|
|
|
|
func initService(daemonCli *DaemonCli) (bool, bool, error) {
|
2016-04-23 00:16:14 +00:00
|
|
|
if *flUnregisterService {
|
|
|
|
if *flRegisterService {
|
2017-03-08 23:18:30 +00:00
|
|
|
return true, false, errors.New("--register-service and --unregister-service cannot be used together")
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
2017-03-08 23:18:30 +00:00
|
|
|
return true, false, unregisterService()
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if *flRegisterService {
|
2017-03-08 23:18:30 +00:00
|
|
|
return true, false, registerService()
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !*flRunService {
|
2017-03-08 23:18:30 +00:00
|
|
|
return false, false, nil
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
|
2022-10-11 20:54:40 +00:00
|
|
|
// Check if we're running as a Windows service or interactively.
|
|
|
|
isService, err := svc.IsWindowsService()
|
2016-04-23 00:16:14 +00:00
|
|
|
if err != nil {
|
2017-03-08 23:18:30 +00:00
|
|
|
return false, false, err
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h := &handler{
|
2016-08-03 16:20:46 +00:00
|
|
|
tosvc: make(chan bool),
|
|
|
|
fromsvc: make(chan error),
|
|
|
|
daemonCli: daemonCli,
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
|
2023-09-15 11:51:51 +00:00
|
|
|
var eventLog *eventlog.Log
|
2022-10-11 20:54:40 +00:00
|
|
|
if isService {
|
2023-09-15 11:51:51 +00:00
|
|
|
eventLog, err = eventlog.Open(*flServiceName)
|
2016-04-23 00:16:14 +00:00
|
|
|
if err != nil {
|
2017-03-08 23:18:30 +00:00
|
|
|
return false, false, err
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-15 11:51:51 +00:00
|
|
|
log.L.Logger.AddHook(&etwHook{eventLog})
|
|
|
|
log.L.Logger.SetOutput(io.Discard)
|
2016-04-23 00:16:14 +00:00
|
|
|
|
|
|
|
service = h
|
|
|
|
go func() {
|
2022-10-11 20:54:40 +00:00
|
|
|
if isService {
|
2016-04-23 00:16:14 +00:00
|
|
|
err = svc.Run(*flServiceName, h)
|
2022-10-11 20:54:40 +00:00
|
|
|
} else {
|
|
|
|
err = debug.Run(*flServiceName, h)
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.fromsvc <- err
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Wait for the first signal from the service handler.
|
|
|
|
err = <-h.fromsvc
|
|
|
|
if err != nil {
|
2017-03-08 23:18:30 +00:00
|
|
|
return false, false, err
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
2017-03-08 23:18:30 +00:00
|
|
|
return false, true, nil
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) started() error {
|
|
|
|
// This must be delayed until daemonCli initializes Config.Root
|
2016-08-03 16:20:46 +00:00
|
|
|
err := initPanicFile(filepath.Join(h.daemonCli.Config.Root, "panic.log"))
|
2016-04-23 00:16:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
h.tosvc <- false
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) stopped(err error) {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debugf("Stopping service: %v", err)
|
2016-04-23 00:16:14 +00:00
|
|
|
h.tosvc <- err != nil
|
|
|
|
<-h.fromsvc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) Execute(_ []string, r <-chan svc.ChangeRequest, s chan<- svc.Status) (bool, uint32) {
|
|
|
|
s <- svc.Status{State: svc.StartPending, Accepts: 0}
|
|
|
|
// Unblock initService()
|
|
|
|
h.fromsvc <- nil
|
|
|
|
|
|
|
|
// Wait for initialization to complete.
|
|
|
|
failed := <-h.tosvc
|
|
|
|
if failed {
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debug("Aborting service start due to failure during initialization")
|
2016-04-23 00:16:14 +00:00
|
|
|
return true, 1
|
|
|
|
}
|
|
|
|
|
|
|
|
s <- svc.Status{State: svc.Running, Accepts: svc.AcceptStop | svc.AcceptShutdown | svc.Accepted(windows.SERVICE_ACCEPT_PARAMCHANGE)}
|
2023-06-23 00:33:17 +00:00
|
|
|
log.G(context.TODO()).Debug("Service running")
|
2016-04-23 00:16:14 +00:00
|
|
|
Loop:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case failed = <-h.tosvc:
|
|
|
|
break Loop
|
|
|
|
case c := <-r:
|
|
|
|
switch c.Cmd {
|
|
|
|
case svc.Cmd(windows.SERVICE_CONTROL_PARAMCHANGE):
|
2016-08-03 16:20:46 +00:00
|
|
|
h.daemonCli.reloadConfig()
|
2016-04-23 00:16:14 +00:00
|
|
|
case svc.Interrogate:
|
|
|
|
s <- c.CurrentStatus
|
|
|
|
case svc.Stop, svc.Shutdown:
|
|
|
|
s <- svc.Status{State: svc.StopPending, Accepts: 0}
|
2016-08-03 16:20:46 +00:00
|
|
|
h.daemonCli.stop()
|
2016-04-23 00:16:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
removePanicFile()
|
|
|
|
if failed {
|
|
|
|
return true, 1
|
|
|
|
}
|
|
|
|
return false, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func initPanicFile(path string) error {
|
|
|
|
var err error
|
2021-11-02 14:36:14 +00:00
|
|
|
panicFile, err = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o200)
|
2016-04-23 00:16:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := panicFile.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are contents in the file already, move the file out of the way
|
|
|
|
// and replace it.
|
|
|
|
if st.Size() > 0 {
|
|
|
|
panicFile.Close()
|
|
|
|
os.Rename(path, path+".old")
|
|
|
|
panicFile, err = os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update STD_ERROR_HANDLE to point to the panic file so that Go writes to
|
|
|
|
// it when it panics. Remember the old stderr to restore it before removing
|
|
|
|
// the panic file.
|
2022-10-11 23:28:42 +00:00
|
|
|
h, err := windows.GetStdHandle(windows.STD_ERROR_HANDLE)
|
2016-04-23 00:16:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
oldStderr = h
|
|
|
|
|
2022-10-11 23:28:42 +00:00
|
|
|
err = windows.SetStdHandle(windows.STD_ERROR_HANDLE, windows.Handle(panicFile.Fd()))
|
|
|
|
if err != nil {
|
2016-04-23 00:16:14 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-30 22:36:42 +00:00
|
|
|
// Reset os.Stderr to the panic file (so fmt.Fprintf(os.Stderr,...) actually gets redirected)
|
2022-10-11 23:28:42 +00:00
|
|
|
os.Stderr = os.NewFile(panicFile.Fd(), "/dev/stderr")
|
2017-03-30 22:36:42 +00:00
|
|
|
|
|
|
|
// Force threads that panic to write to stderr (the panicFile handle now), otherwise it will go into the ether
|
2023-09-15 11:51:51 +00:00
|
|
|
log.L.Logger.SetOutput(os.Stderr)
|
2017-03-30 22:36:42 +00:00
|
|
|
|
2016-04-23 00:16:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removePanicFile() {
|
|
|
|
if st, err := panicFile.Stat(); err == nil {
|
|
|
|
if st.Size() == 0 {
|
2022-10-11 23:28:42 +00:00
|
|
|
windows.SetStdHandle(windows.STD_ERROR_HANDLE, oldStderr)
|
2016-04-23 00:16:14 +00:00
|
|
|
panicFile.Close()
|
|
|
|
os.Remove(panicFile.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|