1 // RUN: %clangxx -std=c++11 -O0 -g %s -o %t && %run %t 2>&1 | FileCheck %s
9 #include <initializer_list>
11 constexpr int std_signals
[] = {
42 constexpr int no_change_act_signals
[] = {
47 void signal_handler(int) {}
48 void signal_action_handler(int, siginfo_t
*, void*) {}
50 void test_signal_custom() {
51 for (int signum
: std_signals
) {
52 auto* ret
= signal(signum
, &signal_handler
);
53 assert(ret
!= SIG_ERR
);
56 for (int signum
= SIGRTMIN
; signum
<= SIGRTMAX
; ++signum
) {
57 auto* ret
= signal(signum
, &signal_handler
);
58 assert(ret
!= SIG_ERR
);
61 for (int signum
: no_change_act_signals
) {
62 auto* ret
= signal(signum
, &signal_handler
);
64 assert(ret
== SIG_ERR
);
65 assert(err
== EINVAL
);
73 auto* ret
= signal(signum
, &signal_handler
);
75 assert(ret
== SIG_ERR
);
76 assert(err
== EINVAL
);
80 void test_signal_ignore() {
81 for (int signum
: std_signals
) {
82 auto* ret
= signal(signum
, SIG_IGN
);
83 if (signum
!= SIGCHLD
) {
84 // POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN
85 // though POSIX.1-2001 and later allow this possibility.
86 assert(ret
!= SIG_ERR
);
90 for (int signum
= SIGRTMIN
; signum
<= SIGRTMAX
; ++signum
) {
91 auto* ret
= signal(signum
, SIG_IGN
);
92 assert(ret
!= SIG_ERR
);
95 for (int signum
: no_change_act_signals
) {
96 auto* ret
= signal(signum
, SIG_IGN
);
98 assert(ret
== SIG_ERR
);
99 assert(err
== EINVAL
);
107 auto* ret
= signal(signum
, SIG_IGN
);
109 assert(ret
== SIG_ERR
);
110 assert(err
== EINVAL
);
114 void test_signal_default() {
115 for (int signum
: std_signals
) {
116 auto* ret
= signal(signum
, SIG_DFL
);
117 assert(ret
!= SIG_ERR
);
120 for (int signum
= SIGRTMIN
; signum
<= SIGRTMAX
; ++signum
) {
121 auto* ret
= signal(signum
, SIG_DFL
);
122 assert(ret
!= SIG_ERR
);
131 auto* ret
= signal(signum
, SIG_DFL
);
133 assert(ret
== SIG_ERR
);
134 assert(err
== EINVAL
);
138 void test_sigaction_custom() {
139 struct sigaction act
= {}, oldact
;
141 act
.sa_handler
= &signal_handler
;
142 sigemptyset(&act
.sa_mask
);
145 for (int signum
: std_signals
) {
146 int ret
= sigaction(signum
, &act
, &oldact
);
150 for (int signum
= SIGRTMIN
; signum
<= SIGRTMAX
; ++signum
) {
151 int ret
= sigaction(signum
, &act
, &oldact
);
155 for (int signum
: no_change_act_signals
) {
156 int ret
= sigaction(signum
, &act
, &oldact
);
159 assert(err
== EINVAL
);
167 int ret
= sigaction(signum
, &act
, &oldact
);
170 assert(err
== EINVAL
);
173 act
.sa_handler
= nullptr;
174 act
.sa_sigaction
= &signal_action_handler
;
175 act
.sa_flags
= SA_SIGINFO
;
177 for (int signum
: std_signals
) {
178 int ret
= sigaction(signum
, &act
, &oldact
);
182 for (int signum
= SIGRTMIN
; signum
<= SIGRTMAX
; ++signum
) {
183 int ret
= sigaction(signum
, &act
, &oldact
);
187 for (int signum
: no_change_act_signals
) {
188 int ret
= sigaction(signum
, &act
, &oldact
);
191 assert(err
== EINVAL
);
199 int ret
= sigaction(signum
, &act
, &oldact
);
202 assert(err
== EINVAL
);
206 void test_sigaction_ignore() {
207 struct sigaction act
= {}, oldact
;
209 act
.sa_handler
= SIG_IGN
;
210 sigemptyset(&act
.sa_mask
);
213 for (int signum
: std_signals
) {
214 int ret
= sigaction(signum
, &act
, &oldact
);
215 if (signum
!= SIGCHLD
) {
216 // POSIX.1-1990 disallowed setting the action for SIGCHLD to SIG_IGN
217 // though POSIX.1-2001 and later allow this possibility.
222 for (int signum
= SIGRTMIN
; signum
<= SIGRTMAX
; ++signum
) {
223 int ret
= sigaction(signum
, &act
, &oldact
);
227 for (int signum
: no_change_act_signals
) {
228 int ret
= sigaction(signum
, &act
, &oldact
);
231 assert(err
== EINVAL
);
239 int ret
= sigaction(signum
, &act
, &oldact
);
242 assert(err
== EINVAL
);
246 void test_sigaction_default() {
247 struct sigaction act
= {}, oldact
;
249 act
.sa_handler
= SIG_DFL
;
250 sigemptyset(&act
.sa_mask
);
253 for (int signum
: std_signals
) {
254 int ret
= sigaction(signum
, &act
, &oldact
);
258 for (int signum
= SIGRTMIN
; signum
<= SIGRTMAX
; ++signum
) {
259 int ret
= sigaction(signum
, &act
, &oldact
);
269 int ret
= sigaction(signum
, &act
, &oldact
);
272 assert(err
== EINVAL
);
277 printf("sigaction\n");
279 test_signal_custom();
280 test_signal_ignore();
281 test_signal_default();
283 test_sigaction_custom();
284 test_sigaction_ignore();
285 test_sigaction_default();