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