• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * Test Description:
7  * A pipe has a limited capacity. If the pipe with non block mode is full,
8  * then a write(2) will fail and get EAGAIN error. Otherwise, from 1 to
9  * PIPE_BUF bytes may be written.
10  */
11 #define _GNU_SOURCE
12 #include <unistd.h>
13 #include <stdlib.h>
14 #include <fcntl.h>
15 #include "tst_test.h"
16 #include "lapi/fcntl.h"
17 
18 static int fds[2];
19 static char *wrbuf;
20 static char *rdbuf;
21 static ssize_t max_size, invalid_size;
22 
23 static struct tcase {
24 	int full_flag;
25 	int offset;
26 	char *message;
27 	int check_flag;
28 } tcases[] = {
29 	{1, 0, "Write to full pipe", 1},
30 	/*
31 	 * For a non-empty(unaligned page size) pipe, the sequent large size
32 	 * write(>page_size)will use new pages. So it may exist a hole in
33 	 * page and we print this value instead of checking it.
34 	 */
35 	{0, 1, "Write to non-empty pipe", 0},
36 	{0, 0, "Write to empty pipe", 1},
37 };
38 
verify_pipe(unsigned int n)39 static void verify_pipe(unsigned int n)
40 {
41 	struct tcase *tc = &tcases[n];
42 	int nbytes;
43 
44 	memset(rdbuf, 0, max_size);
45 
46 	tst_res(TINFO, "%s", tc->message);
47 	if (tc->full_flag) {
48 		SAFE_WRITE(1, fds[1], wrbuf, max_size);
49 		TEST(write(fds[1], "x", 1));
50 		if (TST_RET != -1) {
51 			tst_res(TFAIL, "write succeeded unexpectedly");
52 			goto clean_pipe_buf;
53 		}
54 		if (TST_ERR == EAGAIN)
55 			tst_res(TPASS | TTERRNO, "write failed as expected");
56 		else
57 			tst_res(TFAIL | TTERRNO, "write failed, expected EAGAIN but got");
58 	} else {
59 		SAFE_WRITE(1, fds[1], "x", tc->offset);
60 		TEST(write(fds[1], wrbuf, invalid_size));
61 		if (TST_RET == -1) {
62 			tst_res(TFAIL, "write failed unexpectedly");
63 			goto clean_pipe_buf;
64 		}
65 		tst_res(TPASS, "write succeeded as expectedly");
66 	}
67 	SAFE_IOCTL(fds[1], FIONREAD, &nbytes);
68 	if (tc->check_flag) {
69 		if (nbytes == max_size - tc->offset)
70 			tst_res(TPASS, "write %d bytes", nbytes);
71 		else
72 			tst_res(TFAIL, "write expected %ld bytes, got %d bytes",
73 					max_size, nbytes);
74 	} else
75 		tst_res(TPASS, "write %d bytes", nbytes);
76 
77 clean_pipe_buf:
78 	SAFE_READ(0, fds[0], rdbuf, max_size);
79 }
80 
81 
cleanup(void)82 static void cleanup(void)
83 {
84 	if (fds[0] > 0)
85 		SAFE_CLOSE(fds[0]);
86 	if (fds[1] > 0)
87 		SAFE_CLOSE(fds[1]);
88 	if (wrbuf)
89 		free(wrbuf);
90 	if (rdbuf)
91 		free(rdbuf);
92 }
93 
setup(void)94 static void setup(void)
95 {
96 	SAFE_PIPE(fds);
97 
98 	max_size = SAFE_FCNTL(fds[1], F_GETPIPE_SZ);
99 	invalid_size = max_size + 4096;
100 	wrbuf = SAFE_MALLOC(invalid_size);
101 	rdbuf = SAFE_MALLOC(max_size);
102 	memset(wrbuf, 'x', invalid_size);
103 
104 	SAFE_FCNTL(fds[1], F_SETFL, O_NONBLOCK);
105 	SAFE_FCNTL(fds[0], F_SETFL, O_NONBLOCK);
106 }
107 
108 static struct tst_test test = {
109 	.test = verify_pipe,
110 	.setup = setup,
111 	.cleanup = cleanup,
112 	.tcnt = ARRAY_SIZE(tcases),
113 };
114