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 | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 17 | * |
| 18 | * |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 19 | * NAME |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 20 | * fork10.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 21 | * |
| 22 | * DESCRIPTION |
| 23 | * Check inheritance of file descriptor by children, they |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 24 | * should all be refering to the same file. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 25 | * |
| 26 | * ALGORITHM |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 27 | * Child reads several chars and exits. |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 28 | * Parent forks another child, have the child and parent attempt to use |
| 29 | * that location |
| 30 | * |
| 31 | * USAGE |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 32 | * fork10 |
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 | ec78e0a | 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 | ec78e0a | 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> |
| 44 | #include <fcntl.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 45 | #include <stdio.h> |
| 46 | #include <errno.h> |
| 47 | #include "test.h" |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 48 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 49 | char *TCID = "fork10"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 50 | int TST_TOTAL = 1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 51 | |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 52 | static void setup(void); |
| 53 | static void cleanup(void); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 54 | |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 55 | static char pidbuf[10]; |
| 56 | static char fnamebuf[40]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 57 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 58 | int main(int ac, char **av) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 59 | { |
| 60 | int status, pid, fildes; |
| 61 | char parchar[2]; |
| 62 | char chilchar[2]; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 63 | |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 64 | int lc; |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 65 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 66 | |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 67 | fildes = -1; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 68 | |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 69 | msg = parse_opts(ac, av, NULL, NULL); |
| 70 | if (msg != NULL) |
Garrett Cooper | 1569799 | 2010-12-18 06:31:28 -0800 | [diff] [blame] | 71 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
| 72 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 73 | setup(); |
| 74 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 75 | for (lc = 0; TEST_LOOPING(lc); lc++) { |
Caspar Zhang | d59a659 | 2013-03-07 14:59:12 +0800 | [diff] [blame] | 76 | tst_count = 0; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 78 | fildes = creat(fnamebuf, 0600); |
| 79 | if (fildes < 0) |
| 80 | tst_brkm(TBROK | TERRNO, cleanup, |
| 81 | "Parent: cannot open %s for " "write", |
| 82 | fnamebuf); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 83 | write(fildes, "ABCDEFGHIJKLMNOPQRSTUVWXYZ\n", 27); |
| 84 | close(fildes); |
| 85 | |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 86 | fildes = open(fnamebuf, 0); |
| 87 | if (fildes == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 88 | tst_brkm(TBROK, cleanup, "Parent: cannot open %s for " |
| 89 | "reading", fnamebuf); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 90 | |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 91 | pid = fork(); |
| 92 | if (pid == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 93 | tst_brkm(TBROK, cleanup, "fork() #1 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 95 | if (pid == 0) { /* child */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 96 | 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 102 | } else { /* parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 103 | wait(&status); |
| 104 | |
| 105 | /* parent starts second child */ |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 106 | pid = fork(); |
| 107 | if (pid == -1) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 108 | tst_brkm(TBROK, cleanup, "fork() #2 failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 109 | |
| 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_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 123 | } else { /* parent */ |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 124 | (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 Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 147 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 148 | close(fildes); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 149 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 150 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 153 | static void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 154 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 155 | tst_sig(FORK, DEF_HANDLER, cleanup); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 156 | umask(0); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | TEST_PAUSE; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 158 | tst_tmpdir(); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 159 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 160 | strcpy(fnamebuf, "fork10."); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 161 | sprintf(pidbuf, "%d", getpid()); |
| 162 | strcat(fnamebuf, pidbuf); |
| 163 | } |
| 164 | |
Mike Frysinger | c57fba5 | 2014-04-09 18:56:30 -0400 | [diff] [blame] | 165 | static void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 167 | tst_rmdir(); |
Wanlong Gao | ec78e0a | 2012-09-18 17:14:01 +0800 | [diff] [blame] | 168 | } |