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