blob: 73bcdaaed855c955acddf0968a98c6a7e57392cd [file] [log] [blame]
robbiew906d4682003-01-06 17:23:07 +00001/*
2 *
3 * Copyright (c) International Business Machines Corp., 2002
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
robbiew906d4682003-01-06 17:23:07 +000018 */
19
20/*
21 * NAME
22 * pipe03.c
23 *
24 * DESCRIPTION
subrata_modak4bb656a2009-02-26 12:02:09 +000025 * Make sure that writing to the read end of a pipe and reading from
robbiew906d4682003-01-06 17:23:07 +000026 * the write end of a pipe both fail.
27 *
28 * ALGORITHM
29 * 1. Open a pipe
30 * 2. Attempt to write to the [0] descriptor (expect -1)
31 * 3. Attempt to read from the [1] descriptor (expect -1)
32 *
33 * USAGE: <for command-line>
34 * pipe03 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
35 * where, -c n : Run n copies concurrently.
36 * -f : Turn off functionality Testing.
37 * -i n : Execute test n times.
38 * -I x : Execute test for x seconds.
39 * -P x : Pause for x seconds between iterations.
40 * -t : Turn on syscall timing.
41 *
42 * HISTORY
43 * 11/2002 Ported by Paul Larson
44 */
45#include <unistd.h>
46#include <errno.h>
47#include "test.h"
robbiew906d4682003-01-06 17:23:07 +000048
49char *TCID = "pipe03";
50int TST_TOTAL = 1;
robbiew906d4682003-01-06 17:23:07 +000051
robbiew906d4682003-01-06 17:23:07 +000052void setup(void);
53void cleanup(void);
54
robbiewa728d282003-09-08 21:36:41 +000055ssize_t safe_read(int fd, void *buf, size_t count)
56{
57 ssize_t n;
58
59 do {
60 n = read(fd, buf, count);
61 } while (n < 0 && errno == EINTR);
62
63 return n;
64}
65
robbiew906d4682003-01-06 17:23:07 +000066int main(int ac, char **av)
67{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020068 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020069 const char *msg;
robbiew906d4682003-01-06 17:23:07 +000070
subrata_modak56207ce2009-03-23 13:35:39 +000071 int fildes[2]; /* fds for pipe read and write */
robbiew906d4682003-01-06 17:23:07 +000072 char rbuf[BUFSIZ];
73
Garrett Cooper53740502010-12-16 00:04:01 -080074 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080075 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
robbiew906d4682003-01-06 17:23:07 +000076
77 setup();
78
79 for (lc = 0; TEST_LOOPING(lc); lc++) {
80
Caspar Zhangd59a6592013-03-07 14:59:12 +080081 /* reset tst_count in case we are looping */
82 tst_count = 0;
robbiew906d4682003-01-06 17:23:07 +000083
84 TEST(pipe(fildes));
85
86 if (TEST_RETURN == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +080087 tst_brkm(TBROK | TTERRNO, cleanup,
88 "pipe() failed unexpectedly");
robbiew906d4682003-01-06 17:23:07 +000089
90 TEST(write(fildes[0], "A", 1));
91 if (TEST_RETURN == -1 && TEST_ERRNO == EBADF)
92 tst_resm(TPASS, "expected failure writing to "
subrata_modak56207ce2009-03-23 13:35:39 +000093 "read end of pipe");
robbiew906d4682003-01-06 17:23:07 +000094 else
Wanlong Gao354ebb42012-12-07 10:10:04 +080095 tst_resm(TFAIL | TTERRNO,
96 "success when writing to read "
97 "end of pipe ret=%ld", TEST_RETURN);
robbiew906d4682003-01-06 17:23:07 +000098
robbiewa728d282003-09-08 21:36:41 +000099 TEST(safe_read(fildes[1], rbuf, 1));
robbiew906d4682003-01-06 17:23:07 +0000100 if (TEST_RETURN == -1 && TEST_ERRNO == EBADF)
101 tst_resm(TPASS, "expected failure reading from "
subrata_modak56207ce2009-03-23 13:35:39 +0000102 "write end of pipe");
robbiew906d4682003-01-06 17:23:07 +0000103 else
Wanlong Gao354ebb42012-12-07 10:10:04 +0800104 tst_resm(TFAIL | TTERRNO, "success when reading from "
Garrett Cooperc452c252010-12-19 10:12:42 -0800105 "write end of pipe ret=%ld", TEST_RETURN);
robbiew906d4682003-01-06 17:23:07 +0000106 }
107 cleanup();
108
Garrett Cooper53740502010-12-16 00:04:01 -0800109 tst_exit();
robbiew906d4682003-01-06 17:23:07 +0000110}
111
112/*
113 * setup() - performs all ONE TIME setup for this test.
114 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400115void setup(void)
robbiew906d4682003-01-06 17:23:07 +0000116{
Garrett Cooper2c282152010-12-16 00:55:50 -0800117
robbiew906d4682003-01-06 17:23:07 +0000118 tst_sig(NOFORK, DEF_HANDLER, cleanup);
119
robbiew906d4682003-01-06 17:23:07 +0000120 TEST_PAUSE;
121}
122
123/*
124 * cleanup() - performs all ONE TIME cleanup for this test at
125 * completion or premature exit.
126 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400127void cleanup(void)
robbiew906d4682003-01-06 17:23:07 +0000128{
Garrett Cooperc452c252010-12-19 10:12:42 -0800129}