blob: 26c644b1ff87359e1daa445144c7eb6b6e314ee1 [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 * pipe01.c
23 *
24 * DESCRIPTION
25 * Testcase to check the basic functionality of the pipe(2) syscall:
26 * Check that both ends of the pipe (both file descriptors) are
27 * available to a process opening the pipe.
28 *
29 * ALGORITHM
30 * Write a string of characters down a pipe; read the string from the
31 * other file descriptor. Test passes if both can be done, as reported
32 * by the number of characters written and read.
33 *
34 * USAGE: <for command-line>
35 * pipe01 [-c n] [-f] [-i n] [-I x] [-P x] [-t]
36 * where, -c n : Run n copies concurrently.
37 * -f : Turn off functionality Testing.
38 * -i n : Execute test n times.
39 * -I x : Execute test for x seconds.
40 * -P x : Pause for x seconds between iterations.
41 * -t : Turn on syscall timing.
42 *
43 * RESTRICITONS
44 * NONE
45 */
46#include <unistd.h>
47#include <errno.h>
robbiewbc3c43e2003-07-28 15:05:38 +000048#include <string.h>
plars865695b2001-08-27 22:15:12 +000049#include "test.h"
plars865695b2001-08-27 22:15:12 +000050
51char *TCID = "pipe01";
52int TST_TOTAL = 1;
plars865695b2001-08-27 22:15:12 +000053
54void setup(void);
55void cleanup(void);
56
robbiewa728d282003-09-08 21:36:41 +000057ssize_t safe_read(int fd, void *buf, size_t count)
58{
59 ssize_t n;
60
61 do {
62 n = read(fd, buf, count);
63 } while (n < 0 && errno == EINTR);
64
65 return n;
66}
67
plars74948ad2002-11-14 16:16:14 +000068int main(int ac, char **av)
plars865695b2001-08-27 22:15:12 +000069{
Cyril Hrubis89af32a2012-10-24 16:39:11 +020070 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +020071 const char *msg;
plars865695b2001-08-27 22:15:12 +000072
subrata_modak56207ce2009-03-23 13:35:39 +000073 int fildes[2]; /* fds for pipe read and 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/written to pipe */
plars865695b2001-08-27 22:15:12 +000076 int greater, length;
plars865695b2001-08-27 22:15:12 +000077
Garrett Cooper53740502010-12-16 00:04:01 -080078 if ((msg = parse_opts(ac, av, NULL, NULL)) != NULL)
Garrett Cooper60fa8012010-11-22 13:50:58 -080079 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
plars865695b2001-08-27 22:15:12 +000080
81 setup();
82
83 for (lc = 0; TEST_LOOPING(lc); lc++) {
84
Caspar Zhangd59a6592013-03-07 14:59:12 +080085 /* reset tst_count in case we are looping */
86 tst_count = 0;
plars865695b2001-08-27 22:15:12 +000087
88 TEST(pipe(fildes));
89
90 if (TEST_RETURN == -1) {
91 tst_resm(TFAIL, "pipe() failed unexpectedly - errno %d",
92 TEST_ERRNO);
93 continue;
94 }
95
Cyril Hrubise38b9612014-06-02 17:20:57 +020096 strcpy(wrbuf, "abcdefghijklmnopqrstuvwxyz");
97 length = strlen(wrbuf);
plars865695b2001-08-27 22:15:12 +000098
Cyril Hrubise38b9612014-06-02 17:20:57 +020099 if ((written = write(fildes[1], wrbuf, length)) == -1) {
100 tst_brkm(TBROK, cleanup, "write() failed");
plars865695b2001-08-27 22:15:12 +0000101 }
plars865695b2001-08-27 22:15:12 +0000102
Cyril Hrubise38b9612014-06-02 17:20:57 +0200103 if (written < 0 || written > 26) {
104 tst_resm(TFAIL, "Condition #1 test failed");
105 continue;
106 }
107
108 if ((red = safe_read(fildes[0], rebuf, written)) == -1) {
109 tst_brkm(TBROK | TERRNO, cleanup,
110 "read() failed");
111 }
112
113 if (red < 0 || red > written) {
114 tst_resm(TFAIL, "Condition #2 test failed");
115 continue;
116 }
117
118 /* are the strings written and read equal */
119 if ((greater = strncmp(rebuf, wrbuf, red)) != 0) {
120 tst_resm(TFAIL, "Condition #3 test failed");
121 continue;
122 }
123 tst_resm(TPASS, "pipe() functionality is correct");
124 }
125
126 cleanup();
Garrett Cooper53740502010-12-16 00:04:01 -0800127 tst_exit();
plars865695b2001-08-27 22:15:12 +0000128}
129
130/*
131 * setup() - performs all ONE TIME setup for this test.
132 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400133void setup(void)
plars865695b2001-08-27 22:15:12 +0000134{
Garrett Cooper2c282152010-12-16 00:55:50 -0800135
plars865695b2001-08-27 22:15:12 +0000136 tst_sig(NOFORK, DEF_HANDLER, cleanup);
137
plars865695b2001-08-27 22:15:12 +0000138 TEST_PAUSE;
139}
140
141/*
142 * cleanup() - performs all ONE TIME cleanup for this test at
143 * completion or premature exit.
144 */
Mike Frysingerc57fba52014-04-09 18:56:30 -0400145void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000146{
Chris Dearmanec6edca2012-10-17 19:54:01 -0700147}