• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013 Fujitsu Ltd.
3  * Author: Xiaoguang Wang <wangxg.fnst@cn.fujitsu.com>
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of version 2 of the GNU General Public License as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it would be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16  */
17 
18 /*
19  * Description:
20  * Verify that,
21  *  1. dup3() fails with -1 return value and sets errno to EINVAL
22  *     if flags contain an invalid value or oldfd was equal to newfd.
23  */
24 
25 #define _GNU_SOURCE
26 
27 #include <stdio.h>
28 #include <errno.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <string.h>
32 #include <signal.h>
33 #include <sys/types.h>
34 
35 #include "test.h"
36 #include "safe_macros.h"
37 #include "lapi/fcntl.h"
38 #include "lapi/syscalls.h"
39 
40 
41 static void setup(void);
42 static void cleanup(void);
43 
44 #define INVALID_FLAG	-1
45 
46 static int old_fd;
47 static int new_fd;
48 
49 static struct test_case_t {
50 	int *oldfd;
51 	int *newfd;
52 	int flags;
53 	int exp_errno;
54 } test_cases[] = {
55 	{&old_fd, &old_fd, O_CLOEXEC, EINVAL},
56 	{&old_fd, &old_fd, 0, EINVAL},
57 	{&old_fd, &new_fd, INVALID_FLAG, EINVAL}
58 };
59 
60 char *TCID = "dup3_02";
61 int TST_TOTAL = ARRAY_SIZE(test_cases);
62 
main(int ac,char ** av)63 int main(int ac, char **av)
64 {
65 	int lc;
66 	int i;
67 
68 	tst_parse_opts(ac, av, NULL, NULL);
69 
70 	setup();
71 
72 	for (lc = 0; TEST_LOOPING(lc); lc++) {
73 		tst_count = 0;
74 
75 		for (i = 0; i < TST_TOTAL; i++) {
76 			TEST(ltp_syscall(__NR_dup3, *(test_cases[i].oldfd),
77 			     *(test_cases[i].newfd), test_cases[i].flags));
78 
79 			if (TEST_RETURN != -1) {
80 				tst_resm(TFAIL, "dup3 succeeded unexpectedly");
81 				continue;
82 			}
83 
84 			if (TEST_ERRNO == test_cases[i].exp_errno) {
85 				tst_resm(TPASS | TTERRNO,
86 					 "dup3 failed as expected");
87 			} else {
88 				tst_resm(TFAIL | TTERRNO,
89 					 "dup3 failed unexpectedly; expected:"
90 					 "%d - %s", test_cases[i].exp_errno,
91 					 strerror(test_cases[i].exp_errno));
92 			}
93 		}
94 	}
95 
96 	cleanup();
97 	tst_exit();
98 }
99 
setup(void)100 static void setup(void)
101 {
102 	tst_sig(NOFORK, DEF_HANDLER, cleanup);
103 
104 	tst_tmpdir();
105 
106 	TEST_PAUSE;
107 
108 	old_fd = SAFE_CREAT(cleanup, "testeinval.file", 0644);
109 	new_fd = -1;
110 }
111 
cleanup(void)112 static void cleanup(void)
113 {
114 	if (old_fd > 0)
115 		SAFE_CLOSE(NULL, old_fd);
116 
117 	tst_rmdir();
118 }
119