• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <sys/sendfile.h>
16 #include "tst_test.h"
17 
18 #define MAX_FILL_DATA_LENGTH 0xFFFFFFF
19 
20 static int p[2];
21 static int in_fd;
22 static int out_fd;
23 
setup(void)24 static void setup(void)
25 {
26 	int i;
27 
28 	tst_fill_file("in_file", 'a', 10, 1);
29 	in_fd = SAFE_OPEN("in_file", O_RDONLY);
30 
31 	SAFE_SOCKETPAIR(PF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0, p);
32 	out_fd = p[1];
33 
34 	for (i = 0; i < MAX_FILL_DATA_LENGTH; ++i) {
35 		TEST(write(out_fd, "a", 1));
36 		if (TST_RET < 0) {
37 			if (TST_ERR == EAGAIN)
38 				return;
39 			else
40 				tst_brk(TBROK | TTERRNO, "write(out_fd, buf, 1)");
41 		}
42 	}
43 
44 	tst_brk(TBROK, "Failed to get EAGAIN after %i bytes",
45 	        MAX_FILL_DATA_LENGTH);
46 }
47 
cleanup(void)48 static void cleanup(void)
49 {
50 	if (p[0])
51 		SAFE_CLOSE(p[0]);
52 	if (p[1])
53 		SAFE_CLOSE(p[1]);
54 	SAFE_CLOSE(in_fd);
55 }
56 
run(void)57 static void run(void)
58 {
59 	TST_EXP_FAIL(sendfile(out_fd, in_fd, NULL, 1), EAGAIN,
60 		     "sendfile(out_fd, in_fd, NULL, 1) with blocked out_fd");
61 }
62 
63 static struct tst_test test = {
64 	.needs_tmpdir = 1,
65 	.cleanup = cleanup,
66 	.setup = setup,
67 	.test_all = run,
68 };
69