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