1 /******************************************************************************/
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               */
18 /* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA    */
19 /*                                                                            */
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>
59 #include <errno.h>
60 
61 #include "test.h"
62 #include "lapi/fcntl.h"
63 #include "linux_syscall_numbers.h"
64 
65 char *TCID = "dup3_01";
66 int TST_TOTAL = 1;
67 
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 /******************************************************************************/
cleanup(void)86 void cleanup(void)
87 {
88 	tst_rmdir();
89 }
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 /******************************************************************************/
setup(void)109 void setup(void)
110 {
111 	/* Capture signals if any */
112 	/* Create temporary directories */
113 	TEST_PAUSE;
114 	tst_tmpdir();
115 }
116 
main(int argc,char * argv[])117 int main(int argc, char *argv[])
118 {
119 	int fd, coe;
120 
121 	if ((tst_kvercmp(2, 6, 27)) < 0)
122 		tst_brkm(TCONF, NULL,
123 			 "This test can only run on kernels that are 2.6.27 and higher");
124 	setup();
125 
126 	fd = ltp_syscall(__NR_dup3, 1, 4, 0);
127 	if (fd == -1) {
128 		tst_brkm(TFAIL | TERRNO, cleanup, "dup3(0) failed");
129 	}
130 	coe = fcntl(fd, F_GETFD);
131 	if (coe == -1) {
132 		tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed");
133 	}
134 	if (coe & FD_CLOEXEC) {
135 		tst_brkm(TFAIL, cleanup, "dup3(0) set close-on-exec flag");
136 	}
137 	close(fd);
138 
139 	fd = ltp_syscall(__NR_dup3, 1, 4, O_CLOEXEC);
140 	if (fd == -1) {
141 		tst_brkm(TFAIL | TERRNO, cleanup, "dup3(O_CLOEXEC) failed");
142 	}
143 	coe = fcntl(fd, F_GETFD);
144 	if (coe == -1) {
145 		tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed");
146 	}
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();
155 	tst_exit();
156 }
157