blob: dd484951397d46b0eaad8cd16123f892fd3df32f [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>
48#include <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
robbiewa728d282003-09-08 21:36:41 +000058ssize_t safe_read(int fd, void *buf, size_t count)
59{
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;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020072 const char *msg;
plars865695b2001-08-27 22:15:12 +000073
subrata_modak56207ce2009-03-23 13:35:39 +000074 int fd[2]; /* fds for pipe read/write */
plars865695b2001-08-27 22:15:12 +000075 char wrbuf[BUFSIZ], rebuf[BUFSIZ];
subrata_modak56207ce2009-03-23 13:35:39 +000076 int red, written; /* no of chars read and */
77 /* written to pipe */
plars865695b2001-08-27 22:15:12 +000078 int length, greater, forkstat;
subrata_modak56207ce2009-03-23 13:35:39 +000079 int retval = 0, status, e_code;
plars865695b2001-08-27 22:15:12 +000080
Garrett Cooper53740502010-12-16 00:04:01 -080081 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080082 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000083
84 setup();
85
86 for (lc = 0; TEST_LOOPING(lc); lc++) {
87
Caspar Zhangd59a6592013-03-07 14:59:12 +080088 /* reset tst_count in case we are looping */
89 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000090
91 TEST(pipe(fd));
92
93 if (TEST_RETURN == -1) {
plars40b56e82001-09-14 20:00:04 +000094 retval = 1;
plars865695b2001-08-27 22:15:12 +000095 tst_resm(TFAIL, "pipe creation failed");
96 continue;
97 }
98
subrata_modakda8fc612007-04-13 11:11:20 +000099 strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz");
subrata_modak56207ce2009-03-23 13:35:39 +0000100 length = strlen(wrbuf) + 1;
plars865695b2001-08-27 22:15:12 +0000101
102 written = write(fd[1], wrbuf, length);
103
104 /* did write write at least some chars */
105 if ((written < 0) || (written > length)) {
106 tst_brkm(TBROK, cleanup, "write to pipe failed");
107 }
108
robbiewd34d5812005-07-11 22:28:09 +0000109 forkstat = FORK_OR_VFORK();
plars865695b2001-08-27 22:15:12 +0000110
111 if (forkstat == -1) {
112 tst_brkm(TBROK, cleanup, "fork() failed");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 }
plars865695b2001-08-27 22:15:12 +0000114
subrata_modak56207ce2009-03-23 13:35:39 +0000115 if (forkstat == 0) { /* child */
robbiewa728d282003-09-08 21:36:41 +0000116 red = safe_read(fd[0], rebuf, written);
plars865695b2001-08-27 22:15:12 +0000117
118 /* did read , get at least some chars */
119 if ((red < 0) || (red > written)) {
120 tst_brkm(TBROK, cleanup, "read pipe failed");
121 }
122
123 greater = strcmp(rebuf, wrbuf);
124
125 /* are the strings written and read equal */
126 if (greater == 0) {
127 tst_resm(TPASS, "functionality is correct");
128 } else {
plars40b56e82001-09-14 20:00:04 +0000129 retval = 1;
plars865695b2001-08-27 22:15:12 +0000130 tst_resm(TFAIL, "read & write strings do "
131 "not match");
132 }
plars40b56e82001-09-14 20:00:04 +0000133 exit(retval);
plars865695b2001-08-27 22:15:12 +0000134 } else { /* parent */
subrata_modak56207ce2009-03-23 13:35:39 +0000135 /* wait for the child to finish */
136 wait(&status);
137 /* make sure the child returned a good exit status */
138 e_code = status >> 8;
139 if (e_code != 0) {
140 tst_resm(TFAIL, "Failures reported above");
141 }
plars865695b2001-08-27 22:15:12 +0000142 }
143 }
144 cleanup();
145
Garrett Cooper53740502010-12-16 00:04:01 -0800146 tst_exit();
plars865695b2001-08-27 22:15:12 +0000147}
148
149/*
150 * setup() - performs all ONE TIME setup for this test.
151 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400152void setup(void)
plars865695b2001-08-27 22:15:12 +0000153{
Garrett Cooper2c282152010-12-16 00:55:50 -0800154
plars865695b2001-08-27 22:15:12 +0000155 tst_sig(FORK, DEF_HANDLER, cleanup);
156
plars865695b2001-08-27 22:15:12 +0000157 TEST_PAUSE;
158}
159
160/*
161 * cleanup() - performs all ONE TIME cleanup for this test at
162 * completion or premature exit.
163 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400164void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000165{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700166}