1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <stdio.h>
18 #include <signal.h>
19 #include <setjmp.h>
20 
21 static sigjmp_buf sbuf;
22 
handler(int sig)23 void handler(int sig)
24 {
25     printf("In handler()\n");
26     siglongjmp(sbuf, 99);
27     printf("After calling siglongjmp, but you won't see me here.\n");
28 }
29 
foo()30 void foo()
31 {
32     struct sigaction sact;
33     sigset_t sset;
34     printf("In foo()\n");
35 
36  /* Setup signal handler for SIGUSR2 */
37     sigemptyset(&sact.sa_mask);
38     sact.sa_flags = 0;
39     sact.sa_handler = handler;
40     sigaction(SIGUSR2, &sact, NULL);
41 
42  /* Unblock SIGUSR2 */
43     sigemptyset(&sset);
44     sigaddset(&sset, SIGUSR2);
45     sigprocmask(SIG_UNBLOCK, &sset, NULL);
46 
47  /* Action */
48     kill(getpid(), SIGUSR2);
49 
50     printf("After calling kill, but you won't see me here.\n");
51 }
52 
bar()53 void bar()
54 {
55     printf("In bar()\n");
56     foo();
57     printf("After calling foo, but you won't see me here.\n");
58 }
59 
main()60 int main()
61 {
62     int code, ret;
63     sigset_t sset;
64     printf("In main()\n");
65     sigemptyset(&sset);
66     sigaddset(&sset, SIGUSR1);
67     sigaddset(&sset, SIGUSR2);
68     sigprocmask(SIG_SETMASK, &sset, NULL);
69     if ((code = sigsetjmp(sbuf, 1)) == 0)
70     {
71         bar();
72         ret = 0xDEADBEEF;
73     }
74     else
75     {
76         printf("siglongjmp() back to main, code = %d\n", code);
77       /* Make sure SIGUSR2 still blocked */
78         sigprocmask(SIG_SETMASK, NULL, &sset);
79         ret = sigismember(&sset, SIGUSR2)? 0 : -1;
80     }
81     printf("ret = %d\n", ret);
82 
83     return ret;
84 }
85