blob: 2472fb2dfb3b4e70c9f12c7caf1ab4d9e1328eed [file] [log] [blame]
subrata_modak7382a062009-01-16 10:16:53 +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_modak7382a062009-01-16 10:16:53 +000019/* */
20/******************************************************************************/
21/******************************************************************************/
22/* */
23/* File: dup3_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=336dd1f70ff62d7dd8655228caed4c5bfc818c56 */
28/* says: */
29/* This patch adds the new dup3 syscall. It extends the old dup2 syscall by */
30/* one parameter which is meant to hold a flag value. Support for the */
31/* O_CLOEXEC flag is added in this patch. The following test must be adjusted */
32/* for architectures other than x86 and x86-64 and in case the */
33/* syscall numbers changed. */
34/* */
35/* Usage: <for command-line> */
36/* dup3_01 [-c n] [-e][-i n] [-I x] [-p x] [-t] */
37/* where, -c n : Run n copies concurrently. */
38/* -e : Turn on errno logging. */
39/* -i n : Execute test n times. */
40/* -I x : Execute test for x seconds. */
41/* -P x : Pause for x seconds between iterations. */
42/* -t : Turn on syscall timing. */
43/* */
44/* Total Tests: 1 */
45/* */
46/* Test Name: dup3_01 */
47/* */
48/* Author: Ulrich Drepper <drepper@redhat.com> */
49/* */
50/* History: Created - Jan 13 2009 - Ulrich Drepper <drepper@redhat.com> */
51/* Ported to LTP */
52/* - Jan 13 2009 - Subrata <subrata@linux.vnet.ibm.com> */
53/******************************************************************************/
54#include <fcntl.h>
55#include <stdio.h>
56#include <time.h>
57#include <unistd.h>
58#include <sys/syscall.h>
subrata_modakc488f902009-02-23 07:17:33 +000059#include <errno.h>
subrata_modak7382a062009-01-16 10:16:53 +000060
subrata_modak7382a062009-01-16 10:16:53 +000061#include "test.h"
Cyril Hrubis98f87472014-01-08 13:32:41 +010062#include "lapi/fcntl.h"
subrata_modak115006c2009-02-04 06:16:40 +000063#include "linux_syscall_numbers.h"
subrata_modak7382a062009-01-16 10:16:53 +000064
Cyril Hrubisfdce7d52013-04-04 18:35:48 +020065char *TCID = "dup3_01";
66int TST_TOTAL = 1;
subrata_modak7382a062009-01-16 10:16:53 +000067
68/* Extern Global Functions */
69/******************************************************************************/
70/* */
71/* Function: cleanup */
72/* */
73/* Description: Performs all one time clean up for this test on successful */
74/* completion, premature exit or failure. Closes all temporary */
75/* files, removes all temporary directories exits the test with */
76/* appropriate return code by calling tst_exit() function. */
77/* */
78/* Input: None. */
79/* */
80/* Output: None. */
81/* */
82/* Return: On failure - Exits calling tst_exit(). Non '0' return code. */
83/* On success - Exits calling tst_exit(). With '0' return code. */
84/* */
85/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -040086void cleanup(void)
subrata_modak56207ce2009-03-23 13:35:39 +000087{
subrata_modak56207ce2009-03-23 13:35:39 +000088 tst_rmdir();
subrata_modak7382a062009-01-16 10:16:53 +000089}
90
91/* Local Functions */
92/******************************************************************************/
93/* */
94/* Function: setup */
95/* */
96/* Description: Performs all one time setup for this test. This function is */
97/* typically used to capture signals, create temporary dirs */
98/* and temporary files that may be used in the course of this */
99/* test. */
100/* */
101/* Input: None. */
102/* */
103/* Output: None. */
104/* */
105/* Return: On failure - Exits by calling cleanup(). */
106/* On success - returns 0. */
107/* */
108/******************************************************************************/
Mike Frysingerc57fba52014-04-09 18:56:30 -0400109void setup(void)
subrata_modak56207ce2009-03-23 13:35:39 +0000110{
111 /* Capture signals if any */
112 /* Create temporary directories */
113 TEST_PAUSE;
114 tst_tmpdir();
subrata_modak7382a062009-01-16 10:16:53 +0000115}
116
subrata_modak56207ce2009-03-23 13:35:39 +0000117int main(int argc, char *argv[])
118{
119 int fd, coe;
subrata_modak7382a062009-01-16 10:16:53 +0000120
Garrett Cooperabf8e532010-12-17 02:57:37 -0800121 if ((tst_kvercmp(2, 6, 27)) < 0)
Garrett Coopera4f4cec2011-02-13 18:25:49 -0800122 tst_brkm(TCONF, NULL,
subrata_modak56207ce2009-03-23 13:35:39 +0000123 "This test can only run on kernels that are 2.6.27 and higher");
subrata_modak56207ce2009-03-23 13:35:39 +0000124 setup();
subrata_modak7382a062009-01-16 10:16:53 +0000125
Jan Stancek359980f2013-02-15 10:16:05 +0100126 fd = ltp_syscall(__NR_dup3, 1, 4, 0);
Garrett Cooper53740502010-12-16 00:04:01 -0800127 if (fd == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800128 tst_brkm(TFAIL | TERRNO, cleanup, "dup3(0) failed");
subrata_modak56207ce2009-03-23 13:35:39 +0000129 }
Garrett Cooper53740502010-12-16 00:04:01 -0800130 coe = fcntl(fd, F_GETFD);
131 if (coe == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800132 tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed");
Garrett Cooper53740502010-12-16 00:04:01 -0800133 }
134 if (coe & FD_CLOEXEC) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800135 tst_brkm(TFAIL, cleanup, "dup3(0) set close-on-exec flag");
Garrett Cooper53740502010-12-16 00:04:01 -0800136 }
137 close(fd);
138
Jan Stancek359980f2013-02-15 10:16:05 +0100139 fd = ltp_syscall(__NR_dup3, 1, 4, O_CLOEXEC);
Garrett Cooper53740502010-12-16 00:04:01 -0800140 if (fd == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800141 tst_brkm(TFAIL | TERRNO, cleanup, "dup3(O_CLOEXEC) failed");
Garrett Cooper53740502010-12-16 00:04:01 -0800142 }
143 coe = fcntl(fd, F_GETFD);
144 if (coe == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800145 tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed");
Garrett Cooper53740502010-12-16 00:04:01 -0800146 }
147 if ((coe & FD_CLOEXEC) == 0) {
148 tst_brkm(TFAIL, cleanup,
149 "dup3(O_CLOEXEC) set close-on-exec flag");
150 }
151 close(fd);
152 tst_resm(TPASS, "dup3(O_CLOEXEC) PASSED");
153
154 cleanup();
subrata_modak56207ce2009-03-23 13:35:39 +0000155 tst_exit();
Garrett Coopera4f4cec2011-02-13 18:25:49 -0800156}