1 /*
2  * Copyright (c) 2004, 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 file is a stress test for the function pthread_exit.
18  *
19  * It aims to check that:
20  *  -> when the threads are joinable, pthread_join always retrieve the
21  *     correct value.
22  *  -> pthread_exit() frees all the resources used by the threads.
23  *
24  * The second assertion is implicitly checked by monitoring the system
25  * while the stress test is running.
26  *
27  */
28 
29 /********************************************************************************************/
30 /****************************** standard includes *****************************************/
31 /********************************************************************************************/
32 #include <pthread.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #include <errno.h>
39 #include <signal.h>
40 #include <semaphore.h>
41 
42 /********************************************************************************************/
43 /******************************   Test framework   *****************************************/
44 /********************************************************************************************/
45 #include "testfrmw.h"
46 #include "testfrmw.c"
47  /* This header is responsible for defining the following macros:
48   * UNRESOLVED(ret, descr);
49   *    where descr is a description of the error and ret is an int (error code for example)
50   * FAILED(descr);
51   *    where descr is a short text saying why the test has failed.
52   * PASSED();
53   *    No parameter.
54   *
55   * Both three macros shall terminate the calling process.
56   * The testcase shall not terminate in any other maneer.
57   *
58   * The other file defines the functions
59   * void output_init()
60   * void output(char * string, ...)
61   *
62   * Those may be used to output information.
63   */
64 
65 /********************************************************************************************/
66 /********************************** Configuration ******************************************/
67 /********************************************************************************************/
68 #ifndef SCALABILITY_FACTOR
69 #define SCALABILITY_FACTOR 1
70 #endif
71 #ifndef VERBOSE
72 #define VERBOSE 1
73 #endif
74 
75 #define FACTOR 5
76 
77 /* This testcase needs the XSI features */
78 #ifndef WITHOUT_XOPEN
79 /********************************************************************************************/
80 /***********************************    Test case   *****************************************/
81 /********************************************************************************************/
82 
83 #include "threads_scenarii.c"
84 
85 /* This file will define the following objects:
86  * scenarii: array of struct __scenario type.
87  * NSCENAR : macro giving the total # of scenarii
88  * scenar_init(): function to call before use the scenarii array.
89  * scenar_fini(): function to call after end of use of the scenarii array.
90  */
91 
92 /********************************************************************************************/
93 /***********************************    Real Test   *****************************************/
94 /********************************************************************************************/
95 
96 char do_it = 1;
97 long long iterations = 0;
98 
99 /* Handler for user request to terminate */
sighdl(int sig)100 void sighdl(int sig)
101 {
102 	/* do_it = 0 */
103 	do {
104 		do_it = 0;
105 	}
106 	while (do_it);
107 }
108 
109 /* Cleanup handler to make sure the thread is exiting */
cleanup(void * arg)110 void cleanup(void *arg)
111 {
112 	int ret = 0;
113 	sem_t *sem = (sem_t *) arg;
114 
115 	/* Signal we're done (especially in case of a detached thread) */
116 	do {
117 		ret = sem_post(sem);
118 	}
119 	while ((ret == -1) && (errno == EINTR));
120 	if (ret == -1) {
121 		UNRESOLVED(errno, "Failed to wait for the semaphore");
122 	}
123 }
124 
125 /* Thread routine */
threaded(void * arg)126 void *threaded(void *arg)
127 {
128 	pthread_cleanup_push(cleanup, &scenarii[sc].sem);
129 
130 	pthread_exit(arg);
131 	FAILED("the pthread_exit routine returned");
132 
133 	pthread_cleanup_pop(1);
134 
135 	return NULL;		/* For the sake of compiler */
136 }
137 
138 /* main routine */
main(int argc,char * argv[])139 int main(int argc, char *argv[])
140 {
141 	int ret, i;
142 	void *rval;
143 	struct sigaction sa;
144 
145 	pthread_t threads[NSCENAR * SCALABILITY_FACTOR * FACTOR];
146 	int rets[NSCENAR * SCALABILITY_FACTOR * FACTOR];
147 
148 	/* Initialize output */
149 	output_init();
150 
151 	/* Initialize scenarii table */
152 	scenar_init();
153 
154 	/* Register the signal handler for SIGUSR1 */
155 	sigemptyset(&sa.sa_mask);
156 	sa.sa_flags = 0;
157 	sa.sa_handler = sighdl;
158 	if ((ret = sigaction(SIGUSR1, &sa, NULL))) {
159 		UNRESOLVED(ret, "Unable to register signal handler");
160 	}
161 	if ((ret = sigaction(SIGALRM, &sa, NULL))) {
162 		UNRESOLVED(ret, "Unable to register signal handler");
163 	}
164 #if VERBOSE > 1
165 	output("[parent] Signal handler registered\n");
166 #endif
167 
168 	while (do_it) {
169 		/* Create all the threads */
170 		for (i = 0; i < SCALABILITY_FACTOR * FACTOR; i++) {
171 			for (sc = 0; sc < NSCENAR; sc++) {
172 				/* Skip the alternative stack threads */
173 				if (scenarii[sc].altstack != 0)
174 					continue;
175 
176 				rets[i * NSCENAR + sc] =
177 				    pthread_create(&threads[i * NSCENAR + sc],
178 						   &scenarii[sc].ta, threaded,
179 						   &threads[i * NSCENAR + sc]);
180 				switch (scenarii[sc].result) {
181 				case 0:	/* Operation was expected to succeed */
182 					if (rets[i * NSCENAR + sc] != 0) {
183 						UNRESOLVED(rets
184 							   [i * NSCENAR + sc],
185 							   "Failed to create this thread");
186 					}
187 					break;
188 
189 				case 1:	/* Operation was expected to fail */
190 					if (rets[i * NSCENAR + sc] == 0) {
191 						UNRESOLVED(-1,
192 							   "An error was expected but the thread creation succeeded");
193 					}
194 					break;
195 
196 				case 2:	/* We did not know the expected result */
197 				default:
198 #if VERBOSE > 5
199 					if (rets[i * NSCENAR + sc] == 0) {
200 						output
201 						    ("Thread has been created successfully for this scenario\n");
202 					} else {
203 						output
204 						    ("Thread creation failed with the error: %s\n",
205 						     strerror(rets
206 							      [i * NSCENAR +
207 							       sc]));
208 					}
209 #endif
210 					;
211 				}
212 				if (rets[i * NSCENAR + sc] == 0) {
213 					/* Just wait for the thread to terminate */
214 					do {
215 						ret =
216 						    sem_wait(&scenarii[sc].sem);
217 					}
218 					while ((ret == -1) && (errno == EINTR));
219 					if (ret == -1) {
220 						UNRESOLVED(errno,
221 							   "Failed to wait for the semaphore");
222 					}
223 				}
224 			}
225 		}
226 
227 		/* Join all the joinable threads and check the value */
228 		for (i = 0; i < SCALABILITY_FACTOR * FACTOR; i++) {
229 			for (sc = 0; sc < NSCENAR; sc++) {
230 				if ((scenarii[sc].altstack == 0)
231 				    && (scenarii[sc].detached == 0)
232 				    && (rets[i * NSCENAR + sc] == 0)) {
233 					ret =
234 					    pthread_join(threads
235 							 [i * NSCENAR + sc],
236 							 &rval);
237 					if (ret != 0) {
238 						UNRESOLVED(ret,
239 							   "Unable to join a thread");
240 					}
241 
242 					if (rval !=
243 					    (void *)&threads[i * NSCENAR +
244 							     sc]) {
245 						output
246 						    ("arg: %p -- got %p -- NULL=%p\n",
247 						     &threads[i * NSCENAR + sc],
248 						     rval, NULL);
249 						FAILED
250 						    ("The retrieved error value is corrupted");
251 					}
252 				}
253 			}
254 		}
255 
256 		iterations++;
257 	}
258 
259 	/* Destroy scenarii attributes */
260 	scenar_fini();
261 
262 	/* Test passed */
263 	output("pthread_exit stress test PASSED -- %llu iterations\n",
264 	       iterations);
265 	PASSED;
266 }
267 
268 #else /* WITHOUT_XOPEN */
main(int argc,char * argv[])269 int main(int argc, char *argv[])
270 {
271 	output_init();
272 	UNTESTED("This test requires XSI features");
273 }
274 #endif
275