瀏覽代碼

Handle SIGHUP signal

世界 3 年之前
父節點
當前提交
aa8cdaee22
共有 1 個文件被更改,包括 14 次插入18 次删除
  1. 14 18
      cmd/sing-box/cmd_run.go

+ 14 - 18
cmd/sing-box/cmd_run.go

@@ -3,7 +3,6 @@ package main
 import (
 	"context"
 	"io"
-	"net/http"
 	"os"
 	"os/signal"
 	runtimeDebug "runtime/debug"
@@ -13,7 +12,6 @@ import (
 	"github.com/sagernet/sing-box/common/json"
 	"github.com/sagernet/sing-box/log"
 	"github.com/sagernet/sing-box/option"
-	"github.com/sagernet/sing/common/debug"
 	E "github.com/sagernet/sing/common/exceptions"
 
 	"github.com/spf13/cobra"
@@ -81,21 +79,19 @@ func create() (*box.Box, context.CancelFunc, error) {
 }
 
 func run() error {
-	instance, cancel, err := create()
-	if err != nil {
-		return err
-	}
-	if debug.Enabled {
-		http.HandleFunc("/debug/close", func(writer http.ResponseWriter, request *http.Request) {
-			cancel()
-			instance.Close()
-		})
+	for {
+		instance, cancel, err := create()
+		if err != nil {
+			return err
+		}
+		runtimeDebug.FreeOSMemory()
+		osSignals := make(chan os.Signal, 1)
+		signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
+		osSignal := <-osSignals
+		cancel()
+		instance.Close()
+		if osSignal != syscall.SIGHUP {
+			return nil
+		}
 	}
-	runtimeDebug.FreeOSMemory()
-	osSignals := make(chan os.Signal, 1)
-	signal.Notify(osSignals, os.Interrupt, syscall.SIGTERM)
-	<-osSignals
-	cancel()
-	instance.Close()
-	return nil
 }