maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
 2// +build darwin dragonfly freebsd linux netbsd openbsd solaris
 3
 4/*
 5Maddy Mail Server - Composable all-in-one email server.
 6Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
 7
 8This program is free software: you can redistribute it and/or modify
 9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
12
13This program is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along with this program.  If not, see <https://www.gnu.org/licenses/>.
20*/
21
22package maddy
23
24import (
25	"os"
26	"os/signal"
27	"syscall"
28
29	"github.com/foxcpp/maddy/framework/hooks"
30	"github.com/foxcpp/maddy/framework/log"
31)
32
33// handleSignals function creates and listens on OS signals channel.
34//
35// OS-specific signals that correspond to the program termination
36// (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)
42
43	for {
44		switch s := <-sig; s {
45		case syscall.SIGUSR1:
46			log.Printf("signal received (%s), rotating logs", s.String())
47			systemdStatus(SDReloading, "Reopening logs...")
48			hooks.RunHooks(hooks.EventLogRotate)
49			systemdStatus(SDReady, "Listening for incoming connections...")
50		case syscall.SIGUSR2:
51			log.Printf("signal received (%s), reloading state", s.String())
52			systemdStatus(SDReloading, "Reloading state...")
53			hooks.RunHooks(hooks.EventReload)
54			systemdStatus(SDReady, "Listening for incoming connections...")
55		default:
56			go func() {
57				s := handleSignals()
58				log.Printf("forced shutdown due to signal (%v)!", s)
59				os.Exit(1)
60			}()
61
62			log.Printf("signal received (%v), next signal will force immediate shutdown.", s)
63			return s
64		}
65	}
66}