plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 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 | */ |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 40 | #include <sys/types.h> |
| 41 | #include <sys/wait.h> |
| 42 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 43 | #include <stdio.h> |
| 44 | #include <signal.h> |
| 45 | #include <errno.h> |
| 46 | #include "test.h" |
| 47 | #include "usctest.h" |
| 48 | |
| 49 | void cleanup(void); |
| 50 | void setup(void); |
| 51 | |
| 52 | char *TCID = "exit01"; |
| 53 | int TST_TOTAL = 1; |
| 54 | extern int Tst_count; |
| 55 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 56 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | { |
| 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 Cooper | e1f008e | 2010-12-14 00:21:59 -0800 | [diff] [blame^] | 64 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 65 | tst_brkm(TBROK, cleanup, "OPTION PARSIkNG ERROR - %s", msg); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 66 | /*NOTREACHED*/} |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 67 | |
| 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 | |
vapier | c8b68a4 | 2009-08-28 12:26:07 +0000 | [diff] [blame] | 79 | if ((pid = FORK_OR_VFORK()) == -1) |
| 80 | tst_brkm(TBROK|TERRNO, cleanup, "fork() failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 81 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 82 | if (pid == 0) { /* parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 129 | /*NOTREACHED*/ return 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* |
| 133 | * setup() - performs all ONE TIME setup for this test |
| 134 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 135 | void setup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 136 | { |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 150 | void cleanup() |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | { |
| 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 | } |