blob: 37f870bc1a9c3a8f0fc853cb8af72b802cbe374a [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/*
21 * NAME
22 * exit01.c
23 *
24 * DESCRIPTION
25 * Check that exit returns the correct values to the waiting parent
26 *
27 * ALGORITHM
28 * Fork a process that immediately calls exit() with a known
29 * value. Check for that value in the parent.
30 *
31 * USAGE
32 * exit01
33 *
34 * HISTORY
35 * 07/2001 Ported by Wayne Boyer
36 *
37 * RESTRICTIONS
38 * None
39 */
plars74948ad2002-11-14 16:16:14 +000040#include <sys/types.h>
41#include <sys/wait.h>
42#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000043#include <stdio.h>
44#include <signal.h>
45#include <errno.h>
46#include "test.h"
47#include "usctest.h"
48
49void cleanup(void);
50void setup(void);
51
52char *TCID = "exit01";
53int TST_TOTAL = 1;
54extern int Tst_count;
55
plars74948ad2002-11-14 16:16:14 +000056int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000057{
58 int pid, npid, sig, nsig, exno, nexno, status;
59 int rval = 0;
60 int lc; /* loop counter */
61 char *msg; /* message returned from parse_opts */
62
63 /* parse standard options */
Garrett Coopere1f008e2010-12-14 00:21:59 -080064 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +000065 tst_brkm(TBROK, cleanup, "OPTION PARSIkNG ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +000066 /*NOTREACHED*/}
plars865695b2001-08-27 22:15:12 +000067
68 setup(); /* global setup for test */
69
70 /* check looping state if -i option given */
71 for (lc = 0; TEST_LOOPING(lc); lc++) {
72
73 /* reset Tst_count in case we are looping. */
74 Tst_count = 0;
75
76 sig = 0;
77 exno = 1;
78
vapierc8b68a42009-08-28 12:26:07 +000079 if ((pid = FORK_OR_VFORK()) == -1)
80 tst_brkm(TBROK|TERRNO, cleanup, "fork() failed");
plars865695b2001-08-27 22:15:12 +000081
subrata_modak56207ce2009-03-23 13:35:39 +000082 if (pid == 0) { /* parent */
plars865695b2001-08-27 22:15:12 +000083 exit(exno);
84 } else {
85 sleep(1); /* let child start */
86 npid = wait(&status);
87
88 if (npid != pid) {
89 tst_resm(TFAIL, "wait error: "
90 "unexpected pid returned");
91 rval = 1;
92 }
93
94 nsig = status % 256;
95
96 /*
97 * Check if the core dump bit has been set, bit # 7
98 */
99 if (nsig >= 128) {
100 nsig = nsig - 128;
101 }
102
103 /*
104 * nsig is the signal number returned by wait
105 */
106 if (nsig != sig) {
107 tst_resm(TFAIL, "wait error: "
108 "unexpected signal returned");
109 rval = 1;
110 }
111
112 /*
113 * nexno is the exit number returned by wait
114 */
115 nexno = status / 256;
116 if (nexno != exno) {
117 tst_resm(TFAIL, "wait error: "
118 "unexpected exit number returned");
119 rval = 1;
120 }
121 }
122
123 if (rval != 1) {
124 tst_resm(TPASS, "exit() test PASSED");
125 }
126 }
127 cleanup();
128
subrata_modak56207ce2009-03-23 13:35:39 +0000129 /*NOTREACHED*/ return 0;
plars865695b2001-08-27 22:15:12 +0000130}
131
132/*
133 * setup() - performs all ONE TIME setup for this test
134 */
subrata_modak56207ce2009-03-23 13:35:39 +0000135void setup()
plars865695b2001-08-27 22:15:12 +0000136{
137 /* capture signals */
138 tst_sig(FORK, DEF_HANDLER, cleanup);
139
140 umask(0);
141
142 /* Pause if that option was specified */
143 TEST_PAUSE;
144}
145
146/*
147 * cleanup() - performs all ONE TIME cleanup for this test at completion or
148 * premature exit.
149 */
subrata_modak56207ce2009-03-23 13:35:39 +0000150void cleanup()
plars865695b2001-08-27 22:15:12 +0000151{
152 /*
153 * print timing stats if that option was specified.
154 * print errno log if that option was specified.
155 */
156 TEST_CLEANUP;
157
158 /* exit with return code appropriate for results */
159 tst_exit();
160}