plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * Copyright (c) 2012 Cyril Hrubis <chrubis@suse.cz> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 4 | * |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 9 | * |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 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. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 14 | * |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 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 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 21 | * Check that exit returns the correct values to the waiting parent |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 22 | */ |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 23 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 24 | #include <sys/types.h> |
| 25 | #include <sys/wait.h> |
| 26 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <signal.h> |
| 29 | #include <errno.h> |
| 30 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 31 | |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 32 | static void cleanup(void); |
| 33 | static void setup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 34 | |
| 35 | char *TCID = "exit01"; |
| 36 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 37 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 38 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | { |
| 40 | int pid, npid, sig, nsig, exno, nexno, status; |
| 41 | int rval = 0; |
Cyril Hrubis | 89af32a | 2012-10-24 16:39:11 +0200 | [diff] [blame] | 42 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 43 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | |
Garrett Cooper | 45e285d | 2010-11-22 12:19:25 -0800 | [diff] [blame] | 45 | if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | tst_brkm(TBROK, cleanup, "OPTION PARSIkNG ERROR - %s", msg); |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 47 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 49 | setup(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
| 52 | |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 53 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
| 55 | sig = 0; |
| 56 | exno = 1; |
| 57 | |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 58 | pid = FORK_OR_VFORK(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 60 | switch (pid) { |
| 61 | case 0: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 62 | exit(exno); |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 63 | break; |
| 64 | case -1: |
| 65 | tst_brkm(TBROK | TERRNO, cleanup, "fork() failed"); |
| 66 | break; |
| 67 | default: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | 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 Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 103 | break; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | if (rval != 1) { |
| 107 | tst_resm(TPASS, "exit() test PASSED"); |
| 108 | } |
| 109 | } |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 110 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 111 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 112 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 115 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 116 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 117 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 118 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 119 | TEST_PAUSE; |
| 120 | } |
| 121 | |
Cyril Hrubis | 7e92025 | 2012-12-11 19:11:58 +0100 | [diff] [blame] | 122 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 123 | { |
Chris Dearman | ec6edca | 2012-10-17 19:54:01 -0700 | [diff] [blame] | 124 | } |