1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) International Business Machines Corp., 2001
4 * 07/2001 Ported by Wayne Boyer
5 * Copyright (c) 2023 SUSE LLC Avinesh Kumar <avinesh.kumar@suse.com>
6 */
7
8 /*\
9 * [Description]
10 *
11 * Verify that, when a parent process opens a pipe, a child process can
12 * read from it.
13 */
14
15 #include <stdio.h>
16 #include "tst_test.h"
17
18 static int fds[2];
19
run(void)20 static void run(void)
21 {
22 int wr_cnt, rd_cnt;
23 char wrbuf[] = "abcdefghijklmnopqrstuvwxyz";
24 char rdbuf[BUFSIZ];
25
26 SAFE_PIPE(fds);
27 wr_cnt = SAFE_WRITE(SAFE_WRITE_ALL, fds[1], wrbuf, sizeof(wrbuf));
28
29 if (!SAFE_FORK()) {
30 rd_cnt = SAFE_READ(1, fds[0], rdbuf, wr_cnt);
31 TST_EXP_EQ_LU(wr_cnt, rd_cnt);
32 }
33
34 tst_reap_children();
35 SAFE_CLOSE(fds[0]);
36 SAFE_CLOSE(fds[1]);
37 }
38
cleanup(void)39 static void cleanup(void)
40 {
41 if (fds[0] > 0)
42 SAFE_CLOSE(fds[0]);
43
44 if (fds[1] > 0)
45 SAFE_CLOSE(fds[1]);
46 }
47
48 static struct tst_test test = {
49 .test_all = run,
50 .forks_child = 1,
51 .cleanup = cleanup
52 };
53