aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2018-05-03 14:50:57 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2018-05-03 14:55:10 +0200
commit209dd22ea0c6ea06aca47cb53f68ae2cf0d40831 (patch)
treeb65d08d1a5678bf93279570aaf796eb19511db6e
parentStart to dust off Darwin (diff)
downloadwireguard-go-209dd22ea0c6ea06aca47cb53f68ae2cf0d40831.tar.xz
wireguard-go-209dd22ea0c6ea06aca47cb53f68ae2cf0d40831.zip
Daemonize with environment variable
-rw-r--r--daemon.go25
-rw-r--r--main.go25
2 files changed, 19 insertions, 31 deletions
diff --git a/daemon.go b/daemon.go
deleted file mode 100644
index e2ded87..0000000
--- a/daemon.go
+++ /dev/null
@@ -1,25 +0,0 @@
-package main
-
-import (
- "os"
-)
-
-func Daemonize(attr *os.ProcAttr) error {
- path, err := os.Executable()
- if err != nil {
- return err
- }
-
- argv := []string{os.Args[0], "--foreground"}
- argv = append(argv, os.Args[1:]...)
- process, err := os.StartProcess(
- path,
- argv,
- attr,
- )
- if err != nil {
- return err
- }
- process.Release()
- return nil
-}
diff --git a/main.go b/main.go
index 3358469..f0705c8 100644
--- a/main.go
+++ b/main.go
@@ -4,7 +4,6 @@ import (
"fmt"
"os"
"os/signal"
- "runtime"
"strconv"
)
@@ -16,6 +15,7 @@ const (
const (
ENV_WG_TUN_FD = "WG_TUN_FD"
ENV_WG_UAPI_FD = "WG_UAPI_FD"
+ ENV_WG_PROCESS_FOREGROUND = "WG_PROCESS_FOREGROUND"
)
func printUsage() {
@@ -55,6 +55,10 @@ func main() {
interfaceName = os.Args[1]
}
+ if !foreground {
+ foreground = os.Getenv(ENV_WG_PROCESS_FOREGROUND) == "1"
+ }
+
// get log level (default: info)
logLevel := func() int {
@@ -129,6 +133,7 @@ func main() {
env := os.Environ()
env = append(env, fmt.Sprintf("%s=3", ENV_WG_TUN_FD))
env = append(env, fmt.Sprintf("%s=4", ENV_WG_UAPI_FD))
+ env = append(env, fmt.Sprintf("%s=1", ENV_WG_PROCESS_FOREGROUND))
attr := &os.ProcAttr{
Files: []*os.File{
nil, // stdin
@@ -140,18 +145,26 @@ func main() {
Dir: ".",
Env: env,
}
- err = Daemonize(attr)
+
+ path, err := os.Executable()
+ if err != nil {
+ logger.Error.Println("Failed to determine executable:", err)
+ os.Exit(ExitSetupFailed)
+ }
+
+ process, err := os.StartProcess(
+ path,
+ os.Args,
+ attr,
+ )
if err != nil {
logger.Error.Println("Failed to daemonize:", err)
os.Exit(ExitSetupFailed)
}
+ process.Release()
return
}
- // increase number of go workers (for Go <1.5)
-
- runtime.GOMAXPROCS(runtime.NumCPU())
-
// create wireguard device
device := NewDevice(tun, logger)