blob: 89984ac8fb6cf91f317f8ce1106841e07dbaccc9 [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
plars865695b2001-08-27 22:15:12 +00002 * 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 Gao4548c6c2012-10-19 18:03:36 +080016 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Wanlong Gao705d6932012-09-18 17:14:01 +080017 *
plars865695b2001-08-27 22:15:12 +000018 * NAME
Wanlong Gao705d6932012-09-18 17:14:01 +080019 * fork08.c
plars865695b2001-08-27 22:15:12 +000020 *
21 * DESCRIPTION
subrata_modak4bb656a2009-02-26 12:02:09 +000022 * Check if the parent's file descriptors are affected by
Wanlong Gao705d6932012-09-18 17:14:01 +080023 * actions in the child; they should not be.
plars865695b2001-08-27 22:15:12 +000024 *
25 * ALGORITHM
Wanlong Gao705d6932012-09-18 17:14:01 +080026 * 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.
plars865695b2001-08-27 22:15:12 +000030 *
31 * USAGE
Wanlong Gao705d6932012-09-18 17:14:01 +080032 * fork08
plars865695b2001-08-27 22:15:12 +000033 *
34 * HISTORY
35 * 07/2001 Ported by Wayne Boyer
36 *
37 * RESTRICTIONS
Wanlong Gao705d6932012-09-18 17:14:01 +080038 * None
plars865695b2001-08-27 22:15:12 +000039 */
Wanlong Gao705d6932012-09-18 17:14:01 +080040
plars74948ad2002-11-14 16:16:14 +000041#include <sys/types.h>
42#include <sys/wait.h>
43#include <sys/stat.h>
plars865695b2001-08-27 22:15:12 +000044#include <stdio.h>
45#include "test.h"
plars865695b2001-08-27 22:15:12 +000046
47char *TCID = "fork08";
48int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000049
Wanlong Gao705d6932012-09-18 17:14:01 +080050static void setup(void);
51static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000052
Wanlong Gao705d6932012-09-18 17:14:01 +080053static char pbuf[10];
54static char fnamebuf[40];
plars865695b2001-08-27 22:15:12 +000055
plars74948ad2002-11-14 16:16:14 +000056int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000057{
58 int status, count, forks, pid1;
59 int ch_r_stat;
60 FILE *rea, *writ;
61
Wanlong Gao705d6932012-09-18 17:14:01 +080062 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020063 const char *msg;
plars865695b2001-08-27 22:15:12 +000064
Wanlong Gao705d6932012-09-18 17:14:01 +080065 msg = parse_opts(ac, av, NULL, NULL);
66 if (msg != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080067 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000068
plars865695b2001-08-27 22:15:12 +000069 setup();
70
plars865695b2001-08-27 22:15:12 +000071 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080072 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000073
Wanlong Gao705d6932012-09-18 17:14:01 +080074 writ = fopen(fnamebuf, "w");
75 if (writ == NULL)
plars865695b2001-08-27 22:15:12 +000076 tst_resm(TFAIL, "failed to fopen file for write");
Wanlong Gao705d6932012-09-18 17:14:01 +080077 rea = fopen(fnamebuf, "r");
78 if (rea == NULL)
plars865695b2001-08-27 22:15:12 +000079 tst_resm(TFAIL, "failed to fopen file for read");
80
subrata_modak56207ce2009-03-23 13:35:39 +000081 fprintf(writ, "abcdefghijklmnopqrstuv");
plars865695b2001-08-27 22:15:12 +000082 fflush(writ);
83 sleep(1);
84
subrata_modak4bb656a2009-02-26 12:02:09 +000085 if ((getc(rea)) != 'a')
plars865695b2001-08-27 22:15:12 +000086 tst_resm(TFAIL, "getc from read side was confused");
87
88 forks = 0;
89
Wanlong Gao705d6932012-09-18 17:14:01 +080090forkone:
plars865695b2001-08-27 22:15:12 +000091 ++forks;
92
Wanlong Gao705d6932012-09-18 17:14:01 +080093 pid1 = fork();
94 if (pid1 != 0) {
plars865695b2001-08-27 22:15:12 +000095 tst_resm(TINFO, "parent forksval: %d", forks);
96
97 if ((pid1 != (-1)) && (forks < 2))
98 goto forkone;
Wanlong Gao705d6932012-09-18 17:14:01 +080099 else if (pid1 < 0)
plars865695b2001-08-27 22:15:12 +0000100 tst_resm(TINFO, "Fork failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000101 } else { /* child */
plars865695b2001-08-27 22:15:12 +0000102 /*
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_modak56207ce2009-03-23 13:35:39 +0000125 } else if (ch_r_stat == EOF) {
plars865695b2001-08-27 22:15:12 +0000126 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_modak56207ce2009-03-23 13:35:39 +0000134 } else { /* end of second child */
plars865695b2001-08-27 22:15:12 +0000135 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);
plars74948ad2002-11-14 16:16:14 +0000144 status >>= 8;
Wanlong Gao705d6932012-09-18 17:14:01 +0800145 if (status == 0)
plars865695b2001-08-27 22:15:12 +0000146 tst_resm(TPASS, "parent test PASSED");
Wanlong Gao705d6932012-09-18 17:14:01 +0800147 else
plars865695b2001-08-27 22:15:12 +0000148 tst_resm(TFAIL, "parent test FAILED");
plars865695b2001-08-27 22:15:12 +0000149 }
150
151 tst_resm(TINFO, "Number of processes forked is %d", forks);
152 fclose(rea);
153 fclose(writ);
154 }
plars865695b2001-08-27 22:15:12 +0000155
Wanlong Gao705d6932012-09-18 17:14:01 +0800156 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800157 tst_exit();
plars865695b2001-08-27 22:15:12 +0000158}
subrata_modakbdbaec52009-02-26 12:14:51 +0000159
Mike Frysingerc57fba52014-04-09 18:56:30 -0400160static void setup(void)
plars865695b2001-08-27 22:15:12 +0000161{
plars865695b2001-08-27 22:15:12 +0000162 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000163 umask(0);
plars865695b2001-08-27 22:15:12 +0000164 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000165 tst_tmpdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000166
plars865695b2001-08-27 22:15:12 +0000167 strcpy(fnamebuf, "fork07.");
168 sprintf(pbuf, "%d", getpid());
169 strcat(fnamebuf, pbuf);
170}
171
Mike Frysingerc57fba52014-04-09 18:56:30 -0400172static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000173{
plars865695b2001-08-27 22:15:12 +0000174 tst_rmdir();
Wanlong Gao705d6932012-09-18 17:14:01 +0800175}