summaryrefslogtreecommitdiffhomepage
path: root/src/main.go
diff options
context:
space:
mode:
authorMathias Hall-Andersen <mathias@hall-andersen.dk>2017-11-14 18:26:28 +0100
committerMathias Hall-Andersen <mathias@hall-andersen.dk>2017-11-14 18:26:28 +0100
commit88801529fd4097993f7c448b1c3eee0abc8cb51c (patch)
treed1750dc52489b7d5c665918ae522aae5b8fc4f6b /src/main.go
parent69fe86edf0ba371b9b0a54e522ec20d33e0ae129 (diff)
Moved TUN device creation to pre-fork
Diffstat (limited to 'src/main.go')
-rw-r--r--src/main.go102
1 files changed, 69 insertions, 33 deletions
diff --git a/src/main.go b/src/main.go
index eb3c67f..3808c9c 100644
--- a/src/main.go
+++ b/src/main.go
@@ -2,10 +2,14 @@ package main
import (
"fmt"
- "log"
"os"
"os/signal"
"runtime"
+ "strconv"
+)
+
+const (
+ EnvWGTunFD = "WG_TUN_FD"
)
func printUsage() {
@@ -43,28 +47,6 @@ func main() {
interfaceName = os.Args[1]
}
- // daemonize the process
-
- if !foreground {
- err := Daemonize()
- if err != nil {
- log.Println("Failed to daemonize:", err)
- }
- return
- }
-
- // increase number of go workers (for Go <1.5)
-
- runtime.GOMAXPROCS(runtime.NumCPU())
-
- // open TUN device
-
- tun, err := CreateTUN(interfaceName)
- if err != nil {
- log.Println("Failed to create tun device:", err)
- return
- }
-
// get log level (default: info)
logLevel := func() int {
@@ -79,22 +61,76 @@ func main() {
return LogLevelInfo
}()
- // create wireguard device
+ logger := NewLogger(
+ logLevel,
+ fmt.Sprintf("(%s) ", interfaceName),
+ )
+ logger.Debug.Println("Debug log enabled")
- device := NewDevice(tun, logLevel)
+ // open TUN device
- logInfo := device.log.Info
- logError := device.log.Error
- logDebug := device.log.Debug
+ tun, err := func() (TUNDevice, error) {
+ tunFdStr := os.Getenv(EnvWGTunFD)
+ if tunFdStr == "" {
+ return CreateTUN(interfaceName)
+ }
- logInfo.Println("Device started")
- logDebug.Println("Debug log enabled")
+ // construct tun device from supplied FD
+
+ fd, err := strconv.ParseUint(tunFdStr, 10, 32)
+ if err != nil {
+ return nil, err
+ }
+
+ file := os.NewFile(uintptr(fd), "/dev/net/tun")
+ return CreateTUNFromFile(interfaceName, file)
+ }()
+
+ if err != nil {
+ logger.Error.Println("Failed to create TUN device:", err)
+ }
+
+ // daemonize the process
+
+ if !foreground {
+ env := os.Environ()
+ _, ok := os.LookupEnv(EnvWGTunFD)
+ if !ok {
+ kvp := fmt.Sprintf("%s=3", EnvWGTunFD)
+ env = append(env, kvp)
+ }
+ attr := &os.ProcAttr{
+ Files: []*os.File{
+ nil, // stdin
+ nil, // stdout
+ nil, // stderr
+ tun.File(),
+ },
+ Dir: ".",
+ Env: env,
+ }
+ err = Daemonize(attr)
+ if err != nil {
+ logger.Error.Println("Failed to daemonize:", err)
+ }
+ return
+ }
+
+ // increase number of go workers (for Go <1.5)
+
+ runtime.GOMAXPROCS(runtime.NumCPU())
+
+ // create wireguard device
+
+ device := NewDevice(tun, logger)
+ logger.Info.Println("Device started")
// start configuration lister
uapi, err := NewUAPIListener(interfaceName)
if err != nil {
- logError.Fatal("UAPI listen error:", err)
+ logger.Error.Println("UAPI listen error:", err)
+ return
}
errs := make(chan error)
@@ -112,7 +148,7 @@ func main() {
}
}()
- logInfo.Println("UAPI listener started")
+ logger.Info.Println("UAPI listener started")
// wait for program to terminate
@@ -129,5 +165,5 @@ func main() {
uapi.Close()
- logInfo.Println("Closing")
+ logger.Info.Println("Shutting down")
}