1//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris2// +build darwin dragonfly freebsd linux netbsd openbsd solaris34/*5Maddy Mail Server - Composable all-in-one email server.6Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors78This program is free software: you can redistribute it and/or modify9it under the terms of the GNU General Public License as published by10the Free Software Foundation, either version 3 of the License, or11(at your option) any later version.1213This program is distributed in the hope that it will be useful,14but WITHOUT ANY WARRANTY; without even the implied warranty of15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the16GNU General Public License for more details.1718You should have received a copy of the GNU General Public License19along with this program. If not, see <https://www.gnu.org/licenses/>.20*/2122package maddy2324import (25 "os"26 "os/signal"27 "syscall"2829 "github.com/foxcpp/maddy/framework/hooks"30 "github.com/foxcpp/maddy/framework/log"31)3233// handleSignals function creates and listens on OS signals channel.34//35// OS-specific signals that correspond to the program termination36// (SIGTERM, SIGHUP, SIGINT) will cause this function to return.37//38// SIGUSR1 will call reinitLogging without returning.39func handleSignals() os.Signal {40 sig := make(chan os.Signal, 5)41 signal.Notify(sig, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP, syscall.SIGINT, syscall.SIGUSR1, syscall.SIGUSR2)4243 for {44 switch s := <-sig; s {45 case syscall.SIGUSR1:46 log.Printf("signal received (%s), rotating logs", s.String())47 hooks.RunHooks(hooks.EventLogRotate)48 case syscall.SIGUSR2:49 log.Printf("signal received (%s), reloading state", s.String())50 hooks.RunHooks(hooks.EventReload)51 default:52 go func() {53 s := handleSignals()54 log.Printf("forced shutdown due to signal (%v)!", s)55 os.Exit(1)56 }()5758 log.Printf("signal received (%v), next signal will force immediate shutdown.", s)59 return s60 }61 }62}