1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *
4  *   Copyright (c) International Business Machines Corp., 2001
5  */
6 
7 /*
8  * DESCRIPTION
9  *	Testcase to check that write() sets errno to EAGAIN
10  *
11  * ALGORITHM
12  *	Create a named pipe (fifo), open it in O_NONBLOCK mode, and
13  *	attempt to write to it when it is full, write(2) should fail
14  */
15 
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <signal.h>
19 #include <setjmp.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <stdio.h>
23 #include "tst_test.h"
24 
25 static char fifo[100];
26 static int rfd, wfd;
27 static long page_size;
28 
verify_write(void)29 static void verify_write(void)
30 {
31 	char wbuf[8 * page_size];
32 
33 	TEST(write(wfd, wbuf, sizeof(wbuf)));
34 
35 	if (TST_RET != -1) {
36 		tst_res(TFAIL, "write() succeeded unexpectedly");
37 		return;
38 	}
39 
40 	if (TST_ERR != EAGAIN) {
41 		tst_res(TFAIL | TTERRNO,
42 			"write() failed unexpectedly, expected EAGAIN");
43 		return;
44 	}
45 
46 	tst_res(TPASS | TTERRNO, "write() failed expectedly");
47 }
48 
setup(void)49 static void setup(void)
50 {
51 	page_size = getpagesize();
52 
53 	char wbuf[17 * page_size];
54 
55 	sprintf(fifo, "%s.%d", fifo, getpid());
56 
57 	SAFE_MKNOD(fifo, S_IFIFO | 0777, 0);
58 
59 	rfd = SAFE_OPEN(fifo, O_RDONLY | O_NONBLOCK);
60 	wfd = SAFE_OPEN(fifo, O_WRONLY | O_NONBLOCK);
61 
62 	SAFE_WRITE(0, wfd, wbuf, sizeof(wbuf));
63 }
64 
cleanup(void)65 static void cleanup(void)
66 {
67 	if (rfd > 0)
68 		SAFE_CLOSE(rfd);
69 
70 	if (wfd > 0)
71 		SAFE_CLOSE(wfd);
72 }
73 
74 static struct tst_test test = {
75 	.needs_tmpdir = 1,
76 	.setup = setup,
77 	.cleanup = cleanup,
78 	.test_all = verify_write,
79 };
80