1/*
2 * Copyright (c) 2014 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 *   Basic test for fcntl(2) using F_DUPFD_CLOEXEC argument.
22 */
23
24#include <stdio.h>
25#include <errno.h>
26#include <unistd.h>
27#include <string.h>
28#include <signal.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <pwd.h>
32
33#include "test.h"
34#include "safe_macros.h"
35#include "lapi/fcntl.h"
36
37char *TCID = "fcntl29";
38int TST_TOTAL = 1;
39
40static void setup(void);
41static void cleanup(void);
42
43static int test_fd;
44
45int main(int ac, char **av)
46{
47	int lc, dup_fd;
48
49	tst_parse_opts(ac, av, NULL, NULL);
50
51	setup();
52
53	for (lc = 0; TEST_LOOPING(lc); lc++) {
54		tst_count = 0;
55
56		TEST(fcntl(test_fd, F_DUPFD_CLOEXEC, 0));
57		if (TEST_RETURN < 0) {
58			tst_brkm(TFAIL | TTERRNO, cleanup, "fcntl "
59				 "test F_DUPFD_CLOEXEC failed");
60		}
61		dup_fd = TEST_RETURN;
62
63		TEST(fcntl(dup_fd, F_GETFD));
64		if (TEST_RETURN < 0) {
65			SAFE_CLOSE(cleanup, dup_fd);
66			tst_brkm(TFAIL | TTERRNO, cleanup, "fcntl "
67				 "test F_GETFD failed");
68		}
69
70		if (TEST_RETURN & FD_CLOEXEC) {
71			tst_resm(TPASS, "fcntl test "
72				 "F_DUPFD_CLOEXEC success");
73		} else {
74			tst_resm(TFAIL, "fcntl test "
75				 "F_DUPFD_CLOEXEC fail");
76		}
77
78		SAFE_CLOSE(cleanup, dup_fd);
79	}
80
81	cleanup();
82	tst_exit();
83}
84
85static void setup(void)
86{
87	tst_sig(NOFORK, DEF_HANDLER, cleanup);
88
89	tst_tmpdir();
90
91	TEST_PAUSE;
92
93	test_fd = SAFE_CREAT(cleanup, "testfile", 0644);
94}
95
96static void cleanup(void)
97{
98	if (test_fd > 0)
99		SAFE_CLOSE(NULL, test_fd);
100
101	tst_rmdir();
102}
103