• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "lapi/syscalls.h"
64 
65 char *TCID = "dup3_01";
66 int TST_TOTAL = 1;
67 
cleanup(void)68 void cleanup(void)
69 {
70 	tst_rmdir();
71 }
72 
setup(void)73 void setup(void)
74 {
75 	TEST_PAUSE;
76 	tst_tmpdir();
77 }
78 
main(int argc,char * argv[])79 int main(int argc, char *argv[])
80 {
81 	int fd, coe;
82 
83 	tst_parse_opts(argc, argv, NULL, NULL);
84 
85 	if ((tst_kvercmp(2, 6, 27)) < 0)
86 		tst_brkm(TCONF, NULL,
87 			 "This test can only run on kernels that are 2.6.27 and higher");
88 	setup();
89 
90 	fd = ltp_syscall(__NR_dup3, 1, 4, 0);
91 	if (fd == -1) {
92 		tst_brkm(TFAIL | TERRNO, cleanup, "dup3(0) failed");
93 	}
94 	coe = fcntl(fd, F_GETFD);
95 	if (coe == -1) {
96 		tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed");
97 	}
98 	if (coe & FD_CLOEXEC) {
99 		tst_brkm(TFAIL, cleanup, "dup3(0) set close-on-exec flag");
100 	}
101 	close(fd);
102 
103 	fd = ltp_syscall(__NR_dup3, 1, 4, O_CLOEXEC);
104 	if (fd == -1) {
105 		tst_brkm(TFAIL | TERRNO, cleanup, "dup3(O_CLOEXEC) failed");
106 	}
107 	coe = fcntl(fd, F_GETFD);
108 	if (coe == -1) {
109 		tst_brkm(TBROK | TERRNO, cleanup, "fcntl failed");
110 	}
111 	if ((coe & FD_CLOEXEC) == 0) {
112 		tst_brkm(TFAIL, cleanup,
113 			 "dup3(O_CLOEXEC) set close-on-exec flag");
114 	}
115 	close(fd);
116 	tst_resm(TPASS, "dup3(O_CLOEXEC) PASSED");
117 
118 	cleanup();
119 	tst_exit();
120 }
121