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