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 * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
6f08c3bdfSopenharmony_ci *
7f08c3bdfSopenharmony_ci * Author: Ulrich Drepper <drepper@redhat.com>
8f08c3bdfSopenharmony_ci *
9f08c3bdfSopenharmony_ci * History:
10f08c3bdfSopenharmony_ci *  Created - Jan 13 2009 - Ulrich Drepper <drepper@redhat.com>
11f08c3bdfSopenharmony_ci *  Ported to LTP - Jan 13 2009 - Subrata <subrata@linux.vnet.ibm.com>
12f08c3bdfSopenharmony_ci *  Converted into new api - Apri 15 2020 - Yang Xu <xuyang2018.jy@cn.fujitsu.com>
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#define _GNU_SOURCE
16f08c3bdfSopenharmony_ci#include <stdio.h>
17f08c3bdfSopenharmony_ci#include <unistd.h>
18f08c3bdfSopenharmony_ci#include "lapi/fcntl.h"
19f08c3bdfSopenharmony_ci#include "tst_test.h"
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_cistatic int fds[2];
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_cistatic struct tcase {
24f08c3bdfSopenharmony_ci	int flags;
25f08c3bdfSopenharmony_ci	int cmd;
26f08c3bdfSopenharmony_ci	int check_read_end;
27f08c3bdfSopenharmony_ci	char *message;
28f08c3bdfSopenharmony_ci} tcases[] = {
29f08c3bdfSopenharmony_ci	{0, F_GETFD, 1, "Test pipe2 with 0 flag"},
30f08c3bdfSopenharmony_ci	{O_CLOEXEC, F_GETFD, 1, "Test pipe2 using O_CLOEXEC flag"},
31f08c3bdfSopenharmony_ci	/*
32f08c3bdfSopenharmony_ci	 * It may get EINVAL error on older kernel because this flag was
33f08c3bdfSopenharmony_ci	 * introduced since kernel 3.4. We only test flag in write end
34f08c3bdfSopenharmony_ci	 * because this flag was used to make pipe buffer marked with the
35f08c3bdfSopenharmony_ci	 * PIPE_BUF_FLAG_PACKET flag. In read end, kernel also checks buffer
36f08c3bdfSopenharmony_ci	 * flag instead of O_DIRECT. So it make no sense to check this flag
37f08c3bdfSopenharmony_ci	 * in fds[0].
38f08c3bdfSopenharmony_ci	 */
39f08c3bdfSopenharmony_ci	{O_DIRECT, F_GETFL, 0, "Test pipe2 using O_DIRECT flag"},
40f08c3bdfSopenharmony_ci	{O_NONBLOCK, F_GETFL, 1, "Test pipe2 using O_NONBLOCK flag"},
41f08c3bdfSopenharmony_ci};
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_cistatic void cleanup(void)
44f08c3bdfSopenharmony_ci{
45f08c3bdfSopenharmony_ci	if (fds[0] > 0)
46f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[0]);
47f08c3bdfSopenharmony_ci	if (fds[1] > 1)
48f08c3bdfSopenharmony_ci		SAFE_CLOSE(fds[1]);
49f08c3bdfSopenharmony_ci}
50f08c3bdfSopenharmony_ci
51f08c3bdfSopenharmony_cistatic void verify_pipe2(unsigned int n)
52f08c3bdfSopenharmony_ci{
53f08c3bdfSopenharmony_ci	struct tcase *tc = &tcases[n];
54f08c3bdfSopenharmony_ci	int get_flag = 0, i = 0;
55f08c3bdfSopenharmony_ci
56f08c3bdfSopenharmony_ci	tst_res(TINFO, "%s ", tc->message);
57f08c3bdfSopenharmony_ci
58f08c3bdfSopenharmony_ci	SAFE_PIPE2(fds, tc->flags);
59f08c3bdfSopenharmony_ci	for (i = 0; i < 2; i++) {
60f08c3bdfSopenharmony_ci		if (i == 0 && !tc->check_read_end)
61f08c3bdfSopenharmony_ci			continue;
62f08c3bdfSopenharmony_ci		get_flag = SAFE_FCNTL(fds[i], tc->cmd);
63f08c3bdfSopenharmony_ci		if ((get_flag && tc->flags) || (tc->flags == get_flag))
64f08c3bdfSopenharmony_ci			tst_res(TPASS, "pipe2 fds[%d] gets expected flag(%d)", i, tc->flags);
65f08c3bdfSopenharmony_ci		else
66f08c3bdfSopenharmony_ci			tst_res(TFAIL, "pipe2 fds[%d] doesn't get expected flag(%d), get flag(%d)",
67f08c3bdfSopenharmony_ci					i, tc->flags, get_flag);
68f08c3bdfSopenharmony_ci	}
69f08c3bdfSopenharmony_ci	cleanup();
70f08c3bdfSopenharmony_ci}
71f08c3bdfSopenharmony_ci
72f08c3bdfSopenharmony_cistatic struct tst_test test = {
73f08c3bdfSopenharmony_ci	.tcnt = ARRAY_SIZE(tcases),
74f08c3bdfSopenharmony_ci	.test = verify_pipe2,
75f08c3bdfSopenharmony_ci	.cleanup = cleanup,
76f08c3bdfSopenharmony_ci};
77