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