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