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