aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/services/boot.go
diff options
context:
space:
mode:
Diffstat (limited to 'services/boot.go')
-rw-r--r--services/boot.go47
1 files changed, 47 insertions, 0 deletions
diff --git a/services/boot.go b/services/boot.go
new file mode 100644
index 00000000..ae7aadda
--- /dev/null
+++ b/services/boot.go
@@ -0,0 +1,47 @@
+/* SPDX-License-Identifier: MIT
+ *
+ * Copyright (C) 2019-2022 WireGuard LLC. All Rights Reserved.
+ */
+
+package services
+
+import (
+ "errors"
+ "log"
+ "sync"
+ "time"
+
+ "golang.org/x/sys/windows"
+ "golang.org/x/sys/windows/svc"
+ "golang.zx2c4.com/wireguard/windows/version"
+)
+
+var (
+ startedAtBoot bool
+ startedAtBootOnce sync.Once
+)
+
+func StartedAtBoot() bool {
+ startedAtBootOnce.Do(func() {
+ if isService, err := svc.IsWindowsService(); err == nil && !isService {
+ return
+ }
+ if reason, err := svc.DynamicStartReason(); err == nil {
+ startedAtBoot = (reason&svc.StartReasonAuto) != 0 || (reason&svc.StartReasonDelayedAuto) != 0
+ } else if errors.Is(err, windows.ERROR_PROC_NOT_FOUND) {
+ // TODO: Below this line is Windows 7 compatibility code, which hopefully we can delete at some point.
+ startedAtBoot = windows.DurationSinceBoot() < time.Minute*10
+ } else {
+ log.Printf("Unable to determine service start reason: %v", err)
+ }
+ })
+ return startedAtBoot
+}
+
+func PrintStarting() {
+ boot := ""
+ if StartedAtBoot() {
+ boot = " at boot"
+ }
+ log.Printf("Starting%s %s", boot, version.UserAgent())
+}