2021-07-26 13:09:12 +02:00
|
|
|
//go:build !windows
|
2021-02-26 23:37:07 +01:00
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
package procutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2020-04-30 01:15:39 +02:00
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/logger"
|
2019-05-22 23:16:55 +02:00
|
|
|
)
|
|
|
|
|
2019-06-03 15:52:08 +02:00
|
|
|
// WaitForSigterm waits for either SIGTERM or SIGINT
|
2019-05-22 23:16:55 +02:00
|
|
|
//
|
|
|
|
// Returns the caught signal.
|
2020-04-30 01:09:27 +02:00
|
|
|
//
|
|
|
|
// It also prevent from program termination on SIGHUP signal,
|
|
|
|
// since this signal is frequently used for config reloading.
|
2019-05-22 23:16:55 +02:00
|
|
|
func WaitForSigterm() os.Signal {
|
|
|
|
ch := make(chan os.Signal, 1)
|
2020-05-05 08:27:38 +02:00
|
|
|
signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGHUP)
|
2020-04-30 01:09:27 +02:00
|
|
|
for {
|
|
|
|
sig := <-ch
|
|
|
|
if sig == syscall.SIGHUP {
|
|
|
|
// Prevent from the program stop on SIGHUP
|
|
|
|
continue
|
|
|
|
}
|
2022-10-20 20:40:17 +02:00
|
|
|
// Stop listening for SIGINT and SIGTERM signals,
|
|
|
|
// so the app could be interrupted by sending these signals again
|
|
|
|
// in the case if the caller doesn't finish the app gracefully.
|
|
|
|
signal.Stop(ch)
|
2020-04-30 01:09:27 +02:00
|
|
|
return sig
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2020-04-30 01:15:39 +02:00
|
|
|
|
|
|
|
// SelfSIGHUP sends SIGHUP signal to the current process.
|
|
|
|
func SelfSIGHUP() {
|
|
|
|
if err := syscall.Kill(syscall.Getpid(), syscall.SIGHUP); err != nil {
|
|
|
|
logger.Panicf("FATAL: cannot send SIGHUP to itself: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2020-05-05 08:27:38 +02:00
|
|
|
|
|
|
|
// NewSighupChan returns a channel, which is triggered on every SIGHUP.
|
|
|
|
func NewSighupChan() <-chan os.Signal {
|
|
|
|
ch := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(ch, syscall.SIGHUP)
|
|
|
|
return ch
|
|
|
|
}
|