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