move swap on/off variants into same file
[trinity.git] / signals.c
bloba6cac95aa2ba3388ad0f25f2d0901b7c9762aefa
1 #include <stdlib.h>
2 #include <signal.h>
3 #include <unistd.h>
5 #include "trinity.h" // __unused__
6 #include "params.h" // debug
7 #include "pids.h"
8 #include "signals.h"
9 #include "shm.h"
11 jmp_buf ret_jump;
13 int sigwas;
15 static void ctrlc_handler(__unused__ int sig)
17 shm->exit_reason = EXIT_SIGINT;
20 static void sighandler(int sig)
22 int slot;
24 sigwas = sig;
26 switch (sig) {
27 case SIGALRM:
28 slot = find_pid_slot(getpid());
29 if (slot == PIDSLOT_NOT_FOUND)
30 _exit(EXIT_SUCCESS); /* Hell knows what happened, just bail. */
32 /* Check if we're blocking because we're stuck on an fd. */
33 if (check_if_fd(slot) == TRUE) {
35 /* avoid doing it again from other threads. */
36 shm->fd_lifetime = 0;
38 /* TODO: Somehow mark the fd in the parent not to be used again too. */
41 /* Re-arm the alarm. */
42 alarm(1);
44 /* TODO: If we get back here after the 10s alarm, we should exit instead of longjmp */
46 /* Jump back, maybe we'll make progress. */
47 (void)signal(sig, sighandler);
48 siglongjmp(ret_jump, 1);
49 break;
51 default:
52 _exit(EXIT_SUCCESS);
56 void mask_signals_child(void)
58 struct sigaction sa;
59 sigset_t ss;
60 int i;
62 for (i = 1; i < 512; i++) {
63 (void)sigfillset(&ss);
64 sa.sa_flags = SA_RESTART;
65 sa.sa_handler = sighandler;
66 sa.sa_mask = ss;
67 (void)sigaction(i, &sa, NULL);
69 /* we want default behaviour for child process signals */
70 (void)signal(SIGCHLD, SIG_DFL);
72 /* ignore signals we don't care about */
73 (void)signal(SIGFPE, SIG_IGN);
74 (void)signal(SIGXCPU, SIG_IGN);
75 (void)signal(SIGTSTP, SIG_IGN);
76 (void)signal(SIGWINCH, SIG_IGN);
77 (void)signal(SIGIO, SIG_IGN);
78 (void)signal(SIGPIPE, SIG_IGN);
80 /* Ignore the RT signals. */
81 for (i = SIGRTMIN; i <= SIGRTMAX; i++)
82 (void)signal(i, SIG_IGN);
84 /* If we are in debug mode, we want segfaults and core dumps */
85 if (debug == TRUE) {
86 (void)signal(SIGABRT, SIG_DFL);
87 (void)signal(SIGSEGV, SIG_DFL);
90 /* trap ctrl-c */
91 (void)signal(SIGINT, ctrlc_handler);
95 void setup_main_signals(void)
97 /* we want default behaviour for child process signals */
98 (void)signal(SIGFPE, SIG_DFL);
99 (void)signal(SIGCHLD, SIG_DFL);
101 (void)signal(SIGINT, ctrlc_handler);