Just a fuzzy trim...
[midnight-commander.git] / slang / slsignal.c
blob659a52ccb5aee8b63cf95f20fb075d5724281473
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"
18 /* This function will cause system calls to be restarted after signal if possible */
19 SLSig_Fun_Type *SLsignal (int sig, SLSig_Fun_Type *f)
21 #ifdef SLANG_POSIX_SIGNALS
22 struct sigaction old_sa, new_sa;
24 # ifdef SIGALRM
25 /* We want system calls to be interrupted by SIGALRM. */
26 if (sig == SIGALRM) return SLsignal_intr (sig, f);
27 # endif
29 sigemptyset (&new_sa.sa_mask);
30 new_sa.sa_handler = f;
32 new_sa.sa_flags = 0;
33 # ifdef SA_RESTART
34 new_sa.sa_flags |= SA_RESTART;
35 # endif
37 if (-1 == sigaction (sig, &new_sa, &old_sa))
38 return (SLSig_Fun_Type *) SIG_ERR;
40 return old_sa.sa_handler;
41 #else
42 /* Not POSIX. */
43 return signal (sig, f);
44 #endif
47 /* This function will NOT cause system calls to be restarted after
48 * signal if possible
50 SLSig_Fun_Type *SLsignal_intr (int sig, SLSig_Fun_Type *f)
52 #ifdef SLANG_POSIX_SIGNALS
53 struct sigaction old_sa, new_sa;
55 sigemptyset (&new_sa.sa_mask);
56 new_sa.sa_handler = f;
58 new_sa.sa_flags = 0;
59 # ifdef SA_INTERRUPT
60 new_sa.sa_flags |= SA_INTERRUPT;
61 # endif
63 if (-1 == sigaction (sig, &new_sa, &old_sa))
64 return (SLSig_Fun_Type *) SIG_ERR;
66 return old_sa.sa_handler;
67 #else
68 /* Not POSIX. */
69 return signal (sig, f);
70 #endif
74 /* We are primarily interested in blocking signals that would cause the
75 * application to reset the tty. These include suspend signals and
76 * possibly interrupt signals.
78 #ifdef SLANG_POSIX_SIGNALS
79 static sigset_t Old_Signal_Mask;
80 #endif
82 static volatile unsigned int Blocked_Depth;
84 int SLsig_block_signals (void)
86 #ifdef SLANG_POSIX_SIGNALS
87 sigset_t new_mask;
88 #endif
90 Blocked_Depth++;
91 if (Blocked_Depth != 1)
93 return 0;
96 #ifdef SLANG_POSIX_SIGNALS
97 sigemptyset (&new_mask);
98 # ifdef SIGQUIT
99 sigaddset (&new_mask, SIGQUIT);
100 # endif
101 # ifdef SIGTSTP
102 sigaddset (&new_mask, SIGTSTP);
103 # endif
104 # ifdef SIGINT
105 sigaddset (&new_mask, SIGINT);
106 # endif
107 # ifdef SIGTTIN
108 sigaddset (&new_mask, SIGTTIN);
109 # endif
110 # ifdef SIGTTOU
111 sigaddset (&new_mask, SIGTTOU);
112 # endif
114 (void) sigprocmask (SIG_BLOCK, &new_mask, &Old_Signal_Mask);
115 return 0;
116 #else
117 /* Not implemented. */
118 return -1;
119 #endif
122 int SLsig_unblock_signals (void)
124 if (Blocked_Depth == 0)
125 return -1;
127 Blocked_Depth--;
129 if (Blocked_Depth != 0)
130 return 0;
132 #ifdef SLANG_POSIX_SIGNALS
133 (void) sigprocmask (SIG_SETMASK, &Old_Signal_Mask, NULL);
134 return 0;
135 #else
136 return -1;
137 #endif