1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker *
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) International Business Machines Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker *
5*49cdfc7eSAndroid Build Coastguard Worker * This program is free software; you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker * it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker * the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker * (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13*49cdfc7eSAndroid Build Coastguard Worker * the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker *
15*49cdfc7eSAndroid Build Coastguard Worker * You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker * along with this program; if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker */
19*49cdfc7eSAndroid Build Coastguard Worker
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker * NAME
22*49cdfc7eSAndroid Build Coastguard Worker * pipe09.c
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * DESCRIPTION
25*49cdfc7eSAndroid Build Coastguard Worker * Check that two processes can use the same pipe at the same time.
26*49cdfc7eSAndroid Build Coastguard Worker *
27*49cdfc7eSAndroid Build Coastguard Worker * ALGORITHM
28*49cdfc7eSAndroid Build Coastguard Worker * 1. Open a pipe
29*49cdfc7eSAndroid Build Coastguard Worker * 2. Fork a child which writes to the pipe
30*49cdfc7eSAndroid Build Coastguard Worker * 3. Fork another child which writes a different character to the pipe
31*49cdfc7eSAndroid Build Coastguard Worker * 4. Have the parent read from the pipe
32*49cdfc7eSAndroid Build Coastguard Worker * 5. It should get the characters from both children.
33*49cdfc7eSAndroid Build Coastguard Worker *
34*49cdfc7eSAndroid Build Coastguard Worker * USAGE: <for command-line>
35*49cdfc7eSAndroid Build Coastguard Worker * pipe09 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
36*49cdfc7eSAndroid Build Coastguard Worker * where, -c n : Run n copies concurrently.
37*49cdfc7eSAndroid Build Coastguard Worker * -f : Turn off functionality Testing.
38*49cdfc7eSAndroid Build Coastguard Worker * -i n : Execute test n times.
39*49cdfc7eSAndroid Build Coastguard Worker * -I x : Execute test for x seconds.
40*49cdfc7eSAndroid Build Coastguard Worker * -P x : Pause for x seconds between iterations.
41*49cdfc7eSAndroid Build Coastguard Worker * -t : Turn on syscall timing.
42*49cdfc7eSAndroid Build Coastguard Worker *
43*49cdfc7eSAndroid Build Coastguard Worker * HISTORY
44*49cdfc7eSAndroid Build Coastguard Worker * 07/2001 Ported by Wayne Boyer
45*49cdfc7eSAndroid Build Coastguard Worker *
46*49cdfc7eSAndroid Build Coastguard Worker * RESTRICTIONS
47*49cdfc7eSAndroid Build Coastguard Worker * None
48*49cdfc7eSAndroid Build Coastguard Worker */
49*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
50*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
51*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
52*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
53*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
54*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker #define PIPEWRTCNT 100 /* must be an even number */
57*49cdfc7eSAndroid Build Coastguard Worker
58*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "pipe09";
59*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 1;
60*49cdfc7eSAndroid Build Coastguard Worker
61*49cdfc7eSAndroid Build Coastguard Worker void setup(void);
62*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void);
63*49cdfc7eSAndroid Build Coastguard Worker
do_read(int fd,void * buf,size_t count)64*49cdfc7eSAndroid Build Coastguard Worker ssize_t do_read(int fd, void *buf, size_t count)
65*49cdfc7eSAndroid Build Coastguard Worker {
66*49cdfc7eSAndroid Build Coastguard Worker ssize_t n;
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker do {
69*49cdfc7eSAndroid Build Coastguard Worker n = read(fd, buf, count);
70*49cdfc7eSAndroid Build Coastguard Worker } while (n < 0 && errno == EINTR);
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker return n;
73*49cdfc7eSAndroid Build Coastguard Worker }
74*49cdfc7eSAndroid Build Coastguard Worker
main(int ac,char ** av)75*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
76*49cdfc7eSAndroid Build Coastguard Worker {
77*49cdfc7eSAndroid Build Coastguard Worker int lc;
78*49cdfc7eSAndroid Build Coastguard Worker
79*49cdfc7eSAndroid Build Coastguard Worker int i, red, wtstatus;
80*49cdfc7eSAndroid Build Coastguard Worker int pipefd[2]; /* fds for pipe read/write */
81*49cdfc7eSAndroid Build Coastguard Worker char rebuf[BUFSIZ];
82*49cdfc7eSAndroid Build Coastguard Worker int Acnt = 0, Bcnt = 0; /* count 'A' and 'B' */
83*49cdfc7eSAndroid Build Coastguard Worker int fork_1, fork_2; /* ret values in parent */
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker tst_parse_opts(ac, av, NULL, NULL);
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker setup();
88*49cdfc7eSAndroid Build Coastguard Worker
89*49cdfc7eSAndroid Build Coastguard Worker for (lc = 0; TEST_LOOPING(lc); lc++) {
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker /* reset tst_count in case we are looping */
92*49cdfc7eSAndroid Build Coastguard Worker tst_count = 0;
93*49cdfc7eSAndroid Build Coastguard Worker
94*49cdfc7eSAndroid Build Coastguard Worker TEST(pipe(pipefd));
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker if (TEST_RETURN == -1) {
97*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "pipe() call failed");
98*49cdfc7eSAndroid Build Coastguard Worker continue;
99*49cdfc7eSAndroid Build Coastguard Worker }
100*49cdfc7eSAndroid Build Coastguard Worker
101*49cdfc7eSAndroid Build Coastguard Worker if ((fork_1 = tst_fork()) == -1) {
102*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "fork() #1 failed");
103*49cdfc7eSAndroid Build Coastguard Worker }
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker if (fork_1 == 0) { /* 1st child */
106*49cdfc7eSAndroid Build Coastguard Worker if (close(pipefd[0]) != 0) {
107*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN, "pipefd[0] close failed, "
108*49cdfc7eSAndroid Build Coastguard Worker "errno = %d", errno);
109*49cdfc7eSAndroid Build Coastguard Worker exit(1);
110*49cdfc7eSAndroid Build Coastguard Worker }
111*49cdfc7eSAndroid Build Coastguard Worker
112*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < PIPEWRTCNT / 2; ++i) {
113*49cdfc7eSAndroid Build Coastguard Worker if (write(pipefd[1], "A", 1) != 1) {
114*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TWARN, "write to pipe failed");
115*49cdfc7eSAndroid Build Coastguard Worker exit(1);
116*49cdfc7eSAndroid Build Coastguard Worker }
117*49cdfc7eSAndroid Build Coastguard Worker }
118*49cdfc7eSAndroid Build Coastguard Worker exit(0);
119*49cdfc7eSAndroid Build Coastguard Worker }
120*49cdfc7eSAndroid Build Coastguard Worker
121*49cdfc7eSAndroid Build Coastguard Worker /* parent */
122*49cdfc7eSAndroid Build Coastguard Worker
123*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(cleanup, fork_1, &wtstatus, 0);
124*49cdfc7eSAndroid Build Coastguard Worker if (WIFEXITED(wtstatus) && WEXITSTATUS(wtstatus) != 0) {
125*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "child exited abnormally");
126*49cdfc7eSAndroid Build Coastguard Worker }
127*49cdfc7eSAndroid Build Coastguard Worker
128*49cdfc7eSAndroid Build Coastguard Worker if ((fork_2 = tst_fork()) == -1) {
129*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "fork() #2 failed");
130*49cdfc7eSAndroid Build Coastguard Worker }
131*49cdfc7eSAndroid Build Coastguard Worker
132*49cdfc7eSAndroid Build Coastguard Worker if (fork_2 == 0) { /* 2nd child */
133*49cdfc7eSAndroid Build Coastguard Worker if (close(pipefd[0]) != 0) {
134*49cdfc7eSAndroid Build Coastguard Worker perror("pipefd[0] close failed");
135*49cdfc7eSAndroid Build Coastguard Worker exit(1);
136*49cdfc7eSAndroid Build Coastguard Worker }
137*49cdfc7eSAndroid Build Coastguard Worker
138*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < PIPEWRTCNT / 2; ++i) {
139*49cdfc7eSAndroid Build Coastguard Worker if (write(pipefd[1], "B", 1) != 1) {
140*49cdfc7eSAndroid Build Coastguard Worker perror("write to pipe failed");
141*49cdfc7eSAndroid Build Coastguard Worker exit(1);
142*49cdfc7eSAndroid Build Coastguard Worker }
143*49cdfc7eSAndroid Build Coastguard Worker }
144*49cdfc7eSAndroid Build Coastguard Worker exit(0);
145*49cdfc7eSAndroid Build Coastguard Worker }
146*49cdfc7eSAndroid Build Coastguard Worker
147*49cdfc7eSAndroid Build Coastguard Worker /* parent */
148*49cdfc7eSAndroid Build Coastguard Worker
149*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(cleanup, fork_2, &wtstatus, 0);
150*49cdfc7eSAndroid Build Coastguard Worker if (WEXITSTATUS(wtstatus) != 0) {
151*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK, cleanup, "problem detected in child, "
152*49cdfc7eSAndroid Build Coastguard Worker "wait status %d, errno = %d", wtstatus, errno);
153*49cdfc7eSAndroid Build Coastguard Worker }
154*49cdfc7eSAndroid Build Coastguard Worker
155*49cdfc7eSAndroid Build Coastguard Worker if (close(pipefd[1]) != 0) {
156*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup,
157*49cdfc7eSAndroid Build Coastguard Worker "pipefd[1] close failed");
158*49cdfc7eSAndroid Build Coastguard Worker }
159*49cdfc7eSAndroid Build Coastguard Worker
160*49cdfc7eSAndroid Build Coastguard Worker while ((red = do_read(pipefd[0], rebuf, 100)) > 0) {
161*49cdfc7eSAndroid Build Coastguard Worker for (i = 0; i < red; i++) {
162*49cdfc7eSAndroid Build Coastguard Worker if (rebuf[i] == 'A') {
163*49cdfc7eSAndroid Build Coastguard Worker Acnt++;
164*49cdfc7eSAndroid Build Coastguard Worker continue;
165*49cdfc7eSAndroid Build Coastguard Worker }
166*49cdfc7eSAndroid Build Coastguard Worker if (rebuf[i] == 'B') {
167*49cdfc7eSAndroid Build Coastguard Worker Bcnt++;
168*49cdfc7eSAndroid Build Coastguard Worker continue;
169*49cdfc7eSAndroid Build Coastguard Worker }
170*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "got bogus '%c' character",
171*49cdfc7eSAndroid Build Coastguard Worker rebuf[i]);
172*49cdfc7eSAndroid Build Coastguard Worker break;
173*49cdfc7eSAndroid Build Coastguard Worker }
174*49cdfc7eSAndroid Build Coastguard Worker }
175*49cdfc7eSAndroid Build Coastguard Worker
176*49cdfc7eSAndroid Build Coastguard Worker if (red == -1) {
177*49cdfc7eSAndroid Build Coastguard Worker tst_brkm(TBROK | TERRNO, cleanup,
178*49cdfc7eSAndroid Build Coastguard Worker "reading pipefd pipe failed");
179*49cdfc7eSAndroid Build Coastguard Worker }
180*49cdfc7eSAndroid Build Coastguard Worker
181*49cdfc7eSAndroid Build Coastguard Worker if (Bcnt == Acnt && Bcnt == (PIPEWRTCNT / 2)) {
182*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TPASS, "functionality appears to be correct");
183*49cdfc7eSAndroid Build Coastguard Worker } else {
184*49cdfc7eSAndroid Build Coastguard Worker tst_resm(TFAIL, "functionality is not correct - Acnt "
185*49cdfc7eSAndroid Build Coastguard Worker "= %d, Bcnt = %d", Acnt, Bcnt);
186*49cdfc7eSAndroid Build Coastguard Worker }
187*49cdfc7eSAndroid Build Coastguard Worker
188*49cdfc7eSAndroid Build Coastguard Worker /* clean up things in case we are looping */
189*49cdfc7eSAndroid Build Coastguard Worker Acnt = Bcnt = 0;
190*49cdfc7eSAndroid Build Coastguard Worker }
191*49cdfc7eSAndroid Build Coastguard Worker cleanup();
192*49cdfc7eSAndroid Build Coastguard Worker
193*49cdfc7eSAndroid Build Coastguard Worker tst_exit();
194*49cdfc7eSAndroid Build Coastguard Worker }
195*49cdfc7eSAndroid Build Coastguard Worker
196*49cdfc7eSAndroid Build Coastguard Worker /*
197*49cdfc7eSAndroid Build Coastguard Worker * setup() - performs all ONE TIME setup for this test.
198*49cdfc7eSAndroid Build Coastguard Worker */
setup(void)199*49cdfc7eSAndroid Build Coastguard Worker void setup(void)
200*49cdfc7eSAndroid Build Coastguard Worker {
201*49cdfc7eSAndroid Build Coastguard Worker
202*49cdfc7eSAndroid Build Coastguard Worker tst_sig(FORK, DEF_HANDLER, cleanup);
203*49cdfc7eSAndroid Build Coastguard Worker
204*49cdfc7eSAndroid Build Coastguard Worker TEST_PAUSE;
205*49cdfc7eSAndroid Build Coastguard Worker }
206*49cdfc7eSAndroid Build Coastguard Worker
207*49cdfc7eSAndroid Build Coastguard Worker /*
208*49cdfc7eSAndroid Build Coastguard Worker * cleanup() - performs all ONE TIME cleanup for this test at
209*49cdfc7eSAndroid Build Coastguard Worker * completion or premature exit.
210*49cdfc7eSAndroid Build Coastguard Worker */
cleanup(void)211*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
212*49cdfc7eSAndroid Build Coastguard Worker {
213*49cdfc7eSAndroid Build Coastguard Worker }
214