Copy rpoolmgr.h from trunk
[reactos.git] / rosapps / mc / slang / slsignal.c
1 #include "config.h"
2
3 #include <stdio.h>
4 #include <signal.h>
5
6 #ifdef HAVE_STDLIB_H
7 # include <stdlib.h>
8 #endif
9
10 #ifdef HAVE_UNISTD_H
11 # include <unistd.h>
12 #endif
13
14 #include <errno.h>
15
16 #include "slang.h"
17 #include "_slang.h"
18
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)
21 {
22 #ifdef SLANG_POSIX_SIGNALS
23 struct sigaction old_sa, new_sa;
24
25 # ifdef SIGALRM
26 /* We want system calls to be interrupted by SIGALRM. */
27 if (sig == SIGALRM) return SLsignal_intr (sig, f);
28 # endif
29
30 sigemptyset (&new_sa.sa_mask);
31 new_sa.sa_handler = f;
32
33 new_sa.sa_flags = 0;
34 # ifdef SA_RESTART
35 new_sa.sa_flags |= SA_RESTART;
36 # endif
37
38 if (-1 == sigaction (sig, &new_sa, &old_sa))
39 return (SLSig_Fun_Type *) SIG_ERR;
40
41 return old_sa.sa_handler;
42 #else
43 /* Not POSIX. */
44 return signal (sig, f);
45 #endif
46 }
47
48 /* This function will NOT cause system calls to be restarted after
49 * signal if possible
50 */
51 SLSig_Fun_Type *SLsignal_intr (int sig, SLSig_Fun_Type *f)
52 {
53 #ifdef SLANG_POSIX_SIGNALS
54 struct sigaction old_sa, new_sa;
55
56 sigemptyset (&new_sa.sa_mask);
57 new_sa.sa_handler = f;
58
59 new_sa.sa_flags = 0;
60 # ifdef SA_INTERRUPT
61 new_sa.sa_flags |= SA_INTERRUPT;
62 # endif
63
64 if (-1 == sigaction (sig, &new_sa, &old_sa))
65 return (SLSig_Fun_Type *) SIG_ERR;
66
67 return old_sa.sa_handler;
68 #else
69 /* Not POSIX. */
70 return signal (sig, f);
71 #endif
72 }
73
74
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.
78 */
79 #ifdef SLANG_POSIX_SIGNALS
80 static sigset_t Old_Signal_Mask;
81 #endif
82
83 static volatile unsigned int Blocked_Depth;
84
85 int SLsig_block_signals (void)
86 {
87 #ifdef SLANG_POSIX_SIGNALS
88 sigset_t new_mask;
89 #endif
90
91 Blocked_Depth++;
92 if (Blocked_Depth != 1)
93 {
94 return 0;
95 }
96
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
114
115 (void) sigprocmask (SIG_BLOCK, &new_mask, &Old_Signal_Mask);
116 return 0;
117 #else
118 /* Not implemented. */
119 return -1;
120 #endif
121 }
122
123 int SLsig_unblock_signals (void)
124 {
125 if (Blocked_Depth == 0)
126 return -1;
127
128 Blocked_Depth--;
129
130 if (Blocked_Depth != 0)
131 return 0;
132
133 #ifdef SLANG_POSIX_SIGNALS
134 (void) sigprocmask (SIG_SETMASK, &Old_Signal_Mask, NULL);
135 return 0;
136 #else
137 return -1;
138 #endif
139 }