blob: 5601c7362a1943f5ef232a12ae58a1c46e73a980 [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
70/* Harness Specific Include Files. */
71#include "test.h"
72#include "usctest.h"
subrata_modak115006c2009-02-04 06:16:40 +000073#include "linux_syscall_numbers.h"
subrata_modak768a0812009-01-16 10:18:58 +000074
75#ifndef O_CLOEXEC
Wanlong Gao354ebb42012-12-07 10:10:04 +080076#define O_CLOEXEC 02000000
subrata_modak768a0812009-01-16 10:18:58 +000077#endif
78
subrata_modak768a0812009-01-16 10:18:58 +000079/* Extern Global Variables */
subrata_modak768a0812009-01-16 10:18:58 +000080
81/* Global Variables */
subrata_modak56207ce2009-03-23 13:35:39 +000082char *TCID = "pipe2_01"; /* test program identifier. */
83int testno;
84int TST_TOTAL = 1; /* total number of tests in this file. */
subrata_modak768a0812009-01-16 10:18:58 +000085
86/* Extern Global Functions */
87/******************************************************************************/
88/* */
89/* Function: cleanup */
90/* */
91/* Description: Performs all one time clean up for this test on successful */
92/* completion, premature exit or failure. Closes all temporary */
93/* files, removes all temporary directories exits the test with */
94/* appropriate return code by calling tst_exit() function. */
95/* */
96/* Input: None. */
97/* */
98/* Output: None. */
99/* */
100/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
101/* On success - Exits calling tst_exit(). With '0' return code. */
102/* */
103/******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000104extern void cleanup()
105{
Garrett Cooper2c282152010-12-16 00:55:50 -0800106
subrata_modak56207ce2009-03-23 13:35:39 +0000107 TEST_CLEANUP;
108 tst_rmdir();
subrata_modak768a0812009-01-16 10:18:58 +0000109
subrata_modak768a0812009-01-16 10:18:58 +0000110}
111
112/* Local Functions */
113/******************************************************************************/
114/* */
115/* Function: setup */
116/* */
117/* Description: Performs all one time setup for this test. This function is */
118/* typically used to capture signals, create temporary dirs */
119/* and temporary files that may be used in the course of this */
120/* test. */
121/* */
122/* Input: None. */
123/* */
124/* Output: None. */
125/* */
126/* Return: On failure - Exits by calling cleanup(). */
127/* On success - returns 0. */
128/* */
129/******************************************************************************/
subrata_modak56207ce2009-03-23 13:35:39 +0000130void setup()
131{
132 /* Capture signals if any */
133 /* Create temporary directories */
134 TEST_PAUSE;
135 tst_tmpdir();
subrata_modak768a0812009-01-16 10:18:58 +0000136}
137
subrata_modak56207ce2009-03-23 13:35:39 +0000138int main(int argc, char *argv[])
139{
140 int fd[2], i, coe;
Cyril Hrubis89af32a2012-10-24 16:39:11 +0200141 int lc;
142 char *msg;
subrata_modak768a0812009-01-16 10:18:58 +0000143
subrata_modak56207ce2009-03-23 13:35:39 +0000144 /* Parse standard options given to run the test. */
Garrett Cooper45e285d2010-11-22 12:19:25 -0800145 msg = parse_opts(argc, argv, NULL, NULL);
146 if (msg != NULL) {
subrata_modak56207ce2009-03-23 13:35:39 +0000147 tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg);
148 tst_exit();
149 }
150 if ((tst_kvercmp(2, 6, 27)) < 0) {
151 tst_resm(TCONF,
152 "This test can only run on kernels that are 2.6.27 and higher");
153 tst_exit();
154 }
155 setup();
subrata_modak768a0812009-01-16 10:18:58 +0000156
subrata_modak56207ce2009-03-23 13:35:39 +0000157 for (lc = 0; TEST_LOOPING(lc); ++lc) {
158 Tst_count = 0;
159 for (testno = 0; testno < TST_TOTAL; ++testno) {
Jan Stancek359980f2013-02-15 10:16:05 +0100160 if (ltp_syscall(__NR_pipe2, fd, 0) != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000161 tst_resm(TFAIL, "pipe2(0) failed");
162 cleanup();
163 tst_exit();
164 }
165 for (i = 0; i < 2; ++i) {
166 coe = fcntl(fd[i], F_GETFD);
167 if (coe == -1) {
168 tst_brkm(TBROK, cleanup,
169 "fcntl failed");
170 tst_exit();
171 }
172 if (coe & FD_CLOEXEC) {
173 tst_resm(TFAIL,
174 "pipe2(0) set close-on-exit for fd[%d]",
175 i);
176 cleanup();
177 tst_exit();
178 }
179 }
180 close(fd[0]);
181 close(fd[1]);
subrata_modak768a0812009-01-16 10:18:58 +0000182
Jan Stancek359980f2013-02-15 10:16:05 +0100183 if (ltp_syscall(__NR_pipe2, fd, O_CLOEXEC) != 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000184 tst_resm(TFAIL, "pipe2(O_CLOEXEC) failed");
185 cleanup();
186 tst_exit();
187 }
188 for (i = 0; i < 2; ++i) {
189 coe = fcntl(fd[i], F_GETFD);
190 if (coe == -1) {
191 tst_brkm(TBROK, cleanup,
192 "fcntl failed");
193 tst_exit();
194 }
195 if ((coe & FD_CLOEXEC) == 0) {
196 tst_resm(TFAIL,
197 "pipe2(O_CLOEXEC) does not set close-on-exit for fd[%d]",
198 i);
199 cleanup();
200 tst_exit();
201 }
202 }
203 close(fd[0]);
204 close(fd[1]);
205 tst_resm(TPASS, "pipe2(O_CLOEXEC) PASSED");
206 cleanup();
207 }
208 }
209 tst_exit();
Chris Dearmanec6edca2012-10-17 19:54:01 -0700210}