-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
controlsvc.go
53 lines (45 loc) · 1.51 KB
/
controlsvc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
//go:build windows
// +build windows
// Package controlsvc contains shared code for controlling the Windows agent service.
package controlsvc
import (
"context"
"errors"
"fmt"
"time"
"github.com/DataDog/datadog-agent/pkg/trace/config"
"github.com/DataDog/datadog-agent/pkg/util/winutil"
"golang.org/x/sys/windows/svc"
)
// StartService starts the agent service via the Service Control Manager
func StartService() error {
return winutil.StartService(config.ServiceName)
}
// RestartService restarts the agent service by calling StopService and StartService
func RestartService() error {
err := StopService()
if err == nil {
err = StartService()
}
return err
}
// StopService stops the agent service via the Service Control Manager
func StopService() error {
var err error
if err = winutil.StopService(config.ServiceName); err != nil {
return fmt.Errorf("error stopping service %v", err)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err = winutil.WaitForState(ctx, config.ServiceName, svc.Stopped); err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return fmt.Errorf("timed out waiting for %s service to stop", config.ServiceName)
}
return fmt.Errorf("error stopping service %v", err)
}
return nil
}