blob: a215c002e49d59d49bb1699f9c5b4fdcbe63c373 [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 Gaoec78e0a2012-09-18 17:14:01 +080017 *
18 *
plars865695b2001-08-27 22:15:12 +000019 * NAME
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080020 * fork10.c
plars865695b2001-08-27 22:15:12 +000021 *
22 * DESCRIPTION
23 * Check inheritance of file descriptor by children, they
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080024 * should all be refering to the same file.
plars865695b2001-08-27 22:15:12 +000025 *
26 * ALGORITHM
subrata_modak4bb656a2009-02-26 12:02:09 +000027 * Child reads several chars and exits.
plars865695b2001-08-27 22:15:12 +000028 * Parent forks another child, have the child and parent attempt to use
29 * that location
30 *
31 * USAGE
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080032 * fork10
plars865695b2001-08-27 22:15:12 +000033 *
34 * HISTORY
35 * 07/2001 Ported by Wayne Boyer
36 *
37 * RESTRICTIONS
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080038 * None
plars865695b2001-08-27 22:15:12 +000039 */
Wanlong Gaoec78e0a2012-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>
44#include <fcntl.h>
plars865695b2001-08-27 22:15:12 +000045#include <stdio.h>
46#include <errno.h>
47#include "test.h"
plars865695b2001-08-27 22:15:12 +000048
nstrazfa31d552002-05-14 16:50:06 +000049char *TCID = "fork10";
plars865695b2001-08-27 22:15:12 +000050int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000051
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080052static void setup(void);
53static void cleanup(void);
plars865695b2001-08-27 22:15:12 +000054
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080055static char pidbuf[10];
56static char fnamebuf[40];
plars865695b2001-08-27 22:15:12 +000057
plars74948ad2002-11-14 16:16:14 +000058int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000059{
60 int status, pid, fildes;
61 char parchar[2];
62 char chilchar[2];
plars865695b2001-08-27 22:15:12 +000063
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080064 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020065 const char *msg;
plars865695b2001-08-27 22:15:12 +000066
Garrett Cooper15697992010-12-18 06:31:28 -080067 fildes = -1;
plars865695b2001-08-27 22:15:12 +000068
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080069 msg = parse_opts(ac, av, NULL, NULL);
70 if (msg != NULL)
Garrett Cooper15697992010-12-18 06:31:28 -080071 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
72
plars865695b2001-08-27 22:15:12 +000073 setup();
74
plars865695b2001-08-27 22:15:12 +000075 for (lc = 0; TEST_LOOPING(lc); lc++) {
Caspar Zhangd59a6592013-03-07 14:59:12 +080076 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000077
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080078 fildes = creat(fnamebuf, 0600);
79 if (fildes < 0)
80 tst_brkm(TBROK | TERRNO, cleanup,
81 "Parent: cannot open %s for " "write",
82 fnamebuf);
plars865695b2001-08-27 22:15:12 +000083 write(fildes, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 27);
84 close(fildes);
85
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080086 fildes = open(fnamebuf, 0);
87 if (fildes == -1)
plars865695b2001-08-27 22:15:12 +000088 tst_brkm(TBROK, cleanup, "Parent: cannot open %s for "
89 "reading", fnamebuf);
plars865695b2001-08-27 22:15:12 +000090
Wanlong Gaoec78e0a2012-09-18 17:14:01 +080091 pid = fork();
92 if (pid == -1)
plars865695b2001-08-27 22:15:12 +000093 tst_brkm(TBROK, cleanup, "fork() #1 failed");
plars865695b2001-08-27 22:15:12 +000094
subrata_modak56207ce2009-03-23 13:35:39 +000095 if (pid == 0) { /* child */
plars865695b2001-08-27 22:15:12 +000096 tst_resm(TINFO, "fork child A");
97 if (lseek(fildes, 10L, 0) == -1L) {
98 tst_resm(TFAIL, "bad lseek by child");
99 exit(1);
100 }
101 exit(0);
subrata_modak56207ce2009-03-23 13:35:39 +0000102 } else { /* parent */
plars865695b2001-08-27 22:15:12 +0000103 wait(&status);
104
105 /* parent starts second child */
Wanlong Gaoec78e0a2012-09-18 17:14:01 +0800106 pid = fork();
107 if (pid == -1)
plars865695b2001-08-27 22:15:12 +0000108 tst_brkm(TBROK, cleanup, "fork() #2 failed");
plars865695b2001-08-27 22:15:12 +0000109
110 if (pid == 0) { /* child */
111 if (read(fildes, chilchar, 1) <= 0) {
112 tst_resm(TFAIL, "Child can't read "
113 "file");
114 exit(1);
115 } else {
116 if (chilchar[0] != 'K') {
117 chilchar[1] = '\n';
118 exit(1);
119 } else {
120 exit(0);
121 }
122 }
subrata_modak56207ce2009-03-23 13:35:39 +0000123 } else { /* parent */
plars865695b2001-08-27 22:15:12 +0000124 (void)wait(&status);
125 if (status >> 8 != 0) {
126 tst_resm(TFAIL, "Bad return from "
127 "second child");
128 continue;
129 }
130 /* parent reads */
131 if (read(fildes, parchar, 1) <= 0) {
132 tst_resm(TFAIL, "Parent cannot read "
133 "file");
134 continue;
135 } else {
136 write(fildes, parchar, 1);
137 if (parchar[0] != 'L') {
138 parchar[1] = '\n';
139 tst_resm(TFAIL, "Test failed");
140 continue;
141 }
142 }
143 }
144 }
145 tst_resm(TPASS, "test 1 PASSED");
146 }
Wanlong Gaoec78e0a2012-09-18 17:14:01 +0800147
subrata_modak56207ce2009-03-23 13:35:39 +0000148 close(fildes);
plars865695b2001-08-27 22:15:12 +0000149 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800150 tst_exit();
plars865695b2001-08-27 22:15:12 +0000151}
152
Mike Frysingerc57fba52014-04-09 18:56:30 -0400153static void setup(void)
plars865695b2001-08-27 22:15:12 +0000154{
plars865695b2001-08-27 22:15:12 +0000155 tst_sig(FORK, DEF_HANDLER, cleanup);
plars865695b2001-08-27 22:15:12 +0000156 umask(0);
plars865695b2001-08-27 22:15:12 +0000157 TEST_PAUSE;
plars865695b2001-08-27 22:15:12 +0000158 tst_tmpdir();
subrata_modakbdbaec52009-02-26 12:14:51 +0000159
nstrazfa31d552002-05-14 16:50:06 +0000160 strcpy(fnamebuf, "fork10.");
plars865695b2001-08-27 22:15:12 +0000161 sprintf(pidbuf, "%d", getpid());
162 strcat(fnamebuf, pidbuf);
163}
164
Mike Frysingerc57fba52014-04-09 18:56:30 -0400165static void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000166{
plars865695b2001-08-27 22:15:12 +0000167 tst_rmdir();
Wanlong Gaoec78e0a2012-09-18 17:14:01 +0800168}