1 /*
2  * Copyright (c) International Business Machines  Corp., 2001
3  * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
4  *
5  * This program is free software;  you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY;  without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13  * the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program;  if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 /*
21  * Check that exit returns the correct values to the waiting parent
22  */
23 
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <sys/stat.h>
27 #include <stdio.h>
28 #include <signal.h>
29 #include <errno.h>
30 #include "test.h"
31 
32 static void cleanup(void);
33 static void setup(void);
34 
35 char *TCID = "exit01";
36 int TST_TOTAL = 1;
37 
main(int ac,char ** av)38 int main(int ac, char **av)
39 {
40 	int pid, npid, sig, nsig, exno, nexno, status;
41 	int rval = 0;
42 	int lc;
43 
44 	tst_parse_opts(ac, av, NULL, NULL);
45 
46 	setup();
47 
48 	for (lc = 0; TEST_LOOPING(lc); lc++) {
49 
50 		tst_count = 0;
51 
52 		sig = 0;
53 		exno = 1;
54 
55 		pid = FORK_OR_VFORK();
56 
57 		switch (pid) {
58 		case 0:
59 			exit(exno);
60 		break;
61 		case -1:
62 			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
63 		break;
64 		default:
65 			npid = wait(&status);
66 
67 			if (npid != pid) {
68 				tst_resm(TFAIL, "wait error: "
69 					 "unexpected pid returned");
70 				rval = 1;
71 			}
72 
73 			nsig = status % 256;
74 
75 			/*
76 			 * Check if the core dump bit has been set, bit # 7
77 			 */
78 			if (nsig >= 128) {
79 				nsig = nsig - 128;
80 			}
81 
82 			/*
83 			 * nsig is the signal number returned by wait
84 			 */
85 			if (nsig != sig) {
86 				tst_resm(TFAIL, "wait error: "
87 					 "unexpected signal returned");
88 				rval = 1;
89 			}
90 
91 			/*
92 			 * nexno is the exit number returned by wait
93 			 */
94 			nexno = status / 256;
95 			if (nexno != exno) {
96 				tst_resm(TFAIL, "wait error: "
97 					 "unexpected exit number returned");
98 				rval = 1;
99 			}
100 		break;
101 		}
102 
103 		if (rval != 1) {
104 			tst_resm(TPASS, "exit() test PASSED");
105 		}
106 	}
107 
108 	cleanup();
109 	tst_exit();
110 }
111 
setup(void)112 static void setup(void)
113 {
114 	tst_sig(FORK, DEF_HANDLER, cleanup);
115 
116 	TEST_PAUSE;
117 }
118 
cleanup(void)119 static void cleanup(void)
120 {
121 }
122