xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/write/write04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) Linux Test Project, 2002-2022
4  * Copyright (c) International Business Machines Corp., 2001
5  */
6 
7 /*\
8  * [Description]
9  *
10  * Verify that write(2) fails with errno EAGAIN when attempt to write to fifo
11  * opened in O_NONBLOCK mode.
12  */
13 
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <signal.h>
17 #include <setjmp.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <stdio.h>
21 #include "tst_test.h"
22 
23 static char fifo[100];
24 static int rfd, wfd;
25 static long page_size;
26 
verify_write(void)27 static void verify_write(void)
28 {
29 	char wbuf[8 * page_size];
30 
31 	TST_EXP_FAIL2(write(wfd, wbuf, sizeof(wbuf)), EAGAIN);
32 }
33 
setup(void)34 static void setup(void)
35 {
36 	page_size = getpagesize();
37 
38 	char wbuf[17 * page_size];
39 
40 	sprintf(fifo, "%s.%d", fifo, getpid());
41 
42 	SAFE_MKNOD(fifo, S_IFIFO | 0777, 0);
43 
44 	rfd = SAFE_OPEN(fifo, O_RDONLY | O_NONBLOCK);
45 	wfd = SAFE_OPEN(fifo, O_WRONLY | O_NONBLOCK);
46 
47 	SAFE_WRITE(SAFE_WRITE_ANY, wfd, wbuf, sizeof(wbuf));
48 }
49 
cleanup(void)50 static void cleanup(void)
51 {
52 	if (rfd > 0)
53 		SAFE_CLOSE(rfd);
54 
55 	if (wfd > 0)
56 		SAFE_CLOSE(wfd);
57 }
58 
59 static struct tst_test test = {
60 	.needs_tmpdir = 1,
61 	.setup = setup,
62 	.cleanup = cleanup,
63 	.test_all = verify_write,
64 };
65