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 assertion:
18 *
19 * If Real-Time Signals extension is supported, and several signals in the
20 * range SIGRTMIN-SIGRTMAX are selected, the lower numbered is returned first.
21 
22 * The steps are:
23 * -> mask SIGRTMIN-SIGRTMAX
24 * -> raise the signals in the range SIGRTMIN-SIGRTMAX
25 * -> sigwait a signal and check we get always teh lowest-ordered.
26 
27 * The test fails if we select an unexpected signal.
28 
29 */
30 
31 
32 /******************************************************************************/
33 /*************************** standard includes ********************************/
34 /******************************************************************************/
35 #include <pthread.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include <signal.h>
43 #include <errno.h>
44 
45 /******************************************************************************/
46 /***************************   Test framework   *******************************/
47 /******************************************************************************/
48 #include "../testfrmw/testfrmw.h"
49 #include "../testfrmw/testfrmw.c"
50 /* This header is responsible for defining the following macros:
51  * UNRESOLVED(ret, descr);
52  *    where descr is a description of the error and ret is an int
53  *   (error code for example)
54  * FAILED(descr);
55  *    where descr is a short text saying why the test has failed.
56  * PASSED();
57  *    No parameter.
58  *
59  * Both three macros shall terminate the calling process.
60  * The testcase shall not terminate in any other maneer.
61  *
62  * The other file defines the functions
63  * void output_init()
64  * void output(char * string, ...)
65  *
66  * Those may be used to output information.
67  */
68 
69 /******************************************************************************/
70 /**************************** Configuration ***********************************/
71 /******************************************************************************/
72 #ifndef VERBOSE
73 #define VERBOSE 1
74 #endif
75 
76 /******************************************************************************/
77 /***************************    Test case   ***********************************/
78 /******************************************************************************/
79 
80 /* The main test function. */
main(void)81 int main(void)
82 {
83 	int ret, i, sig;
84 	long rts;
85 	sigset_t set;
86 
87 	/* Initialize output */
88 	output_init();
89 
90 	/* Test the RTS extension */
91 	rts = sysconf(_SC_REALTIME_SIGNALS);
92 
93 	if (rts < 0L) {
94 		UNTESTED("This test needs the RTS extension");
95 	}
96 
97 	/* Set the signal mask */
98 	ret = sigemptyset(&set);
99 
100 	if (ret != 0) {
101 		UNRESOLVED(ret, "Failed to empty signal set");
102 	}
103 
104 	/* Add all SIGRT signals */
105 	for (i = SIGRTMIN; i <= SIGRTMAX; i++) {
106 
107 		ret = sigaddset(&set, i);
108 
109 		if (ret != 0) {
110 			UNRESOLVED(ret, "failed to add signal to signal set");
111 		}
112 	}
113 
114 	/* Block all RT signals */
115 	ret = pthread_sigmask(SIG_BLOCK, &set, NULL);
116 
117 	if (ret != 0) {
118 		UNRESOLVED(ret, "Failed to block RT signals");
119 	}
120 
121 	/* raise the signals in no particular order */
122 	for (i = SIGRTMIN + 1; i <= SIGRTMAX; i += 3) {
123 		ret = raise(i);
124 
125 		if (ret != 0) {
126 			UNRESOLVED(ret, "Failed to raise the signal");
127 		}
128 	}
129 
130 	for (i = SIGRTMIN; i <= SIGRTMAX; i += 3) {
131 		ret = raise(i);
132 
133 		if (ret != 0) {
134 			UNRESOLVED(ret, "Failed to raise the signal");
135 		}
136 	}
137 
138 	for (i = SIGRTMIN + 2; i <= SIGRTMAX; i += 3) {
139 		ret = raise(i);
140 
141 		if (ret != 0) {
142 			UNRESOLVED(ret, "Failed to raise the signal");
143 		}
144 	}
145 
146 	/* All RT signals are pending */
147 
148 	/* Check the signals are delivered in order */
149 	for (i = SIGRTMIN; i <= SIGRTMAX; i++) {
150 		ret = sigwait(&set, &sig);
151 
152 		if (ret != 0) {
153 			UNRESOLVED(ret, "Failed to sigwait for RT signal");
154 		}
155 
156 		if (sig != i) {
157 			output("SIGRTMIN: %d, SIGRTMAX: %d, i: %d, sig:%d\n",
158 			       SIGRTMIN, SIGRTMAX, i, sig);
159 			FAILED("Got wrong signal");
160 		}
161 	}
162 
163 	/* Test passed */
164 #if VERBOSE > 0
165 	output("Test passed\n");
166 
167 #endif
168 	PASSED;
169 }
170