From: Urban Wallasch Date: Sun, 7 Apr 2019 20:08:59 +0000 (+0200) Subject: * Replaced old exit strategy by waiting for SIGINT / SIGTERM. X-Git-Url: https://git.packet-gain.de/?a=commitdiff_plain;h=80729fe9540fa9000da55ab17bf3f694518c1eea;p=gogopherd.git * Replaced old exit strategy by waiting for SIGINT / SIGTERM. --- diff --git a/gogopherd.go b/gogopherd.go index af26bcb..a89fe5a 100644 --- a/gogopherd.go +++ b/gogopherd.go @@ -14,15 +14,18 @@ package main import ( "bufio" - "fmt" "io" "io/ioutil" "net" "os" + "os/signal" "path/filepath" "strings" + "syscall" ) +var shutting_down bool = false + func createIndex(dirname string, selector string) (string, error) { fi, err := ioutil.ReadDir(dirname) if check(err, "Readdir "+dirname) != nil { @@ -236,6 +239,9 @@ func handleRequest(conn net.Conn) { func serveTCP(sock net.Listener) { for { conn, err := sock.Accept() + if shutting_down { + return + } checkFatal(err, "Accept") logger.Print("TCP connect from ", conn.RemoteAddr()) go handleRequest(conn) @@ -250,11 +256,14 @@ func main() { defer tsock.Close() logger.Print("listening on TCP ", bindaddr) go serveTCP(tsock) - // send ready signal - fmt.Println("") - // exit on any input on stdin - reader := bufio.NewReader(os.Stdin) - reader.ReadRune() + + // wait for signal + sigchan := make(chan os.Signal) + signal.Notify(sigchan, syscall.SIGINT, syscall.SIGTERM) + sig := <-sigchan + tracer.Print("Received signal: ", sig) + shutting_down = true + tracer.Print("Bye.") } /* EOF */