xref: /aosp_15_r20/external/ltp/testcases/open_posix_testsuite/conformance/interfaces/pthread_exit/6-1.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
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 sample test aims to check the following assertion:
18  *
19  * If the calling thread is the last thread in the process,
20  * the effect are as if an implicit call to exit(0) had been made.
21 
22  * The steps are:
23  *
24  * -> main creates a thread.
25  * -> this thread forks(). The new process contains only 1 thread.
26  * -> the thread in the new process calls pthread_exit(non-0 value).
27  * -> main process joins the child process and checks the behavior
28  *     is as if exit(0) had been called.
29  *     The checked items are:
30  *       -> the return value.
31  *       -> the atexit() routines have been called.
32   */
33 
34 /********************************************************************************************/
35 /****************************** standard includes *****************************************/
36 /********************************************************************************************/
37 #include <pthread.h>
38 #include <stdarg.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 
44 #include <sched.h>
45 #include <semaphore.h>
46 #include <errno.h>
47 #include <assert.h>
48 #include <sys/wait.h>
49 #include <sys/mman.h>
50 
51 /********************************************************************************************/
52 /******************************   Test framework   *****************************************/
53 /********************************************************************************************/
54 #include "../testfrmw/testfrmw.h"
55 #include "../testfrmw/testfrmw.c"
56  /* This header is responsible for defining the following macros:
57   * UNRESOLVED(ret, descr);
58   *    where descr is a description of the error and ret is an int (error code for example)
59   * FAILED(descr);
60   *    where descr is a short text saying why the test has failed.
61   * PASSED();
62   *    No parameter.
63   *
64   * Both three macros shall terminate the calling process.
65   * The testcase shall not terminate in any other maneer.
66   *
67   * The other file defines the functions
68   * void output_init()
69   * void output(char * string, ...)
70   *
71   * Those may be used to output information.
72   */
73 
74 /********************************************************************************************/
75 /********************************** Configuration ******************************************/
76 /********************************************************************************************/
77 #ifndef VERBOSE
78 #define VERBOSE 1
79 #endif
80 
81 /********************************************************************************************/
82 /***********************************    Test cases  *****************************************/
83 /********************************************************************************************/
84 
85 #include "../testfrmw/threads_scenarii.c"
86 
87 /* This file will define the following objects:
88  * scenarii: array of struct __scenario type.
89  * NSCENAR : macro giving the total # of scenarii
90  * scenar_init(): function to call before use the scenarii array.
91  * scenar_fini(): function to call after end of use of the scenarii array.
92  */
93 
94 /********************************************************************************************/
95 /***********************************    Real Test   *****************************************/
96 /********************************************************************************************/
97 
98 /* This will be used to control that atexit() has been called */
99 static int *ctl;
100 static long mf;
101 static unsigned int sc;
102 
clnp(void)103 static void clnp(void)
104 {
105 	*ctl = 1;
106 }
107 
108 /* Thread routine */
threaded(void * arg PTS_ATTRIBUTE_UNUSED)109 static void *threaded(void *arg PTS_ATTRIBUTE_UNUSED)
110 {
111 	int ret = 0;
112 
113 	pid_t pid, chk;
114 	int status;
115 
116 	if (mf > 0)
117 		*ctl = 0;
118 
119 #if VERBOSE > 0
120 	fflush(stdout);
121 #endif
122 
123 	pid = fork();
124 	if (pid == (pid_t) - 1) {
125 		UNRESOLVED(errno, "Failed to fork()");
126 	}
127 	if (pid == 0) {
128 		/* children */
129 		if (mf > 0) {
130 			ret = atexit(clnp);
131 			if (ret != 0) {
132 				UNRESOLVED(ret,
133 					   "Failed to register atexit function");
134 			}
135 		}
136 
137 		/* exit the last (and only) thread */
138 		pthread_exit(&ret);
139 
140 		FAILED
141 		    ("pthread_exit() did not terminate the process when there was only 1 thread");
142 	}
143 
144 	/* Only the parent process goes this far */
145 	chk = waitpid(pid, &status, 0);
146 	if (chk != pid) {
147 		output("Expected pid: %i. Got %i\n", (int)pid, (int)chk);
148 		UNRESOLVED(errno, "Waitpid failed");
149 	}
150 
151 	if (WIFSIGNALED(status)) {
152 		output("Child process killed with signal %d\n",
153 		       WTERMSIG(status));
154 		UNRESOLVED(-1, "Child process was killed");
155 	}
156 
157 	if (WIFEXITED(status)) {
158 		ret = WEXITSTATUS(status);
159 	} else {
160 		UNRESOLVED(-1, "Child process was neither killed nor exited");
161 	}
162 
163 	if (ret != 0) {
164 		output("Exit status was: %i\n", ret);
165 		FAILED("The child process did not exit with 0 status.");
166 	}
167 
168 	if (mf > 0)
169 		if (*ctl != 1)
170 			FAILED
171 			    ("pthread_exit() in the last thread did not execute atexit() routines");
172 
173 	/* Signal we're done (especially in case of a detached thread) */
174 	do {
175 		ret = sem_post(&scenarii[sc].sem);
176 	}
177 	while ((ret == -1) && (errno == EINTR));
178 	if (ret == -1) {
179 		UNRESOLVED(errno, "Failed to wait for the semaphore");
180 	}
181 
182 	return NULL;
183 }
184 
185 /* Main routine */
main(void)186 int main(void)
187 {
188 	int ret = 0;
189 	pthread_t child;
190 
191 	mf = sysconf(_SC_MAPPED_FILES);
192 
193 	output_init();
194 
195 	scenar_init();
196 
197 	/* We want to share some memory with the child process */
198 	if (mf > 0) {
199 		/* We will place the test data in a mmaped file */
200 		char filename[] = "/tmp/pthread_exit_6-1-XXXXXX";
201 		size_t sz;
202 		void *mmaped;
203 		int fd;
204 		char *tmp;
205 
206 		/* We now create the temp files */
207 		fd = mkstemp(filename);
208 		if (fd == -1) {
209 			UNRESOLVED(errno,
210 				   "Temporary file could not be created");
211 		}
212 
213 		/* and make sure the file will be deleted when closed */
214 		unlink(filename);
215 
216 #if VERBOSE > 1
217 		output("Temp file created (%s).\n", filename);
218 #endif
219 
220 		sz = (size_t) sysconf(_SC_PAGESIZE);
221 
222 		tmp = calloc(1, sz);
223 		if (tmp == NULL) {
224 			UNRESOLVED(errno, "Memory allocation failed");
225 		}
226 
227 		/* Write the data to the file.  */
228 		if (write(fd, tmp, sz) != (ssize_t) sz) {
229 			UNRESOLVED(sz, "Writting to the file failed");
230 		}
231 
232 		free(tmp);
233 
234 		/* Now we can map the file in memory */
235 		mmaped =
236 		    mmap(NULL, sz, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
237 		if (mmaped == MAP_FAILED) {
238 			UNRESOLVED(errno, "mmap failed");
239 		}
240 
241 		ctl = (int *)mmaped;
242 
243 		/* Our datatest structure is now in shared memory */
244 #if VERBOSE > 1
245 		output("Testdata allocated in shared memory.\n");
246 #endif
247 	}
248 
249 	for (sc = 0; sc < NSCENAR; sc++) {
250 #if VERBOSE > 0
251 		output("-----\n");
252 		output("Starting test with scenario (%i): %s\n", sc,
253 		       scenarii[sc].descr);
254 #endif
255 
256 		ret = pthread_create(&child, &scenarii[sc].ta, threaded, &ctl);
257 		switch (scenarii[sc].result) {
258 		case 0:	/* Operation was expected to succeed */
259 			if (ret != 0) {
260 				UNRESOLVED(ret, "Failed to create this thread");
261 			}
262 			break;
263 
264 		case 1:	/* Operation was expected to fail */
265 			if (ret == 0) {
266 				UNRESOLVED(-1,
267 					   "An error was expected but the thread creation succeeded");
268 			}
269 			break;
270 
271 		case 2:	/* We did not know the expected result */
272 		default:
273 #if VERBOSE > 0
274 			if (ret == 0) {
275 				output
276 				    ("Thread has been created successfully for this scenario\n");
277 			} else {
278 				output
279 				    ("Thread creation failed with the error: %s\n",
280 				     strerror(ret));
281 			}
282 #endif
283 		}
284 		if (ret == 0) {	/* The new thread is running */
285 			if (scenarii[sc].detached == 0) {
286 				ret = pthread_join(child, NULL);
287 				if (ret != 0) {
288 					UNRESOLVED(ret,
289 						   "Unable to join a thread");
290 				}
291 			} else {
292 				/* Just wait for the thread to terminate */
293 				do {
294 					ret = sem_wait(&scenarii[sc].sem);
295 				}
296 				while ((ret == -1) && (errno == EINTR));
297 				if (ret == -1) {
298 					UNRESOLVED(errno,
299 						   "Failed to wait for the semaphore");
300 				}
301 			}
302 		}
303 	}
304 
305 	scenar_fini();
306 #if VERBOSE > 0
307 	output("-----\n");
308 	output("All test data destroyed\n");
309 	output("Test PASSED\n");
310 #endif
311 
312 	PASSED;
313 }
314