initiate: fix Cannot create another system semaphore error (#1653)

This commit is contained in:
Jan-Otto Kröpke 2024-09-28 19:53:12 +02:00 committed by GitHub
parent a1defadf1e
commit b67b930ffc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 30 deletions

View File

@ -39,7 +39,17 @@ import (
) )
func main() { func main() {
os.Exit(run()) exitCode := run()
// If we are running as a service, we need to signal the service control manager that we are done.
if !initiate.IsService {
os.Exit(exitCode)
}
initiate.ExitCodeCh <- exitCode
// Wait for the service control manager to signal that we are done.
<-initiate.StopCh
} }
func run() int { func run() int {

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"os" "os"
"golang.org/x/sys/windows"
"golang.org/x/sys/windows/svc" "golang.org/x/sys/windows/svc"
"golang.org/x/sys/windows/svc/eventlog" "golang.org/x/sys/windows/svc/eventlog"
) )
@ -15,61 +16,80 @@ const (
type windowsExporterService struct{} type windowsExporterService struct{}
var logger *eventlog.Log func (s *windowsExporterService) Execute(_ []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (bool, uint32) {
//nolint:nonamedreturns
func (s *windowsExporterService) Execute(_ []string, r <-chan svc.ChangeRequest, changes chan<- svc.Status) (ssec bool, errno uint32) {
const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown const cmdsAccepted = svc.AcceptStop | svc.AcceptShutdown
changes <- svc.Status{State: svc.StartPending} changes <- svc.Status{State: svc.StartPending}
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted} changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
for c := range r { for {
select {
case exitCodeCh := <-ExitCodeCh:
changes <- svc.Status{State: svc.StopPending}
return true, uint32(exitCodeCh)
case c := <-r:
switch c.Cmd { switch c.Cmd {
case svc.Interrogate: case svc.Interrogate:
changes <- c.CurrentStatus changes <- c.CurrentStatus
case svc.Stop, svc.Shutdown: case svc.Stop, svc.Shutdown:
_ = logger.Info(100, "Service Stop Received") _ = logToEventToLog(windows.EVENTLOG_INFORMATION_TYPE, "service stop received")
changes <- svc.Status{State: svc.StopPending} changes <- svc.Status{State: svc.StopPending}
return return false, 0
default: default:
_ = logger.Error(102, fmt.Sprintf("unexpected control request #%d", c)) _ = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("unexpected control request #%d", c))
}
} }
} }
return
} }
var StopCh = make(chan bool) var (
IsService bool
ExitCodeCh = make(chan int)
StopCh = make(chan struct{})
)
//nolint:gochecknoinits //nolint:gochecknoinits
func init() { func init() {
isService, err := svc.IsWindowsService() var err error
IsService, err = svc.IsWindowsService()
if err != nil { if err != nil {
logger, err = eventlog.Open("windows_exporter") err = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("Failed to detect service: %v", err))
if err != nil { if err != nil {
os.Exit(2) os.Exit(2)
} }
_ = logger.Error(102, fmt.Sprintf("Failed to detect service: %v", err))
os.Exit(1) os.Exit(1)
} }
if isService { if IsService {
logger, err = eventlog.Open("windows_exporter") err = logToEventToLog(windows.EVENTLOG_INFORMATION_TYPE, "Attempting to start exporter service")
if err != nil {
os.Exit(2)
}
_ = logger.Info(100, "Attempting to start exporter service")
go func() { go func() {
err = svc.Run(serviceName, &windowsExporterService{}) err = svc.Run(serviceName, &windowsExporterService{})
if err != nil { if err != nil {
_ = logger.Error(102, fmt.Sprintf("Failed to start service: %v", err)) _ = logToEventToLog(windows.EVENTLOG_ERROR_TYPE, fmt.Sprintf("Failed to start service: %v", err))
} }
StopCh <- true
StopCh <- struct{}{}
}() }()
} }
} }
func logToEventToLog(eType uint16, msg string) error {
eventLog, err := eventlog.Open("windows_exporter")
if err != nil {
return fmt.Errorf("failed to open event log: %w", err)
}
p, err := windows.UTF16PtrFromString(msg)
if err != nil {
return fmt.Errorf("error convert string to UTF-16: %w", err)
}
ss := []*uint16{p, nil, nil, nil, nil, nil, nil, nil, nil}
return windows.ReportEvent(eventLog.Handle, eType, 0, 3299, 0, 9, 0, &ss[0], nil)
}