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 the signal action was set with the signal() function, getting it into oact
21 * then reinstalling it with act must be valid.
22 *
23 * The steps are:
24 * -> register a signal handler for SIGTTIN with signal().
25 * -> check this signal handler works.
26 * -> change the signal handler with sigaction, saving old handler in oact.
27 * -> check the new signal handler works.
28 * -> set the old signal handler back
29 * -> check the old signal handler still works.
30 *
31 * The test fails if a signal handler does not work as expected.
32 */
33
34 /* We are testing conformance to IEEE Std 1003.1, 2003 Edition */
35 #define _POSIX_C_SOURCE 200112L
36
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <signal.h>
43 #include <errno.h>
44
45 #include "posixtest.h"
46
47 static volatile sig_atomic_t called = 1;
48
handler_1()49 static void handler_1()
50 {
51 called++;
52 }
53
handler_2()54 static void handler_2()
55 {
56 called--;
57 }
58
main(void)59 int main(void)
60 {
61 int ret;
62
63 struct sigaction sa, save;
64
65 if (signal(SIGTTIN, handler_1) == SIG_ERR) {
66 perror("Failed to register signal handler");
67 return PTS_UNRESOLVED;
68 }
69
70 /* As whether signal handler is restored to default when executed
71 is implementation defined, we cannot check it was registered here. */
72
73 /* Set the new signal handler with sigaction*/
74 sa.sa_flags = 0;
75
76 sa.sa_handler = handler_2;
77
78 ret = sigemptyset(&sa.sa_mask);
79
80 if (ret != 0) {
81 perror("Failed to empty signal set");
82 return PTS_UNRESOLVED;
83 }
84
85 ret = sigaction(SIGTTIN, &sa, &save);
86
87 if (ret != 0) {
88 perror("Failed to register signal handler");
89 return PTS_UNRESOLVED;
90 }
91
92 /* Check the signal handler has been set up */
93 ret = raise(SIGTTIN);
94
95 if (ret != 0) {
96 perror("Failed to raise signal");
97 return PTS_UNRESOLVED;
98 }
99
100 if (called != 0) {
101 fprintf(stderr, "Handler was not executed\n");
102 return PTS_FAIL;
103 }
104
105 /* Restore the first signal handler */
106 ret = sigaction(SIGTTIN, &save, 0);
107
108 if (ret != 0) {
109 perror("Failed to restore signal handler");
110 return PTS_UNRESOLVED;
111 }
112
113 /* Check the signal handler has been set up */
114 ret = raise(SIGTTIN);
115
116 if (ret != 0) {
117 perror("Failed to raise signal");
118 return PTS_UNRESOLVED;
119 }
120
121 if (called != 1) {
122 fprintf(stderr, "Handler was not executed\n");
123 return PTS_FAIL;
124 }
125
126 printf("Test PASSED\n");
127 return PTS_PASS;
128 }
129