Giovanni's Diary > Subjects > Programming > Gists >
C / set_signals.c
Setup an handler for various POSIX signals.
// SPDX-License-Identifier: MIT // Author: Giovanni Santini // Github: @San7o #define _POSIX_SOURCE #include <signal.h> #include <stddef.h> #include <stdio.h> void sig_shutdown_handler(int signum) { (void) signum; // ... return; } // Setup SIGINT, SIGTERM and SIGQUIT int set_signals(void) { struct sigaction act; act.sa_handler = sig_shutdown_handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); if (sigaction(SIGINT, &act, NULL) == -1) // Ctrl+C { perror("Error setting SIGINT"); return 1; } if (sigaction(SIGTERM, &act, NULL) == -1) // hard shutdown { perror("Error setting SIGTERM"); return 1; } if (sigaction(SIGQUIT, &act, NULL) == -1) // hard shutdown { perror("Error setting SIGQUIT"); return 1; } return 0; }