blob: c501ff8d0b16e6e14861d24b3ddc000ca2d20809 [file] [log] [blame]
subrata_modak768a0812009-01-16 10:18:58 +00001/******************************************************************************/
2/* */
3/* Copyright (c) Ulrich Drepper <drepper@redhat.com> */
4/* Copyright (c) International Business Machines Corp., 2009 */
5/* */
6/* This program is free software; you can redistribute it and/or modify */
7/* it under the terms of the GNU General Public License as published by */
8/* the Free Software Foundation; either version 2 of the License, or */
9/* (at your option) any later version. */
10/* */
11/* This program is distributed in the hope that it will be useful, */
12/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
13/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See */
14/* the GNU General Public License for more details. */
15/* */
16/* You should have received a copy of the GNU General Public License */
17/* along with this program; if not, write to the Free Software */
Wanlong Gao4548c6c2012-10-19 18:03:36 +080018/* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
subrata_modak768a0812009-01-16 10:18:58 +000019/* */
20/******************************************************************************/
21/******************************************************************************/
22/* */
23/* File: pipe2_01.c */
24/* */
25/* Description: This Program tests the new system call introduced in 2.6.27. */
26/* Ulrich´s comment as in: */
27/* http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=ed8cae8ba01348bfd83333f4648dd807b04d7f08 */
28/* says: */
29/* This patch introduces the new syscall pipe2 which is like pipe but it also */
30/* takes an additional parameter which takes a flag value. This patch */
31/* implements the handling of O_CLOEXEC for the flag. I did not add support */
32/* for the new syscall for the architectures which have a special sys_pipe */
33/* implementation. I think the maintainers of those archs have the chance to */
34/* go with the unified implementation but that's up to them. */
35/* */
36/* The implementation introduces do_pipe_flags. I did that instead of */
37/* changing all callers of do_pipe because some of the callers are written in */
38/* assembler. I would probably screw up changing the assembly code. To avoid */
39/* breaking code do_pipe is now a small wrapper around do_pipe_flags. Once */
40/* all callers are changed over to do_pipe_flags the old do_pipe function can */
41/* be removed. */
42/* The following test must be adjusted for architectures other than x86 and */
43/* x86-64 and in case the syscall numbers changed. */
44/* */
45/* Usage: <for command-line> */
46/* pipe2_01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */
47/* where, -c n : Run n copies concurrently. */
48/* -e : Turn on errno logging. */
49/* -i n : Execute test n times. */
50/* -I x : Execute test for x seconds. */
51/* -P x : Pause for x seconds between iterations. */
52/* -t : Turn on syscall timing. */
53/* */
54/* Total Tests: 1 */
55/* */
56/* Test Name: pipe2_01 */
57/* */
58/* Author: Ulrich Drepper <drepper@redhat.com> */
59/* */
60/* History: Created - Jan 13 2009 - Ulrich Drepper <drepper@redhat.com> */
61/* Ported to LTP */
62/* - Jan 13 2009 - Subrata <subrata@linux.vnet.ibm.com> */
63/******************************************************************************/
64#include <fcntl.h>
65#include <stdio.h>
66#include <unistd.h>
67#include <sys/syscall.h>
subrata_modakc488f902009-02-23 07:17:33 +000068#include <errno.h>
subrata_modak768a0812009-01-16 10:18:58 +000069
subrata_modak768a0812009-01-16 10:18:58 +000070#include "test.h"
Cyril Hrubis98f87472014-01-08 13:32:41 +010071#include "lapi/fcntl.h"
subrata_modak115006c2009-02-04 06:16:40 +000072#include "linux_syscall_numbers.h"
subrata_modak768a0812009-01-16 10:18:58 +000073
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020074char *TCID = "pipe2_01";
subrata_modak56207ce2009-03-23 13:35:39 +000075int testno;
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020076int TST_TOTAL = 1;
subrata_modak768a0812009-01-16 10:18:58 +000077
78/* Extern Global Functions */
79/******************************************************************************/
80/* */
81/* Function: cleanup */
82/* */
83/* Description: Performs all one time clean up for this test on successful */
84/* completion, premature exit or failure. Closes all temporary */
85/* files, removes all temporary directories exits the test with */
86/* appropriate return code by calling tst_exit() function. */
87/* */
88/* Input: None. */
89/* */
90/* Output: None. */
91/* */
92/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
93/* On success - Exits calling tst_exit(). With '0' return code. */
94/* */
95/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -040096void cleanup(void)
subrata_modak56207ce2009-03-23 13:35:39 +000097{
Garrett Cooper2c282152010-12-16 00:55:50 -080098
subrata_modak56207ce2009-03-23 13:35:39 +000099 tst_rmdir();
subrata_modak768a0812009-01-16 10:18:58 +0000100
subrata_modak768a0812009-01-16 10:18:58 +0000101}
102
103/* Local Functions */
104/******************************************************************************/
105/* */
106/* Function: setup */
107/* */
108/* Description: Performs all one time setup for this test. This function is */
109/* typically used to capture signals, create temporary dirs */
110/* and temporary files that may be used in the course of this */
111/* test. */
112/* */
113/* Input: None. */
114/* */
115/* Output: None. */
116/* */
117/* Return: On failure - Exits by calling cleanup(). */
118/* On success - returns 0. */
119/* */
120/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400121void setup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000122{
123 /* Capture signals if any */
124 /* Create temporary directories */
125 TEST_PAUSE;
126 tst_tmpdir();
subrata_modak768a0812009-01-16 10:18:58 +0000127}
128
subrata_modak56207ce2009-03-23 13:35:39 +0000129int main(int argc, char *argv[])
130{
131 int fd[2], i, coe;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200132 int lc;
Cyril Hrubis0b9589f2014-05-27 17:40:33 +0200133 const char *msg;
subrata_modak768a0812009-01-16 10:18:58 +0000134
Garrett Cooper45e285d2010-11-22 12:19:25 -0800135 msg = parse_opts(argc, argv, NULL, NULL);
136 if (msg != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000137 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
subrata_modak56207ce2009-03-23 13:35:39 +0000138 }
139 if ((tst_kvercmp(2, 6, 27)) < 0) {
Cyril Hrubis526fdf82014-12-04 14:35:01 +0100140 tst_brkm(TCONF,
141 NULL,
subrata_modak56207ce2009-03-23 13:35:39 +0000142 "This test can only run on kernels that are 2.6.27 and higher");
subrata_modak56207ce2009-03-23 13:35:39 +0000143 }
144 setup();
subrata_modak768a0812009-01-16 10:18:58 +0000145
subrata_modak56207ce2009-03-23 13:35:39 +0000146 for (lc = 0; TEST_LOOPING(lc); ++lc) {
Caspar Zhangd59a6592013-03-07 14:59:12 +0800147 tst_count = 0;
subrata_modak56207ce2009-03-23 13:35:39 +0000148 for (testno = 0; testno < TST_TOTAL; ++testno) {
Jan Stancek359980f2013-02-15 10:16:05 +0100149 if (ltp_syscall(__NR_pipe2, fd, 0) != 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100150 tst_brkm(TFAIL, cleanup, "pipe2(0) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000151 }
152 for (i = 0; i < 2; ++i) {
153 coe = fcntl(fd[i], F_GETFD);
154 if (coe == -1) {
155 tst_brkm(TBROK, cleanup,
156 "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000157 }
158 if (coe & FD_CLOEXEC) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100159 tst_brkm(TFAIL,
160 cleanup, "pipe2(0) set close-on-exit for fd[%d]",
subrata_modak56207ce2009-03-23 13:35:39 +0000161 i);
subrata_modak56207ce2009-03-23 13:35:39 +0000162 }
163 }
164 close(fd[0]);
165 close(fd[1]);
subrata_modak768a0812009-01-16 10:18:58 +0000166
Jan Stancek359980f2013-02-15 10:16:05 +0100167 if (ltp_syscall(__NR_pipe2, fd, O_CLOEXEC) != 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100168 tst_brkm(TFAIL, cleanup,
169 "pipe2(O_CLOEXEC) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000170 }
171 for (i = 0; i < 2; ++i) {
172 coe = fcntl(fd[i], F_GETFD);
173 if (coe == -1) {
174 tst_brkm(TBROK, cleanup,
175 "fcntl failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000176 }
177 if ((coe & FD_CLOEXEC) == 0) {
Cyril Hrubis9fa8ad02014-12-16 13:20:49 +0100178 tst_brkm(TFAIL,
179 cleanup, "pipe2(O_CLOEXEC) does not set close-on-exit for fd[%d]",
subrata_modak56207ce2009-03-23 13:35:39 +0000180 i);
subrata_modak56207ce2009-03-23 13:35:39 +0000181 }
182 }
183 close(fd[0]);
184 close(fd[1]);
185 tst_resm(TPASS, "pipe2(O_CLOEXEC) PASSED");
186 cleanup();
187 }
188 }
189 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700190}