]> git.ipfire.org Git - people/stevee/guardian.git/blobdiff - guardian
Capture process signals.
[people/stevee/guardian.git] / guardian
index 3435c096690ec8a8f4d27342b51b7e17416c5e5b..2ec8ac4b4cf59cd7399b4767d5a2e9183dc1cb23 100644 (file)
--- a/guardian
+++ b/guardian
@@ -102,6 +102,9 @@ while(1) {
 ## is starting.
 #
 sub Init () {
+       # Setup signal handler.
+       &SignalHandler();
+
        # Setup IPC mechanism via Socket in an own thread.
        threads->create(\&Socket);
 
@@ -260,3 +263,29 @@ sub Init_fileposition ($) {
        # Return the position.
        return $position;
 }
+
+#
+## Function for capturing process signals.
+#
+## This function captures any sent process signals and will call various
+## actions, basend on the captured signal.
+#
+sub SignalHandler {
+       $SIG{INT} = \&Shutdown;
+       $SIG{TERM} = \&Shutdown;
+       $SIG{QUIT} = \&Shutdown;
+}
+
+#
+## Shutdown function.
+#
+## This function is used to do a clean shutdown of guardian. It will be called
+## by the signal handler when recieving INT (2), QUIT (3) and TERM (15) signals.
+#
+sub Shutdown () {
+       # Remove socket file on exit.
+       &Guardian::Socket::RemoveSocketFile();
+
+       # Exit guardian.
+       exit;
+}