aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/signal.c
diff options
context:
space:
mode:
authorEgor Tensin <Egor.Tensin@gmail.com>2023-05-06 14:07:00 +0200
committerEgor Tensin <Egor.Tensin@gmail.com>2023-05-06 14:07:27 +0200
commit6abf7fc4f8ab149ab97b4670192b869183a73f23 (patch)
tree0518827057c765e99525599745719dae80952b39 /src/signal.c
parentDockerfile: refactoring (diff)
downloadcimple-6abf7fc4f8ab149ab97b4670192b869183a73f23.tar.gz
cimple-6abf7fc4f8ab149ab97b4670192b869183a73f23.zip
get rid of __attribute__((constructor))
Explicit is better than implicit.
Diffstat (limited to '')
-rw-r--r--src/signal.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/signal.c b/src/signal.c
index b5c27ec..d721f67 100644
--- a/src/signal.c
+++ b/src/signal.c
@@ -6,12 +6,55 @@
*/
#include "signal.h"
+#include "compiler.h"
#include "log.h"
#include <signal.h>
+#include <string.h>
volatile sig_atomic_t global_stop_flag = 0;
+static void set_global_stop_flag(UNUSED int signum)
+{
+ global_stop_flag = 1;
+}
+
+static int my_sigaction(int signo, const struct sigaction *act)
+{
+ int ret = 0;
+
+ ret = sigaction(signo, act, NULL);
+ if (ret < 0) {
+ log_errno("sigaction");
+ return ret;
+ }
+
+ return ret;
+}
+
+int signal_install_global_handler()
+{
+ int ret = 0;
+
+ struct sigaction sa;
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = set_global_stop_flag;
+
+ /* Don't care about proper cleanup here; we exit the program if this
+ * fails anyway. */
+ ret = my_sigaction(SIGINT, &sa);
+ if (ret < 0)
+ return ret;
+ ret = my_sigaction(SIGQUIT, &sa);
+ if (ret < 0)
+ return ret;
+ ret = my_sigaction(SIGTERM, &sa);
+ if (ret < 0)
+ return ret;
+
+ return ret;
+}
+
int signal_set(const sigset_t *new, sigset_t *old)
{
int ret = 0;