1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright (c) International Business Machines  Corp., 2001
4f08c3bdfSopenharmony_ci * Copyright (c) Red Hat Inc., 2007
5f08c3bdfSopenharmony_ci * 12/2007 Copyed from sendfile03.c by Masatake YAMATO
6f08c3bdfSopenharmony_ci */
7f08c3bdfSopenharmony_ci
8f08c3bdfSopenharmony_ci/*\
9f08c3bdfSopenharmony_ci * [Description]
10f08c3bdfSopenharmony_ci *
11f08c3bdfSopenharmony_ci * Testcase to test that sendfile(2) system call returns EAGAIN
12f08c3bdfSopenharmony_ci * when passing full out_fd opened with O_NONBLOCK.
13f08c3bdfSopenharmony_ci */
14f08c3bdfSopenharmony_ci
15f08c3bdfSopenharmony_ci#include <sys/sendfile.h>
16f08c3bdfSopenharmony_ci#include "tst_test.h"
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#define MAX_FILL_DATA_LENGTH 0xFFFFFFF
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_cistatic int p[2];
21f08c3bdfSopenharmony_cistatic int in_fd;
22f08c3bdfSopenharmony_cistatic int out_fd;
23f08c3bdfSopenharmony_ci
24f08c3bdfSopenharmony_cistatic void setup(void)
25f08c3bdfSopenharmony_ci{
26f08c3bdfSopenharmony_ci	int i;
27f08c3bdfSopenharmony_ci
28f08c3bdfSopenharmony_ci	tst_fill_file("in_file", 'a', 10, 1);
29f08c3bdfSopenharmony_ci	in_fd = SAFE_OPEN("in_file", O_RDONLY);
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	SAFE_SOCKETPAIR(PF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, p);
32f08c3bdfSopenharmony_ci	out_fd = p[1];
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci	for (i = 0; i < MAX_FILL_DATA_LENGTH; ++i) {
35f08c3bdfSopenharmony_ci		TEST(write(out_fd, "a", 1));
36f08c3bdfSopenharmony_ci		if (TST_RET < 0) {
37f08c3bdfSopenharmony_ci			if (TST_ERR == EAGAIN)
38f08c3bdfSopenharmony_ci				return;
39f08c3bdfSopenharmony_ci			else
40f08c3bdfSopenharmony_ci				tst_brk(TBROK | TTERRNO, "write(out_fd, buf, 1)");
41f08c3bdfSopenharmony_ci		}
42f08c3bdfSopenharmony_ci	}
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	tst_brk(TBROK, "Failed to get EAGAIN after %i bytes",
45f08c3bdfSopenharmony_ci	        MAX_FILL_DATA_LENGTH);
46f08c3bdfSopenharmony_ci}
47f08c3bdfSopenharmony_ci
48f08c3bdfSopenharmony_cistatic void cleanup(void)
49f08c3bdfSopenharmony_ci{
50f08c3bdfSopenharmony_ci	if (p[0])
51f08c3bdfSopenharmony_ci		SAFE_CLOSE(p[0]);
52f08c3bdfSopenharmony_ci	if (p[1])
53f08c3bdfSopenharmony_ci		SAFE_CLOSE(p[1]);
54f08c3bdfSopenharmony_ci	SAFE_CLOSE(in_fd);
55f08c3bdfSopenharmony_ci}
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_cistatic void run(void)
58f08c3bdfSopenharmony_ci{
59f08c3bdfSopenharmony_ci	TST_EXP_FAIL(sendfile(out_fd, in_fd, NULL, 1), EAGAIN,
60f08c3bdfSopenharmony_ci		     "sendfile(out_fd, in_fd, NULL, 1) with blocked out_fd");
61f08c3bdfSopenharmony_ci}
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_cistatic struct tst_test test = {
64f08c3bdfSopenharmony_ci	.needs_tmpdir = 1,
65f08c3bdfSopenharmony_ci	.cleanup = cleanup,
66f08c3bdfSopenharmony_ci	.setup = setup,
67f08c3bdfSopenharmony_ci	.test_all = run,
68f08c3bdfSopenharmony_ci};
69