blob: 48f722e3e1e3526346caf1227dd377a375293cac [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 * pipe10.c
23 *
24 * DESCRIPTION
25 * Check that parent can open a pipe and have a child read from it
26 *
27 * ALGORITHM
28 * Parent opens pipe, child reads. Passes if child can read all the
29 * characters written by the parent.
30 *
31 * USAGE: <for command-line>
32 * pipe10 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
33 * where, -c n : Run n copies concurrently.
34 * -f : Turn off functionality Testing.
35 * -i n : Execute test n times.
36 * -I x : Execute test for x seconds.
37 * -P x : Pause for x seconds between iterations.
38 * -t : Turn on syscall timing.
39 *
40 * HISTORY
41 * 07/2001 Ported by Wayne Boyer
42 *
43 * RESTRICTIONS
44 * None
45 */
46#include <errno.h>
47#include <unistd.h>
Steven Jackson249f4052016-12-13 16:16:00 +000048#include <sys/wait.h>
robbiewbc3c43e2003-07-28 15:05:38 +000049#include <string.h>
50#include "test.h"
plars865695b2001-08-27 22:15:12 +000051
52char *TCID = "pipe10";
53int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000054
55void setup(void);
56void cleanup(void);
57
Cyril Hrubisf467e7b2016-02-08 18:50:38 +010058ssize_t do_read(int fd, void *buf, size_t count)
robbiewa728d282003-09-08 21:36:41 +000059{
60 ssize_t n;
61
62 do {
63 n = read(fd, buf, count);
64 } while (n < 0 && errno == EINTR);
65
66 return n;
67}
68
plars74948ad2002-11-14 16:16:14 +000069int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000070{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020071 int lc;
plars865695b2001-08-27 22:15:12 +000072
subrata_modak56207ce2009-03-23 13:35:39 +000073 int fd[2]; /* fds for pipe read/write */
plars865695b2001-08-27 22:15:12 +000074 char wrbuf[BUFSIZ], rebuf[BUFSIZ];
subrata_modak56207ce2009-03-23 13:35:39 +000075 int red, written; /* no of chars read and */
76 /* written to pipe */
plars865695b2001-08-27 22:15:12 +000077 int length, greater, forkstat;
subrata_modak56207ce2009-03-23 13:35:39 +000078 int retval = 0, status, e_code;
plars865695b2001-08-27 22:15:12 +000079
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010080 tst_parse_opts(ac, av, NULL, NULL);
plars865695b2001-08-27 22:15:12 +000081
82 setup();
83
84 for (lc = 0; TEST_LOOPING(lc); lc++) {
85
Caspar Zhangd59a6592013-03-07 14:59:12 +080086 /* reset tst_count in case we are looping */
87 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000088
89 TEST(pipe(fd));
90
91 if (TEST_RETURN == -1) {
plars40b56e82001-09-14 20:00:04 +000092 retval = 1;
plars865695b2001-08-27 22:15:12 +000093 tst_resm(TFAIL, "pipe creation failed");
94 continue;
95 }
96
subrata_modakda8fc612007-04-13 11:11:20 +000097 strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz");
subrata_modak56207ce2009-03-23 13:35:39 +000098 length = strlen(wrbuf) + 1;
plars865695b2001-08-27 22:15:12 +000099
100 written = write(fd[1], wrbuf, length);
101
102 /* did write write at least some chars */
103 if ((written < 0) || (written > length)) {
104 tst_brkm(TBROK, cleanup, "write to pipe failed");
105 }
106
robbiewd34d5812005-07-11 22:28:09 +0000107 forkstat = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000108
109 if (forkstat == -1) {
110 tst_brkm(TBROK, cleanup, "fork() failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800111 }
plars865695b2001-08-27 22:15:12 +0000112
subrata_modak56207ce2009-03-23 13:35:39 +0000113 if (forkstat == 0) { /* child */
Cyril Hrubisf467e7b2016-02-08 18:50:38 +0100114 red = do_read(fd[0], rebuf, written);
plars865695b2001-08-27 22:15:12 +0000115
116 /* did read , get at least some chars */
117 if ((red < 0) || (red > written)) {
118 tst_brkm(TBROK, cleanup, "read pipe failed");
119 }
120
121 greater = strcmp(rebuf, wrbuf);
122
123 /* are the strings written and read equal */
124 if (greater == 0) {
125 tst_resm(TPASS, "functionality is correct");
126 } else {
plars40b56e82001-09-14 20:00:04 +0000127 retval = 1;
plars865695b2001-08-27 22:15:12 +0000128 tst_resm(TFAIL, "read & write strings do "
129 "not match");
130 }
plars40b56e82001-09-14 20:00:04 +0000131 exit(retval);
plars865695b2001-08-27 22:15:12 +0000132 } else { /* parent */
subrata_modak56207ce2009-03-23 13:35:39 +0000133 /* wait for the child to finish */
134 wait(&status);
135 /* make sure the child returned a good exit status */
136 e_code = status >> 8;
137 if (e_code != 0) {
138 tst_resm(TFAIL, "Failures reported above");
139 }
plars865695b2001-08-27 22:15:12 +0000140 }
141 }
142 cleanup();
143
Garrett Cooper53740502010-12-16 00:04:01 -0800144 tst_exit();
plars865695b2001-08-27 22:15:12 +0000145}
146
147/*
148 * setup() - performs all ONE TIME setup for this test.
149 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400150void setup(void)
plars865695b2001-08-27 22:15:12 +0000151{
Garrett Cooper2c282152010-12-16 00:55:50 -0800152
plars865695b2001-08-27 22:15:12 +0000153 tst_sig(FORK, DEF_HANDLER, cleanup);
154
plars865695b2001-08-27 22:15:12 +0000155 TEST_PAUSE;
156}
157
158/*
159 * cleanup() - performs all ONE TIME cleanup for this test at
160 * completion or premature exit.
161 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400162void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000163{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700164}