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