plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 2 | * Copyright (c) International Business Machines Corp., 2001 |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 12 | * the GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 17 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | * NAME |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 19 | * fork08.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 20 | * |
| 21 | * DESCRIPTION |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 22 | * Check if the parent's file descriptors are affected by |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 23 | * actions in the child; they should not be. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 24 | * |
| 25 | * ALGORITHM |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 26 | * Parent opens a file. |
| 27 | * Forks a child which closes a file. |
| 28 | * Parent forks a second child which attempts to read the (closed) |
| 29 | * file. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 30 | * |
| 31 | * USAGE |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 32 | * fork08 |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 33 | * |
| 34 | * HISTORY |
| 35 | * 07/2001 Ported by Wayne Boyer |
| 36 | * |
| 37 | * RESTRICTIONS |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 38 | * None |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 39 | */ |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 40 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 41 | #include <sys/types.h> |
| 42 | #include <sys/wait.h> |
| 43 | #include <sys/stat.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 44 | #include <stdio.h> |
| 45 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 46 | |
| 47 | char *TCID = "fork08"; |
| 48 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 49 | |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 50 | static void setup(void); |
| 51 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 52 | |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 53 | static char pbuf[10]; |
| 54 | static char fnamebuf[40]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 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 status, count, forks, pid1; |
| 59 | int ch_r_stat; |
| 60 | FILE *rea, *writ; |
| 61 | |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 62 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 63 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 65 | msg = parse_opts(ac, av, NULL, NULL); |
| 66 | if (msg != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 67 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | setup(); |
| 70 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 72 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 74 | writ = fopen(fnamebuf, "w"); |
| 75 | if (writ == NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | tst_resm(TFAIL, "failed to fopen file for write"); |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 77 | rea = fopen(fnamebuf, "r"); |
| 78 | if (rea == NULL) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 79 | tst_resm(TFAIL, "failed to fopen file for read"); |
| 80 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 81 | fprintf(writ, "abcdefghijklmnopqrstuv"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 82 | fflush(writ); |
| 83 | sleep(1); |
| 84 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 85 | if ((getc(rea)) != 'a') |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 86 | tst_resm(TFAIL, "getc from read side was confused"); |
| 87 | |
| 88 | forks = 0; |
| 89 | |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 90 | forkone: |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 91 | ++forks; |
| 92 | |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 93 | pid1 = fork(); |
| 94 | if (pid1 != 0) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 95 | tst_resm(TINFO, "parent forksval: %d", forks); |
| 96 | |
| 97 | if ((pid1 != (-1)) && (forks < 2)) |
| 98 | goto forkone; |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 99 | else if (pid1 < 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | tst_resm(TINFO, "Fork failed"); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 101 | } else { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | /* |
| 103 | * If first child close the file descriptor for the |
| 104 | * read stream |
| 105 | */ |
| 106 | if (forks == 1) { |
| 107 | if ((fclose(rea)) == -1) { |
| 108 | tst_resm(TFAIL, "error in first child" |
| 109 | " closing fildes"); |
| 110 | } |
| 111 | _exit(0); |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * If second child attempt to read from the file |
| 116 | */ |
| 117 | else if (forks == 2) { |
| 118 | ch_r_stat = getc(rea); |
| 119 | tst_resm(TINFO, "second child got char: %c", |
| 120 | ch_r_stat); |
| 121 | if (ch_r_stat == 'b') { |
| 122 | tst_resm(TPASS, "Test passed in child" |
| 123 | "number %d", forks); |
| 124 | exit(0); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 125 | } else if (ch_r_stat == EOF) { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 126 | tst_resm(TFAIL, "Second child got " |
| 127 | "EOF"); |
| 128 | exit(-1); |
| 129 | } else { |
| 130 | tst_resm(TFAIL, "test failed in child" |
| 131 | "no %d", forks); |
| 132 | exit(-1); |
| 133 | } |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | } else { /* end of second child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 135 | tst_resm(TINFO, "forksnumber: %d", forks); |
| 136 | exit(3); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | for (count = 0; count <= forks; count++) { |
| 141 | wait(&status); |
| 142 | tst_resm(TINFO, "exit status of wait " |
| 143 | " expected 0 got %d", status); |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 144 | status >>= 8; |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 145 | if (status == 0) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 146 | tst_resm(TPASS, "parent test PASSED"); |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 147 | else |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 148 | tst_resm(TFAIL, "parent test FAILED"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | tst_resm(TINFO, "Number of processes forked is %d", forks); |
| 152 | fclose(rea); |
| 153 | fclose(writ); |
| 154 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 156 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 157 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | } |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 159 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 160 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 162 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | umask(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 164 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 165 | tst_tmpdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 166 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | strcpy(fnamebuf, "fork07."); |
| 168 | sprintf(pbuf, "%d", getpid()); |
| 169 | strcat(fnamebuf, pbuf); |
| 170 | } |
| 171 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 172 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 173 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 174 | tst_rmdir(); |
Wanlong Gao | 705d693 | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 175 | } |