blob: 006d0295499f1fc0f43db6a8a43326a6a298497b [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2001
4 *
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.
9 *
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.
14 *
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
Wanlong Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
22 * pipe09.c
23 *
24 * DESCRIPTION
25 * Check that two processes can use the same pipe at the same time.
26 *
27 * ALGORITHM
28 * 1. Open a pipe
29 * 2. Fork a child which writes to the pipe
30 * 3. Fork another child which writes a different character to the pipe
31 * 4. Have the parent read from the pipe
32 * 5. It should get the characters from both children.
33 *
34 * USAGE: <for command-line>
35 * pipe09 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
36 * where, -c n : Run n copies concurrently.
37 * -f : Turn off functionality Testing.
38 * -i n : Execute test n times.
39 * -I x : Execute test for x seconds.
40 * -P x : Pause for x seconds between iterations.
41 * -t : Turn on syscall timing.
42 *
43 * HISTORY
44 * 07/2001 Ported by Wayne Boyer
45 *
46 * RESTRICTIONS
47 * None
48 */
49#include <unistd.h>
50#include <signal.h>
51#include <wait.h>
52#include <errno.h>
53#include "test.h"
plars865695b2001-08-27 22:15:12 +000054
subrata_modak56207ce2009-03-23 13:35:39 +000055#define PIPEWRTCNT 100 /* must be an even number */
plars865695b2001-08-27 22:15:12 +000056
57char *TCID = "pipe09";
58int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000059
60void setup(void);
61void cleanup(void);
62
robbiewa728d282003-09-08 21:36:41 +000063ssize_t safe_read(int fd, void *buf, size_t count)
64{
65 ssize_t n;
66
67 do {
68 n = read(fd, buf, count);
69 } while (n < 0 && errno == EINTR);
70
71 return n;
72}
73
plars74948ad2002-11-14 16:16:14 +000074int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000075{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020076 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020077 const char *msg;
plars865695b2001-08-27 22:15:12 +000078
plars74948ad2002-11-14 16:16:14 +000079 int i, red, wtstatus;
subrata_modak56207ce2009-03-23 13:35:39 +000080 int pipefd[2]; /* fds for pipe read/write */
plars865695b2001-08-27 22:15:12 +000081 char rebuf[BUFSIZ];
subrata_modak56207ce2009-03-23 13:35:39 +000082 int Acnt = 0, Bcnt = 0; /* count 'A' and 'B' */
83 int fork_1, fork_2; /* ret values in parent */
plars865695b2001-08-27 22:15:12 +000084
Garrett Cooper53740502010-12-16 00:04:01 -080085 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080086 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000087
88 setup();
89
90 for (lc = 0; TEST_LOOPING(lc); lc++) {
91
Caspar Zhangd59a6592013-03-07 14:59:12 +080092 /* reset tst_count in case we are looping */
93 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000094
95 TEST(pipe(pipefd));
96
97 if (TEST_RETURN == -1) {
98 tst_resm(TFAIL, "pipe() call failed");
99 continue;
100 }
101
robbiewd34d5812005-07-11 22:28:09 +0000102 if ((fork_1 = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000103 tst_brkm(TBROK, cleanup, "fork() #1 failed");
Garrett Cooper53740502010-12-16 00:04:01 -0800104 }
plars865695b2001-08-27 22:15:12 +0000105
106 if (fork_1 == 0) { /* 1st child */
107 if (close(pipefd[0]) != 0) {
108 tst_resm(TWARN, "pipefd[0] close failed, "
109 "errno = %d", errno);
110 exit(1);
111 }
112
113 for (i = 0; i < PIPEWRTCNT / 2; ++i) {
114 if (write(pipefd[1], "A", 1) != 1) {
115 tst_resm(TWARN, "write to pipe failed");
116 exit(1);
117 }
118 }
119 exit(0);
120 }
121
122 /* parent */
123
Garrett Cooper53740502010-12-16 00:04:01 -0800124 if (waitpid(fork_1, &wtstatus, 0) == -1)
125 tst_brkm(TBROK, cleanup, "waitpid failed");
Garrett Cooperc452c252010-12-19 10:12:42 -0800126 if (WIFEXITED(wtstatus) && WEXITSTATUS(wtstatus) != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800127 tst_brkm(TBROK, cleanup, "child exited abnormally");
plars865695b2001-08-27 22:15:12 +0000128 }
129
robbiewd34d5812005-07-11 22:28:09 +0000130 if ((fork_2 = FORK_OR_VFORK()) == -1) {
plars865695b2001-08-27 22:15:12 +0000131 tst_brkm(TBROK, cleanup, "fork() #2 failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 }
plars865695b2001-08-27 22:15:12 +0000133
134 if (fork_2 == 0) { /* 2nd child */
135 if (close(pipefd[0]) != 0) {
Garrett Cooper53740502010-12-16 00:04:01 -0800136 perror("pipefd[0] close failed");
plars865695b2001-08-27 22:15:12 +0000137 exit(1);
138 }
139
140 for (i = 0; i < PIPEWRTCNT / 2; ++i) {
141 if (write(pipefd[1], "B", 1) != 1) {
Garrett Cooper53740502010-12-16 00:04:01 -0800142 perror("write to pipe failed");
plars865695b2001-08-27 22:15:12 +0000143 exit(1);
144 }
145 }
146 exit(0);
147 }
148
149 /* parent */
150
Garrett Cooper53740502010-12-16 00:04:01 -0800151 if (waitpid(fork_2, &wtstatus, 0) == -1)
152 tst_brkm(TBROK, cleanup, "waitpid failed");
plars865695b2001-08-27 22:15:12 +0000153 if (WEXITSTATUS(wtstatus) != 0) {
154 tst_brkm(TBROK, cleanup, "problem detected in child, "
155 "wait status %d, errno = %d", wtstatus, errno);
156 }
157
158 if (close(pipefd[1]) != 0) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800159 tst_brkm(TBROK | TERRNO, cleanup,
160 "pipefd[1] close failed");
Garrett Cooper53740502010-12-16 00:04:01 -0800161 }
plars865695b2001-08-27 22:15:12 +0000162
robbiewa728d282003-09-08 21:36:41 +0000163 while ((red = safe_read(pipefd[0], rebuf, 100)) > 0) {
plars865695b2001-08-27 22:15:12 +0000164 for (i = 0; i < red; i++) {
165 if (rebuf[i] == 'A') {
166 Acnt++;
167 continue;
168 }
169 if (rebuf[i] == 'B') {
170 Bcnt++;
171 continue;
172 }
173 tst_resm(TFAIL, "got bogus '%c' character",
174 rebuf[i]);
175 break;
176 }
177 }
178
179 if (red == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800180 tst_brkm(TBROK | TERRNO, cleanup,
181 "reading pipefd pipe failed");
plars865695b2001-08-27 22:15:12 +0000182 }
183
184 if (Bcnt == Acnt && Bcnt == (PIPEWRTCNT / 2)) {
185 tst_resm(TPASS, "functionality appears to be correct");
186 } else {
187 tst_resm(TFAIL, "functionality is not correct - Acnt "
188 "= %d, Bcnt = %d", Acnt, Bcnt);
189 }
190
191 /* clean up things in case we are looping */
192 Acnt = Bcnt = 0;
193 }
194 cleanup();
195
Garrett Cooper53740502010-12-16 00:04:01 -0800196 tst_exit();
plars865695b2001-08-27 22:15:12 +0000197}
198
199/*
200 * setup() - performs all ONE TIME setup for this test.
201 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400202void setup(void)
plars865695b2001-08-27 22:15:12 +0000203{
Garrett Cooper2c282152010-12-16 00:55:50 -0800204
plars865695b2001-08-27 22:15:12 +0000205 tst_sig(FORK, DEF_HANDLER, cleanup);
206
plars865695b2001-08-27 22:15:12 +0000207 TEST_PAUSE;
208}
209
210/*
211 * cleanup() - performs all ONE TIME cleanup for this test at
212 * completion or premature exit.
213 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400214void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000215{
Garrett Cooperc452c252010-12-19 10:12:42 -0800216}