changed author email address
[midnight-commander.git] / slang / slsignal.c
blob7d4c19d17b8023e39f48836f2dafc8465af5a56c
1 #include "config.h"
3 #include <stdio.h>
4 #include <signal.h>
6 #ifdef HAVE_STDLIB_H
7 # include <stdlib.h>
8 #endif
10 #ifdef HAVE_UNISTD_H
11 # include <unistd.h>
12 #endif
14 #include <errno.h>
16 #include "slang.h"
17 #include "_slang.h"
19 /* This function will cause system calls to be restarted after signal if possible */
20 SLSig_Fun_Type *SLsignal (int sig, SLSig_Fun_Type *f)
22 #ifdef SLANG_POSIX_SIGNALS
23 struct sigaction old_sa, new_sa;
25 # ifdef SIGALRM
26 /* We want system calls to be interrupted by SIGALRM. */
27 if (sig == SIGALRM) return SLsignal_intr (sig, f);
28 # endif
30 sigemptyset (&new_sa.sa_mask);
31 new_sa.sa_handler = f;
33 new_sa.sa_flags = 0;
34 # ifdef SA_RESTART
35 new_sa.sa_flags |= SA_RESTART;
36 # endif
38 if (-1 == sigaction (sig, &new_sa, &old_sa))
39 return (SLSig_Fun_Type *) SIG_ERR;
41 return old_sa.sa_handler;
42 #else
43 /* Not POSIX. */
44 return signal (sig, f);
45 #endif
48 /* This function will NOT cause system calls to be restarted after
49 * signal if possible
51 SLSig_Fun_Type *SLsignal_intr (int sig, SLSig_Fun_Type *f)
53 #ifdef SLANG_POSIX_SIGNALS
54 struct sigaction old_sa, new_sa;
56 sigemptyset (&new_sa.sa_mask);
57 new_sa.sa_handler = f;
59 new_sa.sa_flags = 0;
60 # ifdef SA_INTERRUPT
61 new_sa.sa_flags |= SA_INTERRUPT;
62 # endif
64 if (-1 == sigaction (sig, &new_sa, &old_sa))
65 return (SLSig_Fun_Type *) SIG_ERR;
67 return old_sa.sa_handler;
68 #else
69 /* Not POSIX. */
70 return signal (sig, f);
71 #endif
75 /* We are primarily interested in blocking signals that would cause the
76 * application to reset the tty. These include suspend signals and
77 * possibly interrupt signals.
79 #ifdef SLANG_POSIX_SIGNALS
80 static sigset_t Old_Signal_Mask;
81 #endif
83 static volatile unsigned int Blocked_Depth;
85 int SLsig_block_signals (void)
87 #ifdef SLANG_POSIX_SIGNALS
88 sigset_t new_mask;
89 #endif
91 Blocked_Depth++;
92 if (Blocked_Depth != 1)
94 return 0;
97 #ifdef SLANG_POSIX_SIGNALS
98 sigemptyset (&new_mask);
99 # ifdef SIGQUIT
100 sigaddset (&new_mask, SIGQUIT);
101 # endif
102 # ifdef SIGTSTP
103 sigaddset (&new_mask, SIGTSTP);
104 # endif
105 # ifdef SIGINT
106 sigaddset (&new_mask, SIGINT);
107 # endif
108 # ifdef SIGTTIN
109 sigaddset (&new_mask, SIGTTIN);
110 # endif
111 # ifdef SIGTTOU
112 sigaddset (&new_mask, SIGTTOU);
113 # endif
115 (void) sigprocmask (SIG_BLOCK, &new_mask, &Old_Signal_Mask);
116 return 0;
117 #else
118 /* Not implemented. */
119 return -1;
120 #endif
123 int SLsig_unblock_signals (void)
125 if (Blocked_Depth == 0)
126 return -1;
128 Blocked_Depth--;
130 if (Blocked_Depth != 0)
131 return 0;
133 #ifdef SLANG_POSIX_SIGNALS
134 (void) sigprocmask (SIG_SETMASK, &Old_Signal_Mask, NULL);
135 return 0;
136 #else
137 return -1;
138 #endif