1 /*
2 * Copyright (c) 2005, Bull S.A..  All rights reserved.
3 * Created by: Sebastien Decugis
4 
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 
17 * This sample test aims to check the following assertions:
18 *
19 * sigaction returns -1 and errno is set to EINVAL if signal number is invalid
20 * or an attempt to do an operation which is not allowed is made.
21 
22 * The steps are:
23 * -> Try setting a signal handler for signal SIGRTMAX + 1
24 * -> Try setting a signal handler for SIGKILL
25 * -> Try setting a signal handler for SIGSTOP
26 * -> Try ignoring SIGSTOP
27 * -> Try ignoring SIGKILL
28 
29 * The test fails if the signals are not delivered in FIFO order.
30 */
31 
32 
33 /******************************************************************************/
34 /*************************** standard includes ********************************/
35 /******************************************************************************/
36 #include <pthread.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 
43 #include <signal.h>
44 #include <errno.h>
45 
46 /******************************************************************************/
47 /***************************   Test framework   *******************************/
48 /******************************************************************************/
49 #include "../testfrmw/testfrmw.h"
50 #include "../testfrmw/testfrmw.c"
51 /* This header is responsible for defining the following macros:
52  * UNRESOLVED(ret, descr);
53  *    where descr is a description of the error and ret is an int
54  *   (error code for example)
55  * FAILED(descr);
56  *    where descr is a short text saying why the test has failed.
57  * PASSED();
58  *    No parameter.
59  *
60  * Both three macros shall terminate the calling process.
61  * The testcase shall not terminate in any other maneer.
62  *
63  * The other file defines the functions
64  * void output_init()
65  * void output(char * string, ...)
66  *
67  * Those may be used to output information.
68  */
69 
70 /******************************************************************************/
71 /**************************** Configuration ***********************************/
72 /******************************************************************************/
73 #ifndef VERBOSE
74 #define VERBOSE 1
75 #endif
76 
77 #define SIG_INVALID SIGRTMAX+10
78 
79 /******************************************************************************/
80 /***************************    Test case   ***********************************/
81 /******************************************************************************/
82 
handler(int signo LTP_ATTRIBUTE_UNUSED)83 void handler(int signo LTP_ATTRIBUTE_UNUSED)
84 {
85 	return;
86 }
87 
88 /* main function */
main(void)89 int main(void)
90 {
91 	int ret;
92 
93 	struct sigaction sa;
94 
95 	/* Initialize output */
96 	output_init();
97 
98 	/* Set the signal handler */
99 	sa.sa_flags = 0;
100 
101 	sa.sa_handler = handler;
102 
103 	ret = sigemptyset(&sa.sa_mask);
104 
105 	if (ret != 0) {
106 		UNRESOLVED(ret, "Failed to empty signal set");
107 	}
108 
109 	/* Install the signal handler for SIGRTMAX */
110 #if VERBOSE > 0
111 	output("Trying to catch invalid signal %d\n", SIG_INVALID);
112 
113 #endif
114 	ret = sigaction(SIG_INVALID, &sa, 0);
115 
116 	if (ret == 0) {
117 		output("Is signal %d valid on this implementation?\n",
118 		       SIG_INVALID);
119 		FAILED("Setting handler for invalid signal did not fail");
120 	}
121 
122 	if (errno != EINVAL) {
123 		output("Got error %d (%s) instead of %d (%s)\n",
124 		       errno, strerror(errno), EINVAL, strerror(EINVAL));
125 		FAILED("Wrong error code returned");
126 	}
127 
128 	/* Install the signal handler for SIGKILL */
129 #if VERBOSE > 0
130 	output("Trying to catch unauthorized signal SIGKILL (%d)\n", SIGKILL);
131 
132 #endif
133 	ret = sigaction(SIGKILL, &sa, 0);
134 
135 	if (ret == 0) {
136 		FAILED("Setting handler for SIGKILL did not fail");
137 	}
138 
139 	if (errno != EINVAL) {
140 		output("Got error %d (%s) instead of %d (%s)\n",
141 		       errno, strerror(errno), EINVAL, strerror(EINVAL));
142 		FAILED("Wrong error code returned");
143 	}
144 
145 	/* Install the signal handler for SIGSTOP */
146 #if VERBOSE > 0
147 	output("Trying to catch unauthorized signal SIGSTOP (%d)\n", SIGSTOP);
148 
149 #endif
150 	ret = sigaction(SIGSTOP, &sa, 0);
151 
152 	if (ret == 0) {
153 		FAILED("Setting handler for SIGSTOP did not fail");
154 	}
155 
156 	if (errno != EINVAL) {
157 		output("Got error %d (%s) instead of %d (%s)\n",
158 		       errno, strerror(errno), EINVAL, strerror(EINVAL));
159 		FAILED("Wrong error code returned");
160 	}
161 
162 	sa.sa_handler = SIG_IGN;
163 
164 	/* Ingrore SIGKILL */
165 #if VERBOSE > 0
166 	output("Trying to ignore unauthorized signal SIGKILL (%d)\n", SIGKILL);
167 
168 #endif
169 	ret = sigaction(SIGKILL, &sa, 0);
170 
171 	if (ret == 0) {
172 		FAILED("Ignoring SIGKILL did not fail");
173 	}
174 
175 	if (errno != EINVAL) {
176 		output("Got error %d (%s) instead of %d (%s)\n",
177 		       errno, strerror(errno), EINVAL, strerror(EINVAL));
178 		FAILED("Wrong error code returned");
179 	}
180 
181 	/* Ignore SIGSTOP */
182 #if VERBOSE > 0
183 	output("Trying to ignore unauthorized signal SIGSTOP (%d)\n", SIGSTOP);
184 
185 #endif
186 	ret = sigaction(SIGSTOP, &sa, 0);
187 
188 	if (ret == 0) {
189 		FAILED("Ignoring SIGSTOP did not fail");
190 	}
191 
192 	if (errno != EINVAL) {
193 		output("Got error %d (%s) instead of %d (%s)\n",
194 		       errno, strerror(errno), EINVAL, strerror(EINVAL));
195 		FAILED("Wrong error code returned");
196 	}
197 
198 	/* Test passed */
199 #if VERBOSE > 0
200 
201 	output("Test passed\n");
202 
203 #endif
204 
205 	PASSED;
206 }
207