blob: 9ca82a169f81d0758cd27a6c1d932450a172bd4c [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
Cyril Hrubis7e920252012-12-11 19:11:58 +01002 * Copyright (c) International Business Machines Corp., 2001
3 * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz>
plars865695b2001-08-27 22:15:12 +00004 *
Cyril Hrubis7e920252012-12-11 19:11:58 +01005 * 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.
plars865695b2001-08-27 22:15:12 +00009 *
Cyril Hrubis7e920252012-12-11 19:11:58 +010010 * 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.
plars865695b2001-08-27 22:15:12 +000014 *
Cyril Hrubis7e920252012-12-11 19:11:58 +010015 * 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
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
Cyril Hrubis7e920252012-12-11 19:11:58 +010021 * Check that exit returns the correct values to the waiting parent
plars865695b2001-08-27 22:15:12 +000022 */
Cyril Hrubis7e920252012-12-11 19:11:58 +010023
plars74948ad2002-11-14 16:16:14 +000024#include <sys/types.h>
25#include <sys/wait.h>
26#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000027#include <stdio.h>
28#include <signal.h>
29#include <errno.h>
30#include "test.h"
plars865695b2001-08-27 22:15:12 +000031
Cyril Hrubis7e920252012-12-11 19:11:58 +010032static void cleanup(void);
33static void setup(void);
plars865695b2001-08-27 22:15:12 +000034
35char *TCID = "exit01";
36int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000037
plars74948ad2002-11-14 16:16:14 +000038int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000039{
40 int pid, npid, sig, nsig, exno, nexno, status;
41 int rval = 0;
Cyril Hrubis89af32a2012-10-24 16:39:11 +020042 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020043 const char *msg;
plars865695b2001-08-27 22:15:12 +000044
Garrett Cooper45e285d2010-11-22 12:19:25 -080045 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) {
plars865695b2001-08-27 22:15:12 +000046 tst_brkm(TBROK, cleanup, "OPTION PARSIkNG ERROR - %s", msg);
Cyril Hrubis7e920252012-12-11 19:11:58 +010047 }
plars865695b2001-08-27 22:15:12 +000048
Cyril Hrubis7e920252012-12-11 19:11:58 +010049 setup();
plars865695b2001-08-27 22:15:12 +000050
plars865695b2001-08-27 22:15:12 +000051 for (lc = 0; TEST_LOOPING(lc); lc++) {
52
Caspar Zhangd59a6592013-03-07 14:59:12 +080053 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000054
55 sig = 0;
56 exno = 1;
57
Cyril Hrubis7e920252012-12-11 19:11:58 +010058 pid = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +000059
Cyril Hrubis7e920252012-12-11 19:11:58 +010060 switch (pid) {
61 case 0:
plars865695b2001-08-27 22:15:12 +000062 exit(exno);
Cyril Hrubis7e920252012-12-11 19:11:58 +010063 break;
64 case -1:
65 tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
66 break;
67 default:
plars865695b2001-08-27 22:15:12 +000068 npid = wait(&status);
69
70 if (npid != pid) {
71 tst_resm(TFAIL, "wait error: "
72 "unexpected pid returned");
73 rval = 1;
74 }
75
76 nsig = status % 256;
77
78 /*
79 * Check if the core dump bit has been set, bit # 7
80 */
81 if (nsig >= 128) {
82 nsig = nsig - 128;
83 }
84
85 /*
86 * nsig is the signal number returned by wait
87 */
88 if (nsig != sig) {
89 tst_resm(TFAIL, "wait error: "
90 "unexpected signal returned");
91 rval = 1;
92 }
93
94 /*
95 * nexno is the exit number returned by wait
96 */
97 nexno = status / 256;
98 if (nexno != exno) {
99 tst_resm(TFAIL, "wait error: "
100 "unexpected exit number returned");
101 rval = 1;
102 }
Cyril Hrubis7e920252012-12-11 19:11:58 +0100103 break;
plars865695b2001-08-27 22:15:12 +0000104 }
105
106 if (rval != 1) {
107 tst_resm(TPASS, "exit() test PASSED");
108 }
109 }
Cyril Hrubis7e920252012-12-11 19:11:58 +0100110
plars865695b2001-08-27 22:15:12 +0000111 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800112 tst_exit();
plars865695b2001-08-27 22:15:12 +0000113}
114
Cyril Hrubis7e920252012-12-11 19:11:58 +0100115static void setup(void)
plars865695b2001-08-27 22:15:12 +0000116{
plars865695b2001-08-27 22:15:12 +0000117 tst_sig(FORK, DEF_HANDLER, cleanup);
118
plars865695b2001-08-27 22:15:12 +0000119 TEST_PAUSE;
120}
121
Cyril Hrubis7e920252012-12-11 19:11:58 +0100122static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000123{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700124}