1 /*
2 * Copyright (c) 2005, Bull S.A.. All rights reserved.
3 * Created by: Sebastien Decugis
4 * Copyright (c) 2013 Cyril Hrubis <chrubis@suse.cz>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of version 2 of the GNU General Public License as
8 * published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it would be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 * This sample test aims to check the following assertions:
19 *
20 * If SA_SIGINFO is not set in sa_flags, sa_handler is used as the signal
21 * handling function.
22 *
23 * The steps are:
24 * -> register a handler for SIGSEGV without SA_SIGINFO, and a known function
25 * as sa_handler
26 * -> raise SIGSEGV, and check the function has been called.
27 *
28 * The test fails if the function is not called
29 */
30
31
32 #include <pthread.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38
39 #include <signal.h>
40 #include <errno.h>
41
42 #include "posixtest.h"
43
44 static volatile sig_atomic_t called = 0;
45
handler()46 static void handler()
47 {
48 called = 1;
49 }
50
main(void)51 int main(void)
52 {
53 int ret;
54 struct sigaction sa;
55
56 /* Set the signal handler */
57 sa.sa_flags = 0;
58 sa.sa_handler = handler;
59 ret = sigemptyset(&sa.sa_mask);
60
61 if (ret != 0) {
62 perror("Failed to empty signal set");
63 return PTS_UNRESOLVED;
64 }
65
66 /* Install the signal handler for SIGSEGV */
67 ret = sigaction(SIGSEGV, &sa, 0);
68
69 if (ret != 0) {
70 perror("Failed to set signal handler");
71 return PTS_UNRESOLVED;
72 }
73
74 if (called) {
75 fprintf(stderr,
76 "The signal handler has been called before signal was raised");
77 return PTS_FAIL;
78 }
79
80 ret = raise(SIGSEGV);
81
82 if (ret != 0) {
83 perror("Failed to raise SIGSEGV");
84 return PTS_UNRESOLVED;
85 }
86
87 if (!called) {
88 fprintf(stderr, "The sa_handler was not called\n");
89 return PTS_FAIL;
90 }
91
92 printf("Test PASSED\n");
93
94 return PTS_PASS;
95 }
96