1*d5c9a868SElliott Hughes /* Copyright 1996,1997,2001,2002,2007,2009 Alain Knaff.
2*d5c9a868SElliott Hughes * This file is part of mtools.
3*d5c9a868SElliott Hughes *
4*d5c9a868SElliott Hughes * Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes * it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes * the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes * (at your option) any later version.
8*d5c9a868SElliott Hughes *
9*d5c9a868SElliott Hughes * Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*d5c9a868SElliott Hughes * GNU General Public License for more details.
13*d5c9a868SElliott Hughes *
14*d5c9a868SElliott Hughes * You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes */
17*d5c9a868SElliott Hughes
18*d5c9a868SElliott Hughes #include "sysincludes.h"
19*d5c9a868SElliott Hughes #include "mtools.h"
20*d5c9a868SElliott Hughes
21*d5c9a868SElliott Hughes #undef got_signal
22*d5c9a868SElliott Hughes
23*d5c9a868SElliott Hughes int got_signal = 0;
24*d5c9a868SElliott Hughes
signal_handler(int dummy UNUSEDP)25*d5c9a868SElliott Hughes static void signal_handler(int dummy UNUSEDP)
26*d5c9a868SElliott Hughes {
27*d5c9a868SElliott Hughes got_signal = 1;
28*d5c9a868SElliott Hughes #if 0
29*d5c9a868SElliott Hughes signal(SIGHUP, SIG_IGN);
30*d5c9a868SElliott Hughes signal(SIGINT, SIG_IGN);
31*d5c9a868SElliott Hughes signal(SIGTERM, SIG_IGN);
32*d5c9a868SElliott Hughes signal(SIGQUIT, SIG_IGN);
33*d5c9a868SElliott Hughes #endif
34*d5c9a868SElliott Hughes }
35*d5c9a868SElliott Hughes
36*d5c9a868SElliott Hughes #if 0
37*d5c9a868SElliott Hughes int do_gotsignal(char *f, int n)
38*d5c9a868SElliott Hughes {
39*d5c9a868SElliott Hughes if(got_signal)
40*d5c9a868SElliott Hughes fprintf(stderr, "file=%s line=%d\n", f, n);
41*d5c9a868SElliott Hughes return got_signal;
42*d5c9a868SElliott Hughes }
43*d5c9a868SElliott Hughes #endif
44*d5c9a868SElliott Hughes
setup_signal(void)45*d5c9a868SElliott Hughes void setup_signal(void)
46*d5c9a868SElliott Hughes {
47*d5c9a868SElliott Hughes /* catch signals */
48*d5c9a868SElliott Hughes #ifdef SIGHUP
49*d5c9a868SElliott Hughes signal(SIGHUP, signal_handler);
50*d5c9a868SElliott Hughes #endif
51*d5c9a868SElliott Hughes #ifdef SIGINT
52*d5c9a868SElliott Hughes signal(SIGINT, signal_handler);
53*d5c9a868SElliott Hughes #endif
54*d5c9a868SElliott Hughes #ifdef SIGTERM
55*d5c9a868SElliott Hughes signal(SIGTERM, signal_handler);
56*d5c9a868SElliott Hughes #endif
57*d5c9a868SElliott Hughes #ifdef SIGQUIT
58*d5c9a868SElliott Hughes signal(SIGQUIT, signal_handler);
59*d5c9a868SElliott Hughes #endif
60*d5c9a868SElliott Hughes }
61*d5c9a868SElliott Hughes
62*d5c9a868SElliott Hughes #ifdef HAVE_SIGACTION
_allow_interrupt(saved_sig_state * ss,int sig,int slot)63*d5c9a868SElliott Hughes static void _allow_interrupt(saved_sig_state *ss, int sig, int slot)
64*d5c9a868SElliott Hughes {
65*d5c9a868SElliott Hughes struct sigaction new;
66*d5c9a868SElliott Hughes
67*d5c9a868SElliott Hughes memset(&new, 0, sizeof(new));
68*d5c9a868SElliott Hughes new.sa_handler = signal_handler;
69*d5c9a868SElliott Hughes new.sa_flags &= ~SA_RESTART;
70*d5c9a868SElliott Hughes
71*d5c9a868SElliott Hughes if(sigaction(sig, &new, &ss->sa[slot]) < 0) {
72*d5c9a868SElliott Hughes perror("sigaction");
73*d5c9a868SElliott Hughes exit(1);
74*d5c9a868SElliott Hughes }
75*d5c9a868SElliott Hughes }
76*d5c9a868SElliott Hughes #endif
77*d5c9a868SElliott Hughes
78*d5c9a868SElliott Hughes /* Allow syscalls to be interrupted by signal */
allow_interrupts(saved_sig_state * ss)79*d5c9a868SElliott Hughes void allow_interrupts(saved_sig_state *ss)
80*d5c9a868SElliott Hughes {
81*d5c9a868SElliott Hughes #ifdef HAVE_SIGACTION
82*d5c9a868SElliott Hughes
83*d5c9a868SElliott Hughes # ifdef SIGHUP
84*d5c9a868SElliott Hughes _allow_interrupt(ss, SIGINT, 0);
85*d5c9a868SElliott Hughes # endif
86*d5c9a868SElliott Hughes
87*d5c9a868SElliott Hughes # ifdef SIGINT
88*d5c9a868SElliott Hughes _allow_interrupt(ss, SIGINT, 1);
89*d5c9a868SElliott Hughes # endif
90*d5c9a868SElliott Hughes
91*d5c9a868SElliott Hughes # ifdef SIGTERM
92*d5c9a868SElliott Hughes _allow_interrupt(ss, SIGINT, 2);
93*d5c9a868SElliott Hughes # endif
94*d5c9a868SElliott Hughes
95*d5c9a868SElliott Hughes # ifdef SIGQUIT
96*d5c9a868SElliott Hughes _allow_interrupt(ss, SIGINT, 3);
97*d5c9a868SElliott Hughes # endif
98*d5c9a868SElliott Hughes
99*d5c9a868SElliott Hughes #endif
100*d5c9a868SElliott Hughes }
101*d5c9a868SElliott Hughes
102*d5c9a868SElliott Hughes #ifdef HAVE_SIGACTION
_restore_interrupt(saved_sig_state * ss,int sig,int slot)103*d5c9a868SElliott Hughes static void _restore_interrupt(saved_sig_state *ss, int sig, int slot)
104*d5c9a868SElliott Hughes {
105*d5c9a868SElliott Hughes if(sigaction(sig, &ss->sa[slot], NULL) < 0) {
106*d5c9a868SElliott Hughes perror("restore sigaction");
107*d5c9a868SElliott Hughes exit(1);
108*d5c9a868SElliott Hughes }
109*d5c9a868SElliott Hughes }
110*d5c9a868SElliott Hughes #endif
111*d5c9a868SElliott Hughes
112*d5c9a868SElliott Hughes /* Restore syscalls to be interrupted by signal */
restore_interrupts(saved_sig_state * ss)113*d5c9a868SElliott Hughes void restore_interrupts(saved_sig_state *ss)
114*d5c9a868SElliott Hughes {
115*d5c9a868SElliott Hughes #ifdef HAVE_SIGACTION
116*d5c9a868SElliott Hughes
117*d5c9a868SElliott Hughes # ifdef SIGHUP
118*d5c9a868SElliott Hughes _restore_interrupt(ss, SIGINT, 0);
119*d5c9a868SElliott Hughes # endif
120*d5c9a868SElliott Hughes
121*d5c9a868SElliott Hughes # ifdef SIGINT
122*d5c9a868SElliott Hughes _restore_interrupt(ss, SIGINT, 1);
123*d5c9a868SElliott Hughes # endif
124*d5c9a868SElliott Hughes
125*d5c9a868SElliott Hughes # ifdef SIGTERM
126*d5c9a868SElliott Hughes _restore_interrupt(ss, SIGINT, 2);
127*d5c9a868SElliott Hughes # endif
128*d5c9a868SElliott Hughes
129*d5c9a868SElliott Hughes # ifdef SIGQUIT
130*d5c9a868SElliott Hughes _restore_interrupt(ss, SIGINT, 3);
131*d5c9a868SElliott Hughes # endif
132*d5c9a868SElliott Hughes
133*d5c9a868SElliott Hughes #endif
134*d5c9a868SElliott Hughes }
135