• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 set in sa_flags and Real Time Signals extension is supported,
21 * sa_sigaction is used as the signal handling function.
22 *
23 * The steps are:
24 * -> test for RTS extension
25 * -> register a handler for SIGURG with SA_SIGINFO, and a known function
26 *   as sa_sigaction
27 * -> raise SIGURG, and check the function has been called.
28 *
29 * The test fails if the function is not called
30 */
31 
32 
33 #include <pthread.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <signal.h>
40 #include <errno.h>
41 #include "posixtest.h"
42 
43 static volatile sig_atomic_t called = 0;
44 
handler(int sig,siginfo_t * info,void * context)45 static void handler(int sig, siginfo_t *info, void *context)
46 {
47 	(void) sig;
48 	(void) context;
49 
50 	if (info->si_signo != SIGURG) {
51 		PTS_WRITE_MSG("Wrong signal generated?\n");
52 		_exit(PTS_FAIL);
53 	}
54 
55 	called = 1;
56 }
57 
main(void)58 int main(void)
59 {
60 	int ret;
61 	long rts;
62 
63 	struct sigaction sa;
64 
65 	/* Test the RTS extension */
66 	rts = sysconf(_SC_REALTIME_SIGNALS);
67 
68 	if (rts < 0L) {
69 		fprintf(stderr, "This test needs the RTS extension");
70 		return PTS_UNTESTED;
71 	}
72 
73 	/* Set the signal handler */
74 	sa.sa_flags = SA_SIGINFO;
75 	sa.sa_sigaction = handler;
76 
77 	ret = sigemptyset(&sa.sa_mask);
78 
79 	if (ret != 0) {
80 		perror("Failed to empty signal set");
81 		return PTS_UNRESOLVED;
82 	}
83 
84 	/* Install the signal handler for SIGURG */
85 	ret = sigaction(SIGURG, &sa, 0);
86 
87 	if (ret != 0) {
88 		perror("Failed to set signal handler");
89 		return PTS_UNTESTED;
90 	}
91 
92 	if (called) {
93 		fprintf(stderr,
94 			"The signal handler has been called before signal was raised");
95 		return PTS_FAIL;
96 	}
97 
98 	ret = raise(SIGURG);
99 
100 	if (ret != 0) {
101 		perror("Failed to raise SIGURG");
102 		return PTS_UNRESOLVED;
103 	}
104 
105 	if (!called) {
106 		fprintf(stderr, "The sa_handler was not called");
107 		return PTS_FAIL;
108 	}
109 
110 	printf("Test PASSED\n");
111 
112 	return PTS_PASS;
113 }
114