1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
4 * Author: Yang Xu <xuyang2018.jy@cn.fujitsu.com>
5 *
6 * This case is designed to test the basic functionality about the
7 * O_CLOEXEC flag of pipe2.
8 */
9
10 #define _GNU_SOURCE
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include "lapi/fcntl.h"
15 #include "tst_test.h"
16
17 #define TESTBIN "pipe2_02_child"
18 static int fds[2];
19
cleanup(void)20 static void cleanup(void)
21 {
22 if (fds[0] > 0)
23 SAFE_CLOSE(fds[0]);
24 if (fds[1] > 0)
25 SAFE_CLOSE(fds[1]);
26 }
27
verify_pipe2(void)28 static void verify_pipe2(void)
29 {
30 int pid, status;
31 char buf[20];
32
33 SAFE_PIPE2(fds, O_CLOEXEC);
34 sprintf(buf, "%d", fds[1]);
35 pid = SAFE_FORK();
36 if (pid == 0)
37 SAFE_EXECLP(TESTBIN, TESTBIN, buf, NULL);
38
39 SAFE_WAIT(&status);
40 if (WIFEXITED(status)) {
41 switch (WEXITSTATUS(status)) {
42 case 0:
43 tst_res(TPASS, "test O_CLOEXEC for pipe2 success");
44 break;
45 case 1:
46 tst_res(TFAIL, "test O_CLOEXEC for pipe2 failed");
47 break;
48 default:
49 tst_brk(TBROK, "execlp() failed");
50 }
51 } else {
52 tst_brk(TBROK, "%s exits with unexpected error", TESTBIN);
53 }
54 cleanup();
55 }
56
57 static struct tst_test test = {
58 .resource_files = (const char *const []) {
59 TESTBIN,
60 NULL
61 },
62 .cleanup = cleanup,
63 .forks_child = 1,
64 .needs_root = 1,
65 .test_all = verify_pipe2,
66 };
67