stop DNS server properly when interrupted with ctrl+c, SIGTERM, SIGHUP or SIGQUIT
This commit is contained in:
parent
b0149972cc
commit
7ddc71006b
20
app.go
20
app.go
@ -7,8 +7,10 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gobuffalo/packr"
|
"github.com/gobuffalo/packr"
|
||||||
@ -164,10 +166,13 @@ func main() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
// Eat all args so that coredns can start happily
|
signal_channel := make(chan os.Signal)
|
||||||
if len(os.Args) > 1 {
|
signal.Notify(signal_channel, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGQUIT)
|
||||||
os.Args = os.Args[:1]
|
go func() {
|
||||||
}
|
<-signal_channel
|
||||||
|
cleanup()
|
||||||
|
os.Exit(0)
|
||||||
|
}()
|
||||||
|
|
||||||
// Save the updated config
|
// Save the updated config
|
||||||
err := config.write()
|
err := config.write()
|
||||||
@ -192,6 +197,13 @@ func main() {
|
|||||||
log.Fatal(http.ListenAndServe(address, nil))
|
log.Fatal(http.ListenAndServe(address, nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func cleanup() {
|
||||||
|
err := stopDNSServer()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("Couldn't stop DNS server: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func getInput() (string, error) {
|
func getInput() (string, error) {
|
||||||
scanner := bufio.NewScanner(os.Stdin)
|
scanner := bufio.NewScanner(os.Stdin)
|
||||||
scanner.Scan()
|
scanner.Scan()
|
||||||
|
13
coredns.go
13
coredns.go
@ -74,3 +74,16 @@ func reconfigureDNSServer() error {
|
|||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func stopDNSServer() error {
|
||||||
|
if !isRunning() {
|
||||||
|
return fmt.Errorf("Refusing to stop forwarding DNS server: not running")
|
||||||
|
}
|
||||||
|
|
||||||
|
err := dnsServer.Stop()
|
||||||
|
if err != nil {
|
||||||
|
return errorx.Decorate(err, "Couldn't stop forwarding DNS server")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user