xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/read/read03.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) International Business Machines  Corp., 2001
4*49cdfc7eSAndroid Build Coastguard Worker  */
5*49cdfc7eSAndroid Build Coastguard Worker 
6*49cdfc7eSAndroid Build Coastguard Worker /*\
7*49cdfc7eSAndroid Build Coastguard Worker  * [Description]
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * Testcase to check if read() successfully sets errno to EAGAIN when read from
10*49cdfc7eSAndroid Build Coastguard Worker  * a pipe (fifo, opened in O_NONBLOCK mode) without writing to it.
11*49cdfc7eSAndroid Build Coastguard Worker  */
12*49cdfc7eSAndroid Build Coastguard Worker 
13*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
14*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
15*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
16*49cdfc7eSAndroid Build Coastguard Worker 
17*49cdfc7eSAndroid Build Coastguard Worker static char fifo[100];
18*49cdfc7eSAndroid Build Coastguard Worker static int rfd, wfd;
19*49cdfc7eSAndroid Build Coastguard Worker 
verify_read(void)20*49cdfc7eSAndroid Build Coastguard Worker static void verify_read(void)
21*49cdfc7eSAndroid Build Coastguard Worker {
22*49cdfc7eSAndroid Build Coastguard Worker 	int c;
23*49cdfc7eSAndroid Build Coastguard Worker 
24*49cdfc7eSAndroid Build Coastguard Worker 	TST_EXP_FAIL(read(rfd, &c, 1), EAGAIN,
25*49cdfc7eSAndroid Build Coastguard Worker 		     "read() when nothing is written to a pipe");
26*49cdfc7eSAndroid Build Coastguard Worker }
27*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)28*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
29*49cdfc7eSAndroid Build Coastguard Worker {
30*49cdfc7eSAndroid Build Coastguard Worker 	struct stat buf;
31*49cdfc7eSAndroid Build Coastguard Worker 
32*49cdfc7eSAndroid Build Coastguard Worker 	sprintf(fifo, "fifo.%d", getpid());
33*49cdfc7eSAndroid Build Coastguard Worker 
34*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_MKNOD(fifo, S_IFIFO | 0777, 0);
35*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_STAT(fifo, &buf);
36*49cdfc7eSAndroid Build Coastguard Worker 
37*49cdfc7eSAndroid Build Coastguard Worker 	if ((buf.st_mode & S_IFIFO) == 0)
38*49cdfc7eSAndroid Build Coastguard Worker 		tst_brk(TBROK, "Mode does not indicate fifo file");
39*49cdfc7eSAndroid Build Coastguard Worker 
40*49cdfc7eSAndroid Build Coastguard Worker 	rfd = SAFE_OPEN(fifo, O_RDONLY | O_NONBLOCK);
41*49cdfc7eSAndroid Build Coastguard Worker 	wfd = SAFE_OPEN(fifo, O_WRONLY | O_NONBLOCK);
42*49cdfc7eSAndroid Build Coastguard Worker }
43*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)44*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
45*49cdfc7eSAndroid Build Coastguard Worker {
46*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(rfd);
47*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_CLOSE(wfd);
48*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_UNLINK(fifo);
49*49cdfc7eSAndroid Build Coastguard Worker }
50*49cdfc7eSAndroid Build Coastguard Worker 
51*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
52*49cdfc7eSAndroid Build Coastguard Worker 	.needs_tmpdir = 1,
53*49cdfc7eSAndroid Build Coastguard Worker 	.setup = setup,
54*49cdfc7eSAndroid Build Coastguard Worker 	.cleanup = cleanup,
55*49cdfc7eSAndroid Build Coastguard Worker 	.test_all = verify_read,
56*49cdfc7eSAndroid Build Coastguard Worker };
57