1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) Ulrich Drepper <drepper@redhat.com>
4 * Copyright (c) International Business Machines Corp., 2009
5 * Created - Jan 13 2009 - Ulrich Drepper <drepper@redhat.com>
6 * Ported to LTP - Jan 13 2009 - Subrata <subrata@linux.vnet.ibm.com>
7 */
8
9 /*\
10 * [Description]
11 *
12 * Testcase to check whether dup3() supports O_CLOEXEC flag.
13 */
14
15 #define _GNU_SOURCE
16
17 #include <stdio.h>
18 #include <errno.h>
19 #include <unistd.h>
20 #include "tst_test.h"
21 #include "tst_safe_macros.h"
22
23 static int fd = -1;
24
25 static struct tcase {
26 int coe_flag;
27 char *desc;
28 } tcases[] = {
29 {0, "dup3(1, 4, 0)"},
30 {O_CLOEXEC, "dup3(1, 4, O_CLOEXEC)"},
31 };
32
cleanup(void)33 static void cleanup(void)
34 {
35 if (fd > -1)
36 close(fd);
37 }
38
run(unsigned int i)39 static void run(unsigned int i)
40 {
41 int ret;
42 struct tcase *tc = tcases + i;
43 TST_EXP_FD_SILENT(dup3(1, 4, tc->coe_flag), "dup3(1, 4, %d)", tc->coe_flag);
44
45 fd = TST_RET;
46 ret = SAFE_FCNTL(fd, F_GETFD);
47 if (tc->coe_flag) {
48 if (ret & FD_CLOEXEC)
49 tst_res(TPASS, "%s set close-on-exec flag", tc->desc);
50 else
51 tst_res(TFAIL, "%s set close-on-exec flag", tc->desc);
52 } else {
53 if (ret & FD_CLOEXEC)
54 tst_res(TFAIL, "%s set close-on-exec flag", tc->desc);
55 else
56 tst_res(TPASS, "%s set close-on-exec flag", tc->desc);
57 }
58 };
59
60 static struct tst_test test = {
61 .tcnt = ARRAY_SIZE(tcases),
62 .test = run,
63 .cleanup = cleanup,
64 };
65