functional: add mntent test
[libc-test.git] / src / functional / setjmp.c
blob7ba09d38802ba2316626ee0dba1e7bc234930c99
1 #include <string.h>
2 #include <signal.h>
3 #include <setjmp.h>
4 #include "test.h"
6 #define TEST(c, ...) ((c) ? 1 : (t_error(#c" failed: " __VA_ARGS__),0))
8 int main(void)
10 volatile int x = 0, r;
11 jmp_buf jb;
12 sigjmp_buf sjb;
13 volatile sigset_t oldset;
14 sigset_t set, set2;
16 if (!setjmp(jb)) {
17 x = 1;
18 longjmp(jb, 1);
20 TEST(x==1, "setjmp/longjmp seems to have been bypassed\n");
22 x = 0;
23 r = setjmp(jb);
24 if (!x) {
25 x = 1;
26 longjmp(jb, 0);
28 TEST(r==1, "longjmp(jb, 0) caused setjmp to return %d\n", r);
30 sigemptyset(&set);
31 sigaddset(&set, SIGUSR1);
32 sigprocmask(SIG_UNBLOCK, &set, &set2);
33 oldset = set2;
35 /* Improve the chances of catching failure of sigsetjmp to
36 * properly save the signal mask in the sigjmb_buf. */
37 memset(&sjb, -1, sizeof sjb);
39 if (!sigsetjmp(sjb, 1)) {
40 sigemptyset(&set);
41 sigaddset(&set, SIGUSR1);
42 sigprocmask(SIG_BLOCK, &set, 0);
43 siglongjmp(sjb, 1);
45 set = oldset;
46 sigprocmask(SIG_SETMASK, &set, &set2);
47 TEST(sigismember(&set2, SIGUSR1)==0, "siglongjmp failed to restore mask\n");
49 sigemptyset(&set);
50 sigaddset(&set, SIGUSR1);
51 sigprocmask(SIG_UNBLOCK, &set, &set2);
52 oldset = set2;
54 if (!sigsetjmp(sjb, 0)) {
55 sigemptyset(&set);
56 sigaddset(&set, SIGUSR1);
57 sigprocmask(SIG_BLOCK, &set, 0);
58 siglongjmp(sjb, 1);
60 set = oldset;
61 sigprocmask(SIG_SETMASK, &set, &set2);
62 TEST(sigismember(&set2, SIGUSR1)==1, "siglongjmp incorrectly restored mask\n");
64 return t_status;